@monogrid/gainmap-js 3.6.0 → 3.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +86 -13
- package/dist/GainMapNode-CntuYSg-.js +338 -0
- package/dist/GainMapNode-DtVNqKtN.js +5 -0
- package/dist/{warn-deprecated-Bk17fVbj.js → Loader-D82gF-Rq.js} +3 -19
- package/dist/{warn-deprecated-D0kPpXtA.js → Loader-EFF6JZel.js} +2 -2
- package/dist/{QuadRenderer-CXtNzoCY.js → QuadRenderer-DD6leG7y.js} +1 -1
- package/dist/{QuadRenderer-DcBTBLz6.js → QuadRenderer-DoK2pW5M.js} +1 -1
- package/dist/{_commonjsHelpers-CnYhShyx.js → _commonjsHelpers-C0JuzTK_.js} +1 -1
- package/dist/{_commonjsHelpers-Cdw2adc2.js → _commonjsHelpers-p2JmW4Hh.js} +1 -1
- package/dist/{compress-C42bpk6O.js → compress-BTsSIi_B.js} +1 -1
- package/dist/{compress-CzMz2bpX.js → compress-PmebyFxR.js} +1 -1
- package/dist/core/webgpu/RenderTargetNode.d.ts +79 -0
- package/dist/decode/shared/utils/warn-deprecated.d.ts +1 -1
- package/dist/decode/webgl/decode.d.ts +0 -1
- package/dist/decode/webgl/index.d.ts +0 -1
- package/dist/decode/webgl/loaders/GainMapLoader.d.ts +1 -3
- package/dist/decode/webgl/loaders/HDRJPGLoader.d.ts +1 -3
- package/dist/decode/webgl/materials/GainMapDecoderMaterial.d.ts +0 -1
- package/dist/decode/webgpu/nodes/GainMapNode.d.ts +17 -18
- package/dist/decode.js +5 -18
- package/dist/decode.min.js +2 -2
- package/dist/decode.umd.cjs +1 -30
- package/dist/decode.umd.min.cjs +2 -2
- package/dist/decode.webgpu.js +41 -228
- package/dist/decode.webgpu.min.js +2 -2
- package/dist/decode.webgpu.umd.cjs +186 -62
- package/dist/decode.webgpu.umd.min.cjs +2 -2
- package/dist/encode/webgpu/GainMapEncoder.d.ts +142 -0
- package/dist/encode/webgpu/find-texture-min-max.d.ts +27 -0
- package/dist/encode/webgpu/index.d.ts +8 -0
- package/dist/encode/webgpu/nodes/GainMapEncoderNode.d.ts +92 -0
- package/dist/encode/webgpu/nodes/SDRRenditionNode.d.ts +120 -0
- package/dist/encode/webgpu/read-render-target-pixels.d.ts +11 -0
- package/dist/encode.js +5 -36
- package/dist/encode.min.js +2 -2
- package/dist/encode.umd.cjs +1 -1
- package/dist/encode.umd.min.cjs +1 -1
- package/dist/encode.webgpu.js +635 -0
- package/dist/encode.webgpu.min.js +5 -0
- package/dist/encode.webgpu.umd.cjs +1081 -0
- package/dist/encode.webgpu.umd.min.cjs +5 -0
- package/dist/get-data-texture-CkaFVcv-.js +5 -0
- package/dist/get-data-texture-DkHkbczN.js +40 -0
- package/dist/{iso-gainmap-metadata-CEaaU7RT.js → iso-gainmap-metadata-BJxZGPtk.js} +1 -1
- package/dist/{iso-gainmap-metadata-ByNqnUPn.js → iso-gainmap-metadata-Bx6ha9th.js} +1 -1
- package/dist/libultrahdr.js +2 -2
- package/dist/libultrahdr.min.js +2 -2
- package/dist/libultrahdr.umd.cjs +1 -1
- package/dist/libultrahdr.umd.min.cjs +1 -1
- package/dist/worker-interface.js +2 -2
- package/dist/worker-interface.min.js +2 -2
- package/dist/worker-interface.umd.cjs +1 -1
- package/dist/worker-interface.umd.min.cjs +1 -1
- package/dist/worker.js +3 -3
- package/dist/worker.min.js +2 -2
- package/dist/worker.umd.cjs +1 -1
- package/dist/worker.umd.min.cjs +1 -1
- package/package.json +10 -2
package/dist/decode.webgpu.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @monogrid/gainmap-js v3.
|
|
2
|
+
* @monogrid/gainmap-js v3.7.0
|
|
3
3
|
* With ❤️, by MONOGRID <gainmap@monogrid.com>
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
export { M as MPFExtractor, b as extractISO, d as extractXMP } from './
|
|
8
|
-
import { ClampToEdgeWrapping, LinearFilter, Scene, OrthographicCamera, Mesh, PlaneGeometry, RenderTarget, RGBAFormat, UVMapping, WebGPURenderer, DataTexture, LinearSRGBColorSpace, ShaderMaterial, Texture, MeshBasicNodeMaterial, NoBlending, Vector3, FileLoader,
|
|
6
|
+
import { c as createDecodeFunction, L as LoaderBaseShared, p as processImages, a as createTextures, e as extractGainmapFromJPEG, X as XMPMetadataNotFoundError, G as GainMapNotFoundError } from './Loader-D82gF-Rq.js';
|
|
7
|
+
export { M as MPFExtractor, b as extractISO, d as extractXMP } from './Loader-D82gF-Rq.js';
|
|
8
|
+
import { ClampToEdgeWrapping, LinearFilter, Scene, OrthographicCamera, Mesh, PlaneGeometry, RenderTarget, RGBAFormat, UVMapping, WebGPURenderer, DataTexture, LinearSRGBColorSpace, ShaderMaterial, Texture, MeshBasicNodeMaterial, NoBlending, Vector3, FileLoader, Loader } from 'three/webgpu';
|
|
9
9
|
import 'three';
|
|
10
|
-
import { vec3, texture, uniform
|
|
11
|
-
import './
|
|
10
|
+
import { vec3, texture, uniform } from 'three/tsl';
|
|
11
|
+
import { G as GainMapNode } from './GainMapNode-CntuYSg-.js';
|
|
12
|
+
export { g as gainMapNode } from './GainMapNode-CntuYSg-.js';
|
|
13
|
+
import './iso-gainmap-metadata-Bx6ha9th.js';
|
|
12
14
|
|
|
13
15
|
/**
|
|
14
16
|
* Utility class used for rendering a texture with a material (WebGPU version)
|
|
@@ -115,7 +117,20 @@ class QuadRenderer {
|
|
|
115
117
|
if (!this._supportsReadPixels)
|
|
116
118
|
throw new Error('Can\'t read pixels in this browser');
|
|
117
119
|
const out = await this._renderer.readRenderTargetPixelsAsync(this._renderTarget, 0, 0, this._width, this._height);
|
|
118
|
-
|
|
120
|
+
if (!this._renderer.backend.isWebGPUBackend) {
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
// the WebGPU backend returns rows top-down, each padded to 256 bytes,
|
|
124
|
+
// the WebGL backend returns tightly packed rows bottom-up:
|
|
125
|
+
// repack to the WebGL layout so both backends return the same data
|
|
126
|
+
const rowLength = this._width * 4;
|
|
127
|
+
const paddedRowLength = Math.ceil(rowLength * out.BYTES_PER_ELEMENT / 256) * 256 / out.BYTES_PER_ELEMENT;
|
|
128
|
+
const packed = new out.constructor(rowLength * this._height);
|
|
129
|
+
for (let y = 0; y < this._height; y++) {
|
|
130
|
+
const start = (this._height - 1 - y) * paddedRowLength;
|
|
131
|
+
packed.set(out.subarray(start, start + rowLength), y * rowLength);
|
|
132
|
+
}
|
|
133
|
+
return packed;
|
|
119
134
|
}
|
|
120
135
|
/**
|
|
121
136
|
* Performs a readPixel operation in the renderTarget
|
|
@@ -236,10 +251,26 @@ class QuadRenderer {
|
|
|
236
251
|
get colorSpace() { return this._colorSpace; }
|
|
237
252
|
}
|
|
238
253
|
|
|
254
|
+
const warned = new Set();
|
|
255
|
+
/**
|
|
256
|
+
* Warns once per API that a render target based decode API is going away in v4.0.0
|
|
257
|
+
*
|
|
258
|
+
* @param name deprecated API
|
|
259
|
+
* @param entry entry point exposing the deprecated API
|
|
260
|
+
* @param replacement node based API replacing it, from `@monogrid/gainmap-js/webgpu`
|
|
261
|
+
*/
|
|
262
|
+
const warnDeprecated = (name, entry, replacement) => {
|
|
263
|
+
const key = `${entry}:${name}`;
|
|
264
|
+
if (warned.has(key))
|
|
265
|
+
return;
|
|
266
|
+
warned.add(key);
|
|
267
|
+
console.warn(`gainmap-js: ${name} from '${entry}' is deprecated and will be removed in v4.0.0. Use ${replacement} from '@monogrid/gainmap-js/webgpu' with WebGPURenderer (which also has a WebGL2 backend) instead. It is not a drop-in replacement (it returns a GainMapNode, not a render target), see https://github.com/MONOGRID/gainmap-js#migrating-to-v400`);
|
|
268
|
+
};
|
|
269
|
+
|
|
239
270
|
// min half float value
|
|
240
271
|
const HALF_FLOAT_MIN = vec3(-65504, -65504, -65504);
|
|
241
272
|
// max half float value
|
|
242
|
-
const HALF_FLOAT_MAX
|
|
273
|
+
const HALF_FLOAT_MAX = vec3(65504, 65504, 65504);
|
|
243
274
|
/**
|
|
244
275
|
* A Material which is able to decode the Gainmap into a full HDR Representation using TSL (Three.js Shading Language)
|
|
245
276
|
*
|
|
@@ -294,7 +325,7 @@ class GainMapDecoderMaterial extends MeshBasicNodeMaterial {
|
|
|
294
325
|
// the mathematically equivalent pow(2, x) which has proper vec3 typings.
|
|
295
326
|
const gain = vec3(2).pow(logBoost.mul(this._weightFactorUniform));
|
|
296
327
|
const hdrColor = rgb.add(this._offsetSdrUniform).mul(gain).sub(this._offsetHdrUniform);
|
|
297
|
-
const clampedHdrColor = hdrColor.min(HALF_FLOAT_MAX
|
|
328
|
+
const clampedHdrColor = hdrColor.min(HALF_FLOAT_MAX).max(HALF_FLOAT_MIN);
|
|
298
329
|
this.colorNode = clampedHdrColor;
|
|
299
330
|
}
|
|
300
331
|
get sdr() { return this._sdrTexture.value; }
|
|
@@ -690,224 +721,6 @@ class GainMapLoader extends LoaderBaseWebGPU {
|
|
|
690
721
|
}
|
|
691
722
|
}
|
|
692
723
|
|
|
693
|
-
// max half float value
|
|
694
|
-
const HALF_FLOAT_MAX = 65504;
|
|
695
|
-
/**
|
|
696
|
-
* A TSL texture node holding the HDR image decoded from an SDR image + gain map.
|
|
697
|
-
*
|
|
698
|
-
* The decode runs on the GPU into a HalfFloat render target, and only when something changed
|
|
699
|
-
* (textures loaded/swapped or any uniform value changed): sampling the node costs a single
|
|
700
|
-
* texture read, like any other texture.
|
|
701
|
-
*
|
|
702
|
-
* The node updates itself before rendering whenever it is part of a rendered node graph
|
|
703
|
-
* (material, `scene.backgroundNode`, ...). When only its texture is used (`node.value`,
|
|
704
|
-
* e.g. `scene.environment = node.value`) call {@link GainMapNode.decode} yourself.
|
|
705
|
-
*
|
|
706
|
-
* Re-decoding also flags the texture for PMREM regeneration,
|
|
707
|
-
* so environment maps are recomputed only when the image actually changes.
|
|
708
|
-
*
|
|
709
|
-
* `node.value` gets its final size once loaded: three.js PMREMs don't follow texture resizes,
|
|
710
|
-
* so assign it to `scene.environment` (or `pmremTexture()`) after load.
|
|
711
|
-
*
|
|
712
|
-
* @category Nodes
|
|
713
|
-
* @group Nodes
|
|
714
|
-
*
|
|
715
|
-
* @example
|
|
716
|
-
* import { GainMapNode } from '@monogrid/gainmap-js/webgpu'
|
|
717
|
-
* import { equirectUV } from 'three/tsl'
|
|
718
|
-
*
|
|
719
|
-
* const hdr = new GainMapNode()
|
|
720
|
-
* .setTextures(sdrTexture, gainMapTexture)
|
|
721
|
-
* .setMetadata(metadata)
|
|
722
|
-
*
|
|
723
|
-
* material.colorNode = hdr
|
|
724
|
-
* scene.backgroundNode = hdr.sample(equirectUV())
|
|
725
|
-
* hdr.maxDisplayBoost.value = 2 // re-decodes once, on the next frame
|
|
726
|
-
*/
|
|
727
|
-
class GainMapNode extends TextureNode {
|
|
728
|
-
static get type() { return 'GainMapNode'; }
|
|
729
|
-
/**
|
|
730
|
-
* Shared textures and uniforms
|
|
731
|
-
*/
|
|
732
|
-
uniforms;
|
|
733
|
-
/**
|
|
734
|
-
* The render target holding the decoded HDR image
|
|
735
|
-
*/
|
|
736
|
-
renderTarget;
|
|
737
|
-
_quadMesh;
|
|
738
|
-
_material;
|
|
739
|
-
// filled by RendererUtils.resetRendererState
|
|
740
|
-
_rendererState = {};
|
|
741
|
-
_state = [];
|
|
742
|
-
constructor() {
|
|
743
|
-
const renderTarget = new RenderTarget(1, 1, {
|
|
744
|
-
type: HalfFloatType,
|
|
745
|
-
colorSpace: LinearSRGBColorSpace,
|
|
746
|
-
depthBuffer: false,
|
|
747
|
-
generateMipmaps: true,
|
|
748
|
-
minFilter: LinearMipmapLinearFilter,
|
|
749
|
-
magFilter: LinearFilter
|
|
750
|
-
});
|
|
751
|
-
super(renderTarget.texture);
|
|
752
|
-
this.renderTarget = renderTarget;
|
|
753
|
-
this.updateBeforeType = NodeUpdateType.RENDER;
|
|
754
|
-
const u = this.uniforms = {
|
|
755
|
-
sdr: texture(new Texture()),
|
|
756
|
-
gainMap: texture(new Texture()),
|
|
757
|
-
gamma: uniform(new Vector3(1, 1, 1)),
|
|
758
|
-
offsetSdr: uniform(new Vector3()),
|
|
759
|
-
offsetHdr: uniform(new Vector3()),
|
|
760
|
-
gainMapMin: uniform(new Vector3()),
|
|
761
|
-
gainMapMax: uniform(new Vector3(1, 1, 1)),
|
|
762
|
-
hdrCapacityMin: uniform(0),
|
|
763
|
-
hdrCapacityMax: uniform(1),
|
|
764
|
-
maxDisplayBoost: uniform(1)
|
|
765
|
-
};
|
|
766
|
-
const logRecovery = u.gainMap.rgb.pow(vec3(1).div(u.gamma));
|
|
767
|
-
const logBoost = mix(u.gainMapMin, u.gainMapMax, logRecovery);
|
|
768
|
-
const weight = saturate(log2(u.maxDisplayBoost).sub(u.hdrCapacityMin).div(u.hdrCapacityMax.sub(u.hdrCapacityMin)));
|
|
769
|
-
const hdr = u.sdr.rgb.add(u.offsetSdr).mul(exp2(logBoost.mul(weight))).sub(u.offsetHdr);
|
|
770
|
-
this._material = new MeshBasicNodeMaterial();
|
|
771
|
-
this._material.name = 'GainMapDecoder';
|
|
772
|
-
this._material.colorNode = vec4(hdr.clamp(-HALF_FLOAT_MAX, HALF_FLOAT_MAX), 1);
|
|
773
|
-
this._quadMesh = new QuadMesh(this._material);
|
|
774
|
-
}
|
|
775
|
-
/** The SDR rendition texture (sRGB) */
|
|
776
|
-
get sdr() { return this.uniforms.sdr.value; }
|
|
777
|
-
/** The gain map recovery texture (linear) */
|
|
778
|
-
get gainMap() { return this.uniforms.gainMap.value; }
|
|
779
|
-
/** @see {@link GainMapMetadata.gamma} */
|
|
780
|
-
get gamma() { return this.uniforms.gamma; }
|
|
781
|
-
/** @see {@link GainMapMetadata.offsetSdr} */
|
|
782
|
-
get offsetSdr() { return this.uniforms.offsetSdr; }
|
|
783
|
-
/** @see {@link GainMapMetadata.offsetHdr} */
|
|
784
|
-
get offsetHdr() { return this.uniforms.offsetHdr; }
|
|
785
|
-
/** @see {@link GainMapMetadata.gainMapMin} */
|
|
786
|
-
get gainMapMin() { return this.uniforms.gainMapMin; }
|
|
787
|
-
/** @see {@link GainMapMetadata.gainMapMax} */
|
|
788
|
-
get gainMapMax() { return this.uniforms.gainMapMax; }
|
|
789
|
-
/** @see {@link GainMapMetadata.hdrCapacityMin} */
|
|
790
|
-
get hdrCapacityMin() { return this.uniforms.hdrCapacityMin; }
|
|
791
|
-
/** @see {@link GainMapMetadata.hdrCapacityMax} */
|
|
792
|
-
get hdrCapacityMax() { return this.uniforms.hdrCapacityMax; }
|
|
793
|
-
/**
|
|
794
|
-
* @see {@link GainmapDecodingParameters.maxDisplayBoost}
|
|
795
|
-
* @remarks Non Logarithmic space
|
|
796
|
-
*/
|
|
797
|
-
get maxDisplayBoost() { return this.uniforms.maxDisplayBoost; }
|
|
798
|
-
/** Width of the SDR image, 0 until loaded */
|
|
799
|
-
get width() { return this.sdr.image?.width ?? 0; }
|
|
800
|
-
/** Height of the SDR image, 0 until loaded */
|
|
801
|
-
get height() { return this.sdr.image?.height ?? 0; }
|
|
802
|
-
/**
|
|
803
|
-
* Sets the SDR and gain map textures.
|
|
804
|
-
* The SDR texture is expected in sRGB color space, the gain map in linear.
|
|
805
|
-
*
|
|
806
|
-
* @param sdr
|
|
807
|
-
* @param gainMap
|
|
808
|
-
*/
|
|
809
|
-
setTextures(sdr, gainMap) {
|
|
810
|
-
this.uniforms.sdr.value = sdr;
|
|
811
|
-
this.uniforms.gainMap.value = gainMap;
|
|
812
|
-
// size the render target right away, so `value` has its final size before the first decode
|
|
813
|
-
if (this.width > 0 && this.height > 0)
|
|
814
|
-
this.renderTarget.setSize(this.width, this.height);
|
|
815
|
-
return this;
|
|
816
|
-
}
|
|
817
|
-
/**
|
|
818
|
-
* Copies the gain map metadata into the uniforms.
|
|
819
|
-
* Also sets `maxDisplayBoost` to fully restore the HDR range.
|
|
820
|
-
*
|
|
821
|
-
* @param metadata
|
|
822
|
-
*/
|
|
823
|
-
setMetadata(metadata) {
|
|
824
|
-
const u = this.uniforms;
|
|
825
|
-
u.gamma.value.fromArray(metadata.gamma);
|
|
826
|
-
u.offsetSdr.value.fromArray(metadata.offsetSdr);
|
|
827
|
-
u.offsetHdr.value.fromArray(metadata.offsetHdr);
|
|
828
|
-
u.gainMapMin.value.fromArray(metadata.gainMapMin);
|
|
829
|
-
u.gainMapMax.value.fromArray(metadata.gainMapMax);
|
|
830
|
-
u.hdrCapacityMin.value = metadata.hdrCapacityMin;
|
|
831
|
-
u.hdrCapacityMax.value = metadata.hdrCapacityMax;
|
|
832
|
-
u.maxDisplayBoost.value = Math.pow(2, metadata.hdrCapacityMax);
|
|
833
|
-
return this;
|
|
834
|
-
}
|
|
835
|
-
_currentState() {
|
|
836
|
-
const u = this.uniforms;
|
|
837
|
-
return [
|
|
838
|
-
this.sdr.id, this.sdr.version, this.gainMap.id, this.gainMap.version,
|
|
839
|
-
...u.gamma.value.toArray(), ...u.offsetSdr.value.toArray(), ...u.offsetHdr.value.toArray(),
|
|
840
|
-
...u.gainMapMin.value.toArray(), ...u.gainMapMax.value.toArray(),
|
|
841
|
-
u.hdrCapacityMin.value, u.hdrCapacityMax.value, u.maxDisplayBoost.value
|
|
842
|
-
];
|
|
843
|
-
}
|
|
844
|
-
/**
|
|
845
|
-
* Decodes the image into {@link GainMapNode.renderTarget}, only if something changed since the last decode.
|
|
846
|
-
*
|
|
847
|
-
* Called automatically when the node is part of a rendered node graph.
|
|
848
|
-
*
|
|
849
|
-
* @param renderer
|
|
850
|
-
* @returns `true` if the image was decoded
|
|
851
|
-
*/
|
|
852
|
-
decode(renderer) {
|
|
853
|
-
// not loaded yet
|
|
854
|
-
if (this.width === 0 || this.height === 0)
|
|
855
|
-
return false;
|
|
856
|
-
const state = this._currentState();
|
|
857
|
-
if (state.length === this._state.length && state.every((v, i) => v === this._state[i]))
|
|
858
|
-
return false;
|
|
859
|
-
this._state = state;
|
|
860
|
-
RendererUtils.resetRendererState(renderer, this._rendererState);
|
|
861
|
-
renderer.setRenderTarget(this.renderTarget);
|
|
862
|
-
this._quadMesh.render(renderer);
|
|
863
|
-
RendererUtils.restoreRendererState(renderer, this._rendererState);
|
|
864
|
-
this.value.needsPMREMUpdate = true;
|
|
865
|
-
return true;
|
|
866
|
-
}
|
|
867
|
-
updateBefore(frame) {
|
|
868
|
-
if (frame.renderer)
|
|
869
|
-
this.decode(frame.renderer);
|
|
870
|
-
return undefined;
|
|
871
|
-
}
|
|
872
|
-
set value(value) {
|
|
873
|
-
if (this.renderTarget && value !== this.renderTarget.texture) {
|
|
874
|
-
throw new Error('GainMapNode: the value of this node is its render target texture and can\'t be replaced');
|
|
875
|
-
}
|
|
876
|
-
}
|
|
877
|
-
get value() {
|
|
878
|
-
return this.renderTarget?.texture;
|
|
879
|
-
}
|
|
880
|
-
/**
|
|
881
|
-
* Clones created by `sample()` & co. are plain TextureNodes reading this node's texture,
|
|
882
|
-
* they keep it updated before rendering as well
|
|
883
|
-
*/
|
|
884
|
-
clone() {
|
|
885
|
-
const node = new TextureNode(this.value, this.uvNode, this.levelNode, this.biasNode);
|
|
886
|
-
node.sampler = this.sampler;
|
|
887
|
-
node.updateBeforeType = NodeUpdateType.RENDER;
|
|
888
|
-
node.updateBefore = (frame) => this.updateBefore(frame);
|
|
889
|
-
return node;
|
|
890
|
-
}
|
|
891
|
-
/**
|
|
892
|
-
* Disposes the render target, the SDR and gain map textures
|
|
893
|
-
*/
|
|
894
|
-
dispose() {
|
|
895
|
-
this.renderTarget.dispose();
|
|
896
|
-
this._material.dispose();
|
|
897
|
-
this.sdr.dispose();
|
|
898
|
-
this.gainMap.dispose();
|
|
899
|
-
super.dispose();
|
|
900
|
-
}
|
|
901
|
-
}
|
|
902
|
-
/**
|
|
903
|
-
* TSL function creating a {@link GainMapNode} from an SDR texture, a gain map texture and its metadata
|
|
904
|
-
*
|
|
905
|
-
* @param sdr
|
|
906
|
-
* @param gainMap
|
|
907
|
-
* @param metadata
|
|
908
|
-
*/
|
|
909
|
-
const gainMapNode = (sdr, gainMap, metadata) => new GainMapNode().setTextures(sdr, gainMap).setMetadata(metadata);
|
|
910
|
-
|
|
911
724
|
/**
|
|
912
725
|
* Base class for loaders returning a {@link GainMapNode}
|
|
913
726
|
* @template TUrl - The type of URL used to load resources
|
|
@@ -1252,4 +1065,4 @@ class HDRJPGNodeLoader extends LoaderBaseNode {
|
|
|
1252
1065
|
}
|
|
1253
1066
|
}
|
|
1254
1067
|
|
|
1255
|
-
export { GainMapDecoderMaterial, GainMapLoader, GainMapNode, GainMapNodeLoader, GainMapNotFoundError, HDRJPGLoader, HDRJPGNodeLoader, HDRJPGLoader as JPEGRLoader, LoaderBaseShared, QuadRenderer, XMPMetadataNotFoundError, createDecodeFunction, createTextures, decode, extractGainmapFromJPEG,
|
|
1068
|
+
export { GainMapDecoderMaterial, GainMapLoader, GainMapNode, GainMapNodeLoader, GainMapNotFoundError, HDRJPGLoader, HDRJPGNodeLoader, HDRJPGLoader as JPEGRLoader, LoaderBaseShared, QuadRenderer, XMPMetadataNotFoundError, createDecodeFunction, createTextures, decode, extractGainmapFromJPEG, processImages };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @monogrid/gainmap-js v3.
|
|
2
|
+
* @monogrid/gainmap-js v3.7.0
|
|
3
3
|
* With ❤️, by MONOGRID <gainmap@monogrid.com>
|
|
4
4
|
*/
|
|
5
|
-
import{w as e,c as t,L as r,p as a,a as i,e as s,X as n,G as o}from"./warn-deprecated-D0kPpXtA.js";export{M as MPFExtractor,b as extractISO,d as extractXMP}from"./warn-deprecated-D0kPpXtA.js";import{ClampToEdgeWrapping as h,LinearFilter as p,Scene as m,OrthographicCamera as g,Mesh as f,PlaneGeometry as u,RenderTarget as l,RGBAFormat as c,UVMapping as _,WebGPURenderer as y,DataTexture as w,LinearSRGBColorSpace as x,ShaderMaterial as v,Texture as T,MeshBasicNodeMaterial as C,NoBlending as S,Vector3 as U,FileLoader as R,TextureNode as E,LinearMipmapLinearFilter as D,HalfFloatType as H,NodeUpdateType as O,QuadMesh as B,RendererUtils as F,Loader as P}from"three/webgpu";import"three";import{vec3 as q,texture as N,uniform as A,mix as I,saturate as z,log2 as L,exp2 as G,vec4 as j}from"three/tsl";import"./iso-gainmap-metadata-CEaaU7RT.js";class W{_renderer;_rendererIsDisposable=!1;_material;_scene;_camera;_quad;_renderTarget;_width;_height;_type;_colorSpace;_supportsReadPixels=!0;constructor(e){this._width=e.width,this._height=e.height,this._type=e.type,this._colorSpace=e.colorSpace;const t={format:c,depthBuffer:!1,stencilBuffer:!1,type:this._type,colorSpace:this._colorSpace,anisotropy:void 0!==e.renderTargetOptions?.anisotropy?e.renderTargetOptions?.anisotropy:1,generateMipmaps:void 0!==e.renderTargetOptions?.generateMipmaps&&e.renderTargetOptions?.generateMipmaps,magFilter:void 0!==e.renderTargetOptions?.magFilter?e.renderTargetOptions?.magFilter:p,minFilter:void 0!==e.renderTargetOptions?.minFilter?e.renderTargetOptions?.minFilter:p,samples:void 0!==e.renderTargetOptions?.samples?e.renderTargetOptions?.samples:void 0,wrapS:void 0!==e.renderTargetOptions?.wrapS?e.renderTargetOptions?.wrapS:h,wrapT:void 0!==e.renderTargetOptions?.wrapT?e.renderTargetOptions?.wrapT:h};this._material=e.material,e.renderer?this._renderer=e.renderer:(this._renderer=W.instantiateRenderer(),this._rendererIsDisposable=!0),this._scene=new m,this._camera=new g,this._camera.position.set(0,0,10),this._camera.left=-.5,this._camera.right=.5,this._camera.top=.5,this._camera.bottom=-.5,this._camera.updateProjectionMatrix(),this._quad=new f(new u,this._material),this._quad.geometry.computeBoundingBox(),this._scene.add(this._quad),this._renderTarget=new l(this.width,this.height,t),this._renderTarget.texture.mapping=void 0!==e.renderTargetOptions?.mapping?e.renderTargetOptions?.mapping:_}static instantiateRenderer(){const e=new y;return e.setSize(128,128),e}render=async()=>{this._renderer.hasInitialized()||await this._renderer.init(),this._renderer.setRenderTarget(this._renderTarget);try{this._renderer.render(this._scene,this._camera)}catch(e){throw this._renderer.setRenderTarget(null),e}this._renderer.setRenderTarget(null)};async toArray(){if(!this._supportsReadPixels)throw new Error("Can't read pixels in this browser");return await this._renderer.readRenderTargetPixelsAsync(this._renderTarget,0,0,this._width,this._height)}async toDataTexture(e){const t=new w(await this.toArray(),this.width,this.height,c,this._type,e?.mapping||_,e?.wrapS||h,e?.wrapT||h,e?.magFilter||p,e?.minFilter||p,e?.anisotropy||1,x);return t.flipY=void 0===e?.flipY||e?.flipY,t.generateMipmaps=void 0!==e?.generateMipmaps&&e?.generateMipmaps,t}disposeOnDemandRenderer(){this._renderer.setRenderTarget(null),this._rendererIsDisposable&&this._renderer.dispose().catch(console.error)}dispose(e){e&&this.renderTarget.dispose(),this.material instanceof v&&Object.values(this.material.uniforms).forEach(e=>{e.value instanceof T&&e.value.dispose()}),Object.values(this.material).forEach(e=>{e instanceof T&&e.dispose()}),this.material.dispose(),this._quad.geometry.dispose(),this.disposeOnDemandRenderer()}get width(){return this._width}set width(e){this._width=e,this._renderTarget.setSize(this._width,this._height)}get height(){return this._height}set height(e){this._height=e,this._renderTarget.setSize(this._width,this._height)}get renderer(){return this._renderer}get renderTarget(){return this._renderTarget}set renderTarget(e){this._renderTarget=e,this._width=e.width,this._height=e.height}get material(){return this._material}get type(){return this._type}get colorSpace(){return this._colorSpace}}const J=q(-65504,-65504,-65504),Q=q(65504,65504,65504);class Y extends C{_maxDisplayBoost;_hdrCapacityMin;_hdrCapacityMax;_gammaUniform;_offsetHdrUniform;_offsetSdrUniform;_gainMapMinUniform;_gainMapMaxUniform;_weightFactorUniform;_sdrTexture;_gainMapTexture;constructor({gamma:e,offsetHdr:t,offsetSdr:r,gainMapMin:a,gainMapMax:i,maxDisplayBoost:s,hdrCapacityMin:n,hdrCapacityMax:o,sdr:h,gainMap:d}){super(),this.name="GainMapDecoderMaterial",this.blending=S,this.depthTest=!1,this.depthWrite=!1,this._sdrTexture=N(h),this._gainMapTexture=N(d),this._gammaUniform=A(new U(1/e[0],1/e[1],1/e[2])),this._offsetHdrUniform=A(new U(t[0],t[1],t[2])),this._offsetSdrUniform=A(new U(r[0],r[1],r[2])),this._gainMapMinUniform=A(new U(a[0],a[1],a[2])),this._gainMapMaxUniform=A(new U(i[0],i[1],i[2]));const p=(Math.log2(s)-n)/(o-n);this._weightFactorUniform=A(p,"float"),this._maxDisplayBoost=s,this._hdrCapacityMin=n,this._hdrCapacityMax=o;const m=this._sdrTexture.rgb,g=this._gainMapTexture.rgb.pow(this._gammaUniform),f=this._gainMapMinUniform.mul(g.oneMinus()).add(this._gainMapMaxUniform.mul(g)),u=q(2).pow(f.mul(this._weightFactorUniform)),l=m.add(this._offsetSdrUniform).mul(u).sub(this._offsetHdrUniform).min(Q).max(J);this.colorNode=l}get sdr(){return this._sdrTexture.value}set sdr(e){this._sdrTexture.value=e}get gainMap(){return this._gainMapTexture.value}set gainMap(e){this._gainMapTexture.value=e}get offsetHdr(){return[this._offsetHdrUniform.value.x,this._offsetHdrUniform.value.y,this._offsetHdrUniform.value.z]}set offsetHdr(e){this._offsetHdrUniform.value.x=e[0],this._offsetHdrUniform.value.y=e[1],this._offsetHdrUniform.value.z=e[2]}get offsetSdr(){return[this._offsetSdrUniform.value.x,this._offsetSdrUniform.value.y,this._offsetSdrUniform.value.z]}set offsetSdr(e){this._offsetSdrUniform.value.x=e[0],this._offsetSdrUniform.value.y=e[1],this._offsetSdrUniform.value.z=e[2]}get gainMapMin(){return[this._gainMapMinUniform.value.x,this._gainMapMinUniform.value.y,this._gainMapMinUniform.value.z]}set gainMapMin(e){this._gainMapMinUniform.value.x=e[0],this._gainMapMinUniform.value.y=e[1],this._gainMapMinUniform.value.z=e[2]}get gainMapMax(){return[this._gainMapMaxUniform.value.x,this._gainMapMaxUniform.value.y,this._gainMapMaxUniform.value.z]}set gainMapMax(e){this._gainMapMaxUniform.value.x=e[0],this._gainMapMaxUniform.value.y=e[1],this._gainMapMaxUniform.value.z=e[2]}get gamma(){return[1/this._gammaUniform.value.x,1/this._gammaUniform.value.y,1/this._gammaUniform.value.z]}set gamma(e){this._gammaUniform.value.x=1/e[0],this._gammaUniform.value.y=1/e[1],this._gammaUniform.value.z=1/e[2]}get hdrCapacityMin(){return this._hdrCapacityMin}set hdrCapacityMin(e){this._hdrCapacityMin=e,this.calculateWeight()}get hdrCapacityMax(){return this._hdrCapacityMax}set hdrCapacityMax(e){this._hdrCapacityMax=e,this.calculateWeight()}get maxDisplayBoost(){return this._maxDisplayBoost}set maxDisplayBoost(e){this._maxDisplayBoost=Math.max(1,Math.min(65504,e)),this.calculateWeight()}calculateWeight(){const e=(Math.log2(this._maxDisplayBoost)-this._hdrCapacityMin)/(this._hdrCapacityMax-this._hdrCapacityMin);this._weightFactorUniform.value=Math.max(0,Math.min(1,e))}}const X=t({renderer:y,createMaterial:e=>new Y(e),createQuadRenderer:e=>new W(e)}),$=async t=>{if(e("decode()","@monogrid/gainmap-js/webgpu","gainMapNode()"),!t.renderer)throw new Error("Renderer is required for decode function");const r=X({...t,renderer:t.renderer});try{await r.render()}catch(e){throw r.disposeOnDemandRenderer(),e}return r};class k extends r{constructor(e,t){super({renderer:e,createMaterial:e=>new Y(e),createQuadRenderer:e=>new W(e)},t)}async render(e,t,r,a){const{sdrImage:i,gainMapImage:s,needsFlip:n}=await this.processImages(r,a,"from-image"),{gainMap:o,sdr:h}=this.createTextures(i,s,n);this.updateQuadRenderer(e,i,o,h,t),await e.render()}}class K extends k{constructor(t,r){super(t,r),e("GainMapLoader","@monogrid/gainmap-js/webgpu","GainMapNodeLoader")}load([e,t,r],a,i,s){const n=this.prepareQuadRenderer();let o,h,d;const p=async()=>{if(o&&h&&d){try{await this.render(n,d,o,h)}catch(a){return this.manager.itemError(e),this.manager.itemError(t),this.manager.itemError(r),"function"==typeof s&&s(a),void n.disposeOnDemandRenderer()}"function"==typeof a&&a(n),this.manager.itemEnd(e),this.manager.itemEnd(t),this.manager.itemEnd(r),n.disposeOnDemandRenderer()}};let m=!0,g=0,f=0,u=!0,l=0,c=0,M=!0,_=0,y=0;const w=()=>{if("function"==typeof i){i(new ProgressEvent("progress",{lengthComputable:m&&u&&M,loaded:f+c+y,total:g+l+_}))}};this.manager.itemStart(e),this.manager.itemStart(t),this.manager.itemStart(r);const x=new R(this._internalLoadingManager);x.setResponseType("arraybuffer"),x.setRequestHeader(this.requestHeader),x.setPath(this.path),x.setWithCredentials(this.withCredentials),x.load(e,async e=>{if("string"==typeof e)throw new Error("Invalid sdr buffer");o=e,await p()},e=>{m=e.lengthComputable,f=e.loaded,g=e.total,w()},t=>{this.manager.itemError(e),"function"==typeof s&&s(t)});const v=new R(this._internalLoadingManager);v.setResponseType("arraybuffer"),v.setRequestHeader(this.requestHeader),v.setPath(this.path),v.setWithCredentials(this.withCredentials),v.load(t,async e=>{if("string"==typeof e)throw new Error("Invalid gainmap buffer");h=e,await p()},e=>{u=e.lengthComputable,c=e.loaded,l=e.total,w()},e=>{this.manager.itemError(t),"function"==typeof s&&s(e)});const T=new R(this._internalLoadingManager);return T.setRequestHeader(this.requestHeader),T.setPath(this.path),T.setWithCredentials(this.withCredentials),T.load(r,async e=>{if("string"!=typeof e)throw new Error("Invalid metadata string");d=JSON.parse(e),await p()},e=>{M=e.lengthComputable,y=e.loaded,_=e.total,w()},e=>{this.manager.itemError(r),"function"==typeof s&&s(e)}),n}}class V extends E{static get type(){return"GainMapNode"}uniforms;renderTarget;_quadMesh;_material;_rendererState={};_state=[];constructor(){const e=new l(1,1,{type:H,colorSpace:x,depthBuffer:!1,generateMipmaps:!0,minFilter:D,magFilter:p});super(e.texture),this.renderTarget=e,this.updateBeforeType=O.RENDER;const t=this.uniforms={sdr:N(new T),gainMap:N(new T),gamma:A(new U(1,1,1)),offsetSdr:A(new U),offsetHdr:A(new U),gainMapMin:A(new U),gainMapMax:A(new U(1,1,1)),hdrCapacityMin:A(0),hdrCapacityMax:A(1),maxDisplayBoost:A(1)},r=t.gainMap.rgb.pow(q(1).div(t.gamma)),a=I(t.gainMapMin,t.gainMapMax,r),i=z(L(t.maxDisplayBoost).sub(t.hdrCapacityMin).div(t.hdrCapacityMax.sub(t.hdrCapacityMin))),s=t.sdr.rgb.add(t.offsetSdr).mul(G(a.mul(i))).sub(t.offsetHdr);this._material=new C,this._material.name="GainMapDecoder",this._material.colorNode=j(s.clamp(-65504,65504),1),this._quadMesh=new B(this._material)}get sdr(){return this.uniforms.sdr.value}get gainMap(){return this.uniforms.gainMap.value}get gamma(){return this.uniforms.gamma}get offsetSdr(){return this.uniforms.offsetSdr}get offsetHdr(){return this.uniforms.offsetHdr}get gainMapMin(){return this.uniforms.gainMapMin}get gainMapMax(){return this.uniforms.gainMapMax}get hdrCapacityMin(){return this.uniforms.hdrCapacityMin}get hdrCapacityMax(){return this.uniforms.hdrCapacityMax}get maxDisplayBoost(){return this.uniforms.maxDisplayBoost}get width(){return this.sdr.image?.width??0}get height(){return this.sdr.image?.height??0}setTextures(e,t){return this.uniforms.sdr.value=e,this.uniforms.gainMap.value=t,this.width>0&&this.height>0&&this.renderTarget.setSize(this.width,this.height),this}setMetadata(e){const t=this.uniforms;return t.gamma.value.fromArray(e.gamma),t.offsetSdr.value.fromArray(e.offsetSdr),t.offsetHdr.value.fromArray(e.offsetHdr),t.gainMapMin.value.fromArray(e.gainMapMin),t.gainMapMax.value.fromArray(e.gainMapMax),t.hdrCapacityMin.value=e.hdrCapacityMin,t.hdrCapacityMax.value=e.hdrCapacityMax,t.maxDisplayBoost.value=Math.pow(2,e.hdrCapacityMax),this}_currentState(){const e=this.uniforms;return[this.sdr.id,this.sdr.version,this.gainMap.id,this.gainMap.version,...e.gamma.value.toArray(),...e.offsetSdr.value.toArray(),...e.offsetHdr.value.toArray(),...e.gainMapMin.value.toArray(),...e.gainMapMax.value.toArray(),e.hdrCapacityMin.value,e.hdrCapacityMax.value,e.maxDisplayBoost.value]}decode(e){if(0===this.width||0===this.height)return!1;const t=this._currentState();return(t.length!==this._state.length||!t.every((e,t)=>e===this._state[t]))&&(this._state=t,F.resetRendererState(e,this._rendererState),e.setRenderTarget(this.renderTarget),this._quadMesh.render(e),F.restoreRendererState(e,this._rendererState),this.value.needsPMREMUpdate=!0,!0)}updateBefore(e){e.renderer&&this.decode(e.renderer)}set value(e){if(this.renderTarget&&e!==this.renderTarget.texture)throw new Error("GainMapNode: the value of this node is its render target texture and can't be replaced")}get value(){return this.renderTarget?.texture}clone(){const e=new E(this.value,this.uvNode,this.levelNode,this.biasNode);return e.sampler=this.sampler,e.updateBeforeType=O.RENDER,e.updateBefore=e=>this.updateBefore(e),e}dispose(){this.renderTarget.dispose(),this._material.dispose(),this.sdr.dispose(),this.gainMap.dispose(),super.dispose()}}const Z=(e,t,r)=>(new V).setTextures(e,t).setMetadata(r);class ee extends P{createFileLoader(e){const t=new R(this.manager);return e&&t.setResponseType(e),t.setRequestHeader(this.requestHeader),t.setPath(this.path),t.setWithCredentials(this.withCredentials),t}async fillNode(e,t,r,s){const{sdrImage:n,gainMapImage:o}=await a(r,s,"from-image"),{sdr:h,gainMap:d}=i(n,o,!0);e.setTextures(h,d).setMetadata(t)}}class te extends ee{load(e,t,r,a){const i=new V,s=e.map(()=>({lengthComputable:!0,loaded:0,total:0})),n=(e,t,a)=>new Promise((i,n)=>{this.manager.itemStart(e),this.createFileLoader(a).load(e,i,e=>{s[t]={lengthComputable:e.lengthComputable,loaded:e.loaded,total:e.total},"function"==typeof r&&r(new ProgressEvent("progress",{lengthComputable:s.every(e=>e.lengthComputable),loaded:s.reduce((e,t)=>e+t.loaded,0),total:s.reduce((e,t)=>e+t.total,0)}))},n)}),[o,h,d]=e;return Promise.all([n(o,0,"arraybuffer"),n(h,1,"arraybuffer"),n(d,2)]).then(async([e,t,r])=>{if("string"==typeof e||"string"==typeof t||"string"!=typeof r)throw new Error("Invalid response types");const a=JSON.parse(r);await this.fillNode(i,a,e,t)}).then(()=>{"function"==typeof t&&t(i),e.forEach(e=>this.manager.itemEnd(e))},t=>{e.forEach(e=>this.manager.itemError(e)),"function"==typeof a&&a(t)}),i}}class re extends k{constructor(t,r){super(t,r),e("HDRJPGLoader","@monogrid/gainmap-js/webgpu","HDRJPGNodeLoader")}load(e,t,r,a){const i=this.prepareQuadRenderer(),h=new R(this._internalLoadingManager);return h.setResponseType("arraybuffer"),h.setRequestHeader(this.requestHeader),h.setPath(this.path),h.setWithCredentials(this.withCredentials),this.manager.itemStart(e),h.load(e,async r=>{if("string"==typeof r)throw new Error("Invalid buffer, received [string], was expecting [ArrayBuffer]");const h=new Uint8Array(r);let d,p,m;try{const e=await s(h);d=e.sdr,p=e.gainMap,m=e.metadata}catch(t){if(!(t instanceof n||t instanceof o))throw t;console.warn(`Failure to reconstruct an HDR image from ${e}: Gain map metadata not found in the file, HDRJPGLoader will render the SDR jpeg`),m={gainMapMin:[0,0,0],gainMapMax:[1,1,1],gamma:[1,1,1],hdrCapacityMin:0,hdrCapacityMax:1,offsetHdr:[0,0,0],offsetSdr:[0,0,0]},d=h}try{await this.render(i,m,d.buffer,p?.buffer)}catch(t){return this.manager.itemError(e),"function"==typeof a&&a(t),void i.disposeOnDemandRenderer()}"function"==typeof t&&t(i),this.manager.itemEnd(e),i.disposeOnDemandRenderer()},r,t=>{this.manager.itemError(e),"function"==typeof a&&a(t)}),i}}class ae extends ee{load(e,t,r,a){const i=new V;return this.manager.itemStart(e),this.createFileLoader("arraybuffer").load(e,async r=>{try{if("string"==typeof r)throw new Error("Invalid buffer, received [string], was expecting [ArrayBuffer]");const t=new Uint8Array(r);let a,h,d;try{const e=await s(t);a=e.sdr,h=e.gainMap,d=e.metadata}catch(r){if(!(r instanceof n||r instanceof o))throw r;console.warn(`Failure to reconstruct an HDR image from ${e}: Gain map metadata not found in the file, HDRJPGNodeLoader will render the SDR jpeg`),d={gainMapMin:[0,0,0],gainMapMax:[1,1,1],gamma:[1,1,1],hdrCapacityMin:0,hdrCapacityMax:1,offsetHdr:[0,0,0],offsetSdr:[0,0,0]},a=t}await this.fillNode(i,d,a.buffer,h?.buffer)}catch(t){return this.manager.itemError(e),void("function"==typeof a&&a(t))}"function"==typeof t&&t(i),this.manager.itemEnd(e)},r,t=>{this.manager.itemError(e),"function"==typeof a&&a(t)}),i}}export{Y as GainMapDecoderMaterial,K as GainMapLoader,V as GainMapNode,te as GainMapNodeLoader,o as GainMapNotFoundError,re as HDRJPGLoader,ae as HDRJPGNodeLoader,re as JPEGRLoader,r as LoaderBaseShared,W as QuadRenderer,n as XMPMetadataNotFoundError,t as createDecodeFunction,i as createTextures,$ as decode,s as extractGainmapFromJPEG,Z as gainMapNode,a as processImages};
|
|
5
|
+
import{c as e,L as t,p as r,a,e as i,X as n,G as s}from"./Loader-EFF6JZel.js";export{M as MPFExtractor,b as extractISO,d as extractXMP}from"./Loader-EFF6JZel.js";import{ClampToEdgeWrapping as o,LinearFilter as h,Scene as p,OrthographicCamera as m,Mesh as f,PlaneGeometry as l,RenderTarget as c,RGBAFormat as u,UVMapping as _,WebGPURenderer as w,DataTexture as y,LinearSRGBColorSpace as x,ShaderMaterial as v,Texture as T,MeshBasicNodeMaterial as U,NoBlending as R,Vector3 as E,FileLoader as S,Loader as C}from"three/webgpu";import"three";import{vec3 as H,texture as O,uniform as D}from"three/tsl";import{G as P}from"./GainMapNode-DtVNqKtN.js";export{g as gainMapNode}from"./GainMapNode-DtVNqKtN.js";import"./iso-gainmap-metadata-BJxZGPtk.js";class L{_renderer;_rendererIsDisposable=!1;_material;_scene;_camera;_quad;_renderTarget;_width;_height;_type;_colorSpace;_supportsReadPixels=!0;constructor(e){this._width=e.width,this._height=e.height,this._type=e.type,this._colorSpace=e.colorSpace;const t={format:u,depthBuffer:!1,stencilBuffer:!1,type:this._type,colorSpace:this._colorSpace,anisotropy:void 0!==e.renderTargetOptions?.anisotropy?e.renderTargetOptions?.anisotropy:1,generateMipmaps:void 0!==e.renderTargetOptions?.generateMipmaps&&e.renderTargetOptions?.generateMipmaps,magFilter:void 0!==e.renderTargetOptions?.magFilter?e.renderTargetOptions?.magFilter:h,minFilter:void 0!==e.renderTargetOptions?.minFilter?e.renderTargetOptions?.minFilter:h,samples:void 0!==e.renderTargetOptions?.samples?e.renderTargetOptions?.samples:void 0,wrapS:void 0!==e.renderTargetOptions?.wrapS?e.renderTargetOptions?.wrapS:o,wrapT:void 0!==e.renderTargetOptions?.wrapT?e.renderTargetOptions?.wrapT:o};this._material=e.material,e.renderer?this._renderer=e.renderer:(this._renderer=L.instantiateRenderer(),this._rendererIsDisposable=!0),this._scene=new p,this._camera=new m,this._camera.position.set(0,0,10),this._camera.left=-.5,this._camera.right=.5,this._camera.top=.5,this._camera.bottom=-.5,this._camera.updateProjectionMatrix(),this._quad=new f(new l,this._material),this._quad.geometry.computeBoundingBox(),this._scene.add(this._quad),this._renderTarget=new c(this.width,this.height,t),this._renderTarget.texture.mapping=void 0!==e.renderTargetOptions?.mapping?e.renderTargetOptions?.mapping:_}static instantiateRenderer(){const e=new w;return e.setSize(128,128),e}render=async()=>{this._renderer.hasInitialized()||await this._renderer.init(),this._renderer.setRenderTarget(this._renderTarget);try{this._renderer.render(this._scene,this._camera)}catch(e){throw this._renderer.setRenderTarget(null),e}this._renderer.setRenderTarget(null)};async toArray(){if(!this._supportsReadPixels)throw new Error("Can't read pixels in this browser");const e=await this._renderer.readRenderTargetPixelsAsync(this._renderTarget,0,0,this._width,this._height);if(!this._renderer.backend.isWebGPUBackend)return e;const t=4*this._width,r=256*Math.ceil(t*e.BYTES_PER_ELEMENT/256)/e.BYTES_PER_ELEMENT,a=new e.constructor(t*this._height);for(let i=0;i<this._height;i++){const n=(this._height-1-i)*r;a.set(e.subarray(n,n+t),i*t)}return a}async toDataTexture(e){const t=new y(await this.toArray(),this.width,this.height,u,this._type,e?.mapping||_,e?.wrapS||o,e?.wrapT||o,e?.magFilter||h,e?.minFilter||h,e?.anisotropy||1,x);return t.flipY=void 0===e?.flipY||e?.flipY,t.generateMipmaps=void 0!==e?.generateMipmaps&&e?.generateMipmaps,t}disposeOnDemandRenderer(){this._renderer.setRenderTarget(null),this._rendererIsDisposable&&this._renderer.dispose().catch(console.error)}dispose(e){e&&this.renderTarget.dispose(),this.material instanceof v&&Object.values(this.material.uniforms).forEach(e=>{e.value instanceof T&&e.value.dispose()}),Object.values(this.material).forEach(e=>{e instanceof T&&e.dispose()}),this.material.dispose(),this._quad.geometry.dispose(),this.disposeOnDemandRenderer()}get width(){return this._width}set width(e){this._width=e,this._renderTarget.setSize(this._width,this._height)}get height(){return this._height}set height(e){this._height=e,this._renderTarget.setSize(this._width,this._height)}get renderer(){return this._renderer}get renderTarget(){return this._renderTarget}set renderTarget(e){this._renderTarget=e,this._width=e.width,this._height=e.height}get material(){return this._material}get type(){return this._type}get colorSpace(){return this._colorSpace}}const F=new Set,G=(e,t,r)=>{const a=`${t}:${e}`;F.has(a)||(F.add(a),console.warn(`gainmap-js: ${e} from '${t}' is deprecated and will be removed in v4.0.0. Use ${r} from '@monogrid/gainmap-js/webgpu' with WebGPURenderer (which also has a WebGL2 backend) instead. It is not a drop-in replacement (it returns a GainMapNode, not a render target), see https://github.com/MONOGRID/gainmap-js#migrating-to-v400`))},I=H(-65504,-65504,-65504),B=H(65504,65504,65504);class N extends U{_maxDisplayBoost;_hdrCapacityMin;_hdrCapacityMax;_gammaUniform;_offsetHdrUniform;_offsetSdrUniform;_gainMapMinUniform;_gainMapMaxUniform;_weightFactorUniform;_sdrTexture;_gainMapTexture;constructor({gamma:e,offsetHdr:t,offsetSdr:r,gainMapMin:a,gainMapMax:i,maxDisplayBoost:n,hdrCapacityMin:s,hdrCapacityMax:o,sdr:h,gainMap:d}){super(),this.name="GainMapDecoderMaterial",this.blending=R,this.depthTest=!1,this.depthWrite=!1,this._sdrTexture=O(h),this._gainMapTexture=O(d),this._gammaUniform=D(new E(1/e[0],1/e[1],1/e[2])),this._offsetHdrUniform=D(new E(t[0],t[1],t[2])),this._offsetSdrUniform=D(new E(r[0],r[1],r[2])),this._gainMapMinUniform=D(new E(a[0],a[1],a[2])),this._gainMapMaxUniform=D(new E(i[0],i[1],i[2]));const p=(Math.log2(n)-s)/(o-s);this._weightFactorUniform=D(p,"float"),this._maxDisplayBoost=n,this._hdrCapacityMin=s,this._hdrCapacityMax=o;const m=this._sdrTexture.rgb,g=this._gainMapTexture.rgb.pow(this._gammaUniform),f=this._gainMapMinUniform.mul(g.oneMinus()).add(this._gainMapMaxUniform.mul(g)),l=H(2).pow(f.mul(this._weightFactorUniform)),c=m.add(this._offsetSdrUniform).mul(l).sub(this._offsetHdrUniform).min(B).max(I);this.colorNode=c}get sdr(){return this._sdrTexture.value}set sdr(e){this._sdrTexture.value=e}get gainMap(){return this._gainMapTexture.value}set gainMap(e){this._gainMapTexture.value=e}get offsetHdr(){return[this._offsetHdrUniform.value.x,this._offsetHdrUniform.value.y,this._offsetHdrUniform.value.z]}set offsetHdr(e){this._offsetHdrUniform.value.x=e[0],this._offsetHdrUniform.value.y=e[1],this._offsetHdrUniform.value.z=e[2]}get offsetSdr(){return[this._offsetSdrUniform.value.x,this._offsetSdrUniform.value.y,this._offsetSdrUniform.value.z]}set offsetSdr(e){this._offsetSdrUniform.value.x=e[0],this._offsetSdrUniform.value.y=e[1],this._offsetSdrUniform.value.z=e[2]}get gainMapMin(){return[this._gainMapMinUniform.value.x,this._gainMapMinUniform.value.y,this._gainMapMinUniform.value.z]}set gainMapMin(e){this._gainMapMinUniform.value.x=e[0],this._gainMapMinUniform.value.y=e[1],this._gainMapMinUniform.value.z=e[2]}get gainMapMax(){return[this._gainMapMaxUniform.value.x,this._gainMapMaxUniform.value.y,this._gainMapMaxUniform.value.z]}set gainMapMax(e){this._gainMapMaxUniform.value.x=e[0],this._gainMapMaxUniform.value.y=e[1],this._gainMapMaxUniform.value.z=e[2]}get gamma(){return[1/this._gammaUniform.value.x,1/this._gammaUniform.value.y,1/this._gammaUniform.value.z]}set gamma(e){this._gammaUniform.value.x=1/e[0],this._gammaUniform.value.y=1/e[1],this._gammaUniform.value.z=1/e[2]}get hdrCapacityMin(){return this._hdrCapacityMin}set hdrCapacityMin(e){this._hdrCapacityMin=e,this.calculateWeight()}get hdrCapacityMax(){return this._hdrCapacityMax}set hdrCapacityMax(e){this._hdrCapacityMax=e,this.calculateWeight()}get maxDisplayBoost(){return this._maxDisplayBoost}set maxDisplayBoost(e){this._maxDisplayBoost=Math.max(1,Math.min(65504,e)),this.calculateWeight()}calculateWeight(){const e=(Math.log2(this._maxDisplayBoost)-this._hdrCapacityMin)/(this._hdrCapacityMax-this._hdrCapacityMin);this._weightFactorUniform.value=Math.max(0,Math.min(1,e))}}const j=e({renderer:w,createMaterial:e=>new N(e),createQuadRenderer:e=>new L(e)}),q=async e=>{if(G("decode()","@monogrid/gainmap-js/webgpu","gainMapNode()"),!e.renderer)throw new Error("Renderer is required for decode function");const t=j({...e,renderer:e.renderer});try{await t.render()}catch(e){throw t.disposeOnDemandRenderer(),e}return t};class z extends t{constructor(e,t){super({renderer:e,createMaterial:e=>new N(e),createQuadRenderer:e=>new L(e)},t)}async render(e,t,r,a){const{sdrImage:i,gainMapImage:n,needsFlip:s}=await this.processImages(r,a,"from-image"),{gainMap:o,sdr:h}=this.createTextures(i,n,s);this.updateQuadRenderer(e,i,o,h,t),await e.render()}}class W extends z{constructor(e,t){super(e,t),G("GainMapLoader","@monogrid/gainmap-js/webgpu","GainMapNodeLoader")}load([e,t,r],a,i,n){const s=this.prepareQuadRenderer();let o,h,d;const p=async()=>{if(o&&h&&d){try{await this.render(s,d,o,h)}catch(a){return this.manager.itemError(e),this.manager.itemError(t),this.manager.itemError(r),"function"==typeof n&&n(a),void s.disposeOnDemandRenderer()}"function"==typeof a&&a(s),this.manager.itemEnd(e),this.manager.itemEnd(t),this.manager.itemEnd(r),s.disposeOnDemandRenderer()}};let m=!0,g=0,f=0,l=!0,c=0,u=0,_=!0,M=0,w=0;const y=()=>{if("function"==typeof i){i(new ProgressEvent("progress",{lengthComputable:m&&l&&_,loaded:f+u+w,total:g+c+M}))}};this.manager.itemStart(e),this.manager.itemStart(t),this.manager.itemStart(r);const x=new S(this._internalLoadingManager);x.setResponseType("arraybuffer"),x.setRequestHeader(this.requestHeader),x.setPath(this.path),x.setWithCredentials(this.withCredentials),x.load(e,async e=>{if("string"==typeof e)throw new Error("Invalid sdr buffer");o=e,await p()},e=>{m=e.lengthComputable,f=e.loaded,g=e.total,y()},t=>{this.manager.itemError(e),"function"==typeof n&&n(t)});const v=new S(this._internalLoadingManager);v.setResponseType("arraybuffer"),v.setRequestHeader(this.requestHeader),v.setPath(this.path),v.setWithCredentials(this.withCredentials),v.load(t,async e=>{if("string"==typeof e)throw new Error("Invalid gainmap buffer");h=e,await p()},e=>{l=e.lengthComputable,u=e.loaded,c=e.total,y()},e=>{this.manager.itemError(t),"function"==typeof n&&n(e)});const T=new S(this._internalLoadingManager);return T.setRequestHeader(this.requestHeader),T.setPath(this.path),T.setWithCredentials(this.withCredentials),T.load(r,async e=>{if("string"!=typeof e)throw new Error("Invalid metadata string");d=JSON.parse(e),await p()},e=>{_=e.lengthComputable,w=e.loaded,M=e.total,y()},e=>{this.manager.itemError(r),"function"==typeof n&&n(e)}),s}}class A extends C{createFileLoader(e){const t=new S(this.manager);return e&&t.setResponseType(e),t.setRequestHeader(this.requestHeader),t.setPath(this.path),t.setWithCredentials(this.withCredentials),t}async fillNode(e,t,i,n){const{sdrImage:s,gainMapImage:o}=await r(i,n,"from-image"),{sdr:h,gainMap:d}=a(s,o,!0);e.setTextures(h,d).setMetadata(t)}}class J extends A{load(e,t,r,a){const i=new P,n=e.map(()=>({lengthComputable:!0,loaded:0,total:0})),s=(e,t,a)=>new Promise((i,s)=>{this.manager.itemStart(e),this.createFileLoader(a).load(e,i,e=>{n[t]={lengthComputable:e.lengthComputable,loaded:e.loaded,total:e.total},"function"==typeof r&&r(new ProgressEvent("progress",{lengthComputable:n.every(e=>e.lengthComputable),loaded:n.reduce((e,t)=>e+t.loaded,0),total:n.reduce((e,t)=>e+t.total,0)}))},s)}),[o,h,d]=e;return Promise.all([s(o,0,"arraybuffer"),s(h,1,"arraybuffer"),s(d,2)]).then(async([e,t,r])=>{if("string"==typeof e||"string"==typeof t||"string"!=typeof r)throw new Error("Invalid response types");const a=JSON.parse(r);await this.fillNode(i,a,e,t)}).then(()=>{"function"==typeof t&&t(i),e.forEach(e=>this.manager.itemEnd(e))},t=>{e.forEach(e=>this.manager.itemError(e)),"function"==typeof a&&a(t)}),i}}class $ extends z{constructor(e,t){super(e,t),G("HDRJPGLoader","@monogrid/gainmap-js/webgpu","HDRJPGNodeLoader")}load(e,t,r,a){const o=this.prepareQuadRenderer(),h=new S(this._internalLoadingManager);return h.setResponseType("arraybuffer"),h.setRequestHeader(this.requestHeader),h.setPath(this.path),h.setWithCredentials(this.withCredentials),this.manager.itemStart(e),h.load(e,async r=>{if("string"==typeof r)throw new Error("Invalid buffer, received [string], was expecting [ArrayBuffer]");const h=new Uint8Array(r);let d,p,m;try{const e=await i(h);d=e.sdr,p=e.gainMap,m=e.metadata}catch(t){if(!(t instanceof n||t instanceof s))throw t;console.warn(`Failure to reconstruct an HDR image from ${e}: Gain map metadata not found in the file, HDRJPGLoader will render the SDR jpeg`),m={gainMapMin:[0,0,0],gainMapMax:[1,1,1],gamma:[1,1,1],hdrCapacityMin:0,hdrCapacityMax:1,offsetHdr:[0,0,0],offsetSdr:[0,0,0]},d=h}try{await this.render(o,m,d.buffer,p?.buffer)}catch(t){return this.manager.itemError(e),"function"==typeof a&&a(t),void o.disposeOnDemandRenderer()}"function"==typeof t&&t(o),this.manager.itemEnd(e),o.disposeOnDemandRenderer()},r,t=>{this.manager.itemError(e),"function"==typeof a&&a(t)}),o}}class Q extends A{load(e,t,r,a){const o=new P;return this.manager.itemStart(e),this.createFileLoader("arraybuffer").load(e,async r=>{try{if("string"==typeof r)throw new Error("Invalid buffer, received [string], was expecting [ArrayBuffer]");const t=new Uint8Array(r);let a,h,d;try{const e=await i(t);a=e.sdr,h=e.gainMap,d=e.metadata}catch(r){if(!(r instanceof n||r instanceof s))throw r;console.warn(`Failure to reconstruct an HDR image from ${e}: Gain map metadata not found in the file, HDRJPGNodeLoader will render the SDR jpeg`),d={gainMapMin:[0,0,0],gainMapMax:[1,1,1],gamma:[1,1,1],hdrCapacityMin:0,hdrCapacityMax:1,offsetHdr:[0,0,0],offsetSdr:[0,0,0]},a=t}await this.fillNode(o,d,a.buffer,h?.buffer)}catch(t){return this.manager.itemError(e),void("function"==typeof a&&a(t))}"function"==typeof t&&t(o),this.manager.itemEnd(e)},r,t=>{this.manager.itemError(e),"function"==typeof a&&a(t)}),o}}export{N as GainMapDecoderMaterial,W as GainMapLoader,P as GainMapNode,J as GainMapNodeLoader,s as GainMapNotFoundError,$ as HDRJPGLoader,Q as HDRJPGNodeLoader,$ as JPEGRLoader,t as LoaderBaseShared,L as QuadRenderer,n as XMPMetadataNotFoundError,e as createDecodeFunction,a as createTextures,q as decode,i as extractGainmapFromJPEG,r as processImages};
|