@bb-studio/exr 0.3.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/dist/index.js CHANGED
@@ -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;
@@ -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
- const decodedView = new Uint16Array(decoded.buffer, decoded.byteOffset, decodedWords);
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
- const dcView = new Uint16Array(dcBytes.buffer, dcBytes.byteOffset, dcWords);
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
- const decodedRle = decodeRunLength(decoded, header.rleRawSize);
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.");
@@ -2810,7 +2856,9 @@ function buildChannelMeta(part, selectedChannelNames, outputWindow = part.dataWi
2810
2856
  sampledHeight,
2811
2857
  sampleOriginX,
2812
2858
  sampleOriginY,
2813
- data: selected ? new Float32Array(sampledWidth * sampledHeight) : new Float32Array(0)
2859
+ data: new Float32Array(0),
2860
+ stride: 1,
2861
+ lane: 0
2814
2862
  };
2815
2863
  });
2816
2864
  }
@@ -2836,46 +2884,65 @@ function getExpectedUncompressedChunkSize(part, channelMeta, chunkY, linesInChun
2836
2884
  }
2837
2885
  function decodeRowIntoChannel(meta, blockData, blockPointer, rowOffset, blockView) {
2838
2886
  const destination = meta.data;
2887
+ const stride = meta.stride;
2888
+ let target = rowOffset * stride + meta.lane;
2839
2889
  const sampleCount = meta.sampledWidth;
2840
2890
  const sourceByteOffset = blockData.byteOffset + blockPointer;
2841
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
+ }
2842
2904
  if ((sourceByteOffset & 1) === 0) {
2843
2905
  const source = new Uint16Array(blockData.buffer, sourceByteOffset, sampleCount);
2844
- for (let i = 0; i < sampleCount; i++) {
2845
- destination[rowOffset + i] = float16ToFloat32(source[i]);
2906
+ for (let i = 0; i < sampleCount; i++, target += stride) {
2907
+ destination[target] = float16ToFloat32(source[i]);
2846
2908
  }
2847
2909
  return;
2848
2910
  }
2849
- for (let i = 0; i < sampleCount; i++) {
2850
- destination[rowOffset + i] = float16ToFloat32(
2851
- blockView.getUint16(blockPointer + (i << 1), true)
2852
- );
2911
+ for (let i = 0; i < sampleCount; i++, target += stride) {
2912
+ destination[target] = float16ToFloat32(blockView.getUint16(blockPointer + (i << 1), true));
2853
2913
  }
2854
2914
  return;
2855
2915
  }
2916
+ if (destination instanceof Uint16Array) throw new Error("Non-HALF channel in native HALF output");
2856
2917
  if (meta.pixelType === 2) {
2857
2918
  if ((sourceByteOffset & 3) === 0) {
2858
2919
  const source = new Float32Array(blockData.buffer, sourceByteOffset, sampleCount);
2859
- destination.set(source, rowOffset);
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
+ }
2860
2927
  return;
2861
2928
  }
2862
- for (let i = 0; i < sampleCount; i++) {
2863
- destination[rowOffset + i] = blockView.getFloat32(blockPointer + (i << 2), true);
2929
+ for (let i = 0; i < sampleCount; i++, target += stride) {
2930
+ destination[target] = blockView.getFloat32(blockPointer + (i << 2), true);
2864
2931
  }
2865
2932
  return;
2866
2933
  }
2867
2934
  if ((sourceByteOffset & 3) === 0) {
2868
2935
  const source = new Uint32Array(blockData.buffer, sourceByteOffset, sampleCount);
2869
- for (let i = 0; i < sampleCount; i++) {
2870
- destination[rowOffset + i] = source[i] * INV_UINT32_MAX;
2936
+ for (let i = 0; i < sampleCount; i++, target += stride) {
2937
+ destination[target] = source[i] * INV_UINT32_MAX;
2871
2938
  }
2872
2939
  return;
2873
2940
  }
2874
- for (let i = 0; i < sampleCount; i++) {
2875
- destination[rowOffset + i] = blockView.getUint32(blockPointer + (i << 2), true) * INV_UINT32_MAX;
2941
+ for (let i = 0; i < sampleCount; i++, target += stride) {
2942
+ destination[target] = blockView.getUint32(blockPointer + (i << 2), true) * INV_UINT32_MAX;
2876
2943
  }
2877
2944
  }
2878
- function createScanlineDecoder(part, isMultipart, options, window) {
2945
+ function createScanlineDecoderWithOutput(part, isMultipart, options, window, createOutput) {
2879
2946
  const onEvent = options.onEvent;
2880
2947
  const compression = part.compression ?? 0;
2881
2948
  const handler = getReadCompressionHandler(compression);
@@ -2900,11 +2967,12 @@ function createScanlineDecoder(part, isMultipart, options, window) {
2900
2967
  }
2901
2968
  }
2902
2969
  const channelMeta = buildChannelMeta(part, selectedChannelNames, window);
2970
+ const finish = createOutput(channelMeta, window);
2903
2971
  const zipScratch = {
2904
2972
  inflate: new Uint8Array(0),
2905
2973
  output: new Uint8Array(0)
2906
2974
  };
2907
- function decodeChunk(buffer, view, chunkOffset, chunkIndex) {
2975
+ function decodeChunk(buffer, view, chunkOffset, chunkIndex, workerBlock) {
2908
2976
  const chunkHeaderSize = isMultipart ? 12 : 8;
2909
2977
  if (chunkOffset < 0 || chunkOffset + chunkHeaderSize > view.byteLength) {
2910
2978
  throw new ExrError("MALFORMED_CHUNK", "Chunk header is outside file bounds.", {
@@ -2913,9 +2981,9 @@ function createScanlineDecoder(part, isMultipart, options, window) {
2913
2981
  chunkOffset
2914
2982
  });
2915
2983
  }
2916
- let chunkY = 0;
2917
- let dataSize = 0;
2918
- let dataPtr = chunkOffset;
2984
+ let chunkY;
2985
+ let dataSize;
2986
+ let dataPtr;
2919
2987
  if (isMultipart) {
2920
2988
  const partNumber = view.getInt32(chunkOffset, true);
2921
2989
  chunkY = view.getInt32(chunkOffset + 4, true);
@@ -2964,7 +3032,7 @@ function createScanlineDecoder(part, isMultipart, options, window) {
2964
3032
  );
2965
3033
  const predecodedZipBlock = options.predecodedZipBlocks?.get(chunkIndex);
2966
3034
  const predecodedDwaBlock = options.predecodedDwaBlocks?.get(chunkIndex);
2967
- const predecodedBlock = predecodedZipBlock ?? predecodedDwaBlock;
3035
+ const predecodedBlock = workerBlock ?? predecodedZipBlock ?? predecodedDwaBlock;
2968
3036
  const blockData = predecodedBlock ? predecodedBlock : dataSize === expectedUncompressedSize ? new Uint8Array(buffer, dataPtr, dataSize) : compressionHandler.decodeBlock({
2969
3037
  buffer,
2970
3038
  dataPtr,
@@ -3030,30 +3098,126 @@ function createScanlineDecoder(part, isMultipart, options, window) {
3030
3098
  }
3031
3099
  return dataSize;
3032
3100
  }
3033
- function finish() {
3034
- const decodedChannels = {};
3035
- for (const meta of channelMeta) {
3036
- if (!meta.selected) continue;
3037
- decodedChannels[meta.channel.name] = {
3038
- pixelType: meta.channel.pixelType,
3039
- xSampling: meta.channel.xSampling,
3040
- ySampling: meta.channel.ySampling,
3041
- sampledWidth: meta.sampledWidth,
3042
- sampledHeight: meta.sampledHeight,
3043
- sampleOriginX: meta.sampleOriginX,
3044
- sampleOriginY: meta.sampleOriginY,
3045
- data: meta.data
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
3046
3129
  };
3047
3130
  }
3048
3131
  return {
3049
3132
  width: window.xMax - window.xMin + 1,
3050
3133
  height: window.yMax - window.yMin + 1,
3051
- channels: decodedChannels
3134
+ channels
3052
3135
  };
3053
- }
3054
- return { decodeChunk, finish };
3136
+ };
3055
3137
  }
3056
- function decodeExrPart(bufferInput, structure, options) {
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) {
3057
3221
  const buffer = toArrayBuffer(bufferInput);
3058
3222
  const onEvent = options.onEvent;
3059
3223
  const t0 = nowMs2();
@@ -3124,27 +3288,95 @@ function decodeExrPart(bufferInput, structure, options) {
3124
3288
  ms: (nowMs2() - tOffsets).toFixed(3)
3125
3289
  }
3126
3290
  });
3127
- const decoder = createScanlineDecoder(part, structure.isMultipart, options, part.dataWindow);
3291
+ const decoder = createScanlineDecoderWithOutput(
3292
+ part,
3293
+ structure.isMultipart,
3294
+ options,
3295
+ part.dataWindow,
3296
+ createOutput
3297
+ );
3128
3298
  const tDecode = nowMs2();
3129
3299
  let bytesRead = 0;
3130
- for (let chunkIndex = 0; chunkIndex < chunkOffsets.length; chunkIndex++) {
3131
- bytesRead += decoder.decodeChunk(buffer, view, chunkOffsets[chunkIndex], chunkIndex);
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;
3132
3343
  }
3133
- const result = decoder.finish();
3134
- emit2(onEvent, {
3135
- phase: "decode",
3136
- level: "info",
3137
- code: "decode.complete",
3138
- message: `Decoded part ${part.id}.`,
3139
- metrics: {
3140
- partId: part.id,
3141
- channels: Object.keys(result.channels).length,
3142
- readKB: (bytesRead / 1024).toFixed(1),
3143
- ms: (nowMs2() - tDecode).toFixed(3),
3144
- totalMs: (nowMs2() - t0).toFixed(3)
3145
- }
3146
- });
3147
- return result;
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));
3148
3380
  }
3149
3381
 
3150
3382
  // src/reader/openExr.ts
@@ -3463,21 +3695,6 @@ function inspectExrImage(buffer, options = {}) {
3463
3695
  }
3464
3696
 
3465
3697
  // src/reader/expandSampledChannels.ts
3466
- function clamp(value, min, max) {
3467
- if (value < min) return min;
3468
- if (value > max) return max;
3469
- return value;
3470
- }
3471
- function buildNearestSampleMap(length, worldMin, sampleOrigin, sampling, sampledLength) {
3472
- const output = new Int32Array(length);
3473
- if (sampledLength <= 0) return output;
3474
- for (let i = 0; i < length; i++) {
3475
- const world = worldMin + i;
3476
- const sample = Math.round((world - sampleOrigin) / sampling);
3477
- output[i] = clamp(sample, 0, sampledLength - 1);
3478
- }
3479
- return output;
3480
- }
3481
3698
  function expandSampledChannel(width, height, xMin, yMin, channel) {
3482
3699
  if (channel.sampledWidth === 0 || channel.sampledHeight === 0 || channel.data.length === 0) {
3483
3700
  return new Float32Array(width * height);
@@ -3526,54 +3743,11 @@ function expandDecodedPartChannels(decoded, dataWindow) {
3526
3743
  return output;
3527
3744
  }
3528
3745
 
3529
- // src/reader/rgbaChannels.ts
3530
- function normalizeChannelName(name) {
3531
- return name.split(".").pop()?.toUpperCase() ?? "";
3532
- }
3533
- function pickChannel(channels, aliases) {
3534
- for (const alias of aliases) {
3535
- const exact2 = channels[alias];
3536
- if (exact2) return exact2;
3537
- }
3538
- const normalizedAliases = new Set(aliases.map((alias) => alias.toUpperCase()));
3539
- for (const [name, channel] of Object.entries(channels)) {
3540
- if (normalizedAliases.has(normalizeChannelName(name))) return channel;
3541
- }
3542
- return void 0;
3543
- }
3544
- function decodedPartToRgba(width, height, dataWindow, channels) {
3545
- const expanded = expandDecodedPartChannels({ width, height, channels }, dataWindow);
3546
- const fallback = Object.values(expanded)[0] ?? new Float32Array(width * height);
3547
- const red = pickChannel(expanded, ["R", "RED"]) ?? fallback;
3548
- const luma = pickChannel(expanded, ["Y", "LUMA", "LUMINANCE"]);
3549
- const green = pickChannel(expanded, ["G", "GREEN"]) ?? luma ?? red;
3550
- const blue = pickChannel(expanded, ["B", "BLUE"]) ?? luma ?? red;
3551
- const alpha = pickChannel(expanded, ["A", "ALPHA"]);
3552
- const pixelCount = width * height;
3553
- const rgba = new Float32Array(pixelCount * 4);
3554
- for (let index = 0; index < pixelCount; index += 1) {
3555
- const offset = index * 4;
3556
- rgba[offset] = Number.isFinite(red[index]) ? red[index] : 0;
3557
- rgba[offset + 1] = Number.isFinite(green[index]) ? green[index] : rgba[offset];
3558
- rgba[offset + 2] = Number.isFinite(blue[index]) ? blue[index] : rgba[offset];
3559
- rgba[offset + 3] = alpha ? Math.max(0, alpha[index]) : 1;
3560
- }
3561
- return rgba;
3562
- }
3563
-
3564
3746
  // src/reader/decodeExrRgba.ts
3565
- function decodeExrRgba(buffer, options = {}) {
3566
- const info = inspectExrImage(buffer, options);
3567
- const decoded = decodeExrPart(buffer, info.structure, {
3568
- partId: info.partId,
3569
- predecodedZipBlocks: options.decode?.predecodedZipBlocks,
3570
- predecodedDwaBlocks: options.decode?.predecodedDwaBlocks,
3571
- onEvent: options.onEvent
3572
- });
3747
+ function getExrRgbaImageMetadata(info) {
3573
3748
  return {
3574
3749
  width: info.width,
3575
3750
  height: info.height,
3576
- rgba: decodedPartToRgba(info.width, info.height, info.dataWindow, decoded.channels),
3577
3751
  partId: info.partId,
3578
3752
  dataWindow: info.dataWindow,
3579
3753
  displayWindow: info.displayWindow,
@@ -3581,6 +3755,25 @@ function decodeExrRgba(buffer, options = {}) {
3581
3755
  attributes: info.part.attributes
3582
3756
  };
3583
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
+ }
3584
3777
 
3585
3778
  // src/writer/sampling.ts
3586
3779
  function modulo5(value, base) {
@@ -4414,7 +4607,7 @@ function floatBitsToFloat24Bits(bits) {
4414
4607
  const sign = bits & 2147483648;
4415
4608
  const exponent = bits & 2139095040;
4416
4609
  const mantissa = bits & 8388607;
4417
- let reduced = 0;
4610
+ let reduced;
4418
4611
  if (exponent === 2139095040) {
4419
4612
  if (mantissa !== 0) {
4420
4613
  const shrunkMantissa = mantissa >>> 8;
@@ -5310,11 +5503,13 @@ export {
5310
5503
  EXR_EVENT_CODES,
5311
5504
  ExrError,
5312
5505
  bufferExrSource,
5506
+ decodeExrNativeRgba,
5313
5507
  decodeExrPart,
5314
5508
  decodeExrRgba,
5315
5509
  encodeExrRgba,
5316
5510
  expandDecodedPartChannels,
5317
5511
  expandSampledChannel,
5512
+ getExrRgbaStoragePrecision,
5318
5513
  inspectExrImage,
5319
5514
  isExrError,
5320
5515
  openExr,