@monogrid/gainmap-js 2.0.0 → 2.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## [2.0.2](https://github.com/MONOGRID/gainmap-js/compare/v2.0.1...v2.0.2) (2023-11-14)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **decode:** improves compatibility with browsers with no createImageBitmap ([12c7609](https://github.com/MONOGRID/gainmap-js/commit/12c7609ee815a460f95419aab328b412e759f012))
7
+ * **decoder:** fix bug when using decodeResult.renderTarget.texture as source for PMREMGenerator ([4ebb983](https://github.com/MONOGRID/gainmap-js/commit/4ebb983d5bbc7f524e4b1231f630e3714cf1f870))
8
+
9
+ ## [2.0.1](https://github.com/MONOGRID/gainmap-js/compare/v2.0.0...v2.0.1) (2023-11-14)
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * **decode:** implements proper feature testing for QuadRenderer.toArray ([20109ad](https://github.com/MONOGRID/gainmap-js/commit/20109ad31977124c1169f096e8ccd36628599f89))
15
+
1
16
  # [2.0.0](https://github.com/MONOGRID/gainmap-js/compare/v1.1.1...v2.0.0) (2023-11-13)
2
17
 
3
18
 
@@ -1,10 +1,78 @@
1
1
  /**
2
- * @monogrid/gainmap-js v1.1.1
2
+ * @monogrid/gainmap-js v2.0.1
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
6
- import { HalfFloatType, FloatType, Scene, OrthographicCamera, Mesh, PlaneGeometry, WebGLRenderTarget, RGBAFormat, LinearFilter, LinearMipMapLinearFilter, RepeatWrapping, WebGLRenderer, IntType, ShortType, ByteType, UnsignedIntType, UnsignedByteType, DataTexture, UVMapping, NoColorSpace } from 'three';
6
+ import { Scene, OrthographicCamera, HalfFloatType, FloatType, Mesh, PlaneGeometry, WebGLRenderTarget, RGBAFormat, LinearFilter, LinearMipMapLinearFilter, RepeatWrapping, WebGLRenderer, DataTexture, UVMapping, NoColorSpace, IntType, ShortType, ByteType, UnsignedIntType, UnsignedByteType, MeshBasicMaterial } from 'three';
7
7
 
8
+ const getBufferForType = (type, width, height) => {
9
+ let out;
10
+ switch (type) {
11
+ case UnsignedByteType:
12
+ out = new Uint8ClampedArray(width * height * 4);
13
+ break;
14
+ case HalfFloatType:
15
+ out = new Uint16Array(width * height * 4);
16
+ break;
17
+ case UnsignedIntType:
18
+ out = new Uint32Array(width * height * 4);
19
+ break;
20
+ case ByteType:
21
+ out = new Int8Array(width * height * 4);
22
+ break;
23
+ case ShortType:
24
+ out = new Int16Array(width * height * 4);
25
+ break;
26
+ case IntType:
27
+ out = new Int32Array(width * height * 4);
28
+ break;
29
+ case FloatType:
30
+ out = new Float32Array(width * height * 4);
31
+ break;
32
+ default:
33
+ throw new Error('Unsupported data type');
34
+ }
35
+ return out;
36
+ };
37
+ let _canReadPixelsResult;
38
+ /**
39
+ * Test if this browser implementation can correctly read pixels from the specified
40
+ * Render target type.
41
+ *
42
+ * Runs only once
43
+ *
44
+ * @param type
45
+ * @param renderer
46
+ * @param camera
47
+ * @returns
48
+ */
49
+ const canReadPixels = (type, renderer, camera) => {
50
+ if (_canReadPixelsResult !== undefined)
51
+ return _canReadPixelsResult;
52
+ const testRT = new WebGLRenderTarget(1, 1, {
53
+ type,
54
+ colorSpace: NoColorSpace,
55
+ format: RGBAFormat,
56
+ magFilter: LinearFilter,
57
+ minFilter: LinearFilter,
58
+ wrapS: RepeatWrapping,
59
+ wrapT: RepeatWrapping,
60
+ depthBuffer: false,
61
+ stencilBuffer: false,
62
+ generateMipmaps: true
63
+ });
64
+ renderer.setRenderTarget(testRT);
65
+ const mesh = new Mesh(new PlaneGeometry(), new MeshBasicMaterial({ color: 0xffffff }));
66
+ renderer.render(mesh, camera);
67
+ renderer.setRenderTarget(null);
68
+ const out = getBufferForType(type, testRT.width, testRT.height);
69
+ renderer.readRenderTargetPixels(testRT, 0, 0, testRT.width, testRT.height, out);
70
+ testRT.dispose();
71
+ mesh.geometry.dispose();
72
+ mesh.material.dispose();
73
+ _canReadPixelsResult = out[0] !== 0;
74
+ return _canReadPixelsResult;
75
+ };
8
76
  /**
9
77
  * Utility structure used for rendering a texture with a material
10
78
  *
@@ -19,6 +87,7 @@ class QuadRenderer {
19
87
  */
20
88
  constructor(width, height, type, colorSpace, material, renderer) {
21
89
  this._rendererIsDisposable = false;
90
+ this._supportsReadPixels = true;
22
91
  /**
23
92
  * Renders the input texture using the specified material
24
93
  */
@@ -37,9 +106,6 @@ class QuadRenderer {
37
106
  this._height = height;
38
107
  this._type = type;
39
108
  this._colorSpace = colorSpace;
40
- if (type === HalfFloatType && navigator.userAgent.toLowerCase().includes('firefox')) {
41
- this._type = FloatType;
42
- }
43
109
  this._material = material;
44
110
  if (renderer) {
45
111
  this._renderer = renderer;
@@ -56,6 +122,22 @@ class QuadRenderer {
56
122
  this._camera.top = 0.5;
57
123
  this._camera.bottom = -0.5;
58
124
  this._camera.updateProjectionMatrix();
125
+ if (!canReadPixels(this._type, this._renderer, this._camera)) {
126
+ let alternativeType;
127
+ switch (this._type) {
128
+ case HalfFloatType:
129
+ alternativeType = this._renderer.extensions.has('EXT_color_buffer_float') ? FloatType : undefined;
130
+ break;
131
+ }
132
+ if (alternativeType !== undefined) {
133
+ console.warn(`This browser does not support reading pixels from ${this._type} RenderTargets, switching to ${FloatType}`);
134
+ this._type = alternativeType;
135
+ }
136
+ else {
137
+ this._supportsReadPixels = false;
138
+ console.warn('This browser dos not support toArray or toDataTexture, calls to those methods will result in an error thrown');
139
+ }
140
+ }
59
141
  this._quad = new Mesh(new PlaneGeometry(), this._material);
60
142
  this._quad.geometry.computeBoundingBox();
61
143
  this._scene.add(this._quad);
@@ -89,35 +171,13 @@ class QuadRenderer {
89
171
  /**
90
172
  * Obtains a Buffer containing the rendered texture.
91
173
  *
92
- * @returns
174
+ * @throws Error if the browser cannot read pixels from this RenderTarget type.
175
+ * @returns a TypedArray containing RGBA values from this renderer
93
176
  */
94
177
  toArray() {
95
- let out;
96
- switch (this._type) {
97
- case UnsignedByteType:
98
- out = new Uint8ClampedArray(this._width * this._height * 4);
99
- break;
100
- case HalfFloatType:
101
- out = new Uint16Array(this._width * this._height * 4);
102
- break;
103
- case UnsignedIntType:
104
- out = new Uint32Array(this._width * this._height * 4);
105
- break;
106
- case ByteType:
107
- out = new Int8Array(this._width * this._height * 4);
108
- break;
109
- case ShortType:
110
- out = new Int16Array(this._width * this._height * 4);
111
- break;
112
- case IntType:
113
- out = new Int32Array(this._width * this._height * 4);
114
- break;
115
- case FloatType:
116
- out = new Float32Array(this._width * this._height * 4);
117
- break;
118
- default:
119
- throw new Error('Unsupported data type');
120
- }
178
+ if (!this._supportsReadPixels)
179
+ throw new Error('Can\'t read pixels in this browser');
180
+ const out = getBufferForType(this._type, this._width, this._height);
121
181
  this._renderer.readRenderTargetPixels(this._renderTarget, 0, 0, this._width, this._height, out);
122
182
  return out;
123
183
  }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @monogrid/gainmap-js v1.1.1
2
+ * @monogrid/gainmap-js v2.0.1
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @monogrid/gainmap-js v1.1.1
2
+ * @monogrid/gainmap-js v2.0.1
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
@@ -22,6 +22,7 @@ export declare class QuadRenderer<TType extends TextureDataType, TMaterial exten
22
22
  private _height;
23
23
  private _type;
24
24
  private _colorSpace;
25
+ private _supportsReadPixels;
25
26
  /**
26
27
  *
27
28
  * @param sourceTexture
@@ -41,7 +42,8 @@ export declare class QuadRenderer<TType extends TextureDataType, TMaterial exten
41
42
  /**
42
43
  * Obtains a Buffer containing the rendered texture.
43
44
  *
44
- * @returns
45
+ * @throws Error if the browser cannot read pixels from this RenderTarget type.
46
+ * @returns a TypedArray containing RGBA values from this renderer
45
47
  */
46
48
  toArray(): TextureDataTypeToBufferType<TType>;
47
49
  toDataTexture(): DataTexture;
@@ -1,7 +1,7 @@
1
1
  import { HalfFloatType } from 'three';
2
- import { LoaderBase } from '../../core/LoaderBase';
3
2
  import { QuadRenderer } from '../../core/QuadRenderer';
4
3
  import { GainMapDecoderMaterial } from '../materials/GainMapDecoderMaterial';
4
+ import { LoaderBase } from './LoaderBase';
5
5
  /**
6
6
  * A Three.js Loader for the gain map format.
7
7
  *
@@ -1,7 +1,7 @@
1
1
  import { HalfFloatType } from 'three';
2
- import { LoaderBase } from '../../core/LoaderBase';
3
2
  import { QuadRenderer } from '../../core/QuadRenderer';
4
3
  import { GainMapDecoderMaterial } from '../../decode/materials/GainMapDecoderMaterial';
4
+ import { LoaderBase } from './LoaderBase';
5
5
  /**
6
6
  * A Three.js Loader for a JPEG with embedded gainmap metadata.
7
7
  *
@@ -1,7 +1,7 @@
1
1
  import { HalfFloatType, Loader, LoadingManager, WebGLRenderer } from 'three';
2
- import { type GainMapMetadata } from '../core/types';
3
- import { GainMapDecoderMaterial } from '../decode/materials/GainMapDecoderMaterial';
4
- import { QuadRenderer } from './QuadRenderer';
2
+ import { QuadRenderer } from '../../core/QuadRenderer';
3
+ import { type GainMapMetadata } from '../../core/types';
4
+ import { GainMapDecoderMaterial } from '../materials/GainMapDecoderMaterial';
5
5
  export declare class LoaderBase<TUrl = string> extends Loader<QuadRenderer<typeof HalfFloatType, GainMapDecoderMaterial>, TUrl> {
6
6
  private renderer;
7
7
  /**
package/dist/decode.js CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
- * @monogrid/gainmap-js v1.1.1
2
+ * @monogrid/gainmap-js v2.0.1
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
6
- import { Q as QuadRenderer } from './QuadRenderer-IDZuHqZM.js';
6
+ import { Q as QuadRenderer } from './QuadRenderer-XJq0chjd.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
  *
@@ -518,19 +540,35 @@ class LoaderBase extends Loader {
518
540
  */
519
541
  async render(quadRenderer, gainMapBuffer, sdrBuffer, metadata) {
520
542
  const gainMapBlob = new Blob([gainMapBuffer], { type: 'image/jpeg' });
521
- // TODO: figure out why result.sdr is not usable here, problem is in the libultrahdr-wasm repo
522
- // we use the original image buffer instead
523
543
  const sdrBlob = new Blob([sdrBuffer], { type: 'image/jpeg' });
524
- const [gainMapImageBitmap, sdrImageBitmap] = await Promise.all([
525
- createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }),
526
- createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
527
- ]);
528
- 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;
529
566
  gainMap.needsUpdate = true;
530
- 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;
531
569
  sdr.needsUpdate = true;
532
- quadRenderer.width = sdrImageBitmap.width;
533
- quadRenderer.height = sdrImageBitmap.height;
570
+ quadRenderer.width = sdrImage.width;
571
+ quadRenderer.height = sdrImage.height;
534
572
  quadRenderer.material.gainMap = gainMap;
535
573
  quadRenderer.material.sdr = sdr;
536
574
  quadRenderer.material.gainMapMin = metadata.gainMapMin;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @monogrid/gainmap-js v1.1.1
2
+ * @monogrid/gainmap-js v2.0.1
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
@@ -9,6 +9,74 @@
9
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@monogrid/gainmap-js"] = {}, global.three));
10
10
  })(this, (function (exports, three) { 'use strict';
11
11
 
12
+ const getBufferForType = (type, width, height) => {
13
+ let out;
14
+ switch (type) {
15
+ case three.UnsignedByteType:
16
+ out = new Uint8ClampedArray(width * height * 4);
17
+ break;
18
+ case three.HalfFloatType:
19
+ out = new Uint16Array(width * height * 4);
20
+ break;
21
+ case three.UnsignedIntType:
22
+ out = new Uint32Array(width * height * 4);
23
+ break;
24
+ case three.ByteType:
25
+ out = new Int8Array(width * height * 4);
26
+ break;
27
+ case three.ShortType:
28
+ out = new Int16Array(width * height * 4);
29
+ break;
30
+ case three.IntType:
31
+ out = new Int32Array(width * height * 4);
32
+ break;
33
+ case three.FloatType:
34
+ out = new Float32Array(width * height * 4);
35
+ break;
36
+ default:
37
+ throw new Error('Unsupported data type');
38
+ }
39
+ return out;
40
+ };
41
+ let _canReadPixelsResult;
42
+ /**
43
+ * Test if this browser implementation can correctly read pixels from the specified
44
+ * Render target type.
45
+ *
46
+ * Runs only once
47
+ *
48
+ * @param type
49
+ * @param renderer
50
+ * @param camera
51
+ * @returns
52
+ */
53
+ const canReadPixels = (type, renderer, camera) => {
54
+ if (_canReadPixelsResult !== undefined)
55
+ return _canReadPixelsResult;
56
+ const testRT = new three.WebGLRenderTarget(1, 1, {
57
+ type,
58
+ colorSpace: three.NoColorSpace,
59
+ format: three.RGBAFormat,
60
+ magFilter: three.LinearFilter,
61
+ minFilter: three.LinearFilter,
62
+ wrapS: three.RepeatWrapping,
63
+ wrapT: three.RepeatWrapping,
64
+ depthBuffer: false,
65
+ stencilBuffer: false,
66
+ generateMipmaps: true
67
+ });
68
+ renderer.setRenderTarget(testRT);
69
+ const mesh = new three.Mesh(new three.PlaneGeometry(), new three.MeshBasicMaterial({ color: 0xffffff }));
70
+ renderer.render(mesh, camera);
71
+ renderer.setRenderTarget(null);
72
+ const out = getBufferForType(type, testRT.width, testRT.height);
73
+ renderer.readRenderTargetPixels(testRT, 0, 0, testRT.width, testRT.height, out);
74
+ testRT.dispose();
75
+ mesh.geometry.dispose();
76
+ mesh.material.dispose();
77
+ _canReadPixelsResult = out[0] !== 0;
78
+ return _canReadPixelsResult;
79
+ };
12
80
  /**
13
81
  * Utility structure used for rendering a texture with a material
14
82
  *
@@ -23,6 +91,7 @@
23
91
  */
24
92
  constructor(width, height, type, colorSpace, material, renderer) {
25
93
  this._rendererIsDisposable = false;
94
+ this._supportsReadPixels = true;
26
95
  /**
27
96
  * Renders the input texture using the specified material
28
97
  */
@@ -41,9 +110,6 @@
41
110
  this._height = height;
42
111
  this._type = type;
43
112
  this._colorSpace = colorSpace;
44
- if (type === three.HalfFloatType && navigator.userAgent.toLowerCase().includes('firefox')) {
45
- this._type = three.FloatType;
46
- }
47
113
  this._material = material;
48
114
  if (renderer) {
49
115
  this._renderer = renderer;
@@ -60,6 +126,22 @@
60
126
  this._camera.top = 0.5;
61
127
  this._camera.bottom = -0.5;
62
128
  this._camera.updateProjectionMatrix();
129
+ if (!canReadPixels(this._type, this._renderer, this._camera)) {
130
+ let alternativeType;
131
+ switch (this._type) {
132
+ case three.HalfFloatType:
133
+ alternativeType = this._renderer.extensions.has('EXT_color_buffer_float') ? three.FloatType : undefined;
134
+ break;
135
+ }
136
+ if (alternativeType !== undefined) {
137
+ console.warn(`This browser does not support reading pixels from ${this._type} RenderTargets, switching to ${three.FloatType}`);
138
+ this._type = alternativeType;
139
+ }
140
+ else {
141
+ this._supportsReadPixels = false;
142
+ console.warn('This browser dos not support toArray or toDataTexture, calls to those methods will result in an error thrown');
143
+ }
144
+ }
63
145
  this._quad = new three.Mesh(new three.PlaneGeometry(), this._material);
64
146
  this._quad.geometry.computeBoundingBox();
65
147
  this._scene.add(this._quad);
@@ -93,35 +175,13 @@
93
175
  /**
94
176
  * Obtains a Buffer containing the rendered texture.
95
177
  *
96
- * @returns
178
+ * @throws Error if the browser cannot read pixels from this RenderTarget type.
179
+ * @returns a TypedArray containing RGBA values from this renderer
97
180
  */
98
181
  toArray() {
99
- let out;
100
- switch (this._type) {
101
- case three.UnsignedByteType:
102
- out = new Uint8ClampedArray(this._width * this._height * 4);
103
- break;
104
- case three.HalfFloatType:
105
- out = new Uint16Array(this._width * this._height * 4);
106
- break;
107
- case three.UnsignedIntType:
108
- out = new Uint32Array(this._width * this._height * 4);
109
- break;
110
- case three.ByteType:
111
- out = new Int8Array(this._width * this._height * 4);
112
- break;
113
- case three.ShortType:
114
- out = new Int16Array(this._width * this._height * 4);
115
- break;
116
- case three.IntType:
117
- out = new Int32Array(this._width * this._height * 4);
118
- break;
119
- case three.FloatType:
120
- out = new Float32Array(this._width * this._height * 4);
121
- break;
122
- default:
123
- throw new Error('Unsupported data type');
124
- }
182
+ if (!this._supportsReadPixels)
183
+ throw new Error('Can\'t read pixels in this browser');
184
+ const out = getBufferForType(this._type, this._width, this._height);
125
185
  this._renderer.readRenderTargetPixels(this._renderTarget, 0, 0, this._width, this._height, out);
126
186
  return out;
127
187
  }
@@ -188,6 +248,13 @@ void main() {
188
248
  }
189
249
  `;
190
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
+
191
258
  uniform sampler2D sdr;
192
259
  uniform sampler2D gainMap;
193
260
  uniform vec3 gamma;
@@ -205,7 +272,7 @@ void main() {
205
272
  vec3 logRecovery = pow(recovery, gamma);
206
273
  vec3 logBoost = gainMapMin * (1.0 - logRecovery) + gainMapMax * logRecovery;
207
274
  vec3 hdrColor = (rgb + offsetSdr) * exp2(logBoost * weightFactor) - offsetHdr;
208
- gl_FragColor = vec4(hdrColor, 1.0);
275
+ gl_FragColor = vec4( min(FLT_MAX, hdrColor), 1.0 );
209
276
  }
210
277
  `;
211
278
  /**
@@ -307,11 +374,12 @@ void main() {
307
374
  */
308
375
  get maxDisplayBoost() { return this._maxDisplayBoost; }
309
376
  set maxDisplayBoost(value) {
310
- this._maxDisplayBoost = value;
377
+ this._maxDisplayBoost = Math.max(1, Math.min(65504, value));
311
378
  this.calculateWeight();
312
379
  }
313
380
  calculateWeight() {
314
- 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));
315
383
  }
316
384
  }
317
385
 
@@ -651,6 +719,20 @@ void main() {
651
719
  };
652
720
  };
653
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
+ };
654
736
  class LoaderBase extends three.Loader {
655
737
  /**
656
738
  *
@@ -691,19 +773,35 @@ void main() {
691
773
  */
692
774
  async render(quadRenderer, gainMapBuffer, sdrBuffer, metadata) {
693
775
  const gainMapBlob = new Blob([gainMapBuffer], { type: 'image/jpeg' });
694
- // TODO: figure out why result.sdr is not usable here, problem is in the libultrahdr-wasm repo
695
- // we use the original image buffer instead
696
776
  const sdrBlob = new Blob([sdrBuffer], { type: 'image/jpeg' });
697
- const [gainMapImageBitmap, sdrImageBitmap] = await Promise.all([
698
- createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }),
699
- createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
700
- ]);
701
- 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;
702
799
  gainMap.needsUpdate = true;
703
- 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;
704
802
  sdr.needsUpdate = true;
705
- quadRenderer.width = sdrImageBitmap.width;
706
- quadRenderer.height = sdrImageBitmap.height;
803
+ quadRenderer.width = sdrImage.width;
804
+ quadRenderer.height = sdrImage.height;
707
805
  quadRenderer.material.gainMap = gainMap;
708
806
  quadRenderer.material.sdr = sdr;
709
807
  quadRenderer.material.gainMapMin = metadata.gainMapMin;
package/dist/encode.js CHANGED
@@ -1,11 +1,11 @@
1
1
  /**
2
- * @monogrid/gainmap-js v1.1.1
2
+ * @monogrid/gainmap-js v2.0.1
3
3
  * With ❤️, by MONOGRID <hello@mono-grid.com>
4
4
  */
5
5
 
6
- import { c as compress } from './compress-Cw5IwRTb.js';
6
+ import { c as compress } from './compress-OWkN-Bv4.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-IDZuHqZM.js';
8
+ import { Q as QuadRenderer } from './QuadRenderer-XJq0chjd.js';
9
9
 
10
10
  /**
11
11
  * Utility function to obtain a `DataTexture` from various input formats