@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
@@ -0,0 +1,31 @@
1
+ import { GainMapMetadata } from '../core/types';
2
+ /**
3
+ * Decodes a JPEG file with an embedded Gainmap and XMP Metadata (aka JPEG-R)
4
+ *
5
+ * @category Decoding
6
+ * @group Decoding
7
+ *
8
+ * @example
9
+ * import { decodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
10
+ *
11
+ * // fetch a JPEG image containing a gainmap as ArrayBuffer
12
+ * const gainmap = new Uint8Array(await (await fetch('gainmap.jpeg')).arrayBuffer())
13
+ *
14
+ * // extract data from the JPEG
15
+ * const { gainMap, sdr, parsedMetadata } = await decodeJPEGMetadata(gainmap)
16
+ *
17
+ * @param file A Jpeg file Uint8Array.
18
+ * @returns The decoded data
19
+ * @throws {Error} if the provided file cannot be parsed or does not contain a valid Gainmap
20
+ */
21
+ export declare const decodeJPEGMetadata: (file: Uint8Array) => Promise<{
22
+ /**
23
+ * Parsed metadata
24
+ */
25
+ parsedMetadata: GainMapMetadata;
26
+ metadata: string | Uint8ClampedArray | Int8Array | ArrayBuffer | Uint8Array;
27
+ success: boolean;
28
+ errorMessage: any;
29
+ sdr: any;
30
+ gainMap: any;
31
+ }>;
@@ -0,0 +1,84 @@
1
+ import { type GainMapMetadata } from '../core/types';
2
+ import { type CompressedImage } from '../encode/types';
3
+ /**
4
+ * Encapsulates a Gainmap into a single JPEG file (aka: JPEG-R) with the base map
5
+ * as the sdr visualization and the gainMap encoded into a MPF (Multi-Picture Format) tag.
6
+ *
7
+ * @category Encoding
8
+ * @group Encoding
9
+ *
10
+ * @example
11
+ * import { compress, encode, findTextureMinMax } from '@monogrid/gainmap-js'
12
+ * import { encodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
13
+ * import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
14
+ *
15
+ * // load an HDR file
16
+ * const loader = new EXRLoader()
17
+ * const image = await loader.loadAsync('image.exr')
18
+ *
19
+ * // find RAW RGB Max value of a texture
20
+ * const textureMax = await findTextureMinMax(image)
21
+ *
22
+ * // Encode the gainmap
23
+ * const encodingResult = encode({
24
+ * image,
25
+ * maxContentBoost: Math.max.apply(this, textureMax)
26
+ * })
27
+ *
28
+ * // obtain the RAW RGBA SDR buffer and create an ImageData
29
+ * const sdrImageData = new ImageData(
30
+ * encodingResult.sdr.toArray(),
31
+ * encodingResult.sdr.width,
32
+ * encodingResult.sdr.height
33
+ * )
34
+ * // obtain the RAW RGBA Gain map buffer and create an ImageData
35
+ * const gainMapImageData = new ImageData(
36
+ * encodingResult.gainMap.toArray(),
37
+ * encodingResult.gainMap.width,
38
+ * encodingResult.gainMap.height
39
+ * )
40
+ *
41
+ * // parallel compress the RAW buffers into the specified mimeType
42
+ * const mimeType = 'image/jpeg'
43
+ * const quality = 0.9
44
+ *
45
+ * const [sdr, gainMap] = await Promise.all([
46
+ * compress({
47
+ * source: sdrImageData,
48
+ * mimeType,
49
+ * quality,
50
+ * flipY: true // output needs to be flipped
51
+ * }),
52
+ * compress({
53
+ * source: gainMapImageData,
54
+ * mimeType,
55
+ * quality,
56
+ * flipY: true // output needs to be flipped
57
+ * })
58
+ * ])
59
+ *
60
+ * // obtain the metadata which will be embedded into
61
+ * // and XMP tag inside the final JPEG file
62
+ * const metadata = encodingResult.getMetadata()
63
+ *
64
+ * // embed the compressed images + metadata into a single
65
+ * // JPEG file
66
+ * const jpeg = await encodeJPEGMetadata({
67
+ * ...encodingResult,
68
+ * ...metadata,
69
+ * sdr,
70
+ * gainMap
71
+ * })
72
+ *
73
+ * // `jpeg` will be an `Uint8Array` which can be saved somewhere
74
+ *
75
+ *
76
+ * @param encodingResult
77
+ * @returns an Uint8Array representing a JPEG-R file
78
+ * @throws {Error} If `encodingResult.sdr.mimeType !== 'image/jpeg'`
79
+ * @throws {Error} If `encodingResult.gainMap.mimeType !== 'image/jpeg'`
80
+ */
81
+ export declare const encodeJPEGMetadata: (encodingResult: GainMapMetadata & {
82
+ sdr: CompressedImage;
83
+ gainMap: CompressedImage;
84
+ }) => Promise<Uint8Array>;
@@ -0,0 +1,9 @@
1
+ import { MainModule } from '../../libultrahdr-wasm/build/libultrahdr';
2
+ /**
3
+ * Instances the WASM module and returns it, only one module will be created upon multiple calls.
4
+ * @category General
5
+ * @group General
6
+ *
7
+ * @returns
8
+ */
9
+ export declare const getLibrary: () => Promise<MainModule>;
@@ -1,124 +1,4 @@
1
- import { MainModule } from '../libultrahdr-wasm/build/libultrahdr';
2
- import { CompressedImage, GainMapMetadata } from './types';
3
1
  export * from '../libultrahdr-wasm/build/libultrahdr';
4
- export * from './loaders/JPEGRLoader';
5
- /**
6
- * Instances the WASM module and returns it, only one module will be created upon multiple calls.
7
- * @category General
8
- * @group General
9
- *
10
- * @returns
11
- */
12
- export declare const getLibrary: () => Promise<MainModule>;
13
- /**
14
- * Encapsulates a Gainmap into a single JPEG file (aka: JPEG-R) with the base map
15
- * as the sdr visualization and the gainMap encoded into a MPF (Multi-Picture Format) tag.
16
- *
17
- * @category Encoding
18
- * @group Encoding
19
- *
20
- * @example
21
- * import { compress, encode, findTextureMinMax } from '@monogrid/gainmap-js'
22
- * import { encodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
23
- * import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
24
- *
25
- * // load an HDR file
26
- * const loader = new EXRLoader()
27
- * const image = await loader.loadAsync('image.exr')
28
- *
29
- * // find RAW RGB Max value of a texture
30
- * const textureMax = await findTextureMinMax(image)
31
- *
32
- * // Encode the gainmap
33
- * const encodingResult = encode({
34
- * image,
35
- * maxContentBoost: Math.max.apply(this, textureMax)
36
- * })
37
- *
38
- * // obtain the RAW RGBA SDR buffer and create an ImageData
39
- * const sdrImageData = new ImageData(
40
- * encodingResult.sdr.toArray(),
41
- * encodingResult.sdr.width,
42
- * encodingResult.sdr.height
43
- * )
44
- * // obtain the RAW RGBA Gain map buffer and create an ImageData
45
- * const gainMapImageData = new ImageData(
46
- * encodingResult.gainMap.toArray(),
47
- * encodingResult.gainMap.width,
48
- * encodingResult.gainMap.height
49
- * )
50
- *
51
- * // parallel compress the RAW buffers into the specified mimeType
52
- * const mimeType = 'image/jpeg'
53
- * const quality = 0.9
54
- *
55
- * const [sdr, gainMap] = await Promise.all([
56
- * compress({
57
- * source: sdrImageData,
58
- * mimeType,
59
- * quality,
60
- * flipY: true // output needs to be flipped
61
- * }),
62
- * compress({
63
- * source: gainMapImageData,
64
- * mimeType,
65
- * quality,
66
- * flipY: true // output needs to be flipped
67
- * })
68
- * ])
69
- *
70
- * // obtain the metadata which will be embedded into
71
- * // and XMP tag inside the final JPEG file
72
- * const metadata = encodingResult.getMetadata()
73
- *
74
- * // embed the compressed images + metadata into a single
75
- * // JPEG file
76
- * const jpeg = await encodeJPEGMetadata({
77
- * ...encodingResult,
78
- * ...metadata,
79
- * sdr,
80
- * gainMap
81
- * })
82
- *
83
- * // `jpeg` will be an `Uint8Array` which can be saved somewhere
84
- *
85
- *
86
- * @param encodingResult
87
- * @returns an Uint8Array representing a JPEG-R file
88
- * @throws {Error} If `encodingResult.sdr.mimeType !== 'image/jpeg'`
89
- * @throws {Error} If `encodingResult.gainMap.mimeType !== 'image/jpeg'`
90
- */
91
- export declare const encodeJPEGMetadata: (encodingResult: GainMapMetadata & {
92
- sdr: CompressedImage;
93
- gainMap: CompressedImage;
94
- }) => Promise<Uint8Array>;
95
- /**
96
- * Decodes a JPEG file with an embedded Gainmap and XMP Metadata (aka JPEG-R)
97
- *
98
- * @category Decoding
99
- * @group Decoding
100
- *
101
- * @example
102
- * import { decodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
103
- *
104
- * // fetch a JPEG image containing a gainmap as ArrayBuffer
105
- * const gainmap = new Uint8Array(await (await fetch('gainmap.jpeg')).arrayBuffer())
106
- *
107
- * // extract data from the JPEG
108
- * const { gainMap, sdr, parsedMetadata } = await decodeJPEGMetadata(gainmap)
109
- *
110
- * @param file A Jpeg file Uint8Array.
111
- * @returns The decoded data
112
- * @throws {Error} if the provided file cannot be parsed or does not contain a valid Gainmap
113
- */
114
- export declare const decodeJPEGMetadata: (file: Uint8Array) => Promise<{
115
- /**
116
- * Parsed metadata
117
- */
118
- parsedMetadata: GainMapMetadata;
119
- metadata: string | Uint8ClampedArray | ArrayBuffer | Uint8Array | Int8Array;
120
- success: boolean;
121
- errorMessage: any;
122
- sdr: any;
123
- gainMap: any;
124
- }>;
2
+ export * from './libultrahdr/decode-jpeg-metadata';
3
+ export * from './libultrahdr/encode-jpeg-metadata';
4
+ export * from './libultrahdr/library';
@@ -1,11 +1,8 @@
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
- import { FileLoader } from 'three';
7
- import { L as LoaderBase } from './LoaderBase-aa4842c4.js';
8
-
9
6
  var Module = (() => {
10
7
  var _scriptDir = import.meta.url;
11
8
 
@@ -6503,80 +6500,6 @@ run();
6503
6500
  })();
6504
6501
  var libultrahdr = Module;
6505
6502
 
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
6503
  // @ts-expect-error untyped
6581
6504
  let library;
6582
6505
  /**
@@ -6592,92 +6515,7 @@ const getLibrary = async () => {
6592
6515
  }
6593
6516
  return library;
6594
6517
  };
6595
- /**
6596
- * Encapsulates a Gainmap into a single JPEG file (aka: JPEG-R) with the base map
6597
- * as the sdr visualization and the gainMap encoded into a MPF (Multi-Picture Format) tag.
6598
- *
6599
- * @category Encoding
6600
- * @group Encoding
6601
- *
6602
- * @example
6603
- * import { compress, encode, findTextureMinMax } from '@monogrid/gainmap-js'
6604
- * import { encodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
6605
- * import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
6606
- *
6607
- * // load an HDR file
6608
- * const loader = new EXRLoader()
6609
- * const image = await loader.loadAsync('image.exr')
6610
- *
6611
- * // find RAW RGB Max value of a texture
6612
- * const textureMax = await findTextureMinMax(image)
6613
- *
6614
- * // Encode the gainmap
6615
- * const encodingResult = encode({
6616
- * image,
6617
- * maxContentBoost: Math.max.apply(this, textureMax)
6618
- * })
6619
- *
6620
- * // obtain the RAW RGBA SDR buffer and create an ImageData
6621
- * const sdrImageData = new ImageData(
6622
- * encodingResult.sdr.toArray(),
6623
- * encodingResult.sdr.width,
6624
- * encodingResult.sdr.height
6625
- * )
6626
- * // obtain the RAW RGBA Gain map buffer and create an ImageData
6627
- * const gainMapImageData = new ImageData(
6628
- * encodingResult.gainMap.toArray(),
6629
- * encodingResult.gainMap.width,
6630
- * encodingResult.gainMap.height
6631
- * )
6632
- *
6633
- * // parallel compress the RAW buffers into the specified mimeType
6634
- * const mimeType = 'image/jpeg'
6635
- * const quality = 0.9
6636
- *
6637
- * const [sdr, gainMap] = await Promise.all([
6638
- * compress({
6639
- * source: sdrImageData,
6640
- * mimeType,
6641
- * quality,
6642
- * flipY: true // output needs to be flipped
6643
- * }),
6644
- * compress({
6645
- * source: gainMapImageData,
6646
- * mimeType,
6647
- * quality,
6648
- * flipY: true // output needs to be flipped
6649
- * })
6650
- * ])
6651
- *
6652
- * // obtain the metadata which will be embedded into
6653
- * // and XMP tag inside the final JPEG file
6654
- * const metadata = encodingResult.getMetadata()
6655
- *
6656
- * // embed the compressed images + metadata into a single
6657
- * // JPEG file
6658
- * const jpeg = await encodeJPEGMetadata({
6659
- * ...encodingResult,
6660
- * ...metadata,
6661
- * sdr,
6662
- * gainMap
6663
- * })
6664
- *
6665
- * // `jpeg` will be an `Uint8Array` which can be saved somewhere
6666
- *
6667
- *
6668
- * @param encodingResult
6669
- * @returns an Uint8Array representing a JPEG-R file
6670
- * @throws {Error} If `encodingResult.sdr.mimeType !== 'image/jpeg'`
6671
- * @throws {Error} If `encodingResult.gainMap.mimeType !== 'image/jpeg'`
6672
- */
6673
- const encodeJPEGMetadata = async (encodingResult) => {
6674
- const lib = await getLibrary();
6675
- if (encodingResult.sdr.mimeType !== 'image/jpeg')
6676
- throw new Error('This function expects an SDR image compressed in jpeg');
6677
- if (encodingResult.gainMap.mimeType !== 'image/jpeg')
6678
- throw new Error('This function expects a GainMap image compressed in jpeg');
6679
- 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
- };
6518
+
6681
6519
  /**
6682
6520
  *
6683
6521
  * @param description
@@ -6738,8 +6576,8 @@ const decodeJPEGMetadata = async (file) => {
6738
6576
  if (!result.success)
6739
6577
  throw new Error(result.errorMessage);
6740
6578
  const parser = new DOMParser();
6741
- const xmldocuemnt = parser.parseFromString(result.metadata, 'text/xml');
6742
- const description = xmldocuemnt.getElementsByTagName('rdf:Description')[0];
6579
+ const xmlDocument = parser.parseFromString(result.metadata, 'text/xml');
6580
+ const description = xmlDocument.getElementsByTagName('rdf:Description')[0];
6743
6581
  const gainMapMin = getAttribute(description, 'hdrgm:GainMapMin', '0');
6744
6582
  const gainMapMax = getAttribute(description, 'hdrgm:GainMapMax');
6745
6583
  const gamma = getAttribute(description, 'hdrgm:Gamma', '1');
@@ -6767,4 +6605,91 @@ const decodeJPEGMetadata = async (file) => {
6767
6605
  parsedMetadata });
6768
6606
  };
6769
6607
 
6770
- export { JPEGRLoader, decodeJPEGMetadata, encodeJPEGMetadata, getLibrary };
6608
+ /**
6609
+ * Encapsulates a Gainmap into a single JPEG file (aka: JPEG-R) with the base map
6610
+ * as the sdr visualization and the gainMap encoded into a MPF (Multi-Picture Format) tag.
6611
+ *
6612
+ * @category Encoding
6613
+ * @group Encoding
6614
+ *
6615
+ * @example
6616
+ * import { compress, encode, findTextureMinMax } from '@monogrid/gainmap-js'
6617
+ * import { encodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
6618
+ * import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
6619
+ *
6620
+ * // load an HDR file
6621
+ * const loader = new EXRLoader()
6622
+ * const image = await loader.loadAsync('image.exr')
6623
+ *
6624
+ * // find RAW RGB Max value of a texture
6625
+ * const textureMax = await findTextureMinMax(image)
6626
+ *
6627
+ * // Encode the gainmap
6628
+ * const encodingResult = encode({
6629
+ * image,
6630
+ * maxContentBoost: Math.max.apply(this, textureMax)
6631
+ * })
6632
+ *
6633
+ * // obtain the RAW RGBA SDR buffer and create an ImageData
6634
+ * const sdrImageData = new ImageData(
6635
+ * encodingResult.sdr.toArray(),
6636
+ * encodingResult.sdr.width,
6637
+ * encodingResult.sdr.height
6638
+ * )
6639
+ * // obtain the RAW RGBA Gain map buffer and create an ImageData
6640
+ * const gainMapImageData = new ImageData(
6641
+ * encodingResult.gainMap.toArray(),
6642
+ * encodingResult.gainMap.width,
6643
+ * encodingResult.gainMap.height
6644
+ * )
6645
+ *
6646
+ * // parallel compress the RAW buffers into the specified mimeType
6647
+ * const mimeType = 'image/jpeg'
6648
+ * const quality = 0.9
6649
+ *
6650
+ * const [sdr, gainMap] = await Promise.all([
6651
+ * compress({
6652
+ * source: sdrImageData,
6653
+ * mimeType,
6654
+ * quality,
6655
+ * flipY: true // output needs to be flipped
6656
+ * }),
6657
+ * compress({
6658
+ * source: gainMapImageData,
6659
+ * mimeType,
6660
+ * quality,
6661
+ * flipY: true // output needs to be flipped
6662
+ * })
6663
+ * ])
6664
+ *
6665
+ * // obtain the metadata which will be embedded into
6666
+ * // and XMP tag inside the final JPEG file
6667
+ * const metadata = encodingResult.getMetadata()
6668
+ *
6669
+ * // embed the compressed images + metadata into a single
6670
+ * // JPEG file
6671
+ * const jpeg = await encodeJPEGMetadata({
6672
+ * ...encodingResult,
6673
+ * ...metadata,
6674
+ * sdr,
6675
+ * gainMap
6676
+ * })
6677
+ *
6678
+ * // `jpeg` will be an `Uint8Array` which can be saved somewhere
6679
+ *
6680
+ *
6681
+ * @param encodingResult
6682
+ * @returns an Uint8Array representing a JPEG-R file
6683
+ * @throws {Error} If `encodingResult.sdr.mimeType !== 'image/jpeg'`
6684
+ * @throws {Error} If `encodingResult.gainMap.mimeType !== 'image/jpeg'`
6685
+ */
6686
+ const encodeJPEGMetadata = async (encodingResult) => {
6687
+ const lib = await getLibrary();
6688
+ if (encodingResult.sdr.mimeType !== 'image/jpeg')
6689
+ throw new Error('This function expects an SDR image compressed in jpeg');
6690
+ if (encodingResult.gainMap.mimeType !== 'image/jpeg')
6691
+ throw new Error('This function expects a GainMap image compressed in jpeg');
6692
+ 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);
6693
+ };
6694
+
6695
+ export { decodeJPEGMetadata, encodeJPEGMetadata, getLibrary };