@monogrid/gainmap-js 1.0.2

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 (61) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/CODE_OF_CONDUCT.md +128 -0
  3. package/CONTRIBUTING.md +130 -0
  4. package/LICENSE +21 -0
  5. package/README.md +149 -0
  6. package/dist/_commonjsHelpers-1789f0cf.js +12 -0
  7. package/dist/compress-a4b59cab.js +82 -0
  8. package/dist/decode.d.ts +37 -0
  9. package/dist/encode-utils/compress.d.ts +12 -0
  10. package/dist/encode-utils/find-texture-min-max.d.ts +15 -0
  11. package/dist/encode-utils/get-gainmap.d.ts +13 -0
  12. package/dist/encode-utils/get-sdr-rendition.d.ts +16 -0
  13. package/dist/encode.d.ts +117 -0
  14. package/dist/index.d.ts +11 -0
  15. package/dist/index.js +976 -0
  16. package/dist/index.umd.js +1069 -0
  17. package/dist/libultrahdr-esm.wasm +0 -0
  18. package/dist/libultrahdr.d.ts +129 -0
  19. package/dist/libultrahdr.js +6728 -0
  20. package/dist/libultrahdr.umd.js +6740 -0
  21. package/dist/materials/GainMapDecoderMaterial.d.ts +26 -0
  22. package/dist/materials/GainMapEncoderMaterial.d.ts +43 -0
  23. package/dist/materials/SDRMaterial.d.ts +35 -0
  24. package/dist/materials/index.d.ts +3 -0
  25. package/dist/types.d.ts +252 -0
  26. package/dist/utils/QuadRenderer.d.ts +73 -0
  27. package/dist/utils/get-data-texture.d.ts +14 -0
  28. package/dist/worker-interface.d.ts +26 -0
  29. package/dist/worker-interface.js +501 -0
  30. package/dist/worker-interface.umd.js +516 -0
  31. package/dist/worker-types.d.ts +24 -0
  32. package/dist/worker.d.ts +1 -0
  33. package/dist/worker.js +123 -0
  34. package/dist/worker.umd.js +207 -0
  35. package/examples/README.md +3 -0
  36. package/examples/compress.ts +43 -0
  37. package/examples/decode-from-jpeg.ts +40 -0
  38. package/examples/decode-from-separate-data.ts +41 -0
  39. package/examples/decode-to-datatexture.ts +65 -0
  40. package/examples/encode-and-compress.ts +29 -0
  41. package/examples/encode-jpeg-metadata.ts +57 -0
  42. package/examples/encode.ts +28 -0
  43. package/examples/globals.d.ts +6 -0
  44. package/examples/worker.ts +41 -0
  45. package/libultrahdr-wasm/LICENSE +202 -0
  46. package/libultrahdr-wasm/README.md +99 -0
  47. package/libultrahdr-wasm/build/libultrahdr-debug.js +7156 -0
  48. package/libultrahdr-wasm/build/libultrahdr-debug.wasm +0 -0
  49. package/libultrahdr-wasm/build/libultrahdr-esm.js +6633 -0
  50. package/libultrahdr-wasm/build/libultrahdr-esm.wasm +0 -0
  51. package/libultrahdr-wasm/build/libultrahdr.d.ts +30 -0
  52. package/libultrahdr-wasm/build/libultrahdr.js +6591 -0
  53. package/libultrahdr-wasm/build/libultrahdr.wasm +0 -0
  54. package/package.json +143 -0
  55. package/reports/eslint-examples.json +1 -0
  56. package/reports/eslint-src.json +1 -0
  57. package/reports/eslint-tests.json +1 -0
  58. package/reports/junit.xml +35 -0
  59. package/reports/typecheck-examples.log +0 -0
  60. package/reports/typecheck-tests.log +0 -0
  61. package/reports/typecheck.log +0 -0
@@ -0,0 +1,207 @@
1
+ /**
2
+ * @monogrid/gainmap-js v1.0.1
3
+ * With ❤️, by MONOGRID <hello@mono-grid.com>
4
+ */
5
+
6
+ (function (factory) {
7
+ typeof define === 'function' && define.amd ? define(factory) :
8
+ factory();
9
+ })((function () { 'use strict';
10
+
11
+ function getDefaultExportFromCjs (x) {
12
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
13
+ }
14
+
15
+ var isPromise$2 = {exports: {}};
16
+
17
+ isPromise$2.exports = isPromise$1;
18
+ isPromise$2.exports.default = isPromise$1;
19
+
20
+ function isPromise$1(obj) {
21
+ return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
22
+ }
23
+
24
+ var isPromiseExports = isPromise$2.exports;
25
+
26
+ var isPromise = isPromiseExports;
27
+
28
+ function registerPromiseWorker(callback) {
29
+
30
+ function postOutgoingMessage(e, messageId, error, result) {
31
+ function postMessage(msg, transferList) {
32
+ /* istanbul ignore if */
33
+ if (typeof self.postMessage !== 'function') { // service worker
34
+ e.ports[0].postMessage(msg, transferList);
35
+ } else { // web worker
36
+ self.postMessage(msg, transferList);
37
+ }
38
+ }
39
+ if (error) {
40
+ /* istanbul ignore else */
41
+ if (typeof console !== 'undefined' && 'error' in console) {
42
+ // This is to make errors easier to debug. I think it's important
43
+ // enough to just leave here without giving the user an option
44
+ // to silence it.
45
+ console.error('Worker caught an error:', error);
46
+ }
47
+ postMessage([messageId, {
48
+ message: error.message
49
+ }]);
50
+ } else {
51
+ if (result instanceof MessageWithTransferList) {
52
+ postMessage([messageId, null, result.message], result.transferList);
53
+ } else {
54
+ postMessage([messageId, null, result]);
55
+ }
56
+ }
57
+ }
58
+
59
+ function tryCatchFunc(callback, message) {
60
+ try {
61
+ return {res: callback(message, withTransferList)};
62
+ } catch (e) {
63
+ return {err: e};
64
+ }
65
+ }
66
+
67
+ function withTransferList(resMessage, transferList) {
68
+ return new MessageWithTransferList(resMessage, transferList);
69
+ }
70
+
71
+ function handleIncomingMessage(e, callback, messageId, message) {
72
+
73
+ var result = tryCatchFunc(callback, message);
74
+
75
+ if (result.err) {
76
+ postOutgoingMessage(e, messageId, result.err);
77
+ } else if (!isPromise(result.res)) {
78
+ postOutgoingMessage(e, messageId, null, result.res);
79
+ } else {
80
+ result.res.then(function (finalResult) {
81
+ postOutgoingMessage(e, messageId, null, finalResult);
82
+ }, function (finalError) {
83
+ postOutgoingMessage(e, messageId, finalError);
84
+ });
85
+ }
86
+ }
87
+
88
+ function onIncomingMessage(e) {
89
+ var payload = e.data;
90
+ if (!Array.isArray(payload) || payload.length !== 2) {
91
+ // message doens't match communication format; ignore
92
+ return;
93
+ }
94
+ var messageId = payload[0];
95
+ var message = payload[1];
96
+
97
+ if (typeof callback !== 'function') {
98
+ postOutgoingMessage(e, messageId, new Error(
99
+ 'Please pass a function into register().'));
100
+ } else {
101
+ handleIncomingMessage(e, callback, messageId, message);
102
+ }
103
+ }
104
+
105
+ function MessageWithTransferList(message, transferList) {
106
+ this.message = message;
107
+ this.transferList = transferList;
108
+ }
109
+
110
+ self.addEventListener('message', onIncomingMessage);
111
+ }
112
+
113
+ var register = registerPromiseWorker;
114
+
115
+ var registerPromiseWorker$1 = /*@__PURE__*/getDefaultExportFromCjs(register);
116
+
117
+ /**
118
+ * Used internally
119
+ *
120
+ * @internal
121
+ * @param canvas
122
+ * @param mimeType
123
+ * @param quality
124
+ * @returns
125
+ */
126
+ const canvasToBlob = async (canvas, mimeType, quality) => {
127
+ if (canvas instanceof OffscreenCanvas) {
128
+ return canvas.convertToBlob({ type: mimeType, quality: quality || 0.9 });
129
+ }
130
+ return new Promise((resolve, reject) => {
131
+ canvas.toBlob((res) => {
132
+ if (res)
133
+ resolve(res);
134
+ else
135
+ reject('Failed to convert canvas to blob');
136
+ }, mimeType, quality || 0.9);
137
+ });
138
+ };
139
+ /**
140
+ * Converts a RAW RGBA image buffer into the provided `mimeType` using the provided `quality`
141
+ *
142
+ * @category Compression
143
+ * @group Compression
144
+ * @param params
145
+ * @throws {Error} if the browser does not support [createImageBitmap](https://caniuse.com/createimagebitmap)
146
+ * @throws {Error} if the provided source image cannot be decoded
147
+ * @throws {Error} if the function fails to create a canvas context
148
+ */
149
+ const compress = async (params) => {
150
+ if (typeof createImageBitmap === 'undefined')
151
+ throw new Error('createImageBitmap() not supported.');
152
+ const { source, mimeType, quality, flipY } = params;
153
+ // eslint-disable-next-line no-undef
154
+ let imageBitmapSource;
155
+ if ((source instanceof Uint8Array || source instanceof Uint8ClampedArray) && 'sourceMimeType' in params) {
156
+ imageBitmapSource = new Blob([source], { type: params.sourceMimeType });
157
+ }
158
+ else if (source instanceof ImageData) {
159
+ imageBitmapSource = source;
160
+ }
161
+ else {
162
+ throw new Error('Invalid source image');
163
+ }
164
+ const img = await createImageBitmap(imageBitmapSource);
165
+ const width = img.width;
166
+ const height = img.height;
167
+ let canvas;
168
+ if (typeof OffscreenCanvas !== 'undefined') {
169
+ canvas = new OffscreenCanvas(width, height);
170
+ }
171
+ else {
172
+ canvas = document.createElement('canvas');
173
+ }
174
+ const ctx = canvas.getContext('2d');
175
+ if (!ctx)
176
+ throw new Error('Failed to create canvas Context');
177
+ // flip Y
178
+ if (flipY === true) {
179
+ ctx.translate(0, height);
180
+ ctx.scale(1, -1);
181
+ }
182
+ ctx.drawImage(img, 0, 0, width, height);
183
+ const blob = await canvasToBlob(canvas, mimeType, quality || 0.9);
184
+ const data = new Uint8Array(await blob.arrayBuffer());
185
+ return {
186
+ data,
187
+ mimeType,
188
+ width,
189
+ height
190
+ };
191
+ };
192
+
193
+ // @ts-expect-error untyped lib
194
+ const _compress = async (message, withTransferList) => {
195
+ const result = await compress(message.payload);
196
+ return withTransferList(Object.assign(Object.assign({}, result), { source: message.payload.source instanceof ImageData ? message.payload.source.data : new Uint8ClampedArray(message.payload.source) }), [result.data.buffer, message.payload.source instanceof ImageData ? message.payload.source.data.buffer : message.payload.source.buffer]);
197
+ };
198
+ registerPromiseWorker$1(async (message, withTransferList) => {
199
+ switch (message.type) {
200
+ // case 'encode-gainmap-buffers':
201
+ // return encodeGainmapBuffers(message, withTransferList)
202
+ case 'compress':
203
+ return _compress(message, withTransferList);
204
+ }
205
+ });
206
+
207
+ }));
@@ -0,0 +1,3 @@
1
+ Examples of use cases using this library
2
+
3
+ Kept here for linting & typechecking purposes of the final use cases
@@ -0,0 +1,43 @@
1
+ /* eslint-disable unused-imports/no-unused-vars */
2
+ import { compress, encode, findTextureMinMax } from '@monogrid/gainmap-js'
3
+ import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
4
+
5
+ // load an HDR file
6
+ const loader = new EXRLoader()
7
+ const image = await loader.loadAsync('image.exr')
8
+
9
+ // find RAW RGB Max value of a texture
10
+ const textureMax = await findTextureMinMax(image)
11
+
12
+ // Encode the gainmap
13
+ const encodingResult = encode({
14
+ image,
15
+ // this will encode the full HDR range
16
+ maxContentBoost: Math.max.apply(this, textureMax)
17
+ })
18
+
19
+ // obtain the RAW RGBA SDR buffer and create an ImageData
20
+ const sdrImageData = new ImageData(encodingResult.sdr.toArray(), encodingResult.sdr.width, encodingResult.sdr.height)
21
+ // obtain the RAW RGBA Gain map buffer and create an ImageData
22
+ const gainMapImageData = new ImageData(encodingResult.gainMap.toArray(), encodingResult.gainMap.width, encodingResult.gainMap.height)
23
+
24
+ // parallel compress the RAW buffers into the specified mimeType
25
+ const mimeType = 'image/jpeg'
26
+ const quality = 0.9
27
+ const [sdr, gainMap] = await Promise.all([
28
+ compress({
29
+ source: sdrImageData,
30
+ mimeType,
31
+ quality,
32
+ flipY: true // output needs to be flipped
33
+ }),
34
+ compress({
35
+ source: gainMapImageData,
36
+ mimeType,
37
+ quality,
38
+ flipY: true // output needs to be flipped
39
+ })
40
+ ])
41
+
42
+ // `sdr` will contain a JPEG which can be saved somewhere
43
+ // `gainMap` will contain a JPEG which can be saved somewhere
@@ -0,0 +1,40 @@
1
+ /* eslint-disable unused-imports/no-unused-vars */
2
+
3
+ import { decode } from '@monogrid/gainmap-js'
4
+ import { decodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
5
+ import {
6
+ Mesh,
7
+ MeshBasicMaterial,
8
+ PerspectiveCamera,
9
+ PlaneGeometry,
10
+ Scene,
11
+ WebGLRenderer
12
+ } from 'three'
13
+
14
+ const renderer = new WebGLRenderer()
15
+
16
+ // fetch a JPEG image containing a gainmap as ArrayBuffer
17
+ const gainmap = await (await fetch('gainmap.jpeg')).arrayBuffer()
18
+
19
+ // extract data from the JPEG
20
+ const { sdr, gainMap, parsedMetadata } = await decodeJPEGMetadata(new Uint8Array(gainmap))
21
+
22
+ // restore the HDR texture
23
+ const result = await decode({
24
+ sdr,
25
+ gainMap,
26
+ // this allows to use `result.renderTarget.texture` directly
27
+ renderer,
28
+ // this will restore the full HDR range
29
+ maxDisplayBoost: Math.pow(2, parsedMetadata.hdrCapacityMax),
30
+ ...parsedMetadata
31
+ })
32
+
33
+ const scene = new Scene()
34
+ // `result` can be used to populate a Texture
35
+ const mesh = new Mesh(
36
+ new PlaneGeometry(),
37
+ new MeshBasicMaterial({ map: result.renderTarget.texture })
38
+ )
39
+ scene.add(mesh)
40
+ renderer.render(scene, new PerspectiveCamera())
@@ -0,0 +1,41 @@
1
+ /* eslint-disable unused-imports/no-unused-vars */
2
+ import { decode } from '@monogrid/gainmap-js'
3
+ import {
4
+ Mesh,
5
+ MeshBasicMaterial,
6
+ PerspectiveCamera,
7
+ PlaneGeometry,
8
+ Scene,
9
+ TextureLoader,
10
+ WebGLRenderer
11
+ } from 'three'
12
+
13
+ const renderer = new WebGLRenderer()
14
+
15
+ const loader = new TextureLoader()
16
+
17
+ // load SDR Representation
18
+ const sdr = await loader.loadAsync('sdr.jpg')
19
+ // load Gain map recovery image
20
+ const gainMap = await loader.loadAsync('gainmap.jpg')
21
+ // load metadata
22
+ const metadata = await (await fetch('metadata.json')).json()
23
+
24
+ const result = await decode({
25
+ sdr,
26
+ gainMap,
27
+ // this allows to use `result.renderTarget.texture` directly
28
+ renderer,
29
+ // this will restore the full HDR range
30
+ maxDisplayBoost: Math.pow(2, metadata.hdrCapacityMax),
31
+ ...metadata
32
+ })
33
+
34
+ const scene = new Scene()
35
+ // `result` can be used to populate a Texture
36
+ const mesh = new Mesh(
37
+ new PlaneGeometry(),
38
+ new MeshBasicMaterial({ map: result.renderTarget.texture })
39
+ )
40
+ scene.add(mesh)
41
+ renderer.render(scene, new PerspectiveCamera())
@@ -0,0 +1,65 @@
1
+ /* eslint-disable unused-imports/no-unused-vars */
2
+
3
+ import { decode } from '@monogrid/gainmap-js'
4
+ import { decodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
5
+ import {
6
+ DataTexture,
7
+ HalfFloatType,
8
+ LinearFilter,
9
+ LinearMipMapLinearFilter,
10
+ Mesh,
11
+ MeshBasicMaterial,
12
+ NoColorSpace,
13
+ PerspectiveCamera,
14
+ PlaneGeometry,
15
+ RepeatWrapping,
16
+ RGBAFormat,
17
+ Scene,
18
+ UVMapping,
19
+ WebGLRenderer
20
+ } from 'three'
21
+
22
+ // fetch a JPEG image containing a gainmap as ArrayBuffer
23
+ const gainmap = await (await fetch('gainmap.jpeg')).arrayBuffer()
24
+
25
+ // extract data from the JPEG
26
+ const { sdr, gainMap, parsedMetadata } = await decodeJPEGMetadata(new Uint8Array(gainmap))
27
+
28
+ // restore the HDR texture
29
+ // !!!
30
+ // NOTICE THE ABSCENCE
31
+ // OF THE `renderer`parameter
32
+ // !!!
33
+ const result = await decode({
34
+ sdr,
35
+ gainMap,
36
+ // this will restore the full HDR range
37
+ maxDisplayBoost: Math.pow(2, parsedMetadata.hdrCapacityMax),
38
+ ...parsedMetadata
39
+ })
40
+
41
+ // create the datatexture from the result array
42
+ const map = new DataTexture(
43
+ result.toArray(), // get data
44
+ result.width, // width is provided
45
+ result.height, // height is provided
46
+ RGBAFormat, // must be RGBAFormat
47
+ HalfFloatType, // must be HalfFloatType
48
+ UVMapping,
49
+ RepeatWrapping,
50
+ RepeatWrapping,
51
+ LinearFilter,
52
+ LinearMipMapLinearFilter,
53
+ 1,
54
+ NoColorSpace // must be NoColorSpace
55
+ )
56
+
57
+ const scene = new Scene()
58
+ const mesh = new Mesh(
59
+ new PlaneGeometry(),
60
+ new MeshBasicMaterial({ map })
61
+ )
62
+ scene.add(mesh)
63
+
64
+ const renderer = new WebGLRenderer()
65
+ renderer.render(scene, new PerspectiveCamera())
@@ -0,0 +1,29 @@
1
+ /* eslint-disable unused-imports/no-unused-vars */
2
+ import { encodeAndCompress, findTextureMinMax } from '@monogrid/gainmap-js'
3
+ import { encodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
4
+ import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
5
+
6
+ // load an HDR file
7
+ const loader = new EXRLoader()
8
+ const image = await loader.loadAsync('image.exr')
9
+
10
+ // find RAW RGB Max value of a texture
11
+ const textureMax = await findTextureMinMax(image)
12
+
13
+ // Encode the gainmap
14
+ const encodingResult = await encodeAndCompress({
15
+ image,
16
+ // this will encode the full HDR range
17
+ maxContentBoost: Math.max.apply(this, textureMax),
18
+ mimeType: 'image/jpeg'
19
+ })
20
+
21
+ // embed the compressed images + metadata into a single
22
+ // JPEG file
23
+ const jpeg = await encodeJPEGMetadata({
24
+ ...encodingResult,
25
+ sdr: encodingResult.sdr,
26
+ gainMap: encodingResult.gainMap
27
+ })
28
+
29
+ // `jpeg` will be an `Uint8Array` which can be saved somewhere
@@ -0,0 +1,57 @@
1
+ /* eslint-disable unused-imports/no-unused-vars */
2
+ import { compress, encode, findTextureMinMax } from '@monogrid/gainmap-js'
3
+ import { encodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
4
+ import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
5
+
6
+ // load an HDR file
7
+ const loader = new EXRLoader()
8
+ const image = await loader.loadAsync('image.exr')
9
+
10
+ // find RAW RGB Max value of a texture
11
+ const textureMax = await findTextureMinMax(image)
12
+
13
+ // Encode the gainmap
14
+ const encodingResult = encode({
15
+ image,
16
+ // this will encode the full HDR range
17
+ maxContentBoost: Math.max.apply(this, textureMax)
18
+ })
19
+
20
+ // obtain the RAW RGBA SDR buffer and create an ImageData
21
+ const sdrImageData = new ImageData(encodingResult.sdr.toArray(), encodingResult.sdr.width, encodingResult.sdr.height)
22
+ // obtain the RAW RGBA Gain map buffer and create an ImageData
23
+ const gainMapImageData = new ImageData(encodingResult.gainMap.toArray(), encodingResult.gainMap.width, encodingResult.gainMap.height)
24
+
25
+ // parallel compress the RAW buffers into the specified mimeType
26
+ const mimeType = 'image/jpeg'
27
+ const quality = 0.9
28
+
29
+ const [sdr, gainMap] = await Promise.all([
30
+ compress({
31
+ source: sdrImageData,
32
+ mimeType,
33
+ quality,
34
+ flipY: true // output needs to be flipped
35
+ }),
36
+ compress({
37
+ source: gainMapImageData,
38
+ mimeType,
39
+ quality,
40
+ flipY: true // output needs to be flipped
41
+ })
42
+ ])
43
+
44
+ // obtain the metadata which will be embedded into
45
+ // and XMP tag inside the final JPEG file
46
+ const metadata = encodingResult.getMetadata()
47
+
48
+ // embed the compressed images + metadata into a single
49
+ // JPEG file
50
+ const jpeg = await encodeJPEGMetadata({
51
+ ...encodingResult,
52
+ ...metadata,
53
+ sdr,
54
+ gainMap
55
+ })
56
+
57
+ // `jpeg` will be an `Uint8Array` which can be saved somewhere
@@ -0,0 +1,28 @@
1
+ /* eslint-disable unused-imports/no-unused-vars */
2
+ import { encode, findTextureMinMax } from '@monogrid/gainmap-js'
3
+ import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
4
+
5
+ // load an HDR file
6
+ const loader = new EXRLoader()
7
+ const image = await loader.loadAsync('image.exr')
8
+
9
+ // find RAW RGB Max value of a texture
10
+ const textureMax = await findTextureMinMax(image)
11
+
12
+ // Encode the gainmap
13
+ const encodingResult = encode({
14
+ image,
15
+ // this will encode the full HDR range
16
+ maxContentBoost: Math.max.apply(this, textureMax)
17
+ })
18
+
19
+ // can be re-encoded after changing parameters
20
+ encodingResult.sdr.material.exposure = 0.9
21
+ encodingResult.sdr.render()
22
+ // or
23
+ encodingResult.gainMap.material.gamma = [1.1, 1.1, 1.1]
24
+ encodingResult.gainMap.render()
25
+
26
+ // must be manually disposed
27
+ encodingResult.sdr.dispose()
28
+ encodingResult.gainMap.dispose()
@@ -0,0 +1,6 @@
1
+ declare module '*?worker' {
2
+ const workerConstructor: {
3
+ new (): Worker
4
+ }
5
+ export default workerConstructor
6
+ }
@@ -0,0 +1,41 @@
1
+ /* eslint-disable unused-imports/no-unused-vars */
2
+ import { encodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
3
+ // this assumes a vite-like bundler understands the `?worker` import
4
+ import GainMapWorker from '@monogrid/gainmap-js/worker?worker'
5
+ import { getPromiseWorker, getWorkerInterface } from '@monogrid/gainmap-js/worker-interface'
6
+ import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
7
+
8
+ import { encodeAndCompress } from '../dist/encode'
9
+ import { findTextureMinMax } from '../dist/encode-utils/find-texture-min-max'
10
+
11
+ // turn our Worker into a PromiseWorker
12
+ const promiseWorker = getPromiseWorker(new GainMapWorker())
13
+ // get the interface
14
+ const workerInterface = getWorkerInterface(promiseWorker)
15
+
16
+ // load an HDR file
17
+ const loader = new EXRLoader()
18
+ const image = await loader.loadAsync('image.exr')
19
+
20
+ // find RAW RGB Max value of a texture
21
+ const textureMax = await findTextureMinMax(image)
22
+
23
+ // Encode the gainmap
24
+ const encodingResult = await encodeAndCompress({
25
+ image,
26
+ // this will encode the full HDR range
27
+ maxContentBoost: Math.max.apply(this, textureMax),
28
+ // use our worker for compressing the image
29
+ withWorker: workerInterface,
30
+ mimeType: 'image/jpeg'
31
+ })
32
+
33
+ // embed the compressed images + metadata into a single
34
+ // JPEG file
35
+ const jpeg = await encodeJPEGMetadata({
36
+ ...encodingResult,
37
+ sdr: encodingResult.sdr,
38
+ gainMap: encodingResult.gainMap
39
+ })
40
+
41
+ // `jpeg` will be an `Uint8Array` which can be saved somewhere