Debian Codegraph @

node/colorjs.io/src/spaces/ictcp.js

raw
1 import ColorSpace from "../ColorSpace.js";
2 import { multiply_v3_m3x3 } from "../util.js";
3 import XYZ_Abs_D65 from "./xyz-abs-d65.js";
4
5 /** @import { Matrix3x3, Vector3 } from "../types.js" */
6
7 const c1 = 3424 / 4096;
8 const c2 = 2413 / 128;
9 const c3 = 2392 / 128;
10 const m1 = 2610 / 16384;
11 const m2 = 2523 / 32;
12 const im1 = 16384 / 2610;
13 const im2 = 32 / 2523;
14
15 // Reference (rational-form) matrices, kept for documentation but not used directly:
16 // linear-light Rec.2020 to LMS (with crosstalk) and its inverse
17 // rational terms from Jan Fröhlich, Encoding High Dynamic Range
18 // and Wide Color Gamut Imagery, p.97 and ITU-R BT.2124-0 pp.2-3
19 /*
20 const Rec2020toLMS = [
21 [ 1688 / 4096, 2146 / 4096, 262 / 4096 ],
22 [ 683 / 4096, 2951 / 4096, 462 / 4096 ],
23 [ 99 / 4096, 309 / 4096, 3688 / 4096 ]
24 ];
25 const LMStoRec2020 = [
26 [ 3.4375568932814012112, -2.5072112125095058195, 0.069654319228104608382],
27 [-0.79142868665644156125, 1.9838372198740089874, -0.19240853321756742626 ],
28 [-0.025646662911506476363, -0.099240248643945566751, 1.1248869115554520431 ]
29 ];
30 */
31
32 /**
33 * Matrices used by this color space, also available as `ICTCP.M`
34 * @type {Record<string, Matrix3x3>}
35 */
36 // prettier-ignore
37 export const M = {
38 // includes the 4% crosstalk components, from the Dolby "What is ICtCp" paper
39 XYZtoLMS: [
40 [ 0.3592832590121217, 0.6976051147779502, -0.0358915932320290 ],
41 [ -0.1920808463704993, 1.1004767970374321, 0.0753748658519118 ],
42 [ 0.0070797844607479, 0.0748396662186362, 0.8433265453898765 ],
43 ],
44 // includes the Ebner LMS coefficients, the rotation,
45 // and the scaling to the [-0.5,0.5] range
46 LMStoIPT: [
47 [ 2048 / 4096, 2048 / 4096, 0 ],
48 [ 6610 / 4096, -13613 / 4096, 7003 / 4096 ],
49 [ 17933 / 4096, -17390 / 4096, -543 / 4096 ],
50 ],
51 // inverted matrices, calculated from the above
52 IPTtoLMS: [
53 [ 0.9999999999999998, 0.0086090370379328, 0.1110296250030260 ],
54 [ 0.9999999999999998, -0.0086090370379328, -0.1110296250030259 ],
55 [ 0.9999999999999998, 0.5600313357106791, -0.3206271749873188 ],
56 ],
57 LMStoXYZ: [
58 [ 2.0701522183894223, -1.3263473389671563, 0.2066510476294053 ],
59 [ 0.3647385209748072, 0.6805660249472273, -0.0453045459220347 ],
60 [ -0.0497472075358123, -0.0492609666966131, 1.1880659249923042 ],
61 ],
62 };
63
64 // Only the PQ form of ICtCp is implemented here. There is also an HLG form.
65 // from Dolby, "WHAT IS ICTCP?"
66 // https://professional.dolby.com/siteassets/pdfs/ictcp_dolbywhitepaper_v071.pdf
67 // and
68 // Dolby, "Perceptual Color Volume
69 // Measuring the Distinguishable Colors of HDR and WCG Displays"
70 // https://professional.dolby.com/siteassets/pdfs/dolby-vision-measuring-perceptual-color-volume-v7.1.pdf
71 export default new ColorSpace({
72 id: "ictcp",
73 name: "ICTCP",
74 // From BT.2100-2 page 7:
75 // During production, signal values are expected to exceed the
76 // range E′ = [0.0 : 1.0]. This provides processing headroom and avoids
77 // signal degradation during cascaded processing. Such values of E′,
78 // below 0.0 or exceeding 1.0, should not be clipped during production
79 // and exchange.
80 // Values below 0.0 should not be clipped in reference displays (even
81 // though they represent “negative” light) to allow the black level of
82 // the signal (LB) to be properly set using test signals known as “PLUGE”
83 coords: {
84 i: {
85 refRange: [0, 1], // Constant luminance,
86 name: "I",
87 },
88 ct: {
89 refRange: [-0.5, 0.5], // Full BT.2020 gamut in range [-0.5, 0.5]
90 name: "CT",
91 },
92 cp: {
93 refRange: [-0.5, 0.5],
94 name: "CP",
95 },
96 },
97
98 base: XYZ_Abs_D65,
99 M,
100 fromBase (XYZ) {
101 // move to LMS cone domain
102 let LMS = multiply_v3_m3x3(XYZ, M.XYZtoLMS);
103
104 return LMStoICtCp(LMS);
105 },
106 toBase (ICtCp) {
107 let LMS = ICtCptoLMS(ICtCp);
108
109 return multiply_v3_m3x3(LMS, M.LMStoXYZ);
110 },
111
112 formats: {
113 ictcp: {
114 coords: [
115 "<percentage> | <number>",
116 "<number> | <percentage>",
117 "<number> | <percentage>",
118 ],
119 },
120 },
121 });
122
123 /**
124 *
125 * @param {Vector3} LMS
126 * @returns {Vector3}
127 */
128 function LMStoICtCp (LMS) {
129 // apply the PQ EOTF
130 // we can't ever be dividing by zero because of the "1 +" in the denominator
131 let PQLMS = /** @type {Vector3} */ (
132 LMS.map(function (val) {
133 let num = c1 + c2 * (val / 10000) ** m1;
134 let denom = 1 + c3 * (val / 10000) ** m1;
135
136 return (num / denom) ** m2;
137 })
138 );
139
140 // LMS to IPT, with rotation for Y'C'bC'r compatibility
141 return multiply_v3_m3x3(PQLMS, M.LMStoIPT);
142 }
143
144 /**
145 *
146 * @param {Vector3} ICtCp
147 * @returns {Vector3}
148 */
149 function ICtCptoLMS (ICtCp) {
150 let PQLMS = multiply_v3_m3x3(ICtCp, M.IPTtoLMS);
151
152 // From BT.2124-0 Annex 2 Conversion 3
153 let LMS = /** @type {Vector3} */ (
154 PQLMS.map(function (val) {
155 let num = Math.max(val ** im2 - c1, 0);
156 let denom = c2 - c3 * val ** im2;
157 return 10000 * (num / denom) ** im1;
158 })
159 );
160
161 return LMS;
162 }