-
Notifications
You must be signed in to change notification settings - Fork 2
/
canvas-hdr.html
254 lines (231 loc) · 8.57 KB
/
canvas-hdr.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
<html>
<head>
<script>
function webglCompileShader(gl, vertCode, fragCode) {
let vertShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertShader, vertCode);
gl.compileShader(vertShader);
let fragShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragShader, fragCode);
gl.compileShader(fragShader);
let shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertShader);
gl.attachShader(shaderProgram, fragShader);
gl.linkProgram(shaderProgram);
return shaderProgram;
}
function webglRender(gl, image) {
// Create a texture X.
let tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
{
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, image.width, image.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
}
// Create the program to draw a point sprite.
let vs = `attribute vec4 position;
varying vec2 texcoord;
void main() {
texcoord.xy = vec2(position.x, position.y);
gl_Position = position;
gl_Position.xy *= 2.0;
gl_Position.xy -= vec2(1.0, 1.0);
gl_Position.y *= -1.0;
}`;
let fs = `precision mediump float;
uniform sampler2D tex;
varying vec2 texcoord;
void main() {
gl_FragColor = texture2D(tex, texcoord);
gl_FragColor.a = 1.0;
}`;
let program = webglCompileShader(gl, vs, fs);
gl.useProgram(program);
var vertices = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertices);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0,0, 1,0, 1,1, 0,1]), gl.STATIC_DRAW);
var indices = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indices);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([0,1,2, 0,2,3]), gl.STATIC_DRAW);
var positionLocation = gl.getAttribLocation(program, "position");
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(positionLocation);
gl.uniform1i(gl.getUniformLocation(program, "tex"), 0);
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
}
function imageDataFill(image_data, value) {
for (var i = 0; i < image_data.width * image_data.height; ++i) {
image_data.data[4*i+0] = value;
image_data.data[4*i+1] = value;
image_data.data[4*i+2] = value;
image_data.data[4*i+3] = 1.0;
}
}
function drawManualCanvas(redLine) {
var element = document.getElementById("Canvas2D_Manual");
var context = element.getContext('2d', {colorSpace:'srgb', pixelFormat:'float16'});
context.clearRect(0, 0, element.width, element.height);
var image_data = new ImageData(32, 32, {colorSpace:'srgb-linear', storageFormat:'float32'});
context.font = "10px Arial";
for (var i = 0; i <= 1024/32; ++i) {
var value = i / 2;
imageDataFill(image_data, value);
context.putImageData(image_data, i*image_data.width, 0);
context.fillStyle = 'black';
context.fillText(value.toFixed(2), i*image_data.width, 44);
}
if (redLine) {
var x = redLine * 64;
context.beginPath();
context.moveTo(x, 0);
context.lineTo(x, 50);
context.lineWidth = 4;
context.strokeStyle = 'red';
context.stroke();
context.fillStyle = 'red';
context.fillText(redLine.toFixed(2), x, 64);
}
}
window.onload = function() {
var image_hlg = new Image();
image_hlg.onload = function() {
{
var element_2d = document.getElementById("Canvas2D_HLG");
var context_2d = element_2d.getContext('2d', {colorSpace:'rec2100-hlg', pixelFormat:'float16'});
context_2d.drawImage(image_hlg, 0, 0, 512, 256);
}
{
var element_gl = document.getElementById("CanvasGL_HLG");
gl = element_gl.getContext('webgl2');
gl.drawingBufferColorSpace = 'rec2100-hlg';
gl.unpackColorSpace = 'rec2100-hlg';
webglRender(gl, image_hlg);
}
}
image_hlg.src = "cosmos-hlg.avif";
var image_pq = new Image();
image_pq.onload = function() {
{
var element_2d = document.getElementById("Canvas2D_PQ");
var context_2d = element_2d.getContext('2d', {colorSpace:'rec2100-pq', pixelFormat:'float16'});
context_2d.drawImage(image_pq, 0, 0, 512, 256);
}
{
var element_gl = document.getElementById("CanvasGL_PQ");
gl = element_gl.getContext('webgl2');
gl.drawingBufferColorSpace = 'rec2100-pq';
gl.unpackColorSpace = 'rec2100-pq';
webglRender(gl, image_pq);
}
}
image_pq.src = "cosmos-pq.avif";
drawManualCanvas(null);
async function screenDetails() {
const screens = await window.getScreenDetails();
const s = screens.currentScreen;
function updateCurrentScreenInfo(s) {
drawManualCanvas(s.highDynamicRangeHeadroom);
document.getElementById('Details').innerText =
'HDR headroom: ' + s.highDynamicRangeHeadroom + ', ' +
'Gamut: {' +
'r:[' + s.redPrimaryX.toFixed(4) + '], [' + s.redPrimaryY.toFixed(4) + '], ' +
'g:[' + s.greenPrimaryX.toFixed(4) + '], [' + s.greenPrimaryY.toFixed(4) + '], ' +
'b:[' + s.bluePrimaryX.toFixed(4) + '], [' + s.bluePrimaryY.toFixed(4) + ']}';
}
updateCurrentScreenInfo(s);
screens.addEventListener('currentscreenchange', (event) => {
updateCurrentScreenInfo(s);
});
}
DetailsButton.addEventListener('click', screenDetails);
MetadataSlider.addEventListener('change', event => {
var value = Math.pow(10, MetadataSlider.value)
const metadata = {
redPrimaryX:0.708,
redPrimaryY:0.292,
greenPrimaryX:0.170,
greenPrimaryY:0.797,
bluePrimaryX:0.131,
bluePrimaryY:0.046,
whitePointX:0.3127,
whitePointY:0.3290,
minimumLuminance:0,
maximumLuminance:value};
const configuration = {
mode:'default',
smpteSt2086Metadata:metadata
};
document.getElementById('Canvas2D_PQ').configureHighDynamicRange(configuration);
// This doesn't do anything yet ...
// document.getElementById('CanvasGL_PQ').configureHighDynamicRange(configuration);
document.getElementById('MetadataSliderValue').innerText = 'Max luminance: ' + value.toFixed(0) + ' nits.';
});
};
</script>
</head>
<body>
<h1>HDR Canvas Example</h1>
<p>
This is an experimental feature.
Use Chrome 108 or higher (Canary as of time of writing).
</p>
<p>
In chrome://flags, set the following:
<ul>
<li>Experimental Web Platform features: Enabled</li>
<li>Out-of-process 2D canvas rasterization: Enabled</li>
</ul>
</p>
<h1>Image examples</h1>
<p>
Below are two images representing the same scene.
The image on the left is HLG, and the image on the right is PQ.
<p>
<img width="512" height="256" src="cosmos-hlg.avif">
<img width="512" height="256" src="cosmos-pq.avif">
</p>
<h1>2D Canvas examples (HLG and PQ, automatically tone mapped)</h1>
<p>
The canvas on the left is in color space rec2100-hlg, and the above HLG image from the left is drawn to it.
</p>
<p>
The canvas on the right is in color space rec2100-pq, and the above PQ image from the right is drawn to it.
</p>
<p>
The following slider may be used to adjust the SMPTE ST 2408 maximum luminance value (might not have an effect on all platforms yet):
</p>
<p>
<input id="MetadataSlider" type="range" min="2" max="4" step="0.01" value="4">
</p>
<p id="MetadataSliderValue"></p>
<p>
<canvas id="Canvas2D_HLG" width="512" height="256"></canvas>
<canvas id="Canvas2D_PQ" width="512" height="256"></canvas>
</p>
<h1>WebGL examples (HLG and PQ, automatically tone mapped)</h1>
<p>
The canvas on the left is in color space rec2100-hlg, and the above HLG image from the left is drawn to it.
</p>
<p>
The canvas on the right is in color space rec2100-pq, and the above PQ image from the right is drawn to it.
</p>
<p>
(The PQ canvas is not affected by the above metadata slider, because that code is not written yet).
</p>
<p>
<canvas id="CanvasGL_HLG" width="512" height="256"></canvas>
<canvas id="CanvasGL_PQ" width="512" height="256"></canvas>
</p>
<h1>Manual tone map example</h1>
<p>This canvas has a float16 backing, and its color space is extended-sRGB.</p>
<p>This canvas is populated by ImageData with increasing linear light values.</p>
<p>Press the button below to draw a red line at the screen's maximum brightness.</p>
</p>
<button type="button" id="DetailsButton">Query Screen Details</button>
<p id="Details"></p>
<p>
<canvas id="Canvas2D_Manual" width="1056" height="256"></canvas>
</p>
</body>