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
//! Scalar reference kernels for 16-bit packed RGB sources (Tier 8 finish).
//!
//! Input planes are `&[u16]`. Each u16 sample is either LE- or BE-encoded on
//! disk/wire; the `<const BE: bool>` const-generic parameter selects the
//! interpretation.  When `BE = false` the input is LE-encoded; when `BE = true`
//! the input is BE-encoded.  In both cases each element is converted to
//! host-native byte order on load via `u16::from_le` / `u16::from_be`, which
//! are no-ops when the source byte order already matches the host.  This
//! mirrors the SIMD `load_endian_u16x*` helpers and keeps the scalar reference
//! correct on big-endian hosts (s390x).
//!
//! # Format layouts
//!
//! | Format  | Elements per pixel | Channel order |
//! |---------|--------------------|---------------|
//! | Rgb48   | 3                  | R, G, B       |
//! | Bgr48   | 3                  | B, G, R       |
//! | Rgba64  | 4                  | R, G, B, A    |
//! | Bgra64  | 4                  | B, G, R, A    |
//!
//! # Depth-conversion convention
//!
//! - u16 → u8: `(v >> 8) as u8` (high-byte extraction, matching Y216 / Ship 11d).
//! - u16 → u16: identity copy (no scaling).

// ---- Endian load helper ------------------------------------------------------

/// Load one u16 element from a source whose byte order is selected by `BE`,
/// returning the value in host-native byte order.
///
/// `u16::from_be` / `u16::from_le` are target-endian aware: each is a no-op
/// when the source byte order matches the host, and a `swap_bytes` otherwise.
/// This matches the SIMD `load_endian_u16x*` helpers and keeps the scalar
/// reference correct on big-endian hosts (s390x).
///
/// The `if BE` branch is evaluated at compile time (monomorphization), so the
/// unused branch is entirely eliminated from the generated binary.
#[inline(always)]
fn load_u16<const BE: bool>(v: u16) -> u16 {
  if BE { u16::from_be(v) } else { u16::from_le(v) }
}

// ---- Rgb48 family (3 u16 elements per pixel: R, G, B) ----------------------

/// Rgb48 → packed u8 RGB: narrow each 16-bit channel via `>> 8`.
///
/// When `BE = true` each u16 element is byte-swapped on load so the channel
/// value is in host-native order before narrowing.
///
/// Input stride: `width * 3` u16 elements, output: `width * 3` bytes.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn rgb48_to_rgb_row<const BE: bool>(rgb48: &[u16], rgb_out: &mut [u8], width: usize) {
  debug_assert!(rgb48.len() >= width * 3, "rgb48 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");
  for x in 0..width {
    let src = x * 3;
    let dst = x * 3;
    rgb_out[dst] = (load_u16::<BE>(rgb48[src]) >> 8) as u8;
    rgb_out[dst + 1] = (load_u16::<BE>(rgb48[src + 1]) >> 8) as u8;
    rgb_out[dst + 2] = (load_u16::<BE>(rgb48[src + 2]) >> 8) as u8;
  }
}

/// Rgb48 → packed u16 RGB: copy with optional byte-swap (already R, G, B order).
///
/// When `BE = true` each element is byte-swapped so the output contains
/// host-native u16 values.
///
/// Input and output stride: `width * 3` u16 elements.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn rgb48_to_rgb_u16_row<const BE: bool>(
  rgb48: &[u16],
  rgb_u16_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgb48.len() >= width * 3, "rgb48 row too short");
  debug_assert!(rgb_u16_out.len() >= width * 3, "rgb_u16_out row too short");
  if BE {
    for i in 0..width * 3 {
      rgb_u16_out[i] = u16::from_be(rgb48[i]);
    }
  } else {
    // LE source: use the target-endian-aware load on each element so big-endian
    // hosts also receive host-native u16 output.
    for i in 0..width * 3 {
      rgb_u16_out[i] = u16::from_le(rgb48[i]);
    }
  }
}

/// Rgb48 → packed u8 RGBA: narrow each 16-bit channel via `>> 8`, force alpha = 0xFF.
///
/// When `BE = true` each u16 element is byte-swapped on load.
///
/// Input stride: `width * 3` u16 elements, output: `width * 4` bytes.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn rgb48_to_rgba_row<const BE: bool>(rgb48: &[u16], rgba_out: &mut [u8], width: usize) {
  debug_assert!(rgb48.len() >= width * 3, "rgb48 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");
  for x in 0..width {
    let src = x * 3;
    let dst = x * 4;
    rgba_out[dst] = (load_u16::<BE>(rgb48[src]) >> 8) as u8;
    rgba_out[dst + 1] = (load_u16::<BE>(rgb48[src + 1]) >> 8) as u8;
    rgba_out[dst + 2] = (load_u16::<BE>(rgb48[src + 2]) >> 8) as u8;
    rgba_out[dst + 3] = 0xFF;
  }
}

/// Rgb48 → packed u16 RGBA: copy R/G/B (with optional byte-swap), force alpha = 0xFFFF.
///
/// When `BE = true` each element is byte-swapped to produce host-native output.
///
/// Input stride: `width * 3` u16 elements, output: `width * 4` u16 elements.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn rgb48_to_rgba_u16_row<const BE: bool>(
  rgb48: &[u16],
  rgba_u16_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgb48.len() >= width * 3, "rgb48 row too short");
  debug_assert!(
    rgba_u16_out.len() >= width * 4,
    "rgba_u16_out row too short"
  );
  for x in 0..width {
    let src = x * 3;
    let dst = x * 4;
    rgba_u16_out[dst] = load_u16::<BE>(rgb48[src]);
    rgba_u16_out[dst + 1] = load_u16::<BE>(rgb48[src + 1]);
    rgba_u16_out[dst + 2] = load_u16::<BE>(rgb48[src + 2]);
    rgba_u16_out[dst + 3] = 0xFFFF;
  }
}

// ---- Bgr48 family (3 u16 elements per pixel: B, G, R) ----------------------

/// Bgr48 → packed u8 RGB: narrow via `>> 8`, swap B↔R on output.
///
/// When `BE = true` each u16 element is byte-swapped on load.
///
/// Source layout `[B, G, R]` → output layout `[R, G, B]`.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn bgr48_to_rgb_row<const BE: bool>(bgr48: &[u16], rgb_out: &mut [u8], width: usize) {
  debug_assert!(bgr48.len() >= width * 3, "bgr48 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");
  for x in 0..width {
    let src = x * 3;
    let dst = x * 3;
    rgb_out[dst] = (load_u16::<BE>(bgr48[src + 2]) >> 8) as u8; // R (from B-G-R position 2)
    rgb_out[dst + 1] = (load_u16::<BE>(bgr48[src + 1]) >> 8) as u8; // G (unchanged)
    rgb_out[dst + 2] = (load_u16::<BE>(bgr48[src]) >> 8) as u8; // B (from B-G-R position 0)
  }
}

/// Bgr48 → packed u16 RGB: copy with B↔R swap (and optional byte-swap).
///
/// When `BE = true` each element is byte-swapped to produce host-native output.
///
/// Source layout `[B, G, R]` → output layout `[R, G, B]`.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn bgr48_to_rgb_u16_row<const BE: bool>(
  bgr48: &[u16],
  rgb_u16_out: &mut [u16],
  width: usize,
) {
  debug_assert!(bgr48.len() >= width * 3, "bgr48 row too short");
  debug_assert!(rgb_u16_out.len() >= width * 3, "rgb_u16_out row too short");
  for x in 0..width {
    let src = x * 3;
    let dst = x * 3;
    rgb_u16_out[dst] = load_u16::<BE>(bgr48[src + 2]); // R
    rgb_u16_out[dst + 1] = load_u16::<BE>(bgr48[src + 1]); // G
    rgb_u16_out[dst + 2] = load_u16::<BE>(bgr48[src]); // B
  }
}

/// Bgr48 → packed u8 RGBA: narrow + B↔R swap + force alpha = 0xFF.
///
/// When `BE = true` each u16 element is byte-swapped on load.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn bgr48_to_rgba_row<const BE: bool>(bgr48: &[u16], rgba_out: &mut [u8], width: usize) {
  debug_assert!(bgr48.len() >= width * 3, "bgr48 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");
  for x in 0..width {
    let src = x * 3;
    let dst = x * 4;
    rgba_out[dst] = (load_u16::<BE>(bgr48[src + 2]) >> 8) as u8; // R
    rgba_out[dst + 1] = (load_u16::<BE>(bgr48[src + 1]) >> 8) as u8; // G
    rgba_out[dst + 2] = (load_u16::<BE>(bgr48[src]) >> 8) as u8; // B
    rgba_out[dst + 3] = 0xFF;
  }
}

/// Bgr48 → packed u16 RGBA: B↔R swap (+ optional byte-swap) + force alpha = 0xFFFF.
///
/// When `BE = true` each element is byte-swapped to produce host-native output.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn bgr48_to_rgba_u16_row<const BE: bool>(
  bgr48: &[u16],
  rgba_u16_out: &mut [u16],
  width: usize,
) {
  debug_assert!(bgr48.len() >= width * 3, "bgr48 row too short");
  debug_assert!(
    rgba_u16_out.len() >= width * 4,
    "rgba_u16_out row too short"
  );
  for x in 0..width {
    let src = x * 3;
    let dst = x * 4;
    rgba_u16_out[dst] = load_u16::<BE>(bgr48[src + 2]); // R
    rgba_u16_out[dst + 1] = load_u16::<BE>(bgr48[src + 1]); // G
    rgba_u16_out[dst + 2] = load_u16::<BE>(bgr48[src]); // B
    rgba_u16_out[dst + 3] = 0xFFFF;
  }
}

// ---- Rgba64 family (4 u16 elements per pixel: R, G, B, A) ------------------

/// Rgba64 → packed u8 RGB: drop alpha, narrow R/G/B via `>> 8`.
///
/// When `BE = true` each u16 element is byte-swapped on load.
///
/// Input stride: `width * 4` u16 elements, output: `width * 3` bytes.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn rgba64_to_rgb_row<const BE: bool>(rgba64: &[u16], rgb_out: &mut [u8], width: usize) {
  debug_assert!(rgba64.len() >= width * 4, "rgba64 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");
  for x in 0..width {
    let src = x * 4;
    let dst = x * 3;
    rgb_out[dst] = (load_u16::<BE>(rgba64[src]) >> 8) as u8;
    rgb_out[dst + 1] = (load_u16::<BE>(rgba64[src + 1]) >> 8) as u8;
    rgb_out[dst + 2] = (load_u16::<BE>(rgba64[src + 2]) >> 8) as u8;
  }
}

/// Rgba64 → packed u16 RGB: drop alpha, copy R/G/B (with optional byte-swap).
///
/// When `BE = true` each element is byte-swapped to produce host-native output.
///
/// Input stride: `width * 4` u16 elements, output: `width * 3` u16 elements.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn rgba64_to_rgb_u16_row<const BE: bool>(
  rgba64: &[u16],
  rgb_u16_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgba64.len() >= width * 4, "rgba64 row too short");
  debug_assert!(rgb_u16_out.len() >= width * 3, "rgb_u16_out row too short");
  for x in 0..width {
    let src = x * 4;
    let dst = x * 3;
    rgb_u16_out[dst] = load_u16::<BE>(rgba64[src]);
    rgb_u16_out[dst + 1] = load_u16::<BE>(rgba64[src + 1]);
    rgb_u16_out[dst + 2] = load_u16::<BE>(rgba64[src + 2]);
  }
}

/// Rgba64 → packed u8 RGBA: narrow all 4 channels via `>> 8` (source alpha passes through).
///
/// When `BE = true` each u16 element is byte-swapped on load.
///
/// Input and output stride: `width * 4` elements.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn rgba64_to_rgba_row<const BE: bool>(
  rgba64: &[u16],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgba64.len() >= width * 4, "rgba64 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");
  for x in 0..width {
    let i = x * 4;
    rgba_out[i] = (load_u16::<BE>(rgba64[i]) >> 8) as u8;
    rgba_out[i + 1] = (load_u16::<BE>(rgba64[i + 1]) >> 8) as u8;
    rgba_out[i + 2] = (load_u16::<BE>(rgba64[i + 2]) >> 8) as u8;
    rgba_out[i + 3] = (load_u16::<BE>(rgba64[i + 3]) >> 8) as u8;
  }
}

/// Rgba64 → packed u16 RGBA: copy all 4 channels (with optional byte-swap).
///
/// When `BE = true` each element is byte-swapped to produce host-native output.
///
/// Input and output stride: `width * 4` u16 elements.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn rgba64_to_rgba_u16_row<const BE: bool>(
  rgba64: &[u16],
  rgba_u16_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgba64.len() >= width * 4, "rgba64 row too short");
  debug_assert!(
    rgba_u16_out.len() >= width * 4,
    "rgba_u16_out row too short"
  );
  if BE {
    for i in 0..width * 4 {
      rgba_u16_out[i] = u16::from_be(rgba64[i]);
    }
  } else {
    // LE source: use the target-endian-aware load on each element so big-endian
    // hosts also receive host-native u16 output.
    for i in 0..width * 4 {
      rgba_u16_out[i] = u16::from_le(rgba64[i]);
    }
  }
}

// ---- Bgra64 family (4 u16 elements per pixel: B, G, R, A) ------------------

/// Bgra64 → packed u8 RGB: drop alpha, narrow via `>> 8`, swap B↔R on output.
///
/// When `BE = true` each u16 element is byte-swapped on load.
///
/// Source layout `[B, G, R, A]` → output layout `[R, G, B]`.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn bgra64_to_rgb_row<const BE: bool>(bgra64: &[u16], rgb_out: &mut [u8], width: usize) {
  debug_assert!(bgra64.len() >= width * 4, "bgra64 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");
  for x in 0..width {
    let src = x * 4;
    let dst = x * 3;
    rgb_out[dst] = (load_u16::<BE>(bgra64[src + 2]) >> 8) as u8; // R (from position 2)
    rgb_out[dst + 1] = (load_u16::<BE>(bgra64[src + 1]) >> 8) as u8; // G (unchanged)
    rgb_out[dst + 2] = (load_u16::<BE>(bgra64[src]) >> 8) as u8; // B (from position 0)
  }
}

/// Bgra64 → packed u16 RGB: drop alpha, B↔R swap (+ optional byte-swap).
///
/// When `BE = true` each element is byte-swapped to produce host-native output.
///
/// Source layout `[B, G, R, A]` → output layout `[R, G, B]`.
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn bgra64_to_rgb_u16_row<const BE: bool>(
  bgra64: &[u16],
  rgb_u16_out: &mut [u16],
  width: usize,
) {
  debug_assert!(bgra64.len() >= width * 4, "bgra64 row too short");
  debug_assert!(rgb_u16_out.len() >= width * 3, "rgb_u16_out row too short");
  for x in 0..width {
    let src = x * 4;
    let dst = x * 3;
    rgb_u16_out[dst] = load_u16::<BE>(bgra64[src + 2]); // R
    rgb_u16_out[dst + 1] = load_u16::<BE>(bgra64[src + 1]); // G
    rgb_u16_out[dst + 2] = load_u16::<BE>(bgra64[src]); // B
  }
}

/// Bgra64 → packed u8 RGBA: narrow via `>> 8`, swap B↔R, pass through source alpha.
///
/// When `BE = true` each u16 element is byte-swapped on load.
///
/// Source layout `[B, G, R, A]` → output layout `[R, G, B, A]` (all narrowed `>> 8`).
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn bgra64_to_rgba_row<const BE: bool>(
  bgra64: &[u16],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(bgra64.len() >= width * 4, "bgra64 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");
  for x in 0..width {
    let src = x * 4;
    let dst = x * 4;
    rgba_out[dst] = (load_u16::<BE>(bgra64[src + 2]) >> 8) as u8; // R
    rgba_out[dst + 1] = (load_u16::<BE>(bgra64[src + 1]) >> 8) as u8; // G
    rgba_out[dst + 2] = (load_u16::<BE>(bgra64[src]) >> 8) as u8; // B
    rgba_out[dst + 3] = (load_u16::<BE>(bgra64[src + 3]) >> 8) as u8; // A
  }
}

/// Bgra64 → packed u16 RGBA: B↔R swap (+ optional byte-swap), pass through source alpha.
///
/// When `BE = true` each element is byte-swapped to produce host-native output.
///
/// Source layout `[B, G, R, A]` → output layout `[R, G, B, A]` (all native u16).
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn bgra64_to_rgba_u16_row<const BE: bool>(
  bgra64: &[u16],
  rgba_u16_out: &mut [u16],
  width: usize,
) {
  debug_assert!(bgra64.len() >= width * 4, "bgra64 row too short");
  debug_assert!(
    rgba_u16_out.len() >= width * 4,
    "rgba_u16_out row too short"
  );
  for x in 0..width {
    let src = x * 4;
    let dst = x * 4;
    rgba_u16_out[dst] = load_u16::<BE>(bgra64[src + 2]); // R
    rgba_u16_out[dst + 1] = load_u16::<BE>(bgra64[src + 1]); // G
    rgba_u16_out[dst + 2] = load_u16::<BE>(bgra64[src]); // B
    rgba_u16_out[dst + 3] = load_u16::<BE>(bgra64[src + 3]); // A (byte-order corrected)
  }
}

// ---- Unit tests -------------------------------------------------------------

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