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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332

#[cfg(feature = "docs-rs")]
#[path = "ffi.ref.rs"]
mod ffi;

#[cfg(not(feature = "docs-rs"))]
#[allow(non_upper_case_globals, non_camel_case_types, non_snake_case, unused, improper_ctypes)]
mod ffi;

#[macro_use]
mod utils;

pub mod err {
    #[derive(Debug)]
    pub enum Error {
        Vips(Option<String>),
        NulError(std::ffi::NulError),
        Io(std::io::Error),
        Boxed(Box<dyn std::error::Error + Send + Sync>),
    }

    impl Error {
        pub(crate) fn from_vips() -> Self {
            let out = unsafe {
                let ptr = crate::ffi::vips_error_buffer();
                if ptr.is_null() {
                    None
                } else {
                    Some(std::ffi::CStr::from_ptr(ptr).to_string_lossy().into_owned())
                }
            };
            Error::Vips(out)
        }
    }

    impl std::fmt::Display for Error {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            use Error::*;
            match self {
                Vips(ref e) => {
                    let msg =
                        e.as_ref().map(|x| x.as_str()).unwrap_or_else(|| "Unknown VIPS error");
                    write!(f, "{}", msg)?;
                }
                NulError(ref e) => {
                    write!(f, "{}", e)?;
                }
                Io(ref e) => {
                    write!(f, "{}", e)?;
                }
                Boxed(ref e) => {
                    write!(f, "{}", e)?;
                }
            }

            Ok(())
        }
    }

    impl From<std::ffi::NulError> for Error {
        fn from(t: std::ffi::NulError) -> Self {
            Error::NulError(t)
        }
    }

    impl std::error::Error for Error {}

    pub type Result<T> = std::result::Result<T, Error>;
}

use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int, c_void};
use std::ptr;

pub use ffi::{VipsAngle, VipsBandFormat, VipsKernel};

const NULL_TERM: *const c_char = ptr::null();

#[derive(Default)]
pub struct InitOptions {
    name: Option<String>,
    leak_checks_on: Option<bool>,
}

impl InitOptions {
    pub fn with_name<I: Into<String>>(mut self, name: I) -> Self {
        self.name = Some(name.into());
        self
    }
    pub fn with_leak_checks(mut self, on: bool) -> Self {
        self.leak_checks_on = Some(on);
        self
    }
}

/// Call this if you need to specify a name for your program in VIPS,
/// or if you need to turn VIPS leak checking on. Otherwise, any image
/// open/build operations will initialize VIPS

pub fn initialize_with_options(opts: InitOptions) {
    initialize_with_maybe_options(Some(opts))
}

fn initialize_with_maybe_options(opts: Option<InitOptions>) {
    static INIT: std::sync::Once = std::sync::Once::new();
    INIT.call_once(|| unsafe {
        let opts = opts.unwrap_or_else(InitOptions::default);
        match opts.name.as_ref().and_then(|n| CString::new(n.clone()).ok()) {
            Some(name) => {
                ffi::vips_init(name.as_ptr());
            }
            None => {
                let name = CStr::from_bytes_with_nul_unchecked(b"rips\0");
                ffi::vips_init(name.as_ptr());
            }
        };

        if let Some(&leak_checks_on) = opts.leak_checks_on.as_ref() {
            ffi::vips_leak_set(leak_checks_on as c_int);
        }

        assert_eq!(libc::atexit(cleanup), 0);
    });

    extern "C" fn cleanup() {
        unsafe {
            ffi::vips_shutdown();
        }
    }
}

fn initialize() {
    initialize_with_maybe_options(None)
}

pub struct Image(*mut ffi::VipsImage);

impl Drop for Image {
    fn drop(&mut self) {
        unsafe {
            ffi::g_object_unref(self.0 as *mut c_void);
        }
    }
}

impl Image {
    fn new() -> Self {
        Image(ptr::null_mut())
    }

    pub fn width(&self) -> i32 {
        unsafe { ffi::vips_image_get_width(self.0) }
    }

    pub fn height(&self) -> i32 {
        unsafe { ffi::vips_image_get_height(self.0) }
    }

    pub fn from_file<S: Into<Vec<u8>>>(path: S) -> err::Result<Image> {
        initialize();

        let path = CString::new(path)?;
        let ptr = unsafe { ffi::vips_image_new_from_file(path.as_ptr(), NULL_TERM) };
        if ptr.is_null() {
            Err(err::Error::from_vips())
        } else {
            Ok(Image(ptr))
        }
    }

    pub fn from_memory(
        buf: Vec<u8>,
        width: i32,
        height: i32,
        bands: i32,
        format: VipsBandFormat,
    ) -> err::Result<Image> {
        pub unsafe extern "C" fn post_close(_: *mut ffi::VipsImage, user_data: *mut c_void) {
            let buf = Box::from_raw(user_data as *mut Box<[u8]>);
            drop(buf);
        }

        initialize();

        let buf = buf.into_boxed_slice();

        let ptr = unsafe {
            ffi::vips_image_new_from_memory(
                buf.as_ptr() as *const c_void,
                buf.len(),
                width,
                height,
                bands,
                format,
            )
        };

        let buf = Box::new(buf);

        let raw: *mut c_void = Box::into_raw(buf) as *mut c_void;

        unsafe {
            let callback: unsafe extern "C" fn() = std::mem::transmute(post_close as *const ());
            ffi::g_signal_connect_data(
                ptr as *mut c_void,
                "postclose\0".as_ptr() as *const c_char,
                Some(callback),
                raw,
                None,
                ffi::GConnectFlags::G_CONNECT_AFTER,
            );
        };

        if ptr.is_null() {
            Err(err::Error::from_vips())
        } else {
            Ok(Image(ptr))
        }
    }

    pub fn resize(
        &self,
        scale: f64,
        vscale: Option<f64>,
        kernel: Option<VipsKernel>,
    ) -> err::Result<Image> {
        let mut out = Image::new();

        let ret = unsafe {
            var_args!(ffi::vips_resize, 
                args => [self.0, &mut out.0, scale,], 
                opts => [(vscale, vscale, "vscale\0".as_ptr(), vscale), (kernel, kernel, "kernel\0".as_ptr(), kernel),],
                term => NULL_TERM)
        };

        match ret {
            0 => Ok(out),
            _ => Err(err::Error::from_vips()),
        }
    }

    pub fn resize_to(&self, width: Option<i32>, height: Option<i32>) -> err::Result<Image> {
        match (width, height) {
            (Some(width), Some(height)) => {
                let hscale = f64::from(width) / f64::from(self.width());
                let vscale = f64::from(height) / f64::from(self.height());
                if hscale != vscale {
                    self.resize(hscale, Some(vscale), None)
                } else {
                    self.resize(hscale, None, None)
                }
            }
            (Some(width), None) => {
                self.resize(f64::from(width) / f64::from(self.width()), None, None)
            }
            (None, Some(height)) => {
                self.resize(f64::from(height) / f64::from(self.height()), None, None)
            }
            (None, None) => self.resize(1.0, None, None),
        }
    }

    pub fn crop(&self, left: i32, top: i32, width: i32, height: i32) -> err::Result<Image> {
        let mut out = Image::new();

        let ret =
            unsafe { ffi::vips_crop(self.0, &mut out.0, left, top, width, height, NULL_TERM) };

        match ret {
            0 => Ok(out),
            _ => Err(err::Error::from_vips()),
        }
    }

    pub fn rotate(&self, angle: VipsAngle) -> err::Result<Image> {
        let mut out = Image::new();

        let ret = unsafe { ffi::vips_rot(self.0, &mut out.0, angle, NULL_TERM) };

        match ret {
            0 => Ok(out),
            _ => Err(err::Error::from_vips()),
        }
    }

    pub fn write_to_file<S: Into<Vec<u8>>>(&self, path: S) -> err::Result<()> {
        let path = CString::new(path)?;
        let ret = unsafe { ffi::vips_image_write_to_file(self.0, path.as_ptr(), NULL_TERM) };
        match ret {
            0 => Ok(()),
            _ => Err(err::Error::from_vips()),
        }
    }

    pub fn to_buffer(&self, suffix: &str) -> err::Result<Vec<u8>> {
        let suffix = CString::new(String::from(suffix))?;
        let mut size = 0usize;
        let mut buf = ptr::null_mut::<u8>();

        unsafe {
            let ret = ffi::vips_image_write_to_buffer(
                self.0,
                suffix.as_ptr(),
                &mut buf as *mut *mut u8 as *mut *mut c_void,
                &mut size,
                NULL_TERM,
            );

            let slice = std::slice::from_raw_parts_mut(buf as *mut u8, size);
            let boxed: Box<[u8]> = Box::from_raw(slice);
            let out = boxed.into_vec();

            match ret {
                0 => Ok(out),
                _ => Err(err::Error::from_vips()),
            }
        }
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        let mut size = 0usize;
        unsafe {
            let memory: *mut u8 = ffi::vips_image_write_to_memory(self.0, &mut size) as *mut u8;
            let slice = std::slice::from_raw_parts_mut(memory, size);
            let boxed: Box<[u8]> = Box::from_raw(slice);
            boxed.into_vec()
        }
    }
}

unsafe impl Send for Image {}
unsafe impl Sync for Image {}