@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.js
CHANGED
|
@@ -101,6 +101,12 @@ var BinaryReader = class {
|
|
|
101
101
|
this.offset += 4;
|
|
102
102
|
return value;
|
|
103
103
|
}
|
|
104
|
+
readFloat32() {
|
|
105
|
+
this.ensureAvailable(4, "f32", "float32");
|
|
106
|
+
const value = this.view.getFloat32(this.offset, true);
|
|
107
|
+
this.offset += 4;
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
104
110
|
readCString(context) {
|
|
105
111
|
let value = "";
|
|
106
112
|
while (this.offset < this.view.byteLength) {
|
|
@@ -237,10 +243,19 @@ function parsePartHeader(reader, index) {
|
|
|
237
243
|
value = reader.readInt32();
|
|
238
244
|
} else if (type === "float") {
|
|
239
245
|
ensureExactAttributeSize(name, type, size, 4);
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
value =
|
|
246
|
+
value = reader.readFloat32();
|
|
247
|
+
} else if (type === "chromaticities") {
|
|
248
|
+
ensureExactAttributeSize(name, type, size, 32);
|
|
249
|
+
value = {
|
|
250
|
+
redX: reader.readFloat32(),
|
|
251
|
+
redY: reader.readFloat32(),
|
|
252
|
+
greenX: reader.readFloat32(),
|
|
253
|
+
greenY: reader.readFloat32(),
|
|
254
|
+
blueX: reader.readFloat32(),
|
|
255
|
+
blueY: reader.readFloat32(),
|
|
256
|
+
whiteX: reader.readFloat32(),
|
|
257
|
+
whiteY: reader.readFloat32()
|
|
258
|
+
};
|
|
244
259
|
} else {
|
|
245
260
|
reader.skip(size, `unknown attribute ${name}`);
|
|
246
261
|
value = `<${type} data>`;
|
|
@@ -3102,6 +3117,176 @@ function readExr(buffer, options = {}) {
|
|
|
3102
3117
|
};
|
|
3103
3118
|
}
|
|
3104
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
|
+
|
|
3105
3290
|
// src/writer/sampling.ts
|
|
3106
3291
|
function modulo5(value, base) {
|
|
3107
3292
|
const result = value % base;
|
|
@@ -4186,6 +4371,25 @@ var EXR_VERSION = 2;
|
|
|
4186
4371
|
var EXR_MULTIPART_FLAG = 16;
|
|
4187
4372
|
var UINT32_MAX2 = 4294967295;
|
|
4188
4373
|
var UTF8_ENCODER = new TextEncoder();
|
|
4374
|
+
var RESERVED_ATTRIBUTE_NAMES = /* @__PURE__ */ new Set([
|
|
4375
|
+
"channels",
|
|
4376
|
+
"compression",
|
|
4377
|
+
"dataWindow",
|
|
4378
|
+
"displayWindow",
|
|
4379
|
+
"lineOrder",
|
|
4380
|
+
"name",
|
|
4381
|
+
"type"
|
|
4382
|
+
]);
|
|
4383
|
+
var CHROMATICITY_FIELDS = [
|
|
4384
|
+
"redX",
|
|
4385
|
+
"redY",
|
|
4386
|
+
"greenX",
|
|
4387
|
+
"greenY",
|
|
4388
|
+
"blueX",
|
|
4389
|
+
"blueY",
|
|
4390
|
+
"whiteX",
|
|
4391
|
+
"whiteY"
|
|
4392
|
+
];
|
|
4189
4393
|
var ByteWriter = class {
|
|
4190
4394
|
bytes = [];
|
|
4191
4395
|
get length() {
|
|
@@ -4280,9 +4484,13 @@ function normalizeChannel(channel, partId, dataWindow) {
|
|
|
4280
4484
|
}
|
|
4281
4485
|
const pixelType = channel.pixelType;
|
|
4282
4486
|
if (pixelType !== 0 && pixelType !== 1 && pixelType !== 2) {
|
|
4283
|
-
throw new ExrError(
|
|
4284
|
-
|
|
4285
|
-
|
|
4487
|
+
throw new ExrError(
|
|
4488
|
+
"INVALID_WRITE_INPUT",
|
|
4489
|
+
`Unsupported channel pixel type for ${channel.name}.`,
|
|
4490
|
+
{
|
|
4491
|
+
partId
|
|
4492
|
+
}
|
|
4493
|
+
);
|
|
4286
4494
|
}
|
|
4287
4495
|
const xSampling = channel.xSampling ?? 1;
|
|
4288
4496
|
const ySampling = channel.ySampling ?? 1;
|
|
@@ -4301,9 +4509,13 @@ function normalizeChannel(channel, partId, dataWindow) {
|
|
|
4301
4509
|
const sampledHeight = countSamplesInRange5(dataWindow.yMin, dataWindow.yMax, ySampling);
|
|
4302
4510
|
const expectedLength = sampledWidth * sampledHeight;
|
|
4303
4511
|
if (!(channel.data instanceof Float32Array)) {
|
|
4304
|
-
throw new ExrError(
|
|
4305
|
-
|
|
4306
|
-
|
|
4512
|
+
throw new ExrError(
|
|
4513
|
+
"INVALID_WRITE_INPUT",
|
|
4514
|
+
`Channel ${channel.name} data must be Float32Array.`,
|
|
4515
|
+
{
|
|
4516
|
+
partId
|
|
4517
|
+
}
|
|
4518
|
+
);
|
|
4307
4519
|
}
|
|
4308
4520
|
if (channel.data.length !== expectedLength) {
|
|
4309
4521
|
throw new ExrError("INVALID_WRITE_INPUT", `Channel ${channel.name} has invalid data length.`, {
|
|
@@ -4351,6 +4563,7 @@ function normalizeParts(input) {
|
|
|
4351
4563
|
const chunkCount = Math.ceil(height / linesPerBlock);
|
|
4352
4564
|
const includeNameAttribute = isMultipart || typeof part.name === "string";
|
|
4353
4565
|
const includeTypeAttribute = isMultipart || typeof part.type === "string";
|
|
4566
|
+
const attributes = normalizeAttributes(part.attributes, index);
|
|
4354
4567
|
return {
|
|
4355
4568
|
id: index,
|
|
4356
4569
|
compression,
|
|
@@ -4362,10 +4575,90 @@ function normalizeParts(input) {
|
|
|
4362
4575
|
linesPerBlock,
|
|
4363
4576
|
chunkCount,
|
|
4364
4577
|
includeNameAttribute,
|
|
4365
|
-
includeTypeAttribute
|
|
4578
|
+
includeTypeAttribute,
|
|
4579
|
+
attributes
|
|
4366
4580
|
};
|
|
4367
4581
|
});
|
|
4368
4582
|
}
|
|
4583
|
+
function normalizeAttributes(attributes, partId) {
|
|
4584
|
+
if (attributes === void 0) return [];
|
|
4585
|
+
if (!attributes || typeof attributes !== "object" || Array.isArray(attributes)) {
|
|
4586
|
+
throw new ExrError("INVALID_WRITE_INPUT", "Part attributes must be an object.", { partId });
|
|
4587
|
+
}
|
|
4588
|
+
return Object.entries(attributes).map(([name, attribute]) => {
|
|
4589
|
+
if (!name || name.includes("\0")) {
|
|
4590
|
+
throw new ExrError("INVALID_WRITE_INPUT", "Attribute names must be non-empty C strings.", {
|
|
4591
|
+
partId,
|
|
4592
|
+
attribute: name
|
|
4593
|
+
});
|
|
4594
|
+
}
|
|
4595
|
+
if (RESERVED_ATTRIBUTE_NAMES.has(name)) {
|
|
4596
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name} is managed by the EXR writer.`, {
|
|
4597
|
+
partId,
|
|
4598
|
+
attribute: name
|
|
4599
|
+
});
|
|
4600
|
+
}
|
|
4601
|
+
validateAttribute(name, attribute, partId);
|
|
4602
|
+
return [name, attribute];
|
|
4603
|
+
});
|
|
4604
|
+
}
|
|
4605
|
+
function validateFiniteAttributeNumber(name, value, partId, field = "value") {
|
|
4606
|
+
if (!Number.isFinite(value)) {
|
|
4607
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name}.${field} must be finite.`, {
|
|
4608
|
+
partId,
|
|
4609
|
+
attribute: name,
|
|
4610
|
+
field
|
|
4611
|
+
});
|
|
4612
|
+
}
|
|
4613
|
+
}
|
|
4614
|
+
function validateAttribute(name, attribute, partId) {
|
|
4615
|
+
if (!attribute || typeof attribute !== "object") {
|
|
4616
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name} is invalid.`, {
|
|
4617
|
+
partId,
|
|
4618
|
+
attribute: name
|
|
4619
|
+
});
|
|
4620
|
+
}
|
|
4621
|
+
if (attribute.type === "string") {
|
|
4622
|
+
if (typeof attribute.value !== "string") {
|
|
4623
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name}.value must be a string.`, {
|
|
4624
|
+
partId,
|
|
4625
|
+
attribute: name
|
|
4626
|
+
});
|
|
4627
|
+
}
|
|
4628
|
+
return;
|
|
4629
|
+
}
|
|
4630
|
+
if (attribute.type === "int") {
|
|
4631
|
+
validateFiniteAttributeNumber(name, attribute.value, partId);
|
|
4632
|
+
if (!Number.isInteger(attribute.value) || attribute.value < -2147483648 || attribute.value > 2147483647) {
|
|
4633
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name}.value must be an int32.`, {
|
|
4634
|
+
partId,
|
|
4635
|
+
attribute: name
|
|
4636
|
+
});
|
|
4637
|
+
}
|
|
4638
|
+
return;
|
|
4639
|
+
}
|
|
4640
|
+
if (attribute.type === "float") {
|
|
4641
|
+
validateFiniteAttributeNumber(name, attribute.value, partId);
|
|
4642
|
+
return;
|
|
4643
|
+
}
|
|
4644
|
+
if (attribute.type === "chromaticities") {
|
|
4645
|
+
if (!attribute.value || typeof attribute.value !== "object") {
|
|
4646
|
+
throw new ExrError(
|
|
4647
|
+
"INVALID_WRITE_INPUT",
|
|
4648
|
+
`Attribute ${name}.value must contain chromaticities.`,
|
|
4649
|
+
{ partId, attribute: name }
|
|
4650
|
+
);
|
|
4651
|
+
}
|
|
4652
|
+
for (const field of CHROMATICITY_FIELDS) {
|
|
4653
|
+
validateFiniteAttributeNumber(name, attribute.value[field], partId, field);
|
|
4654
|
+
}
|
|
4655
|
+
return;
|
|
4656
|
+
}
|
|
4657
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name} has an unsupported type.`, {
|
|
4658
|
+
partId,
|
|
4659
|
+
attribute: name
|
|
4660
|
+
});
|
|
4661
|
+
}
|
|
4369
4662
|
function writeAttribute(writer, name, type, payload) {
|
|
4370
4663
|
writer.writeCString(name);
|
|
4371
4664
|
writer.writeCString(type);
|
|
@@ -4380,12 +4673,50 @@ function writeInt32Payload(values) {
|
|
|
4380
4673
|
}
|
|
4381
4674
|
return out;
|
|
4382
4675
|
}
|
|
4676
|
+
function writeFloat32Payload(values) {
|
|
4677
|
+
const out = new Uint8Array(values.length * 4);
|
|
4678
|
+
const view = new DataView(out.buffer);
|
|
4679
|
+
for (let i = 0; i < values.length; i++) {
|
|
4680
|
+
view.setFloat32(i * 4, values[i], true);
|
|
4681
|
+
}
|
|
4682
|
+
return out;
|
|
4683
|
+
}
|
|
4383
4684
|
function writeCompressionPayload(compression) {
|
|
4384
4685
|
return Uint8Array.of(compression & 255);
|
|
4385
4686
|
}
|
|
4386
4687
|
function writeStringPayload(value) {
|
|
4387
4688
|
return UTF8_ENCODER.encode(value);
|
|
4388
4689
|
}
|
|
4690
|
+
function writeCustomAttribute(writer, name, attribute) {
|
|
4691
|
+
if (attribute.type === "string") {
|
|
4692
|
+
writeAttribute(writer, name, attribute.type, writeStringPayload(attribute.value));
|
|
4693
|
+
return;
|
|
4694
|
+
}
|
|
4695
|
+
if (attribute.type === "int") {
|
|
4696
|
+
writeAttribute(writer, name, attribute.type, writeInt32Payload([attribute.value]));
|
|
4697
|
+
return;
|
|
4698
|
+
}
|
|
4699
|
+
if (attribute.type === "float") {
|
|
4700
|
+
writeAttribute(writer, name, attribute.type, writeFloat32Payload([attribute.value]));
|
|
4701
|
+
return;
|
|
4702
|
+
}
|
|
4703
|
+
const value = attribute.value;
|
|
4704
|
+
writeAttribute(
|
|
4705
|
+
writer,
|
|
4706
|
+
name,
|
|
4707
|
+
attribute.type,
|
|
4708
|
+
writeFloat32Payload([
|
|
4709
|
+
value.redX,
|
|
4710
|
+
value.redY,
|
|
4711
|
+
value.greenX,
|
|
4712
|
+
value.greenY,
|
|
4713
|
+
value.blueX,
|
|
4714
|
+
value.blueY,
|
|
4715
|
+
value.whiteX,
|
|
4716
|
+
value.whiteY
|
|
4717
|
+
])
|
|
4718
|
+
);
|
|
4719
|
+
}
|
|
4389
4720
|
function writeChannelsPayload(channels) {
|
|
4390
4721
|
const writer = new ByteWriter();
|
|
4391
4722
|
for (const channel of channels) {
|
|
@@ -4433,6 +4764,9 @@ function writePartHeader(writer, part) {
|
|
|
4433
4764
|
if (part.includeTypeAttribute) {
|
|
4434
4765
|
writeAttribute(writer, "type", "string", writeStringPayload(part.type));
|
|
4435
4766
|
}
|
|
4767
|
+
for (const [name, attribute] of part.attributes) {
|
|
4768
|
+
writeCustomAttribute(writer, name, attribute);
|
|
4769
|
+
}
|
|
4436
4770
|
writer.writeUint8(0);
|
|
4437
4771
|
}
|
|
4438
4772
|
function getRawChunkSize(part, chunkY, linesInChunk) {
|
|
@@ -4578,10 +4912,113 @@ function writeExr(input, options = {}) {
|
|
|
4578
4912
|
});
|
|
4579
4913
|
return out;
|
|
4580
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
|
+
}
|
|
4581
5013
|
export {
|
|
4582
5014
|
EXR_EVENT_CODES,
|
|
4583
5015
|
ExrError,
|
|
4584
5016
|
decodeExrPart,
|
|
5017
|
+
decodeExrRgba,
|
|
5018
|
+
encodeExrRgba,
|
|
5019
|
+
expandDecodedPartChannels,
|
|
5020
|
+
expandSampledChannel,
|
|
5021
|
+
inspectExrImage,
|
|
4585
5022
|
isExrError,
|
|
4586
5023
|
parseExr,
|
|
4587
5024
|
parseExrStructure,
|