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
use crate::bindings; use crate::bindings::{VipsArrayDouble, VipsArrayImage, VipsArrayInt}; use crate::error::Error; use crate::Result; use crate::VipsImage; use std::ffi::c_void; use std::ffi::CString; pub(crate) struct VipsArrayIntWrapper { pub ctx: *mut VipsArrayInt, } pub(crate) struct VipsArrayDoubleWrapper { pub ctx: *mut VipsArrayDouble, } pub(crate) struct VipsArrayImageWrapper { pub ctx: *mut VipsArrayImage, } impl Drop for VipsArrayIntWrapper { fn drop(&mut self) { unsafe { bindings::vips_area_unref(self.ctx as *mut bindings::VipsArea); } } } impl Drop for VipsArrayDoubleWrapper { fn drop(&mut self) { unsafe { bindings::vips_area_unref(self.ctx as *mut bindings::VipsArea); } } } impl Drop for VipsArrayImageWrapper { fn drop(&mut self) { unsafe { bindings::vips_area_unref(self.ctx as *mut bindings::VipsArea); } } } impl From<&[i32]> for VipsArrayIntWrapper { #[inline] fn from(array: &[i32]) -> Self { VipsArrayIntWrapper { ctx: unsafe { bindings::vips_array_int_new(array.as_ptr(), array.len() as i32) }, } } } impl From<&[f64]> for VipsArrayDoubleWrapper { #[inline] fn from(array: &[f64]) -> Self { VipsArrayDoubleWrapper { ctx: unsafe { bindings::vips_array_double_new(array.as_ptr(), array.len() as i32) }, } } } impl From<&[VipsImage]> for VipsArrayImageWrapper { #[inline] fn from(array: &[VipsImage]) -> Self { let len = array.len() as i32; let as_vips = array.iter().map(|v| v.ctx).collect::<Vec<_>>().as_mut_ptr(); VipsArrayImageWrapper { ctx: unsafe { bindings::vips_array_image_new(as_vips, len) }, } } } #[inline] pub fn result<T>(res: i32, output: T, error: Error) -> Result<T> { if res == 0 { Ok(output) } else { Err(error) } } #[inline] pub(crate) fn new_c_string(string: &str) -> Result<CString> { CString::new(string).map_err(|_| Error::InitializationError("Error initializing C string.")) } #[inline] pub(crate) unsafe fn new_byte_array(buf: *mut c_void, size: u64) -> Vec<u8> { Vec::from_raw_parts(buf as *mut u8, size as usize, size as usize) } #[inline] pub unsafe fn new_int_array(array: *mut i32, size: u64) -> Vec<i32> { Vec::from(std::slice::from_raw_parts(array as *mut i32, size as usize)) } #[inline] pub unsafe fn new_double_array(array: *mut f64, size: u64) -> Vec<f64> { Vec::from(std::slice::from_raw_parts(array as *mut f64, size as usize)) }