@monogrid/gainmap-js 1.0.2 → 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.
Files changed (54) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +103 -19
  3. package/dist/LoaderBase-eee19701.js +382 -0
  4. package/dist/_commonjsHelpers-1789f0cf.js +1 -1
  5. package/dist/compress-a4b59cab.js +1 -1
  6. package/dist/decode.d.ts +25 -6
  7. package/dist/encode-utils/get-sdr-rendition.d.ts +2 -2
  8. package/dist/encode.d.ts +3 -3
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.js +189 -258
  11. package/dist/index.umd.js +331 -27
  12. package/dist/libultrahdr/decode-jpeg-metadata.d.ts +31 -0
  13. package/dist/libultrahdr/encode-jpeg-metadata.d.ts +83 -0
  14. package/dist/libultrahdr/library.d.ts +9 -0
  15. package/dist/libultrahdr-esm.wasm +0 -0
  16. package/dist/libultrahdr.d.ts +4 -128
  17. package/dist/libultrahdr.js +322 -278
  18. package/dist/libultrahdr.umd.js +687 -271
  19. package/dist/loaders/GainMapLoader.d.ts +68 -0
  20. package/dist/loaders/JPEGRLoader.d.ts +60 -0
  21. package/dist/loaders/LoaderBase.d.ts +27 -0
  22. package/dist/materials/GainMapDecoderMaterial.d.ts +44 -1
  23. package/dist/materials/GainMapEncoderMaterial.d.ts +29 -2
  24. package/dist/types.d.ts +26 -6
  25. package/dist/utils/QuadRenderer.d.ts +8 -1
  26. package/dist/worker-interface.js +1 -1
  27. package/dist/worker-interface.umd.js +1 -1
  28. package/dist/worker-types.d.ts +1 -1
  29. package/dist/worker.js +1 -1
  30. package/dist/worker.umd.js +1 -1
  31. package/libultrahdr-wasm/build/libultrahdr-debug.js +156 -182
  32. package/libultrahdr-wasm/build/libultrahdr-debug.wasm +0 -0
  33. package/libultrahdr-wasm/build/libultrahdr-esm.js +153 -182
  34. package/libultrahdr-wasm/build/libultrahdr-esm.wasm +0 -0
  35. package/libultrahdr-wasm/build/libultrahdr.js +151 -180
  36. package/libultrahdr-wasm/build/libultrahdr.wasm +0 -0
  37. package/package.json +1 -1
  38. package/examples/README.md +0 -3
  39. package/examples/compress.ts +0 -43
  40. package/examples/decode-from-jpeg.ts +0 -40
  41. package/examples/decode-from-separate-data.ts +0 -41
  42. package/examples/decode-to-datatexture.ts +0 -65
  43. package/examples/encode-and-compress.ts +0 -29
  44. package/examples/encode-jpeg-metadata.ts +0 -57
  45. package/examples/encode.ts +0 -28
  46. package/examples/globals.d.ts +0 -6
  47. package/examples/worker.ts +0 -41
  48. package/reports/eslint-examples.json +0 -1
  49. package/reports/eslint-src.json +0 -1
  50. package/reports/eslint-tests.json +0 -1
  51. package/reports/junit.xml +0 -35
  52. package/reports/typecheck-examples.log +0 -0
  53. package/reports/typecheck-tests.log +0 -0
  54. package/reports/typecheck.log +0 -0
package/dist/index.umd.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @monogrid/gainmap-js v1.0.1
2
+ * @monogrid/gainmap-js v1.1.0
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
@@ -58,10 +58,10 @@ void main() {
58
58
  sdr: { value: sdr },
59
59
  gainMap: { value: gainMap },
60
60
  gamma: { value: new three.Vector3(1.0 / gamma[0], 1.0 / gamma[1], 1.0 / gamma[2]) },
61
- offsetHdr: { value: new three.Vector3(offsetHdr[0], offsetHdr[1], offsetHdr[2]) },
62
- offsetSdr: { value: new three.Vector3(offsetSdr[0], offsetSdr[1], offsetSdr[2]) },
63
- gainMapMin: { value: new three.Vector3(gainMapMin[0], gainMapMin[1], gainMapMin[2]) },
64
- gainMapMax: { value: new three.Vector3(gainMapMax[0], gainMapMax[1], gainMapMax[2]) },
61
+ offsetHdr: { value: new three.Vector3().fromArray(offsetHdr) },
62
+ offsetSdr: { value: new three.Vector3().fromArray(offsetSdr) },
63
+ gainMapMin: { value: new three.Vector3().fromArray(gainMapMin) },
64
+ gainMapMax: { value: new three.Vector3().fromArray(gainMapMax) },
65
65
  weightFactor: {
66
66
  value: (Math.log2(maxDisplayBoost) - hdrCapacityMin) / (hdrCapacityMax - hdrCapacityMin)
67
67
  }
@@ -76,12 +76,71 @@ void main() {
76
76
  this.needsUpdate = true;
77
77
  this.uniformsNeedUpdate = true;
78
78
  }
79
+ get sdr() { return this.uniforms.sdr.value; }
80
+ set sdr(value) { this.uniforms.sdr.value = value; }
81
+ get gainMap() { return this.uniforms.gainMap.value; }
82
+ set gainMap(value) { this.uniforms.gainMap.value = value; }
79
83
  /**
80
- * @see {@link GainmapDecodingParameters}
84
+ * @see {@link GainMapMetadata.offsetHdr}
85
+ */
86
+ get offsetHdr() { return this.uniforms.offsetHdr.value.toArray(); }
87
+ set offsetHdr(value) { this.uniforms.offsetHdr.value.fromArray(value); }
88
+ /**
89
+ * @see {@link GainMapMetadata.offsetSdr}
90
+ */
91
+ get offsetSdr() { return this.uniforms.offsetSdr.value.toArray(); }
92
+ set offsetSdr(value) { this.uniforms.offsetSdr.value.fromArray(value); }
93
+ /**
94
+ * @see {@link GainMapMetadata.gainMapMin}
95
+ */
96
+ get gainMapMin() { return this.uniforms.gainMapMin.value.toArray(); }
97
+ set gainMapMin(value) { this.uniforms.gainMapMin.value.fromArray(value); }
98
+ /**
99
+ * @see {@link GainMapMetadata.gainMapMax}
100
+ */
101
+ get gainMapMax() { return this.uniforms.gainMapMax.value.toArray(); }
102
+ set gainMapMax(value) { this.uniforms.gainMapMax.value.fromArray(value); }
103
+ /**
104
+ * @see {@link GainmapEncodingParameters.gamma}
105
+ */
106
+ get gamma() {
107
+ const g = this.uniforms.gamma.value;
108
+ return [1 / g.x, 1 / g.y, 1 / g.z];
109
+ }
110
+ set gamma(value) {
111
+ const g = this.uniforms.gamma.value;
112
+ g.x = 1.0 / value[0];
113
+ g.y = 1.0 / value[1];
114
+ g.z = 1.0 / value[2];
115
+ }
116
+ /**
117
+ * @see {@link GainMapMetadata.hdrCapacityMin}
118
+ * @remarks Logarithmic space
119
+ */
120
+ get hdrCapacityMin() { return this._hdrCapacityMin; }
121
+ set hdrCapacityMin(value) {
122
+ this._hdrCapacityMin = value;
123
+ this.calculateWeight();
124
+ }
125
+ /**
126
+ * @see {@link GainMapMetadata.hdrCapacityMin}
127
+ * @remarks Logarithmic space
128
+ */
129
+ get hdrCapacityMax() { return this._hdrCapacityMax; }
130
+ set hdrCapacityMax(value) {
131
+ this._hdrCapacityMax = value;
132
+ this.calculateWeight();
133
+ }
134
+ /**
135
+ * @see {@link GainmapDecodingParameters.maxDisplayBoost}
136
+ * @remarks Non Logarithmic space
81
137
  */
82
138
  get maxDisplayBoost() { return this._maxDisplayBoost; }
83
139
  set maxDisplayBoost(value) {
84
140
  this._maxDisplayBoost = value;
141
+ this.calculateWeight();
142
+ }
143
+ calculateWeight() {
85
144
  this.uniforms.weightFactor.value = (Math.log2(this._maxDisplayBoost) - this._hdrCapacityMin) / (this._hdrCapacityMax - this._hdrCapacityMin);
86
145
  }
87
146
  }
@@ -117,6 +176,10 @@ void main() {
117
176
  this._width = width;
118
177
  this._height = height;
119
178
  this._type = type;
179
+ this._colorSpace = colorSpace;
180
+ if (type === three.HalfFloatType && navigator.userAgent.toLowerCase().includes('firefox')) {
181
+ this._type = three.FloatType;
182
+ }
120
183
  this._material = material;
121
184
  if (renderer) {
122
185
  this._renderer = renderer;
@@ -137,7 +200,7 @@ void main() {
137
200
  this._quad.geometry.computeBoundingBox();
138
201
  this._scene.add(this._quad);
139
202
  this._renderTarget = new three.WebGLRenderTarget(width, height, {
140
- type,
203
+ type: this._type,
141
204
  colorSpace,
142
205
  format: three.RGBAFormat,
143
206
  magFilter: three.LinearFilter,
@@ -198,6 +261,9 @@ void main() {
198
261
  this._renderer.readRenderTargetPixels(this._renderTarget, 0, 0, this._width, this._height, out);
199
262
  return out;
200
263
  }
264
+ toDataTexture() {
265
+ return new three.DataTexture(this.toArray(), this.width, this.height, three.RGBAFormat, this._type, three.UVMapping, three.RepeatWrapping, three.RepeatWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, 1, three.NoColorSpace);
266
+ }
201
267
  /**
202
268
  * If using a disposable renderer, it will dispose it.
203
269
  */
@@ -242,6 +308,11 @@ void main() {
242
308
  * The `Material` used.
243
309
  */
244
310
  get material() { return this._material; }
311
+ /**
312
+ *
313
+ */
314
+ get type() { return this._type; }
315
+ get colorSpace() { return this._colorSpace; }
245
316
  }
246
317
 
247
318
  /**
@@ -251,27 +322,46 @@ void main() {
251
322
  * @group Decoding
252
323
  * @example
253
324
  * import { decode } from '@monogrid/gainmap-js'
254
- * import { Mesh, MeshBasicMaterial, PlaneGeometry, TextureLoader } from 'three'
325
+ * import {
326
+ * Mesh,
327
+ * MeshBasicMaterial,
328
+ * PerspectiveCamera,
329
+ * PlaneGeometry,
330
+ * Scene,
331
+ * TextureLoader,
332
+ * WebGLRenderer
333
+ * } from 'three'
334
+ *
335
+ * const renderer = new WebGLRenderer()
255
336
  *
256
- * const loader = new TextureLoader()
337
+ * const textureLoader = new TextureLoader()
257
338
  *
258
339
  * // load SDR Representation
259
- * const sdr = await loader.loadAsync('sdr.jpg')
340
+ * const sdr = await textureLoader.loadAsync('sdr.jpg')
260
341
  * // load Gain map recovery image
261
- * const gainMap = await loader.loadAsync('gainmap.jpg')
342
+ * const gainMap = await textureLoader.loadAsync('gainmap.jpg')
262
343
  * // load metadata
263
344
  * const metadata = await (await fetch('metadata.json')).json()
264
345
  *
265
346
  * const result = await decode({
266
347
  * sdr,
267
348
  * gainMap,
349
+ * // this allows to use `result.renderTarget.texture` directly
350
+ * renderer,
268
351
  * // this will restore the full HDR range
269
352
  * maxDisplayBoost: Math.pow(2, metadata.hdrCapacityMax),
270
353
  * ...metadata
271
354
  * })
272
355
  *
273
- * // result can be used to populate a Texture
274
- * const mesh = new Mesh(new PlaneGeometry(), new MeshBasicMaterial({ map: result.renderTarget.texture }))
356
+ * const scene = new Scene()
357
+ * // `result` can be used to populate a Texture
358
+ * const mesh = new Mesh(
359
+ * new PlaneGeometry(),
360
+ * new MeshBasicMaterial({ map: result.renderTarget.texture })
361
+ * )
362
+ * scene.add(mesh)
363
+ * renderer.render(scene, new PerspectiveCamera())
364
+ *
275
365
  *
276
366
  * @param params
277
367
  * @returns
@@ -458,23 +548,33 @@ void main() {
458
548
  this.needsUpdate = true;
459
549
  this.uniformsNeedUpdate = true;
460
550
  }
551
+ /**
552
+ * @see {@link GainmapEncodingParameters.gamma}
553
+ */
461
554
  get gamma() { return this._gamma; }
462
555
  set gamma(value) {
463
556
  this._gamma = value;
464
557
  this.uniforms.gamma.value = new three.Vector3().fromArray(value);
465
558
  }
559
+ /**
560
+ * @see {@link GainmapEncodingParameters.offsetHdr}
561
+ */
466
562
  get offsetHdr() { return this._offsetHdr; }
467
563
  set offsetHdr(value) {
468
564
  this._offsetHdr = value;
469
565
  this.uniforms.offsetHdr.value = new three.Vector3().fromArray(value);
470
566
  }
567
+ /**
568
+ * @see {@link GainmapEncodingParameters.offsetSdr}
569
+ */
471
570
  get offsetSdr() { return this._offsetSdr; }
472
571
  set offsetSdr(value) {
473
572
  this._offsetSdr = value;
474
573
  this.uniforms.offsetSdr.value = new three.Vector3().fromArray(value);
475
574
  }
476
575
  /**
477
- * Non logarithmic space
576
+ * @see {@link GainmapEncodingParameters.minContentBoost}
577
+ * @remarks Non logarithmic space
478
578
  */
479
579
  get minContentBoost() { return this._minContentBoost; }
480
580
  set minContentBoost(value) {
@@ -482,16 +582,33 @@ void main() {
482
582
  this.uniforms.minLog2.value = Math.log2(value);
483
583
  }
484
584
  /**
485
- * Non logarithmic space
585
+ * @see {@link GainmapEncodingParameters.maxContentBoost}
586
+ * @remarks Non logarithmic space
486
587
  */
487
588
  get maxContentBoost() { return this._maxContentBoost; }
488
589
  set maxContentBoost(value) {
489
590
  this._maxContentBoost = value;
490
591
  this.uniforms.maxLog2.value = Math.log2(value);
491
592
  }
593
+ /**
594
+ * @see {@link GainMapMetadata.gainMapMin}
595
+ * @remarks Logarithmic space
596
+ */
492
597
  get gainMapMin() { return [Math.log2(this._minContentBoost), Math.log2(this._minContentBoost), Math.log2(this._minContentBoost)]; }
598
+ /**
599
+ * @see {@link GainMapMetadata.gainMapMax}
600
+ * @remarks Logarithmic space
601
+ */
493
602
  get gainMapMax() { return [Math.log2(this._maxContentBoost), Math.log2(this._maxContentBoost), Math.log2(this._maxContentBoost)]; }
603
+ /**
604
+ * @see {@link GainMapMetadata.hdrCapacityMin}
605
+ * @remarks Logarithmic space
606
+ */
494
607
  get hdrCapacityMin() { return Math.min(Math.max(0, this.gainMapMin[0]), Math.max(0, this.gainMapMin[1]), Math.max(0, this.gainMapMin[2])); }
608
+ /**
609
+ * @see {@link GainMapMetadata.hdrCapacityMax}
610
+ * @remarks Logarithmic space
611
+ */
495
612
  get hdrCapacityMax() { return Math.min(Math.max(0, this.gainMapMax[0]), Math.max(0, this.gainMapMax[1]), Math.max(0, this.gainMapMax[2])); }
496
613
  }
497
614
 
@@ -756,8 +873,8 @@ void main() {
756
873
  * @group Encoding Functions
757
874
  *
758
875
  * @param hdrTexture The HDR image to be rendered
759
- * @param renderer (optional) WebGLRenderer to use diring the rendering, a disposable renderer will be create and destroyed if this is not provided.
760
- * @param toneMapping (optional) Tonemapping to be applied to the SDR Rendition
876
+ * @param renderer (optional) WebGLRenderer to use during the rendering, a disposable renderer will be create and destroyed if this is not provided.
877
+ * @param toneMapping (optional) Tone mapping to be applied to the SDR Rendition
761
878
  * @throws {Error} if the WebGLRenderer fails to render the SDR image
762
879
  */
763
880
  const getSDRRendition = (hdrTexture, renderer, toneMapping) => {
@@ -820,7 +937,7 @@ void main() {
820
937
  * encodingResult.sdr.dispose()
821
938
  * encodingResult.gainMap.dispose()
822
939
  *
823
- * @param params Encoding Paramaters
940
+ * @param params Encoding Parameters
824
941
  * @returns
825
942
  */
826
943
  const encode = (params) => {
@@ -855,7 +972,7 @@ void main() {
855
972
  * @remarks
856
973
  * if a `renderer` parameter is not provided
857
974
  * This function will automatically dispose its "disposable"
858
- * renderer, no need to disapose it manually later
975
+ * renderer, no need to dispose it manually later
859
976
  *
860
977
  * @category Encoding Functions
861
978
  * @group Encoding Functions
@@ -889,7 +1006,7 @@ void main() {
889
1006
  * // `jpeg` will be an `Uint8Array` which can be saved somewhere
890
1007
  *
891
1008
  *
892
- * @param params Encoding Paramaters
1009
+ * @param params Encoding Parameters
893
1010
  * @throws {Error} if the browser does not support [createImageBitmap](https://caniuse.com/createimagebitmap)
894
1011
  */
895
1012
  const encodeAndCompress = async (params) => {
@@ -1016,14 +1133,14 @@ void main() {
1016
1133
  let w = srcTex.image.width;
1017
1134
  let h = srcTex.image.height;
1018
1135
  const quadRenderer = new QuadRenderer(w, h, srcTex.type, srcTex.colorSpace, mat, renderer);
1019
- const framebuffers = [];
1136
+ const frameBuffers = [];
1020
1137
  while (w > 1 || h > 1) {
1021
1138
  w = Math.max(1, (w + cellSize - 1) / cellSize | 0);
1022
1139
  h = Math.max(1, (h + cellSize - 1) / cellSize | 0);
1023
1140
  const fb = new three.WebGLRenderTarget(w, h, {
1024
- type: srcTex.type,
1141
+ type: quadRenderer.type,
1025
1142
  format: srcTex.format,
1026
- colorSpace: srcTex.colorSpace,
1143
+ colorSpace: quadRenderer.colorSpace,
1027
1144
  minFilter: three.NearestFilter,
1028
1145
  magFilter: three.NearestFilter,
1029
1146
  wrapS: three.ClampToEdgeWrapping,
@@ -1032,11 +1149,11 @@ void main() {
1032
1149
  depthBuffer: false,
1033
1150
  stencilBuffer: false
1034
1151
  });
1035
- framebuffers.push(fb);
1152
+ frameBuffers.push(fb);
1036
1153
  }
1037
1154
  w = srcTex.image.width;
1038
1155
  h = srcTex.image.height;
1039
- framebuffers.forEach((fbi) => {
1156
+ frameBuffers.forEach((fbi) => {
1040
1157
  w = Math.max(1, (w + cellSize - 1) / cellSize | 0);
1041
1158
  h = Math.max(1, (h + cellSize - 1) / cellSize | 0);
1042
1159
  quadRenderer.renderTarget = fbi;
@@ -1047,12 +1164,199 @@ void main() {
1047
1164
  });
1048
1165
  const out = quadRenderer.toArray();
1049
1166
  quadRenderer.dispose();
1050
- framebuffers.forEach(fb => fb.dispose());
1051
- return [three.DataUtils.fromHalfFloat(out[0]), three.DataUtils.fromHalfFloat(out[1]), three.DataUtils.fromHalfFloat(out[2])];
1167
+ frameBuffers.forEach(fb => fb.dispose());
1168
+ return [
1169
+ quadRenderer.type === three.FloatType ? out[0] : three.DataUtils.fromHalfFloat(out[0]),
1170
+ quadRenderer.type === three.FloatType ? out[1] : three.DataUtils.fromHalfFloat(out[1]),
1171
+ quadRenderer.type === three.FloatType ? out[2] : three.DataUtils.fromHalfFloat(out[2])
1172
+ ];
1052
1173
  };
1053
1174
 
1175
+ class LoaderBase extends three.Loader {
1176
+ /**
1177
+ *
1178
+ * @param renderer
1179
+ * @param manager
1180
+ */
1181
+ constructor(renderer, manager) {
1182
+ super(manager);
1183
+ this.renderer = renderer;
1184
+ }
1185
+ /**
1186
+ * @private
1187
+ * @returns
1188
+ */
1189
+ prepareQuadRenderer() {
1190
+ // temporary values
1191
+ const material = new GainMapDecoderMaterial({
1192
+ gainMapMax: [1, 1, 1],
1193
+ gainMapMin: [0, 0, 0],
1194
+ gamma: [1, 1, 1],
1195
+ offsetHdr: [1, 1, 1],
1196
+ offsetSdr: [1, 1, 1],
1197
+ hdrCapacityMax: 1,
1198
+ hdrCapacityMin: 0,
1199
+ maxDisplayBoost: 1,
1200
+ gainMap: new three.Texture(),
1201
+ sdr: new three.Texture()
1202
+ });
1203
+ return new QuadRenderer(16, 16, three.HalfFloatType, three.NoColorSpace, material, this.renderer);
1204
+ }
1205
+ /**
1206
+ *
1207
+ * @private
1208
+ * @param quadRenderer
1209
+ * @param gainMapBuffer
1210
+ * @param sdrBuffer
1211
+ * @param metadata
1212
+ */
1213
+ async render(quadRenderer, gainMapBuffer, sdrBuffer, metadata) {
1214
+ const gainMapBlob = new Blob([gainMapBuffer], { type: 'image/jpeg' });
1215
+ // TODO: figure out why result.sdr is not usable here, problem is in the libultrahdr-wasm repo
1216
+ // we use the original image buffer instead
1217
+ const sdrBlob = new Blob([sdrBuffer], { type: 'image/jpeg' });
1218
+ const [gainMapImageBitmap, sdrImageBitmap] = await Promise.all([
1219
+ createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }),
1220
+ createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
1221
+ ]);
1222
+ const gainMap = new three.Texture(gainMapImageBitmap, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.NoColorSpace);
1223
+ gainMap.needsUpdate = true;
1224
+ const sdr = new three.Texture(sdrImageBitmap, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.SRGBColorSpace);
1225
+ sdr.needsUpdate = true;
1226
+ quadRenderer.width = sdrImageBitmap.width;
1227
+ quadRenderer.height = sdrImageBitmap.height;
1228
+ quadRenderer.material.gainMap = gainMap;
1229
+ quadRenderer.material.sdr = sdr;
1230
+ quadRenderer.material.gainMapMin = metadata.gainMapMin;
1231
+ quadRenderer.material.gainMapMax = metadata.gainMapMax;
1232
+ quadRenderer.material.offsetHdr = metadata.offsetHdr;
1233
+ quadRenderer.material.offsetSdr = metadata.offsetSdr;
1234
+ quadRenderer.material.gamma = metadata.gamma;
1235
+ quadRenderer.material.hdrCapacityMin = metadata.hdrCapacityMin;
1236
+ quadRenderer.material.hdrCapacityMax = metadata.hdrCapacityMax;
1237
+ quadRenderer.material.maxDisplayBoost = Math.pow(2, metadata.hdrCapacityMax);
1238
+ quadRenderer.material.needsUpdate = true;
1239
+ quadRenderer.render();
1240
+ }
1241
+ }
1242
+
1243
+ /**
1244
+ * A Three.js Loader for the gain map format.
1245
+ *
1246
+ * @category Loaders
1247
+ * @group Loaders
1248
+ *
1249
+ * @example
1250
+ * import { GainMapLoader } from '@monogrid/gainmap-js'
1251
+ * import {
1252
+ * EquirectangularReflectionMapping,
1253
+ * LinearFilter,
1254
+ * Mesh,
1255
+ * MeshBasicMaterial,
1256
+ * PerspectiveCamera,
1257
+ * PlaneGeometry,
1258
+ * Scene,
1259
+ * WebGLRenderer
1260
+ * } from 'three'
1261
+ *
1262
+ * const renderer = new WebGLRenderer()
1263
+ *
1264
+ * const loader = new GainMapLoader(renderer)
1265
+ *
1266
+ * const result = loader.load(['sdr.jpeg', 'gainmap.jpeg', 'metadata.json'])
1267
+ * // `result` can be used to populate a Texture
1268
+ *
1269
+ * const scene = new Scene()
1270
+ * const mesh = new Mesh(
1271
+ * new PlaneGeometry(),
1272
+ * new MeshBasicMaterial({ map: result.renderTarget.texture })
1273
+ * )
1274
+ * scene.add(mesh)
1275
+ * renderer.render(scene, new PerspectiveCamera())
1276
+ *
1277
+ * // `result.renderTarget.texture` must be
1278
+ * // converted to `DataTexture` in order
1279
+ * // to use it as Equirectangular scene background
1280
+ * // if needed
1281
+ *
1282
+ * scene.background = result.toDataTexture()
1283
+ * scene.background.mapping = EquirectangularReflectionMapping
1284
+ * scene.background.minFilter = LinearFilter
1285
+ *
1286
+ *
1287
+ */
1288
+ class GainMapLoader extends LoaderBase {
1289
+ /**
1290
+ * Loads a gainmap using separate data
1291
+ * * sdr image
1292
+ * * gain map image
1293
+ * * metadata json
1294
+ *
1295
+ * useful for webp gain maps
1296
+ *
1297
+ * @param sdrUrl
1298
+ * @param gainMapUrl
1299
+ * @param metadataUrl
1300
+ * @param onLoad
1301
+ * @param onProgress
1302
+ * @param onError
1303
+ * @returns
1304
+ */
1305
+ load([sdrUrl, gainMapUrl, metadataUrl], onLoad, onProgress, onError) {
1306
+ const quadRenderer = this.prepareQuadRenderer();
1307
+ let sdr;
1308
+ let gainMap;
1309
+ let metadata;
1310
+ const loadCheck = async () => {
1311
+ if (sdr && gainMap && metadata) {
1312
+ await this.render(quadRenderer, gainMap, sdr, metadata);
1313
+ if (typeof onLoad === 'function')
1314
+ onLoad(quadRenderer);
1315
+ quadRenderer.dispose();
1316
+ }
1317
+ };
1318
+ const progressHandler = (e) => {
1319
+ };
1320
+ const sdrLoader = new three.FileLoader(this.manager);
1321
+ sdrLoader.setResponseType('arraybuffer');
1322
+ sdrLoader.setRequestHeader(this.requestHeader);
1323
+ sdrLoader.setPath(this.path);
1324
+ sdrLoader.setWithCredentials(this.withCredentials);
1325
+ sdrLoader.load(sdrUrl, async (buffer) => {
1326
+ if (typeof buffer === 'string')
1327
+ throw new Error('Invalid sdr buffer');
1328
+ sdr = buffer;
1329
+ loadCheck();
1330
+ }, progressHandler, onError);
1331
+ const gainMapLoader = new three.FileLoader(this.manager);
1332
+ gainMapLoader.setResponseType('arraybuffer');
1333
+ gainMapLoader.setRequestHeader(this.requestHeader);
1334
+ gainMapLoader.setPath(this.path);
1335
+ gainMapLoader.setWithCredentials(this.withCredentials);
1336
+ gainMapLoader.load(gainMapUrl, async (buffer) => {
1337
+ if (typeof buffer === 'string')
1338
+ throw new Error('Invalid gainmap buffer');
1339
+ gainMap = buffer;
1340
+ loadCheck();
1341
+ }, progressHandler, onError);
1342
+ const metadataLoader = new three.FileLoader(this.manager);
1343
+ // metadataLoader.setResponseType('json')
1344
+ metadataLoader.setRequestHeader(this.requestHeader);
1345
+ metadataLoader.setPath(this.path);
1346
+ metadataLoader.setWithCredentials(this.withCredentials);
1347
+ metadataLoader.load(metadataUrl, async (json) => {
1348
+ if (typeof json !== 'string')
1349
+ throw new Error('Invalid metadata string');
1350
+ metadata = JSON.parse(json);
1351
+ loadCheck();
1352
+ }, progressHandler, onError);
1353
+ return quadRenderer;
1354
+ }
1355
+ }
1356
+
1054
1357
  exports.GainMapDecoderMaterial = GainMapDecoderMaterial;
1055
1358
  exports.GainMapEncoderMaterial = GainMapEncoderMaterial;
1359
+ exports.GainMapLoader = GainMapLoader;
1056
1360
  exports.QuadRenderer = QuadRenderer;
1057
1361
  exports.SDRMaterial = SDRMaterial;
1058
1362
  exports.compress = compress;
@@ -0,0 +1,31 @@
1
+ import { GainMapMetadata } from '../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 | ArrayBuffer | Uint8Array | Int8Array;
27
+ success: boolean;
28
+ errorMessage: any;
29
+ sdr: any;
30
+ gainMap: any;
31
+ }>;
@@ -0,0 +1,83 @@
1
+ import { type CompressedImage, type GainMapMetadata } from '../types';
2
+ /**
3
+ * Encapsulates a Gainmap into a single JPEG file (aka: JPEG-R) with the base map
4
+ * as the sdr visualization and the gainMap encoded into a MPF (Multi-Picture Format) tag.
5
+ *
6
+ * @category Encoding
7
+ * @group Encoding
8
+ *
9
+ * @example
10
+ * import { compress, encode, findTextureMinMax } from '@monogrid/gainmap-js'
11
+ * import { encodeJPEGMetadata } from '@monogrid/gainmap-js/libultrahdr'
12
+ * import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
13
+ *
14
+ * // load an HDR file
15
+ * const loader = new EXRLoader()
16
+ * const image = await loader.loadAsync('image.exr')
17
+ *
18
+ * // find RAW RGB Max value of a texture
19
+ * const textureMax = await findTextureMinMax(image)
20
+ *
21
+ * // Encode the gainmap
22
+ * const encodingResult = encode({
23
+ * image,
24
+ * maxContentBoost: Math.max.apply(this, textureMax)
25
+ * })
26
+ *
27
+ * // obtain the RAW RGBA SDR buffer and create an ImageData
28
+ * const sdrImageData = new ImageData(
29
+ * encodingResult.sdr.toArray(),
30
+ * encodingResult.sdr.width,
31
+ * encodingResult.sdr.height
32
+ * )
33
+ * // obtain the RAW RGBA Gain map buffer and create an ImageData
34
+ * const gainMapImageData = new ImageData(
35
+ * encodingResult.gainMap.toArray(),
36
+ * encodingResult.gainMap.width,
37
+ * encodingResult.gainMap.height
38
+ * )
39
+ *
40
+ * // parallel compress the RAW buffers into the specified mimeType
41
+ * const mimeType = 'image/jpeg'
42
+ * const quality = 0.9
43
+ *
44
+ * const [sdr, gainMap] = await Promise.all([
45
+ * compress({
46
+ * source: sdrImageData,
47
+ * mimeType,
48
+ * quality,
49
+ * flipY: true // output needs to be flipped
50
+ * }),
51
+ * compress({
52
+ * source: gainMapImageData,
53
+ * mimeType,
54
+ * quality,
55
+ * flipY: true // output needs to be flipped
56
+ * })
57
+ * ])
58
+ *
59
+ * // obtain the metadata which will be embedded into
60
+ * // and XMP tag inside the final JPEG file
61
+ * const metadata = encodingResult.getMetadata()
62
+ *
63
+ * // embed the compressed images + metadata into a single
64
+ * // JPEG file
65
+ * const jpeg = await encodeJPEGMetadata({
66
+ * ...encodingResult,
67
+ * ...metadata,
68
+ * sdr,
69
+ * gainMap
70
+ * })
71
+ *
72
+ * // `jpeg` will be an `Uint8Array` which can be saved somewhere
73
+ *
74
+ *
75
+ * @param encodingResult
76
+ * @returns an Uint8Array representing a JPEG-R file
77
+ * @throws {Error} If `encodingResult.sdr.mimeType !== 'image/jpeg'`
78
+ * @throws {Error} If `encodingResult.gainMap.mimeType !== 'image/jpeg'`
79
+ */
80
+ export declare const encodeJPEGMetadata: (encodingResult: GainMapMetadata & {
81
+ sdr: CompressedImage;
82
+ gainMap: CompressedImage;
83
+ }) => 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>;
Binary file