@bb-studio/exr 0.2.0 → 0.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 +37 -0
- package/README.md +151 -4
- package/THIRD_PARTY_LICENSES.md +2 -2
- package/dist/browser/chunk-RJALO6PD.js +9 -0
- package/dist/browser/chunk-RJALO6PD.js.map +1 -0
- package/dist/browser/dwaDecodeWorker.js +21 -16
- package/dist/browser/dwaDecodeWorker.js.map +1 -1
- package/dist/browser/index.d.ts +65 -8
- package/dist/browser/index.js +854 -488
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/zipDecodeWorker.js +13 -11
- package/dist/browser/zipDecodeWorker.js.map +1 -1
- package/dist/index.cjs +699 -219
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +66 -4
- package/dist/index.d.ts +66 -4
- package/dist/index.js +695 -219
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -159,7 +159,7 @@ function parsePartHeader(reader, index) {
|
|
|
159
159
|
}
|
|
160
160
|
const attributes = {};
|
|
161
161
|
const channels = [];
|
|
162
|
-
|
|
162
|
+
for (; ; ) {
|
|
163
163
|
const name = reader.readCString("attribute name");
|
|
164
164
|
if (name === "") {
|
|
165
165
|
break;
|
|
@@ -465,6 +465,58 @@ function float32ToFloat16(value) {
|
|
|
465
465
|
return sign | exponent << 10 | mantissa + 4096 >> 13;
|
|
466
466
|
}
|
|
467
467
|
|
|
468
|
+
// src/reader/rgbaChannels.ts
|
|
469
|
+
function normalizeChannelName(name) {
|
|
470
|
+
return name.split(".").pop()?.toUpperCase() ?? "";
|
|
471
|
+
}
|
|
472
|
+
function pickChannel(channels, aliases) {
|
|
473
|
+
for (const alias of aliases) {
|
|
474
|
+
const exact2 = channels[alias];
|
|
475
|
+
if (exact2) return exact2;
|
|
476
|
+
}
|
|
477
|
+
const normalizedAliases = new Set(aliases.map((alias) => alias.toUpperCase()));
|
|
478
|
+
for (const [name, channel] of Object.entries(channels)) {
|
|
479
|
+
if (normalizedAliases.has(normalizeChannelName(name))) return channel;
|
|
480
|
+
}
|
|
481
|
+
return void 0;
|
|
482
|
+
}
|
|
483
|
+
function selectRgbaChannels(channels) {
|
|
484
|
+
const red = pickChannel(channels, ["R", "RED"]) ?? Object.values(channels)[0];
|
|
485
|
+
const luma = pickChannel(channels, ["Y", "LUMA", "LUMINANCE"]);
|
|
486
|
+
return [
|
|
487
|
+
red,
|
|
488
|
+
pickChannel(channels, ["G", "GREEN"]) ?? luma ?? red,
|
|
489
|
+
pickChannel(channels, ["B", "BLUE"]) ?? luma ?? red,
|
|
490
|
+
pickChannel(channels, ["A", "ALPHA"])
|
|
491
|
+
];
|
|
492
|
+
}
|
|
493
|
+
function getExrRgbaStoragePrecision(channels, channelNames) {
|
|
494
|
+
const names = channelNames && new Set(channelNames);
|
|
495
|
+
const selected = selectRgbaChannels(
|
|
496
|
+
Object.fromEntries(
|
|
497
|
+
channels.filter((channel) => !names || names.has(channel.name)).map((channel) => [channel.name, channel])
|
|
498
|
+
)
|
|
499
|
+
);
|
|
500
|
+
return selected.every((channel) => !channel || channel.pixelType === 1) ? "float16" : "float32";
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// src/reader/sampleMap.ts
|
|
504
|
+
function clamp(value, min, max) {
|
|
505
|
+
if (value < min) return min;
|
|
506
|
+
if (value > max) return max;
|
|
507
|
+
return value;
|
|
508
|
+
}
|
|
509
|
+
function buildNearestSampleMap(length, worldMin, sampleOrigin, sampling, sampledLength) {
|
|
510
|
+
const output = new Int32Array(length);
|
|
511
|
+
if (sampledLength <= 0) return output;
|
|
512
|
+
for (let i = 0; i < length; i++) {
|
|
513
|
+
const world = worldMin + i;
|
|
514
|
+
const sample = Math.round((world - sampleOrigin) / sampling);
|
|
515
|
+
output[i] = clamp(sample, 0, sampledLength - 1);
|
|
516
|
+
}
|
|
517
|
+
return output;
|
|
518
|
+
}
|
|
519
|
+
|
|
468
520
|
// src/reader/compression/b44.ts
|
|
469
521
|
var HALF_VALUES_PER_BLOCK = 16;
|
|
470
522
|
var B44_BLOCK_SIZE = 14;
|
|
@@ -692,7 +744,7 @@ function decodeB44Block(context) {
|
|
|
692
744
|
if (!isSampledCoordinate(y, layout.sampleOriginY, layout.ySampling)) {
|
|
693
745
|
continue;
|
|
694
746
|
}
|
|
695
|
-
if (
|
|
747
|
+
if (y < layout.firstChunkSampleY) {
|
|
696
748
|
continue;
|
|
697
749
|
}
|
|
698
750
|
const sampledRow = Math.floor((y - layout.firstChunkSampleY) / layout.ySampling);
|
|
@@ -1649,9 +1701,7 @@ function decodeDwaChunk(context) {
|
|
|
1649
1701
|
if (decodedWords !== header.totalAcUncompressedCount) {
|
|
1650
1702
|
throw new Error("Invalid DWA AC DEFLATE payload length.");
|
|
1651
1703
|
}
|
|
1652
|
-
|
|
1653
|
-
acBuffer = new Uint16Array(decodedWords);
|
|
1654
|
-
acBuffer.set(decodedView);
|
|
1704
|
+
acBuffer = new Uint16Array(decoded.buffer, decoded.byteOffset, decodedWords);
|
|
1655
1705
|
cursor.value += header.acCompressedSize;
|
|
1656
1706
|
break;
|
|
1657
1707
|
}
|
|
@@ -1676,9 +1726,7 @@ function decodeDwaChunk(context) {
|
|
|
1676
1726
|
if (dcWords !== header.totalDcUncompressedCount) {
|
|
1677
1727
|
throw new Error("Invalid DWA DC payload length.");
|
|
1678
1728
|
}
|
|
1679
|
-
|
|
1680
|
-
dcBuffer = new Uint16Array(dcWords);
|
|
1681
|
-
dcBuffer.set(dcView);
|
|
1729
|
+
dcBuffer = new Uint16Array(dcBytes.buffer, dcBytes.byteOffset, dcWords);
|
|
1682
1730
|
cursor.value += header.dcCompressedSize;
|
|
1683
1731
|
} else if (header.totalDcUncompressedCount !== 0) {
|
|
1684
1732
|
throw new Error("Invalid DWA DC payload metadata.");
|
|
@@ -1694,9 +1742,7 @@ function decodeDwaChunk(context) {
|
|
|
1694
1742
|
if (decoded.byteLength !== header.rleUncompressedSize) {
|
|
1695
1743
|
throw new Error("Invalid DWA RLE intermediate payload length.");
|
|
1696
1744
|
}
|
|
1697
|
-
|
|
1698
|
-
rleBuffer = new Uint8Array(decodedRle.byteLength);
|
|
1699
|
-
rleBuffer.set(decodedRle);
|
|
1745
|
+
rleBuffer = decodeRunLength(decoded, header.rleRawSize);
|
|
1700
1746
|
cursor.value += header.rleCompressedSize;
|
|
1701
1747
|
} else if (header.rleCompressedSize !== 0 || header.rleUncompressedSize !== 0) {
|
|
1702
1748
|
throw new Error("Invalid DWA RLE payload metadata.");
|
|
@@ -2727,6 +2773,37 @@ function getSupportedReadCompressionText() {
|
|
|
2727
2773
|
return Array.from(READ_COMPRESSION_HANDLERS.values()).map((handler) => handler.name).join(", ");
|
|
2728
2774
|
}
|
|
2729
2775
|
|
|
2776
|
+
// src/reader/scanlineIndex.ts
|
|
2777
|
+
function scanlineOffsetTableOffset(structure, partIndex) {
|
|
2778
|
+
let offset = structure.headerEndOffset;
|
|
2779
|
+
for (let i = 0; i < partIndex; i++) {
|
|
2780
|
+
const prior = structure.parts[i];
|
|
2781
|
+
if (!prior.dataWindow) continue;
|
|
2782
|
+
const height = prior.dataWindow.yMax - prior.dataWindow.yMin + 1;
|
|
2783
|
+
offset += Math.ceil(height / getScanlineLinesPerBlock(prior.compression ?? 0)) * 8;
|
|
2784
|
+
}
|
|
2785
|
+
return offset;
|
|
2786
|
+
}
|
|
2787
|
+
function readScanlineOffsets(view, offset, count, partId) {
|
|
2788
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || !Number.isSafeInteger(count) || count < 0 || offset + count * 8 > view.byteLength) {
|
|
2789
|
+
throw new ExrError("MALFORMED_OFFSET_TABLE", "Offset table is truncated or invalid.", {
|
|
2790
|
+
partId,
|
|
2791
|
+
offsetTablePtr: offset,
|
|
2792
|
+
chunkCount: count,
|
|
2793
|
+
size: view.byteLength
|
|
2794
|
+
});
|
|
2795
|
+
}
|
|
2796
|
+
const offsets = new Array(count);
|
|
2797
|
+
for (let i = 0; i < count; i++) {
|
|
2798
|
+
const low = view.getUint32(offset + i * 8, true);
|
|
2799
|
+
const high = view.getUint32(offset + i * 8 + 4, true);
|
|
2800
|
+
if (high > 2097151)
|
|
2801
|
+
throw new ExrError("MALFORMED_OFFSET_TABLE", "Chunk offset exceeds safe integer range.");
|
|
2802
|
+
offsets[i] = high * 4294967296 + low;
|
|
2803
|
+
}
|
|
2804
|
+
return offsets;
|
|
2805
|
+
}
|
|
2806
|
+
|
|
2730
2807
|
// src/reader/decodeExrPart.ts
|
|
2731
2808
|
var UINT32_MAX = 4294967295;
|
|
2732
2809
|
var INV_UINT32_MAX = 1 / UINT32_MAX;
|
|
@@ -2756,21 +2833,9 @@ function isSampledCoordinate4(value, firstSample, sampling) {
|
|
|
2756
2833
|
if (value < firstSample) return false;
|
|
2757
2834
|
return (value - firstSample) % sampling === 0;
|
|
2758
2835
|
}
|
|
2759
|
-
function
|
|
2760
|
-
if (
|
|
2761
|
-
|
|
2762
|
-
}
|
|
2763
|
-
const combined = BigInt(low) + (BigInt(high) << 32n);
|
|
2764
|
-
if (combined > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
2765
|
-
throw new ExrError("MALFORMED_OFFSET_TABLE", "Chunk offset exceeds safe integer range.", {
|
|
2766
|
-
offset: combined.toString()
|
|
2767
|
-
});
|
|
2768
|
-
}
|
|
2769
|
-
return Number(combined);
|
|
2770
|
-
}
|
|
2771
|
-
function buildChannelMeta(part, selectedChannelNames) {
|
|
2772
|
-
if (!part.dataWindow) return [];
|
|
2773
|
-
const { xMin, xMax, yMin, yMax } = part.dataWindow;
|
|
2836
|
+
function buildChannelMeta(part, selectedChannelNames, outputWindow = part.dataWindow) {
|
|
2837
|
+
if (!part.dataWindow || !outputWindow) return [];
|
|
2838
|
+
const { xMin, xMax, yMin, yMax } = outputWindow;
|
|
2774
2839
|
return part.channels.map((channel) => {
|
|
2775
2840
|
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
2776
2841
|
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
@@ -2791,7 +2856,9 @@ function buildChannelMeta(part, selectedChannelNames) {
|
|
|
2791
2856
|
sampledHeight,
|
|
2792
2857
|
sampleOriginX,
|
|
2793
2858
|
sampleOriginY,
|
|
2794
|
-
data:
|
|
2859
|
+
data: new Float32Array(0),
|
|
2860
|
+
stride: 1,
|
|
2861
|
+
lane: 0
|
|
2795
2862
|
};
|
|
2796
2863
|
});
|
|
2797
2864
|
}
|
|
@@ -2817,132 +2884,77 @@ function getExpectedUncompressedChunkSize(part, channelMeta, chunkY, linesInChun
|
|
|
2817
2884
|
}
|
|
2818
2885
|
function decodeRowIntoChannel(meta, blockData, blockPointer, rowOffset, blockView) {
|
|
2819
2886
|
const destination = meta.data;
|
|
2887
|
+
const stride = meta.stride;
|
|
2888
|
+
let target = rowOffset * stride + meta.lane;
|
|
2820
2889
|
const sampleCount = meta.sampledWidth;
|
|
2821
2890
|
const sourceByteOffset = blockData.byteOffset + blockPointer;
|
|
2822
2891
|
if (meta.pixelType === 1) {
|
|
2892
|
+
if (destination instanceof Uint16Array) {
|
|
2893
|
+
if ((sourceByteOffset & 1) === 0) {
|
|
2894
|
+
const source = new Uint16Array(blockData.buffer, sourceByteOffset, sampleCount);
|
|
2895
|
+
if (stride === 1) destination.set(source, target);
|
|
2896
|
+
else
|
|
2897
|
+
for (let i = 0; i < sampleCount; i++, target += stride) destination[target] = source[i];
|
|
2898
|
+
} else {
|
|
2899
|
+
for (let i = 0; i < sampleCount; i++, target += stride)
|
|
2900
|
+
destination[target] = blockView.getUint16(blockPointer + (i << 1), true);
|
|
2901
|
+
}
|
|
2902
|
+
return;
|
|
2903
|
+
}
|
|
2823
2904
|
if ((sourceByteOffset & 1) === 0) {
|
|
2824
2905
|
const source = new Uint16Array(blockData.buffer, sourceByteOffset, sampleCount);
|
|
2825
|
-
for (let i = 0; i < sampleCount; i
|
|
2826
|
-
destination[
|
|
2906
|
+
for (let i = 0; i < sampleCount; i++, target += stride) {
|
|
2907
|
+
destination[target] = float16ToFloat32(source[i]);
|
|
2827
2908
|
}
|
|
2828
2909
|
return;
|
|
2829
2910
|
}
|
|
2830
|
-
for (let i = 0; i < sampleCount; i
|
|
2831
|
-
destination[
|
|
2832
|
-
blockView.getUint16(blockPointer + (i << 1), true)
|
|
2833
|
-
);
|
|
2911
|
+
for (let i = 0; i < sampleCount; i++, target += stride) {
|
|
2912
|
+
destination[target] = float16ToFloat32(blockView.getUint16(blockPointer + (i << 1), true));
|
|
2834
2913
|
}
|
|
2835
2914
|
return;
|
|
2836
2915
|
}
|
|
2916
|
+
if (destination instanceof Uint16Array) throw new Error("Non-HALF channel in native HALF output");
|
|
2837
2917
|
if (meta.pixelType === 2) {
|
|
2838
2918
|
if ((sourceByteOffset & 3) === 0) {
|
|
2839
2919
|
const source = new Float32Array(blockData.buffer, sourceByteOffset, sampleCount);
|
|
2840
|
-
|
|
2920
|
+
if (stride === 1) {
|
|
2921
|
+
destination.set(source, target);
|
|
2922
|
+
} else {
|
|
2923
|
+
for (let i = 0; i < sampleCount; i++, target += stride) {
|
|
2924
|
+
destination[target] = source[i];
|
|
2925
|
+
}
|
|
2926
|
+
}
|
|
2841
2927
|
return;
|
|
2842
2928
|
}
|
|
2843
|
-
for (let i = 0; i < sampleCount; i
|
|
2844
|
-
destination[
|
|
2929
|
+
for (let i = 0; i < sampleCount; i++, target += stride) {
|
|
2930
|
+
destination[target] = blockView.getFloat32(blockPointer + (i << 2), true);
|
|
2845
2931
|
}
|
|
2846
2932
|
return;
|
|
2847
2933
|
}
|
|
2848
2934
|
if ((sourceByteOffset & 3) === 0) {
|
|
2849
2935
|
const source = new Uint32Array(blockData.buffer, sourceByteOffset, sampleCount);
|
|
2850
|
-
for (let i = 0; i < sampleCount; i
|
|
2851
|
-
destination[
|
|
2936
|
+
for (let i = 0; i < sampleCount; i++, target += stride) {
|
|
2937
|
+
destination[target] = source[i] * INV_UINT32_MAX;
|
|
2852
2938
|
}
|
|
2853
2939
|
return;
|
|
2854
2940
|
}
|
|
2855
|
-
for (let i = 0; i < sampleCount; i
|
|
2856
|
-
destination[
|
|
2941
|
+
for (let i = 0; i < sampleCount; i++, target += stride) {
|
|
2942
|
+
destination[target] = blockView.getUint32(blockPointer + (i << 2), true) * INV_UINT32_MAX;
|
|
2857
2943
|
}
|
|
2858
2944
|
}
|
|
2859
|
-
function
|
|
2860
|
-
const buffer = toArrayBuffer(bufferInput);
|
|
2945
|
+
function createScanlineDecoderWithOutput(part, isMultipart, options, window, createOutput) {
|
|
2861
2946
|
const onEvent = options.onEvent;
|
|
2862
|
-
const t0 = nowMs2();
|
|
2863
|
-
const partIndex = structure.parts.findIndex((part2) => part2.id === options.partId);
|
|
2864
|
-
if (partIndex < 0) {
|
|
2865
|
-
throw new ExrError("PART_NOT_FOUND", `Part ${options.partId} was not found in EXR structure.`, {
|
|
2866
|
-
partId: options.partId
|
|
2867
|
-
});
|
|
2868
|
-
}
|
|
2869
|
-
const part = structure.parts[partIndex];
|
|
2870
|
-
if (part.type === "tiledimage") {
|
|
2871
|
-
throw new ExrError("UNSUPPORTED_PART_TYPE", "Tiled EXR parts are not supported.", {
|
|
2872
|
-
partId: options.partId,
|
|
2873
|
-
type: part.type
|
|
2874
|
-
});
|
|
2875
|
-
}
|
|
2876
2947
|
const compression = part.compression ?? 0;
|
|
2877
|
-
const
|
|
2878
|
-
if (!
|
|
2879
|
-
throw new ExrError(
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
partId: options.partId,
|
|
2884
|
-
compression
|
|
2885
|
-
}
|
|
2886
|
-
);
|
|
2887
|
-
}
|
|
2888
|
-
if (!part.dataWindow) {
|
|
2889
|
-
throw new ExrError("MISSING_DATA_WINDOW", "EXR part is missing required dataWindow.", {
|
|
2890
|
-
partId: options.partId
|
|
2891
|
-
});
|
|
2948
|
+
const handler = getReadCompressionHandler(compression);
|
|
2949
|
+
if (!handler)
|
|
2950
|
+
throw new ExrError("UNSUPPORTED_COMPRESSION", "Unsupported compression.", { compression });
|
|
2951
|
+
const dataWindow = part.dataWindow;
|
|
2952
|
+
if (!dataWindow || window.xMin !== dataWindow.xMin || window.xMax !== dataWindow.xMax) {
|
|
2953
|
+
throw new ExrError("MISSING_DATA_WINDOW", "A full-width scanline band is required.");
|
|
2892
2954
|
}
|
|
2893
|
-
const
|
|
2894
|
-
const
|
|
2895
|
-
const height = part.dataWindow.yMax - part.dataWindow.yMin + 1;
|
|
2955
|
+
const compressionHandler = handler;
|
|
2956
|
+
const partWindow = dataWindow;
|
|
2896
2957
|
const linesPerBlock = compressionHandler.linesPerBlock;
|
|
2897
|
-
const chunkCount = Math.ceil(height / linesPerBlock);
|
|
2898
|
-
emit2(onEvent, {
|
|
2899
|
-
phase: "decode",
|
|
2900
|
-
level: "info",
|
|
2901
|
-
code: "decode.setup",
|
|
2902
|
-
message: `Decoder setup for part ${part.id}.`,
|
|
2903
|
-
metrics: {
|
|
2904
|
-
partId: part.id,
|
|
2905
|
-
width,
|
|
2906
|
-
height,
|
|
2907
|
-
channels: part.channels.length,
|
|
2908
|
-
compression: compressionHandler.name || COMPRESSION_NAMES[compression] || "UNKNOWN",
|
|
2909
|
-
chunks: chunkCount
|
|
2910
|
-
}
|
|
2911
|
-
});
|
|
2912
|
-
let offsetTablePtr = structure.headerEndOffset;
|
|
2913
|
-
for (let i = 0; i < partIndex; i++) {
|
|
2914
|
-
const prior = structure.parts[i];
|
|
2915
|
-
if (!prior.dataWindow) continue;
|
|
2916
|
-
const priorHeight = prior.dataWindow.yMax - prior.dataWindow.yMin + 1;
|
|
2917
|
-
const priorChunks = Math.ceil(priorHeight / getScanlineLinesPerBlock(prior.compression ?? 0));
|
|
2918
|
-
offsetTablePtr += priorChunks * 8;
|
|
2919
|
-
}
|
|
2920
|
-
if (offsetTablePtr < 0 || offsetTablePtr + chunkCount * 8 > view.byteLength) {
|
|
2921
|
-
throw new ExrError("MALFORMED_OFFSET_TABLE", "Offset table is truncated or invalid.", {
|
|
2922
|
-
partId: options.partId,
|
|
2923
|
-
offsetTablePtr,
|
|
2924
|
-
chunkCount,
|
|
2925
|
-
size: view.byteLength
|
|
2926
|
-
});
|
|
2927
|
-
}
|
|
2928
|
-
const tOffsets = nowMs2();
|
|
2929
|
-
const chunkOffsets = new Array(chunkCount);
|
|
2930
|
-
for (let i = 0; i < chunkCount; i++) {
|
|
2931
|
-
const low = view.getUint32(offsetTablePtr + i * 8, true);
|
|
2932
|
-
const high = view.getUint32(offsetTablePtr + i * 8 + 4, true);
|
|
2933
|
-
chunkOffsets[i] = toNumberOffset(low, high);
|
|
2934
|
-
}
|
|
2935
|
-
emit2(onEvent, {
|
|
2936
|
-
phase: "decode",
|
|
2937
|
-
level: "info",
|
|
2938
|
-
code: "decode.offsets.read",
|
|
2939
|
-
message: "Read chunk offsets.",
|
|
2940
|
-
metrics: {
|
|
2941
|
-
partId: part.id,
|
|
2942
|
-
count: chunkOffsets.length,
|
|
2943
|
-
ms: (nowMs2() - tOffsets).toFixed(3)
|
|
2944
|
-
}
|
|
2945
|
-
});
|
|
2946
2958
|
const selectedChannelNames = options.channelNames ? new Set(options.channelNames) : void 0;
|
|
2947
2959
|
if (selectedChannelNames) {
|
|
2948
2960
|
for (const name of selectedChannelNames) {
|
|
@@ -2954,16 +2966,14 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
2954
2966
|
}
|
|
2955
2967
|
}
|
|
2956
2968
|
}
|
|
2957
|
-
const channelMeta = buildChannelMeta(part, selectedChannelNames);
|
|
2969
|
+
const channelMeta = buildChannelMeta(part, selectedChannelNames, window);
|
|
2970
|
+
const finish = createOutput(channelMeta, window);
|
|
2958
2971
|
const zipScratch = {
|
|
2959
2972
|
inflate: new Uint8Array(0),
|
|
2960
2973
|
output: new Uint8Array(0)
|
|
2961
2974
|
};
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
for (let chunkIndex = 0; chunkIndex < chunkOffsets.length; chunkIndex++) {
|
|
2965
|
-
const chunkOffset = chunkOffsets[chunkIndex];
|
|
2966
|
-
const chunkHeaderSize = structure.isMultipart ? 12 : 8;
|
|
2975
|
+
function decodeChunk(buffer, view, chunkOffset, chunkIndex, workerBlock) {
|
|
2976
|
+
const chunkHeaderSize = isMultipart ? 12 : 8;
|
|
2967
2977
|
if (chunkOffset < 0 || chunkOffset + chunkHeaderSize > view.byteLength) {
|
|
2968
2978
|
throw new ExrError("MALFORMED_CHUNK", "Chunk header is outside file bounds.", {
|
|
2969
2979
|
partId: part.id,
|
|
@@ -2971,10 +2981,10 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
2971
2981
|
chunkOffset
|
|
2972
2982
|
});
|
|
2973
2983
|
}
|
|
2974
|
-
let chunkY
|
|
2975
|
-
let dataSize
|
|
2976
|
-
let dataPtr
|
|
2977
|
-
if (
|
|
2984
|
+
let chunkY;
|
|
2985
|
+
let dataSize;
|
|
2986
|
+
let dataPtr;
|
|
2987
|
+
if (isMultipart) {
|
|
2978
2988
|
const partNumber = view.getInt32(chunkOffset, true);
|
|
2979
2989
|
chunkY = view.getInt32(chunkOffset + 4, true);
|
|
2980
2990
|
dataSize = view.getInt32(chunkOffset + 8, true);
|
|
@@ -2991,7 +3001,7 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
2991
3001
|
actualPart: partNumber
|
|
2992
3002
|
}
|
|
2993
3003
|
});
|
|
2994
|
-
|
|
3004
|
+
return 0;
|
|
2995
3005
|
}
|
|
2996
3006
|
} else {
|
|
2997
3007
|
chunkY = view.getInt32(chunkOffset, true);
|
|
@@ -3007,7 +3017,13 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
3007
3017
|
dataSize
|
|
3008
3018
|
});
|
|
3009
3019
|
}
|
|
3010
|
-
const linesInChunk = Math.max(0, Math.min(linesPerBlock,
|
|
3020
|
+
const linesInChunk = Math.max(0, Math.min(linesPerBlock, partWindow.yMax - chunkY + 1));
|
|
3021
|
+
if (linesInChunk <= 0 || chunkY < window.yMin || chunkY + linesInChunk - 1 > window.yMax) {
|
|
3022
|
+
throw new ExrError("MALFORMED_CHUNK", "Chunk is outside the requested scanline band.", {
|
|
3023
|
+
chunkY,
|
|
3024
|
+
chunkIndex
|
|
3025
|
+
});
|
|
3026
|
+
}
|
|
3011
3027
|
const expectedUncompressedSize = getExpectedUncompressedChunkSize(
|
|
3012
3028
|
part,
|
|
3013
3029
|
channelMeta,
|
|
@@ -3016,7 +3032,7 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
3016
3032
|
);
|
|
3017
3033
|
const predecodedZipBlock = options.predecodedZipBlocks?.get(chunkIndex);
|
|
3018
3034
|
const predecodedDwaBlock = options.predecodedDwaBlocks?.get(chunkIndex);
|
|
3019
|
-
const predecodedBlock = predecodedZipBlock ?? predecodedDwaBlock;
|
|
3035
|
+
const predecodedBlock = workerBlock ?? predecodedZipBlock ?? predecodedDwaBlock;
|
|
3020
3036
|
const blockData = predecodedBlock ? predecodedBlock : dataSize === expectedUncompressedSize ? new Uint8Array(buffer, dataPtr, dataSize) : compressionHandler.decodeBlock({
|
|
3021
3037
|
buffer,
|
|
3022
3038
|
dataPtr,
|
|
@@ -3029,12 +3045,11 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
3029
3045
|
linesInChunk,
|
|
3030
3046
|
zipScratch: compression === 2 || compression === 3 ? zipScratch : void 0
|
|
3031
3047
|
});
|
|
3032
|
-
bytesRead += dataSize;
|
|
3033
3048
|
let blockPointer = 0;
|
|
3034
3049
|
const blockView = new DataView(blockData.buffer, blockData.byteOffset, blockData.byteLength);
|
|
3035
3050
|
for (let dy = 0; dy < linesInChunk; dy++) {
|
|
3036
3051
|
const y = chunkY + dy;
|
|
3037
|
-
if (y <
|
|
3052
|
+
if (y < partWindow.yMin) {
|
|
3038
3053
|
continue;
|
|
3039
3054
|
}
|
|
3040
3055
|
for (const meta of channelMeta) {
|
|
@@ -3081,40 +3096,536 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
3081
3096
|
}
|
|
3082
3097
|
});
|
|
3083
3098
|
}
|
|
3099
|
+
return dataSize;
|
|
3084
3100
|
}
|
|
3085
|
-
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3101
|
+
return {
|
|
3102
|
+
decodeChunk,
|
|
3103
|
+
finish,
|
|
3104
|
+
getPlanarBytes: () => channelMeta.reduce(
|
|
3105
|
+
(bytes, meta) => bytes + (meta.stride === 1 ? meta.data.byteLength : 0),
|
|
3106
|
+
0
|
|
3107
|
+
)
|
|
3108
|
+
};
|
|
3109
|
+
}
|
|
3110
|
+
function createPlanarOutput(meta, window) {
|
|
3111
|
+
for (const channel of meta) {
|
|
3112
|
+
if (channel.selected)
|
|
3113
|
+
channel.data = new Float32Array(channel.sampledWidth * channel.sampledHeight);
|
|
3114
|
+
}
|
|
3115
|
+
return () => {
|
|
3116
|
+
const channels = {};
|
|
3117
|
+
for (const entry of meta) {
|
|
3118
|
+
if (!entry.selected) continue;
|
|
3119
|
+
if (!(entry.data instanceof Float32Array)) throw new Error("Invalid planar storage");
|
|
3120
|
+
channels[entry.channel.name] = {
|
|
3121
|
+
pixelType: entry.channel.pixelType,
|
|
3122
|
+
xSampling: entry.channel.xSampling,
|
|
3123
|
+
ySampling: entry.channel.ySampling,
|
|
3124
|
+
sampledWidth: entry.sampledWidth,
|
|
3125
|
+
sampledHeight: entry.sampledHeight,
|
|
3126
|
+
sampleOriginX: entry.sampleOriginX,
|
|
3127
|
+
sampleOriginY: entry.sampleOriginY,
|
|
3128
|
+
data: entry.data
|
|
3129
|
+
};
|
|
3130
|
+
}
|
|
3131
|
+
return {
|
|
3132
|
+
width: window.xMax - window.xMin + 1,
|
|
3133
|
+
height: window.yMax - window.yMin + 1,
|
|
3134
|
+
channels
|
|
3097
3135
|
};
|
|
3136
|
+
};
|
|
3137
|
+
}
|
|
3138
|
+
function createScanlineDecoder(part, isMultipart, options, window) {
|
|
3139
|
+
return createScanlineDecoderWithOutput(part, isMultipart, options, window, createPlanarOutput);
|
|
3140
|
+
}
|
|
3141
|
+
function createRgbaOutput(meta, window, allocate) {
|
|
3142
|
+
const width = window.xMax - window.xMin + 1;
|
|
3143
|
+
const height = window.yMax - window.yMin + 1;
|
|
3144
|
+
const channels = Object.fromEntries(
|
|
3145
|
+
meta.filter((entry) => entry.selected).map((entry) => [entry.channel.name, entry])
|
|
3146
|
+
);
|
|
3147
|
+
const selected = selectRgbaChannels(channels);
|
|
3148
|
+
const rgba = allocate(width * height * 4);
|
|
3149
|
+
const lanes = selected.map((entry) => entry === void 0 ? -1 : selected.indexOf(entry));
|
|
3150
|
+
const sampled = [];
|
|
3151
|
+
for (const entry of meta) {
|
|
3152
|
+
const lane = selected.indexOf(entry);
|
|
3153
|
+
entry.selected = lane >= 0;
|
|
3154
|
+
if (!entry.selected) continue;
|
|
3155
|
+
if (entry.sampledWidth === width && entry.sampledHeight === height && entry.sampleOriginX === window.xMin && entry.sampleOriginY === window.yMin && entry.channel.xSampling <= 1 && entry.channel.ySampling <= 1) {
|
|
3156
|
+
entry.data = rgba;
|
|
3157
|
+
entry.stride = 4;
|
|
3158
|
+
entry.lane = lane;
|
|
3159
|
+
} else {
|
|
3160
|
+
entry.data = allocate(entry.sampledWidth * entry.sampledHeight);
|
|
3161
|
+
sampled.push({ entry, lane });
|
|
3162
|
+
}
|
|
3163
|
+
}
|
|
3164
|
+
return () => {
|
|
3165
|
+
for (const { entry, lane } of sampled) {
|
|
3166
|
+
if (!entry.data.length) continue;
|
|
3167
|
+
const xMap = buildNearestSampleMap(
|
|
3168
|
+
width,
|
|
3169
|
+
window.xMin,
|
|
3170
|
+
entry.sampleOriginX,
|
|
3171
|
+
Math.max(1, entry.channel.xSampling),
|
|
3172
|
+
entry.sampledWidth
|
|
3173
|
+
);
|
|
3174
|
+
const yMap = buildNearestSampleMap(
|
|
3175
|
+
height,
|
|
3176
|
+
window.yMin,
|
|
3177
|
+
entry.sampleOriginY,
|
|
3178
|
+
Math.max(1, entry.channel.ySampling),
|
|
3179
|
+
entry.sampledHeight
|
|
3180
|
+
);
|
|
3181
|
+
for (let y = 0; y < height; y++) {
|
|
3182
|
+
const row = yMap[y] * entry.sampledWidth;
|
|
3183
|
+
let offset = y * width * 4 + lane;
|
|
3184
|
+
for (let x = 0; x < width; x++, offset += 4) {
|
|
3185
|
+
rgba[offset] = entry.data[row + xMap[x]];
|
|
3186
|
+
}
|
|
3187
|
+
}
|
|
3188
|
+
}
|
|
3189
|
+
const [r, g, b, a] = lanes;
|
|
3190
|
+
if (rgba instanceof Uint16Array) {
|
|
3191
|
+
const finite = (bits) => (bits & 31744) !== 31744;
|
|
3192
|
+
for (let offset = 0; offset < rgba.length; offset += 4) {
|
|
3193
|
+
const rawRed = r < 0 ? 0 : rgba[offset + r];
|
|
3194
|
+
const rawGreen = g < 0 ? rawRed : rgba[offset + g];
|
|
3195
|
+
const rawBlue = b < 0 ? rawRed : rgba[offset + b];
|
|
3196
|
+
const rawAlpha = a < 0 ? 15360 : rgba[offset + a];
|
|
3197
|
+
const red = finite(rawRed) ? rawRed : 0;
|
|
3198
|
+
rgba[offset] = red;
|
|
3199
|
+
rgba[offset + 1] = finite(rawGreen) ? rawGreen : red;
|
|
3200
|
+
rgba[offset + 2] = finite(rawBlue) ? rawBlue : red;
|
|
3201
|
+
const alphaNaN = (rawAlpha & 32767) > 31744;
|
|
3202
|
+
rgba[offset + 3] = alphaNaN ? 32256 : rawAlpha & 32768 ? 0 : rawAlpha;
|
|
3203
|
+
}
|
|
3204
|
+
return rgba;
|
|
3205
|
+
}
|
|
3206
|
+
for (let offset = 0; offset < rgba.length; offset += 4) {
|
|
3207
|
+
const rawRed = r < 0 ? 0 : rgba[offset + r];
|
|
3208
|
+
const rawGreen = g < 0 ? rawRed : rgba[offset + g];
|
|
3209
|
+
const rawBlue = b < 0 ? rawRed : rgba[offset + b];
|
|
3210
|
+
const alpha = a < 0 ? 1 : Math.max(0, rgba[offset + a]);
|
|
3211
|
+
const red = Number.isFinite(rawRed) ? rawRed : 0;
|
|
3212
|
+
rgba[offset] = red;
|
|
3213
|
+
rgba[offset + 1] = Number.isFinite(rawGreen) ? rawGreen : red;
|
|
3214
|
+
rgba[offset + 2] = Number.isFinite(rawBlue) ? rawBlue : red;
|
|
3215
|
+
rgba[offset + 3] = alpha;
|
|
3216
|
+
}
|
|
3217
|
+
return rgba;
|
|
3218
|
+
};
|
|
3219
|
+
}
|
|
3220
|
+
function preparePartWithOutput(bufferInput, structure, options, createOutput) {
|
|
3221
|
+
const buffer = toArrayBuffer(bufferInput);
|
|
3222
|
+
const onEvent = options.onEvent;
|
|
3223
|
+
const t0 = nowMs2();
|
|
3224
|
+
const partIndex = structure.parts.findIndex((part2) => part2.id === options.partId);
|
|
3225
|
+
if (partIndex < 0) {
|
|
3226
|
+
throw new ExrError("PART_NOT_FOUND", `Part ${options.partId} was not found in EXR structure.`, {
|
|
3227
|
+
partId: options.partId
|
|
3228
|
+
});
|
|
3229
|
+
}
|
|
3230
|
+
const part = structure.parts[partIndex];
|
|
3231
|
+
if ((structure.flags & 2) !== 0 || part.type && part.type !== "scanlineimage" || (structure.flags & 8) !== 0) {
|
|
3232
|
+
throw new ExrError("UNSUPPORTED_PART_TYPE", "Only flat scanline EXR parts are supported.", {
|
|
3233
|
+
partId: options.partId,
|
|
3234
|
+
type: part.type ?? "unknown"
|
|
3235
|
+
});
|
|
3236
|
+
}
|
|
3237
|
+
const compression = part.compression ?? 0;
|
|
3238
|
+
const compressionHandler = getReadCompressionHandler(compression);
|
|
3239
|
+
if (!compressionHandler) {
|
|
3240
|
+
throw new ExrError(
|
|
3241
|
+
"UNSUPPORTED_COMPRESSION",
|
|
3242
|
+
`Unsupported compression. Supported: ${getSupportedReadCompressionText()}.`,
|
|
3243
|
+
{
|
|
3244
|
+
partId: options.partId,
|
|
3245
|
+
compression
|
|
3246
|
+
}
|
|
3247
|
+
);
|
|
3248
|
+
}
|
|
3249
|
+
if (!part.dataWindow) {
|
|
3250
|
+
throw new ExrError("MISSING_DATA_WINDOW", "EXR part is missing required dataWindow.", {
|
|
3251
|
+
partId: options.partId
|
|
3252
|
+
});
|
|
3098
3253
|
}
|
|
3254
|
+
const view = new DataView(buffer);
|
|
3255
|
+
const width = part.dataWindow.xMax - part.dataWindow.xMin + 1;
|
|
3256
|
+
const height = part.dataWindow.yMax - part.dataWindow.yMin + 1;
|
|
3257
|
+
const linesPerBlock = compressionHandler.linesPerBlock;
|
|
3258
|
+
const chunkCount = Math.ceil(height / linesPerBlock);
|
|
3259
|
+
emit2(onEvent, {
|
|
3260
|
+
phase: "decode",
|
|
3261
|
+
level: "info",
|
|
3262
|
+
code: "decode.setup",
|
|
3263
|
+
message: `Decoder setup for part ${part.id}.`,
|
|
3264
|
+
metrics: {
|
|
3265
|
+
partId: part.id,
|
|
3266
|
+
width,
|
|
3267
|
+
height,
|
|
3268
|
+
channels: part.channels.length,
|
|
3269
|
+
compression: compressionHandler.name || COMPRESSION_NAMES[compression] || "UNKNOWN",
|
|
3270
|
+
chunks: chunkCount
|
|
3271
|
+
}
|
|
3272
|
+
});
|
|
3273
|
+
const tOffsets = nowMs2();
|
|
3274
|
+
const chunkOffsets = readScanlineOffsets(
|
|
3275
|
+
view,
|
|
3276
|
+
scanlineOffsetTableOffset(structure, partIndex),
|
|
3277
|
+
chunkCount,
|
|
3278
|
+
part.id
|
|
3279
|
+
);
|
|
3099
3280
|
emit2(onEvent, {
|
|
3100
3281
|
phase: "decode",
|
|
3101
3282
|
level: "info",
|
|
3102
|
-
code: "decode.
|
|
3103
|
-
message:
|
|
3283
|
+
code: "decode.offsets.read",
|
|
3284
|
+
message: "Read chunk offsets.",
|
|
3104
3285
|
metrics: {
|
|
3105
3286
|
partId: part.id,
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
ms: (nowMs2() - tDecode).toFixed(3),
|
|
3109
|
-
totalMs: (nowMs2() - t0).toFixed(3)
|
|
3287
|
+
count: chunkOffsets.length,
|
|
3288
|
+
ms: (nowMs2() - tOffsets).toFixed(3)
|
|
3110
3289
|
}
|
|
3111
3290
|
});
|
|
3291
|
+
const decoder = createScanlineDecoderWithOutput(
|
|
3292
|
+
part,
|
|
3293
|
+
structure.isMultipart,
|
|
3294
|
+
options,
|
|
3295
|
+
part.dataWindow,
|
|
3296
|
+
createOutput
|
|
3297
|
+
);
|
|
3298
|
+
const tDecode = nowMs2();
|
|
3299
|
+
let bytesRead = 0;
|
|
3300
|
+
let assemblyMs = 0;
|
|
3301
|
+
let nextChunk = 0;
|
|
3302
|
+
function consume(chunkIndex, workerBlock) {
|
|
3303
|
+
if (chunkIndex !== nextChunk || chunkIndex >= chunkOffsets.length) {
|
|
3304
|
+
throw new Error("Scanline chunks must be consumed in offset-table order.");
|
|
3305
|
+
}
|
|
3306
|
+
const started = nowMs2();
|
|
3307
|
+
bytesRead += decoder.decodeChunk(
|
|
3308
|
+
buffer,
|
|
3309
|
+
view,
|
|
3310
|
+
chunkOffsets[chunkIndex],
|
|
3311
|
+
chunkIndex,
|
|
3312
|
+
workerBlock
|
|
3313
|
+
);
|
|
3314
|
+
assemblyMs += nowMs2() - started;
|
|
3315
|
+
nextChunk++;
|
|
3316
|
+
}
|
|
3317
|
+
function finish(workerMetrics = {}) {
|
|
3318
|
+
if (nextChunk !== chunkOffsets.length) throw new Error("Incomplete scanline decode.");
|
|
3319
|
+
const tFinalize = nowMs2();
|
|
3320
|
+
const result = decoder.finish();
|
|
3321
|
+
const rgba = result instanceof Float32Array || result instanceof Uint16Array;
|
|
3322
|
+
emit2(onEvent, {
|
|
3323
|
+
phase: "decode",
|
|
3324
|
+
level: "info",
|
|
3325
|
+
code: "decode.complete",
|
|
3326
|
+
message: `Decoded part ${part.id}.`,
|
|
3327
|
+
metrics: {
|
|
3328
|
+
partId: part.id,
|
|
3329
|
+
channels: options.channelNames ? new Set(options.channelNames).size : part.channels.length,
|
|
3330
|
+
outputLayout: rgba ? "rgba" : "planar",
|
|
3331
|
+
...rgba ? { storagePrecision: result instanceof Uint16Array ? "float16" : "float32" } : {},
|
|
3332
|
+
planarBytes: decoder.getPlanarBytes(),
|
|
3333
|
+
rgbaBytes: rgba ? result.byteLength : 0,
|
|
3334
|
+
assemblyMs,
|
|
3335
|
+
finalizeMs: (nowMs2() - tFinalize).toFixed(3),
|
|
3336
|
+
...workerMetrics,
|
|
3337
|
+
readKB: (bytesRead / 1024).toFixed(1),
|
|
3338
|
+
ms: (nowMs2() - tDecode).toFixed(3),
|
|
3339
|
+
totalMs: (nowMs2() - t0).toFixed(3)
|
|
3340
|
+
}
|
|
3341
|
+
});
|
|
3342
|
+
return result;
|
|
3343
|
+
}
|
|
3344
|
+
return { chunkOffsets, consume, finish };
|
|
3345
|
+
}
|
|
3346
|
+
function prepareExrPart(buffer, structure, options) {
|
|
3347
|
+
return preparePartWithOutput(buffer, structure, options, createPlanarOutput);
|
|
3348
|
+
}
|
|
3349
|
+
function prepareExrPartRgba(buffer, structure, options) {
|
|
3350
|
+
return preparePartWithOutput(
|
|
3351
|
+
buffer,
|
|
3352
|
+
structure,
|
|
3353
|
+
options,
|
|
3354
|
+
(meta, window) => createRgbaOutput(meta, window, (length) => new Float32Array(length))
|
|
3355
|
+
);
|
|
3356
|
+
}
|
|
3357
|
+
function consumeAll(session) {
|
|
3358
|
+
for (let i = 0; i < session.chunkOffsets.length; i++) session.consume(i);
|
|
3359
|
+
return session.finish();
|
|
3360
|
+
}
|
|
3361
|
+
function decodeExrPart(buffer, structure, options) {
|
|
3362
|
+
return consumeAll(prepareExrPart(buffer, structure, options));
|
|
3363
|
+
}
|
|
3364
|
+
function decodeExrPartRgba(buffer, structure, options) {
|
|
3365
|
+
return consumeAll(prepareExrPartRgba(buffer, structure, options));
|
|
3366
|
+
}
|
|
3367
|
+
function prepareExrPartNativeRgba(buffer, structure, options) {
|
|
3368
|
+
const part = structure.parts.find((candidate) => candidate.id === options.partId);
|
|
3369
|
+
if (!part || getExrRgbaStoragePrecision(part.channels, options.channelNames) === "float32")
|
|
3370
|
+
return prepareExrPartRgba(buffer, structure, options);
|
|
3371
|
+
return preparePartWithOutput(
|
|
3372
|
+
buffer,
|
|
3373
|
+
structure,
|
|
3374
|
+
options,
|
|
3375
|
+
(meta, window) => createRgbaOutput(meta, window, (length) => new Uint16Array(length))
|
|
3376
|
+
);
|
|
3377
|
+
}
|
|
3378
|
+
function decodeExrPartNativeRgba(buffer, structure, options) {
|
|
3379
|
+
return consumeAll(prepareExrPartNativeRgba(buffer, structure, options));
|
|
3380
|
+
}
|
|
3381
|
+
|
|
3382
|
+
// src/reader/openExr.ts
|
|
3383
|
+
var invalid = (message) => new ExrError("MALFORMED_OFFSET_TABLE", message);
|
|
3384
|
+
async function exact(source, offset, length, signal) {
|
|
3385
|
+
signal?.throwIfAborted();
|
|
3386
|
+
if (!Number.isSafeInteger(offset) || !Number.isSafeInteger(length) || offset < 0 || length < 0 || !Number.isSafeInteger(offset + length) || source.size !== void 0 && offset + length > source.size) {
|
|
3387
|
+
throw invalid("Invalid byte range.");
|
|
3388
|
+
}
|
|
3389
|
+
const bytes = await source.readRange(offset, length, signal);
|
|
3390
|
+
signal?.throwIfAborted();
|
|
3391
|
+
if (bytes.byteLength !== length) throw new ExrError("TRUNCATED_FILE", "Incomplete byte range.");
|
|
3392
|
+
return bytes;
|
|
3393
|
+
}
|
|
3394
|
+
function scanlineLayout(structure, part) {
|
|
3395
|
+
const compression = getReadCompressionHandler(part.compression ?? 0);
|
|
3396
|
+
if (!compression) throw new ExrError("UNSUPPORTED_COMPRESSION", "Unsupported compression.");
|
|
3397
|
+
if (structure.flags & 10 || part.type && part.type !== "scanlineimage") {
|
|
3398
|
+
throw new ExrError(
|
|
3399
|
+
"UNSUPPORTED_PART_TYPE",
|
|
3400
|
+
"Random access currently supports flat scanline EXRs."
|
|
3401
|
+
);
|
|
3402
|
+
}
|
|
3403
|
+
const window = part.dataWindow;
|
|
3404
|
+
if (!window || window.xMax < window.xMin || window.yMax < window.yMin) {
|
|
3405
|
+
throw new ExrError("MISSING_DATA_WINDOW", "Invalid EXR data window.");
|
|
3406
|
+
}
|
|
3407
|
+
const lines = compression.linesPerBlock;
|
|
3408
|
+
const count = Math.ceil((window.yMax - window.yMin + 1) / lines);
|
|
3409
|
+
return { window, lines, count };
|
|
3410
|
+
}
|
|
3411
|
+
function bufferExrSource(input) {
|
|
3412
|
+
const buffer = input instanceof ArrayBuffer ? new Uint8Array(input) : input;
|
|
3112
3413
|
return {
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3414
|
+
size: buffer.byteLength,
|
|
3415
|
+
async readRange(offset, length, signal) {
|
|
3416
|
+
signal?.throwIfAborted();
|
|
3417
|
+
return Uint8Array.from(buffer.subarray(offset, offset + length)).buffer;
|
|
3418
|
+
}
|
|
3419
|
+
};
|
|
3420
|
+
}
|
|
3421
|
+
async function openExr(source, options = {}) {
|
|
3422
|
+
const limit = options.maxMetadataBytes ?? 16 * 1024 * 1024;
|
|
3423
|
+
if (!Number.isSafeInteger(limit) || limit < 8) throw invalid("Invalid metadata budget.");
|
|
3424
|
+
let length = Math.min(64 * 1024, limit, source.size ?? Infinity);
|
|
3425
|
+
let prefix;
|
|
3426
|
+
let structure;
|
|
3427
|
+
for (; ; ) {
|
|
3428
|
+
options.signal?.throwIfAborted();
|
|
3429
|
+
prefix = await source.readRange(0, length, options.signal);
|
|
3430
|
+
options.signal?.throwIfAborted();
|
|
3431
|
+
try {
|
|
3432
|
+
structure = parseExrStructure(prefix);
|
|
3433
|
+
break;
|
|
3434
|
+
} catch (error) {
|
|
3435
|
+
if (!isExrError(error) || error.code !== "TRUNCATED_FILE" || prefix.byteLength < length || length >= limit)
|
|
3436
|
+
throw error;
|
|
3437
|
+
const next = Math.min(length * 2, limit, source.size ?? Infinity);
|
|
3438
|
+
if (next <= length) throw error;
|
|
3439
|
+
length = next;
|
|
3440
|
+
}
|
|
3441
|
+
}
|
|
3442
|
+
if (options.onEvent) parseExrStructure(prefix, { onEvent: options.onEvent });
|
|
3443
|
+
const layouts = structure.parts.map((part) => scanlineLayout(structure, part));
|
|
3444
|
+
const end = structure.headerEndOffset + layouts.reduce((n, l) => n + l.count * 8, 0);
|
|
3445
|
+
if (!Number.isSafeInteger(end) || end > limit)
|
|
3446
|
+
throw invalid("EXR metadata exceeds configured budget.");
|
|
3447
|
+
const table = end <= prefix.byteLength ? prefix : await exact(source, 0, end, options.signal);
|
|
3448
|
+
const view = new DataView(table);
|
|
3449
|
+
let ptr = structure.headerEndOffset;
|
|
3450
|
+
const chunks = [];
|
|
3451
|
+
for (let p = 0; p < layouts.length; p++) {
|
|
3452
|
+
const { window, count, lines } = layouts[p];
|
|
3453
|
+
const offsets = readScanlineOffsets(view, ptr, count, structure.parts[p].id);
|
|
3454
|
+
ptr += count * 8;
|
|
3455
|
+
for (let i = 0; i < count; i++) {
|
|
3456
|
+
const offset = offsets[i];
|
|
3457
|
+
if (!Number.isSafeInteger(offset) || offset < end || source.size !== void 0 && offset >= source.size)
|
|
3458
|
+
throw invalid("Invalid EXR chunk offset.");
|
|
3459
|
+
const yMin = window.yMin + i * lines;
|
|
3460
|
+
chunks.push({
|
|
3461
|
+
index: i,
|
|
3462
|
+
partId: structure.parts[p].id,
|
|
3463
|
+
yMin,
|
|
3464
|
+
yMax: Math.min(window.yMax, yMin + lines - 1),
|
|
3465
|
+
offset
|
|
3466
|
+
});
|
|
3467
|
+
}
|
|
3468
|
+
}
|
|
3469
|
+
const physical = [...chunks].sort((a, b) => a.offset - b.offset);
|
|
3470
|
+
const lengths = /* @__PURE__ */ new Map();
|
|
3471
|
+
const headerSize = structure.isMultipart ? 12 : 8;
|
|
3472
|
+
for (let i = 0; i < physical.length; i++) {
|
|
3473
|
+
const next = physical[i + 1]?.offset ?? source.size;
|
|
3474
|
+
if (next === void 0) continue;
|
|
3475
|
+
const size = next - physical[i].offset;
|
|
3476
|
+
if (size < headerSize) throw invalid("Overlapping EXR chunks.");
|
|
3477
|
+
lengths.set(physical[i].offset, size);
|
|
3478
|
+
}
|
|
3479
|
+
return new ScanlineExrReader(
|
|
3480
|
+
source,
|
|
3481
|
+
structure,
|
|
3482
|
+
chunks.map((c) => ({ ...c, byteLength: lengths.get(c.offset) }))
|
|
3483
|
+
);
|
|
3484
|
+
}
|
|
3485
|
+
function intersect(a, b) {
|
|
3486
|
+
const window = {
|
|
3487
|
+
xMin: Math.max(a.xMin, b.xMin),
|
|
3488
|
+
yMin: Math.max(a.yMin, b.yMin),
|
|
3489
|
+
xMax: Math.min(a.xMax, b.xMax),
|
|
3490
|
+
yMax: Math.min(a.yMax, b.yMax)
|
|
3116
3491
|
};
|
|
3492
|
+
if (Object.values(window).some((v) => !Number.isSafeInteger(v)) || window.xMin > window.xMax || window.yMin > window.yMax) {
|
|
3493
|
+
throw new RangeError("Region does not intersect the data window.");
|
|
3494
|
+
}
|
|
3495
|
+
return window;
|
|
3117
3496
|
}
|
|
3497
|
+
var ScanlineExrReader = class {
|
|
3498
|
+
constructor(source, structure, chunks) {
|
|
3499
|
+
this.source = source;
|
|
3500
|
+
this.structure = structure;
|
|
3501
|
+
this.chunks = chunks;
|
|
3502
|
+
}
|
|
3503
|
+
getChunks(partId) {
|
|
3504
|
+
this.part(partId);
|
|
3505
|
+
return this.chunks.filter((c) => c.partId === partId);
|
|
3506
|
+
}
|
|
3507
|
+
part(id) {
|
|
3508
|
+
const part = this.structure.parts.find((p) => p.id === id);
|
|
3509
|
+
if (!part) throw new ExrError("PART_NOT_FOUND", "EXR part not found.", { partId: id });
|
|
3510
|
+
return part;
|
|
3511
|
+
}
|
|
3512
|
+
/** Yield complete full-width scanline bands. Region selects intersecting chunks, not cropped pixels. */
|
|
3513
|
+
async *decodeChunks(options) {
|
|
3514
|
+
const part = this.part(options.partId);
|
|
3515
|
+
const window = part.dataWindow;
|
|
3516
|
+
const region = options.region ? intersect(window, options.region) : window;
|
|
3517
|
+
const remaining = this.getChunks(part.id).filter(
|
|
3518
|
+
(c) => c.yMax >= region.yMin && c.yMin <= region.yMax
|
|
3519
|
+
);
|
|
3520
|
+
const headerSize = this.structure.isMultipart ? 12 : 8;
|
|
3521
|
+
while (remaining.length) {
|
|
3522
|
+
options.signal?.throwIfAborted();
|
|
3523
|
+
const requestedBytes = typeof options.batchBytes === "function" ? options.batchBytes() : options.batchBytes;
|
|
3524
|
+
const maxBytes = Number.isFinite(requestedBytes) && requestedBytes > 0 ? Math.floor(requestedBytes) : 2 * 1024 * 1024;
|
|
3525
|
+
const priority = options.priority?.();
|
|
3526
|
+
let start = priority ? remaining.findIndex((c) => c.yMax >= priority.yMin && c.yMin <= priority.yMax) : 0;
|
|
3527
|
+
if (start < 0) start = 0;
|
|
3528
|
+
const first = remaining[start];
|
|
3529
|
+
const batch = [first];
|
|
3530
|
+
let size = first.byteLength ?? 0;
|
|
3531
|
+
let rangeOffset = first.offset;
|
|
3532
|
+
let headerBytes;
|
|
3533
|
+
if (first.byteLength !== void 0) {
|
|
3534
|
+
for (let j = start + 1; j < remaining.length; j++) {
|
|
3535
|
+
const prev = batch[batch.length - 1], next = remaining[j];
|
|
3536
|
+
if (next.byteLength === void 0 || next.offset !== prev.offset + prev.byteLength && next.offset + next.byteLength !== prev.offset || next.yMin !== prev.yMax + 1 || size + next.byteLength > maxBytes)
|
|
3537
|
+
break;
|
|
3538
|
+
if (next.offset !== rangeOffset + size && next.offset + next.byteLength !== rangeOffset)
|
|
3539
|
+
break;
|
|
3540
|
+
batch.push(next);
|
|
3541
|
+
rangeOffset = Math.min(rangeOffset, next.offset);
|
|
3542
|
+
size += next.byteLength;
|
|
3543
|
+
}
|
|
3544
|
+
} else {
|
|
3545
|
+
headerBytes = await exact(this.source, first.offset, headerSize, options.signal);
|
|
3546
|
+
const header = new DataView(headerBytes);
|
|
3547
|
+
const payload = header.getInt32(headerSize - 4, true);
|
|
3548
|
+
if (payload < 0) throw new ExrError("MALFORMED_CHUNK", "Invalid EXR chunk length.");
|
|
3549
|
+
size = headerSize + payload;
|
|
3550
|
+
}
|
|
3551
|
+
if (size > 512 * 1024 * 1024)
|
|
3552
|
+
throw new ExrError("MALFORMED_CHUNK", "EXR chunk range exceeds 512 MiB.");
|
|
3553
|
+
let bytes;
|
|
3554
|
+
if (headerBytes) {
|
|
3555
|
+
const payload = await exact(
|
|
3556
|
+
this.source,
|
|
3557
|
+
first.offset + headerSize,
|
|
3558
|
+
size - headerSize,
|
|
3559
|
+
options.signal
|
|
3560
|
+
);
|
|
3561
|
+
const joined = new Uint8Array(size);
|
|
3562
|
+
joined.set(new Uint8Array(headerBytes));
|
|
3563
|
+
joined.set(new Uint8Array(payload), headerSize);
|
|
3564
|
+
bytes = joined.buffer;
|
|
3565
|
+
} else {
|
|
3566
|
+
bytes = await exact(this.source, rangeOffset, size, options.signal);
|
|
3567
|
+
}
|
|
3568
|
+
const view = new DataView(bytes);
|
|
3569
|
+
const band = { ...window, yMin: first.yMin, yMax: batch[batch.length - 1].yMax };
|
|
3570
|
+
const decoder = createScanlineDecoder(part, this.structure.isMultipart, options, band);
|
|
3571
|
+
for (const chunk of batch) {
|
|
3572
|
+
const offset = chunk.offset - rangeOffset;
|
|
3573
|
+
const y = view.getInt32(offset + headerSize - 8, true);
|
|
3574
|
+
const length = view.getInt32(offset + headerSize - 4, true);
|
|
3575
|
+
if (y !== chunk.yMin || length < 0 || length + headerSize > (chunk.byteLength ?? size) || this.structure.isMultipart && view.getInt32(offset, true) !== part.id) {
|
|
3576
|
+
throw new ExrError("MALFORMED_CHUNK", "EXR chunk does not match its index.");
|
|
3577
|
+
}
|
|
3578
|
+
decoder.decodeChunk(bytes, view, offset, chunk.index);
|
|
3579
|
+
}
|
|
3580
|
+
remaining.splice(start, batch.length);
|
|
3581
|
+
yield { ...decoder.finish(), window: band };
|
|
3582
|
+
}
|
|
3583
|
+
}
|
|
3584
|
+
async decodePart(options) {
|
|
3585
|
+
const decoded = await this.decodeRegion({
|
|
3586
|
+
...options,
|
|
3587
|
+
region: this.part(options.partId).dataWindow
|
|
3588
|
+
});
|
|
3589
|
+
return { width: decoded.width, height: decoded.height, channels: decoded.channels };
|
|
3590
|
+
}
|
|
3591
|
+
/** Return sampled-native planes cropped to the intersection with the data window. */
|
|
3592
|
+
async decodeRegion(options) {
|
|
3593
|
+
const part = this.part(options.partId);
|
|
3594
|
+
const window = intersect(part.dataWindow, options.region);
|
|
3595
|
+
const channels = {};
|
|
3596
|
+
const first = (min, sampling) => Math.ceil(min / sampling) * sampling || 0;
|
|
3597
|
+
for await (const band of this.decodeChunks({ ...options, region: window })) {
|
|
3598
|
+
for (const [name, src] of Object.entries(band.channels)) {
|
|
3599
|
+
const xs = Math.max(1, src.xSampling), ys = Math.max(1, src.ySampling);
|
|
3600
|
+
const x = first(window.xMin, xs), y = first(window.yMin, ys);
|
|
3601
|
+
const width = Math.max(0, Math.floor((window.xMax - x) / xs) + 1);
|
|
3602
|
+
const height = Math.max(0, Math.floor((window.yMax - y) / ys) + 1);
|
|
3603
|
+
const dst = channels[name] ??= {
|
|
3604
|
+
...src,
|
|
3605
|
+
sampleOriginX: x,
|
|
3606
|
+
sampleOriginY: y,
|
|
3607
|
+
sampledWidth: width,
|
|
3608
|
+
sampledHeight: height,
|
|
3609
|
+
data: new Float32Array(width * height)
|
|
3610
|
+
};
|
|
3611
|
+
if (!width || !height) continue;
|
|
3612
|
+
const startY = Math.max(y, src.sampleOriginY);
|
|
3613
|
+
const endY = Math.min(window.yMax, band.window.yMax);
|
|
3614
|
+
const startX = (x - src.sampleOriginX) / xs;
|
|
3615
|
+
for (let row = startY; row <= endY; row += ys) {
|
|
3616
|
+
const srcOffset = (row - src.sampleOriginY) / ys * src.sampledWidth + startX;
|
|
3617
|
+
dst.data.set(src.data.subarray(srcOffset, srcOffset + width), (row - y) / ys * width);
|
|
3618
|
+
}
|
|
3619
|
+
}
|
|
3620
|
+
}
|
|
3621
|
+
return {
|
|
3622
|
+
window,
|
|
3623
|
+
width: window.xMax - window.xMin + 1,
|
|
3624
|
+
height: window.yMax - window.yMin + 1,
|
|
3625
|
+
channels
|
|
3626
|
+
};
|
|
3627
|
+
}
|
|
3628
|
+
};
|
|
3118
3629
|
|
|
3119
3630
|
// src/reader/readExr.ts
|
|
3120
3631
|
function readExr(buffer, options = {}) {
|
|
@@ -3184,21 +3695,6 @@ function inspectExrImage(buffer, options = {}) {
|
|
|
3184
3695
|
}
|
|
3185
3696
|
|
|
3186
3697
|
// src/reader/expandSampledChannels.ts
|
|
3187
|
-
function clamp(value, min, max) {
|
|
3188
|
-
if (value < min) return min;
|
|
3189
|
-
if (value > max) return max;
|
|
3190
|
-
return value;
|
|
3191
|
-
}
|
|
3192
|
-
function buildNearestSampleMap(length, worldMin, sampleOrigin, sampling, sampledLength) {
|
|
3193
|
-
const output = new Int32Array(length);
|
|
3194
|
-
if (sampledLength <= 0) return output;
|
|
3195
|
-
for (let i = 0; i < length; i++) {
|
|
3196
|
-
const world = worldMin + i;
|
|
3197
|
-
const sample = Math.round((world - sampleOrigin) / sampling);
|
|
3198
|
-
output[i] = clamp(sample, 0, sampledLength - 1);
|
|
3199
|
-
}
|
|
3200
|
-
return output;
|
|
3201
|
-
}
|
|
3202
3698
|
function expandSampledChannel(width, height, xMin, yMin, channel) {
|
|
3203
3699
|
if (channel.sampledWidth === 0 || channel.sampledHeight === 0 || channel.data.length === 0) {
|
|
3204
3700
|
return new Float32Array(width * height);
|
|
@@ -3247,54 +3743,11 @@ function expandDecodedPartChannels(decoded, dataWindow) {
|
|
|
3247
3743
|
return output;
|
|
3248
3744
|
}
|
|
3249
3745
|
|
|
3250
|
-
// src/reader/rgbaChannels.ts
|
|
3251
|
-
function normalizeChannelName(name) {
|
|
3252
|
-
return name.split(".").pop()?.toUpperCase() ?? "";
|
|
3253
|
-
}
|
|
3254
|
-
function pickChannel(channels, aliases) {
|
|
3255
|
-
for (const alias of aliases) {
|
|
3256
|
-
const exact = channels[alias];
|
|
3257
|
-
if (exact) return exact;
|
|
3258
|
-
}
|
|
3259
|
-
const normalizedAliases = new Set(aliases.map((alias) => alias.toUpperCase()));
|
|
3260
|
-
for (const [name, channel] of Object.entries(channels)) {
|
|
3261
|
-
if (normalizedAliases.has(normalizeChannelName(name))) return channel;
|
|
3262
|
-
}
|
|
3263
|
-
return void 0;
|
|
3264
|
-
}
|
|
3265
|
-
function decodedPartToRgba(width, height, dataWindow, channels) {
|
|
3266
|
-
const expanded = expandDecodedPartChannels({ width, height, channels }, dataWindow);
|
|
3267
|
-
const fallback = Object.values(expanded)[0] ?? new Float32Array(width * height);
|
|
3268
|
-
const red = pickChannel(expanded, ["R", "RED"]) ?? fallback;
|
|
3269
|
-
const luma = pickChannel(expanded, ["Y", "LUMA", "LUMINANCE"]);
|
|
3270
|
-
const green = pickChannel(expanded, ["G", "GREEN"]) ?? luma ?? red;
|
|
3271
|
-
const blue = pickChannel(expanded, ["B", "BLUE"]) ?? luma ?? red;
|
|
3272
|
-
const alpha = pickChannel(expanded, ["A", "ALPHA"]);
|
|
3273
|
-
const pixelCount = width * height;
|
|
3274
|
-
const rgba = new Float32Array(pixelCount * 4);
|
|
3275
|
-
for (let index = 0; index < pixelCount; index += 1) {
|
|
3276
|
-
const offset = index * 4;
|
|
3277
|
-
rgba[offset] = Number.isFinite(red[index]) ? red[index] : 0;
|
|
3278
|
-
rgba[offset + 1] = Number.isFinite(green[index]) ? green[index] : rgba[offset];
|
|
3279
|
-
rgba[offset + 2] = Number.isFinite(blue[index]) ? blue[index] : rgba[offset];
|
|
3280
|
-
rgba[offset + 3] = alpha ? Math.max(0, alpha[index]) : 1;
|
|
3281
|
-
}
|
|
3282
|
-
return rgba;
|
|
3283
|
-
}
|
|
3284
|
-
|
|
3285
3746
|
// src/reader/decodeExrRgba.ts
|
|
3286
|
-
function
|
|
3287
|
-
const info = inspectExrImage(buffer, options);
|
|
3288
|
-
const decoded = decodeExrPart(buffer, info.structure, {
|
|
3289
|
-
partId: info.partId,
|
|
3290
|
-
predecodedZipBlocks: options.decode?.predecodedZipBlocks,
|
|
3291
|
-
predecodedDwaBlocks: options.decode?.predecodedDwaBlocks,
|
|
3292
|
-
onEvent: options.onEvent
|
|
3293
|
-
});
|
|
3747
|
+
function getExrRgbaImageMetadata(info) {
|
|
3294
3748
|
return {
|
|
3295
3749
|
width: info.width,
|
|
3296
3750
|
height: info.height,
|
|
3297
|
-
rgba: decodedPartToRgba(info.width, info.height, info.dataWindow, decoded.channels),
|
|
3298
3751
|
partId: info.partId,
|
|
3299
3752
|
dataWindow: info.dataWindow,
|
|
3300
3753
|
displayWindow: info.displayWindow,
|
|
@@ -3302,6 +3755,25 @@ function decodeExrRgba(buffer, options = {}) {
|
|
|
3302
3755
|
attributes: info.part.attributes
|
|
3303
3756
|
};
|
|
3304
3757
|
}
|
|
3758
|
+
function describeExrNativeRgba(info, rgba) {
|
|
3759
|
+
const metadata = getExrRgbaImageMetadata(info);
|
|
3760
|
+
return rgba instanceof Uint16Array ? { ...metadata, storagePrecision: "float16", rgba } : { ...metadata, storagePrecision: "float32", rgba };
|
|
3761
|
+
}
|
|
3762
|
+
function getDecodeOptions(info, options) {
|
|
3763
|
+
return { ...options.decode, partId: info.partId, onEvent: options.onEvent };
|
|
3764
|
+
}
|
|
3765
|
+
function decodeExrRgba(buffer, options = {}) {
|
|
3766
|
+
const info = inspectExrImage(buffer, options);
|
|
3767
|
+
const rgba = decodeExrPartRgba(buffer, info.structure, getDecodeOptions(info, options));
|
|
3768
|
+
return { ...getExrRgbaImageMetadata(info), rgba };
|
|
3769
|
+
}
|
|
3770
|
+
function decodeExrNativeRgba(buffer, options = {}) {
|
|
3771
|
+
const info = inspectExrImage(buffer, options);
|
|
3772
|
+
return describeExrNativeRgba(
|
|
3773
|
+
info,
|
|
3774
|
+
decodeExrPartNativeRgba(buffer, info.structure, getDecodeOptions(info, options))
|
|
3775
|
+
);
|
|
3776
|
+
}
|
|
3305
3777
|
|
|
3306
3778
|
// src/writer/sampling.ts
|
|
3307
3779
|
function modulo5(value, base) {
|
|
@@ -4135,7 +4607,7 @@ function floatBitsToFloat24Bits(bits) {
|
|
|
4135
4607
|
const sign = bits & 2147483648;
|
|
4136
4608
|
const exponent = bits & 2139095040;
|
|
4137
4609
|
const mantissa = bits & 8388607;
|
|
4138
|
-
let reduced
|
|
4610
|
+
let reduced;
|
|
4139
4611
|
if (exponent === 2139095040) {
|
|
4140
4612
|
if (mantissa !== 0) {
|
|
4141
4613
|
const shrunkMantissa = mantissa >>> 8;
|
|
@@ -5030,13 +5502,17 @@ function encodeExrRgba(image, options) {
|
|
|
5030
5502
|
export {
|
|
5031
5503
|
EXR_EVENT_CODES,
|
|
5032
5504
|
ExrError,
|
|
5505
|
+
bufferExrSource,
|
|
5506
|
+
decodeExrNativeRgba,
|
|
5033
5507
|
decodeExrPart,
|
|
5034
5508
|
decodeExrRgba,
|
|
5035
5509
|
encodeExrRgba,
|
|
5036
5510
|
expandDecodedPartChannels,
|
|
5037
5511
|
expandSampledChannel,
|
|
5512
|
+
getExrRgbaStoragePrecision,
|
|
5038
5513
|
inspectExrImage,
|
|
5039
5514
|
isExrError,
|
|
5515
|
+
openExr,
|
|
5040
5516
|
parseExr,
|
|
5041
5517
|
parseExrStructure,
|
|
5042
5518
|
readExr,
|