Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-develop

Source of release 0.1.0

0.1.0 / src / recipe.lucb

## Recipes: the film process a photo goes through. Every recipe is one chain:
## the file, the camera (a white balance, the exposure, a lens, a filter), the
## film and its development, then an output: a telecine (scanned, graded, seen
## on a display), a release print (printed on Vision 2383 and projected), a
## slide on a lightbox, a print on paper, or a digital intermediate (scanned,
## graded, recorded back to film, printed and projected); the easel frames
## what shows. The white balance, the lens and the filter are each there or
## not; every other stage follows from the output.
##
## Each stage owns sections of the develop parameters: what a photo renders
## with (settings.compile) is its settings with only its recipe's stages at
## work.
from luce_std.collections import Buffer

# mark: Stages ==================================================================

pub let stage_count: usize = 16

## The stages, in the order a photo goes through them.
pub enum Stage as i64:
    file = 0
    white_balance = 1
    exposure = 2
    lens = 3
    filter = 4
    film = 5
    develop = 6
    scan = 7
    grade = 8
    record = 9
    print = 10
    project = 11
    lightbox = 12
    paper = 13
    view = 14
    easel = 15

let stage_names: str[16] = ["File", "White Balance", "Exposure", "Lens", "Filter", "Film", "Develop", "Scan", "Grade", "Record", "Print", "Project",
                                "Lightbox", "Paper", "View", "Easel"]
let stage_keys: str[16] = ["file", "white_balance", "exposure", "lens", "filter", "film", "develop", "scan", "grade", "record", "print", "project",
                           "lightbox", "paper", "view", "easel"]
## The develop sections each stage owns, "|" between.
let owned: str[16] = ["", "White Balance", "Exposure", "Lens", "Filter", "Film", "Develop", "Scan", "Tone|Levels|Curves|Color|Color Mixer|Color Grading|Black & White",
                      "", "Print", "", "", "Paper", "View", "Geometry|Crop|Vignette"]

## For Luce: how many stages there are, and stage `at`'s name and key.
pub func stage_total() -> i64:
    return (i64)stage_count
pub func stage_name(at: i64) -> str:
    return stage_names[(usize)at]
pub func stage_key(at: i64) -> str:
    return stage_keys[(usize)at]

## The stage owning develop section `section`, or -1.
pub func owner_of(section: str) -> i64:
    for at in 0..<stage_count:
        let list = owned[at]
        var first: usize = 0
        let bytes = list.bytes
        for index in 0..=bytes.length:
            if index == bytes.length or bytes[index] == '|':
                if (str)bytes[first..<index] == section:
                    return (i64)at
                first = index + 1
    return -1

## The stage whose key is `key` ("film"), or -1.
pub func key_stage(key: str) -> i64:
    for at in 0..<stage_count:
        if stage_keys[at] == key:
            return (i64)at
    return -1

## Whether stage `at` is one a recipe may leave out (white balance, lens, filter).
pub func stage_optional(at: i64) -> bool:
    return at == (i64)Stage.white_balance or at == (i64)Stage.lens or at == (i64)Stage.filter

# mark: Outputs =================================================================

## What a developed negative or slide goes to, and the stages each takes after
## Develop: six a row, the easel last and repeated to fill it.
pub let output_count: usize = 5
let output_names: str[5] = ["Telecine", "Release Print", "Lightbox", "Paper Print", "Digital Intermediate"]
let output_keys: str[5] = ["telecine", "release_print", "lightbox", "paper_print", "digital_intermediate"]
let output_row: usize = 6
let output_stages: Stage[30] = [.scan, .grade, .view, .easel, .easel, .easel,
                                .print, .project, .easel, .easel, .easel, .easel,
                                .lightbox, .easel, .easel, .easel, .easel, .easel,
                                .paper, .easel, .easel, .easel, .easel, .easel,
                                .scan, .grade, .record, .print, .project, .easel]

pub func output_total() -> i64:
    return (i64)output_count
pub func output_name(at: i64) -> str:
    return output_names[(usize)at]

# mark: Recipes =================================================================

pub struct Recipe:
    ## The output (output_names).
    pub var output: usize
    ## Whether the camera's optional stages are there.
    pub var balance: bool
    pub var lens: bool
    pub var filter: bool

    ## Whether `stage` is in the recipe (and so at work).
    pub func has(stage: i64) -> bool:
        if stage == (i64)Stage.white_balance:
            return self.balance
        if stage == (i64)Stage.lens:
            return self.lens
        if stage == (i64)Stage.filter:
            return self.filter
        if stage == (i64)Stage.file or stage == (i64)Stage.exposure or stage == (i64)Stage.film or stage == (i64)Stage.develop:
            return true
        for at in 0..<output_row:
            if (i64)output_stages[self.output * output_row + at] == stage:
                return true
        return false

    pub func equals(other: const Recipe*) -> bool:
        return self.output == other.output and self.balance == other.balance and self.lens == other.lens and self.filter == other.filter

    ## Put an optional camera stage (white balance, lens, filter) in or take it out.
    pub func set_used(stage: i64, used: bool):
        if stage == (i64)Stage.white_balance:
            self.balance = used
        elif stage == (i64)Stage.lens:
            self.lens = used
        elif stage == (i64)Stage.filter:
            self.filter = used

## The recipe a photo has when its settings name none: Television's.
pub func default_recipe() -> Recipe:
    return mode(0)

# mark: Modes ===================================================================

## Recipes to start from: each mode's output, its optional camera stages, the
## stock it starts with (film_stock: 0-based stock + 1) and the other settings
## it sets (text form). Black and white has a filter and no white balance.
## Super Panavision 70 is 2001: A Space Odyssey's chain: Eastman Color Negative
## 5251 on 65 mm (developed in its ECN-1), printed and projected. Digital
## Intermediate is the 2000s chain, as O Brother, Where Art Thou? was first.
## The darkrooms: Tri-X on Ilford Multigrade, Portra 400 on Endura.
pub let mode_count: usize = 8
let mode_names: str[8] = ["Television", "Cinema", "Slide", "Black & White", "Super Panavision 70", "Digital Intermediate", "Darkroom",
                              "Color Darkroom"]
let mode_recipes: Recipe[8] = [Recipe(output = 0, balance = true), Recipe(output = 1, balance = true), Recipe(output = 2, balance = true),
                               Recipe(output = 0, filter = true), Recipe(output = 1, balance = true), Recipe(output = 4, balance = true),
                               Recipe(output = 3, filter = true), Recipe(output = 3, balance = true)]
let mode_stocks: f64[8] = [3.0, 1.0, 5.0, 6.0, 22.0, 3.0, 18.0, 12.0]
let mode_extras: str[8] = ["", "", "", "", "film_format=65 mm 5-perf", "film_format=Super 35", "paper_stock=Ilford Multigrade IV RC", "paper_stock=Kodak Endura Premier"]

pub func mode_total() -> i64:
    return (i64)mode_count
pub func mode_name(at: i64) -> str:
    return mode_names[(usize)at]

## Mode `at`'s recipe, stock and other settings.
pub func mode(at: usize) -> Recipe:
    return mode_recipes[at]
pub func mode_stock(at: usize) -> f64:
    return mode_stocks[at]
pub func mode_extra(at: usize) -> str:
    return mode_extras[at]

# mark: Text form ===============================================================

## The recipe's text form, the value of settings' "recipe=": the output's key,
## then each optional camera stage there, "|" between ("telecine|white_balance").
pub func text_of(recipe: const Recipe*, out: Buffer*) -> !:
    try out.put_text(output_keys[recipe.output])
    for stage in [Stage.white_balance, Stage.lens, Stage.filter]:
        if recipe.has((i64)stage):
            try out.put_byte('|')
            try out.put_text(stage_keys[(usize)stage])

## A recipe from its text form; what it does not name is not there (an output
## it does not know: a telecine).
pub func parse(text: str) -> Recipe:
    var out = Recipe()
    let bytes = text.bytes
    var first: usize = 0
    var index: usize = 0
    for at in 0..=bytes.length:
        if at != bytes.length and bytes[at] != '|':
            continue
        let part = (str)bytes[first..<at]
        first = at + 1
        if index == 0:
            for output in 0..<output_count:
                if output_keys[output] == part:
                    out.output = output
        else:
            out.set_used(key_stage(part), true)
        index += 1
    return out

# mark: Tests ===================================================================

test "every mode's recipe has the camera, the film and its output":
    for at in 0..<mode_count:
        let made = mode(at)
        for stage in [Stage.file, Stage.exposure, Stage.film, Stage.develop, Stage.easel]:
            assert(made.has((i64)stage))
    let cinema = mode(1)
    assert(cinema.has((i64)Stage.print) and cinema.has((i64)Stage.project) and not cinema.has((i64)Stage.scan))
    let darkroom = mode(6)
    assert(darkroom.has((i64)Stage.filter) and not darkroom.has((i64)Stage.white_balance) and darkroom.has((i64)Stage.paper))
    let intermediate = mode(5)
    assert(intermediate.has((i64)Stage.scan) and intermediate.has((i64)Stage.record) and intermediate.has((i64)Stage.print))

test "the text form round-trips":
    for output in 0..<output_count:
        var made = Recipe(output = output, lens = true, filter = output % 2 == 0)
        var out: Buffer
        defer out.destroy()
        text_of(&made, &out) catch failure:
            assert(false)
            return
        let back = parse(out.text())
        assert(back.output == output and back.lens and not back.balance and back.filter == made.filter)
    assert(parse("lightbox|white_balance").output == 2 and parse("lightbox|white_balance").balance)