@monogrid/gainmap-js 1.1.0 → 1.1.1

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.
@@ -1,10 +1,10 @@
1
1
  /**
2
- * @monogrid/gainmap-js v1.0.2
2
+ * @monogrid/gainmap-js v1.1.0
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
6
6
  import { FileLoader } from 'three';
7
- import { L as LoaderBase } from './LoaderBase-aa4842c4.js';
7
+ import { L as LoaderBase } from './LoaderBase-eee19701.js';
8
8
 
9
9
  var Module = (() => {
10
10
  var _scriptDir = import.meta.url;
@@ -6503,80 +6503,6 @@ run();
6503
6503
  })();
6504
6504
  var libultrahdr = Module;
6505
6505
 
6506
- /**
6507
- * A Three.js Loader for a JPEG with embedded gainmap metadata.
6508
- *
6509
- * @category Loaders
6510
- * @group Loaders
6511
- *
6512
- * @example
6513
- * import { JPEGRLoader } from '@monogrid/gainmap-js/libultrahdr'
6514
- * import {
6515
- * EquirectangularReflectionMapping,
6516
- * LinearFilter,
6517
- * Mesh,
6518
- * MeshBasicMaterial,
6519
- * PerspectiveCamera,
6520
- * PlaneGeometry,
6521
- * Scene,
6522
- * WebGLRenderer
6523
- * } from 'three'
6524
- *
6525
- * const renderer = new WebGLRenderer()
6526
- *
6527
- * const loader = new JPEGRLoader(renderer)
6528
- *
6529
- * const result = loader.load('gainmap.jpeg')
6530
- * // `result` can be used to populate a Texture
6531
- *
6532
- * const scene = new Scene()
6533
- * const mesh = new Mesh(
6534
- * new PlaneGeometry(),
6535
- * new MeshBasicMaterial({ map: result.renderTarget.texture })
6536
- * )
6537
- * scene.add(mesh)
6538
- * renderer.render(scene, new PerspectiveCamera())
6539
- *
6540
- * // `result.renderTarget.texture` must be
6541
- * // converted to `DataTexture` in order
6542
- * // to use it as Equirectanmgular scene background
6543
- * // if needed
6544
- *
6545
- * scene.background = result.toDataTexture()
6546
- * scene.background.mapping = EquirectangularReflectionMapping
6547
- * scene.background.minFilter = LinearFilter
6548
-
6549
- *
6550
- */
6551
- class JPEGRLoader extends LoaderBase {
6552
- /**
6553
- *
6554
- * @param url
6555
- * @param onLoad
6556
- * @param onProgress
6557
- * @param onError
6558
- * @returns
6559
- */
6560
- load(url, onLoad, onProgress, onError) {
6561
- const quadRenderer = this.prepareQuadRenderer();
6562
- const loader = new FileLoader(this.manager);
6563
- loader.setResponseType('arraybuffer');
6564
- loader.setRequestHeader(this.requestHeader);
6565
- loader.setPath(this.path);
6566
- loader.setWithCredentials(this.withCredentials);
6567
- loader.load(url, async (jpeg) => {
6568
- if (typeof jpeg === 'string')
6569
- throw new Error('Invalid buffer');
6570
- const { gainMap: gainMapJPEG, parsedMetadata } = await decodeJPEGMetadata(new Uint8Array(jpeg));
6571
- await this.render(quadRenderer, gainMapJPEG, jpeg, parsedMetadata);
6572
- if (typeof onLoad === 'function')
6573
- onLoad(quadRenderer);
6574
- quadRenderer.dispose();
6575
- }, onProgress, onError);
6576
- return quadRenderer;
6577
- }
6578
- }
6579
-
6580
6506
  // @ts-expect-error untyped
6581
6507
  let library;
6582
6508
  /**
@@ -6592,6 +6518,96 @@ const getLibrary = async () => {
6592
6518
  }
6593
6519
  return library;
6594
6520
  };
6521
+
6522
+ /**
6523
+ *
6524
+ * @param description
6525
+ * @param name
6526
+ * @param defaultValue
6527
+ * @returns
6528
+ */
6529
+ const getAttribute = (description, name, defaultValue) => {
6530
+ var _a;
6531
+ let returnValue;
6532
+ const parsedValue = (_a = description.attributes.getNamedItem(name)) === null || _a === void 0 ? void 0 : _a.nodeValue;
6533
+ if (!parsedValue) {
6534
+ const node = description.getElementsByTagName(name)[0];
6535
+ if (node) {
6536
+ const values = node.getElementsByTagName('rdf:li');
6537
+ if (values.length === 3) {
6538
+ returnValue = Array.from(values).map(v => v.innerHTML);
6539
+ }
6540
+ else {
6541
+ throw new Error(`Gainmap metadata contains an array of items for ${name} but its length is not 3`);
6542
+ }
6543
+ }
6544
+ else {
6545
+ if (defaultValue)
6546
+ return defaultValue;
6547
+ else
6548
+ throw new Error(`Can't find ${name} in gainmap metadata`);
6549
+ }
6550
+ }
6551
+ else {
6552
+ returnValue = parsedValue;
6553
+ }
6554
+ return returnValue;
6555
+ };
6556
+ /**
6557
+ * Decodes a JPEG file with an embedded Gainmap and XMP Metadata (aka JPEG-R)
6558
+ *
6559
+ * @category Decoding
6560
+ * @group Decoding
6561
+ *
6562
+ * @example
6563
+ * import { decodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
6564
+ *
6565
+ * // fetch a JPEG image containing a gainmap as ArrayBuffer
6566
+ * const gainmap = new Uint8Array(await (await fetch('gainmap.jpeg')).arrayBuffer())
6567
+ *
6568
+ * // extract data from the JPEG
6569
+ * const { gainMap, sdr, parsedMetadata } = await decodeJPEGMetadata(gainmap)
6570
+ *
6571
+ * @param file A Jpeg file Uint8Array.
6572
+ * @returns The decoded data
6573
+ * @throws {Error} if the provided file cannot be parsed or does not contain a valid Gainmap
6574
+ */
6575
+ const decodeJPEGMetadata = async (file) => {
6576
+ var _a, _b;
6577
+ const lib = await getLibrary();
6578
+ const result = lib.extractJpegR(file, file.length);
6579
+ if (!result.success)
6580
+ throw new Error(result.errorMessage);
6581
+ const parser = new DOMParser();
6582
+ const xmlDocument = parser.parseFromString(result.metadata, 'text/xml');
6583
+ const description = xmlDocument.getElementsByTagName('rdf:Description')[0];
6584
+ const gainMapMin = getAttribute(description, 'hdrgm:GainMapMin', '0');
6585
+ const gainMapMax = getAttribute(description, 'hdrgm:GainMapMax');
6586
+ const gamma = getAttribute(description, 'hdrgm:Gamma', '1');
6587
+ const offsetSDR = getAttribute(description, 'hdrgm:OffsetSDR', '0.015625');
6588
+ const offsetHDR = getAttribute(description, 'hdrgm:OffsetHDR', '0.015625');
6589
+ let hdrCapacityMin = (_a = description.attributes.getNamedItem('hdrgm:HDRCapacityMin')) === null || _a === void 0 ? void 0 : _a.nodeValue;
6590
+ if (!hdrCapacityMin)
6591
+ hdrCapacityMin = '0';
6592
+ const hdrCapacityMax = (_b = description.attributes.getNamedItem('hdrgm:HDRCapacityMax')) === null || _b === void 0 ? void 0 : _b.nodeValue;
6593
+ if (!hdrCapacityMax)
6594
+ throw new Error('Incomplete gainmap metadata');
6595
+ const parsedMetadata = {
6596
+ gainMapMin: Array.isArray(gainMapMin) ? gainMapMin.map(v => parseFloat(v)) : [parseFloat(gainMapMin), parseFloat(gainMapMin), parseFloat(gainMapMin)],
6597
+ gainMapMax: Array.isArray(gainMapMax) ? gainMapMax.map(v => parseFloat(v)) : [parseFloat(gainMapMax), parseFloat(gainMapMax), parseFloat(gainMapMax)],
6598
+ gamma: Array.isArray(gamma) ? gamma.map(v => parseFloat(v)) : [parseFloat(gamma), parseFloat(gamma), parseFloat(gamma)],
6599
+ offsetSdr: Array.isArray(offsetSDR) ? offsetSDR.map(v => parseFloat(v)) : [parseFloat(offsetSDR), parseFloat(offsetSDR), parseFloat(offsetSDR)],
6600
+ offsetHdr: Array.isArray(offsetHDR) ? offsetHDR.map(v => parseFloat(v)) : [parseFloat(offsetHDR), parseFloat(offsetHDR), parseFloat(offsetHDR)],
6601
+ hdrCapacityMin: parseFloat(hdrCapacityMin),
6602
+ hdrCapacityMax: parseFloat(hdrCapacityMax)
6603
+ };
6604
+ return Object.assign(Object.assign({}, result), {
6605
+ /**
6606
+ * Parsed metadata
6607
+ */
6608
+ parsedMetadata });
6609
+ };
6610
+
6595
6611
  /**
6596
6612
  * Encapsulates a Gainmap into a single JPEG file (aka: JPEG-R) with the base map
6597
6613
  * as the sdr visualization and the gainMap encoded into a MPF (Multi-Picture Format) tag.
@@ -6678,93 +6694,79 @@ const encodeJPEGMetadata = async (encodingResult) => {
6678
6694
  throw new Error('This function expects a GainMap image compressed in jpeg');
6679
6695
  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);
6680
6696
  };
6697
+
6681
6698
  /**
6699
+ * A Three.js Loader for a JPEG with embedded gainmap metadata.
6682
6700
  *
6683
- * @param description
6684
- * @param name
6685
- * @param defaultValue
6686
- * @returns
6687
- */
6688
- const getAttribute = (description, name, defaultValue) => {
6689
- var _a;
6690
- let returnValue;
6691
- const parsedValue = (_a = description.attributes.getNamedItem(name)) === null || _a === void 0 ? void 0 : _a.nodeValue;
6692
- if (!parsedValue) {
6693
- const node = description.getElementsByTagName(name)[0];
6694
- if (node) {
6695
- const values = node.getElementsByTagName('rdf:li');
6696
- if (values.length === 3) {
6697
- returnValue = Array.from(values).map(v => v.innerHTML);
6698
- }
6699
- else {
6700
- throw new Error(`Gainmap metadata contains an array of items for ${name} but its length is not 3`);
6701
- }
6702
- }
6703
- else {
6704
- if (defaultValue)
6705
- return defaultValue;
6706
- else
6707
- throw new Error(`Can't find ${name} in gainmap metadata`);
6708
- }
6709
- }
6710
- else {
6711
- returnValue = parsedValue;
6712
- }
6713
- return returnValue;
6714
- };
6715
- /**
6716
- * Decodes a JPEG file with an embedded Gainmap and XMP Metadata (aka JPEG-R)
6717
- *
6718
- * @category Decoding
6719
- * @group Decoding
6701
+ * @category Loaders
6702
+ * @group Loaders
6720
6703
  *
6721
6704
  * @example
6722
- * import { decodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
6705
+ * import { JPEGRLoader } from '@monogrid/gainmap-js/libultrahdr'
6706
+ * import {
6707
+ * EquirectangularReflectionMapping,
6708
+ * LinearFilter,
6709
+ * Mesh,
6710
+ * MeshBasicMaterial,
6711
+ * PerspectiveCamera,
6712
+ * PlaneGeometry,
6713
+ * Scene,
6714
+ * WebGLRenderer
6715
+ * } from 'three'
6723
6716
  *
6724
- * // fetch a JPEG image containing a gainmap as ArrayBuffer
6725
- * const gainmap = new Uint8Array(await (await fetch('gainmap.jpeg')).arrayBuffer())
6717
+ * const renderer = new WebGLRenderer()
6726
6718
  *
6727
- * // extract data from the JPEG
6728
- * const { gainMap, sdr, parsedMetadata } = await decodeJPEGMetadata(gainmap)
6719
+ * const loader = new JPEGRLoader(renderer)
6720
+ *
6721
+ * const result = loader.load('gainmap.jpeg')
6722
+ * // `result` can be used to populate a Texture
6723
+ *
6724
+ * const scene = new Scene()
6725
+ * const mesh = new Mesh(
6726
+ * new PlaneGeometry(),
6727
+ * new MeshBasicMaterial({ map: result.renderTarget.texture })
6728
+ * )
6729
+ * scene.add(mesh)
6730
+ * renderer.render(scene, new PerspectiveCamera())
6731
+ *
6732
+ * // `result.renderTarget.texture` must be
6733
+ * // converted to `DataTexture` in order
6734
+ * // to use it as Equirectangular scene background
6735
+ * // if needed
6736
+ *
6737
+ * scene.background = result.toDataTexture()
6738
+ * scene.background.mapping = EquirectangularReflectionMapping
6739
+ * scene.background.minFilter = LinearFilter
6740
+
6729
6741
  *
6730
- * @param file A Jpeg file Uint8Array.
6731
- * @returns The decoded data
6732
- * @throws {Error} if the provided file cannot be parsed or does not contain a valid Gainmap
6733
6742
  */
6734
- const decodeJPEGMetadata = async (file) => {
6735
- var _a, _b;
6736
- const lib = await getLibrary();
6737
- const result = lib.extractJpegR(file, file.length);
6738
- if (!result.success)
6739
- throw new Error(result.errorMessage);
6740
- const parser = new DOMParser();
6741
- const xmldocuemnt = parser.parseFromString(result.metadata, 'text/xml');
6742
- const description = xmldocuemnt.getElementsByTagName('rdf:Description')[0];
6743
- const gainMapMin = getAttribute(description, 'hdrgm:GainMapMin', '0');
6744
- const gainMapMax = getAttribute(description, 'hdrgm:GainMapMax');
6745
- const gamma = getAttribute(description, 'hdrgm:Gamma', '1');
6746
- const offsetSDR = getAttribute(description, 'hdrgm:OffsetSDR', '0.015625');
6747
- const offsetHDR = getAttribute(description, 'hdrgm:OffsetHDR', '0.015625');
6748
- let hdrCapacityMin = (_a = description.attributes.getNamedItem('hdrgm:HDRCapacityMin')) === null || _a === void 0 ? void 0 : _a.nodeValue;
6749
- if (!hdrCapacityMin)
6750
- hdrCapacityMin = '0';
6751
- const hdrCapacityMax = (_b = description.attributes.getNamedItem('hdrgm:HDRCapacityMax')) === null || _b === void 0 ? void 0 : _b.nodeValue;
6752
- if (!hdrCapacityMax)
6753
- throw new Error('Incomplete gainmap metadata');
6754
- const parsedMetadata = {
6755
- gainMapMin: Array.isArray(gainMapMin) ? gainMapMin.map(v => parseFloat(v)) : [parseFloat(gainMapMin), parseFloat(gainMapMin), parseFloat(gainMapMin)],
6756
- gainMapMax: Array.isArray(gainMapMax) ? gainMapMax.map(v => parseFloat(v)) : [parseFloat(gainMapMax), parseFloat(gainMapMax), parseFloat(gainMapMax)],
6757
- gamma: Array.isArray(gamma) ? gamma.map(v => parseFloat(v)) : [parseFloat(gamma), parseFloat(gamma), parseFloat(gamma)],
6758
- offsetSdr: Array.isArray(offsetSDR) ? offsetSDR.map(v => parseFloat(v)) : [parseFloat(offsetSDR), parseFloat(offsetSDR), parseFloat(offsetSDR)],
6759
- offsetHdr: Array.isArray(offsetHDR) ? offsetHDR.map(v => parseFloat(v)) : [parseFloat(offsetHDR), parseFloat(offsetHDR), parseFloat(offsetHDR)],
6760
- hdrCapacityMin: parseFloat(hdrCapacityMin),
6761
- hdrCapacityMax: parseFloat(hdrCapacityMax)
6762
- };
6763
- return Object.assign(Object.assign({}, result), {
6764
- /**
6765
- * Parsed metadata
6766
- */
6767
- parsedMetadata });
6768
- };
6743
+ class JPEGRLoader extends LoaderBase {
6744
+ /**
6745
+ *
6746
+ * @param url
6747
+ * @param onLoad
6748
+ * @param onProgress
6749
+ * @param onError
6750
+ * @returns
6751
+ */
6752
+ load(url, onLoad, onProgress, onError) {
6753
+ const quadRenderer = this.prepareQuadRenderer();
6754
+ const loader = new FileLoader(this.manager);
6755
+ loader.setResponseType('arraybuffer');
6756
+ loader.setRequestHeader(this.requestHeader);
6757
+ loader.setPath(this.path);
6758
+ loader.setWithCredentials(this.withCredentials);
6759
+ loader.load(url, async (jpeg) => {
6760
+ if (typeof jpeg === 'string')
6761
+ throw new Error('Invalid buffer');
6762
+ const { gainMap: gainMapJPEG, parsedMetadata } = await decodeJPEGMetadata(new Uint8Array(jpeg));
6763
+ await this.render(quadRenderer, gainMapJPEG, jpeg, parsedMetadata);
6764
+ if (typeof onLoad === 'function')
6765
+ onLoad(quadRenderer);
6766
+ quadRenderer.dispose();
6767
+ }, onProgress, onError);
6768
+ return quadRenderer;
6769
+ }
6770
+ }
6769
6771
 
6770
6772
  export { JPEGRLoader, decodeJPEGMetadata, encodeJPEGMetadata, getLibrary };