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