@joycostudio/chopper 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,6 @@
1
1
  # @joycostudio/chopper
2
2
 
3
- Isomorphic image and GLB optimization for browser workers, Node, the CLI, and
4
- GitHub Actions.
3
+ Isomorphic image and GLB optimization for browser workers, Node, and the CLI.
5
4
 
6
5
  ```ts
7
6
  import { createNodeChopper } from '@joycostudio/chopper/node'
@@ -10,7 +9,6 @@ const chopper = createNodeChopper()
10
9
  const result = await chopper.optimize(
11
10
  { bytes, fileName: 'hero.png' },
12
11
  {
13
- schemaVersion: 1,
14
12
  kind: 'image',
15
13
  encode: { codec: 'webp', quality: 82 },
16
14
  }
@@ -33,5 +31,5 @@ chopper codecs sync public/chopper-codecs
33
31
  The browser defaults to a module worker and verifies the codec manifest and
34
32
  each downloaded artifact. Node resolves the same artifacts from this package.
35
33
 
36
- See the repository README for recipes, CLI commands, config, Action usage, and
37
- the implementation plan.
34
+ See the repository README for recipes, CLI commands, config, and the
35
+ implementation plan.
@@ -1,6 +1,5 @@
1
1
  {
2
- "schemaVersion": 1,
3
- "chopperVersion": "0.4.0",
2
+ "chopperVersion": "0.5.0",
4
3
  "manifestHash": "2046d60deb0f9f224743f4404f3c6e8ba5399f81b2811fa50466e93fce3bd322",
5
4
  "gltfTransformVersion": "4.4.2",
6
5
  "meshoptimizerVersion": "1.0.1",
@@ -7,7 +7,7 @@ var failure = (code, message, cause) => ({
7
7
 
8
8
  // package.json
9
9
  var package_default = {
10
- version: "0.4.0"};
10
+ version: "0.5.0"};
11
11
 
12
12
  // src/codecs/manifest.ts
13
13
  var CODEC_MANIFEST_HASH = "2046d60deb0f9f224743f4404f3c6e8ba5399f81b2811fa50466e93fce3bd322";
@@ -91,7 +91,6 @@ var CODEC_ARTIFACTS = {
91
91
  }
92
92
  };
93
93
  var CODEC_MANIFEST = {
94
- schemaVersion: 1,
95
94
  chopperVersion: package_default.version,
96
95
  manifestHash: CODEC_MANIFEST_HASH,
97
96
  gltfTransformVersion: "4.4.2",
@@ -101,5 +100,5 @@ var CODEC_MANIFEST = {
101
100
  var ENGINE_ID = `chopper@${package_default.version}:${CODEC_MANIFEST_HASH}`;
102
101
 
103
102
  export { CODEC_ARTIFACTS, CODEC_MANIFEST, CODEC_MANIFEST_HASH, ENGINE_ID, failure, success };
104
- //# sourceMappingURL=chunk-M7J64NCO.js.map
105
- //# sourceMappingURL=chunk-M7J64NCO.js.map
103
+ //# sourceMappingURL=chunk-6WQPPVM2.js.map
104
+ //# sourceMappingURL=chunk-6WQPPVM2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/core/result.ts","../../package.json","../../src/codecs/manifest.ts"],"names":[],"mappings":"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","file":"chunk-6WQPPVM2.js","sourcesContent":["export type ChopperErrorCode =\n | 'unsupported_asset'\n | 'invalid_recipe'\n | 'codec_load_failed'\n | 'decode_failed'\n | 'encode_failed'\n | 'validation_failed'\n | 'aborted'\n | 'io_failed'\n\nexport interface ChopperFailure {\n ok: false\n error: {\n code: ChopperErrorCode\n message: string\n cause?: unknown\n }\n}\n\nexport interface ChopperSuccess<T> {\n ok: true\n data: T\n}\n\nexport type ChopperResult<T> = ChopperSuccess<T> | ChopperFailure\n\nexport const success = <T>(data: T): ChopperSuccess<T> => ({ ok: true, data })\n\nexport const failure = (code: ChopperErrorCode, message: string, cause?: unknown): ChopperFailure => ({\n ok: false,\n error: cause === undefined ? { code, message } : { code, message, cause },\n})\n","{\n \"name\": \"@joycostudio/chopper\",\n \"version\": \"0.5.0\",\n \"description\": \"Isomorphic asset optimization pipeline\",\n \"type\": \"module\",\n \"license\": \"ISC\",\n \"author\": \"joyco.studio\",\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/joyco-studio/chopper.git\",\n \"directory\": \"packages/chopper\"\n },\n \"homepage\": \"https://github.com/joyco-studio/chopper#readme\",\n \"bugs\": \"https://github.com/joyco-studio/chopper/issues\",\n \"publishConfig\": {\n \"registry\": \"https://registry.npmjs.org\",\n \"access\": \"public\"\n },\n \"files\": [\n \"dist\",\n \"codecs\",\n \"README.md\",\n \"THIRD_PARTY_NOTICES.md\"\n ],\n \"bin\": {\n \"chopper\": \"dist/cli/index.js\"\n },\n \"exports\": {\n \".\": {\n \"types\": \"./dist/index.d.ts\",\n \"import\": \"./dist/index.js\"\n },\n \"./browser\": {\n \"types\": \"./dist/browser/index.d.ts\",\n \"import\": \"./dist/browser/index.js\"\n },\n \"./node\": {\n \"types\": \"./dist/node/index.d.ts\",\n \"import\": \"./dist/node/index.js\",\n \"default\": \"./dist/node/index.js\"\n },\n \"./config\": {\n \"types\": \"./dist/config/index.d.ts\",\n \"import\": \"./dist/config/index.js\"\n },\n \"./package.json\": \"./package.json\"\n },\n \"scripts\": {\n \"build\": \"pnpm codecs:sync && tsup\",\n \"codecs:sync\": \"tsx ../../scripts/sync-codecs.ts\",\n \"dev\": \"tsup --watch\",\n \"typecheck\": \"tsc --noEmit\"\n },\n \"dependencies\": {\n \"@gltf-transform/core\": \"4.4.2\",\n \"@gltf-transform/extensions\": \"4.4.2\",\n \"@gltf-transform/functions\": \"4.4.2\",\n \"@jsquash/avif\": \"2.1.1\",\n \"@jsquash/jpeg\": \"1.6.0\",\n \"@jsquash/png\": \"3.1.1\",\n \"@jsquash/resize\": \"2.1.1\",\n \"@jsquash/webp\": \"1.5.0\",\n \"draco3dgltf\": \"1.5.7\",\n \"jiti\": \"2.7.0\",\n \"ktx2-encoder\": \"0.6.0\",\n \"meshoptimizer\": \"1.0.1\",\n \"tinyglobby\": \"0.2.17\"\n },\n \"devDependencies\": {\n \"@types/draco3dgltf\": \"1.4.3\"\n }\n}\n","import packageMetadata from '../../package.json'\n\nexport const CODEC_MANIFEST_HASH = '2046d60deb0f9f224743f4404f3c6e8ba5399f81b2811fa50466e93fce3bd322'\n\nexport const CODEC_ARTIFACTS = {\n avifEncoder: {\n packageName: '@jsquash/avif',\n packageVersion: '2.1.1',\n packagePath: 'codec/enc/avif_enc.wasm',\n publicFile: 'avif_enc.wasm',\n sha256: 'd9f2a95164362af48558d176e619becfd49dd97b50b86c679b47100860522b3d',\n },\n jpegDecoder: {\n packageName: '@jsquash/jpeg',\n packageVersion: '1.6.0',\n packagePath: 'codec/dec/mozjpeg_dec.wasm',\n publicFile: 'mozjpeg_dec.wasm',\n sha256: 'a7c4b12169817e779ff4af137981393ae924944e167ad1bd95747c9199162d3e',\n },\n jpegEncoder: {\n packageName: '@jsquash/jpeg',\n packageVersion: '1.6.0',\n packagePath: 'codec/enc/mozjpeg_enc.wasm',\n publicFile: 'mozjpeg_enc.wasm',\n sha256: '24d4177f1c4963e2058b107189249651c61fdef125570e79b1dfb63c8bb49326',\n },\n ktx2Encoder: {\n packageName: 'ktx2-encoder',\n packageVersion: '0.6.0',\n packagePath: 'dist/basis/basis_encoder.wasm',\n publicFile: 'basis_encoder.wasm',\n sha256: '9807719e87cf3d979b0d69ae7112eb88aec6a0e0206c0b2d00dc0bed0d581b80',\n },\n png: {\n packageName: '@jsquash/png',\n packageVersion: '3.1.1',\n packagePath: 'codec/pkg/squoosh_png_bg.wasm',\n publicFile: 'squoosh_png_bg.wasm',\n sha256: '263d6e658808a74b72a1a99c5cc1d619237e70c150db6e41d5d84d3d117ab9be',\n },\n resize: {\n packageName: '@jsquash/resize',\n packageVersion: '2.1.1',\n packagePath: 'lib/resize/pkg/squoosh_resize_bg.wasm',\n publicFile: 'squoosh_resize_bg.wasm',\n sha256: '5b1f702d502c4d0a70b99f78691bd554d566ba95859e4c57af5435955a1d74a5',\n },\n webpEncoder: {\n packageName: '@jsquash/webp',\n packageVersion: '1.5.0',\n packagePath: 'codec/enc/webp_enc.wasm',\n publicFile: 'webp_enc.wasm',\n sha256: 'b6085bb6702f144e9dc6016d58d230b34a84976bf0d080b7390b4b4b137d6ab7',\n },\n dracoDecoderScript: {\n packageName: 'draco3dgltf',\n packageVersion: '1.5.7',\n packagePath: 'draco_decoder_gltf_nodejs.js',\n publicFile: 'draco_decoder_gltf.js',\n sha256: '8bb2952d2ba7d67e1414f8df819410cb0434a666be53f671fff75f68843d76f6',\n },\n dracoDecoderWasm: {\n packageName: 'draco3dgltf',\n packageVersion: '1.5.7',\n packagePath: 'draco_decoder_gltf.wasm',\n publicFile: 'draco_decoder_gltf.wasm',\n sha256: '712db3449ae2041d6e8a224c395bda6cedb49e51322fae38b7db9beb8b381889',\n },\n dracoEncoderScript: {\n packageName: 'draco3dgltf',\n packageVersion: '1.5.7',\n packagePath: 'draco_encoder_gltf_nodejs.js',\n publicFile: 'draco_encoder_gltf.js',\n sha256: '8434adecd1446459601763e499be3697546056bca90b286a538ae0483a00845a',\n },\n dracoEncoderWasm: {\n packageName: 'draco3dgltf',\n packageVersion: '1.5.7',\n packagePath: 'draco_encoder.wasm',\n publicFile: 'draco_encoder.wasm',\n sha256: 'd2a3ac80c91980d5d321c116454834ff36264825c6d1794cc84e42288f158958',\n },\n} as const\n\nexport type CodecArtifactName = keyof typeof CODEC_ARTIFACTS\nexport type ImageWasmModuleName = Extract<\n CodecArtifactName,\n 'avifEncoder' | 'jpegDecoder' | 'jpegEncoder' | 'png' | 'resize' | 'webpEncoder'\n>\n\nexport const CODEC_MANIFEST = {\n chopperVersion: packageMetadata.version,\n manifestHash: CODEC_MANIFEST_HASH,\n gltfTransformVersion: '4.4.2',\n meshoptimizerVersion: '1.0.1',\n artifacts: CODEC_ARTIFACTS,\n} as const\n\nexport const ENGINE_ID = `chopper@${packageMetadata.version}:${CODEC_MANIFEST_HASH}` as const\n"]}
@@ -1,4 +1,4 @@
1
- import { CODEC_MANIFEST_HASH, CODEC_MANIFEST, ENGINE_ID, failure, success, CODEC_ARTIFACTS } from './chunk-M7J64NCO.js';
1
+ import { CODEC_MANIFEST_HASH, CODEC_MANIFEST, ENGINE_ID, failure, success, CODEC_ARTIFACTS } from './chunk-6WQPPVM2.js';
2
2
  import { __commonJS, __toESM } from './chunk-5WRI5ZAA.js';
3
3
 
4
4
  // ../../node_modules/.pnpm/iota-array@1.0.0/node_modules/iota-array/iota.js
@@ -1274,8 +1274,131 @@ var require_ndarray_ops = __commonJS({
1274
1274
  }
1275
1275
  });
1276
1276
 
1277
+ // src/codecs/hdr-source.ts
1278
+ var HDR_SOURCE_MIME_TYPES = {
1279
+ exr: "image/x-exr",
1280
+ hdr: "image/vnd.radiance"
1281
+ };
1282
+ function isHdrSourceMimeType(mimeType) {
1283
+ return mimeType === "image/x-exr" || mimeType === "image/vnd.radiance";
1284
+ }
1285
+ var EXR_MAGIC = [118, 47, 49, 1];
1286
+ var VERSION_NUMBER_FIELD = 255;
1287
+ var NON_IMAGE_FLAG = 2048;
1288
+ var MULTI_PART_FILE_FLAG = 4096;
1289
+ var EXR_COMPRESSION = [
1290
+ { name: "NO_COMPRESSION", supported: true },
1291
+ { name: "RLE", supported: true },
1292
+ { name: "ZIPS", supported: true },
1293
+ { name: "ZIP", supported: true },
1294
+ { name: "PIZ", supported: true },
1295
+ { name: "PXR24", supported: false },
1296
+ { name: "B44", supported: false },
1297
+ { name: "B44A", supported: false },
1298
+ { name: "DWAA", supported: false },
1299
+ { name: "DWAB", supported: false }
1300
+ ];
1301
+ var RADIANCE_MAGIC = [35, 63];
1302
+ var RADIANCE_HEADER_LIMIT = 8192;
1303
+ var MAX_HDR_SOURCE_TEXELS = 12 * 1024 * 1024;
1304
+ function detectHdrContainer(bytes) {
1305
+ if (bytes.length >= 8 && EXR_MAGIC.every((byte, index) => bytes[index] === byte)) return "exr";
1306
+ if (bytes.length >= 8 && RADIANCE_MAGIC.every((byte, index) => bytes[index] === byte)) return "hdr";
1307
+ return void 0;
1308
+ }
1309
+ function readHdrSource(bytes) {
1310
+ const container = detectHdrContainer(bytes);
1311
+ if (!container) return { error: "Not an OpenEXR or Radiance HDR file" };
1312
+ return container === "exr" ? readExr(bytes) : readRadiance(bytes);
1313
+ }
1314
+ function readExr(bytes) {
1315
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1316
+ const version = view.getInt32(4, true);
1317
+ if ((version & VERSION_NUMBER_FIELD) !== 2) {
1318
+ return { error: `Unsupported EXR version: ${version & VERSION_NUMBER_FIELD}` };
1319
+ }
1320
+ if (version & MULTI_PART_FILE_FLAG) return { error: "Multi-part EXR files are not supported" };
1321
+ if (version & NON_IMAGE_FLAG) return { error: "Deep EXR files are not supported" };
1322
+ let size;
1323
+ let compressionIssue;
1324
+ let offset = 8;
1325
+ while (offset < bytes.length) {
1326
+ const name = readNullTerminated(bytes, offset);
1327
+ if (name === void 0) return { error: "Malformed EXR header" };
1328
+ if (name.value === "") break;
1329
+ offset = name.next;
1330
+ const type = readNullTerminated(bytes, offset);
1331
+ if (type === void 0) return { error: "Malformed EXR header" };
1332
+ offset = type.next;
1333
+ if (offset + 4 > bytes.length) return { error: "Malformed EXR header" };
1334
+ const length3 = view.getInt32(offset, true);
1335
+ offset += 4;
1336
+ if (length3 < 0 || offset + length3 > bytes.length) return { error: "Malformed EXR header" };
1337
+ if (name.value === "dataWindow") {
1338
+ if (type.value !== "box2i" || length3 !== 16) return { error: "Malformed EXR dataWindow" };
1339
+ const xMin = view.getInt32(offset, true);
1340
+ const yMin = view.getInt32(offset + 4, true);
1341
+ const xMax = view.getInt32(offset + 8, true);
1342
+ const yMax = view.getInt32(offset + 12, true);
1343
+ const width = xMax - xMin + 1;
1344
+ const height = yMax - yMin + 1;
1345
+ if (width < 1 || height < 1) return { error: "EXR dataWindow is empty" };
1346
+ size = { container: "exr", width, height };
1347
+ }
1348
+ if (name.value === "compression") {
1349
+ if (length3 !== 1) return { error: "Malformed EXR compression attribute" };
1350
+ compressionIssue = exrCompressionIssue(bytes[offset]);
1351
+ }
1352
+ offset += length3;
1353
+ }
1354
+ if (compressionIssue) return { error: compressionIssue };
1355
+ return size ?? { error: "EXR header has no dataWindow attribute" };
1356
+ }
1357
+ function exrCompressionIssue(code) {
1358
+ const codec = EXR_COMPRESSION[code];
1359
+ if (!codec) return `Unsupported EXR compression: unknown codec ${code}`;
1360
+ if (codec.supported) return void 0;
1361
+ const readable = EXR_COMPRESSION.filter((entry) => entry.supported).map((entry) => entry.name).join(", ");
1362
+ return `EXR uses ${codec.name} compression, which the HDR encoder cannot read. Re-export with one of: ${readable}. EXR compression only affects the file on disk \u2014 it does not change the encoded KTX2.`;
1363
+ }
1364
+ function readRadiance(bytes) {
1365
+ const text = latin1(bytes.subarray(0, RADIANCE_HEADER_LIMIT));
1366
+ const lines = text.split("\n");
1367
+ let index = 0;
1368
+ while (index < lines.length && lines[index] !== "" && lines[index] !== "\r") index++;
1369
+ index++;
1370
+ while (index < lines.length && (lines[index] === "" || lines[index] === "\r")) index++;
1371
+ if (index >= lines.length) return { error: "Radiance file has no resolution line" };
1372
+ const resolution = /^([+-][XY])\s+(\d+)\s+([+-][XY])\s+(\d+)\s*$/.exec(lines[index].replace(/\r$/, ""));
1373
+ if (!resolution) return { error: `Malformed Radiance resolution line: ${lines[index].slice(0, 40)}` };
1374
+ const extents = /* @__PURE__ */ new Map([
1375
+ [resolution[1][1], Number(resolution[2])],
1376
+ [resolution[3][1], Number(resolution[4])]
1377
+ ]);
1378
+ const width = extents.get("X");
1379
+ const height = extents.get("Y");
1380
+ if (width === void 0 || height === void 0) {
1381
+ return { error: "Radiance resolution line must pair an X and a Y axis" };
1382
+ }
1383
+ if (width < 1 || height < 1) return { error: "Radiance image is empty" };
1384
+ return { container: "hdr", width, height };
1385
+ }
1386
+ function readNullTerminated(bytes, start) {
1387
+ const limit = Math.min(bytes.length, start + 256);
1388
+ for (let index = start; index < limit; index++) {
1389
+ if (bytes[index] === 0) {
1390
+ return { value: latin1(bytes.subarray(start, index)), next: index + 1 };
1391
+ }
1392
+ }
1393
+ return void 0;
1394
+ }
1395
+ function latin1(bytes) {
1396
+ let value = "";
1397
+ for (const byte of bytes) value += String.fromCharCode(byte);
1398
+ return value;
1399
+ }
1400
+
1277
1401
  // src/core/recipes.ts
1278
- var RECIPE_SCHEMA_VERSION = 1;
1279
1402
  function outputExtension(encoding) {
1280
1403
  if (encoding.codec === "ktx2") return ".ktx2";
1281
1404
  if (encoding.codec === "jpeg") return ".jpg";
@@ -1304,13 +1427,11 @@ function normalizeRecipe(value) {
1304
1427
  const recipe = validated.data;
1305
1428
  if (recipe.kind === "image") {
1306
1429
  return success({
1307
- schemaVersion: RECIPE_SCHEMA_VERSION,
1308
1430
  kind: "image",
1309
1431
  encode: normalizeEncoding(recipe.encode)
1310
1432
  });
1311
1433
  }
1312
1434
  return success({
1313
- schemaVersion: RECIPE_SCHEMA_VERSION,
1314
1435
  kind: "glb",
1315
1436
  methods: recipe.methods ?? [],
1316
1437
  textures: (recipe.textures ?? []).map((texture) => ({
@@ -1364,15 +1485,15 @@ function encodingShapeIssue(encoding) {
1364
1485
  if (!["webp", "avif", "jpeg", "png", "ktx2"].includes(String(candidate.codec))) {
1365
1486
  return `Unsupported image codec: ${String(candidate.codec)}`;
1366
1487
  }
1367
- if (candidate.codec === "ktx2" && candidate.mode !== "uastc" && candidate.mode !== "etc1s") {
1488
+ if (candidate.codec === "ktx2" && candidate.mode !== "uastc" && candidate.mode !== "etc1s" && candidate.mode !== "uastc-hdr") {
1368
1489
  return `Unsupported KTX2 mode: ${String(candidate.mode)}`;
1369
1490
  }
1491
+ if (candidate.codec === "ktx2" && candidate.mode === "uastc-hdr" && candidate.maxSize !== void 0) {
1492
+ return "UASTC HDR does not support maxSize; resizing requires a decoded 8-bit image. Author the source at its target resolution.";
1493
+ }
1370
1494
  return void 0;
1371
1495
  }
1372
1496
  function recipeIssue(recipe) {
1373
- if (recipe.schemaVersion !== RECIPE_SCHEMA_VERSION) {
1374
- return `Unsupported recipe schema version: ${String(recipe.schemaVersion)}`;
1375
- }
1376
1497
  if (recipe.kind === "image") return encodingIssue(recipe.encode);
1377
1498
  const labels = /* @__PURE__ */ new Set();
1378
1499
  const selectors = /* @__PURE__ */ new Set();
@@ -1394,6 +1515,9 @@ function recipeIssue(recipe) {
1394
1515
  }
1395
1516
  selectors.add(key);
1396
1517
  }
1518
+ if (texture.encode.codec === "ktx2" && texture.encode.mode === "uastc-hdr") {
1519
+ return `Texture recipe "${texture.label}" uses UASTC HDR, which KHR_texture_basisu does not cover; HDR is only supported for standalone image recipes`;
1520
+ }
1397
1521
  const issue = encodingIssue(texture.encode);
1398
1522
  if (issue) return issue;
1399
1523
  }
@@ -1415,7 +1539,7 @@ function selectorValues(selector) {
1415
1539
  return success(selector.indices);
1416
1540
  }
1417
1541
  function encodingIssue(encoding) {
1418
- if (encoding.maxSize) {
1542
+ if ("maxSize" in encoding && encoding.maxSize) {
1419
1543
  const [width, height] = encoding.maxSize;
1420
1544
  if (!Number.isInteger(width) || width < 1 || !Number.isInteger(height) || height < 1) {
1421
1545
  return "Texture maxSize must contain positive integers";
@@ -1444,6 +1568,9 @@ function encodingIssue(encoding) {
1444
1568
  return "UASTC RDO lambda must be between 0.001 and 10";
1445
1569
  }
1446
1570
  }
1571
+ if (encoding.codec === "ktx2" && encoding.mode === "uastc-hdr" && encoding.level !== void 0 && (!Number.isInteger(encoding.level) || encoding.level < 0 || encoding.level > 4)) {
1572
+ return "UASTC HDR level must be an integer between 0 and 4";
1573
+ }
1447
1574
  if (encoding.codec === "ktx2" && encoding.mode === "etc1s" && encoding.compression !== void 0 && (!Number.isInteger(encoding.compression) || encoding.compression < 0 || encoding.compression > 6)) {
1448
1575
  return "ETC1S compression must be an integer between 0 and 6";
1449
1576
  }
@@ -1476,6 +1603,14 @@ function normalizeEncoding(encoding) {
1476
1603
  case "png":
1477
1604
  return { ...encoding };
1478
1605
  case "ktx2":
1606
+ if (encoding.mode === "uastc-hdr") {
1607
+ return {
1608
+ ...encoding,
1609
+ level: encoding.level ?? 1,
1610
+ mipmaps: encoding.mipmaps ?? true,
1611
+ zstd: encoding.zstd ?? true
1612
+ };
1613
+ }
1479
1614
  if (encoding.mode === "uastc") {
1480
1615
  return {
1481
1616
  ...encoding,
@@ -1596,8 +1731,11 @@ function detectAsset(input) {
1596
1731
  if (input.bytes.length >= 4 && input.bytes[0] === 103 && input.bytes[1] === 108 && input.bytes[2] === 84 && input.bytes[3] === 70) {
1597
1732
  return success("glb");
1598
1733
  }
1599
- if (isPng(input.bytes) || isJpeg(input.bytes)) return success("image");
1600
- return failure("unsupported_asset", `Unsupported asset: ${input.fileName}; expected GLB, PNG, or JPEG`);
1734
+ if (isPng(input.bytes) || isJpeg(input.bytes) || detectHdrContainer(input.bytes)) return success("image");
1735
+ return failure(
1736
+ "unsupported_asset",
1737
+ `Unsupported asset: ${input.fileName}; expected GLB, PNG, JPEG, EXR, or Radiance HDR`
1738
+ );
1601
1739
  }
1602
1740
  async function inspectImage(input, mimeType, engine) {
1603
1741
  const dimensions = await engine.inspect({ bytes: input.bytes, mimeType });
@@ -1611,6 +1749,8 @@ async function inspectImage(input, mimeType, engine) {
1611
1749
  });
1612
1750
  }
1613
1751
  function imageMimeType(input) {
1752
+ const hdr = detectHdrContainer(input.bytes);
1753
+ if (hdr) return HDR_SOURCE_MIME_TYPES[hdr];
1614
1754
  return isPng(input.bytes) ? "image/png" : "image/jpeg";
1615
1755
  }
1616
1756
  function isPng(bytes) {
@@ -26436,12 +26576,23 @@ function encodeToKTX2(imageBuffer, options = {}) {
26436
26576
  // src/codecs/image-engine.ts
26437
26577
  var CodecLoadError = class extends Error {
26438
26578
  };
26579
+ var SourceError = class extends Error {
26580
+ constructor(code, message) {
26581
+ super(message);
26582
+ this.code = code;
26583
+ }
26584
+ };
26439
26585
  function createWasmImageEngine(loader) {
26440
26586
  const adapters = createAdapterCache(loader);
26441
26587
  return {
26442
26588
  id: ENGINE_ID,
26443
26589
  manifestHash: CODEC_MANIFEST_HASH,
26444
26590
  async inspect(source) {
26591
+ if (isHdrSourceMimeType(source.mimeType)) {
26592
+ const header = readHdrSource(source.bytes);
26593
+ if ("error" in header) return failure("decode_failed", header.error);
26594
+ return success({ width: header.width, height: header.height });
26595
+ }
26445
26596
  try {
26446
26597
  const image = await decodeSource(source, adapters);
26447
26598
  return success({ width: image.width, height: image.height });
@@ -26450,10 +26601,17 @@ function createWasmImageEngine(loader) {
26450
26601
  }
26451
26602
  },
26452
26603
  async encode(source, encoding, signal) {
26453
- const valid = validateRecipe({ schemaVersion: 1, kind: "image", encode: encoding });
26604
+ const valid = validateRecipe({ kind: "image", encode: encoding });
26454
26605
  if (!valid.ok) return valid;
26606
+ const mismatch = sourcePairingIssue(source, encoding);
26607
+ if (mismatch) return mismatch;
26455
26608
  try {
26456
26609
  assertNotAborted2(signal);
26610
+ if (isHdrEncoding(encoding)) {
26611
+ const encoded2 = await encodeKtx2Hdr(source, encoding, await loader.basisEncoderUrl());
26612
+ assertNotAborted2(signal);
26613
+ return success(encoded2);
26614
+ }
26457
26615
  let image = await decodeSource(source, adapters);
26458
26616
  assertNotAborted2(signal);
26459
26617
  if (encoding.maxSize) {
@@ -26608,6 +26766,45 @@ async function encodeRaster(image, encoding, adapters) {
26608
26766
  }
26609
26767
  return { bytes: toUint8Array(bytes), mimeType: outputMimeType(encoding) };
26610
26768
  }
26769
+ var megapixels = (texels) => (texels / 1e6).toFixed(1);
26770
+ function isHdrEncoding(encoding) {
26771
+ return encoding.codec === "ktx2" && encoding.mode === "uastc-hdr";
26772
+ }
26773
+ function sourcePairingIssue(source, encoding) {
26774
+ const isHdrSource = isHdrSourceMimeType(source.mimeType);
26775
+ if (isHdrEncoding(encoding) && !isHdrSource) {
26776
+ return failure("invalid_recipe", `UASTC HDR requires an EXR or Radiance HDR source; received ${source.mimeType}`);
26777
+ }
26778
+ if (isHdrSource && !isHdrEncoding(encoding)) {
26779
+ return failure(
26780
+ "invalid_recipe",
26781
+ "HDR sources can only be encoded as KTX2 with mode 'uastc-hdr'; chopper does not decode HDR pixels"
26782
+ );
26783
+ }
26784
+ return void 0;
26785
+ }
26786
+ async function encodeKtx2Hdr(source, encoding, wasmUrl) {
26787
+ const header = readHdrSource(source.bytes);
26788
+ if ("error" in header) throw new SourceError("decode_failed", header.error);
26789
+ const texels = header.width * header.height;
26790
+ if (texels > MAX_HDR_SOURCE_TEXELS) {
26791
+ throw new SourceError(
26792
+ "unsupported_asset",
26793
+ `Source is ${header.width}x${header.height} (${megapixels(texels)} Mpix), over the ${megapixels(MAX_HDR_SOURCE_TEXELS)} Mpix HDR encoder limit. Reduce the total pixel count (3072x3072 and 4096x3072 both fit) or split the source into tiles.`
26794
+ );
26795
+ }
26796
+ const bytes = await encodeToKTX2(source.bytes, {
26797
+ isHDR: true,
26798
+ imageType: header.container,
26799
+ isUASTC: true,
26800
+ hdrQualityLevel: encoding.level ?? 1,
26801
+ needSupercompression: encoding.zstd ?? true,
26802
+ generateMipmap: encoding.mipmaps ?? true,
26803
+ isKTX2File: true,
26804
+ wasmUrl
26805
+ });
26806
+ return { bytes, mimeType: outputMimeType(encoding), width: header.width, height: header.height };
26807
+ }
26611
26808
  async function encodeKtx2(source, image, encoding, wasmUrl) {
26612
26809
  const srgb = encoding.colorSpace !== "linear";
26613
26810
  const bytes = await encodeToKTX2(source.bytes, {
@@ -26633,7 +26830,9 @@ async function encodeKtx2(source, image, encoding, wasmUrl) {
26633
26830
  return { bytes, mimeType: outputMimeType(encoding) };
26634
26831
  }
26635
26832
  function operationFailure(code, message, cause) {
26636
- return cause instanceof CodecLoadError ? failure("codec_load_failed", cause.message, cause.cause) : failure(code, message, cause);
26833
+ if (cause instanceof CodecLoadError) return failure("codec_load_failed", cause.message, cause.cause);
26834
+ if (cause instanceof SourceError) return failure(cause.code, cause.message);
26835
+ return failure(code, message, cause);
26637
26836
  }
26638
26837
  function assertNotAborted2(signal) {
26639
26838
  if (signal?.aborted) throw new DOMException("Optimization was aborted", "AbortError");
@@ -26743,5 +26942,5 @@ is-buffer/index.js:
26743
26942
  */
26744
26943
 
26745
26944
  export { createInProcessChopper };
26746
- //# sourceMappingURL=in-process-7XITYSC4.js.map
26747
- //# sourceMappingURL=in-process-7XITYSC4.js.map
26945
+ //# sourceMappingURL=in-process-P6TQNY2L.js.map
26946
+ //# sourceMappingURL=in-process-P6TQNY2L.js.map