@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.
Files changed (51) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +21 -8
  3. package/dist/QuadRenderer-IDZuHqZM.js +178 -0
  4. package/dist/{_commonjsHelpers-1789f0cf.js → _commonjsHelpers-G_2thcKl.js} +1 -1
  5. package/dist/{compress-a4b59cab.js → compress-Cw5IwRTb.js} +1 -1
  6. package/dist/{loaders → core}/LoaderBase.d.ts +3 -3
  7. package/dist/{utils → core}/QuadRenderer.d.ts +6 -0
  8. package/dist/core/types.d.ts +64 -0
  9. package/dist/decode/decode.d.ts +56 -0
  10. package/dist/decode/extract.d.ts +10 -0
  11. package/dist/decode/index.d.ts +8 -0
  12. package/dist/{loaders → decode/loaders}/GainMapLoader.d.ts +4 -4
  13. package/dist/{loaders → decode/loaders}/JPEGRLoader.d.ts +5 -5
  14. package/dist/{materials → decode/materials}/GainMapDecoderMaterial.d.ts +3 -2
  15. package/dist/decode/types.d.ts +38 -0
  16. package/dist/decode/utils/MPFExtractor.d.ts +22 -0
  17. package/dist/decode/utils/extract-metadata-from-jpeg.d.ts +7 -0
  18. package/dist/decode.d.ts +1 -56
  19. package/dist/decode.js +737 -0
  20. package/dist/decode.umd.js +917 -0
  21. package/dist/{encode-utils → encode}/compress.d.ts +1 -1
  22. package/dist/encode/encode-and-compress.d.ts +61 -0
  23. package/dist/encode/encode.d.ts +58 -0
  24. package/dist/{encode-utils → encode}/get-gainmap.d.ts +3 -3
  25. package/dist/{encode-utils → encode}/get-sdr-rendition.d.ts +4 -5
  26. package/dist/encode/index.d.ts +10 -0
  27. package/dist/encode/types.d.ts +172 -0
  28. package/dist/encode.d.ts +1 -117
  29. package/dist/{index.js → encode.js} +42 -230
  30. package/dist/encode.umd.js +972 -0
  31. package/dist/libultrahdr/decode-jpeg-metadata.d.ts +31 -0
  32. package/dist/libultrahdr/encode-jpeg-metadata.d.ts +84 -0
  33. package/dist/libultrahdr/library.d.ts +9 -0
  34. package/dist/libultrahdr.d.ts +3 -123
  35. package/dist/libultrahdr.js +92 -167
  36. package/dist/libultrahdr.umd.js +91 -530
  37. package/dist/worker-interface.js +2 -2
  38. package/dist/worker-interface.umd.js +1 -3
  39. package/dist/worker-types.d.ts +2 -2
  40. package/dist/worker.js +3 -3
  41. package/dist/worker.umd.js +1 -1
  42. package/package.json +36 -29
  43. package/dist/LoaderBase-aa4842c4.js +0 -373
  44. package/dist/index.d.ts +0 -11
  45. package/dist/index.umd.js +0 -1360
  46. package/dist/materials/index.d.ts +0 -3
  47. package/dist/types.d.ts +0 -262
  48. /package/dist/{utils → core}/get-data-texture.d.ts +0 -0
  49. /package/dist/{encode-utils → encode}/find-texture-min-max.d.ts +0 -0
  50. /package/dist/{materials → encode/materials}/GainMapEncoderMaterial.d.ts +0 -0
  51. /package/dist/{materials → encode/materials}/SDRMaterial.d.ts +0 -0
@@ -1,16 +1,17 @@
1
1
  /**
2
- * @monogrid/gainmap-js v1.0.2
2
+ * @monogrid/gainmap-js v1.1.1
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
6
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.libultrahdr = {}, global.three));
10
- })(this, (function (exports, three) { 'use strict';
7
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
8
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
9
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.libultrahdr = {}));
10
+ })(this, (function (exports) { 'use strict';
11
11
 
12
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
12
13
  var Module = (() => {
13
- var _scriptDir = (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('libultrahdr.umd.js', document.baseURI).href));
14
+ var _scriptDir = (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('libultrahdr.umd.js', document.baseURI).href));
14
15
 
15
16
  return (
16
17
  async function(moduleArg = {}) {
@@ -106,7 +107,7 @@
106
107
  // TODO: Swap all `require()`'s with `import()`'s?
107
108
  const { createRequire } = await import('module');
108
109
  /** @suppress{duplicate} */
109
- var require$1 = createRequire((typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('libultrahdr.umd.js', document.baseURI).href)));
110
+ var require$1 = createRequire((typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('libultrahdr.umd.js', document.baseURI).href)));
110
111
  // These modules will usually be used on Node.js. Load them eagerly to avoid
111
112
  // the complexity of lazy-loading.
112
113
  var fs = require$1('fs');
@@ -118,7 +119,7 @@
118
119
  // EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
119
120
  // since there's no way getting the current absolute path of the module when
120
121
  // support for that is not available.
121
- scriptDirectory = require$1('url').fileURLToPath(new URL('./', (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('libultrahdr.umd.js', document.baseURI).href)))); // includes trailing slash
122
+ scriptDirectory = require$1('url').fileURLToPath(new URL('./', (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('libultrahdr.umd.js', document.baseURI).href)))); // includes trailing slash
122
123
  }
123
124
 
124
125
  // include: node_shell_read.js
@@ -712,7 +713,7 @@
712
713
  }
713
714
  } else {
714
715
  // Use bundler-friendly `new URL(..., import.meta.url)` pattern; works in browsers too.
715
- wasmBinaryFile = new URL('libultrahdr-esm.wasm', (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('libultrahdr.umd.js', document.baseURI).href))).href;
716
+ wasmBinaryFile = new URL('libultrahdr-esm.wasm', (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('libultrahdr.umd.js', document.baseURI).href))).href;
716
717
  }
717
718
 
718
719
  function getBinarySync(file) {
@@ -6506,460 +6507,111 @@
6506
6507
  })();
6507
6508
  var libultrahdr = Module;
6508
6509
 
6509
- const vertexShader = /* glsl */ `
6510
- varying vec2 vUv;
6511
-
6512
- void main() {
6513
- vUv = uv;
6514
- gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
6515
- }
6516
- `;
6517
- const fragmentShader = /* glsl */ `
6518
- uniform sampler2D sdr;
6519
- uniform sampler2D gainMap;
6520
- uniform vec3 gamma;
6521
- uniform vec3 offsetHdr;
6522
- uniform vec3 offsetSdr;
6523
- uniform vec3 gainMapMin;
6524
- uniform vec3 gainMapMax;
6525
- uniform float weightFactor;
6526
-
6527
- varying vec2 vUv;
6528
-
6529
- void main() {
6530
- vec3 rgb = texture2D(sdr, vUv).rgb;
6531
- vec3 recovery = texture2D(gainMap, vUv).rgb;
6532
- vec3 logRecovery = pow(recovery, gamma);
6533
- vec3 logBoost = gainMapMin * (1.0 - logRecovery) + gainMapMax * logRecovery;
6534
- vec3 hdrColor = (rgb + offsetSdr) * exp2(logBoost * weightFactor) - offsetHdr;
6535
- gl_FragColor = vec4(hdrColor, 1.0);
6536
- }
6537
- `;
6510
+ // @ts-expect-error untyped
6511
+ let library;
6538
6512
  /**
6539
- * A Material which is able to decode the Gainmap into a full HDR Representation
6513
+ * Instances the WASM module and returns it, only one module will be created upon multiple calls.
6514
+ * @category General
6515
+ * @group General
6540
6516
  *
6541
- * @category Materials
6542
- * @group Materials
6517
+ * @returns
6543
6518
  */
6544
- class GainMapDecoderMaterial extends three.ShaderMaterial {
6545
- /**
6546
- *
6547
- * @param params
6548
- */
6549
- constructor({ gamma, offsetHdr, offsetSdr, gainMapMin, gainMapMax, maxDisplayBoost, hdrCapacityMin, hdrCapacityMax, sdr, gainMap }) {
6550
- super({
6551
- name: 'GainMapDecoderMaterial',
6552
- vertexShader,
6553
- fragmentShader,
6554
- uniforms: {
6555
- sdr: { value: sdr },
6556
- gainMap: { value: gainMap },
6557
- gamma: { value: new three.Vector3(1.0 / gamma[0], 1.0 / gamma[1], 1.0 / gamma[2]) },
6558
- offsetHdr: { value: new three.Vector3().fromArray(offsetHdr) },
6559
- offsetSdr: { value: new three.Vector3().fromArray(offsetSdr) },
6560
- gainMapMin: { value: new three.Vector3().fromArray(gainMapMin) },
6561
- gainMapMax: { value: new three.Vector3().fromArray(gainMapMax) },
6562
- weightFactor: {
6563
- value: (Math.log2(maxDisplayBoost) - hdrCapacityMin) / (hdrCapacityMax - hdrCapacityMin)
6564
- }
6565
- },
6566
- blending: three.NoBlending,
6567
- depthTest: false,
6568
- depthWrite: false
6569
- });
6570
- this._maxDisplayBoost = maxDisplayBoost;
6571
- this._hdrCapacityMin = hdrCapacityMin;
6572
- this._hdrCapacityMax = hdrCapacityMax;
6573
- this.needsUpdate = true;
6574
- this.uniformsNeedUpdate = true;
6575
- }
6576
- get sdr() { return this.uniforms.sdr.value; }
6577
- set sdr(value) { this.uniforms.sdr.value = value; }
6578
- get gainMap() { return this.uniforms.gainMap.value; }
6579
- set gainMap(value) { this.uniforms.gainMap.value = value; }
6580
- /**
6581
- * @see {@link GainMapMetadata.offsetHdr}
6582
- */
6583
- get offsetHdr() { return this.uniforms.offsetHdr.value.toArray(); }
6584
- set offsetHdr(value) { this.uniforms.offsetHdr.value.fromArray(value); }
6585
- /**
6586
- * @see {@link GainMapMetadata.offsetSdr}
6587
- */
6588
- get offsetSdr() { return this.uniforms.offsetSdr.value.toArray(); }
6589
- set offsetSdr(value) { this.uniforms.offsetSdr.value.fromArray(value); }
6590
- /**
6591
- * @see {@link GainMapMetadata.gainMapMin}
6592
- */
6593
- get gainMapMin() { return this.uniforms.gainMapMin.value.toArray(); }
6594
- set gainMapMin(value) { this.uniforms.gainMapMin.value.fromArray(value); }
6595
- /**
6596
- * @see {@link GainMapMetadata.gainMapMax}
6597
- */
6598
- get gainMapMax() { return this.uniforms.gainMapMax.value.toArray(); }
6599
- set gainMapMax(value) { this.uniforms.gainMapMax.value.fromArray(value); }
6600
- /**
6601
- * @see {@link GainmapEncodingParameters.gamma}
6602
- */
6603
- get gamma() {
6604
- const g = this.uniforms.gamma.value;
6605
- return [1 / g.x, 1 / g.y, 1 / g.z];
6606
- }
6607
- set gamma(value) {
6608
- const g = this.uniforms.gamma.value;
6609
- g.x = 1.0 / value[0];
6610
- g.y = 1.0 / value[1];
6611
- g.z = 1.0 / value[2];
6612
- }
6613
- /**
6614
- * @see {@link GainMapMetadata.hdrCapacityMin}
6615
- * @remarks Logarithmic space
6616
- */
6617
- get hdrCapacityMin() { return this._hdrCapacityMin; }
6618
- set hdrCapacityMin(value) {
6619
- this._hdrCapacityMin = value;
6620
- this.calculateWeight();
6621
- }
6622
- /**
6623
- * @see {@link GainMapMetadata.hdrCapacityMin}
6624
- * @remarks Logarithmic space
6625
- */
6626
- get hdrCapacityMax() { return this._hdrCapacityMax; }
6627
- set hdrCapacityMax(value) {
6628
- this._hdrCapacityMax = value;
6629
- this.calculateWeight();
6630
- }
6631
- /**
6632
- * @see {@link GainmapDecodingParameters.maxDisplayBoost}
6633
- * @remarks Non Logarithmic space
6634
- */
6635
- get maxDisplayBoost() { return this._maxDisplayBoost; }
6636
- set maxDisplayBoost(value) {
6637
- this._maxDisplayBoost = value;
6638
- this.calculateWeight();
6639
- }
6640
- calculateWeight() {
6641
- this.uniforms.weightFactor.value = (Math.log2(this._maxDisplayBoost) - this._hdrCapacityMin) / (this._hdrCapacityMax - this._hdrCapacityMin);
6519
+ const getLibrary = async () => {
6520
+ if (!library) {
6521
+ library = await libultrahdr();
6642
6522
  }
6643
- }
6523
+ return library;
6524
+ };
6644
6525
 
6645
6526
  /**
6646
- * Utility structure used for rendering a texture with a material
6647
6527
  *
6648
- * @category General
6649
- * @group General
6528
+ * @param description
6529
+ * @param name
6530
+ * @param defaultValue
6531
+ * @returns
6650
6532
  */
6651
- class QuadRenderer {
6652
- /**
6653
- *
6654
- * @param sourceTexture
6655
- * @param renderer
6656
- */
6657
- constructor(width, height, type, colorSpace, material, renderer) {
6658
- this._rendererIsDisposable = false;
6659
- /**
6660
- * Renders the input texture using the specified material
6661
- */
6662
- this.render = () => {
6663
- this._renderer.setRenderTarget(this._renderTarget);
6664
- try {
6665
- this._renderer.render(this._scene, this._camera);
6533
+ const getAttribute = (description, name, defaultValue) => {
6534
+ var _a;
6535
+ let returnValue;
6536
+ const parsedValue = (_a = description.attributes.getNamedItem(name)) === null || _a === void 0 ? void 0 : _a.nodeValue;
6537
+ if (!parsedValue) {
6538
+ const node = description.getElementsByTagName(name)[0];
6539
+ if (node) {
6540
+ const values = node.getElementsByTagName('rdf:li');
6541
+ if (values.length === 3) {
6542
+ returnValue = Array.from(values).map(v => v.innerHTML);
6666
6543
  }
6667
- catch (e) {
6668
- this._renderer.setRenderTarget(null);
6669
- throw e;
6544
+ else {
6545
+ throw new Error(`Gainmap metadata contains an array of items for ${name} but its length is not 3`);
6670
6546
  }
6671
- this._renderer.setRenderTarget(null);
6672
- };
6673
- this._width = width;
6674
- this._height = height;
6675
- this._type = type;
6676
- this._material = material;
6677
- if (renderer) {
6678
- this._renderer = renderer;
6679
6547
  }
6680
6548
  else {
6681
- this._renderer = QuadRenderer.instantiateRenderer();
6682
- this._rendererIsDisposable = true;
6683
- }
6684
- this._scene = new three.Scene();
6685
- this._camera = new three.OrthographicCamera();
6686
- this._camera.position.set(0, 0, 10);
6687
- this._camera.left = -0.5;
6688
- this._camera.right = 0.5;
6689
- this._camera.top = 0.5;
6690
- this._camera.bottom = -0.5;
6691
- this._camera.updateProjectionMatrix();
6692
- this._quad = new three.Mesh(new three.PlaneGeometry(), this._material);
6693
- this._quad.geometry.computeBoundingBox();
6694
- this._scene.add(this._quad);
6695
- this._renderTarget = new three.WebGLRenderTarget(width, height, {
6696
- type,
6697
- colorSpace,
6698
- format: three.RGBAFormat,
6699
- magFilter: three.LinearFilter,
6700
- minFilter: three.LinearMipMapLinearFilter,
6701
- wrapS: three.RepeatWrapping,
6702
- wrapT: three.RepeatWrapping,
6703
- depthBuffer: false,
6704
- stencilBuffer: false,
6705
- generateMipmaps: true
6706
- });
6707
- }
6708
- /**
6709
- * Instantiates a temporary renderer
6710
- *
6711
- * @returns
6712
- */
6713
- static instantiateRenderer() {
6714
- const renderer = new three.WebGLRenderer();
6715
- renderer.setSize(128, 128);
6716
- // renderer.outputColorSpace = SRGBColorSpace
6717
- // renderer.toneMapping = LinearToneMapping
6718
- // renderer.debug.checkShaderErrors = false
6719
- // this._rendererIsDisposable = true
6720
- return renderer;
6721
- }
6722
- /**
6723
- * Obtains a Buffer containing the rendered texture.
6724
- *
6725
- * @returns
6726
- */
6727
- toArray() {
6728
- let out;
6729
- switch (this._type) {
6730
- case three.UnsignedByteType:
6731
- out = new Uint8ClampedArray(this._width * this._height * 4);
6732
- break;
6733
- case three.HalfFloatType:
6734
- out = new Uint16Array(this._width * this._height * 4);
6735
- break;
6736
- case three.UnsignedIntType:
6737
- out = new Uint32Array(this._width * this._height * 4);
6738
- break;
6739
- case three.ByteType:
6740
- out = new Int8Array(this._width * this._height * 4);
6741
- break;
6742
- case three.ShortType:
6743
- out = new Int16Array(this._width * this._height * 4);
6744
- break;
6745
- case three.IntType:
6746
- out = new Int32Array(this._width * this._height * 4);
6747
- break;
6748
- case three.FloatType:
6749
- out = new Float32Array(this._width * this._height * 4);
6750
- break;
6751
- default:
6752
- throw new Error('Unsupported data type');
6753
- }
6754
- this._renderer.readRenderTargetPixels(this._renderTarget, 0, 0, this._width, this._height, out);
6755
- return out;
6756
- }
6757
- toDataTexture() {
6758
- return new three.DataTexture(this.toArray(), this.width, this.height, three.RGBAFormat, three.HalfFloatType, three.UVMapping, three.RepeatWrapping, three.RepeatWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, 1, three.NoColorSpace);
6759
- }
6760
- /**
6761
- * If using a disposable renderer, it will dispose it.
6762
- */
6763
- dispose() {
6764
- this._renderer.setRenderTarget(null);
6765
- if (this._rendererIsDisposable) {
6766
- this._renderer.dispose();
6767
- this._renderer.forceContextLoss();
6549
+ if (defaultValue)
6550
+ return defaultValue;
6551
+ else
6552
+ throw new Error(`Can't find ${name} in gainmap metadata`);
6768
6553
  }
6769
6554
  }
6770
- /**
6771
- * Width of the texture
6772
- */
6773
- get width() { return this._width; }
6774
- set width(value) {
6775
- this._width = value;
6776
- this._renderTarget.setSize(this._width, this._height);
6777
- }
6778
- /**
6779
- * Height of the texture
6780
- */
6781
- get height() { return this._height; }
6782
- set height(value) {
6783
- this._height = value;
6784
- this._renderTarget.setSize(this._width, this._height);
6785
- }
6786
- /**
6787
- * The renderer used
6788
- */
6789
- get renderer() { return this._renderer; }
6790
- /**
6791
- * The `WebGLRenderTarget` used.
6792
- */
6793
- get renderTarget() { return this._renderTarget; }
6794
- set renderTarget(value) {
6795
- this._renderTarget = value;
6796
- this._width = value.width;
6797
- this._height = value.height;
6798
- // this._type = value.texture.type
6799
- }
6800
- /**
6801
- * The `Material` used.
6802
- */
6803
- get material() { return this._material; }
6804
- }
6805
-
6806
- class LoaderBase extends three.Loader {
6807
- /**
6808
- *
6809
- * @param renderer
6810
- * @param manager
6811
- */
6812
- constructor(renderer, manager) {
6813
- super(manager);
6814
- this.renderer = renderer;
6815
- }
6816
- /**
6817
- * @private
6818
- * @returns
6819
- */
6820
- prepareQuadRenderer() {
6821
- // temporary values
6822
- const material = new GainMapDecoderMaterial({
6823
- gainMapMax: [1, 1, 1],
6824
- gainMapMin: [0, 0, 0],
6825
- gamma: [1, 1, 1],
6826
- offsetHdr: [1, 1, 1],
6827
- offsetSdr: [1, 1, 1],
6828
- hdrCapacityMax: 1,
6829
- hdrCapacityMin: 0,
6830
- maxDisplayBoost: 1,
6831
- gainMap: new three.Texture(),
6832
- sdr: new three.Texture()
6833
- });
6834
- return new QuadRenderer(16, 16, three.HalfFloatType, three.NoColorSpace, material, this.renderer);
6835
- }
6836
- /**
6837
- *
6838
- * @private
6839
- * @param quadRenderer
6840
- * @param gainMapBuffer
6841
- * @param sdrBuffer
6842
- * @param metadata
6843
- */
6844
- async render(quadRenderer, gainMapBuffer, sdrBuffer, metadata) {
6845
- const gainMapBlob = new Blob([gainMapBuffer], { type: 'image/jpeg' });
6846
- // TODO: figure out why result.sdr is not usable here, problem is in the libultrahdr-wasm repo
6847
- // we use the original image buffer instead
6848
- const sdrBlob = new Blob([sdrBuffer], { type: 'image/jpeg' });
6849
- const [gainMapImageBitmap, sdrImageBitmap] = await Promise.all([
6850
- createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }),
6851
- createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
6852
- ]);
6853
- const gainMap = new three.Texture(gainMapImageBitmap, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.NoColorSpace);
6854
- gainMap.needsUpdate = true;
6855
- const sdr = new three.Texture(sdrImageBitmap, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.SRGBColorSpace);
6856
- sdr.needsUpdate = true;
6857
- quadRenderer.width = sdrImageBitmap.width;
6858
- quadRenderer.height = sdrImageBitmap.height;
6859
- quadRenderer.material.gainMap = gainMap;
6860
- quadRenderer.material.sdr = sdr;
6861
- quadRenderer.material.gainMapMin = metadata.gainMapMin;
6862
- quadRenderer.material.gainMapMax = metadata.gainMapMax;
6863
- quadRenderer.material.offsetHdr = metadata.offsetHdr;
6864
- quadRenderer.material.offsetSdr = metadata.offsetSdr;
6865
- quadRenderer.material.gamma = metadata.gamma;
6866
- quadRenderer.material.hdrCapacityMin = metadata.hdrCapacityMin;
6867
- quadRenderer.material.hdrCapacityMax = metadata.hdrCapacityMax;
6868
- quadRenderer.material.maxDisplayBoost = Math.pow(2, metadata.hdrCapacityMax);
6869
- quadRenderer.material.needsUpdate = true;
6870
- quadRenderer.render();
6555
+ else {
6556
+ returnValue = parsedValue;
6871
6557
  }
6872
- }
6873
-
6558
+ return returnValue;
6559
+ };
6874
6560
  /**
6875
- * A Three.js Loader for a JPEG with embedded gainmap metadata.
6561
+ * Decodes a JPEG file with an embedded Gainmap and XMP Metadata (aka JPEG-R)
6876
6562
  *
6877
- * @category Loaders
6878
- * @group Loaders
6563
+ * @category Decoding
6564
+ * @group Decoding
6879
6565
  *
6880
6566
  * @example
6881
- * import { JPEGRLoader } from '@monogrid/gainmap-js/libultrahdr'
6882
- * import {
6883
- * EquirectangularReflectionMapping,
6884
- * LinearFilter,
6885
- * Mesh,
6886
- * MeshBasicMaterial,
6887
- * PerspectiveCamera,
6888
- * PlaneGeometry,
6889
- * Scene,
6890
- * WebGLRenderer
6891
- * } from 'three'
6892
- *
6893
- * const renderer = new WebGLRenderer()
6894
- *
6895
- * const loader = new JPEGRLoader(renderer)
6896
- *
6897
- * const result = loader.load('gainmap.jpeg')
6898
- * // `result` can be used to populate a Texture
6899
- *
6900
- * const scene = new Scene()
6901
- * const mesh = new Mesh(
6902
- * new PlaneGeometry(),
6903
- * new MeshBasicMaterial({ map: result.renderTarget.texture })
6904
- * )
6905
- * scene.add(mesh)
6906
- * renderer.render(scene, new PerspectiveCamera())
6907
- *
6908
- * // `result.renderTarget.texture` must be
6909
- * // converted to `DataTexture` in order
6910
- * // to use it as Equirectanmgular scene background
6911
- * // if needed
6567
+ * import { decodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
6912
6568
  *
6913
- * scene.background = result.toDataTexture()
6914
- * scene.background.mapping = EquirectangularReflectionMapping
6915
- * scene.background.minFilter = LinearFilter
6916
-
6569
+ * // fetch a JPEG image containing a gainmap as ArrayBuffer
6570
+ * const gainmap = new Uint8Array(await (await fetch('gainmap.jpeg')).arrayBuffer())
6917
6571
  *
6918
- */
6919
- class JPEGRLoader extends LoaderBase {
6920
- /**
6921
- *
6922
- * @param url
6923
- * @param onLoad
6924
- * @param onProgress
6925
- * @param onError
6926
- * @returns
6927
- */
6928
- load(url, onLoad, onProgress, onError) {
6929
- const quadRenderer = this.prepareQuadRenderer();
6930
- const loader = new three.FileLoader(this.manager);
6931
- loader.setResponseType('arraybuffer');
6932
- loader.setRequestHeader(this.requestHeader);
6933
- loader.setPath(this.path);
6934
- loader.setWithCredentials(this.withCredentials);
6935
- loader.load(url, async (jpeg) => {
6936
- if (typeof jpeg === 'string')
6937
- throw new Error('Invalid buffer');
6938
- const { gainMap: gainMapJPEG, parsedMetadata } = await decodeJPEGMetadata(new Uint8Array(jpeg));
6939
- await this.render(quadRenderer, gainMapJPEG, jpeg, parsedMetadata);
6940
- if (typeof onLoad === 'function')
6941
- onLoad(quadRenderer);
6942
- quadRenderer.dispose();
6943
- }, onProgress, onError);
6944
- return quadRenderer;
6945
- }
6946
- }
6947
-
6948
- // @ts-expect-error untyped
6949
- let library;
6950
- /**
6951
- * Instances the WASM module and returns it, only one module will be created upon multiple calls.
6952
- * @category General
6953
- * @group General
6572
+ * // extract data from the JPEG
6573
+ * const { gainMap, sdr, parsedMetadata } = await decodeJPEGMetadata(gainmap)
6954
6574
  *
6955
- * @returns
6575
+ * @param file A Jpeg file Uint8Array.
6576
+ * @returns The decoded data
6577
+ * @throws {Error} if the provided file cannot be parsed or does not contain a valid Gainmap
6956
6578
  */
6957
- const getLibrary = async () => {
6958
- if (!library) {
6959
- library = await libultrahdr();
6960
- }
6961
- return library;
6579
+ const decodeJPEGMetadata = async (file) => {
6580
+ var _a, _b;
6581
+ const lib = await getLibrary();
6582
+ const result = lib.extractJpegR(file, file.length);
6583
+ if (!result.success)
6584
+ throw new Error(result.errorMessage);
6585
+ const parser = new DOMParser();
6586
+ const xmlDocument = parser.parseFromString(result.metadata, 'text/xml');
6587
+ const description = xmlDocument.getElementsByTagName('rdf:Description')[0];
6588
+ const gainMapMin = getAttribute(description, 'hdrgm:GainMapMin', '0');
6589
+ const gainMapMax = getAttribute(description, 'hdrgm:GainMapMax');
6590
+ const gamma = getAttribute(description, 'hdrgm:Gamma', '1');
6591
+ const offsetSDR = getAttribute(description, 'hdrgm:OffsetSDR', '0.015625');
6592
+ const offsetHDR = getAttribute(description, 'hdrgm:OffsetHDR', '0.015625');
6593
+ let hdrCapacityMin = (_a = description.attributes.getNamedItem('hdrgm:HDRCapacityMin')) === null || _a === void 0 ? void 0 : _a.nodeValue;
6594
+ if (!hdrCapacityMin)
6595
+ hdrCapacityMin = '0';
6596
+ const hdrCapacityMax = (_b = description.attributes.getNamedItem('hdrgm:HDRCapacityMax')) === null || _b === void 0 ? void 0 : _b.nodeValue;
6597
+ if (!hdrCapacityMax)
6598
+ throw new Error('Incomplete gainmap metadata');
6599
+ const parsedMetadata = {
6600
+ gainMapMin: Array.isArray(gainMapMin) ? gainMapMin.map(v => parseFloat(v)) : [parseFloat(gainMapMin), parseFloat(gainMapMin), parseFloat(gainMapMin)],
6601
+ gainMapMax: Array.isArray(gainMapMax) ? gainMapMax.map(v => parseFloat(v)) : [parseFloat(gainMapMax), parseFloat(gainMapMax), parseFloat(gainMapMax)],
6602
+ gamma: Array.isArray(gamma) ? gamma.map(v => parseFloat(v)) : [parseFloat(gamma), parseFloat(gamma), parseFloat(gamma)],
6603
+ offsetSdr: Array.isArray(offsetSDR) ? offsetSDR.map(v => parseFloat(v)) : [parseFloat(offsetSDR), parseFloat(offsetSDR), parseFloat(offsetSDR)],
6604
+ offsetHdr: Array.isArray(offsetHDR) ? offsetHDR.map(v => parseFloat(v)) : [parseFloat(offsetHDR), parseFloat(offsetHDR), parseFloat(offsetHDR)],
6605
+ hdrCapacityMin: parseFloat(hdrCapacityMin),
6606
+ hdrCapacityMax: parseFloat(hdrCapacityMax)
6607
+ };
6608
+ return Object.assign(Object.assign({}, result), {
6609
+ /**
6610
+ * Parsed metadata
6611
+ */
6612
+ parsedMetadata });
6962
6613
  };
6614
+
6963
6615
  /**
6964
6616
  * Encapsulates a Gainmap into a single JPEG file (aka: JPEG-R) with the base map
6965
6617
  * as the sdr visualization and the gainMap encoded into a MPF (Multi-Picture Format) tag.
@@ -7046,100 +6698,9 @@ void main() {
7046
6698
  throw new Error('This function expects a GainMap image compressed in jpeg');
7047
6699
  return lib.appendGainMap(encodingResult.sdr.width, encodingResult.sdr.height, encodingResult.sdr.data, encodingResult.sdr.data.length, encodingResult.gainMap.data, encodingResult.gainMap.data.length, encodingResult.gainMapMax.reduce((p, n) => p + n, 0) / encodingResult.gainMapMax.length, encodingResult.gainMapMin.reduce((p, n) => p + n, 0) / encodingResult.gainMapMin.length, encodingResult.gamma.reduce((p, n) => p + n, 0) / encodingResult.gamma.length, encodingResult.offsetSdr.reduce((p, n) => p + n, 0) / encodingResult.offsetSdr.length, encodingResult.offsetHdr.reduce((p, n) => p + n, 0) / encodingResult.offsetHdr.length, encodingResult.hdrCapacityMin, encodingResult.hdrCapacityMax);
7048
6700
  };
7049
- /**
7050
- *
7051
- * @param description
7052
- * @param name
7053
- * @param defaultValue
7054
- * @returns
7055
- */
7056
- const getAttribute = (description, name, defaultValue) => {
7057
- var _a;
7058
- let returnValue;
7059
- const parsedValue = (_a = description.attributes.getNamedItem(name)) === null || _a === void 0 ? void 0 : _a.nodeValue;
7060
- if (!parsedValue) {
7061
- const node = description.getElementsByTagName(name)[0];
7062
- if (node) {
7063
- const values = node.getElementsByTagName('rdf:li');
7064
- if (values.length === 3) {
7065
- returnValue = Array.from(values).map(v => v.innerHTML);
7066
- }
7067
- else {
7068
- throw new Error(`Gainmap metadata contains an array of items for ${name} but its length is not 3`);
7069
- }
7070
- }
7071
- else {
7072
- if (defaultValue)
7073
- return defaultValue;
7074
- else
7075
- throw new Error(`Can't find ${name} in gainmap metadata`);
7076
- }
7077
- }
7078
- else {
7079
- returnValue = parsedValue;
7080
- }
7081
- return returnValue;
7082
- };
7083
- /**
7084
- * Decodes a JPEG file with an embedded Gainmap and XMP Metadata (aka JPEG-R)
7085
- *
7086
- * @category Decoding
7087
- * @group Decoding
7088
- *
7089
- * @example
7090
- * import { decodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
7091
- *
7092
- * // fetch a JPEG image containing a gainmap as ArrayBuffer
7093
- * const gainmap = new Uint8Array(await (await fetch('gainmap.jpeg')).arrayBuffer())
7094
- *
7095
- * // extract data from the JPEG
7096
- * const { gainMap, sdr, parsedMetadata } = await decodeJPEGMetadata(gainmap)
7097
- *
7098
- * @param file A Jpeg file Uint8Array.
7099
- * @returns The decoded data
7100
- * @throws {Error} if the provided file cannot be parsed or does not contain a valid Gainmap
7101
- */
7102
- const decodeJPEGMetadata = async (file) => {
7103
- var _a, _b;
7104
- const lib = await getLibrary();
7105
- const result = lib.extractJpegR(file, file.length);
7106
- if (!result.success)
7107
- throw new Error(result.errorMessage);
7108
- const parser = new DOMParser();
7109
- const xmldocuemnt = parser.parseFromString(result.metadata, 'text/xml');
7110
- const description = xmldocuemnt.getElementsByTagName('rdf:Description')[0];
7111
- const gainMapMin = getAttribute(description, 'hdrgm:GainMapMin', '0');
7112
- const gainMapMax = getAttribute(description, 'hdrgm:GainMapMax');
7113
- const gamma = getAttribute(description, 'hdrgm:Gamma', '1');
7114
- const offsetSDR = getAttribute(description, 'hdrgm:OffsetSDR', '0.015625');
7115
- const offsetHDR = getAttribute(description, 'hdrgm:OffsetHDR', '0.015625');
7116
- let hdrCapacityMin = (_a = description.attributes.getNamedItem('hdrgm:HDRCapacityMin')) === null || _a === void 0 ? void 0 : _a.nodeValue;
7117
- if (!hdrCapacityMin)
7118
- hdrCapacityMin = '0';
7119
- const hdrCapacityMax = (_b = description.attributes.getNamedItem('hdrgm:HDRCapacityMax')) === null || _b === void 0 ? void 0 : _b.nodeValue;
7120
- if (!hdrCapacityMax)
7121
- throw new Error('Incomplete gainmap metadata');
7122
- const parsedMetadata = {
7123
- gainMapMin: Array.isArray(gainMapMin) ? gainMapMin.map(v => parseFloat(v)) : [parseFloat(gainMapMin), parseFloat(gainMapMin), parseFloat(gainMapMin)],
7124
- gainMapMax: Array.isArray(gainMapMax) ? gainMapMax.map(v => parseFloat(v)) : [parseFloat(gainMapMax), parseFloat(gainMapMax), parseFloat(gainMapMax)],
7125
- gamma: Array.isArray(gamma) ? gamma.map(v => parseFloat(v)) : [parseFloat(gamma), parseFloat(gamma), parseFloat(gamma)],
7126
- offsetSdr: Array.isArray(offsetSDR) ? offsetSDR.map(v => parseFloat(v)) : [parseFloat(offsetSDR), parseFloat(offsetSDR), parseFloat(offsetSDR)],
7127
- offsetHdr: Array.isArray(offsetHDR) ? offsetHDR.map(v => parseFloat(v)) : [parseFloat(offsetHDR), parseFloat(offsetHDR), parseFloat(offsetHDR)],
7128
- hdrCapacityMin: parseFloat(hdrCapacityMin),
7129
- hdrCapacityMax: parseFloat(hdrCapacityMax)
7130
- };
7131
- return Object.assign(Object.assign({}, result), {
7132
- /**
7133
- * Parsed metadata
7134
- */
7135
- parsedMetadata });
7136
- };
7137
6701
 
7138
- exports.JPEGRLoader = JPEGRLoader;
7139
6702
  exports.decodeJPEGMetadata = decodeJPEGMetadata;
7140
6703
  exports.encodeJPEGMetadata = encodeJPEGMetadata;
7141
6704
  exports.getLibrary = getLibrary;
7142
6705
 
7143
- Object.defineProperty(exports, '__esModule', { value: true });
7144
-
7145
6706
  }));