@joycostudio/chopper 0.8.0 → 0.9.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 +120 -3
- package/THIRD_PARTY_NOTICES.md +44 -9
- package/codecs/manifest.json +9 -2
- package/codecs/zstd.wasm +0 -0
- package/dist/browser/{chunk-TNQ7E4HG.js → chunk-STDFGSQX.js} +11 -4
- package/dist/browser/chunk-STDFGSQX.js.map +1 -0
- package/dist/browser/{in-process-3KN2LY3L.js → in-process-MJMPQZDQ.js} +251 -14
- package/dist/browser/in-process-MJMPQZDQ.js.map +1 -0
- package/dist/browser/index.d.ts +32 -5
- package/dist/browser/index.js +2 -2
- package/dist/browser/worker.js +274 -14
- package/dist/browser/worker.js.map +1 -1
- package/dist/cli/index.js +285 -29
- package/dist/cli/index.js.map +1 -1
- package/dist/config/index.d.ts +52 -7
- package/dist/config/index.js +186 -5
- package/dist/config/index.js.map +1 -1
- package/dist/index.d.ts +43 -9
- package/dist/index.js +38 -4
- package/dist/index.js.map +1 -1
- package/dist/node/index.d.ts +59 -8
- package/dist/node/index.js +283 -29
- package/dist/node/index.js.map +1 -1
- package/package.json +5 -2
- package/dist/browser/chunk-TNQ7E4HG.js.map +0 -1
- package/dist/browser/in-process-3KN2LY3L.js.map +0 -1
package/dist/cli/index.js
CHANGED
|
@@ -226,11 +226,18 @@ function latin1(bytes) {
|
|
|
226
226
|
|
|
227
227
|
// package.json
|
|
228
228
|
var package_default = {
|
|
229
|
-
version: "0.
|
|
229
|
+
version: "0.9.0"};
|
|
230
230
|
|
|
231
231
|
// src/codecs/manifest.ts
|
|
232
|
-
var CODEC_MANIFEST_HASH = "
|
|
232
|
+
var CODEC_MANIFEST_HASH = "1a43d902a113a7dcfa7801bcbd90998e401898dbad555f913e1e7f4352d9c63c";
|
|
233
233
|
var CODEC_ARTIFACTS = {
|
|
234
|
+
zstdEncoder: {
|
|
235
|
+
packageName: "@bokuweb/zstd-wasm",
|
|
236
|
+
packageVersion: "0.0.27",
|
|
237
|
+
packagePath: "dist/common/zstd.wasm",
|
|
238
|
+
publicFile: "zstd.wasm",
|
|
239
|
+
sha256: "80260eee5795ee8072bb5821e203730c74dca3909938f5ac1128da67f96223df"
|
|
240
|
+
},
|
|
234
241
|
avifEncoder: {
|
|
235
242
|
packageName: "@jsquash/avif",
|
|
236
243
|
packageVersion: "2.1.1",
|
|
@@ -320,6 +327,123 @@ var failure = (code, message, cause) => ({
|
|
|
320
327
|
error: cause === void 0 ? { code, message } : { code, message, cause }
|
|
321
328
|
});
|
|
322
329
|
|
|
330
|
+
// src/codecs/raw-ktx2.ts
|
|
331
|
+
var IDENTIFIER = new Uint8Array([171, 75, 84, 88, 32, 50, 48, 187, 13, 10, 26, 10]);
|
|
332
|
+
var HEADER_SIZE = 104;
|
|
333
|
+
var DFD_SIZE = 92;
|
|
334
|
+
function rawSourceIssue(source) {
|
|
335
|
+
if (source.mimeType !== "image/png" && source.mimeType !== "image/jpeg") {
|
|
336
|
+
return failure(
|
|
337
|
+
"unsupported_asset",
|
|
338
|
+
"Raw KTX2 requires an 8-bit PNG or JPEG source; HDR/float sources are unsupported"
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
if (source.mimeType === "image/png" && source.bytes.length > 24 && source.bytes[24] === 16) {
|
|
342
|
+
return failure("unsupported_asset", "Raw KTX2 does not support 16-bit PNG; author an RGBA8 source");
|
|
343
|
+
}
|
|
344
|
+
return void 0;
|
|
345
|
+
}
|
|
346
|
+
function rgbaDescriptor(srgb) {
|
|
347
|
+
const bytes = new Uint8Array(DFD_SIZE);
|
|
348
|
+
const view = new DataView(bytes.buffer);
|
|
349
|
+
view.setUint32(0, DFD_SIZE, true);
|
|
350
|
+
view.setUint16(8, 2, true);
|
|
351
|
+
view.setUint16(10, DFD_SIZE - 4, true);
|
|
352
|
+
bytes[12] = 1;
|
|
353
|
+
bytes[13] = srgb ? 1 : 0;
|
|
354
|
+
bytes[14] = srgb ? 2 : 1;
|
|
355
|
+
bytes[20] = 4;
|
|
356
|
+
for (let channel = 0; channel < 4; channel++) {
|
|
357
|
+
const offset = 28 + channel * 16;
|
|
358
|
+
view.setUint16(offset, channel * 8, true);
|
|
359
|
+
bytes[offset + 2] = 7;
|
|
360
|
+
bytes[offset + 3] = channel === 3 ? 15 | (srgb ? 16 : 0) : channel;
|
|
361
|
+
view.setUint32(offset + 12, 255, true);
|
|
362
|
+
}
|
|
363
|
+
return bytes;
|
|
364
|
+
}
|
|
365
|
+
function writeRawKtx2(width, height, payload, encoding) {
|
|
366
|
+
const srgb = encoding.colorSpace === "srgb";
|
|
367
|
+
const descriptor = rgbaDescriptor(srgb);
|
|
368
|
+
const orientation = new TextEncoder().encode("KTXorientation\0rd\0");
|
|
369
|
+
const kvdOffset = HEADER_SIZE + descriptor.length;
|
|
370
|
+
const kvdLength = 4 + Math.ceil(orientation.length / 4) * 4;
|
|
371
|
+
const levelOffset = kvdOffset + kvdLength;
|
|
372
|
+
const bytes = new Uint8Array(levelOffset + payload.length);
|
|
373
|
+
const view = new DataView(bytes.buffer);
|
|
374
|
+
bytes.set(IDENTIFIER);
|
|
375
|
+
[
|
|
376
|
+
srgb ? 43 : 37,
|
|
377
|
+
1,
|
|
378
|
+
width,
|
|
379
|
+
height,
|
|
380
|
+
0,
|
|
381
|
+
0,
|
|
382
|
+
1,
|
|
383
|
+
1,
|
|
384
|
+
encoding.zstd === false ? 0 : 2,
|
|
385
|
+
HEADER_SIZE,
|
|
386
|
+
descriptor.length,
|
|
387
|
+
kvdOffset,
|
|
388
|
+
kvdLength
|
|
389
|
+
].forEach((value, index) => {
|
|
390
|
+
view.setUint32(12 + index * 4, value, true);
|
|
391
|
+
});
|
|
392
|
+
view.setBigUint64(80, BigInt(levelOffset), true);
|
|
393
|
+
view.setBigUint64(88, BigInt(payload.length), true);
|
|
394
|
+
view.setBigUint64(96, BigInt(width * height * 4), true);
|
|
395
|
+
bytes.set(descriptor, HEADER_SIZE);
|
|
396
|
+
view.setUint32(kvdOffset, orientation.length, true);
|
|
397
|
+
bytes.set(orientation, kvdOffset + 4);
|
|
398
|
+
bytes.set(payload, levelOffset);
|
|
399
|
+
return bytes;
|
|
400
|
+
}
|
|
401
|
+
function validateRawKtx2(bytes, width, height, encoding) {
|
|
402
|
+
if (bytes.length < HEADER_SIZE || !IDENTIFIER.every((byte, i) => bytes[i] === byte)) return false;
|
|
403
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
404
|
+
const u32 = (offset) => view.getUint32(offset, true);
|
|
405
|
+
const u64 = (offset) => Number(view.getBigUint64(offset, true));
|
|
406
|
+
const expected = [
|
|
407
|
+
encoding.colorSpace === "srgb" ? 43 : 37,
|
|
408
|
+
1,
|
|
409
|
+
width,
|
|
410
|
+
height,
|
|
411
|
+
0,
|
|
412
|
+
0,
|
|
413
|
+
1,
|
|
414
|
+
1,
|
|
415
|
+
encoding.zstd === false ? 0 : 2
|
|
416
|
+
];
|
|
417
|
+
if (!expected.every((value, i) => u32(12 + i * 4) === value)) return false;
|
|
418
|
+
if (width < 1 || height < 1 || !Number.isSafeInteger(width * height * 4)) return false;
|
|
419
|
+
if (u64(64) !== 0 || u64(72) !== 0 || u64(96) !== width * height * 4) return false;
|
|
420
|
+
const dfdOffset = u32(48);
|
|
421
|
+
const dfdLength = u32(52);
|
|
422
|
+
const kvdOffset = u32(56);
|
|
423
|
+
const kvdLength = u32(60);
|
|
424
|
+
const levelOffset = u64(80);
|
|
425
|
+
const levelLength = u64(88);
|
|
426
|
+
if (dfdOffset < HEADER_SIZE || dfdOffset % 4 !== 0 || dfdLength !== DFD_SIZE || kvdOffset < dfdOffset + dfdLength || kvdOffset % 4 !== 0 || kvdLength % 4 !== 0 || levelOffset < kvdOffset + kvdLength || !Number.isSafeInteger(levelOffset) || !Number.isSafeInteger(levelLength) || levelLength < 1 || levelOffset + levelLength !== bytes.length)
|
|
427
|
+
return false;
|
|
428
|
+
if (encoding.zstd === false && (levelOffset % 4 !== 0 || levelLength !== width * height * 4)) return false;
|
|
429
|
+
if (!rgbaDescriptor(encoding.colorSpace === "srgb").every((byte, i) => bytes[dfdOffset + i] === byte)) return false;
|
|
430
|
+
let orientation;
|
|
431
|
+
for (let offset = kvdOffset; offset < kvdOffset + kvdLength; ) {
|
|
432
|
+
const length = u32(offset);
|
|
433
|
+
const end = offset + 4 + length;
|
|
434
|
+
if (length < 2 || end > kvdOffset + kvdLength) return false;
|
|
435
|
+
const entry = bytes.subarray(offset + 4, end);
|
|
436
|
+
const separator = entry.indexOf(0);
|
|
437
|
+
if (separator < 1) return false;
|
|
438
|
+
if (new TextDecoder().decode(entry.subarray(0, separator)) === "KTXorientation") {
|
|
439
|
+
if (orientation !== void 0) return false;
|
|
440
|
+
orientation = new TextDecoder().decode(entry.subarray(separator + 1));
|
|
441
|
+
}
|
|
442
|
+
offset += 4 + Math.ceil(length / 4) * 4;
|
|
443
|
+
}
|
|
444
|
+
return orientation === "rd\0";
|
|
445
|
+
}
|
|
446
|
+
|
|
323
447
|
// src/core/object-validation.ts
|
|
324
448
|
function unknownFieldIssue(value, allowedFields, subject) {
|
|
325
449
|
const field = Object.keys(value).find((key) => !allowedFields.includes(key));
|
|
@@ -406,6 +530,9 @@ function recipeShapeIssue(recipe) {
|
|
|
406
530
|
if (!Array.isArray(selector[valueField])) return "Texture recipe selectors are malformed";
|
|
407
531
|
const encodingIssue2 = encodingShapeIssue(entry.encode);
|
|
408
532
|
if (encodingIssue2) return encodingIssue2;
|
|
533
|
+
if (entry.encode.codec === "ktx2" && entry.encode.mode === "raw") {
|
|
534
|
+
return `Texture recipe "${entry.label}" uses raw RGBA8, which KHR_texture_basisu does not cover; raw mode is only supported for standalone image recipes`;
|
|
535
|
+
}
|
|
409
536
|
}
|
|
410
537
|
if (candidate.geometry !== void 0 && candidate.geometry !== false && (!candidate.geometry || typeof candidate.geometry !== "object" || !["draco", "meshopt"].includes(String(candidate.geometry.codec)))) {
|
|
411
538
|
return "GLB geometry must be false, Draco, or Meshopt";
|
|
@@ -426,15 +553,18 @@ function encodingShapeIssue(encoding) {
|
|
|
426
553
|
if (!["webp", "avif", "jpeg", "png", "ktx2"].includes(String(candidate.codec))) {
|
|
427
554
|
return `Unsupported image codec: ${String(candidate.codec)}`;
|
|
428
555
|
}
|
|
429
|
-
if (candidate.codec === "ktx2" && candidate.mode !== "uastc" && candidate.mode !== "etc1s" && candidate.mode !== "uastc-hdr") {
|
|
556
|
+
if (candidate.codec === "ktx2" && candidate.mode !== "uastc" && candidate.mode !== "etc1s" && candidate.mode !== "raw" && candidate.mode !== "uastc-hdr") {
|
|
430
557
|
return `Unsupported KTX2 mode: ${String(candidate.mode)}`;
|
|
431
558
|
}
|
|
432
559
|
if (candidate.codec === "ktx2" && candidate.mode === "uastc-hdr" && candidate.maxSize !== void 0) {
|
|
433
560
|
return "UASTC HDR does not support maxSize; resizing requires a decoded 8-bit image. Author the source at its target resolution.";
|
|
434
561
|
}
|
|
435
|
-
const allowedFields = candidate.codec === "webp" ? ["codec", "maxSize", "quality", "effort", "lossless", "nearLossless"] : candidate.codec === "avif" ? ["codec", "maxSize", "quality", "effort", "lossless", "chromaSubsampling"] : candidate.codec === "jpeg" ? ["codec", "maxSize", "quality", "chromaSubsampling"] : candidate.codec === "png" ? ["codec", "maxSize"] : candidate.mode === "uastc" ? ["codec", "mode", "maxSize", "colorSpace", "normalMap", "level", "mipmaps", "zstd", "rdo"] : candidate.mode === "etc1s" ? ["codec", "mode", "maxSize", "colorSpace", "normalMap", "quality", "compression", "mipmaps"] : ["codec", "mode", "level", "mipmaps", "zstd"];
|
|
562
|
+
const allowedFields = candidate.codec === "webp" ? ["codec", "maxSize", "quality", "effort", "lossless", "nearLossless"] : candidate.codec === "avif" ? ["codec", "maxSize", "quality", "effort", "lossless", "chromaSubsampling"] : candidate.codec === "jpeg" ? ["codec", "maxSize", "quality", "chromaSubsampling"] : candidate.codec === "png" ? ["codec", "maxSize"] : candidate.mode === "uastc" ? ["codec", "mode", "maxSize", "colorSpace", "normalMap", "level", "mipmaps", "zstd", "rdo"] : candidate.mode === "etc1s" ? ["codec", "mode", "maxSize", "colorSpace", "normalMap", "quality", "compression", "mipmaps"] : candidate.mode === "raw" ? ["codec", "mode", "colorSpace", "mipmaps", "zstd", "zstdLevel"] : ["codec", "mode", "level", "mipmaps", "zstd"];
|
|
436
563
|
const unknownField = unknownFieldIssue(candidate, allowedFields, "image encoding");
|
|
437
564
|
if (unknownField) return unknownField;
|
|
565
|
+
if (candidate.codec === "ktx2" && candidate.mode === "raw" && candidate.zstd === false && candidate.zstdLevel !== void 0) {
|
|
566
|
+
return "Raw KTX2 zstdLevel requires Zstandard supercompression";
|
|
567
|
+
}
|
|
438
568
|
if (candidate.codec === "ktx2" && candidate.mode === "uastc" && candidate.rdo !== void 0) {
|
|
439
569
|
if (!candidate.rdo || typeof candidate.rdo !== "object" || Array.isArray(candidate.rdo)) {
|
|
440
570
|
return "UASTC RDO must be an object";
|
|
@@ -490,6 +620,18 @@ function selectorValues(selector) {
|
|
|
490
620
|
return success(selector.indices);
|
|
491
621
|
}
|
|
492
622
|
function encodingIssue(encoding) {
|
|
623
|
+
if (encoding.codec === "ktx2" && encoding.mode === "raw") {
|
|
624
|
+
if (encoding.colorSpace !== void 0 && encoding.colorSpace !== "linear" && encoding.colorSpace !== "srgb") {
|
|
625
|
+
return "Raw KTX2 colorSpace must be linear or srgb";
|
|
626
|
+
}
|
|
627
|
+
if (encoding.mipmaps !== void 0 && encoding.mipmaps !== false) return "Raw KTX2 mipmaps must be false";
|
|
628
|
+
if (encoding.zstd !== void 0 && typeof encoding.zstd !== "boolean") return "Raw KTX2 zstd must be a boolean";
|
|
629
|
+
if (encoding.zstdLevel !== void 0) {
|
|
630
|
+
if (!Number.isInteger(encoding.zstdLevel) || encoding.zstdLevel < 1 || encoding.zstdLevel > 19) {
|
|
631
|
+
return "Raw KTX2 zstdLevel must be an integer between 1 and 19";
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
493
635
|
if ("maxSize" in encoding && encoding.maxSize) {
|
|
494
636
|
const [width, height] = encoding.maxSize;
|
|
495
637
|
if (!Number.isInteger(width) || width < 1 || !Number.isInteger(height) || height < 1) {
|
|
@@ -554,6 +696,15 @@ function normalizeEncoding(encoding) {
|
|
|
554
696
|
case "png":
|
|
555
697
|
return { ...encoding };
|
|
556
698
|
case "ktx2":
|
|
699
|
+
if (encoding.mode === "raw") {
|
|
700
|
+
return {
|
|
701
|
+
codec: "ktx2",
|
|
702
|
+
mode: "raw",
|
|
703
|
+
colorSpace: encoding.colorSpace ?? "linear",
|
|
704
|
+
mipmaps: false,
|
|
705
|
+
...encoding.zstd === false ? { zstd: false } : { zstd: true, zstdLevel: encoding.zstdLevel ?? 19 }
|
|
706
|
+
};
|
|
707
|
+
}
|
|
557
708
|
if (encoding.mode === "uastc-hdr") {
|
|
558
709
|
return {
|
|
559
710
|
...encoding,
|
|
@@ -642,6 +793,11 @@ function createChopper(runtime) {
|
|
|
642
793
|
if (kind.data !== "image" || recipe.kind !== "image") {
|
|
643
794
|
return failure("invalid_recipe", "Recipe and input type do not match");
|
|
644
795
|
}
|
|
796
|
+
const rawEncoding = recipe.encode.codec === "ktx2" && recipe.encode.mode === "raw" ? recipe.encode : void 0;
|
|
797
|
+
if (rawEncoding) {
|
|
798
|
+
const issue = rawSourceIssue({ bytes: input.bytes, mimeType: imageMimeType(input) });
|
|
799
|
+
if (issue) return issue;
|
|
800
|
+
}
|
|
645
801
|
const inspection = await inspectImage(input, imageMimeType(input), runtime.image);
|
|
646
802
|
if (!inspection.ok) return inspection;
|
|
647
803
|
const encoded = await runtime.image.encode(
|
|
@@ -654,6 +810,12 @@ function createChopper(runtime) {
|
|
|
654
810
|
if (!hasExpectedSignature(encoded.data.bytes, encoded.data.mimeType)) {
|
|
655
811
|
return failure("validation_failed", `Encoded bytes do not match ${encoded.data.mimeType}`);
|
|
656
812
|
}
|
|
813
|
+
if (rawEncoding && (encoded.data.mimeType !== "image/ktx2" || !validateRawKtx2(encoded.data.bytes, inspection.data.width, inspection.data.height, rawEncoding))) {
|
|
814
|
+
return failure(
|
|
815
|
+
"validation_failed",
|
|
816
|
+
"Raw KTX2 output does not match the source dimensions or RGBA8 container contract"
|
|
817
|
+
);
|
|
818
|
+
}
|
|
657
819
|
const report = createReport({
|
|
658
820
|
recipe: normalized.data,
|
|
659
821
|
beforeBytes: input.bytes.byteLength,
|
|
@@ -997,6 +1159,57 @@ function inspectDocument(document, byteLength) {
|
|
|
997
1159
|
}
|
|
998
1160
|
};
|
|
999
1161
|
}
|
|
1162
|
+
|
|
1163
|
+
// src/codecs/zstd.ts
|
|
1164
|
+
async function createZstdEncoder(module) {
|
|
1165
|
+
let memory;
|
|
1166
|
+
const abort = () => {
|
|
1167
|
+
throw new Error("Zstandard WASM aborted");
|
|
1168
|
+
};
|
|
1169
|
+
const instance = await WebAssembly.instantiate(module, {
|
|
1170
|
+
a: {
|
|
1171
|
+
a: abort,
|
|
1172
|
+
// proc_exit
|
|
1173
|
+
b: () => {
|
|
1174
|
+
},
|
|
1175
|
+
// emscripten_runtime_keepalive_clear
|
|
1176
|
+
c: abort,
|
|
1177
|
+
// abort_js
|
|
1178
|
+
d: abort,
|
|
1179
|
+
// setitimer_js (unused by the synchronous compressor)
|
|
1180
|
+
e: (requestedSize) => {
|
|
1181
|
+
try {
|
|
1182
|
+
memory.grow(Math.ceil((requestedSize - memory.buffer.byteLength) / 65536));
|
|
1183
|
+
return 1;
|
|
1184
|
+
} catch {
|
|
1185
|
+
return 0;
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
});
|
|
1190
|
+
const api = instance.exports;
|
|
1191
|
+
memory = api.f;
|
|
1192
|
+
api.g();
|
|
1193
|
+
return (data, level) => {
|
|
1194
|
+
let source = 0;
|
|
1195
|
+
let destination = 0;
|
|
1196
|
+
try {
|
|
1197
|
+
const capacity = api.i(data.length);
|
|
1198
|
+
source = api.s(data.length);
|
|
1199
|
+
destination = api.s(capacity);
|
|
1200
|
+
if (!source || !destination) throw new Error("Could not allocate Zstandard buffers");
|
|
1201
|
+
new Uint8Array(memory.buffer, source, data.length).set(data);
|
|
1202
|
+
const size = api.m(destination, capacity, source, data.length, level);
|
|
1203
|
+
if (api.h(size)) throw new Error(`Zstandard compression failed: ${size}`);
|
|
1204
|
+
return new Uint8Array(memory.buffer, destination, size).slice();
|
|
1205
|
+
} finally {
|
|
1206
|
+
if (source) api.t(source);
|
|
1207
|
+
if (destination) api.t(destination);
|
|
1208
|
+
}
|
|
1209
|
+
};
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
// src/codecs/image-engine.ts
|
|
1000
1213
|
var CodecLoadError = class extends Error {
|
|
1001
1214
|
};
|
|
1002
1215
|
var SourceError = class extends Error {
|
|
@@ -1011,12 +1224,11 @@ function createWasmImageEngine(loader) {
|
|
|
1011
1224
|
id: ENGINE_ID,
|
|
1012
1225
|
manifestHash: CODEC_MANIFEST_HASH,
|
|
1013
1226
|
async inspect(source) {
|
|
1014
|
-
if (isHdrSourceMimeType(source.mimeType)) {
|
|
1015
|
-
const header = readHdrSource(source.bytes);
|
|
1016
|
-
if ("error" in header) return failure("decode_failed", header.error);
|
|
1017
|
-
return success({ width: header.width, height: header.height });
|
|
1018
|
-
}
|
|
1019
1227
|
try {
|
|
1228
|
+
if (isHdrSourceMimeType(source.mimeType)) {
|
|
1229
|
+
const header = readSupportedHdrSource(source.bytes);
|
|
1230
|
+
return success({ width: header.width, height: header.height });
|
|
1231
|
+
}
|
|
1020
1232
|
const image = await decodeSource(source, adapters);
|
|
1021
1233
|
return success({ width: image.width, height: image.height });
|
|
1022
1234
|
} catch (cause) {
|
|
@@ -1026,6 +1238,10 @@ function createWasmImageEngine(loader) {
|
|
|
1026
1238
|
async encode(source, encoding, signal) {
|
|
1027
1239
|
const valid = validateRecipe({ kind: "image", encode: encoding });
|
|
1028
1240
|
if (!valid.ok) return valid;
|
|
1241
|
+
if (encoding.codec === "ktx2" && encoding.mode === "raw") {
|
|
1242
|
+
const issue = rawSourceIssue(source);
|
|
1243
|
+
if (issue) return issue;
|
|
1244
|
+
}
|
|
1029
1245
|
const mismatch = sourcePairingIssue(source, encoding);
|
|
1030
1246
|
if (mismatch) return mismatch;
|
|
1031
1247
|
try {
|
|
@@ -1035,8 +1251,30 @@ function createWasmImageEngine(loader) {
|
|
|
1035
1251
|
assertNotAborted2(signal);
|
|
1036
1252
|
return success(encoded2);
|
|
1037
1253
|
}
|
|
1038
|
-
let image
|
|
1254
|
+
let image;
|
|
1255
|
+
try {
|
|
1256
|
+
image = await decodeSource(source, adapters);
|
|
1257
|
+
} catch (cause) {
|
|
1258
|
+
if (encoding.codec === "ktx2" && encoding.mode === "raw") {
|
|
1259
|
+
return operationFailure("decode_failed", "Could not decode image", cause);
|
|
1260
|
+
}
|
|
1261
|
+
throw cause;
|
|
1262
|
+
}
|
|
1039
1263
|
assertNotAborted2(signal);
|
|
1264
|
+
if (encoding.codec === "ktx2" && encoding.mode === "raw") {
|
|
1265
|
+
const rgba = new Uint8Array(image.data.buffer, image.data.byteOffset, image.data.byteLength);
|
|
1266
|
+
if (rgba.length !== image.width * image.height * 4) throw new Error("Decoder did not return RGBA8 pixels");
|
|
1267
|
+
const compress = encoding.zstd === false ? void 0 : await adapters.zstd();
|
|
1268
|
+
assertNotAborted2(signal);
|
|
1269
|
+
const payload = compress ? compress(rgba, encoding.zstdLevel ?? 19) : rgba;
|
|
1270
|
+
assertNotAborted2(signal);
|
|
1271
|
+
return success({
|
|
1272
|
+
bytes: writeRawKtx2(image.width, image.height, payload, encoding),
|
|
1273
|
+
mimeType: outputMimeType(encoding),
|
|
1274
|
+
width: image.width,
|
|
1275
|
+
height: image.height
|
|
1276
|
+
});
|
|
1277
|
+
}
|
|
1040
1278
|
if (encoding.maxSize) {
|
|
1041
1279
|
image = await resizeWithin(image, encoding.maxSize, adapters);
|
|
1042
1280
|
}
|
|
@@ -1064,6 +1302,7 @@ function createAdapterCache(loader) {
|
|
|
1064
1302
|
let png;
|
|
1065
1303
|
let resizeModule;
|
|
1066
1304
|
let webp;
|
|
1305
|
+
let zstd;
|
|
1067
1306
|
const load = async (name) => {
|
|
1068
1307
|
try {
|
|
1069
1308
|
return await loader.load(name);
|
|
@@ -1072,6 +1311,9 @@ function createAdapterCache(loader) {
|
|
|
1072
1311
|
}
|
|
1073
1312
|
};
|
|
1074
1313
|
return {
|
|
1314
|
+
zstd: () => zstd ??= load("zstdEncoder").then(createZstdEncoder).catch((cause) => {
|
|
1315
|
+
throw new CodecLoadError("Could not initialize zstdEncoder", { cause });
|
|
1316
|
+
}),
|
|
1075
1317
|
avif: () => avif ??= load("avifEncoder").then(
|
|
1076
1318
|
(module) => initEmscriptenModule(avifEncoderFactory, module)
|
|
1077
1319
|
),
|
|
@@ -1206,16 +1448,20 @@ function sourcePairingIssue(source, encoding) {
|
|
|
1206
1448
|
}
|
|
1207
1449
|
return void 0;
|
|
1208
1450
|
}
|
|
1209
|
-
|
|
1210
|
-
const header = readHdrSource(
|
|
1451
|
+
function readSupportedHdrSource(bytes) {
|
|
1452
|
+
const header = readHdrSource(bytes);
|
|
1211
1453
|
if ("error" in header) throw new SourceError("decode_failed", header.error);
|
|
1212
1454
|
const texels = header.width * header.height;
|
|
1213
1455
|
if (texels > MAX_HDR_SOURCE_TEXELS) {
|
|
1214
1456
|
throw new SourceError(
|
|
1215
1457
|
"unsupported_asset",
|
|
1216
|
-
`Source is ${header.width}x${header.height} (${megapixels(texels)} Mpix), over the ${megapixels(MAX_HDR_SOURCE_TEXELS)} Mpix HDR
|
|
1458
|
+
`Source is ${header.width}x${header.height} (${megapixels(texels)} Mpix), over the ${megapixels(MAX_HDR_SOURCE_TEXELS)} Mpix HDR source limit. Reduce the total pixel count (3072x3072 and 4096x3072 both fit) or split the source into tiles.`
|
|
1217
1459
|
);
|
|
1218
1460
|
}
|
|
1461
|
+
return header;
|
|
1462
|
+
}
|
|
1463
|
+
async function encodeKtx2Hdr(source, encoding, wasmUrl) {
|
|
1464
|
+
const header = readSupportedHdrSource(source.bytes);
|
|
1219
1465
|
const bytes = await encodeToKTX2(source.bytes, {
|
|
1220
1466
|
isHDR: true,
|
|
1221
1467
|
imageType: header.container,
|
|
@@ -1402,7 +1648,11 @@ function validateConfig(config) {
|
|
|
1402
1648
|
return failure("invalid_recipe", `Config rule ${index} must be an object`);
|
|
1403
1649
|
}
|
|
1404
1650
|
const entry = rule;
|
|
1405
|
-
const ruleUnknownField = unknownFieldIssue(
|
|
1651
|
+
const ruleUnknownField = unknownFieldIssue(
|
|
1652
|
+
entry,
|
|
1653
|
+
["include", "exclude", "recipe", "filename"],
|
|
1654
|
+
`config rule ${index}`
|
|
1655
|
+
);
|
|
1406
1656
|
if (ruleUnknownField) return failure("invalid_recipe", ruleUnknownField);
|
|
1407
1657
|
if (!isStringArray(entry.include)) {
|
|
1408
1658
|
return failure("invalid_recipe", `Config rule ${index} include must be an array of glob patterns`);
|
|
@@ -1416,6 +1666,9 @@ function validateConfig(config) {
|
|
|
1416
1666
|
if (invalidGlob) {
|
|
1417
1667
|
return failure("invalid_recipe", `Config rule ${index} glob must be project-relative: ${invalidGlob}`);
|
|
1418
1668
|
}
|
|
1669
|
+
if (entry.filename !== void 0 && typeof entry.filename !== "function") {
|
|
1670
|
+
return failure("invalid_recipe", `Config rule ${index} filename must be a function`);
|
|
1671
|
+
}
|
|
1419
1672
|
const recipe = validateRecipe(entry.recipe);
|
|
1420
1673
|
if (!recipe.ok) return recipe;
|
|
1421
1674
|
}
|
|
@@ -1496,7 +1749,8 @@ function createConfigAssetPlanner(config) {
|
|
|
1496
1749
|
for (const { rule, matches } of prepared.data.rules) {
|
|
1497
1750
|
if (!matches(normalizedInput)) continue;
|
|
1498
1751
|
const output = resolveOutput(rule, normalizedInput, prepared.data.outputDirectory);
|
|
1499
|
-
|
|
1752
|
+
if (!output.ok) return output;
|
|
1753
|
+
planned.set(output.data, { input: normalizedInput, output: output.data, rule });
|
|
1500
1754
|
}
|
|
1501
1755
|
const outputs = [...planned.values()];
|
|
1502
1756
|
return outputs.length ? success({ kind: "source", input: normalizedInput, outputs }) : success({ kind: "unmatched", input: normalizedInput });
|
|
@@ -1546,23 +1800,25 @@ function resolveOutput(rule, input, outputDirectory) {
|
|
|
1546
1800
|
const dot = base.lastIndexOf(".");
|
|
1547
1801
|
const name = dot <= 0 ? base : base.slice(0, dot);
|
|
1548
1802
|
const extension = rule.recipe.kind === "glb" ? ".glb" : outputExtension(rule.recipe.encode);
|
|
1549
|
-
|
|
1803
|
+
let filename = name;
|
|
1804
|
+
if (rule.filename) {
|
|
1805
|
+
try {
|
|
1806
|
+
filename = rule.filename({ input, directory, name, recipe: rule.recipe });
|
|
1807
|
+
} catch (cause) {
|
|
1808
|
+
return failure("invalid_recipe", `Config rule filename threw for ${input}`, cause);
|
|
1809
|
+
}
|
|
1810
|
+
if (!isSafePathSegment(filename)) {
|
|
1811
|
+
return failure(
|
|
1812
|
+
"invalid_recipe",
|
|
1813
|
+
`Config rule filename must return exactly one safe path segment for ${input}: ${String(filename)}`
|
|
1814
|
+
);
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1817
|
+
return success(`${directory ? `${directory}/` : ""}${outputDirectory}/${filename}${extension}`);
|
|
1550
1818
|
}
|
|
1551
1819
|
function isGeneratedOutput(input, outputDirectory) {
|
|
1552
1820
|
return input.split("/").slice(0, -1).includes(outputDirectory);
|
|
1553
1821
|
}
|
|
1554
|
-
|
|
1555
|
-
// src/config/index.ts
|
|
1556
|
-
function createChopper2(config) {
|
|
1557
|
-
const { buildDirectory = DEFAULT_BUILD_DIRECTORY, outputDirectory = DEFAULT_OUTPUT_DIRECTORY, ...rest } = config;
|
|
1558
|
-
const defined = { buildDirectory, outputDirectory, ...rest };
|
|
1559
|
-
const planner = createConfigAssetPlanner(defined);
|
|
1560
|
-
Object.defineProperties(defined, {
|
|
1561
|
-
route: { enumerable: false, value: planner.route },
|
|
1562
|
-
resolve: { enumerable: false, value: planner.resolve }
|
|
1563
|
-
});
|
|
1564
|
-
return defined;
|
|
1565
|
-
}
|
|
1566
1822
|
async function loadConfig(path = "chopper.config.ts") {
|
|
1567
1823
|
const absolutePath = resolve(path);
|
|
1568
1824
|
try {
|
|
@@ -1615,7 +1871,7 @@ async function planAssets(config, cwd) {
|
|
|
1615
1871
|
if (isAbsolute(relativeRoot) || relativeRoot === ".." || relativeRoot.startsWith(`..${sep}`)) {
|
|
1616
1872
|
return failure("invalid_recipe", `Build directory must be inside the project: ${configured.buildDirectory}`);
|
|
1617
1873
|
}
|
|
1618
|
-
const planner =
|
|
1874
|
+
const planner = createConfigAssetPlanner(configured);
|
|
1619
1875
|
const discoveryPatterns = configured.rules.flatMap(
|
|
1620
1876
|
({ include }) => include.filter((pattern) => !pattern.startsWith("!") || pattern.startsWith("!("))
|
|
1621
1877
|
);
|