use kallo_spectral::Spectrum;
use crate::scan::solve3;
use crate::spectral::GRID_LEN;
#[derive(Debug, Clone, PartialEq)]
pub struct ExposureModel {
pub reconstruct: [[f64; 3]; 3],
pub basis: [[f64; GRID_LEN]; 3],
pub film: [[f64; GRID_LEN]; 3],
pub reference: [f64; 3],
}
#[derive(Debug, Clone, PartialEq)]
pub struct PrintModel {
pub analytical: [[f64; 3]; 3],
pub clamped: bool,
pub dye: [[f64; GRID_LEN]; 3],
pub paper: [[f64; GRID_LEN]; 3],
pub illuminant: [f64; GRID_LEN],
pub mask: [f64; GRID_LEN],
pub masking: [f64; 3],
pub masking_reference: f64,
pub filters: [f64; 3],
pub filter_bands: [[f64; GRID_LEN]; 3],
pub reference: [f64; 3],
}
#[derive(Debug, Clone, PartialEq)]
pub struct ScanModel {
pub analytical: [[f64; 3]; 3],
pub clamped: bool,
pub dye: [[f64; GRID_LEN]; 3],
pub illuminant: [f64; GRID_LEN],
pub cmf: [[f64; GRID_LEN]; 3],
pub matrix: [[f64; 3]; 3],
pub white_xyz: [f64; 3],
pub white_rgb: [f64; 3],
pub neutral: [[f64; 3]; NEUTRAL_ENTRIES],
pub neutral_max_density: f64,
pub base_density: f64,
}
pub const NEUTRAL_ENTRIES: usize = crate::scan::NEUTRAL_ENTRIES;
#[derive(Debug, Clone, PartialEq)]
pub struct SpectralModel {
pub scalar: crate::pipeline::ScalarModel,
pub step_nm: f64,
pub exposure: Option<ExposureModel>,
pub enlarger: Option<PrintModel>,
pub scan: Option<ScanModel>,
}
impl SpectralModel {
#[must_use]
pub fn is_spectral(&self) -> bool {
self.exposure.is_some() || self.enlarger.is_some() || self.scan.is_some()
}
}
pub(crate) fn inverse3(matrix: [[f64; 3]; 3]) -> Option<[[f64; 3]; 3]> {
let mut inverse = [[0.0_f64; 3]; 3];
for column in 0..3 {
let mut unit = [0.0_f64; 3];
unit[column] = 1.0;
let solved = solve3(matrix, unit)?;
for (row, entries) in inverse.iter_mut().enumerate() {
entries[column] = solved[row];
}
}
Some(inverse)
}
pub(crate) fn table(spectrum: &Spectrum) -> Option<[f64; GRID_LEN]> {
spectrum.values().try_into().ok()
}
pub(crate) fn tables(spectra: &[Spectrum; 3]) -> Option<[[f64; GRID_LEN]; 3]> {
Some([
table(&spectra[0])?,
table(&spectra[1])?,
table(&spectra[2])?,
])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_inverse_multiplies_back_to_the_identity() {
let matrix = [[2.0, -1.0, 0.5], [0.0, 3.0, -2.0], [1.0, 1.0, 1.0]];
let inverse = inverse3(matrix).expect("the test matrix is invertible");
for (row, products) in matrix.iter().enumerate() {
for (column, inverse_column) in [0, 1, 2].iter().enumerate() {
let sum: f64 = products
.iter()
.zip(&inverse)
.map(|(left, right)| left * right[*inverse_column])
.sum();
let expected = if row == column { 1.0 } else { 0.0 };
assert!((sum - expected).abs() < 1e-12, "{row},{column}: {sum}");
}
}
}
#[test]
fn a_singular_matrix_has_no_inverse() {
let matrix = [[1.0, 2.0, 3.0], [2.0, 4.0, 6.0], [0.0, 1.0, 1.0]];
assert!(inverse3(matrix).is_none());
}
#[test]
fn a_plain_render_has_a_scalar_model_and_no_spectral_one() {
let prepared = crate::pipeline::RenderParams::default().prepare();
assert!(prepared.scalar_model().is_some());
assert!(prepared.spectral_model().is_none());
}
}