rs_vips/
region.rs

1// (c) Copyright 2019-2025 OLX
2// (c) Copyright 2025 mrdkprj
3use crate::bindings::{self, _VipsBlob, 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 From<*mut bindings::VipsBlob> for VipsBlob {
24    fn from(value: *mut bindings::VipsBlob) -> Self {
25        Self {
26            ctx: value,
27        }
28    }
29}
30
31#[allow(clippy::from_over_into)]
32impl Into<Vec<u8>> for VipsBlob {
33    fn into(self) -> Vec<u8> {
34        unsafe {
35            if self
36                .ctx
37                .is_null()
38            {
39                return Vec::new();
40            }
41
42            let mut size: u64 = 0;
43            let bytes = bindings::vips_blob_get(
44                self.ctx,
45                &mut size,
46            );
47            // unref area
48            unref_area(self.ctx);
49            Vec::from_raw_parts(
50                bytes as *mut u8,
51                size as usize,
52                size as usize,
53            )
54        }
55    }
56}
57
58pub(crate) fn unref_area(blob: *mut _VipsBlob) {
59    unsafe {
60        (*blob)
61            .area
62            .free_fn = None;
63        vips_area_unref(&mut (*blob).area);
64    }
65}