fovea 0.2.0

A high-precision, type-safe computer vision library guaranteeing absolute image correctness at compile time
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//! sRGB gamma-encoded pixel types.
//!
//! Same memory layouts as their linear counterparts (`Rgb8`, `Rgba8`, etc.)
//! but representing sRGB-encoded values. These types intentionally do NOT
//! implement `LinearPixel` or `LinearSpace`, so the compiler rejects
//! attempts to use them with interpolation algorithms. Convert to linear
//! light first with the `SrgbGamma` strategy.

use fovea_derive::{HomogeneousPixel, PlainPixel, WhiteChannel, ZeroablePixel};

use std::num::Saturating;

use crate::pixel::impl_origin_invariant_pixel;

// ═══════════════════════════════════════════════════════════════════════════════
// sRGB gamma-encoded pixel types
//
// Same memory layout as Rgb8 / Rgba8 but representing sRGB-encoded values.
// These types intentionally do NOT implement `LinearPixel` or `LinearSpace`,
// so they cannot be passed to algorithms that assume linear light (e.g.
// bilinear resize).  Convert to linear with the `SrgbGamma` strategy first.
// ═══════════════════════════════════════════════════════════════════════════════

/// sRGB-encoded RGB pixel with 8-bit depth per channel.
///
/// This type has the same memory layout as [`Rgb8`](crate::pixel::Rgb8) (three `Saturating<u8>`
/// in R, G, B order) but represents **gamma-encoded** sRGB values.
///
/// It does **not** implement [`LinearPixel`](crate::pixel::LinearPixel) or [`LinearSpace`](crate::pixel::LinearSpace), so the
/// compiler will reject attempts to use it with interpolation algorithms
/// like bilinear resize.  Convert to linear light first using the
/// `SrgbGamma` conversion strategy:
///
/// ```
/// # use fovea::pixel::{Srgb8, RgbF32};
/// # use fovea::transform::{ConvertPixel, SrgbGamma};
/// let srgb = Srgb8::new(128, 64, 200);
/// let linear: RgbF32 = SrgbGamma.convert(&srgb);
/// ```
#[repr(C)]
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    PlainPixel,
    HomogeneousPixel,
    ZeroablePixel,
    WhiteChannel,
)]
pub struct Srgb8 {
    /// Red channel (sRGB-encoded).
    pub r: Saturating<u8>,
    /// Green channel (sRGB-encoded).
    pub g: Saturating<u8>,
    /// Blue channel (sRGB-encoded).
    pub b: Saturating<u8>,
}

impl Srgb8 {
    /// Creates an `Srgb8` pixel from the given 8-bit sRGB-encoded R, G, B channel values.
    pub fn new(r: u8, g: u8, b: u8) -> Self {
        Srgb8 {
            r: Saturating(r),
            g: Saturating(g),
            b: Saturating(b),
        }
    }
}

/// sRGB-encoded RGBA pixel with 8-bit depth per channel.
///
/// This type has the same memory layout as [`Rgba8`](crate::pixel::Rgba8) (four `Saturating<u8>`
/// in R, G, B, A order) but the R, G, B channels represent **gamma-encoded**
/// sRGB values.  The alpha channel is always linear, as required by the sRGB
/// specification.
///
/// It does **not** implement [`LinearPixel`](crate::pixel::LinearPixel) or [`LinearSpace`](crate::pixel::LinearSpace).  Convert to
/// linear light first using the `SrgbGamma` conversion strategy:
///
/// ```
/// # use fovea::pixel::{Srgba8, RgbaF32};
/// # use fovea::transform::{ConvertPixel, SrgbGamma};
/// let srgb = Srgba8::new(128, 64, 200, 255);
/// let linear: RgbaF32 = SrgbGamma.convert(&srgb);
/// ```
#[repr(C)]
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    PlainPixel,
    HomogeneousPixel,
    ZeroablePixel,
    WhiteChannel,
)]
pub struct Srgba8 {
    /// Red channel (sRGB-encoded).
    pub r: Saturating<u8>,
    /// Green channel (sRGB-encoded).
    pub g: Saturating<u8>,
    /// Blue channel (sRGB-encoded).
    pub b: Saturating<u8>,
    /// Alpha channel (linear).
    pub a: Saturating<u8>,
}

impl Srgba8 {
    /// Creates an `Srgba8` pixel. R, G, B are sRGB-encoded (gamma); A is linear.
    pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Srgba8 {
            r: Saturating(r),
            g: Saturating(g),
            b: Saturating(b),
            a: Saturating(a),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// sRGB gamma-encoded grayscale pixel types
//
// Same memory layout as Mono8 / MonoA8 but representing sRGB-encoded values.
// These types intentionally do NOT implement `LinearPixel` or `LinearSpace`,
// so they cannot be passed to algorithms that assume linear light (e.g.
// bilinear resize).  Convert to linear with the `SrgbGamma` strategy first.
//
// Why "sRGB" for a grayscale pixel?
// The sRGB specification (IEC 61966-2-1) defines a single transfer function
// (gamma curve) that applies identically to each channel.  For a grayscale
// image there is only one channel, but the *same* transfer function applies.
// PNG 8-bit grayscale is sRGB-encoded by convention, so calling the type
// `SrgbMono8` is technically correct — the best kind of correct. 😉
// ═══════════════════════════════════════════════════════════════════════════════

/// sRGB-encoded grayscale pixel with 8-bit depth.
///
/// This type has the same memory layout as [`Mono8`](crate::pixel::Mono8) (a single
/// `Saturating<u8>`) but represents a **gamma-encoded** sRGB value.
///
/// It does **not** implement [`LinearPixel`](crate::pixel::LinearPixel) or [`LinearSpace`](crate::pixel::LinearSpace), so the
/// compiler will reject attempts to use it with interpolation algorithms
/// like bilinear resize.  Convert to linear light first using the
/// `SrgbGamma` conversion strategy:
///
/// ```
/// # use fovea::pixel::{SrgbMono8, MonoF32};
/// # use fovea::transform::{ConvertPixel, SrgbGamma};
/// let srgb = SrgbMono8::new(128);
/// let linear: MonoF32 = SrgbGamma.convert(&srgb);
/// // Mid-gray sRGB ≈ 0.216 linear, not 0.502
/// assert!((linear.0 - 0.216).abs() < 0.001);
/// ```
///
/// # Why "sRGB" for grayscale?
///
/// The sRGB specification (IEC 61966-2-1) defines a single transfer
/// function that applies per-channel.  A grayscale image has one channel
/// but uses the exact same curve.  PNG 8-bit grayscale is sRGB-encoded
/// by convention.
#[repr(transparent)]
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    Ord,
    PartialOrd,
    PlainPixel,
    HomogeneousPixel,
    ZeroablePixel,
    WhiteChannel,
)]
pub struct SrgbMono8(pub Saturating<u8>);

impl SrgbMono8 {
    /// Creates an `SrgbMono8` pixel with the given sRGB-encoded 8-bit value.
    pub fn new(value: u8) -> Self {
        SrgbMono8(Saturating(value))
    }
}

/// sRGB-encoded grayscale-with-alpha pixel, 8-bit depth per channel.
///
/// This type has the same memory layout as [`MonoA8`](crate::pixel::MonoA8) (two `Saturating<u8>`
/// in V, A order) but the value channel represents a **gamma-encoded** sRGB
/// intensity.  The alpha channel is always linear, as required by the sRGB
/// specification.
///
/// It does **not** implement [`LinearPixel`](crate::pixel::LinearPixel) or [`LinearSpace`](crate::pixel::LinearSpace), so the
/// compiler will reject attempts to use it with interpolation algorithms
/// like bilinear resize.  Convert to linear light first using the
/// `SrgbGamma` conversion strategy.
///
/// # Why "sRGB" for grayscale?
///
/// See [`SrgbMono8`] — the sRGB transfer function is defined per-channel
/// and applies identically to a single-channel image.
#[repr(C)]
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    PlainPixel,
    HomogeneousPixel,
    ZeroablePixel,
    WhiteChannel,
)]
pub struct SrgbMonoA8 {
    /// Value (luminance) channel (sRGB-encoded).
    pub v: Saturating<u8>,
    /// Alpha channel (linear).
    pub a: Saturating<u8>,
}

impl SrgbMonoA8 {
    /// Creates an `SrgbMonoA8` pixel. `v` is sRGB-encoded (gamma); `a` is linear alpha.
    pub fn new(v: u8, a: u8) -> Self {
        SrgbMonoA8 {
            v: Saturating(v),
            a: Saturating(a),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// sRGB gamma-encoded 16-bit pixel types
//
// Same memory layout as Rgb16 / Rgba16 / Mono16 / MonoA16 but representing
// sRGB-encoded values.  These types intentionally do NOT implement
// `LinearPixel` or `LinearSpace`, so they cannot be passed to algorithms
// that assume linear light (e.g. bilinear resize).  Convert to linear with
// the `SrgbGamma` strategy first.
//
// 16-bit sRGB PNGs are not rare: any 16-bit PNG exported from Photoshop,
// GIMP, Lightroom, Krita, or darktable in an sRGB working space carries
// sRGB/iCCP metadata.  The type encodes the transfer function so the
// compiler can enforce correct usage.
// ═══════════════════════════════════════════════════════════════════════════════

/// sRGB-encoded RGB pixel with 16-bit depth per channel.
///
/// This type has the same memory layout as [`Rgb16`](crate::pixel::Rgb16) (three `Saturating<u16>`
/// in R, G, B order) but represents **gamma-encoded** sRGB values.
///
/// It does **not** implement [`LinearPixel`](crate::pixel::LinearPixel) or [`LinearSpace`](crate::pixel::LinearSpace), so the
/// compiler will reject attempts to use it with interpolation algorithms
/// like bilinear resize.  Convert to linear light first using the
/// `SrgbGamma` conversion strategy:
///
/// ```
/// # use fovea::pixel::{Srgb16, RgbF32};
/// # use fovea::transform::{ConvertPixel, SrgbGamma};
/// let srgb = Srgb16::new(32768, 16384, 65535);
/// let linear: RgbF32 = SrgbGamma.convert(&srgb);
/// ```
#[repr(C)]
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    PlainPixel,
    HomogeneousPixel,
    ZeroablePixel,
    WhiteChannel,
)]
pub struct Srgb16 {
    /// Red channel (sRGB-encoded).
    pub r: Saturating<u16>,
    /// Green channel (sRGB-encoded).
    pub g: Saturating<u16>,
    /// Blue channel (sRGB-encoded).
    pub b: Saturating<u16>,
}

impl Srgb16 {
    /// Creates an `Srgb16` pixel from the given 16-bit sRGB-encoded R, G, B channel values.
    pub fn new(r: u16, g: u16, b: u16) -> Self {
        Srgb16 {
            r: Saturating(r),
            g: Saturating(g),
            b: Saturating(b),
        }
    }
}

/// sRGB-encoded RGBA pixel with 16-bit depth per channel.
///
/// This type has the same memory layout as [`Rgba16`](crate::pixel::Rgba16) (four `Saturating<u16>`
/// in R, G, B, A order) but the R, G, B channels represent **gamma-encoded**
/// sRGB values.  The alpha channel is always linear, as required by the sRGB
/// specification.
///
/// It does **not** implement [`LinearPixel`](crate::pixel::LinearPixel) or [`LinearSpace`](crate::pixel::LinearSpace).  Convert to
/// linear light first using the `SrgbGamma` conversion strategy:
///
/// ```
/// # use fovea::pixel::{Srgba16, RgbaF32};
/// # use fovea::transform::{ConvertPixel, SrgbGamma};
/// let srgb = Srgba16::new(32768, 16384, 65535, 65535);
/// let linear: RgbaF32 = SrgbGamma.convert(&srgb);
/// ```
#[repr(C)]
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    PlainPixel,
    HomogeneousPixel,
    ZeroablePixel,
    WhiteChannel,
)]
pub struct Srgba16 {
    /// Red channel (sRGB-encoded).
    pub r: Saturating<u16>,
    /// Green channel (sRGB-encoded).
    pub g: Saturating<u16>,
    /// Blue channel (sRGB-encoded).
    pub b: Saturating<u16>,
    /// Alpha channel (linear).
    pub a: Saturating<u16>,
}

impl Srgba16 {
    /// Creates an `Srgba16` pixel. R, G, B are 16-bit sRGB-encoded (gamma); A is linear.
    pub fn new(r: u16, g: u16, b: u16, a: u16) -> Self {
        Srgba16 {
            r: Saturating(r),
            g: Saturating(g),
            b: Saturating(b),
            a: Saturating(a),
        }
    }
}

/// sRGB-encoded grayscale pixel with 16-bit depth.
///
/// This type has the same memory layout as [`Mono16`](crate::pixel::Mono16) (a single
/// `Saturating<u16>`) but represents a **gamma-encoded** sRGB value.
///
/// It does **not** implement [`LinearPixel`](crate::pixel::LinearPixel) or [`LinearSpace`](crate::pixel::LinearSpace), so the
/// compiler will reject attempts to use it with interpolation algorithms
/// like bilinear resize.  Convert to linear light first using the
/// `SrgbGamma` conversion strategy:
///
/// ```
/// # use fovea::pixel::{SrgbMono16, MonoF32};
/// # use fovea::transform::{ConvertPixel, SrgbGamma};
/// let srgb = SrgbMono16::new(32768);
/// let linear: MonoF32 = SrgbGamma.convert(&srgb);
/// ```
///
/// # Why "sRGB" for grayscale?
///
/// See [`SrgbMono8`] — the sRGB transfer function is defined per-channel
/// and applies identically to a single-channel image.
#[repr(transparent)]
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    Ord,
    PartialOrd,
    PlainPixel,
    HomogeneousPixel,
    ZeroablePixel,
    WhiteChannel,
)]
pub struct SrgbMono16(pub Saturating<u16>);

impl SrgbMono16 {
    /// Creates an `SrgbMono16` pixel with the given sRGB-encoded 16-bit value.
    pub fn new(value: u16) -> Self {
        SrgbMono16(Saturating(value))
    }
}

/// sRGB-encoded grayscale-with-alpha pixel, 16-bit depth per channel.
///
/// This type has the same memory layout as [`MonoA16`](crate::pixel::MonoA16) (two `Saturating<u16>`
/// in V, A order) but the value channel represents a **gamma-encoded** sRGB
/// intensity.  The alpha channel is always linear, as required by the sRGB
/// specification.
///
/// It does **not** implement [`LinearPixel`](crate::pixel::LinearPixel) or [`LinearSpace`](crate::pixel::LinearSpace), so the
/// compiler will reject attempts to use it with interpolation algorithms
/// like bilinear resize.  Convert to linear light first using the
/// `SrgbGamma` conversion strategy.
///
/// # Why "sRGB" for grayscale?
///
/// See [`SrgbMono8`] — the sRGB transfer function is defined per-channel
/// and applies identically to a single-channel image.
#[repr(C)]
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    PlainPixel,
    HomogeneousPixel,
    ZeroablePixel,
    WhiteChannel,
)]
pub struct SrgbMonoA16 {
    /// Value (luminance) channel (sRGB-encoded).
    pub v: Saturating<u16>,
    /// Alpha channel (linear).
    pub a: Saturating<u16>,
}

impl SrgbMonoA16 {
    /// Creates an `SrgbMonoA16` pixel. `v` is 16-bit sRGB-encoded (gamma); `a` is linear alpha.
    pub fn new(v: u16, a: u16) -> Self {
        SrgbMonoA16 {
            v: Saturating(v),
            a: Saturating(a),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// sRGB gamma-encoded BGR-order pixel types (OpenCV interop)
//
// Same memory layout as Bgr8 / Bgra8 / Bgr16 / Bgra16 (blue first) but
// representing sRGB-encoded values.  This is the exact combination produced by
// OpenCV's `cv::imread`: gamma-encoded sRGB samples stored in B, G, R channel
// order.  Neither `Bgr8` (linear, wrong color space) nor `Srgb8` (gamma but RGB
// order) models it, so wrapping an OpenCV buffer as either lies about the data.
//
// Like the other sRGB types, these intentionally do NOT implement `LinearPixel`
// or `LinearSpace`: the compiler rejects passing them to interpolation
// algorithms.  Reorder to the RGB-order sRGB family with the `ColorSwap`
// strategy, or decode straight to linear-light BGR float with `SrgbGamma`.
// ═══════════════════════════════════════════════════════════════════════════════

/// sRGB-encoded BGR pixel with 8-bit depth per channel.
///
/// This type has the same memory layout as [`Bgr8`](crate::pixel::Bgr8) (three `Saturating<u8>`
/// in B, G, R order) but represents **gamma-encoded** sRGB values.  It models
/// the default output of OpenCV's `cv::imread` (`CV_8UC3`): sRGB-gamma samples
/// in BGR channel order.
///
/// It does **not** implement [`LinearPixel`](crate::pixel::LinearPixel) or [`LinearSpace`](crate::pixel::LinearSpace), so the
/// compiler will reject attempts to use it with interpolation algorithms like
/// bilinear resize.  Bridge to the rest of the pipeline with the `ColorSwap`
/// strategy (reorder to [`Srgb8`](crate::pixel::Srgb8), keeping the gamma intact) or decode
/// straight to linear-light BGR with `SrgbGamma`:
///
/// ```
/// # use fovea::pixel::{SrgbBgr8, Srgb8, BgrF32, RgbF32};
/// # use fovea::transform::{ConvertPixel, ConvertPixelExt, ColorSwap, SrgbGamma};
/// let bgr = SrgbBgr8::new(200, 64, 128); // B, G, R (as stored by OpenCV)
///
/// // Decode in place to linear-light BGR float:
/// let linear: BgrF32 = SrgbGamma.convert(&bgr);
///
/// // Or reorder to RGB-order sRGB, then decode to linear RGB (OpenCV → linear):
/// let rgb: RgbF32 = ColorSwap.then::<Srgb8, _>(SrgbGamma).convert(&bgr);
/// ```
#[repr(C)]
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    PlainPixel,
    HomogeneousPixel,
    ZeroablePixel,
    WhiteChannel,
)]
pub struct SrgbBgr8 {
    /// Blue channel (sRGB-encoded).
    pub b: Saturating<u8>,
    /// Green channel (sRGB-encoded).
    pub g: Saturating<u8>,
    /// Red channel (sRGB-encoded).
    pub r: Saturating<u8>,
}

impl SrgbBgr8 {
    /// Creates an `SrgbBgr8` pixel from the given 8-bit sRGB-encoded B, G, R channel values.
    ///
    /// Note: parameter order is B, G, R — matching the in-memory layout (and OpenCV).
    pub fn new(b: u8, g: u8, r: u8) -> Self {
        SrgbBgr8 {
            b: Saturating(b),
            g: Saturating(g),
            r: Saturating(r),
        }
    }
}

/// sRGB-encoded BGRA pixel with 8-bit depth per channel.
///
/// This type has the same memory layout as [`Bgra8`](crate::pixel::Bgra8) (four `Saturating<u8>`
/// in B, G, R, A order) but the B, G, R channels represent **gamma-encoded**
/// sRGB values.  The alpha channel is always linear, as required by the sRGB
/// specification.  It models OpenCV's `CV_8UC4` (sRGB-gamma BGRA) buffers.
///
/// It does **not** implement [`LinearPixel`](crate::pixel::LinearPixel) or [`LinearSpace`](crate::pixel::LinearSpace).  Reorder to
/// [`Srgba8`](crate::pixel::Srgba8) with `ColorSwap` or decode to linear-light [`BgraF32`](crate::pixel::BgraF32) with
/// the `SrgbGamma` conversion strategy:
///
/// ```
/// # use fovea::pixel::{SrgbBgra8, BgraF32};
/// # use fovea::transform::{ConvertPixel, SrgbGamma};
/// let bgra = SrgbBgra8::new(200, 64, 128, 255); // B, G, R, A
/// let linear: BgraF32 = SrgbGamma.convert(&bgra);
/// ```
#[repr(C)]
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    PlainPixel,
    HomogeneousPixel,
    ZeroablePixel,
    WhiteChannel,
)]
pub struct SrgbBgra8 {
    /// Blue channel (sRGB-encoded).
    pub b: Saturating<u8>,
    /// Green channel (sRGB-encoded).
    pub g: Saturating<u8>,
    /// Red channel (sRGB-encoded).
    pub r: Saturating<u8>,
    /// Alpha channel (linear).
    pub a: Saturating<u8>,
}

impl SrgbBgra8 {
    /// Creates an `SrgbBgra8` pixel. B, G, R are sRGB-encoded (gamma); A is linear.
    ///
    /// Note: parameter order is B, G, R, A — matching the in-memory layout (and OpenCV).
    pub fn new(b: u8, g: u8, r: u8, a: u8) -> Self {
        SrgbBgra8 {
            b: Saturating(b),
            g: Saturating(g),
            r: Saturating(r),
            a: Saturating(a),
        }
    }
}

/// sRGB-encoded BGR pixel with 16-bit depth per channel.
///
/// This type has the same memory layout as [`Bgr16`](crate::pixel::Bgr16) (three `Saturating<u16>`
/// in B, G, R order) but represents **gamma-encoded** sRGB values.  It models a
/// 16-bit OpenCV `CV_16UC3` buffer holding sRGB-gamma samples in BGR order.
///
/// It does **not** implement [`LinearPixel`](crate::pixel::LinearPixel) or [`LinearSpace`](crate::pixel::LinearSpace).  Reorder to
/// [`Srgb16`](crate::pixel::Srgb16) with `ColorSwap` or decode to linear-light [`BgrF32`](crate::pixel::BgrF32) with the
/// `SrgbGamma` conversion strategy:
///
/// ```
/// # use fovea::pixel::{SrgbBgr16, BgrF32};
/// # use fovea::transform::{ConvertPixel, SrgbGamma};
/// let bgr = SrgbBgr16::new(50000, 16384, 32768); // B, G, R
/// let linear: BgrF32 = SrgbGamma.convert(&bgr);
/// ```
#[repr(C)]
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    PlainPixel,
    HomogeneousPixel,
    ZeroablePixel,
    WhiteChannel,
)]
pub struct SrgbBgr16 {
    /// Blue channel (sRGB-encoded).
    pub b: Saturating<u16>,
    /// Green channel (sRGB-encoded).
    pub g: Saturating<u16>,
    /// Red channel (sRGB-encoded).
    pub r: Saturating<u16>,
}

impl SrgbBgr16 {
    /// Creates an `SrgbBgr16` pixel from the given 16-bit sRGB-encoded B, G, R channel values (B first).
    pub fn new(b: u16, g: u16, r: u16) -> Self {
        SrgbBgr16 {
            b: Saturating(b),
            g: Saturating(g),
            r: Saturating(r),
        }
    }
}

/// sRGB-encoded BGRA pixel with 16-bit depth per channel.
///
/// This type has the same memory layout as [`Bgra16`](crate::pixel::Bgra16) (four `Saturating<u16>`
/// in B, G, R, A order) but the B, G, R channels represent **gamma-encoded**
/// sRGB values.  The alpha channel is always linear, as required by the sRGB
/// specification.  It models a 16-bit OpenCV `CV_16UC4` sRGB-gamma BGRA buffer.
///
/// It does **not** implement [`LinearPixel`](crate::pixel::LinearPixel) or [`LinearSpace`](crate::pixel::LinearSpace).  Reorder to
/// [`Srgba16`](crate::pixel::Srgba16) with `ColorSwap` or decode to linear-light [`BgraF32`](crate::pixel::BgraF32) with
/// the `SrgbGamma` conversion strategy:
///
/// ```
/// # use fovea::pixel::{SrgbBgra16, BgraF32};
/// # use fovea::transform::{ConvertPixel, SrgbGamma};
/// let bgra = SrgbBgra16::new(50000, 16384, 32768, 65535); // B, G, R, A
/// let linear: BgraF32 = SrgbGamma.convert(&bgra);
/// ```
#[repr(C)]
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    PlainPixel,
    HomogeneousPixel,
    ZeroablePixel,
    WhiteChannel,
)]
pub struct SrgbBgra16 {
    /// Blue channel (sRGB-encoded).
    pub b: Saturating<u16>,
    /// Green channel (sRGB-encoded).
    pub g: Saturating<u16>,
    /// Red channel (sRGB-encoded).
    pub r: Saturating<u16>,
    /// Alpha channel (linear).
    pub a: Saturating<u16>,
}

impl SrgbBgra16 {
    /// Creates an `SrgbBgra16` pixel. B, G, R are 16-bit sRGB-encoded (gamma); A is linear.
    pub fn new(b: u16, g: u16, r: u16, a: u16) -> Self {
        SrgbBgra16 {
            b: Saturating(b),
            g: Saturating(g),
            r: Saturating(r),
            a: Saturating(a),
        }
    }
}

// ---------------------------------------------------------------------------
// OriginInvariantPixel impls
// ---------------------------------------------------------------------------
//
// The sRGB gamma encoding is a per-pixel value transform, not a
// coordinate-dependent one: a gamma-encoded sample means the same thing
// wherever it sits, so cropping preserves its meaning. (These types still
// reject *interpolation* by withholding `LinearSpace`; origin-invariance and
// linear-space membership are independent axes — Philosophy §2.)
impl_origin_invariant_pixel!(
    Srgb8,
    Srgba8,
    SrgbMono8,
    SrgbMonoA8,
    Srgb16,
    Srgba16,
    SrgbMono16,
    SrgbMonoA16,
    SrgbBgr8,
    SrgbBgra8,
    SrgbBgr16,
    SrgbBgra16,
);