Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript
4.6 KB
Raw Blame
#!/usr/bin/env python3
"""Regenerates packages/core/src/color/fixtures/aces-ocio-reference.ts.

The reference values come from OpenColorIO's builtin ACES 1.3 transforms (an independent implementation of the
ACES CTL), not from OpenReel code:

  python3 -m venv /tmp/aces-venv && /tmp/aces-venv/bin/pip install opencolorio numpy colour-science
  /tmp/aces-venv/bin/python scripts/aces-reference.py

Inputs are ACEScg (AP1 linear) triples; outputs are the SDR-VIDEO_1.0 transform to sRGB / BT.1886 display codes and
the HDR-VIDEO-1000nit-15nit-REC2020lim_1.1 transform to Rec.2020 linear display light in nits.
"""
import json
import os

import numpy as np
import PyOpenColorIO as ocio

cfg = ocio.Config.CreateRaw()


def builtin(style):
    t = ocio.BuiltinTransform()
    t.setStyle(style)
    return t


def processor(*transforms):
    group = ocio.GroupTransform()
    for t in transforms:
        group.appendTransform(t)
    return cfg.getProcessor(group).getDefaultCPUProcessor()


to_aces = builtin("ACEScg_to_ACES2065-1")
sdr = processor(to_aces, builtin("ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - SDR-VIDEO_1.0"))
hdr = processor(to_aces, builtin("ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-VIDEO-1000nit-15nit-REC2020lim_1.1"))

rng = np.random.default_rng(7)
points = [[v, v, v] for v in [0, 0.0001, 0.001, 0.01, 0.05, 0.18, 0.5, 1, 2, 4, 8, 16, 100]]
points += [
    [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [0, 1, 1], [1, 0, 1],
    [0.5, 0.1, 0.05], [0.05, 0.3, 0.1], [0.1, 0.1, 0.6], [0.9, 0.5, 0.2],
    [0.18, 0.02, 0.05], [0.02, 0.18, 0.5], [4, 0.5, 0.2], [0.3, 2, 0.3], [0.12, 0.08, 0.06],
]
points += [[float(10 ** rng.uniform(-3, 1)) for _ in range(3)] for _ in range(40)]
src = np.array(points, dtype=np.float32)


def rgb_to_xyz(r, g, b, w):
    def xyz(x, y):
        return np.array([x / y, 1, (1 - x - y) / y])
    p = np.column_stack([xyz(*r), xyz(*g), xyz(*b)])
    return p * np.linalg.solve(p, xyz(*w))


m709 = rgb_to_xyz((0.64, 0.33), (0.3, 0.6), (0.15, 0.06), (0.3127, 0.329))
m2020 = rgb_to_xyz((0.708, 0.292), (0.17, 0.797), (0.131, 0.046), (0.3127, 0.329))

xyz_sdr = src.copy()
sdr.applyRGB(xyz_sdr)
xyz_hdr = src.copy()
hdr.applyRGB(xyz_hdr)
lin709 = np.clip((np.linalg.inv(m709) @ xyz_sdr.T.astype(np.float64)).T, 0, 1)
lin2020 = np.clip((np.linalg.inv(m2020) @ xyz_hdr.T.astype(np.float64)).T, 0, None)
srgb = np.where(lin709 <= 0.0031308, lin709 * 12.92, 1.055 * np.power(lin709, 1 / 2.4) - 0.055)

# Gamut -> ACEScg matrices. AP1 -> Rec.709 comes from OCIO's builtin; the rest from colour-science
# (Bradford adaptation to the ACES white), an independent implementation of the same published chromaticities.
def basis_matrix(style):
    p = processor(builtin(style))
    cols = np.eye(3, dtype=np.float32)
    p.applyRGB(cols)
    return cols.T.astype(np.float64)  # applyRGB maps each row; transpose gives column-vector form


matrices = {"ap1ToRec709": basis_matrix("UTILITY - ACES-AP1_to_LINEAR-REC709_BFD").tolist(), "ap1ToXyzD65": basis_matrix("UTILITY - ACES-AP1_to_CIE-XYZ-D65_BFD").tolist()}
try:
    import colour

    names = {
        "rec709": "ITU-R BT.709", "p3": "P3-D65", "bt2020": "ITU-R BT.2020", "sgamut3cine": "S-Gamut3.Cine", "vgamut": "V-Gamut",
        "awg3": "ARRI Wide Gamut 3", "awg4": "ARRI Wide Gamut 4", "cinemagamut": "Cinema Gamut", "bmdwg": "Blackmagic Wide Gamut",
    }
    acescg = colour.RGB_COLOURSPACES["ACEScg"]
    matrices["toAcescg"] = {
        key: colour.matrix_RGB_to_RGB(colour.RGB_COLOURSPACES[name], acescg, chromatic_adaptation_transform="Bradford").tolist()
        for key, name in names.items()
    }
except ImportError:
    print("colour-science not installed: keeping the previous toAcescg matrices")

round_all = lambda a, n: [[round(float(x), n) for x in row] for row in a]
out = {
    "note": "Generated by scripts/aces-reference.py with OpenColorIO builtin ACES 1.3 transforms. srgb / rec709: display code values 0..1. hdrNits: Rec.2020 linear display luminance in nits (SDR-VIDEO_1.0 and HDR-VIDEO-1000nit-15nit-REC2020lim_1.1).",
    "acescg": round_all(points, 7),
    "srgb": round_all(srgb, 7),
    "rec709": round_all(np.power(lin709, 1 / 2.4), 7),
    "hdrNits": round_all(lin2020 * 100, 5),
    "matrices": matrices,
}
path = os.path.join(os.path.dirname(__file__), "..", "packages", "core", "src", "color", "fixtures", "aces-ocio-reference.ts")
with open(path, "w") as f:
    f.write("// Generated by scripts/aces-reference.py (OpenColorIO builtin ACES 1.3 transforms). Do not edit.\n")
    f.write("export const ACES_REFERENCE = " + json.dumps(out, separators=(",", ":")) + ";\n")
print("wrote", os.path.normpath(path), len(points), "points")