colconv 0.1.0

SIMD-dispatched color-conversion kernels covering the FFmpeg AVPixelFormat space, with a Sink-based API so consumers pick which derived outputs (RGB / Luma / HSV / custom) they want without paying for the ones they don't.
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Scalar gray → {RGB, RGBA, HSV, luma, luma_u16} kernels.
//!
//! # Gray8 (u8 source)
//!
//! `gray8_to_rgb_row`  — broadcast Y to each R/G/B channel.
//! `gray8_to_rgba_row` — broadcast Y + force α = 0xFF.
//! `gray8_to_hsv_row`  — H=0, S=0, V=Y (S=0 convention: H is fixed to 0).
//! `gray8_to_luma_u16_row` — zero-extend Y to u16 (`out[x] = y[x] as u16`).
//!   (luma u8 identity copy is handled by the sinker directly via
//!   `copy_from_slice`, no dedicated kernel needed.)
//!
//! # GrayN / Gray16 (u16 source)
//!
//! `gray_n_to_rgb_row<BITS>`       — mask → downshift to u8, broadcast.
//! `gray_n_to_rgba_row<BITS>`      — same + α = 0xFF.
//! `gray_n_to_rgb_u16_row<BITS>`   — mask, keep native depth, broadcast.
//! `gray_n_to_rgba_u16_row<BITS>`  — same + α = full-range max for BITS.
//! `gray_n_to_luma_row<BITS>`      — mask → downshift to u8.
//! `gray_n_to_luma_u16_row<BITS>`  — mask → identity (samples already u16).
//! `gray_n_to_hsv_row<BITS>`       — mask → downshift to u8 → H=0 S=0 V=Y8.
//!
//! `gray16_to_rgb_row`    — `>> 8` to u8, broadcast.
//! `gray16_to_rgba_row`   — same + α = 0xFF.
//! `gray16_to_rgb_u16_row`  — native u16, broadcast.
//! `gray16_to_rgba_u16_row` — native u16 + α = 0xFFFF.
//! `gray16_to_luma_row`   — `>> 8` to u8.
//! `gray16_to_luma_u16_row` — identity copy to u16.
//! `gray16_to_hsv_row`    — `>> 8` to u8 → H=0 S=0 V=Y8.
//!
//! # HSV S=0 convention
//!
//! When S=0 (which is always for gray sources — delta = 0), H is set to 0.
//! This matches OpenCV `cv2.COLOR_GRAY2HSV` behavior.
//!
//! # `full_range` parameter (RGB/RGBA/HSV outputs only)
//!
//! - `full_range = true`: raw Y is used directly (broadcast / downshift).
//!   This is the standard full-range path.
//! - `full_range = false`: limited-range Y is rescaled to full range before
//!   broadcast. For 8-bit limited range: black=16, white=235. The rescaled
//!   value is `clamp_u8(((y - black) * 255 + range/2) / range)`.
//!
//! **Luma outputs** (`with_luma`, `with_luma_u16`) always pass Y through
//! without rescaling — the caller is explicitly requesting the source luma
//! plane as-is, regardless of `full_range`.

use super::bits_mask;

// ---- helpers ----------------------------------------------------------------

/// Broadcasts a `u8` gray value to packed RGB (3 bytes: R=G=B=y).
#[inline(always)]
fn broadcast_u8_to_rgb(y: u8, out: &mut [u8], x: usize) {
  let i = x * 3;
  out[i] = y;
  out[i + 1] = y;
  out[i + 2] = y;
}

/// Broadcasts a `u8` gray value to packed RGBA (4 bytes: R=G=B=y, A=0xFF).
#[inline(always)]
fn broadcast_u8_to_rgba(y: u8, out: &mut [u8], x: usize) {
  let i = x * 4;
  out[i] = y;
  out[i + 1] = y;
  out[i + 2] = y;
  out[i + 3] = 0xFF;
}

/// Broadcasts a `u16` gray value to packed u16 RGB (3 u16: R=G=B=y).
#[inline(always)]
fn broadcast_u16_to_rgb(y: u16, out: &mut [u16], x: usize) {
  let i = x * 3;
  out[i] = y;
  out[i + 1] = y;
  out[i + 2] = y;
}

/// Broadcasts a `u16` gray value to packed u16 RGBA (4 u16: R=G=B=y, A=alpha).
#[inline(always)]
fn broadcast_u16_to_rgba(y: u16, alpha: u16, out: &mut [u16], x: usize) {
  let i = x * 4;
  out[i] = y;
  out[i + 1] = y;
  out[i + 2] = y;
  out[i + 3] = alpha;
}

/// Rescales a limited-range 8-bit luma value to full-range u8.
///
/// Limited-range 8-bit: black=16, white=235 (range=219).
/// Formula: `clamp_u8(((y - 16) * 255 + 109) / 219)` (109 = 219/2, rounding).
#[inline(always)]
fn limited_to_full_u8(y: u8) -> u8 {
  let y = y as i32;
  let rescaled = (y - 16) * 255 + 109; // 109 = 219/2 for rounding
  let result = rescaled / 219;
  result.clamp(0, 255) as u8
}

/// Rescales a limited-range N-bit luma value to full-range u8.
///
/// Limited-range: black = 16 << (BITS-8), range = 219 << (BITS-8).
#[inline(always)]
fn limited_n_to_full_u8<const BITS: u32>(y: u16) -> u8 {
  let black = 16i32 << (BITS - 8);
  let range = 219i32 << (BITS - 8);
  let y = y as i32;
  let rescaled = (y - black) * 255 + range / 2;
  let result = rescaled / range;
  result.clamp(0, 255) as u8
}

/// Rescales a limited-range N-bit luma value to full-range u16 (native depth).
///
/// Limited-range: black = 16 << (BITS-8), range = 219 << (BITS-8).
/// Output is clamped to [0, max_native] where max_native = (1 << BITS) - 1.
///
/// Math runs in `i64` to keep the `(y - black) * max_native` product safe at
/// `BITS = 16`: limited-range white `60160` minus black `4096` times
/// `max_native = 65535` is `~3.67 x 10^9`, which overflows `i32`. Lower bit
/// depths fit in `i32` but using `i64` uniformly keeps one signature and
/// avoids per-BITS branches.
#[inline(always)]
fn limited_n_to_full_u16<const BITS: u32>(y: u16) -> u16 {
  let black = 16i64 << (BITS - 8);
  let range = 219i64 << (BITS - 8);
  let max_native = ((1u64 << BITS) - 1) as i64;
  let y = y as i64;
  let rescaled = (y - black) * max_native + range / 2;
  let result = rescaled / range;
  result.clamp(0, max_native) as u16
}

/// Rescales a limited-range 16-bit luma value to full-range u8.
///
/// Limited-range 16-bit: black = 16 << 8 = 4096, range = 219 << 8 = 56064.
#[inline(always)]
fn limited_16_to_full_u8(y: u16) -> u8 {
  limited_n_to_full_u8::<16>(y)
}

/// Rescales a limited-range 16-bit luma value to full-range u16.
///
/// Limited-range 16-bit: black = 4096, range = 56064, max_native = 65535.
#[inline(always)]
fn limited_16_to_full_u16(y: u16) -> u16 {
  limited_n_to_full_u16::<16>(y)
}

// ---- Gray8 ------------------------------------------------------------------

/// Broadcasts each `u8` gray sample to packed RGB (`R = G = B = Y`).
///
/// When `full_range = false`, limited-range Y (black=16, white=235) is
/// rescaled to full-range [0, 255] before broadcast.
/// When `full_range = true`, raw Y is used directly.
///
/// Luma outputs always pass Y through without rescaling.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray8_to_rgb_row(y_plane: &[u8], out: &mut [u8], width: usize, full_range: bool) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width * 3, "out too short");
  for (x, &y) in y_plane[..width].iter().enumerate() {
    let y_out = if full_range { y } else { limited_to_full_u8(y) };
    broadcast_u8_to_rgb(y_out, out, x);
  }
}

/// Broadcasts each `u8` gray sample to packed RGBA (`R = G = B = Y`, `A = 0xFF`).
///
/// When `full_range = false`, limited-range Y is rescaled to full range.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray8_to_rgba_row(y_plane: &[u8], out: &mut [u8], width: usize, full_range: bool) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width * 4, "out too short");
  for (x, &y) in y_plane[..width].iter().enumerate() {
    let y_out = if full_range { y } else { limited_to_full_u8(y) };
    broadcast_u8_to_rgba(y_out, out, x);
  }
}

/// Gray8 → HSV row. Convention: S=0, H=0, V=Y (or rescaled Y if limited range).
///
/// Gray sources are achromatic (saturation = 0). When S=0, H is
/// undefined in the continuous HSV model; this crate fixes H=0 to
/// match OpenCV's `cv2.COLOR_GRAY2HSV` convention and avoid
/// non-deterministic hue output.
///
/// When `full_range = false`, the V channel uses the rescaled luma value.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray8_to_hsv_row(
  y_plane: &[u8],
  h_out: &mut [u8],
  s_out: &mut [u8],
  v_out: &mut [u8],
  width: usize,
  full_range: bool,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(h_out.len() >= width, "H out too short");
  debug_assert!(s_out.len() >= width, "S out too short");
  debug_assert!(v_out.len() >= width, "V out too short");
  for (x, &y) in y_plane[..width].iter().enumerate() {
    h_out[x] = 0;
    s_out[x] = 0;
    v_out[x] = if full_range { y } else { limited_to_full_u8(y) };
  }
}

// ---- GrayN (u16 low-bit-packed, BITS in {9,10,12,14}) ----------------------

/// GrayN → packed RGB u8. Masks to BITS bits, downshifts `BITS - 8` to u8,
/// broadcasts.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// When `full_range = false`, limited-range Y is rescaled to [0, 255]
/// before broadcast. Luma outputs always pass Y through without rescaling.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray_n_to_rgb_row<const BITS: u32, const BE: bool>(
  y_plane: &[u16],
  out: &mut [u8],
  width: usize,
  full_range: bool,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width * 3, "out too short");
  let mask = bits_mask::<BITS>();
  let shift = BITS - 8;
  for (x, &raw) in y_plane[..width].iter().enumerate() {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    let masked = raw & mask;
    let y8 = if full_range {
      (masked >> shift) as u8
    } else {
      limited_n_to_full_u8::<BITS>(masked)
    };
    broadcast_u8_to_rgb(y8, out, x);
  }
}

/// GrayN → packed RGBA u8. Masks to BITS bits, downshifts to u8, broadcasts,
/// α = 0xFF.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// When `full_range = false`, limited-range Y is rescaled to [0, 255].
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray_n_to_rgba_row<const BITS: u32, const BE: bool>(
  y_plane: &[u16],
  out: &mut [u8],
  width: usize,
  full_range: bool,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width * 4, "out too short");
  let mask = bits_mask::<BITS>();
  let shift = BITS - 8;
  for (x, &raw) in y_plane[..width].iter().enumerate() {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    let masked = raw & mask;
    let y8 = if full_range {
      (masked >> shift) as u8
    } else {
      limited_n_to_full_u8::<BITS>(masked)
    };
    broadcast_u8_to_rgba(y8, out, x);
  }
}

/// GrayN → packed u16 RGB. Masks to BITS bits, broadcasts at native depth.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// When `full_range = false`, limited-range Y is rescaled to full native range
/// [0, (1<<BITS)-1] before broadcast.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray_n_to_rgb_u16_row<const BITS: u32, const BE: bool>(
  y_plane: &[u16],
  out: &mut [u16],
  width: usize,
  full_range: bool,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width * 3, "out too short");
  let mask = bits_mask::<BITS>();
  for (x, &raw) in y_plane[..width].iter().enumerate() {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    let masked = raw & mask;
    let y_out = if full_range {
      masked
    } else {
      limited_n_to_full_u16::<BITS>(masked)
    };
    broadcast_u16_to_rgb(y_out, out, x);
  }
}

/// GrayN → packed u16 RGBA. Masks to BITS bits, broadcasts, α = `(1 << BITS) - 1`.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// When `full_range = false`, limited-range Y is rescaled to full native range.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray_n_to_rgba_u16_row<const BITS: u32, const BE: bool>(
  y_plane: &[u16],
  out: &mut [u16],
  width: usize,
  full_range: bool,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width * 4, "out too short");
  let mask = bits_mask::<BITS>();
  let alpha = mask; // full-range max for BITS
  for (x, &raw) in y_plane[..width].iter().enumerate() {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    let masked = raw & mask;
    let y_out = if full_range {
      masked
    } else {
      limited_n_to_full_u16::<BITS>(masked)
    };
    broadcast_u16_to_rgba(y_out, alpha, out, x);
  }
}

/// GrayN → luma u8. Masks to BITS bits, downshifts `BITS - 8`.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// Always passes raw Y through without `full_range` rescaling —
/// the caller is explicitly requesting the source luma plane as-is.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray_n_to_luma_row<const BITS: u32, const BE: bool>(
  y_plane: &[u16],
  out: &mut [u8],
  width: usize,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width, "out too short");
  let mask = bits_mask::<BITS>();
  let shift = BITS - 8;
  for (out_byte, &raw) in out[..width].iter_mut().zip(y_plane[..width].iter()) {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    *out_byte = ((raw & mask) >> shift) as u8;
  }
}

/// GrayN → luma u16. Masks to BITS bits, identity copy.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// Always passes raw Y through without `full_range` rescaling —
/// the caller is explicitly requesting the source luma plane as-is.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray_n_to_luma_u16_row<const BITS: u32, const BE: bool>(
  y_plane: &[u16],
  out: &mut [u16],
  width: usize,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width, "out too short");
  let mask = bits_mask::<BITS>();
  for (out_el, &raw) in out[..width].iter_mut().zip(y_plane[..width].iter()) {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    *out_el = raw & mask;
  }
}

/// GrayN → HSV u8. Masks to BITS bits, downshifts to u8, H=0 S=0 V=Y8.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// When `full_range = false`, the V channel uses the rescaled luma value.
/// See [`gray8_to_hsv_row`] for the S=0 convention.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray_n_to_hsv_row<const BITS: u32, const BE: bool>(
  y_plane: &[u16],
  h_out: &mut [u8],
  s_out: &mut [u8],
  v_out: &mut [u8],
  width: usize,
  full_range: bool,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(h_out.len() >= width, "H out too short");
  debug_assert!(s_out.len() >= width, "S out too short");
  debug_assert!(v_out.len() >= width, "V out too short");
  let mask = bits_mask::<BITS>();
  let shift = BITS - 8;
  for (x, &raw) in y_plane[..width].iter().enumerate() {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    let masked = raw & mask;
    h_out[x] = 0;
    s_out[x] = 0;
    v_out[x] = if full_range {
      (masked >> shift) as u8
    } else {
      limited_n_to_full_u8::<BITS>(masked)
    };
  }
}

// ---- Gray16 (u16, all 16 bits active) ----------------------------------------

/// Gray16 → packed RGB u8. Downshifts `>> 8` to u8, broadcasts.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// When `full_range = false`, limited-range Y (black=4096, white=56064+4096)
/// is rescaled to [0, 255] before broadcast.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray16_to_rgb_row<const BE: bool>(
  y_plane: &[u16],
  out: &mut [u8],
  width: usize,
  full_range: bool,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width * 3, "out too short");
  for (x, &raw) in y_plane[..width].iter().enumerate() {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    let y8 = if full_range {
      (raw >> 8) as u8
    } else {
      limited_16_to_full_u8(raw)
    };
    broadcast_u8_to_rgb(y8, out, x);
  }
}

/// Gray16 → packed RGBA u8. Downshifts `>> 8`, broadcasts, α = 0xFF.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// When `full_range = false`, limited-range Y is rescaled to [0, 255].
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray16_to_rgba_row<const BE: bool>(
  y_plane: &[u16],
  out: &mut [u8],
  width: usize,
  full_range: bool,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width * 4, "out too short");
  for (x, &raw) in y_plane[..width].iter().enumerate() {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    let y8 = if full_range {
      (raw >> 8) as u8
    } else {
      limited_16_to_full_u8(raw)
    };
    broadcast_u8_to_rgba(y8, out, x);
  }
}

/// Gray16 → packed u16 RGB. Identity broadcast, native 16-bit depth.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// When `full_range = false`, limited-range Y is rescaled to [0, 65535].
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray16_to_rgb_u16_row<const BE: bool>(
  y_plane: &[u16],
  out: &mut [u16],
  width: usize,
  full_range: bool,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width * 3, "out too short");
  for (x, &raw) in y_plane[..width].iter().enumerate() {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    let y_out = if full_range {
      raw
    } else {
      limited_16_to_full_u16(raw)
    };
    broadcast_u16_to_rgb(y_out, out, x);
  }
}

/// Gray16 → packed u16 RGBA. Identity broadcast, α = 0xFFFF.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// When `full_range = false`, limited-range Y is rescaled to [0, 65535].
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray16_to_rgba_u16_row<const BE: bool>(
  y_plane: &[u16],
  out: &mut [u16],
  width: usize,
  full_range: bool,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width * 4, "out too short");
  for (x, &raw) in y_plane[..width].iter().enumerate() {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    let y_out = if full_range {
      raw
    } else {
      limited_16_to_full_u16(raw)
    };
    broadcast_u16_to_rgba(y_out, 0xFFFF, out, x);
  }
}

/// Gray16 → luma u8. Downshifts `>> 8`.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// Always passes raw Y through without `full_range` rescaling —
/// the caller is explicitly requesting the source luma plane as-is.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray16_to_luma_row<const BE: bool>(y_plane: &[u16], out: &mut [u8], width: usize) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width, "out too short");
  for (out_byte, &raw) in out[..width].iter_mut().zip(y_plane[..width].iter()) {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    *out_byte = (raw >> 8) as u8;
  }
}

/// Gray16 → luma u16. Identity copy (or byte-swap copy for BE).
///
/// When `BE = true`, each u16 sample is byte-swapped before output.
/// Always passes raw Y through without `full_range` rescaling —
/// the caller is explicitly requesting the source luma plane as-is.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray16_to_luma_u16_row<const BE: bool>(
  y_plane: &[u16],
  out: &mut [u16],
  width: usize,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(out.len() >= width, "out too short");
  for (o, &raw) in out[..width].iter_mut().zip(y_plane[..width].iter()) {
    *o = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
  }
}

/// Gray16 → HSV u8. `>> 8` to u8, H=0 S=0 V=Y8.
///
/// When `BE = true`, each u16 sample is byte-swapped before processing.
/// When `full_range = false`, the V channel uses the rescaled luma value.
/// See [`gray8_to_hsv_row`] for the S=0 convention.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn gray16_to_hsv_row<const BE: bool>(
  y_plane: &[u16],
  h_out: &mut [u8],
  s_out: &mut [u8],
  v_out: &mut [u8],
  width: usize,
  full_range: bool,
) {
  debug_assert!(y_plane.len() >= width, "y_plane too short");
  debug_assert!(h_out.len() >= width, "H out too short");
  debug_assert!(s_out.len() >= width, "S out too short");
  debug_assert!(v_out.len() >= width, "V out too short");
  for (x, &raw) in y_plane[..width].iter().enumerate() {
    let raw = if BE {
      u16::from_be(raw)
    } else {
      u16::from_le(raw)
    };
    h_out[x] = 0;
    s_out[x] = 0;
    v_out[x] = if full_range {
      (raw >> 8) as u8
    } else {
      limited_16_to_full_u8(raw)
    };
  }
}

#[cfg(all(test, feature = "std"))]
mod tests;