Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-develop

Source of release 0.1.0

0.1.0 / src / film_paper.lucb

## Paper: the negative printed on photographic paper, as a darkroom printed it,
## and the print seen by reflection under D50 light. It replaces the view's
## tone scale, as the cinema print does (film_print.lucb).
##
## 1. The negative's printing density: what the paper sees through it, at 0.6
##    density per decade about grey. A color negative printed straight (no
##    scanner between) hands its own, read through the paper's spectral
##    sensitivity under the enlarger's lamp (film.lucb's `printing_onto`);
##    otherwise the image comes the Cineon way (grey 0.70 over D-min). A push,
##    a pull or a developer's character reaches the print through it.
## 2. The enlarger. A condenser enlarger shows a silver negative's density
##    steeper than a diffuse light does: the Callier effect, Q about 1.3 for
##    black-and-white negatives (Mees and James), none for dye images. An
##    enlarger lens also adds flare, an even exposure (1 % with a diffuser, 2 %
##    with a condenser, estimates); a contact print has none.
## 3. The paper's characteristic curve (reflection density over base) at the
##    paper's log exposure: the negative's density taken off the printing
##    exposure, set so grey prints at 0.70 (the print's mid-grey), moved by
##    Print Exposure in stops. A variable-contrast black-and-white paper has a
##    curve per filter grade, its own speed each; between two grades, the
##    densities between theirs. A color paper has three layers, the dichroic
##    head's filtration (in CC units: 10 = 0.1 density) taken off each.
## 4. Seen: black and white as neutral reflectance (its curves are reflection
##    densities); color as the dyes' spectra by reflection (film_print's model
##    with Williams and Clapper's multiple internal reflection, its curves
##    unmixed into dye amounts through it) under D50, the paper white the
##    display's white.
## 5. The surface and the room. A matte surface scatters part of the light
##    straight back, before it reaches the image: Kodak's Polycontrast IV RC
##    curves read a D-max near 1.7 on its matte N surface against 2.1 on glossy
##    F in the same developer (2.2 against 1.7 in Polymax RT): 1.3 % of white
##    reflected flat. Its lustre E reads as glossy F. The papers' curves are
##    glossy; the surface adds its reflection to them, and the room's light
##    glancing off any surface 1 % more (viewing flare: an estimate, Hunt's
##    typical figure for prints). Both lift the blacks, not the whites.
import sync
from luce_std import math
from luce_color import xyz
import film_print
from film_print import PrintModel
import film_stocks
from film_stocks import PaperStock, PrintStock

let negative_gamma: f64 = 0.6
## Where grey prints: a print's mid-grey reflection density.
let grey_density: f64 = 0.7
## What a surface reflects before the image (glossy, lustre, matte: share of
## white), and the room's viewing flare on any surface.
let surface_reflection: f64[3] = [0.0, 0.0, 0.0131]
let viewing_flare: f64 = 0.01

## The light a print's surface and its room add to it (share of white).
pub func surface_and_room(chosen: const PaperSettings*) -> f64:
    return surface_reflection[(usize)math.clamp((f64)chosen.surface, 0.0, 2.0)] + viewing_flare

## Reflectance `r` (of white) as seen with `extra` added by surface and room.
pub func with_surface(r: f64, extra: f64) -> f64:
    return (r + extra) / (1.0 + extra)

## The Callier coefficient of a condenser enlarger for a silver negative.
let callier: f64 = 1.3
## Enlarger flare, a share of grey's exposure, by enlarger: diffusion,
## condenser, contact.
let flares: f64[3] = [0.01, 0.02, 0.0]

## How a print is made: the paper (film_stocks.paper_at), its grade (variable
## contrast), the enlarger (0 diffusion, 1 condenser, 2 contact), the exposure
## off grey's (stops), the color head's filtration (CC: yellow, magenta, cyan),
## and whether the negative's image is silver (black and white).
pub struct PaperSettings:
    pub var paper: usize = 0
    pub var grade: f64 = 2.0
    pub var enlarger: i64 = 0
    ## 0 glossy, 1 lustre, 2 matte.
    pub var surface: i64 = 0
    pub var stops: f64 = 0.0
    pub var filtration: f64[3] = [0.0, 0.0, 0.0]
    pub var silver: bool = true

## Layer or grade curve `curve` of `paper` at log exposure `log_e`, held at its
## ends, over its least.
func density(paper: const PaperStock*, curve: usize, log_e: f64) -> f64:
    let n = paper.curve_count
    let first = curve * film_stocks.curve_most
    var least = paper.curves[first]
    for at in 0..<n:
        least = math.min(least, paper.curves[first + at])
    let position = math.clamp((log_e - paper.curve_first) / paper.curve_step, 0.0, (f64)(n - 1))
    let below = (usize)math.min(math.floor(position), (f64)(n - 2))
    let t = position - (f64)below
    return paper.curves[first + below] * (1.0 - t) + paper.curves[first + below + 1] * t - least

## Where curve `curve` reaches grey's density: its printing exposure for grey.
func grey_exposure(paper: const PaperStock*, curve: usize) -> f64:
    let n = paper.curve_count
    var previous = density(paper, curve, paper.curve_first)
    for at in 1..<n:
        let log_e = paper.curve_first + paper.curve_step * (f64)at
        let here = density(paper, curve, log_e)
        if here >= grey_density:
            let t = (grey_density - previous) / math.max(here - previous, 1.0e-9)
            return log_e - paper.curve_step * (1.0 - t)
        previous = here
    return paper.curve_first + paper.curve_step * (f64)(n - 1)

## A color paper as a print material for film_print's model.
func as_print(paper: const PaperStock*) -> PrintStock:
    var out = PrintStock(name = paper.name, dyes = paper.dyes, curve_first = paper.curve_first, curve_step = paper.curve_step,
                         curve_count = paper.curve_count)
    for at in 0..<film_stocks.curve_most * 3:
        out.curves[at] = paper.curves[at]
    return out

## The color papers' models, made on first use and kept.
var models: PrintModel*?[16]
var models_lock: sync.Mutex

func color_model(at: usize) -> PrintModel*:
    models_lock.lock()
    defer models_lock.unlock()
    if let made = models[at]:
        return made
    let paper = film_stocks.paper_at(at)
    let high = paper.curve_first + paper.curve_step * (f64)(paper.curve_count - 1)
    # D50 viewing light, no projector flare; grey at the mid-grey density.
    let made = film_print.make(as_print(&paper), 5000.0, paper.curve_first, high, grey_density, 0.0, true)
    models[at] = made
    return made

## The exposure an enlarger gives the paper for printing density `d` (over
## grey's), around `grey_at`: the Callier effect, the exposure set, the flare.
func exposure(d: f64, grey_at: f64, chosen: const PaperSettings*, filter: f64) -> f64:
    let q = callier if chosen.enlarger == 1 and chosen.silver else 1.0
    let flare = flares[(usize)math.clamp((f64)chosen.enlarger, 0.0, 2.0)]
    let log_e = grey_at - q * d + chosen.stops * 0.30103 - filter
    return math.log10(math.pow(10.0, log_e) * (1.0 - flare) + flare * math.pow(10.0, grey_at))

## Scene light `rgb` (AP1) printed on paper as `chosen` says and seen under D50:
## XYZ adapted to D65, the paper white at Y = 1.
pub func printed(rgb: f64[3], chosen: const PaperSettings*) -> xyz.XYZ:
    let paper = film_stocks.paper_at(chosen.paper)
    if paper.kind == 1:
        let model = color_model(chosen.paper)
        var amounts: f64[3]
        # Filtration: yellow holds back the blue-sensitive (yellow-forming)
        # layer, magenta the green, cyan the red.
        let filters: f64[3] = [chosen.filtration[2] / 100.0, chosen.filtration[1] / 100.0, chosen.filtration[0] / 100.0]
        for layer in 0..<(usize)3:
            let d = negative_gamma * math.log10(math.max(rgb[layer], 1.0e-6) / 0.18)
            let grey_at = film_print.printer_of(model, layer)
            amounts[layer] = film_print.amount_in(model, layer, exposure(d, grey_at, chosen, filters[layer]))
        let shown = film_print.seen(model, amounts)
        # The surface and the room add light the white of D65's color.
        let extra = surface_and_room(chosen)
        return xyz.XYZ((shown.x + extra * 0.95047) / (1.0 + extra), (shown.y + extra) / (1.0 + extra), (shown.z + extra * 1.08883) / (1.0 + extra))
    # Black and white: the negative's density from the light's luminance; the
    # grades around the chosen one, each at its own speed.
    let luminance = 0.272228717 * rgb[0] + 0.674081766 * rgb[1] + 0.053689517 * rgb[2]
    let d = negative_gamma * math.log10(math.max(luminance, 1.0e-6) / 0.18)
    var below: usize = 0
    for at in 0..<paper.grade_count:
        if paper.grades[at] <= chosen.grade:
            below = at
    let above = below + 1 if below + 1 < paper.grade_count else below
    let span = paper.grades[above] - paper.grades[below]
    let t = math.clamp((chosen.grade - paper.grades[below]) / span, 0.0, 1.0) if span > 0.0 else 0.0
    let low = density(&paper, below, exposure(d, grey_exposure(&paper, below), chosen, 0.0))
    let high = density(&paper, above, exposure(d, grey_exposure(&paper, above), chosen, 0.0))
    let reflectance = with_surface(math.pow(10.0, -(low * (1.0 - t) + high * t)), surface_and_room(chosen))
    return xyz.XYZ(reflectance * 0.95047, reflectance, reflectance * 1.08883)

## The paper view's data for the GPU (film_print.lucb's layout, then row 2: 10
## the enlarger's Callier factor, flare and the exposure set (log E); 11 the
## filtration per layer (log E); for black and white 12 the grades below and
## above the chosen one, how far between, how many; 13 their grey exposures,
## the curves' first log E and step; 14 their count; rows 3 on each grade's
## density over its least).
pub func pack(chosen: const PaperSettings*, out: f32[]):
    let paper = film_stocks.paper_at(chosen.paper)
    let q = callier if chosen.enlarger == 1 and chosen.silver else 1.0
    let flare = flares[(usize)math.clamp((f64)chosen.enlarger, 0.0, 2.0)]
    if paper.kind == 1:
        film_print.pack(color_model(chosen.paper), 1.0, out)
        film_print.put(out, 2, 11, chosen.filtration[2] / 100.0, chosen.filtration[1] / 100.0, chosen.filtration[0] / 100.0, 0.0)
    else:
        var below: usize = 0
        for at in 0..<paper.grade_count:
            if paper.grades[at] <= chosen.grade:
                below = at
        let above = below + 1 if below + 1 < paper.grade_count else below
        let span = paper.grades[above] - paper.grades[below]
        let t = math.clamp((chosen.grade - paper.grades[below]) / span, 0.0, 1.0) if span > 0.0 else 0.0
        film_print.put(out, 2, 0, 0.0, 0.0, 0.0, 2.0)
        film_print.put(out, 2, 12, (f64)below, (f64)above, t, (f64)paper.grade_count)
        film_print.put(out, 2, 13, grey_exposure(&paper, below), grey_exposure(&paper, above), paper.curve_first, paper.curve_step)
        film_print.put(out, 2, 14, (f64)paper.curve_count, 0.0, 0.0, 0.0)
        for grade in [below, above]:
            for at in 0..<paper.curve_count:
                film_print.put(out, 3 + grade, at, density(&paper, grade, paper.curve_first + paper.curve_step * (f64)at), 0.0, 0.0, 0.0)
    film_print.put(out, 2, 10, q, flare, chosen.stops * 0.30103, surface_and_room(chosen))

## The paper settings `settings` choose (paper_* and the film stock's kind).
pub func chosen_by(paper: f64, grade: f64, enlarger: f64, stops: f64, yellow: f64, magenta: f64, cyan: f64, silver: bool,
                   surface: f64 = 0.0) -> PaperSettings:
    return PaperSettings(paper = (usize)math.clamp(paper, 0.0, (f64)(film_stocks.paper_count - 1)), grade = grade,
                         enlarger = (i64)math.clamp(enlarger, 0.0, 2.0), stops = stops, filtration = [yellow, magenta, cyan], silver = silver,
                         surface = (i64)math.clamp(surface, 0.0, 2.0))

test "grey prints at the paper's mid-grey; a harder grade is steeper":
    var chosen = PaperSettings()
    let grey = printed([0.18, 0.18, 0.18], &chosen)
    assert(math.abs(-math.log10(grey.y) - grey_density) < 0.02)
    let soft_shadow = printed([0.045, 0.045, 0.045], &chosen).y
    chosen.grade = 5.0
    let hard_shadow = printed([0.045, 0.045, 0.045], &chosen).y
    assert(hard_shadow < soft_shadow)
    # A condenser makes a silver negative print harder still.
    chosen.grade = 2.0
    chosen.enlarger = 1
    assert(printed([0.045, 0.045, 0.045], &chosen).y < soft_shadow)

test "a color paper prints grey neutral and filtration moves its color":
    var chosen = PaperSettings(paper = 3)
    let grey = printed([0.18, 0.18, 0.18], &chosen)
    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.03 and math.abs(y - 0.329) < 0.03)
    assert(grey.y > 0.1 and grey.y < 0.35)
    chosen.filtration = [20.0, 0.0, 0.0]
    let yellowed = printed([0.18, 0.18, 0.18], &chosen)
    # Less blue exposure: less yellow dye, the print bluer.
    assert(yellowed.z / yellowed.y > grey.z / grey.y)

test "a matte print's blacks are lighter than a glossy one's, as Kodak measured":
    # Polycontrast IV RC's black: glossy F reads about 2.1, matte N 1.7 (the
    # sheet's curves); seen in a room each takes 1 % more white. Matte comes out
    # 0.15–0.35 lighter in its deepest black than glossy.
    var glossy = PaperSettings(paper = 2, grade = 3.0)
    var matte = PaperSettings(paper = 2, grade = 3.0, surface = 2)
    let black: f64[3] = [0.0002, 0.0002, 0.0002]
    let glossy_black = -math.log10(printed(black, &glossy).y)
    let matte_black = -math.log10(printed(black, &matte).y)
    assert(glossy_black - matte_black > 0.15 and glossy_black - matte_black < 0.35)
    # The whites stay where they were.
    let white: f64[3] = [50.0, 50.0, 50.0]
    assert(math.abs(printed(white, &glossy).y - printed(white, &matte).y) < 0.02)