@monogrid/gainmap-js 3.1.0 → 3.3.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.
Files changed (87) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/CODE_OF_CONDUCT.md +1 -1
  3. package/CONTRIBUTING.md +1 -1
  4. package/README.md +113 -57
  5. package/dist/Loader-CsjVTcg5.js +424 -0
  6. package/dist/Loader-D4XqFES7.js +5 -0
  7. package/dist/{QuadRenderer-DuOPRGA4.js → QuadRenderer-BqJ2Q8P5.js} +39 -30
  8. package/dist/QuadRenderer-De5PMMtY.js +5 -0
  9. package/dist/{_commonjsHelpers-DeBGiDfT.js → _commonjsHelpers-C2Rq7Bzs.js} +2 -2
  10. package/dist/_commonjsHelpers-FnzxO7yL.js +5 -0
  11. package/dist/compress-HAw5HoXG.js +5 -0
  12. package/dist/{compress-B6OswAu8.js → compress-bMJEJ5JM.js} +4 -3
  13. package/dist/core/QuadRenderer.d.ts +2 -2
  14. package/dist/core/get-data-texture.d.ts +4 -3
  15. package/dist/core/types.d.ts +7 -1
  16. package/dist/decode/index.d.ts +1 -11
  17. package/dist/decode/shared/Loader.d.ts +59 -0
  18. package/dist/decode/shared/decode.d.ts +24 -0
  19. package/dist/decode/{extract.d.ts → shared/extract.d.ts} +4 -4
  20. package/dist/decode/shared/index.d.ts +8 -0
  21. package/dist/decode/shared/types.d.ts +53 -0
  22. package/dist/decode/{utils → shared/utils}/MPFExtractor.d.ts +1 -1
  23. package/dist/decode/{utils → shared/utils}/extractXMP.d.ts +1 -1
  24. package/dist/decode/{decode.d.ts → webgl/decode.d.ts} +6 -6
  25. package/dist/decode/webgl/index.d.ts +8 -0
  26. package/dist/decode/{loaders → webgl/loaders}/GainMapLoader.d.ts +5 -6
  27. package/dist/decode/{loaders → webgl/loaders}/HDRJPGLoader.d.ts +6 -7
  28. package/dist/decode/webgl/loaders/LoaderBaseWebGL.d.ts +20 -0
  29. package/dist/decode/{materials → webgl/materials}/GainMapDecoderMaterial.d.ts +2 -2
  30. package/dist/decode/webgpu/core/QuadRenderer.d.ts +153 -0
  31. package/dist/decode/webgpu/decode.d.ts +60 -0
  32. package/dist/decode/webgpu/index.d.ts +8 -0
  33. package/dist/decode/webgpu/loaders/GainMapLoader.d.ts +69 -0
  34. package/dist/decode/webgpu/loaders/HDRJPGLoader.d.ts +65 -0
  35. package/dist/decode/webgpu/loaders/LoaderBaseWebGPU.d.ts +20 -0
  36. package/dist/decode/webgpu/materials/GainMapDecoderMaterial.d.ts +78 -0
  37. package/dist/decode.js +45 -426
  38. package/dist/decode.min.js +5 -0
  39. package/dist/decode.umd.cjs +360 -314
  40. package/dist/decode.umd.min.cjs +5 -0
  41. package/dist/decode.webgpu.js +814 -0
  42. package/dist/decode.webgpu.min.js +5 -0
  43. package/dist/decode.webgpu.umd.cjs +1244 -0
  44. package/dist/decode.webgpu.umd.min.cjs +5 -0
  45. package/dist/encode/encode-and-compress.d.ts +4 -4
  46. package/dist/encode/encode.d.ts +3 -3
  47. package/dist/encode/find-texture-min-max.d.ts +4 -3
  48. package/dist/encode/types.d.ts +6 -5
  49. package/dist/encode.js +33 -12
  50. package/dist/encode.min.js +5 -0
  51. package/dist/encode.umd.cjs +70 -39
  52. package/dist/encode.umd.min.cjs +5 -0
  53. package/dist/libultrahdr/encode-jpeg-metadata.d.ts +6 -6
  54. package/dist/libultrahdr/jpeg-assembler.d.ts +41 -0
  55. package/dist/libultrahdr/jpeg-markers.d.ts +47 -0
  56. package/dist/libultrahdr/mpf-generator.d.ts +20 -0
  57. package/dist/libultrahdr/xmp-generator.d.ts +33 -0
  58. package/dist/libultrahdr.d.ts +0 -3
  59. package/dist/libultrahdr.js +548 -6598
  60. package/dist/libultrahdr.min.js +5 -0
  61. package/dist/libultrahdr.umd.cjs +633 -6686
  62. package/dist/libultrahdr.umd.min.cjs +5 -0
  63. package/dist/third-party.txt +301 -0
  64. package/dist/worker-interface.js +74 -64
  65. package/dist/worker-interface.min.js +5 -0
  66. package/dist/worker-interface.umd.cjs +73 -63
  67. package/dist/worker-interface.umd.min.cjs +5 -0
  68. package/dist/worker.js +103 -88
  69. package/dist/worker.min.js +5 -0
  70. package/dist/worker.umd.cjs +105 -89
  71. package/dist/worker.umd.min.cjs +5 -0
  72. package/package.json +79 -72
  73. package/dist/decode/loaders/LoaderBase.d.ts +0 -45
  74. package/dist/decode/types.d.ts +0 -42
  75. package/dist/libultrahdr/decode-jpeg-metadata.d.ts +0 -28
  76. package/dist/libultrahdr/library.d.ts +0 -9
  77. package/dist/libultrahdr-esm.wasm +0 -0
  78. package/libultrahdr-wasm/build/libultrahdr-debug.js +0 -7156
  79. package/libultrahdr-wasm/build/libultrahdr-debug.wasm +0 -0
  80. package/libultrahdr-wasm/build/libultrahdr-esm.js +0 -6633
  81. package/libultrahdr-wasm/build/libultrahdr-esm.wasm +0 -0
  82. package/libultrahdr-wasm/build/libultrahdr.d.ts +0 -30
  83. package/libultrahdr-wasm/build/libultrahdr.js +0 -6591
  84. package/libultrahdr-wasm/build/libultrahdr.wasm +0 -0
  85. /package/dist/decode/{errors → shared/errors}/GainMapNotFoundError.d.ts +0 -0
  86. /package/dist/decode/{errors → shared/errors}/XMPMetadataNotFoundError.d.ts +0 -0
  87. /package/dist/decode/{utils → shared/utils}/get-html-image-from-blob.d.ts +0 -0
@@ -1,6 +1,6 @@
1
1
  /**
2
- * @monogrid/gainmap-js v3.1.0
3
- * With ❤️, by MONOGRID <rnd@monogrid.com>
2
+ * @monogrid/gainmap-js v3.3.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: ((_a = options.renderTargetOptions) === null || _a === void 0 ? void 0 : _a.anisotropy) !== undefined ? (_b = options.renderTargetOptions) === null || _b === void 0 ? void 0 : _b.anisotropy : 1,
113
- generateMipmaps: ((_c = options.renderTargetOptions) === null || _c === void 0 ? void 0 : _c.generateMipmaps) !== undefined ? (_d = options.renderTargetOptions) === null || _d === void 0 ? void 0 : _d.generateMipmaps : false,
114
- magFilter: ((_e = options.renderTargetOptions) === null || _e === void 0 ? void 0 : _e.magFilter) !== undefined ? (_f = options.renderTargetOptions) === null || _f === void 0 ? void 0 : _f.magFilter : three.LinearFilter,
115
- minFilter: ((_g = options.renderTargetOptions) === null || _g === void 0 ? void 0 : _g.minFilter) !== undefined ? (_h = options.renderTargetOptions) === null || _h === void 0 ? void 0 : _h.minFilter : three.LinearFilter,
116
- samples: ((_j = options.renderTargetOptions) === null || _j === void 0 ? void 0 : _j.samples) !== undefined ? (_k = options.renderTargetOptions) === null || _k === void 0 ? void 0 : _k.samples : undefined,
117
- wrapS: ((_l = options.renderTargetOptions) === null || _l === void 0 ? void 0 : _l.wrapS) !== undefined ? (_m = options.renderTargetOptions) === null || _m === void 0 ? void 0 : _m.wrapS : three.ClampToEdgeWrapping,
118
- wrapT: ((_o = options.renderTargetOptions) === null || _o === void 0 ? void 0 : _o.wrapT) !== undefined ? (_p = options.renderTargetOptions) === null || _p === void 0 ? void 0 : _p.wrapT : three.ClampToEdgeWrapping
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 = ((_q = options.renderTargetOptions) === null || _q === void 0 ? void 0 : _q.mapping) !== undefined ? (_r = options.renderTargetOptions) === null || _r === void 0 ? void 0 : _r.mapping : three.UVMapping;
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
- (options === null || options === void 0 ? void 0 : options.mapping) || three.UVMapping, (options === null || options === void 0 ? void 0 : options.wrapS) || three.ClampToEdgeWrapping, (options === null || options === void 0 ? void 0 : options.wrapT) || three.ClampToEdgeWrapping, (options === null || options === void 0 ? void 0 : options.magFilter) || three.LinearFilter, (options === null || options === void 0 ? void 0 : options.minFilter) || three.LinearFilter, (options === null || options === void 0 ? void 0 : options.anisotropy) || 1,
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 = (options === null || options === void 0 ? void 0 : options.generateMipmaps) !== undefined ? options === null || options === void 0 ? void 0 : options.generateMipmaps : false;
210
+ returnValue.generateMipmaps = options?.generateMipmaps !== undefined ? options?.generateMipmaps : false;
202
211
  return returnValue;
203
212
  }
204
213
  /**
@@ -302,244 +311,41 @@
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
- * A Material which is able to decode the Gainmap into a full HDR Representation
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
- class GainMapDecoderMaterial extends three.ShaderMaterial {
347
- /**
348
- *
349
- * @param params
350
- */
351
- constructor({ gamma, offsetHdr, offsetSdr, gainMapMin, gainMapMax, maxDisplayBoost, hdrCapacityMin, hdrCapacityMax, sdr, gainMap }) {
352
- super({
353
- name: 'GainMapDecoderMaterial',
354
- vertexShader,
355
- fragmentShader,
356
- uniforms: {
357
- sdr: { value: sdr },
358
- gainMap: { value: gainMap },
359
- gamma: { value: new three.Vector3(1.0 / gamma[0], 1.0 / gamma[1], 1.0 / gamma[2]) },
360
- offsetHdr: { value: new three.Vector3().fromArray(offsetHdr) },
361
- offsetSdr: { value: new three.Vector3().fromArray(offsetSdr) },
362
- gainMapMin: { value: new three.Vector3().fromArray(gainMapMin) },
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
- this._maxDisplayBoost = maxDisplayBoost;
373
- this._hdrCapacityMin = hdrCapacityMin;
374
- this._hdrCapacityMax = hdrCapacityMax;
375
- this.needsUpdate = true;
376
- this.uniformsNeedUpdate = true;
377
- }
378
- get sdr() { return this.uniforms.sdr.value; }
379
- set sdr(value) { this.uniforms.sdr.value = value; }
380
- get gainMap() { return this.uniforms.gainMap.value; }
381
- set gainMap(value) { this.uniforms.gainMap.value = value; }
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
 
@@ -618,6 +424,7 @@ void main() {
618
424
  * Kudos to: Phil Harvey (exiftool), Jaume Sanchez (android-lens-blur-depth-extractor)
619
425
  */
620
426
  class MPFExtractor {
427
+ options;
621
428
  constructor(options) {
622
429
  this.options = {
623
430
  debug: options && options.debug !== undefined ? options.debug : false,
@@ -814,52 +621,40 @@ void main() {
814
621
  return new Promise((resolve, reject) => {
815
622
  const img = document.createElement('img');
816
623
  img.onload = () => { resolve(img); };
624
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
817
625
  img.onerror = (e) => { reject(e); };
818
626
  img.src = URL.createObjectURL(blob);
819
627
  });
820
628
  };
821
629
 
822
- class LoaderBase extends three.Loader {
823
- /**
824
- *
825
- * @param renderer
826
- * @param manager
827
- */
828
- constructor(renderer, manager) {
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) {
829
639
  super(manager);
830
- if (renderer)
831
- this._renderer = renderer;
640
+ this._config = config;
641
+ if (config.renderer)
642
+ this._renderer = config.renderer;
832
643
  this._internalLoadingManager = new three.LoadingManager();
833
644
  }
834
- /**
835
- * Specify the renderer to use when rendering the gain map
836
- *
837
- * @param renderer
838
- * @returns
839
- */
840
645
  setRenderer(renderer) {
841
646
  this._renderer = renderer;
842
647
  return this;
843
648
  }
844
- /**
845
- * Specify the renderTarget options to use when rendering the gain map
846
- *
847
- * @param options
848
- * @returns
849
- */
850
649
  setRenderTargetOptions(options) {
851
650
  this._renderTargetOptions = options;
852
651
  return this;
853
652
  }
854
- /**
855
- * @private
856
- * @returns
857
- */
858
653
  prepareQuadRenderer() {
859
- if (!this._renderer)
860
- console.warn('WARNING: An existing WebGL 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.');
861
- // temporary values
862
- const material = new GainMapDecoderMaterial({
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({
863
658
  gainMapMax: [1, 1, 1],
864
659
  gainMapMin: [0, 0, 0],
865
660
  gamma: [1, 1, 1],
@@ -871,7 +666,7 @@ void main() {
871
666
  gainMap: new three.Texture(),
872
667
  sdr: new three.Texture()
873
668
  });
874
- return new QuadRenderer({
669
+ return this._config.createQuadRenderer({
875
670
  width: 16,
876
671
  height: 16,
877
672
  type: three.HalfFloatType,
@@ -881,15 +676,7 @@ void main() {
881
676
  renderTargetOptions: this._renderTargetOptions
882
677
  });
883
678
  }
884
- /**
885
- * @private
886
- * @param quadRenderer
887
- * @param metadata
888
- * @param sdrBuffer
889
- * @param gainMapBuffer
890
- */
891
- async render(quadRenderer, metadata, sdrBuffer, gainMapBuffer) {
892
- // this is optional, will render a black gain-map if not present
679
+ async processImages(sdrBuffer, gainMapBuffer, imageOrientation) {
893
680
  const gainMapBlob = gainMapBuffer ? new Blob([gainMapBuffer], { type: 'image/jpeg' }) : undefined;
894
681
  const sdrBlob = new Blob([sdrBuffer], { type: 'image/jpeg' });
895
682
  let sdrImage;
@@ -902,22 +689,28 @@ void main() {
902
689
  ]);
903
690
  gainMapImage = res[0];
904
691
  sdrImage = res[1];
905
- needsFlip = true;
692
+ needsFlip = imageOrientation === 'flipY';
906
693
  }
907
694
  else {
908
695
  const res = await Promise.all([
909
- gainMapBlob ? createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }) : Promise.resolve(undefined),
910
- createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
696
+ gainMapBlob ? createImageBitmap(gainMapBlob, { imageOrientation: imageOrientation || 'flipY' }) : Promise.resolve(undefined),
697
+ createImageBitmap(sdrBlob, { imageOrientation: imageOrientation || 'flipY' })
911
698
  ]);
912
699
  gainMapImage = res[0];
913
700
  sdrImage = res[1];
914
701
  }
702
+ return { sdrImage, gainMapImage, needsFlip };
703
+ }
704
+ createTextures(sdrImage, gainMapImage, needsFlip) {
915
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);
916
706
  gainMap.flipY = needsFlip;
917
707
  gainMap.needsUpdate = true;
918
708
  const sdr = new three.Texture(sdrImage, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.SRGBColorSpace);
919
709
  sdr.flipY = needsFlip;
920
710
  sdr.needsUpdate = true;
711
+ return { gainMap, sdr };
712
+ }
713
+ updateQuadRenderer(quadRenderer, sdrImage, gainMap, sdr, metadata) {
921
714
  quadRenderer.width = sdrImage.width;
922
715
  quadRenderer.height = sdrImage.height;
923
716
  quadRenderer.material.gainMap = gainMap;
@@ -931,6 +724,257 @@ void main() {
931
724
  quadRenderer.material.hdrCapacityMax = metadata.hdrCapacityMax;
932
725
  quadRenderer.material.maxDisplayBoost = Math.pow(2, metadata.hdrCapacityMax);
933
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);
934
978
  quadRenderer.render();
935
979
  }
936
980
  }
@@ -945,7 +989,6 @@ void main() {
945
989
  * import { GainMapLoader } from '@monogrid/gainmap-js'
946
990
  * import {
947
991
  * EquirectangularReflectionMapping,
948
- * LinearFilter,
949
992
  * Mesh,
950
993
  * MeshBasicMaterial,
951
994
  * PerspectiveCamera,
@@ -957,6 +1000,7 @@ void main() {
957
1000
  * const renderer = new WebGLRenderer()
958
1001
  *
959
1002
  * const loader = new GainMapLoader(renderer)
1003
+ * .setRenderTargetOptions({ mapping: EquirectangularReflectionMapping })
960
1004
  *
961
1005
  * const result = await loader.loadAsync(['sdr.jpeg', 'gainmap.jpeg', 'metadata.json'])
962
1006
  * // `result` can be used to populate a Texture
@@ -976,14 +1020,13 @@ void main() {
976
1020
  * // it was previously needed to convert it
977
1021
  * // to a DataTexture with `result.toDataTexture()`
978
1022
  * scene.background = result.renderTarget.texture
979
- * scene.background.mapping = EquirectangularReflectionMapping
980
1023
  *
981
1024
  * // result must be manually disposed
982
1025
  * // when you are done using it
983
1026
  * result.dispose()
984
1027
  *
985
1028
  */
986
- class GainMapLoader extends LoaderBase {
1029
+ class GainMapLoader extends LoaderBaseWebGL {
987
1030
  /**
988
1031
  * Loads a gainmap using separate data
989
1032
  * * sdr image
@@ -994,7 +1037,7 @@ void main() {
994
1037
  *
995
1038
  * @param urls An array in the form of [sdr.jpg, gainmap.jpg, metadata.json]
996
1039
  * @param onLoad Load complete callback, will receive the result
997
- * @param onProgress Progress callback, will receive a {@link ProgressEvent}
1040
+ * @param onProgress Progress callback, will receive a `ProgressEvent`
998
1041
  * @param onError Error callback
999
1042
  * @returns
1000
1043
  */
@@ -1131,7 +1174,6 @@ void main() {
1131
1174
  * import { HDRJPGLoader } from '@monogrid/gainmap-js'
1132
1175
  * import {
1133
1176
  * EquirectangularReflectionMapping,
1134
- * LinearFilter,
1135
1177
  * Mesh,
1136
1178
  * MeshBasicMaterial,
1137
1179
  * PerspectiveCamera,
@@ -1143,6 +1185,7 @@ void main() {
1143
1185
  * const renderer = new WebGLRenderer()
1144
1186
  *
1145
1187
  * const loader = new HDRJPGLoader(renderer)
1188
+ * .setRenderTargetOptions({ mapping: EquirectangularReflectionMapping })
1146
1189
  *
1147
1190
  * const result = await loader.loadAsync('gainmap.jpeg')
1148
1191
  * // `result` can be used to populate a Texture
@@ -1162,21 +1205,20 @@ void main() {
1162
1205
  * // it was previously needed to convert it
1163
1206
  * // to a DataTexture with `result.toDataTexture()`
1164
1207
  * scene.background = result.renderTarget.texture
1165
- * scene.background.mapping = EquirectangularReflectionMapping
1166
1208
  *
1167
1209
  * // result must be manually disposed
1168
1210
  * // when you are done using it
1169
1211
  * result.dispose()
1170
1212
  *
1171
1213
  */
1172
- class HDRJPGLoader extends LoaderBase {
1214
+ class HDRJPGLoader extends LoaderBaseWebGL {
1173
1215
  /**
1174
1216
  * Loads a JPEG containing gain map metadata
1175
1217
  * Renders a normal SDR image if gainmap data is not found
1176
1218
  *
1177
- * @param url An array in the form of [sdr.jpg, gainmap.jpg, metadata.json]
1219
+ * @param url Path to a JPEG file containing embedded gain map metadata
1178
1220
  * @param onLoad Load complete callback, will receive the result
1179
- * @param onProgress Progress callback, will receive a {@link ProgressEvent}
1221
+ * @param onProgress Progress callback, will receive a `ProgressEvent`
1180
1222
  * @param onError Error callback
1181
1223
  * @returns
1182
1224
  */
@@ -1226,7 +1268,7 @@ void main() {
1226
1268
  }
1227
1269
  // solves #16
1228
1270
  try {
1229
- await this.render(quadRenderer, metadata, sdrJPEG, gainMapJPEG);
1271
+ await this.render(quadRenderer, metadata, sdrJPEG.buffer, gainMapJPEG?.buffer);
1230
1272
  }
1231
1273
  catch (error) {
1232
1274
  this.manager.itemError(url);
@@ -1250,10 +1292,14 @@ void main() {
1250
1292
 
1251
1293
  exports.GainMapDecoderMaterial = GainMapDecoderMaterial;
1252
1294
  exports.GainMapLoader = GainMapLoader;
1295
+ exports.GainMapNotFoundError = GainMapNotFoundError;
1253
1296
  exports.HDRJPGLoader = HDRJPGLoader;
1254
1297
  exports.JPEGRLoader = HDRJPGLoader;
1298
+ exports.LoaderBaseShared = LoaderBaseShared;
1255
1299
  exports.MPFExtractor = MPFExtractor;
1256
1300
  exports.QuadRenderer = QuadRenderer;
1301
+ exports.XMPMetadataNotFoundError = XMPMetadataNotFoundError;
1302
+ exports.createDecodeFunction = createDecodeFunction;
1257
1303
  exports.decode = decode;
1258
1304
  exports.extractGainmapFromJPEG = extractGainmapFromJPEG;
1259
1305
  exports.extractXMP = extractXMP;