Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-develop

Source of release 0.1.0

0.1.0 / src / film_print.lucb

## Print: scene light printed on Kodak Vision 2383 and projected, as a view. This
## is the cinema's release print, the classic "film look" of print-emulation LUTs,
## and it replaces the view's tone scale instead of stacking on it: the print's
## own toe and shoulder are the tone mapping.
##
## 1. Printing density, what the print stock sees of the negative: each print
##    layer's log exposure is its printer light less it. A negative printed
##    straight (film.lucb's `printing_onto`) hands its own, read through
##    SMPTE RP 180 from its dyes (film_model.lucb); a scan, a grade or a film
##    recorder in between hands the image the Cineon way, as recorders write
##    it: 0.6 density per decade of scene light, grey (0.18) at 0.70 over D-min.
## 2. Printer lights put grey on the print's visual neutral of density 1.0: the
##    amounts of dye Kodak's normalized curves define (each 1.0), so grey shows
##    at a tenth of the print's white, as a cinema shows it.
## 3. The print develops each layer through its characteristic curve (Status A,
##    unmixed into dye amounts as film_model.lucb does for negatives; the layers
##    of a print do not hold each other back).
## 4. With the bleach bypassed (ENR, CCE), silver kept beside the dyes, neutral
##    density where the print is dense (`print_silver_density`).
## 5. Projection: the dyes' spectral density under a xenon arc (taken as a 5600 K
##    black body), white the clear print, 0.2% of white added as projector flare,
##    and the eye adapted to the screen: the light's white goes to the display's.
import memory
import sync
from luce_std import math
from luce_color import spectral
from luce_color import xyz
from film_stocks import PrintStock, kodak_2383
import film_stocks
import densitometry

## The printing-density encoding: density per decade of scene light, and grey's.
let negative_gamma: f64 = 0.6
## Kodak's LAD aim for a projection print (H-61B): the LAD patch, 0.05 below an
## 18 % grey card in printing density on the negative (Kodak's Digital LAD
## guide, H-387), prints as the 1.0 visual-density neutral, which on 2383 reads
## Status A 1.09 red, 1.06 green, 1.03 blue.
let lad_below_grey: f64 = 0.05
let lad_status_a: f64[3] = [1.09, 1.06, 1.03]
let grey_density: f64 = 0.7
## Projector flare, a share of white; and the projector's light.
let projector_flare: f64 = 0.002
let projector_kelvin: f64 = 5600.0

## A print's development over log H: 1024 samples over its stock's range (2383's
## from -1.5 to 3.5).
let samples: usize = 1024
## The most dye (in units of a layer's dye at 1.0) unmixing gives a layer.
let most_dye: f64 = 10.0

## A print material's model: its stock, its development unmixed into dye
## amounts, the light it is seen by, its flare. 2383 projected here; a color
## paper seen by reflection (film_paper.lucb).
pub struct PrintModel:
    var stock: PrintStock
    var low: f64
    var high: f64
    ## Where grey prints: each layer's dye amount 1.0 (Kodak's print dyes make a
    ## visual neutral of 1.0 there), or, when set, each layer's density over
    ## base (a paper's mid-grey, 0.70).
    var grey_density: f64
    var flare: f64
    ## Dye amounts per layer over log H, the layers' printer lights (grey's log H),
    ## and Status A's bands.
    var curve: f64[3072]
    var printer: f64[3]
    var bands: f64[243]
    ## The projector's light, the clear print's white under it, and the matrix from
    ## that white to D65 (Bradford).
    var light: spectral.Spectrum
    var white: xyz.XYZ
    var adapt: xyz.Matrix3
    ## A paper seen by reflection (`reflectance`), not a film by transmission.
    var reflective: bool

    ## What comes back of light at a wavelength where the dyes' density is `d`:
    ## transmitted once through a film; off a paper, `reflectance`'s.
    func passed(d: f64) -> f64:
        return reflectance(d) if self.reflective else math.pow(10.0, -d)

    ## Status A densities of dye `amounts` (cyan, magenta, yellow) over the base.
    func read(amounts: f64[3]) -> f64[3]:
        var out: f64[3]
        for channel in 0..<(usize)3:
            var sum = 0.0
            for at in 0..<spectral.samples:
                let d = amounts[0] * self.stock.dyes[at] + amounts[1] * self.stock.dyes[81 + at] + amounts[2] * self.stock.dyes[162 + at]
                sum += self.passed(d) * self.bands[channel * 81 + at]
            out[channel] = -math.log10(math.max(sum, 1.0e-12))
        return out

    ## Layer `layer`'s dye at log H `log_h`.
    func amount(layer: usize, log_h: f64) -> f64:
        let position = math.clamp((log_h - self.low) / (self.high - self.low), 0.0, 1.0) * (f64)(samples - 1)
        let below = (usize)math.min(math.floor(position), (f64)(samples - 2))
        let t = position - (f64)below
        return self.curve[below * 3 + layer] * (1.0 - t) + self.curve[(below + 1) * 3 + layer] * t

## Layer `channel`'s Status A density over the print's D-min at `log_h`, held at
## the ends of what Kodak plots.
func over_minimum(stock: const PrintStock*, channel: usize, log_h: f64) -> f64:
    let n = stock.curve_count
    let curve = stock.curves[channel * film_stocks.curve_most..<channel * film_stocks.curve_most + n]
    var least = curve[0]
    for value in curve:
        least = math.min(least, value)
    let position = math.clamp((log_h - stock.curve_first) / stock.curve_step, 0.0, (f64)(n - 1))
    let below = (usize)math.min(math.floor(position), (f64)(n - 2))
    let t = position - (f64)below
    return curve[below] + (curve[below + 1] - curve[below]) * t - least

## Fill `out` (its stock, bands and light set): the curves, printer lights and white.
func build(out: PrintModel*):
    # The neutral ramp unmixed into dye amounts, by Newton from the last.
    var a: f64[3] = [0.0, 0.0, 0.0]
    for at in 0..<samples:
        let log_h = out.low + (out.high - out.low) * (f64)at / (f64)(samples - 1)
        let target: f64[3] = [over_minimum(&out.stock, 0, log_h), over_minimum(&out.stock, 1, log_h), over_minimum(&out.stock, 2, log_h)]
        for _ in 0..<30:
            let here = out.read(a)
            let residual: f64[3] = [here[0] - target[0], here[1] - target[1], here[2] - target[2]]
            if math.abs(residual[0]) + math.abs(residual[1]) + math.abs(residual[2]) < 1.0e-7:
                break
            var jacobian: f64[9]
            for k in 0..<(usize)3:
                var nudged = a
                nudged[k] += 1.0e-4
                let moved = out.read(nudged)
                for row in 0..<(usize)3:
                    jacobian[row * 3 + k] = (moved[row] - here[row]) / 1.0e-4
            let inverse = invert(jacobian) else break
            # Steps held short and amounts bounded: near a paper's D-max the
            # reading flattens and a full step would run away.
            for k in 0..<(usize)3:
                let step = inverse[k * 3] * residual[0] + inverse[k * 3 + 1] * residual[1] + inverse[k * 3 + 2] * residual[2]
                a[k] = math.clamp(a[k] - math.clamp(step, -0.5, 0.5), 0.0, most_dye)
        # A layer's dye only grows with its exposure (unmixing near D-min can
        # wobble one against another).
        for k in 0..<(usize)3:
            let before = out.curve[(at - 1) * 3 + k] if at > 0 else 0.0
            out.curve[at * 3 + k] = math.max(math.max(a[k], 0.0), before)
    # Printer lights: each layer's log H where it forms grey's dye (1.0), or
    # reaches grey's density over base.
    for layer in 0..<(usize)3:
        out.printer[layer] = out.high
        var previous = 0.0
        for at in 0..<samples:
            let log_h = out.low + (out.high - out.low) * (f64)at / (f64)(samples - 1)
            let reached = over_minimum(&out.stock, layer, log_h) if out.grey_density > 0.0 else out.curve[at * 3 + layer]
            let aim = out.grey_density if out.grey_density > 0.0 else 1.0
            if at > 0 and reached >= aim:
                let t = (aim - previous) / math.max(reached - previous, 1.0e-9)
                out.printer[layer] = out.low + (out.high - out.low) * ((f64)(at - 1) + t) / (f64)(samples - 1)
                break
            previous = reached
    out.white = out.light.xyz()
    let white_point = xyz.Chromaticity(out.white.x / (out.white.x + out.white.y + out.white.z), out.white.y / (out.white.x + out.white.y + out.white.z))
    out.adapt = xyz.adapt(white_point, xyz.d65)
    if out.grey_density < 0.0:
        visual_grey(out)
        # The aim is the LAD patch's, not grey's: grey's printer light is that
        # far below it.
        for layer in 0..<(usize)3:
            out.printer[layer] -= lad_below_grey

## Each layer's log H where `out` forms dye `amounts`, as its printer lights.
func printers_for(out: PrintModel*, amounts: f64[3]):
    for layer in 0..<(usize)3:
        for at in 1..<samples:
            if out.curve[at * 3 + layer] >= amounts[layer]:
                let low = out.curve[(at - 1) * 3 + layer]
                let high = out.curve[at * 3 + layer]
                let t = (amounts[layer] - low) / math.max(high - low, 1.0e-9)
                out.printer[layer] = out.low + (out.high - out.low) * ((f64)(at - 1) + t) / (f64)(samples - 1)
                break

## Printer lights that print the LAD patch as a visual neutral at a tenth of
## white: the dye amounts that look so (Newton on Y and x, y), and their log H.
func visual_grey(out: PrintModel*):
    var amounts: f64[3] = [1.0, 1.0, 1.0]
    for _ in 0..<40:
        let miss = grey_miss(out, amounts)
        if math.abs(miss[0]) + math.abs(miss[1]) + math.abs(miss[2]) < 1.0e-7:
            break
        var jacobian: f64[9]
        for k in 0..<(usize)3:
            var nudged = amounts
            nudged[k] += 1.0e-4
            let moved = grey_miss(out, nudged)
            for row in 0..<(usize)3:
                jacobian[row * 3 + k] = (moved[row] - miss[row]) / 1.0e-4
        let inverse = invert(jacobian) else break
        for k in 0..<(usize)3:
            amounts[k] -= inverse[k * 3] * miss[0] + inverse[k * 3 + 1] * miss[1] + inverse[k * 3 + 2] * miss[2]
            amounts[k] = math.max(amounts[k], 0.0)
    printers_for(out, amounts)

## How far dye `amounts` look from a neutral grey at a tenth of white: log Y,
## and the chromaticity off D65's.
func grey_miss(model: const PrintModel*, amounts: f64[3]) -> f64[3]:
    let shown = seen(model, amounts)
    let sum = shown.x + shown.y + shown.z
    return [math.log10(math.max(shown.y, 1.0e-9) / 0.1), shown.x / sum - 0.3127, shown.y / sum - 0.329]

func invert(m: f64[9]) -> f64[9]?:
    let det = m[0] * (m[4] * m[8] - m[5] * m[7]) - m[1] * (m[3] * m[8] - m[5] * m[6]) + m[2] * (m[3] * m[7] - m[4] * m[6])
    if math.abs(det) < 1.0e-14:
        return none
    return [(m[4] * m[8] - m[5] * m[7]) / det, (m[2] * m[7] - m[1] * m[8]) / det, (m[1] * m[5] - m[2] * m[4]) / det,
            (m[5] * m[6] - m[3] * m[8]) / det, (m[0] * m[8] - m[2] * m[6]) / det, (m[2] * m[3] - m[0] * m[5]) / det,
            (m[3] * m[7] - m[4] * m[6]) / det, (m[1] * m[6] - m[0] * m[7]) / det, (m[0] * m[4] - m[1] * m[3]) / det]

# mark: Reflection ============================================================

## A paper print's reflectance, relative to its white, where its dye layer has
## density `d` at normal incidence: Williams and Clapper (1953) in Shore and
## Spoonhower's general form (Hébert and Hersch, JIST 2004). Light lit at 45°
## crosses the gelatin once on its way down (refracted to 28°), is scattered
## by the paper base (reflectance ρ, 0.9, flat: an estimate), and comes up
## diffusely: what reaches the surface below the critical angle leaves, the
## rest (about 60 % of diffuse light) reflects back down through the dyes for
## another pass. So density in reflection is not twice density in
## transmission, nor proportional to dye: mixtures and dark colors fall short.
## Glossy surface reflection the 45°/0° geometry still sees bounds the deepest
## black: 0.1 % of white (an estimate), a limit of 3.0, above the Status A D-max
## Kodak measures on its glossy Endura papers (2.66–2.76, 2.6 over base).
## The angular integrals depend only on the transmittance, so the curve is
## made once over d (`reflection_table`) and read linearly.
let base_reflectance: f64 = 0.9
let surface_seen: f64 = 0.001
let gelatin_index: f64 = 1.5
let reflection_most: f64 = 6.0
let reflection_samples: usize = 600

var reflection_table: f64[600]
var reflection_once: sync.Once

func reflectance(d: f64) -> f64:
    reflection_once.run(make_reflection)
    let position = math.clamp(d / reflection_most, 0.0, 1.0) * (f64)(reflection_samples - 1)
    let below = (usize)math.min(math.floor(position), (f64)(reflection_samples - 2))
    let t = position - (f64)below
    return reflection_table[below] * (1.0 - t) + reflection_table[below + 1] * t

func make_reflection():
    let white = body(1.0)
    for at in 0..<reflection_samples:
        let d = reflection_most * (f64)at / (f64)(reflection_samples - 1)
        reflection_table[at] = (body(math.pow(10.0, -d)) + surface_seen * white) / (white * (1.0 + surface_seen))

## Light returned through a dye layer of transmittance `t` at normal incidence
## (Shore–Spoonhower: down at 28°, diffuse up with multiple internal reflection).
func body(t: f64) -> f64:
    let inward = math.asin(math.sin(math.pi / 4.0) / gelatin_index)
    var escaping = 0.0
    var returning = 0.0
    let steps = 90
    for at in 0..<steps:
        let theta = ((f64)at + 0.5) / (f64)steps * math.pi / 2.0
        let weight = math.sin(2.0 * theta) * (math.pi / 2.0) / (f64)steps
        let inside = internal(theta)
        escaping += (1.0 - inside) * math.pow(t, 1.0 / math.cos(theta)) * weight
        returning += inside * math.pow(t, 2.0 / math.cos(theta)) * weight
    let down = (1.0 - internal_at_entry()) * math.pow(t, 1.0 / math.cos(inward))
    return down * base_reflectance * escaping / (1.0 - base_reflectance * returning)

## Fresnel reflectance inside gelatin toward air at `theta` (total beyond the
## critical angle), unpolarized.
func internal(theta: f64) -> f64:
    let outside = gelatin_index * math.sin(theta)
    if outside >= 1.0:
        return 1.0
    let cos_i = math.cos(theta)
    let cos_t = math.sqrt(1.0 - outside * outside)
    let rs = (gelatin_index * cos_i - cos_t) / (gelatin_index * cos_i + cos_t)
    let rp = (gelatin_index * cos_t - cos_i) / (gelatin_index * cos_t + cos_i)
    return 0.5 * (rs * rs + rp * rp)

## The surface's reflectance for light arriving at 45° from air (what does not
## enter), unpolarized.
func internal_at_entry() -> f64:
    let theta = math.pi / 4.0
    let sin_t = math.sin(theta) / gelatin_index
    let cos_i = math.cos(theta)
    let cos_t = math.sqrt(1.0 - sin_t * sin_t)
    let rs = (cos_i - gelatin_index * cos_t) / (cos_i + gelatin_index * cos_t)
    let rp = (cos_t - gelatin_index * cos_i) / (cos_t + gelatin_index * cos_i)
    return 0.5 * (rs * rs + rp * rp)

# mark: For the GPU =============================================================

## The print view's data for the GPU (view.lucb's `pack`, shaders/develop.frag's
## `printed`), in single floats, `print_atlas_width` texels a row, RGBA:
##   row 0     the curves: each layer's dye amount over log H, `samples` of them
##             from `low` to `high`
##   row 1     0–80 the dyes (cyan, magenta, yellow) at each wavelength; 100–180
##             the light times the observer (X, Y, Z); 200–799 `reflectance`
##   row 2     scalars: 0 the printer lights (grey's log H) and the kind (0 a
##             print, 1 a color paper, 2 a black-and-white paper); 1 low,
##             high, flare, reflective; 2 the white (XYZ) and the scale to Y = 1
##             with flare; 3–5 the adaptation to D65, by rows
pub let print_atlas_width: usize = 1024
pub let print_atlas_rows: usize = 19

## Write `model`'s part of the print view's data into `out` (see above), as
## material `kind`.
pub func pack(model: const PrintModel*, kind: f64, out: f32[]):
    for at in 0..<samples:
        put(out, 0, at, model.curve[at * 3], model.curve[at * 3 + 1], model.curve[at * 3 + 2], 0.0)
    for at in 0..<spectral.samples:
        put(out, 1, at, model.stock.dyes[at], model.stock.dyes[81 + at], model.stock.dyes[162 + at], 0.0)
        let power = model.light.values[at]
        put(out, 1, 100 + at, power * spectral.observer[at * 3], power * spectral.observer[at * 3 + 1], power * spectral.observer[at * 3 + 2], 0.0)
    if model.reflective:
        for at in 0..<reflection_samples:
            put(out, 1, 200 + at, reflectance(reflection_most * (f64)at / (f64)(reflection_samples - 1)), 0.0, 0.0, 0.0)
    put(out, 2, 0, model.printer[0], model.printer[1], model.printer[2], kind)
    put(out, 2, 1, model.low, model.high, model.flare, 1.0 if model.reflective else 0.0)
    put(out, 2, 2, model.white.x, model.white.y, model.white.z, 1.0 / (model.white.y * (1.0 + model.flare)))
    for row in 0..<(usize)3:
        put(out, 2, 3 + row, model.adapt.m[row * 3], model.adapt.m[row * 3 + 1], model.adapt.m[row * 3 + 2], 0.0)

## Write texel `column` of row `row` of the print view's data.
pub func put(out: f32[], row: usize, column: usize, r: f64, g: f64, b: f64, a: f64):
    let at = (row * print_atlas_width + column) * 4
    out[at] = (f32)r
    out[at + 1] = (f32)g
    out[at + 2] = (f32)b
    out[at + 3] = (f32)a

## The projected print's data: the model for `material`, the share of its
## silver kept (`silver`, as density per unit of the dyes' mean, row 2's 9).
pub func pack_projection(silver: f64, material: i64, out: f32[]):
    built_once.run(build_model)
    var model = built else trap("the print model was not built")
    if material == 1:
        dye_transfer_once.run(build_dye_transfer)
        model = dye_transfer else trap("the dye-transfer model was not built")
    pack(model, 0.0, out)
    put(out, 2, 9, silver * print_silver_density, 0.0, 0.0, 0.0)

## Built once, on first use, and kept.
var built: PrintModel*? = none
var built_once: sync.Once

func build_model():
    built = make(kodak_2383(), projector_kelvin, -1.5, 3.5, -1.0, projector_flare)

## A model of `stock` seen by a black body of `kelvin`, developed over log H
## `low`..`high`, grey at `grey_at` density over base (0: dye 1.0; below 0: the
## LAD patch a visual neutral at a tenth of white, Kodak's aim), with `flare`
## (a share of white); kept for the program's life.
pub func make(stock: PrintStock, kelvin: f64, low: f64, high: f64, grey_at: f64, flare: f64, reflective: bool = false) -> PrintModel*:
    let made = new PrintModel(stock = stock, low = low, high = high, grey_density = grey_at, flare = flare, bands = densitometry.status_a().bands,
                              light = spectral.planck(kelvin), reflective = reflective) in memory.heap catch failure:
        trap("the print model could not be allocated")
    build(made)
    return made

## Layer `layer`'s dye in `model` at log H `log_h`, and its printer light (grey's
## log H).
pub func amount_in(model: const PrintModel*, layer: usize, log_h: f64) -> f64:
    return model.amount(layer, log_h)
pub func printer_of(model: const PrintModel*, layer: usize) -> f64:
    return model.printer[layer]

## Dye `amounts` in `model` (plus `neutral`, density of silver alike at every
## wavelength), seen by its light: XYZ adapted to D65, white at Y = 1, its flare
## added.
pub func seen(model: const PrintModel*, amounts: f64[3], neutral: f64 = 0.0) -> xyz.XYZ:
    var shown = xyz.XYZ(0.0, 0.0, 0.0)
    for at in 0..<spectral.samples:
        let d = amounts[0] * model.stock.dyes[at] + amounts[1] * model.stock.dyes[81 + at] + amounts[2] * model.stock.dyes[162 + at] + neutral
        let power = model.light.values[at] * model.passed(d)
        shown.x += power * spectral.observer[at * 3]
        shown.y += power * spectral.observer[at * 3 + 1]
        shown.z += power * spectral.observer[at * 3 + 2]
    let scale = 1.0 / (model.white.y * (1.0 + model.flare))
    let lit = xyz.XYZ((shown.x + model.flare * model.white.x) * scale, (shown.y + model.flare * model.white.y) * scale,
                      (shown.z + model.flare * model.white.z) * scale)
    return xyz.apply(model.adapt, lit)

## Technicolor dye transfer (imbibition): three gelatin relief matrices, each
## soaked in its dye and pressed onto a blank, so the print is dye alone, with
## no coupler chemistry, no mask and no print grain. Technicolor never published
## its dyes; Flueckiger et al. (2018) measured a 1949 print's: yellow at 460 nm
## (110 nm wide at half its peak), magenta at 540 with a second peak at 575,
## cyan at 660 with one at 720, and their off-band absorption (yellow 12 % of
## its peak at 600–750 nm, magenta 25 % at 400–430, cyan 22 % near 400). Its
## tone is not published either: 2383's shape at 1.1 times its density (dye
## transfer reached higher densities than print film, up to 5.0 against 3.8),
## grey as a visual neutral at a tenth of white, as 2383's. All an estimate.
func gaussian(nm: f64, centre: f64, full_width: f64) -> f64:
    let sigma = full_width / 2.3548
    let d = (nm - centre) / sigma
    return math.exp(-0.5 * d * d)

func dye_transfer_stock() -> PrintStock:
    var out = kodak_2383()
    out.name = "Technicolor dye transfer"
    for at in 0..<spectral.samples:
        let nm = spectral.wavelength(at)
        let edge = 1.0 / (1.0 + math.exp(-(nm - 600.0) / 15.0))
        let blue_tail = 1.0 / (1.0 + math.exp((nm - 430.0) / 12.0))
        out.dyes[at] = gaussian(nm, 660.0, 90.0) + 0.85 * gaussian(nm, 720.0, 80.0) + 0.22 * blue_tail
        out.dyes[81 + at] = gaussian(nm, 540.0, 70.0) + 0.8 * gaussian(nm, 575.0, 60.0) + 0.25 * blue_tail
        out.dyes[162 + at] = gaussian(nm, 460.0, 110.0) + 0.12 * edge
    for at in 0..<film_stocks.curve_most * 3:
        out.curves[at] *= 1.1
    return out

var dye_transfer: PrintModel*? = none
var dye_transfer_once: sync.Once

func build_dye_transfer():
    dye_transfer = make(dye_transfer_stock(), projector_kelvin, -1.5, 3.5, -1.0, projector_flare)

## Silver left in a print whose bleach was bypassed (Technicolor's ENR,
## Deluxe's CCE, skip bleach): neutral density per unit of the print's own
## density over its base, all of it kept. Labs' infrared readings put a fully
## unbleached print 1.8 over normal at a D-max near 4: 0.45. The silver sits
## where the print is dense, in the shadows: blacks deepen, color drains there.
let print_silver_density: f64 = 0.45

## The share of a print's silver kept for its infrared density at D-max, the
## number labs set ENR and CCE by (an IR densitometer at 1000 nm, which sees the
## silver and hardly the dyes): a normal print reads 0.60, one with no bleach at
## all 2.40 (ENR runs 0.40–1.00, CCE about 1.85; American Cinematographer, 1998).
pub func silver_kept(infrared: f64) -> f64:
    return math.clamp((infrared - 0.6) / 1.8, 0.0, 1.0)

## Scene light `rgb` (AP1) printed and projected: XYZ adapted to D65, the print's
## white at Y = 1. `silver` is the share of the print's silver kept (0..1).
## `material`: 0 Kodak Vision 2383, 1 Technicolor dye transfer.
pub func projected(rgb: f64[3], silver: f64 = 0.0, material: i64 = 0) -> xyz.XYZ:
    built_once.run(build_model)
    var model = built else trap("the print model was not built")
    if material == 1:
        dye_transfer_once.run(build_dye_transfer)
        model = dye_transfer else trap("the dye-transfer model was not built")
    var amounts: f64[3]
    for layer in 0..<(usize)3:
        let negative = negative_gamma * math.log10(math.max(rgb[layer], 1.0e-6) / 0.18)
        amounts[layer] = model.amount(layer, model.printer[layer] - negative)
    # The kept silver: the dyes' amounts are visual densities (Kodak's
    # normalized dyes make a visual neutral of 1.0 at 1.0 each), so their mean
    # is the print's density over its base.
    let kept = silver * print_silver_density * math.max((amounts[0] + amounts[1] + amounts[2]) / 3.0, 0.0)
    return seen(model, amounts, kept)

test "2383 prints the LAD patch near Kodak's Status A aim; its printer Jacobian":
    built_once.run(build_model)
    let model = built else trap("the print model was not built")
    var at_lad: f64[3]
    for layer in 0..<(usize)3:
        # The LAD patch: 0.05 below grey in printing density (grey is 0 here).
        at_lad[layer] = model.amount(layer, model.printer[layer] + lad_below_grey)
    let read = model.read(at_lad)
    # The patch prints a visual neutral (Kodak's intent); read through ISO
    # 5-3's Status A it comes within a few hundredths of Kodak's 1.09 / 1.06 / 1.03.
    print(f"2383 LAD patch reads Status A {read[0]} {read[1]} {read[2]} (Kodak 1.09 1.06 1.03)")
    for k in 0..<(usize)3:
        assert(math.abs(read[k] - lad_status_a[k]) < 0.04)
    # The printer Jacobian at the LAD patch: Status A density i per point
    # (0.025 log E) of printer channel j alone, and of all three together.
    # Kodak H-61B gives "about 0.07" a point near aim; its quantities (which
    # channel moves, the density metric, END curves shifted for clarity) are
    # not yet matched to these, so the comparison stays open.
    var together: f64[3] = [0.0, 0.0, 0.0]
    for channel in 0..<(usize)3:
        var moved = at_lad
        moved[channel] = model.amount(channel, model.printer[channel] + lad_below_grey + 0.025)
        let after = model.read(moved)
        print(f"2383 printer channel {channel}: Status A {after[0] - read[0]} {after[1] - read[1]} {after[2] - read[2]} a point")
        for k in 0..<(usize)3:
            together[k] += after[k] - read[k]
        # The channel's own density moves most: within the curves' slopes at
        # the patch (3.1–4.4 per log E: 0.08–0.11 a point).
        assert(after[channel] - read[channel] > 0.07 and after[channel] - read[channel] < 0.115)
    print(f"2383 all three channels a point: Status A {together[0]} {together[1]} {together[2]}")

test "the LAD patch prints near a tenth of white, neutral; white shows white and black near black":
    # The LAD patch at Status A 1.09 / 1.06 / 1.03 is about a visual 1.0
    # (projector flare lifts it a little); grey, two printer points lighter.
    let patch = 0.18 * math.pow(10.0, -lad_below_grey / negative_gamma)
    let grey = projected([patch, patch, patch])
    assert(grey.y > 0.09 and grey.y < 0.13)
    let x = grey.x / (grey.x + grey.y + grey.z)
    let y = grey.y / (grey.x + grey.y + grey.z)
    assert(math.abs(x - 0.3127) < 0.01 and math.abs(y - 0.329) < 0.01)
    let white = projected([20.0, 20.0, 20.0])
    assert(white.y > 0.8)
    let black = projected([0.0005, 0.0005, 0.0005])
    assert(black.y < 0.01)
    var previous = -1.0
    for step in 0..<(usize)30:
        let v = 0.0005 * math.exp2((f64)step * 0.5)
        let shown = projected([v, v, v]).y
        assert(shown >= previous - 1.0e-4)
        previous = shown

test "silver kept in a print deepens its shadows, leaves its whites":
    # A shadow above the projector's flare.
    let shadow = projected([0.05, 0.05, 0.05])
    let kept = projected([0.05, 0.05, 0.05], 1.0)
    assert(kept.y < shadow.y * 0.6)
    let white = projected([20.0, 20.0, 20.0], 1.0)
    assert(white.y > 0.75)

test "dye transfer prints grey near neutral, denser in the shadows than 2383":
    let grey = projected([0.18, 0.18, 0.18], 0.0, 1)
    let x = grey.x / (grey.x + grey.y + grey.z)
    let y = grey.y / (grey.x + grey.y + grey.z)
    assert(math.abs(x - 0.3127) < 0.04 and math.abs(y - 0.329) < 0.04)
    assert(projected([0.03, 0.03, 0.03], 0.0, 1).y < projected([0.03, 0.03, 0.03]).y)