@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.cjs CHANGED
@@ -23,11 +23,13 @@ __export(index_exports, {
23
23
  EXR_EVENT_CODES: () => EXR_EVENT_CODES,
24
24
  ExrError: () => ExrError,
25
25
  bufferExrSource: () => bufferExrSource,
26
+ decodeExrNativeRgba: () => decodeExrNativeRgba,
26
27
  decodeExrPart: () => decodeExrPart,
27
28
  decodeExrRgba: () => decodeExrRgba,
28
29
  encodeExrRgba: () => encodeExrRgba,
29
30
  expandDecodedPartChannels: () => expandDecodedPartChannels,
30
31
  expandSampledChannel: () => expandSampledChannel,
32
+ getExrRgbaStoragePrecision: () => getExrRgbaStoragePrecision,
31
33
  inspectExrImage: () => inspectExrImage,
32
34
  isExrError: () => isExrError,
33
35
  openExr: () => openExr,
@@ -506,6 +508,58 @@ function float32ToFloat16(value) {
506
508
  return sign | exponent << 10 | mantissa + 4096 >> 13;
507
509
  }
508
510
 
511
+ // src/reader/rgbaChannels.ts
512
+ function normalizeChannelName(name) {
513
+ return name.split(".").pop()?.toUpperCase() ?? "";
514
+ }
515
+ function pickChannel(channels, aliases) {
516
+ for (const alias of aliases) {
517
+ const exact2 = channels[alias];
518
+ if (exact2) return exact2;
519
+ }
520
+ const normalizedAliases = new Set(aliases.map((alias) => alias.toUpperCase()));
521
+ for (const [name, channel] of Object.entries(channels)) {
522
+ if (normalizedAliases.has(normalizeChannelName(name))) return channel;
523
+ }
524
+ return void 0;
525
+ }
526
+ function selectRgbaChannels(channels) {
527
+ const red = pickChannel(channels, ["R", "RED"]) ?? Object.values(channels)[0];
528
+ const luma = pickChannel(channels, ["Y", "LUMA", "LUMINANCE"]);
529
+ return [
530
+ red,
531
+ pickChannel(channels, ["G", "GREEN"]) ?? luma ?? red,
532
+ pickChannel(channels, ["B", "BLUE"]) ?? luma ?? red,
533
+ pickChannel(channels, ["A", "ALPHA"])
534
+ ];
535
+ }
536
+ function getExrRgbaStoragePrecision(channels, channelNames) {
537
+ const names = channelNames && new Set(channelNames);
538
+ const selected = selectRgbaChannels(
539
+ Object.fromEntries(
540
+ channels.filter((channel) => !names || names.has(channel.name)).map((channel) => [channel.name, channel])
541
+ )
542
+ );
543
+ return selected.every((channel) => !channel || channel.pixelType === 1) ? "float16" : "float32";
544
+ }
545
+
546
+ // src/reader/sampleMap.ts
547
+ function clamp(value, min, max) {
548
+ if (value < min) return min;
549
+ if (value > max) return max;
550
+ return value;
551
+ }
552
+ function buildNearestSampleMap(length, worldMin, sampleOrigin, sampling, sampledLength) {
553
+ const output = new Int32Array(length);
554
+ if (sampledLength <= 0) return output;
555
+ for (let i = 0; i < length; i++) {
556
+ const world = worldMin + i;
557
+ const sample = Math.round((world - sampleOrigin) / sampling);
558
+ output[i] = clamp(sample, 0, sampledLength - 1);
559
+ }
560
+ return output;
561
+ }
562
+
509
563
  // src/reader/compression/b44.ts
510
564
  var HALF_VALUES_PER_BLOCK = 16;
511
565
  var B44_BLOCK_SIZE = 14;
@@ -1690,9 +1744,7 @@ function decodeDwaChunk(context) {
1690
1744
  if (decodedWords !== header.totalAcUncompressedCount) {
1691
1745
  throw new Error("Invalid DWA AC DEFLATE payload length.");
1692
1746
  }
1693
- const decodedView = new Uint16Array(decoded.buffer, decoded.byteOffset, decodedWords);
1694
- acBuffer = new Uint16Array(decodedWords);
1695
- acBuffer.set(decodedView);
1747
+ acBuffer = new Uint16Array(decoded.buffer, decoded.byteOffset, decodedWords);
1696
1748
  cursor.value += header.acCompressedSize;
1697
1749
  break;
1698
1750
  }
@@ -1717,9 +1769,7 @@ function decodeDwaChunk(context) {
1717
1769
  if (dcWords !== header.totalDcUncompressedCount) {
1718
1770
  throw new Error("Invalid DWA DC payload length.");
1719
1771
  }
1720
- const dcView = new Uint16Array(dcBytes.buffer, dcBytes.byteOffset, dcWords);
1721
- dcBuffer = new Uint16Array(dcWords);
1722
- dcBuffer.set(dcView);
1772
+ dcBuffer = new Uint16Array(dcBytes.buffer, dcBytes.byteOffset, dcWords);
1723
1773
  cursor.value += header.dcCompressedSize;
1724
1774
  } else if (header.totalDcUncompressedCount !== 0) {
1725
1775
  throw new Error("Invalid DWA DC payload metadata.");
@@ -1735,9 +1785,7 @@ function decodeDwaChunk(context) {
1735
1785
  if (decoded.byteLength !== header.rleUncompressedSize) {
1736
1786
  throw new Error("Invalid DWA RLE intermediate payload length.");
1737
1787
  }
1738
- const decodedRle = decodeRunLength(decoded, header.rleRawSize);
1739
- rleBuffer = new Uint8Array(decodedRle.byteLength);
1740
- rleBuffer.set(decodedRle);
1788
+ rleBuffer = decodeRunLength(decoded, header.rleRawSize);
1741
1789
  cursor.value += header.rleCompressedSize;
1742
1790
  } else if (header.rleCompressedSize !== 0 || header.rleUncompressedSize !== 0) {
1743
1791
  throw new Error("Invalid DWA RLE payload metadata.");
@@ -2851,7 +2899,9 @@ function buildChannelMeta(part, selectedChannelNames, outputWindow = part.dataWi
2851
2899
  sampledHeight,
2852
2900
  sampleOriginX,
2853
2901
  sampleOriginY,
2854
- data: selected ? new Float32Array(sampledWidth * sampledHeight) : new Float32Array(0)
2902
+ data: new Float32Array(0),
2903
+ stride: 1,
2904
+ lane: 0
2855
2905
  };
2856
2906
  });
2857
2907
  }
@@ -2877,46 +2927,65 @@ function getExpectedUncompressedChunkSize(part, channelMeta, chunkY, linesInChun
2877
2927
  }
2878
2928
  function decodeRowIntoChannel(meta, blockData, blockPointer, rowOffset, blockView) {
2879
2929
  const destination = meta.data;
2930
+ const stride = meta.stride;
2931
+ let target = rowOffset * stride + meta.lane;
2880
2932
  const sampleCount = meta.sampledWidth;
2881
2933
  const sourceByteOffset = blockData.byteOffset + blockPointer;
2882
2934
  if (meta.pixelType === 1) {
2935
+ if (destination instanceof Uint16Array) {
2936
+ if ((sourceByteOffset & 1) === 0) {
2937
+ const source = new Uint16Array(blockData.buffer, sourceByteOffset, sampleCount);
2938
+ if (stride === 1) destination.set(source, target);
2939
+ else
2940
+ for (let i = 0; i < sampleCount; i++, target += stride) destination[target] = source[i];
2941
+ } else {
2942
+ for (let i = 0; i < sampleCount; i++, target += stride)
2943
+ destination[target] = blockView.getUint16(blockPointer + (i << 1), true);
2944
+ }
2945
+ return;
2946
+ }
2883
2947
  if ((sourceByteOffset & 1) === 0) {
2884
2948
  const source = new Uint16Array(blockData.buffer, sourceByteOffset, sampleCount);
2885
- for (let i = 0; i < sampleCount; i++) {
2886
- destination[rowOffset + i] = float16ToFloat32(source[i]);
2949
+ for (let i = 0; i < sampleCount; i++, target += stride) {
2950
+ destination[target] = float16ToFloat32(source[i]);
2887
2951
  }
2888
2952
  return;
2889
2953
  }
2890
- for (let i = 0; i < sampleCount; i++) {
2891
- destination[rowOffset + i] = float16ToFloat32(
2892
- blockView.getUint16(blockPointer + (i << 1), true)
2893
- );
2954
+ for (let i = 0; i < sampleCount; i++, target += stride) {
2955
+ destination[target] = float16ToFloat32(blockView.getUint16(blockPointer + (i << 1), true));
2894
2956
  }
2895
2957
  return;
2896
2958
  }
2959
+ if (destination instanceof Uint16Array) throw new Error("Non-HALF channel in native HALF output");
2897
2960
  if (meta.pixelType === 2) {
2898
2961
  if ((sourceByteOffset & 3) === 0) {
2899
2962
  const source = new Float32Array(blockData.buffer, sourceByteOffset, sampleCount);
2900
- destination.set(source, rowOffset);
2963
+ if (stride === 1) {
2964
+ destination.set(source, target);
2965
+ } else {
2966
+ for (let i = 0; i < sampleCount; i++, target += stride) {
2967
+ destination[target] = source[i];
2968
+ }
2969
+ }
2901
2970
  return;
2902
2971
  }
2903
- for (let i = 0; i < sampleCount; i++) {
2904
- destination[rowOffset + i] = blockView.getFloat32(blockPointer + (i << 2), true);
2972
+ for (let i = 0; i < sampleCount; i++, target += stride) {
2973
+ destination[target] = blockView.getFloat32(blockPointer + (i << 2), true);
2905
2974
  }
2906
2975
  return;
2907
2976
  }
2908
2977
  if ((sourceByteOffset & 3) === 0) {
2909
2978
  const source = new Uint32Array(blockData.buffer, sourceByteOffset, sampleCount);
2910
- for (let i = 0; i < sampleCount; i++) {
2911
- destination[rowOffset + i] = source[i] * INV_UINT32_MAX;
2979
+ for (let i = 0; i < sampleCount; i++, target += stride) {
2980
+ destination[target] = source[i] * INV_UINT32_MAX;
2912
2981
  }
2913
2982
  return;
2914
2983
  }
2915
- for (let i = 0; i < sampleCount; i++) {
2916
- destination[rowOffset + i] = blockView.getUint32(blockPointer + (i << 2), true) * INV_UINT32_MAX;
2984
+ for (let i = 0; i < sampleCount; i++, target += stride) {
2985
+ destination[target] = blockView.getUint32(blockPointer + (i << 2), true) * INV_UINT32_MAX;
2917
2986
  }
2918
2987
  }
2919
- function createScanlineDecoder(part, isMultipart, options, window) {
2988
+ function createScanlineDecoderWithOutput(part, isMultipart, options, window, createOutput) {
2920
2989
  const onEvent = options.onEvent;
2921
2990
  const compression = part.compression ?? 0;
2922
2991
  const handler = getReadCompressionHandler(compression);
@@ -2941,11 +3010,12 @@ function createScanlineDecoder(part, isMultipart, options, window) {
2941
3010
  }
2942
3011
  }
2943
3012
  const channelMeta = buildChannelMeta(part, selectedChannelNames, window);
3013
+ const finish = createOutput(channelMeta, window);
2944
3014
  const zipScratch = {
2945
3015
  inflate: new Uint8Array(0),
2946
3016
  output: new Uint8Array(0)
2947
3017
  };
2948
- function decodeChunk(buffer, view, chunkOffset, chunkIndex) {
3018
+ function decodeChunk(buffer, view, chunkOffset, chunkIndex, workerBlock) {
2949
3019
  const chunkHeaderSize = isMultipart ? 12 : 8;
2950
3020
  if (chunkOffset < 0 || chunkOffset + chunkHeaderSize > view.byteLength) {
2951
3021
  throw new ExrError("MALFORMED_CHUNK", "Chunk header is outside file bounds.", {
@@ -2954,9 +3024,9 @@ function createScanlineDecoder(part, isMultipart, options, window) {
2954
3024
  chunkOffset
2955
3025
  });
2956
3026
  }
2957
- let chunkY = 0;
2958
- let dataSize = 0;
2959
- let dataPtr = chunkOffset;
3027
+ let chunkY;
3028
+ let dataSize;
3029
+ let dataPtr;
2960
3030
  if (isMultipart) {
2961
3031
  const partNumber = view.getInt32(chunkOffset, true);
2962
3032
  chunkY = view.getInt32(chunkOffset + 4, true);
@@ -3005,7 +3075,7 @@ function createScanlineDecoder(part, isMultipart, options, window) {
3005
3075
  );
3006
3076
  const predecodedZipBlock = options.predecodedZipBlocks?.get(chunkIndex);
3007
3077
  const predecodedDwaBlock = options.predecodedDwaBlocks?.get(chunkIndex);
3008
- const predecodedBlock = predecodedZipBlock ?? predecodedDwaBlock;
3078
+ const predecodedBlock = workerBlock ?? predecodedZipBlock ?? predecodedDwaBlock;
3009
3079
  const blockData = predecodedBlock ? predecodedBlock : dataSize === expectedUncompressedSize ? new Uint8Array(buffer, dataPtr, dataSize) : compressionHandler.decodeBlock({
3010
3080
  buffer,
3011
3081
  dataPtr,
@@ -3071,30 +3141,126 @@ function createScanlineDecoder(part, isMultipart, options, window) {
3071
3141
  }
3072
3142
  return dataSize;
3073
3143
  }
3074
- function finish() {
3075
- const decodedChannels = {};
3076
- for (const meta of channelMeta) {
3077
- if (!meta.selected) continue;
3078
- decodedChannels[meta.channel.name] = {
3079
- pixelType: meta.channel.pixelType,
3080
- xSampling: meta.channel.xSampling,
3081
- ySampling: meta.channel.ySampling,
3082
- sampledWidth: meta.sampledWidth,
3083
- sampledHeight: meta.sampledHeight,
3084
- sampleOriginX: meta.sampleOriginX,
3085
- sampleOriginY: meta.sampleOriginY,
3086
- data: meta.data
3144
+ return {
3145
+ decodeChunk,
3146
+ finish,
3147
+ getPlanarBytes: () => channelMeta.reduce(
3148
+ (bytes, meta) => bytes + (meta.stride === 1 ? meta.data.byteLength : 0),
3149
+ 0
3150
+ )
3151
+ };
3152
+ }
3153
+ function createPlanarOutput(meta, window) {
3154
+ for (const channel of meta) {
3155
+ if (channel.selected)
3156
+ channel.data = new Float32Array(channel.sampledWidth * channel.sampledHeight);
3157
+ }
3158
+ return () => {
3159
+ const channels = {};
3160
+ for (const entry of meta) {
3161
+ if (!entry.selected) continue;
3162
+ if (!(entry.data instanceof Float32Array)) throw new Error("Invalid planar storage");
3163
+ channels[entry.channel.name] = {
3164
+ pixelType: entry.channel.pixelType,
3165
+ xSampling: entry.channel.xSampling,
3166
+ ySampling: entry.channel.ySampling,
3167
+ sampledWidth: entry.sampledWidth,
3168
+ sampledHeight: entry.sampledHeight,
3169
+ sampleOriginX: entry.sampleOriginX,
3170
+ sampleOriginY: entry.sampleOriginY,
3171
+ data: entry.data
3087
3172
  };
3088
3173
  }
3089
3174
  return {
3090
3175
  width: window.xMax - window.xMin + 1,
3091
3176
  height: window.yMax - window.yMin + 1,
3092
- channels: decodedChannels
3177
+ channels
3093
3178
  };
3094
- }
3095
- return { decodeChunk, finish };
3179
+ };
3096
3180
  }
3097
- function decodeExrPart(bufferInput, structure, options) {
3181
+ function createScanlineDecoder(part, isMultipart, options, window) {
3182
+ return createScanlineDecoderWithOutput(part, isMultipart, options, window, createPlanarOutput);
3183
+ }
3184
+ function createRgbaOutput(meta, window, allocate) {
3185
+ const width = window.xMax - window.xMin + 1;
3186
+ const height = window.yMax - window.yMin + 1;
3187
+ const channels = Object.fromEntries(
3188
+ meta.filter((entry) => entry.selected).map((entry) => [entry.channel.name, entry])
3189
+ );
3190
+ const selected = selectRgbaChannels(channels);
3191
+ const rgba = allocate(width * height * 4);
3192
+ const lanes = selected.map((entry) => entry === void 0 ? -1 : selected.indexOf(entry));
3193
+ const sampled = [];
3194
+ for (const entry of meta) {
3195
+ const lane = selected.indexOf(entry);
3196
+ entry.selected = lane >= 0;
3197
+ if (!entry.selected) continue;
3198
+ if (entry.sampledWidth === width && entry.sampledHeight === height && entry.sampleOriginX === window.xMin && entry.sampleOriginY === window.yMin && entry.channel.xSampling <= 1 && entry.channel.ySampling <= 1) {
3199
+ entry.data = rgba;
3200
+ entry.stride = 4;
3201
+ entry.lane = lane;
3202
+ } else {
3203
+ entry.data = allocate(entry.sampledWidth * entry.sampledHeight);
3204
+ sampled.push({ entry, lane });
3205
+ }
3206
+ }
3207
+ return () => {
3208
+ for (const { entry, lane } of sampled) {
3209
+ if (!entry.data.length) continue;
3210
+ const xMap = buildNearestSampleMap(
3211
+ width,
3212
+ window.xMin,
3213
+ entry.sampleOriginX,
3214
+ Math.max(1, entry.channel.xSampling),
3215
+ entry.sampledWidth
3216
+ );
3217
+ const yMap = buildNearestSampleMap(
3218
+ height,
3219
+ window.yMin,
3220
+ entry.sampleOriginY,
3221
+ Math.max(1, entry.channel.ySampling),
3222
+ entry.sampledHeight
3223
+ );
3224
+ for (let y = 0; y < height; y++) {
3225
+ const row = yMap[y] * entry.sampledWidth;
3226
+ let offset = y * width * 4 + lane;
3227
+ for (let x = 0; x < width; x++, offset += 4) {
3228
+ rgba[offset] = entry.data[row + xMap[x]];
3229
+ }
3230
+ }
3231
+ }
3232
+ const [r, g, b, a] = lanes;
3233
+ if (rgba instanceof Uint16Array) {
3234
+ const finite = (bits) => (bits & 31744) !== 31744;
3235
+ for (let offset = 0; offset < rgba.length; offset += 4) {
3236
+ const rawRed = r < 0 ? 0 : rgba[offset + r];
3237
+ const rawGreen = g < 0 ? rawRed : rgba[offset + g];
3238
+ const rawBlue = b < 0 ? rawRed : rgba[offset + b];
3239
+ const rawAlpha = a < 0 ? 15360 : rgba[offset + a];
3240
+ const red = finite(rawRed) ? rawRed : 0;
3241
+ rgba[offset] = red;
3242
+ rgba[offset + 1] = finite(rawGreen) ? rawGreen : red;
3243
+ rgba[offset + 2] = finite(rawBlue) ? rawBlue : red;
3244
+ const alphaNaN = (rawAlpha & 32767) > 31744;
3245
+ rgba[offset + 3] = alphaNaN ? 32256 : rawAlpha & 32768 ? 0 : rawAlpha;
3246
+ }
3247
+ return rgba;
3248
+ }
3249
+ for (let offset = 0; offset < rgba.length; offset += 4) {
3250
+ const rawRed = r < 0 ? 0 : rgba[offset + r];
3251
+ const rawGreen = g < 0 ? rawRed : rgba[offset + g];
3252
+ const rawBlue = b < 0 ? rawRed : rgba[offset + b];
3253
+ const alpha = a < 0 ? 1 : Math.max(0, rgba[offset + a]);
3254
+ const red = Number.isFinite(rawRed) ? rawRed : 0;
3255
+ rgba[offset] = red;
3256
+ rgba[offset + 1] = Number.isFinite(rawGreen) ? rawGreen : red;
3257
+ rgba[offset + 2] = Number.isFinite(rawBlue) ? rawBlue : red;
3258
+ rgba[offset + 3] = alpha;
3259
+ }
3260
+ return rgba;
3261
+ };
3262
+ }
3263
+ function preparePartWithOutput(bufferInput, structure, options, createOutput) {
3098
3264
  const buffer = toArrayBuffer(bufferInput);
3099
3265
  const onEvent = options.onEvent;
3100
3266
  const t0 = nowMs2();
@@ -3165,27 +3331,95 @@ function decodeExrPart(bufferInput, structure, options) {
3165
3331
  ms: (nowMs2() - tOffsets).toFixed(3)
3166
3332
  }
3167
3333
  });
3168
- const decoder = createScanlineDecoder(part, structure.isMultipart, options, part.dataWindow);
3334
+ const decoder = createScanlineDecoderWithOutput(
3335
+ part,
3336
+ structure.isMultipart,
3337
+ options,
3338
+ part.dataWindow,
3339
+ createOutput
3340
+ );
3169
3341
  const tDecode = nowMs2();
3170
3342
  let bytesRead = 0;
3171
- for (let chunkIndex = 0; chunkIndex < chunkOffsets.length; chunkIndex++) {
3172
- bytesRead += decoder.decodeChunk(buffer, view, chunkOffsets[chunkIndex], chunkIndex);
3343
+ let assemblyMs = 0;
3344
+ let nextChunk = 0;
3345
+ function consume(chunkIndex, workerBlock) {
3346
+ if (chunkIndex !== nextChunk || chunkIndex >= chunkOffsets.length) {
3347
+ throw new Error("Scanline chunks must be consumed in offset-table order.");
3348
+ }
3349
+ const started = nowMs2();
3350
+ bytesRead += decoder.decodeChunk(
3351
+ buffer,
3352
+ view,
3353
+ chunkOffsets[chunkIndex],
3354
+ chunkIndex,
3355
+ workerBlock
3356
+ );
3357
+ assemblyMs += nowMs2() - started;
3358
+ nextChunk++;
3359
+ }
3360
+ function finish(workerMetrics = {}) {
3361
+ if (nextChunk !== chunkOffsets.length) throw new Error("Incomplete scanline decode.");
3362
+ const tFinalize = nowMs2();
3363
+ const result = decoder.finish();
3364
+ const rgba = result instanceof Float32Array || result instanceof Uint16Array;
3365
+ emit2(onEvent, {
3366
+ phase: "decode",
3367
+ level: "info",
3368
+ code: "decode.complete",
3369
+ message: `Decoded part ${part.id}.`,
3370
+ metrics: {
3371
+ partId: part.id,
3372
+ channels: options.channelNames ? new Set(options.channelNames).size : part.channels.length,
3373
+ outputLayout: rgba ? "rgba" : "planar",
3374
+ ...rgba ? { storagePrecision: result instanceof Uint16Array ? "float16" : "float32" } : {},
3375
+ planarBytes: decoder.getPlanarBytes(),
3376
+ rgbaBytes: rgba ? result.byteLength : 0,
3377
+ assemblyMs,
3378
+ finalizeMs: (nowMs2() - tFinalize).toFixed(3),
3379
+ ...workerMetrics,
3380
+ readKB: (bytesRead / 1024).toFixed(1),
3381
+ ms: (nowMs2() - tDecode).toFixed(3),
3382
+ totalMs: (nowMs2() - t0).toFixed(3)
3383
+ }
3384
+ });
3385
+ return result;
3173
3386
  }
3174
- const result = decoder.finish();
3175
- emit2(onEvent, {
3176
- phase: "decode",
3177
- level: "info",
3178
- code: "decode.complete",
3179
- message: `Decoded part ${part.id}.`,
3180
- metrics: {
3181
- partId: part.id,
3182
- channels: Object.keys(result.channels).length,
3183
- readKB: (bytesRead / 1024).toFixed(1),
3184
- ms: (nowMs2() - tDecode).toFixed(3),
3185
- totalMs: (nowMs2() - t0).toFixed(3)
3186
- }
3187
- });
3188
- return result;
3387
+ return { chunkOffsets, consume, finish };
3388
+ }
3389
+ function prepareExrPart(buffer, structure, options) {
3390
+ return preparePartWithOutput(buffer, structure, options, createPlanarOutput);
3391
+ }
3392
+ function prepareExrPartRgba(buffer, structure, options) {
3393
+ return preparePartWithOutput(
3394
+ buffer,
3395
+ structure,
3396
+ options,
3397
+ (meta, window) => createRgbaOutput(meta, window, (length) => new Float32Array(length))
3398
+ );
3399
+ }
3400
+ function consumeAll(session) {
3401
+ for (let i = 0; i < session.chunkOffsets.length; i++) session.consume(i);
3402
+ return session.finish();
3403
+ }
3404
+ function decodeExrPart(buffer, structure, options) {
3405
+ return consumeAll(prepareExrPart(buffer, structure, options));
3406
+ }
3407
+ function decodeExrPartRgba(buffer, structure, options) {
3408
+ return consumeAll(prepareExrPartRgba(buffer, structure, options));
3409
+ }
3410
+ function prepareExrPartNativeRgba(buffer, structure, options) {
3411
+ const part = structure.parts.find((candidate) => candidate.id === options.partId);
3412
+ if (!part || getExrRgbaStoragePrecision(part.channels, options.channelNames) === "float32")
3413
+ return prepareExrPartRgba(buffer, structure, options);
3414
+ return preparePartWithOutput(
3415
+ buffer,
3416
+ structure,
3417
+ options,
3418
+ (meta, window) => createRgbaOutput(meta, window, (length) => new Uint16Array(length))
3419
+ );
3420
+ }
3421
+ function decodeExrPartNativeRgba(buffer, structure, options) {
3422
+ return consumeAll(prepareExrPartNativeRgba(buffer, structure, options));
3189
3423
  }
3190
3424
 
3191
3425
  // src/reader/openExr.ts
@@ -3504,21 +3738,6 @@ function inspectExrImage(buffer, options = {}) {
3504
3738
  }
3505
3739
 
3506
3740
  // src/reader/expandSampledChannels.ts
3507
- function clamp(value, min, max) {
3508
- if (value < min) return min;
3509
- if (value > max) return max;
3510
- return value;
3511
- }
3512
- function buildNearestSampleMap(length, worldMin, sampleOrigin, sampling, sampledLength) {
3513
- const output = new Int32Array(length);
3514
- if (sampledLength <= 0) return output;
3515
- for (let i = 0; i < length; i++) {
3516
- const world = worldMin + i;
3517
- const sample = Math.round((world - sampleOrigin) / sampling);
3518
- output[i] = clamp(sample, 0, sampledLength - 1);
3519
- }
3520
- return output;
3521
- }
3522
3741
  function expandSampledChannel(width, height, xMin, yMin, channel) {
3523
3742
  if (channel.sampledWidth === 0 || channel.sampledHeight === 0 || channel.data.length === 0) {
3524
3743
  return new Float32Array(width * height);
@@ -3567,54 +3786,11 @@ function expandDecodedPartChannels(decoded, dataWindow) {
3567
3786
  return output;
3568
3787
  }
3569
3788
 
3570
- // src/reader/rgbaChannels.ts
3571
- function normalizeChannelName(name) {
3572
- return name.split(".").pop()?.toUpperCase() ?? "";
3573
- }
3574
- function pickChannel(channels, aliases) {
3575
- for (const alias of aliases) {
3576
- const exact2 = channels[alias];
3577
- if (exact2) return exact2;
3578
- }
3579
- const normalizedAliases = new Set(aliases.map((alias) => alias.toUpperCase()));
3580
- for (const [name, channel] of Object.entries(channels)) {
3581
- if (normalizedAliases.has(normalizeChannelName(name))) return channel;
3582
- }
3583
- return void 0;
3584
- }
3585
- function decodedPartToRgba(width, height, dataWindow, channels) {
3586
- const expanded = expandDecodedPartChannels({ width, height, channels }, dataWindow);
3587
- const fallback = Object.values(expanded)[0] ?? new Float32Array(width * height);
3588
- const red = pickChannel(expanded, ["R", "RED"]) ?? fallback;
3589
- const luma = pickChannel(expanded, ["Y", "LUMA", "LUMINANCE"]);
3590
- const green = pickChannel(expanded, ["G", "GREEN"]) ?? luma ?? red;
3591
- const blue = pickChannel(expanded, ["B", "BLUE"]) ?? luma ?? red;
3592
- const alpha = pickChannel(expanded, ["A", "ALPHA"]);
3593
- const pixelCount = width * height;
3594
- const rgba = new Float32Array(pixelCount * 4);
3595
- for (let index = 0; index < pixelCount; index += 1) {
3596
- const offset = index * 4;
3597
- rgba[offset] = Number.isFinite(red[index]) ? red[index] : 0;
3598
- rgba[offset + 1] = Number.isFinite(green[index]) ? green[index] : rgba[offset];
3599
- rgba[offset + 2] = Number.isFinite(blue[index]) ? blue[index] : rgba[offset];
3600
- rgba[offset + 3] = alpha ? Math.max(0, alpha[index]) : 1;
3601
- }
3602
- return rgba;
3603
- }
3604
-
3605
3789
  // src/reader/decodeExrRgba.ts
3606
- function decodeExrRgba(buffer, options = {}) {
3607
- const info = inspectExrImage(buffer, options);
3608
- const decoded = decodeExrPart(buffer, info.structure, {
3609
- partId: info.partId,
3610
- predecodedZipBlocks: options.decode?.predecodedZipBlocks,
3611
- predecodedDwaBlocks: options.decode?.predecodedDwaBlocks,
3612
- onEvent: options.onEvent
3613
- });
3790
+ function getExrRgbaImageMetadata(info) {
3614
3791
  return {
3615
3792
  width: info.width,
3616
3793
  height: info.height,
3617
- rgba: decodedPartToRgba(info.width, info.height, info.dataWindow, decoded.channels),
3618
3794
  partId: info.partId,
3619
3795
  dataWindow: info.dataWindow,
3620
3796
  displayWindow: info.displayWindow,
@@ -3622,6 +3798,25 @@ function decodeExrRgba(buffer, options = {}) {
3622
3798
  attributes: info.part.attributes
3623
3799
  };
3624
3800
  }
3801
+ function describeExrNativeRgba(info, rgba) {
3802
+ const metadata = getExrRgbaImageMetadata(info);
3803
+ return rgba instanceof Uint16Array ? { ...metadata, storagePrecision: "float16", rgba } : { ...metadata, storagePrecision: "float32", rgba };
3804
+ }
3805
+ function getDecodeOptions(info, options) {
3806
+ return { ...options.decode, partId: info.partId, onEvent: options.onEvent };
3807
+ }
3808
+ function decodeExrRgba(buffer, options = {}) {
3809
+ const info = inspectExrImage(buffer, options);
3810
+ const rgba = decodeExrPartRgba(buffer, info.structure, getDecodeOptions(info, options));
3811
+ return { ...getExrRgbaImageMetadata(info), rgba };
3812
+ }
3813
+ function decodeExrNativeRgba(buffer, options = {}) {
3814
+ const info = inspectExrImage(buffer, options);
3815
+ return describeExrNativeRgba(
3816
+ info,
3817
+ decodeExrPartNativeRgba(buffer, info.structure, getDecodeOptions(info, options))
3818
+ );
3819
+ }
3625
3820
 
3626
3821
  // src/writer/sampling.ts
3627
3822
  function modulo5(value, base) {
@@ -4455,7 +4650,7 @@ function floatBitsToFloat24Bits(bits) {
4455
4650
  const sign = bits & 2147483648;
4456
4651
  const exponent = bits & 2139095040;
4457
4652
  const mantissa = bits & 8388607;
4458
- let reduced = 0;
4653
+ let reduced;
4459
4654
  if (exponent === 2139095040) {
4460
4655
  if (mantissa !== 0) {
4461
4656
  const shrunkMantissa = mantissa >>> 8;
@@ -5352,11 +5547,13 @@ function encodeExrRgba(image, options) {
5352
5547
  EXR_EVENT_CODES,
5353
5548
  ExrError,
5354
5549
  bufferExrSource,
5550
+ decodeExrNativeRgba,
5355
5551
  decodeExrPart,
5356
5552
  decodeExrRgba,
5357
5553
  encodeExrRgba,
5358
5554
  expandDecodedPartChannels,
5359
5555
  expandSampledChannel,
5556
+ getExrRgbaStoragePrecision,
5360
5557
  inspectExrImage,
5361
5558
  isExrError,
5362
5559
  openExr,