@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/browser/index.d.ts
CHANGED
|
@@ -150,7 +150,28 @@ interface UastcHdrEncoding {
|
|
|
150
150
|
zstd?: boolean;
|
|
151
151
|
}
|
|
152
152
|
type RasterEncoding = WebpEncoding | AvifEncoding | JpegEncoding | PngEncoding;
|
|
153
|
-
|
|
153
|
+
/** Standalone, lossless RGBA8 texels. Color-space metadata never transforms samples. */
|
|
154
|
+
type RawKtx2Encoding = {
|
|
155
|
+
/** Store decoded RGBA8 pixels in KTX2. */
|
|
156
|
+
codec: 'ktx2';
|
|
157
|
+
/** Preserve original dimensions and every decoded RGBA8 byte, including transparent RGB. */
|
|
158
|
+
mode: 'raw';
|
|
159
|
+
/** Label unchanged samples; alpha is always linear. @defaultValue `'linear'` */
|
|
160
|
+
colorSpace?: 'linear' | 'srgb';
|
|
161
|
+
/** Raw mode stores exactly one base level. @defaultValue `false` */
|
|
162
|
+
mipmaps?: false;
|
|
163
|
+
} & ({
|
|
164
|
+
/** Compress the texels for delivery; GPU storage remains RGBA8. @defaultValue `true` */
|
|
165
|
+
zstd?: true;
|
|
166
|
+
/** Offline compression effort, integer 1–19; never changes fidelity. @defaultValue `19` */
|
|
167
|
+
zstdLevel?: number;
|
|
168
|
+
} | {
|
|
169
|
+
/** Store texels without supercompression. */
|
|
170
|
+
zstd: false;
|
|
171
|
+
/** Effort is unavailable when Zstandard is disabled. */
|
|
172
|
+
zstdLevel?: never;
|
|
173
|
+
});
|
|
174
|
+
type ImageEncoding = RasterEncoding | UastcEncoding | Etc1sEncoding | UastcHdrEncoding | RawKtx2Encoding;
|
|
154
175
|
type GlbProperty = 'accessor' | 'animation' | 'buffer' | 'camera' | 'material' | 'mesh' | 'node' | 'primitive' | 'primitiveTarget' | 'skin' | 'texture';
|
|
155
176
|
type GlbMethodRecipe = {
|
|
156
177
|
/** Merge equivalent accessors, materials, meshes, skins, or textures. */
|
|
@@ -213,8 +234,8 @@ interface EmbeddedTextureRecipe {
|
|
|
213
234
|
label: string;
|
|
214
235
|
/** Textures to encode. A texture cannot be claimed by more than one selector. */
|
|
215
236
|
select: TextureSelector;
|
|
216
|
-
/** Output encoding for the selected textures. UASTC HDR
|
|
217
|
-
encode: ImageEncoding
|
|
237
|
+
/** Output encoding for the selected textures. Raw RGBA8 and UASTC HDR are not supported inside GLBs. */
|
|
238
|
+
encode: Exclude<ImageEncoding, RawKtx2Encoding>;
|
|
218
239
|
}
|
|
219
240
|
type GeometryEncoding = {
|
|
220
241
|
/** Compress geometry with `KHR_draco_mesh_compression` for broad decoder support. */
|
|
@@ -240,7 +261,13 @@ interface ImageAssetRecipe {
|
|
|
240
261
|
encode: ImageEncoding;
|
|
241
262
|
}
|
|
242
263
|
type AssetRecipe = GlbAssetRecipe | ImageAssetRecipe;
|
|
243
|
-
type NormalizedImageEncoding = (Required<Omit<WebpEncoding, 'maxSize'>> & Pick<WebpEncoding, 'maxSize'>) | (Required<Omit<AvifEncoding, 'maxSize'>> & Pick<AvifEncoding, 'maxSize'>) | (Required<Omit<JpegEncoding, 'maxSize'>> & Pick<JpegEncoding, 'maxSize'>) | PngEncoding | (Required<Omit<UastcEncoding, 'maxSize' | 'rdo'>> & Pick<UastcEncoding, 'maxSize' | 'rdo'>) | (Required<Omit<Etc1sEncoding, 'maxSize'>> & Pick<Etc1sEncoding, 'maxSize'>) | Required<UastcHdrEncoding
|
|
264
|
+
type NormalizedImageEncoding = (Required<Omit<WebpEncoding, 'maxSize'>> & Pick<WebpEncoding, 'maxSize'>) | (Required<Omit<AvifEncoding, 'maxSize'>> & Pick<AvifEncoding, 'maxSize'>) | (Required<Omit<JpegEncoding, 'maxSize'>> & Pick<JpegEncoding, 'maxSize'>) | PngEncoding | (Required<Omit<UastcEncoding, 'maxSize' | 'rdo'>> & Pick<UastcEncoding, 'maxSize' | 'rdo'>) | (Required<Omit<Etc1sEncoding, 'maxSize'>> & Pick<Etc1sEncoding, 'maxSize'>) | Required<UastcHdrEncoding> | (Required<Pick<RawKtx2Encoding, 'codec' | 'mode' | 'colorSpace' | 'mipmaps'>> & ({
|
|
265
|
+
zstd: true;
|
|
266
|
+
zstdLevel: number;
|
|
267
|
+
} | {
|
|
268
|
+
zstd: false;
|
|
269
|
+
zstdLevel?: never;
|
|
270
|
+
}));
|
|
244
271
|
type NormalizedAssetRecipe = {
|
|
245
272
|
/** Identifies the normalized recipe as a standalone image recipe. */
|
|
246
273
|
kind: 'image';
|
|
@@ -254,7 +281,7 @@ type NormalizedAssetRecipe = {
|
|
|
254
281
|
/** Embedded texture groups with every codec default resolved. */
|
|
255
282
|
textures: readonly (Omit<EmbeddedTextureRecipe, 'encode'> & {
|
|
256
283
|
/** Output encoding with every codec default resolved. */
|
|
257
|
-
encode: NormalizedImageEncoding
|
|
284
|
+
encode: Exclude<NormalizedImageEncoding, RawKtx2Encoding>;
|
|
258
285
|
})[];
|
|
259
286
|
/** Resolved geometry codec, or `false` when geometry compression is disabled. */
|
|
260
287
|
geometry: GeometryEncoding | false;
|
package/dist/browser/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { failure, CODEC_MANIFEST_HASH, CODEC_MANIFEST, ENGINE_ID } from './chunk-
|
|
1
|
+
import { failure, CODEC_MANIFEST_HASH, CODEC_MANIFEST, ENGINE_ID } from './chunk-STDFGSQX.js';
|
|
2
2
|
import './chunk-5WRI5ZAA.js';
|
|
3
3
|
|
|
4
4
|
// src/browser/chopper.ts
|
|
@@ -6,7 +6,7 @@ async function createBrowserChopper(options = {}) {
|
|
|
6
6
|
if ((options.execution ?? "worker") === "worker") {
|
|
7
7
|
return createWorkerChopper(options);
|
|
8
8
|
}
|
|
9
|
-
const { createInProcessChopper } = await import('./in-process-
|
|
9
|
+
const { createInProcessChopper } = await import('./in-process-MJMPQZDQ.js');
|
|
10
10
|
return createInProcessChopper(options);
|
|
11
11
|
}
|
|
12
12
|
function createWorkerChopper(options) {
|
package/dist/browser/worker.js
CHANGED
|
@@ -49,7 +49,7 @@ var init_package = __esm({
|
|
|
49
49
|
"package.json"() {
|
|
50
50
|
package_default = {
|
|
51
51
|
name: "@joycostudio/chopper",
|
|
52
|
-
version: "0.
|
|
52
|
+
version: "0.9.0",
|
|
53
53
|
description: "Isomorphic asset optimization pipeline",
|
|
54
54
|
type: "module",
|
|
55
55
|
license: "ISC",
|
|
@@ -101,6 +101,7 @@ var init_package = __esm({
|
|
|
101
101
|
typecheck: "tsc --noEmit"
|
|
102
102
|
},
|
|
103
103
|
dependencies: {
|
|
104
|
+
"@bokuweb/zstd-wasm": "0.0.27",
|
|
104
105
|
"@gltf-transform/core": "4.4.2",
|
|
105
106
|
"@gltf-transform/extensions": "4.4.2",
|
|
106
107
|
"@gltf-transform/functions": "4.4.2",
|
|
@@ -118,7 +119,9 @@ var init_package = __esm({
|
|
|
118
119
|
},
|
|
119
120
|
devDependencies: {
|
|
120
121
|
"@types/draco3dgltf": "1.4.3",
|
|
121
|
-
"@types/picomatch": "4.0.3"
|
|
122
|
+
"@types/picomatch": "4.0.3",
|
|
123
|
+
fzstd: "0.1.1",
|
|
124
|
+
"ktx-parse": "1.0.1"
|
|
122
125
|
}
|
|
123
126
|
};
|
|
124
127
|
}
|
|
@@ -129,8 +132,15 @@ var CODEC_MANIFEST_HASH, CODEC_ARTIFACTS, CODEC_MANIFEST, ENGINE_ID;
|
|
|
129
132
|
var init_manifest = __esm({
|
|
130
133
|
"src/codecs/manifest.ts"() {
|
|
131
134
|
init_package();
|
|
132
|
-
CODEC_MANIFEST_HASH = "
|
|
135
|
+
CODEC_MANIFEST_HASH = "1a43d902a113a7dcfa7801bcbd90998e401898dbad555f913e1e7f4352d9c63c";
|
|
133
136
|
CODEC_ARTIFACTS = {
|
|
137
|
+
zstdEncoder: {
|
|
138
|
+
packageName: "@bokuweb/zstd-wasm",
|
|
139
|
+
packageVersion: "0.0.27",
|
|
140
|
+
packagePath: "dist/common/zstd.wasm",
|
|
141
|
+
publicFile: "zstd.wasm",
|
|
142
|
+
sha256: "80260eee5795ee8072bb5821e203730c74dca3909938f5ac1128da67f96223df"
|
|
143
|
+
},
|
|
134
144
|
avifEncoder: {
|
|
135
145
|
packageName: "@jsquash/avif",
|
|
136
146
|
packageVersion: "2.1.1",
|
|
@@ -349,6 +359,129 @@ var init_hdr_source = __esm({
|
|
|
349
359
|
}
|
|
350
360
|
});
|
|
351
361
|
|
|
362
|
+
// src/codecs/raw-ktx2.ts
|
|
363
|
+
function rawSourceIssue(source) {
|
|
364
|
+
if (source.mimeType !== "image/png" && source.mimeType !== "image/jpeg") {
|
|
365
|
+
return failure(
|
|
366
|
+
"unsupported_asset",
|
|
367
|
+
"Raw KTX2 requires an 8-bit PNG or JPEG source; HDR/float sources are unsupported"
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
if (source.mimeType === "image/png" && source.bytes.length > 24 && source.bytes[24] === 16) {
|
|
371
|
+
return failure("unsupported_asset", "Raw KTX2 does not support 16-bit PNG; author an RGBA8 source");
|
|
372
|
+
}
|
|
373
|
+
return void 0;
|
|
374
|
+
}
|
|
375
|
+
function rgbaDescriptor(srgb) {
|
|
376
|
+
const bytes = new Uint8Array(DFD_SIZE);
|
|
377
|
+
const view = new DataView(bytes.buffer);
|
|
378
|
+
view.setUint32(0, DFD_SIZE, true);
|
|
379
|
+
view.setUint16(8, 2, true);
|
|
380
|
+
view.setUint16(10, DFD_SIZE - 4, true);
|
|
381
|
+
bytes[12] = 1;
|
|
382
|
+
bytes[13] = srgb ? 1 : 0;
|
|
383
|
+
bytes[14] = srgb ? 2 : 1;
|
|
384
|
+
bytes[20] = 4;
|
|
385
|
+
for (let channel = 0; channel < 4; channel++) {
|
|
386
|
+
const offset = 28 + channel * 16;
|
|
387
|
+
view.setUint16(offset, channel * 8, true);
|
|
388
|
+
bytes[offset + 2] = 7;
|
|
389
|
+
bytes[offset + 3] = channel === 3 ? 15 | (srgb ? 16 : 0) : channel;
|
|
390
|
+
view.setUint32(offset + 12, 255, true);
|
|
391
|
+
}
|
|
392
|
+
return bytes;
|
|
393
|
+
}
|
|
394
|
+
function writeRawKtx2(width, height, payload, encoding) {
|
|
395
|
+
const srgb = encoding.colorSpace === "srgb";
|
|
396
|
+
const descriptor = rgbaDescriptor(srgb);
|
|
397
|
+
const orientation = new TextEncoder().encode("KTXorientation\0rd\0");
|
|
398
|
+
const kvdOffset = HEADER_SIZE + descriptor.length;
|
|
399
|
+
const kvdLength = 4 + Math.ceil(orientation.length / 4) * 4;
|
|
400
|
+
const levelOffset = kvdOffset + kvdLength;
|
|
401
|
+
const bytes = new Uint8Array(levelOffset + payload.length);
|
|
402
|
+
const view = new DataView(bytes.buffer);
|
|
403
|
+
bytes.set(IDENTIFIER);
|
|
404
|
+
[
|
|
405
|
+
srgb ? 43 : 37,
|
|
406
|
+
1,
|
|
407
|
+
width,
|
|
408
|
+
height,
|
|
409
|
+
0,
|
|
410
|
+
0,
|
|
411
|
+
1,
|
|
412
|
+
1,
|
|
413
|
+
encoding.zstd === false ? 0 : 2,
|
|
414
|
+
HEADER_SIZE,
|
|
415
|
+
descriptor.length,
|
|
416
|
+
kvdOffset,
|
|
417
|
+
kvdLength
|
|
418
|
+
].forEach((value, index) => {
|
|
419
|
+
view.setUint32(12 + index * 4, value, true);
|
|
420
|
+
});
|
|
421
|
+
view.setBigUint64(80, BigInt(levelOffset), true);
|
|
422
|
+
view.setBigUint64(88, BigInt(payload.length), true);
|
|
423
|
+
view.setBigUint64(96, BigInt(width * height * 4), true);
|
|
424
|
+
bytes.set(descriptor, HEADER_SIZE);
|
|
425
|
+
view.setUint32(kvdOffset, orientation.length, true);
|
|
426
|
+
bytes.set(orientation, kvdOffset + 4);
|
|
427
|
+
bytes.set(payload, levelOffset);
|
|
428
|
+
return bytes;
|
|
429
|
+
}
|
|
430
|
+
function validateRawKtx2(bytes, width, height, encoding) {
|
|
431
|
+
if (bytes.length < HEADER_SIZE || !IDENTIFIER.every((byte, i) => bytes[i] === byte)) return false;
|
|
432
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
433
|
+
const u32 = (offset) => view.getUint32(offset, true);
|
|
434
|
+
const u64 = (offset) => Number(view.getBigUint64(offset, true));
|
|
435
|
+
const expected = [
|
|
436
|
+
encoding.colorSpace === "srgb" ? 43 : 37,
|
|
437
|
+
1,
|
|
438
|
+
width,
|
|
439
|
+
height,
|
|
440
|
+
0,
|
|
441
|
+
0,
|
|
442
|
+
1,
|
|
443
|
+
1,
|
|
444
|
+
encoding.zstd === false ? 0 : 2
|
|
445
|
+
];
|
|
446
|
+
if (!expected.every((value, i) => u32(12 + i * 4) === value)) return false;
|
|
447
|
+
if (width < 1 || height < 1 || !Number.isSafeInteger(width * height * 4)) return false;
|
|
448
|
+
if (u64(64) !== 0 || u64(72) !== 0 || u64(96) !== width * height * 4) return false;
|
|
449
|
+
const dfdOffset = u32(48);
|
|
450
|
+
const dfdLength = u32(52);
|
|
451
|
+
const kvdOffset = u32(56);
|
|
452
|
+
const kvdLength = u32(60);
|
|
453
|
+
const levelOffset = u64(80);
|
|
454
|
+
const levelLength = u64(88);
|
|
455
|
+
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)
|
|
456
|
+
return false;
|
|
457
|
+
if (encoding.zstd === false && (levelOffset % 4 !== 0 || levelLength !== width * height * 4)) return false;
|
|
458
|
+
if (!rgbaDescriptor(encoding.colorSpace === "srgb").every((byte, i) => bytes[dfdOffset + i] === byte)) return false;
|
|
459
|
+
let orientation;
|
|
460
|
+
for (let offset = kvdOffset; offset < kvdOffset + kvdLength; ) {
|
|
461
|
+
const length3 = u32(offset);
|
|
462
|
+
const end = offset + 4 + length3;
|
|
463
|
+
if (length3 < 2 || end > kvdOffset + kvdLength) return false;
|
|
464
|
+
const entry = bytes.subarray(offset + 4, end);
|
|
465
|
+
const separator = entry.indexOf(0);
|
|
466
|
+
if (separator < 1) return false;
|
|
467
|
+
if (new TextDecoder().decode(entry.subarray(0, separator)) === "KTXorientation") {
|
|
468
|
+
if (orientation !== void 0) return false;
|
|
469
|
+
orientation = new TextDecoder().decode(entry.subarray(separator + 1));
|
|
470
|
+
}
|
|
471
|
+
offset += 4 + Math.ceil(length3 / 4) * 4;
|
|
472
|
+
}
|
|
473
|
+
return orientation === "rd\0";
|
|
474
|
+
}
|
|
475
|
+
var IDENTIFIER, HEADER_SIZE, DFD_SIZE;
|
|
476
|
+
var init_raw_ktx2 = __esm({
|
|
477
|
+
"src/codecs/raw-ktx2.ts"() {
|
|
478
|
+
init_result();
|
|
479
|
+
IDENTIFIER = new Uint8Array([171, 75, 84, 88, 32, 50, 48, 187, 13, 10, 26, 10]);
|
|
480
|
+
HEADER_SIZE = 104;
|
|
481
|
+
DFD_SIZE = 92;
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
|
|
352
485
|
// src/core/recipes.ts
|
|
353
486
|
function outputExtension(encoding) {
|
|
354
487
|
if (encoding.codec === "ktx2") return ".ktx2";
|
|
@@ -454,6 +587,9 @@ function recipeShapeIssue(recipe) {
|
|
|
454
587
|
if (!Array.isArray(selector[valueField])) return "Texture recipe selectors are malformed";
|
|
455
588
|
const encodingIssue2 = encodingShapeIssue(entry.encode);
|
|
456
589
|
if (encodingIssue2) return encodingIssue2;
|
|
590
|
+
if (entry.encode.codec === "ktx2" && entry.encode.mode === "raw") {
|
|
591
|
+
return `Texture recipe "${entry.label}" uses raw RGBA8, which KHR_texture_basisu does not cover; raw mode is only supported for standalone image recipes`;
|
|
592
|
+
}
|
|
457
593
|
}
|
|
458
594
|
if (candidate.geometry !== void 0 && candidate.geometry !== false && (!candidate.geometry || typeof candidate.geometry !== "object" || !["draco", "meshopt"].includes(String(candidate.geometry.codec)))) {
|
|
459
595
|
return "GLB geometry must be false, Draco, or Meshopt";
|
|
@@ -474,15 +610,18 @@ function encodingShapeIssue(encoding) {
|
|
|
474
610
|
if (!["webp", "avif", "jpeg", "png", "ktx2"].includes(String(candidate.codec))) {
|
|
475
611
|
return `Unsupported image codec: ${String(candidate.codec)}`;
|
|
476
612
|
}
|
|
477
|
-
if (candidate.codec === "ktx2" && candidate.mode !== "uastc" && candidate.mode !== "etc1s" && candidate.mode !== "uastc-hdr") {
|
|
613
|
+
if (candidate.codec === "ktx2" && candidate.mode !== "uastc" && candidate.mode !== "etc1s" && candidate.mode !== "raw" && candidate.mode !== "uastc-hdr") {
|
|
478
614
|
return `Unsupported KTX2 mode: ${String(candidate.mode)}`;
|
|
479
615
|
}
|
|
480
616
|
if (candidate.codec === "ktx2" && candidate.mode === "uastc-hdr" && candidate.maxSize !== void 0) {
|
|
481
617
|
return "UASTC HDR does not support maxSize; resizing requires a decoded 8-bit image. Author the source at its target resolution.";
|
|
482
618
|
}
|
|
483
|
-
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"];
|
|
619
|
+
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"];
|
|
484
620
|
const unknownField = unknownFieldIssue(candidate, allowedFields, "image encoding");
|
|
485
621
|
if (unknownField) return unknownField;
|
|
622
|
+
if (candidate.codec === "ktx2" && candidate.mode === "raw" && candidate.zstd === false && candidate.zstdLevel !== void 0) {
|
|
623
|
+
return "Raw KTX2 zstdLevel requires Zstandard supercompression";
|
|
624
|
+
}
|
|
486
625
|
if (candidate.codec === "ktx2" && candidate.mode === "uastc" && candidate.rdo !== void 0) {
|
|
487
626
|
if (!candidate.rdo || typeof candidate.rdo !== "object" || Array.isArray(candidate.rdo)) {
|
|
488
627
|
return "UASTC RDO must be an object";
|
|
@@ -538,6 +677,18 @@ function selectorValues(selector) {
|
|
|
538
677
|
return success(selector.indices);
|
|
539
678
|
}
|
|
540
679
|
function encodingIssue(encoding) {
|
|
680
|
+
if (encoding.codec === "ktx2" && encoding.mode === "raw") {
|
|
681
|
+
if (encoding.colorSpace !== void 0 && encoding.colorSpace !== "linear" && encoding.colorSpace !== "srgb") {
|
|
682
|
+
return "Raw KTX2 colorSpace must be linear or srgb";
|
|
683
|
+
}
|
|
684
|
+
if (encoding.mipmaps !== void 0 && encoding.mipmaps !== false) return "Raw KTX2 mipmaps must be false";
|
|
685
|
+
if (encoding.zstd !== void 0 && typeof encoding.zstd !== "boolean") return "Raw KTX2 zstd must be a boolean";
|
|
686
|
+
if (encoding.zstdLevel !== void 0) {
|
|
687
|
+
if (!Number.isInteger(encoding.zstdLevel) || encoding.zstdLevel < 1 || encoding.zstdLevel > 19) {
|
|
688
|
+
return "Raw KTX2 zstdLevel must be an integer between 1 and 19";
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
541
692
|
if ("maxSize" in encoding && encoding.maxSize) {
|
|
542
693
|
const [width, height] = encoding.maxSize;
|
|
543
694
|
if (!Number.isInteger(width) || width < 1 || !Number.isInteger(height) || height < 1) {
|
|
@@ -602,6 +753,15 @@ function normalizeEncoding(encoding) {
|
|
|
602
753
|
case "png":
|
|
603
754
|
return { ...encoding };
|
|
604
755
|
case "ktx2":
|
|
756
|
+
if (encoding.mode === "raw") {
|
|
757
|
+
return {
|
|
758
|
+
codec: "ktx2",
|
|
759
|
+
mode: "raw",
|
|
760
|
+
colorSpace: encoding.colorSpace ?? "linear",
|
|
761
|
+
mipmaps: false,
|
|
762
|
+
...encoding.zstd === false ? { zstd: false } : { zstd: true, zstdLevel: encoding.zstdLevel ?? 19 }
|
|
763
|
+
};
|
|
764
|
+
}
|
|
605
765
|
if (encoding.mode === "uastc-hdr") {
|
|
606
766
|
return {
|
|
607
767
|
...encoding,
|
|
@@ -696,6 +856,11 @@ function createChopper(runtime) {
|
|
|
696
856
|
if (kind.data !== "image" || recipe.kind !== "image") {
|
|
697
857
|
return failure("invalid_recipe", "Recipe and input type do not match");
|
|
698
858
|
}
|
|
859
|
+
const rawEncoding = recipe.encode.codec === "ktx2" && recipe.encode.mode === "raw" ? recipe.encode : void 0;
|
|
860
|
+
if (rawEncoding) {
|
|
861
|
+
const issue = rawSourceIssue({ bytes: input.bytes, mimeType: imageMimeType(input) });
|
|
862
|
+
if (issue) return issue;
|
|
863
|
+
}
|
|
699
864
|
const inspection = await inspectImage(input, imageMimeType(input), runtime.image);
|
|
700
865
|
if (!inspection.ok) return inspection;
|
|
701
866
|
const encoded = await runtime.image.encode(
|
|
@@ -708,6 +873,12 @@ function createChopper(runtime) {
|
|
|
708
873
|
if (!hasExpectedSignature(encoded.data.bytes, encoded.data.mimeType)) {
|
|
709
874
|
return failure("validation_failed", `Encoded bytes do not match ${encoded.data.mimeType}`);
|
|
710
875
|
}
|
|
876
|
+
if (rawEncoding && (encoded.data.mimeType !== "image/ktx2" || !validateRawKtx2(encoded.data.bytes, inspection.data.width, inspection.data.height, rawEncoding))) {
|
|
877
|
+
return failure(
|
|
878
|
+
"validation_failed",
|
|
879
|
+
"Raw KTX2 output does not match the source dimensions or RGBA8 container contract"
|
|
880
|
+
);
|
|
881
|
+
}
|
|
711
882
|
const report = createReport({
|
|
712
883
|
recipe: normalized.data,
|
|
713
884
|
beforeBytes: input.bytes.byteLength,
|
|
@@ -802,6 +973,7 @@ var init_chopper = __esm({
|
|
|
802
973
|
"src/core/chopper.ts"() {
|
|
803
974
|
init_hdr_source();
|
|
804
975
|
init_manifest();
|
|
976
|
+
init_raw_ktx2();
|
|
805
977
|
init_recipes();
|
|
806
978
|
init_result();
|
|
807
979
|
init_validation();
|
|
@@ -27119,6 +27291,59 @@ var init_web = __esm({
|
|
|
27119
27291
|
}
|
|
27120
27292
|
});
|
|
27121
27293
|
|
|
27294
|
+
// src/codecs/zstd.ts
|
|
27295
|
+
async function createZstdEncoder(module) {
|
|
27296
|
+
let memory;
|
|
27297
|
+
const abort = () => {
|
|
27298
|
+
throw new Error("Zstandard WASM aborted");
|
|
27299
|
+
};
|
|
27300
|
+
const instance = await WebAssembly.instantiate(module, {
|
|
27301
|
+
a: {
|
|
27302
|
+
a: abort,
|
|
27303
|
+
// proc_exit
|
|
27304
|
+
b: () => {
|
|
27305
|
+
},
|
|
27306
|
+
// emscripten_runtime_keepalive_clear
|
|
27307
|
+
c: abort,
|
|
27308
|
+
// abort_js
|
|
27309
|
+
d: abort,
|
|
27310
|
+
// setitimer_js (unused by the synchronous compressor)
|
|
27311
|
+
e: (requestedSize) => {
|
|
27312
|
+
try {
|
|
27313
|
+
memory.grow(Math.ceil((requestedSize - memory.buffer.byteLength) / 65536));
|
|
27314
|
+
return 1;
|
|
27315
|
+
} catch {
|
|
27316
|
+
return 0;
|
|
27317
|
+
}
|
|
27318
|
+
}
|
|
27319
|
+
}
|
|
27320
|
+
});
|
|
27321
|
+
const api = instance.exports;
|
|
27322
|
+
memory = api.f;
|
|
27323
|
+
api.g();
|
|
27324
|
+
return (data, level) => {
|
|
27325
|
+
let source = 0;
|
|
27326
|
+
let destination = 0;
|
|
27327
|
+
try {
|
|
27328
|
+
const capacity = api.i(data.length);
|
|
27329
|
+
source = api.s(data.length);
|
|
27330
|
+
destination = api.s(capacity);
|
|
27331
|
+
if (!source || !destination) throw new Error("Could not allocate Zstandard buffers");
|
|
27332
|
+
new Uint8Array(memory.buffer, source, data.length).set(data);
|
|
27333
|
+
const size = api.m(destination, capacity, source, data.length, level);
|
|
27334
|
+
if (api.h(size)) throw new Error(`Zstandard compression failed: ${size}`);
|
|
27335
|
+
return new Uint8Array(memory.buffer, destination, size).slice();
|
|
27336
|
+
} finally {
|
|
27337
|
+
if (source) api.t(source);
|
|
27338
|
+
if (destination) api.t(destination);
|
|
27339
|
+
}
|
|
27340
|
+
};
|
|
27341
|
+
}
|
|
27342
|
+
var init_zstd = __esm({
|
|
27343
|
+
"src/codecs/zstd.ts"() {
|
|
27344
|
+
}
|
|
27345
|
+
});
|
|
27346
|
+
|
|
27122
27347
|
// src/codecs/image-engine.ts
|
|
27123
27348
|
function createWasmImageEngine(loader) {
|
|
27124
27349
|
const adapters = createAdapterCache(loader);
|
|
@@ -27126,12 +27351,11 @@ function createWasmImageEngine(loader) {
|
|
|
27126
27351
|
id: ENGINE_ID,
|
|
27127
27352
|
manifestHash: CODEC_MANIFEST_HASH,
|
|
27128
27353
|
async inspect(source) {
|
|
27129
|
-
if (isHdrSourceMimeType(source.mimeType)) {
|
|
27130
|
-
const header = readHdrSource(source.bytes);
|
|
27131
|
-
if ("error" in header) return failure("decode_failed", header.error);
|
|
27132
|
-
return success({ width: header.width, height: header.height });
|
|
27133
|
-
}
|
|
27134
27354
|
try {
|
|
27355
|
+
if (isHdrSourceMimeType(source.mimeType)) {
|
|
27356
|
+
const header = readSupportedHdrSource(source.bytes);
|
|
27357
|
+
return success({ width: header.width, height: header.height });
|
|
27358
|
+
}
|
|
27135
27359
|
const image = await decodeSource(source, adapters);
|
|
27136
27360
|
return success({ width: image.width, height: image.height });
|
|
27137
27361
|
} catch (cause) {
|
|
@@ -27141,6 +27365,10 @@ function createWasmImageEngine(loader) {
|
|
|
27141
27365
|
async encode(source, encoding, signal) {
|
|
27142
27366
|
const valid = validateRecipe({ kind: "image", encode: encoding });
|
|
27143
27367
|
if (!valid.ok) return valid;
|
|
27368
|
+
if (encoding.codec === "ktx2" && encoding.mode === "raw") {
|
|
27369
|
+
const issue = rawSourceIssue(source);
|
|
27370
|
+
if (issue) return issue;
|
|
27371
|
+
}
|
|
27144
27372
|
const mismatch = sourcePairingIssue(source, encoding);
|
|
27145
27373
|
if (mismatch) return mismatch;
|
|
27146
27374
|
try {
|
|
@@ -27150,8 +27378,30 @@ function createWasmImageEngine(loader) {
|
|
|
27150
27378
|
assertNotAborted2(signal);
|
|
27151
27379
|
return success(encoded2);
|
|
27152
27380
|
}
|
|
27153
|
-
let image
|
|
27381
|
+
let image;
|
|
27382
|
+
try {
|
|
27383
|
+
image = await decodeSource(source, adapters);
|
|
27384
|
+
} catch (cause) {
|
|
27385
|
+
if (encoding.codec === "ktx2" && encoding.mode === "raw") {
|
|
27386
|
+
return operationFailure("decode_failed", "Could not decode image", cause);
|
|
27387
|
+
}
|
|
27388
|
+
throw cause;
|
|
27389
|
+
}
|
|
27154
27390
|
assertNotAborted2(signal);
|
|
27391
|
+
if (encoding.codec === "ktx2" && encoding.mode === "raw") {
|
|
27392
|
+
const rgba = new Uint8Array(image.data.buffer, image.data.byteOffset, image.data.byteLength);
|
|
27393
|
+
if (rgba.length !== image.width * image.height * 4) throw new Error("Decoder did not return RGBA8 pixels");
|
|
27394
|
+
const compress = encoding.zstd === false ? void 0 : await adapters.zstd();
|
|
27395
|
+
assertNotAborted2(signal);
|
|
27396
|
+
const payload = compress ? compress(rgba, encoding.zstdLevel ?? 19) : rgba;
|
|
27397
|
+
assertNotAborted2(signal);
|
|
27398
|
+
return success({
|
|
27399
|
+
bytes: writeRawKtx2(image.width, image.height, payload, encoding),
|
|
27400
|
+
mimeType: outputMimeType(encoding),
|
|
27401
|
+
width: image.width,
|
|
27402
|
+
height: image.height
|
|
27403
|
+
});
|
|
27404
|
+
}
|
|
27155
27405
|
if (encoding.maxSize) {
|
|
27156
27406
|
image = await resizeWithin(image, encoding.maxSize, adapters);
|
|
27157
27407
|
}
|
|
@@ -27179,6 +27429,7 @@ function createAdapterCache(loader) {
|
|
|
27179
27429
|
let png;
|
|
27180
27430
|
let resizeModule;
|
|
27181
27431
|
let webp;
|
|
27432
|
+
let zstd;
|
|
27182
27433
|
const load2 = async (name) => {
|
|
27183
27434
|
try {
|
|
27184
27435
|
return await loader.load(name);
|
|
@@ -27187,6 +27438,9 @@ function createAdapterCache(loader) {
|
|
|
27187
27438
|
}
|
|
27188
27439
|
};
|
|
27189
27440
|
return {
|
|
27441
|
+
zstd: () => zstd ??= load2("zstdEncoder").then(createZstdEncoder).catch((cause) => {
|
|
27442
|
+
throw new CodecLoadError("Could not initialize zstdEncoder", { cause });
|
|
27443
|
+
}),
|
|
27190
27444
|
avif: () => avif ??= load2("avifEncoder").then(
|
|
27191
27445
|
(module) => initEmscriptenModule(avif_enc_default, module)
|
|
27192
27446
|
),
|
|
@@ -27320,16 +27574,20 @@ function sourcePairingIssue(source, encoding) {
|
|
|
27320
27574
|
}
|
|
27321
27575
|
return void 0;
|
|
27322
27576
|
}
|
|
27323
|
-
|
|
27324
|
-
const header = readHdrSource(
|
|
27577
|
+
function readSupportedHdrSource(bytes) {
|
|
27578
|
+
const header = readHdrSource(bytes);
|
|
27325
27579
|
if ("error" in header) throw new SourceError("decode_failed", header.error);
|
|
27326
27580
|
const texels = header.width * header.height;
|
|
27327
27581
|
if (texels > MAX_HDR_SOURCE_TEXELS) {
|
|
27328
27582
|
throw new SourceError(
|
|
27329
27583
|
"unsupported_asset",
|
|
27330
|
-
`Source is ${header.width}x${header.height} (${megapixels(texels)} Mpix), over the ${megapixels(MAX_HDR_SOURCE_TEXELS)} Mpix HDR
|
|
27584
|
+
`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.`
|
|
27331
27585
|
);
|
|
27332
27586
|
}
|
|
27587
|
+
return header;
|
|
27588
|
+
}
|
|
27589
|
+
async function encodeKtx2Hdr(source, encoding, wasmUrl) {
|
|
27590
|
+
const header = readSupportedHdrSource(source.bytes);
|
|
27333
27591
|
const bytes = await encodeToKTX2(source.bytes, {
|
|
27334
27592
|
isHDR: true,
|
|
27335
27593
|
imageType: header.container,
|
|
@@ -27407,6 +27665,8 @@ var init_image_engine = __esm({
|
|
|
27407
27665
|
init_validation();
|
|
27408
27666
|
init_hdr_source();
|
|
27409
27667
|
init_manifest();
|
|
27668
|
+
init_raw_ktx2();
|
|
27669
|
+
init_zstd();
|
|
27410
27670
|
CodecLoadError = class extends Error {
|
|
27411
27671
|
};
|
|
27412
27672
|
SourceError = class extends Error {
|