@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/dist/index.d.cts CHANGED
@@ -63,6 +63,8 @@ interface ExrStructure {
63
63
  type ParseExrOptions = EventCapableOptions;
64
64
  interface DecodeExrPartOptions extends EventCapableOptions {
65
65
  partId: number;
66
+ /** Exact physical channel names to materialize. Omit to decode every channel in the part. */
67
+ channelNames?: readonly string[];
66
68
  predecodedZipBlocks?: Map<number, Uint8Array>;
67
69
  predecodedDwaBlocks?: Map<number, Uint8Array>;
68
70
  }
@@ -116,7 +118,7 @@ interface WriteExrInput {
116
118
  }
117
119
  type WriteExrOptions = EventCapableOptions;
118
120
 
119
- type ExrErrorCode = 'BUFFER_TOO_SMALL' | 'INVALID_MAGIC' | 'TRUNCATED_FILE' | 'MALFORMED_HEADER' | 'PART_NOT_FOUND' | 'UNSUPPORTED_PART_TYPE' | 'UNSUPPORTED_COMPRESSION' | 'MISSING_DATA_WINDOW' | 'MALFORMED_OFFSET_TABLE' | 'MALFORMED_CHUNK' | 'DECOMPRESSION_FAILED' | 'INVALID_WRITE_INPUT' | 'UNSUPPORTED_WRITE_COMPRESSION' | 'ENCODING_FAILED';
121
+ type ExrErrorCode = 'BUFFER_TOO_SMALL' | 'INVALID_MAGIC' | 'TRUNCATED_FILE' | 'MALFORMED_HEADER' | 'PART_NOT_FOUND' | 'CHANNEL_NOT_FOUND' | 'UNSUPPORTED_PART_TYPE' | 'UNSUPPORTED_COMPRESSION' | 'MISSING_DATA_WINDOW' | 'MALFORMED_OFFSET_TABLE' | 'MALFORMED_CHUNK' | 'DECOMPRESSION_FAILED' | 'INVALID_WRITE_INPUT' | 'UNSUPPORTED_WRITE_COMPRESSION' | 'ENCODING_FAILED';
120
122
  declare class ExrError extends Error {
121
123
  readonly code: ExrErrorCode;
122
124
  readonly details?: Record<string, string | number>;
@@ -140,6 +142,64 @@ interface ReadExrResult {
140
142
  }
141
143
  declare function readExr(buffer: ExrBinaryInput, options?: ReadExrOptions): ReadExrResult;
142
144
 
145
+ interface ExrImageInfo {
146
+ structure: ExrStructure;
147
+ part: ExrPart;
148
+ partId: number;
149
+ width: number;
150
+ height: number;
151
+ dataWindow: ExrWindow;
152
+ displayWindow?: ExrWindow;
153
+ }
154
+ interface InspectExrImageOptions extends ParseExrOptions {
155
+ partId?: number;
156
+ }
157
+ declare function inspectExrImage(buffer: ExrBinaryInput, options?: InspectExrImageOptions): ExrImageInfo;
158
+
159
+ declare function expandSampledChannel(width: number, height: number, xMin: number, yMin: number, channel: DecodedChannel): Float32Array;
160
+ declare function expandDecodedPartChannels(decoded: DecodedPart, dataWindow: ExrWindow): Record<string, Float32Array>;
161
+
162
+ interface DecodeExrRgbaOptions extends ParseExrOptions {
163
+ partId?: number;
164
+ decode?: Omit<DecodeExrPartOptions, 'partId' | 'onEvent'>;
165
+ }
166
+ interface DecodedExrRgbaImage {
167
+ width: number;
168
+ height: number;
169
+ rgba: Float32Array;
170
+ partId: number;
171
+ dataWindow: ExrWindow;
172
+ displayWindow?: ExrWindow;
173
+ channels: readonly ExrChannel[];
174
+ attributes: Readonly<Record<string, unknown>>;
175
+ }
176
+ declare function decodeExrRgba(buffer: ExrBinaryInput, options?: DecodeExrRgbaOptions): DecodedExrRgbaImage;
177
+
143
178
  declare function writeExr(input: WriteExrInput, options?: WriteExrOptions): Uint8Array;
144
179
 
145
- export { type DecodeExrPartOptions, type DecodedChannel, type DecodedPart, EXR_EVENT_CODES, type EventCapableOptions, type ExrBinaryInput, type ExrChannel, type ExrChromaticities, type ExrCompression, ExrError, type ExrErrorCode, type ExrEvent, type ExrEventCallback, type ExrEventCode, type ExrEventLevel, type ExrEventPhase, type ExrPart, type ExrPixelType, type ExrStructure, type ExrWindow, type ParseExrOptions, type ReadExrOptions, type ReadExrResult, type WriteExrAttribute, type WriteExrChannelInput, type WriteExrInput, type WriteExrOptions, type WriteExrPartInput, decodeExrPart, isExrError, parseExr, parseExrStructure, readExr, toArrayBuffer, writeExr };
180
+ type ExrRgbaPrecision = 'half' | 'float';
181
+ /** UINT uses the decoder's normalized [0, 1] representation of the full uint32 range. */
182
+ type ExrNamedChannelPrecision = 'uint' | ExrRgbaPrecision;
183
+ interface ExrRgbaNamedChannel {
184
+ name: string;
185
+ data: Float32Array;
186
+ precision?: ExrNamedChannelPrecision;
187
+ }
188
+ interface EncodeExrRgbaImage {
189
+ width: number;
190
+ height: number;
191
+ rgba: Float32Array;
192
+ namedChannels?: readonly ExrRgbaNamedChannel[];
193
+ dataWindow?: ExrWindow;
194
+ displayWindow?: ExrWindow;
195
+ }
196
+ interface EncodeExrRgbaOptions {
197
+ precision: ExrRgbaPrecision;
198
+ includeAlpha: boolean;
199
+ includeColorChannels?: boolean;
200
+ compression?: ExrCompression | number;
201
+ attributes?: Readonly<Record<string, WriteExrAttribute>>;
202
+ }
203
+ declare function encodeExrRgba(image: EncodeExrRgbaImage, options: EncodeExrRgbaOptions): Uint8Array;
204
+
205
+ export { type DecodeExrPartOptions, type DecodeExrRgbaOptions, type DecodedChannel, type DecodedExrRgbaImage, type DecodedPart, EXR_EVENT_CODES, type EncodeExrRgbaImage, type EncodeExrRgbaOptions, type EventCapableOptions, type ExrBinaryInput, type ExrChannel, type ExrChromaticities, type ExrCompression, ExrError, type ExrErrorCode, type ExrEvent, type ExrEventCallback, type ExrEventCode, type ExrEventLevel, type ExrEventPhase, type ExrImageInfo, type ExrNamedChannelPrecision, type ExrPart, type ExrPixelType, type ExrRgbaNamedChannel, type ExrRgbaPrecision, type ExrStructure, type ExrWindow, type InspectExrImageOptions, type ParseExrOptions, type ReadExrOptions, type ReadExrResult, type WriteExrAttribute, type WriteExrChannelInput, type WriteExrInput, type WriteExrOptions, type WriteExrPartInput, decodeExrPart, decodeExrRgba, encodeExrRgba, expandDecodedPartChannels, expandSampledChannel, inspectExrImage, isExrError, parseExr, parseExrStructure, readExr, toArrayBuffer, writeExr };
package/dist/index.d.ts CHANGED
@@ -63,6 +63,8 @@ interface ExrStructure {
63
63
  type ParseExrOptions = EventCapableOptions;
64
64
  interface DecodeExrPartOptions extends EventCapableOptions {
65
65
  partId: number;
66
+ /** Exact physical channel names to materialize. Omit to decode every channel in the part. */
67
+ channelNames?: readonly string[];
66
68
  predecodedZipBlocks?: Map<number, Uint8Array>;
67
69
  predecodedDwaBlocks?: Map<number, Uint8Array>;
68
70
  }
@@ -116,7 +118,7 @@ interface WriteExrInput {
116
118
  }
117
119
  type WriteExrOptions = EventCapableOptions;
118
120
 
119
- type ExrErrorCode = 'BUFFER_TOO_SMALL' | 'INVALID_MAGIC' | 'TRUNCATED_FILE' | 'MALFORMED_HEADER' | 'PART_NOT_FOUND' | 'UNSUPPORTED_PART_TYPE' | 'UNSUPPORTED_COMPRESSION' | 'MISSING_DATA_WINDOW' | 'MALFORMED_OFFSET_TABLE' | 'MALFORMED_CHUNK' | 'DECOMPRESSION_FAILED' | 'INVALID_WRITE_INPUT' | 'UNSUPPORTED_WRITE_COMPRESSION' | 'ENCODING_FAILED';
121
+ type ExrErrorCode = 'BUFFER_TOO_SMALL' | 'INVALID_MAGIC' | 'TRUNCATED_FILE' | 'MALFORMED_HEADER' | 'PART_NOT_FOUND' | 'CHANNEL_NOT_FOUND' | 'UNSUPPORTED_PART_TYPE' | 'UNSUPPORTED_COMPRESSION' | 'MISSING_DATA_WINDOW' | 'MALFORMED_OFFSET_TABLE' | 'MALFORMED_CHUNK' | 'DECOMPRESSION_FAILED' | 'INVALID_WRITE_INPUT' | 'UNSUPPORTED_WRITE_COMPRESSION' | 'ENCODING_FAILED';
120
122
  declare class ExrError extends Error {
121
123
  readonly code: ExrErrorCode;
122
124
  readonly details?: Record<string, string | number>;
@@ -140,6 +142,64 @@ interface ReadExrResult {
140
142
  }
141
143
  declare function readExr(buffer: ExrBinaryInput, options?: ReadExrOptions): ReadExrResult;
142
144
 
145
+ interface ExrImageInfo {
146
+ structure: ExrStructure;
147
+ part: ExrPart;
148
+ partId: number;
149
+ width: number;
150
+ height: number;
151
+ dataWindow: ExrWindow;
152
+ displayWindow?: ExrWindow;
153
+ }
154
+ interface InspectExrImageOptions extends ParseExrOptions {
155
+ partId?: number;
156
+ }
157
+ declare function inspectExrImage(buffer: ExrBinaryInput, options?: InspectExrImageOptions): ExrImageInfo;
158
+
159
+ declare function expandSampledChannel(width: number, height: number, xMin: number, yMin: number, channel: DecodedChannel): Float32Array;
160
+ declare function expandDecodedPartChannels(decoded: DecodedPart, dataWindow: ExrWindow): Record<string, Float32Array>;
161
+
162
+ interface DecodeExrRgbaOptions extends ParseExrOptions {
163
+ partId?: number;
164
+ decode?: Omit<DecodeExrPartOptions, 'partId' | 'onEvent'>;
165
+ }
166
+ interface DecodedExrRgbaImage {
167
+ width: number;
168
+ height: number;
169
+ rgba: Float32Array;
170
+ partId: number;
171
+ dataWindow: ExrWindow;
172
+ displayWindow?: ExrWindow;
173
+ channels: readonly ExrChannel[];
174
+ attributes: Readonly<Record<string, unknown>>;
175
+ }
176
+ declare function decodeExrRgba(buffer: ExrBinaryInput, options?: DecodeExrRgbaOptions): DecodedExrRgbaImage;
177
+
143
178
  declare function writeExr(input: WriteExrInput, options?: WriteExrOptions): Uint8Array;
144
179
 
145
- export { type DecodeExrPartOptions, type DecodedChannel, type DecodedPart, EXR_EVENT_CODES, type EventCapableOptions, type ExrBinaryInput, type ExrChannel, type ExrChromaticities, type ExrCompression, ExrError, type ExrErrorCode, type ExrEvent, type ExrEventCallback, type ExrEventCode, type ExrEventLevel, type ExrEventPhase, type ExrPart, type ExrPixelType, type ExrStructure, type ExrWindow, type ParseExrOptions, type ReadExrOptions, type ReadExrResult, type WriteExrAttribute, type WriteExrChannelInput, type WriteExrInput, type WriteExrOptions, type WriteExrPartInput, decodeExrPart, isExrError, parseExr, parseExrStructure, readExr, toArrayBuffer, writeExr };
180
+ type ExrRgbaPrecision = 'half' | 'float';
181
+ /** UINT uses the decoder's normalized [0, 1] representation of the full uint32 range. */
182
+ type ExrNamedChannelPrecision = 'uint' | ExrRgbaPrecision;
183
+ interface ExrRgbaNamedChannel {
184
+ name: string;
185
+ data: Float32Array;
186
+ precision?: ExrNamedChannelPrecision;
187
+ }
188
+ interface EncodeExrRgbaImage {
189
+ width: number;
190
+ height: number;
191
+ rgba: Float32Array;
192
+ namedChannels?: readonly ExrRgbaNamedChannel[];
193
+ dataWindow?: ExrWindow;
194
+ displayWindow?: ExrWindow;
195
+ }
196
+ interface EncodeExrRgbaOptions {
197
+ precision: ExrRgbaPrecision;
198
+ includeAlpha: boolean;
199
+ includeColorChannels?: boolean;
200
+ compression?: ExrCompression | number;
201
+ attributes?: Readonly<Record<string, WriteExrAttribute>>;
202
+ }
203
+ declare function encodeExrRgba(image: EncodeExrRgbaImage, options: EncodeExrRgbaOptions): Uint8Array;
204
+
205
+ export { type DecodeExrPartOptions, type DecodeExrRgbaOptions, type DecodedChannel, type DecodedExrRgbaImage, type DecodedPart, EXR_EVENT_CODES, type EncodeExrRgbaImage, type EncodeExrRgbaOptions, type EventCapableOptions, type ExrBinaryInput, type ExrChannel, type ExrChromaticities, type ExrCompression, ExrError, type ExrErrorCode, type ExrEvent, type ExrEventCallback, type ExrEventCode, type ExrEventLevel, type ExrEventPhase, type ExrImageInfo, type ExrNamedChannelPrecision, type ExrPart, type ExrPixelType, type ExrRgbaNamedChannel, type ExrRgbaPrecision, type ExrStructure, type ExrWindow, type InspectExrImageOptions, type ParseExrOptions, type ReadExrOptions, type ReadExrResult, type WriteExrAttribute, type WriteExrChannelInput, type WriteExrInput, type WriteExrOptions, type WriteExrPartInput, decodeExrPart, decodeExrRgba, encodeExrRgba, expandDecodedPartChannels, expandSampledChannel, inspectExrImage, isExrError, parseExr, parseExrStructure, readExr, toArrayBuffer, writeExr };
package/dist/index.js CHANGED
@@ -2768,7 +2768,7 @@ function toNumberOffset(low, high) {
2768
2768
  }
2769
2769
  return Number(combined);
2770
2770
  }
2771
- function buildChannelMeta(part) {
2771
+ function buildChannelMeta(part, selectedChannelNames) {
2772
2772
  if (!part.dataWindow) return [];
2773
2773
  const { xMin, xMax, yMin, yMax } = part.dataWindow;
2774
2774
  return part.channels.map((channel) => {
@@ -2779,8 +2779,10 @@ function buildChannelMeta(part) {
2779
2779
  const sampledWidth = countSamplesInRange4(xMin, xMax, xSampling);
2780
2780
  const sampledHeight = countSamplesInRange4(yMin, yMax, ySampling);
2781
2781
  const bytesPerSample = channel.pixelType === 1 ? 2 : 4;
2782
+ const selected = selectedChannelNames?.has(channel.name) ?? true;
2782
2783
  return {
2783
2784
  channel,
2785
+ selected,
2784
2786
  pixelType: channel.pixelType,
2785
2787
  ySampling,
2786
2788
  bytesPerSample,
@@ -2789,7 +2791,7 @@ function buildChannelMeta(part) {
2789
2791
  sampledHeight,
2790
2792
  sampleOriginX,
2791
2793
  sampleOriginY,
2792
- data: new Float32Array(sampledWidth * sampledHeight)
2794
+ data: selected ? new Float32Array(sampledWidth * sampledHeight) : new Float32Array(0)
2793
2795
  };
2794
2796
  });
2795
2797
  }
@@ -2941,7 +2943,18 @@ function decodeExrPart(bufferInput, structure, options) {
2941
2943
  ms: (nowMs2() - tOffsets).toFixed(3)
2942
2944
  }
2943
2945
  });
2944
- const channelMeta = buildChannelMeta(part);
2946
+ const selectedChannelNames = options.channelNames ? new Set(options.channelNames) : void 0;
2947
+ if (selectedChannelNames) {
2948
+ for (const name of selectedChannelNames) {
2949
+ if (!part.channels.some((channel) => channel.name === name)) {
2950
+ throw new ExrError("CHANNEL_NOT_FOUND", `Channel ${name} was not found in EXR part.`, {
2951
+ partId: part.id,
2952
+ channel: name
2953
+ });
2954
+ }
2955
+ }
2956
+ }
2957
+ const channelMeta = buildChannelMeta(part, selectedChannelNames);
2945
2958
  const zipScratch = {
2946
2959
  inflate: new Uint8Array(0),
2947
2960
  output: new Uint8Array(0)
@@ -3048,8 +3061,10 @@ function decodeExrPart(bufferInput, structure, options) {
3048
3061
  blockPointer += byteLength;
3049
3062
  continue;
3050
3063
  }
3051
- const rowOffset = sampledRow * meta.sampledWidth;
3052
- decodeRowIntoChannel(meta, blockData, blockPointer, rowOffset, blockView);
3064
+ if (meta.selected) {
3065
+ const rowOffset = sampledRow * meta.sampledWidth;
3066
+ decodeRowIntoChannel(meta, blockData, blockPointer, rowOffset, blockView);
3067
+ }
3053
3068
  blockPointer += byteLength;
3054
3069
  }
3055
3070
  }
@@ -3069,6 +3084,7 @@ function decodeExrPart(bufferInput, structure, options) {
3069
3084
  }
3070
3085
  const decodedChannels = {};
3071
3086
  for (const meta of channelMeta) {
3087
+ if (!meta.selected) continue;
3072
3088
  decodedChannels[meta.channel.name] = {
3073
3089
  pixelType: meta.channel.pixelType,
3074
3090
  xSampling: meta.channel.xSampling,
@@ -3117,6 +3133,176 @@ function readExr(buffer, options = {}) {
3117
3133
  };
3118
3134
  }
3119
3135
 
3136
+ // src/reader/exrImage.ts
3137
+ function selectExrImagePart(structure, partId) {
3138
+ if (partId !== void 0) {
3139
+ const part2 = structure.parts.find((candidate) => candidate.id === partId);
3140
+ if (!part2) {
3141
+ throw new ExrError("PART_NOT_FOUND", `Part ${partId} was not found in EXR structure.`, {
3142
+ partId
3143
+ });
3144
+ }
3145
+ if (!part2.dataWindow) {
3146
+ throw new ExrError("MISSING_DATA_WINDOW", `Part ${partId} is missing a data window.`, {
3147
+ partId
3148
+ });
3149
+ }
3150
+ return part2;
3151
+ }
3152
+ const part = structure.parts.find((candidate) => candidate.dataWindow);
3153
+ if (!part?.dataWindow) {
3154
+ throw new ExrError(
3155
+ "MISSING_DATA_WINDOW",
3156
+ "EXR file does not contain a decodable scanline part."
3157
+ );
3158
+ }
3159
+ return part;
3160
+ }
3161
+ function getExrPartDimensions(part) {
3162
+ if (!part.dataWindow) {
3163
+ throw new ExrError("MISSING_DATA_WINDOW", `Part ${part.id} is missing a data window.`, {
3164
+ partId: part.id
3165
+ });
3166
+ }
3167
+ return {
3168
+ width: part.dataWindow.xMax - part.dataWindow.xMin + 1,
3169
+ height: part.dataWindow.yMax - part.dataWindow.yMin + 1
3170
+ };
3171
+ }
3172
+ function inspectExrImage(buffer, options = {}) {
3173
+ const structure = parseExrStructure(buffer, { onEvent: options.onEvent });
3174
+ const part = selectExrImagePart(structure, options.partId);
3175
+ const dimensions = getExrPartDimensions(part);
3176
+ return {
3177
+ structure,
3178
+ part,
3179
+ partId: part.id,
3180
+ ...dimensions,
3181
+ dataWindow: part.dataWindow,
3182
+ displayWindow: part.displayWindow
3183
+ };
3184
+ }
3185
+
3186
+ // 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
+ function expandSampledChannel(width, height, xMin, yMin, channel) {
3203
+ if (channel.sampledWidth === 0 || channel.sampledHeight === 0 || channel.data.length === 0) {
3204
+ return new Float32Array(width * height);
3205
+ }
3206
+ const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
3207
+ const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
3208
+ if (xSampling === 1 && ySampling === 1 && channel.sampledWidth === width && channel.sampledHeight === height && channel.sampleOriginX === xMin && channel.sampleOriginY === yMin) {
3209
+ return channel.data;
3210
+ }
3211
+ const output = new Float32Array(width * height);
3212
+ const xSampleMap = buildNearestSampleMap(
3213
+ width,
3214
+ xMin,
3215
+ channel.sampleOriginX,
3216
+ xSampling,
3217
+ channel.sampledWidth
3218
+ );
3219
+ const ySampleMap = buildNearestSampleMap(
3220
+ height,
3221
+ yMin,
3222
+ channel.sampleOriginY,
3223
+ ySampling,
3224
+ channel.sampledHeight
3225
+ );
3226
+ for (let y = 0; y < height; y++) {
3227
+ const sampleY = ySampleMap[y];
3228
+ const targetRow = y * width;
3229
+ const sourceRow = sampleY * channel.sampledWidth;
3230
+ for (let x = 0; x < width; x++) {
3231
+ output[targetRow + x] = channel.data[sourceRow + xSampleMap[x]];
3232
+ }
3233
+ }
3234
+ return output;
3235
+ }
3236
+ function expandDecodedPartChannels(decoded, dataWindow) {
3237
+ const output = {};
3238
+ for (const [name, channel] of Object.entries(decoded.channels)) {
3239
+ output[name] = expandSampledChannel(
3240
+ decoded.width,
3241
+ decoded.height,
3242
+ dataWindow.xMin,
3243
+ dataWindow.yMin,
3244
+ channel
3245
+ );
3246
+ }
3247
+ return output;
3248
+ }
3249
+
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
+ // src/reader/decodeExrRgba.ts
3286
+ function decodeExrRgba(buffer, options = {}) {
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
+ });
3294
+ return {
3295
+ width: info.width,
3296
+ height: info.height,
3297
+ rgba: decodedPartToRgba(info.width, info.height, info.dataWindow, decoded.channels),
3298
+ partId: info.partId,
3299
+ dataWindow: info.dataWindow,
3300
+ displayWindow: info.displayWindow,
3301
+ channels: info.part.channels,
3302
+ attributes: info.part.attributes
3303
+ };
3304
+ }
3305
+
3120
3306
  // src/writer/sampling.ts
3121
3307
  function modulo5(value, base) {
3122
3308
  const result = value % base;
@@ -4742,10 +4928,114 @@ function writeExr(input, options = {}) {
4742
4928
  });
4743
4929
  return out;
4744
4930
  }
4931
+
4932
+ // src/writer/encodeExrRgba.ts
4933
+ function pixelTypeForPrecision(precision) {
4934
+ if (precision === "uint") return 0;
4935
+ return precision === "half" ? 1 : 2;
4936
+ }
4937
+ function extractRgbaChannel(rgba, offset) {
4938
+ const data = new Float32Array(rgba.length / 4);
4939
+ for (let index = 0; index < data.length; index += 1) {
4940
+ data[index] = rgba[index * 4 + offset];
4941
+ }
4942
+ return data;
4943
+ }
4944
+ function resolveDataWindow(image) {
4945
+ const dataWindow = image.dataWindow ?? {
4946
+ xMin: 0,
4947
+ yMin: 0,
4948
+ xMax: image.width - 1,
4949
+ yMax: image.height - 1
4950
+ };
4951
+ const width = dataWindow.xMax - dataWindow.xMin + 1;
4952
+ const height = dataWindow.yMax - dataWindow.yMin + 1;
4953
+ if (width !== image.width || height !== image.height) {
4954
+ throw new ExrError(
4955
+ "INVALID_WRITE_INPUT",
4956
+ "RGBA image dimensions do not match the EXR data window.",
4957
+ {
4958
+ width: image.width,
4959
+ height: image.height,
4960
+ dataWindowWidth: width,
4961
+ dataWindowHeight: height
4962
+ }
4963
+ );
4964
+ }
4965
+ return dataWindow;
4966
+ }
4967
+ function appendNamedChannels(channels, image) {
4968
+ const pixelCount = image.width * image.height;
4969
+ const names = new Set(channels.map((channel) => channel.name));
4970
+ for (const namedChannel of image.namedChannels ?? []) {
4971
+ const name = namedChannel.name.trim();
4972
+ if (!name || name.includes("\0")) {
4973
+ throw new ExrError("INVALID_WRITE_INPUT", "OpenEXR named channels require a valid name.");
4974
+ }
4975
+ if (names.has(name)) {
4976
+ throw new ExrError("INVALID_WRITE_INPUT", `OpenEXR channel name "${name}" is duplicated.`);
4977
+ }
4978
+ if (namedChannel.data.length !== pixelCount) {
4979
+ throw new ExrError(
4980
+ "INVALID_WRITE_INPUT",
4981
+ `OpenEXR channel "${name}" data length does not match the image dimensions.`,
4982
+ { expected: pixelCount, actual: namedChannel.data.length }
4983
+ );
4984
+ }
4985
+ names.add(name);
4986
+ channels.push({
4987
+ name,
4988
+ pixelType: pixelTypeForPrecision(namedChannel.precision ?? "float"),
4989
+ data: namedChannel.data
4990
+ });
4991
+ }
4992
+ }
4993
+ function encodeExrRgba(image, options) {
4994
+ const pixelCount = image.width * image.height;
4995
+ if (image.width <= 0 || image.height <= 0 || image.rgba.length !== pixelCount * 4) {
4996
+ throw new ExrError(
4997
+ "INVALID_WRITE_INPUT",
4998
+ "OpenEXR RGBA data length does not match the image dimensions.",
4999
+ { width: image.width, height: image.height, actual: image.rgba.length }
5000
+ );
5001
+ }
5002
+ const pixelType = pixelTypeForPrecision(options.precision);
5003
+ const channels = [];
5004
+ if (options.includeColorChannels !== false) {
5005
+ channels.push(
5006
+ { name: "R", pixelType, data: extractRgbaChannel(image.rgba, 0) },
5007
+ { name: "G", pixelType, data: extractRgbaChannel(image.rgba, 1) },
5008
+ { name: "B", pixelType, data: extractRgbaChannel(image.rgba, 2) }
5009
+ );
5010
+ }
5011
+ if (options.includeAlpha) {
5012
+ channels.push({ name: "A", pixelType, data: extractRgbaChannel(image.rgba, 3) });
5013
+ }
5014
+ appendNamedChannels(channels, image);
5015
+ if (channels.length === 0) {
5016
+ throw new ExrError("INVALID_WRITE_INPUT", "OpenEXR export requires at least one channel.");
5017
+ }
5018
+ return writeExr({
5019
+ parts: [
5020
+ {
5021
+ compression: options.compression ?? 3,
5022
+ dataWindow: resolveDataWindow(image),
5023
+ ...image.displayWindow ? { displayWindow: image.displayWindow } : {},
5024
+ channels,
5025
+ ...options.attributes ? { attributes: options.attributes } : {}
5026
+ }
5027
+ ]
5028
+ });
5029
+ }
4745
5030
  export {
4746
5031
  EXR_EVENT_CODES,
4747
5032
  ExrError,
4748
5033
  decodeExrPart,
5034
+ decodeExrRgba,
5035
+ encodeExrRgba,
5036
+ expandDecodedPartChannels,
5037
+ expandSampledChannel,
5038
+ inspectExrImage,
4749
5039
  isExrError,
4750
5040
  parseExr,
4751
5041
  parseExrStructure,