@bb-studio/exr 0.1.0 → 0.1.2

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
@@ -33,6 +33,16 @@ interface ExrWindow {
33
33
  xMax: number;
34
34
  yMax: number;
35
35
  }
36
+ interface ExrChromaticities {
37
+ redX: number;
38
+ redY: number;
39
+ greenX: number;
40
+ greenY: number;
41
+ blueX: number;
42
+ blueY: number;
43
+ whiteX: number;
44
+ whiteY: number;
45
+ }
36
46
  interface ExrPart {
37
47
  id: number;
38
48
  attributes: Record<string, unknown>;
@@ -79,6 +89,19 @@ interface WriteExrChannelInput {
79
89
  xSampling?: number;
80
90
  ySampling?: number;
81
91
  }
92
+ type WriteExrAttribute = {
93
+ type: 'string';
94
+ value: string;
95
+ } | {
96
+ type: 'int';
97
+ value: number;
98
+ } | {
99
+ type: 'float';
100
+ value: number;
101
+ } | {
102
+ type: 'chromaticities';
103
+ value: ExrChromaticities;
104
+ };
82
105
  interface WriteExrPartInput {
83
106
  channels: WriteExrChannelInput[];
84
107
  compression: ExrCompression | number;
@@ -86,6 +109,7 @@ interface WriteExrPartInput {
86
109
  displayWindow?: ExrWindow;
87
110
  name?: string;
88
111
  type?: string;
112
+ attributes?: Readonly<Record<string, WriteExrAttribute>>;
89
113
  }
90
114
  interface WriteExrInput {
91
115
  parts: WriteExrPartInput[];
@@ -118,4 +142,4 @@ declare function readExr(buffer: ExrBinaryInput, options?: ReadExrOptions): Read
118
142
 
119
143
  declare function writeExr(input: WriteExrInput, options?: WriteExrOptions): Uint8Array;
120
144
 
121
- export { type DecodeExrPartOptions, type DecodedChannel, type DecodedPart, EXR_EVENT_CODES, type EventCapableOptions, type ExrBinaryInput, type ExrChannel, 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 WriteExrChannelInput, type WriteExrInput, type WriteExrOptions, type WriteExrPartInput, decodeExrPart, isExrError, parseExr, parseExrStructure, readExr, toArrayBuffer, writeExr };
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 };
package/dist/index.d.ts CHANGED
@@ -33,6 +33,16 @@ interface ExrWindow {
33
33
  xMax: number;
34
34
  yMax: number;
35
35
  }
36
+ interface ExrChromaticities {
37
+ redX: number;
38
+ redY: number;
39
+ greenX: number;
40
+ greenY: number;
41
+ blueX: number;
42
+ blueY: number;
43
+ whiteX: number;
44
+ whiteY: number;
45
+ }
36
46
  interface ExrPart {
37
47
  id: number;
38
48
  attributes: Record<string, unknown>;
@@ -79,6 +89,19 @@ interface WriteExrChannelInput {
79
89
  xSampling?: number;
80
90
  ySampling?: number;
81
91
  }
92
+ type WriteExrAttribute = {
93
+ type: 'string';
94
+ value: string;
95
+ } | {
96
+ type: 'int';
97
+ value: number;
98
+ } | {
99
+ type: 'float';
100
+ value: number;
101
+ } | {
102
+ type: 'chromaticities';
103
+ value: ExrChromaticities;
104
+ };
82
105
  interface WriteExrPartInput {
83
106
  channels: WriteExrChannelInput[];
84
107
  compression: ExrCompression | number;
@@ -86,6 +109,7 @@ interface WriteExrPartInput {
86
109
  displayWindow?: ExrWindow;
87
110
  name?: string;
88
111
  type?: string;
112
+ attributes?: Readonly<Record<string, WriteExrAttribute>>;
89
113
  }
90
114
  interface WriteExrInput {
91
115
  parts: WriteExrPartInput[];
@@ -118,4 +142,4 @@ declare function readExr(buffer: ExrBinaryInput, options?: ReadExrOptions): Read
118
142
 
119
143
  declare function writeExr(input: WriteExrInput, options?: WriteExrOptions): Uint8Array;
120
144
 
121
- export { type DecodeExrPartOptions, type DecodedChannel, type DecodedPart, EXR_EVENT_CODES, type EventCapableOptions, type ExrBinaryInput, type ExrChannel, 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 WriteExrChannelInput, type WriteExrInput, type WriteExrOptions, type WriteExrPartInput, decodeExrPart, isExrError, parseExr, parseExrStructure, readExr, toArrayBuffer, writeExr };
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 };
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
- const asInt = reader.readUint32();
241
- const scratch = new DataView(new ArrayBuffer(4));
242
- scratch.setUint32(0, asInt, true);
243
- value = scratch.getFloat32(0, true);
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>`;
@@ -4186,6 +4201,25 @@ var EXR_VERSION = 2;
4186
4201
  var EXR_MULTIPART_FLAG = 16;
4187
4202
  var UINT32_MAX2 = 4294967295;
4188
4203
  var UTF8_ENCODER = new TextEncoder();
4204
+ var RESERVED_ATTRIBUTE_NAMES = /* @__PURE__ */ new Set([
4205
+ "channels",
4206
+ "compression",
4207
+ "dataWindow",
4208
+ "displayWindow",
4209
+ "lineOrder",
4210
+ "name",
4211
+ "type"
4212
+ ]);
4213
+ var CHROMATICITY_FIELDS = [
4214
+ "redX",
4215
+ "redY",
4216
+ "greenX",
4217
+ "greenY",
4218
+ "blueX",
4219
+ "blueY",
4220
+ "whiteX",
4221
+ "whiteY"
4222
+ ];
4189
4223
  var ByteWriter = class {
4190
4224
  bytes = [];
4191
4225
  get length() {
@@ -4280,9 +4314,13 @@ function normalizeChannel(channel, partId, dataWindow) {
4280
4314
  }
4281
4315
  const pixelType = channel.pixelType;
4282
4316
  if (pixelType !== 0 && pixelType !== 1 && pixelType !== 2) {
4283
- throw new ExrError("INVALID_WRITE_INPUT", `Unsupported channel pixel type for ${channel.name}.`, {
4284
- partId
4285
- });
4317
+ throw new ExrError(
4318
+ "INVALID_WRITE_INPUT",
4319
+ `Unsupported channel pixel type for ${channel.name}.`,
4320
+ {
4321
+ partId
4322
+ }
4323
+ );
4286
4324
  }
4287
4325
  const xSampling = channel.xSampling ?? 1;
4288
4326
  const ySampling = channel.ySampling ?? 1;
@@ -4301,9 +4339,13 @@ function normalizeChannel(channel, partId, dataWindow) {
4301
4339
  const sampledHeight = countSamplesInRange5(dataWindow.yMin, dataWindow.yMax, ySampling);
4302
4340
  const expectedLength = sampledWidth * sampledHeight;
4303
4341
  if (!(channel.data instanceof Float32Array)) {
4304
- throw new ExrError("INVALID_WRITE_INPUT", `Channel ${channel.name} data must be Float32Array.`, {
4305
- partId
4306
- });
4342
+ throw new ExrError(
4343
+ "INVALID_WRITE_INPUT",
4344
+ `Channel ${channel.name} data must be Float32Array.`,
4345
+ {
4346
+ partId
4347
+ }
4348
+ );
4307
4349
  }
4308
4350
  if (channel.data.length !== expectedLength) {
4309
4351
  throw new ExrError("INVALID_WRITE_INPUT", `Channel ${channel.name} has invalid data length.`, {
@@ -4351,6 +4393,7 @@ function normalizeParts(input) {
4351
4393
  const chunkCount = Math.ceil(height / linesPerBlock);
4352
4394
  const includeNameAttribute = isMultipart || typeof part.name === "string";
4353
4395
  const includeTypeAttribute = isMultipart || typeof part.type === "string";
4396
+ const attributes = normalizeAttributes(part.attributes, index);
4354
4397
  return {
4355
4398
  id: index,
4356
4399
  compression,
@@ -4362,10 +4405,90 @@ function normalizeParts(input) {
4362
4405
  linesPerBlock,
4363
4406
  chunkCount,
4364
4407
  includeNameAttribute,
4365
- includeTypeAttribute
4408
+ includeTypeAttribute,
4409
+ attributes
4366
4410
  };
4367
4411
  });
4368
4412
  }
4413
+ function normalizeAttributes(attributes, partId) {
4414
+ if (attributes === void 0) return [];
4415
+ if (!attributes || typeof attributes !== "object" || Array.isArray(attributes)) {
4416
+ throw new ExrError("INVALID_WRITE_INPUT", "Part attributes must be an object.", { partId });
4417
+ }
4418
+ return Object.entries(attributes).map(([name, attribute]) => {
4419
+ if (!name || name.includes("\0")) {
4420
+ throw new ExrError("INVALID_WRITE_INPUT", "Attribute names must be non-empty C strings.", {
4421
+ partId,
4422
+ attribute: name
4423
+ });
4424
+ }
4425
+ if (RESERVED_ATTRIBUTE_NAMES.has(name)) {
4426
+ throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name} is managed by the EXR writer.`, {
4427
+ partId,
4428
+ attribute: name
4429
+ });
4430
+ }
4431
+ validateAttribute(name, attribute, partId);
4432
+ return [name, attribute];
4433
+ });
4434
+ }
4435
+ function validateFiniteAttributeNumber(name, value, partId, field = "value") {
4436
+ if (!Number.isFinite(value)) {
4437
+ throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name}.${field} must be finite.`, {
4438
+ partId,
4439
+ attribute: name,
4440
+ field
4441
+ });
4442
+ }
4443
+ }
4444
+ function validateAttribute(name, attribute, partId) {
4445
+ if (!attribute || typeof attribute !== "object") {
4446
+ throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name} is invalid.`, {
4447
+ partId,
4448
+ attribute: name
4449
+ });
4450
+ }
4451
+ if (attribute.type === "string") {
4452
+ if (typeof attribute.value !== "string") {
4453
+ throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name}.value must be a string.`, {
4454
+ partId,
4455
+ attribute: name
4456
+ });
4457
+ }
4458
+ return;
4459
+ }
4460
+ if (attribute.type === "int") {
4461
+ validateFiniteAttributeNumber(name, attribute.value, partId);
4462
+ if (!Number.isInteger(attribute.value) || attribute.value < -2147483648 || attribute.value > 2147483647) {
4463
+ throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name}.value must be an int32.`, {
4464
+ partId,
4465
+ attribute: name
4466
+ });
4467
+ }
4468
+ return;
4469
+ }
4470
+ if (attribute.type === "float") {
4471
+ validateFiniteAttributeNumber(name, attribute.value, partId);
4472
+ return;
4473
+ }
4474
+ if (attribute.type === "chromaticities") {
4475
+ if (!attribute.value || typeof attribute.value !== "object") {
4476
+ throw new ExrError(
4477
+ "INVALID_WRITE_INPUT",
4478
+ `Attribute ${name}.value must contain chromaticities.`,
4479
+ { partId, attribute: name }
4480
+ );
4481
+ }
4482
+ for (const field of CHROMATICITY_FIELDS) {
4483
+ validateFiniteAttributeNumber(name, attribute.value[field], partId, field);
4484
+ }
4485
+ return;
4486
+ }
4487
+ throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name} has an unsupported type.`, {
4488
+ partId,
4489
+ attribute: name
4490
+ });
4491
+ }
4369
4492
  function writeAttribute(writer, name, type, payload) {
4370
4493
  writer.writeCString(name);
4371
4494
  writer.writeCString(type);
@@ -4380,12 +4503,50 @@ function writeInt32Payload(values) {
4380
4503
  }
4381
4504
  return out;
4382
4505
  }
4506
+ function writeFloat32Payload(values) {
4507
+ const out = new Uint8Array(values.length * 4);
4508
+ const view = new DataView(out.buffer);
4509
+ for (let i = 0; i < values.length; i++) {
4510
+ view.setFloat32(i * 4, values[i], true);
4511
+ }
4512
+ return out;
4513
+ }
4383
4514
  function writeCompressionPayload(compression) {
4384
4515
  return Uint8Array.of(compression & 255);
4385
4516
  }
4386
4517
  function writeStringPayload(value) {
4387
4518
  return UTF8_ENCODER.encode(value);
4388
4519
  }
4520
+ function writeCustomAttribute(writer, name, attribute) {
4521
+ if (attribute.type === "string") {
4522
+ writeAttribute(writer, name, attribute.type, writeStringPayload(attribute.value));
4523
+ return;
4524
+ }
4525
+ if (attribute.type === "int") {
4526
+ writeAttribute(writer, name, attribute.type, writeInt32Payload([attribute.value]));
4527
+ return;
4528
+ }
4529
+ if (attribute.type === "float") {
4530
+ writeAttribute(writer, name, attribute.type, writeFloat32Payload([attribute.value]));
4531
+ return;
4532
+ }
4533
+ const value = attribute.value;
4534
+ writeAttribute(
4535
+ writer,
4536
+ name,
4537
+ attribute.type,
4538
+ writeFloat32Payload([
4539
+ value.redX,
4540
+ value.redY,
4541
+ value.greenX,
4542
+ value.greenY,
4543
+ value.blueX,
4544
+ value.blueY,
4545
+ value.whiteX,
4546
+ value.whiteY
4547
+ ])
4548
+ );
4549
+ }
4389
4550
  function writeChannelsPayload(channels) {
4390
4551
  const writer = new ByteWriter();
4391
4552
  for (const channel of channels) {
@@ -4433,6 +4594,9 @@ function writePartHeader(writer, part) {
4433
4594
  if (part.includeTypeAttribute) {
4434
4595
  writeAttribute(writer, "type", "string", writeStringPayload(part.type));
4435
4596
  }
4597
+ for (const [name, attribute] of part.attributes) {
4598
+ writeCustomAttribute(writer, name, attribute);
4599
+ }
4436
4600
  writer.writeUint8(0);
4437
4601
  }
4438
4602
  function getRawChunkSize(part, chunkY, linesInChunk) {