1use crate::bindings;
3use crate::bindings::{VipsArrayDouble, VipsArrayImage, VipsArrayInt};
4use crate::error::Error;
5use crate::Result;
6use crate::VipsImage;
7use std::ffi::c_void;
8use std::ffi::CString;
9
10pub(crate) struct VipsArrayIntWrapper {
11 pub ctx: *mut VipsArrayInt,
12}
13pub(crate) struct VipsArrayDoubleWrapper {
14 pub ctx: *mut VipsArrayDouble,
15}
16pub(crate) struct VipsArrayImageWrapper {
17 pub ctx: *mut VipsArrayImage,
18}
19
20impl Drop for VipsArrayIntWrapper {
21 fn drop(&mut self) {
22 unsafe {
23 bindings::vips_area_unref(self.ctx as *mut bindings::VipsArea);
24 }
25 }
26}
27
28impl Drop for VipsArrayDoubleWrapper {
29 fn drop(&mut self) {
30 unsafe {
31 bindings::vips_area_unref(self.ctx as *mut bindings::VipsArea);
32 }
33 }
34}
35
36impl Drop for VipsArrayImageWrapper {
37 fn drop(&mut self) {
38 unsafe {
39 bindings::vips_area_unref(self.ctx as *mut bindings::VipsArea);
40 }
41 }
42}
43
44impl From<&[i32]> for VipsArrayIntWrapper {
45 #[inline]
46 fn from(array: &[i32]) -> Self {
47 VipsArrayIntWrapper {
48 ctx: unsafe { bindings::vips_array_int_new(array.as_ptr(), array.len() as i32) },
49 }
50 }
51}
52
53impl From<&[f64]> for VipsArrayDoubleWrapper {
54 #[inline]
55 fn from(array: &[f64]) -> Self {
56 VipsArrayDoubleWrapper {
57 ctx: unsafe { bindings::vips_array_double_new(array.as_ptr(), array.len() as i32) },
58 }
59 }
60}
61
62impl From<&[VipsImage]> for VipsArrayImageWrapper {
63 #[inline]
64 fn from(array: &[VipsImage]) -> Self {
65 let len = array.len() as i32;
66 let as_vips = array.iter().map(|v| v.ctx).collect::<Vec<_>>().as_mut_ptr();
67 VipsArrayImageWrapper {
68 ctx: unsafe { bindings::vips_array_image_new(as_vips, len) },
69 }
70 }
71}
72
73#[inline]
74pub fn result<T>(res: i32, output: T, error: Error) -> Result<T> {
75 if res == 0 {
76 Ok(output)
77 } else {
78 Err(error)
79 }
80}
81
82#[inline]
83pub(crate) fn new_c_string(string: &str) -> Result<CString> {
84 CString::new(string).map_err(|_| Error::InitializationError("Error initializing C string."))
85}
86
87#[inline]
88pub(crate) unsafe fn new_byte_array(buf: *mut c_void, size: u64) -> Vec<u8> {
89 Vec::from_raw_parts(buf as *mut u8, size as usize, size as usize)
90}
91
92#[inline]
93pub unsafe fn new_int_array(array: *mut i32, size: u64) -> Vec<i32> {
94 Vec::from(std::slice::from_raw_parts(array as *mut i32, size as usize))
95}
96
97#[inline]
98pub unsafe fn new_double_array(array: *mut f64, size: u64) -> Vec<f64> {
99 Vec::from(std::slice::from_raw_parts(array as *mut f64, size as usize))
100}