1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use crate::Buffer;
use crate::PixelFormat;

use glib::translate::ToGlibPtr;
use glib_sys::GDestroyNotify;

use std::ffi::c_void;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ImageError {
	InvalidStatus(crate::BufferStatus),
	InvalidPayloadType(crate::BufferPayloadType),
	UnsupportedPixelFormat(PixelFormat),
}

impl Buffer {
	/// Create an Aravis buffer that owns its own data from a pre-allocated raw buffer.
	///
	/// The `destroy_callback` argument is called to destroy the buffer, and should free the resources associated with the buffer.
	///
	/// # Safety
	/// The pointer and length parameter must indicate a valid memory region where Aravis can safely write data to until the `destroy_callback` is called.
	pub fn new_preallocated_owned<F: FnOnce()>(
		data: *mut u8,
		len: usize,
		destroy_callback: F,
	) -> Self {
		extern "C" fn run_callback<F: FnOnce()>(user_data: *mut c_void) {
			unsafe {
				let function = Box::from_raw(user_data as *mut F);
				function()
			}
		}

		let user_data = Box::leak(Box::new(destroy_callback)) as *mut F as *mut c_void;
		unsafe { Self::preallocated(data as *mut c_void, len, user_data, Some(run_callback::<F>)) }
	}

	/// Create an Aravis buffer from a pre-allocated raw buffer.
	///
	/// The created buffer has no registered user data or destroy callback,
	/// so management of the underlying buffer has to be done externally.
	/// The buffer can be identified later when it is returned by a stream only byt the data pointer.
	///
	/// # Safety
	/// The resulting buffer borrows the data, but it carries no lifetime.
	/// The user has to ensure the buffer stays valid.
	#[deprecated(note = "Use new_preallocated_borrowed instead")]
	pub unsafe fn new_preallocated(data: *mut u8, len: usize) -> Self {
		Self::preallocated(data as *mut c_void, len, std::ptr::null_mut(), None)
	}

	/// Create an Aravis buffer from a pre-allocated raw buffer.
	///
	/// The created buffer has no registered user data or destroy callback,
	/// so management of the underlying buffer has to be done externally.
	/// The buffer can be identified later when it is returned by a stream only by the data pointer.
	///
	/// # Safety
	/// The pointer and length parameter must indicate a valid memory region where Aravis can safely write data to until the buffer is dropped.
	/// The created buffer borrows the memory buffer, but it carries no lifetime.
	/// See [`Self::new_preallocated_owned`] for a safer (but still unsafe) alternative.
	pub unsafe fn new_preallocated_borrowed(data: *mut u8, len: usize) -> Self {
		Self::preallocated(data as *mut c_void, len, std::ptr::null_mut(), None)
	}

	unsafe fn preallocated(
		data: *mut c_void,
		len: usize,
		user_data: *mut c_void,
		destory_callback: GDestroyNotify,
	) -> Self {
		let buffer =
			aravis_sys::arv_buffer_new_full(len, data as *mut c_void, user_data, destory_callback);
		glib::translate::from_glib_full(buffer)
	}

	/// Create a new buffer backed by a leaked `Box<[u8]>`.
	///
	/// The buffer can later be turned into an image using `[Self::into_image]`.
	/// If the buffer is dropped without taking ownership of the data again, the memory is leaked.
	pub fn new_leaked_box(len: usize) -> Self {
		#[cfg(feature = "nightly")]
		{
			let mut buffer = Box::<[u8]>::new_uninit_slice(len);
			let data = std::mem::MaybeUninit::slice_as_mut_ptr(&mut buffer);
			let result = unsafe { Buffer::new_preallocated_borrowed(data, len) };
			std::mem::forget(buffer);
			result
		}
		#[cfg(not(feature = "nightly"))]
		{
			let mut buffer = vec![0u8; len].into_boxed_slice();
			let result = unsafe { Buffer::new_preallocated_borrowed(buffer.as_mut_ptr(), len) };
			std::mem::forget(buffer);
			result
		}
	}

	/// Create a new buffer for an image of the specified format and dimensions, backed by a leaked `Box<[u8]>`.
	///
	/// The buffer can later be turned into an image using `[Self::into_image]`.
	/// If the buffer is dropped without taking ownership of the data again, the memory is leaked.
	pub fn new_leaked_image(format: crate::PixelFormat, width: usize, height: usize) -> Self {
		let byte_len = crate::buffer_size_wh(format, width, height);
		Self::new_leaked_box(byte_len)
	}

	/// Get a pointer to the raw data and the length of the buffer.
	pub fn data(&self) -> (*mut u8, usize) {
		unsafe {
			let mut size = 0usize;
			let data =
				aravis_sys::arv_buffer_get_data(self.to_glib_none().0, &mut size as *mut usize);
			(data as *mut u8, size)
		}
	}

	/// Convert the buffer into an image.
	///
	/// # Safety
	/// This function assumes the buffer is backed by a leaked box,
	/// such as created by [`Buffer::new_leaked_box`].
	///
	/// This function takes ownership of the leaked box,
	/// so the memory will be freed when the resulting image is dropped.
	pub unsafe fn into_image(self) -> Result<image::DynamicImage, ImageError> {
		use image::DynamicImage;
		use image::ImageBuffer;

		let (data, len) = self.data();
		let data = Vec::from(box_slice_from_raw(data, len));

		let status = self.status();
		if status != crate::BufferStatus::Success {
			return Err(ImageError::InvalidStatus(status));
		}

		let payload = self.payload_type();
		if payload != crate::BufferPayloadType::Image {
			return Err(ImageError::InvalidPayloadType(payload));
		}

		let width = self.image_width() as u32;
		let height = self.image_height() as u32;
		let format = self.image_pixel_format();

		match format {
			PixelFormat::RGB_8_PACKED => {
				return Ok(DynamicImage::ImageRgb8(
					ImageBuffer::from_raw(width, height, data).unwrap(),
				))
			}
			PixelFormat::MONO_8 => {
				return Ok(DynamicImage::ImageLuma8(
					ImageBuffer::from_raw(width, height, data).unwrap(),
				))
			}
			_ => (),
		};

		#[cfg(feature = "bayer")]
		{
			if let Some(filter) = debayer::filter(format) {
				return debayer::debayer(width, height, filter, &data);
			}
		}

		Err(ImageError::UnsupportedPixelFormat(format))
	}
}

#[cfg(feature = "bayer")]
mod debayer {
	use crate::ImageError;
	use crate::PixelFormat;
	use image::DynamicImage;
	use image::ImageBuffer;

	pub fn filter(format: PixelFormat) -> Option<bayer::CFA> {
		match format {
			PixelFormat::BAYER_BG_8 => Some(bayer::CFA::BGGR),
			PixelFormat::BAYER_GB_8 => Some(bayer::CFA::GBRG),
			PixelFormat::BAYER_GR_8 => Some(bayer::CFA::GRBG),
			PixelFormat::BAYER_RG_8 => Some(bayer::CFA::RGGB),
			_ => None,
		}
	}

	pub fn debayer(
		width: u32,
		height: u32,
		filter: bayer::CFA,
		mut data: &[u8],
	) -> Result<DynamicImage, ImageError> {
		let mut buffer = vec![0u8; width as usize * height as usize * 3];
		let mut dest = bayer::RasterMut::new(
			width as usize,
			height as usize,
			bayer::RasterDepth::Depth8,
			&mut buffer,
		);
		bayer::run_demosaic(
			&mut data,
			bayer::BayerDepth::Depth8,
			filter,
			bayer::Demosaic::Linear,
			&mut dest,
		)
		.unwrap();
		Ok(DynamicImage::ImageRgb8(
			ImageBuffer::from_raw(width, height, buffer).unwrap(),
		))
	}
}

unsafe fn box_slice_from_raw<T>(data: *mut T, len: usize) -> Box<[T]> {
	Box::from_raw(std::slice::from_raw_parts_mut(data, len))
}

impl std::fmt::Display for ImageError {
	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
		match self {
			Self::InvalidStatus(x) => write!(f, "invalid buffer status: {}", x),
			Self::InvalidPayloadType(x) => write!(f, "invalid buffer payload type: {}", x),
			Self::UnsupportedPixelFormat(x) => write!(f, "unsupported pixel format: {}", x.raw()),
		}
	}
}

impl std::error::Error for ImageError {}