dymokomi/luce-raw
Source of release 0.8.1
#!/usr/bin/env python3
"""Regenerate src/raw/matrices_*.lucb from LibRaw's colordata.cpp.
The matrices are Adobe DNG Converter's XYZ(D65)-to-camera matrices, times 10000,
as dcraw's adobe_coeff and LibRaw's colordata publish them, with the black and
white levels those tables give. Usage: tools/matrices.py PATH/TO/colordata.cpp
"""
import re, sys
from pathlib import Path
MAKERS = { # LibRaw maker: (file suffix, table name)
"Canon": ("canon", "canon_colors"), "Nikon": ("nikon", "nikon_colors"),
"Sony": ("sony", "sony_colors"), "Panasonic": ("panasonic", "panasonic_colors"),
"Leica": ("panasonic", "leica_colors"), "Fujifilm": ("fujifilm", "fujifilm_colors"),
"Olympus": ("olympus", "olympus_colors"), "Pentax": ("pentax", "pentax_colors"),
"Ricoh": ("pentax", "ricoh_colors"),
}
# Bodies LibRaw's tables do not list yet, appended after their maker's rows while
# LibRaw lacks them: Adobe DNG Converter's matrix as darktable's rawspeed publishes
# it (data/cameras.xml). The X-E5's is the X-T5's: the same 40 MP X-Trans 5 HR sensor.
EXTRA = {
"Fujifilm": [("X-E5", 0, 0, [11809, -5358, -1141, -4248, 12164, 2343, -514, 1097, 5848])],
}
ROOT = Path(__file__).resolve().parents[1] / "src/raw"
def number(text):
text = text.strip()
return int(text, 16) if text.lower().startswith(("0x", "-0x")) else int(text)
def main():
source = Path(sys.argv[1]).read_text()
source = re.sub(r"/\*.*?\*/", "", source, flags=re.S)
source = re.sub(r"//[^\n]*", "", source)
entry = re.compile(r'\{\s*LIBRAW_CAMERAMAKER_(\w+)\s*,\s*"([^"]*)"\s*,\s*(-?\w+)\s*,\s*(-?\w+)\s*,\s*\{([^}]*)\}\s*\}')
tables = {}
for maker, prefix, black, white, trans in entry.findall(source):
if maker not in MAKERS:
continue
values = [number(v) for v in trans.split(",") if v.strip()][:9]
values += [0] * (9 - len(values))
tables.setdefault(maker, []).append((prefix, number(black), number(white), values))
for maker, rows in EXTRA.items():
listed = {row[0] for row in tables.get(maker, [])}
tables.setdefault(maker, []).extend(row for row in rows if row[0] not in listed)
files = {}
for maker, rows in tables.items():
suffix, name = MAKERS[maker]
lines = [f"## {maker}: model prefix, black (negative: only when the file states none),",
f"## white (zero: the file's), XYZ-to-camera matrix times 10000.",
f"let {name}: CameraColor[{len(rows)}] = ["]
for prefix, black, white, values in rows:
lines.append(f' CameraColor(prefix = "{prefix}", black = {black}, white = {white}, xyz = [{", ".join(map(str, values))}]),')
lines.append("]")
files.setdefault(suffix, []).append("\n".join(lines))
for suffix, blocks in files.items():
header = ["#" + "=" * 94, "#", f"# raw/matrices_{suffix} - Camera color matrices: {suffix.capitalize()}", "#",
"# DESCRIPTION:",
"# Generated by tools/matrices.py from LibRaw's colordata.cpp: Adobe DNG Converter's",
"# matrices as dcraw's adobe_coeff publishes them. Entries are searched in order,",
"# so a longer prefix precedes the shorter one it extends. Bodies LibRaw does not",
"# list yet follow their maker's rows (tools/matrices.py EXTRA).", "#", "#" + "=" * 94, ""]
(ROOT / f"matrices_{suffix}.lucb").write_text("\n".join(header) + "\n" + "\n\n".join(blocks) + "\n")
print(suffix, sum(b.count("CameraColor(") for b in blocks))
if __name__ == "__main__":
main()