@bb-studio/exr 0.1.2 → 0.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/README.md +34 -3
- package/dist/browser/dwaDecodeWorker.js.map +1 -1
- package/dist/browser/index.d.ts +21 -1
- package/dist/browser/index.js +467 -6
- package/dist/browser/index.js.map +1 -1
- package/dist/index.cjs +300 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +62 -2
- package/dist/index.d.ts +62 -2
- package/dist/index.js +295 -5
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/browser/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/shared/constants.ts
|
|
2
|
+
var EXR_MAGIC = 20000630;
|
|
2
3
|
var COMPRESSION_NAMES = {
|
|
3
4
|
0: "NO_COMPRESSION",
|
|
4
5
|
1: "RLE_COMPRESSION",
|
|
@@ -2389,7 +2390,7 @@ function toNumberOffset(low, high) {
|
|
|
2389
2390
|
}
|
|
2390
2391
|
return Number(combined);
|
|
2391
2392
|
}
|
|
2392
|
-
function buildChannelMeta(part) {
|
|
2393
|
+
function buildChannelMeta(part, selectedChannelNames) {
|
|
2393
2394
|
if (!part.dataWindow) return [];
|
|
2394
2395
|
const { xMin, xMax, yMin, yMax } = part.dataWindow;
|
|
2395
2396
|
return part.channels.map((channel) => {
|
|
@@ -2400,8 +2401,10 @@ function buildChannelMeta(part) {
|
|
|
2400
2401
|
const sampledWidth = countSamplesInRange4(xMin, xMax, xSampling);
|
|
2401
2402
|
const sampledHeight = countSamplesInRange4(yMin, yMax, ySampling);
|
|
2402
2403
|
const bytesPerSample = channel.pixelType === 1 ? 2 : 4;
|
|
2404
|
+
const selected = selectedChannelNames?.has(channel.name) ?? true;
|
|
2403
2405
|
return {
|
|
2404
2406
|
channel,
|
|
2407
|
+
selected,
|
|
2405
2408
|
pixelType: channel.pixelType,
|
|
2406
2409
|
ySampling,
|
|
2407
2410
|
bytesPerSample,
|
|
@@ -2410,7 +2413,7 @@ function buildChannelMeta(part) {
|
|
|
2410
2413
|
sampledHeight,
|
|
2411
2414
|
sampleOriginX,
|
|
2412
2415
|
sampleOriginY,
|
|
2413
|
-
data: new Float32Array(sampledWidth * sampledHeight)
|
|
2416
|
+
data: selected ? new Float32Array(sampledWidth * sampledHeight) : new Float32Array(0)
|
|
2414
2417
|
};
|
|
2415
2418
|
});
|
|
2416
2419
|
}
|
|
@@ -2562,7 +2565,18 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
2562
2565
|
ms: (nowMs() - tOffsets).toFixed(3)
|
|
2563
2566
|
}
|
|
2564
2567
|
});
|
|
2565
|
-
const
|
|
2568
|
+
const selectedChannelNames = options.channelNames ? new Set(options.channelNames) : void 0;
|
|
2569
|
+
if (selectedChannelNames) {
|
|
2570
|
+
for (const name of selectedChannelNames) {
|
|
2571
|
+
if (!part.channels.some((channel) => channel.name === name)) {
|
|
2572
|
+
throw new ExrError("CHANNEL_NOT_FOUND", `Channel ${name} was not found in EXR part.`, {
|
|
2573
|
+
partId: part.id,
|
|
2574
|
+
channel: name
|
|
2575
|
+
});
|
|
2576
|
+
}
|
|
2577
|
+
}
|
|
2578
|
+
}
|
|
2579
|
+
const channelMeta = buildChannelMeta(part, selectedChannelNames);
|
|
2566
2580
|
const zipScratch = {
|
|
2567
2581
|
inflate: new Uint8Array(0),
|
|
2568
2582
|
output: new Uint8Array(0)
|
|
@@ -2669,8 +2683,10 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
2669
2683
|
blockPointer += byteLength;
|
|
2670
2684
|
continue;
|
|
2671
2685
|
}
|
|
2672
|
-
|
|
2673
|
-
|
|
2686
|
+
if (meta.selected) {
|
|
2687
|
+
const rowOffset = sampledRow * meta.sampledWidth;
|
|
2688
|
+
decodeRowIntoChannel(meta, blockData, blockPointer, rowOffset, blockView);
|
|
2689
|
+
}
|
|
2674
2690
|
blockPointer += byteLength;
|
|
2675
2691
|
}
|
|
2676
2692
|
}
|
|
@@ -2690,6 +2706,7 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
2690
2706
|
}
|
|
2691
2707
|
const decodedChannels = {};
|
|
2692
2708
|
for (const meta of channelMeta) {
|
|
2709
|
+
if (!meta.selected) continue;
|
|
2693
2710
|
decodedChannels[meta.channel.name] = {
|
|
2694
2711
|
pixelType: meta.channel.pixelType,
|
|
2695
2712
|
xSampling: meta.channel.xSampling,
|
|
@@ -3093,13 +3110,14 @@ async function decodeExrPartWithWorkers(bufferInput, structure, options) {
|
|
|
3093
3110
|
const mergedDwaBlocks = mergePredecodedMaps(options.predecodedDwaBlocks, predecodedDwaBlocks);
|
|
3094
3111
|
return decodeExrPart(buffer, structure, {
|
|
3095
3112
|
partId: options.partId,
|
|
3113
|
+
channelNames: options.channelNames,
|
|
3096
3114
|
onEvent: options.onEvent,
|
|
3097
3115
|
predecodedZipBlocks: mergedZipBlocks,
|
|
3098
3116
|
predecodedDwaBlocks: mergedDwaBlocks
|
|
3099
3117
|
});
|
|
3100
3118
|
}
|
|
3101
3119
|
|
|
3102
|
-
// src/
|
|
3120
|
+
// src/reader/expandSampledChannels.ts
|
|
3103
3121
|
function clamp2(value, min, max) {
|
|
3104
3122
|
if (value < min) return min;
|
|
3105
3123
|
if (value > max) return max;
|
|
@@ -3162,8 +3180,451 @@ function expandDecodedPartChannels(decoded, dataWindow) {
|
|
|
3162
3180
|
}
|
|
3163
3181
|
return output;
|
|
3164
3182
|
}
|
|
3183
|
+
|
|
3184
|
+
// src/reader/rgbaChannels.ts
|
|
3185
|
+
function normalizeChannelName(name) {
|
|
3186
|
+
return name.split(".").pop()?.toUpperCase() ?? "";
|
|
3187
|
+
}
|
|
3188
|
+
function pickChannel(channels, aliases) {
|
|
3189
|
+
for (const alias of aliases) {
|
|
3190
|
+
const exact = channels[alias];
|
|
3191
|
+
if (exact) return exact;
|
|
3192
|
+
}
|
|
3193
|
+
const normalizedAliases = new Set(aliases.map((alias) => alias.toUpperCase()));
|
|
3194
|
+
for (const [name, channel] of Object.entries(channels)) {
|
|
3195
|
+
if (normalizedAliases.has(normalizeChannelName(name))) return channel;
|
|
3196
|
+
}
|
|
3197
|
+
return void 0;
|
|
3198
|
+
}
|
|
3199
|
+
function decodedPartToRgba(width, height, dataWindow, channels) {
|
|
3200
|
+
const expanded = expandDecodedPartChannels({ width, height, channels }, dataWindow);
|
|
3201
|
+
const fallback = Object.values(expanded)[0] ?? new Float32Array(width * height);
|
|
3202
|
+
const red = pickChannel(expanded, ["R", "RED"]) ?? fallback;
|
|
3203
|
+
const luma = pickChannel(expanded, ["Y", "LUMA", "LUMINANCE"]);
|
|
3204
|
+
const green = pickChannel(expanded, ["G", "GREEN"]) ?? luma ?? red;
|
|
3205
|
+
const blue = pickChannel(expanded, ["B", "BLUE"]) ?? luma ?? red;
|
|
3206
|
+
const alpha = pickChannel(expanded, ["A", "ALPHA"]);
|
|
3207
|
+
const pixelCount = width * height;
|
|
3208
|
+
const rgba = new Float32Array(pixelCount * 4);
|
|
3209
|
+
for (let index = 0; index < pixelCount; index += 1) {
|
|
3210
|
+
const offset = index * 4;
|
|
3211
|
+
rgba[offset] = Number.isFinite(red[index]) ? red[index] : 0;
|
|
3212
|
+
rgba[offset + 1] = Number.isFinite(green[index]) ? green[index] : rgba[offset];
|
|
3213
|
+
rgba[offset + 2] = Number.isFinite(blue[index]) ? blue[index] : rgba[offset];
|
|
3214
|
+
rgba[offset + 3] = alpha ? Math.max(0, alpha[index]) : 1;
|
|
3215
|
+
}
|
|
3216
|
+
return rgba;
|
|
3217
|
+
}
|
|
3218
|
+
|
|
3219
|
+
// src/reader/parseExrStructure.ts
|
|
3220
|
+
function nowMs2() {
|
|
3221
|
+
return typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
3222
|
+
}
|
|
3223
|
+
var BinaryReader = class {
|
|
3224
|
+
constructor(buffer) {
|
|
3225
|
+
this.buffer = buffer;
|
|
3226
|
+
this.view = new DataView(buffer);
|
|
3227
|
+
}
|
|
3228
|
+
view;
|
|
3229
|
+
offset = 0;
|
|
3230
|
+
get byteLength() {
|
|
3231
|
+
return this.view.byteLength;
|
|
3232
|
+
}
|
|
3233
|
+
ensureAvailable(count, code, context) {
|
|
3234
|
+
if (count < 0 || this.offset + count > this.view.byteLength) {
|
|
3235
|
+
throw new ExrError("TRUNCATED_FILE", `Unexpected EOF while reading ${context}.`, {
|
|
3236
|
+
code,
|
|
3237
|
+
offset: this.offset,
|
|
3238
|
+
requested: count,
|
|
3239
|
+
size: this.view.byteLength
|
|
3240
|
+
});
|
|
3241
|
+
}
|
|
3242
|
+
}
|
|
3243
|
+
peekUint8() {
|
|
3244
|
+
this.ensureAvailable(1, "peek_u8", "byte");
|
|
3245
|
+
return this.view.getUint8(this.offset);
|
|
3246
|
+
}
|
|
3247
|
+
readUint8() {
|
|
3248
|
+
this.ensureAvailable(1, "u8", "byte");
|
|
3249
|
+
const value = this.view.getUint8(this.offset);
|
|
3250
|
+
this.offset += 1;
|
|
3251
|
+
return value;
|
|
3252
|
+
}
|
|
3253
|
+
readInt32() {
|
|
3254
|
+
this.ensureAvailable(4, "i32", "int32");
|
|
3255
|
+
const value = this.view.getInt32(this.offset, true);
|
|
3256
|
+
this.offset += 4;
|
|
3257
|
+
return value;
|
|
3258
|
+
}
|
|
3259
|
+
readUint32() {
|
|
3260
|
+
this.ensureAvailable(4, "u32", "uint32");
|
|
3261
|
+
const value = this.view.getUint32(this.offset, true);
|
|
3262
|
+
this.offset += 4;
|
|
3263
|
+
return value;
|
|
3264
|
+
}
|
|
3265
|
+
readFloat32() {
|
|
3266
|
+
this.ensureAvailable(4, "f32", "float32");
|
|
3267
|
+
const value = this.view.getFloat32(this.offset, true);
|
|
3268
|
+
this.offset += 4;
|
|
3269
|
+
return value;
|
|
3270
|
+
}
|
|
3271
|
+
readCString(context) {
|
|
3272
|
+
let value = "";
|
|
3273
|
+
while (this.offset < this.view.byteLength) {
|
|
3274
|
+
const ch = this.readUint8();
|
|
3275
|
+
if (ch === 0) return value;
|
|
3276
|
+
value += String.fromCharCode(ch);
|
|
3277
|
+
}
|
|
3278
|
+
throw new ExrError("TRUNCATED_FILE", `Unterminated string while reading ${context}.`, {
|
|
3279
|
+
offset: this.offset
|
|
3280
|
+
});
|
|
3281
|
+
}
|
|
3282
|
+
readCStringWithin(limit, context) {
|
|
3283
|
+
if (limit < this.offset || limit > this.view.byteLength) {
|
|
3284
|
+
throw new ExrError("MALFORMED_HEADER", `Invalid bounded read while parsing ${context}.`, {
|
|
3285
|
+
offset: this.offset,
|
|
3286
|
+
limit
|
|
3287
|
+
});
|
|
3288
|
+
}
|
|
3289
|
+
let value = "";
|
|
3290
|
+
while (this.offset < limit) {
|
|
3291
|
+
const ch = this.readUint8();
|
|
3292
|
+
if (ch === 0) return value;
|
|
3293
|
+
value += String.fromCharCode(ch);
|
|
3294
|
+
}
|
|
3295
|
+
throw new ExrError("MALFORMED_HEADER", `Unterminated string while reading ${context}.`, {
|
|
3296
|
+
offset: this.offset,
|
|
3297
|
+
limit
|
|
3298
|
+
});
|
|
3299
|
+
}
|
|
3300
|
+
skip(count, context) {
|
|
3301
|
+
this.ensureAvailable(count, "skip", context);
|
|
3302
|
+
this.offset += count;
|
|
3303
|
+
}
|
|
3304
|
+
};
|
|
3305
|
+
function emit2(onEvent, event) {
|
|
3306
|
+
onEvent?.(event);
|
|
3307
|
+
}
|
|
3308
|
+
function ensureExactAttributeSize(name, type, size, expected) {
|
|
3309
|
+
if (size !== expected) {
|
|
3310
|
+
throw new ExrError("MALFORMED_HEADER", `Invalid ${type} attribute size for ${name}.`, {
|
|
3311
|
+
attribute: name,
|
|
3312
|
+
expected,
|
|
3313
|
+
actual: size
|
|
3314
|
+
});
|
|
3315
|
+
}
|
|
3316
|
+
}
|
|
3317
|
+
function parsePartHeader(reader, index) {
|
|
3318
|
+
if (reader.peekUint8() === 0) {
|
|
3319
|
+
return null;
|
|
3320
|
+
}
|
|
3321
|
+
const attributes = {};
|
|
3322
|
+
const channels = [];
|
|
3323
|
+
while (reader.offset < reader.byteLength) {
|
|
3324
|
+
const name = reader.readCString("attribute name");
|
|
3325
|
+
if (name === "") {
|
|
3326
|
+
break;
|
|
3327
|
+
}
|
|
3328
|
+
const type = reader.readCString(`attribute type for ${name}`);
|
|
3329
|
+
const size = reader.readInt32();
|
|
3330
|
+
if (size < 0) {
|
|
3331
|
+
throw new ExrError("MALFORMED_HEADER", `Negative attribute size for ${name}.`, {
|
|
3332
|
+
attribute: name,
|
|
3333
|
+
size
|
|
3334
|
+
});
|
|
3335
|
+
}
|
|
3336
|
+
const valueStart = reader.offset;
|
|
3337
|
+
reader.ensureAvailable(size, "attribute_size", `attribute payload for ${name}`);
|
|
3338
|
+
let value;
|
|
3339
|
+
if (type === "chlist") {
|
|
3340
|
+
const channelEnd = valueStart + size;
|
|
3341
|
+
let sawChannelListTerminator = false;
|
|
3342
|
+
while (reader.offset < channelEnd) {
|
|
3343
|
+
const channelName = reader.readCStringWithin(channelEnd, "channel name");
|
|
3344
|
+
if (channelName === "") {
|
|
3345
|
+
sawChannelListTerminator = true;
|
|
3346
|
+
break;
|
|
3347
|
+
}
|
|
3348
|
+
if (reader.offset + 16 > channelEnd) {
|
|
3349
|
+
throw new ExrError("MALFORMED_HEADER", `Truncated channel record for ${channelName}.`, {
|
|
3350
|
+
attribute: name,
|
|
3351
|
+
channel: channelName,
|
|
3352
|
+
offset: reader.offset,
|
|
3353
|
+
channelEnd
|
|
3354
|
+
});
|
|
3355
|
+
}
|
|
3356
|
+
const pixelType = reader.readInt32();
|
|
3357
|
+
const pLinear = reader.readUint8();
|
|
3358
|
+
reader.skip(3, "channel reserved bytes");
|
|
3359
|
+
const xSampling = reader.readInt32();
|
|
3360
|
+
const ySampling = reader.readInt32();
|
|
3361
|
+
channels.push({
|
|
3362
|
+
name: channelName,
|
|
3363
|
+
pixelType,
|
|
3364
|
+
pLinear,
|
|
3365
|
+
xSampling,
|
|
3366
|
+
ySampling
|
|
3367
|
+
});
|
|
3368
|
+
}
|
|
3369
|
+
if (!sawChannelListTerminator) {
|
|
3370
|
+
throw new ExrError("MALFORMED_HEADER", "Channel list payload is missing terminator.", {
|
|
3371
|
+
attribute: name,
|
|
3372
|
+
size
|
|
3373
|
+
});
|
|
3374
|
+
}
|
|
3375
|
+
if (reader.offset !== channelEnd) {
|
|
3376
|
+
throw new ExrError("MALFORMED_HEADER", "Channel list payload had trailing bytes.", {
|
|
3377
|
+
attribute: name,
|
|
3378
|
+
offset: reader.offset,
|
|
3379
|
+
expectedEnd: channelEnd
|
|
3380
|
+
});
|
|
3381
|
+
}
|
|
3382
|
+
value = `(${channels.length} channels)`;
|
|
3383
|
+
} else if (type === "compression") {
|
|
3384
|
+
ensureExactAttributeSize(name, type, size, 1);
|
|
3385
|
+
value = reader.readUint8();
|
|
3386
|
+
} else if (type === "box2i") {
|
|
3387
|
+
ensureExactAttributeSize(name, type, size, 16);
|
|
3388
|
+
value = {
|
|
3389
|
+
xMin: reader.readInt32(),
|
|
3390
|
+
yMin: reader.readInt32(),
|
|
3391
|
+
xMax: reader.readInt32(),
|
|
3392
|
+
yMax: reader.readInt32()
|
|
3393
|
+
};
|
|
3394
|
+
} else if (type === "string") {
|
|
3395
|
+
let stringValue = "";
|
|
3396
|
+
for (let i = 0; i < size; i++) {
|
|
3397
|
+
const charCode = reader.readUint8();
|
|
3398
|
+
if (charCode === 0) break;
|
|
3399
|
+
stringValue += String.fromCharCode(charCode);
|
|
3400
|
+
}
|
|
3401
|
+
value = stringValue;
|
|
3402
|
+
} else if (type === "int") {
|
|
3403
|
+
ensureExactAttributeSize(name, type, size, 4);
|
|
3404
|
+
value = reader.readInt32();
|
|
3405
|
+
} else if (type === "float") {
|
|
3406
|
+
ensureExactAttributeSize(name, type, size, 4);
|
|
3407
|
+
value = reader.readFloat32();
|
|
3408
|
+
} else if (type === "chromaticities") {
|
|
3409
|
+
ensureExactAttributeSize(name, type, size, 32);
|
|
3410
|
+
value = {
|
|
3411
|
+
redX: reader.readFloat32(),
|
|
3412
|
+
redY: reader.readFloat32(),
|
|
3413
|
+
greenX: reader.readFloat32(),
|
|
3414
|
+
greenY: reader.readFloat32(),
|
|
3415
|
+
blueX: reader.readFloat32(),
|
|
3416
|
+
blueY: reader.readFloat32(),
|
|
3417
|
+
whiteX: reader.readFloat32(),
|
|
3418
|
+
whiteY: reader.readFloat32()
|
|
3419
|
+
};
|
|
3420
|
+
} else {
|
|
3421
|
+
reader.skip(size, `unknown attribute ${name}`);
|
|
3422
|
+
value = `<${type} data>`;
|
|
3423
|
+
}
|
|
3424
|
+
reader.offset = valueStart + size;
|
|
3425
|
+
attributes[name] = value;
|
|
3426
|
+
}
|
|
3427
|
+
return {
|
|
3428
|
+
id: index,
|
|
3429
|
+
attributes,
|
|
3430
|
+
channels,
|
|
3431
|
+
dataWindow: attributes.dataWindow,
|
|
3432
|
+
displayWindow: attributes.displayWindow,
|
|
3433
|
+
compression: attributes.compression,
|
|
3434
|
+
type: attributes.type
|
|
3435
|
+
};
|
|
3436
|
+
}
|
|
3437
|
+
function parseExrStructure(bufferInput, options = {}) {
|
|
3438
|
+
const onEvent = options.onEvent;
|
|
3439
|
+
const t0 = nowMs2();
|
|
3440
|
+
const buffer = toArrayBuffer(bufferInput);
|
|
3441
|
+
if (buffer.byteLength < 8) {
|
|
3442
|
+
throw new ExrError("BUFFER_TOO_SMALL", "Buffer is too small to be an EXR file.", {
|
|
3443
|
+
size: buffer.byteLength
|
|
3444
|
+
});
|
|
3445
|
+
}
|
|
3446
|
+
const reader = new BinaryReader(buffer);
|
|
3447
|
+
const tMagic = nowMs2();
|
|
3448
|
+
const magic = reader.readUint32();
|
|
3449
|
+
if (magic !== EXR_MAGIC) {
|
|
3450
|
+
emit2(onEvent, {
|
|
3451
|
+
phase: "parse",
|
|
3452
|
+
level: "error",
|
|
3453
|
+
code: "parse.magic.invalid",
|
|
3454
|
+
message: "Invalid EXR magic number.",
|
|
3455
|
+
metrics: {
|
|
3456
|
+
expected: `0x${EXR_MAGIC.toString(16)}`,
|
|
3457
|
+
actual: `0x${magic.toString(16)}`
|
|
3458
|
+
}
|
|
3459
|
+
});
|
|
3460
|
+
throw new ExrError("INVALID_MAGIC", "Invalid EXR magic number.", {
|
|
3461
|
+
expected: EXR_MAGIC,
|
|
3462
|
+
actual: magic
|
|
3463
|
+
});
|
|
3464
|
+
}
|
|
3465
|
+
emit2(onEvent, {
|
|
3466
|
+
phase: "parse",
|
|
3467
|
+
level: "info",
|
|
3468
|
+
code: "parse.magic.ok",
|
|
3469
|
+
message: "Parsed EXR magic number.",
|
|
3470
|
+
metrics: {
|
|
3471
|
+
ms: (nowMs2() - tMagic).toFixed(3),
|
|
3472
|
+
magic: `0x${EXR_MAGIC.toString(16)}`
|
|
3473
|
+
}
|
|
3474
|
+
});
|
|
3475
|
+
const tVersion = nowMs2();
|
|
3476
|
+
const versionField = reader.readUint32();
|
|
3477
|
+
const version = versionField & 255;
|
|
3478
|
+
const flags = versionField >> 8;
|
|
3479
|
+
const isMultipart = (flags & 16) !== 0;
|
|
3480
|
+
emit2(onEvent, {
|
|
3481
|
+
phase: "parse",
|
|
3482
|
+
level: "info",
|
|
3483
|
+
code: "parse.version.ok",
|
|
3484
|
+
message: "Parsed EXR version and flags.",
|
|
3485
|
+
metrics: {
|
|
3486
|
+
ms: (nowMs2() - tVersion).toFixed(3),
|
|
3487
|
+
version,
|
|
3488
|
+
flags,
|
|
3489
|
+
multipart: isMultipart ? 1 : 0
|
|
3490
|
+
}
|
|
3491
|
+
});
|
|
3492
|
+
const parts = [];
|
|
3493
|
+
let partIndex = 0;
|
|
3494
|
+
while (reader.offset < reader.byteLength) {
|
|
3495
|
+
const tHeader = nowMs2();
|
|
3496
|
+
const part = parsePartHeader(reader, partIndex);
|
|
3497
|
+
if (!part) {
|
|
3498
|
+
if (isMultipart) {
|
|
3499
|
+
reader.skip(1, "multipart header terminator");
|
|
3500
|
+
}
|
|
3501
|
+
break;
|
|
3502
|
+
}
|
|
3503
|
+
parts.push(part);
|
|
3504
|
+
emit2(onEvent, {
|
|
3505
|
+
phase: "parse",
|
|
3506
|
+
level: "info",
|
|
3507
|
+
code: "parse.part.ok",
|
|
3508
|
+
message: `Parsed part ${part.id} header.`,
|
|
3509
|
+
metrics: {
|
|
3510
|
+
ms: (nowMs2() - tHeader).toFixed(3),
|
|
3511
|
+
partId: part.id,
|
|
3512
|
+
channels: part.channels.length,
|
|
3513
|
+
compression: COMPRESSION_NAMES[part.compression ?? -1] || "UNKNOWN"
|
|
3514
|
+
}
|
|
3515
|
+
});
|
|
3516
|
+
partIndex += 1;
|
|
3517
|
+
if (!isMultipart) {
|
|
3518
|
+
break;
|
|
3519
|
+
}
|
|
3520
|
+
if (reader.offset >= reader.byteLength) {
|
|
3521
|
+
throw new ExrError("TRUNCATED_FILE", "Unexpected EOF while reading multipart headers.", {
|
|
3522
|
+
offset: reader.offset
|
|
3523
|
+
});
|
|
3524
|
+
}
|
|
3525
|
+
if (reader.peekUint8() === 0) {
|
|
3526
|
+
reader.skip(1, "multipart headers terminator");
|
|
3527
|
+
break;
|
|
3528
|
+
}
|
|
3529
|
+
}
|
|
3530
|
+
if (parts.length === 0) {
|
|
3531
|
+
throw new ExrError("MALFORMED_HEADER", "No EXR headers were parsed.", {
|
|
3532
|
+
offset: reader.offset
|
|
3533
|
+
});
|
|
3534
|
+
}
|
|
3535
|
+
const structure = {
|
|
3536
|
+
magic,
|
|
3537
|
+
version,
|
|
3538
|
+
flags,
|
|
3539
|
+
isMultipart,
|
|
3540
|
+
parts,
|
|
3541
|
+
headerEndOffset: reader.offset
|
|
3542
|
+
};
|
|
3543
|
+
emit2(onEvent, {
|
|
3544
|
+
phase: "parse",
|
|
3545
|
+
level: "info",
|
|
3546
|
+
code: "parse.complete",
|
|
3547
|
+
message: "EXR structure parsed.",
|
|
3548
|
+
metrics: {
|
|
3549
|
+
ms: (nowMs2() - t0).toFixed(3),
|
|
3550
|
+
parts: parts.length,
|
|
3551
|
+
headerEndOffset: structure.headerEndOffset
|
|
3552
|
+
}
|
|
3553
|
+
});
|
|
3554
|
+
return structure;
|
|
3555
|
+
}
|
|
3556
|
+
|
|
3557
|
+
// src/reader/exrImage.ts
|
|
3558
|
+
function selectExrImagePart(structure, partId) {
|
|
3559
|
+
if (partId !== void 0) {
|
|
3560
|
+
const part2 = structure.parts.find((candidate) => candidate.id === partId);
|
|
3561
|
+
if (!part2) {
|
|
3562
|
+
throw new ExrError("PART_NOT_FOUND", `Part ${partId} was not found in EXR structure.`, {
|
|
3563
|
+
partId
|
|
3564
|
+
});
|
|
3565
|
+
}
|
|
3566
|
+
if (!part2.dataWindow) {
|
|
3567
|
+
throw new ExrError("MISSING_DATA_WINDOW", `Part ${partId} is missing a data window.`, {
|
|
3568
|
+
partId
|
|
3569
|
+
});
|
|
3570
|
+
}
|
|
3571
|
+
return part2;
|
|
3572
|
+
}
|
|
3573
|
+
const part = structure.parts.find((candidate) => candidate.dataWindow);
|
|
3574
|
+
if (!part?.dataWindow) {
|
|
3575
|
+
throw new ExrError(
|
|
3576
|
+
"MISSING_DATA_WINDOW",
|
|
3577
|
+
"EXR file does not contain a decodable scanline part."
|
|
3578
|
+
);
|
|
3579
|
+
}
|
|
3580
|
+
return part;
|
|
3581
|
+
}
|
|
3582
|
+
function getExrPartDimensions(part) {
|
|
3583
|
+
if (!part.dataWindow) {
|
|
3584
|
+
throw new ExrError("MISSING_DATA_WINDOW", `Part ${part.id} is missing a data window.`, {
|
|
3585
|
+
partId: part.id
|
|
3586
|
+
});
|
|
3587
|
+
}
|
|
3588
|
+
return {
|
|
3589
|
+
width: part.dataWindow.xMax - part.dataWindow.xMin + 1,
|
|
3590
|
+
height: part.dataWindow.yMax - part.dataWindow.yMin + 1
|
|
3591
|
+
};
|
|
3592
|
+
}
|
|
3593
|
+
function inspectExrImage(buffer, options = {}) {
|
|
3594
|
+
const structure = parseExrStructure(buffer, { onEvent: options.onEvent });
|
|
3595
|
+
const part = selectExrImagePart(structure, options.partId);
|
|
3596
|
+
const dimensions = getExrPartDimensions(part);
|
|
3597
|
+
return {
|
|
3598
|
+
structure,
|
|
3599
|
+
part,
|
|
3600
|
+
partId: part.id,
|
|
3601
|
+
...dimensions,
|
|
3602
|
+
dataWindow: part.dataWindow,
|
|
3603
|
+
displayWindow: part.displayWindow
|
|
3604
|
+
};
|
|
3605
|
+
}
|
|
3606
|
+
|
|
3607
|
+
// src/browser/decodeExrRgbaWithWorkers.ts
|
|
3608
|
+
async function decodeExrRgbaWithWorkers(buffer, options = {}) {
|
|
3609
|
+
const info = inspectExrImage(buffer, options);
|
|
3610
|
+
const decoded = await decodeExrPartWithWorkers(buffer, info.structure, {
|
|
3611
|
+
...options,
|
|
3612
|
+
partId: info.partId
|
|
3613
|
+
});
|
|
3614
|
+
return {
|
|
3615
|
+
width: info.width,
|
|
3616
|
+
height: info.height,
|
|
3617
|
+
rgba: decodedPartToRgba(info.width, info.height, info.dataWindow, decoded.channels),
|
|
3618
|
+
partId: info.partId,
|
|
3619
|
+
dataWindow: info.dataWindow,
|
|
3620
|
+
displayWindow: info.displayWindow,
|
|
3621
|
+
channels: info.part.channels,
|
|
3622
|
+
attributes: info.part.attributes
|
|
3623
|
+
};
|
|
3624
|
+
}
|
|
3165
3625
|
export {
|
|
3166
3626
|
decodeExrPartWithWorkers,
|
|
3627
|
+
decodeExrRgbaWithWorkers,
|
|
3167
3628
|
expandDecodedPartChannels,
|
|
3168
3629
|
expandSampledChannel
|
|
3169
3630
|
};
|