@bb-studio/exr 0.1.1 → 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 +46 -6
- package/dist/browser/dwaDecodeWorker.js.map +1 -1
- 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 +453 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +81 -1
- package/dist/index.d.ts +81 -1
- package/dist/index.js +448 -11
- package/dist/index.js.map +1 -1
- package/package.json +63 -63
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,
|
|
@@ -135,6 +140,12 @@ var BinaryReader = class {
|
|
|
135
140
|
this.offset += 4;
|
|
136
141
|
return value;
|
|
137
142
|
}
|
|
143
|
+
readFloat32() {
|
|
144
|
+
this.ensureAvailable(4, "f32", "float32");
|
|
145
|
+
const value = this.view.getFloat32(this.offset, true);
|
|
146
|
+
this.offset += 4;
|
|
147
|
+
return value;
|
|
148
|
+
}
|
|
138
149
|
readCString(context) {
|
|
139
150
|
let value = "";
|
|
140
151
|
while (this.offset < this.view.byteLength) {
|
|
@@ -271,10 +282,19 @@ function parsePartHeader(reader, index) {
|
|
|
271
282
|
value = reader.readInt32();
|
|
272
283
|
} else if (type === "float") {
|
|
273
284
|
ensureExactAttributeSize(name, type, size, 4);
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
value =
|
|
285
|
+
value = reader.readFloat32();
|
|
286
|
+
} else if (type === "chromaticities") {
|
|
287
|
+
ensureExactAttributeSize(name, type, size, 32);
|
|
288
|
+
value = {
|
|
289
|
+
redX: reader.readFloat32(),
|
|
290
|
+
redY: reader.readFloat32(),
|
|
291
|
+
greenX: reader.readFloat32(),
|
|
292
|
+
greenY: reader.readFloat32(),
|
|
293
|
+
blueX: reader.readFloat32(),
|
|
294
|
+
blueY: reader.readFloat32(),
|
|
295
|
+
whiteX: reader.readFloat32(),
|
|
296
|
+
whiteY: reader.readFloat32()
|
|
297
|
+
};
|
|
278
298
|
} else {
|
|
279
299
|
reader.skip(size, `unknown attribute ${name}`);
|
|
280
300
|
value = `<${type} data>`;
|
|
@@ -3136,6 +3156,176 @@ function readExr(buffer, options = {}) {
|
|
|
3136
3156
|
};
|
|
3137
3157
|
}
|
|
3138
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
|
+
|
|
3139
3329
|
// src/writer/sampling.ts
|
|
3140
3330
|
function modulo5(value, base) {
|
|
3141
3331
|
const result = value % base;
|
|
@@ -4220,6 +4410,25 @@ var EXR_VERSION = 2;
|
|
|
4220
4410
|
var EXR_MULTIPART_FLAG = 16;
|
|
4221
4411
|
var UINT32_MAX2 = 4294967295;
|
|
4222
4412
|
var UTF8_ENCODER = new TextEncoder();
|
|
4413
|
+
var RESERVED_ATTRIBUTE_NAMES = /* @__PURE__ */ new Set([
|
|
4414
|
+
"channels",
|
|
4415
|
+
"compression",
|
|
4416
|
+
"dataWindow",
|
|
4417
|
+
"displayWindow",
|
|
4418
|
+
"lineOrder",
|
|
4419
|
+
"name",
|
|
4420
|
+
"type"
|
|
4421
|
+
]);
|
|
4422
|
+
var CHROMATICITY_FIELDS = [
|
|
4423
|
+
"redX",
|
|
4424
|
+
"redY",
|
|
4425
|
+
"greenX",
|
|
4426
|
+
"greenY",
|
|
4427
|
+
"blueX",
|
|
4428
|
+
"blueY",
|
|
4429
|
+
"whiteX",
|
|
4430
|
+
"whiteY"
|
|
4431
|
+
];
|
|
4223
4432
|
var ByteWriter = class {
|
|
4224
4433
|
bytes = [];
|
|
4225
4434
|
get length() {
|
|
@@ -4314,9 +4523,13 @@ function normalizeChannel(channel, partId, dataWindow) {
|
|
|
4314
4523
|
}
|
|
4315
4524
|
const pixelType = channel.pixelType;
|
|
4316
4525
|
if (pixelType !== 0 && pixelType !== 1 && pixelType !== 2) {
|
|
4317
|
-
throw new ExrError(
|
|
4318
|
-
|
|
4319
|
-
|
|
4526
|
+
throw new ExrError(
|
|
4527
|
+
"INVALID_WRITE_INPUT",
|
|
4528
|
+
`Unsupported channel pixel type for ${channel.name}.`,
|
|
4529
|
+
{
|
|
4530
|
+
partId
|
|
4531
|
+
}
|
|
4532
|
+
);
|
|
4320
4533
|
}
|
|
4321
4534
|
const xSampling = channel.xSampling ?? 1;
|
|
4322
4535
|
const ySampling = channel.ySampling ?? 1;
|
|
@@ -4335,9 +4548,13 @@ function normalizeChannel(channel, partId, dataWindow) {
|
|
|
4335
4548
|
const sampledHeight = countSamplesInRange5(dataWindow.yMin, dataWindow.yMax, ySampling);
|
|
4336
4549
|
const expectedLength = sampledWidth * sampledHeight;
|
|
4337
4550
|
if (!(channel.data instanceof Float32Array)) {
|
|
4338
|
-
throw new ExrError(
|
|
4339
|
-
|
|
4340
|
-
|
|
4551
|
+
throw new ExrError(
|
|
4552
|
+
"INVALID_WRITE_INPUT",
|
|
4553
|
+
`Channel ${channel.name} data must be Float32Array.`,
|
|
4554
|
+
{
|
|
4555
|
+
partId
|
|
4556
|
+
}
|
|
4557
|
+
);
|
|
4341
4558
|
}
|
|
4342
4559
|
if (channel.data.length !== expectedLength) {
|
|
4343
4560
|
throw new ExrError("INVALID_WRITE_INPUT", `Channel ${channel.name} has invalid data length.`, {
|
|
@@ -4385,6 +4602,7 @@ function normalizeParts(input) {
|
|
|
4385
4602
|
const chunkCount = Math.ceil(height / linesPerBlock);
|
|
4386
4603
|
const includeNameAttribute = isMultipart || typeof part.name === "string";
|
|
4387
4604
|
const includeTypeAttribute = isMultipart || typeof part.type === "string";
|
|
4605
|
+
const attributes = normalizeAttributes(part.attributes, index);
|
|
4388
4606
|
return {
|
|
4389
4607
|
id: index,
|
|
4390
4608
|
compression,
|
|
@@ -4396,10 +4614,90 @@ function normalizeParts(input) {
|
|
|
4396
4614
|
linesPerBlock,
|
|
4397
4615
|
chunkCount,
|
|
4398
4616
|
includeNameAttribute,
|
|
4399
|
-
includeTypeAttribute
|
|
4617
|
+
includeTypeAttribute,
|
|
4618
|
+
attributes
|
|
4400
4619
|
};
|
|
4401
4620
|
});
|
|
4402
4621
|
}
|
|
4622
|
+
function normalizeAttributes(attributes, partId) {
|
|
4623
|
+
if (attributes === void 0) return [];
|
|
4624
|
+
if (!attributes || typeof attributes !== "object" || Array.isArray(attributes)) {
|
|
4625
|
+
throw new ExrError("INVALID_WRITE_INPUT", "Part attributes must be an object.", { partId });
|
|
4626
|
+
}
|
|
4627
|
+
return Object.entries(attributes).map(([name, attribute]) => {
|
|
4628
|
+
if (!name || name.includes("\0")) {
|
|
4629
|
+
throw new ExrError("INVALID_WRITE_INPUT", "Attribute names must be non-empty C strings.", {
|
|
4630
|
+
partId,
|
|
4631
|
+
attribute: name
|
|
4632
|
+
});
|
|
4633
|
+
}
|
|
4634
|
+
if (RESERVED_ATTRIBUTE_NAMES.has(name)) {
|
|
4635
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name} is managed by the EXR writer.`, {
|
|
4636
|
+
partId,
|
|
4637
|
+
attribute: name
|
|
4638
|
+
});
|
|
4639
|
+
}
|
|
4640
|
+
validateAttribute(name, attribute, partId);
|
|
4641
|
+
return [name, attribute];
|
|
4642
|
+
});
|
|
4643
|
+
}
|
|
4644
|
+
function validateFiniteAttributeNumber(name, value, partId, field = "value") {
|
|
4645
|
+
if (!Number.isFinite(value)) {
|
|
4646
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name}.${field} must be finite.`, {
|
|
4647
|
+
partId,
|
|
4648
|
+
attribute: name,
|
|
4649
|
+
field
|
|
4650
|
+
});
|
|
4651
|
+
}
|
|
4652
|
+
}
|
|
4653
|
+
function validateAttribute(name, attribute, partId) {
|
|
4654
|
+
if (!attribute || typeof attribute !== "object") {
|
|
4655
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name} is invalid.`, {
|
|
4656
|
+
partId,
|
|
4657
|
+
attribute: name
|
|
4658
|
+
});
|
|
4659
|
+
}
|
|
4660
|
+
if (attribute.type === "string") {
|
|
4661
|
+
if (typeof attribute.value !== "string") {
|
|
4662
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name}.value must be a string.`, {
|
|
4663
|
+
partId,
|
|
4664
|
+
attribute: name
|
|
4665
|
+
});
|
|
4666
|
+
}
|
|
4667
|
+
return;
|
|
4668
|
+
}
|
|
4669
|
+
if (attribute.type === "int") {
|
|
4670
|
+
validateFiniteAttributeNumber(name, attribute.value, partId);
|
|
4671
|
+
if (!Number.isInteger(attribute.value) || attribute.value < -2147483648 || attribute.value > 2147483647) {
|
|
4672
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name}.value must be an int32.`, {
|
|
4673
|
+
partId,
|
|
4674
|
+
attribute: name
|
|
4675
|
+
});
|
|
4676
|
+
}
|
|
4677
|
+
return;
|
|
4678
|
+
}
|
|
4679
|
+
if (attribute.type === "float") {
|
|
4680
|
+
validateFiniteAttributeNumber(name, attribute.value, partId);
|
|
4681
|
+
return;
|
|
4682
|
+
}
|
|
4683
|
+
if (attribute.type === "chromaticities") {
|
|
4684
|
+
if (!attribute.value || typeof attribute.value !== "object") {
|
|
4685
|
+
throw new ExrError(
|
|
4686
|
+
"INVALID_WRITE_INPUT",
|
|
4687
|
+
`Attribute ${name}.value must contain chromaticities.`,
|
|
4688
|
+
{ partId, attribute: name }
|
|
4689
|
+
);
|
|
4690
|
+
}
|
|
4691
|
+
for (const field of CHROMATICITY_FIELDS) {
|
|
4692
|
+
validateFiniteAttributeNumber(name, attribute.value[field], partId, field);
|
|
4693
|
+
}
|
|
4694
|
+
return;
|
|
4695
|
+
}
|
|
4696
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name} has an unsupported type.`, {
|
|
4697
|
+
partId,
|
|
4698
|
+
attribute: name
|
|
4699
|
+
});
|
|
4700
|
+
}
|
|
4403
4701
|
function writeAttribute(writer, name, type, payload) {
|
|
4404
4702
|
writer.writeCString(name);
|
|
4405
4703
|
writer.writeCString(type);
|
|
@@ -4414,12 +4712,50 @@ function writeInt32Payload(values) {
|
|
|
4414
4712
|
}
|
|
4415
4713
|
return out;
|
|
4416
4714
|
}
|
|
4715
|
+
function writeFloat32Payload(values) {
|
|
4716
|
+
const out = new Uint8Array(values.length * 4);
|
|
4717
|
+
const view = new DataView(out.buffer);
|
|
4718
|
+
for (let i = 0; i < values.length; i++) {
|
|
4719
|
+
view.setFloat32(i * 4, values[i], true);
|
|
4720
|
+
}
|
|
4721
|
+
return out;
|
|
4722
|
+
}
|
|
4417
4723
|
function writeCompressionPayload(compression) {
|
|
4418
4724
|
return Uint8Array.of(compression & 255);
|
|
4419
4725
|
}
|
|
4420
4726
|
function writeStringPayload(value) {
|
|
4421
4727
|
return UTF8_ENCODER.encode(value);
|
|
4422
4728
|
}
|
|
4729
|
+
function writeCustomAttribute(writer, name, attribute) {
|
|
4730
|
+
if (attribute.type === "string") {
|
|
4731
|
+
writeAttribute(writer, name, attribute.type, writeStringPayload(attribute.value));
|
|
4732
|
+
return;
|
|
4733
|
+
}
|
|
4734
|
+
if (attribute.type === "int") {
|
|
4735
|
+
writeAttribute(writer, name, attribute.type, writeInt32Payload([attribute.value]));
|
|
4736
|
+
return;
|
|
4737
|
+
}
|
|
4738
|
+
if (attribute.type === "float") {
|
|
4739
|
+
writeAttribute(writer, name, attribute.type, writeFloat32Payload([attribute.value]));
|
|
4740
|
+
return;
|
|
4741
|
+
}
|
|
4742
|
+
const value = attribute.value;
|
|
4743
|
+
writeAttribute(
|
|
4744
|
+
writer,
|
|
4745
|
+
name,
|
|
4746
|
+
attribute.type,
|
|
4747
|
+
writeFloat32Payload([
|
|
4748
|
+
value.redX,
|
|
4749
|
+
value.redY,
|
|
4750
|
+
value.greenX,
|
|
4751
|
+
value.greenY,
|
|
4752
|
+
value.blueX,
|
|
4753
|
+
value.blueY,
|
|
4754
|
+
value.whiteX,
|
|
4755
|
+
value.whiteY
|
|
4756
|
+
])
|
|
4757
|
+
);
|
|
4758
|
+
}
|
|
4423
4759
|
function writeChannelsPayload(channels) {
|
|
4424
4760
|
const writer = new ByteWriter();
|
|
4425
4761
|
for (const channel of channels) {
|
|
@@ -4467,6 +4803,9 @@ function writePartHeader(writer, part) {
|
|
|
4467
4803
|
if (part.includeTypeAttribute) {
|
|
4468
4804
|
writeAttribute(writer, "type", "string", writeStringPayload(part.type));
|
|
4469
4805
|
}
|
|
4806
|
+
for (const [name, attribute] of part.attributes) {
|
|
4807
|
+
writeCustomAttribute(writer, name, attribute);
|
|
4808
|
+
}
|
|
4470
4809
|
writer.writeUint8(0);
|
|
4471
4810
|
}
|
|
4472
4811
|
function getRawChunkSize(part, chunkY, linesInChunk) {
|
|
@@ -4612,11 +4951,114 @@ function writeExr(input, options = {}) {
|
|
|
4612
4951
|
});
|
|
4613
4952
|
return out;
|
|
4614
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
|
+
}
|
|
4615
5052
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4616
5053
|
0 && (module.exports = {
|
|
4617
5054
|
EXR_EVENT_CODES,
|
|
4618
5055
|
ExrError,
|
|
4619
5056
|
decodeExrPart,
|
|
5057
|
+
decodeExrRgba,
|
|
5058
|
+
encodeExrRgba,
|
|
5059
|
+
expandDecodedPartChannels,
|
|
5060
|
+
expandSampledChannel,
|
|
5061
|
+
inspectExrImage,
|
|
4620
5062
|
isExrError,
|
|
4621
5063
|
parseExr,
|
|
4622
5064
|
parseExrStructure,
|