ffmpeg_next_crossfix/software/scaling/
vector.rs1use std::marker::PhantomData;
2use std::slice;
3
4use ffi::*;
5use libc::{c_double, c_int};
6
7pub struct Vector<'a> {
8 ptr: *mut SwsVector,
9
10 _own: bool,
11 _marker: PhantomData<&'a ()>,
12}
13
14impl<'a> Vector<'a> {
15 pub unsafe fn wrap(ptr: *mut SwsVector) -> Self {
16 Vector {
17 ptr,
18 _own: false,
19 _marker: PhantomData,
20 }
21 }
22
23 pub unsafe fn as_ptr(&self) -> *const SwsVector {
24 self.ptr as *const _
25 }
26
27 pub unsafe fn as_mut_ptr(&mut self) -> *mut SwsVector {
28 self.ptr
29 }
30}
31
32impl<'a> Vector<'a> {
33 pub fn new(length: usize) -> Self {
34 unsafe {
35 Vector {
36 ptr: sws_allocVec(length as c_int),
37 _own: true,
38 _marker: PhantomData,
39 }
40 }
41 }
42
43 pub fn gaussian(variance: f64, quality: f64) -> Self {
44 unsafe {
45 Vector {
46 ptr: sws_getGaussianVec(variance as c_double, quality as c_double),
47 _own: true,
48 _marker: PhantomData,
49 }
50 }
51 }
52
53 pub fn value(value: f64, length: usize) -> Self {
54 unsafe {
55 Vector {
56 ptr: sws_getConstVec(value as c_double, length as c_int),
57 _own: true,
58 _marker: PhantomData,
59 }
60 }
61 }
62
63 pub fn identity() -> Self {
64 unsafe {
65 Vector {
66 ptr: sws_getIdentityVec(),
67 _own: true,
68 _marker: PhantomData,
69 }
70 }
71 }
72
73 pub fn scale(&mut self, scalar: f64) {
74 unsafe {
75 sws_scaleVec(self.as_mut_ptr(), scalar as c_double);
76 }
77 }
78
79 pub fn normalize(&mut self, height: f64) {
80 unsafe {
81 sws_normalizeVec(self.as_mut_ptr(), height as c_double);
82 }
83 }
84
85 pub fn conv(&mut self, other: &Vector) {
86 unsafe {
87 sws_convVec(self.as_mut_ptr(), other.as_ptr() as *mut _);
88 }
89 }
90
91 pub fn add(&mut self, other: &Vector) {
92 unsafe {
93 sws_addVec(self.as_mut_ptr(), other.as_ptr() as *mut _);
94 }
95 }
96
97 pub fn sub(&mut self, other: &Vector) {
98 unsafe {
99 sws_subVec(self.as_mut_ptr(), other.as_ptr() as *mut _);
100 }
101 }
102
103 pub fn shift(&mut self, value: usize) {
104 unsafe {
105 sws_shiftVec(self.as_mut_ptr(), value as c_int);
106 }
107 }
108
109 pub fn coefficients(&self) -> &[f64] {
110 unsafe { slice::from_raw_parts((*self.as_ptr()).coeff, (*self.as_ptr()).length as usize) }
111 }
112
113 pub fn coefficients_mut(&self) -> &[f64] {
114 unsafe {
115 slice::from_raw_parts_mut((*self.as_ptr()).coeff, (*self.as_ptr()).length as usize)
116 }
117 }
118}
119
120impl<'a> Clone for Vector<'a> {
121 fn clone(&self) -> Self {
122 unsafe {
123 Vector {
124 ptr: sws_cloneVec(self.as_ptr() as *mut _),
125 _own: true,
126 _marker: PhantomData,
127 }
128 }
129 }
130}
131
132impl<'a> Drop for Vector<'a> {
133 fn drop(&mut self) {
134 unsafe {
135 if self._own {
136 sws_freeVec(self.as_mut_ptr());
137 }
138 }
139 }
140}