use crate::curve::CharacteristicCurve;
use crate::spectral::GRID_LEN;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FilmProcess {
Negative,
Reversal,
Monochrome,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Provenance {
pub publication: &'static str,
pub page: &'static str,
pub retrieved: &'static str,
pub checksum: &'static str,
}
impl Provenance {
pub const UNKNOWN: Self = Self {
publication: "",
page: "",
retrieved: "",
checksum: "",
};
}
#[derive(Debug, Clone, Copy)]
pub struct FilmStock {
pub id: &'static str,
pub short: &'static str,
pub process: FilmProcess,
pub extra_cyan: Option<crate::dye::Emulsion>,
pub cl_layer: Option<crate::dye::InhibitorLayer>,
pub label: &'static str,
pub note: &'static str,
pub channels: [CharacteristicCurve; 3],
pub base_density: [f64; 3],
pub grains_per_um2: [f64; 3],
pub layers: &'static [[f64; GRID_LEN]; 3],
pub dyes: &'static [[f64; GRID_LEN]; 3],
pub mask: &'static [f64; GRID_LEN],
pub sigma_um: Option<[f64; 3]>,
pub reference_time_min: f64,
pub reference_temp_c: f64,
pub provenance: Provenance,
pub measured: &'static [(&'static str, &'static str)],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PaperEmulsion {
Colour,
Silver,
}
#[derive(Debug, Clone, Copy)]
pub struct PaperStock {
pub id: &'static str,
pub short: &'static str,
pub emulsion: PaperEmulsion,
pub label: &'static str,
pub note: &'static str,
pub channel: CharacteristicCurve,
pub target_mid_reflectance: f64,
pub dyes: &'static [[f64; GRID_LEN]; 3],
pub layers: &'static [[f64; GRID_LEN]; 3],
pub provenance: Provenance,
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)
}
include!(concat!(env!("OUT_DIR"), "/film_stocks.rs"));
#[must_use]
pub fn film_stock(id: &str) -> Option<&'static FilmStock> {
FILM_STOCKS.iter().find(|stock| stock.id == 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;
#[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:?}"
);
}
#[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
),
}
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
),
}
}
}
#[test]
fn every_film_carries_its_own_three_spectra() {
for stock in FILM_STOCKS {
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
);
}
}
}
#[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
);
}
}
}
#[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]
);
}
}
#[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;
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}"
);
}
}
#[test]
fn film_curves_start_at_zero_density() {
for stock in FILM_STOCKS {
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
);
}
}
}
}
#[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
);
}
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"
);
}
#[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");
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}");
}
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}"
);
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());
}
#[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);
}
}
}