rs_vips/
interpolate.rs

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