@joycostudio/chopper 0.7.1 → 0.8.1
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 +70 -15
- package/THIRD_PARTY_NOTICES.md +44 -9
- package/codecs/manifest.json +9 -2
- package/codecs/zstd.wasm +0 -0
- package/dist/browser/{chunk-7Z5IRY5V.js → chunk-6PIXH7DA.js} +11 -4
- package/dist/browser/chunk-6PIXH7DA.js.map +1 -0
- package/dist/browser/{in-process-XV5GR557.js → in-process-FAMTTLSK.js} +251 -14
- package/dist/browser/in-process-FAMTTLSK.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 +295 -58
- package/dist/cli/index.js.map +1 -1
- package/dist/config/index.d.ts +47 -21
- package/dist/config/index.js +45 -37
- 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 +53 -21
- package/dist/node/index.js +292 -57
- package/dist/node/index.js.map +1 -1
- package/package.json +5 -2
- package/dist/browser/chunk-7Z5IRY5V.js.map +0 -1
- package/dist/browser/in-process-XV5GR557.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.8.1"};
|
|
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,
|
|
@@ -1363,19 +1609,27 @@ function createNodeChopper(options = {}) {
|
|
|
1363
1609
|
}
|
|
1364
1610
|
|
|
1365
1611
|
// src/config/validation.ts
|
|
1366
|
-
var
|
|
1612
|
+
var DEFAULT_BUILD_DIRECTORY = ".chopper";
|
|
1613
|
+
var DEFAULT_OUTPUT_DIRECTORY = "compressed";
|
|
1367
1614
|
function validateConfig(config) {
|
|
1368
1615
|
if (!config || typeof config !== "object" || Array.isArray(config)) {
|
|
1369
1616
|
return failure("invalid_recipe", "Config must be an object");
|
|
1370
1617
|
}
|
|
1371
1618
|
const candidate = config;
|
|
1372
|
-
const unknownField = unknownFieldIssue(
|
|
1619
|
+
const unknownField = unknownFieldIssue(
|
|
1620
|
+
candidate,
|
|
1621
|
+
["buildDirectory", "outputDirectory", "rules", "concurrency"],
|
|
1622
|
+
"config"
|
|
1623
|
+
);
|
|
1373
1624
|
if (unknownField) return failure("invalid_recipe", unknownField);
|
|
1374
|
-
if (candidate.
|
|
1375
|
-
return failure("invalid_recipe", "Config
|
|
1625
|
+
if (candidate.buildDirectory !== void 0 && (typeof candidate.buildDirectory !== "string" || !candidate.buildDirectory)) {
|
|
1626
|
+
return failure("invalid_recipe", "Config buildDirectory must be a non-empty string");
|
|
1376
1627
|
}
|
|
1377
|
-
if (candidate.
|
|
1378
|
-
return failure(
|
|
1628
|
+
if (candidate.buildDirectory !== void 0 && !isProjectRelativePath(candidate.buildDirectory, true)) {
|
|
1629
|
+
return failure(
|
|
1630
|
+
"invalid_recipe",
|
|
1631
|
+
`Build directory must be a project-relative POSIX path: ${candidate.buildDirectory}`
|
|
1632
|
+
);
|
|
1379
1633
|
}
|
|
1380
1634
|
if (candidate.outputDirectory !== void 0 && !isSafePathSegment(candidate.outputDirectory)) {
|
|
1381
1635
|
return failure(
|
|
@@ -1394,7 +1648,7 @@ function validateConfig(config) {
|
|
|
1394
1648
|
return failure("invalid_recipe", `Config rule ${index} must be an object`);
|
|
1395
1649
|
}
|
|
1396
1650
|
const entry = rule;
|
|
1397
|
-
const ruleUnknownField = unknownFieldIssue(entry, ["include", "exclude", "recipe"
|
|
1651
|
+
const ruleUnknownField = unknownFieldIssue(entry, ["include", "exclude", "recipe"], `config rule ${index}`);
|
|
1398
1652
|
if (ruleUnknownField) return failure("invalid_recipe", ruleUnknownField);
|
|
1399
1653
|
if (!isStringArray(entry.include)) {
|
|
1400
1654
|
return failure("invalid_recipe", `Config rule ${index} include must be an array of glob patterns`);
|
|
@@ -1408,19 +1662,14 @@ function validateConfig(config) {
|
|
|
1408
1662
|
if (invalidGlob) {
|
|
1409
1663
|
return failure("invalid_recipe", `Config rule ${index} glob must be project-relative: ${invalidGlob}`);
|
|
1410
1664
|
}
|
|
1411
|
-
if (entry.path !== void 0 && (typeof entry.path !== "string" || !entry.path)) {
|
|
1412
|
-
return failure("invalid_recipe", `Config rule ${index} path must be a non-empty string`);
|
|
1413
|
-
}
|
|
1414
|
-
if (candidate.outputDirectory !== void 0 && entry.path !== void 0) {
|
|
1415
|
-
return failure(
|
|
1416
|
-
"invalid_recipe",
|
|
1417
|
-
`Config rule ${index} path cannot be combined with outputDirectory; authoritative sibling output routing determines the output path`
|
|
1418
|
-
);
|
|
1419
|
-
}
|
|
1420
1665
|
const recipe = validateRecipe(entry.recipe);
|
|
1421
1666
|
if (!recipe.ok) return recipe;
|
|
1422
1667
|
}
|
|
1423
|
-
return success({
|
|
1668
|
+
return success({
|
|
1669
|
+
...config,
|
|
1670
|
+
buildDirectory: candidate.buildDirectory ?? DEFAULT_BUILD_DIRECTORY,
|
|
1671
|
+
outputDirectory: candidate.outputDirectory ?? DEFAULT_OUTPUT_DIRECTORY
|
|
1672
|
+
});
|
|
1424
1673
|
}
|
|
1425
1674
|
function normalizeProjectPath(path) {
|
|
1426
1675
|
if (typeof path !== "string") return void 0;
|
|
@@ -1486,15 +1735,14 @@ function createConfigAssetPlanner(config) {
|
|
|
1486
1735
|
if (!normalizedInput) {
|
|
1487
1736
|
return failure("invalid_recipe", `Asset input must be a project-relative POSIX path: ${input}`);
|
|
1488
1737
|
}
|
|
1489
|
-
if (
|
|
1738
|
+
if (isGeneratedOutput(normalizedInput, prepared.data.outputDirectory)) {
|
|
1490
1739
|
return success({ kind: "output", input: normalizedInput });
|
|
1491
1740
|
}
|
|
1492
1741
|
const planned = /* @__PURE__ */ new Map();
|
|
1493
1742
|
for (const { rule, matches } of prepared.data.rules) {
|
|
1494
1743
|
if (!matches(normalizedInput)) continue;
|
|
1495
1744
|
const output = resolveOutput(rule, normalizedInput, prepared.data.outputDirectory);
|
|
1496
|
-
|
|
1497
|
-
planned.set(output.data, { input: normalizedInput, output: output.data, rule });
|
|
1745
|
+
planned.set(output, { input: normalizedInput, output, rule });
|
|
1498
1746
|
}
|
|
1499
1747
|
const outputs = [...planned.values()];
|
|
1500
1748
|
return outputs.length ? success({ kind: "source", input: normalizedInput, outputs }) : success({ kind: "unmatched", input: normalizedInput });
|
|
@@ -1544,19 +1792,7 @@ function resolveOutput(rule, input, outputDirectory) {
|
|
|
1544
1792
|
const dot = base.lastIndexOf(".");
|
|
1545
1793
|
const name = dot <= 0 ? base : base.slice(0, dot);
|
|
1546
1794
|
const extension = rule.recipe.kind === "glb" ? ".glb" : outputExtension(rule.recipe.encode);
|
|
1547
|
-
|
|
1548
|
-
return success(`${directory ? `${directory}/` : ""}${outputDirectory}/${name}${extension}`);
|
|
1549
|
-
}
|
|
1550
|
-
const template = rule.path ?? "{dir}/{name}.{ext}";
|
|
1551
|
-
if (template.includes("\\") || template.startsWith("/") || /^[A-Za-z]:\//.test(template)) {
|
|
1552
|
-
return failure("invalid_recipe", `Output path must stay inside the configured output directory: ${template}`);
|
|
1553
|
-
}
|
|
1554
|
-
const rendered = template.replaceAll("{dir}/", directory ? `${directory}/` : "").replaceAll("{dir}", directory).replaceAll("{name}", name).replaceAll("{ext}", extension.slice(1));
|
|
1555
|
-
if (/\{[^{}]+\}/.test(rendered)) {
|
|
1556
|
-
return failure("invalid_recipe", `Output path contains an unsupported placeholder: ${template}`);
|
|
1557
|
-
}
|
|
1558
|
-
const output = normalizeProjectPath(rendered);
|
|
1559
|
-
return output ? success(output) : failure("invalid_recipe", `Output path must stay inside the configured output directory: ${template}`);
|
|
1795
|
+
return `${directory ? `${directory}/` : ""}${outputDirectory}/${name}${extension}`;
|
|
1560
1796
|
}
|
|
1561
1797
|
function isGeneratedOutput(input, outputDirectory) {
|
|
1562
1798
|
return input.split("/").slice(0, -1).includes(outputDirectory);
|
|
@@ -1564,8 +1800,8 @@ function isGeneratedOutput(input, outputDirectory) {
|
|
|
1564
1800
|
|
|
1565
1801
|
// src/config/index.ts
|
|
1566
1802
|
function createChopper2(config) {
|
|
1567
|
-
const {
|
|
1568
|
-
const defined = {
|
|
1803
|
+
const { buildDirectory = DEFAULT_BUILD_DIRECTORY, outputDirectory = DEFAULT_OUTPUT_DIRECTORY, ...rest } = config;
|
|
1804
|
+
const defined = { buildDirectory, outputDirectory, ...rest };
|
|
1569
1805
|
const planner = createConfigAssetPlanner(defined);
|
|
1570
1806
|
Object.defineProperties(defined, {
|
|
1571
1807
|
route: { enumerable: false, value: planner.route },
|
|
@@ -1608,17 +1844,22 @@ async function runConfig(options = {}) {
|
|
|
1608
1844
|
config.concurrency ?? 2,
|
|
1609
1845
|
(asset) => executeAsset(asset, mode, cwd, chopper2)
|
|
1610
1846
|
);
|
|
1611
|
-
return success({
|
|
1847
|
+
return success({
|
|
1848
|
+
mode,
|
|
1849
|
+
buildDirectory: relative(cwd, resolve(cwd, config.buildDirectory)),
|
|
1850
|
+
files,
|
|
1851
|
+
totals: summarize(files)
|
|
1852
|
+
});
|
|
1612
1853
|
}
|
|
1613
1854
|
async function planAssets(config, cwd) {
|
|
1614
1855
|
const validated = validateConfig(config);
|
|
1615
1856
|
if (!validated.ok) return validated;
|
|
1616
1857
|
const configured = validated.data;
|
|
1617
1858
|
const planned = [];
|
|
1618
|
-
const
|
|
1619
|
-
const relativeRoot = relative(cwd,
|
|
1859
|
+
const buildRoot = resolve(cwd, configured.buildDirectory);
|
|
1860
|
+
const relativeRoot = relative(cwd, buildRoot);
|
|
1620
1861
|
if (isAbsolute(relativeRoot) || relativeRoot === ".." || relativeRoot.startsWith(`..${sep}`)) {
|
|
1621
|
-
return failure("invalid_recipe", `
|
|
1862
|
+
return failure("invalid_recipe", `Build directory must be inside the project: ${configured.buildDirectory}`);
|
|
1622
1863
|
}
|
|
1623
1864
|
const planner = createChopper2(configured);
|
|
1624
1865
|
const discoveryPatterns = configured.rules.flatMap(
|
|
@@ -1632,13 +1873,12 @@ async function planAssets(config, cwd) {
|
|
|
1632
1873
|
planned.push(
|
|
1633
1874
|
...routed.data.outputs.map(({ input, output, rule }) => ({
|
|
1634
1875
|
input: resolve(cwd, input),
|
|
1635
|
-
output: resolve(
|
|
1876
|
+
output: resolve(buildRoot, output),
|
|
1636
1877
|
rule
|
|
1637
1878
|
}))
|
|
1638
1879
|
);
|
|
1639
1880
|
}
|
|
1640
1881
|
const resolved = /* @__PURE__ */ new Map();
|
|
1641
|
-
const inputs = new Set(planned.map(({ input }) => input));
|
|
1642
1882
|
for (const asset of planned) {
|
|
1643
1883
|
const owner = resolved.get(asset.output);
|
|
1644
1884
|
if (owner && owner.input !== asset.input) {
|
|
@@ -1647,9 +1887,6 @@ async function planAssets(config, cwd) {
|
|
|
1647
1887
|
`Output collision: ${relative(cwd, owner.input)} and ${relative(cwd, asset.input)} both write ${relative(cwd, asset.output)}`
|
|
1648
1888
|
);
|
|
1649
1889
|
}
|
|
1650
|
-
if (asset.output !== asset.input && inputs.has(asset.output)) {
|
|
1651
|
-
return failure("invalid_recipe", `Output loop: ${relative(cwd, asset.output)} is also a matched input`);
|
|
1652
|
-
}
|
|
1653
1890
|
resolved.set(asset.output, asset);
|
|
1654
1891
|
}
|
|
1655
1892
|
return success([...resolved.values()]);
|
|
@@ -1808,7 +2045,7 @@ var COMMANDS = {
|
|
|
1808
2045
|
"Options:",
|
|
1809
2046
|
CONFIG_OPTION,
|
|
1810
2047
|
"",
|
|
1811
|
-
"Optimizes each matched asset and writes it into the config
|
|
2048
|
+
"Optimizes each matched asset and writes it into the config build directory",
|
|
1812
2049
|
"through a temporary sibling and an atomic rename. Prints the batch report as",
|
|
1813
2050
|
"JSON and exits 1 when any asset fails."
|
|
1814
2051
|
].join("\n"),
|