@monogrid/gainmap-js 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/README.md +21 -8
- package/dist/QuadRenderer-IDZuHqZM.js +178 -0
- package/dist/{_commonjsHelpers-1789f0cf.js → _commonjsHelpers-G_2thcKl.js} +1 -1
- package/dist/{compress-a4b59cab.js → compress-Cw5IwRTb.js} +1 -1
- package/dist/{loaders → core}/LoaderBase.d.ts +3 -3
- package/dist/{utils → core}/QuadRenderer.d.ts +6 -0
- package/dist/core/types.d.ts +64 -0
- package/dist/decode/decode.d.ts +56 -0
- package/dist/decode/extract.d.ts +10 -0
- package/dist/decode/index.d.ts +8 -0
- package/dist/{loaders → decode/loaders}/GainMapLoader.d.ts +4 -4
- package/dist/{loaders → decode/loaders}/JPEGRLoader.d.ts +5 -5
- package/dist/{materials → decode/materials}/GainMapDecoderMaterial.d.ts +3 -2
- package/dist/decode/types.d.ts +38 -0
- package/dist/decode/utils/MPFExtractor.d.ts +22 -0
- package/dist/decode/utils/extract-metadata-from-jpeg.d.ts +7 -0
- package/dist/decode.d.ts +1 -56
- package/dist/decode.js +737 -0
- package/dist/decode.umd.js +917 -0
- package/dist/{encode-utils → encode}/compress.d.ts +1 -1
- package/dist/encode/encode-and-compress.d.ts +61 -0
- package/dist/encode/encode.d.ts +58 -0
- package/dist/{encode-utils → encode}/get-gainmap.d.ts +3 -3
- package/dist/{encode-utils → encode}/get-sdr-rendition.d.ts +4 -5
- package/dist/encode/index.d.ts +10 -0
- package/dist/encode/types.d.ts +172 -0
- package/dist/encode.d.ts +1 -117
- package/dist/{index.js → encode.js} +42 -230
- package/dist/encode.umd.js +972 -0
- package/dist/libultrahdr/decode-jpeg-metadata.d.ts +31 -0
- package/dist/libultrahdr/encode-jpeg-metadata.d.ts +84 -0
- package/dist/libultrahdr/library.d.ts +9 -0
- package/dist/libultrahdr.d.ts +3 -123
- package/dist/libultrahdr.js +92 -167
- package/dist/libultrahdr.umd.js +91 -530
- package/dist/worker-interface.js +2 -2
- package/dist/worker-interface.umd.js +1 -3
- package/dist/worker-types.d.ts +2 -2
- package/dist/worker.js +3 -3
- package/dist/worker.umd.js +1 -1
- package/package.json +36 -29
- package/dist/LoaderBase-aa4842c4.js +0 -373
- package/dist/index.d.ts +0 -11
- package/dist/index.umd.js +0 -1360
- package/dist/materials/index.d.ts +0 -3
- package/dist/types.d.ts +0 -262
- /package/dist/{utils → core}/get-data-texture.d.ts +0 -0
- /package/dist/{encode-utils → encode}/find-texture-min-max.d.ts +0 -0
- /package/dist/{materials → encode/materials}/GainMapEncoderMaterial.d.ts +0 -0
- /package/dist/{materials → encode/materials}/SDRMaterial.d.ts +0 -0
|
@@ -0,0 +1,917 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @monogrid/gainmap-js v1.1.1
|
|
3
|
+
* With ❤️, by MONOGRID <hello@mono-grid.com>
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
(function (global, factory) {
|
|
7
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('three')) :
|
|
8
|
+
typeof define === 'function' && define.amd ? define(['exports', 'three'], factory) :
|
|
9
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@monogrid/gainmap-js"] = {}, global.three));
|
|
10
|
+
})(this, (function (exports, three) { 'use strict';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Utility structure used for rendering a texture with a material
|
|
14
|
+
*
|
|
15
|
+
* @category General
|
|
16
|
+
* @group General
|
|
17
|
+
*/
|
|
18
|
+
class QuadRenderer {
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param sourceTexture
|
|
22
|
+
* @param renderer
|
|
23
|
+
*/
|
|
24
|
+
constructor(width, height, type, colorSpace, material, renderer) {
|
|
25
|
+
this._rendererIsDisposable = false;
|
|
26
|
+
/**
|
|
27
|
+
* Renders the input texture using the specified material
|
|
28
|
+
*/
|
|
29
|
+
this.render = () => {
|
|
30
|
+
this._renderer.setRenderTarget(this._renderTarget);
|
|
31
|
+
try {
|
|
32
|
+
this._renderer.render(this._scene, this._camera);
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
this._renderer.setRenderTarget(null);
|
|
36
|
+
throw e;
|
|
37
|
+
}
|
|
38
|
+
this._renderer.setRenderTarget(null);
|
|
39
|
+
};
|
|
40
|
+
this._width = width;
|
|
41
|
+
this._height = height;
|
|
42
|
+
this._type = type;
|
|
43
|
+
this._colorSpace = colorSpace;
|
|
44
|
+
if (type === three.HalfFloatType && navigator.userAgent.toLowerCase().includes('firefox')) {
|
|
45
|
+
this._type = three.FloatType;
|
|
46
|
+
}
|
|
47
|
+
this._material = material;
|
|
48
|
+
if (renderer) {
|
|
49
|
+
this._renderer = renderer;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
this._renderer = QuadRenderer.instantiateRenderer();
|
|
53
|
+
this._rendererIsDisposable = true;
|
|
54
|
+
}
|
|
55
|
+
this._scene = new three.Scene();
|
|
56
|
+
this._camera = new three.OrthographicCamera();
|
|
57
|
+
this._camera.position.set(0, 0, 10);
|
|
58
|
+
this._camera.left = -0.5;
|
|
59
|
+
this._camera.right = 0.5;
|
|
60
|
+
this._camera.top = 0.5;
|
|
61
|
+
this._camera.bottom = -0.5;
|
|
62
|
+
this._camera.updateProjectionMatrix();
|
|
63
|
+
this._quad = new three.Mesh(new three.PlaneGeometry(), this._material);
|
|
64
|
+
this._quad.geometry.computeBoundingBox();
|
|
65
|
+
this._scene.add(this._quad);
|
|
66
|
+
this._renderTarget = new three.WebGLRenderTarget(width, height, {
|
|
67
|
+
type: this._type,
|
|
68
|
+
colorSpace,
|
|
69
|
+
format: three.RGBAFormat,
|
|
70
|
+
magFilter: three.LinearFilter,
|
|
71
|
+
minFilter: three.LinearMipMapLinearFilter,
|
|
72
|
+
wrapS: three.RepeatWrapping,
|
|
73
|
+
wrapT: three.RepeatWrapping,
|
|
74
|
+
depthBuffer: false,
|
|
75
|
+
stencilBuffer: false,
|
|
76
|
+
generateMipmaps: true
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Instantiates a temporary renderer
|
|
81
|
+
*
|
|
82
|
+
* @returns
|
|
83
|
+
*/
|
|
84
|
+
static instantiateRenderer() {
|
|
85
|
+
const renderer = new three.WebGLRenderer();
|
|
86
|
+
renderer.setSize(128, 128);
|
|
87
|
+
// renderer.outputColorSpace = SRGBColorSpace
|
|
88
|
+
// renderer.toneMapping = LinearToneMapping
|
|
89
|
+
// renderer.debug.checkShaderErrors = false
|
|
90
|
+
// this._rendererIsDisposable = true
|
|
91
|
+
return renderer;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Obtains a Buffer containing the rendered texture.
|
|
95
|
+
*
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
|
+
toArray() {
|
|
99
|
+
let out;
|
|
100
|
+
switch (this._type) {
|
|
101
|
+
case three.UnsignedByteType:
|
|
102
|
+
out = new Uint8ClampedArray(this._width * this._height * 4);
|
|
103
|
+
break;
|
|
104
|
+
case three.HalfFloatType:
|
|
105
|
+
out = new Uint16Array(this._width * this._height * 4);
|
|
106
|
+
break;
|
|
107
|
+
case three.UnsignedIntType:
|
|
108
|
+
out = new Uint32Array(this._width * this._height * 4);
|
|
109
|
+
break;
|
|
110
|
+
case three.ByteType:
|
|
111
|
+
out = new Int8Array(this._width * this._height * 4);
|
|
112
|
+
break;
|
|
113
|
+
case three.ShortType:
|
|
114
|
+
out = new Int16Array(this._width * this._height * 4);
|
|
115
|
+
break;
|
|
116
|
+
case three.IntType:
|
|
117
|
+
out = new Int32Array(this._width * this._height * 4);
|
|
118
|
+
break;
|
|
119
|
+
case three.FloatType:
|
|
120
|
+
out = new Float32Array(this._width * this._height * 4);
|
|
121
|
+
break;
|
|
122
|
+
default:
|
|
123
|
+
throw new Error('Unsupported data type');
|
|
124
|
+
}
|
|
125
|
+
this._renderer.readRenderTargetPixels(this._renderTarget, 0, 0, this._width, this._height, out);
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
toDataTexture() {
|
|
129
|
+
return new three.DataTexture(this.toArray(), this.width, this.height, three.RGBAFormat, this._type, three.UVMapping, three.RepeatWrapping, three.RepeatWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, 1, three.NoColorSpace);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* If using a disposable renderer, it will dispose it.
|
|
133
|
+
*/
|
|
134
|
+
dispose() {
|
|
135
|
+
this._renderer.setRenderTarget(null);
|
|
136
|
+
if (this._rendererIsDisposable) {
|
|
137
|
+
this._renderer.dispose();
|
|
138
|
+
this._renderer.forceContextLoss();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Width of the texture
|
|
143
|
+
*/
|
|
144
|
+
get width() { return this._width; }
|
|
145
|
+
set width(value) {
|
|
146
|
+
this._width = value;
|
|
147
|
+
this._renderTarget.setSize(this._width, this._height);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Height of the texture
|
|
151
|
+
*/
|
|
152
|
+
get height() { return this._height; }
|
|
153
|
+
set height(value) {
|
|
154
|
+
this._height = value;
|
|
155
|
+
this._renderTarget.setSize(this._width, this._height);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The renderer used
|
|
159
|
+
*/
|
|
160
|
+
get renderer() { return this._renderer; }
|
|
161
|
+
/**
|
|
162
|
+
* The `WebGLRenderTarget` used.
|
|
163
|
+
*/
|
|
164
|
+
get renderTarget() { return this._renderTarget; }
|
|
165
|
+
set renderTarget(value) {
|
|
166
|
+
this._renderTarget = value;
|
|
167
|
+
this._width = value.width;
|
|
168
|
+
this._height = value.height;
|
|
169
|
+
// this._type = value.texture.type
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The `Material` used.
|
|
173
|
+
*/
|
|
174
|
+
get material() { return this._material; }
|
|
175
|
+
/**
|
|
176
|
+
*
|
|
177
|
+
*/
|
|
178
|
+
get type() { return this._type; }
|
|
179
|
+
get colorSpace() { return this._colorSpace; }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const vertexShader = /* glsl */ `
|
|
183
|
+
varying vec2 vUv;
|
|
184
|
+
|
|
185
|
+
void main() {
|
|
186
|
+
vUv = uv;
|
|
187
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
188
|
+
}
|
|
189
|
+
`;
|
|
190
|
+
const fragmentShader = /* glsl */ `
|
|
191
|
+
uniform sampler2D sdr;
|
|
192
|
+
uniform sampler2D gainMap;
|
|
193
|
+
uniform vec3 gamma;
|
|
194
|
+
uniform vec3 offsetHdr;
|
|
195
|
+
uniform vec3 offsetSdr;
|
|
196
|
+
uniform vec3 gainMapMin;
|
|
197
|
+
uniform vec3 gainMapMax;
|
|
198
|
+
uniform float weightFactor;
|
|
199
|
+
|
|
200
|
+
varying vec2 vUv;
|
|
201
|
+
|
|
202
|
+
void main() {
|
|
203
|
+
vec3 rgb = texture2D(sdr, vUv).rgb;
|
|
204
|
+
vec3 recovery = texture2D(gainMap, vUv).rgb;
|
|
205
|
+
vec3 logRecovery = pow(recovery, gamma);
|
|
206
|
+
vec3 logBoost = gainMapMin * (1.0 - logRecovery) + gainMapMax * logRecovery;
|
|
207
|
+
vec3 hdrColor = (rgb + offsetSdr) * exp2(logBoost * weightFactor) - offsetHdr;
|
|
208
|
+
gl_FragColor = vec4(hdrColor, 1.0);
|
|
209
|
+
}
|
|
210
|
+
`;
|
|
211
|
+
/**
|
|
212
|
+
* A Material which is able to decode the Gainmap into a full HDR Representation
|
|
213
|
+
*
|
|
214
|
+
* @category Materials
|
|
215
|
+
* @group Materials
|
|
216
|
+
*/
|
|
217
|
+
class GainMapDecoderMaterial extends three.ShaderMaterial {
|
|
218
|
+
/**
|
|
219
|
+
*
|
|
220
|
+
* @param params
|
|
221
|
+
*/
|
|
222
|
+
constructor({ gamma, offsetHdr, offsetSdr, gainMapMin, gainMapMax, maxDisplayBoost, hdrCapacityMin, hdrCapacityMax, sdr, gainMap }) {
|
|
223
|
+
super({
|
|
224
|
+
name: 'GainMapDecoderMaterial',
|
|
225
|
+
vertexShader,
|
|
226
|
+
fragmentShader,
|
|
227
|
+
uniforms: {
|
|
228
|
+
sdr: { value: sdr },
|
|
229
|
+
gainMap: { value: gainMap },
|
|
230
|
+
gamma: { value: new three.Vector3(1.0 / gamma[0], 1.0 / gamma[1], 1.0 / gamma[2]) },
|
|
231
|
+
offsetHdr: { value: new three.Vector3().fromArray(offsetHdr) },
|
|
232
|
+
offsetSdr: { value: new three.Vector3().fromArray(offsetSdr) },
|
|
233
|
+
gainMapMin: { value: new three.Vector3().fromArray(gainMapMin) },
|
|
234
|
+
gainMapMax: { value: new three.Vector3().fromArray(gainMapMax) },
|
|
235
|
+
weightFactor: {
|
|
236
|
+
value: (Math.log2(maxDisplayBoost) - hdrCapacityMin) / (hdrCapacityMax - hdrCapacityMin)
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
blending: three.NoBlending,
|
|
240
|
+
depthTest: false,
|
|
241
|
+
depthWrite: false
|
|
242
|
+
});
|
|
243
|
+
this._maxDisplayBoost = maxDisplayBoost;
|
|
244
|
+
this._hdrCapacityMin = hdrCapacityMin;
|
|
245
|
+
this._hdrCapacityMax = hdrCapacityMax;
|
|
246
|
+
this.needsUpdate = true;
|
|
247
|
+
this.uniformsNeedUpdate = true;
|
|
248
|
+
}
|
|
249
|
+
get sdr() { return this.uniforms.sdr.value; }
|
|
250
|
+
set sdr(value) { this.uniforms.sdr.value = value; }
|
|
251
|
+
get gainMap() { return this.uniforms.gainMap.value; }
|
|
252
|
+
set gainMap(value) { this.uniforms.gainMap.value = value; }
|
|
253
|
+
/**
|
|
254
|
+
* @see {@link GainMapMetadata.offsetHdr}
|
|
255
|
+
*/
|
|
256
|
+
get offsetHdr() { return this.uniforms.offsetHdr.value.toArray(); }
|
|
257
|
+
set offsetHdr(value) { this.uniforms.offsetHdr.value.fromArray(value); }
|
|
258
|
+
/**
|
|
259
|
+
* @see {@link GainMapMetadata.offsetSdr}
|
|
260
|
+
*/
|
|
261
|
+
get offsetSdr() { return this.uniforms.offsetSdr.value.toArray(); }
|
|
262
|
+
set offsetSdr(value) { this.uniforms.offsetSdr.value.fromArray(value); }
|
|
263
|
+
/**
|
|
264
|
+
* @see {@link GainMapMetadata.gainMapMin}
|
|
265
|
+
*/
|
|
266
|
+
get gainMapMin() { return this.uniforms.gainMapMin.value.toArray(); }
|
|
267
|
+
set gainMapMin(value) { this.uniforms.gainMapMin.value.fromArray(value); }
|
|
268
|
+
/**
|
|
269
|
+
* @see {@link GainMapMetadata.gainMapMax}
|
|
270
|
+
*/
|
|
271
|
+
get gainMapMax() { return this.uniforms.gainMapMax.value.toArray(); }
|
|
272
|
+
set gainMapMax(value) { this.uniforms.gainMapMax.value.fromArray(value); }
|
|
273
|
+
/**
|
|
274
|
+
* @see {@link GainMapMetadata.gamma}
|
|
275
|
+
*/
|
|
276
|
+
get gamma() {
|
|
277
|
+
const g = this.uniforms.gamma.value;
|
|
278
|
+
return [1 / g.x, 1 / g.y, 1 / g.z];
|
|
279
|
+
}
|
|
280
|
+
set gamma(value) {
|
|
281
|
+
const g = this.uniforms.gamma.value;
|
|
282
|
+
g.x = 1.0 / value[0];
|
|
283
|
+
g.y = 1.0 / value[1];
|
|
284
|
+
g.z = 1.0 / value[2];
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* @see {@link GainMapMetadata.hdrCapacityMin}
|
|
288
|
+
* @remarks Logarithmic space
|
|
289
|
+
*/
|
|
290
|
+
get hdrCapacityMin() { return this._hdrCapacityMin; }
|
|
291
|
+
set hdrCapacityMin(value) {
|
|
292
|
+
this._hdrCapacityMin = value;
|
|
293
|
+
this.calculateWeight();
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* @see {@link GainMapMetadata.hdrCapacityMin}
|
|
297
|
+
* @remarks Logarithmic space
|
|
298
|
+
*/
|
|
299
|
+
get hdrCapacityMax() { return this._hdrCapacityMax; }
|
|
300
|
+
set hdrCapacityMax(value) {
|
|
301
|
+
this._hdrCapacityMax = value;
|
|
302
|
+
this.calculateWeight();
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* @see {@link GainmapDecodingParameters.maxDisplayBoost}
|
|
306
|
+
* @remarks Non Logarithmic space
|
|
307
|
+
*/
|
|
308
|
+
get maxDisplayBoost() { return this._maxDisplayBoost; }
|
|
309
|
+
set maxDisplayBoost(value) {
|
|
310
|
+
this._maxDisplayBoost = value;
|
|
311
|
+
this.calculateWeight();
|
|
312
|
+
}
|
|
313
|
+
calculateWeight() {
|
|
314
|
+
this.uniforms.weightFactor.value = (Math.log2(this._maxDisplayBoost) - this._hdrCapacityMin) / (this._hdrCapacityMax - this._hdrCapacityMin);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Decodes a gain map using a WebGLRenderTarget
|
|
320
|
+
*
|
|
321
|
+
* @category Decoding
|
|
322
|
+
* @group Decoding
|
|
323
|
+
* @example
|
|
324
|
+
* import { decode } from '@monogrid/gainmap-js'
|
|
325
|
+
* import {
|
|
326
|
+
* Mesh,
|
|
327
|
+
* MeshBasicMaterial,
|
|
328
|
+
* PerspectiveCamera,
|
|
329
|
+
* PlaneGeometry,
|
|
330
|
+
* Scene,
|
|
331
|
+
* TextureLoader,
|
|
332
|
+
* WebGLRenderer
|
|
333
|
+
* } from 'three'
|
|
334
|
+
*
|
|
335
|
+
* const renderer = new WebGLRenderer()
|
|
336
|
+
*
|
|
337
|
+
* const textureLoader = new TextureLoader()
|
|
338
|
+
*
|
|
339
|
+
* // load SDR Representation
|
|
340
|
+
* const sdr = await textureLoader.loadAsync('sdr.jpg')
|
|
341
|
+
* // load Gain map recovery image
|
|
342
|
+
* const gainMap = await textureLoader.loadAsync('gainmap.jpg')
|
|
343
|
+
* // load metadata
|
|
344
|
+
* const metadata = await (await fetch('metadata.json')).json()
|
|
345
|
+
*
|
|
346
|
+
* const result = await decode({
|
|
347
|
+
* sdr,
|
|
348
|
+
* gainMap,
|
|
349
|
+
* // this allows to use `result.renderTarget.texture` directly
|
|
350
|
+
* renderer,
|
|
351
|
+
* // this will restore the full HDR range
|
|
352
|
+
* maxDisplayBoost: Math.pow(2, metadata.hdrCapacityMax),
|
|
353
|
+
* ...metadata
|
|
354
|
+
* })
|
|
355
|
+
*
|
|
356
|
+
* const scene = new Scene()
|
|
357
|
+
* // `result` can be used to populate a Texture
|
|
358
|
+
* const mesh = new Mesh(
|
|
359
|
+
* new PlaneGeometry(),
|
|
360
|
+
* new MeshBasicMaterial({ map: result.renderTarget.texture })
|
|
361
|
+
* )
|
|
362
|
+
* scene.add(mesh)
|
|
363
|
+
* renderer.render(scene, new PerspectiveCamera())
|
|
364
|
+
*
|
|
365
|
+
*
|
|
366
|
+
* @param params
|
|
367
|
+
* @returns
|
|
368
|
+
* @throws {Error} if the WebGLRenderer fails to render the gain map
|
|
369
|
+
*/
|
|
370
|
+
const decode = (params) => {
|
|
371
|
+
const { sdr, gainMap, renderer } = params;
|
|
372
|
+
if (sdr.colorSpace !== three.SRGBColorSpace) {
|
|
373
|
+
console.warn('SDR Colorspace needs to be *SRGBColorSpace*, setting it automatically');
|
|
374
|
+
sdr.colorSpace = three.SRGBColorSpace;
|
|
375
|
+
}
|
|
376
|
+
sdr.needsUpdate = true;
|
|
377
|
+
if (gainMap.colorSpace !== three.NoColorSpace) {
|
|
378
|
+
console.warn('Gainmap Colorspace needs to be *NoColorSpace*');
|
|
379
|
+
gainMap.colorSpace = three.NoColorSpace;
|
|
380
|
+
}
|
|
381
|
+
gainMap.needsUpdate = true;
|
|
382
|
+
const material = new GainMapDecoderMaterial(Object.assign(Object.assign({}, params), { sdr,
|
|
383
|
+
gainMap }));
|
|
384
|
+
const quadRenderer = new QuadRenderer(sdr.image.width, sdr.image.height, three.HalfFloatType, three.NoColorSpace, material, renderer);
|
|
385
|
+
try {
|
|
386
|
+
quadRenderer.render();
|
|
387
|
+
}
|
|
388
|
+
catch (e) {
|
|
389
|
+
quadRenderer.dispose();
|
|
390
|
+
throw e;
|
|
391
|
+
}
|
|
392
|
+
return quadRenderer;
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
const getAttribute = (description, name, defaultValue) => {
|
|
396
|
+
var _a;
|
|
397
|
+
let returnValue;
|
|
398
|
+
const parsedValue = (_a = description.attributes.getNamedItem(name)) === null || _a === void 0 ? void 0 : _a.nodeValue;
|
|
399
|
+
if (!parsedValue) {
|
|
400
|
+
const node = description.getElementsByTagName(name)[0];
|
|
401
|
+
if (node) {
|
|
402
|
+
const values = node.getElementsByTagName('rdf:li');
|
|
403
|
+
if (values.length === 3) {
|
|
404
|
+
returnValue = Array.from(values).map(v => v.innerHTML);
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
throw new Error(`Gainmap metadata contains an array of items for ${name} but its length is not 3`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
if (defaultValue)
|
|
412
|
+
return defaultValue;
|
|
413
|
+
else
|
|
414
|
+
throw new Error(`Can't find ${name} in gainmap metadata`);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
else {
|
|
418
|
+
returnValue = parsedValue;
|
|
419
|
+
}
|
|
420
|
+
return returnValue;
|
|
421
|
+
};
|
|
422
|
+
/**
|
|
423
|
+
*
|
|
424
|
+
* @param input
|
|
425
|
+
* @returns
|
|
426
|
+
*/
|
|
427
|
+
const extractXMP = (input) => {
|
|
428
|
+
var _a, _b;
|
|
429
|
+
let str;
|
|
430
|
+
// support node test evnvironment
|
|
431
|
+
if (typeof TextDecoder !== 'undefined')
|
|
432
|
+
str = new TextDecoder().decode(input);
|
|
433
|
+
else
|
|
434
|
+
str = input.toString();
|
|
435
|
+
let start = str.indexOf('<x:xmpmeta');
|
|
436
|
+
const parser = new DOMParser();
|
|
437
|
+
while (start !== -1) {
|
|
438
|
+
const end = str.indexOf('x:xmpmeta>', start);
|
|
439
|
+
str.slice(start, end + 10);
|
|
440
|
+
const xmpBlock = str.slice(start, end + 10);
|
|
441
|
+
try {
|
|
442
|
+
const xmlDocument = parser.parseFromString(xmpBlock, 'text/xml');
|
|
443
|
+
const description = xmlDocument.getElementsByTagName('rdf:Description')[0];
|
|
444
|
+
const gainMapMin = getAttribute(description, 'hdrgm:GainMapMin', '0');
|
|
445
|
+
const gainMapMax = getAttribute(description, 'hdrgm:GainMapMax');
|
|
446
|
+
const gamma = getAttribute(description, 'hdrgm:Gamma', '1');
|
|
447
|
+
const offsetSDR = getAttribute(description, 'hdrgm:OffsetSDR', '0.015625');
|
|
448
|
+
const offsetHDR = getAttribute(description, 'hdrgm:OffsetHDR', '0.015625');
|
|
449
|
+
let hdrCapacityMin = (_a = description.attributes.getNamedItem('hdrgm:HDRCapacityMin')) === null || _a === void 0 ? void 0 : _a.nodeValue;
|
|
450
|
+
if (!hdrCapacityMin)
|
|
451
|
+
hdrCapacityMin = '0';
|
|
452
|
+
const hdrCapacityMax = (_b = description.attributes.getNamedItem('hdrgm:HDRCapacityMax')) === null || _b === void 0 ? void 0 : _b.nodeValue;
|
|
453
|
+
if (!hdrCapacityMax)
|
|
454
|
+
throw new Error('Incomplete gainmap metadata');
|
|
455
|
+
return {
|
|
456
|
+
gainMapMin: Array.isArray(gainMapMin) ? gainMapMin.map(v => parseFloat(v)) : [parseFloat(gainMapMin), parseFloat(gainMapMin), parseFloat(gainMapMin)],
|
|
457
|
+
gainMapMax: Array.isArray(gainMapMax) ? gainMapMax.map(v => parseFloat(v)) : [parseFloat(gainMapMax), parseFloat(gainMapMax), parseFloat(gainMapMax)],
|
|
458
|
+
gamma: Array.isArray(gamma) ? gamma.map(v => parseFloat(v)) : [parseFloat(gamma), parseFloat(gamma), parseFloat(gamma)],
|
|
459
|
+
offsetSdr: Array.isArray(offsetSDR) ? offsetSDR.map(v => parseFloat(v)) : [parseFloat(offsetSDR), parseFloat(offsetSDR), parseFloat(offsetSDR)],
|
|
460
|
+
offsetHdr: Array.isArray(offsetHDR) ? offsetHDR.map(v => parseFloat(v)) : [parseFloat(offsetHDR), parseFloat(offsetHDR), parseFloat(offsetHDR)],
|
|
461
|
+
hdrCapacityMin: parseFloat(hdrCapacityMin),
|
|
462
|
+
hdrCapacityMax: parseFloat(hdrCapacityMax)
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
catch (e) {
|
|
466
|
+
}
|
|
467
|
+
start = str.indexOf('<x:xmpmeta', end);
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* MPF Extractor (Multi Picture Format Extractor)
|
|
473
|
+
* By Henrik S Nilsson 2019
|
|
474
|
+
*
|
|
475
|
+
* Extracts images stored in images based on the MPF format (found here: https://www.cipa.jp/e/std/std-sec.html
|
|
476
|
+
* under "CIPA DC-007-Translation-2021 Multi-Picture Format"
|
|
477
|
+
*
|
|
478
|
+
* Overly commented, and without intention of being complete or production ready.
|
|
479
|
+
* Created to extract depth maps from iPhone images, and to learn about image metadata.
|
|
480
|
+
* Kudos to: Phil Harvey (exiftool), Jaume Sanchez (android-lens-blur-depth-extractor)
|
|
481
|
+
*/
|
|
482
|
+
class MPFExtractor {
|
|
483
|
+
constructor(options) {
|
|
484
|
+
this.options = {
|
|
485
|
+
debug: options && options.debug !== undefined ? options.debug : false,
|
|
486
|
+
extractFII: options && options.extractFII !== undefined ? options.extractFII : true,
|
|
487
|
+
extractNonFII: options && options.extractNonFII !== undefined ? options.extractNonFII : true
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
extract(imageArrayBuffer) {
|
|
491
|
+
return new Promise((resolve, reject) => {
|
|
492
|
+
const debug = this.options.debug;
|
|
493
|
+
const dataView = new DataView(imageArrayBuffer.buffer);
|
|
494
|
+
// If you're executing this line on a big endian machine, it'll be reversed.
|
|
495
|
+
// bigEnd further down though, refers to the endianness of the image itself.
|
|
496
|
+
if (dataView.getUint16(0) !== 0xffd8) {
|
|
497
|
+
reject('Not a valid jpeg');
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
const length = dataView.byteLength;
|
|
501
|
+
let offset = 2;
|
|
502
|
+
let loops = 0;
|
|
503
|
+
let marker; // APP# marker
|
|
504
|
+
while (offset < length) {
|
|
505
|
+
if (++loops > 250) {
|
|
506
|
+
reject(`Found no marker after ${loops} loops 😵`);
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
if (dataView.getUint8(offset) !== 0xff) {
|
|
510
|
+
reject(`Not a valid marker at offset 0x${offset.toString(16)}, found: 0x${dataView.getUint8(offset).toString(16)}`);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
marker = dataView.getUint8(offset + 1);
|
|
514
|
+
if (debug)
|
|
515
|
+
console.log(`Marker: ${marker.toString(16)}`);
|
|
516
|
+
if (marker === 0xe2) {
|
|
517
|
+
if (debug)
|
|
518
|
+
console.log('Found APP2 marker (0xffe2)');
|
|
519
|
+
// Works for iPhone 8 Plus, X, and XSMax. Or any photos of MPF format.
|
|
520
|
+
// Great way to visualize image information in html is using Exiftool. E.g.:
|
|
521
|
+
// ./exiftool.exe -htmldump -wantTrailer photo.jpg > photo.html
|
|
522
|
+
const formatPt = offset + 4;
|
|
523
|
+
/*
|
|
524
|
+
* Structure of the MP Format Identifier
|
|
525
|
+
*
|
|
526
|
+
* Offset Addr. | Code (Hex) | Description
|
|
527
|
+
* +00 ff Marker Prefix <-- offset
|
|
528
|
+
* +01 e2 APP2
|
|
529
|
+
* +02 #n APP2 Field Length
|
|
530
|
+
* +03 #n APP2 Field Length
|
|
531
|
+
* +04 4d 'M' <-- formatPt
|
|
532
|
+
* +05 50 'P'
|
|
533
|
+
* +06 46 'F'
|
|
534
|
+
* +07 00 NULL
|
|
535
|
+
* <-- tiffOffset
|
|
536
|
+
*/
|
|
537
|
+
if (dataView.getUint32(formatPt) === 0x4d504600) {
|
|
538
|
+
// Found MPF tag, so we start dig out sub images
|
|
539
|
+
const tiffOffset = formatPt + 4;
|
|
540
|
+
let bigEnd; // Endianness from TIFF header
|
|
541
|
+
// Test for TIFF validity and endianness
|
|
542
|
+
// 0x4949 and 0x4D4D ('II' and 'MM') marks Little Endian and Big Endian
|
|
543
|
+
if (dataView.getUint16(tiffOffset) === 0x4949) {
|
|
544
|
+
bigEnd = false;
|
|
545
|
+
}
|
|
546
|
+
else if (dataView.getUint16(tiffOffset) === 0x4d4d) {
|
|
547
|
+
bigEnd = true;
|
|
548
|
+
}
|
|
549
|
+
else {
|
|
550
|
+
reject('No valid endianness marker found in TIFF header');
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
if (dataView.getUint16(tiffOffset + 2, !bigEnd) !== 0x002a) {
|
|
554
|
+
reject('Not valid TIFF data! (no 0x002A marker)');
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
// 32 bit number stating the offset from the start of the 8 Byte MP Header
|
|
558
|
+
// to MP Index IFD Least possible value is thus 8 (means 0 offset)
|
|
559
|
+
const firstIFDOffset = dataView.getUint32(tiffOffset + 4, !bigEnd);
|
|
560
|
+
if (firstIFDOffset < 0x00000008) {
|
|
561
|
+
reject('Not valid TIFF data! (First offset less than 8)');
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
// Move ahead to MP Index IFD
|
|
565
|
+
// Assume we're at the first IFD, so firstIFDOffset points to
|
|
566
|
+
// MP Index IFD and not MP Attributes IFD. (If we try extract from a sub image,
|
|
567
|
+
// we fail silently here due to this assumption)
|
|
568
|
+
// Count (2 Byte) | MP Index Fields a.k.a. MP Entries (count * 12 Byte) | Offset of Next IFD (4 Byte)
|
|
569
|
+
const dirStart = tiffOffset + firstIFDOffset; // Start of IFD (Image File Directory)
|
|
570
|
+
const count = dataView.getUint16(dirStart, !bigEnd); // Count of MPEntries (2 Byte)
|
|
571
|
+
// Extract info from MPEntries (starting after Count)
|
|
572
|
+
const entriesStart = dirStart + 2;
|
|
573
|
+
let numberOfImages = 0;
|
|
574
|
+
for (let i = entriesStart; i < entriesStart + 12 * count; i += 12) {
|
|
575
|
+
// Each entry is 12 Bytes long
|
|
576
|
+
// Check MP Index IFD tags, here we only take tag 0xb001 = Number of images
|
|
577
|
+
if (dataView.getUint16(i, !bigEnd) === 0xb001) {
|
|
578
|
+
// stored in Last 4 bytes of its 12 Byte entry.
|
|
579
|
+
numberOfImages = dataView.getUint32(i + 8, !bigEnd);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
const nextIFDOffsetLen = 4; // 4 Byte offset field that appears after MP Index IFD tags
|
|
583
|
+
const MPImageListValPt = dirStart + 2 + count * 12 + nextIFDOffsetLen;
|
|
584
|
+
const images = [];
|
|
585
|
+
for (let i = MPImageListValPt; i < MPImageListValPt + numberOfImages * 16; i += 16) {
|
|
586
|
+
const image = {
|
|
587
|
+
MPType: dataView.getUint32(i, !bigEnd),
|
|
588
|
+
size: dataView.getUint32(i + 4, !bigEnd),
|
|
589
|
+
// This offset is specified relative to the address of the MP Endian
|
|
590
|
+
// field in the MP Header, unless the image is a First Individual Image,
|
|
591
|
+
// in which case the value of the offset shall be NULL (0x00000000).
|
|
592
|
+
dataOffset: dataView.getUint32(i + 8, !bigEnd),
|
|
593
|
+
dependantImages: dataView.getUint32(i + 12, !bigEnd),
|
|
594
|
+
start: -1,
|
|
595
|
+
end: -1,
|
|
596
|
+
isFII: false
|
|
597
|
+
};
|
|
598
|
+
if (!image.dataOffset) {
|
|
599
|
+
// dataOffset is 0x00000000 for First Individual Image
|
|
600
|
+
image.start = 0;
|
|
601
|
+
image.isFII = true;
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
image.start = tiffOffset + image.dataOffset;
|
|
605
|
+
image.isFII = false;
|
|
606
|
+
}
|
|
607
|
+
image.end = image.start + image.size;
|
|
608
|
+
images.push(image);
|
|
609
|
+
}
|
|
610
|
+
if (this.options.extractNonFII && images.length) {
|
|
611
|
+
const bufferBlob = new Blob([dataView]);
|
|
612
|
+
const imgs = [];
|
|
613
|
+
for (const image of images) {
|
|
614
|
+
if (image.isFII && !this.options.extractFII) {
|
|
615
|
+
continue; // Skip FII
|
|
616
|
+
}
|
|
617
|
+
const imageBlob = bufferBlob.slice(image.start, image.end + 1, 'image/jpeg');
|
|
618
|
+
// we don't need this
|
|
619
|
+
// const imageUrl = URL.createObjectURL(imageBlob)
|
|
620
|
+
// image.img = document.createElement('img')
|
|
621
|
+
// image.img.src = imageUrl
|
|
622
|
+
imgs.push(imageBlob);
|
|
623
|
+
}
|
|
624
|
+
resolve(imgs);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
offset += 2 + dataView.getUint16(offset + 2);
|
|
629
|
+
}
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
*
|
|
636
|
+
* @param jpegFile
|
|
637
|
+
* @returns
|
|
638
|
+
*/
|
|
639
|
+
const extractGainmapFromJPEG = async (jpegFile) => {
|
|
640
|
+
const metadata = extractXMP(jpegFile);
|
|
641
|
+
if (!metadata)
|
|
642
|
+
throw new Error('Gain map XMP metadata not found');
|
|
643
|
+
const mpfExtractor = new MPFExtractor({ extractFII: true, extractNonFII: true });
|
|
644
|
+
const images = await mpfExtractor.extract(jpegFile);
|
|
645
|
+
if (images.length !== 2)
|
|
646
|
+
throw new Error('Gain map recovery image not found');
|
|
647
|
+
return {
|
|
648
|
+
sdr: new Uint8Array(await images[0].arrayBuffer()),
|
|
649
|
+
gainMap: new Uint8Array(await images[1].arrayBuffer()),
|
|
650
|
+
metadata
|
|
651
|
+
};
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
class LoaderBase extends three.Loader {
|
|
655
|
+
/**
|
|
656
|
+
*
|
|
657
|
+
* @param renderer
|
|
658
|
+
* @param manager
|
|
659
|
+
*/
|
|
660
|
+
constructor(renderer, manager) {
|
|
661
|
+
super(manager);
|
|
662
|
+
this.renderer = renderer;
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* @private
|
|
666
|
+
* @returns
|
|
667
|
+
*/
|
|
668
|
+
prepareQuadRenderer() {
|
|
669
|
+
// temporary values
|
|
670
|
+
const material = new GainMapDecoderMaterial({
|
|
671
|
+
gainMapMax: [1, 1, 1],
|
|
672
|
+
gainMapMin: [0, 0, 0],
|
|
673
|
+
gamma: [1, 1, 1],
|
|
674
|
+
offsetHdr: [1, 1, 1],
|
|
675
|
+
offsetSdr: [1, 1, 1],
|
|
676
|
+
hdrCapacityMax: 1,
|
|
677
|
+
hdrCapacityMin: 0,
|
|
678
|
+
maxDisplayBoost: 1,
|
|
679
|
+
gainMap: new three.Texture(),
|
|
680
|
+
sdr: new three.Texture()
|
|
681
|
+
});
|
|
682
|
+
return new QuadRenderer(16, 16, three.HalfFloatType, three.NoColorSpace, material, this.renderer);
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
*
|
|
686
|
+
* @private
|
|
687
|
+
* @param quadRenderer
|
|
688
|
+
* @param gainMapBuffer
|
|
689
|
+
* @param sdrBuffer
|
|
690
|
+
* @param metadata
|
|
691
|
+
*/
|
|
692
|
+
async render(quadRenderer, gainMapBuffer, sdrBuffer, metadata) {
|
|
693
|
+
const gainMapBlob = new Blob([gainMapBuffer], { type: 'image/jpeg' });
|
|
694
|
+
// TODO: figure out why result.sdr is not usable here, problem is in the libultrahdr-wasm repo
|
|
695
|
+
// we use the original image buffer instead
|
|
696
|
+
const sdrBlob = new Blob([sdrBuffer], { type: 'image/jpeg' });
|
|
697
|
+
const [gainMapImageBitmap, sdrImageBitmap] = await Promise.all([
|
|
698
|
+
createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }),
|
|
699
|
+
createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
|
|
700
|
+
]);
|
|
701
|
+
const gainMap = new three.Texture(gainMapImageBitmap, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.NoColorSpace);
|
|
702
|
+
gainMap.needsUpdate = true;
|
|
703
|
+
const sdr = new three.Texture(sdrImageBitmap, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.SRGBColorSpace);
|
|
704
|
+
sdr.needsUpdate = true;
|
|
705
|
+
quadRenderer.width = sdrImageBitmap.width;
|
|
706
|
+
quadRenderer.height = sdrImageBitmap.height;
|
|
707
|
+
quadRenderer.material.gainMap = gainMap;
|
|
708
|
+
quadRenderer.material.sdr = sdr;
|
|
709
|
+
quadRenderer.material.gainMapMin = metadata.gainMapMin;
|
|
710
|
+
quadRenderer.material.gainMapMax = metadata.gainMapMax;
|
|
711
|
+
quadRenderer.material.offsetHdr = metadata.offsetHdr;
|
|
712
|
+
quadRenderer.material.offsetSdr = metadata.offsetSdr;
|
|
713
|
+
quadRenderer.material.gamma = metadata.gamma;
|
|
714
|
+
quadRenderer.material.hdrCapacityMin = metadata.hdrCapacityMin;
|
|
715
|
+
quadRenderer.material.hdrCapacityMax = metadata.hdrCapacityMax;
|
|
716
|
+
quadRenderer.material.maxDisplayBoost = Math.pow(2, metadata.hdrCapacityMax);
|
|
717
|
+
quadRenderer.material.needsUpdate = true;
|
|
718
|
+
quadRenderer.render();
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* A Three.js Loader for the gain map format.
|
|
724
|
+
*
|
|
725
|
+
* @category Loaders
|
|
726
|
+
* @group Loaders
|
|
727
|
+
*
|
|
728
|
+
* @example
|
|
729
|
+
* import { GainMapLoader } from '@monogrid/gainmap-js'
|
|
730
|
+
* import {
|
|
731
|
+
* EquirectangularReflectionMapping,
|
|
732
|
+
* LinearFilter,
|
|
733
|
+
* Mesh,
|
|
734
|
+
* MeshBasicMaterial,
|
|
735
|
+
* PerspectiveCamera,
|
|
736
|
+
* PlaneGeometry,
|
|
737
|
+
* Scene,
|
|
738
|
+
* WebGLRenderer
|
|
739
|
+
* } from 'three'
|
|
740
|
+
*
|
|
741
|
+
* const renderer = new WebGLRenderer()
|
|
742
|
+
*
|
|
743
|
+
* const loader = new GainMapLoader(renderer)
|
|
744
|
+
*
|
|
745
|
+
* const result = loader.load(['sdr.jpeg', 'gainmap.jpeg', 'metadata.json'])
|
|
746
|
+
* // `result` can be used to populate a Texture
|
|
747
|
+
*
|
|
748
|
+
* const scene = new Scene()
|
|
749
|
+
* const mesh = new Mesh(
|
|
750
|
+
* new PlaneGeometry(),
|
|
751
|
+
* new MeshBasicMaterial({ map: result.renderTarget.texture })
|
|
752
|
+
* )
|
|
753
|
+
* scene.add(mesh)
|
|
754
|
+
* renderer.render(scene, new PerspectiveCamera())
|
|
755
|
+
*
|
|
756
|
+
* // `result.renderTarget.texture` must be
|
|
757
|
+
* // converted to `DataTexture` in order
|
|
758
|
+
* // to use it as Equirectangular scene background
|
|
759
|
+
* // if needed
|
|
760
|
+
*
|
|
761
|
+
* scene.background = result.toDataTexture()
|
|
762
|
+
* scene.background.mapping = EquirectangularReflectionMapping
|
|
763
|
+
* scene.background.minFilter = LinearFilter
|
|
764
|
+
*
|
|
765
|
+
*
|
|
766
|
+
*/
|
|
767
|
+
class GainMapLoader extends LoaderBase {
|
|
768
|
+
/**
|
|
769
|
+
* Loads a gainmap using separate data
|
|
770
|
+
* * sdr image
|
|
771
|
+
* * gain map image
|
|
772
|
+
* * metadata json
|
|
773
|
+
*
|
|
774
|
+
* useful for webp gain maps
|
|
775
|
+
*
|
|
776
|
+
* @param sdrUrl
|
|
777
|
+
* @param gainMapUrl
|
|
778
|
+
* @param metadataUrl
|
|
779
|
+
* @param onLoad
|
|
780
|
+
* @param onProgress
|
|
781
|
+
* @param onError
|
|
782
|
+
* @returns
|
|
783
|
+
*/
|
|
784
|
+
load([sdrUrl, gainMapUrl, metadataUrl], onLoad, onProgress, onError) {
|
|
785
|
+
const quadRenderer = this.prepareQuadRenderer();
|
|
786
|
+
let sdr;
|
|
787
|
+
let gainMap;
|
|
788
|
+
let metadata;
|
|
789
|
+
const loadCheck = async () => {
|
|
790
|
+
if (sdr && gainMap && metadata) {
|
|
791
|
+
await this.render(quadRenderer, gainMap, sdr, metadata);
|
|
792
|
+
if (typeof onLoad === 'function')
|
|
793
|
+
onLoad(quadRenderer);
|
|
794
|
+
quadRenderer.dispose();
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
const progressHandler = (e) => {
|
|
798
|
+
};
|
|
799
|
+
const sdrLoader = new three.FileLoader(this.manager);
|
|
800
|
+
sdrLoader.setResponseType('arraybuffer');
|
|
801
|
+
sdrLoader.setRequestHeader(this.requestHeader);
|
|
802
|
+
sdrLoader.setPath(this.path);
|
|
803
|
+
sdrLoader.setWithCredentials(this.withCredentials);
|
|
804
|
+
sdrLoader.load(sdrUrl, async (buffer) => {
|
|
805
|
+
if (typeof buffer === 'string')
|
|
806
|
+
throw new Error('Invalid sdr buffer');
|
|
807
|
+
sdr = buffer;
|
|
808
|
+
loadCheck();
|
|
809
|
+
}, progressHandler, onError);
|
|
810
|
+
const gainMapLoader = new three.FileLoader(this.manager);
|
|
811
|
+
gainMapLoader.setResponseType('arraybuffer');
|
|
812
|
+
gainMapLoader.setRequestHeader(this.requestHeader);
|
|
813
|
+
gainMapLoader.setPath(this.path);
|
|
814
|
+
gainMapLoader.setWithCredentials(this.withCredentials);
|
|
815
|
+
gainMapLoader.load(gainMapUrl, async (buffer) => {
|
|
816
|
+
if (typeof buffer === 'string')
|
|
817
|
+
throw new Error('Invalid gainmap buffer');
|
|
818
|
+
gainMap = buffer;
|
|
819
|
+
loadCheck();
|
|
820
|
+
}, progressHandler, onError);
|
|
821
|
+
const metadataLoader = new three.FileLoader(this.manager);
|
|
822
|
+
// metadataLoader.setResponseType('json')
|
|
823
|
+
metadataLoader.setRequestHeader(this.requestHeader);
|
|
824
|
+
metadataLoader.setPath(this.path);
|
|
825
|
+
metadataLoader.setWithCredentials(this.withCredentials);
|
|
826
|
+
metadataLoader.load(metadataUrl, async (json) => {
|
|
827
|
+
if (typeof json !== 'string')
|
|
828
|
+
throw new Error('Invalid metadata string');
|
|
829
|
+
metadata = JSON.parse(json);
|
|
830
|
+
loadCheck();
|
|
831
|
+
}, progressHandler, onError);
|
|
832
|
+
return quadRenderer;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
/**
|
|
837
|
+
* A Three.js Loader for a JPEG with embedded gainmap metadata.
|
|
838
|
+
*
|
|
839
|
+
* @category Loaders
|
|
840
|
+
* @group Loaders
|
|
841
|
+
*
|
|
842
|
+
* @example
|
|
843
|
+
* import { JPEGRLoader } from '@monogrid/gainmap-js'
|
|
844
|
+
* import {
|
|
845
|
+
* EquirectangularReflectionMapping,
|
|
846
|
+
* LinearFilter,
|
|
847
|
+
* Mesh,
|
|
848
|
+
* MeshBasicMaterial,
|
|
849
|
+
* PerspectiveCamera,
|
|
850
|
+
* PlaneGeometry,
|
|
851
|
+
* Scene,
|
|
852
|
+
* WebGLRenderer
|
|
853
|
+
* } from 'three'
|
|
854
|
+
*
|
|
855
|
+
* const renderer = new WebGLRenderer()
|
|
856
|
+
*
|
|
857
|
+
* const loader = new JPEGRLoader(renderer)
|
|
858
|
+
*
|
|
859
|
+
* const result = loader.load('gainmap.jpeg')
|
|
860
|
+
* // `result` can be used to populate a Texture
|
|
861
|
+
*
|
|
862
|
+
* const scene = new Scene()
|
|
863
|
+
* const mesh = new Mesh(
|
|
864
|
+
* new PlaneGeometry(),
|
|
865
|
+
* new MeshBasicMaterial({ map: result.renderTarget.texture })
|
|
866
|
+
* )
|
|
867
|
+
* scene.add(mesh)
|
|
868
|
+
* renderer.render(scene, new PerspectiveCamera())
|
|
869
|
+
*
|
|
870
|
+
* // `result.renderTarget.texture` must be
|
|
871
|
+
* // converted to `DataTexture` in order
|
|
872
|
+
* // to use it as Equirectangular scene background
|
|
873
|
+
* // if needed
|
|
874
|
+
*
|
|
875
|
+
* scene.background = result.toDataTexture()
|
|
876
|
+
* scene.background.mapping = EquirectangularReflectionMapping
|
|
877
|
+
* scene.background.minFilter = LinearFilter
|
|
878
|
+
|
|
879
|
+
*
|
|
880
|
+
*/
|
|
881
|
+
class JPEGRLoader extends LoaderBase {
|
|
882
|
+
/**
|
|
883
|
+
*
|
|
884
|
+
* @param url
|
|
885
|
+
* @param onLoad
|
|
886
|
+
* @param onProgress
|
|
887
|
+
* @param onError
|
|
888
|
+
* @returns
|
|
889
|
+
*/
|
|
890
|
+
load(url, onLoad, onProgress, onError) {
|
|
891
|
+
const quadRenderer = this.prepareQuadRenderer();
|
|
892
|
+
const loader = new three.FileLoader(this.manager);
|
|
893
|
+
loader.setResponseType('arraybuffer');
|
|
894
|
+
loader.setRequestHeader(this.requestHeader);
|
|
895
|
+
loader.setPath(this.path);
|
|
896
|
+
loader.setWithCredentials(this.withCredentials);
|
|
897
|
+
loader.load(url, async (jpeg) => {
|
|
898
|
+
if (typeof jpeg === 'string')
|
|
899
|
+
throw new Error('Invalid buffer');
|
|
900
|
+
const { gainMap: gainMapJPEG, sdr: sdrJPEG, metadata } = await extractGainmapFromJPEG(new Uint8Array(jpeg));
|
|
901
|
+
await this.render(quadRenderer, gainMapJPEG, sdrJPEG, metadata);
|
|
902
|
+
if (typeof onLoad === 'function')
|
|
903
|
+
onLoad(quadRenderer);
|
|
904
|
+
quadRenderer.dispose();
|
|
905
|
+
}, onProgress, onError);
|
|
906
|
+
return quadRenderer;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
exports.GainMapDecoderMaterial = GainMapDecoderMaterial;
|
|
911
|
+
exports.GainMapLoader = GainMapLoader;
|
|
912
|
+
exports.JPEGRLoader = JPEGRLoader;
|
|
913
|
+
exports.QuadRenderer = QuadRenderer;
|
|
914
|
+
exports.decode = decode;
|
|
915
|
+
exports.extractGainmapFromJPEG = extractGainmapFromJPEG;
|
|
916
|
+
|
|
917
|
+
}));
|