@monogrid/gainmap-js 3.1.0 → 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 +7 -0
- package/CODE_OF_CONDUCT.md +1 -1
- package/CONTRIBUTING.md +1 -1
- package/README.md +105 -2
- package/dist/Loader-BZcKUVaX.js +424 -0
- package/dist/{QuadRenderer-DuOPRGA4.js → QuadRenderer-Y5tm-R3m.js} +39 -30
- package/dist/{_commonjsHelpers-DeBGiDfT.js → _commonjsHelpers-CuGYGi_5.js} +2 -2
- package/dist/{compress-B6OswAu8.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/{utils → shared/utils}/extractXMP.d.ts +1 -1
- 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 -426
- package/dist/decode.umd.cjs +360 -314
- 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/encode-jpeg-metadata.d.ts +2 -2
- package/dist/libultrahdr.js +11 -23
- package/dist/libultrahdr.umd.cjs +11 -23
- 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/{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.
|
|
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,397 +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 getXMLValue = (xml, tag, defaultValue) => {
|
|
254
|
-
// Check for attribute format first: tag="value"
|
|
255
|
-
const attributeMatch = new RegExp(`${tag}="([^"]*)"`, 'i').exec(xml);
|
|
256
|
-
if (attributeMatch)
|
|
257
|
-
return attributeMatch[1];
|
|
258
|
-
// Check for tag format: <tag>value</tag> or <tag><rdf:li>value</rdf:li>...</tag>
|
|
259
|
-
const tagMatch = new RegExp(`<${tag}[^>]*>([\\s\\S]*?)</${tag}>`, 'i').exec(xml);
|
|
260
|
-
if (tagMatch) {
|
|
261
|
-
// Check if it contains rdf:li elements
|
|
262
|
-
const liValues = tagMatch[1].match(/<rdf:li>([^<]*)<\/rdf:li>/g);
|
|
263
|
-
if (liValues && liValues.length === 3) {
|
|
264
|
-
return liValues.map(v => v.replace(/<\/?rdf:li>/g, ''));
|
|
265
|
-
}
|
|
266
|
-
return tagMatch[1].trim();
|
|
267
|
-
}
|
|
268
|
-
if (defaultValue !== undefined)
|
|
269
|
-
return defaultValue;
|
|
270
|
-
throw new Error(`Can't find ${tag} in gainmap metadata`);
|
|
271
|
-
};
|
|
272
|
-
const extractXMP = (input) => {
|
|
273
|
-
let str;
|
|
274
|
-
// support node test environment
|
|
275
|
-
if (typeof TextDecoder !== 'undefined')
|
|
276
|
-
str = new TextDecoder().decode(input);
|
|
277
|
-
else
|
|
278
|
-
str = input.toString();
|
|
279
|
-
let start = str.indexOf('<x:xmpmeta');
|
|
280
|
-
while (start !== -1) {
|
|
281
|
-
const end = str.indexOf('x:xmpmeta>', start);
|
|
282
|
-
const xmpBlock = str.slice(start, end + 10);
|
|
283
|
-
try {
|
|
284
|
-
const gainMapMin = getXMLValue(xmpBlock, 'hdrgm:GainMapMin', '0');
|
|
285
|
-
const gainMapMax = getXMLValue(xmpBlock, 'hdrgm:GainMapMax');
|
|
286
|
-
const gamma = getXMLValue(xmpBlock, 'hdrgm:Gamma', '1');
|
|
287
|
-
const offsetSDR = getXMLValue(xmpBlock, 'hdrgm:OffsetSDR', '0.015625');
|
|
288
|
-
const offsetHDR = getXMLValue(xmpBlock, 'hdrgm:OffsetHDR', '0.015625');
|
|
289
|
-
// These are always attributes, so we can use a simpler regex
|
|
290
|
-
const hdrCapacityMinMatch = /hdrgm:HDRCapacityMin="([^"]*)"/.exec(xmpBlock);
|
|
291
|
-
const hdrCapacityMin = hdrCapacityMinMatch ? hdrCapacityMinMatch[1] : '0';
|
|
292
|
-
const hdrCapacityMaxMatch = /hdrgm:HDRCapacityMax="([^"]*)"/.exec(xmpBlock);
|
|
293
|
-
if (!hdrCapacityMaxMatch)
|
|
294
|
-
throw new Error('Incomplete gainmap metadata');
|
|
295
|
-
const hdrCapacityMax = hdrCapacityMaxMatch[1];
|
|
296
|
-
return {
|
|
297
|
-
gainMapMin: Array.isArray(gainMapMin) ? gainMapMin.map(v => parseFloat(v)) : [parseFloat(gainMapMin), parseFloat(gainMapMin), parseFloat(gainMapMin)],
|
|
298
|
-
gainMapMax: Array.isArray(gainMapMax) ? gainMapMax.map(v => parseFloat(v)) : [parseFloat(gainMapMax), parseFloat(gainMapMax), parseFloat(gainMapMax)],
|
|
299
|
-
gamma: Array.isArray(gamma) ? gamma.map(v => parseFloat(v)) : [parseFloat(gamma), parseFloat(gamma), parseFloat(gamma)],
|
|
300
|
-
offsetSdr: Array.isArray(offsetSDR) ? offsetSDR.map(v => parseFloat(v)) : [parseFloat(offsetSDR), parseFloat(offsetSDR), parseFloat(offsetSDR)],
|
|
301
|
-
offsetHdr: Array.isArray(offsetHDR) ? offsetHDR.map(v => parseFloat(v)) : [parseFloat(offsetHDR), parseFloat(offsetHDR), parseFloat(offsetHDR)],
|
|
302
|
-
hdrCapacityMin: parseFloat(hdrCapacityMin),
|
|
303
|
-
hdrCapacityMax: parseFloat(hdrCapacityMax)
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
catch (e) {
|
|
307
|
-
// Continue searching for another xmpmeta block if this one fails
|
|
308
|
-
}
|
|
309
|
-
start = str.indexOf('<x:xmpmeta', end);
|
|
310
|
-
}
|
|
311
|
-
};
|
|
312
|
-
|
|
313
236
|
/**
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
* Extracts images stored in images based on the MPF format (found here: https://www.cipa.jp/e/std/std-sec.html
|
|
318
|
-
* under "CIPA DC-007-Translation-2021 Multi-Picture Format"
|
|
319
|
-
*
|
|
320
|
-
* Overly commented, and without intention of being complete or production ready.
|
|
321
|
-
* Created to extract depth maps from iPhone images, and to learn about image metadata.
|
|
322
|
-
* Kudos to: Phil Harvey (exiftool), Jaume Sanchez (android-lens-blur-depth-extractor)
|
|
237
|
+
* Base class for WebGL loaders
|
|
238
|
+
* @template TUrl - The type of URL used to load resources
|
|
323
239
|
*/
|
|
324
|
-
class
|
|
325
|
-
constructor(options) {
|
|
326
|
-
this.options = {
|
|
327
|
-
debug: options && options.debug !== undefined ? options.debug : false,
|
|
328
|
-
extractFII: options && options.extractFII !== undefined ? options.extractFII : true,
|
|
329
|
-
extractNonFII: options && options.extractNonFII !== undefined ? options.extractNonFII : true
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
extract(imageArrayBuffer) {
|
|
333
|
-
return new Promise((resolve, reject) => {
|
|
334
|
-
const debug = this.options.debug;
|
|
335
|
-
const dataView = new DataView(imageArrayBuffer.buffer);
|
|
336
|
-
// If you're executing this line on a big endian machine, it'll be reversed.
|
|
337
|
-
// bigEnd further down though, refers to the endianness of the image itself.
|
|
338
|
-
if (dataView.getUint16(0) !== 0xffd8) {
|
|
339
|
-
reject(new Error('Not a valid jpeg'));
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
const length = dataView.byteLength;
|
|
343
|
-
let offset = 2;
|
|
344
|
-
let loops = 0;
|
|
345
|
-
let marker; // APP# marker
|
|
346
|
-
while (offset < length) {
|
|
347
|
-
if (++loops > 250) {
|
|
348
|
-
reject(new Error(`Found no marker after ${loops} loops 😵`));
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
if (dataView.getUint8(offset) !== 0xff) {
|
|
352
|
-
reject(new Error(`Not a valid marker at offset 0x${offset.toString(16)}, found: 0x${dataView.getUint8(offset).toString(16)}`));
|
|
353
|
-
return;
|
|
354
|
-
}
|
|
355
|
-
marker = dataView.getUint8(offset + 1);
|
|
356
|
-
if (debug)
|
|
357
|
-
console.log(`Marker: ${marker.toString(16)}`);
|
|
358
|
-
if (marker === 0xe2) {
|
|
359
|
-
if (debug)
|
|
360
|
-
console.log('Found APP2 marker (0xffe2)');
|
|
361
|
-
// Works for iPhone 8 Plus, X, and XSMax. Or any photos of MPF format.
|
|
362
|
-
// Great way to visualize image information in html is using Exiftool. E.g.:
|
|
363
|
-
// ./exiftool.exe -htmldump -wantTrailer photo.jpg > photo.html
|
|
364
|
-
const formatPt = offset + 4;
|
|
365
|
-
/*
|
|
366
|
-
* Structure of the MP Format Identifier
|
|
367
|
-
*
|
|
368
|
-
* Offset Addr. | Code (Hex) | Description
|
|
369
|
-
* +00 ff Marker Prefix <-- offset
|
|
370
|
-
* +01 e2 APP2
|
|
371
|
-
* +02 #n APP2 Field Length
|
|
372
|
-
* +03 #n APP2 Field Length
|
|
373
|
-
* +04 4d 'M' <-- formatPt
|
|
374
|
-
* +05 50 'P'
|
|
375
|
-
* +06 46 'F'
|
|
376
|
-
* +07 00 NULL
|
|
377
|
-
* <-- tiffOffset
|
|
378
|
-
*/
|
|
379
|
-
if (dataView.getUint32(formatPt) === 0x4d504600) {
|
|
380
|
-
// Found MPF tag, so we start dig out sub images
|
|
381
|
-
const tiffOffset = formatPt + 4;
|
|
382
|
-
let bigEnd; // Endianness from TIFF header
|
|
383
|
-
// Test for TIFF validity and endianness
|
|
384
|
-
// 0x4949 and 0x4D4D ('II' and 'MM') marks Little Endian and Big Endian
|
|
385
|
-
if (dataView.getUint16(tiffOffset) === 0x4949) {
|
|
386
|
-
bigEnd = false;
|
|
387
|
-
}
|
|
388
|
-
else if (dataView.getUint16(tiffOffset) === 0x4d4d) {
|
|
389
|
-
bigEnd = true;
|
|
390
|
-
}
|
|
391
|
-
else {
|
|
392
|
-
reject(new Error('No valid endianness marker found in TIFF header'));
|
|
393
|
-
return;
|
|
394
|
-
}
|
|
395
|
-
if (dataView.getUint16(tiffOffset + 2, !bigEnd) !== 0x002a) {
|
|
396
|
-
reject(new Error('Not valid TIFF data! (no 0x002A marker)'));
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
// 32 bit number stating the offset from the start of the 8 Byte MP Header
|
|
400
|
-
// to MP Index IFD Least possible value is thus 8 (means 0 offset)
|
|
401
|
-
const firstIFDOffset = dataView.getUint32(tiffOffset + 4, !bigEnd);
|
|
402
|
-
if (firstIFDOffset < 0x00000008) {
|
|
403
|
-
reject(new Error('Not valid TIFF data! (First offset less than 8)'));
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
// Move ahead to MP Index IFD
|
|
407
|
-
// Assume we're at the first IFD, so firstIFDOffset points to
|
|
408
|
-
// MP Index IFD and not MP Attributes IFD. (If we try extract from a sub image,
|
|
409
|
-
// we fail silently here due to this assumption)
|
|
410
|
-
// Count (2 Byte) | MP Index Fields a.k.a. MP Entries (count * 12 Byte) | Offset of Next IFD (4 Byte)
|
|
411
|
-
const dirStart = tiffOffset + firstIFDOffset; // Start of IFD (Image File Directory)
|
|
412
|
-
const count = dataView.getUint16(dirStart, !bigEnd); // Count of MPEntries (2 Byte)
|
|
413
|
-
// Extract info from MPEntries (starting after Count)
|
|
414
|
-
const entriesStart = dirStart + 2;
|
|
415
|
-
let numberOfImages = 0;
|
|
416
|
-
for (let i = entriesStart; i < entriesStart + 12 * count; i += 12) {
|
|
417
|
-
// Each entry is 12 Bytes long
|
|
418
|
-
// Check MP Index IFD tags, here we only take tag 0xb001 = Number of images
|
|
419
|
-
if (dataView.getUint16(i, !bigEnd) === 0xb001) {
|
|
420
|
-
// stored in Last 4 bytes of its 12 Byte entry.
|
|
421
|
-
numberOfImages = dataView.getUint32(i + 8, !bigEnd);
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
const nextIFDOffsetLen = 4; // 4 Byte offset field that appears after MP Index IFD tags
|
|
425
|
-
const MPImageListValPt = dirStart + 2 + count * 12 + nextIFDOffsetLen;
|
|
426
|
-
const images = [];
|
|
427
|
-
for (let i = MPImageListValPt; i < MPImageListValPt + numberOfImages * 16; i += 16) {
|
|
428
|
-
const image = {
|
|
429
|
-
MPType: dataView.getUint32(i, !bigEnd),
|
|
430
|
-
size: dataView.getUint32(i + 4, !bigEnd),
|
|
431
|
-
// This offset is specified relative to the address of the MP Endian
|
|
432
|
-
// field in the MP Header, unless the image is a First Individual Image,
|
|
433
|
-
// in which case the value of the offset shall be NULL (0x00000000).
|
|
434
|
-
dataOffset: dataView.getUint32(i + 8, !bigEnd),
|
|
435
|
-
dependantImages: dataView.getUint32(i + 12, !bigEnd),
|
|
436
|
-
start: -1,
|
|
437
|
-
end: -1,
|
|
438
|
-
isFII: false
|
|
439
|
-
};
|
|
440
|
-
if (!image.dataOffset) {
|
|
441
|
-
// dataOffset is 0x00000000 for First Individual Image
|
|
442
|
-
image.start = 0;
|
|
443
|
-
image.isFII = true;
|
|
444
|
-
}
|
|
445
|
-
else {
|
|
446
|
-
image.start = tiffOffset + image.dataOffset;
|
|
447
|
-
image.isFII = false;
|
|
448
|
-
}
|
|
449
|
-
image.end = image.start + image.size;
|
|
450
|
-
images.push(image);
|
|
451
|
-
}
|
|
452
|
-
if (this.options.extractNonFII && images.length) {
|
|
453
|
-
const bufferBlob = new Blob([dataView]);
|
|
454
|
-
const imgs = [];
|
|
455
|
-
for (const image of images) {
|
|
456
|
-
if (image.isFII && !this.options.extractFII) {
|
|
457
|
-
continue; // Skip FII
|
|
458
|
-
}
|
|
459
|
-
const imageBlob = bufferBlob.slice(image.start, image.end + 1, 'image/jpeg');
|
|
460
|
-
// we don't need this
|
|
461
|
-
// const imageUrl = URL.createObjectURL(imageBlob)
|
|
462
|
-
// image.img = document.createElement('img')
|
|
463
|
-
// image.img.src = imageUrl
|
|
464
|
-
imgs.push(imageBlob);
|
|
465
|
-
}
|
|
466
|
-
resolve(imgs);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
offset += 2 + dataView.getUint16(offset + 2);
|
|
471
|
-
}
|
|
472
|
-
});
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
/**
|
|
477
|
-
* Extracts XMP Metadata and the gain map recovery image
|
|
478
|
-
* from a single JPEG file.
|
|
479
|
-
*
|
|
480
|
-
* @category Decoding Functions
|
|
481
|
-
* @group Decoding Functions
|
|
482
|
-
* @param jpegFile an `Uint8Array` containing and encoded JPEG file
|
|
483
|
-
* @returns an sdr `Uint8Array` compressed in JPEG, a gainMap `Uint8Array` compressed in JPEG and the XMP parsed XMP metadata
|
|
484
|
-
* @throws Error if XMP Metadata is not found
|
|
485
|
-
* @throws Error if Gain map image is not found
|
|
486
|
-
* @example
|
|
487
|
-
* import { FileLoader } from 'three'
|
|
488
|
-
* import { extractGainmapFromJPEG } from '@monogrid/gainmap-js'
|
|
489
|
-
*
|
|
490
|
-
* const jpegFile = await new FileLoader()
|
|
491
|
-
* .setResponseType('arraybuffer')
|
|
492
|
-
* .loadAsync('image.jpg')
|
|
493
|
-
*
|
|
494
|
-
* const { sdr, gainMap, metadata } = extractGainmapFromJPEG(jpegFile)
|
|
495
|
-
*/
|
|
496
|
-
const extractGainmapFromJPEG = async (jpegFile) => {
|
|
497
|
-
const metadata = extractXMP(jpegFile);
|
|
498
|
-
if (!metadata)
|
|
499
|
-
throw new XMPMetadataNotFoundError('Gain map XMP metadata not found');
|
|
500
|
-
const mpfExtractor = new MPFExtractor({ extractFII: true, extractNonFII: true });
|
|
501
|
-
const images = await mpfExtractor.extract(jpegFile);
|
|
502
|
-
if (images.length !== 2)
|
|
503
|
-
throw new GainMapNotFoundError('Gain map recovery image not found');
|
|
504
|
-
return {
|
|
505
|
-
sdr: new Uint8Array(await images[0].arrayBuffer()),
|
|
506
|
-
gainMap: new Uint8Array(await images[1].arrayBuffer()),
|
|
507
|
-
metadata
|
|
508
|
-
};
|
|
509
|
-
};
|
|
510
|
-
|
|
511
|
-
/**
|
|
512
|
-
* private function, async get image from blob
|
|
513
|
-
*
|
|
514
|
-
* @param blob
|
|
515
|
-
* @returns
|
|
516
|
-
*/
|
|
517
|
-
const getHTMLImageFromBlob = (blob) => {
|
|
518
|
-
return new Promise((resolve, reject) => {
|
|
519
|
-
const img = document.createElement('img');
|
|
520
|
-
img.onload = () => { resolve(img); };
|
|
521
|
-
img.onerror = (e) => { reject(e); };
|
|
522
|
-
img.src = URL.createObjectURL(blob);
|
|
523
|
-
});
|
|
524
|
-
};
|
|
525
|
-
|
|
526
|
-
class LoaderBase extends Loader {
|
|
527
|
-
/**
|
|
528
|
-
*
|
|
529
|
-
* @param renderer
|
|
530
|
-
* @param manager
|
|
531
|
-
*/
|
|
240
|
+
class LoaderBaseWebGL extends LoaderBaseShared {
|
|
532
241
|
constructor(renderer, manager) {
|
|
533
|
-
super(
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
/**
|
|
539
|
-
* Specify the renderer to use when rendering the gain map
|
|
540
|
-
*
|
|
541
|
-
* @param renderer
|
|
542
|
-
* @returns
|
|
543
|
-
*/
|
|
544
|
-
setRenderer(renderer) {
|
|
545
|
-
this._renderer = renderer;
|
|
546
|
-
return this;
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
* Specify the renderTarget options to use when rendering the gain map
|
|
550
|
-
*
|
|
551
|
-
* @param options
|
|
552
|
-
* @returns
|
|
553
|
-
*/
|
|
554
|
-
setRenderTargetOptions(options) {
|
|
555
|
-
this._renderTargetOptions = options;
|
|
556
|
-
return this;
|
|
242
|
+
super({
|
|
243
|
+
renderer,
|
|
244
|
+
createMaterial: (params) => new GainMapDecoderMaterial(params),
|
|
245
|
+
createQuadRenderer: (params) => new QuadRenderer(params)
|
|
246
|
+
}, manager);
|
|
557
247
|
}
|
|
558
248
|
/**
|
|
559
249
|
* @private
|
|
560
|
-
* @
|
|
250
|
+
* @param quadRenderer
|
|
251
|
+
* @param metadata
|
|
252
|
+
* @param sdrBuffer
|
|
253
|
+
* @param gainMapBuffer
|
|
561
254
|
*/
|
|
562
|
-
prepareQuadRenderer() {
|
|
563
|
-
if (!this._renderer)
|
|
564
|
-
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.');
|
|
565
|
-
// temporary values
|
|
566
|
-
const material = new GainMapDecoderMaterial({
|
|
567
|
-
gainMapMax: [1, 1, 1],
|
|
568
|
-
gainMapMin: [0, 0, 0],
|
|
569
|
-
gamma: [1, 1, 1],
|
|
570
|
-
offsetHdr: [1, 1, 1],
|
|
571
|
-
offsetSdr: [1, 1, 1],
|
|
572
|
-
hdrCapacityMax: 1,
|
|
573
|
-
hdrCapacityMin: 0,
|
|
574
|
-
maxDisplayBoost: 1,
|
|
575
|
-
gainMap: new Texture(),
|
|
576
|
-
sdr: new Texture()
|
|
577
|
-
});
|
|
578
|
-
return new QuadRenderer({
|
|
579
|
-
width: 16,
|
|
580
|
-
height: 16,
|
|
581
|
-
type: HalfFloatType,
|
|
582
|
-
colorSpace: LinearSRGBColorSpace,
|
|
583
|
-
material,
|
|
584
|
-
renderer: this._renderer,
|
|
585
|
-
renderTargetOptions: this._renderTargetOptions
|
|
586
|
-
});
|
|
587
|
-
}
|
|
588
|
-
/**
|
|
589
|
-
* @private
|
|
590
|
-
* @param quadRenderer
|
|
591
|
-
* @param metadata
|
|
592
|
-
* @param sdrBuffer
|
|
593
|
-
* @param gainMapBuffer
|
|
594
|
-
*/
|
|
595
255
|
async render(quadRenderer, metadata, sdrBuffer, gainMapBuffer) {
|
|
596
|
-
|
|
597
|
-
const
|
|
598
|
-
|
|
599
|
-
let sdrImage;
|
|
600
|
-
let gainMapImage;
|
|
601
|
-
let needsFlip = false;
|
|
602
|
-
if (typeof createImageBitmap === 'undefined') {
|
|
603
|
-
const res = await Promise.all([
|
|
604
|
-
gainMapBlob ? getHTMLImageFromBlob(gainMapBlob) : Promise.resolve(undefined),
|
|
605
|
-
getHTMLImageFromBlob(sdrBlob)
|
|
606
|
-
]);
|
|
607
|
-
gainMapImage = res[0];
|
|
608
|
-
sdrImage = res[1];
|
|
609
|
-
needsFlip = true;
|
|
610
|
-
}
|
|
611
|
-
else {
|
|
612
|
-
const res = await Promise.all([
|
|
613
|
-
gainMapBlob ? createImageBitmap(gainMapBlob, { imageOrientation: 'flipY' }) : Promise.resolve(undefined),
|
|
614
|
-
createImageBitmap(sdrBlob, { imageOrientation: 'flipY' })
|
|
615
|
-
]);
|
|
616
|
-
gainMapImage = res[0];
|
|
617
|
-
sdrImage = res[1];
|
|
618
|
-
}
|
|
619
|
-
const gainMap = new Texture(gainMapImage || new ImageData(2, 2), UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, LinearMipMapLinearFilter, RGBAFormat, UnsignedByteType, 1, LinearSRGBColorSpace);
|
|
620
|
-
gainMap.flipY = needsFlip;
|
|
621
|
-
gainMap.needsUpdate = true;
|
|
622
|
-
const sdr = new Texture(sdrImage, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, LinearMipMapLinearFilter, RGBAFormat, UnsignedByteType, 1, SRGBColorSpace);
|
|
623
|
-
sdr.flipY = needsFlip;
|
|
624
|
-
sdr.needsUpdate = true;
|
|
625
|
-
quadRenderer.width = sdrImage.width;
|
|
626
|
-
quadRenderer.height = sdrImage.height;
|
|
627
|
-
quadRenderer.material.gainMap = gainMap;
|
|
628
|
-
quadRenderer.material.sdr = sdr;
|
|
629
|
-
quadRenderer.material.gainMapMin = metadata.gainMapMin;
|
|
630
|
-
quadRenderer.material.gainMapMax = metadata.gainMapMax;
|
|
631
|
-
quadRenderer.material.offsetHdr = metadata.offsetHdr;
|
|
632
|
-
quadRenderer.material.offsetSdr = metadata.offsetSdr;
|
|
633
|
-
quadRenderer.material.gamma = metadata.gamma;
|
|
634
|
-
quadRenderer.material.hdrCapacityMin = metadata.hdrCapacityMin;
|
|
635
|
-
quadRenderer.material.hdrCapacityMax = metadata.hdrCapacityMax;
|
|
636
|
-
quadRenderer.material.maxDisplayBoost = Math.pow(2, metadata.hdrCapacityMax);
|
|
637
|
-
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);
|
|
638
259
|
quadRenderer.render();
|
|
639
260
|
}
|
|
640
261
|
}
|
|
@@ -649,7 +270,6 @@ class LoaderBase extends Loader {
|
|
|
649
270
|
* import { GainMapLoader } from '@monogrid/gainmap-js'
|
|
650
271
|
* import {
|
|
651
272
|
* EquirectangularReflectionMapping,
|
|
652
|
-
* LinearFilter,
|
|
653
273
|
* Mesh,
|
|
654
274
|
* MeshBasicMaterial,
|
|
655
275
|
* PerspectiveCamera,
|
|
@@ -661,6 +281,7 @@ class LoaderBase extends Loader {
|
|
|
661
281
|
* const renderer = new WebGLRenderer()
|
|
662
282
|
*
|
|
663
283
|
* const loader = new GainMapLoader(renderer)
|
|
284
|
+
* .setRenderTargetOptions({ mapping: EquirectangularReflectionMapping })
|
|
664
285
|
*
|
|
665
286
|
* const result = await loader.loadAsync(['sdr.jpeg', 'gainmap.jpeg', 'metadata.json'])
|
|
666
287
|
* // `result` can be used to populate a Texture
|
|
@@ -680,14 +301,13 @@ class LoaderBase extends Loader {
|
|
|
680
301
|
* // it was previously needed to convert it
|
|
681
302
|
* // to a DataTexture with `result.toDataTexture()`
|
|
682
303
|
* scene.background = result.renderTarget.texture
|
|
683
|
-
* scene.background.mapping = EquirectangularReflectionMapping
|
|
684
304
|
*
|
|
685
305
|
* // result must be manually disposed
|
|
686
306
|
* // when you are done using it
|
|
687
307
|
* result.dispose()
|
|
688
308
|
*
|
|
689
309
|
*/
|
|
690
|
-
class GainMapLoader extends
|
|
310
|
+
class GainMapLoader extends LoaderBaseWebGL {
|
|
691
311
|
/**
|
|
692
312
|
* Loads a gainmap using separate data
|
|
693
313
|
* * sdr image
|
|
@@ -698,7 +318,7 @@ class GainMapLoader extends LoaderBase {
|
|
|
698
318
|
*
|
|
699
319
|
* @param urls An array in the form of [sdr.jpg, gainmap.jpg, metadata.json]
|
|
700
320
|
* @param onLoad Load complete callback, will receive the result
|
|
701
|
-
* @param onProgress Progress callback, will receive a
|
|
321
|
+
* @param onProgress Progress callback, will receive a `ProgressEvent`
|
|
702
322
|
* @param onError Error callback
|
|
703
323
|
* @returns
|
|
704
324
|
*/
|
|
@@ -835,7 +455,6 @@ class GainMapLoader extends LoaderBase {
|
|
|
835
455
|
* import { HDRJPGLoader } from '@monogrid/gainmap-js'
|
|
836
456
|
* import {
|
|
837
457
|
* EquirectangularReflectionMapping,
|
|
838
|
-
* LinearFilter,
|
|
839
458
|
* Mesh,
|
|
840
459
|
* MeshBasicMaterial,
|
|
841
460
|
* PerspectiveCamera,
|
|
@@ -847,6 +466,7 @@ class GainMapLoader extends LoaderBase {
|
|
|
847
466
|
* const renderer = new WebGLRenderer()
|
|
848
467
|
*
|
|
849
468
|
* const loader = new HDRJPGLoader(renderer)
|
|
469
|
+
* .setRenderTargetOptions({ mapping: EquirectangularReflectionMapping })
|
|
850
470
|
*
|
|
851
471
|
* const result = await loader.loadAsync('gainmap.jpeg')
|
|
852
472
|
* // `result` can be used to populate a Texture
|
|
@@ -866,21 +486,20 @@ class GainMapLoader extends LoaderBase {
|
|
|
866
486
|
* // it was previously needed to convert it
|
|
867
487
|
* // to a DataTexture with `result.toDataTexture()`
|
|
868
488
|
* scene.background = result.renderTarget.texture
|
|
869
|
-
* scene.background.mapping = EquirectangularReflectionMapping
|
|
870
489
|
*
|
|
871
490
|
* // result must be manually disposed
|
|
872
491
|
* // when you are done using it
|
|
873
492
|
* result.dispose()
|
|
874
493
|
*
|
|
875
494
|
*/
|
|
876
|
-
class HDRJPGLoader extends
|
|
495
|
+
class HDRJPGLoader extends LoaderBaseWebGL {
|
|
877
496
|
/**
|
|
878
497
|
* Loads a JPEG containing gain map metadata
|
|
879
498
|
* Renders a normal SDR image if gainmap data is not found
|
|
880
499
|
*
|
|
881
|
-
* @param url
|
|
500
|
+
* @param url Path to a JPEG file containing embedded gain map metadata
|
|
882
501
|
* @param onLoad Load complete callback, will receive the result
|
|
883
|
-
* @param onProgress Progress callback, will receive a
|
|
502
|
+
* @param onProgress Progress callback, will receive a `ProgressEvent`
|
|
884
503
|
* @param onError Error callback
|
|
885
504
|
* @returns
|
|
886
505
|
*/
|
|
@@ -930,7 +549,7 @@ class HDRJPGLoader extends LoaderBase {
|
|
|
930
549
|
}
|
|
931
550
|
// solves #16
|
|
932
551
|
try {
|
|
933
|
-
await this.render(quadRenderer, metadata, sdrJPEG, gainMapJPEG);
|
|
552
|
+
await this.render(quadRenderer, metadata, sdrJPEG.buffer, gainMapJPEG?.buffer);
|
|
934
553
|
}
|
|
935
554
|
catch (error) {
|
|
936
555
|
this.manager.itemError(url);
|
|
@@ -952,4 +571,4 @@ class HDRJPGLoader extends LoaderBase {
|
|
|
952
571
|
}
|
|
953
572
|
}
|
|
954
573
|
|
|
955
|
-
export { GainMapDecoderMaterial, GainMapLoader, HDRJPGLoader, HDRJPGLoader as JPEGRLoader,
|
|
574
|
+
export { GainMapDecoderMaterial, GainMapLoader, GainMapNotFoundError, HDRJPGLoader, HDRJPGLoader as JPEGRLoader, LoaderBaseShared, QuadRenderer, XMPMetadataNotFoundError, createDecodeFunction, decode, extractGainmapFromJPEG };
|