libvips_rs/
manual.rs

1// (c) Copyright 2019-2023 MIT
2// this is manually created because it doesn't follow the standard from the introspection output
3
4/// VipsLinear (linear), calculate (a * in + b)
5/// inp: `&VipsImage` -> Input image
6/// a: `&[f64]` -> Multiply by this. Must have equal len as b
7/// b: `&[f64]` -> Add this. Must have equal len as a
8/// returns `VipsImage` - Output image
9pub fn linear(inp: &VipsImage, a: &mut[f64], b: &mut[f64]) -> Result<VipsImage> {
10    unsafe {
11        if a.len() != b.len() {
12            return Err(Error::LinearError)
13        }
14        let inp_in: *mut bindings::VipsImage = inp.ctx;
15        let a_in: *mut f64 = a.as_mut_ptr();
16        let b_in: *mut f64 = b.as_mut_ptr();
17        let mut out_out: *mut bindings::VipsImage = null_mut();
18
19        let vips_op_response =
20            bindings::vips_linear(inp_in, &mut out_out, a_in, b_in, b.len() as i32, NULL);
21        utils::result(
22            vips_op_response,
23            VipsImage { ctx: out_out },
24            Error::LinearError,
25        )
26    }
27}
28
29/// Options for linear operation
30pub struct LinearOptions {
31    /// uchar: `bool` -> Output should be uchar
32    /// default: false
33    pub uchar: bool,
34}
35
36impl std::default::Default for LinearOptions {
37    fn default() -> Self {
38        LinearOptions { uchar: false }
39    }
40}
41
42/// VipsLinear (linear), calculate (a * in + b)
43/// inp: `&VipsImage` -> Input image
44/// a: `&[f64]` -> Multiply by this. Must have equal len as b
45/// b: `&[f64]` -> Add this. Must have equal len as a
46/// linear_options: `&LinearOptions` -> optional arguments
47/// returns `VipsImage` - Output image
48pub fn linear_with_opts(
49    inp: &VipsImage,
50    a: &mut [f64],
51    b: &mut [f64],
52    linear_options: &LinearOptions,
53) -> Result<VipsImage> {
54    unsafe {
55        if a.len() != b.len() {
56            return Err(Error::LinearError)
57        }
58        let inp_in: *mut bindings::VipsImage = inp.ctx;
59        let a_in: *mut f64 = a.as_mut_ptr();
60        let b_in: *mut f64 = b.as_mut_ptr();
61        let mut out_out: *mut bindings::VipsImage = null_mut();
62
63        let uchar_in: i32 = if linear_options.uchar { 1 } else { 0 };
64        let uchar_in_name = utils::new_c_string("uchar")?;
65
66        let vips_op_response = bindings::vips_linear(
67            inp_in,
68            &mut out_out,
69            a_in,
70            b_in,
71            b.len() as i32,
72            uchar_in_name.as_ptr(),
73            uchar_in,
74            NULL,
75        );
76        utils::result(
77            vips_op_response,
78            VipsImage { ctx: out_out },
79            Error::LinearError,
80        )
81    }
82}
83
84/// VipsGetpoint (getpoint), read a point from an image
85/// inp: `&VipsImage` -> Input image
86/// x: `i32` -> Point to read
87/// min: 0, max: 10000000, default: 0
88/// y: `i32` -> Point to read
89/// min: 0, max: 10000000, default: 0
90/// returns `Vec<f64>` - Array of output values
91pub fn getpoint(inp: &VipsImage, x: i32, y: i32) -> Result<Vec<f64>> {
92    unsafe {
93        let inp_in: *mut bindings::VipsImage = inp.ctx;
94        let mut out_array_size: i32 = 0;
95        let mut out_array: *mut f64 = null_mut();
96
97        let vips_op_response = bindings::vips_getpoint(
98            inp_in,
99            &mut out_array,
100            &mut out_array_size,
101            x,
102            y,
103            NULL,
104        );
105        utils::result(
106            vips_op_response,
107            utils::new_double_array(out_array, out_array_size.try_into().unwrap()),
108            Error::GetpointError,
109        )
110    }
111}
112
113/// VipsCase (case), use pixel values to pick cases from an array of images
114/// index: `&VipsImage` -> Index image
115/// cases: `&mut [VipsImage]` -> Array of case images
116/// n: `i32` -> number of case images
117/// returns `VipsImage` - Output image
118pub fn case(index: &VipsImage, cases: &mut [VipsImage], n: i32) -> Result<VipsImage> {
119    unsafe {
120        let index_in: *mut bindings::VipsImage = index.ctx;
121        let cases_in: *mut *mut bindings::VipsImage =
122            cases.iter().map(|v| v.ctx).collect::<Vec<_>>().as_mut_ptr();
123        let mut out_out: *mut bindings::VipsImage = null_mut();
124
125        let vips_op_response = bindings::vips_case(index_in, cases_in, &mut out_out, n, NULL);
126        utils::result(
127            vips_op_response,
128            VipsImage { ctx: out_out },
129            Error::CaseError,
130        )
131    }
132}