rs_vips/
interpolate.rs

1use crate::{bindings, error::Error, utils, Result};
2use std::ffi::*;
3
4#[derive(Debug, Clone)]
5pub struct VipsInterpolate {
6    pub(crate) ctx: *mut bindings::VipsInterpolate,
7}
8
9impl Default for VipsInterpolate {
10    fn default() -> VipsInterpolate {
11        unsafe {
12            VipsInterpolate {
13                ctx: bindings::vips_interpolate_nearest_static(),
14            }
15        }
16    }
17}
18
19impl VipsInterpolate {
20    /// defaults to vips_interpolate_nearest_static
21    pub fn new() -> VipsInterpolate {
22        unsafe {
23            VipsInterpolate {
24                ctx: bindings::vips_interpolate_nearest_static(),
25            }
26        }
27    }
28
29    /// A convenience function that returns a nearest-neighbour interpolator you don’t need to free.
30    pub fn new_from_neasest_static() -> VipsInterpolate {
31        unsafe {
32            VipsInterpolate {
33                ctx: bindings::vips_interpolate_nearest_static(),
34            }
35        }
36    }
37
38    /// A convenience function that returns a bilinear interpolator you don’t need to free.
39    pub fn new_from_bilinear_static() -> VipsInterpolate {
40        unsafe {
41            VipsInterpolate {
42                ctx: bindings::vips_interpolate_bilinear_static(),
43            }
44        }
45    }
46
47    /// Look up an interpolator from a nickname and make one.
48    pub fn new_from_name(name: &str) -> Result<VipsInterpolate> {
49        unsafe {
50            let nickname = utils::new_c_string(name)?;
51            let res = bindings::vips_interpolate_new(nickname.as_ptr());
52            if res.is_null() {
53                Err(
54                    Error::InitializationError(
55                        "Cannot initialize interpolator with provided nickname".to_string(),
56                    ),
57                )
58            } else {
59                Ok(
60                    VipsInterpolate {
61                        ctx: res,
62                    },
63                )
64            }
65        }
66    }
67
68    /// Look up an interpolators desired window size.
69    pub fn get_window_size(&self) -> i32 {
70        unsafe { bindings::vips_interpolate_get_window_size(self.ctx) }
71    }
72
73    /// Look up an interpolators desired window offset.
74    pub fn get_windows_offset(&self) -> i32 {
75        unsafe { bindings::vips_interpolate_get_window_offset(self.ctx) }
76    }
77}
78
79impl Drop for VipsInterpolate {
80    fn drop(&mut self) {
81        unsafe {
82            if !self
83                .ctx
84                .is_null()
85            {
86                bindings::g_object_unref(self.ctx as *mut c_void);
87            }
88        }
89    }
90}
91
92impl From<*mut bindings::VipsInterpolate> for VipsInterpolate {
93    fn from(value: *mut bindings::VipsInterpolate) -> Self {
94        Self {
95            ctx: value,
96        }
97    }
98}