-
Notifications
You must be signed in to change notification settings - Fork 2
/
hdr-on-canvas.html
306 lines (279 loc) · 9.22 KB
/
hdr-on-canvas.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<html>
<head>
<script>
////////////////////////////////////////////////////////////////////////////////
// Color conversion math
function srgbLinearLuminance(r, g, b) {
return 0.2126*r + 0.7152*g + 0.0722*b;
}
function rec2020LinearLuminance(r, g, b) {
return 0.2627*r + 0.6780*g + 0.0593*b;
}
function srgbToLinear(x) {
if (x < 0.0)
return 0.0;
if (x < 0.04045)
return x / 12.92;
return Math.pow((x + 0.055)/1.044, 2.4);
}
function linearToSrgb(x) {
if (x < 0.003130800090713953)
return 12.919999999992248*x;
return Math.pow(1.1371188301409823*x, 0.4166666666666667) - 0.05499994754780801;
}
function pqSignalToNits(v) {
const c1 = 107.0 / 128.0;
const c2 = 2413.0 / 128.0;
const c3 = 2392.0 / 128.0;
const m1 = 1305.0 / 8192.0;
const m2 = 2523.0 / 32.0;
const p = Math.pow(Math.min(Math.max(v, 0.0), 1.0), 1.0 / m2);
return 10000.0 * Math.pow(Math.max(p - c1, 0.0) / (c2 - c3 * p), 1.0 / m1);
}
function pqNitsToSignal(L) {
const c1 = 107.0 / 128.0;
const c2 = 2413.0 / 128.0;
const c3 = 2392.0 / 128.0;
const m1 = 1305.0 / 8192.0;
const m2 = 2523.0 / 32.0;
const v = Math.pow(Math.min(Math.max(L / 10000.0, 0.0), 1.0), m1);
return Math.pow((c1 + c2 * v) / (1.0 + c3 * v), m2);
}
// Maps [0, 1] signal to [0, 1] linear.
function hlgInvOetf(E) {
const a = 0.17883277;
const b = 1 - 4*a;
const c = 0.5 - a * Math.log(4 * a);
if (E <= 0)
return 0;
if (E >= 1)
return 1;
if (E <= 0.5)
return Math.pow(E, 2) / 3;
return (Math.exp((E - c) / a) + b) / 12;
}
function hlgOotfGamma(Lw) {
if (Lw < 400) {
const kappa = 1.111;
return 1.2 * Math.pow(kappa, Math.log(Lw / 1000) / Math.log(2))
}
return 1.2 + 0.42 * Math.log(Lw / 1000) / Math.log(10)
}
function tonemapIturBt2408(Eprime, Lw, Lmax, Lb, Lmin) {
let NormalizedSignal = function(L) {
return (pqNitsToSignal(L) - pqNitsToSignal(Lb)) /
(pqNitsToSignal(Lw) - pqNitsToSignal(Lb))
};
minLum = NormalizedSignal(Lmin);
maxLum = NormalizedSignal(Lmax);
const KS = 1.5 * maxLum - 0.5;
const b = minLum;
let T = function(A) {
return (A - KS) / (1 - KS);
};
let P = function(B) {
TB1 = T(B);
TB2 = TB1 * TB1;
TB3 = TB2 * TB1;
return ( 2*TB3 - 3*TB2 + 1) * KS +
( TB3 - 2*TB2 + TB1) * (1 - KS) +
(-2*TB3 + 3*TB2 ) * maxLum;
};
const E1 = NormalizedSignal(Eprime);
const E2 = E1 < KS ? E1 : P(E1);
const E3 = E2 + b*(1 - E2)**4;
const E4 = E3 * (pqNitsToSignal(Lw) - pqNitsToSignal(Lb)) + pqNitsToSignal(Lb);
return pqSignalToNits(E4);
}
const srgb_to_xyz = [
[0.4360413, 0.3851129, 0.1430458],
[0.2224845, 0.7169051, 0.0606104],
[0.0139202, 0.0970672, 0.7139126]];
const xyz_to_srgb = [
[ 3.1341864, -1.6172090, -0.4906941],
[-0.9787485, 1.9161301, 0.0334334],
[ 0.0719639, -0.2289939, 1.4057537]];
const p3_to_xyz = [
[ 0.5151187, 0.2919778, 0.1571035],
[ 0.2411892, 0.6922441, 0.0665668],
[-0.0010505, 0.0418791, 0.7840713]];
const xyz_to_p3 = [
[ 2.4039840, -0.9899063, -0.3976415],
[-0.8422229, 1.7977437, 0.0160354],
[ 0.0482059, -0.0974068, 1.2740049]];
const rec2020_to_xyz = [
[ 0.673459, 0.165661, 0.125100 ],
[ 0.279033, 0.675338, 0.0456288 ],
[ -0.00193139, 0.0299794, 0.797162 ]];
function applyMatrix(A, x) {
return [
A[0][0]*x[0] + A[0][1]*x[1] + A[0][2]*x[2],
A[1][0]*x[0] + A[1][1]*x[1] + A[1][2]*x[2],
A[2][0]*x[0] + A[2][1]*x[1] + A[2][2]*x[2]];
}
let rowToString = function(context_2d, y, c) {
let kNumSteps = 21;
let kBlockSize = 64;
let text = '[';
for (let x = 32; x < kNumSteps * kBlockSize; x += kBlockSize) {
let data = context_2d.getImageData(x, y, 1, 1);
text += (data.data[c] / 255).toFixed(2) + ', ';
}
text += ']';
return text;
}
let drawStuff = function(elementName, image, outPrefix, outElement, resize) {
var element_2d = document.getElementById(elementName);
var context_2d = element_2d.getContext('2d');
if (resize) {
element_2d.width = image.width;
element_2d.height = image.height;
}
// Clear the full canvas to red to make sure we don't get stale results.
context_2d.fillStyle = 'red';
let x = 0;
let y = 0;
if (!resize) {
y = 16;
}
context_2d.fillRect(x, y, element_2d.width, element_2d.height);
context_2d.drawImage(image, x, y, element_2d.width, element_2d.height);
if (resize) {
element_2d.style = 'max-width:80%';
}
document.getElementById(outElement).innerHTML = outPrefix + rowToString(context_2d, 32, 0);
}
let drawVideos = function() {
drawStuff('SRGBCanvas', document.getElementById('SRGBVideo'), 'Y(vid) = ', 'SRGB_Y_VID', false);
drawStuff('HLGCanvas',
document.getElementById('HLGVideo'),
'Grey values read back from video drawn to canvas:<br>Y = ',
'HLG_Y_VID',
false);
drawStuff('PQCanvas',
document.getElementById('PQVideo'),
'Grey values read back from video drawn to canvas:<br>Y = ',
'PQ_Y_VID',
false);
}
window.onload = function() {
var image_srgb = new Image();
image_srgb.onload = function() {
drawStuff('SRGBCanvas', image_srgb, 'Y(img) = ', 'SRGB_Y', true);
}
image_srgb.src = "staircase-srgb.avif";
var image_hlg = new Image();
image_hlg.onload = function() {
drawStuff('HLGCanvas',
image_hlg,
'Grey values read back from image drawn to canvas:<br>Y = ',
'HLG_Y',
true);
{
let text = 'Y\' = [';
for (let x = 0; x <= 20; x += 1) {
let x_quant = Math.round(255 * (x / 20)) / 255;
let x_linear = hlgInvOetf(x_quant);
let x_gammaed = Math.pow(x_linear, hlgOotfGamma(100));
let x_srgb = linearToSrgb(x_gammaed);
text += x_srgb.toFixed(2) + ', ';
}
text += ']';
HLG_Y_TM.innerHTML = 'Manually computed tonemapped values from ITU-R BT.2100 with Lw=100<br>';
HLG_Y_TM.innerText += text;
}
}
image_hlg.src = "staircase-hlg.avif";
var image_pq = new Image();
image_pq.onload = function() {
{
var element_2d = document.getElementById("PQCanvas");
var context_2d = element_2d.getContext('2d');
element_2d.width = image_pq.width;
element_2d.height = image_pq.height;
context_2d.drawImage(image_pq, 0, 0, image_pq.width, image_pq.height);
element_2d.style = 'max-width:80%';
PQ_Y.innerHTML = 'Grey values read back from canvas:<br>';
PQ_Y.innerHTML += 'Y= ' + rowToString(context_2d, 32, 0);
PQ_R.innerHTML = 'Red values read back from canvas:<br>';
PQ_R.innerHTML += 'R = ' + rowToString(context_2d, 32 + 64, 0);
{
let text = 'Y\' = [';
for (let x = 0; x <= 20; x += 1) {
let x_quant = Math.round(255 * (x / 20)) / 255;
let x_nits = pqSignalToNits(x_quant);
let x_nits_tonemapped = tonemapIturBt2408(x_nits, 1000, 203, 0.0005, 0);
let x_tonemapped = x_nits_tonemapped / 203;
let x_srgb = linearToSrgb(x_tonemapped);
text += x_srgb.toFixed(2) + ', ';
}
text += ']';
PQ_Y_TM.innerHTML = 'Manually computed tonemapped values from ITU-R BT.2408 mapping [0.0005, 1000] to [0, 203]<br>';
PQ_Y_TM.innerText += text;
}
{
let text = 'R\' = [';
for (let x = 0; x <= 20; x += 1) {
let x_quant = Math.round(255 * (x / 20)) / 255;
let x_nits = pqSignalToNits(x_quant);
let x_nits_tonemapped = tonemapIturBt2408(x_nits, 1000, 203, 0.0005, 0);
let x_tonemapped = x_nits_tonemapped / 203;
let rgb = [x_tonemapped, 0, 0];
rgb = applyMatrix(rec2020_to_xyz, rgb);
rgb = applyMatrix(xyz_to_srgb, rgb);
let x_srgb = linearToSrgb(rgb[0]);
text += x_srgb.toFixed(2) + ', ';
}
text += ']';
PQ_R_TM.innerHTML = 'Manually computed tonemapped values from ITU-R BT.2408 mapping [0.0005, 1000] to [0, 203]<br>';
PQ_R_TM.innerText += text;
}
}
}
image_pq.src = "staircase-pq.avif";
VideoButton.addEventListener('click', drawVideos);
SRGBVideo.controls=false;
HLGVideo.controls=false;
PQVideo.controls=false;
}
</script>
</head>
<body>
<p><button type="button" id="VideoButton">Draw the video to the canvas (shifted down by 16 pixels)</button></p>
<h1>sRGB</h1>
<p>As a canvas</p>
<p><canvas id="SRGBCanvas"></canvas></p>
<p>As an image</p>
<p><img src="staircase-srgb.avif" style="max-width:80%;"></p>
<p>As a video</p>
<p><video id="SRGBVideo", src="staircase-srgb.mp4" style="max-width:80%;"></p>
<p id="SRGB_Y"></p>
<p id="SRGB_Y_VID"></p>
<h1>HLG</h1>
<p>As a canvas</p>
<p><canvas id="HLGCanvas"></canvas></p>
<p id="HLGText"></p>
<p id="HLGTextVid"></p>
<p>As an image</p>
<p><img src="staircase-hlg.avif" style="max-width:80%;"></p>
<p>As a video</p>
<p><video id="HLGVideo", src="staircase-hlg.mp4" style="max-width:80%;"></p>
<p id="HLG_Y"></p>
<p id="HLG_Y_VID"></p>
<p id="HLG_Y_TM"></p>
<h1>PQ</h1>
<p>As a canvas</p>
<p><canvas id="PQCanvas"></canvas></p>
<p id="PQText"></p>
<p id="PQTextVid"></p>
<p>As an image</p>
<p><img src="staircase-pq.avif" style="max-width:80%;"></p>
<p>As a video</p>
<p><video id="PQVideo", src="staircase-pq.mp4" style="max-width:80%;"></p>
<p id="PQ_Y"></p>
<p id="PQ_Y_VID"></p>
<p id="PQ_Y_TM"></p>
<p id="PQ_R"></p>
<p id="PQ_R_TM"></p>
</body>
</html>