@bb-studio/exr 0.1.2 → 0.1.3

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
@@ -140,6 +140,62 @@ interface ReadExrResult {
140
140
  }
141
141
  declare function readExr(buffer: ExrBinaryInput, options?: ReadExrOptions): ReadExrResult;
142
142
 
143
+ interface ExrImageInfo {
144
+ structure: ExrStructure;
145
+ part: ExrPart;
146
+ partId: number;
147
+ width: number;
148
+ height: number;
149
+ dataWindow: ExrWindow;
150
+ displayWindow?: ExrWindow;
151
+ }
152
+ interface InspectExrImageOptions extends ParseExrOptions {
153
+ partId?: number;
154
+ }
155
+ declare function inspectExrImage(buffer: ExrBinaryInput, options?: InspectExrImageOptions): ExrImageInfo;
156
+
157
+ declare function expandSampledChannel(width: number, height: number, xMin: number, yMin: number, channel: DecodedChannel): Float32Array;
158
+ declare function expandDecodedPartChannels(decoded: DecodedPart, dataWindow: ExrWindow): Record<string, Float32Array>;
159
+
160
+ interface DecodeExrRgbaOptions extends ParseExrOptions {
161
+ partId?: number;
162
+ decode?: Omit<DecodeExrPartOptions, 'partId' | 'onEvent'>;
163
+ }
164
+ interface DecodedExrRgbaImage {
165
+ width: number;
166
+ height: number;
167
+ rgba: Float32Array;
168
+ partId: number;
169
+ dataWindow: ExrWindow;
170
+ displayWindow?: ExrWindow;
171
+ channels: readonly ExrChannel[];
172
+ attributes: Readonly<Record<string, unknown>>;
173
+ }
174
+ declare function decodeExrRgba(buffer: ExrBinaryInput, options?: DecodeExrRgbaOptions): DecodedExrRgbaImage;
175
+
143
176
  declare function writeExr(input: WriteExrInput, options?: WriteExrOptions): Uint8Array;
144
177
 
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 };
178
+ type ExrRgbaPrecision = 'half' | 'float';
179
+ interface ExrRgbaNamedChannel {
180
+ name: string;
181
+ data: Float32Array;
182
+ precision?: ExrRgbaPrecision;
183
+ }
184
+ interface EncodeExrRgbaImage {
185
+ width: number;
186
+ height: number;
187
+ rgba: Float32Array;
188
+ namedChannels?: readonly ExrRgbaNamedChannel[];
189
+ dataWindow?: ExrWindow;
190
+ displayWindow?: ExrWindow;
191
+ }
192
+ interface EncodeExrRgbaOptions {
193
+ precision: ExrRgbaPrecision;
194
+ includeAlpha: boolean;
195
+ includeColorChannels?: boolean;
196
+ compression?: ExrCompression | number;
197
+ attributes?: Readonly<Record<string, WriteExrAttribute>>;
198
+ }
199
+ declare function encodeExrRgba(image: EncodeExrRgbaImage, options: EncodeExrRgbaOptions): Uint8Array;
200
+
201
+ 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 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
@@ -140,6 +140,62 @@ interface ReadExrResult {
140
140
  }
141
141
  declare function readExr(buffer: ExrBinaryInput, options?: ReadExrOptions): ReadExrResult;
142
142
 
143
+ interface ExrImageInfo {
144
+ structure: ExrStructure;
145
+ part: ExrPart;
146
+ partId: number;
147
+ width: number;
148
+ height: number;
149
+ dataWindow: ExrWindow;
150
+ displayWindow?: ExrWindow;
151
+ }
152
+ interface InspectExrImageOptions extends ParseExrOptions {
153
+ partId?: number;
154
+ }
155
+ declare function inspectExrImage(buffer: ExrBinaryInput, options?: InspectExrImageOptions): ExrImageInfo;
156
+
157
+ declare function expandSampledChannel(width: number, height: number, xMin: number, yMin: number, channel: DecodedChannel): Float32Array;
158
+ declare function expandDecodedPartChannels(decoded: DecodedPart, dataWindow: ExrWindow): Record<string, Float32Array>;
159
+
160
+ interface DecodeExrRgbaOptions extends ParseExrOptions {
161
+ partId?: number;
162
+ decode?: Omit<DecodeExrPartOptions, 'partId' | 'onEvent'>;
163
+ }
164
+ interface DecodedExrRgbaImage {
165
+ width: number;
166
+ height: number;
167
+ rgba: Float32Array;
168
+ partId: number;
169
+ dataWindow: ExrWindow;
170
+ displayWindow?: ExrWindow;
171
+ channels: readonly ExrChannel[];
172
+ attributes: Readonly<Record<string, unknown>>;
173
+ }
174
+ declare function decodeExrRgba(buffer: ExrBinaryInput, options?: DecodeExrRgbaOptions): DecodedExrRgbaImage;
175
+
143
176
  declare function writeExr(input: WriteExrInput, options?: WriteExrOptions): Uint8Array;
144
177
 
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 };
178
+ type ExrRgbaPrecision = 'half' | 'float';
179
+ interface ExrRgbaNamedChannel {
180
+ name: string;
181
+ data: Float32Array;
182
+ precision?: ExrRgbaPrecision;
183
+ }
184
+ interface EncodeExrRgbaImage {
185
+ width: number;
186
+ height: number;
187
+ rgba: Float32Array;
188
+ namedChannels?: readonly ExrRgbaNamedChannel[];
189
+ dataWindow?: ExrWindow;
190
+ displayWindow?: ExrWindow;
191
+ }
192
+ interface EncodeExrRgbaOptions {
193
+ precision: ExrRgbaPrecision;
194
+ includeAlpha: boolean;
195
+ includeColorChannels?: boolean;
196
+ compression?: ExrCompression | number;
197
+ attributes?: Readonly<Record<string, WriteExrAttribute>>;
198
+ }
199
+ declare function encodeExrRgba(image: EncodeExrRgbaImage, options: EncodeExrRgbaOptions): Uint8Array;
200
+
201
+ 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 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
@@ -3117,6 +3117,176 @@ function readExr(buffer, options = {}) {
3117
3117
  };
3118
3118
  }
3119
3119
 
3120
+ // src/reader/exrImage.ts
3121
+ function selectExrImagePart(structure, partId) {
3122
+ if (partId !== void 0) {
3123
+ const part2 = structure.parts.find((candidate) => candidate.id === partId);
3124
+ if (!part2) {
3125
+ throw new ExrError("PART_NOT_FOUND", `Part ${partId} was not found in EXR structure.`, {
3126
+ partId
3127
+ });
3128
+ }
3129
+ if (!part2.dataWindow) {
3130
+ throw new ExrError("MISSING_DATA_WINDOW", `Part ${partId} is missing a data window.`, {
3131
+ partId
3132
+ });
3133
+ }
3134
+ return part2;
3135
+ }
3136
+ const part = structure.parts.find((candidate) => candidate.dataWindow);
3137
+ if (!part?.dataWindow) {
3138
+ throw new ExrError(
3139
+ "MISSING_DATA_WINDOW",
3140
+ "EXR file does not contain a decodable scanline part."
3141
+ );
3142
+ }
3143
+ return part;
3144
+ }
3145
+ function getExrPartDimensions(part) {
3146
+ if (!part.dataWindow) {
3147
+ throw new ExrError("MISSING_DATA_WINDOW", `Part ${part.id} is missing a data window.`, {
3148
+ partId: part.id
3149
+ });
3150
+ }
3151
+ return {
3152
+ width: part.dataWindow.xMax - part.dataWindow.xMin + 1,
3153
+ height: part.dataWindow.yMax - part.dataWindow.yMin + 1
3154
+ };
3155
+ }
3156
+ function inspectExrImage(buffer, options = {}) {
3157
+ const structure = parseExrStructure(buffer, { onEvent: options.onEvent });
3158
+ const part = selectExrImagePart(structure, options.partId);
3159
+ const dimensions = getExrPartDimensions(part);
3160
+ return {
3161
+ structure,
3162
+ part,
3163
+ partId: part.id,
3164
+ ...dimensions,
3165
+ dataWindow: part.dataWindow,
3166
+ displayWindow: part.displayWindow
3167
+ };
3168
+ }
3169
+
3170
+ // src/reader/expandSampledChannels.ts
3171
+ function clamp(value, min, max) {
3172
+ if (value < min) return min;
3173
+ if (value > max) return max;
3174
+ return value;
3175
+ }
3176
+ function buildNearestSampleMap(length, worldMin, sampleOrigin, sampling, sampledLength) {
3177
+ const output = new Int32Array(length);
3178
+ if (sampledLength <= 0) return output;
3179
+ for (let i = 0; i < length; i++) {
3180
+ const world = worldMin + i;
3181
+ const sample = Math.round((world - sampleOrigin) / sampling);
3182
+ output[i] = clamp(sample, 0, sampledLength - 1);
3183
+ }
3184
+ return output;
3185
+ }
3186
+ function expandSampledChannel(width, height, xMin, yMin, channel) {
3187
+ if (channel.sampledWidth === 0 || channel.sampledHeight === 0 || channel.data.length === 0) {
3188
+ return new Float32Array(width * height);
3189
+ }
3190
+ const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
3191
+ const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
3192
+ if (xSampling === 1 && ySampling === 1 && channel.sampledWidth === width && channel.sampledHeight === height && channel.sampleOriginX === xMin && channel.sampleOriginY === yMin) {
3193
+ return channel.data;
3194
+ }
3195
+ const output = new Float32Array(width * height);
3196
+ const xSampleMap = buildNearestSampleMap(
3197
+ width,
3198
+ xMin,
3199
+ channel.sampleOriginX,
3200
+ xSampling,
3201
+ channel.sampledWidth
3202
+ );
3203
+ const ySampleMap = buildNearestSampleMap(
3204
+ height,
3205
+ yMin,
3206
+ channel.sampleOriginY,
3207
+ ySampling,
3208
+ channel.sampledHeight
3209
+ );
3210
+ for (let y = 0; y < height; y++) {
3211
+ const sampleY = ySampleMap[y];
3212
+ const targetRow = y * width;
3213
+ const sourceRow = sampleY * channel.sampledWidth;
3214
+ for (let x = 0; x < width; x++) {
3215
+ output[targetRow + x] = channel.data[sourceRow + xSampleMap[x]];
3216
+ }
3217
+ }
3218
+ return output;
3219
+ }
3220
+ function expandDecodedPartChannels(decoded, dataWindow) {
3221
+ const output = {};
3222
+ for (const [name, channel] of Object.entries(decoded.channels)) {
3223
+ output[name] = expandSampledChannel(
3224
+ decoded.width,
3225
+ decoded.height,
3226
+ dataWindow.xMin,
3227
+ dataWindow.yMin,
3228
+ channel
3229
+ );
3230
+ }
3231
+ return output;
3232
+ }
3233
+
3234
+ // src/reader/rgbaChannels.ts
3235
+ function normalizeChannelName(name) {
3236
+ return name.split(".").pop()?.toUpperCase() ?? "";
3237
+ }
3238
+ function pickChannel(channels, aliases) {
3239
+ for (const alias of aliases) {
3240
+ const exact = channels[alias];
3241
+ if (exact) return exact;
3242
+ }
3243
+ const normalizedAliases = new Set(aliases.map((alias) => alias.toUpperCase()));
3244
+ for (const [name, channel] of Object.entries(channels)) {
3245
+ if (normalizedAliases.has(normalizeChannelName(name))) return channel;
3246
+ }
3247
+ return void 0;
3248
+ }
3249
+ function decodedPartToRgba(width, height, dataWindow, channels) {
3250
+ const expanded = expandDecodedPartChannels({ width, height, channels }, dataWindow);
3251
+ const fallback = Object.values(expanded)[0] ?? new Float32Array(width * height);
3252
+ const red = pickChannel(expanded, ["R", "RED"]) ?? fallback;
3253
+ const luma = pickChannel(expanded, ["Y", "LUMA", "LUMINANCE"]);
3254
+ const green = pickChannel(expanded, ["G", "GREEN"]) ?? luma ?? red;
3255
+ const blue = pickChannel(expanded, ["B", "BLUE"]) ?? luma ?? red;
3256
+ const alpha = pickChannel(expanded, ["A", "ALPHA"]);
3257
+ const pixelCount = width * height;
3258
+ const rgba = new Float32Array(pixelCount * 4);
3259
+ for (let index = 0; index < pixelCount; index += 1) {
3260
+ const offset = index * 4;
3261
+ rgba[offset] = Number.isFinite(red[index]) ? red[index] : 0;
3262
+ rgba[offset + 1] = Number.isFinite(green[index]) ? green[index] : rgba[offset];
3263
+ rgba[offset + 2] = Number.isFinite(blue[index]) ? blue[index] : rgba[offset];
3264
+ rgba[offset + 3] = alpha ? Math.max(0, alpha[index]) : 1;
3265
+ }
3266
+ return rgba;
3267
+ }
3268
+
3269
+ // src/reader/decodeExrRgba.ts
3270
+ function decodeExrRgba(buffer, options = {}) {
3271
+ const info = inspectExrImage(buffer, options);
3272
+ const decoded = decodeExrPart(buffer, info.structure, {
3273
+ partId: info.partId,
3274
+ predecodedZipBlocks: options.decode?.predecodedZipBlocks,
3275
+ predecodedDwaBlocks: options.decode?.predecodedDwaBlocks,
3276
+ onEvent: options.onEvent
3277
+ });
3278
+ return {
3279
+ width: info.width,
3280
+ height: info.height,
3281
+ rgba: decodedPartToRgba(info.width, info.height, info.dataWindow, decoded.channels),
3282
+ partId: info.partId,
3283
+ dataWindow: info.dataWindow,
3284
+ displayWindow: info.displayWindow,
3285
+ channels: info.part.channels,
3286
+ attributes: info.part.attributes
3287
+ };
3288
+ }
3289
+
3120
3290
  // src/writer/sampling.ts
3121
3291
  function modulo5(value, base) {
3122
3292
  const result = value % base;
@@ -4742,10 +4912,113 @@ function writeExr(input, options = {}) {
4742
4912
  });
4743
4913
  return out;
4744
4914
  }
4915
+
4916
+ // src/writer/encodeExrRgba.ts
4917
+ function pixelTypeForPrecision(precision) {
4918
+ return precision === "half" ? 1 : 2;
4919
+ }
4920
+ function extractRgbaChannel(rgba, offset) {
4921
+ const data = new Float32Array(rgba.length / 4);
4922
+ for (let index = 0; index < data.length; index += 1) {
4923
+ data[index] = rgba[index * 4 + offset];
4924
+ }
4925
+ return data;
4926
+ }
4927
+ function resolveDataWindow(image) {
4928
+ const dataWindow = image.dataWindow ?? {
4929
+ xMin: 0,
4930
+ yMin: 0,
4931
+ xMax: image.width - 1,
4932
+ yMax: image.height - 1
4933
+ };
4934
+ const width = dataWindow.xMax - dataWindow.xMin + 1;
4935
+ const height = dataWindow.yMax - dataWindow.yMin + 1;
4936
+ if (width !== image.width || height !== image.height) {
4937
+ throw new ExrError(
4938
+ "INVALID_WRITE_INPUT",
4939
+ "RGBA image dimensions do not match the EXR data window.",
4940
+ {
4941
+ width: image.width,
4942
+ height: image.height,
4943
+ dataWindowWidth: width,
4944
+ dataWindowHeight: height
4945
+ }
4946
+ );
4947
+ }
4948
+ return dataWindow;
4949
+ }
4950
+ function appendNamedChannels(channels, image) {
4951
+ const pixelCount = image.width * image.height;
4952
+ const names = new Set(channels.map((channel) => channel.name));
4953
+ for (const namedChannel of image.namedChannels ?? []) {
4954
+ const name = namedChannel.name.trim();
4955
+ if (!name || name.includes("\0")) {
4956
+ throw new ExrError("INVALID_WRITE_INPUT", "OpenEXR named channels require a valid name.");
4957
+ }
4958
+ if (names.has(name)) {
4959
+ throw new ExrError("INVALID_WRITE_INPUT", `OpenEXR channel name "${name}" is duplicated.`);
4960
+ }
4961
+ if (namedChannel.data.length !== pixelCount) {
4962
+ throw new ExrError(
4963
+ "INVALID_WRITE_INPUT",
4964
+ `OpenEXR channel "${name}" data length does not match the image dimensions.`,
4965
+ { expected: pixelCount, actual: namedChannel.data.length }
4966
+ );
4967
+ }
4968
+ names.add(name);
4969
+ channels.push({
4970
+ name,
4971
+ pixelType: pixelTypeForPrecision(namedChannel.precision ?? "float"),
4972
+ data: namedChannel.data
4973
+ });
4974
+ }
4975
+ }
4976
+ function encodeExrRgba(image, options) {
4977
+ const pixelCount = image.width * image.height;
4978
+ if (image.width <= 0 || image.height <= 0 || image.rgba.length !== pixelCount * 4) {
4979
+ throw new ExrError(
4980
+ "INVALID_WRITE_INPUT",
4981
+ "OpenEXR RGBA data length does not match the image dimensions.",
4982
+ { width: image.width, height: image.height, actual: image.rgba.length }
4983
+ );
4984
+ }
4985
+ const pixelType = pixelTypeForPrecision(options.precision);
4986
+ const channels = [];
4987
+ if (options.includeColorChannels !== false) {
4988
+ channels.push(
4989
+ { name: "R", pixelType, data: extractRgbaChannel(image.rgba, 0) },
4990
+ { name: "G", pixelType, data: extractRgbaChannel(image.rgba, 1) },
4991
+ { name: "B", pixelType, data: extractRgbaChannel(image.rgba, 2) }
4992
+ );
4993
+ }
4994
+ if (options.includeAlpha) {
4995
+ channels.push({ name: "A", pixelType, data: extractRgbaChannel(image.rgba, 3) });
4996
+ }
4997
+ appendNamedChannels(channels, image);
4998
+ if (channels.length === 0) {
4999
+ throw new ExrError("INVALID_WRITE_INPUT", "OpenEXR export requires at least one channel.");
5000
+ }
5001
+ return writeExr({
5002
+ parts: [
5003
+ {
5004
+ compression: options.compression ?? 3,
5005
+ dataWindow: resolveDataWindow(image),
5006
+ ...image.displayWindow ? { displayWindow: image.displayWindow } : {},
5007
+ channels,
5008
+ ...options.attributes ? { attributes: options.attributes } : {}
5009
+ }
5010
+ ]
5011
+ });
5012
+ }
4745
5013
  export {
4746
5014
  EXR_EVENT_CODES,
4747
5015
  ExrError,
4748
5016
  decodeExrPart,
5017
+ decodeExrRgba,
5018
+ encodeExrRgba,
5019
+ expandDecodedPartChannels,
5020
+ expandSampledChannel,
5021
+ inspectExrImage,
4749
5022
  isExrError,
4750
5023
  parseExr,
4751
5024
  parseExrStructure,