@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.cjs CHANGED
@@ -135,6 +135,12 @@ var BinaryReader = class {
135
135
  this.offset += 4;
136
136
  return value;
137
137
  }
138
+ readFloat32() {
139
+ this.ensureAvailable(4, "f32", "float32");
140
+ const value = this.view.getFloat32(this.offset, true);
141
+ this.offset += 4;
142
+ return value;
143
+ }
138
144
  readCString(context) {
139
145
  let value = "";
140
146
  while (this.offset < this.view.byteLength) {
@@ -271,10 +277,19 @@ function parsePartHeader(reader, index) {
271
277
  value = reader.readInt32();
272
278
  } else if (type === "float") {
273
279
  ensureExactAttributeSize(name, type, size, 4);
274
- const asInt = reader.readUint32();
275
- const scratch = new DataView(new ArrayBuffer(4));
276
- scratch.setUint32(0, asInt, true);
277
- value = scratch.getFloat32(0, true);
280
+ value = reader.readFloat32();
281
+ } else if (type === "chromaticities") {
282
+ ensureExactAttributeSize(name, type, size, 32);
283
+ value = {
284
+ redX: reader.readFloat32(),
285
+ redY: reader.readFloat32(),
286
+ greenX: reader.readFloat32(),
287
+ greenY: reader.readFloat32(),
288
+ blueX: reader.readFloat32(),
289
+ blueY: reader.readFloat32(),
290
+ whiteX: reader.readFloat32(),
291
+ whiteY: reader.readFloat32()
292
+ };
278
293
  } else {
279
294
  reader.skip(size, `unknown attribute ${name}`);
280
295
  value = `<${type} data>`;
@@ -4220,6 +4235,25 @@ var EXR_VERSION = 2;
4220
4235
  var EXR_MULTIPART_FLAG = 16;
4221
4236
  var UINT32_MAX2 = 4294967295;
4222
4237
  var UTF8_ENCODER = new TextEncoder();
4238
+ var RESERVED_ATTRIBUTE_NAMES = /* @__PURE__ */ new Set([
4239
+ "channels",
4240
+ "compression",
4241
+ "dataWindow",
4242
+ "displayWindow",
4243
+ "lineOrder",
4244
+ "name",
4245
+ "type"
4246
+ ]);
4247
+ var CHROMATICITY_FIELDS = [
4248
+ "redX",
4249
+ "redY",
4250
+ "greenX",
4251
+ "greenY",
4252
+ "blueX",
4253
+ "blueY",
4254
+ "whiteX",
4255
+ "whiteY"
4256
+ ];
4223
4257
  var ByteWriter = class {
4224
4258
  bytes = [];
4225
4259
  get length() {
@@ -4314,9 +4348,13 @@ function normalizeChannel(channel, partId, dataWindow) {
4314
4348
  }
4315
4349
  const pixelType = channel.pixelType;
4316
4350
  if (pixelType !== 0 && pixelType !== 1 && pixelType !== 2) {
4317
- throw new ExrError("INVALID_WRITE_INPUT", `Unsupported channel pixel type for ${channel.name}.`, {
4318
- partId
4319
- });
4351
+ throw new ExrError(
4352
+ "INVALID_WRITE_INPUT",
4353
+ `Unsupported channel pixel type for ${channel.name}.`,
4354
+ {
4355
+ partId
4356
+ }
4357
+ );
4320
4358
  }
4321
4359
  const xSampling = channel.xSampling ?? 1;
4322
4360
  const ySampling = channel.ySampling ?? 1;
@@ -4335,9 +4373,13 @@ function normalizeChannel(channel, partId, dataWindow) {
4335
4373
  const sampledHeight = countSamplesInRange5(dataWindow.yMin, dataWindow.yMax, ySampling);
4336
4374
  const expectedLength = sampledWidth * sampledHeight;
4337
4375
  if (!(channel.data instanceof Float32Array)) {
4338
- throw new ExrError("INVALID_WRITE_INPUT", `Channel ${channel.name} data must be Float32Array.`, {
4339
- partId
4340
- });
4376
+ throw new ExrError(
4377
+ "INVALID_WRITE_INPUT",
4378
+ `Channel ${channel.name} data must be Float32Array.`,
4379
+ {
4380
+ partId
4381
+ }
4382
+ );
4341
4383
  }
4342
4384
  if (channel.data.length !== expectedLength) {
4343
4385
  throw new ExrError("INVALID_WRITE_INPUT", `Channel ${channel.name} has invalid data length.`, {
@@ -4385,6 +4427,7 @@ function normalizeParts(input) {
4385
4427
  const chunkCount = Math.ceil(height / linesPerBlock);
4386
4428
  const includeNameAttribute = isMultipart || typeof part.name === "string";
4387
4429
  const includeTypeAttribute = isMultipart || typeof part.type === "string";
4430
+ const attributes = normalizeAttributes(part.attributes, index);
4388
4431
  return {
4389
4432
  id: index,
4390
4433
  compression,
@@ -4396,10 +4439,90 @@ function normalizeParts(input) {
4396
4439
  linesPerBlock,
4397
4440
  chunkCount,
4398
4441
  includeNameAttribute,
4399
- includeTypeAttribute
4442
+ includeTypeAttribute,
4443
+ attributes
4400
4444
  };
4401
4445
  });
4402
4446
  }
4447
+ function normalizeAttributes(attributes, partId) {
4448
+ if (attributes === void 0) return [];
4449
+ if (!attributes || typeof attributes !== "object" || Array.isArray(attributes)) {
4450
+ throw new ExrError("INVALID_WRITE_INPUT", "Part attributes must be an object.", { partId });
4451
+ }
4452
+ return Object.entries(attributes).map(([name, attribute]) => {
4453
+ if (!name || name.includes("\0")) {
4454
+ throw new ExrError("INVALID_WRITE_INPUT", "Attribute names must be non-empty C strings.", {
4455
+ partId,
4456
+ attribute: name
4457
+ });
4458
+ }
4459
+ if (RESERVED_ATTRIBUTE_NAMES.has(name)) {
4460
+ throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name} is managed by the EXR writer.`, {
4461
+ partId,
4462
+ attribute: name
4463
+ });
4464
+ }
4465
+ validateAttribute(name, attribute, partId);
4466
+ return [name, attribute];
4467
+ });
4468
+ }
4469
+ function validateFiniteAttributeNumber(name, value, partId, field = "value") {
4470
+ if (!Number.isFinite(value)) {
4471
+ throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name}.${field} must be finite.`, {
4472
+ partId,
4473
+ attribute: name,
4474
+ field
4475
+ });
4476
+ }
4477
+ }
4478
+ function validateAttribute(name, attribute, partId) {
4479
+ if (!attribute || typeof attribute !== "object") {
4480
+ throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name} is invalid.`, {
4481
+ partId,
4482
+ attribute: name
4483
+ });
4484
+ }
4485
+ if (attribute.type === "string") {
4486
+ if (typeof attribute.value !== "string") {
4487
+ throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name}.value must be a string.`, {
4488
+ partId,
4489
+ attribute: name
4490
+ });
4491
+ }
4492
+ return;
4493
+ }
4494
+ if (attribute.type === "int") {
4495
+ validateFiniteAttributeNumber(name, attribute.value, partId);
4496
+ if (!Number.isInteger(attribute.value) || attribute.value < -2147483648 || attribute.value > 2147483647) {
4497
+ throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name}.value must be an int32.`, {
4498
+ partId,
4499
+ attribute: name
4500
+ });
4501
+ }
4502
+ return;
4503
+ }
4504
+ if (attribute.type === "float") {
4505
+ validateFiniteAttributeNumber(name, attribute.value, partId);
4506
+ return;
4507
+ }
4508
+ if (attribute.type === "chromaticities") {
4509
+ if (!attribute.value || typeof attribute.value !== "object") {
4510
+ throw new ExrError(
4511
+ "INVALID_WRITE_INPUT",
4512
+ `Attribute ${name}.value must contain chromaticities.`,
4513
+ { partId, attribute: name }
4514
+ );
4515
+ }
4516
+ for (const field of CHROMATICITY_FIELDS) {
4517
+ validateFiniteAttributeNumber(name, attribute.value[field], partId, field);
4518
+ }
4519
+ return;
4520
+ }
4521
+ throw new ExrError("INVALID_WRITE_INPUT", `Attribute ${name} has an unsupported type.`, {
4522
+ partId,
4523
+ attribute: name
4524
+ });
4525
+ }
4403
4526
  function writeAttribute(writer, name, type, payload) {
4404
4527
  writer.writeCString(name);
4405
4528
  writer.writeCString(type);
@@ -4414,12 +4537,50 @@ function writeInt32Payload(values) {
4414
4537
  }
4415
4538
  return out;
4416
4539
  }
4540
+ function writeFloat32Payload(values) {
4541
+ const out = new Uint8Array(values.length * 4);
4542
+ const view = new DataView(out.buffer);
4543
+ for (let i = 0; i < values.length; i++) {
4544
+ view.setFloat32(i * 4, values[i], true);
4545
+ }
4546
+ return out;
4547
+ }
4417
4548
  function writeCompressionPayload(compression) {
4418
4549
  return Uint8Array.of(compression & 255);
4419
4550
  }
4420
4551
  function writeStringPayload(value) {
4421
4552
  return UTF8_ENCODER.encode(value);
4422
4553
  }
4554
+ function writeCustomAttribute(writer, name, attribute) {
4555
+ if (attribute.type === "string") {
4556
+ writeAttribute(writer, name, attribute.type, writeStringPayload(attribute.value));
4557
+ return;
4558
+ }
4559
+ if (attribute.type === "int") {
4560
+ writeAttribute(writer, name, attribute.type, writeInt32Payload([attribute.value]));
4561
+ return;
4562
+ }
4563
+ if (attribute.type === "float") {
4564
+ writeAttribute(writer, name, attribute.type, writeFloat32Payload([attribute.value]));
4565
+ return;
4566
+ }
4567
+ const value = attribute.value;
4568
+ writeAttribute(
4569
+ writer,
4570
+ name,
4571
+ attribute.type,
4572
+ writeFloat32Payload([
4573
+ value.redX,
4574
+ value.redY,
4575
+ value.greenX,
4576
+ value.greenY,
4577
+ value.blueX,
4578
+ value.blueY,
4579
+ value.whiteX,
4580
+ value.whiteY
4581
+ ])
4582
+ );
4583
+ }
4423
4584
  function writeChannelsPayload(channels) {
4424
4585
  const writer = new ByteWriter();
4425
4586
  for (const channel of channels) {
@@ -4467,6 +4628,9 @@ function writePartHeader(writer, part) {
4467
4628
  if (part.includeTypeAttribute) {
4468
4629
  writeAttribute(writer, "type", "string", writeStringPayload(part.type));
4469
4630
  }
4631
+ for (const [name, attribute] of part.attributes) {
4632
+ writeCustomAttribute(writer, name, attribute);
4633
+ }
4470
4634
  writer.writeUint8(0);
4471
4635
  }
4472
4636
  function getRawChunkSize(part, chunkY, linesInChunk) {