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
//! A baked lookup table for the spectral exposure.
//!
//! The hybrid reconstruction costs a non-negative least squares per pixel, which
//! is fine for a benchmark and hopeless for a 24 MP image. But the expensive part
//! produces only three numbers: the log exposure of each film layer. So the model
//! can be *distilled* into a three-dimensional table over RGB and interpolated
//! per pixel.
//!
//! The table is indexed on a **logarithmic** axis. Linear light spans several
//! decades between a shadow and a highlight, and a linear axis would spend most
//! of its resolution on values nobody photographs. Uniform steps in log10 give
//! uniform resolution in stops.

use std::path::{Path, PathBuf};

use crate::spectral::SpectralContext;

/// Magic bytes at the start of a baked table.
const MAGIC: &[u8; 4] = b"KLUT";
/// Format version. Bumped when the layout changes.
const VERSION: u16 = 2;
/// Channels per sample: the model has always produced three.
const CHANNELS: u16 = 3;
/// Largest side accepted on read, to bound allocation from a hostile file.
const MAX_SIZE: usize = 256;

/// A baked table could not be written or read.
#[derive(Debug)]
pub enum LutError {
    /// The file could not be opened, read or written.
    Io {
        /// The path that failed.
        path: PathBuf,
        /// The underlying OS error.
        source: std::io::Error,
    },
    /// The file does not start with `KLUT`.
    Magic,
    /// The file was written by a future or older layout.
    Version(u16),
    /// The declared side length is outside the accepted range.
    Size(u32),
    /// The sample count or channel count does not match the header.
    Layout,
    /// The payload does not match its checksum.
    Checksum,
    /// The table declares a provenance string longer than the header allows.
    ProvenanceTooLong,
}

impl std::fmt::Display for LutError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io { path, source } => write!(f, "could not access {}: {source}", path.display()),
            Self::Magic => write!(f, "not a Kallo lookup table"),
            Self::Version(version) => write!(f, "unsupported table version {version}"),
            Self::Size(size) => write!(f, "table side {size} is outside 2..={MAX_SIZE}"),
            Self::Layout => write!(f, "the table's declared layout does not match its size"),
            Self::Checksum => write!(f, "the table is corrupt (checksum mismatch)"),
            Self::ProvenanceTooLong => write!(f, "the table's provenance string is too long"),
        }
    }
}

impl std::error::Error for LutError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io { source, .. } => Some(source),
            _ => None,
        }
    }
}

fn crc32(bytes: &[u8]) -> u32 {
    let mut crc = 0xFFFF_FFFF_u32;
    for byte in bytes {
        crc ^= u32::from(*byte);
        for _ in 0..8 {
            let mask = (crc & 1).wrapping_neg();
            crc = (crc >> 1) ^ (0xEDB8_8320 & mask);
        }
    }
    !crc
}

/// log10 of the smallest input the table covers.
pub const LOG_RANGE_MIN: f64 = -4.0;
/// log10 of the largest input the table covers.
pub const LOG_RANGE_MAX: f64 = 0.602_06; // log10(4.0)

/// A cubed lookup table mapping linear RGB to per-layer log exposure.
#[derive(Debug, Clone)]
pub struct ExposureLut {
    size: usize,
    /// `size^3` entries, index order `(r, g, b)`.
    data: Vec<[f32; 3]>,
    /// Which model baked it: camera, film, grid. Descriptive and checkable.
    identity: String,
}

impl ExposureLut {
    /// Bake a table by sampling `context` on a `size^3` grid.
    ///
    /// A size of 33 gives about 36 k samples: a few hundred milliseconds in
    /// release, half a megabyte on disk as `f32`.
    ///
    /// # Panics
    ///
    /// Panics if `size < 2`.
    #[must_use]
    pub fn bake(size: usize, context: &SpectralContext) -> Self {
        assert!(
            size >= 2,
            "a lookup table needs at least two samples per axis"
        );
        let mut data = Vec::with_capacity(size * size * size);
        for r in 0..size {
            let red = decode(r, size);
            for g in 0..size {
                let green = decode(g, size);
                for b in 0..size {
                    let blue = decode(b, size);
                    let log_e = context
                        .expose([red, green, blue])
                        .unwrap_or([LOG_RANGE_MIN; 3]);
                    data.push([log_e[0] as f32, log_e[1] as f32, log_e[2] as f32]);
                }
            }
        }
        Self {
            size,
            data,
            identity: context.identity(),
        }
    }

    /// The model identity recorded at bake time.
    #[must_use]
    pub fn identity(&self) -> &str {
        &self.identity
    }

    /// Number of samples per axis.
    #[must_use]
    pub fn size(&self) -> usize {
        self.size
    }

    /// Bytes the table would occupy on disk as `f32`.
    #[must_use]
    pub fn byte_size(&self) -> usize {
        self.data.len() * 3 * std::mem::size_of::<f32>()
    }

    /// Write the table to `path` in the `.klut` format.
    ///
    /// Layout, little endian: magic (4), version (u16), side (u32),
    /// channels (u16), `side^3 * 3` f32, crc32 of everything before it.
    ///
    /// # Errors
    ///
    /// Returns [`LutError::Io`] if the file cannot be written.
    pub fn write(&self, path: impl AsRef<Path>) -> Result<(), LutError> {
        let path = path.as_ref();
        let identity = self.identity.as_bytes();
        let Ok(identity_len) = u16::try_from(identity.len()) else {
            return Err(LutError::ProvenanceTooLong);
        };

        let mut bytes = Vec::with_capacity(14 + identity.len() + self.data.len() * 12 + 4);
        bytes.extend_from_slice(MAGIC);
        bytes.extend_from_slice(&VERSION.to_le_bytes());
        bytes.extend_from_slice(&(self.size as u32).to_le_bytes());
        bytes.extend_from_slice(&CHANNELS.to_le_bytes());
        bytes.extend_from_slice(&identity_len.to_le_bytes());
        bytes.extend_from_slice(identity);
        for entry in &self.data {
            for value in entry {
                bytes.extend_from_slice(&value.to_le_bytes());
            }
        }
        let checksum = crc32(&bytes);
        bytes.extend_from_slice(&checksum.to_le_bytes());

        std::fs::write(path, bytes).map_err(|source| LutError::Io {
            path: path.to_path_buf(),
            source,
        })
    }

    /// Read a table written by [`Self::write`].
    ///
    /// # Errors
    ///
    /// Returns a [`LutError`] for a missing file, a foreign format, a future
    /// version, a size outside `2..=256`, a layout mismatch, or a bad checksum.
    pub fn read(path: impl AsRef<Path>) -> Result<Self, LutError> {
        let path = path.as_ref();
        let bytes = std::fs::read(path).map_err(|source| LutError::Io {
            path: path.to_path_buf(),
            source,
        })?;

        if bytes.len() < 18 || &bytes[0..4] != MAGIC {
            return Err(LutError::Magic);
        }
        let version = u16::from_le_bytes([bytes[4], bytes[5]]);
        if version != VERSION {
            return Err(LutError::Version(version));
        }
        let size = u32::from_le_bytes([bytes[6], bytes[7], bytes[8], bytes[9]]) as usize;
        if !(2..=MAX_SIZE).contains(&size) {
            return Err(LutError::Size(size as u32));
        }
        let channels = u16::from_le_bytes([bytes[10], bytes[11]]);
        if channels != CHANNELS {
            return Err(LutError::Layout);
        }

        let identity_len = u16::from_le_bytes([bytes[12], bytes[13]]) as usize;
        let payload_start = 14 + identity_len;
        let expected = payload_start + size * size * size * usize::from(CHANNELS) * 4;
        if bytes.len() != expected + 4 {
            return Err(LutError::Layout);
        }
        let stored = u32::from_le_bytes([
            bytes[expected],
            bytes[expected + 1],
            bytes[expected + 2],
            bytes[expected + 3],
        ]);
        if crc32(&bytes[..expected]) != stored {
            return Err(LutError::Checksum);
        }

        let identity = String::from_utf8_lossy(&bytes[14..payload_start]).into_owned();

        let mut data = Vec::with_capacity(size * size * size);
        for chunk in bytes[payload_start..expected].chunks_exact(12) {
            let mut entry = [0.0_f32; 3];
            for (index, value) in entry.iter_mut().enumerate() {
                let offset = index * 4;
                *value = f32::from_le_bytes([
                    chunk[offset],
                    chunk[offset + 1],
                    chunk[offset + 2],
                    chunk[offset + 3],
                ]);
            }
            data.push(entry);
        }

        Ok(Self {
            size,
            data,
            identity,
        })
    }

    /// Trilinear interpolation, clamped at the edges.
    #[must_use]
    pub fn sample(&self, rgb: [f64; 3]) -> [f64; 3] {
        let positions = rgb.map(|value| encode(value) * (self.size - 1) as f64);
        let lower = positions.map(|p| (p.floor() as usize).min(self.size - 1));
        let upper = lower.map(|l| (l + 1).min(self.size - 1));
        let weights = std::array::from_fn::<f64, 3, _>(|axis| {
            let p = positions[axis];
            let base = lower[axis] as f64;
            (p - base).clamp(0.0, 1.0)
        });

        let mut out = [0.0_f64; 3];
        for corner in 0..8 {
            let pick = |axis: usize| {
                if (corner >> axis) & 1 == 1 {
                    upper[axis]
                } else {
                    lower[axis]
                }
            };
            let weight = (0..3).fold(1.0, |acc, axis| {
                acc * if (corner >> axis) & 1 == 1 {
                    weights[axis]
                } else {
                    1.0 - weights[axis]
                }
            });
            let index = (pick(0) * self.size + pick(1)) * self.size + pick(2);
            let entry = self.data[index];
            for channel in 0..3 {
                out[channel] += weight * f64::from(entry[channel]);
            }
        }
        out
    }
}

/// Map a linear value to the `[0, 1]` table coordinate.
fn encode(value: f64) -> f64 {
    let log = value.max(1e-10).log10();
    ((log - LOG_RANGE_MIN) / (LOG_RANGE_MAX - LOG_RANGE_MIN)).clamp(0.0, 1.0)
}

/// Inverse of [`encode`].
fn decode(index: usize, size: usize) -> f64 {
    let u = index as f64 / (size - 1) as f64;
    10.0_f64.powf(LOG_RANGE_MIN + u * (LOG_RANGE_MAX - LOG_RANGE_MIN))
}

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

    #[test]
    fn encode_decode_round_trip() {
        for value in [0.0005, 0.01, 0.18, 1.0, 3.0] {
            let round_trip =
                10.0_f64.powf(LOG_RANGE_MIN + encode(value) * (LOG_RANGE_MAX - LOG_RANGE_MIN));
            assert!(
                (round_trip - value).abs() < 1e-3 * value.max(1.0),
                "{value} -> {round_trip}"
            );
        }
    }

    #[test]
    fn the_table_matches_the_model_it_was_baked_from() {
        let context = SpectralContext::new();
        let lut = ExposureLut::bake(33, &context);

        // The error is strongly skewed: the mean is tiny and the tail lives in
        // the extreme corners, where the polynomial reconstruction clamps at
        // zero and the mapping has a kink. Asserting a single maximum would
        // either fail on a corner or hide the distribution.
        let mut errors = Vec::new();
        for r in 0..10 {
            for g in 0..10 {
                for b in 0..10 {
                    let rgb = [
                        0.01 + 0.09 * f64::from(r),
                        0.01 + 0.09 * f64::from(g),
                        0.01 + 0.09 * f64::from(b),
                    ];
                    let direct = context.expose(rgb).unwrap();
                    let baked = lut.sample(rgb);
                    for channel in 0..3 {
                        errors.push((direct[channel] - baked[channel]).abs());
                    }
                }
            }
        }
        errors.sort_by(|a, b| a.partial_cmp(b).unwrap());
        let count = errors.len();
        let mean = errors.iter().sum::<f64>() / count as f64;
        let p99 = errors[count * 99 / 100];

        assert!(mean < 0.005, "mean error {mean} decades");
        assert!(p99 < 0.05, "p99 error {p99} decades");
    }

    #[test]
    fn a_written_table_reads_back_identical() {
        let context = SpectralContext::new();
        let table = ExposureLut::bake(9, &context);

        let path = std::env::temp_dir().join(format!("klut-test-{}.klut", std::process::id()));
        table.write(&path).expect("write");
        let loaded = ExposureLut::read(&path).expect("read");
        let _ = std::fs::remove_file(&path);

        assert_eq!(loaded.size(), table.size());
        assert_eq!(loaded.identity(), table.identity());
        let probe = [0.2, 0.35, 0.05];
        assert_eq!(loaded.sample(probe), table.sample(probe));
    }

    #[test]
    fn the_identity_names_the_model() {
        let context = SpectralContext::new();
        let identity = context.identity();
        assert!(identity.contains("camera="), "{identity}");
        assert!(identity.contains("film="), "{identity}");
        assert!(identity.contains("grid="), "{identity}");
    }

    #[test]
    fn a_corrupt_table_is_rejected() {
        let context = SpectralContext::new();
        let table = ExposureLut::bake(4, &context);
        let path = std::env::temp_dir().join(format!("klut-bad-{}.klut", std::process::id()));
        table.write(&path).expect("write");

        let mut bytes = std::fs::read(&path).expect("read back");
        let last = bytes.len() - 1;
        bytes[last] ^= 0xFF;
        std::fs::write(&path, &bytes).expect("rewrite");

        let result = ExposureLut::read(&path);
        let _ = std::fs::remove_file(&path);
        assert!(matches!(result, Err(LutError::Checksum)), "{result:?}");
    }

    #[test]
    fn the_table_is_bounded_by_its_inputs() {
        let context = SpectralContext::new();
        let lut = ExposureLut::bake(9, &context);
        for rgb in [[0.0; 3], [1.0; 3], [10.0; 3]] {
            let out = lut.sample(rgb);
            for value in out {
                assert!(value.is_finite());
            }
        }
    }
}