Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-raw

Source of release 0.8.2

0.8.2 / src / raw / api.lucb

#==============================================================================================
#
#   raw/api - detect, probe, decode, develop, info, white balance and sensor
#
#   DESCRIPTION:
#       The public surface (recognize.lucb adds `recognize`, preview.lucb
#       `preview`). parse identifies the format and fills a Meta; probe sizes a
#       Raster from it without reading samples; develop unpacks, levels,
#       demosaics and hands linear sRGB tiles to a sink, and decode is develop
#       into the Raster. info and white_balance answer questions about the
#       camera's white balance for an editor's open dialog.
#
#==============================================================================================

## Whether this package can develop `data`: a DNG or a maker raw (CR2, CR3,
## NEF, ARW, ORF, RW2, PEF, RAF) in a coding it reads. Plain TIFFs answer false; for
## raws it cannot develop, `recognize` answers true and `probe` says why.
pub func detect(data: const u8[]) -> bool:
    var meta = parse(data) catch failure:
        return false
    meta.close()
    return true

## The format, camera and color facts of a raw file.
func parse(file: const u8[]) -> Meta!:
    var meta = Meta()
    errdefer meta.close()
    if file.length < 16:
        error(corrupt, "raw file is truncated")
    let foreign = foreign_raw(file)
    if foreign.length > 0:
        error(unsupported, foreign)
    if (str)file[..<8] == "FUJIFILM":
        try parse_raf(file, &meta)
    elif is_cr3(file):
        try parse_cr3(file, &meta)
    else:
        let tree = try Tree.walk(file, 0, 0)
        if try tree.holding(50706) != none:
            try parse_dng(file, &meta, &tree)
        else:
            let holder = try tree.holding(271) else error(unsupported, "TIFF file names no camera maker")
            let make = try holder.text(271)
            meta.make = make
            meta.model = try holder.text(272)
            if starts(make, "Canon"):
                try parse_cr2(file, &meta, &tree)
            elif starts(make, "NIKON"):
                try parse_nef(file, &meta, &tree)
            elif starts(make, "SONY"):
                try parse_arw(file, &meta, &tree)
            elif starts(make, "OLYMPUS") or starts(make, "OM Digital"):
                try parse_orf(file, &meta, &tree)
            elif starts(make, "Panasonic") or starts(make, "LEICA"):
                try parse_rw2(file, &meta, &tree)
            elif starts(make, "PENTAX") or starts(make, "RICOH"):
                try parse_pef(file, &meta, &tree)
            else:
                error(unsupported, "raw files from this camera maker are not supported")
    if not meta.profile.dng:
        lookup_matrix(&meta, true)
    elif meta.profile.matrices == 0:
        lookup_matrix(&meta, false)
    if meta.quarter_black:
        for i in 0..<meta.black.length:
            meta.black[i] = math.floor(meta.black[i] / 4.0)
        meta.white = [4095.0, 4095.0, 4095.0]
    try meta.settle()
    return meta

## Whether `text` begins with `prefix`, ignoring ASCII case.
func starts(text: str, prefix: str) -> bool:
    if text.length < prefix.length:
        return false
    let a = text.bytes
    let b = prefix.bytes
    for i in 0..<b.length:
        let x = a[i] | 32 if a[i] >= 'A' and a[i] <= 'Z' else a[i]
        let y = b[i] | 32 if b[i] >= 'A' and b[i] <= 'Z' else b[i]
        if x != y:
            return false
    return true

## The developed size of `meta`: crop or active area, halved for `half`, then
## orientation.
func developed_size(meta: const Meta*, crop: bool, half: bool) -> (usize, usize):
    var w = meta.crop_width if crop else meta.width
    var h = meta.crop_height if crop else meta.height
    if half:
        w = w // 2
        h = h // 2
    return (h, w) if meta.orientation >= 5 and meta.orientation <= 8 else (w, h)

## The size `develop` (or `decode`) gives `data` with `options`: width and
## height after crop, half size and orientation.
pub func dimensions(data: const u8[], options: Options = Options()) -> (usize, usize)!:
    var meta = try parse(data)
    defer meta.close()
    return developed_size(&meta, options.crop, options.half)

## What `data` is: format, camera, sizes, as-shot white balance, baseline
## exposure and the color matrix's source. The strings view `data`.
pub func info(data: const u8[]) -> Info!:
    var meta = try parse(data)
    defer meta.close()
    let look = try development(&meta, 0.0, 0.0)
    let (w, h) = developed_size(&meta, true, false)
    return Info(format = meta.format, make = meta.make, model = meta.model, raw_width = meta.raw_width, raw_height = meta.raw_height, width = w, height = h, bits = meta.bits, as_shot = look.balance, baseline_exposure = meta.baseline_exposure, matrix = meta.matrix, orientation = meta.orientation)

## The white balance of `data`'s camera at `temperature` (kelvin) and `tint`:
## the multipliers that make that light neutral. Zero temperature answers the
## as-shot white balance.
pub func white_balance(data: const u8[], temperature: f64, tint: f64) -> WhiteBalance!:
    if temperature < 0.0 or (temperature > 0.0 and (temperature < 1667.0 or temperature > 50000.0)) or tint < -150.0 or tint > 150.0:
        error(invalid, "temperature must be 1667-50000 K and tint within ±150")
    var meta = try parse(data)
    defer meta.close()
    return (try development(&meta, temperature, tint)).balance

## How `data`'s camera turns white-balanced values into color, for an editor that
## develops once with `camera` set and applies white balance and the color
## matrix itself: `balance` is `white_balance`'s for `temperature` and `tint`
## (zero temperature: as shot), and `to_srgb` takes values balanced with its
## multipliers to linear sRGB (D65), row-major. A develop with `camera` gives
## camera values balanced with the as-shot multipliers; another balance is
## those values times its multipliers over the as-shot ones, then its `to_srgb`.
## `gain` is the baseline exposure `Options.exposure` applies (2^stops).
pub func rendering(data: const u8[], temperature: f64 = 0.0, tint: f64 = 0.0) -> Rendering!:
    if temperature < 0.0 or (temperature > 0.0 and (temperature < 1667.0 or temperature > 50000.0)) or tint < -150.0 or tint > 150.0:
        error(invalid, "temperature must be 1667-50000 K and tint within ±150")
    var meta = try parse(data)
    defer meta.close()
    let look = try development(&meta, temperature, tint)
    return Rendering(balance = look.balance, to_srgb = look.to_rgb, gain = math.exp2(meta.baseline_exposure))

## Size `image` for `image.encoded`: three float channels R, G and B at the
## developed size, with the format, camera and matrix source as attributes
## ("make", "model", "raw.matrix").
pub func probe(image: Raster*, options: Options = Options()) -> !:
    var meta = try parse(image.encoded)
    defer meta.close()
    if image.frame != 0:
        error(invalid, "raw files have one frame")
    let (w, h) = developed_size(&meta, options.crop, options.half)
    image.width = w
    image.height = h
    image.bits = 32
    if image.channels.length == 0:
        try image.standard(3, .float32)
    _ = try image.validate()
    image.file_format = meta.format
    try image.attribute("make", "string", meta.make.bytes)
    try image.attribute("model", "string", meta.model.bytes)
    try image.attribute("raw.matrix", "string", meta.matrix.bytes)

## Develop `image.encoded` into `image.pixels` (allocated here when empty) as
## linear sRGB with `options`. `probe` must have sized the image with the same
## options.
pub func decode(image: Raster*, options: Options = Options()) -> !:
    var meta = try parse(image.encoded)
    defer meta.close()
    let (w, h) = developed_size(&meta, options.crop, options.half)
    if image.width != w or image.height != h or image.channels.length != 3:
        error(invalid, "raw image was not probed with these options")
    if image.pixels.length == 0:
        try image.allocate()
    if image.pixels.length != w * h * 3:
        error(invalid, "raw image pixels have the wrong size")
    var settings = options
    settings.alpha = false
    try develop((const u8[])image.encoded, settings, (void*)image, raster_sink)

## Copy one tile into the Raster `context`.
func raster_sink(context: void*?, tile: const Tile*) -> !:
    let image = (Raster*)(context else return)
    let pixels = image.pixels.data
    let source = tile.pixels.data
    for y in 0..<tile.height:
        let target = pixels + ((tile.y + y) * image.width + tile.x) * 3
        let row = source + y * tile.width * 3
        for i in 0..<tile.width * 3:
            target[i] = (f64)row[i]

## Develop `data` with `options` into tiles of the developed image (the size
## `dimensions` answers), handing each to `sink(context, tile)` as it is
## finished. Tiles are `options.tile` pixels square (smaller at the right and
## bottom edges), aligned to multiples of it, float32 RGB or RGBA in linear
## sRGB (or camera RGB with `camera`). The sink runs on worker threads, several
## at once and in no particular order; a failure it returns stops the develop
## and is returned.
pub func develop(data: const u8[], options: Options, context: void*?, sink: func(void*?, const Tile*) -> unit!) -> !:
    if options.temperature != 0.0 and (options.temperature < 1667.0 or options.temperature > 50000.0 or options.tint < -150.0 or options.tint > 150.0):
        error(invalid, "temperature must be 1667-50000 K and tint within ±150")
    let t0 = time.now()
    var meta = try parse(data)
    defer meta.close()
    var look = try development(&meta, options.temperature, options.tint)
    if options.camera:
        look.to_rgb = diagonal([1.0, 1.0, 1.0])
    let threads = options.threads if options.threads > 0 else (usize)os.cpus()
    let t1 = time.now()
    if pipelines(&meta, options, threads):
        try develop_pipelined(data, &meta, &look, options, threads, context, sink, t1 - t0)
        return
    var mosaic = try unpack(data, &meta, threads)
    defer mosaic.close()
    meta.measure_black(&mosaic)
    let t2 = time.now()
    var plane = try normalize(&meta, &mosaic, &look, options.crop, options.half, threads)
    defer plane.close()
    mosaic.close()
    let t3 = time.now()
    let gain = math.exp2(meta.baseline_exposure) if options.exposure else 1.0
    let (demosaic_ns, output_ns) = try develop_plane(&plane, &look, options.quality, gain, meta.orientation, options.tile, options.alpha, threads, context, sink)
    let t4 = time.now()
    if let timings = options.timings:
        *timings = Timings(parse = t1 - t0, unpack = t2 - t1, levels = t3 - t2, develop = t4 - t3, demosaic = demosaic_ns, output = output_ns, total = t4 - t0)

## A raw's sensor samples after the file's linearization, with the facts that
## normalize them: for inspecting a raw or checking a decoder against another.
## `cfa` and `black` are the 2×2 colors (0 red, 1 green, 2 blue) and black
## levels at the active area's top-left; `pattern` is the 6×6 filter from that
## corner (`xtrans` when it is Fujifilm's X-Trans rather than a tiled 2×2); `samples` is owned, release it with
## `close`.
pub struct Sensor:
    pub var samples: u16[]
    pub var width: usize
    pub var height: usize
    pub var channels: usize
    pub var top: usize
    pub var left: usize
    pub var active_width: usize
    pub var active_height: usize
    pub var cfa: u8[4]
    pub var pattern: u8[36]
    pub var xtrans: bool
    pub var black: f64[4]
    pub var white: f64

    ## Free the samples.
    pub func close():
        if self.samples.length > 0:
            free(self.samples) in memory.heap
        self = Sensor()

## The sensor samples and levels of `data`.
pub func sensor(data: const u8[]) -> Sensor!:
    var meta = try parse(data)
    defer meta.close()
    var mosaic = try unpack(data, &meta, (usize)os.cpus())
    meta.measure_black(&mosaic)
    var result = Sensor(samples = mosaic.data, width = mosaic.width, height = mosaic.height, channels = mosaic.samples, top = meta.top, left = meta.left, active_width = meta.width, active_height = meta.height, cfa = meta.cfa, pattern = meta.pattern, xtrans = meta.xtrans, white = meta.white[0])
    for i: usize in 0..<4:
        let at = ((i // 2) % meta.black_rows * meta.black_columns + (i % 2) % meta.black_columns) * meta.samples
        result.black[i] = meta.black[at]
    return result