@monogrid/gainmap-js 3.0.6 → 3.2.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 +14 -0
- package/CODE_OF_CONDUCT.md +1 -1
- package/CONTRIBUTING.md +1 -1
- package/README.md +106 -2
- package/dist/Loader-BZcKUVaX.js +424 -0
- package/dist/{QuadRenderer-B3h-sJtp.js → QuadRenderer-Y5tm-R3m.js} +39 -30
- package/dist/{_commonjsHelpers-UqyNh5ZN.js → _commonjsHelpers-CuGYGi_5.js} +2 -2
- package/dist/{compress-CAiPfiRr.js → compress-BaKKOAXI.js} +2 -3
- package/dist/core/QuadRenderer.d.ts +2 -2
- package/dist/core/get-data-texture.d.ts +4 -3
- package/dist/core/types.d.ts +2 -1
- package/dist/decode/index.d.ts +1 -11
- package/dist/decode/shared/Loader.d.ts +59 -0
- package/dist/decode/shared/decode.d.ts +24 -0
- package/dist/decode/{extract.d.ts → shared/extract.d.ts} +4 -4
- package/dist/decode/shared/index.d.ts +8 -0
- package/dist/decode/shared/types.d.ts +53 -0
- package/dist/decode/{utils → shared/utils}/MPFExtractor.d.ts +1 -1
- package/dist/decode/shared/utils/extractXMP.d.ts +2 -0
- package/dist/decode/{decode.d.ts → webgl/decode.d.ts} +6 -6
- package/dist/decode/webgl/index.d.ts +8 -0
- package/dist/decode/{loaders → webgl/loaders}/GainMapLoader.d.ts +5 -6
- package/dist/decode/{loaders → webgl/loaders}/HDRJPGLoader.d.ts +6 -7
- package/dist/decode/webgl/loaders/LoaderBaseWebGL.d.ts +20 -0
- package/dist/decode/{materials → webgl/materials}/GainMapDecoderMaterial.d.ts +2 -2
- package/dist/decode/webgpu/core/QuadRenderer.d.ts +153 -0
- package/dist/decode/webgpu/decode.d.ts +60 -0
- package/dist/decode/webgpu/index.d.ts +8 -0
- package/dist/decode/webgpu/loaders/GainMapLoader.d.ts +69 -0
- package/dist/decode/webgpu/loaders/HDRJPGLoader.d.ts +65 -0
- package/dist/decode/webgpu/loaders/LoaderBaseWebGPU.d.ts +20 -0
- package/dist/decode/webgpu/materials/GainMapDecoderMaterial.d.ts +78 -0
- package/dist/decode.js +45 -442
- package/dist/decode.umd.cjs +388 -358
- package/dist/decode.webgpu.js +814 -0
- package/dist/decode.webgpu.umd.cjs +1244 -0
- package/dist/encode/encode-and-compress.d.ts +3 -3
- package/dist/encode/encode.d.ts +3 -3
- package/dist/encode/find-texture-min-max.d.ts +4 -3
- package/dist/encode/types.d.ts +4 -3
- package/dist/encode.js +32 -11
- package/dist/encode.umd.cjs +67 -38
- package/dist/libultrahdr/decode-jpeg-metadata.d.ts +0 -3
- package/dist/libultrahdr/encode-jpeg-metadata.d.ts +2 -2
- package/dist/libultrahdr.js +42 -75
- package/dist/libultrahdr.umd.cjs +46 -79
- package/dist/worker-interface.js +74 -64
- package/dist/worker-interface.umd.cjs +73 -63
- package/dist/worker.js +103 -88
- package/dist/worker.umd.cjs +103 -89
- package/package.json +79 -72
- package/dist/decode/loaders/LoaderBase.d.ts +0 -45
- package/dist/decode/types.d.ts +0 -42
- package/dist/decode/utils/extractXMP.d.ts +0 -7
- /package/dist/decode/{errors → shared/errors}/GainMapNotFoundError.d.ts +0 -0
- /package/dist/decode/{errors → shared/errors}/XMPMetadataNotFoundError.d.ts +0 -0
- /package/dist/decode/{utils → shared/utils}/get-html-image-from-blob.d.ts +0 -0
package/dist/decode.umd.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @monogrid/gainmap-js v3.0
|
|
3
|
-
* With ❤️, by MONOGRID <
|
|
2
|
+
* @monogrid/gainmap-js v3.2.0
|
|
3
|
+
* With ❤️, by MONOGRID <gainmap@monogrid.com>
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
(function (global, factory) {
|
|
@@ -74,29 +74,24 @@
|
|
|
74
74
|
* @group Core
|
|
75
75
|
*/
|
|
76
76
|
class QuadRenderer {
|
|
77
|
+
_renderer;
|
|
78
|
+
_rendererIsDisposable = false;
|
|
79
|
+
_material;
|
|
80
|
+
_scene;
|
|
81
|
+
_camera;
|
|
82
|
+
_quad;
|
|
83
|
+
_renderTarget;
|
|
84
|
+
_width;
|
|
85
|
+
_height;
|
|
86
|
+
_type;
|
|
87
|
+
_colorSpace;
|
|
88
|
+
_supportsReadPixels = true;
|
|
77
89
|
/**
|
|
78
90
|
* Constructs a new QuadRenderer
|
|
79
91
|
*
|
|
80
92
|
* @param options Parameters for this QuadRenderer
|
|
81
93
|
*/
|
|
82
94
|
constructor(options) {
|
|
83
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
|
|
84
|
-
this._rendererIsDisposable = false;
|
|
85
|
-
this._supportsReadPixels = true;
|
|
86
|
-
/**
|
|
87
|
-
* Renders the input texture using the specified material
|
|
88
|
-
*/
|
|
89
|
-
this.render = () => {
|
|
90
|
-
this._renderer.setRenderTarget(this._renderTarget);
|
|
91
|
-
try {
|
|
92
|
-
this._renderer.render(this._scene, this._camera);
|
|
93
|
-
}
|
|
94
|
-
catch (e) {
|
|
95
|
-
this._renderer.setRenderTarget(null);
|
|
96
|
-
throw e;
|
|
97
|
-
}
|
|
98
|
-
this._renderer.setRenderTarget(null);
|
|
99
|
-
};
|
|
100
95
|
this._width = options.width;
|
|
101
96
|
this._height = options.height;
|
|
102
97
|
this._type = options.type;
|
|
@@ -109,13 +104,13 @@
|
|
|
109
104
|
// user options
|
|
110
105
|
type: this._type, // set in class property
|
|
111
106
|
colorSpace: this._colorSpace, // set in class property
|
|
112
|
-
anisotropy:
|
|
113
|
-
generateMipmaps:
|
|
114
|
-
magFilter:
|
|
115
|
-
minFilter:
|
|
116
|
-
samples:
|
|
117
|
-
wrapS:
|
|
118
|
-
wrapT:
|
|
107
|
+
anisotropy: options.renderTargetOptions?.anisotropy !== undefined ? options.renderTargetOptions?.anisotropy : 1,
|
|
108
|
+
generateMipmaps: options.renderTargetOptions?.generateMipmaps !== undefined ? options.renderTargetOptions?.generateMipmaps : false,
|
|
109
|
+
magFilter: options.renderTargetOptions?.magFilter !== undefined ? options.renderTargetOptions?.magFilter : three.LinearFilter,
|
|
110
|
+
minFilter: options.renderTargetOptions?.minFilter !== undefined ? options.renderTargetOptions?.minFilter : three.LinearFilter,
|
|
111
|
+
samples: options.renderTargetOptions?.samples !== undefined ? options.renderTargetOptions?.samples : undefined,
|
|
112
|
+
wrapS: options.renderTargetOptions?.wrapS !== undefined ? options.renderTargetOptions?.wrapS : three.ClampToEdgeWrapping,
|
|
113
|
+
wrapT: options.renderTargetOptions?.wrapT !== undefined ? options.renderTargetOptions?.wrapT : three.ClampToEdgeWrapping
|
|
119
114
|
};
|
|
120
115
|
this._material = options.material;
|
|
121
116
|
if (options.renderer) {
|
|
@@ -153,7 +148,7 @@
|
|
|
153
148
|
this._quad.geometry.computeBoundingBox();
|
|
154
149
|
this._scene.add(this._quad);
|
|
155
150
|
this._renderTarget = new three.WebGLRenderTarget(this.width, this.height, rtOptions);
|
|
156
|
-
this._renderTarget.texture.mapping =
|
|
151
|
+
this._renderTarget.texture.mapping = options.renderTargetOptions?.mapping !== undefined ? options.renderTargetOptions?.mapping : three.UVMapping;
|
|
157
152
|
}
|
|
158
153
|
/**
|
|
159
154
|
* Instantiates a temporary renderer
|
|
@@ -169,6 +164,20 @@
|
|
|
169
164
|
// this._rendererIsDisposable = true
|
|
170
165
|
return renderer;
|
|
171
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* Renders the input texture using the specified material
|
|
169
|
+
*/
|
|
170
|
+
render = () => {
|
|
171
|
+
this._renderer.setRenderTarget(this._renderTarget);
|
|
172
|
+
try {
|
|
173
|
+
this._renderer.render(this._scene, this._camera);
|
|
174
|
+
}
|
|
175
|
+
catch (e) {
|
|
176
|
+
this._renderer.setRenderTarget(null);
|
|
177
|
+
throw e;
|
|
178
|
+
}
|
|
179
|
+
this._renderer.setRenderTarget(null);
|
|
180
|
+
};
|
|
172
181
|
/**
|
|
173
182
|
* Obtains a Buffer containing the rendered texture.
|
|
174
183
|
*
|
|
@@ -194,11 +203,11 @@
|
|
|
194
203
|
// fixed values
|
|
195
204
|
this.toArray(), this.width, this.height, three.RGBAFormat, this._type,
|
|
196
205
|
// user values
|
|
197
|
-
|
|
206
|
+
options?.mapping || three.UVMapping, options?.wrapS || three.ClampToEdgeWrapping, options?.wrapT || three.ClampToEdgeWrapping, options?.magFilter || three.LinearFilter, options?.minFilter || three.LinearFilter, options?.anisotropy || 1,
|
|
198
207
|
// fixed value
|
|
199
208
|
three.LinearSRGBColorSpace);
|
|
200
209
|
// set this afterwards, we can't set it in constructor
|
|
201
|
-
returnValue.generateMipmaps =
|
|
210
|
+
returnValue.generateMipmaps = options?.generateMipmaps !== undefined ? options?.generateMipmaps : false;
|
|
202
211
|
return returnValue;
|
|
203
212
|
}
|
|
204
213
|
/**
|
|
@@ -302,284 +311,67 @@
|
|
|
302
311
|
get colorSpace() { return this._colorSpace; }
|
|
303
312
|
}
|
|
304
313
|
|
|
305
|
-
const vertexShader = /* glsl */ `
|
|
306
|
-
varying vec2 vUv;
|
|
307
|
-
|
|
308
|
-
void main() {
|
|
309
|
-
vUv = uv;
|
|
310
|
-
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
311
|
-
}
|
|
312
|
-
`;
|
|
313
|
-
const fragmentShader = /* glsl */ `
|
|
314
|
-
// min half float value
|
|
315
|
-
#define HALF_FLOAT_MIN vec3( -65504, -65504, -65504 )
|
|
316
|
-
// max half float value
|
|
317
|
-
#define HALF_FLOAT_MAX vec3( 65504, 65504, 65504 )
|
|
318
|
-
|
|
319
|
-
uniform sampler2D sdr;
|
|
320
|
-
uniform sampler2D gainMap;
|
|
321
|
-
uniform vec3 gamma;
|
|
322
|
-
uniform vec3 offsetHdr;
|
|
323
|
-
uniform vec3 offsetSdr;
|
|
324
|
-
uniform vec3 gainMapMin;
|
|
325
|
-
uniform vec3 gainMapMax;
|
|
326
|
-
uniform float weightFactor;
|
|
327
|
-
|
|
328
|
-
varying vec2 vUv;
|
|
329
|
-
|
|
330
|
-
void main() {
|
|
331
|
-
vec3 rgb = texture2D( sdr, vUv ).rgb;
|
|
332
|
-
vec3 recovery = texture2D( gainMap, vUv ).rgb;
|
|
333
|
-
vec3 logRecovery = pow( recovery, gamma );
|
|
334
|
-
vec3 logBoost = gainMapMin * ( 1.0 - logRecovery ) + gainMapMax * logRecovery;
|
|
335
|
-
vec3 hdrColor = (rgb + offsetSdr) * exp2( logBoost * weightFactor ) - offsetHdr;
|
|
336
|
-
vec3 clampedHdrColor = max( HALF_FLOAT_MIN, min( HALF_FLOAT_MAX, hdrColor ));
|
|
337
|
-
gl_FragColor = vec4( clampedHdrColor , 1.0 );
|
|
338
|
-
}
|
|
339
|
-
`;
|
|
340
314
|
/**
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
* @category Materials
|
|
344
|
-
* @group Materials
|
|
315
|
+
* Shared decode implementation factory
|
|
316
|
+
* Creates a decode function that prepares a QuadRenderer with the given parameters
|
|
345
317
|
*/
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
gainMapMax: { value: new three.Vector3().fromArray(gainMapMax) },
|
|
364
|
-
weightFactor: {
|
|
365
|
-
value: (Math.log2(maxDisplayBoost) - hdrCapacityMin) / (hdrCapacityMax - hdrCapacityMin)
|
|
366
|
-
}
|
|
367
|
-
},
|
|
368
|
-
blending: three.NoBlending,
|
|
369
|
-
depthTest: false,
|
|
370
|
-
depthWrite: false
|
|
318
|
+
function createDecodeFunction(config) {
|
|
319
|
+
return (params) => {
|
|
320
|
+
const { sdr, gainMap, renderer } = params;
|
|
321
|
+
if (sdr.colorSpace !== three.SRGBColorSpace) {
|
|
322
|
+
console.warn('SDR Colorspace needs to be *SRGBColorSpace*, setting it automatically');
|
|
323
|
+
sdr.colorSpace = three.SRGBColorSpace;
|
|
324
|
+
}
|
|
325
|
+
sdr.needsUpdate = true;
|
|
326
|
+
if (gainMap.colorSpace !== three.LinearSRGBColorSpace) {
|
|
327
|
+
console.warn('Gainmap Colorspace needs to be *LinearSRGBColorSpace*, setting it automatically');
|
|
328
|
+
gainMap.colorSpace = three.LinearSRGBColorSpace;
|
|
329
|
+
}
|
|
330
|
+
gainMap.needsUpdate = true;
|
|
331
|
+
const material = config.createMaterial({
|
|
332
|
+
...params,
|
|
333
|
+
sdr,
|
|
334
|
+
gainMap
|
|
371
335
|
});
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
* @see {@link GainMapMetadata.offsetHdr}
|
|
384
|
-
*/
|
|
385
|
-
get offsetHdr() { return this.uniforms.offsetHdr.value.toArray(); }
|
|
386
|
-
set offsetHdr(value) { this.uniforms.offsetHdr.value.fromArray(value); }
|
|
387
|
-
/**
|
|
388
|
-
* @see {@link GainMapMetadata.offsetSdr}
|
|
389
|
-
*/
|
|
390
|
-
get offsetSdr() { return this.uniforms.offsetSdr.value.toArray(); }
|
|
391
|
-
set offsetSdr(value) { this.uniforms.offsetSdr.value.fromArray(value); }
|
|
392
|
-
/**
|
|
393
|
-
* @see {@link GainMapMetadata.gainMapMin}
|
|
394
|
-
*/
|
|
395
|
-
get gainMapMin() { return this.uniforms.gainMapMin.value.toArray(); }
|
|
396
|
-
set gainMapMin(value) { this.uniforms.gainMapMin.value.fromArray(value); }
|
|
397
|
-
/**
|
|
398
|
-
* @see {@link GainMapMetadata.gainMapMax}
|
|
399
|
-
*/
|
|
400
|
-
get gainMapMax() { return this.uniforms.gainMapMax.value.toArray(); }
|
|
401
|
-
set gainMapMax(value) { this.uniforms.gainMapMax.value.fromArray(value); }
|
|
402
|
-
/**
|
|
403
|
-
* @see {@link GainMapMetadata.gamma}
|
|
404
|
-
*/
|
|
405
|
-
get gamma() {
|
|
406
|
-
const g = this.uniforms.gamma.value;
|
|
407
|
-
return [1 / g.x, 1 / g.y, 1 / g.z];
|
|
408
|
-
}
|
|
409
|
-
set gamma(value) {
|
|
410
|
-
const g = this.uniforms.gamma.value;
|
|
411
|
-
g.x = 1.0 / value[0];
|
|
412
|
-
g.y = 1.0 / value[1];
|
|
413
|
-
g.z = 1.0 / value[2];
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* @see {@link GainMapMetadata.hdrCapacityMin}
|
|
417
|
-
* @remarks Logarithmic space
|
|
418
|
-
*/
|
|
419
|
-
get hdrCapacityMin() { return this._hdrCapacityMin; }
|
|
420
|
-
set hdrCapacityMin(value) {
|
|
421
|
-
this._hdrCapacityMin = value;
|
|
422
|
-
this.calculateWeight();
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* @see {@link GainMapMetadata.hdrCapacityMin}
|
|
426
|
-
* @remarks Logarithmic space
|
|
427
|
-
*/
|
|
428
|
-
get hdrCapacityMax() { return this._hdrCapacityMax; }
|
|
429
|
-
set hdrCapacityMax(value) {
|
|
430
|
-
this._hdrCapacityMax = value;
|
|
431
|
-
this.calculateWeight();
|
|
432
|
-
}
|
|
433
|
-
/**
|
|
434
|
-
* @see {@link GainmapDecodingParameters.maxDisplayBoost}
|
|
435
|
-
* @remarks Non Logarithmic space
|
|
436
|
-
*/
|
|
437
|
-
get maxDisplayBoost() { return this._maxDisplayBoost; }
|
|
438
|
-
set maxDisplayBoost(value) {
|
|
439
|
-
this._maxDisplayBoost = Math.max(1, Math.min(65504, value));
|
|
440
|
-
this.calculateWeight();
|
|
441
|
-
}
|
|
442
|
-
calculateWeight() {
|
|
443
|
-
const val = (Math.log2(this._maxDisplayBoost) - this._hdrCapacityMin) / (this._hdrCapacityMax - this._hdrCapacityMin);
|
|
444
|
-
this.uniforms.weightFactor.value = Math.max(0, Math.min(1, val));
|
|
445
|
-
}
|
|
336
|
+
const quadRenderer = config.createQuadRenderer({
|
|
337
|
+
width: sdr.image.width,
|
|
338
|
+
height: sdr.image.height,
|
|
339
|
+
type: three.HalfFloatType,
|
|
340
|
+
colorSpace: three.LinearSRGBColorSpace,
|
|
341
|
+
material,
|
|
342
|
+
renderer,
|
|
343
|
+
renderTargetOptions: params.renderTargetOptions
|
|
344
|
+
});
|
|
345
|
+
return quadRenderer;
|
|
346
|
+
};
|
|
446
347
|
}
|
|
447
348
|
|
|
448
|
-
/**
|
|
449
|
-
* Decodes a gain map using a WebGLRenderTarget
|
|
450
|
-
*
|
|
451
|
-
* @category Decoding Functions
|
|
452
|
-
* @group Decoding Functions
|
|
453
|
-
* @example
|
|
454
|
-
* import { decode } from '@monogrid/gainmap-js'
|
|
455
|
-
* import {
|
|
456
|
-
* Mesh,
|
|
457
|
-
* MeshBasicMaterial,
|
|
458
|
-
* PerspectiveCamera,
|
|
459
|
-
* PlaneGeometry,
|
|
460
|
-
* Scene,
|
|
461
|
-
* TextureLoader,
|
|
462
|
-
* WebGLRenderer
|
|
463
|
-
* } from 'three'
|
|
464
|
-
*
|
|
465
|
-
* const renderer = new WebGLRenderer()
|
|
466
|
-
*
|
|
467
|
-
* const textureLoader = new TextureLoader()
|
|
468
|
-
*
|
|
469
|
-
* // load SDR Representation
|
|
470
|
-
* const sdr = await textureLoader.loadAsync('sdr.jpg')
|
|
471
|
-
* // load Gain map recovery image
|
|
472
|
-
* const gainMap = await textureLoader.loadAsync('gainmap.jpg')
|
|
473
|
-
* // load metadata
|
|
474
|
-
* const metadata = await (await fetch('metadata.json')).json()
|
|
475
|
-
*
|
|
476
|
-
* const result = await decode({
|
|
477
|
-
* sdr,
|
|
478
|
-
* gainMap,
|
|
479
|
-
* // this allows to use `result.renderTarget.texture` directly
|
|
480
|
-
* renderer,
|
|
481
|
-
* // this will restore the full HDR range
|
|
482
|
-
* maxDisplayBoost: Math.pow(2, metadata.hdrCapacityMax),
|
|
483
|
-
* ...metadata
|
|
484
|
-
* })
|
|
485
|
-
*
|
|
486
|
-
* const scene = new Scene()
|
|
487
|
-
* // `result` can be used to populate a Texture
|
|
488
|
-
* const mesh = new Mesh(
|
|
489
|
-
* new PlaneGeometry(),
|
|
490
|
-
* new MeshBasicMaterial({ map: result.renderTarget.texture })
|
|
491
|
-
* )
|
|
492
|
-
* scene.add(mesh)
|
|
493
|
-
* renderer.render(scene, new PerspectiveCamera())
|
|
494
|
-
*
|
|
495
|
-
* // result must be manually disposed
|
|
496
|
-
* // when you are done using it
|
|
497
|
-
* result.dispose()
|
|
498
|
-
*
|
|
499
|
-
* @param params
|
|
500
|
-
* @returns
|
|
501
|
-
* @throws {Error} if the WebGLRenderer fails to render the gain map
|
|
502
|
-
*/
|
|
503
|
-
const decode = (params) => {
|
|
504
|
-
const { sdr, gainMap, renderer } = params;
|
|
505
|
-
if (sdr.colorSpace !== three.SRGBColorSpace) {
|
|
506
|
-
console.warn('SDR Colorspace needs to be *SRGBColorSpace*, setting it automatically');
|
|
507
|
-
sdr.colorSpace = three.SRGBColorSpace;
|
|
508
|
-
}
|
|
509
|
-
sdr.needsUpdate = true;
|
|
510
|
-
if (gainMap.colorSpace !== three.LinearSRGBColorSpace) {
|
|
511
|
-
console.warn('Gainmap Colorspace needs to be *LinearSRGBColorSpace*, setting it automatically');
|
|
512
|
-
gainMap.colorSpace = three.LinearSRGBColorSpace;
|
|
513
|
-
}
|
|
514
|
-
gainMap.needsUpdate = true;
|
|
515
|
-
const material = new GainMapDecoderMaterial({
|
|
516
|
-
...params,
|
|
517
|
-
sdr,
|
|
518
|
-
gainMap
|
|
519
|
-
});
|
|
520
|
-
const quadRenderer = new QuadRenderer({
|
|
521
|
-
// TODO: three types are generic, eslint complains here, see how we can solve
|
|
522
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
|
523
|
-
width: sdr.image.width,
|
|
524
|
-
// TODO: three types are generic, eslint complains here, see how we can solve
|
|
525
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
|
526
|
-
height: sdr.image.height,
|
|
527
|
-
type: three.HalfFloatType,
|
|
528
|
-
colorSpace: three.LinearSRGBColorSpace,
|
|
529
|
-
material,
|
|
530
|
-
renderer,
|
|
531
|
-
renderTargetOptions: params.renderTargetOptions
|
|
532
|
-
});
|
|
533
|
-
try {
|
|
534
|
-
quadRenderer.render();
|
|
535
|
-
}
|
|
536
|
-
catch (e) {
|
|
537
|
-
quadRenderer.disposeOnDemandRenderer();
|
|
538
|
-
throw e;
|
|
539
|
-
}
|
|
540
|
-
return quadRenderer;
|
|
541
|
-
};
|
|
542
|
-
|
|
543
349
|
class GainMapNotFoundError extends Error {
|
|
544
350
|
}
|
|
545
351
|
|
|
546
352
|
class XMPMetadataNotFoundError extends Error {
|
|
547
353
|
}
|
|
548
354
|
|
|
549
|
-
const
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
throw new Error(`Gainmap metadata contains an array of items for ${name} but its length is not 3`);
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
else {
|
|
565
|
-
if (defaultValue)
|
|
566
|
-
return defaultValue;
|
|
567
|
-
else
|
|
568
|
-
throw new Error(`Can't find ${name} in gainmap metadata`);
|
|
355
|
+
const getXMLValue = (xml, tag, defaultValue) => {
|
|
356
|
+
// Check for attribute format first: tag="value"
|
|
357
|
+
const attributeMatch = new RegExp(`${tag}="([^"]*)"`, 'i').exec(xml);
|
|
358
|
+
if (attributeMatch)
|
|
359
|
+
return attributeMatch[1];
|
|
360
|
+
// Check for tag format: <tag>value</tag> or <tag><rdf:li>value</rdf:li>...</tag>
|
|
361
|
+
const tagMatch = new RegExp(`<${tag}[^>]*>([\\s\\S]*?)</${tag}>`, 'i').exec(xml);
|
|
362
|
+
if (tagMatch) {
|
|
363
|
+
// Check if it contains rdf:li elements
|
|
364
|
+
const liValues = tagMatch[1].match(/<rdf:li>([^<]*)<\/rdf:li>/g);
|
|
365
|
+
if (liValues && liValues.length === 3) {
|
|
366
|
+
return liValues.map(v => v.replace(/<\/?rdf:li>/g, ''));
|
|
569
367
|
}
|
|
368
|
+
return tagMatch[1].trim();
|
|
570
369
|
}
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
}
|
|
574
|
-
return returnValue;
|
|
370
|
+
if (defaultValue !== undefined)
|
|
371
|
+
return defaultValue;
|
|
372
|
+
throw new Error(`Can't find ${tag} in gainmap metadata`);
|
|
575
373
|
};
|
|
576
|
-
/**
|
|
577
|
-
*
|
|
578
|
-
* @param input
|
|
579
|
-
* @returns
|
|
580
|
-
*/
|
|
581
374
|
const extractXMP = (input) => {
|
|
582
|
-
var _a, _b;
|
|
583
375
|
let str;
|
|
584
376
|
// support node test environment
|
|
585
377
|
if (typeof TextDecoder !== 'undefined')
|
|
@@ -587,25 +379,22 @@ void main() {
|
|
|
587
379
|
else
|
|
588
380
|
str = input.toString();
|
|
589
381
|
let start = str.indexOf('<x:xmpmeta');
|
|
590
|
-
const parser = new DOMParser();
|
|
591
382
|
while (start !== -1) {
|
|
592
383
|
const end = str.indexOf('x:xmpmeta>', start);
|
|
593
|
-
str.slice(start, end + 10);
|
|
594
384
|
const xmpBlock = str.slice(start, end + 10);
|
|
595
385
|
try {
|
|
596
|
-
const
|
|
597
|
-
const
|
|
598
|
-
const
|
|
599
|
-
const
|
|
600
|
-
const
|
|
601
|
-
|
|
602
|
-
const
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
const hdrCapacityMax = (_b = description.attributes.getNamedItem('hdrgm:HDRCapacityMax')) === null || _b === void 0 ? void 0 : _b.nodeValue;
|
|
607
|
-
if (!hdrCapacityMax)
|
|
386
|
+
const gainMapMin = getXMLValue(xmpBlock, 'hdrgm:GainMapMin', '0');
|
|
387
|
+
const gainMapMax = getXMLValue(xmpBlock, 'hdrgm:GainMapMax');
|
|
388
|
+
const gamma = getXMLValue(xmpBlock, 'hdrgm:Gamma', '1');
|
|
389
|
+
const offsetSDR = getXMLValue(xmpBlock, 'hdrgm:OffsetSDR', '0.015625');
|
|
390
|
+
const offsetHDR = getXMLValue(xmpBlock, 'hdrgm:OffsetHDR', '0.015625');
|
|
391
|
+
// These are always attributes, so we can use a simpler regex
|
|
392
|
+
const hdrCapacityMinMatch = /hdrgm:HDRCapacityMin="([^"]*)"/.exec(xmpBlock);
|
|
393
|
+
const hdrCapacityMin = hdrCapacityMinMatch ? hdrCapacityMinMatch[1] : '0';
|
|
394
|
+
const hdrCapacityMaxMatch = /hdrgm:HDRCapacityMax="([^"]*)"/.exec(xmpBlock);
|
|
395
|
+
if (!hdrCapacityMaxMatch)
|
|
608
396
|
throw new Error('Incomplete gainmap metadata');
|
|
397
|
+
const hdrCapacityMax = hdrCapacityMaxMatch[1];
|
|
609
398
|
return {
|
|
610
399
|
gainMapMin: Array.isArray(gainMapMin) ? gainMapMin.map(v => parseFloat(v)) : [parseFloat(gainMapMin), parseFloat(gainMapMin), parseFloat(gainMapMin)],
|
|
611
400
|
gainMapMax: Array.isArray(gainMapMax) ? gainMapMax.map(v => parseFloat(v)) : [parseFloat(gainMapMax), parseFloat(gainMapMax), parseFloat(gainMapMax)],
|
|
@@ -617,6 +406,7 @@ void main() {
|
|
|
617
406
|
};
|
|
618
407
|
}
|
|
619
408
|
catch (e) {
|
|
409
|
+
// Continue searching for another xmpmeta block if this one fails
|
|
620
410
|
}
|
|
621
411
|
start = str.indexOf('<x:xmpmeta', end);
|
|
622
412
|
}
|
|
@@ -634,6 +424,7 @@ void main() {
|
|
|
634
424
|
* Kudos to: Phil Harvey (exiftool), Jaume Sanchez (android-lens-blur-depth-extractor)
|
|
635
425
|
*/
|
|
636
426
|
class MPFExtractor {
|
|
427
|
+
options;
|
|
637
428
|
constructor(options) {
|
|
638
429
|
this.options = {
|
|
639
430
|
debug: options && options.debug !== undefined ? options.debug : false,
|
|
@@ -830,52 +621,40 @@ void main() {
|
|
|
830
621
|
return new Promise((resolve, reject) => {
|
|
831
622
|
const img = document.createElement('img');
|
|
832
623
|
img.onload = () => { resolve(img); };
|
|
624
|
+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
833
625
|
img.onerror = (e) => { reject(e); };
|
|
834
626
|
img.src = URL.createObjectURL(blob);
|
|
835
627
|
});
|
|
836
628
|
};
|
|
837
629
|
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
630
|
+
/**
|
|
631
|
+
* Shared base class for loaders that extracts common logic
|
|
632
|
+
*/
|
|
633
|
+
class LoaderBaseShared extends three.Loader {
|
|
634
|
+
_renderer;
|
|
635
|
+
_renderTargetOptions;
|
|
636
|
+
_internalLoadingManager;
|
|
637
|
+
_config;
|
|
638
|
+
constructor(config, manager) {
|
|
845
639
|
super(manager);
|
|
846
|
-
|
|
847
|
-
|
|
640
|
+
this._config = config;
|
|
641
|
+
if (config.renderer)
|
|
642
|
+
this._renderer = config.renderer;
|
|
848
643
|
this._internalLoadingManager = new three.LoadingManager();
|
|
849
644
|
}
|
|
850
|
-
/**
|
|
851
|
-
* Specify the renderer to use when rendering the gain map
|
|
852
|
-
*
|
|
853
|
-
* @param renderer
|
|
854
|
-
* @returns
|
|
855
|
-
*/
|
|
856
645
|
setRenderer(renderer) {
|
|
857
646
|
this._renderer = renderer;
|
|
858
647
|
return this;
|
|
859
648
|
}
|
|
860
|
-
/**
|
|
861
|
-
* Specify the renderTarget options to use when rendering the gain map
|
|
862
|
-
*
|
|
863
|
-
* @param options
|
|
864
|
-
* @returns
|
|
865
|
-
*/
|
|
866
649
|
setRenderTargetOptions(options) {
|
|
867
650
|
this._renderTargetOptions = options;
|
|
868
651
|
return this;
|
|
869
652
|
}
|
|
870
|
-
/**
|
|
871
|
-
* @private
|
|
872
|
-
* @returns
|
|
873
|
-
*/
|
|
874
653
|
prepareQuadRenderer() {
|
|
875
|
-
if (!this._renderer)
|
|
876
|
-
console.warn('WARNING:
|
|
877
|
-
|
|
878
|
-
const material =
|
|
654
|
+
if (!this._renderer) {
|
|
655
|
+
console.warn('WARNING: A Renderer was not passed to this Loader constructor or in setRenderer, the result of this Loader will need to be converted to a Data Texture with toDataTexture() before you can use it in your renderer.');
|
|
656
|
+
}
|
|
657
|
+
const material = this._config.createMaterial({
|
|
879
658
|
gainMapMax: [1, 1, 1],
|
|
880
659
|
gainMapMin: [0, 0, 0],
|
|
881
660
|
gamma: [1, 1, 1],
|
|
@@ -887,7 +666,7 @@ void main() {
|
|
|
887
666
|
gainMap: new three.Texture(),
|
|
888
667
|
sdr: new three.Texture()
|
|
889
668
|
});
|
|
890
|
-
return
|
|
669
|
+
return this._config.createQuadRenderer({
|
|
891
670
|
width: 16,
|
|
892
671
|
height: 16,
|
|
893
672
|
type: three.HalfFloatType,
|
|
@@ -897,15 +676,7 @@ void main() {
|
|
|
897
676
|
renderTargetOptions: this._renderTargetOptions
|
|
898
677
|
});
|
|
899
678
|
}
|
|
900
|
-
|
|
901
|
-
* @private
|
|
902
|
-
* @param quadRenderer
|
|
903
|
-
* @param metadata
|
|
904
|
-
* @param sdrBuffer
|
|
905
|
-
* @param gainMapBuffer
|
|
906
|
-
*/
|
|
907
|
-
async render(quadRenderer, metadata, sdrBuffer, gainMapBuffer) {
|
|
908
|
-
// this is optional, will render a black gain-map if not present
|
|
679
|
+
async processImages(sdrBuffer, gainMapBuffer, imageOrientation) {
|
|
909
680
|
const gainMapBlob = gainMapBuffer ? new Blob([gainMapBuffer], { type: 'image/jpeg' }) : undefined;
|
|
910
681
|
const sdrBlob = new Blob([sdrBuffer], { type: 'image/jpeg' });
|
|
911
682
|
let sdrImage;
|
|
@@ -918,22 +689,28 @@ void main() {
|
|
|
918
689
|
]);
|
|
919
690
|
gainMapImage = res[0];
|
|
920
691
|
sdrImage = res[1];
|
|
921
|
-
needsFlip =
|
|
692
|
+
needsFlip = imageOrientation === 'flipY';
|
|
922
693
|
}
|
|
923
694
|
else {
|
|
924
695
|
const res = await Promise.all([
|
|
925
|
-
gainMapBlob ? createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }) : Promise.resolve(undefined),
|
|
926
|
-
createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
|
|
696
|
+
gainMapBlob ? createImageBitmap(gainMapBlob, { imageOrientation: imageOrientation || 'flipY' }) : Promise.resolve(undefined),
|
|
697
|
+
createImageBitmap(sdrBlob, { imageOrientation: imageOrientation || 'flipY' })
|
|
927
698
|
]);
|
|
928
699
|
gainMapImage = res[0];
|
|
929
700
|
sdrImage = res[1];
|
|
930
701
|
}
|
|
702
|
+
return { sdrImage, gainMapImage, needsFlip };
|
|
703
|
+
}
|
|
704
|
+
createTextures(sdrImage, gainMapImage, needsFlip) {
|
|
931
705
|
const gainMap = new three.Texture(gainMapImage || new ImageData(2, 2), three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.LinearSRGBColorSpace);
|
|
932
706
|
gainMap.flipY = needsFlip;
|
|
933
707
|
gainMap.needsUpdate = true;
|
|
934
708
|
const sdr = new three.Texture(sdrImage, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.SRGBColorSpace);
|
|
935
709
|
sdr.flipY = needsFlip;
|
|
936
710
|
sdr.needsUpdate = true;
|
|
711
|
+
return { gainMap, sdr };
|
|
712
|
+
}
|
|
713
|
+
updateQuadRenderer(quadRenderer, sdrImage, gainMap, sdr, metadata) {
|
|
937
714
|
quadRenderer.width = sdrImage.width;
|
|
938
715
|
quadRenderer.height = sdrImage.height;
|
|
939
716
|
quadRenderer.material.gainMap = gainMap;
|
|
@@ -947,6 +724,257 @@ void main() {
|
|
|
947
724
|
quadRenderer.material.hdrCapacityMax = metadata.hdrCapacityMax;
|
|
948
725
|
quadRenderer.material.maxDisplayBoost = Math.pow(2, metadata.hdrCapacityMax);
|
|
949
726
|
quadRenderer.material.needsUpdate = true;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
const vertexShader = /* glsl */ `
|
|
731
|
+
varying vec2 vUv;
|
|
732
|
+
|
|
733
|
+
void main() {
|
|
734
|
+
vUv = uv;
|
|
735
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
736
|
+
}
|
|
737
|
+
`;
|
|
738
|
+
const fragmentShader = /* glsl */ `
|
|
739
|
+
// min half float value
|
|
740
|
+
#define HALF_FLOAT_MIN vec3( -65504, -65504, -65504 )
|
|
741
|
+
// max half float value
|
|
742
|
+
#define HALF_FLOAT_MAX vec3( 65504, 65504, 65504 )
|
|
743
|
+
|
|
744
|
+
uniform sampler2D sdr;
|
|
745
|
+
uniform sampler2D gainMap;
|
|
746
|
+
uniform vec3 gamma;
|
|
747
|
+
uniform vec3 offsetHdr;
|
|
748
|
+
uniform vec3 offsetSdr;
|
|
749
|
+
uniform vec3 gainMapMin;
|
|
750
|
+
uniform vec3 gainMapMax;
|
|
751
|
+
uniform float weightFactor;
|
|
752
|
+
|
|
753
|
+
varying vec2 vUv;
|
|
754
|
+
|
|
755
|
+
void main() {
|
|
756
|
+
vec3 rgb = texture2D( sdr, vUv ).rgb;
|
|
757
|
+
vec3 recovery = texture2D( gainMap, vUv ).rgb;
|
|
758
|
+
vec3 logRecovery = pow( recovery, gamma );
|
|
759
|
+
vec3 logBoost = gainMapMin * ( 1.0 - logRecovery ) + gainMapMax * logRecovery;
|
|
760
|
+
vec3 hdrColor = (rgb + offsetSdr) * exp2( logBoost * weightFactor ) - offsetHdr;
|
|
761
|
+
vec3 clampedHdrColor = max( HALF_FLOAT_MIN, min( HALF_FLOAT_MAX, hdrColor ));
|
|
762
|
+
gl_FragColor = vec4( clampedHdrColor , 1.0 );
|
|
763
|
+
}
|
|
764
|
+
`;
|
|
765
|
+
/**
|
|
766
|
+
* A Material which is able to decode the Gainmap into a full HDR Representation
|
|
767
|
+
*
|
|
768
|
+
* @category Materials
|
|
769
|
+
* @group Materials
|
|
770
|
+
*/
|
|
771
|
+
class GainMapDecoderMaterial extends three.ShaderMaterial {
|
|
772
|
+
_maxDisplayBoost;
|
|
773
|
+
_hdrCapacityMin;
|
|
774
|
+
_hdrCapacityMax;
|
|
775
|
+
/**
|
|
776
|
+
*
|
|
777
|
+
* @param params
|
|
778
|
+
*/
|
|
779
|
+
constructor({ gamma, offsetHdr, offsetSdr, gainMapMin, gainMapMax, maxDisplayBoost, hdrCapacityMin, hdrCapacityMax, sdr, gainMap }) {
|
|
780
|
+
super({
|
|
781
|
+
name: 'GainMapDecoderMaterial',
|
|
782
|
+
vertexShader,
|
|
783
|
+
fragmentShader,
|
|
784
|
+
uniforms: {
|
|
785
|
+
sdr: { value: sdr },
|
|
786
|
+
gainMap: { value: gainMap },
|
|
787
|
+
gamma: { value: new three.Vector3(1.0 / gamma[0], 1.0 / gamma[1], 1.0 / gamma[2]) },
|
|
788
|
+
offsetHdr: { value: new three.Vector3().fromArray(offsetHdr) },
|
|
789
|
+
offsetSdr: { value: new three.Vector3().fromArray(offsetSdr) },
|
|
790
|
+
gainMapMin: { value: new three.Vector3().fromArray(gainMapMin) },
|
|
791
|
+
gainMapMax: { value: new three.Vector3().fromArray(gainMapMax) },
|
|
792
|
+
weightFactor: {
|
|
793
|
+
value: (Math.log2(maxDisplayBoost) - hdrCapacityMin) / (hdrCapacityMax - hdrCapacityMin)
|
|
794
|
+
}
|
|
795
|
+
},
|
|
796
|
+
blending: three.NoBlending,
|
|
797
|
+
depthTest: false,
|
|
798
|
+
depthWrite: false
|
|
799
|
+
});
|
|
800
|
+
this._maxDisplayBoost = maxDisplayBoost;
|
|
801
|
+
this._hdrCapacityMin = hdrCapacityMin;
|
|
802
|
+
this._hdrCapacityMax = hdrCapacityMax;
|
|
803
|
+
this.needsUpdate = true;
|
|
804
|
+
this.uniformsNeedUpdate = true;
|
|
805
|
+
}
|
|
806
|
+
get sdr() { return this.uniforms.sdr.value; }
|
|
807
|
+
set sdr(value) { this.uniforms.sdr.value = value; }
|
|
808
|
+
get gainMap() { return this.uniforms.gainMap.value; }
|
|
809
|
+
set gainMap(value) { this.uniforms.gainMap.value = value; }
|
|
810
|
+
/**
|
|
811
|
+
* @see {@link GainMapMetadata.offsetHdr}
|
|
812
|
+
*/
|
|
813
|
+
get offsetHdr() { return this.uniforms.offsetHdr.value.toArray(); }
|
|
814
|
+
set offsetHdr(value) { this.uniforms.offsetHdr.value.fromArray(value); }
|
|
815
|
+
/**
|
|
816
|
+
* @see {@link GainMapMetadata.offsetSdr}
|
|
817
|
+
*/
|
|
818
|
+
get offsetSdr() { return this.uniforms.offsetSdr.value.toArray(); }
|
|
819
|
+
set offsetSdr(value) { this.uniforms.offsetSdr.value.fromArray(value); }
|
|
820
|
+
/**
|
|
821
|
+
* @see {@link GainMapMetadata.gainMapMin}
|
|
822
|
+
*/
|
|
823
|
+
get gainMapMin() { return this.uniforms.gainMapMin.value.toArray(); }
|
|
824
|
+
set gainMapMin(value) { this.uniforms.gainMapMin.value.fromArray(value); }
|
|
825
|
+
/**
|
|
826
|
+
* @see {@link GainMapMetadata.gainMapMax}
|
|
827
|
+
*/
|
|
828
|
+
get gainMapMax() { return this.uniforms.gainMapMax.value.toArray(); }
|
|
829
|
+
set gainMapMax(value) { this.uniforms.gainMapMax.value.fromArray(value); }
|
|
830
|
+
/**
|
|
831
|
+
* @see {@link GainMapMetadata.gamma}
|
|
832
|
+
*/
|
|
833
|
+
get gamma() {
|
|
834
|
+
const g = this.uniforms.gamma.value;
|
|
835
|
+
return [1 / g.x, 1 / g.y, 1 / g.z];
|
|
836
|
+
}
|
|
837
|
+
set gamma(value) {
|
|
838
|
+
const g = this.uniforms.gamma.value;
|
|
839
|
+
g.x = 1.0 / value[0];
|
|
840
|
+
g.y = 1.0 / value[1];
|
|
841
|
+
g.z = 1.0 / value[2];
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* @see {@link GainMapMetadata.hdrCapacityMin}
|
|
845
|
+
* @remarks Logarithmic space
|
|
846
|
+
*/
|
|
847
|
+
get hdrCapacityMin() { return this._hdrCapacityMin; }
|
|
848
|
+
set hdrCapacityMin(value) {
|
|
849
|
+
this._hdrCapacityMin = value;
|
|
850
|
+
this.calculateWeight();
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* @see {@link GainMapMetadata.hdrCapacityMin}
|
|
854
|
+
* @remarks Logarithmic space
|
|
855
|
+
*/
|
|
856
|
+
get hdrCapacityMax() { return this._hdrCapacityMax; }
|
|
857
|
+
set hdrCapacityMax(value) {
|
|
858
|
+
this._hdrCapacityMax = value;
|
|
859
|
+
this.calculateWeight();
|
|
860
|
+
}
|
|
861
|
+
/**
|
|
862
|
+
* @see {@link GainmapDecodingParameters.maxDisplayBoost}
|
|
863
|
+
* @remarks Non Logarithmic space
|
|
864
|
+
*/
|
|
865
|
+
get maxDisplayBoost() { return this._maxDisplayBoost; }
|
|
866
|
+
set maxDisplayBoost(value) {
|
|
867
|
+
this._maxDisplayBoost = Math.max(1, Math.min(65504, value));
|
|
868
|
+
this.calculateWeight();
|
|
869
|
+
}
|
|
870
|
+
calculateWeight() {
|
|
871
|
+
const val = (Math.log2(this._maxDisplayBoost) - this._hdrCapacityMin) / (this._hdrCapacityMax - this._hdrCapacityMin);
|
|
872
|
+
this.uniforms.weightFactor.value = Math.max(0, Math.min(1, val));
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
const decodeImpl = createDecodeFunction({
|
|
877
|
+
renderer: three.WebGLRenderer,
|
|
878
|
+
createMaterial: (params) => new GainMapDecoderMaterial(params),
|
|
879
|
+
createQuadRenderer: (params) => new QuadRenderer(params)
|
|
880
|
+
});
|
|
881
|
+
/**
|
|
882
|
+
* Decodes a gain map using a WebGL RenderTarget
|
|
883
|
+
*
|
|
884
|
+
* @category Decoding Functions
|
|
885
|
+
* @group Decoding Functions
|
|
886
|
+
* @example
|
|
887
|
+
* import { decode } from '@monogrid/gainmap-js'
|
|
888
|
+
* import {
|
|
889
|
+
* Mesh,
|
|
890
|
+
* MeshBasicMaterial,
|
|
891
|
+
* PerspectiveCamera,
|
|
892
|
+
* PlaneGeometry,
|
|
893
|
+
* Scene,
|
|
894
|
+
* TextureLoader,
|
|
895
|
+
* WebGLRenderer
|
|
896
|
+
* } from 'three'
|
|
897
|
+
*
|
|
898
|
+
* const renderer = new WebGLRenderer()
|
|
899
|
+
*
|
|
900
|
+
* const textureLoader = new TextureLoader()
|
|
901
|
+
*
|
|
902
|
+
* // load SDR Representation
|
|
903
|
+
* const sdr = await textureLoader.loadAsync('sdr.jpg')
|
|
904
|
+
* // load Gain map recovery image
|
|
905
|
+
* const gainMap = await textureLoader.loadAsync('gainmap.jpg')
|
|
906
|
+
* // load metadata
|
|
907
|
+
* const metadata = await (await fetch('metadata.json')).json()
|
|
908
|
+
*
|
|
909
|
+
* const result = decode({
|
|
910
|
+
* sdr,
|
|
911
|
+
* gainMap,
|
|
912
|
+
* // this allows to use `result.renderTarget.texture` directly
|
|
913
|
+
* renderer,
|
|
914
|
+
* // this will restore the full HDR range
|
|
915
|
+
* maxDisplayBoost: Math.pow(2, metadata.hdrCapacityMax),
|
|
916
|
+
* ...metadata
|
|
917
|
+
* })
|
|
918
|
+
*
|
|
919
|
+
* const scene = new Scene()
|
|
920
|
+
* // `result` can be used to populate a Texture
|
|
921
|
+
* const mesh = new Mesh(
|
|
922
|
+
* new PlaneGeometry(),
|
|
923
|
+
* new MeshBasicMaterial({ map: result.renderTarget.texture })
|
|
924
|
+
* )
|
|
925
|
+
* scene.add(mesh)
|
|
926
|
+
* renderer.render(scene, new PerspectiveCamera())
|
|
927
|
+
*
|
|
928
|
+
* // result must be manually disposed
|
|
929
|
+
* // when you are done using it
|
|
930
|
+
* result.dispose()
|
|
931
|
+
*
|
|
932
|
+
* @param params
|
|
933
|
+
* @returns
|
|
934
|
+
* @throws {Error} if the WebGLRenderer fails to render the gain map
|
|
935
|
+
*/
|
|
936
|
+
const decode = (params) => {
|
|
937
|
+
// Ensure renderer is defined for the base function
|
|
938
|
+
if (!params.renderer) {
|
|
939
|
+
throw new Error('Renderer is required for decode function');
|
|
940
|
+
}
|
|
941
|
+
const quadRenderer = decodeImpl({
|
|
942
|
+
...params,
|
|
943
|
+
renderer: params.renderer
|
|
944
|
+
});
|
|
945
|
+
try {
|
|
946
|
+
quadRenderer.render();
|
|
947
|
+
}
|
|
948
|
+
catch (e) {
|
|
949
|
+
quadRenderer.disposeOnDemandRenderer();
|
|
950
|
+
throw e;
|
|
951
|
+
}
|
|
952
|
+
return quadRenderer;
|
|
953
|
+
};
|
|
954
|
+
|
|
955
|
+
/**
|
|
956
|
+
* Base class for WebGL loaders
|
|
957
|
+
* @template TUrl - The type of URL used to load resources
|
|
958
|
+
*/
|
|
959
|
+
class LoaderBaseWebGL extends LoaderBaseShared {
|
|
960
|
+
constructor(renderer, manager) {
|
|
961
|
+
super({
|
|
962
|
+
renderer,
|
|
963
|
+
createMaterial: (params) => new GainMapDecoderMaterial(params),
|
|
964
|
+
createQuadRenderer: (params) => new QuadRenderer(params)
|
|
965
|
+
}, manager);
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* @private
|
|
969
|
+
* @param quadRenderer
|
|
970
|
+
* @param metadata
|
|
971
|
+
* @param sdrBuffer
|
|
972
|
+
* @param gainMapBuffer
|
|
973
|
+
*/
|
|
974
|
+
async render(quadRenderer, metadata, sdrBuffer, gainMapBuffer) {
|
|
975
|
+
const { sdrImage, gainMapImage, needsFlip } = await this.processImages(sdrBuffer, gainMapBuffer, 'flipY');
|
|
976
|
+
const { gainMap, sdr } = this.createTextures(sdrImage, gainMapImage, needsFlip);
|
|
977
|
+
this.updateQuadRenderer(quadRenderer, sdrImage, gainMap, sdr, metadata);
|
|
950
978
|
quadRenderer.render();
|
|
951
979
|
}
|
|
952
980
|
}
|
|
@@ -961,7 +989,6 @@ void main() {
|
|
|
961
989
|
* import { GainMapLoader } from '@monogrid/gainmap-js'
|
|
962
990
|
* import {
|
|
963
991
|
* EquirectangularReflectionMapping,
|
|
964
|
-
* LinearFilter,
|
|
965
992
|
* Mesh,
|
|
966
993
|
* MeshBasicMaterial,
|
|
967
994
|
* PerspectiveCamera,
|
|
@@ -973,6 +1000,7 @@ void main() {
|
|
|
973
1000
|
* const renderer = new WebGLRenderer()
|
|
974
1001
|
*
|
|
975
1002
|
* const loader = new GainMapLoader(renderer)
|
|
1003
|
+
* .setRenderTargetOptions({ mapping: EquirectangularReflectionMapping })
|
|
976
1004
|
*
|
|
977
1005
|
* const result = await loader.loadAsync(['sdr.jpeg', 'gainmap.jpeg', 'metadata.json'])
|
|
978
1006
|
* // `result` can be used to populate a Texture
|
|
@@ -992,14 +1020,13 @@ void main() {
|
|
|
992
1020
|
* // it was previously needed to convert it
|
|
993
1021
|
* // to a DataTexture with `result.toDataTexture()`
|
|
994
1022
|
* scene.background = result.renderTarget.texture
|
|
995
|
-
* scene.background.mapping = EquirectangularReflectionMapping
|
|
996
1023
|
*
|
|
997
1024
|
* // result must be manually disposed
|
|
998
1025
|
* // when you are done using it
|
|
999
1026
|
* result.dispose()
|
|
1000
1027
|
*
|
|
1001
1028
|
*/
|
|
1002
|
-
class GainMapLoader extends
|
|
1029
|
+
class GainMapLoader extends LoaderBaseWebGL {
|
|
1003
1030
|
/**
|
|
1004
1031
|
* Loads a gainmap using separate data
|
|
1005
1032
|
* * sdr image
|
|
@@ -1010,7 +1037,7 @@ void main() {
|
|
|
1010
1037
|
*
|
|
1011
1038
|
* @param urls An array in the form of [sdr.jpg, gainmap.jpg, metadata.json]
|
|
1012
1039
|
* @param onLoad Load complete callback, will receive the result
|
|
1013
|
-
* @param onProgress Progress callback, will receive a
|
|
1040
|
+
* @param onProgress Progress callback, will receive a `ProgressEvent`
|
|
1014
1041
|
* @param onError Error callback
|
|
1015
1042
|
* @returns
|
|
1016
1043
|
*/
|
|
@@ -1147,7 +1174,6 @@ void main() {
|
|
|
1147
1174
|
* import { HDRJPGLoader } from '@monogrid/gainmap-js'
|
|
1148
1175
|
* import {
|
|
1149
1176
|
* EquirectangularReflectionMapping,
|
|
1150
|
-
* LinearFilter,
|
|
1151
1177
|
* Mesh,
|
|
1152
1178
|
* MeshBasicMaterial,
|
|
1153
1179
|
* PerspectiveCamera,
|
|
@@ -1159,6 +1185,7 @@ void main() {
|
|
|
1159
1185
|
* const renderer = new WebGLRenderer()
|
|
1160
1186
|
*
|
|
1161
1187
|
* const loader = new HDRJPGLoader(renderer)
|
|
1188
|
+
* .setRenderTargetOptions({ mapping: EquirectangularReflectionMapping })
|
|
1162
1189
|
*
|
|
1163
1190
|
* const result = await loader.loadAsync('gainmap.jpeg')
|
|
1164
1191
|
* // `result` can be used to populate a Texture
|
|
@@ -1178,21 +1205,20 @@ void main() {
|
|
|
1178
1205
|
* // it was previously needed to convert it
|
|
1179
1206
|
* // to a DataTexture with `result.toDataTexture()`
|
|
1180
1207
|
* scene.background = result.renderTarget.texture
|
|
1181
|
-
* scene.background.mapping = EquirectangularReflectionMapping
|
|
1182
1208
|
*
|
|
1183
1209
|
* // result must be manually disposed
|
|
1184
1210
|
* // when you are done using it
|
|
1185
1211
|
* result.dispose()
|
|
1186
1212
|
*
|
|
1187
1213
|
*/
|
|
1188
|
-
class HDRJPGLoader extends
|
|
1214
|
+
class HDRJPGLoader extends LoaderBaseWebGL {
|
|
1189
1215
|
/**
|
|
1190
1216
|
* Loads a JPEG containing gain map metadata
|
|
1191
1217
|
* Renders a normal SDR image if gainmap data is not found
|
|
1192
1218
|
*
|
|
1193
|
-
* @param url
|
|
1219
|
+
* @param url Path to a JPEG file containing embedded gain map metadata
|
|
1194
1220
|
* @param onLoad Load complete callback, will receive the result
|
|
1195
|
-
* @param onProgress Progress callback, will receive a
|
|
1221
|
+
* @param onProgress Progress callback, will receive a `ProgressEvent`
|
|
1196
1222
|
* @param onError Error callback
|
|
1197
1223
|
* @returns
|
|
1198
1224
|
*/
|
|
@@ -1242,7 +1268,7 @@ void main() {
|
|
|
1242
1268
|
}
|
|
1243
1269
|
// solves #16
|
|
1244
1270
|
try {
|
|
1245
|
-
await this.render(quadRenderer, metadata, sdrJPEG, gainMapJPEG);
|
|
1271
|
+
await this.render(quadRenderer, metadata, sdrJPEG.buffer, gainMapJPEG?.buffer);
|
|
1246
1272
|
}
|
|
1247
1273
|
catch (error) {
|
|
1248
1274
|
this.manager.itemError(url);
|
|
@@ -1266,10 +1292,14 @@ void main() {
|
|
|
1266
1292
|
|
|
1267
1293
|
exports.GainMapDecoderMaterial = GainMapDecoderMaterial;
|
|
1268
1294
|
exports.GainMapLoader = GainMapLoader;
|
|
1295
|
+
exports.GainMapNotFoundError = GainMapNotFoundError;
|
|
1269
1296
|
exports.HDRJPGLoader = HDRJPGLoader;
|
|
1270
1297
|
exports.JPEGRLoader = HDRJPGLoader;
|
|
1298
|
+
exports.LoaderBaseShared = LoaderBaseShared;
|
|
1271
1299
|
exports.MPFExtractor = MPFExtractor;
|
|
1272
1300
|
exports.QuadRenderer = QuadRenderer;
|
|
1301
|
+
exports.XMPMetadataNotFoundError = XMPMetadataNotFoundError;
|
|
1302
|
+
exports.createDecodeFunction = createDecodeFunction;
|
|
1273
1303
|
exports.decode = decode;
|
|
1274
1304
|
exports.extractGainmapFromJPEG = extractGainmapFromJPEG;
|
|
1275
1305
|
exports.extractXMP = extractXMP;
|