fovea 0.1.1

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
//! 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;

// ═══════════════════════════════════════════════════════════════════════════════
// 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 {
    pub r: Saturating<u8>,
    pub g: Saturating<u8>,
    pub b: Saturating<u8>,
}

impl Srgb8 {
    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 {
    pub r: Saturating<u8>,
    pub g: Saturating<u8>,
    pub b: Saturating<u8>,
    pub a: Saturating<u8>,
}

impl Srgba8 {
    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 {
    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 {
    pub v: Saturating<u8>,
    pub a: Saturating<u8>,
}

impl SrgbMonoA8 {
    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 {
    pub r: Saturating<u16>,
    pub g: Saturating<u16>,
    pub b: Saturating<u16>,
}

impl Srgb16 {
    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 {
    pub r: Saturating<u16>,
    pub g: Saturating<u16>,
    pub b: Saturating<u16>,
    pub a: Saturating<u16>,
}

impl Srgba16 {
    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 {
    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 {
    pub v: Saturating<u16>,
    pub a: Saturating<u16>,
}

impl SrgbMonoA16 {
    pub fn new(v: u16, a: u16) -> Self {
        SrgbMonoA16 {
            v: Saturating(v),
            a: Saturating(a),
        }
    }
}