@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/README.md +20 -2
- package/dist/browser/index.d.ts +19 -1
- package/dist/browser/index.js +445 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/index.cjs +278 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +57 -1
- package/dist/index.d.ts +57 -1
- package/dist/index.js +273 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -23,6 +23,11 @@ __export(index_exports, {
|
|
|
23
23
|
EXR_EVENT_CODES: () => EXR_EVENT_CODES,
|
|
24
24
|
ExrError: () => ExrError,
|
|
25
25
|
decodeExrPart: () => decodeExrPart,
|
|
26
|
+
decodeExrRgba: () => decodeExrRgba,
|
|
27
|
+
encodeExrRgba: () => encodeExrRgba,
|
|
28
|
+
expandDecodedPartChannels: () => expandDecodedPartChannels,
|
|
29
|
+
expandSampledChannel: () => expandSampledChannel,
|
|
30
|
+
inspectExrImage: () => inspectExrImage,
|
|
26
31
|
isExrError: () => isExrError,
|
|
27
32
|
parseExr: () => parseExr,
|
|
28
33
|
parseExrStructure: () => parseExrStructure,
|
|
@@ -3151,6 +3156,176 @@ function readExr(buffer, options = {}) {
|
|
|
3151
3156
|
};
|
|
3152
3157
|
}
|
|
3153
3158
|
|
|
3159
|
+
// src/reader/exrImage.ts
|
|
3160
|
+
function selectExrImagePart(structure, partId) {
|
|
3161
|
+
if (partId !== void 0) {
|
|
3162
|
+
const part2 = structure.parts.find((candidate) => candidate.id === partId);
|
|
3163
|
+
if (!part2) {
|
|
3164
|
+
throw new ExrError("PART_NOT_FOUND", `Part ${partId} was not found in EXR structure.`, {
|
|
3165
|
+
partId
|
|
3166
|
+
});
|
|
3167
|
+
}
|
|
3168
|
+
if (!part2.dataWindow) {
|
|
3169
|
+
throw new ExrError("MISSING_DATA_WINDOW", `Part ${partId} is missing a data window.`, {
|
|
3170
|
+
partId
|
|
3171
|
+
});
|
|
3172
|
+
}
|
|
3173
|
+
return part2;
|
|
3174
|
+
}
|
|
3175
|
+
const part = structure.parts.find((candidate) => candidate.dataWindow);
|
|
3176
|
+
if (!part?.dataWindow) {
|
|
3177
|
+
throw new ExrError(
|
|
3178
|
+
"MISSING_DATA_WINDOW",
|
|
3179
|
+
"EXR file does not contain a decodable scanline part."
|
|
3180
|
+
);
|
|
3181
|
+
}
|
|
3182
|
+
return part;
|
|
3183
|
+
}
|
|
3184
|
+
function getExrPartDimensions(part) {
|
|
3185
|
+
if (!part.dataWindow) {
|
|
3186
|
+
throw new ExrError("MISSING_DATA_WINDOW", `Part ${part.id} is missing a data window.`, {
|
|
3187
|
+
partId: part.id
|
|
3188
|
+
});
|
|
3189
|
+
}
|
|
3190
|
+
return {
|
|
3191
|
+
width: part.dataWindow.xMax - part.dataWindow.xMin + 1,
|
|
3192
|
+
height: part.dataWindow.yMax - part.dataWindow.yMin + 1
|
|
3193
|
+
};
|
|
3194
|
+
}
|
|
3195
|
+
function inspectExrImage(buffer, options = {}) {
|
|
3196
|
+
const structure = parseExrStructure(buffer, { onEvent: options.onEvent });
|
|
3197
|
+
const part = selectExrImagePart(structure, options.partId);
|
|
3198
|
+
const dimensions = getExrPartDimensions(part);
|
|
3199
|
+
return {
|
|
3200
|
+
structure,
|
|
3201
|
+
part,
|
|
3202
|
+
partId: part.id,
|
|
3203
|
+
...dimensions,
|
|
3204
|
+
dataWindow: part.dataWindow,
|
|
3205
|
+
displayWindow: part.displayWindow
|
|
3206
|
+
};
|
|
3207
|
+
}
|
|
3208
|
+
|
|
3209
|
+
// src/reader/expandSampledChannels.ts
|
|
3210
|
+
function clamp(value, min, max) {
|
|
3211
|
+
if (value < min) return min;
|
|
3212
|
+
if (value > max) return max;
|
|
3213
|
+
return value;
|
|
3214
|
+
}
|
|
3215
|
+
function buildNearestSampleMap(length, worldMin, sampleOrigin, sampling, sampledLength) {
|
|
3216
|
+
const output = new Int32Array(length);
|
|
3217
|
+
if (sampledLength <= 0) return output;
|
|
3218
|
+
for (let i = 0; i < length; i++) {
|
|
3219
|
+
const world = worldMin + i;
|
|
3220
|
+
const sample = Math.round((world - sampleOrigin) / sampling);
|
|
3221
|
+
output[i] = clamp(sample, 0, sampledLength - 1);
|
|
3222
|
+
}
|
|
3223
|
+
return output;
|
|
3224
|
+
}
|
|
3225
|
+
function expandSampledChannel(width, height, xMin, yMin, channel) {
|
|
3226
|
+
if (channel.sampledWidth === 0 || channel.sampledHeight === 0 || channel.data.length === 0) {
|
|
3227
|
+
return new Float32Array(width * height);
|
|
3228
|
+
}
|
|
3229
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
3230
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
3231
|
+
if (xSampling === 1 && ySampling === 1 && channel.sampledWidth === width && channel.sampledHeight === height && channel.sampleOriginX === xMin && channel.sampleOriginY === yMin) {
|
|
3232
|
+
return channel.data;
|
|
3233
|
+
}
|
|
3234
|
+
const output = new Float32Array(width * height);
|
|
3235
|
+
const xSampleMap = buildNearestSampleMap(
|
|
3236
|
+
width,
|
|
3237
|
+
xMin,
|
|
3238
|
+
channel.sampleOriginX,
|
|
3239
|
+
xSampling,
|
|
3240
|
+
channel.sampledWidth
|
|
3241
|
+
);
|
|
3242
|
+
const ySampleMap = buildNearestSampleMap(
|
|
3243
|
+
height,
|
|
3244
|
+
yMin,
|
|
3245
|
+
channel.sampleOriginY,
|
|
3246
|
+
ySampling,
|
|
3247
|
+
channel.sampledHeight
|
|
3248
|
+
);
|
|
3249
|
+
for (let y = 0; y < height; y++) {
|
|
3250
|
+
const sampleY = ySampleMap[y];
|
|
3251
|
+
const targetRow = y * width;
|
|
3252
|
+
const sourceRow = sampleY * channel.sampledWidth;
|
|
3253
|
+
for (let x = 0; x < width; x++) {
|
|
3254
|
+
output[targetRow + x] = channel.data[sourceRow + xSampleMap[x]];
|
|
3255
|
+
}
|
|
3256
|
+
}
|
|
3257
|
+
return output;
|
|
3258
|
+
}
|
|
3259
|
+
function expandDecodedPartChannels(decoded, dataWindow) {
|
|
3260
|
+
const output = {};
|
|
3261
|
+
for (const [name, channel] of Object.entries(decoded.channels)) {
|
|
3262
|
+
output[name] = expandSampledChannel(
|
|
3263
|
+
decoded.width,
|
|
3264
|
+
decoded.height,
|
|
3265
|
+
dataWindow.xMin,
|
|
3266
|
+
dataWindow.yMin,
|
|
3267
|
+
channel
|
|
3268
|
+
);
|
|
3269
|
+
}
|
|
3270
|
+
return output;
|
|
3271
|
+
}
|
|
3272
|
+
|
|
3273
|
+
// src/reader/rgbaChannels.ts
|
|
3274
|
+
function normalizeChannelName(name) {
|
|
3275
|
+
return name.split(".").pop()?.toUpperCase() ?? "";
|
|
3276
|
+
}
|
|
3277
|
+
function pickChannel(channels, aliases) {
|
|
3278
|
+
for (const alias of aliases) {
|
|
3279
|
+
const exact = channels[alias];
|
|
3280
|
+
if (exact) return exact;
|
|
3281
|
+
}
|
|
3282
|
+
const normalizedAliases = new Set(aliases.map((alias) => alias.toUpperCase()));
|
|
3283
|
+
for (const [name, channel] of Object.entries(channels)) {
|
|
3284
|
+
if (normalizedAliases.has(normalizeChannelName(name))) return channel;
|
|
3285
|
+
}
|
|
3286
|
+
return void 0;
|
|
3287
|
+
}
|
|
3288
|
+
function decodedPartToRgba(width, height, dataWindow, channels) {
|
|
3289
|
+
const expanded = expandDecodedPartChannels({ width, height, channels }, dataWindow);
|
|
3290
|
+
const fallback = Object.values(expanded)[0] ?? new Float32Array(width * height);
|
|
3291
|
+
const red = pickChannel(expanded, ["R", "RED"]) ?? fallback;
|
|
3292
|
+
const luma = pickChannel(expanded, ["Y", "LUMA", "LUMINANCE"]);
|
|
3293
|
+
const green = pickChannel(expanded, ["G", "GREEN"]) ?? luma ?? red;
|
|
3294
|
+
const blue = pickChannel(expanded, ["B", "BLUE"]) ?? luma ?? red;
|
|
3295
|
+
const alpha = pickChannel(expanded, ["A", "ALPHA"]);
|
|
3296
|
+
const pixelCount = width * height;
|
|
3297
|
+
const rgba = new Float32Array(pixelCount * 4);
|
|
3298
|
+
for (let index = 0; index < pixelCount; index += 1) {
|
|
3299
|
+
const offset = index * 4;
|
|
3300
|
+
rgba[offset] = Number.isFinite(red[index]) ? red[index] : 0;
|
|
3301
|
+
rgba[offset + 1] = Number.isFinite(green[index]) ? green[index] : rgba[offset];
|
|
3302
|
+
rgba[offset + 2] = Number.isFinite(blue[index]) ? blue[index] : rgba[offset];
|
|
3303
|
+
rgba[offset + 3] = alpha ? Math.max(0, alpha[index]) : 1;
|
|
3304
|
+
}
|
|
3305
|
+
return rgba;
|
|
3306
|
+
}
|
|
3307
|
+
|
|
3308
|
+
// src/reader/decodeExrRgba.ts
|
|
3309
|
+
function decodeExrRgba(buffer, options = {}) {
|
|
3310
|
+
const info = inspectExrImage(buffer, options);
|
|
3311
|
+
const decoded = decodeExrPart(buffer, info.structure, {
|
|
3312
|
+
partId: info.partId,
|
|
3313
|
+
predecodedZipBlocks: options.decode?.predecodedZipBlocks,
|
|
3314
|
+
predecodedDwaBlocks: options.decode?.predecodedDwaBlocks,
|
|
3315
|
+
onEvent: options.onEvent
|
|
3316
|
+
});
|
|
3317
|
+
return {
|
|
3318
|
+
width: info.width,
|
|
3319
|
+
height: info.height,
|
|
3320
|
+
rgba: decodedPartToRgba(info.width, info.height, info.dataWindow, decoded.channels),
|
|
3321
|
+
partId: info.partId,
|
|
3322
|
+
dataWindow: info.dataWindow,
|
|
3323
|
+
displayWindow: info.displayWindow,
|
|
3324
|
+
channels: info.part.channels,
|
|
3325
|
+
attributes: info.part.attributes
|
|
3326
|
+
};
|
|
3327
|
+
}
|
|
3328
|
+
|
|
3154
3329
|
// src/writer/sampling.ts
|
|
3155
3330
|
function modulo5(value, base) {
|
|
3156
3331
|
const result = value % base;
|
|
@@ -4776,11 +4951,114 @@ function writeExr(input, options = {}) {
|
|
|
4776
4951
|
});
|
|
4777
4952
|
return out;
|
|
4778
4953
|
}
|
|
4954
|
+
|
|
4955
|
+
// src/writer/encodeExrRgba.ts
|
|
4956
|
+
function pixelTypeForPrecision(precision) {
|
|
4957
|
+
return precision === "half" ? 1 : 2;
|
|
4958
|
+
}
|
|
4959
|
+
function extractRgbaChannel(rgba, offset) {
|
|
4960
|
+
const data = new Float32Array(rgba.length / 4);
|
|
4961
|
+
for (let index = 0; index < data.length; index += 1) {
|
|
4962
|
+
data[index] = rgba[index * 4 + offset];
|
|
4963
|
+
}
|
|
4964
|
+
return data;
|
|
4965
|
+
}
|
|
4966
|
+
function resolveDataWindow(image) {
|
|
4967
|
+
const dataWindow = image.dataWindow ?? {
|
|
4968
|
+
xMin: 0,
|
|
4969
|
+
yMin: 0,
|
|
4970
|
+
xMax: image.width - 1,
|
|
4971
|
+
yMax: image.height - 1
|
|
4972
|
+
};
|
|
4973
|
+
const width = dataWindow.xMax - dataWindow.xMin + 1;
|
|
4974
|
+
const height = dataWindow.yMax - dataWindow.yMin + 1;
|
|
4975
|
+
if (width !== image.width || height !== image.height) {
|
|
4976
|
+
throw new ExrError(
|
|
4977
|
+
"INVALID_WRITE_INPUT",
|
|
4978
|
+
"RGBA image dimensions do not match the EXR data window.",
|
|
4979
|
+
{
|
|
4980
|
+
width: image.width,
|
|
4981
|
+
height: image.height,
|
|
4982
|
+
dataWindowWidth: width,
|
|
4983
|
+
dataWindowHeight: height
|
|
4984
|
+
}
|
|
4985
|
+
);
|
|
4986
|
+
}
|
|
4987
|
+
return dataWindow;
|
|
4988
|
+
}
|
|
4989
|
+
function appendNamedChannels(channels, image) {
|
|
4990
|
+
const pixelCount = image.width * image.height;
|
|
4991
|
+
const names = new Set(channels.map((channel) => channel.name));
|
|
4992
|
+
for (const namedChannel of image.namedChannels ?? []) {
|
|
4993
|
+
const name = namedChannel.name.trim();
|
|
4994
|
+
if (!name || name.includes("\0")) {
|
|
4995
|
+
throw new ExrError("INVALID_WRITE_INPUT", "OpenEXR named channels require a valid name.");
|
|
4996
|
+
}
|
|
4997
|
+
if (names.has(name)) {
|
|
4998
|
+
throw new ExrError("INVALID_WRITE_INPUT", `OpenEXR channel name "${name}" is duplicated.`);
|
|
4999
|
+
}
|
|
5000
|
+
if (namedChannel.data.length !== pixelCount) {
|
|
5001
|
+
throw new ExrError(
|
|
5002
|
+
"INVALID_WRITE_INPUT",
|
|
5003
|
+
`OpenEXR channel "${name}" data length does not match the image dimensions.`,
|
|
5004
|
+
{ expected: pixelCount, actual: namedChannel.data.length }
|
|
5005
|
+
);
|
|
5006
|
+
}
|
|
5007
|
+
names.add(name);
|
|
5008
|
+
channels.push({
|
|
5009
|
+
name,
|
|
5010
|
+
pixelType: pixelTypeForPrecision(namedChannel.precision ?? "float"),
|
|
5011
|
+
data: namedChannel.data
|
|
5012
|
+
});
|
|
5013
|
+
}
|
|
5014
|
+
}
|
|
5015
|
+
function encodeExrRgba(image, options) {
|
|
5016
|
+
const pixelCount = image.width * image.height;
|
|
5017
|
+
if (image.width <= 0 || image.height <= 0 || image.rgba.length !== pixelCount * 4) {
|
|
5018
|
+
throw new ExrError(
|
|
5019
|
+
"INVALID_WRITE_INPUT",
|
|
5020
|
+
"OpenEXR RGBA data length does not match the image dimensions.",
|
|
5021
|
+
{ width: image.width, height: image.height, actual: image.rgba.length }
|
|
5022
|
+
);
|
|
5023
|
+
}
|
|
5024
|
+
const pixelType = pixelTypeForPrecision(options.precision);
|
|
5025
|
+
const channels = [];
|
|
5026
|
+
if (options.includeColorChannels !== false) {
|
|
5027
|
+
channels.push(
|
|
5028
|
+
{ name: "R", pixelType, data: extractRgbaChannel(image.rgba, 0) },
|
|
5029
|
+
{ name: "G", pixelType, data: extractRgbaChannel(image.rgba, 1) },
|
|
5030
|
+
{ name: "B", pixelType, data: extractRgbaChannel(image.rgba, 2) }
|
|
5031
|
+
);
|
|
5032
|
+
}
|
|
5033
|
+
if (options.includeAlpha) {
|
|
5034
|
+
channels.push({ name: "A", pixelType, data: extractRgbaChannel(image.rgba, 3) });
|
|
5035
|
+
}
|
|
5036
|
+
appendNamedChannels(channels, image);
|
|
5037
|
+
if (channels.length === 0) {
|
|
5038
|
+
throw new ExrError("INVALID_WRITE_INPUT", "OpenEXR export requires at least one channel.");
|
|
5039
|
+
}
|
|
5040
|
+
return writeExr({
|
|
5041
|
+
parts: [
|
|
5042
|
+
{
|
|
5043
|
+
compression: options.compression ?? 3,
|
|
5044
|
+
dataWindow: resolveDataWindow(image),
|
|
5045
|
+
...image.displayWindow ? { displayWindow: image.displayWindow } : {},
|
|
5046
|
+
channels,
|
|
5047
|
+
...options.attributes ? { attributes: options.attributes } : {}
|
|
5048
|
+
}
|
|
5049
|
+
]
|
|
5050
|
+
});
|
|
5051
|
+
}
|
|
4779
5052
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4780
5053
|
0 && (module.exports = {
|
|
4781
5054
|
EXR_EVENT_CODES,
|
|
4782
5055
|
ExrError,
|
|
4783
5056
|
decodeExrPart,
|
|
5057
|
+
decodeExrRgba,
|
|
5058
|
+
encodeExrRgba,
|
|
5059
|
+
expandDecodedPartChannels,
|
|
5060
|
+
expandSampledChannel,
|
|
5061
|
+
inspectExrImage,
|
|
4784
5062
|
isExrError,
|
|
4785
5063
|
parseExr,
|
|
4786
5064
|
parseExrStructure,
|