@monogrid/gainmap-js 3.0.6 → 3.2.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/CODE_OF_CONDUCT.md +1 -1
- package/CONTRIBUTING.md +1 -1
- package/README.md +106 -2
- package/dist/Loader-BZcKUVaX.js +424 -0
- package/dist/{QuadRenderer-B3h-sJtp.js → QuadRenderer-Y5tm-R3m.js} +39 -30
- package/dist/{_commonjsHelpers-UqyNh5ZN.js → _commonjsHelpers-CuGYGi_5.js} +2 -2
- package/dist/{compress-CAiPfiRr.js → compress-BaKKOAXI.js} +2 -3
- package/dist/core/QuadRenderer.d.ts +2 -2
- package/dist/core/get-data-texture.d.ts +4 -3
- package/dist/core/types.d.ts +2 -1
- package/dist/decode/index.d.ts +1 -11
- package/dist/decode/shared/Loader.d.ts +59 -0
- package/dist/decode/shared/decode.d.ts +24 -0
- package/dist/decode/{extract.d.ts → shared/extract.d.ts} +4 -4
- package/dist/decode/shared/index.d.ts +8 -0
- package/dist/decode/shared/types.d.ts +53 -0
- package/dist/decode/{utils → shared/utils}/MPFExtractor.d.ts +1 -1
- package/dist/decode/shared/utils/extractXMP.d.ts +2 -0
- package/dist/decode/{decode.d.ts → webgl/decode.d.ts} +6 -6
- package/dist/decode/webgl/index.d.ts +8 -0
- package/dist/decode/{loaders → webgl/loaders}/GainMapLoader.d.ts +5 -6
- package/dist/decode/{loaders → webgl/loaders}/HDRJPGLoader.d.ts +6 -7
- package/dist/decode/webgl/loaders/LoaderBaseWebGL.d.ts +20 -0
- package/dist/decode/{materials → webgl/materials}/GainMapDecoderMaterial.d.ts +2 -2
- package/dist/decode/webgpu/core/QuadRenderer.d.ts +153 -0
- package/dist/decode/webgpu/decode.d.ts +60 -0
- package/dist/decode/webgpu/index.d.ts +8 -0
- package/dist/decode/webgpu/loaders/GainMapLoader.d.ts +69 -0
- package/dist/decode/webgpu/loaders/HDRJPGLoader.d.ts +65 -0
- package/dist/decode/webgpu/loaders/LoaderBaseWebGPU.d.ts +20 -0
- package/dist/decode/webgpu/materials/GainMapDecoderMaterial.d.ts +78 -0
- package/dist/decode.js +45 -442
- package/dist/decode.umd.cjs +388 -358
- package/dist/decode.webgpu.js +814 -0
- package/dist/decode.webgpu.umd.cjs +1244 -0
- package/dist/encode/encode-and-compress.d.ts +3 -3
- package/dist/encode/encode.d.ts +3 -3
- package/dist/encode/find-texture-min-max.d.ts +4 -3
- package/dist/encode/types.d.ts +4 -3
- package/dist/encode.js +32 -11
- package/dist/encode.umd.cjs +67 -38
- package/dist/libultrahdr/decode-jpeg-metadata.d.ts +0 -3
- package/dist/libultrahdr/encode-jpeg-metadata.d.ts +2 -2
- package/dist/libultrahdr.js +42 -75
- package/dist/libultrahdr.umd.cjs +46 -79
- package/dist/worker-interface.js +74 -64
- package/dist/worker-interface.umd.cjs +73 -63
- package/dist/worker.js +103 -88
- package/dist/worker.umd.cjs +103 -89
- package/package.json +79 -72
- package/dist/decode/loaders/LoaderBase.d.ts +0 -45
- package/dist/decode/types.d.ts +0 -42
- package/dist/decode/utils/extractXMP.d.ts +0 -7
- /package/dist/decode/{errors → shared/errors}/GainMapNotFoundError.d.ts +0 -0
- /package/dist/decode/{errors → shared/errors}/XMPMetadataNotFoundError.d.ts +0 -0
- /package/dist/decode/{utils → shared/utils}/get-html-image-from-blob.d.ts +0 -0
package/dist/decode.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @monogrid/gainmap-js v3.0
|
|
3
|
-
* With ❤️, by MONOGRID <
|
|
2
|
+
* @monogrid/gainmap-js v3.2.0
|
|
3
|
+
* With ❤️, by MONOGRID <gainmap@monogrid.com>
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { Q as QuadRenderer } from './QuadRenderer-
|
|
7
|
-
import {
|
|
6
|
+
import { Q as QuadRenderer } from './QuadRenderer-Y5tm-R3m.js';
|
|
7
|
+
import { c as createDecodeFunction, L as LoaderBaseShared, e as extractGainmapFromJPEG, X as XMPMetadataNotFoundError, G as GainMapNotFoundError } from './Loader-BZcKUVaX.js';
|
|
8
|
+
export { M as MPFExtractor, a as extractXMP } from './Loader-BZcKUVaX.js';
|
|
9
|
+
import { ShaderMaterial, NoBlending, Vector3, WebGLRenderer, FileLoader } from 'three';
|
|
8
10
|
|
|
9
11
|
const vertexShader = /* glsl */ `
|
|
10
12
|
varying vec2 vUv;
|
|
@@ -48,6 +50,9 @@ void main() {
|
|
|
48
50
|
* @group Materials
|
|
49
51
|
*/
|
|
50
52
|
class GainMapDecoderMaterial extends ShaderMaterial {
|
|
53
|
+
_maxDisplayBoost;
|
|
54
|
+
_hdrCapacityMin;
|
|
55
|
+
_hdrCapacityMax;
|
|
51
56
|
/**
|
|
52
57
|
*
|
|
53
58
|
* @param params
|
|
@@ -149,8 +154,13 @@ class GainMapDecoderMaterial extends ShaderMaterial {
|
|
|
149
154
|
}
|
|
150
155
|
}
|
|
151
156
|
|
|
157
|
+
const decodeImpl = createDecodeFunction({
|
|
158
|
+
renderer: WebGLRenderer,
|
|
159
|
+
createMaterial: (params) => new GainMapDecoderMaterial(params),
|
|
160
|
+
createQuadRenderer: (params) => new QuadRenderer(params)
|
|
161
|
+
});
|
|
152
162
|
/**
|
|
153
|
-
* Decodes a gain map using a
|
|
163
|
+
* Decodes a gain map using a WebGL RenderTarget
|
|
154
164
|
*
|
|
155
165
|
* @category Decoding Functions
|
|
156
166
|
* @group Decoding Functions
|
|
@@ -177,7 +187,7 @@ class GainMapDecoderMaterial extends ShaderMaterial {
|
|
|
177
187
|
* // load metadata
|
|
178
188
|
* const metadata = await (await fetch('metadata.json')).json()
|
|
179
189
|
*
|
|
180
|
-
* const result =
|
|
190
|
+
* const result = decode({
|
|
181
191
|
* sdr,
|
|
182
192
|
* gainMap,
|
|
183
193
|
* // this allows to use `result.renderTarget.texture` directly
|
|
@@ -205,34 +215,13 @@ class GainMapDecoderMaterial extends ShaderMaterial {
|
|
|
205
215
|
* @throws {Error} if the WebGLRenderer fails to render the gain map
|
|
206
216
|
*/
|
|
207
217
|
const decode = (params) => {
|
|
208
|
-
|
|
209
|
-
if (
|
|
210
|
-
|
|
211
|
-
sdr.colorSpace = SRGBColorSpace;
|
|
218
|
+
// Ensure renderer is defined for the base function
|
|
219
|
+
if (!params.renderer) {
|
|
220
|
+
throw new Error('Renderer is required for decode function');
|
|
212
221
|
}
|
|
213
|
-
|
|
214
|
-
if (gainMap.colorSpace !== LinearSRGBColorSpace) {
|
|
215
|
-
console.warn('Gainmap Colorspace needs to be *LinearSRGBColorSpace*, setting it automatically');
|
|
216
|
-
gainMap.colorSpace = LinearSRGBColorSpace;
|
|
217
|
-
}
|
|
218
|
-
gainMap.needsUpdate = true;
|
|
219
|
-
const material = new GainMapDecoderMaterial({
|
|
222
|
+
const quadRenderer = decodeImpl({
|
|
220
223
|
...params,
|
|
221
|
-
|
|
222
|
-
gainMap
|
|
223
|
-
});
|
|
224
|
-
const quadRenderer = new QuadRenderer({
|
|
225
|
-
// TODO: three types are generic, eslint complains here, see how we can solve
|
|
226
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
|
227
|
-
width: sdr.image.width,
|
|
228
|
-
// TODO: three types are generic, eslint complains here, see how we can solve
|
|
229
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
|
230
|
-
height: sdr.image.height,
|
|
231
|
-
type: HalfFloatType,
|
|
232
|
-
colorSpace: LinearSRGBColorSpace,
|
|
233
|
-
material,
|
|
234
|
-
renderer,
|
|
235
|
-
renderTargetOptions: params.renderTargetOptions
|
|
224
|
+
renderer: params.renderer
|
|
236
225
|
});
|
|
237
226
|
try {
|
|
238
227
|
quadRenderer.render();
|
|
@@ -244,413 +233,29 @@ const decode = (params) => {
|
|
|
244
233
|
return quadRenderer;
|
|
245
234
|
};
|
|
246
235
|
|
|
247
|
-
class GainMapNotFoundError extends Error {
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
class XMPMetadataNotFoundError extends Error {
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
const getAttribute = (description, name, defaultValue) => {
|
|
254
|
-
var _a;
|
|
255
|
-
let returnValue;
|
|
256
|
-
const parsedValue = (_a = description.attributes.getNamedItem(name)) === null || _a === void 0 ? void 0 : _a.nodeValue;
|
|
257
|
-
if (!parsedValue) {
|
|
258
|
-
const node = description.getElementsByTagName(name)[0];
|
|
259
|
-
if (node) {
|
|
260
|
-
const values = node.getElementsByTagName('rdf:li');
|
|
261
|
-
if (values.length === 3) {
|
|
262
|
-
returnValue = Array.from(values).map(v => v.innerHTML);
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
throw new Error(`Gainmap metadata contains an array of items for ${name} but its length is not 3`);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
if (defaultValue)
|
|
270
|
-
return defaultValue;
|
|
271
|
-
else
|
|
272
|
-
throw new Error(`Can't find ${name} in gainmap metadata`);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
else {
|
|
276
|
-
returnValue = parsedValue;
|
|
277
|
-
}
|
|
278
|
-
return returnValue;
|
|
279
|
-
};
|
|
280
|
-
/**
|
|
281
|
-
*
|
|
282
|
-
* @param input
|
|
283
|
-
* @returns
|
|
284
|
-
*/
|
|
285
|
-
const extractXMP = (input) => {
|
|
286
|
-
var _a, _b;
|
|
287
|
-
let str;
|
|
288
|
-
// support node test environment
|
|
289
|
-
if (typeof TextDecoder !== 'undefined')
|
|
290
|
-
str = new TextDecoder().decode(input);
|
|
291
|
-
else
|
|
292
|
-
str = input.toString();
|
|
293
|
-
let start = str.indexOf('<x:xmpmeta');
|
|
294
|
-
const parser = new DOMParser();
|
|
295
|
-
while (start !== -1) {
|
|
296
|
-
const end = str.indexOf('x:xmpmeta>', start);
|
|
297
|
-
str.slice(start, end + 10);
|
|
298
|
-
const xmpBlock = str.slice(start, end + 10);
|
|
299
|
-
try {
|
|
300
|
-
const xmlDocument = parser.parseFromString(xmpBlock, 'text/xml');
|
|
301
|
-
const description = xmlDocument.getElementsByTagName('rdf:Description')[0];
|
|
302
|
-
const gainMapMin = getAttribute(description, 'hdrgm:GainMapMin', '0');
|
|
303
|
-
const gainMapMax = getAttribute(description, 'hdrgm:GainMapMax');
|
|
304
|
-
const gamma = getAttribute(description, 'hdrgm:Gamma', '1');
|
|
305
|
-
const offsetSDR = getAttribute(description, 'hdrgm:OffsetSDR', '0.015625');
|
|
306
|
-
const offsetHDR = getAttribute(description, 'hdrgm:OffsetHDR', '0.015625');
|
|
307
|
-
let hdrCapacityMin = (_a = description.attributes.getNamedItem('hdrgm:HDRCapacityMin')) === null || _a === void 0 ? void 0 : _a.nodeValue;
|
|
308
|
-
if (!hdrCapacityMin)
|
|
309
|
-
hdrCapacityMin = '0';
|
|
310
|
-
const hdrCapacityMax = (_b = description.attributes.getNamedItem('hdrgm:HDRCapacityMax')) === null || _b === void 0 ? void 0 : _b.nodeValue;
|
|
311
|
-
if (!hdrCapacityMax)
|
|
312
|
-
throw new Error('Incomplete gainmap metadata');
|
|
313
|
-
return {
|
|
314
|
-
gainMapMin: Array.isArray(gainMapMin) ? gainMapMin.map(v => parseFloat(v)) : [parseFloat(gainMapMin), parseFloat(gainMapMin), parseFloat(gainMapMin)],
|
|
315
|
-
gainMapMax: Array.isArray(gainMapMax) ? gainMapMax.map(v => parseFloat(v)) : [parseFloat(gainMapMax), parseFloat(gainMapMax), parseFloat(gainMapMax)],
|
|
316
|
-
gamma: Array.isArray(gamma) ? gamma.map(v => parseFloat(v)) : [parseFloat(gamma), parseFloat(gamma), parseFloat(gamma)],
|
|
317
|
-
offsetSdr: Array.isArray(offsetSDR) ? offsetSDR.map(v => parseFloat(v)) : [parseFloat(offsetSDR), parseFloat(offsetSDR), parseFloat(offsetSDR)],
|
|
318
|
-
offsetHdr: Array.isArray(offsetHDR) ? offsetHDR.map(v => parseFloat(v)) : [parseFloat(offsetHDR), parseFloat(offsetHDR), parseFloat(offsetHDR)],
|
|
319
|
-
hdrCapacityMin: parseFloat(hdrCapacityMin),
|
|
320
|
-
hdrCapacityMax: parseFloat(hdrCapacityMax)
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
catch (e) {
|
|
324
|
-
}
|
|
325
|
-
start = str.indexOf('<x:xmpmeta', end);
|
|
326
|
-
}
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* MPF Extractor (Multi Picture Format Extractor)
|
|
331
|
-
* By Henrik S Nilsson 2019
|
|
332
|
-
*
|
|
333
|
-
* Extracts images stored in images based on the MPF format (found here: https://www.cipa.jp/e/std/std-sec.html
|
|
334
|
-
* under "CIPA DC-007-Translation-2021 Multi-Picture Format"
|
|
335
|
-
*
|
|
336
|
-
* Overly commented, and without intention of being complete or production ready.
|
|
337
|
-
* Created to extract depth maps from iPhone images, and to learn about image metadata.
|
|
338
|
-
* Kudos to: Phil Harvey (exiftool), Jaume Sanchez (android-lens-blur-depth-extractor)
|
|
339
|
-
*/
|
|
340
|
-
class MPFExtractor {
|
|
341
|
-
constructor(options) {
|
|
342
|
-
this.options = {
|
|
343
|
-
debug: options && options.debug !== undefined ? options.debug : false,
|
|
344
|
-
extractFII: options && options.extractFII !== undefined ? options.extractFII : true,
|
|
345
|
-
extractNonFII: options && options.extractNonFII !== undefined ? options.extractNonFII : true
|
|
346
|
-
};
|
|
347
|
-
}
|
|
348
|
-
extract(imageArrayBuffer) {
|
|
349
|
-
return new Promise((resolve, reject) => {
|
|
350
|
-
const debug = this.options.debug;
|
|
351
|
-
const dataView = new DataView(imageArrayBuffer.buffer);
|
|
352
|
-
// If you're executing this line on a big endian machine, it'll be reversed.
|
|
353
|
-
// bigEnd further down though, refers to the endianness of the image itself.
|
|
354
|
-
if (dataView.getUint16(0) !== 0xffd8) {
|
|
355
|
-
reject(new Error('Not a valid jpeg'));
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
358
|
-
const length = dataView.byteLength;
|
|
359
|
-
let offset = 2;
|
|
360
|
-
let loops = 0;
|
|
361
|
-
let marker; // APP# marker
|
|
362
|
-
while (offset < length) {
|
|
363
|
-
if (++loops > 250) {
|
|
364
|
-
reject(new Error(`Found no marker after ${loops} loops 😵`));
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
if (dataView.getUint8(offset) !== 0xff) {
|
|
368
|
-
reject(new Error(`Not a valid marker at offset 0x${offset.toString(16)}, found: 0x${dataView.getUint8(offset).toString(16)}`));
|
|
369
|
-
return;
|
|
370
|
-
}
|
|
371
|
-
marker = dataView.getUint8(offset + 1);
|
|
372
|
-
if (debug)
|
|
373
|
-
console.log(`Marker: ${marker.toString(16)}`);
|
|
374
|
-
if (marker === 0xe2) {
|
|
375
|
-
if (debug)
|
|
376
|
-
console.log('Found APP2 marker (0xffe2)');
|
|
377
|
-
// Works for iPhone 8 Plus, X, and XSMax. Or any photos of MPF format.
|
|
378
|
-
// Great way to visualize image information in html is using Exiftool. E.g.:
|
|
379
|
-
// ./exiftool.exe -htmldump -wantTrailer photo.jpg > photo.html
|
|
380
|
-
const formatPt = offset + 4;
|
|
381
|
-
/*
|
|
382
|
-
* Structure of the MP Format Identifier
|
|
383
|
-
*
|
|
384
|
-
* Offset Addr. | Code (Hex) | Description
|
|
385
|
-
* +00 ff Marker Prefix <-- offset
|
|
386
|
-
* +01 e2 APP2
|
|
387
|
-
* +02 #n APP2 Field Length
|
|
388
|
-
* +03 #n APP2 Field Length
|
|
389
|
-
* +04 4d 'M' <-- formatPt
|
|
390
|
-
* +05 50 'P'
|
|
391
|
-
* +06 46 'F'
|
|
392
|
-
* +07 00 NULL
|
|
393
|
-
* <-- tiffOffset
|
|
394
|
-
*/
|
|
395
|
-
if (dataView.getUint32(formatPt) === 0x4d504600) {
|
|
396
|
-
// Found MPF tag, so we start dig out sub images
|
|
397
|
-
const tiffOffset = formatPt + 4;
|
|
398
|
-
let bigEnd; // Endianness from TIFF header
|
|
399
|
-
// Test for TIFF validity and endianness
|
|
400
|
-
// 0x4949 and 0x4D4D ('II' and 'MM') marks Little Endian and Big Endian
|
|
401
|
-
if (dataView.getUint16(tiffOffset) === 0x4949) {
|
|
402
|
-
bigEnd = false;
|
|
403
|
-
}
|
|
404
|
-
else if (dataView.getUint16(tiffOffset) === 0x4d4d) {
|
|
405
|
-
bigEnd = true;
|
|
406
|
-
}
|
|
407
|
-
else {
|
|
408
|
-
reject(new Error('No valid endianness marker found in TIFF header'));
|
|
409
|
-
return;
|
|
410
|
-
}
|
|
411
|
-
if (dataView.getUint16(tiffOffset + 2, !bigEnd) !== 0x002a) {
|
|
412
|
-
reject(new Error('Not valid TIFF data! (no 0x002A marker)'));
|
|
413
|
-
return;
|
|
414
|
-
}
|
|
415
|
-
// 32 bit number stating the offset from the start of the 8 Byte MP Header
|
|
416
|
-
// to MP Index IFD Least possible value is thus 8 (means 0 offset)
|
|
417
|
-
const firstIFDOffset = dataView.getUint32(tiffOffset + 4, !bigEnd);
|
|
418
|
-
if (firstIFDOffset < 0x00000008) {
|
|
419
|
-
reject(new Error('Not valid TIFF data! (First offset less than 8)'));
|
|
420
|
-
return;
|
|
421
|
-
}
|
|
422
|
-
// Move ahead to MP Index IFD
|
|
423
|
-
// Assume we're at the first IFD, so firstIFDOffset points to
|
|
424
|
-
// MP Index IFD and not MP Attributes IFD. (If we try extract from a sub image,
|
|
425
|
-
// we fail silently here due to this assumption)
|
|
426
|
-
// Count (2 Byte) | MP Index Fields a.k.a. MP Entries (count * 12 Byte) | Offset of Next IFD (4 Byte)
|
|
427
|
-
const dirStart = tiffOffset + firstIFDOffset; // Start of IFD (Image File Directory)
|
|
428
|
-
const count = dataView.getUint16(dirStart, !bigEnd); // Count of MPEntries (2 Byte)
|
|
429
|
-
// Extract info from MPEntries (starting after Count)
|
|
430
|
-
const entriesStart = dirStart + 2;
|
|
431
|
-
let numberOfImages = 0;
|
|
432
|
-
for (let i = entriesStart; i < entriesStart + 12 * count; i += 12) {
|
|
433
|
-
// Each entry is 12 Bytes long
|
|
434
|
-
// Check MP Index IFD tags, here we only take tag 0xb001 = Number of images
|
|
435
|
-
if (dataView.getUint16(i, !bigEnd) === 0xb001) {
|
|
436
|
-
// stored in Last 4 bytes of its 12 Byte entry.
|
|
437
|
-
numberOfImages = dataView.getUint32(i + 8, !bigEnd);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
const nextIFDOffsetLen = 4; // 4 Byte offset field that appears after MP Index IFD tags
|
|
441
|
-
const MPImageListValPt = dirStart + 2 + count * 12 + nextIFDOffsetLen;
|
|
442
|
-
const images = [];
|
|
443
|
-
for (let i = MPImageListValPt; i < MPImageListValPt + numberOfImages * 16; i += 16) {
|
|
444
|
-
const image = {
|
|
445
|
-
MPType: dataView.getUint32(i, !bigEnd),
|
|
446
|
-
size: dataView.getUint32(i + 4, !bigEnd),
|
|
447
|
-
// This offset is specified relative to the address of the MP Endian
|
|
448
|
-
// field in the MP Header, unless the image is a First Individual Image,
|
|
449
|
-
// in which case the value of the offset shall be NULL (0x00000000).
|
|
450
|
-
dataOffset: dataView.getUint32(i + 8, !bigEnd),
|
|
451
|
-
dependantImages: dataView.getUint32(i + 12, !bigEnd),
|
|
452
|
-
start: -1,
|
|
453
|
-
end: -1,
|
|
454
|
-
isFII: false
|
|
455
|
-
};
|
|
456
|
-
if (!image.dataOffset) {
|
|
457
|
-
// dataOffset is 0x00000000 for First Individual Image
|
|
458
|
-
image.start = 0;
|
|
459
|
-
image.isFII = true;
|
|
460
|
-
}
|
|
461
|
-
else {
|
|
462
|
-
image.start = tiffOffset + image.dataOffset;
|
|
463
|
-
image.isFII = false;
|
|
464
|
-
}
|
|
465
|
-
image.end = image.start + image.size;
|
|
466
|
-
images.push(image);
|
|
467
|
-
}
|
|
468
|
-
if (this.options.extractNonFII && images.length) {
|
|
469
|
-
const bufferBlob = new Blob([dataView]);
|
|
470
|
-
const imgs = [];
|
|
471
|
-
for (const image of images) {
|
|
472
|
-
if (image.isFII && !this.options.extractFII) {
|
|
473
|
-
continue; // Skip FII
|
|
474
|
-
}
|
|
475
|
-
const imageBlob = bufferBlob.slice(image.start, image.end + 1, 'image/jpeg');
|
|
476
|
-
// we don't need this
|
|
477
|
-
// const imageUrl = URL.createObjectURL(imageBlob)
|
|
478
|
-
// image.img = document.createElement('img')
|
|
479
|
-
// image.img.src = imageUrl
|
|
480
|
-
imgs.push(imageBlob);
|
|
481
|
-
}
|
|
482
|
-
resolve(imgs);
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
offset += 2 + dataView.getUint16(offset + 2);
|
|
487
|
-
}
|
|
488
|
-
});
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
/**
|
|
493
|
-
* Extracts XMP Metadata and the gain map recovery image
|
|
494
|
-
* from a single JPEG file.
|
|
495
|
-
*
|
|
496
|
-
* @category Decoding Functions
|
|
497
|
-
* @group Decoding Functions
|
|
498
|
-
* @param jpegFile an `Uint8Array` containing and encoded JPEG file
|
|
499
|
-
* @returns an sdr `Uint8Array` compressed in JPEG, a gainMap `Uint8Array` compressed in JPEG and the XMP parsed XMP metadata
|
|
500
|
-
* @throws Error if XMP Metadata is not found
|
|
501
|
-
* @throws Error if Gain map image is not found
|
|
502
|
-
* @example
|
|
503
|
-
* import { FileLoader } from 'three'
|
|
504
|
-
* import { extractGainmapFromJPEG } from '@monogrid/gainmap-js'
|
|
505
|
-
*
|
|
506
|
-
* const jpegFile = await new FileLoader()
|
|
507
|
-
* .setResponseType('arraybuffer')
|
|
508
|
-
* .loadAsync('image.jpg')
|
|
509
|
-
*
|
|
510
|
-
* const { sdr, gainMap, metadata } = extractGainmapFromJPEG(jpegFile)
|
|
511
|
-
*/
|
|
512
|
-
const extractGainmapFromJPEG = async (jpegFile) => {
|
|
513
|
-
const metadata = extractXMP(jpegFile);
|
|
514
|
-
if (!metadata)
|
|
515
|
-
throw new XMPMetadataNotFoundError('Gain map XMP metadata not found');
|
|
516
|
-
const mpfExtractor = new MPFExtractor({ extractFII: true, extractNonFII: true });
|
|
517
|
-
const images = await mpfExtractor.extract(jpegFile);
|
|
518
|
-
if (images.length !== 2)
|
|
519
|
-
throw new GainMapNotFoundError('Gain map recovery image not found');
|
|
520
|
-
return {
|
|
521
|
-
sdr: new Uint8Array(await images[0].arrayBuffer()),
|
|
522
|
-
gainMap: new Uint8Array(await images[1].arrayBuffer()),
|
|
523
|
-
metadata
|
|
524
|
-
};
|
|
525
|
-
};
|
|
526
|
-
|
|
527
236
|
/**
|
|
528
|
-
*
|
|
529
|
-
*
|
|
530
|
-
* @param blob
|
|
531
|
-
* @returns
|
|
237
|
+
* Base class for WebGL loaders
|
|
238
|
+
* @template TUrl - The type of URL used to load resources
|
|
532
239
|
*/
|
|
533
|
-
|
|
534
|
-
return new Promise((resolve, reject) => {
|
|
535
|
-
const img = document.createElement('img');
|
|
536
|
-
img.onload = () => { resolve(img); };
|
|
537
|
-
img.onerror = (e) => { reject(e); };
|
|
538
|
-
img.src = URL.createObjectURL(blob);
|
|
539
|
-
});
|
|
540
|
-
};
|
|
541
|
-
|
|
542
|
-
class LoaderBase extends Loader {
|
|
543
|
-
/**
|
|
544
|
-
*
|
|
545
|
-
* @param renderer
|
|
546
|
-
* @param manager
|
|
547
|
-
*/
|
|
240
|
+
class LoaderBaseWebGL extends LoaderBaseShared {
|
|
548
241
|
constructor(renderer, manager) {
|
|
549
|
-
super(
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
/**
|
|
555
|
-
* Specify the renderer to use when rendering the gain map
|
|
556
|
-
*
|
|
557
|
-
* @param renderer
|
|
558
|
-
* @returns
|
|
559
|
-
*/
|
|
560
|
-
setRenderer(renderer) {
|
|
561
|
-
this._renderer = renderer;
|
|
562
|
-
return this;
|
|
563
|
-
}
|
|
564
|
-
/**
|
|
565
|
-
* Specify the renderTarget options to use when rendering the gain map
|
|
566
|
-
*
|
|
567
|
-
* @param options
|
|
568
|
-
* @returns
|
|
569
|
-
*/
|
|
570
|
-
setRenderTargetOptions(options) {
|
|
571
|
-
this._renderTargetOptions = options;
|
|
572
|
-
return this;
|
|
242
|
+
super({
|
|
243
|
+
renderer,
|
|
244
|
+
createMaterial: (params) => new GainMapDecoderMaterial(params),
|
|
245
|
+
createQuadRenderer: (params) => new QuadRenderer(params)
|
|
246
|
+
}, manager);
|
|
573
247
|
}
|
|
574
248
|
/**
|
|
575
249
|
* @private
|
|
576
|
-
* @
|
|
250
|
+
* @param quadRenderer
|
|
251
|
+
* @param metadata
|
|
252
|
+
* @param sdrBuffer
|
|
253
|
+
* @param gainMapBuffer
|
|
577
254
|
*/
|
|
578
|
-
prepareQuadRenderer() {
|
|
579
|
-
if (!this._renderer)
|
|
580
|
-
console.warn('WARNING: An existing WebGL Renderer was not passed to this Loader constructor or in setRenderer, the result of this Loader will need to be converted to a Data Texture with toDataTexture() before you can use it in your renderer.');
|
|
581
|
-
// temporary values
|
|
582
|
-
const material = new GainMapDecoderMaterial({
|
|
583
|
-
gainMapMax: [1, 1, 1],
|
|
584
|
-
gainMapMin: [0, 0, 0],
|
|
585
|
-
gamma: [1, 1, 1],
|
|
586
|
-
offsetHdr: [1, 1, 1],
|
|
587
|
-
offsetSdr: [1, 1, 1],
|
|
588
|
-
hdrCapacityMax: 1,
|
|
589
|
-
hdrCapacityMin: 0,
|
|
590
|
-
maxDisplayBoost: 1,
|
|
591
|
-
gainMap: new Texture(),
|
|
592
|
-
sdr: new Texture()
|
|
593
|
-
});
|
|
594
|
-
return new QuadRenderer({
|
|
595
|
-
width: 16,
|
|
596
|
-
height: 16,
|
|
597
|
-
type: HalfFloatType,
|
|
598
|
-
colorSpace: LinearSRGBColorSpace,
|
|
599
|
-
material,
|
|
600
|
-
renderer: this._renderer,
|
|
601
|
-
renderTargetOptions: this._renderTargetOptions
|
|
602
|
-
});
|
|
603
|
-
}
|
|
604
|
-
/**
|
|
605
|
-
* @private
|
|
606
|
-
* @param quadRenderer
|
|
607
|
-
* @param metadata
|
|
608
|
-
* @param sdrBuffer
|
|
609
|
-
* @param gainMapBuffer
|
|
610
|
-
*/
|
|
611
255
|
async render(quadRenderer, metadata, sdrBuffer, gainMapBuffer) {
|
|
612
|
-
|
|
613
|
-
const
|
|
614
|
-
|
|
615
|
-
let sdrImage;
|
|
616
|
-
let gainMapImage;
|
|
617
|
-
let needsFlip = false;
|
|
618
|
-
if (typeof createImageBitmap === 'undefined') {
|
|
619
|
-
const res = await Promise.all([
|
|
620
|
-
gainMapBlob ? getHTMLImageFromBlob(gainMapBlob) : Promise.resolve(undefined),
|
|
621
|
-
getHTMLImageFromBlob(sdrBlob)
|
|
622
|
-
]);
|
|
623
|
-
gainMapImage = res[0];
|
|
624
|
-
sdrImage = res[1];
|
|
625
|
-
needsFlip = true;
|
|
626
|
-
}
|
|
627
|
-
else {
|
|
628
|
-
const res = await Promise.all([
|
|
629
|
-
gainMapBlob ? createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }) : Promise.resolve(undefined),
|
|
630
|
-
createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
|
|
631
|
-
]);
|
|
632
|
-
gainMapImage = res[0];
|
|
633
|
-
sdrImage = res[1];
|
|
634
|
-
}
|
|
635
|
-
const gainMap = new Texture(gainMapImage || new ImageData(2, 2), UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, LinearMipMapLinearFilter, RGBAFormat, UnsignedByteType, 1, LinearSRGBColorSpace);
|
|
636
|
-
gainMap.flipY = needsFlip;
|
|
637
|
-
gainMap.needsUpdate = true;
|
|
638
|
-
const sdr = new Texture(sdrImage, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, LinearMipMapLinearFilter, RGBAFormat, UnsignedByteType, 1, SRGBColorSpace);
|
|
639
|
-
sdr.flipY = needsFlip;
|
|
640
|
-
sdr.needsUpdate = true;
|
|
641
|
-
quadRenderer.width = sdrImage.width;
|
|
642
|
-
quadRenderer.height = sdrImage.height;
|
|
643
|
-
quadRenderer.material.gainMap = gainMap;
|
|
644
|
-
quadRenderer.material.sdr = sdr;
|
|
645
|
-
quadRenderer.material.gainMapMin = metadata.gainMapMin;
|
|
646
|
-
quadRenderer.material.gainMapMax = metadata.gainMapMax;
|
|
647
|
-
quadRenderer.material.offsetHdr = metadata.offsetHdr;
|
|
648
|
-
quadRenderer.material.offsetSdr = metadata.offsetSdr;
|
|
649
|
-
quadRenderer.material.gamma = metadata.gamma;
|
|
650
|
-
quadRenderer.material.hdrCapacityMin = metadata.hdrCapacityMin;
|
|
651
|
-
quadRenderer.material.hdrCapacityMax = metadata.hdrCapacityMax;
|
|
652
|
-
quadRenderer.material.maxDisplayBoost = Math.pow(2, metadata.hdrCapacityMax);
|
|
653
|
-
quadRenderer.material.needsUpdate = true;
|
|
256
|
+
const { sdrImage, gainMapImage, needsFlip } = await this.processImages(sdrBuffer, gainMapBuffer, 'flipY');
|
|
257
|
+
const { gainMap, sdr } = this.createTextures(sdrImage, gainMapImage, needsFlip);
|
|
258
|
+
this.updateQuadRenderer(quadRenderer, sdrImage, gainMap, sdr, metadata);
|
|
654
259
|
quadRenderer.render();
|
|
655
260
|
}
|
|
656
261
|
}
|
|
@@ -665,7 +270,6 @@ class LoaderBase extends Loader {
|
|
|
665
270
|
* import { GainMapLoader } from '@monogrid/gainmap-js'
|
|
666
271
|
* import {
|
|
667
272
|
* EquirectangularReflectionMapping,
|
|
668
|
-
* LinearFilter,
|
|
669
273
|
* Mesh,
|
|
670
274
|
* MeshBasicMaterial,
|
|
671
275
|
* PerspectiveCamera,
|
|
@@ -677,6 +281,7 @@ class LoaderBase extends Loader {
|
|
|
677
281
|
* const renderer = new WebGLRenderer()
|
|
678
282
|
*
|
|
679
283
|
* const loader = new GainMapLoader(renderer)
|
|
284
|
+
* .setRenderTargetOptions({ mapping: EquirectangularReflectionMapping })
|
|
680
285
|
*
|
|
681
286
|
* const result = await loader.loadAsync(['sdr.jpeg', 'gainmap.jpeg', 'metadata.json'])
|
|
682
287
|
* // `result` can be used to populate a Texture
|
|
@@ -696,14 +301,13 @@ class LoaderBase extends Loader {
|
|
|
696
301
|
* // it was previously needed to convert it
|
|
697
302
|
* // to a DataTexture with `result.toDataTexture()`
|
|
698
303
|
* scene.background = result.renderTarget.texture
|
|
699
|
-
* scene.background.mapping = EquirectangularReflectionMapping
|
|
700
304
|
*
|
|
701
305
|
* // result must be manually disposed
|
|
702
306
|
* // when you are done using it
|
|
703
307
|
* result.dispose()
|
|
704
308
|
*
|
|
705
309
|
*/
|
|
706
|
-
class GainMapLoader extends
|
|
310
|
+
class GainMapLoader extends LoaderBaseWebGL {
|
|
707
311
|
/**
|
|
708
312
|
* Loads a gainmap using separate data
|
|
709
313
|
* * sdr image
|
|
@@ -714,7 +318,7 @@ class GainMapLoader extends LoaderBase {
|
|
|
714
318
|
*
|
|
715
319
|
* @param urls An array in the form of [sdr.jpg, gainmap.jpg, metadata.json]
|
|
716
320
|
* @param onLoad Load complete callback, will receive the result
|
|
717
|
-
* @param onProgress Progress callback, will receive a
|
|
321
|
+
* @param onProgress Progress callback, will receive a `ProgressEvent`
|
|
718
322
|
* @param onError Error callback
|
|
719
323
|
* @returns
|
|
720
324
|
*/
|
|
@@ -851,7 +455,6 @@ class GainMapLoader extends LoaderBase {
|
|
|
851
455
|
* import { HDRJPGLoader } from '@monogrid/gainmap-js'
|
|
852
456
|
* import {
|
|
853
457
|
* EquirectangularReflectionMapping,
|
|
854
|
-
* LinearFilter,
|
|
855
458
|
* Mesh,
|
|
856
459
|
* MeshBasicMaterial,
|
|
857
460
|
* PerspectiveCamera,
|
|
@@ -863,6 +466,7 @@ class GainMapLoader extends LoaderBase {
|
|
|
863
466
|
* const renderer = new WebGLRenderer()
|
|
864
467
|
*
|
|
865
468
|
* const loader = new HDRJPGLoader(renderer)
|
|
469
|
+
* .setRenderTargetOptions({ mapping: EquirectangularReflectionMapping })
|
|
866
470
|
*
|
|
867
471
|
* const result = await loader.loadAsync('gainmap.jpeg')
|
|
868
472
|
* // `result` can be used to populate a Texture
|
|
@@ -882,21 +486,20 @@ class GainMapLoader extends LoaderBase {
|
|
|
882
486
|
* // it was previously needed to convert it
|
|
883
487
|
* // to a DataTexture with `result.toDataTexture()`
|
|
884
488
|
* scene.background = result.renderTarget.texture
|
|
885
|
-
* scene.background.mapping = EquirectangularReflectionMapping
|
|
886
489
|
*
|
|
887
490
|
* // result must be manually disposed
|
|
888
491
|
* // when you are done using it
|
|
889
492
|
* result.dispose()
|
|
890
493
|
*
|
|
891
494
|
*/
|
|
892
|
-
class HDRJPGLoader extends
|
|
495
|
+
class HDRJPGLoader extends LoaderBaseWebGL {
|
|
893
496
|
/**
|
|
894
497
|
* Loads a JPEG containing gain map metadata
|
|
895
498
|
* Renders a normal SDR image if gainmap data is not found
|
|
896
499
|
*
|
|
897
|
-
* @param url
|
|
500
|
+
* @param url Path to a JPEG file containing embedded gain map metadata
|
|
898
501
|
* @param onLoad Load complete callback, will receive the result
|
|
899
|
-
* @param onProgress Progress callback, will receive a
|
|
502
|
+
* @param onProgress Progress callback, will receive a `ProgressEvent`
|
|
900
503
|
* @param onError Error callback
|
|
901
504
|
* @returns
|
|
902
505
|
*/
|
|
@@ -946,7 +549,7 @@ class HDRJPGLoader extends LoaderBase {
|
|
|
946
549
|
}
|
|
947
550
|
// solves #16
|
|
948
551
|
try {
|
|
949
|
-
await this.render(quadRenderer, metadata, sdrJPEG, gainMapJPEG);
|
|
552
|
+
await this.render(quadRenderer, metadata, sdrJPEG.buffer, gainMapJPEG?.buffer);
|
|
950
553
|
}
|
|
951
554
|
catch (error) {
|
|
952
555
|
this.manager.itemError(url);
|
|
@@ -968,4 +571,4 @@ class HDRJPGLoader extends LoaderBase {
|
|
|
968
571
|
}
|
|
969
572
|
}
|
|
970
573
|
|
|
971
|
-
export { GainMapDecoderMaterial, GainMapLoader, HDRJPGLoader, HDRJPGLoader as JPEGRLoader,
|
|
574
|
+
export { GainMapDecoderMaterial, GainMapLoader, GainMapNotFoundError, HDRJPGLoader, HDRJPGLoader as JPEGRLoader, LoaderBaseShared, QuadRenderer, XMPMetadataNotFoundError, createDecodeFunction, decode, extractGainmapFromJPEG };
|