@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/README.md +34 -3
- package/dist/browser/dwaDecodeWorker.js.map +1 -1
- package/dist/browser/index.d.ts +21 -1
- package/dist/browser/index.js +467 -6
- package/dist/browser/index.js.map +1 -1
- package/dist/index.cjs +300 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +62 -2
- package/dist/index.d.ts +62 -2
- package/dist/index.js +295 -5
- 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,
|
|
@@ -2802,7 +2807,7 @@ function toNumberOffset(low, high) {
|
|
|
2802
2807
|
}
|
|
2803
2808
|
return Number(combined);
|
|
2804
2809
|
}
|
|
2805
|
-
function buildChannelMeta(part) {
|
|
2810
|
+
function buildChannelMeta(part, selectedChannelNames) {
|
|
2806
2811
|
if (!part.dataWindow) return [];
|
|
2807
2812
|
const { xMin, xMax, yMin, yMax } = part.dataWindow;
|
|
2808
2813
|
return part.channels.map((channel) => {
|
|
@@ -2813,8 +2818,10 @@ function buildChannelMeta(part) {
|
|
|
2813
2818
|
const sampledWidth = countSamplesInRange4(xMin, xMax, xSampling);
|
|
2814
2819
|
const sampledHeight = countSamplesInRange4(yMin, yMax, ySampling);
|
|
2815
2820
|
const bytesPerSample = channel.pixelType === 1 ? 2 : 4;
|
|
2821
|
+
const selected = selectedChannelNames?.has(channel.name) ?? true;
|
|
2816
2822
|
return {
|
|
2817
2823
|
channel,
|
|
2824
|
+
selected,
|
|
2818
2825
|
pixelType: channel.pixelType,
|
|
2819
2826
|
ySampling,
|
|
2820
2827
|
bytesPerSample,
|
|
@@ -2823,7 +2830,7 @@ function buildChannelMeta(part) {
|
|
|
2823
2830
|
sampledHeight,
|
|
2824
2831
|
sampleOriginX,
|
|
2825
2832
|
sampleOriginY,
|
|
2826
|
-
data: new Float32Array(sampledWidth * sampledHeight)
|
|
2833
|
+
data: selected ? new Float32Array(sampledWidth * sampledHeight) : new Float32Array(0)
|
|
2827
2834
|
};
|
|
2828
2835
|
});
|
|
2829
2836
|
}
|
|
@@ -2975,7 +2982,18 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
2975
2982
|
ms: (nowMs2() - tOffsets).toFixed(3)
|
|
2976
2983
|
}
|
|
2977
2984
|
});
|
|
2978
|
-
const
|
|
2985
|
+
const selectedChannelNames = options.channelNames ? new Set(options.channelNames) : void 0;
|
|
2986
|
+
if (selectedChannelNames) {
|
|
2987
|
+
for (const name of selectedChannelNames) {
|
|
2988
|
+
if (!part.channels.some((channel) => channel.name === name)) {
|
|
2989
|
+
throw new ExrError("CHANNEL_NOT_FOUND", `Channel ${name} was not found in EXR part.`, {
|
|
2990
|
+
partId: part.id,
|
|
2991
|
+
channel: name
|
|
2992
|
+
});
|
|
2993
|
+
}
|
|
2994
|
+
}
|
|
2995
|
+
}
|
|
2996
|
+
const channelMeta = buildChannelMeta(part, selectedChannelNames);
|
|
2979
2997
|
const zipScratch = {
|
|
2980
2998
|
inflate: new Uint8Array(0),
|
|
2981
2999
|
output: new Uint8Array(0)
|
|
@@ -3082,8 +3100,10 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
3082
3100
|
blockPointer += byteLength;
|
|
3083
3101
|
continue;
|
|
3084
3102
|
}
|
|
3085
|
-
|
|
3086
|
-
|
|
3103
|
+
if (meta.selected) {
|
|
3104
|
+
const rowOffset = sampledRow * meta.sampledWidth;
|
|
3105
|
+
decodeRowIntoChannel(meta, blockData, blockPointer, rowOffset, blockView);
|
|
3106
|
+
}
|
|
3087
3107
|
blockPointer += byteLength;
|
|
3088
3108
|
}
|
|
3089
3109
|
}
|
|
@@ -3103,6 +3123,7 @@ function decodeExrPart(bufferInput, structure, options) {
|
|
|
3103
3123
|
}
|
|
3104
3124
|
const decodedChannels = {};
|
|
3105
3125
|
for (const meta of channelMeta) {
|
|
3126
|
+
if (!meta.selected) continue;
|
|
3106
3127
|
decodedChannels[meta.channel.name] = {
|
|
3107
3128
|
pixelType: meta.channel.pixelType,
|
|
3108
3129
|
xSampling: meta.channel.xSampling,
|
|
@@ -3151,6 +3172,176 @@ function readExr(buffer, options = {}) {
|
|
|
3151
3172
|
};
|
|
3152
3173
|
}
|
|
3153
3174
|
|
|
3175
|
+
// src/reader/exrImage.ts
|
|
3176
|
+
function selectExrImagePart(structure, partId) {
|
|
3177
|
+
if (partId !== void 0) {
|
|
3178
|
+
const part2 = structure.parts.find((candidate) => candidate.id === partId);
|
|
3179
|
+
if (!part2) {
|
|
3180
|
+
throw new ExrError("PART_NOT_FOUND", `Part ${partId} was not found in EXR structure.`, {
|
|
3181
|
+
partId
|
|
3182
|
+
});
|
|
3183
|
+
}
|
|
3184
|
+
if (!part2.dataWindow) {
|
|
3185
|
+
throw new ExrError("MISSING_DATA_WINDOW", `Part ${partId} is missing a data window.`, {
|
|
3186
|
+
partId
|
|
3187
|
+
});
|
|
3188
|
+
}
|
|
3189
|
+
return part2;
|
|
3190
|
+
}
|
|
3191
|
+
const part = structure.parts.find((candidate) => candidate.dataWindow);
|
|
3192
|
+
if (!part?.dataWindow) {
|
|
3193
|
+
throw new ExrError(
|
|
3194
|
+
"MISSING_DATA_WINDOW",
|
|
3195
|
+
"EXR file does not contain a decodable scanline part."
|
|
3196
|
+
);
|
|
3197
|
+
}
|
|
3198
|
+
return part;
|
|
3199
|
+
}
|
|
3200
|
+
function getExrPartDimensions(part) {
|
|
3201
|
+
if (!part.dataWindow) {
|
|
3202
|
+
throw new ExrError("MISSING_DATA_WINDOW", `Part ${part.id} is missing a data window.`, {
|
|
3203
|
+
partId: part.id
|
|
3204
|
+
});
|
|
3205
|
+
}
|
|
3206
|
+
return {
|
|
3207
|
+
width: part.dataWindow.xMax - part.dataWindow.xMin + 1,
|
|
3208
|
+
height: part.dataWindow.yMax - part.dataWindow.yMin + 1
|
|
3209
|
+
};
|
|
3210
|
+
}
|
|
3211
|
+
function inspectExrImage(buffer, options = {}) {
|
|
3212
|
+
const structure = parseExrStructure(buffer, { onEvent: options.onEvent });
|
|
3213
|
+
const part = selectExrImagePart(structure, options.partId);
|
|
3214
|
+
const dimensions = getExrPartDimensions(part);
|
|
3215
|
+
return {
|
|
3216
|
+
structure,
|
|
3217
|
+
part,
|
|
3218
|
+
partId: part.id,
|
|
3219
|
+
...dimensions,
|
|
3220
|
+
dataWindow: part.dataWindow,
|
|
3221
|
+
displayWindow: part.displayWindow
|
|
3222
|
+
};
|
|
3223
|
+
}
|
|
3224
|
+
|
|
3225
|
+
// src/reader/expandSampledChannels.ts
|
|
3226
|
+
function clamp(value, min, max) {
|
|
3227
|
+
if (value < min) return min;
|
|
3228
|
+
if (value > max) return max;
|
|
3229
|
+
return value;
|
|
3230
|
+
}
|
|
3231
|
+
function buildNearestSampleMap(length, worldMin, sampleOrigin, sampling, sampledLength) {
|
|
3232
|
+
const output = new Int32Array(length);
|
|
3233
|
+
if (sampledLength <= 0) return output;
|
|
3234
|
+
for (let i = 0; i < length; i++) {
|
|
3235
|
+
const world = worldMin + i;
|
|
3236
|
+
const sample = Math.round((world - sampleOrigin) / sampling);
|
|
3237
|
+
output[i] = clamp(sample, 0, sampledLength - 1);
|
|
3238
|
+
}
|
|
3239
|
+
return output;
|
|
3240
|
+
}
|
|
3241
|
+
function expandSampledChannel(width, height, xMin, yMin, channel) {
|
|
3242
|
+
if (channel.sampledWidth === 0 || channel.sampledHeight === 0 || channel.data.length === 0) {
|
|
3243
|
+
return new Float32Array(width * height);
|
|
3244
|
+
}
|
|
3245
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
3246
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
3247
|
+
if (xSampling === 1 && ySampling === 1 && channel.sampledWidth === width && channel.sampledHeight === height && channel.sampleOriginX === xMin && channel.sampleOriginY === yMin) {
|
|
3248
|
+
return channel.data;
|
|
3249
|
+
}
|
|
3250
|
+
const output = new Float32Array(width * height);
|
|
3251
|
+
const xSampleMap = buildNearestSampleMap(
|
|
3252
|
+
width,
|
|
3253
|
+
xMin,
|
|
3254
|
+
channel.sampleOriginX,
|
|
3255
|
+
xSampling,
|
|
3256
|
+
channel.sampledWidth
|
|
3257
|
+
);
|
|
3258
|
+
const ySampleMap = buildNearestSampleMap(
|
|
3259
|
+
height,
|
|
3260
|
+
yMin,
|
|
3261
|
+
channel.sampleOriginY,
|
|
3262
|
+
ySampling,
|
|
3263
|
+
channel.sampledHeight
|
|
3264
|
+
);
|
|
3265
|
+
for (let y = 0; y < height; y++) {
|
|
3266
|
+
const sampleY = ySampleMap[y];
|
|
3267
|
+
const targetRow = y * width;
|
|
3268
|
+
const sourceRow = sampleY * channel.sampledWidth;
|
|
3269
|
+
for (let x = 0; x < width; x++) {
|
|
3270
|
+
output[targetRow + x] = channel.data[sourceRow + xSampleMap[x]];
|
|
3271
|
+
}
|
|
3272
|
+
}
|
|
3273
|
+
return output;
|
|
3274
|
+
}
|
|
3275
|
+
function expandDecodedPartChannels(decoded, dataWindow) {
|
|
3276
|
+
const output = {};
|
|
3277
|
+
for (const [name, channel] of Object.entries(decoded.channels)) {
|
|
3278
|
+
output[name] = expandSampledChannel(
|
|
3279
|
+
decoded.width,
|
|
3280
|
+
decoded.height,
|
|
3281
|
+
dataWindow.xMin,
|
|
3282
|
+
dataWindow.yMin,
|
|
3283
|
+
channel
|
|
3284
|
+
);
|
|
3285
|
+
}
|
|
3286
|
+
return output;
|
|
3287
|
+
}
|
|
3288
|
+
|
|
3289
|
+
// src/reader/rgbaChannels.ts
|
|
3290
|
+
function normalizeChannelName(name) {
|
|
3291
|
+
return name.split(".").pop()?.toUpperCase() ?? "";
|
|
3292
|
+
}
|
|
3293
|
+
function pickChannel(channels, aliases) {
|
|
3294
|
+
for (const alias of aliases) {
|
|
3295
|
+
const exact = channels[alias];
|
|
3296
|
+
if (exact) return exact;
|
|
3297
|
+
}
|
|
3298
|
+
const normalizedAliases = new Set(aliases.map((alias) => alias.toUpperCase()));
|
|
3299
|
+
for (const [name, channel] of Object.entries(channels)) {
|
|
3300
|
+
if (normalizedAliases.has(normalizeChannelName(name))) return channel;
|
|
3301
|
+
}
|
|
3302
|
+
return void 0;
|
|
3303
|
+
}
|
|
3304
|
+
function decodedPartToRgba(width, height, dataWindow, channels) {
|
|
3305
|
+
const expanded = expandDecodedPartChannels({ width, height, channels }, dataWindow);
|
|
3306
|
+
const fallback = Object.values(expanded)[0] ?? new Float32Array(width * height);
|
|
3307
|
+
const red = pickChannel(expanded, ["R", "RED"]) ?? fallback;
|
|
3308
|
+
const luma = pickChannel(expanded, ["Y", "LUMA", "LUMINANCE"]);
|
|
3309
|
+
const green = pickChannel(expanded, ["G", "GREEN"]) ?? luma ?? red;
|
|
3310
|
+
const blue = pickChannel(expanded, ["B", "BLUE"]) ?? luma ?? red;
|
|
3311
|
+
const alpha = pickChannel(expanded, ["A", "ALPHA"]);
|
|
3312
|
+
const pixelCount = width * height;
|
|
3313
|
+
const rgba = new Float32Array(pixelCount * 4);
|
|
3314
|
+
for (let index = 0; index < pixelCount; index += 1) {
|
|
3315
|
+
const offset = index * 4;
|
|
3316
|
+
rgba[offset] = Number.isFinite(red[index]) ? red[index] : 0;
|
|
3317
|
+
rgba[offset + 1] = Number.isFinite(green[index]) ? green[index] : rgba[offset];
|
|
3318
|
+
rgba[offset + 2] = Number.isFinite(blue[index]) ? blue[index] : rgba[offset];
|
|
3319
|
+
rgba[offset + 3] = alpha ? Math.max(0, alpha[index]) : 1;
|
|
3320
|
+
}
|
|
3321
|
+
return rgba;
|
|
3322
|
+
}
|
|
3323
|
+
|
|
3324
|
+
// src/reader/decodeExrRgba.ts
|
|
3325
|
+
function decodeExrRgba(buffer, options = {}) {
|
|
3326
|
+
const info = inspectExrImage(buffer, options);
|
|
3327
|
+
const decoded = decodeExrPart(buffer, info.structure, {
|
|
3328
|
+
partId: info.partId,
|
|
3329
|
+
predecodedZipBlocks: options.decode?.predecodedZipBlocks,
|
|
3330
|
+
predecodedDwaBlocks: options.decode?.predecodedDwaBlocks,
|
|
3331
|
+
onEvent: options.onEvent
|
|
3332
|
+
});
|
|
3333
|
+
return {
|
|
3334
|
+
width: info.width,
|
|
3335
|
+
height: info.height,
|
|
3336
|
+
rgba: decodedPartToRgba(info.width, info.height, info.dataWindow, decoded.channels),
|
|
3337
|
+
partId: info.partId,
|
|
3338
|
+
dataWindow: info.dataWindow,
|
|
3339
|
+
displayWindow: info.displayWindow,
|
|
3340
|
+
channels: info.part.channels,
|
|
3341
|
+
attributes: info.part.attributes
|
|
3342
|
+
};
|
|
3343
|
+
}
|
|
3344
|
+
|
|
3154
3345
|
// src/writer/sampling.ts
|
|
3155
3346
|
function modulo5(value, base) {
|
|
3156
3347
|
const result = value % base;
|
|
@@ -4776,11 +4967,115 @@ function writeExr(input, options = {}) {
|
|
|
4776
4967
|
});
|
|
4777
4968
|
return out;
|
|
4778
4969
|
}
|
|
4970
|
+
|
|
4971
|
+
// src/writer/encodeExrRgba.ts
|
|
4972
|
+
function pixelTypeForPrecision(precision) {
|
|
4973
|
+
if (precision === "uint") return 0;
|
|
4974
|
+
return precision === "half" ? 1 : 2;
|
|
4975
|
+
}
|
|
4976
|
+
function extractRgbaChannel(rgba, offset) {
|
|
4977
|
+
const data = new Float32Array(rgba.length / 4);
|
|
4978
|
+
for (let index = 0; index < data.length; index += 1) {
|
|
4979
|
+
data[index] = rgba[index * 4 + offset];
|
|
4980
|
+
}
|
|
4981
|
+
return data;
|
|
4982
|
+
}
|
|
4983
|
+
function resolveDataWindow(image) {
|
|
4984
|
+
const dataWindow = image.dataWindow ?? {
|
|
4985
|
+
xMin: 0,
|
|
4986
|
+
yMin: 0,
|
|
4987
|
+
xMax: image.width - 1,
|
|
4988
|
+
yMax: image.height - 1
|
|
4989
|
+
};
|
|
4990
|
+
const width = dataWindow.xMax - dataWindow.xMin + 1;
|
|
4991
|
+
const height = dataWindow.yMax - dataWindow.yMin + 1;
|
|
4992
|
+
if (width !== image.width || height !== image.height) {
|
|
4993
|
+
throw new ExrError(
|
|
4994
|
+
"INVALID_WRITE_INPUT",
|
|
4995
|
+
"RGBA image dimensions do not match the EXR data window.",
|
|
4996
|
+
{
|
|
4997
|
+
width: image.width,
|
|
4998
|
+
height: image.height,
|
|
4999
|
+
dataWindowWidth: width,
|
|
5000
|
+
dataWindowHeight: height
|
|
5001
|
+
}
|
|
5002
|
+
);
|
|
5003
|
+
}
|
|
5004
|
+
return dataWindow;
|
|
5005
|
+
}
|
|
5006
|
+
function appendNamedChannels(channels, image) {
|
|
5007
|
+
const pixelCount = image.width * image.height;
|
|
5008
|
+
const names = new Set(channels.map((channel) => channel.name));
|
|
5009
|
+
for (const namedChannel of image.namedChannels ?? []) {
|
|
5010
|
+
const name = namedChannel.name.trim();
|
|
5011
|
+
if (!name || name.includes("\0")) {
|
|
5012
|
+
throw new ExrError("INVALID_WRITE_INPUT", "OpenEXR named channels require a valid name.");
|
|
5013
|
+
}
|
|
5014
|
+
if (names.has(name)) {
|
|
5015
|
+
throw new ExrError("INVALID_WRITE_INPUT", `OpenEXR channel name "${name}" is duplicated.`);
|
|
5016
|
+
}
|
|
5017
|
+
if (namedChannel.data.length !== pixelCount) {
|
|
5018
|
+
throw new ExrError(
|
|
5019
|
+
"INVALID_WRITE_INPUT",
|
|
5020
|
+
`OpenEXR channel "${name}" data length does not match the image dimensions.`,
|
|
5021
|
+
{ expected: pixelCount, actual: namedChannel.data.length }
|
|
5022
|
+
);
|
|
5023
|
+
}
|
|
5024
|
+
names.add(name);
|
|
5025
|
+
channels.push({
|
|
5026
|
+
name,
|
|
5027
|
+
pixelType: pixelTypeForPrecision(namedChannel.precision ?? "float"),
|
|
5028
|
+
data: namedChannel.data
|
|
5029
|
+
});
|
|
5030
|
+
}
|
|
5031
|
+
}
|
|
5032
|
+
function encodeExrRgba(image, options) {
|
|
5033
|
+
const pixelCount = image.width * image.height;
|
|
5034
|
+
if (image.width <= 0 || image.height <= 0 || image.rgba.length !== pixelCount * 4) {
|
|
5035
|
+
throw new ExrError(
|
|
5036
|
+
"INVALID_WRITE_INPUT",
|
|
5037
|
+
"OpenEXR RGBA data length does not match the image dimensions.",
|
|
5038
|
+
{ width: image.width, height: image.height, actual: image.rgba.length }
|
|
5039
|
+
);
|
|
5040
|
+
}
|
|
5041
|
+
const pixelType = pixelTypeForPrecision(options.precision);
|
|
5042
|
+
const channels = [];
|
|
5043
|
+
if (options.includeColorChannels !== false) {
|
|
5044
|
+
channels.push(
|
|
5045
|
+
{ name: "R", pixelType, data: extractRgbaChannel(image.rgba, 0) },
|
|
5046
|
+
{ name: "G", pixelType, data: extractRgbaChannel(image.rgba, 1) },
|
|
5047
|
+
{ name: "B", pixelType, data: extractRgbaChannel(image.rgba, 2) }
|
|
5048
|
+
);
|
|
5049
|
+
}
|
|
5050
|
+
if (options.includeAlpha) {
|
|
5051
|
+
channels.push({ name: "A", pixelType, data: extractRgbaChannel(image.rgba, 3) });
|
|
5052
|
+
}
|
|
5053
|
+
appendNamedChannels(channels, image);
|
|
5054
|
+
if (channels.length === 0) {
|
|
5055
|
+
throw new ExrError("INVALID_WRITE_INPUT", "OpenEXR export requires at least one channel.");
|
|
5056
|
+
}
|
|
5057
|
+
return writeExr({
|
|
5058
|
+
parts: [
|
|
5059
|
+
{
|
|
5060
|
+
compression: options.compression ?? 3,
|
|
5061
|
+
dataWindow: resolveDataWindow(image),
|
|
5062
|
+
...image.displayWindow ? { displayWindow: image.displayWindow } : {},
|
|
5063
|
+
channels,
|
|
5064
|
+
...options.attributes ? { attributes: options.attributes } : {}
|
|
5065
|
+
}
|
|
5066
|
+
]
|
|
5067
|
+
});
|
|
5068
|
+
}
|
|
4779
5069
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4780
5070
|
0 && (module.exports = {
|
|
4781
5071
|
EXR_EVENT_CODES,
|
|
4782
5072
|
ExrError,
|
|
4783
5073
|
decodeExrPart,
|
|
5074
|
+
decodeExrRgba,
|
|
5075
|
+
encodeExrRgba,
|
|
5076
|
+
expandDecodedPartChannels,
|
|
5077
|
+
expandSampledChannel,
|
|
5078
|
+
inspectExrImage,
|
|
4784
5079
|
isExrError,
|
|
4785
5080
|
parseExr,
|
|
4786
5081
|
parseExrStructure,
|