dymokomi/luce-raw
Source of release 0.8.1
0.8.1 / src / raw / matrices.lucb
#==============================================================================================
#
# raw/matrices - The color matrix of a proprietary raw's camera
#
# DESCRIPTION:
# Proprietary raws carry no calibrated color matrix, so each camera's comes
# from the tables in matrices_*.lucb: Adobe's XYZ(D65)-to-camera matrix, with
# the black and white levels dcraw and LibRaw know for that model. A camera
# missing from the tables develops with a generic matrix (camera RGB taken as
# sRGB) and says so through Info.matrix and the "raw.matrix" attribute.
#
#==============================================================================================
## One camera's color facts, as the tables list them.
struct CameraColor:
var prefix: str
var black: i32
var white: i32
var xyz: i32[9]
## The table for a maker's name; Ricoh-made Pentax bodies use Pentax's.
func colors_for(make: str, model: str) -> const CameraColor[]:
var none_found: const CameraColor[]
if starts(make, "RICOH") and starts(model, "PENTAX"): return pentax_colors
if starts(make, "Canon"): return canon_colors
if starts(make, "NIKON"): return nikon_colors
if starts(make, "SONY"): return sony_colors
if starts(make, "Panasonic"): return panasonic_colors
if starts(make, "LEICA"): return leica_colors
if starts(make, "FUJIFILM"): return fujifilm_colors
if starts(make, "OLYMPUS") or starts(make, "OM Digital"): return olympus_colors
if starts(make, "PENTAX"): return pentax_colors
if starts(make, "RICOH"): return ricoh_colors
return none_found
## The model name as the tables spell it: without a leading maker word
## (or "PENTAX", which Ricoh-made bodies carry).
func table_model(make: str, model: str) -> str:
if model.length > 7 and starts(model, "PENTAX "):
return (str)model.bytes[7..]
var word: usize = 0
while word < make.length and make.bytes[word] != ' ':
word += 1
if word > 0 and model.length > word + 1 and model.bytes[word] == ' ' and starts(model, (str)make.bytes[..<word]):
return (str)model.bytes[word + 1..]
return model
## `model` spelled as the tables spell it, into `buffer`: Canon's "R6m2" and
## Olympus's "E-M1MarkII" become "R6 Mark II" and "E-M1 Mark II", as LibRaw
## normalizes them.
func normalized_model(model: str, buffer: u8[]) -> str:
let b = model.bytes
var n: usize = 0
var i: usize = 0
while i < b.length and n + 12 < buffer.length:
var suffix: str = ""
var skip: usize = 0
if i + 2 == b.length and i > 0 and b[i - 1] >= '0' and b[i - 1] <= '9' and b[i] == 'm' and b[i + 1] >= '2' and b[i + 1] <= '4':
suffix = " Mark II" if b[i + 1] == '2' else " Mark III" if b[i + 1] == '3' else " Mark IV"
skip = 2
elif i + 4 <= b.length and (str)b[i..<i + 4] == "Mark" and i > 0 and b[i - 1] != ' ':
suffix = " Mark"
skip = 4
if skip > 0:
for c in suffix.bytes:
buffer[n] = c
n += 1
i += skip
if suffix == " Mark" and i < b.length and b[i] != ' ':
buffer[n] = ' '
n += 1
continue
buffer[n] = b[i]
n += 1
i += 1
return (str)buffer[..<n]
## Give `meta` its camera's matrix, and its levels when `levels`, from the
## tables, or the generic matrix when the camera is not listed. A DNG without
## color matrices takes the matrix only: its levels are its own.
func lookup_matrix(meta: Meta*, levels: bool):
var buffer: u8[96]
let model = normalized_model(table_model(meta.make, meta.model), buffer)
meta.profile = Profile(matrices = 1)
meta.profile.color1 = [3.2404542, -1.5371385, -0.4985314, -0.9692660, 1.8760108, 0.0415560, 0.0556434, -0.2040259, 1.0572252]
meta.matrix = "generic"
for entry in colors_for(meta.make, meta.model):
if not starts(model, entry.prefix):
continue
let stated = meta.black[0] != 0.0 or meta.mask[2] > meta.mask[0]
if levels and (entry.black > 0 or (entry.black < 0 and not stated)):
meta.black_rows = 1
meta.black_columns = 1
meta.black[0] = (f64)(entry.black if entry.black > 0 else -entry.black)
meta.mask = [0, 0, 0, 0]
if levels and entry.white > 0:
meta.white = [(f64)entry.white, (f64)entry.white, (f64)entry.white]
if entry.xyz[0] != 0 or entry.xyz[4] != 0:
for i: usize in 0..<9:
meta.profile.color1[i] = (f64)entry.xyz[i] / 10000.0
meta.matrix = "table"
return