dymokomi/luce-color
Source of release 0.8.2
0.8.2 / src / aces / tonescale.lucb
#==============================================================================================
#
# aces/tonescale - The Daniele tonescale and the chroma compression
#
# DESCRIPTION:
# The tonescale maps scene luminance to display luminance for a peak (100 nits for
# SDR): scene grey 0.18 lands near 10 nits at 100 and a little brighter for HDR
# peaks, a toe absorbs flare, and the curve "hits the roof" at a scene value that
# grows with the peak. It is applied to J through luminance. The chroma compression
# then scales M with J's change and compresses it toward a hue-dependent limit set
# by the reach gamut (AP1) at the peak, with a toe that keeps shadows saturated.
# Each step has an exact inverse (the `un` functions), for the inverse transform.
#
#==============================================================================================
## The tonescale's constants for one peak.
struct Tones:
var n: f64
var n_r: f64
var g: f64
var t_1: f64
var c_t: f64
var s_2: f64
var m_2: f64
var forward_limit: f64
var log_peak: f64
func tones_of(peak: f64) -> Tones:
let n = peak
let n_r = 100.0
let g = 1.15
let c = 0.18
let c_d = 10.013
let w_g = 0.14
let t_1 = 0.04
let r_hit_min = 128.0
let r_hit_max = 896.0
let r_hit = r_hit_min + (r_hit_max - r_hit_min) * (math.log(n / n_r) / math.log(10000.0 / 100.0))
let m_0 = n / n_r
let m_1 = 0.5 * (m_0 + math.sqrt(m_0 * (m_0 + 4.0 * t_1)))
let u = math.pow((r_hit / m_1) / ((r_hit / m_1) + 1.0), g)
let m = m_1 / u
let w_i = math.log(n / 100.0) / math.log(2.0)
let c_t = c_d / n_r * (1.0 + w_i * w_g)
let g_ip = 0.5 * (c_t + math.sqrt(c_t * (c_t + 4.0 * t_1)))
let g_ipp2 = -(m_1 * math.pow(g_ip / m, 1.0 / g)) / (math.pow(g_ip / m, 1.0 / g) - 1.0)
let w_2 = c / g_ipp2
let s_2 = w_2 * m_1 * reference_luminance
let u_2 = math.pow((r_hit / m_1) / ((r_hit / m_1) + w_2), g)
return Tones(n = n, n_r = n_r, g = g, t_1 = t_1, c_t = c_t, s_2 = s_2, m_2 = m_1 / u_2,
forward_limit = 8.0 * r_hit, log_peak = math.log10(n / n_r))
## Display luminance for scene luminance `y` (both 100 at reference white).
func tonescale(y: f64, tones: const Tones*) -> f64:
let f = tones.m_2 * math.pow(y / (y + tones.s_2), tones.g)
return math.max(0.0, f * f / (f + tones.t_1)) * tones.n_r
## Scene luminance for display luminance `y`: tonescale's inverse, held below
## the scene light that reaches the peak (`forward_limit`).
func untonescale(y: f64, tones: const Tones*) -> f64:
let h = math.max(0.0, y / tones.n_r)
# f²/(f + t_1) = h, for f.
let f = 0.5 * (h + math.sqrt(h * h + 4.0 * h * tones.t_1))
let a = math.pow(f / tones.m_2, 1.0 / tones.g)
if a >= 1.0:
return tones.forward_limit * reference_luminance
return math.min(tones.s_2 * a / (1.0 - a), tones.forward_limit * reference_luminance)
## The scene J whose tonemapped J is `j_ts`.
func untonescale_j(j_ts: f64, cam: const Cam*, tones: const Tones*) -> f64:
let scene = untonescale(j_to_y(j_ts, cam), tones)
return math.copysign(y_to_j(scene, cam), j_ts)
## The tonemapped J of achromatic `a`.
func tonescale_a_to_j(a: f64, cam: const Cam*, tones: const Tones*) -> f64:
let toned = tonescale(a_to_y(a, cam), tones)
return math.copysign(y_to_j(toned, cam), a)
## The chroma compression's constants for one peak.
struct Chroma:
var sat: f64
var sat_threshold: f64
var compression: f64
var scale: f64
func chroma_of(peak: f64, tones: const Tones*) -> Chroma:
let squeeze = 2.4
let expand = 1.3
return Chroma(sat = math.max(0.2, expand - expand * 0.69 * tones.log_peak), sat_threshold = 0.5 / tones.n,
compression = squeeze + squeeze * 3.3 * tones.log_peak,
scale = math.pow(0.03379 * peak, 0.30596) - 0.45135)
## The hue-dependent normalization of M (a fit of the reach gamut's shape).
func chroma_norm(cos_h: f64, sin_h: f64, scale: f64) -> f64:
let cos2 = 2.0 * cos_h * cos_h - 1.0
let sin2 = 2.0 * cos_h * sin_h
let cos3 = 4.0 * cos_h * cos_h * cos_h - 3.0 * cos_h
let sin3 = 3.0 * sin_h - 4.0 * sin_h * sin_h * sin_h
let m = 11.34072 * cos_h + 16.46899 * cos2 + 7.88380 * cos3 + 14.66441 * sin_h - 6.37224 * sin2 + 9.19364 * sin3 + 77.12896
return m * scale
## The toe: `x` kept above `limit`, curved below it.
func toe(x: f64, limit: f64, k1_in: f64, k2_in: f64) -> f64:
if x > limit:
return x
let k2 = math.max(k2_in, 0.001)
let k1 = math.sqrt(k1_in * k1_in + k2 * k2)
let k3 = (limit + k1) / (limit + k2)
let minus_b = k3 * x - k1
let minus_ac = k2 * k3 * x
return 0.5 * (minus_b + math.sqrt(minus_b * minus_b + 4.0 * minus_ac))
## The toe's inverse: the `x` whose toe is `y`.
func untoe(y: f64, limit: f64, k1_in: f64, k2_in: f64) -> f64:
if y > limit:
return y
let k2 = math.max(k2_in, 0.001)
let k1 = math.sqrt(k1_in * k1_in + k2 * k2)
let k3 = (limit + k1) / (limit + k2)
# toe solves y² + k1·y = k3·x·(y + k2).
return (y * y + k1 * y) / (k3 * (y + k2))
## The compressed JMh for `jmh` whose tonemapped J is `j_ts`; `m_norm` is
## chroma_norm's, `limit_j` the peak's J and `reach_m` the reach gamut's M at it.
func chroma_compress(jmh: f64[3], j_ts: f64, m_norm: f64, limit_j: f64, gamma_inv: f64, reach_m: f64, chroma: const Chroma*) -> f64[3]:
let j = jmh[0]
let m = jmh[1]
if m == 0.0:
return [j_ts, 0.0, jmh[2]]
let nj = j_ts / limit_j
let snj = math.max(0.0, 1.0 - nj)
let limit = math.pow(nj, gamma_inv) * reach_m / m_norm
var compressed = m * math.pow(j_ts / j, gamma_inv) / m_norm
compressed = limit - toe(limit - compressed, limit - 0.001, snj * chroma.sat, math.sqrt(nj * nj + chroma.sat_threshold))
compressed = toe(compressed, limit, nj * chroma.compression, snj)
return [j_ts, compressed * m_norm, jmh[2]]
## chroma_compress's inverse: the scene JMh, of scene J `j`, for compressed `jmh`.
func chroma_decompress(jmh: f64[3], j: f64, m_norm: f64, limit_j: f64, gamma_inv: f64, reach_m: f64, chroma: const Chroma*) -> f64[3]:
let j_ts = jmh[0]
if jmh[1] == 0.0 or j_ts <= 0.0 or j <= 0.0:
return [j, 0.0, jmh[2]]
let nj = j_ts / limit_j
let snj = math.max(0.0, 1.0 - nj)
let limit = math.pow(nj, gamma_inv) * reach_m / m_norm
var m = untoe(jmh[1] / m_norm, limit, nj * chroma.compression, snj)
m = limit - untoe(limit - m, limit - 0.001, snj * chroma.sat, math.sqrt(nj * nj + chroma.sat_threshold))
return [j, m * m_norm * math.pow(j_ts / j, -gamma_inv), jmh[2]]