@monogrid/gainmap-js 3.2.0 → 3.4.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 +14 -0
- package/README.md +8 -55
- package/dist/Loader-CKVwvejH.js +5 -0
- package/dist/{Loader-BZcKUVaX.js → Loader-DLI-_JDP.js} +1 -1
- package/dist/{QuadRenderer-Y5tm-R3m.js → QuadRenderer-Bj1xl_EK.js} +1 -1
- package/dist/QuadRenderer-BoQMkXLf.js +5 -0
- package/dist/_commonjsHelpers-CDmClQiF.js +5 -0
- package/dist/{_commonjsHelpers-CuGYGi_5.js → _commonjsHelpers-v3qvwW2J.js} +1 -1
- package/dist/{compress-BaKKOAXI.js → compress-C-X439Jm.js} +3 -1
- package/dist/compress-DY-CSunh.js +5 -0
- package/dist/core/QuadRenderer.d.ts +4 -4
- package/dist/core/types.d.ts +14 -0
- package/dist/decode/shared/types.d.ts +3 -13
- package/dist/decode.js +4 -4
- package/dist/decode.min.js +5 -0
- package/dist/decode.umd.cjs +1 -1
- package/dist/decode.umd.min.cjs +5 -0
- package/dist/decode.webgpu.js +3 -3
- package/dist/decode.webgpu.min.js +5 -0
- package/dist/decode.webgpu.umd.cjs +1 -1
- package/dist/decode.webgpu.umd.min.cjs +5 -0
- package/dist/encode/encode-and-compress.d.ts +1 -1
- package/dist/encode/types.d.ts +2 -2
- package/dist/encode.js +130 -5
- package/dist/encode.min.js +5 -0
- package/dist/encode.umd.cjs +129 -2
- package/dist/encode.umd.min.cjs +5 -0
- package/dist/libultrahdr/encode-jpeg-metadata.d.ts +5 -5
- package/dist/libultrahdr/jpeg-assembler.d.ts +41 -0
- package/dist/libultrahdr/jpeg-markers.d.ts +47 -0
- package/dist/libultrahdr/mpf-generator.d.ts +20 -0
- package/dist/libultrahdr/xmp-generator.d.ts +33 -0
- package/dist/libultrahdr.d.ts +0 -3
- package/dist/libultrahdr.js +546 -6584
- package/dist/libultrahdr.min.js +5 -0
- package/dist/libultrahdr.umd.cjs +632 -6673
- package/dist/libultrahdr.umd.min.cjs +5 -0
- package/dist/third-party.txt +301 -0
- package/dist/worker-interface.js +2 -2
- package/dist/worker-interface.min.js +5 -0
- package/dist/worker-interface.umd.cjs +1 -1
- package/dist/worker-interface.umd.min.cjs +5 -0
- package/dist/worker.js +3 -3
- package/dist/worker.min.js +5 -0
- package/dist/worker.umd.cjs +3 -1
- package/dist/worker.umd.min.cjs +5 -0
- package/package.json +2 -2
- package/dist/libultrahdr/decode-jpeg-metadata.d.ts +0 -28
- package/dist/libultrahdr/library.d.ts +0 -9
- package/dist/libultrahdr-esm.wasm +0 -0
- package/libultrahdr-wasm/build/libultrahdr-debug.js +0 -7156
- package/libultrahdr-wasm/build/libultrahdr-debug.wasm +0 -0
- package/libultrahdr-wasm/build/libultrahdr-esm.js +0 -6633
- package/libultrahdr-wasm/build/libultrahdr-esm.wasm +0 -0
- package/libultrahdr-wasm/build/libultrahdr.d.ts +0 -30
- package/libultrahdr-wasm/build/libultrahdr.js +0 -6591
- package/libultrahdr-wasm/build/libultrahdr.wasm +0 -0
package/dist/encode.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @monogrid/gainmap-js v3.
|
|
2
|
+
* @monogrid/gainmap-js v3.4.0
|
|
3
3
|
* With ❤️, by MONOGRID <gainmap@monogrid.com>
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { c as compress } from './compress-
|
|
7
|
-
import { DataTexture, RGBAFormat, UVMapping, RepeatWrapping, LinearFilter, LinearSRGBColorSpace, ShaderMaterial, NoBlending, Vector3, UnsignedByteType, ACESFilmicToneMapping, LinearToneMapping, CineonToneMapping, ReinhardToneMapping, SRGBColorSpace, AlphaFormat, RGBFormat, DepthFormat, DepthStencilFormat, RedFormat, RedIntegerFormat, RGFormat, RGIntegerFormat, RGBIntegerFormat, RGBAIntegerFormat, Vector2, WebGLRenderTarget, ClampToEdgeWrapping, NearestFilter, DataUtils, FloatType } from 'three';
|
|
8
|
-
import { Q as QuadRenderer } from './QuadRenderer-
|
|
6
|
+
import { c as compress } from './compress-C-X439Jm.js';
|
|
7
|
+
import { DataTexture, RGBAFormat, UVMapping, RepeatWrapping, LinearFilter, LinearSRGBColorSpace, ShaderMaterial, NoBlending, Vector3, UnsignedByteType, ACESFilmicToneMapping, LinearToneMapping, NeutralToneMapping, AgXToneMapping, CineonToneMapping, ReinhardToneMapping, SRGBColorSpace, AlphaFormat, RGBFormat, DepthFormat, DepthStencilFormat, RedFormat, RedIntegerFormat, RGFormat, RGIntegerFormat, RGBIntegerFormat, RGBAIntegerFormat, Vector2, WebGLRenderTarget, ClampToEdgeWrapping, NearestFilter, DataUtils, FloatType } from 'three';
|
|
8
|
+
import { Q as QuadRenderer } from './QuadRenderer-Bj1xl_EK.js';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Utility function to obtain a `DataTexture` from various input formats
|
|
@@ -320,6 +320,123 @@ vec3 LinearToneMapping ( vec3 color ) {
|
|
|
320
320
|
return color;
|
|
321
321
|
}
|
|
322
322
|
|
|
323
|
+
// Matrices for rec 2020 <> rec 709 color space conversion
|
|
324
|
+
// matrix provided in row-major order so it has been transposed
|
|
325
|
+
// https://www.itu.int/pub/R-REP-BT.2407-2017
|
|
326
|
+
const mat3 LINEAR_REC2020_TO_LINEAR_SRGB = mat3(
|
|
327
|
+
vec3( 1.6605, - 0.1246, - 0.0182 ),
|
|
328
|
+
vec3( - 0.5876, 1.1329, - 0.1006 ),
|
|
329
|
+
vec3( - 0.0728, - 0.0083, 1.1187 )
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
const mat3 LINEAR_SRGB_TO_LINEAR_REC2020 = mat3(
|
|
333
|
+
vec3( 0.6274, 0.0691, 0.0164 ),
|
|
334
|
+
vec3( 0.3293, 0.9195, 0.0880 ),
|
|
335
|
+
vec3( 0.0433, 0.0113, 0.8956 )
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
// https://iolite-engine.com/blog_posts/minimal_agx_implementation
|
|
339
|
+
// Mean error^2: 3.6705141e-06
|
|
340
|
+
vec3 agxDefaultContrastApprox( vec3 x ) {
|
|
341
|
+
vec3 x2 = x * x;
|
|
342
|
+
vec3 x4 = x2 * x2;
|
|
343
|
+
return + 15.5 * x4 * x2
|
|
344
|
+
- 40.14 * x4 * x
|
|
345
|
+
+ 31.96 * x4
|
|
346
|
+
- 6.868 * x2 * x
|
|
347
|
+
+ 0.4298 * x2
|
|
348
|
+
+ 0.1191 * x
|
|
349
|
+
- 0.00232;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// AgX Tone Mapping implementation based on Filament, which in turn is based
|
|
353
|
+
// on Blender's implementation using rec 2020 primaries
|
|
354
|
+
// https://github.com/google/filament/pull/7236
|
|
355
|
+
// Inputs and outputs are encoded as Linear-sRGB.
|
|
356
|
+
|
|
357
|
+
vec3 AgXToneMapping( vec3 color ) {
|
|
358
|
+
|
|
359
|
+
// AgX constants
|
|
360
|
+
const mat3 AgXInsetMatrix = mat3(
|
|
361
|
+
vec3( 0.856627153315983, 0.137318972929847, 0.11189821299995 ),
|
|
362
|
+
vec3( 0.0951212405381588, 0.761241990602591, 0.0767994186031903 ),
|
|
363
|
+
vec3( 0.0482516061458583, 0.101439036467562, 0.811302368396859 )
|
|
364
|
+
);
|
|
365
|
+
|
|
366
|
+
// explicit AgXOutsetMatrix generated from Filaments AgXOutsetMatrixInv
|
|
367
|
+
const mat3 AgXOutsetMatrix = mat3(
|
|
368
|
+
vec3( 1.1271005818144368, - 0.1413297634984383, - 0.14132976349843826 ),
|
|
369
|
+
vec3( - 0.11060664309660323, 1.157823702216272, - 0.11060664309660294 ),
|
|
370
|
+
vec3( - 0.016493938717834573, - 0.016493938717834257, 1.2519364065950405 )
|
|
371
|
+
);
|
|
372
|
+
|
|
373
|
+
// LOG2_MIN = -10.0
|
|
374
|
+
// LOG2_MAX = +6.5
|
|
375
|
+
// MIDDLE_GRAY = 0.18
|
|
376
|
+
const float AgxMinEv = - 12.47393; // log2( pow( 2, LOG2_MIN ) * MIDDLE_GRAY )
|
|
377
|
+
const float AgxMaxEv = 4.026069; // log2( pow( 2, LOG2_MAX ) * MIDDLE_GRAY )
|
|
378
|
+
|
|
379
|
+
color = LINEAR_SRGB_TO_LINEAR_REC2020 * color;
|
|
380
|
+
|
|
381
|
+
color = AgXInsetMatrix * color;
|
|
382
|
+
|
|
383
|
+
// Log2 encoding
|
|
384
|
+
color = max( color, 1e-10 ); // avoid 0 or negative numbers for log2
|
|
385
|
+
color = log2( color );
|
|
386
|
+
color = ( color - AgxMinEv ) / ( AgxMaxEv - AgxMinEv );
|
|
387
|
+
|
|
388
|
+
color = clamp( color, 0.0, 1.0 );
|
|
389
|
+
|
|
390
|
+
// Apply sigmoid
|
|
391
|
+
color = agxDefaultContrastApprox( color );
|
|
392
|
+
|
|
393
|
+
// Apply AgX look
|
|
394
|
+
// v = agxLook(v, look);
|
|
395
|
+
|
|
396
|
+
color = AgXOutsetMatrix * color;
|
|
397
|
+
|
|
398
|
+
// Linearize
|
|
399
|
+
color = pow( max( vec3( 0.0 ), color ), vec3( 2.2 ) );
|
|
400
|
+
|
|
401
|
+
color = LINEAR_REC2020_TO_LINEAR_SRGB * color;
|
|
402
|
+
|
|
403
|
+
// Gamut mapping. Simple clamp for now.
|
|
404
|
+
color = clamp( color, 0.0, 1.0 );
|
|
405
|
+
|
|
406
|
+
return color;
|
|
407
|
+
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// https://modelviewer.dev/examples/tone-mapping
|
|
411
|
+
|
|
412
|
+
vec3 NeutralToneMapping( vec3 color ) {
|
|
413
|
+
|
|
414
|
+
const float StartCompression = 0.8 - 0.04;
|
|
415
|
+
const float Desaturation = 0.15;
|
|
416
|
+
|
|
417
|
+
float x = min( color.r, min( color.g, color.b ) );
|
|
418
|
+
|
|
419
|
+
float offset = x < 0.08 ? x - 6.25 * x * x : 0.04;
|
|
420
|
+
|
|
421
|
+
color -= offset;
|
|
422
|
+
|
|
423
|
+
float peak = max( color.r, max( color.g, color.b ) );
|
|
424
|
+
|
|
425
|
+
if ( peak < StartCompression ) return color;
|
|
426
|
+
|
|
427
|
+
float d = 1. - StartCompression;
|
|
428
|
+
|
|
429
|
+
float newPeak = 1. - d * d / ( peak + d - StartCompression );
|
|
430
|
+
|
|
431
|
+
color *= newPeak / peak;
|
|
432
|
+
|
|
433
|
+
float g = 1. - 1. / ( Desaturation * ( peak - newPeak ) + 1. );
|
|
434
|
+
|
|
435
|
+
return mix( color, vec3( newPeak ), g );
|
|
436
|
+
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
|
|
323
440
|
|
|
324
441
|
void main() {
|
|
325
442
|
vec4 color = texture2D(map, vUv);
|
|
@@ -395,6 +512,14 @@ class SDRMaterial extends ShaderMaterial {
|
|
|
395
512
|
this.defines.TONEMAPPING_FUNCTION = 'LinearToneMapping';
|
|
396
513
|
valid = true;
|
|
397
514
|
break;
|
|
515
|
+
case AgXToneMapping:
|
|
516
|
+
this.defines.TONEMAPPING_FUNCTION = 'AgXToneMapping';
|
|
517
|
+
valid = true;
|
|
518
|
+
break;
|
|
519
|
+
case NeutralToneMapping:
|
|
520
|
+
this.defines.TONEMAPPING_FUNCTION = 'NeutralToneMapping';
|
|
521
|
+
valid = true;
|
|
522
|
+
break;
|
|
398
523
|
default:
|
|
399
524
|
console.error(`Unsupported toneMapping: ${value}. Using LinearToneMapping.`);
|
|
400
525
|
this.defines.TONEMAPPING_FUNCTION = 'LinearToneMapping';
|
|
@@ -579,7 +704,7 @@ const encode = (params) => {
|
|
|
579
704
|
*
|
|
580
705
|
* // embed the compressed images + metadata into a single
|
|
581
706
|
* // JPEG file
|
|
582
|
-
* const jpeg =
|
|
707
|
+
* const jpeg = encodeJPEGMetadata({
|
|
583
708
|
* ...encodingResult,
|
|
584
709
|
* sdr: encodingResult.sdr,
|
|
585
710
|
* gainMap: encodingResult.gainMap
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @monogrid/gainmap-js v3.4.0
|
|
3
|
+
* With ❤️, by MONOGRID <gainmap@monogrid.com>
|
|
4
|
+
*/
|
|
5
|
+
import{c as n}from"./compress-DY-CSunh.js";import{DataTexture as e,RGBAFormat as t,UVMapping as o,RepeatWrapping as r,LinearFilter as a,LinearSRGBColorSpace as i,ShaderMaterial as s,NoBlending as c,Vector3 as m,UnsignedByteType as l,ACESFilmicToneMapping as p,LinearToneMapping as d,NeutralToneMapping as u,AgXToneMapping as g,CineonToneMapping as f,ReinhardToneMapping as h,SRGBColorSpace as v,AlphaFormat as M,RGBFormat as x,DepthFormat as _,DepthStencilFormat as C,RedFormat as A,RedIntegerFormat as w,RGFormat as T,RGIntegerFormat as E,RGBIntegerFormat as y,RGBAIntegerFormat as I,Vector2 as S,WebGLRenderTarget as N,ClampToEdgeWrapping as R,NearestFilter as L,DataUtils as O,FloatType as b}from"three";import{Q as U}from"./QuadRenderer-BoQMkXLf.js";const P=n=>{let s;if(n instanceof e){if(!(n.image.data instanceof Uint16Array||n.image.data instanceof Float32Array))throw new Error("Provided image is not HDR");s=n}else s=new e(n.data,n.width,n.height,"format"in n?n.format:t,n.type,o,r,r,a,a,1,"colorSpace"in n&&"srgb"===n.colorSpace?n.colorSpace:i),"header"in n&&"gamma"in n&&(s.flipY=!0),s.needsUpdate=!0;return s};class D extends s{_minContentBoost;_maxContentBoost;_offsetSdr;_offsetHdr;_gamma;constructor({sdr:n,hdr:e,offsetSdr:t,offsetHdr:o,maxContentBoost:r,minContentBoost:a,gamma:i}){if(!r)throw new Error("maxContentBoost is required");if(!n)throw new Error("sdr is required");if(!e)throw new Error("hdr is required");const s=i||[1,1,1],l=t||[1/64,1/64,1/64],p=o||[1/64,1/64,1/64],d=a||1,u=Math.max(r,1.0001);super({name:"GainMapEncoderMaterial",vertexShader:"\nvarying vec2 vUv;\n\nvoid main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}\n",fragmentShader:"\n#ifndef saturate\n#define saturate( a ) clamp( a, 0.0, 1.0 )\n#endif\nuniform sampler2D sdr;\nuniform sampler2D hdr;\nuniform vec3 gamma;\nuniform vec3 offsetSdr;\nuniform vec3 offsetHdr;\nuniform float minLog2;\nuniform float maxLog2;\n\nvarying vec2 vUv;\n\nvoid main() {\n vec3 sdrColor = texture2D(sdr, vUv).rgb;\n vec3 hdrColor = texture2D(hdr, vUv).rgb;\n\n vec3 pixelGain = (hdrColor + offsetHdr) / (sdrColor + offsetSdr);\n vec3 logRecovery = (log2(pixelGain) - minLog2) / (maxLog2 - minLog2);\n vec3 clampedRecovery = saturate(logRecovery);\n gl_FragColor = vec4(pow(clampedRecovery, gamma), 1.0);\n}\n",uniforms:{sdr:{value:n},hdr:{value:e},gamma:{value:(new m).fromArray(s)},offsetSdr:{value:(new m).fromArray(l)},offsetHdr:{value:(new m).fromArray(p)},minLog2:{value:Math.log2(d)},maxLog2:{value:Math.log2(u)}},blending:c,depthTest:!1,depthWrite:!1}),this._minContentBoost=d,this._maxContentBoost=u,this._offsetSdr=l,this._offsetHdr=p,this._gamma=s,this.needsUpdate=!0,this.uniformsNeedUpdate=!0}get gamma(){return this._gamma}set gamma(n){this._gamma=n,this.uniforms.gamma.value=(new m).fromArray(n)}get offsetHdr(){return this._offsetHdr}set offsetHdr(n){this._offsetHdr=n,this.uniforms.offsetHdr.value=(new m).fromArray(n)}get offsetSdr(){return this._offsetSdr}set offsetSdr(n){this._offsetSdr=n,this.uniforms.offsetSdr.value=(new m).fromArray(n)}get minContentBoost(){return this._minContentBoost}set minContentBoost(n){this._minContentBoost=n,this.uniforms.minLog2.value=Math.log2(n)}get maxContentBoost(){return this._maxContentBoost}set maxContentBoost(n){this._maxContentBoost=n,this.uniforms.maxLog2.value=Math.log2(n)}get gainMapMin(){return[Math.log2(this._minContentBoost),Math.log2(this._minContentBoost),Math.log2(this._minContentBoost)]}get gainMapMax(){return[Math.log2(this._maxContentBoost),Math.log2(this._maxContentBoost),Math.log2(this._maxContentBoost)]}get hdrCapacityMin(){return Math.min(Math.max(0,this.gainMapMin[0]),Math.max(0,this.gainMapMin[1]),Math.max(0,this.gainMapMin[2]))}get hdrCapacityMax(){return Math.max(Math.max(0,this.gainMapMax[0]),Math.max(0,this.gainMapMax[1]),Math.max(0,this.gainMapMax[2]))}}const B=n=>{const{image:e,sdr:t,renderer:o}=n,r=P(e),a=new D({...n,sdr:t.renderTarget.texture,hdr:r}),s=new U({width:r.image.width,height:r.image.height,type:l,colorSpace:i,material:a,renderer:o,renderTargetOptions:n.renderTargetOptions});try{s.render()}catch(n){throw s.disposeOnDemandRenderer(),n}return s};class F extends s{_brightness=0;_contrast=1;_saturation=1;_exposure=1;_toneMapping;_map;constructor({map:n,toneMapping:e}){super({name:"SDRMaterial",vertexShader:"\nvarying vec2 vUv;\n\nvoid main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}\n",fragmentShader:"\n#ifndef saturate\n#define saturate( a ) clamp( a, 0.0, 1.0 )\n#endif\n\nuniform sampler2D map;\nuniform float brightness;\nuniform float contrast;\nuniform float saturation;\nuniform float exposure;\n\nvarying vec2 vUv;\n\nmat4 brightnessMatrix( float brightness ) {\n return mat4(\n 1, 0, 0, 0,\n 0, 1, 0, 0,\n 0, 0, 1, 0,\n brightness, brightness, brightness, 1\n );\n}\n\nmat4 contrastMatrix( float contrast ) {\n float t = ( 1.0 - contrast ) / 2.0;\n return mat4(\n contrast, 0, 0, 0,\n 0, contrast, 0, 0,\n 0, 0, contrast, 0,\n t, t, t, 1\n );\n}\n\nmat4 saturationMatrix( float saturation ) {\n vec3 luminance = vec3( 0.3086, 0.6094, 0.0820 );\n float oneMinusSat = 1.0 - saturation;\n vec3 red = vec3( luminance.x * oneMinusSat );\n red+= vec3( saturation, 0, 0 );\n vec3 green = vec3( luminance.y * oneMinusSat );\n green += vec3( 0, saturation, 0 );\n vec3 blue = vec3( luminance.z * oneMinusSat );\n blue += vec3( 0, 0, saturation );\n return mat4(\n red, 0,\n green, 0,\n blue, 0,\n 0, 0, 0, 1\n );\n}\n\nvec3 RRTAndODTFit( vec3 v ) {\n vec3 a = v * ( v + 0.0245786 ) - 0.000090537;\n vec3 b = v * ( 0.983729 * v + 0.4329510 ) + 0.238081;\n return a / b;\n}\n\nvec3 ACESFilmicToneMapping( vec3 color ) {\n // sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT\n const mat3 ACESInputMat = mat3(\n vec3( 0.59719, 0.07600, 0.02840 ), // transposed from source\n vec3( 0.35458, 0.90834, 0.13383 ),\n vec3( 0.04823, 0.01566, 0.83777 )\n );\n // ODT_SAT => XYZ => D60_2_D65 => sRGB\n const mat3 ACESOutputMat = mat3(\n vec3( 1.60475, -0.10208, -0.00327 ), // transposed from source\n vec3( -0.53108, 1.10813, -0.07276 ),\n vec3( -0.07367, -0.00605, 1.07602 )\n );\n color = ACESInputMat * color;\n // Apply RRT and ODT\n color = RRTAndODTFit( color );\n color = ACESOutputMat * color;\n // Clamp to [0, 1]\n return saturate( color );\n}\n\n// source: https://www.cs.utah.edu/docs/techreports/2002/pdf/UUCS-02-001.pdf\nvec3 ReinhardToneMapping( vec3 color ) {\n return saturate( color / ( vec3( 1.0 ) + color ) );\n}\n\n// source: http://filmicworlds.com/blog/filmic-tonemapping-operators/\nvec3 CineonToneMapping( vec3 color ) {\n // optimized filmic operator by Jim Hejl and Richard Burgess-Dawson\n color = max( vec3( 0.0 ), color - 0.004 );\n return pow( ( color * ( 6.2 * color + 0.5 ) ) / ( color * ( 6.2 * color + 1.7 ) + 0.06 ), vec3( 2.2 ) );\n}\n\n// nothing\nvec3 LinearToneMapping ( vec3 color ) {\n return color;\n}\n\n// Matrices for rec 2020 <> rec 709 color space conversion\n// matrix provided in row-major order so it has been transposed\n// https://www.itu.int/pub/R-REP-BT.2407-2017\nconst mat3 LINEAR_REC2020_TO_LINEAR_SRGB = mat3(\n vec3( 1.6605, - 0.1246, - 0.0182 ),\n vec3( - 0.5876, 1.1329, - 0.1006 ),\n vec3( - 0.0728, - 0.0083, 1.1187 )\n);\n\nconst mat3 LINEAR_SRGB_TO_LINEAR_REC2020 = mat3(\n vec3( 0.6274, 0.0691, 0.0164 ),\n vec3( 0.3293, 0.9195, 0.0880 ),\n vec3( 0.0433, 0.0113, 0.8956 )\n);\n\n// https://iolite-engine.com/blog_posts/minimal_agx_implementation\n// Mean error^2: 3.6705141e-06\nvec3 agxDefaultContrastApprox( vec3 x ) {\n vec3 x2 = x * x;\n vec3 x4 = x2 * x2;\n return + 15.5 * x4 * x2\n - 40.14 * x4 * x\n + 31.96 * x4\n - 6.868 * x2 * x\n + 0.4298 * x2\n + 0.1191 * x\n - 0.00232;\n}\n\n// AgX Tone Mapping implementation based on Filament, which in turn is based\n// on Blender's implementation using rec 2020 primaries\n// https://github.com/google/filament/pull/7236\n// Inputs and outputs are encoded as Linear-sRGB.\n\nvec3 AgXToneMapping( vec3 color ) {\n\n // AgX constants\n const mat3 AgXInsetMatrix = mat3(\n vec3( 0.856627153315983, 0.137318972929847, 0.11189821299995 ),\n vec3( 0.0951212405381588, 0.761241990602591, 0.0767994186031903 ),\n vec3( 0.0482516061458583, 0.101439036467562, 0.811302368396859 )\n );\n\n // explicit AgXOutsetMatrix generated from Filaments AgXOutsetMatrixInv\n const mat3 AgXOutsetMatrix = mat3(\n vec3( 1.1271005818144368, - 0.1413297634984383, - 0.14132976349843826 ),\n vec3( - 0.11060664309660323, 1.157823702216272, - 0.11060664309660294 ),\n vec3( - 0.016493938717834573, - 0.016493938717834257, 1.2519364065950405 )\n );\n\n // LOG2_MIN = -10.0\n // LOG2_MAX = +6.5\n // MIDDLE_GRAY = 0.18\n const float AgxMinEv = - 12.47393; // log2( pow( 2, LOG2_MIN ) * MIDDLE_GRAY )\n const float AgxMaxEv = 4.026069; // log2( pow( 2, LOG2_MAX ) * MIDDLE_GRAY )\n\n color = LINEAR_SRGB_TO_LINEAR_REC2020 * color;\n\n color = AgXInsetMatrix * color;\n\n // Log2 encoding\n color = max( color, 1e-10 ); // avoid 0 or negative numbers for log2\n color = log2( color );\n color = ( color - AgxMinEv ) / ( AgxMaxEv - AgxMinEv );\n\n color = clamp( color, 0.0, 1.0 );\n\n // Apply sigmoid\n color = agxDefaultContrastApprox( color );\n\n // Apply AgX look\n // v = agxLook(v, look);\n\n color = AgXOutsetMatrix * color;\n\n // Linearize\n color = pow( max( vec3( 0.0 ), color ), vec3( 2.2 ) );\n\n color = LINEAR_REC2020_TO_LINEAR_SRGB * color;\n\n // Gamut mapping. Simple clamp for now.\n color = clamp( color, 0.0, 1.0 );\n\n return color;\n\n}\n\n// https://modelviewer.dev/examples/tone-mapping\n\nvec3 NeutralToneMapping( vec3 color ) {\n\n const float StartCompression = 0.8 - 0.04;\n const float Desaturation = 0.15;\n\n float x = min( color.r, min( color.g, color.b ) );\n\n float offset = x < 0.08 ? x - 6.25 * x * x : 0.04;\n\n color -= offset;\n\n float peak = max( color.r, max( color.g, color.b ) );\n\n if ( peak < StartCompression ) return color;\n\n float d = 1. - StartCompression;\n\n float newPeak = 1. - d * d / ( peak + d - StartCompression );\n\n color *= newPeak / peak;\n\n float g = 1. - 1. / ( Desaturation * ( peak - newPeak ) + 1. );\n\n return mix( color, vec3( newPeak ), g );\n\n}\n\n\n\nvoid main() {\n vec4 color = texture2D(map, vUv);\n\n vec4 exposed = vec4(exposure * color.rgb, color.a);\n\n vec4 tonemapped = vec4(TONEMAPPING_FUNCTION(exposed.rgb), color.a);\n\n vec4 adjusted =\n brightnessMatrix( brightness ) *\n contrastMatrix( contrast ) *\n saturationMatrix( saturation ) *\n tonemapped;\n\n gl_FragColor = adjusted;\n}\n",uniforms:{map:{value:n},brightness:{value:0},contrast:{value:1},saturation:{value:1},exposure:{value:1}},blending:c,depthTest:!1,depthWrite:!1}),this._map=n,this.toneMapping=this._toneMapping=e||p,this.needsUpdate=!0,this.uniformsNeedUpdate=!0}get toneMapping(){return this._toneMapping}set toneMapping(n){let e=!1;switch(n){case p:this.defines.TONEMAPPING_FUNCTION="ACESFilmicToneMapping",e=!0;break;case h:this.defines.TONEMAPPING_FUNCTION="ReinhardToneMapping",e=!0;break;case f:this.defines.TONEMAPPING_FUNCTION="CineonToneMapping",e=!0;break;case d:this.defines.TONEMAPPING_FUNCTION="LinearToneMapping",e=!0;break;case g:this.defines.TONEMAPPING_FUNCTION="AgXToneMapping",e=!0;break;case u:this.defines.TONEMAPPING_FUNCTION="NeutralToneMapping",e=!0;break;default:console.error(`Unsupported toneMapping: ${n}. Using LinearToneMapping.`),this.defines.TONEMAPPING_FUNCTION="LinearToneMapping",this._toneMapping=d}e&&(this._toneMapping=n),this.needsUpdate=!0}get brightness(){return this._brightness}set brightness(n){this._brightness=n,this.uniforms.brightness.value=n}get contrast(){return this._contrast}set contrast(n){this._contrast=n,this.uniforms.contrast.value=n}get saturation(){return this._saturation}set saturation(n){this._saturation=n,this.uniforms.saturation.value=n}get exposure(){return this._exposure}set exposure(n){this._exposure=n,this.uniforms.exposure.value=n}get map(){return this._map}set map(n){this._map=n,this.uniforms.map.value=n}}const G=(n,e,t,o)=>{n.needsUpdate=!0;const r=new U({width:n.image.width,height:n.image.height,type:l,colorSpace:v,material:new F({map:n,toneMapping:t}),renderer:e,renderTargetOptions:o});try{r.render()}catch(n){throw r.disposeOnDemandRenderer(),n}return r},k=n=>{const{image:e,renderer:t}=n,o=P(e),r=G(o,t,n.toneMapping,n.renderTargetOptions),a=B({...n,image:o,sdr:r,renderer:r.renderer});return{sdr:r,gainMap:a,hdr:o,getMetadata:()=>({gainMapMax:a.material.gainMapMax,gainMapMin:a.material.gainMapMin,gamma:a.material.gamma,hdrCapacityMax:a.material.hdrCapacityMax,hdrCapacityMin:a.material.hdrCapacityMin,offsetHdr:a.material.offsetHdr,offsetSdr:a.material.offsetSdr})}},H=async e=>{const t=k(e),{mimeType:o,quality:r,flipY:a,withWorker:i}=e;let s,c,m;const l=new ImageData(t.sdr.toArray(),t.sdr.width,t.sdr.height),p=new ImageData(t.gainMap.toArray(),t.gainMap.width,t.gainMap.height);if(i){const n=await Promise.all([i.compress({source:l,mimeType:o,quality:r,flipY:a}),i.compress({source:p,mimeType:o,quality:r,flipY:a})]);s=n,c=n[0].source,m=n[1].source}else s=await Promise.all([n({source:l,mimeType:o,quality:r,flipY:a}),n({source:p,mimeType:o,quality:r,flipY:a})]),c=l.data,m=p.data;return t.sdr.dispose(),t.gainMap.dispose(),{...t,...t.getMetadata(),sdr:s[0],gainMap:s[1],rawSDR:c,rawGainMap:m}},X=(n,e="max",o)=>{const r=P(n);if(r.format!==M&&r.format!==x&&r.format!==t&&r.format!==_&&r.format!==C&&r.format!==A&&r.format!==w&&r.format!==T&&r.format!==E&&r.format!==y&&r.format!==I)throw new Error("Unsupported texture format");const a=new s({vertexShader:"\nvarying vec2 vUv;\nvoid main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}\n",fragmentShader:"\nprecision mediump float;\n\n#ifndef CELL_SIZE\n #define CELL_SIZE 2\n#endif\n\n#ifndef COMPARE_FUNCTION\n #define COMPARE_FUNCTION max\n#endif\n\n#ifndef INITIAL_VALUE\n #define INITIAL_VALUE 0\n#endif\n\nuniform sampler2D map;\nuniform vec2 u_srcResolution;\n\nvarying vec2 vUv;\n\nvoid main() {\n // compute the first pixel the source cell\n vec2 srcPixel = floor(gl_FragCoord.xy) * float(CELL_SIZE);\n\n // one pixel in source\n vec2 onePixel = vec2(1) / u_srcResolution;\n\n // uv for first pixel in cell. +0.5 for center of pixel\n vec2 uv = (srcPixel + 0.5) * onePixel;\n\n vec4 resultColor = vec4(INITIAL_VALUE);\n\n for (int y = 0; y < CELL_SIZE; ++y) {\n for (int x = 0; x < CELL_SIZE; ++x) {\n resultColor = COMPARE_FUNCTION(resultColor, texture2D(map, uv + vec2(x, y) * onePixel));\n }\n }\n\n gl_FragColor = resultColor;\n}\n",uniforms:{u_srcResolution:{value:new S(r.image.width,r.image.height)},map:{value:r}},defines:{CELL_SIZE:2,COMPARE_FUNCTION:e,INITIAL_VALUE:"max"===e?0:65504}});r.needsUpdate=!0,a.needsUpdate=!0;let i=r.image.width,c=r.image.height;const m=new U({width:i,height:c,type:r.type,colorSpace:r.colorSpace,material:a,renderer:o}),l=[];for(;i>1||c>1;){i=Math.max(1,(i+2-1)/2|0),c=Math.max(1,(c+2-1)/2|0);const n=new N(i,c,{type:m.type,format:r.format,colorSpace:m.colorSpace,minFilter:L,magFilter:L,wrapS:R,wrapT:R,generateMipmaps:!1,depthBuffer:!1,stencilBuffer:!1});l.push(n)}i=r.image.width,c=r.image.height,l.forEach(n=>{i=Math.max(1,(i+2-1)/2|0),c=Math.max(1,(c+2-1)/2|0),m.renderTarget=n,m.render(),a.uniforms.map.value=n.texture,a.uniforms.u_srcResolution.value.x=i,a.uniforms.u_srcResolution.value.y=c});const p=m.toArray();return m.dispose(),l.forEach(n=>n.dispose()),[m.type===b?p[0]:O.fromHalfFloat(p[0]),m.type===b?p[1]:O.fromHalfFloat(p[1]),m.type===b?p[2]:O.fromHalfFloat(p[2])]};export{D as GainMapEncoderMaterial,F as SDRMaterial,n as compress,k as encode,H as encodeAndCompress,X as findTextureMinMax,B as getGainMap,G as getSDRRendition};
|
package/dist/encode.umd.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @monogrid/gainmap-js v3.
|
|
2
|
+
* @monogrid/gainmap-js v3.4.0
|
|
3
3
|
* With ❤️, by MONOGRID <gainmap@monogrid.com>
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -74,6 +74,8 @@
|
|
|
74
74
|
canvas.width = width;
|
|
75
75
|
canvas.height = height;
|
|
76
76
|
}
|
|
77
|
+
// apparently we do need to assert the type here for npx tsc to be happy
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
77
79
|
const ctx = canvas.getContext('2d');
|
|
78
80
|
if (!ctx)
|
|
79
81
|
throw new Error('Failed to create canvas Context');
|
|
@@ -708,6 +710,123 @@ vec3 LinearToneMapping ( vec3 color ) {
|
|
|
708
710
|
return color;
|
|
709
711
|
}
|
|
710
712
|
|
|
713
|
+
// Matrices for rec 2020 <> rec 709 color space conversion
|
|
714
|
+
// matrix provided in row-major order so it has been transposed
|
|
715
|
+
// https://www.itu.int/pub/R-REP-BT.2407-2017
|
|
716
|
+
const mat3 LINEAR_REC2020_TO_LINEAR_SRGB = mat3(
|
|
717
|
+
vec3( 1.6605, - 0.1246, - 0.0182 ),
|
|
718
|
+
vec3( - 0.5876, 1.1329, - 0.1006 ),
|
|
719
|
+
vec3( - 0.0728, - 0.0083, 1.1187 )
|
|
720
|
+
);
|
|
721
|
+
|
|
722
|
+
const mat3 LINEAR_SRGB_TO_LINEAR_REC2020 = mat3(
|
|
723
|
+
vec3( 0.6274, 0.0691, 0.0164 ),
|
|
724
|
+
vec3( 0.3293, 0.9195, 0.0880 ),
|
|
725
|
+
vec3( 0.0433, 0.0113, 0.8956 )
|
|
726
|
+
);
|
|
727
|
+
|
|
728
|
+
// https://iolite-engine.com/blog_posts/minimal_agx_implementation
|
|
729
|
+
// Mean error^2: 3.6705141e-06
|
|
730
|
+
vec3 agxDefaultContrastApprox( vec3 x ) {
|
|
731
|
+
vec3 x2 = x * x;
|
|
732
|
+
vec3 x4 = x2 * x2;
|
|
733
|
+
return + 15.5 * x4 * x2
|
|
734
|
+
- 40.14 * x4 * x
|
|
735
|
+
+ 31.96 * x4
|
|
736
|
+
- 6.868 * x2 * x
|
|
737
|
+
+ 0.4298 * x2
|
|
738
|
+
+ 0.1191 * x
|
|
739
|
+
- 0.00232;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// AgX Tone Mapping implementation based on Filament, which in turn is based
|
|
743
|
+
// on Blender's implementation using rec 2020 primaries
|
|
744
|
+
// https://github.com/google/filament/pull/7236
|
|
745
|
+
// Inputs and outputs are encoded as Linear-sRGB.
|
|
746
|
+
|
|
747
|
+
vec3 AgXToneMapping( vec3 color ) {
|
|
748
|
+
|
|
749
|
+
// AgX constants
|
|
750
|
+
const mat3 AgXInsetMatrix = mat3(
|
|
751
|
+
vec3( 0.856627153315983, 0.137318972929847, 0.11189821299995 ),
|
|
752
|
+
vec3( 0.0951212405381588, 0.761241990602591, 0.0767994186031903 ),
|
|
753
|
+
vec3( 0.0482516061458583, 0.101439036467562, 0.811302368396859 )
|
|
754
|
+
);
|
|
755
|
+
|
|
756
|
+
// explicit AgXOutsetMatrix generated from Filaments AgXOutsetMatrixInv
|
|
757
|
+
const mat3 AgXOutsetMatrix = mat3(
|
|
758
|
+
vec3( 1.1271005818144368, - 0.1413297634984383, - 0.14132976349843826 ),
|
|
759
|
+
vec3( - 0.11060664309660323, 1.157823702216272, - 0.11060664309660294 ),
|
|
760
|
+
vec3( - 0.016493938717834573, - 0.016493938717834257, 1.2519364065950405 )
|
|
761
|
+
);
|
|
762
|
+
|
|
763
|
+
// LOG2_MIN = -10.0
|
|
764
|
+
// LOG2_MAX = +6.5
|
|
765
|
+
// MIDDLE_GRAY = 0.18
|
|
766
|
+
const float AgxMinEv = - 12.47393; // log2( pow( 2, LOG2_MIN ) * MIDDLE_GRAY )
|
|
767
|
+
const float AgxMaxEv = 4.026069; // log2( pow( 2, LOG2_MAX ) * MIDDLE_GRAY )
|
|
768
|
+
|
|
769
|
+
color = LINEAR_SRGB_TO_LINEAR_REC2020 * color;
|
|
770
|
+
|
|
771
|
+
color = AgXInsetMatrix * color;
|
|
772
|
+
|
|
773
|
+
// Log2 encoding
|
|
774
|
+
color = max( color, 1e-10 ); // avoid 0 or negative numbers for log2
|
|
775
|
+
color = log2( color );
|
|
776
|
+
color = ( color - AgxMinEv ) / ( AgxMaxEv - AgxMinEv );
|
|
777
|
+
|
|
778
|
+
color = clamp( color, 0.0, 1.0 );
|
|
779
|
+
|
|
780
|
+
// Apply sigmoid
|
|
781
|
+
color = agxDefaultContrastApprox( color );
|
|
782
|
+
|
|
783
|
+
// Apply AgX look
|
|
784
|
+
// v = agxLook(v, look);
|
|
785
|
+
|
|
786
|
+
color = AgXOutsetMatrix * color;
|
|
787
|
+
|
|
788
|
+
// Linearize
|
|
789
|
+
color = pow( max( vec3( 0.0 ), color ), vec3( 2.2 ) );
|
|
790
|
+
|
|
791
|
+
color = LINEAR_REC2020_TO_LINEAR_SRGB * color;
|
|
792
|
+
|
|
793
|
+
// Gamut mapping. Simple clamp for now.
|
|
794
|
+
color = clamp( color, 0.0, 1.0 );
|
|
795
|
+
|
|
796
|
+
return color;
|
|
797
|
+
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// https://modelviewer.dev/examples/tone-mapping
|
|
801
|
+
|
|
802
|
+
vec3 NeutralToneMapping( vec3 color ) {
|
|
803
|
+
|
|
804
|
+
const float StartCompression = 0.8 - 0.04;
|
|
805
|
+
const float Desaturation = 0.15;
|
|
806
|
+
|
|
807
|
+
float x = min( color.r, min( color.g, color.b ) );
|
|
808
|
+
|
|
809
|
+
float offset = x < 0.08 ? x - 6.25 * x * x : 0.04;
|
|
810
|
+
|
|
811
|
+
color -= offset;
|
|
812
|
+
|
|
813
|
+
float peak = max( color.r, max( color.g, color.b ) );
|
|
814
|
+
|
|
815
|
+
if ( peak < StartCompression ) return color;
|
|
816
|
+
|
|
817
|
+
float d = 1. - StartCompression;
|
|
818
|
+
|
|
819
|
+
float newPeak = 1. - d * d / ( peak + d - StartCompression );
|
|
820
|
+
|
|
821
|
+
color *= newPeak / peak;
|
|
822
|
+
|
|
823
|
+
float g = 1. - 1. / ( Desaturation * ( peak - newPeak ) + 1. );
|
|
824
|
+
|
|
825
|
+
return mix( color, vec3( newPeak ), g );
|
|
826
|
+
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
|
|
711
830
|
|
|
712
831
|
void main() {
|
|
713
832
|
vec4 color = texture2D(map, vUv);
|
|
@@ -783,6 +902,14 @@ void main() {
|
|
|
783
902
|
this.defines.TONEMAPPING_FUNCTION = 'LinearToneMapping';
|
|
784
903
|
valid = true;
|
|
785
904
|
break;
|
|
905
|
+
case three.AgXToneMapping:
|
|
906
|
+
this.defines.TONEMAPPING_FUNCTION = 'AgXToneMapping';
|
|
907
|
+
valid = true;
|
|
908
|
+
break;
|
|
909
|
+
case three.NeutralToneMapping:
|
|
910
|
+
this.defines.TONEMAPPING_FUNCTION = 'NeutralToneMapping';
|
|
911
|
+
valid = true;
|
|
912
|
+
break;
|
|
786
913
|
default:
|
|
787
914
|
console.error(`Unsupported toneMapping: ${value}. Using LinearToneMapping.`);
|
|
788
915
|
this.defines.TONEMAPPING_FUNCTION = 'LinearToneMapping';
|
|
@@ -967,7 +1094,7 @@ void main() {
|
|
|
967
1094
|
*
|
|
968
1095
|
* // embed the compressed images + metadata into a single
|
|
969
1096
|
* // JPEG file
|
|
970
|
-
* const jpeg =
|
|
1097
|
+
* const jpeg = encodeJPEGMetadata({
|
|
971
1098
|
* ...encodingResult,
|
|
972
1099
|
* sdr: encodingResult.sdr,
|
|
973
1100
|
* gainMap: encodingResult.gainMap
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @monogrid/gainmap-js v3.4.0
|
|
3
|
+
* With ❤️, by MONOGRID <gainmap@monogrid.com>
|
|
4
|
+
*/
|
|
5
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("three")):"function"==typeof define&&define.amd?define(["exports","three"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).encode={},e.three)}(this,function(e,t){"use strict";const n=async e=>{if("undefined"==typeof createImageBitmap)throw new Error("createImageBitmap() not supported.");const{source:t,mimeType:n,quality:r,flipY:a}=e;let o;if((t instanceof Uint8Array||t instanceof Uint8ClampedArray)&&"sourceMimeType"in e)o=new Blob([t],{type:e.sourceMimeType});else{if(!(t instanceof ImageData))throw new Error("Invalid source image");o=t}const i=await createImageBitmap(o),s=i.width,p=i.height;let c;"undefined"!=typeof OffscreenCanvas?c=new OffscreenCanvas(s,p):(c=document.createElement("canvas"),c.width=s,c.height=p);const l=c.getContext("2d");if(!l)throw new Error("Failed to create canvas Context");!0===a&&(l.translate(0,p),l.scale(1,-1)),l.drawImage(i,0,0,s,p);const d=await(async(e,t,n)=>{if("undefined"!=typeof OffscreenCanvas&&e instanceof OffscreenCanvas)return e.convertToBlob({type:t,quality:n});if(e instanceof HTMLCanvasElement)return new Promise((r,a)=>{e.toBlob(e=>{e?r(e):a(new Error("Failed to convert canvas to blob"))},t,n)});throw new Error("Unsupported canvas element")})(c,n,r||.9);return{data:new Uint8Array(await d.arrayBuffer()),mimeType:n,width:s,height:p}},r=e=>{let n;if(e instanceof t.DataTexture){if(!(e.image.data instanceof Uint16Array||e.image.data instanceof Float32Array))throw new Error("Provided image is not HDR");n=e}else n=new t.DataTexture(e.data,e.width,e.height,"format"in e?e.format:t.RGBAFormat,e.type,t.UVMapping,t.RepeatWrapping,t.RepeatWrapping,t.LinearFilter,t.LinearFilter,1,"colorSpace"in e&&"srgb"===e.colorSpace?e.colorSpace:t.LinearSRGBColorSpace),"header"in e&&"gamma"in e&&(n.flipY=!0),n.needsUpdate=!0;return n},a=(e,n,r)=>{let a;switch(e){case t.UnsignedByteType:a=new Uint8ClampedArray(n*r*4);break;case t.HalfFloatType:a=new Uint16Array(n*r*4);break;case t.UnsignedIntType:a=new Uint32Array(n*r*4);break;case t.ByteType:a=new Int8Array(n*r*4);break;case t.ShortType:a=new Int16Array(n*r*4);break;case t.IntType:a=new Int32Array(n*r*4);break;case t.FloatType:a=new Float32Array(n*r*4);break;default:throw new Error("Unsupported data type")}return a};let o;class i{_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 n={format:t.RGBAFormat,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:t.LinearFilter,minFilter:void 0!==e.renderTargetOptions?.minFilter?e.renderTargetOptions?.minFilter:t.LinearFilter,samples:void 0!==e.renderTargetOptions?.samples?e.renderTargetOptions?.samples:void 0,wrapS:void 0!==e.renderTargetOptions?.wrapS?e.renderTargetOptions?.wrapS:t.ClampToEdgeWrapping,wrapT:void 0!==e.renderTargetOptions?.wrapT?e.renderTargetOptions?.wrapT:t.ClampToEdgeWrapping};if(this._material=e.material,e.renderer?this._renderer=e.renderer:(this._renderer=i.instantiateRenderer(),this._rendererIsDisposable=!0),this._scene=new t.Scene,this._camera=new t.OrthographicCamera,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(),!((e,n,r,i)=>{if(void 0!==o)return o;const s=new t.WebGLRenderTarget(1,1,i);n.setRenderTarget(s);const p=new t.Mesh(new t.PlaneGeometry,new t.MeshBasicMaterial({color:16777215}));n.render(p,r),n.setRenderTarget(null);const c=a(e,s.width,s.height);return n.readRenderTargetPixels(s,0,0,s.width,s.height,c),s.dispose(),p.geometry.dispose(),p.material.dispose(),o=0!==c[0],o})(this._type,this._renderer,this._camera,n)){let e;if(this._type===t.HalfFloatType)e=this._renderer.extensions.has("EXT_color_buffer_float")?t.FloatType:void 0;void 0!==e?(console.warn(`This browser does not support reading pixels from ${this._type} RenderTargets, switching to ${t.FloatType}`),this._type=e):(this._supportsReadPixels=!1,console.warn("This browser dos not support toArray or toDataTexture, calls to those methods will result in an error thrown"))}this._quad=new t.Mesh(new t.PlaneGeometry,this._material),this._quad.geometry.computeBoundingBox(),this._scene.add(this._quad),this._renderTarget=new t.WebGLRenderTarget(this.width,this.height,n),this._renderTarget.texture.mapping=void 0!==e.renderTargetOptions?.mapping?e.renderTargetOptions?.mapping:t.UVMapping}static instantiateRenderer(){const e=new t.WebGLRenderer;return e.setSize(128,128),e}render=()=>{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)};toArray(){if(!this._supportsReadPixels)throw new Error("Can't read pixels in this browser");const e=a(this._type,this._width,this._height);return this._renderer.readRenderTargetPixels(this._renderTarget,0,0,this._width,this._height,e),e}toDataTexture(e){const n=new t.DataTexture(this.toArray(),this.width,this.height,t.RGBAFormat,this._type,e?.mapping||t.UVMapping,e?.wrapS||t.ClampToEdgeWrapping,e?.wrapT||t.ClampToEdgeWrapping,e?.magFilter||t.LinearFilter,e?.minFilter||t.LinearFilter,e?.anisotropy||1,t.LinearSRGBColorSpace);return n.generateMipmaps=void 0!==e?.generateMipmaps&&e?.generateMipmaps,n}disposeOnDemandRenderer(){this._renderer.setRenderTarget(null),this._rendererIsDisposable&&(this._renderer.dispose(),this._renderer.forceContextLoss())}dispose(e){this.disposeOnDemandRenderer(),e&&this.renderTarget.dispose(),this.material instanceof t.ShaderMaterial&&Object.values(this.material.uniforms).forEach(e=>{e.value instanceof t.Texture&&e.value.dispose()}),Object.values(this.material).forEach(e=>{e instanceof t.Texture&&e.dispose()}),this.material.dispose(),this._quad.geometry.dispose()}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}}class s extends t.ShaderMaterial{_minContentBoost;_maxContentBoost;_offsetSdr;_offsetHdr;_gamma;constructor({sdr:e,hdr:n,offsetSdr:r,offsetHdr:a,maxContentBoost:o,minContentBoost:i,gamma:s}){if(!o)throw new Error("maxContentBoost is required");if(!e)throw new Error("sdr is required");if(!n)throw new Error("hdr is required");const p=s||[1,1,1],c=r||[1/64,1/64,1/64],l=a||[1/64,1/64,1/64],d=i||1,m=Math.max(o,1.0001);super({name:"GainMapEncoderMaterial",vertexShader:"\nvarying vec2 vUv;\n\nvoid main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}\n",fragmentShader:"\n#ifndef saturate\n#define saturate( a ) clamp( a, 0.0, 1.0 )\n#endif\nuniform sampler2D sdr;\nuniform sampler2D hdr;\nuniform vec3 gamma;\nuniform vec3 offsetSdr;\nuniform vec3 offsetHdr;\nuniform float minLog2;\nuniform float maxLog2;\n\nvarying vec2 vUv;\n\nvoid main() {\n vec3 sdrColor = texture2D(sdr, vUv).rgb;\n vec3 hdrColor = texture2D(hdr, vUv).rgb;\n\n vec3 pixelGain = (hdrColor + offsetHdr) / (sdrColor + offsetSdr);\n vec3 logRecovery = (log2(pixelGain) - minLog2) / (maxLog2 - minLog2);\n vec3 clampedRecovery = saturate(logRecovery);\n gl_FragColor = vec4(pow(clampedRecovery, gamma), 1.0);\n}\n",uniforms:{sdr:{value:e},hdr:{value:n},gamma:{value:(new t.Vector3).fromArray(p)},offsetSdr:{value:(new t.Vector3).fromArray(c)},offsetHdr:{value:(new t.Vector3).fromArray(l)},minLog2:{value:Math.log2(d)},maxLog2:{value:Math.log2(m)}},blending:t.NoBlending,depthTest:!1,depthWrite:!1}),this._minContentBoost=d,this._maxContentBoost=m,this._offsetSdr=c,this._offsetHdr=l,this._gamma=p,this.needsUpdate=!0,this.uniformsNeedUpdate=!0}get gamma(){return this._gamma}set gamma(e){this._gamma=e,this.uniforms.gamma.value=(new t.Vector3).fromArray(e)}get offsetHdr(){return this._offsetHdr}set offsetHdr(e){this._offsetHdr=e,this.uniforms.offsetHdr.value=(new t.Vector3).fromArray(e)}get offsetSdr(){return this._offsetSdr}set offsetSdr(e){this._offsetSdr=e,this.uniforms.offsetSdr.value=(new t.Vector3).fromArray(e)}get minContentBoost(){return this._minContentBoost}set minContentBoost(e){this._minContentBoost=e,this.uniforms.minLog2.value=Math.log2(e)}get maxContentBoost(){return this._maxContentBoost}set maxContentBoost(e){this._maxContentBoost=e,this.uniforms.maxLog2.value=Math.log2(e)}get gainMapMin(){return[Math.log2(this._minContentBoost),Math.log2(this._minContentBoost),Math.log2(this._minContentBoost)]}get gainMapMax(){return[Math.log2(this._maxContentBoost),Math.log2(this._maxContentBoost),Math.log2(this._maxContentBoost)]}get hdrCapacityMin(){return Math.min(Math.max(0,this.gainMapMin[0]),Math.max(0,this.gainMapMin[1]),Math.max(0,this.gainMapMin[2]))}get hdrCapacityMax(){return Math.max(Math.max(0,this.gainMapMax[0]),Math.max(0,this.gainMapMax[1]),Math.max(0,this.gainMapMax[2]))}}const p=e=>{const{image:n,sdr:a,renderer:o}=e,p=r(n),c=new s({...e,sdr:a.renderTarget.texture,hdr:p}),l=new i({width:p.image.width,height:p.image.height,type:t.UnsignedByteType,colorSpace:t.LinearSRGBColorSpace,material:c,renderer:o,renderTargetOptions:e.renderTargetOptions});try{l.render()}catch(e){throw l.disposeOnDemandRenderer(),e}return l};class c extends t.ShaderMaterial{_brightness=0;_contrast=1;_saturation=1;_exposure=1;_toneMapping;_map;constructor({map:e,toneMapping:n}){super({name:"SDRMaterial",vertexShader:"\nvarying vec2 vUv;\n\nvoid main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}\n",fragmentShader:"\n#ifndef saturate\n#define saturate( a ) clamp( a, 0.0, 1.0 )\n#endif\n\nuniform sampler2D map;\nuniform float brightness;\nuniform float contrast;\nuniform float saturation;\nuniform float exposure;\n\nvarying vec2 vUv;\n\nmat4 brightnessMatrix( float brightness ) {\n return mat4(\n 1, 0, 0, 0,\n 0, 1, 0, 0,\n 0, 0, 1, 0,\n brightness, brightness, brightness, 1\n );\n}\n\nmat4 contrastMatrix( float contrast ) {\n float t = ( 1.0 - contrast ) / 2.0;\n return mat4(\n contrast, 0, 0, 0,\n 0, contrast, 0, 0,\n 0, 0, contrast, 0,\n t, t, t, 1\n );\n}\n\nmat4 saturationMatrix( float saturation ) {\n vec3 luminance = vec3( 0.3086, 0.6094, 0.0820 );\n float oneMinusSat = 1.0 - saturation;\n vec3 red = vec3( luminance.x * oneMinusSat );\n red+= vec3( saturation, 0, 0 );\n vec3 green = vec3( luminance.y * oneMinusSat );\n green += vec3( 0, saturation, 0 );\n vec3 blue = vec3( luminance.z * oneMinusSat );\n blue += vec3( 0, 0, saturation );\n return mat4(\n red, 0,\n green, 0,\n blue, 0,\n 0, 0, 0, 1\n );\n}\n\nvec3 RRTAndODTFit( vec3 v ) {\n vec3 a = v * ( v + 0.0245786 ) - 0.000090537;\n vec3 b = v * ( 0.983729 * v + 0.4329510 ) + 0.238081;\n return a / b;\n}\n\nvec3 ACESFilmicToneMapping( vec3 color ) {\n // sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT\n const mat3 ACESInputMat = mat3(\n vec3( 0.59719, 0.07600, 0.02840 ), // transposed from source\n vec3( 0.35458, 0.90834, 0.13383 ),\n vec3( 0.04823, 0.01566, 0.83777 )\n );\n // ODT_SAT => XYZ => D60_2_D65 => sRGB\n const mat3 ACESOutputMat = mat3(\n vec3( 1.60475, -0.10208, -0.00327 ), // transposed from source\n vec3( -0.53108, 1.10813, -0.07276 ),\n vec3( -0.07367, -0.00605, 1.07602 )\n );\n color = ACESInputMat * color;\n // Apply RRT and ODT\n color = RRTAndODTFit( color );\n color = ACESOutputMat * color;\n // Clamp to [0, 1]\n return saturate( color );\n}\n\n// source: https://www.cs.utah.edu/docs/techreports/2002/pdf/UUCS-02-001.pdf\nvec3 ReinhardToneMapping( vec3 color ) {\n return saturate( color / ( vec3( 1.0 ) + color ) );\n}\n\n// source: http://filmicworlds.com/blog/filmic-tonemapping-operators/\nvec3 CineonToneMapping( vec3 color ) {\n // optimized filmic operator by Jim Hejl and Richard Burgess-Dawson\n color = max( vec3( 0.0 ), color - 0.004 );\n return pow( ( color * ( 6.2 * color + 0.5 ) ) / ( color * ( 6.2 * color + 1.7 ) + 0.06 ), vec3( 2.2 ) );\n}\n\n// nothing\nvec3 LinearToneMapping ( vec3 color ) {\n return color;\n}\n\n// Matrices for rec 2020 <> rec 709 color space conversion\n// matrix provided in row-major order so it has been transposed\n// https://www.itu.int/pub/R-REP-BT.2407-2017\nconst mat3 LINEAR_REC2020_TO_LINEAR_SRGB = mat3(\n vec3( 1.6605, - 0.1246, - 0.0182 ),\n vec3( - 0.5876, 1.1329, - 0.1006 ),\n vec3( - 0.0728, - 0.0083, 1.1187 )\n);\n\nconst mat3 LINEAR_SRGB_TO_LINEAR_REC2020 = mat3(\n vec3( 0.6274, 0.0691, 0.0164 ),\n vec3( 0.3293, 0.9195, 0.0880 ),\n vec3( 0.0433, 0.0113, 0.8956 )\n);\n\n// https://iolite-engine.com/blog_posts/minimal_agx_implementation\n// Mean error^2: 3.6705141e-06\nvec3 agxDefaultContrastApprox( vec3 x ) {\n vec3 x2 = x * x;\n vec3 x4 = x2 * x2;\n return + 15.5 * x4 * x2\n - 40.14 * x4 * x\n + 31.96 * x4\n - 6.868 * x2 * x\n + 0.4298 * x2\n + 0.1191 * x\n - 0.00232;\n}\n\n// AgX Tone Mapping implementation based on Filament, which in turn is based\n// on Blender's implementation using rec 2020 primaries\n// https://github.com/google/filament/pull/7236\n// Inputs and outputs are encoded as Linear-sRGB.\n\nvec3 AgXToneMapping( vec3 color ) {\n\n // AgX constants\n const mat3 AgXInsetMatrix = mat3(\n vec3( 0.856627153315983, 0.137318972929847, 0.11189821299995 ),\n vec3( 0.0951212405381588, 0.761241990602591, 0.0767994186031903 ),\n vec3( 0.0482516061458583, 0.101439036467562, 0.811302368396859 )\n );\n\n // explicit AgXOutsetMatrix generated from Filaments AgXOutsetMatrixInv\n const mat3 AgXOutsetMatrix = mat3(\n vec3( 1.1271005818144368, - 0.1413297634984383, - 0.14132976349843826 ),\n vec3( - 0.11060664309660323, 1.157823702216272, - 0.11060664309660294 ),\n vec3( - 0.016493938717834573, - 0.016493938717834257, 1.2519364065950405 )\n );\n\n // LOG2_MIN = -10.0\n // LOG2_MAX = +6.5\n // MIDDLE_GRAY = 0.18\n const float AgxMinEv = - 12.47393; // log2( pow( 2, LOG2_MIN ) * MIDDLE_GRAY )\n const float AgxMaxEv = 4.026069; // log2( pow( 2, LOG2_MAX ) * MIDDLE_GRAY )\n\n color = LINEAR_SRGB_TO_LINEAR_REC2020 * color;\n\n color = AgXInsetMatrix * color;\n\n // Log2 encoding\n color = max( color, 1e-10 ); // avoid 0 or negative numbers for log2\n color = log2( color );\n color = ( color - AgxMinEv ) / ( AgxMaxEv - AgxMinEv );\n\n color = clamp( color, 0.0, 1.0 );\n\n // Apply sigmoid\n color = agxDefaultContrastApprox( color );\n\n // Apply AgX look\n // v = agxLook(v, look);\n\n color = AgXOutsetMatrix * color;\n\n // Linearize\n color = pow( max( vec3( 0.0 ), color ), vec3( 2.2 ) );\n\n color = LINEAR_REC2020_TO_LINEAR_SRGB * color;\n\n // Gamut mapping. Simple clamp for now.\n color = clamp( color, 0.0, 1.0 );\n\n return color;\n\n}\n\n// https://modelviewer.dev/examples/tone-mapping\n\nvec3 NeutralToneMapping( vec3 color ) {\n\n const float StartCompression = 0.8 - 0.04;\n const float Desaturation = 0.15;\n\n float x = min( color.r, min( color.g, color.b ) );\n\n float offset = x < 0.08 ? x - 6.25 * x * x : 0.04;\n\n color -= offset;\n\n float peak = max( color.r, max( color.g, color.b ) );\n\n if ( peak < StartCompression ) return color;\n\n float d = 1. - StartCompression;\n\n float newPeak = 1. - d * d / ( peak + d - StartCompression );\n\n color *= newPeak / peak;\n\n float g = 1. - 1. / ( Desaturation * ( peak - newPeak ) + 1. );\n\n return mix( color, vec3( newPeak ), g );\n\n}\n\n\n\nvoid main() {\n vec4 color = texture2D(map, vUv);\n\n vec4 exposed = vec4(exposure * color.rgb, color.a);\n\n vec4 tonemapped = vec4(TONEMAPPING_FUNCTION(exposed.rgb), color.a);\n\n vec4 adjusted =\n brightnessMatrix( brightness ) *\n contrastMatrix( contrast ) *\n saturationMatrix( saturation ) *\n tonemapped;\n\n gl_FragColor = adjusted;\n}\n",uniforms:{map:{value:e},brightness:{value:0},contrast:{value:1},saturation:{value:1},exposure:{value:1}},blending:t.NoBlending,depthTest:!1,depthWrite:!1}),this._map=e,this.toneMapping=this._toneMapping=n||t.ACESFilmicToneMapping,this.needsUpdate=!0,this.uniformsNeedUpdate=!0}get toneMapping(){return this._toneMapping}set toneMapping(e){let n=!1;switch(e){case t.ACESFilmicToneMapping:this.defines.TONEMAPPING_FUNCTION="ACESFilmicToneMapping",n=!0;break;case t.ReinhardToneMapping:this.defines.TONEMAPPING_FUNCTION="ReinhardToneMapping",n=!0;break;case t.CineonToneMapping:this.defines.TONEMAPPING_FUNCTION="CineonToneMapping",n=!0;break;case t.LinearToneMapping:this.defines.TONEMAPPING_FUNCTION="LinearToneMapping",n=!0;break;case t.AgXToneMapping:this.defines.TONEMAPPING_FUNCTION="AgXToneMapping",n=!0;break;case t.NeutralToneMapping:this.defines.TONEMAPPING_FUNCTION="NeutralToneMapping",n=!0;break;default:console.error(`Unsupported toneMapping: ${e}. Using LinearToneMapping.`),this.defines.TONEMAPPING_FUNCTION="LinearToneMapping",this._toneMapping=t.LinearToneMapping}n&&(this._toneMapping=e),this.needsUpdate=!0}get brightness(){return this._brightness}set brightness(e){this._brightness=e,this.uniforms.brightness.value=e}get contrast(){return this._contrast}set contrast(e){this._contrast=e,this.uniforms.contrast.value=e}get saturation(){return this._saturation}set saturation(e){this._saturation=e,this.uniforms.saturation.value=e}get exposure(){return this._exposure}set exposure(e){this._exposure=e,this.uniforms.exposure.value=e}get map(){return this._map}set map(e){this._map=e,this.uniforms.map.value=e}}const l=(e,n,r,a)=>{e.needsUpdate=!0;const o=new i({width:e.image.width,height:e.image.height,type:t.UnsignedByteType,colorSpace:t.SRGBColorSpace,material:new c({map:e,toneMapping:r}),renderer:n,renderTargetOptions:a});try{o.render()}catch(e){throw o.disposeOnDemandRenderer(),e}return o},d=e=>{const{image:t,renderer:n}=e,a=r(t),o=l(a,n,e.toneMapping,e.renderTargetOptions),i=p({...e,image:a,sdr:o,renderer:o.renderer});return{sdr:o,gainMap:i,hdr:a,getMetadata:()=>({gainMapMax:i.material.gainMapMax,gainMapMin:i.material.gainMapMin,gamma:i.material.gamma,hdrCapacityMax:i.material.hdrCapacityMax,hdrCapacityMin:i.material.hdrCapacityMin,offsetHdr:i.material.offsetHdr,offsetSdr:i.material.offsetSdr})}};e.GainMapEncoderMaterial=s,e.SDRMaterial=c,e.compress=n,e.encode=d,e.encodeAndCompress=async e=>{const t=d(e),{mimeType:r,quality:a,flipY:o,withWorker:i}=e;let s,p,c;const l=new ImageData(t.sdr.toArray(),t.sdr.width,t.sdr.height),m=new ImageData(t.gainMap.toArray(),t.gainMap.width,t.gainMap.height);if(i){const e=await Promise.all([i.compress({source:l,mimeType:r,quality:a,flipY:o}),i.compress({source:m,mimeType:r,quality:a,flipY:o})]);s=e,p=e[0].source,c=e[1].source}else s=await Promise.all([n({source:l,mimeType:r,quality:a,flipY:o}),n({source:m,mimeType:r,quality:a,flipY:o})]),p=l.data,c=m.data;return t.sdr.dispose(),t.gainMap.dispose(),{...t,...t.getMetadata(),sdr:s[0],gainMap:s[1],rawSDR:p,rawGainMap:c}},e.findTextureMinMax=(e,n="max",a)=>{const o=r(e);if(o.format!==t.AlphaFormat&&o.format!==t.RGBFormat&&o.format!==t.RGBAFormat&&o.format!==t.DepthFormat&&o.format!==t.DepthStencilFormat&&o.format!==t.RedFormat&&o.format!==t.RedIntegerFormat&&o.format!==t.RGFormat&&o.format!==t.RGIntegerFormat&&o.format!==t.RGBIntegerFormat&&o.format!==t.RGBAIntegerFormat)throw new Error("Unsupported texture format");const s=new t.ShaderMaterial({vertexShader:"\nvarying vec2 vUv;\nvoid main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}\n",fragmentShader:"\nprecision mediump float;\n\n#ifndef CELL_SIZE\n #define CELL_SIZE 2\n#endif\n\n#ifndef COMPARE_FUNCTION\n #define COMPARE_FUNCTION max\n#endif\n\n#ifndef INITIAL_VALUE\n #define INITIAL_VALUE 0\n#endif\n\nuniform sampler2D map;\nuniform vec2 u_srcResolution;\n\nvarying vec2 vUv;\n\nvoid main() {\n // compute the first pixel the source cell\n vec2 srcPixel = floor(gl_FragCoord.xy) * float(CELL_SIZE);\n\n // one pixel in source\n vec2 onePixel = vec2(1) / u_srcResolution;\n\n // uv for first pixel in cell. +0.5 for center of pixel\n vec2 uv = (srcPixel + 0.5) * onePixel;\n\n vec4 resultColor = vec4(INITIAL_VALUE);\n\n for (int y = 0; y < CELL_SIZE; ++y) {\n for (int x = 0; x < CELL_SIZE; ++x) {\n resultColor = COMPARE_FUNCTION(resultColor, texture2D(map, uv + vec2(x, y) * onePixel));\n }\n }\n\n gl_FragColor = resultColor;\n}\n",uniforms:{u_srcResolution:{value:new t.Vector2(o.image.width,o.image.height)},map:{value:o}},defines:{CELL_SIZE:2,COMPARE_FUNCTION:n,INITIAL_VALUE:"max"===n?0:65504}});o.needsUpdate=!0,s.needsUpdate=!0;let p=o.image.width,c=o.image.height;const l=new i({width:p,height:c,type:o.type,colorSpace:o.colorSpace,material:s,renderer:a}),d=[];for(;p>1||c>1;){p=Math.max(1,(p+2-1)/2|0),c=Math.max(1,(c+2-1)/2|0);const e=new t.WebGLRenderTarget(p,c,{type:l.type,format:o.format,colorSpace:l.colorSpace,minFilter:t.NearestFilter,magFilter:t.NearestFilter,wrapS:t.ClampToEdgeWrapping,wrapT:t.ClampToEdgeWrapping,generateMipmaps:!1,depthBuffer:!1,stencilBuffer:!1});d.push(e)}p=o.image.width,c=o.image.height,d.forEach(e=>{p=Math.max(1,(p+2-1)/2|0),c=Math.max(1,(c+2-1)/2|0),l.renderTarget=e,l.render(),s.uniforms.map.value=e.texture,s.uniforms.u_srcResolution.value.x=p,s.uniforms.u_srcResolution.value.y=c});const m=l.toArray();return l.dispose(),d.forEach(e=>e.dispose()),[l.type===t.FloatType?m[0]:t.DataUtils.fromHalfFloat(m[0]),l.type===t.FloatType?m[1]:t.DataUtils.fromHalfFloat(m[1]),l.type===t.FloatType?m[2]:t.DataUtils.fromHalfFloat(m[2])]},e.getGainMap=p,e.getSDRRendition=l});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GainMapMetadata } from '../core/types';
|
|
2
2
|
import { type CompressedImage } from '../encode/types';
|
|
3
3
|
/**
|
|
4
4
|
* Encapsulates a Gainmap into a single JPEG file (aka: JPEG-R) with the base map
|
|
@@ -63,7 +63,7 @@ import { type CompressedImage } from '../encode/types';
|
|
|
63
63
|
*
|
|
64
64
|
* // embed the compressed images + metadata into a single
|
|
65
65
|
* // JPEG file
|
|
66
|
-
* const jpeg =
|
|
66
|
+
* const jpeg = encodeJPEGMetadata({
|
|
67
67
|
* ...encodingResult,
|
|
68
68
|
* ...metadata,
|
|
69
69
|
* sdr,
|
|
@@ -73,12 +73,12 @@ import { type CompressedImage } from '../encode/types';
|
|
|
73
73
|
* // `jpeg` will be an `Uint8Array` which can be saved somewhere
|
|
74
74
|
*
|
|
75
75
|
*
|
|
76
|
-
* @param encodingResult
|
|
77
|
-
* @returns
|
|
76
|
+
* @param encodingResult - Encoding result containing SDR image, gain map image, and metadata
|
|
77
|
+
* @returns A Uint8Array representing a JPEG-R file
|
|
78
78
|
* @throws {Error} If `encodingResult.sdr.mimeType !== 'image/jpeg'`
|
|
79
79
|
* @throws {Error} If `encodingResult.gainMap.mimeType !== 'image/jpeg'`
|
|
80
80
|
*/
|
|
81
81
|
export declare const encodeJPEGMetadata: (encodingResult: GainMapMetadata & {
|
|
82
82
|
sdr: CompressedImage;
|
|
83
83
|
gainMap: CompressedImage;
|
|
84
|
-
}) =>
|
|
84
|
+
}) => Uint8Array<ArrayBuffer>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JPEG assembler for creating JPEG-R (JPEG with gain map) files
|
|
3
|
+
* Based on libultrahdr jpegr.cpp implementation
|
|
4
|
+
*/
|
|
5
|
+
import { GainMapMetadataExtended } from '../core/types';
|
|
6
|
+
import { type CompressedImage } from '../encode/types';
|
|
7
|
+
/**
|
|
8
|
+
* Options for assembling a JPEG with gain map
|
|
9
|
+
*/
|
|
10
|
+
export interface AssembleJpegOptions {
|
|
11
|
+
/** Primary (SDR) JPEG image */
|
|
12
|
+
sdr: CompressedImage;
|
|
13
|
+
/** Gain map JPEG image */
|
|
14
|
+
gainMap: CompressedImage;
|
|
15
|
+
/** Gain map metadata */
|
|
16
|
+
metadata: GainMapMetadataExtended;
|
|
17
|
+
/** Optional EXIF data to embed */
|
|
18
|
+
exif?: Uint8Array<ArrayBuffer>;
|
|
19
|
+
/** Optional ICC color profile */
|
|
20
|
+
icc?: Uint8Array<ArrayBuffer>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Assemble a JPEG-R file (JPEG with embedded gain map)
|
|
24
|
+
*
|
|
25
|
+
* The structure is:
|
|
26
|
+
* 1. Primary image:
|
|
27
|
+
* - SOI
|
|
28
|
+
* - APP1 (EXIF if present)
|
|
29
|
+
* - APP1 (XMP with gain map metadata)
|
|
30
|
+
* - APP2 (ICC profile if present)
|
|
31
|
+
* - APP2 (MPF data)
|
|
32
|
+
* - Rest of primary JPEG data
|
|
33
|
+
* 2. Secondary image (gain map):
|
|
34
|
+
* - SOI
|
|
35
|
+
* - APP1 (XMP with gain map parameters)
|
|
36
|
+
* - Rest of gain map JPEG data
|
|
37
|
+
*
|
|
38
|
+
* @param options - Assembly options
|
|
39
|
+
* @returns Complete JPEG-R file as Uint8Array
|
|
40
|
+
*/
|
|
41
|
+
export declare function assembleJpegWithGainMap(options: AssembleJpegOptions): Uint8Array<ArrayBuffer>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JPEG marker constants
|
|
3
|
+
* Based on JPEG specification and libultrahdr implementation
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* JPEG marker prefix - all markers start with this byte
|
|
7
|
+
*/
|
|
8
|
+
export declare const MARKER_PREFIX = 255;
|
|
9
|
+
/**
|
|
10
|
+
* JPEG markers
|
|
11
|
+
*/
|
|
12
|
+
export declare const MARKERS: {
|
|
13
|
+
/** Start of Image */
|
|
14
|
+
readonly SOI: 216;
|
|
15
|
+
/** End of Image */
|
|
16
|
+
readonly EOI: 217;
|
|
17
|
+
/** Application segment 0 */
|
|
18
|
+
readonly APP0: 224;
|
|
19
|
+
/** Application segment 1 (EXIF/XMP) */
|
|
20
|
+
readonly APP1: 225;
|
|
21
|
+
/** Application segment 2 (ICC/MPF) */
|
|
22
|
+
readonly APP2: 226;
|
|
23
|
+
/** Start of Scan */
|
|
24
|
+
readonly SOS: 218;
|
|
25
|
+
/** Define Quantization Table */
|
|
26
|
+
readonly DQT: 219;
|
|
27
|
+
/** Define Huffman Table */
|
|
28
|
+
readonly DHT: 196;
|
|
29
|
+
/** Start of Frame (baseline DCT) */
|
|
30
|
+
readonly SOF0: 192;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* XMP namespace identifier for APP1 marker
|
|
34
|
+
*/
|
|
35
|
+
export declare const XMP_NAMESPACE = "http://ns.adobe.com/xap/1.0/\0";
|
|
36
|
+
/**
|
|
37
|
+
* EXIF identifier for APP1 marker
|
|
38
|
+
*/
|
|
39
|
+
export declare const EXIF_IDENTIFIER = "Exif\0\0";
|
|
40
|
+
/**
|
|
41
|
+
* MPF signature for APP2 marker
|
|
42
|
+
*/
|
|
43
|
+
export declare const MPF_SIGNATURE = "MPF\0";
|
|
44
|
+
/**
|
|
45
|
+
* ICC profile identifier for APP2 marker
|
|
46
|
+
*/
|
|
47
|
+
export declare const ICC_IDENTIFIER = "ICC_PROFILE\0";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-Picture Format (MPF) generator
|
|
3
|
+
* Based on CIPA DC-007 specification and libultrahdr multipictureformat.cpp
|
|
4
|
+
*
|
|
5
|
+
* MPF is used to embed multiple images in a single JPEG file
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Calculate the total size of the MPF structure
|
|
9
|
+
*/
|
|
10
|
+
export declare function calculateMpfSize(): number;
|
|
11
|
+
/**
|
|
12
|
+
* Generate MPF (Multi-Picture Format) data structure
|
|
13
|
+
*
|
|
14
|
+
* @param primaryImageSize - Size of the primary image in bytes
|
|
15
|
+
* @param primaryImageOffset - Offset of the primary image (typically 0 for FII - First Individual Image)
|
|
16
|
+
* @param secondaryImageSize - Size of the secondary (gain map) image in bytes
|
|
17
|
+
* @param secondaryImageOffset - Offset of the secondary image from the MP Endian field
|
|
18
|
+
* @returns Uint8Array containing the MPF data
|
|
19
|
+
*/
|
|
20
|
+
export declare function generateMpf(primaryImageSize: number, primaryImageOffset: number, secondaryImageSize: number, secondaryImageOffset: number): Uint8Array<ArrayBuffer>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* XMP metadata generator for gain map images
|
|
3
|
+
* Based on libultrahdr jpegrutils.cpp implementation
|
|
4
|
+
*/
|
|
5
|
+
import { type GainMapMetadataExtended } from '../core/types';
|
|
6
|
+
/**
|
|
7
|
+
* Generate XMP metadata for the primary image
|
|
8
|
+
*
|
|
9
|
+
* This XMP contains:
|
|
10
|
+
* - Container directory with references to primary and gain map images
|
|
11
|
+
* - Gain map version
|
|
12
|
+
* - Item metadata for both images
|
|
13
|
+
*
|
|
14
|
+
* @param secondaryImageLength - Length of the secondary (gain map) JPEG in bytes
|
|
15
|
+
* @param metadata - Gain map metadata
|
|
16
|
+
* @returns XMP packet as string
|
|
17
|
+
*/
|
|
18
|
+
export declare function generateXmpForPrimaryImage(secondaryImageLength: number, metadata: GainMapMetadataExtended): string;
|
|
19
|
+
/**
|
|
20
|
+
* Generate XMP metadata for the secondary (gain map) image
|
|
21
|
+
*
|
|
22
|
+
* This XMP contains all the gain map parameters:
|
|
23
|
+
* - Version
|
|
24
|
+
* - Gain map min/max
|
|
25
|
+
* - Gamma
|
|
26
|
+
* - Offset SDR/HDR
|
|
27
|
+
* - HDR capacity min/max
|
|
28
|
+
* - Base rendition flag
|
|
29
|
+
*
|
|
30
|
+
* @param metadata - Gain map metadata
|
|
31
|
+
* @returns XMP packet as string
|
|
32
|
+
*/
|
|
33
|
+
export declare function generateXmpForSecondaryImage(metadata: GainMapMetadataExtended): string;
|
package/dist/libultrahdr.d.ts
CHANGED