Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-develop

Source of release 0.1.0

0.1.0 / src / film_develop.lucb

## Development: the process the film goes through, how long and how warm, and
## what that does to its curve. A process belongs to a family (color negative,
## reversal, black and white) and has its normal time and temperature, its rate
## (how much faster per degree), and what it does compared with its family's
## reference developer: speed (log H), contrast and grain. Each stock names its
## standard process (its data sheet's curves are developed in it); another
## process of the family changes the stock by the ratio of the two.
##
## Development is kinetic. Developed silver grows toward what the exposure made
## developable, at a rate that rises with exposure (a grain carrying more latent
## image develops sooner), after a short induction:
##
##   m(x, t) = m∞(x) · (1 − e^(−k(x)·(t − tᵢ))),   k(x) = k_grey · 10^(β·(x − x_grey))
##
## Dye forms with the silver, so a stock's curve at another time is its
## published curve over fog times S(t)/S(t_normal), S the bracket above. Each
## stock carries k, β and tᵢ relative to its normal development
## (dev/film/fit_development.py fits them: black-and-white films to their sheets'
## development series, within 0.006–0.03 density; color negatives to Kodak's C-41
## control charts; slides' first developer to E100's own push, E-4000). Fog is
## its own population, growing with time and, with about 1.75 times the image's
## activation energy, faster with warmth (Kodak's C-41 charts: D-min moves twice
## as much for +2.2 °C as for +20 % time). Temperature acts through Arrhenius, its
## energy from the process's published rate per degree (E = c·R·T²).
##
## A slide's first developer makes its negative silver image; what it leaves
## undeveloped becomes the dye. Developed longer, it takes more of the shadows'
## halide: less dye, the film rated faster, and its fog takes D-max down.
from luce_std import math
from film_stocks import Stock
import film_stocks

## Families of process: color negative, reversal, black and white (as stock kinds).
pub let color_negative: i64 = 0
pub let reversal: i64 = 1
pub let black_and_white: i64 = 2

## A process: its name, family, normal time (minutes) and temperature (°C) for
## the stock it is tabled for, rate per degree, and against its family's
## reference developer at the same contrast: speed (stops), contrast (ratio),
## grain (ratio of RMS granularity) and compensation (how much the slope above
## grey flattens as developer runs out in the highlights, 0..1) and edge effects
## (how far exhausted developer spreads, µm, and how strongly: the share of a
## step's density difference added as overshoot at its border). A fixed process
## (a two-bath) takes no notice of time or temperature.
pub struct Process:
    pub var name: str
    pub var family: i64
    pub var minutes: f64
    pub var celsius: f64
    pub var rate: f64
    pub var speed: f64
    pub var contrast: f64
    pub var grain: f64
    pub var compensation: f64 = 0.0
    pub var edge: f64 = 0.035
    pub var edge_width: f64 = 20.0
    pub var fixed: bool = false
    ## How it is agitated (`agitations`): 1 continuous (a processing machine),
    ## 2 intermittent (a tank inverted every 30 s), 3 stand.
    pub var agitation: i64 = 2

## The processes. Sources (research_notes/Film stocks and chemistry/
## processes_and_developers.md): Kodak H-24 (ECN-2), Z-131 (C-41), Z-119 (E-6),
## F-4017 and J-109 (Tri-X, D-76, XTOL, HC-110), the Acros II data sheet, Ilford's
## developer sheets (ID-11, Microphen, Perceptol), Agfa's Rodinal sheet, the
## Diafine sheet, and Kodak's 1968 SMPTE paper (ECN-1). Black-and-white times are
## Acros II's at 20 °C: its own sheet's where it gives them (D-76, D-76 1:1,
## Microfine), else Tri-X's times scaled by D-76's ratio between the two films
## (7¼ / 6¾). Speeds and grain are published where the sheets give them; the
## grain of most developers and all compensation are estimates from the
## textbooks' consensus, and so are the edge effects (Nelson's chemical spread
## function: strong for dilute, low-sulfite developers and still stands).
pub let process_count: usize = 19
pub let processes: Process[19] = [
    # Kodak's motion picture negative process (SD-49, CD-3); the reference for
    # color negatives. 8.5 % per degree (C-41's charts).
    Process(name = "ECN-2", family = 0, minutes = 3.0, celsius = 41.1, rate = 0.085, speed = 0.0, contrast = 1.0, grain = 1.0, edge = 0.035, edge_width = 20.0, agitation = 1),
    # The still color negative process (CD-4). Motion picture film in it comes
    # out contrastier and grainier, about a third of a stop faster.
    Process(name = "C-41", family = 0, minutes = 3.25, celsius = 37.8, rate = 0.085, speed = 0.33, contrast = 1.17, grain = 1.15, edge = 0.035, edge_width = 20.0, agitation = 1),
    # The 1960s motion picture process (SD-30): 12 minutes at 21 °C, as Kodak's
    # 1968 paper gives it; for the Eastman Color Negative of that decade.
    Process(name = "ECN-1", family = 0, minutes = 12.0, celsius = 21.1, rate = 0.085, speed = 0.0, contrast = 1.0, grain = 1.0, edge = 0.035, edge_width = 20.0, agitation = 1),
    # The reversal process; times are its first developer's.
    Process(name = "E-6", family = 1, minutes = 6.0, celsius = 37.8, rate = 0.08, speed = 0.0, contrast = 1.0, grain = 1.0, edge = 0.035, edge_width = 20.0, agitation = 1),
    # Kodak D-76 stock; the reference for black and white. Acros II: 7 min, the
    # time of its sheet's middle curve, which the model is built on (its table
    # says 7¼).
    Process(name = "D-76", family = 2, minutes = 7.0, celsius = 20.0, rate = 0.087, speed = 0.0, contrast = 1.0, grain = 1.0, edge = 0.045, edge_width = 30.0),
    # D-76 diluted 1:1 and used once: a little more grain and sharpness, less
    # sensitive to temperature (5.5 % per degree).
    Process(name = "D-76 1:1", family = 2, minutes = 10.5, celsius = 20.0, rate = 0.055, speed = 0.08, contrast = 1.0, grain = 1.07, compensation = 0.05, edge = 0.08, edge_width = 40.0),
    # Ilford's ID-11, the same formula as D-76 (Acros II sheet: 6¾ min against
    # D-76's 7¼).
    Process(name = "ID-11", family = 2, minutes = 6.5, celsius = 20.0, rate = 0.1, speed = 0.0, contrast = 1.0, grain = 1.0, edge = 0.045, edge_width = 30.0),
    # Kodak XTOL (ascorbate): a third of a stop more speed, finer grain.
    Process(name = "XTOL", family = 2, minutes = 7.5, celsius = 20.0, rate = 0.087, speed = 0.33, contrast = 1.0, grain = 0.93, compensation = 0.05, edge = 0.06, edge_width = 30.0),
    Process(name = "XTOL 1:1", family = 2, minutes = 9.7, celsius = 20.0, rate = 0.055, speed = 0.4, contrast = 1.0, grain = 1.0, compensation = 0.08, edge = 0.08, edge_width = 40.0),
    # Kodak HC-110, dilution B.
    Process(name = "HC-110 (B)", family = 2, minutes = 4.0, celsius = 20.0, rate = 0.098, speed = 0.0, contrast = 1.0, grain = 1.1, compensation = 0.05, edge = 0.08, edge_width = 40.0),
    # Agfa Rodinal (p-aminophenol, almost no sulfite): sharp, defined grain,
    # slower; more dilute, slower still and more compensating.
    Process(name = "Rodinal 1+25", family = 2, minutes = 7.5, celsius = 20.0, rate = 0.087, speed = -0.33, contrast = 1.0, grain = 1.32, compensation = 0.05, edge = 0.1, edge_width = 40.0),
    Process(name = "Rodinal 1+50", family = 2, minutes = 12.9, celsius = 20.0, rate = 0.07, speed = -0.5, contrast = 1.0, grain = 1.25, compensation = 0.12, edge = 0.125, edge_width = 60.0),
    # An hour's stand in Rodinal 1+100, agitated only at the start.
    Process(name = "Rodinal 1+100 stand", family = 2, minutes = 60.0, celsius = 20.0, rate = 0.05, speed = -0.67, contrast = 0.85, grain = 1.2, compensation = 0.3, edge = 0.22, edge_width = 100.0, agitation = 3),
    # Ilford Microphen (PQ, borax): up to half a stop more speed.
    Process(name = "Microphen", family = 2, minutes = 6.4, celsius = 20.0, rate = 0.1, speed = 0.5, contrast = 1.0, grain = 1.0, edge = 0.045, edge_width = 30.0),
    # Ilford Perceptol (high sulfite, near neutral): much finer grain, slower.
    Process(name = "Perceptol", family = 2, minutes = 10.7, celsius = 20.0, rate = 0.1, speed = -0.85, contrast = 1.0, grain = 0.8, edge = 0.03, edge_width = 30.0),
    # Fujifilm Microfine: Acros II at EI 100 in 10 min (its sheet), fine grain;
    # 12½ to 5¾ min from 18 to 26 °C.
    Process(name = "Microfine", family = 2, minutes = 10.0, celsius = 20.0, rate = 0.097, speed = 0.0, contrast = 1.0, grain = 0.85, edge = 0.03, edge_width = 30.0),
    # Kodak T-MAX developer 1:4.
    Process(name = "T-MAX developer", family = 2, minutes = 6.4, celsius = 20.0, rate = 0.09, speed = 0.33, contrast = 1.0, grain = 1.05, edge = 0.05, edge_width = 30.0),
    # PMK pyro: its stain masks the grain and holds back the highlights.
    Process(name = "PMK pyro", family = 2, minutes = 12.6, celsius = 20.0, rate = 0.087, speed = -0.2, contrast = 1.0, grain = 0.85, compensation = 0.2, edge = 0.12, edge_width = 45.0),
    # Diafine, two baths of 3 minutes: the developer the emulsion soaked up runs
    # out in the highlights, so time and temperature do not matter; Acros II
    # rated at 200.
    Process(name = "Diafine", family = 2, minutes = 6.0, celsius = 21.0, rate = 0.0, speed = 1.0, contrast = 1.0, grain = 1.15, compensation = 0.35, edge = 0.12, edge_width = 60.0, fixed = true)]

## Agitation: how fast the bath replaces the developer the film used up, as a
## time constant (minutes) by agitation (0 none: the process's own; 1
## continuous, a machine's flow or a rotary drum; 2 intermittent, a tank
## inverted for 5 s every 30 s, Kodak's and Ilford's practice; 3 stand, left
## alone after the first minute: diffusion alone). Estimates: no maker
## publishes the exchange rate; they set the edge effects' strength against
## each process's own (whose effects are in the stock's published MTF).
pub let agitations: f64[4] = [1.0, 0.25, 1.0, 30.0]

## The standard process of each stock kind (an index into `processes`).
let standards: i64[3] = [0, 3, 4]

pub func standard(kind: i64) -> i64:
    return standards[(usize)math.clamp((f64)kind, 0.0, 2.0)]

## The process `stock`'s sheet was developed in: the one it names, else its kind's.
pub func standard_of(stock: const Stock*) -> i64:
    for index in 0..<process_count:
        if processes[index].name == stock.process:
            return (i64)index
    return standard(stock.kind)

## `stock`'s normal time in process `process`: the stock's own in its standard
## process, and in another the process's table time scaled as the stock's is
## (black-and-white films need different times; the tables are Acros II's).
pub func normal_minutes(stock: const Stock*, process: i64) -> f64:
    let table = processes[(usize)process].minutes
    if stock.minutes <= 0.0:
        return table
    return table * stock.minutes / processes[(usize)standard_of(stock)].minutes

## The process develop_process `value` chooses for `stock`: 0 is its standard,
## n the n-th process (from 1); one it cannot go through is the standard. Its
## own family's processes, and a slide's cross-process, C-41.
pub func chosen_for(stock: const Stock*, value: f64) -> i64:
    let at = (i64)math.round(value) - 1
    if at < 0 or at >= (i64)process_count or not (processes[(usize)at].family == stock.kind or crossed(stock.kind, at)):
        return standard_of(stock)
    return at

## Whether process `process` cross-processes a stock of `kind`: slide film in
## C-41, the cross-process labs ran (the "xpro" look).
pub func crossed(kind: i64, process: i64) -> bool:
    return kind == reversal and processes[(usize)process].name == "C-41"

## A slide stock as C-41 develops it: a negative. Its color developer forms dye
## where the first developer would have made silver, the complement of the
## slide's dye, at a lower contrast (a reversal emulsion's slope of 1.6–1.9 gives
## a negative of 1.0–1.5: 0.7 of it, an estimate) over a clear base, with no
## orange mask to correct its dyes.
pub func cross_processed(slide: Stock) -> Stock:
    var out = slide
    out.kind = color_negative
    let n = slide.curve_count
    for layer in 0..<(usize)3:
        let first = layer * film_stocks.curve_most
        var densest = 0.0
        for at in 0..<n:
            densest = math.max(densest, slide.curves[first + at])
        for at in 0..<film_stocks.curve_most:
            let positive = slide.curves[first + (at if at < n else n - 1)]
            out.curves[first + at] = clear_base + cross_contrast * (densest - positive)
    return out

let cross_contrast: f64 = 0.7
## A clear base's density, without the mask (an estimate, 0.05–0.15).
let clear_base: f64 = 0.1

## Development beyond these ratios to normal is not modelled.
let least: f64 = 0.25
let most: f64 = 6.0
## Fog's activation energy over the image's (an estimate from Kodak's C-41
## charts and the textbooks' 1.5–2).
let fog_activation: f64 = 1.75
let gas_constant: f64 = 8.314462618

## How much faster process `process` runs at `celsius` than at its normal
## temperature: Arrhenius, its activation energy from the rate per degree it
## publishes (E = c·R·T²); `power` raises the energy (fog's).
pub func activity(process: i64, celsius: f64, power: f64 = 1.0) -> f64:
    let used = processes[(usize)process]
    let normal = used.celsius + 273.15
    let warm = (celsius if celsius > 0.0 else used.celsius) + 273.15
    let energy = power * used.rate * gas_constant * normal * normal
    return math.exp(energy / gas_constant * (1.0 / normal - 1.0 / warm))

## The development in process `process`, as a ratio to its normal (`normal`
## minutes, 0: its table time), for `minutes` at `celsius` (0 for either: the
## normal): its time scaled by its activity.
pub func ratio(process: i64, minutes: f64, celsius: f64, normal: f64 = 0.0, power: f64 = 1.0) -> f64:
    let used = processes[(usize)process]
    if used.fixed:
        return 1.0
    let base = normal if normal > 0.0 else used.minutes
    let time = minutes if minutes > 0.0 else base
    return math.clamp(time / base * activity(process, celsius, power), least, most)

## How a stock develops: the development's time against normal (for the image
## and for fog), the stock's kinetics, and what the developer does beside time
## and temperature against the stock's standard one: its speed (a shift in log
## H), contrast, grain, compensation (the slope above grey flattening as the
## developer runs out in the highlights) and edge effects beyond the standard
## process's (whose are in the stock's published MTF already): their strength
## (signed) and width (µm).
pub struct Development:
    pub var time: f64 = 1.0
    pub var fog_time: f64 = 1.0
    pub var reversal: bool = false
    pub var grey_log_h: f64 = 0.0
    pub var saturation: f64[3] = [1.0, 1.0, 1.0]
    pub var rate_slope: f64[3] = [0.0, 0.0, 0.0]
    pub var induction: f64 = 0.0
    pub var fog_rate: f64[3] = [0.0, 0.0, 0.0]
    pub var shift: f64 = 0.0
    pub var gain: f64 = 1.0
    pub var grain: f64 = 1.0
    pub var compensation: f64 = 0.0
    pub var edge: f64 = 0.0
    pub var edge_width: f64 = 0.0
    ## How fast the bath replaces used developer (a time constant, minutes:
    ## `agitations`), the process's own and the one chosen, and how long it
    ## develops (minutes).
    pub var renewal: f64 = 1.0
    pub var own_renewal: f64 = 1.0
    pub var minutes: f64 = 1.0
    ## The edge effects of the process used and of the stock's standard one
    ## (whose, at its own agitation, the published MTF holds), and the
    ## standard one's renewal.
    pub var used_edge: f64 = 0.0
    pub var standard_edge: f64 = 0.0
    pub var standard_renewal: f64 = 1.0

    ## How much of its normal silver layer `layer` develops at log H `log_h`:
    ## S(time) / S(normal) of the kinetics above (1 at normal development).
    pub func factor(layer: usize, log_h: f64) -> f64:
        return self.factor_at(layer, log_h, self.time)

    ## The same at development `time` (a ratio to normal).
    func factor_at(layer: usize, log_h: f64, time: f64) -> f64:
        let u = self.saturation[layer] * math.pow(10.0, self.rate_slope[layer] * (log_h - self.grey_log_h))
        let start = self.induction
        let running = math.max(time - start, 0.0) / math.max(1.0 - start, 1.0e-9)
        return (1.0 - math.exp(-u * running)) / math.max(1.0 - math.exp(-u), 1.0e-12)

    ## How far layer `layer` at log H `log_h` has developed `share` of the way
    ## through the development's time (0..1): its silver then over its silver
    ## at the end.
    pub func progress(layer: usize, log_h: f64, share: f64) -> f64:
        let whole = self.factor_at(layer, log_h, self.time)
        return self.factor_at(layer, log_h, self.time * math.clamp(share, 0.0, 1.0)) / math.max(whole, 1.0e-12)

    ## The fog layer `layer` gains over its normal (density; a slide's first
    ## developer: the share of its unexposed halide).
    pub func fog(layer: usize) -> f64:
        return self.fog_rate[layer] * (self.fog_time - 1.0)

## A stock of `kind` developed in process `process` (an index), `minutes` at
## `celsius`: the development against the stock's standard, normal development.
## Cross-processed, the stock is already the negative its process makes
## (`cross_processed`): only time and temperature apply.
pub func developing(stock: const Stock*, process: i64, minutes: f64, celsius: f64, agitation: i64 = 0) -> Development:
    let kind = stock.kind
    let cross = crossed(kind, process)
    let normal_time = normal_minutes(stock, process)
    # Cross-processed, a slide develops as a color negative does: a color
    # negative's kinetics (Portra 400's, Kodak's C-41 charts).
    let kinetic = film_stocks.stock_at(11) if cross else *stock
    var out = Development(time = ratio(process, minutes, celsius, normal_time),
                          fog_time = ratio(process, minutes, celsius, normal_time, fog_activation),
                          reversal = kind == reversal and not cross, grey_log_h = stock.grey_log_h,
                          saturation = kinetic.saturation, rate_slope = kinetic.rate_slope, induction = kinetic.induction,
                          fog_rate = kinetic.fog_rate)
    if processes[(usize)process].fixed:
        out.fog_time = 1.0
    let used = processes[(usize)process]
    let normal = used if cross else processes[(usize)standard_of(stock)]
    out.shift = 0.30103 * (used.speed - normal.speed)
    out.gain = used.contrast / normal.contrast
    out.grain = used.grain / normal.grain
    out.compensation = math.clamp(used.compensation - normal.compensation, 0.0, 0.9)
    out.edge = used.edge - normal.edge
    out.edge_width = used.edge_width
    out.own_renewal = agitations[(usize)used.agitation]
    out.used_edge = used.edge
    out.standard_edge = normal.edge
    out.standard_renewal = agitations[(usize)normal.agitation]
    out.renewal = agitations[(usize)math.clamp((f64)agitation, 0.0, 3.0)] if agitation > 0 else out.own_renewal
    out.minutes = (minutes if minutes > 0.0 else normal_time) if not used.fixed else used.minutes
    return out

test "normal development in the standard process changes nothing":
    for at in 0..<film_stocks.stock_count:
        let stock = film_stocks.stock_at(at)
        let normal = developing(&stock, standard_of(&stock), 0.0, 0.0)
        for layer in 0..<(usize)3:
            for x in [-3.0, -1.5, 0.0]:
                assert(math.abs(normal.factor(layer, x) - 1.0) < 1.0e-12)
            assert(math.abs(normal.fog(layer)) < 1.0e-12)
        assert(normal.shift == 0.0 and normal.gain == 1.0 and normal.grain == 1.0)

test "Tri-X in D-76 for 6, 10 and 12 minutes develops as Kodak's curves show":
    # Kodak F-4017's D-76 series (dev/film/kodak_tri_x_400.json) at log H −2.0,
    # −1.4, −0.8 and −0.2: 8 minutes (the stock's curve), then 6, 10 and 12.
    let tri_x = film_stocks.stock_at(17)
    let d76 = standard_of(&tri_x)
    let base = tri_x.curves[0]
    let xs: f64[4] = [-2.0, -1.4, -0.8, -0.2]
    let normal: f64[4] = [0.7134, 1.0749, 1.4653, 1.8243]
    let minutes: f64[3] = [6.0, 10.0, 12.0]
    let published: f64[12] = [0.5423, 0.831, 1.1669, 1.482, 0.8742, 1.3036, 1.7421, 2.1415, 1.0673, 1.5653, 2.0441, 2.4657]
    for (index, time) in minutes.indexed():
        let grown = developing(&tri_x, d76, time, 20.0)
        for at in 0..<(usize)4:
            let predicted = base + grown.fog(0) + (normal[at] - base) * grown.factor(0, xs[at])
            assert(math.abs(predicted - published[index * 4 + at]) < 0.08)

test "Acros II in D-76 for 4 and 10 minutes develops as Fujifilm's curves show":
    # Fujifilm's Acros II sheet's D-76 series (dev/film/fuji_acros_ii.json) at
    # log H −2.05, −1.45, −0.85 and −0.25: 7 minutes (the stock's curve), then 4
    # and 10. One rate per exposure (dev/film/fit_development.py fits exactly
    # this form) misses Fujifilm's 4 minutes by up to 0.09 in the midtones: its
    # populations develop at their own rates, which the kinetics do not yet
    # separate (docs/PHYSICAL-FILM.md, part 2).
    let acros = film_stocks.stock_at(5)
    let d76 = standard_of(&acros)
    let base = acros.curves[0]
    let xs: f64[4] = [-2.05, -1.45, -0.85, -0.25]
    let normal: f64[4] = [0.2147, 0.4699, 0.8125, 1.3235]
    let minutes: f64[2] = [4.0, 10.0]
    let published: f64[8] = [0.1368, 0.3475, 0.6299, 0.9979, 0.3061, 0.6673, 1.0611, 1.5947]
    for (index, time) in minutes.indexed():
        let grown = developing(&acros, d76, time, 20.0)
        for at in 0..<(usize)4:
            let predicted = base + grown.fog(0) + (normal[at] - base) * grown.factor(0, xs[at])
            assert(math.abs(predicted - published[index * 4 + at]) < 0.1)

## Stock `stock`'s channel `channel` curve at log H `x`, held at its ends.
func stock_curve(stock: const Stock*, channel: usize, x: f64) -> f64:
    let n = stock.curve_count
    let place = math.clamp((x - stock.curve_first) / stock.curve_step, 0.0, (f64)(n - 1))
    let below = (usize)math.min(math.floor(place), (f64)(n - 2))
    let t = place - (f64)below
    let row = channel * film_stocks.curve_most
    return stock.curves[row + below] * (1.0 - t) + stock.curves[row + below + 1] * t

test "Ektachrome E100 pushed in E-6 gains its sheet's push":
    # Kodak E-4000 (E100): push 1, the first developer at 8 minutes (6 normal),
    # rates it EI 200, one stop, read where the slide is 1.0 over its D-min.
    # Past 8 minutes Kodak publishes only Ektachrome E200's series (E-28: EI
    # 320, 640, 1000 at 8, 11, 14), borrowed for the gain beyond 8: +1 and
    # +1 2/3 stops. The first developer's negative image grows (film.lucb's
    # `developed_amount`).
    let e100 = film_stocks.stock_at(15)
    let e6 = standard_of(&e100)
    var mean_gain: f64[3] = [0.0, 0.0, 0.0]
    for channel in 0..<(usize)3:
        var top = -1.0e9
        var bottom = 1.0e9
        for at in 0..<e100.curve_count:
            let v = e100.curves[channel * film_stocks.curve_most + at]
            top = math.max(top, v)
            bottom = math.min(bottom, v)
        let level = bottom + 1.0
        let normal_speed = speed_at(&e100, developing(&e100, e6, 6.0, 38.0), channel, top, bottom, level)
        let minutes: f64[3] = [8.0, 11.0, 14.0]
        var gains: f64[3] = [0.0, 0.0, 0.0]
        for (index, time) in minutes.indexed():
            let pushed = speed_at(&e100, developing(&e100, e6, time, 38.0), channel, top, bottom, level)
            gains[index] = (normal_speed - pushed) / 0.30103
            mean_gain[index] += gains[index] / 3.0
        # One rate shared by the layers: the blue gains a little more (its
        # fog is larger).
        assert(math.abs(gains[0] - 1.0) < 0.15)
        assert(math.abs(gains[1] - gains[0] - 1.0) < 0.15)
        assert(math.abs(gains[2] - gains[0] - 5.0 / 3.0) < 0.25)
    assert(math.abs(mean_gain[0] - 1.0) < 0.05)

## Where (log H) slide `stock`'s channel, developed as `grown`, falls to `level`.
func speed_at(stock: const Stock*, grown: Development, channel: usize, top: f64, bottom: f64, level: f64) -> f64:
    let span = top - bottom
    var previous = 0.0
    var x = stock.curve_first
    let last = stock.curve_first + stock.curve_step * (f64)(stock.curve_count - 1)
    var first = true
    while x <= last:
        let normal = stock_curve(stock, channel, x + grown.shift)
        var taken = math.min(1.0, (top - normal) / span * grown.factor(channel, x + grown.shift))
        taken += grown.fog(channel) * (1.0 - taken)
        let positive = bottom + span * (1.0 - math.clamp(taken, 0.0, 1.0)) * grown.gain
        if not first and previous >= level and positive < level:
            return x - 0.01 + (previous - level) / math.max(previous - positive, 1.0e-9) * 0.01
        previous = positive
        first = false
        x += 0.01
    return last

test "warmer develops faster, and fog faster still":
    let d76 = standard(black_and_white)
    assert(ratio(d76, 7.0, 24.0) > 1.3)
    assert(ratio(d76, 7.0, 24.0, 0.0, 1.75) > ratio(d76, 7.0, 24.0))

test "C-41 for 40 seconds more and less moves contrast as Kodak's control charts do":
    let portra = film_stocks.stock_at(11)
    let c41 = 1
    assert(processes[c41].name == "C-41")
    # Kodak Z-131, Chart 2: HD−LD for ±40 s around 3:15 (±20.5 %), from strips of
    # HD−LD about 0.9 (an assumption: Kodak publishes ECN-2's, 0.86–0.97).
    let normal = normal_minutes(&portra, c41)
    let long = developing(&portra, c41, normal * (1.0 + 40.0 / 195.0), 0.0)
    let short = developing(&portra, c41, normal * (1.0 - 40.0 / 195.0), 0.0)
    let gains: f64[3] = [0.12, 0.10, 0.14]
    let losses: f64[3] = [0.19, 0.17, 0.23]
    for layer in 0..<(usize)3:
        assert(math.abs(0.9 * (long.factor(layer, -1.0) - 1.0) - gains[layer]) < 0.02)
        assert(math.abs(0.9 * (1.0 - short.factor(layer, -1.0)) - losses[layer]) < 0.02)

test "a process of another family is the standard one":
    let vision = film_stocks.stock_at(2)
    assert(chosen_for(&vision, 0.0) == 0 and chosen_for(&vision, 2.0) == 1 and chosen_for(&vision, 5.0) == 0)

test "a two-bath ignores time and temperature; Rodinal stand is slow and compensating":
    let diafine = 18
    assert(processes[diafine].name == "Diafine" and ratio(diafine, 12.0, 28.0) == 1.0)
    let acros = film_stocks.stock_at(5)
    let stand = developing(&acros, 12, 0.0, 0.0)
    assert(processes[12].name == "Rodinal 1+100 stand" and stand.shift < -0.15 and stand.compensation > 0.2)

test "a slide in C-41 becomes a negative, steeper than a negative stock":
    let c41 = 1
    let velvia = film_stocks.stock_at(4)
    assert(chosen_for(&velvia, (f64)(c41 + 1)) == c41 and crossed(reversal, c41) and not crossed(color_negative, c41))
    let negative = cross_processed(velvia)
    assert(negative.kind == color_negative)
    # Density now rises with exposure: the first reading thinnest.
    let n = negative.curve_count
    assert(negative.curves[0] < negative.curves[n - 1] and negative.curves[0] < 0.2)

test "a pushed slide's first developer takes more of its halide":
    let e100 = film_stocks.stock_at(15)
    let push = developing(&e100, standard(reversal), 8.0, 0.0)
    assert(push.reversal and push.factor(1, e100.grey_log_h) > 1.0 and push.fog(1) > 0.0)

test "stocks develop in their sheets' processes":
    # Super Panavision 70 (recipe.lucb's mode) chooses stock 22: 5251, in ECN-1.
    let ecn = film_stocks.stock_at(21)
    assert(ecn.name == "Eastman Color Negative 5251" and processes[(usize)standard_of(&ecn)].name == "ECN-1")
    let tri_x = film_stocks.stock_at(17)
    assert(tri_x.name == "Kodak Tri-X 400" and processes[(usize)standard_of(&tri_x)].name == "D-76" and normal_minutes(&tri_x, standard_of(&tri_x)) == 8.0)