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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//! Bayer / Bayer16 RAW `MixedSinker` impls.
use super::{
GeometryOverflow, InsufficientBuffer, LumaCoefficients, MixedSinker, MixedSinkerError,
RowIndexOutOfRange, RowShapeMismatch, RowSlice, check_dimensions_match, rgb_row_buf_or_scratch,
rgb_row_to_luma_row,
};
use crate::{PixelSink, raw::*, row::*};
// ---- Bayer (8-bit) impl --------------------------------------------------
impl MixedSinker<'_, Bayer> {
/// Sets the luma coefficient set used to derive the luma plane
/// from demosaiced RGB. Only matters when `with_luma` is also
/// attached. Default: [`LumaCoefficients::Bt709`].
///
/// Pick the set that matches the gamut your
/// [`crate::raw::ColorCorrectionMatrix`] targets — see
/// [`LumaCoefficients`] for guidance. Choosing the wrong set
/// still produces a valid `u8` luma plane, but its numeric
/// values won't match what a downstream luma-driven analysis
/// (scene-cut detection, brightness thresholding, perceptual
/// diff) expects for non-grayscale content.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_luma_coefficients(mut self, coeffs: LumaCoefficients) -> Self {
self.set_luma_coefficients(coeffs);
self
}
/// In-place variant of
/// [`with_luma_coefficients`](Self::with_luma_coefficients).
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_luma_coefficients(&mut self, coeffs: LumaCoefficients) -> &mut Self {
self.luma_coefficients_q8 = coeffs.to_q8();
self
}
}
impl BayerSink for MixedSinker<'_, Bayer> {}
impl PixelSink for MixedSinker<'_, Bayer> {
type Input<'r> = BayerRow<'r>;
type Error = MixedSinkerError;
fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
// Bayer accepts odd dimensions — see `BayerFrame::try_new` for
// the rationale (cropped Bayer is a real workflow).
check_dimensions_match(self.width, self.height, width, height)
}
fn process(&mut self, row: BayerRow<'_>) -> Result<(), Self::Error> {
let w = self.width;
let h = self.height;
let idx = row.row();
let use_simd = self.simd;
// Defense-in-depth row-shape checks. The walker always hands
// matching slices, but a caller bypassing the walker (or one of
// the future unsafe SIMD backends being wired up) needs the
// no-panic contract: bad lengths surface as `RowShapeMismatch`,
// not as a kernel-level `assert!` panic.
if row.mid().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::BayerMid,
idx,
w,
row.mid().len(),
)));
}
if row.above().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::BayerAbove,
idx,
w,
row.above().len(),
)));
}
if row.below().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::BayerBelow,
idx,
w,
row.below().len(),
)));
}
if idx >= self.height {
return Err(MixedSinkerError::RowIndexOutOfRange(
RowIndexOutOfRange::new(idx, self.height),
));
}
// `Copy`, captured before the `Self { .. }` destructure so the
// luma path doesn't have to re-borrow `self`.
let luma_coeffs_q8 = self.luma_coefficients_q8;
let Self {
rgb,
luma,
hsv,
rgb_scratch,
..
} = self;
let want_rgb = rgb.is_some();
let want_luma = luma.is_some();
let want_hsv = hsv.is_some();
if !want_rgb && !want_luma && !want_hsv {
return Ok(());
}
let one_plane_start = idx * w;
let one_plane_end = one_plane_start + w;
// 8-bit RGB scratch / output buffer. Bayer always derives every
// output channel from the demosaiced RGB, so the RGB row exists
// unconditionally when any of `rgb` / `luma` / `hsv` is set.
let rgb_row = rgb_row_buf_or_scratch(
rgb.as_deref_mut(),
rgb_scratch,
one_plane_start,
one_plane_end,
w,
h,
)?;
bayer_to_rgb_row(
row.above(),
row.mid(),
row.below(),
row.row_parity(),
row.pattern(),
row.demosaic(),
row.m(),
rgb_row,
use_simd,
);
if let Some(luma) = luma.as_deref_mut() {
rgb_row_to_luma_row(
rgb_row,
&mut luma[one_plane_start..one_plane_end],
luma_coeffs_q8,
);
}
if let Some(hsv) = hsv.as_mut() {
let (h, s, v) = hsv.hsv();
rgb_to_hsv_row(
rgb_row,
&mut h[one_plane_start..one_plane_end],
&mut s[one_plane_start..one_plane_end],
&mut v[one_plane_start..one_plane_end],
w,
use_simd,
);
}
Ok(())
}
}
// ---- Bayer16<BITS> impl --------------------------------------------------
impl<'a, const BITS: u32> MixedSinker<'a, Bayer16<BITS>> {
/// Attaches a packed **`u16`** RGB output buffer.
///
/// Length is measured in `u16` **elements** (not bytes): minimum
/// `width x height x 3`. Output is **low-packed** at `BITS`
/// (10-bit white = 1023, 12-bit = 4095, 14-bit = 16383, 16-bit =
/// 65535) — matches the rest of the high-bit-depth crate.
///
/// Returns `Err(InsufficientRgbU16Buffer)` if
/// `buf.len() < width x height x 3`, or `Err(GeometryOverflow)`
/// on 32-bit overflow.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgb_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgb_u16(buf)?;
Ok(self)
}
/// In-place variant of [`with_rgb_u16`](Self::with_rgb_u16). The
/// required length is measured in `u16` **elements**, not bytes.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgb_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(3)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgb_u16 = Some(buf);
Ok(self)
}
/// Sets the luma coefficient set used to derive the (8-bit)
/// luma plane from demosaiced RGB. Only matters when `with_luma`
/// is also attached. Default: [`LumaCoefficients::Bt709`].
///
/// Pick the set that matches the gamut your
/// [`crate::raw::ColorCorrectionMatrix`] targets — see
/// [`LumaCoefficients`] for guidance. Choosing the wrong set
/// still produces a valid `u8` luma plane, but its numeric
/// values won't match what a downstream luma-driven analysis
/// (scene-cut detection, brightness thresholding, perceptual
/// diff) expects for non-grayscale content.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_luma_coefficients(mut self, coeffs: LumaCoefficients) -> Self {
self.set_luma_coefficients(coeffs);
self
}
/// In-place variant of
/// [`with_luma_coefficients`](Self::with_luma_coefficients).
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_luma_coefficients(&mut self, coeffs: LumaCoefficients) -> &mut Self {
self.luma_coefficients_q8 = coeffs.to_q8();
self
}
}
impl<const BITS: u32> BayerSink16<BITS> for MixedSinker<'_, Bayer16<BITS>> {}
impl<const BITS: u32> PixelSink for MixedSinker<'_, Bayer16<BITS>> {
type Input<'r> = BayerRow16<'r, BITS>;
type Error = MixedSinkerError;
fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
// Bayer accepts odd dimensions — see `BayerFrame::try_new` for
// the rationale (cropped Bayer is a real workflow).
check_dimensions_match(self.width, self.height, width, height)
}
fn process(&mut self, row: BayerRow16<'_, BITS>) -> Result<(), Self::Error> {
let w = self.width;
let h = self.height;
let idx = row.row();
let use_simd = self.simd;
// See the 8-bit Bayer impl for the row-shape rationale.
if row.mid().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::Bayer16Mid,
idx,
w,
row.mid().len(),
)));
}
if row.above().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::Bayer16Above,
idx,
w,
row.above().len(),
)));
}
if row.below().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::Bayer16Below,
idx,
w,
row.below().len(),
)));
}
if idx >= self.height {
return Err(MixedSinkerError::RowIndexOutOfRange(
RowIndexOutOfRange::new(idx, self.height),
));
}
// `Copy`, captured before the `Self { .. }` destructure so the
// luma path doesn't have to re-borrow `self`.
let luma_coeffs_q8 = self.luma_coefficients_q8;
let Self {
rgb,
rgb_u16,
luma,
hsv,
rgb_scratch,
..
} = self;
let one_plane_start = idx * w;
let one_plane_end = one_plane_start + w;
// u16 RGB output runs the native-depth kernel directly. Output
// is low-packed at `BITS` per the `*_to_rgb_u16_row` convention.
if let Some(buf) = rgb_u16.as_deref_mut() {
let rgb_plane_end =
one_plane_end
.checked_mul(3)
.ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
w, h, 3,
)))?;
let rgb_plane_start = one_plane_start * 3;
bayer16_to_rgb_u16_row::<BITS>(
row.above(),
row.mid(),
row.below(),
row.row_parity(),
row.pattern(),
row.demosaic(),
row.m(),
&mut buf[rgb_plane_start..rgb_plane_end],
use_simd,
);
}
let want_rgb = rgb.is_some();
let want_luma = luma.is_some();
let want_hsv = hsv.is_some();
if !want_rgb && !want_luma && !want_hsv {
return Ok(());
}
// 8-bit RGB scratch / output. Same lazy-grow pattern as the
// 8-bit Bayer impl above.
let rgb_row = rgb_row_buf_or_scratch(
rgb.as_deref_mut(),
rgb_scratch,
one_plane_start,
one_plane_end,
w,
h,
)?;
bayer16_to_rgb_row::<BITS>(
row.above(),
row.mid(),
row.below(),
row.row_parity(),
row.pattern(),
row.demosaic(),
row.m(),
rgb_row,
use_simd,
);
if let Some(luma) = luma.as_deref_mut() {
rgb_row_to_luma_row(
rgb_row,
&mut luma[one_plane_start..one_plane_end],
luma_coeffs_q8,
);
}
if let Some(hsv) = hsv.as_mut() {
let (h, s, v) = hsv.hsv();
rgb_to_hsv_row(
rgb_row,
&mut h[one_plane_start..one_plane_end],
&mut s[one_plane_start..one_plane_end],
&mut v[one_plane_start..one_plane_end],
w,
use_simd,
);
}
Ok(())
}
}