Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-raw

Source of release 0.8.0

0.8.0 / src / raw / module.lucb

#==============================================================================================
#
#   raw - Camera RAW files to linear-light RGB
#
#   DESCRIPTION:
#       Reads the sensor data of a camera RAW file and develops it into scene-linear
#       RGB: black and white levels, white balance, highlight clipping, demosaicing
#       and the camera's color matrix into linear sRGB, then orientation. Everything
#       is Luce Base; no platform codec is used. Fragments, in ORDER:
#
#       module.lucb    public options, white balance and file information types
#       tiff.lucb      TIFF directories and their entries, maker notes included
#       bits.lucb      bit readers and canonical Huffman tables
#       ljpeg.lucb     lossless JPEG (ITU T.81 process 14) with tiles and slices
#       meta.lucb      the facts a decoder gathers about one file
#       dng.lucb       DNG: raw directory, levels, crop and color tags
#       canon.lucb     Canon CR2, and the maker note CR3 shares
#       cr3.lucb       Canon CR3's ISO media container
#       crx_lines.lucb, crx_top.lucb, crx_wavelet.lucb, crx_image.lucb, crx_decode.lucb
#                      Canon's CRX coding: subband lines, wavelets, headers, planes
#       nikon.lucb     Nikon NEF and its lossless Huffman coding
#       sony.lucb      Sony ARW, ARW2 blocks and the SR2 white balance
#       olympus.lucb, panasonic.lucb, pentax.lucb, fujifilm.lucb
#                      Olympus ORF, Panasonic RW2, Pentax PEF and Fujifilm RAF
#       fuji_compressed.lucb, fuji_strips.lucb
#                      Fujifilm's compressed RAF coding
#       parallel.lucb  independent work items on several threads
#       serial.lucb    serial Huffman streams decoded on several threads
#       unpack.lucb    sensor samples out of the file into a 16-bit mosaic
#       matrices.lucb, matrices_*.lucb
#                      the table of camera color matrices for proprietary raws
#       color.lucb     matrices, xy and temperature/tint
#       profile.lucb   the DNG color model and the Development for a white balance
#       levels.lucb    black and white levels, white balance and clipping to [0, 1]
#       bilinear.lucb  the fast demosaic
#       ahd.lucb       adaptive homogeneity-directed demosaic
#       xtrans.lucb, xtrans_ranges.lucb, xtrans_tile.lucb, xtrans_steps.lucb
#                      Markesteijn's X-Trans demosaic
#       develop.lucb   tiles through demosaic, matrix and orientation into the Raster
#       pipeline.lucb  developing while Fujifilm and CRX data still decode
#       api.lucb       detect, probe, decode, info, white balance and sensor
#       recognize.lucb which files are camera raws at all
#       preview.lucb   the largest embedded JPEG
#       tests_codec.lucb  bit reader, Huffman and lossless JPEG tests
#       tests_dng.lucb    synthetic DNG decoding, white balance and fuzzing tests
#
#==============================================================================================

## Camera RAW decoding: DNG (uncompressed, lossless JPEG, Deflate; Bayer and
## LinearRaw), Canon CR2 and CR3, Nikon NEF, Sony ARW, Olympus ORF, Panasonic
## RW2, Pentax PEF and Fujifilm RAF (Bayer and X-Trans, uncompressed and
## compressed). `probe` and `decode` fill a luce-raster `Raster` with three
## float channels, R, G and B, in linear sRGB where the sensor's white level
## after white balance is 1.0 (times the file's baseline exposure); `develop`
## hands the same pixels to a sink as float32 tiles while it works, and
## `preview` finds the largest embedded JPEG. `info` and `white_balance` answer
## the as-shot white balance and the multipliers for any temperature and tint.
import memory
from luce_std import math
import thread
import os
import time
from luce_raster import binary
from luce_compress import compress
import testing
from luce_raster.binary import Reader, Buffer
from luce_raster.model import Raster, SampleType
from luce_raster.errors import corrupt, unsupported, invalid, limit

## How carefully to demosaic: `fast` is bilinear, `best` is AHD.
pub enum Quality:
    fast
    best

## A white balance, both as Temperature/Tint and as the multipliers that apply
## it to camera values. Temperature is in kelvin; tint is in Adobe's units,
## positive towards magenta. `multipliers` are the red, green and blue gains,
## scaled so the smallest is one.
pub struct WhiteBalance:
    pub var temperature: f64
    pub var tint: f64
    pub var multipliers: f64[3]

## A camera's color for one white balance (see `rendering`).
pub struct Rendering:
    pub var balance: WhiteBalance
    pub var to_srgb: f64[9]
    pub var gain: f64

## How to develop a raw. A zero `temperature` keeps the as-shot white balance;
## otherwise `temperature` and `tint` choose it. `exposure` applies the file's
## baseline exposure; `crop` applies the camera's default crop; `camera` skips
## the color matrix, leaving white-balanced camera RGB (for profiling); `half`
## develops at half size without demosaicing, one pixel per 2×2 filter cell (as
## LibRaw's half_size), for fast previews; `threads` zero means one per
## processor. `tile` and `alpha` shape `develop`'s tiles: their side in pixels,
## and RGBA with alpha one instead of RGB. `timings`, when set, receives how
## long each stage took.
pub struct Options:
    pub var quality: Quality = .best
    pub var temperature: f64 = 0.0
    pub var tint: f64 = 0.0
    pub var exposure: bool = true
    pub var crop: bool = true
    pub var camera: bool = false
    pub var half: bool = false
    pub var threads: usize = 0
    pub var tile: usize = 256
    pub var alpha: bool = false
    pub var timings: Timings*? = none

## One tile of a developed image, as `develop` hands it to its sink: the
## rectangle at (`x`, `y`) of `width` × `height` pixels, `channels` (3 for RGB,
## 4 for RGBA with alpha one) float32 values each, straight and linear, rows
## packed. `pixels` is valid only during the sink call.
pub struct Tile:
    pub var x: usize
    pub var y: usize
    pub var width: usize
    pub var height: usize
    pub var channels: usize
    pub var pixels: const f32[]

## How long a develop took, in nanoseconds: parsing, unpacking the sensor
## data, levels (black, white, white balance), and the develop proper (wall
## time); within it, `demosaic` and `output` (color matrix, gain, orientation
## and the sink) summed over the worker threads.
pub struct Timings:
    pub var parse: u64
    pub var unpack: u64
    pub var levels: u64
    pub var develop: u64
    pub var demosaic: u64
    pub var output: u64
    pub var total: u64

## What a raw file is: its format ("DNG", "CR2", "NEF", "ARW", "ORF", "RW2",
## "PEF" or "RAF"), camera, sensor and developed sizes, the as-shot white
## balance, the baseline exposure in stops, and where the color matrix came
## from: "dng" (the file's own), "table" (this package's matrix table) or
## "generic" (no matrix for this camera; an approximate sRGB-like fallback).
## The strings view the file's bytes.
pub struct Info:
    pub var format: str
    pub var make: str
    pub var model: str
    pub var raw_width: usize
    pub var raw_height: usize
    pub var width: usize
    pub var height: usize
    pub var bits: usize
    pub var as_shot: WhiteBalance
    pub var baseline_exposure: f64
    pub var matrix: str
    pub var orientation: u64