kallo-core 0.1.0

Reference film model: characteristic curves and development kinetics
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
//! Built-in film and paper stocks.
//!
//! Every film in `data/films/` and every paper in `data/papers/` is here, one
//! entry each, generated at build time from the `.kalofilm` and `.kalopaper`
//! files the renderer can also read one at a time. A stock is not a summary of
//! its sheet: it carries what the fields below say it carries, and it carries
//! what the sheet admitted to in [`FilmStock::measured`] — one line per field,
//! saying whether that field was measured, borrowed from another stock, or left
//! as a compiled placeholder. A product whose whole claim is that the numbers
//! mean something has to be able to say where each one came from.
//!
//! ## A stock carries its own spectra
//!
//! A film stock is not only a curve. It is also which wavelengths each layer
//! records ([`FilmStock::layers`]), what the negative absorbs at print time
//! ([`FilmStock::dyes`]) and the mask between the two ([`FilmStock::mask`]).
//! Those three are what make two films with the same contrast look like
//! different products, and they ride on the stock so that choosing a film
//! changes the colour and not only the density.
//!
//! Not every sheet prints three dyes, so some stocks borrow the measured VISION3
//! spectra — and [`FilmStock::measured`] says so, which is the honest version of
//! a borrow: the number is real, it is simply not this film's.
//!
//! The ids name the real product. A stock called `portra400` that is not Portra
//! 400 would be a lie, and the whole point of this project is that the numbers
//! mean something.
//!
//! ## What `d_min` means, and why it is zero for film
//!
//! A colour negative's minimum density is **not part of its characteristic
//! curve**. It is base, fog and — the part that matters — the orange mask: a
//! coloured coupler that absorbs blue and is consumed where cyan dye forms. It
//! is spectrally structured, so the model carries it as a spectrum
//! ([`FilmStock::mask`]), not as a scalar under the curve.
//!
//! The curve therefore describes how much *dye* has formed, which is zero where
//! nothing was exposed. [`FilmStock::base_density`] is the same minimum density
//! expressed the way the non-spectral print needs it: one status density per
//! layer. The two are not independent numbers — the mask's density at the status
//! filter peaks is this base, and a test checks it.
//!
//! Paper is the exception and keeps its `d_min`: unexposed paper has a density
//! of its own, it has no mask, and there is no second place to put it.

use crate::curve::CharacteristicCurve;
use crate::spectral::GRID_LEN;

/// Which chemistry the stock is, and therefore which stages the render runs.
///
/// A negative is developed, printed onto paper and scanned; a reversal is
/// developed to a positive and scanned directly, with no print between; a silver
/// image has one curve and one sensitivity, no dye and no mask. The stock
/// declares which one it is, and the pipeline follows.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FilmProcess {
    /// Colour negative: developed, printed onto paper, scanned.
    Negative,
    /// Colour reversal: the film is the picture, and there is no print stage.
    Reversal,
    /// Silver image: one curve, one sensitivity, no dye, no mask.
    Monochrome,
}
/// Where a stock's numbers came from.
///
/// A data sheet, a printed page, a date, and the digest of the document itself.
/// It is not decoration: a curve that cannot name its sheet is a curve someone
/// drew, and the difference is the whole product.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Provenance {
    /// The publication, as the manufacturer numbers it: `E-4050`, `AF3-865E`.
    pub publication: &'static str,
    /// The printed page the numbers were read on, as the sheet numbers it.
    pub page: &'static str,
    /// The date the sheet was read, ISO 8601.
    pub retrieved: &'static str,
    /// The digest of the document, `sha256:...`.
    pub checksum: &'static str,
}

impl Provenance {
    /// A stock with no sheet behind it: a test, or a file that did not say.
    pub const UNKNOWN: Self = Self {
        publication: "",
        page: "",
        retrieved: "",
        checksum: "",
    };
}

/// A film stock: one characteristic curve per colour layer.
#[derive(Debug, Clone, Copy)]

pub struct FilmStock {
    /// Stable identifier used on the command line.
    pub id: &'static str,
    /// The name a box or a wall label would use, without the catalogue numbers:
    /// `PORTRA 400`, not `KODAK PROFESSIONAL PORTRA 400, C-41`.
    pub short: &'static str,
    /// The chemistry, and therefore which stages the render runs.
    pub process: FilmProcess,
    /// A second emulsion feeding the cyan dye, if the film has one.
    ///
    /// ADR 0017: a dye may be formed by more than one emulsion, and the
    /// four-layer negatives are the reason. `None` — every stock before those —
    /// is exactly the three-layer model.
    ///
    /// `Some` is not a scalar rendering: it forces the spectral path, because a
    /// fourth layer is a fourth integral, and it makes the GPU decline rather
    /// than replay three layers of a four-layer film.
    pub extra_cyan: Option<crate::dye::Emulsion>,
    /// A cyan-sensitive layer that releases inhibitor instead of dye, if the
    /// film has one — ADR 0018.
    ///
    /// It is not a dye: it forms no density, and its effect on the measured
    /// curve is folded into that curve. Only a frame with structure in it can
    /// show the layer at all, which is why nothing happens unless a render asks
    /// for [`crate::interimage::InterImage`].
    pub cl_layer: Option<crate::dye::InhibitorLayer>,
    /// Human-readable name.
    pub label: &'static str,
    /// What this stock is, and what it is not.
    pub note: &'static str,
    /// Red, green and blue layer curves, in dye formed: `d_min` is the fog above
    /// the base, and zero at a normal development.
    pub channels: [CharacteristicCurve; 3],
    /// Status densities of the unexposed, processed film: base, fog and mask.
    ///
    /// Used by the non-spectral print, which has no spectrum to put there. The
    /// spectral print uses the measured mask instead, and the two agree.
    pub base_density: [f64; 3],
    /// Effective developed grains per square micrometre, per layer.
    ///
    /// "Effective" is doing work in that sentence: a developed image's noise is
    /// dominated by clumping, so the number is far below a count of silver
    /// halide grains. It is whatever reproduces the manufacturer's granularity
    /// curve, and [`crate::GrainField::for_stock`] is where it goes.
    pub grains_per_um2: [f64; 3],
    /// The layer sensitivities on one relative scale, 380–730 nm at 5 nm.
    ///
    /// Which wavelengths reach each layer, and how fast each layer is. This is
    /// the film's *record* of colour: two stocks with the same curves but
    /// different layers separate a scene differently.
    pub layers: &'static [[f64; GRID_LEN]; 3],
    /// The cyan, magenta and yellow dyes, peak-normalised absorption.
    ///
    /// The film's *return* of colour: what the negative passes at print time.
    /// The published curves have D-min subtracted, so the mask is separate.
    pub dyes: &'static [[f64; GRID_LEN]; 3],
    /// Spectral density of base, fog and the orange mask, 380–730 nm at 5 nm.
    pub mask: &'static [f64; GRID_LEN],
    /// The emulsion's blur, measured from the stock's own MTF, per layer.
    ///
    /// `None` means the sheet did not publish a modulation-transfer curve and
    /// the render falls back to the compiled VISION3 fit — a borrow, and the
    /// stock's `note` says so.
    pub sigma_um: Option<[f64; 3]>,
    /// The development the characteristic curve was measured at, in minutes.
    ///
    /// Every sheet draws its curves at a stated development, and the model's
    /// default is not it: DELTA 400's curve is 8 minutes at 24 °C, most C-41
    /// sheets are 3¼ at 38 °C, and the model's own default is 6.5 at 20 °C.
    /// Rendering a stock at a development it was not measured at is a contrast
    /// factor the user did not ask for, so the reference rides on the stock and
    /// `--dev` is what moves away from it.
    pub reference_time_min: f64,
    /// The temperature the curve was measured at, in °C.
    pub reference_temp_c: f64,
    /// The sheet this film was read from.
    pub provenance: Provenance,
    /// One line per field: what was measured, what was borrowed, what is a
    /// placeholder, and what is absent.
    ///
    /// The order is the model's — the curve first, the grain last — rather than
    /// the file's, because a person reading a film's record wants the film's
    /// shape before its texture. Every sentence begins with its own verdict:
    /// `measured: ...`, `borrowed: ...`, `placeholder: ...`, `none`.
    pub measured: &'static [(&'static str, &'static str)],
}

/// What a paper carries.
///
/// A colour paper has three dye-forming layers and records colour; a silver
/// paper has one silver image, no dyes, and records luminance. It is the
/// paper's own fact, not a rendering preference: a colour film printed on a
/// silver paper is a black-and-white print, and a paper that cannot say which
/// it is cannot be printed honestly.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PaperEmulsion {
    /// Three dye-forming layers, RA-4: the print carries colour.
    Colour,
    /// One silver image, no dyes: the print carries luminance.
    Silver,
}

/// A paper stock: one curve, because paper only has to record what is on the
/// negative, not sample a scene.
#[derive(Debug, Clone, Copy)]
pub struct PaperStock {
    /// Stable identifier used on the command line.
    pub id: &'static str,
    /// The name a box or a wall label would use, without the catalogue numbers.
    pub short: &'static str,
    /// What the paper carries: three dye-forming layers, or one silver image.
    pub emulsion: PaperEmulsion,
    /// Human-readable name.
    pub label: &'static str,
    /// What this paper is, and what it is not.
    pub note: &'static str,
    /// The paper's characteristic curve.
    pub channel: CharacteristicCurve,
    /// Linear reflectance a correctly balanced print should reach for an
    /// 18 % grey card. This is what the print balance is solved against.
    pub target_mid_reflectance: f64,
    /// The paper's cyan, magenta and yellow dyes, peak-normalised absorption,
    /// 380–730 nm at 5 nm. What the spectral scan puts between the light and
    /// the eye.
    pub dyes: &'static [[f64; GRID_LEN]; 3],
    /// The paper's three layer sensitivities on one relative scale, 380–730 nm
    /// at 5 nm. What the spectral print integrates the enlarger's light against.
    pub layers: &'static [[f64; GRID_LEN]; 3],
    /// The sheet this paper was read from.
    pub provenance: Provenance,
    /// One line per field, as a film's: measured, borrowed, placeholder or none.
    pub measured: &'static [(&'static str, &'static str)],
}

const fn curve(d_min: f64, d_max: f64, gamma: f64, log_e0: f64) -> CharacteristicCurve {
    CharacteristicCurve::new(d_min, d_max, gamma, log_e0)
}

// The film stocks are generated from the `.kalofilm` files at build time: the
// data is CC BY-SA 4.0 and this program text is MIT or Apache-2.0, and `build.rs` is
// where the two meet. Nothing in this file is a digitised number.
include!(concat!(env!("OUT_DIR"), "/film_stocks.rs"));

// The paper stocks are generated from the `.kalopaper` files the same way the
// films are; see `build.rs`.

/// Look up a film stock by id.
#[must_use]
pub fn film_stock(id: &str) -> Option<&'static FilmStock> {
    FILM_STOCKS.iter().find(|stock| stock.id == id)
}

/// Look up a paper stock by id.
#[must_use]
pub fn paper_stock(id: &str) -> Option<&'static PaperStock> {
    PAPER_STOCKS.iter().find(|stock| stock.id == id)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::grain::GrainField;

    /// The second film is not the first one with another curve.
    ///
    /// Its layer sensitivities were digitised from its own data sheet, and its
    /// curves turn their corners at rates of their own. A stock that borrowed
    /// the first film's tables would render the same colour with a different
    /// contrast, which is the thing this field exists to prevent.
    #[test]
    fn the_second_film_has_its_own_layers() {
        let fuji = film_stock("fuji_nph400").expect("built-in stock");
        let kodak = film_stock("vision3_500t").expect("built-in stock");
        assert_ne!(
            (
                fuji.channels[0].toe_transition,
                fuji.channels[0].shoulder_transition
            ),
            (
                kodak.channels[0].toe_transition,
                kodak.channels[0].shoulder_transition
            ),
            "the two films fit the same corner rates"
        );
        let peak = |table: &[f64; GRID_LEN]| table.iter().copied().fold(0.0_f64, f64::max);
        let ratios: [f64; 3] =
            std::array::from_fn(|layer| peak(&fuji.layers[layer]) / peak(&kodak.layers[layer]));
        assert!(
            ratios.iter().any(|ratio| (ratio - 1.0).abs() > 0.05),
            "the two films share their layer tables: {ratios:?}"
        );
    }

    /// What a stock says about a field matches what it carries.
    ///
    /// The catalogue's value is that its claims are checkable, so they are
    /// checked. A stock that said `none` about its mask while shipping another
    /// film's — which seven of them did until this test existed — is worse than
    /// one that said nothing at all, because a reader believes it.
    ///
    /// The claims are read the way `kalo stocks --json` reads them: the word
    /// before the first colon.
    #[test]
    fn the_claims_match_the_tables() {
        fn claim<'a>(stock: &'a FilmStock, field: &str) -> &'a str {
            stock
                .measured
                .iter()
                .find(|(name, _)| *name == field)
                .map(|(_, text)| text.split(':').next().unwrap_or("").trim())
                .unwrap_or_else(|| panic!("{} says nothing about its {field}", stock.id))
        }

        let reference_mask: &[f64] = FILM_MASK.as_slice();
        let reference_dyes: &[[f64; GRID_LEN]] = FILM_DYES.as_slice();
        for stock in FILM_STOCKS {
            for field in ["curves", "layers", "dyes", "mask", "mtf", "grain"] {
                let verdict = claim(stock, field);
                assert!(
                    matches!(verdict, "measured" | "borrowed" | "placeholder" | "none"),
                    "{}'s {field} begins with {verdict:?}",
                    stock.id
                );
            }

            let masks = stock.mask.as_slice();
            let mask_empty = masks.iter().all(|value| *value == 0.0);
            match claim(stock, "mask") {
                "none" => assert!(
                    mask_empty,
                    "{} says it has no mask and carries one",
                    stock.id
                ),
                "borrowed" => assert!(
                    masks == reference_mask,
                    "{} says its mask is borrowed and it is not the reference",
                    stock.id
                ),
                _ => assert!(
                    !mask_empty,
                    "{} says its mask is measured and has none",
                    stock.id
                ),
            }

            let dyes = stock.dyes.as_slice();
            let dyes_empty = dyes.iter().flatten().all(|value| *value == 0.0);
            match claim(stock, "dyes") {
                "none" => assert!(
                    dyes_empty,
                    "{} says it has no dyes and carries some",
                    stock.id
                ),
                "borrowed" => assert!(
                    dyes == reference_dyes,
                    "{} says its dyes are borrowed and they are not the reference",
                    stock.id
                ),
                _ => assert!(
                    !dyes_empty,
                    "{} says its dyes are measured and has none",
                    stock.id
                ),
            }

            // The render blurs every stock: a stock whose sheet published no MTF
            // runs on the compiled VISION3 fit, so `none` is a claim the model
            // contradicts — the honest word for a sheet without an MTF is
            // `borrowed`, and the note has to say what is borrowed.
            match claim(stock, "mtf") {
                "measured" => assert!(
                    stock.sigma_um.is_some(),
                    "{} says its blur is measured and carries none",
                    stock.id
                ),
                "borrowed" => assert!(
                    stock.sigma_um.is_none(),
                    "{} says its blur is borrowed and carries its own",
                    stock.id
                ),
                other => panic!(
                    "{} claims {other:?} for its blur; a stock without an MTF \
                     borrows the measured VISION3 blur and has to say so",
                    stock.id
                ),
            }
        }
    }

    /// Every film carries three dyes, three layer sensitivities and a mask, on
    /// the grid the model integrates over, and none of them is negative.
    ///
    /// This is the check that catches a table pasted in at the wrong length or
    /// with a minus dropped: a stock with a truncated spectrum still renders,
    /// and it renders the wrong colour in a way nothing else would notice.
    #[test]
    fn every_film_carries_its_own_three_spectra() {
        for stock in FILM_STOCKS {
            // A silver film has no dyes; its record is the grain, and a table of
            // zeros is what "no dye absorption" looks like.
            if stock.process == FilmProcess::Monochrome {
                continue;
            }
            for (channel, table) in stock.dyes.iter().enumerate() {
                let peak = table.iter().copied().fold(0.0_f64, f64::max);
                assert!(
                    (peak - 1.0).abs() < 0.02,
                    "{} dye {channel} peaks at {peak:.3}, not at 1",
                    stock.id
                );
                assert!(
                    table.iter().all(|value| *value >= 0.0),
                    "{} dye {channel} has a negative density",
                    stock.id
                );
            }
            assert!(
                stock.mask.iter().all(|value| *value >= 0.0),
                "{} has a negative mask density",
                stock.id
            );
            for (channel, table) in stock.layers.iter().enumerate() {
                let peak = table.iter().copied().fold(0.0_f64, f64::max);
                assert!(
                    peak > 0.0,
                    "{} layer {channel} is dark everywhere",
                    stock.id
                );
                assert!(
                    table.iter().all(|value| *value >= 0.0),
                    "{} layer {channel} has a negative sensitivity",
                    stock.id
                );
            }
        }
    }

    /// Each colour layer records where its own sheet says it does.
    ///
    /// The failure this catches is a panel read the wrong way round. The three
    /// curves are labelled by the dye they form — Cyan-, Magenta- and
    /// Yellow-Forming — and an extractor that assigns them by position instead
    /// of by label can put the cyan layer's peak in the blue, produce three
    /// smooth plausible tables, and never fail anything else. GOLD 200's sheet
    /// draws four strokes for three layers, so the risk is not hypothetical.
    #[test]
    fn every_colour_layer_peaks_in_its_own_band() {
        for stock in FILM_STOCKS {
            if stock.process == FilmProcess::Monochrome {
                continue;
            }
            for (layer, low, high) in [(0, 590.0, 700.0), (1, 490.0, 600.0), (2, 380.0, 500.0)] {
                let (index, value) = stock.layers[layer].iter().enumerate().fold(
                    (0, f64::NEG_INFINITY),
                    |best, (i, &v)| {
                        if v > best.1 { (i, v) } else { best }
                    },
                );
                let wavelength = 380.0 + 5.0 * index as f64;
                assert!(
                    (low..=high).contains(&wavelength),
                    "{} layer {layer} peaks at {wavelength} nm, outside {low}-{high}",
                    stock.id
                );
                assert!(
                    value > 0.0,
                    "{} layer {layer} is dark at its peak",
                    stock.id
                );
            }
        }
    }

    /// The measured stock's status base and the mask the spectral print adds are
    /// one measurement written two ways. If they ever drift apart, one of the two
    /// prints is wrong and the difference is a colour cast in the shadows.
    #[test]
    fn the_measured_stock_carries_the_mask_as_its_base() {
        let stock = film_stock("vision3_500t").expect("the measured stock");
        let mask = crate::measured::spectrum(&crate::measured::FILM_MASK);
        for (channel, wavelength) in [650.0, 550.0, 450.0].iter().enumerate() {
            let at = mask.interpolate(*wavelength);
            assert!(
                (stock.base_density[channel] - at).abs() < 0.06,
                "layer {channel}: base {:.4} against mask {at:.4}",
                stock.base_density[channel]
            );
        }
    }

    /// The granularity chart gives a *density* fluctuation; the model works in
    /// log exposure, and the curve's slope connects them. This is the round trip
    /// through the whole derivation: at a density of 1 through a 48 µm aperture,
    /// rms granularity has to land where the data sheet put it.
    #[test]
    fn the_measured_grain_reproduces_the_granularity_chart() {
        let stock = film_stock("vision3_500t").expect("the measured stock");
        let mut field = GrainField::for_stock(1, 1, 36.0, 0, stock);
        field.pixel_area_um2 = 48.0 * 48.0;
        // The chart's values at D = 1, in layer order R, G, B.
        for (layer, published) in [0.0060, 0.0077, 0.0197].into_iter().enumerate() {
            let sigma_d = stock.channels[layer].gamma * field.sigma(1.0, layer);
            assert!(
                (sigma_d - published).abs() < 0.2 * published,
                "layer {layer}: {sigma_d:.4} against the chart's {published:.4}"
            );
        }
    }

    /// A film curve is dye, and dye is zero where nothing was exposed. The base
    /// lives on the stock so that a spectral print can put the measured mask
    /// there instead of a scalar.
    #[test]
    fn film_curves_start_at_zero_density() {
        for stock in FILM_STOCKS {
            // A reversal is the exception, and not a small one: its unexposed
            // film is its densest state, so the curve starts at about 3.4 and
            // falls to clear. A negative's curve is dye above base and starts at
            // zero; a reversal's is the picture and starts at its maximum.
            let reversal = stock.process == FilmProcess::Reversal;
            for (layer, channel) in stock.channels.iter().enumerate() {
                if reversal {
                    assert!(
                        channel.d_min > channel.d_max,
                        "{} layer {layer}: reversal curve rises, d_min {} d_max {}",
                        stock.id,
                        channel.d_min,
                        channel.d_max
                    );
                } else {
                    assert!(
                        channel.d_min.abs() < 1e-9,
                        "{} layer {layer}: d_min {}",
                        stock.id,
                        channel.d_min
                    );
                }
            }
        }
    }

    /// Paper keeps its `d_min`, unlike film: unexposed paper has a density of
    /// its own and no mask to carry it. That `d_min` is the paper's white, and
    /// the three measured stocks have three different ones — 0.19 for the
    /// metallic sheet, 0.10 for the two E-4021 papers. The default is one of
    /// the latter, because a model whose prints cannot reflect more than 62 %
    /// is modelling a specialty, not a print.
    #[test]
    fn the_measured_papers_carry_their_white_as_the_toe() {
        let measured = [
            ("portra_endura", 0.0964, 7.0),
            ("supra_endura", 0.1010, 8.0),
            ("endura_premier_metallic", 0.1944, 10.0),
        ];
        for (id, toe, transition) in measured {
            let paper = paper_stock(id).unwrap_or_else(|| panic!("{id} is not a stock"));
            assert!(
                (paper.channel.d_min - toe).abs() < 1e-9,
                "{id}: d_min {} is not the measured toe {toe}",
                paper.channel.d_min
            );
            assert!(
                (paper.channel.toe_transition - transition).abs() < 1e-9,
                "{id}: transition {}, not the {transition} its sheet fitted",
                paper.channel.toe_transition
            );
        }

        // The default paper is a print: its white is around 80 % reflectance,
        // where the metallic stock's is around 62 %.
        let default = paper_stock("portra_endura").expect("the default paper");
        let white = 10.0_f64.powf(-default.channel.d_min);
        assert!(
            (0.7..0.9).contains(&white),
            "the default paper's white came out at {white}"
        );
        assert!(
            10.0_f64.powf(
                -paper_stock("endura_premier_metallic")
                    .unwrap()
                    .channel
                    .d_min
            ) < 0.7,
            "the metallic paper is not greyer than a print"
        );
    }

    /// The paper's spectra belong to the stock, not to the model: E-4021 draws
    /// one set for PORTRA and SUPRA ENDURA, the metallic sheet another, and the
    /// E-4021 paper is the default. If a stock were pointed at the wrong tables
    /// nothing would fail except a spectral render's colour — which is exactly
    /// the failure mode this project exists to make loud.
    #[test]
    fn the_measured_papers_carry_their_own_spectra() {
        let peak = |table: &[f64; crate::spectral::GRID_LEN]| {
            let (index, value) =
                table
                    .iter()
                    .enumerate()
                    .fold((0, f64::NEG_INFINITY), |best, (i, &v)| {
                        if v > best.1 { (i, v) } else { best }
                    });
            (380.0 + 5.0 * index as f64, value)
        };

        let portra = paper_stock("portra_endura").expect("the default paper");
        // The dye peaks the sheet draws: yellow 445, magenta 550, cyan 630 nm.
        for (layer, expected) in [(2, 445.0), (1, 550.0), (0, 630.0)] {
            let (wavelength, value) = peak(&portra.dyes[layer]);
            assert!(
                (wavelength - expected).abs() < 15.0,
                "layer {layer} peaks at {wavelength} nm, not {expected}"
            );
            assert!((value - 1.0).abs() < 0.02, "layer {layer} peaks at {value}");
        }
        // The layer sensitivities: yellow-forming at 475, magenta at 550, cyan
        // at 705, and they are on one scale, so their peaks differ by two orders.
        let yellow = peak(&portra.layers[2]).1;
        let cyan = peak(&portra.layers[0]).1;
        assert!(
            yellow > 20.0 * cyan,
            "the layer speeds came out {yellow:.2} and {cyan:.2}"
        );

        // And the metallic stock is a different paper, with different tables.
        let metallic = paper_stock("endura_premier_metallic").expect("the metallic paper");
        assert!(
            !std::ptr::eq(portra.dyes, metallic.dyes),
            "the two papers share one dye set"
        );
        assert!((peak(&metallic.dyes[0]).0 - peak(&portra.dyes[0]).0).abs() > 5.0);
    }

    #[test]
    fn ids_are_unique() {
        let mut ids: Vec<&str> = FILM_STOCKS.iter().map(|s| s.id).collect();
        ids.sort_unstable();
        let count = ids.len();
        ids.dedup();
        assert_eq!(ids.len(), count);

        let mut ids: Vec<&str> = PAPER_STOCKS.iter().map(|s| s.id).collect();
        ids.sort_unstable();
        let count = ids.len();
        ids.dedup();
        assert_eq!(ids.len(), count);
    }

    #[test]
    fn lookups_work() {
        assert!(film_stock("kodak_ultramax400").is_some());
        assert!(paper_stock("fuji_crystal_archive_pearl").is_some());
        assert!(film_stock("nope").is_none());
    }

    /// ADR 0017 is wired, and this is what keeps its blast radius visible: no
    /// stock in the tree declares a second cyan emulsion yet, so every stock
    /// renders through the three-layer path exactly as it did before.
    #[test]
    fn no_stock_has_a_second_cyan_emulsion_yet() {
        for stock in FILM_STOCKS {
            assert!(stock.extra_cyan.is_none(), "{} declares one", stock.id);
        }
    }
}