rs_vips/
region.rs

1// (c) Copyright 2019-2025 OLX
2// (c) Copyright 2025 mrdkprj
3use crate::bindings::{self, vips_area_unref};
4
5#[derive(Debug, Clone)]
6pub struct VipsBlob {
7    pub(crate) ctx: *mut bindings::VipsBlob,
8}
9
10impl Drop for VipsBlob {
11    fn drop(&mut self) {
12        unsafe {
13            if !self
14                .ctx
15                .is_null()
16            {
17                bindings::g_object_unref(self.ctx as _);
18            }
19        }
20    }
21}
22
23impl VipsBlob {
24    pub(crate) fn area_unref(mut self) {
25        unsafe {
26            (*self.ctx)
27                .area
28                .free_fn = None;
29            vips_area_unref(&mut (*self.ctx).area);
30            self.ctx = std::ptr::null_mut();
31        }
32    }
33}
34
35impl From<*mut bindings::VipsBlob> for VipsBlob {
36    fn from(value: *mut bindings::VipsBlob) -> Self {
37        Self {
38            ctx: value,
39        }
40    }
41}
42
43#[allow(clippy::from_over_into)]
44impl Into<Vec<u8>> for VipsBlob {
45    fn into(self) -> Vec<u8> {
46        unsafe {
47            if self
48                .ctx
49                .is_null()
50            {
51                return Vec::new();
52            }
53
54            let mut size: u64 = 0;
55            let bytes = bindings::vips_blob_get(
56                self.ctx,
57                &mut size,
58            );
59            // unref area
60            self.area_unref();
61            Vec::from_raw_parts(
62                bytes as *mut u8,
63                size as usize,
64                size as usize,
65            )
66        }
67    }
68}