Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-develop

Source of release 0.1.0

0.1.0 / src / process.lucb

## The develop on the CPU: shaders/develop.frag step for step, reading the same
## tables (tables.lucb, film.lucb) with the same filtering, except that the view is
## evaluated exactly instead of through its table. Export uses it, and the
## tests hold the GPU to it. Change both together.
import memory
from luce_std import parallel
from luce_std import math
from luce_color import transfer
from settings import Settings, View
import settings as settings_module
from scene import Scene
from picture import Picture
from tables import Tables
import scene
import tables
import view
import film_print
import lens
import film_paper
import geometry
import film
import film_passes
import film_gpu
from film import Film

## AP1's luminance weights, and AP1 (D60) to and from Oklab's LMS (D65): the
## shader's constants, row-major here.
let ap1_luminance: f64[3] = [0.272228717, 0.674081766, 0.053689517]
let to_lms: f64[9] = [0.631694013, 0.348861603, 0.019335190, 0.270070163, 0.630951453, 0.099005298, 0.098790269, 0.185321549, 0.716367829]
let from_lms: f64[9] = [2.068802739, -1.175172287, 0.106575960, -0.876328522, 2.149760160, -0.273454027, -0.058594228, -0.394073107, 1.451975109]

func apply(m: f64[9], v: f64[3]) -> f64[3]:
    return [m[0] * v[0] + m[1] * v[1] + m[2] * v[2], m[3] * v[0] + m[4] * v[1] + m[5] * v[2], m[6] * v[0] + m[7] * v[1] + m[8] * v[2]]

func smooth01(edge0: f64, edge1: f64, x: f64) -> f64:
    let t = math.clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0)
    return t * t * (3.0 - 2.0 * t)

func luminance(c: f64[3]) -> f64:
    return c[0] * ap1_luminance[0] + c[1] * ap1_luminance[1] + c[2] * ap1_luminance[2]

## Everything one pixel's develop needs besides its value and place.
struct Develop:
    var matrix: f64[9]
    var tables: Tables
    var shown: View
    ## The print view's silver kept (0..1); the lens's veiling glare (lens.lucb).
    var silver: f64
    var veil: f64
    ## How a paper print is made (the paper view), and the print material (the
    ## print view: 0 2383, 1 dye transfer).
    var paper: film_paper.PaperSettings
    var material: i64
    var to_display: f64[9]
    var vibrance: f64
    var saturation: f64
    var aspect: f64
    ## Output (0..1) to source (0..1): geometry.frame_map.
    var map: geometry.Matrix
    ## The display the view renders for (its primaries; sRGB's curve encodes either).
    var output: view.Output
    ## The film stock's model and tables, when one is chosen.
    var film: Film

func radius(u: f64, v: f64, aspect: f64) -> f64:
    let x = (u - 0.5) * 2.0 * aspect
    let y = (v - 0.5) * 2.0
    return math.sqrt(x * x + y * y) / math.sqrt(aspect * aspect + 1.0)

## The lens: vignetting corrected, then its veiling glare, an even light.
func with_lens(d: const Develop*, c: f64[3], u: f64, v: f64) -> f64[3]:
    let value = d.tables.value(5)
    var gain = 1.0
    if value[1] > 0.0:
        let start = value[2] * 0.6
        let t = math.clamp((radius(u, v, d.aspect) - start) / (1.0 - start), 0.0, 1.0)
        gain = 1.0 + value[1] * 2.0 * t * t
    return [c[0] * gain + d.veil, c[1] * gain + d.veil, c[2] * gain + d.veil]

func with_tone(d: const Develop*, c: f64[3]) -> f64[3]:
    let y = luminance(c)
    if y <= 1.0e-10:
        return c
    let gain = math.exp2(d.tables.sample(0, (math.log2(y / 0.18) + 14.0) / 24.0)[0])
    return [c[0] * gain, c[1] * gain, c[2] * gain]

func with_color(d: const Develop*, c: f64[3]) -> f64[3]:
    let y = luminance(c)
    if y <= 1.0e-10:
        return c
    let shape = d.tables.value(4)
    let s = math.log2(y / 0.18)
    let width = 1.0 + 3.0 * shape[1]
    let low = -2.0 + 2.0 * shape[0]
    let high = 2.0 + 2.0 * shape[0]
    let w_shadows = 1.0 - smooth01(low - width, low + width, s)
    let w_highlights = smooth01(high - width, high + width, s)
    let w_midtones = math.max(0.0, 1.0 - w_shadows - w_highlights)
    let lms = apply(to_lms, c)
    let l_ = math.cbrt(lms[0])
    let m_ = math.cbrt(lms[1])
    let s_ = math.cbrt(lms[2])
    let l = 0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_
    let a = 1.9779984951 * l_ - 2.4285922050 * m_ + 0.4505937099 * s_
    let b = 0.0259040371 * l_ + 0.7827717662 * m_ - 0.8086757660 * s_
    let chroma = math.sqrt(a * a + b * b)
    var hue = math.atan2(b, a) / (2.0 * math.pi) + 1.0
    hue -= math.floor(hue)
    let scale = (1.0 + d.saturation) * (1.0 + d.vibrance * (1.0 - smooth01(0.0, 0.2, chroma)))
    let band = d.tables.sample(1, hue)
    let turned = 2.0 * math.pi * band[0]
    var a2 = math.cos(turned) * a - math.sin(turned) * b
    var b2 = math.sin(turned) * a + math.cos(turned) * b
    let factor = math.max(scale * band[1], 0.0)
    a2 *= factor
    b2 *= factor
    var lightness = l * (1.0 + (band[2] - 1.0) * smooth01(0.0, 0.1, chroma))
    let g0 = d.tables.value(0)
    let g1 = d.tables.value(1)
    let g2 = d.tables.value(2)
    let g3 = d.tables.value(3)
    a2 += w_shadows * g0[0] + w_midtones * g1[0] + w_highlights * g2[0] + g3[0]
    b2 += w_shadows * g0[1] + w_midtones * g1[1] + w_highlights * g2[1] + g3[1]
    lightness *= (w_shadows * g0[2] + w_midtones * g1[2] + w_highlights * g2[2]) * g3[2]
    if shape[2] > 0.5:
        lightness *= math.max(0.0, 1.0 + band[3] * chroma * 3.0)
        a2 = shape[3]
        b2 = d.tables.value(5)[0]
    let l2 = lightness + 0.3963377774 * a2 + 0.2158037573 * b2
    let m2 = lightness - 0.1055613458 * a2 - 0.0638541728 * b2
    let s2 = lightness - 0.0894841775 * a2 - 1.2914855480 * b2
    return apply(from_lms, [l2 * l2 * l2, m2 * m2 * m2, s2 * s2 * s2])

func encode(c: f64) -> f64:
    return c * 12.92 if c < 0.0031308 else 1.055 * math.pow(math.max(c, 0.0), 1.0 / 2.4) - 0.055

func decode(c: f64) -> f64:
    return c / 12.92 if c < 0.04045 else math.pow((c + 0.055) / 1.055, 2.4)

func with_curves(d: const Develop*, c: f64[3]) -> f64[3]:
    var e: f64[3]
    for at in 0..<(usize)3:
        e[at] = d.tables.sample(2, encode(math.clamp(c[at], 0.0, 1.0)))[at]
    let luma = e[0] * 0.2126 + e[1] * 0.7152 + e[2] * 0.0722
    if luma > 1.0e-5:
        let ratio = d.tables.sample(2, luma)[3] / luma
        for at in 0..<(usize)3:
            e[at] = math.min(e[at] * ratio, 1.0)
    return [decode(e[0]), decode(e[1]), decode(e[2])]

func with_vignette(d: const Develop*, c: f64[3], u: f64, v: f64) -> f64[3]:
    let v5 = d.tables.value(5)
    let v6 = d.tables.value(6)
    let amount = v5[3]
    if amount == 0.0:
        return c
    let stretch = 1.0 + (d.aspect - 1.0) * (v6[1] + 1.0) * 0.5
    let x = (u - 0.5) * 2.0 * stretch
    let y = (v - 0.5) * 2.0
    let r = math.sqrt(x * x + y * y) / math.sqrt(stretch * stretch + 1.0)
    let start = v6[0]
    let edge = smooth01(start, start + math.max(v6[2], 0.02) * (1.2 - start), r)
    var out = c
    for at in 0..<(usize)3:
        out[at] = c[at] * (1.0 + amount * edge) if amount < 0.0 else c[at] + (1.0 - c[at]) * (amount * edge)
    return out

## One pixel's develop at output position (`u`, `v`), 0..1, to display-linear
## sRGB: black where the geometry leaves the photo.
func develop_pixel(d: const Develop*, source: const Scene*, u: f64, v: f64) -> f64[3]:
    let mx = d.map[0] * u + d.map[1] * v + d.map[2]
    let my = d.map[3] * u + d.map[4] * v + d.map[5]
    let mz = d.map[6] * u + d.map[7] * v + d.map[8]
    if mz <= 0.0:
        return [0.0, 0.0, 0.0]
    let su = mx / mz
    let sv = my / mz
    if su < 0.0 or su > 1.0 or sv < 0.0 or sv > 1.0:
        return [0.0, 0.0, 0.0]
    var c = apply(d.matrix, source.sample(su, sv))
    for at in 0..<(usize)3:
        c[at] = math.max(c[at], 0.0)
    # With film the scene is the filmed one, its lens vignetting done on the negative.
    if not d.film.active():
        c = with_lens(d, c, su, sv)
    c = with_tone(d, c)
    c = with_color(d, c)
    c = view.display(d.shown, c, d.to_display, d.output, d.silver, d.paper, d.material)
    c = with_curves(d, c)
    return with_vignette(d, c, u, v)

## A render under way: the develop, the scene, and where rows go: 8-bit RGBA
## (`bytes`) or 16-bit RGBA (`words`), both sRGB-encoded.
struct Rendering:
    var develop: Develop
    var source: const Scene*
    var width: usize
    var height: usize
    var bytes: u8[]
    var words: u16[]
    ## With film, the filmed scene the develop reads instead, and the samples
    ## across the frame its scan reads it at (0: the scene's own pixels).
    var filmed: Scene
    var scan: usize

    ## Film the scene, when film is on: from here the develop reads the filmed
    ## scene through the identity; with `samples`, the stochastic render (that
    ## many a pixel). On the GPU unless `cpu` (or there is none): the CPU's film
    ## is the reference the GPU is held to, and far slower (tens of seconds
    ## for a 3072 px picture against a few). Call it where the rendering stays.
    func film_scene(samples: usize = 0, cpu: bool = false) -> !:
        if not self.develop.film.active():
            return
        let aspect = (f64)self.source.width() / (f64)self.source.height()
        # Filmed at the scan's samples where the scene is finer (film.scan_samples),
        # and read from there as any scene is, filtered up to the output.
        var sampled = Scene()
        defer sampled.close()
        var filming = self.source
        if self.scan > 0 and (usize)self.source.width() > self.scan:
            sampled = try self.source.sampled(self.scan)
            filming = &sampled
        if not cpu:
            # On the GPU, as the viewer films it; on the CPU where there is none.
            self.filmed = film_gpu.filmed_on_gpu(&self.develop.film, filming, self.develop.matrix, &self.develop.tables, aspect, self.develop.veil,
                                                 (u32)samples) catch failure:
                recover try film_passes.filmed(&self.develop.film, filming, self.develop.matrix, &self.develop.tables, aspect,
                                               self.develop.veil, samples)
        else:
            self.filmed = try film_passes.filmed(&self.develop.film, filming, self.develop.matrix, &self.develop.tables, aspect, self.develop.veil,
                                                 samples)
        self.source = &self.filmed
        self.develop.matrix = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
        # The film took the lens's veil in.
        self.develop.veil = 0.0

    func release():
        self.develop.film.release()
        self.filmed.close()

func render_row(context: void*, y: usize, worker: usize) -> !:
    let job = (Rendering*)context
    let v = ((f64)y + 0.5) / (f64)job.height
    for x in 0..<job.width:
        let shown = develop_pixel(&job.develop, job.source, ((f64)x + 0.5) / (f64)job.width, v)
        let at = y * job.width + x
        if job.bytes.length > 0:
            job.bytes[at * 4] = transfer.srgb_byte(shown[0])
            job.bytes[at * 4 + 1] = transfer.srgb_byte(shown[1])
            job.bytes[at * 4 + 2] = transfer.srgb_byte(shown[2])
            job.bytes[at * 4 + 3] = 255
        else:
            for channel in 0..<(usize)3:
                let encoded = transfer.srgb_encode(math.clamp(shown[channel], 0.0, 1.0))
                job.words[at * 4 + channel] = (u16)(encoded * 65535.0 + 0.5)
            job.words[at * 4 + 3] = 65535

func rendering(source: const Scene*, wanted: const Settings*, output: view.Output) -> Rendering!:
    # What renders: the settings with only the recipe's stages at work.
    let compiled = settings_module.compile(wanted)
    let settings = &compiled
    # The output is the crop at the scene's scale.
    let (out_width, out_height) = geometry.output_size(settings, (f64)source.width(), (f64)source.height())
    let width = (usize)out_width
    let height = (usize)out_height
    let camera = film.camera_settings(settings)
    let matrix = try source.matrix(&camera)
    let mean = source.average()
    let veil = lens.veil(settings, [matrix[0] * mean[0] + matrix[1] * mean[1] + matrix[2] * mean[2],
                                    matrix[3] * mean[0] + matrix[4] * mean[1] + matrix[5] * mean[2],
                                    matrix[6] * mean[0] + matrix[7] * mean[1] + matrix[8] * mean[2]])
    let develop = Develop(matrix = matrix, tables = tables.build(settings), veil = veil, paper = film.paper_settings(settings), material = (i64)settings.get(.print_material),
                          shown = (View)(i64)settings.get(.view), silver = film_print.silver_kept(settings.get(.print_silver)), to_display = view.working_to_output(output), output = output,
                          vibrance = settings.get(.vibrance) / 100.0, saturation = settings.get(.saturation) / 100.0,
                          aspect = out_width / out_height, map = geometry.frame_map(settings, (f64)source.width(), (f64)source.height()))
    var out = Rendering(develop = develop, source = source, width = width, height = height, scan = film.scan_samples(settings))
    out.develop.film.prepare(settings, try source.film_input(&camera)) catch failure:
        out.develop.film.release()
        error(failure.code, failure.message)
    return out

## `scene` developed with `settings` for `output` (sRGB or Display P3 primaries,
## sRGB's curve) into an 8-bit picture of its size, on every processor; film
## grain by the stochastic render with `samples` a pixel (0: the statistical),
## filmed on the GPU unless `cpu` (the reference the GPU is tested against).
pub func render(source: const Scene*, settings: Settings, output: view.Output = view.Output.srgb, samples: usize = 0, cpu: bool = false) -> Picture!:
    var job = try rendering(source, &settings, output)
    defer job.release()
    try job.film_scene(samples, cpu)
    var out = try Picture.create(job.width, job.height)
    errdefer out.close()
    job.bytes = out.bytes()
    try parallel.for_each(job.height, (void*)&job, render_row)
    return out

## `scene` developed with `settings` for `output` into 16-bit RGBA (alpha opaque), rows
## packed, on every processor; the caller frees it.
pub func render16(source: const Scene*, settings: Settings, output: view.Output = view.Output.srgb, samples: usize = 0) -> u16[]!:
    var job = try rendering(source, &settings, output)
    defer job.release()
    try job.film_scene(samples)
    job.words = try new u16[job.width * job.height * 4] --- in memory.heap
    errdefer free(job.words) in memory.heap
    try parallel.for_each(job.height, (void*)&job, render_row)
    return job.words

## The photo at `path` as a scene at most `edge` pixels on its longer side (0:
## full size), turned as `settings` say: what an export or a library's
## developed picture renders.
pub func opened(path: str, settings: const Settings*, edge: usize) -> Scene!:
    var out = try scene.open(path, edge)
    errdefer out.close()
    try out.turn((i64)settings.get(.geometry_turn))
    return out