@monogrid/gainmap-js 2.0.1 → 2.0.3

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## [2.0.3](https://github.com/MONOGRID/gainmap-js/compare/v2.0.2...v2.0.3) (2023-11-15)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **gainmaploader:** fix GainMapLoader progress handler not being calculated correctly ([a8e556a](https://github.com/MONOGRID/gainmap-js/commit/a8e556ab1d465a9bd705960d175464cf8d434ea1))
7
+
8
+ ## [2.0.2](https://github.com/MONOGRID/gainmap-js/compare/v2.0.1...v2.0.2) (2023-11-14)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **decode:** improves compatibility with browsers with no createImageBitmap ([12c7609](https://github.com/MONOGRID/gainmap-js/commit/12c7609ee815a460f95419aab328b412e759f012))
14
+ * **decoder:** fix bug when using decodeResult.renderTarget.texture as source for PMREMGenerator ([4ebb983](https://github.com/MONOGRID/gainmap-js/commit/4ebb983d5bbc7f524e4b1231f630e3714cf1f870))
15
+
1
16
  ## [2.0.1](https://github.com/MONOGRID/gainmap-js/compare/v2.0.0...v2.0.1) (2023-11-14)
2
17
 
3
18
 
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @monogrid/gainmap-js v2.0.0
2
+ * @monogrid/gainmap-js v2.0.2
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @monogrid/gainmap-js v2.0.0
2
+ * @monogrid/gainmap-js v2.0.2
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @monogrid/gainmap-js v2.0.0
2
+ * @monogrid/gainmap-js v2.0.2
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
package/dist/decode.js CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
- * @monogrid/gainmap-js v2.0.0
2
+ * @monogrid/gainmap-js v2.0.2
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
6
- import { Q as QuadRenderer } from './QuadRenderer-2NcH1MyZ.js';
6
+ import { Q as QuadRenderer } from './QuadRenderer-IyQj4ZsN.js';
7
7
  import { ShaderMaterial, Vector3, NoBlending, SRGBColorSpace, NoColorSpace, HalfFloatType, Loader, Texture, UVMapping, ClampToEdgeWrapping, LinearFilter, LinearMipMapLinearFilter, RGBAFormat, UnsignedByteType, FileLoader } from 'three';
8
8
 
9
9
  const vertexShader = /* glsl */ `
@@ -15,6 +15,13 @@ void main() {
15
15
  }
16
16
  `;
17
17
  const fragmentShader = /* glsl */ `
18
+ // this should be the max float value according to https://stackoverflow.com/a/47543127
19
+ // #define FLT_MAX vec3( 3.402823466e+38, 3.402823466e+38, 3.402823466e+38)
20
+
21
+ // in practice, this works for us with iOS problems
22
+ // we encountered
23
+ #define FLT_MAX vec3( 3.402823e+38, 3.402823e+38, 3.402823e+38 )
24
+
18
25
  uniform sampler2D sdr;
19
26
  uniform sampler2D gainMap;
20
27
  uniform vec3 gamma;
@@ -32,7 +39,7 @@ void main() {
32
39
  vec3 logRecovery = pow(recovery, gamma);
33
40
  vec3 logBoost = gainMapMin * (1.0 - logRecovery) + gainMapMax * logRecovery;
34
41
  vec3 hdrColor = (rgb + offsetSdr) * exp2(logBoost * weightFactor) - offsetHdr;
35
- gl_FragColor = vec4(hdrColor, 1.0);
42
+ gl_FragColor = vec4( min(FLT_MAX, hdrColor), 1.0 );
36
43
  }
37
44
  `;
38
45
  /**
@@ -134,11 +141,12 @@ class GainMapDecoderMaterial extends ShaderMaterial {
134
141
  */
135
142
  get maxDisplayBoost() { return this._maxDisplayBoost; }
136
143
  set maxDisplayBoost(value) {
137
- this._maxDisplayBoost = value;
144
+ this._maxDisplayBoost = Math.max(1, Math.min(65504, value));
138
145
  this.calculateWeight();
139
146
  }
140
147
  calculateWeight() {
141
- this.uniforms.weightFactor.value = (Math.log2(this._maxDisplayBoost) - this._hdrCapacityMin) / (this._hdrCapacityMax - this._hdrCapacityMin);
148
+ const val = (Math.log2(this._maxDisplayBoost) - this._hdrCapacityMin) / (this._hdrCapacityMax - this._hdrCapacityMin);
149
+ this.uniforms.weightFactor.value = Math.max(0, Math.min(1, val));
142
150
  }
143
151
  }
144
152
 
@@ -478,6 +486,20 @@ const extractGainmapFromJPEG = async (jpegFile) => {
478
486
  };
479
487
  };
480
488
 
489
+ /**
490
+ * private function, async get image from blob
491
+ *
492
+ * @param blob
493
+ * @returns
494
+ */
495
+ const getImage = (blob) => {
496
+ return new Promise((resolve, reject) => {
497
+ const img = document.createElement('img');
498
+ img.onload = () => { resolve(img); };
499
+ img.onerror = (e) => { reject(e); };
500
+ img.src = URL.createObjectURL(blob);
501
+ });
502
+ };
481
503
  class LoaderBase extends Loader {
482
504
  /**
483
505
  *
@@ -519,16 +541,34 @@ class LoaderBase extends Loader {
519
541
  async render(quadRenderer, gainMapBuffer, sdrBuffer, metadata) {
520
542
  const gainMapBlob = new Blob([gainMapBuffer], { type: 'image/jpeg' });
521
543
  const sdrBlob = new Blob([sdrBuffer], { type: 'image/jpeg' });
522
- const [gainMapImageBitmap, sdrImageBitmap] = await Promise.all([
523
- createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }),
524
- createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
525
- ]);
526
- const gainMap = new Texture(gainMapImageBitmap, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, LinearMipMapLinearFilter, RGBAFormat, UnsignedByteType, 1, NoColorSpace);
544
+ let sdrImage;
545
+ let gainMapImage;
546
+ let needsFlip = false;
547
+ if (typeof createImageBitmap === 'undefined') {
548
+ const res = await Promise.all([
549
+ getImage(gainMapBlob),
550
+ getImage(sdrBlob)
551
+ ]);
552
+ gainMapImage = res[0];
553
+ sdrImage = res[1];
554
+ needsFlip = true;
555
+ }
556
+ else {
557
+ const res = await Promise.all([
558
+ createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }),
559
+ createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
560
+ ]);
561
+ gainMapImage = res[0];
562
+ sdrImage = res[1];
563
+ }
564
+ const gainMap = new Texture(gainMapImage, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, LinearMipMapLinearFilter, RGBAFormat, UnsignedByteType, 1, NoColorSpace);
565
+ gainMap.flipY = needsFlip;
527
566
  gainMap.needsUpdate = true;
528
- const sdr = new Texture(sdrImageBitmap, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, LinearMipMapLinearFilter, RGBAFormat, UnsignedByteType, 1, SRGBColorSpace);
567
+ const sdr = new Texture(sdrImage, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, LinearMipMapLinearFilter, RGBAFormat, UnsignedByteType, 1, SRGBColorSpace);
568
+ sdr.flipY = needsFlip;
529
569
  sdr.needsUpdate = true;
530
- quadRenderer.width = sdrImageBitmap.width;
531
- quadRenderer.height = sdrImageBitmap.height;
570
+ quadRenderer.width = sdrImage.width;
571
+ quadRenderer.height = sdrImage.height;
532
572
  quadRenderer.material.gainMap = gainMap;
533
573
  quadRenderer.material.sdr = sdr;
534
574
  quadRenderer.material.gainMapMin = metadata.gainMapMin;
@@ -619,7 +659,22 @@ class GainMapLoader extends LoaderBase {
619
659
  quadRenderer.dispose();
620
660
  }
621
661
  };
622
- const progressHandler = (e) => {
662
+ let sdrLengthComputable = true;
663
+ let sdrTotal = 0;
664
+ let sdrLoaded = 0;
665
+ let gainMapLengthComputable = true;
666
+ let gainMapTotal = 0;
667
+ let gainMapLoaded = 0;
668
+ let metadataLengthComputable = true;
669
+ let metadataTotal = 0;
670
+ let metadataLoaded = 0;
671
+ const progressHandler = () => {
672
+ if (typeof onProgress === 'function') {
673
+ const total = sdrTotal + gainMapTotal + metadataTotal;
674
+ const loaded = sdrLoaded + gainMapLoaded + metadataLoaded;
675
+ const lengthComputable = sdrLengthComputable && gainMapLengthComputable && metadataLengthComputable;
676
+ onProgress(new ProgressEvent('progress', { lengthComputable, loaded, total }));
677
+ }
623
678
  };
624
679
  const sdrLoader = new FileLoader(this.manager);
625
680
  sdrLoader.setResponseType('arraybuffer');
@@ -631,7 +686,12 @@ class GainMapLoader extends LoaderBase {
631
686
  throw new Error('Invalid sdr buffer');
632
687
  sdr = buffer;
633
688
  loadCheck();
634
- }, progressHandler, onError);
689
+ }, (e) => {
690
+ sdrLengthComputable = e.lengthComputable;
691
+ sdrLoaded = e.loaded;
692
+ sdrTotal = e.total;
693
+ progressHandler();
694
+ }, onError);
635
695
  const gainMapLoader = new FileLoader(this.manager);
636
696
  gainMapLoader.setResponseType('arraybuffer');
637
697
  gainMapLoader.setRequestHeader(this.requestHeader);
@@ -642,7 +702,12 @@ class GainMapLoader extends LoaderBase {
642
702
  throw new Error('Invalid gainmap buffer');
643
703
  gainMap = buffer;
644
704
  loadCheck();
645
- }, progressHandler, onError);
705
+ }, (e) => {
706
+ gainMapLengthComputable = e.lengthComputable;
707
+ gainMapLoaded = e.loaded;
708
+ gainMapTotal = e.total;
709
+ progressHandler();
710
+ }, onError);
646
711
  const metadataLoader = new FileLoader(this.manager);
647
712
  // metadataLoader.setResponseType('json')
648
713
  metadataLoader.setRequestHeader(this.requestHeader);
@@ -653,7 +718,12 @@ class GainMapLoader extends LoaderBase {
653
718
  throw new Error('Invalid metadata string');
654
719
  metadata = JSON.parse(json);
655
720
  loadCheck();
656
- }, progressHandler, onError);
721
+ }, (e) => {
722
+ metadataLengthComputable = e.lengthComputable;
723
+ metadataLoaded = e.loaded;
724
+ metadataTotal = e.total;
725
+ progressHandler();
726
+ }, onError);
657
727
  return quadRenderer;
658
728
  }
659
729
  }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @monogrid/gainmap-js v2.0.0
2
+ * @monogrid/gainmap-js v2.0.2
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
@@ -248,6 +248,13 @@ void main() {
248
248
  }
249
249
  `;
250
250
  const fragmentShader = /* glsl */ `
251
+ // this should be the max float value according to https://stackoverflow.com/a/47543127
252
+ // #define FLT_MAX vec3( 3.402823466e+38, 3.402823466e+38, 3.402823466e+38)
253
+
254
+ // in practice, this works for us with iOS problems
255
+ // we encountered
256
+ #define FLT_MAX vec3( 3.402823e+38, 3.402823e+38, 3.402823e+38 )
257
+
251
258
  uniform sampler2D sdr;
252
259
  uniform sampler2D gainMap;
253
260
  uniform vec3 gamma;
@@ -265,7 +272,7 @@ void main() {
265
272
  vec3 logRecovery = pow(recovery, gamma);
266
273
  vec3 logBoost = gainMapMin * (1.0 - logRecovery) + gainMapMax * logRecovery;
267
274
  vec3 hdrColor = (rgb + offsetSdr) * exp2(logBoost * weightFactor) - offsetHdr;
268
- gl_FragColor = vec4(hdrColor, 1.0);
275
+ gl_FragColor = vec4( min(FLT_MAX, hdrColor), 1.0 );
269
276
  }
270
277
  `;
271
278
  /**
@@ -367,11 +374,12 @@ void main() {
367
374
  */
368
375
  get maxDisplayBoost() { return this._maxDisplayBoost; }
369
376
  set maxDisplayBoost(value) {
370
- this._maxDisplayBoost = value;
377
+ this._maxDisplayBoost = Math.max(1, Math.min(65504, value));
371
378
  this.calculateWeight();
372
379
  }
373
380
  calculateWeight() {
374
- this.uniforms.weightFactor.value = (Math.log2(this._maxDisplayBoost) - this._hdrCapacityMin) / (this._hdrCapacityMax - this._hdrCapacityMin);
381
+ const val = (Math.log2(this._maxDisplayBoost) - this._hdrCapacityMin) / (this._hdrCapacityMax - this._hdrCapacityMin);
382
+ this.uniforms.weightFactor.value = Math.max(0, Math.min(1, val));
375
383
  }
376
384
  }
377
385
 
@@ -711,6 +719,20 @@ void main() {
711
719
  };
712
720
  };
713
721
 
722
+ /**
723
+ * private function, async get image from blob
724
+ *
725
+ * @param blob
726
+ * @returns
727
+ */
728
+ const getImage = (blob) => {
729
+ return new Promise((resolve, reject) => {
730
+ const img = document.createElement('img');
731
+ img.onload = () => { resolve(img); };
732
+ img.onerror = (e) => { reject(e); };
733
+ img.src = URL.createObjectURL(blob);
734
+ });
735
+ };
714
736
  class LoaderBase extends three.Loader {
715
737
  /**
716
738
  *
@@ -752,16 +774,34 @@ void main() {
752
774
  async render(quadRenderer, gainMapBuffer, sdrBuffer, metadata) {
753
775
  const gainMapBlob = new Blob([gainMapBuffer], { type: 'image/jpeg' });
754
776
  const sdrBlob = new Blob([sdrBuffer], { type: 'image/jpeg' });
755
- const [gainMapImageBitmap, sdrImageBitmap] = await Promise.all([
756
- createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }),
757
- createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
758
- ]);
759
- const gainMap = new three.Texture(gainMapImageBitmap, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.NoColorSpace);
777
+ let sdrImage;
778
+ let gainMapImage;
779
+ let needsFlip = false;
780
+ if (typeof createImageBitmap === 'undefined') {
781
+ const res = await Promise.all([
782
+ getImage(gainMapBlob),
783
+ getImage(sdrBlob)
784
+ ]);
785
+ gainMapImage = res[0];
786
+ sdrImage = res[1];
787
+ needsFlip = true;
788
+ }
789
+ else {
790
+ const res = await Promise.all([
791
+ createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }),
792
+ createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
793
+ ]);
794
+ gainMapImage = res[0];
795
+ sdrImage = res[1];
796
+ }
797
+ const gainMap = new three.Texture(gainMapImage, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.NoColorSpace);
798
+ gainMap.flipY = needsFlip;
760
799
  gainMap.needsUpdate = true;
761
- const sdr = new three.Texture(sdrImageBitmap, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.SRGBColorSpace);
800
+ const sdr = new three.Texture(sdrImage, three.UVMapping, three.ClampToEdgeWrapping, three.ClampToEdgeWrapping, three.LinearFilter, three.LinearMipMapLinearFilter, three.RGBAFormat, three.UnsignedByteType, 1, three.SRGBColorSpace);
801
+ sdr.flipY = needsFlip;
762
802
  sdr.needsUpdate = true;
763
- quadRenderer.width = sdrImageBitmap.width;
764
- quadRenderer.height = sdrImageBitmap.height;
803
+ quadRenderer.width = sdrImage.width;
804
+ quadRenderer.height = sdrImage.height;
765
805
  quadRenderer.material.gainMap = gainMap;
766
806
  quadRenderer.material.sdr = sdr;
767
807
  quadRenderer.material.gainMapMin = metadata.gainMapMin;
@@ -852,7 +892,22 @@ void main() {
852
892
  quadRenderer.dispose();
853
893
  }
854
894
  };
855
- const progressHandler = (e) => {
895
+ let sdrLengthComputable = true;
896
+ let sdrTotal = 0;
897
+ let sdrLoaded = 0;
898
+ let gainMapLengthComputable = true;
899
+ let gainMapTotal = 0;
900
+ let gainMapLoaded = 0;
901
+ let metadataLengthComputable = true;
902
+ let metadataTotal = 0;
903
+ let metadataLoaded = 0;
904
+ const progressHandler = () => {
905
+ if (typeof onProgress === 'function') {
906
+ const total = sdrTotal + gainMapTotal + metadataTotal;
907
+ const loaded = sdrLoaded + gainMapLoaded + metadataLoaded;
908
+ const lengthComputable = sdrLengthComputable && gainMapLengthComputable && metadataLengthComputable;
909
+ onProgress(new ProgressEvent('progress', { lengthComputable, loaded, total }));
910
+ }
856
911
  };
857
912
  const sdrLoader = new three.FileLoader(this.manager);
858
913
  sdrLoader.setResponseType('arraybuffer');
@@ -864,7 +919,12 @@ void main() {
864
919
  throw new Error('Invalid sdr buffer');
865
920
  sdr = buffer;
866
921
  loadCheck();
867
- }, progressHandler, onError);
922
+ }, (e) => {
923
+ sdrLengthComputable = e.lengthComputable;
924
+ sdrLoaded = e.loaded;
925
+ sdrTotal = e.total;
926
+ progressHandler();
927
+ }, onError);
868
928
  const gainMapLoader = new three.FileLoader(this.manager);
869
929
  gainMapLoader.setResponseType('arraybuffer');
870
930
  gainMapLoader.setRequestHeader(this.requestHeader);
@@ -875,7 +935,12 @@ void main() {
875
935
  throw new Error('Invalid gainmap buffer');
876
936
  gainMap = buffer;
877
937
  loadCheck();
878
- }, progressHandler, onError);
938
+ }, (e) => {
939
+ gainMapLengthComputable = e.lengthComputable;
940
+ gainMapLoaded = e.loaded;
941
+ gainMapTotal = e.total;
942
+ progressHandler();
943
+ }, onError);
879
944
  const metadataLoader = new three.FileLoader(this.manager);
880
945
  // metadataLoader.setResponseType('json')
881
946
  metadataLoader.setRequestHeader(this.requestHeader);
@@ -886,7 +951,12 @@ void main() {
886
951
  throw new Error('Invalid metadata string');
887
952
  metadata = JSON.parse(json);
888
953
  loadCheck();
889
- }, progressHandler, onError);
954
+ }, (e) => {
955
+ metadataLengthComputable = e.lengthComputable;
956
+ metadataLoaded = e.loaded;
957
+ metadataTotal = e.total;
958
+ progressHandler();
959
+ }, onError);
890
960
  return quadRenderer;
891
961
  }
892
962
  }
package/dist/encode.js CHANGED
@@ -1,11 +1,11 @@
1
1
  /**
2
- * @monogrid/gainmap-js v2.0.0
2
+ * @monogrid/gainmap-js v2.0.2
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
6
- import { c as compress } from './compress-nWDFn7nF.js';
6
+ import { c as compress } from './compress-sMTWjMym.js';
7
7
  import { DataTexture, RGBAFormat, UVMapping, RepeatWrapping, LinearFilter, NoColorSpace, ShaderMaterial, Vector3, NoBlending, UnsignedByteType, ACESFilmicToneMapping, LinearToneMapping, CineonToneMapping, ReinhardToneMapping, SRGBColorSpace, Vector2, WebGLRenderTarget, NearestFilter, ClampToEdgeWrapping, FloatType, DataUtils } from 'three';
8
- import { Q as QuadRenderer } from './QuadRenderer-2NcH1MyZ.js';
8
+ import { Q as QuadRenderer } from './QuadRenderer-IyQj4ZsN.js';
9
9
 
10
10
  /**
11
11
  * Utility function to obtain a `DataTexture` from various input formats
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @monogrid/gainmap-js v2.0.0
2
+ * @monogrid/gainmap-js v2.0.2
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
Binary file