@joycostudio/chopper 0.3.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 +3 -5
- package/codecs/manifest.json +1 -2
- package/dist/browser/{chunk-CC6UVN2A.js → chunk-6WQPPVM2.js} +3 -4
- package/dist/browser/chunk-6WQPPVM2.js.map +1 -0
- package/dist/browser/{in-process-ELNZ7M7W.js → in-process-P6TQNY2L.js} +214 -15
- package/dist/browser/in-process-P6TQNY2L.js.map +1 -0
- package/dist/browser/index.d.ts +20 -8
- package/dist/browser/index.js +2 -2
- package/dist/browser/worker.js +222 -18
- package/dist/browser/worker.js.map +1 -1
- package/dist/cli/index.js +424 -72
- package/dist/cli/index.js.map +1 -1
- package/dist/config/index.d.ts +28 -12
- package/dist/config/index.js.map +1 -1
- package/dist/index.d.ts +36 -11
- package/dist/index.js +30 -13
- package/dist/index.js.map +1 -1
- package/dist/node/index.d.ts +34 -18
- package/dist/node/index.js +237 -32
- package/dist/node/index.js.map +1 -1
- package/package.json +3 -2
- package/dist/browser/chunk-CC6UVN2A.js.map +0 -1
- package/dist/browser/in-process-ELNZ7M7W.js.map +0 -1
package/dist/browser/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
declare const RECIPE_SCHEMA_VERSION: 1;
|
|
2
1
|
type TextureSize = readonly [width: number, height: number];
|
|
3
2
|
interface SizedEncoding {
|
|
4
3
|
/** Maximum dimensions. Aspect ratio is preserved and images are not enlarged. */
|
|
@@ -48,8 +47,25 @@ interface Etc1sEncoding extends SizedEncoding {
|
|
|
48
47
|
compression?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
49
48
|
mipmaps?: boolean;
|
|
50
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* UASTC HDR 4x4. Encodes half-float EXR source into blocks the runtime
|
|
52
|
+
* transcodes to BC6H on desktop, or uploads as ASTC 4x4 SFLOAT where the HDR
|
|
53
|
+
* profile exists. Fixed 8 bits per texel on the GPU regardless of source.
|
|
54
|
+
*
|
|
55
|
+
* Deliberately not a `SizedEncoding`: resizing runs on decoded 8-bit RGBA, and
|
|
56
|
+
* there is no half-float resampler in the pipeline, so HDR sources must already
|
|
57
|
+
* be authored at their target resolution.
|
|
58
|
+
*/
|
|
59
|
+
interface UastcHdrEncoding {
|
|
60
|
+
codec: 'ktx2';
|
|
61
|
+
mode: 'uastc-hdr';
|
|
62
|
+
/** Encoder effort. Higher is slower; rarely helps on low-frequency lightmaps. */
|
|
63
|
+
level?: 0 | 1 | 2 | 3 | 4;
|
|
64
|
+
mipmaps?: boolean;
|
|
65
|
+
zstd?: boolean;
|
|
66
|
+
}
|
|
51
67
|
type RasterEncoding = WebpEncoding | AvifEncoding | JpegEncoding | PngEncoding;
|
|
52
|
-
type ImageEncoding = RasterEncoding | UastcEncoding | Etc1sEncoding;
|
|
68
|
+
type ImageEncoding = RasterEncoding | UastcEncoding | Etc1sEncoding | UastcHdrEncoding;
|
|
53
69
|
type GlbProperty = 'accessor' | 'animation' | 'buffer' | 'camera' | 'material' | 'mesh' | 'node' | 'primitive' | 'primitiveTarget' | 'skin' | 'texture';
|
|
54
70
|
type GlbMethodRecipe = {
|
|
55
71
|
method: 'dedup';
|
|
@@ -88,25 +104,21 @@ type GeometryEncoding = {
|
|
|
88
104
|
codec: 'meshopt';
|
|
89
105
|
};
|
|
90
106
|
interface GlbAssetRecipe {
|
|
91
|
-
schemaVersion: typeof RECIPE_SCHEMA_VERSION;
|
|
92
107
|
kind: 'glb';
|
|
93
108
|
methods?: readonly GlbMethodRecipe[];
|
|
94
109
|
textures?: readonly EmbeddedTextureRecipe[];
|
|
95
110
|
geometry?: GeometryEncoding | false;
|
|
96
111
|
}
|
|
97
112
|
interface ImageAssetRecipe {
|
|
98
|
-
schemaVersion: typeof RECIPE_SCHEMA_VERSION;
|
|
99
113
|
kind: 'image';
|
|
100
114
|
encode: ImageEncoding;
|
|
101
115
|
}
|
|
102
116
|
type AssetRecipe = GlbAssetRecipe | ImageAssetRecipe;
|
|
103
|
-
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'>)
|
|
117
|
+
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>;
|
|
104
118
|
type NormalizedAssetRecipe = {
|
|
105
|
-
schemaVersion: typeof RECIPE_SCHEMA_VERSION;
|
|
106
119
|
kind: 'image';
|
|
107
120
|
encode: NormalizedImageEncoding;
|
|
108
121
|
} | {
|
|
109
|
-
schemaVersion: typeof RECIPE_SCHEMA_VERSION;
|
|
110
122
|
kind: 'glb';
|
|
111
123
|
methods: readonly GlbMethodRecipe[];
|
|
112
124
|
textures: readonly (Omit<EmbeddedTextureRecipe, 'encode'> & {
|
|
@@ -137,7 +149,7 @@ interface AssetInput {
|
|
|
137
149
|
}
|
|
138
150
|
interface ImageInspection {
|
|
139
151
|
kind: 'image';
|
|
140
|
-
mimeType: 'image/png' | 'image/jpeg';
|
|
152
|
+
mimeType: 'image/png' | 'image/jpeg' | 'image/x-exr' | 'image/vnd.radiance';
|
|
141
153
|
width: number;
|
|
142
154
|
height: number;
|
|
143
155
|
byteLength: number;
|
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-6WQPPVM2.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-P6TQNY2L.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.5.0",
|
|
53
53
|
description: "Isomorphic asset optimization pipeline",
|
|
54
54
|
type: "module",
|
|
55
55
|
license: "ISC",
|
|
@@ -85,7 +85,8 @@ var init_package = __esm({
|
|
|
85
85
|
},
|
|
86
86
|
"./node": {
|
|
87
87
|
types: "./dist/node/index.d.ts",
|
|
88
|
-
import: "./dist/node/index.js"
|
|
88
|
+
import: "./dist/node/index.js",
|
|
89
|
+
default: "./dist/node/index.js"
|
|
89
90
|
},
|
|
90
91
|
"./config": {
|
|
91
92
|
types: "./dist/config/index.d.ts",
|
|
@@ -207,7 +208,6 @@ var init_manifest = __esm({
|
|
|
207
208
|
}
|
|
208
209
|
};
|
|
209
210
|
CODEC_MANIFEST = {
|
|
210
|
-
schemaVersion: 1,
|
|
211
211
|
chopperVersion: package_default.version,
|
|
212
212
|
manifestHash: CODEC_MANIFEST_HASH,
|
|
213
213
|
gltfTransformVersion: "4.4.2",
|
|
@@ -218,6 +218,135 @@ var init_manifest = __esm({
|
|
|
218
218
|
}
|
|
219
219
|
});
|
|
220
220
|
|
|
221
|
+
// src/codecs/hdr-source.ts
|
|
222
|
+
function isHdrSourceMimeType(mimeType) {
|
|
223
|
+
return mimeType === "image/x-exr" || mimeType === "image/vnd.radiance";
|
|
224
|
+
}
|
|
225
|
+
function detectHdrContainer(bytes) {
|
|
226
|
+
if (bytes.length >= 8 && EXR_MAGIC.every((byte, index) => bytes[index] === byte)) return "exr";
|
|
227
|
+
if (bytes.length >= 8 && RADIANCE_MAGIC.every((byte, index) => bytes[index] === byte)) return "hdr";
|
|
228
|
+
return void 0;
|
|
229
|
+
}
|
|
230
|
+
function readHdrSource(bytes) {
|
|
231
|
+
const container = detectHdrContainer(bytes);
|
|
232
|
+
if (!container) return { error: "Not an OpenEXR or Radiance HDR file" };
|
|
233
|
+
return container === "exr" ? readExr(bytes) : readRadiance(bytes);
|
|
234
|
+
}
|
|
235
|
+
function readExr(bytes) {
|
|
236
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
237
|
+
const version = view.getInt32(4, true);
|
|
238
|
+
if ((version & VERSION_NUMBER_FIELD) !== 2) {
|
|
239
|
+
return { error: `Unsupported EXR version: ${version & VERSION_NUMBER_FIELD}` };
|
|
240
|
+
}
|
|
241
|
+
if (version & MULTI_PART_FILE_FLAG) return { error: "Multi-part EXR files are not supported" };
|
|
242
|
+
if (version & NON_IMAGE_FLAG) return { error: "Deep EXR files are not supported" };
|
|
243
|
+
let size;
|
|
244
|
+
let compressionIssue;
|
|
245
|
+
let offset = 8;
|
|
246
|
+
while (offset < bytes.length) {
|
|
247
|
+
const name = readNullTerminated(bytes, offset);
|
|
248
|
+
if (name === void 0) return { error: "Malformed EXR header" };
|
|
249
|
+
if (name.value === "") break;
|
|
250
|
+
offset = name.next;
|
|
251
|
+
const type = readNullTerminated(bytes, offset);
|
|
252
|
+
if (type === void 0) return { error: "Malformed EXR header" };
|
|
253
|
+
offset = type.next;
|
|
254
|
+
if (offset + 4 > bytes.length) return { error: "Malformed EXR header" };
|
|
255
|
+
const length3 = view.getInt32(offset, true);
|
|
256
|
+
offset += 4;
|
|
257
|
+
if (length3 < 0 || offset + length3 > bytes.length) return { error: "Malformed EXR header" };
|
|
258
|
+
if (name.value === "dataWindow") {
|
|
259
|
+
if (type.value !== "box2i" || length3 !== 16) return { error: "Malformed EXR dataWindow" };
|
|
260
|
+
const xMin = view.getInt32(offset, true);
|
|
261
|
+
const yMin = view.getInt32(offset + 4, true);
|
|
262
|
+
const xMax = view.getInt32(offset + 8, true);
|
|
263
|
+
const yMax = view.getInt32(offset + 12, true);
|
|
264
|
+
const width = xMax - xMin + 1;
|
|
265
|
+
const height = yMax - yMin + 1;
|
|
266
|
+
if (width < 1 || height < 1) return { error: "EXR dataWindow is empty" };
|
|
267
|
+
size = { container: "exr", width, height };
|
|
268
|
+
}
|
|
269
|
+
if (name.value === "compression") {
|
|
270
|
+
if (length3 !== 1) return { error: "Malformed EXR compression attribute" };
|
|
271
|
+
compressionIssue = exrCompressionIssue(bytes[offset]);
|
|
272
|
+
}
|
|
273
|
+
offset += length3;
|
|
274
|
+
}
|
|
275
|
+
if (compressionIssue) return { error: compressionIssue };
|
|
276
|
+
return size ?? { error: "EXR header has no dataWindow attribute" };
|
|
277
|
+
}
|
|
278
|
+
function exrCompressionIssue(code) {
|
|
279
|
+
const codec = EXR_COMPRESSION[code];
|
|
280
|
+
if (!codec) return `Unsupported EXR compression: unknown codec ${code}`;
|
|
281
|
+
if (codec.supported) return void 0;
|
|
282
|
+
const readable = EXR_COMPRESSION.filter((entry) => entry.supported).map((entry) => entry.name).join(", ");
|
|
283
|
+
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.`;
|
|
284
|
+
}
|
|
285
|
+
function readRadiance(bytes) {
|
|
286
|
+
const text = latin1(bytes.subarray(0, RADIANCE_HEADER_LIMIT));
|
|
287
|
+
const lines = text.split("\n");
|
|
288
|
+
let index = 0;
|
|
289
|
+
while (index < lines.length && lines[index] !== "" && lines[index] !== "\r") index++;
|
|
290
|
+
index++;
|
|
291
|
+
while (index < lines.length && (lines[index] === "" || lines[index] === "\r")) index++;
|
|
292
|
+
if (index >= lines.length) return { error: "Radiance file has no resolution line" };
|
|
293
|
+
const resolution = /^([+-][XY])\s+(\d+)\s+([+-][XY])\s+(\d+)\s*$/.exec(lines[index].replace(/\r$/, ""));
|
|
294
|
+
if (!resolution) return { error: `Malformed Radiance resolution line: ${lines[index].slice(0, 40)}` };
|
|
295
|
+
const extents = /* @__PURE__ */ new Map([
|
|
296
|
+
[resolution[1][1], Number(resolution[2])],
|
|
297
|
+
[resolution[3][1], Number(resolution[4])]
|
|
298
|
+
]);
|
|
299
|
+
const width = extents.get("X");
|
|
300
|
+
const height = extents.get("Y");
|
|
301
|
+
if (width === void 0 || height === void 0) {
|
|
302
|
+
return { error: "Radiance resolution line must pair an X and a Y axis" };
|
|
303
|
+
}
|
|
304
|
+
if (width < 1 || height < 1) return { error: "Radiance image is empty" };
|
|
305
|
+
return { container: "hdr", width, height };
|
|
306
|
+
}
|
|
307
|
+
function readNullTerminated(bytes, start) {
|
|
308
|
+
const limit = Math.min(bytes.length, start + 256);
|
|
309
|
+
for (let index = start; index < limit; index++) {
|
|
310
|
+
if (bytes[index] === 0) {
|
|
311
|
+
return { value: latin1(bytes.subarray(start, index)), next: index + 1 };
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return void 0;
|
|
315
|
+
}
|
|
316
|
+
function latin1(bytes) {
|
|
317
|
+
let value = "";
|
|
318
|
+
for (const byte of bytes) value += String.fromCharCode(byte);
|
|
319
|
+
return value;
|
|
320
|
+
}
|
|
321
|
+
var HDR_SOURCE_MIME_TYPES, EXR_MAGIC, VERSION_NUMBER_FIELD, NON_IMAGE_FLAG, MULTI_PART_FILE_FLAG, EXR_COMPRESSION, RADIANCE_MAGIC, RADIANCE_HEADER_LIMIT, MAX_HDR_SOURCE_TEXELS;
|
|
322
|
+
var init_hdr_source = __esm({
|
|
323
|
+
"src/codecs/hdr-source.ts"() {
|
|
324
|
+
HDR_SOURCE_MIME_TYPES = {
|
|
325
|
+
exr: "image/x-exr",
|
|
326
|
+
hdr: "image/vnd.radiance"
|
|
327
|
+
};
|
|
328
|
+
EXR_MAGIC = [118, 47, 49, 1];
|
|
329
|
+
VERSION_NUMBER_FIELD = 255;
|
|
330
|
+
NON_IMAGE_FLAG = 2048;
|
|
331
|
+
MULTI_PART_FILE_FLAG = 4096;
|
|
332
|
+
EXR_COMPRESSION = [
|
|
333
|
+
{ name: "NO_COMPRESSION", supported: true },
|
|
334
|
+
{ name: "RLE", supported: true },
|
|
335
|
+
{ name: "ZIPS", supported: true },
|
|
336
|
+
{ name: "ZIP", supported: true },
|
|
337
|
+
{ name: "PIZ", supported: true },
|
|
338
|
+
{ name: "PXR24", supported: false },
|
|
339
|
+
{ name: "B44", supported: false },
|
|
340
|
+
{ name: "B44A", supported: false },
|
|
341
|
+
{ name: "DWAA", supported: false },
|
|
342
|
+
{ name: "DWAB", supported: false }
|
|
343
|
+
];
|
|
344
|
+
RADIANCE_MAGIC = [35, 63];
|
|
345
|
+
RADIANCE_HEADER_LIMIT = 8192;
|
|
346
|
+
MAX_HDR_SOURCE_TEXELS = 12 * 1024 * 1024;
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
|
|
221
350
|
// src/core/recipes.ts
|
|
222
351
|
function outputExtension(encoding) {
|
|
223
352
|
if (encoding.codec === "ktx2") return ".ktx2";
|
|
@@ -228,10 +357,8 @@ function outputMimeType(encoding) {
|
|
|
228
357
|
if (encoding.codec === "ktx2") return "image/ktx2";
|
|
229
358
|
return `image/${encoding.codec}`;
|
|
230
359
|
}
|
|
231
|
-
var RECIPE_SCHEMA_VERSION;
|
|
232
360
|
var init_recipes = __esm({
|
|
233
361
|
"src/core/recipes.ts"() {
|
|
234
|
-
RECIPE_SCHEMA_VERSION = 1;
|
|
235
362
|
}
|
|
236
363
|
});
|
|
237
364
|
|
|
@@ -253,13 +380,11 @@ function normalizeRecipe(value) {
|
|
|
253
380
|
const recipe = validated.data;
|
|
254
381
|
if (recipe.kind === "image") {
|
|
255
382
|
return success({
|
|
256
|
-
schemaVersion: RECIPE_SCHEMA_VERSION,
|
|
257
383
|
kind: "image",
|
|
258
384
|
encode: normalizeEncoding(recipe.encode)
|
|
259
385
|
});
|
|
260
386
|
}
|
|
261
387
|
return success({
|
|
262
|
-
schemaVersion: RECIPE_SCHEMA_VERSION,
|
|
263
388
|
kind: "glb",
|
|
264
389
|
methods: recipe.methods ?? [],
|
|
265
390
|
textures: (recipe.textures ?? []).map((texture) => ({
|
|
@@ -313,15 +438,15 @@ function encodingShapeIssue(encoding) {
|
|
|
313
438
|
if (!["webp", "avif", "jpeg", "png", "ktx2"].includes(String(candidate.codec))) {
|
|
314
439
|
return `Unsupported image codec: ${String(candidate.codec)}`;
|
|
315
440
|
}
|
|
316
|
-
if (candidate.codec === "ktx2" && candidate.mode !== "uastc" && candidate.mode !== "etc1s") {
|
|
441
|
+
if (candidate.codec === "ktx2" && candidate.mode !== "uastc" && candidate.mode !== "etc1s" && candidate.mode !== "uastc-hdr") {
|
|
317
442
|
return `Unsupported KTX2 mode: ${String(candidate.mode)}`;
|
|
318
443
|
}
|
|
444
|
+
if (candidate.codec === "ktx2" && candidate.mode === "uastc-hdr" && candidate.maxSize !== void 0) {
|
|
445
|
+
return "UASTC HDR does not support maxSize; resizing requires a decoded 8-bit image. Author the source at its target resolution.";
|
|
446
|
+
}
|
|
319
447
|
return void 0;
|
|
320
448
|
}
|
|
321
449
|
function recipeIssue(recipe) {
|
|
322
|
-
if (recipe.schemaVersion !== RECIPE_SCHEMA_VERSION) {
|
|
323
|
-
return `Unsupported recipe schema version: ${String(recipe.schemaVersion)}`;
|
|
324
|
-
}
|
|
325
450
|
if (recipe.kind === "image") return encodingIssue(recipe.encode);
|
|
326
451
|
const labels = /* @__PURE__ */ new Set();
|
|
327
452
|
const selectors = /* @__PURE__ */ new Set();
|
|
@@ -343,6 +468,9 @@ function recipeIssue(recipe) {
|
|
|
343
468
|
}
|
|
344
469
|
selectors.add(key);
|
|
345
470
|
}
|
|
471
|
+
if (texture.encode.codec === "ktx2" && texture.encode.mode === "uastc-hdr") {
|
|
472
|
+
return `Texture recipe "${texture.label}" uses UASTC HDR, which KHR_texture_basisu does not cover; HDR is only supported for standalone image recipes`;
|
|
473
|
+
}
|
|
346
474
|
const issue = encodingIssue(texture.encode);
|
|
347
475
|
if (issue) return issue;
|
|
348
476
|
}
|
|
@@ -364,7 +492,7 @@ function selectorValues(selector) {
|
|
|
364
492
|
return success(selector.indices);
|
|
365
493
|
}
|
|
366
494
|
function encodingIssue(encoding) {
|
|
367
|
-
if (encoding.maxSize) {
|
|
495
|
+
if ("maxSize" in encoding && encoding.maxSize) {
|
|
368
496
|
const [width, height] = encoding.maxSize;
|
|
369
497
|
if (!Number.isInteger(width) || width < 1 || !Number.isInteger(height) || height < 1) {
|
|
370
498
|
return "Texture maxSize must contain positive integers";
|
|
@@ -393,6 +521,9 @@ function encodingIssue(encoding) {
|
|
|
393
521
|
return "UASTC RDO lambda must be between 0.001 and 10";
|
|
394
522
|
}
|
|
395
523
|
}
|
|
524
|
+
if (encoding.codec === "ktx2" && encoding.mode === "uastc-hdr" && encoding.level !== void 0 && (!Number.isInteger(encoding.level) || encoding.level < 0 || encoding.level > 4)) {
|
|
525
|
+
return "UASTC HDR level must be an integer between 0 and 4";
|
|
526
|
+
}
|
|
396
527
|
if (encoding.codec === "ktx2" && encoding.mode === "etc1s" && encoding.compression !== void 0 && (!Number.isInteger(encoding.compression) || encoding.compression < 0 || encoding.compression > 6)) {
|
|
397
528
|
return "ETC1S compression must be an integer between 0 and 6";
|
|
398
529
|
}
|
|
@@ -425,6 +556,14 @@ function normalizeEncoding(encoding) {
|
|
|
425
556
|
case "png":
|
|
426
557
|
return { ...encoding };
|
|
427
558
|
case "ktx2":
|
|
559
|
+
if (encoding.mode === "uastc-hdr") {
|
|
560
|
+
return {
|
|
561
|
+
...encoding,
|
|
562
|
+
level: encoding.level ?? 1,
|
|
563
|
+
mipmaps: encoding.mipmaps ?? true,
|
|
564
|
+
zstd: encoding.zstd ?? true
|
|
565
|
+
};
|
|
566
|
+
}
|
|
428
567
|
if (encoding.mode === "uastc") {
|
|
429
568
|
return {
|
|
430
569
|
...encoding,
|
|
@@ -448,7 +587,6 @@ function normalizeEncoding(encoding) {
|
|
|
448
587
|
var init_validation = __esm({
|
|
449
588
|
"src/core/validation.ts"() {
|
|
450
589
|
init_result();
|
|
451
|
-
init_recipes();
|
|
452
590
|
}
|
|
453
591
|
});
|
|
454
592
|
|
|
@@ -551,8 +689,11 @@ function detectAsset(input) {
|
|
|
551
689
|
if (input.bytes.length >= 4 && input.bytes[0] === 103 && input.bytes[1] === 108 && input.bytes[2] === 84 && input.bytes[3] === 70) {
|
|
552
690
|
return success("glb");
|
|
553
691
|
}
|
|
554
|
-
if (isPng(input.bytes) || isJpeg(input.bytes)) return success("image");
|
|
555
|
-
return failure(
|
|
692
|
+
if (isPng(input.bytes) || isJpeg(input.bytes) || detectHdrContainer(input.bytes)) return success("image");
|
|
693
|
+
return failure(
|
|
694
|
+
"unsupported_asset",
|
|
695
|
+
`Unsupported asset: ${input.fileName}; expected GLB, PNG, JPEG, EXR, or Radiance HDR`
|
|
696
|
+
);
|
|
556
697
|
}
|
|
557
698
|
async function inspectImage(input, mimeType, engine) {
|
|
558
699
|
const dimensions = await engine.inspect({ bytes: input.bytes, mimeType });
|
|
@@ -566,6 +707,8 @@ async function inspectImage(input, mimeType, engine) {
|
|
|
566
707
|
});
|
|
567
708
|
}
|
|
568
709
|
function imageMimeType(input) {
|
|
710
|
+
const hdr = detectHdrContainer(input.bytes);
|
|
711
|
+
if (hdr) return HDR_SOURCE_MIME_TYPES[hdr];
|
|
569
712
|
return isPng(input.bytes) ? "image/png" : "image/jpeg";
|
|
570
713
|
}
|
|
571
714
|
function isPng(bytes) {
|
|
@@ -610,6 +753,7 @@ function createReport(values) {
|
|
|
610
753
|
}
|
|
611
754
|
var init_chopper = __esm({
|
|
612
755
|
"src/core/chopper.ts"() {
|
|
756
|
+
init_hdr_source();
|
|
613
757
|
init_manifest();
|
|
614
758
|
init_recipes();
|
|
615
759
|
init_result();
|
|
@@ -26935,6 +27079,11 @@ function createWasmImageEngine(loader) {
|
|
|
26935
27079
|
id: ENGINE_ID,
|
|
26936
27080
|
manifestHash: CODEC_MANIFEST_HASH,
|
|
26937
27081
|
async inspect(source) {
|
|
27082
|
+
if (isHdrSourceMimeType(source.mimeType)) {
|
|
27083
|
+
const header = readHdrSource(source.bytes);
|
|
27084
|
+
if ("error" in header) return failure("decode_failed", header.error);
|
|
27085
|
+
return success({ width: header.width, height: header.height });
|
|
27086
|
+
}
|
|
26938
27087
|
try {
|
|
26939
27088
|
const image = await decodeSource(source, adapters);
|
|
26940
27089
|
return success({ width: image.width, height: image.height });
|
|
@@ -26943,10 +27092,17 @@ function createWasmImageEngine(loader) {
|
|
|
26943
27092
|
}
|
|
26944
27093
|
},
|
|
26945
27094
|
async encode(source, encoding, signal) {
|
|
26946
|
-
const valid = validateRecipe({
|
|
27095
|
+
const valid = validateRecipe({ kind: "image", encode: encoding });
|
|
26947
27096
|
if (!valid.ok) return valid;
|
|
27097
|
+
const mismatch = sourcePairingIssue(source, encoding);
|
|
27098
|
+
if (mismatch) return mismatch;
|
|
26948
27099
|
try {
|
|
26949
27100
|
assertNotAborted2(signal);
|
|
27101
|
+
if (isHdrEncoding(encoding)) {
|
|
27102
|
+
const encoded2 = await encodeKtx2Hdr(source, encoding, await loader.basisEncoderUrl());
|
|
27103
|
+
assertNotAborted2(signal);
|
|
27104
|
+
return success(encoded2);
|
|
27105
|
+
}
|
|
26950
27106
|
let image = await decodeSource(source, adapters);
|
|
26951
27107
|
assertNotAborted2(signal);
|
|
26952
27108
|
if (encoding.maxSize) {
|
|
@@ -27101,6 +27257,44 @@ async function encodeRaster(image, encoding, adapters) {
|
|
|
27101
27257
|
}
|
|
27102
27258
|
return { bytes: toUint8Array(bytes), mimeType: outputMimeType(encoding) };
|
|
27103
27259
|
}
|
|
27260
|
+
function isHdrEncoding(encoding) {
|
|
27261
|
+
return encoding.codec === "ktx2" && encoding.mode === "uastc-hdr";
|
|
27262
|
+
}
|
|
27263
|
+
function sourcePairingIssue(source, encoding) {
|
|
27264
|
+
const isHdrSource = isHdrSourceMimeType(source.mimeType);
|
|
27265
|
+
if (isHdrEncoding(encoding) && !isHdrSource) {
|
|
27266
|
+
return failure("invalid_recipe", `UASTC HDR requires an EXR or Radiance HDR source; received ${source.mimeType}`);
|
|
27267
|
+
}
|
|
27268
|
+
if (isHdrSource && !isHdrEncoding(encoding)) {
|
|
27269
|
+
return failure(
|
|
27270
|
+
"invalid_recipe",
|
|
27271
|
+
"HDR sources can only be encoded as KTX2 with mode 'uastc-hdr'; chopper does not decode HDR pixels"
|
|
27272
|
+
);
|
|
27273
|
+
}
|
|
27274
|
+
return void 0;
|
|
27275
|
+
}
|
|
27276
|
+
async function encodeKtx2Hdr(source, encoding, wasmUrl) {
|
|
27277
|
+
const header = readHdrSource(source.bytes);
|
|
27278
|
+
if ("error" in header) throw new SourceError("decode_failed", header.error);
|
|
27279
|
+
const texels = header.width * header.height;
|
|
27280
|
+
if (texels > MAX_HDR_SOURCE_TEXELS) {
|
|
27281
|
+
throw new SourceError(
|
|
27282
|
+
"unsupported_asset",
|
|
27283
|
+
`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.`
|
|
27284
|
+
);
|
|
27285
|
+
}
|
|
27286
|
+
const bytes = await encodeToKTX2(source.bytes, {
|
|
27287
|
+
isHDR: true,
|
|
27288
|
+
imageType: header.container,
|
|
27289
|
+
isUASTC: true,
|
|
27290
|
+
hdrQualityLevel: encoding.level ?? 1,
|
|
27291
|
+
needSupercompression: encoding.zstd ?? true,
|
|
27292
|
+
generateMipmap: encoding.mipmaps ?? true,
|
|
27293
|
+
isKTX2File: true,
|
|
27294
|
+
wasmUrl
|
|
27295
|
+
});
|
|
27296
|
+
return { bytes, mimeType: outputMimeType(encoding), width: header.width, height: header.height };
|
|
27297
|
+
}
|
|
27104
27298
|
async function encodeKtx2(source, image, encoding, wasmUrl) {
|
|
27105
27299
|
const srgb = encoding.colorSpace !== "linear";
|
|
27106
27300
|
const bytes = await encodeToKTX2(source.bytes, {
|
|
@@ -27126,7 +27320,9 @@ async function encodeKtx2(source, image, encoding, wasmUrl) {
|
|
|
27126
27320
|
return { bytes, mimeType: outputMimeType(encoding) };
|
|
27127
27321
|
}
|
|
27128
27322
|
function operationFailure(code, message, cause) {
|
|
27129
|
-
|
|
27323
|
+
if (cause instanceof CodecLoadError) return failure("codec_load_failed", cause.message, cause.cause);
|
|
27324
|
+
if (cause instanceof SourceError) return failure(cause.code, cause.message);
|
|
27325
|
+
return failure(code, message, cause);
|
|
27130
27326
|
}
|
|
27131
27327
|
function assertNotAborted2(signal) {
|
|
27132
27328
|
if (signal?.aborted) throw new DOMException("Optimization was aborted", "AbortError");
|
|
@@ -27144,7 +27340,7 @@ function toArrayBuffer(bytes) {
|
|
|
27144
27340
|
function remapEffort(effort, max2) {
|
|
27145
27341
|
return Math.round(effort / 100 * max2);
|
|
27146
27342
|
}
|
|
27147
|
-
var CodecLoadError;
|
|
27343
|
+
var CodecLoadError, SourceError, megapixels;
|
|
27148
27344
|
var init_image_engine = __esm({
|
|
27149
27345
|
"src/codecs/image-engine.ts"() {
|
|
27150
27346
|
init_avif_enc();
|
|
@@ -27162,9 +27358,17 @@ var init_image_engine = __esm({
|
|
|
27162
27358
|
init_recipes();
|
|
27163
27359
|
init_result();
|
|
27164
27360
|
init_validation();
|
|
27361
|
+
init_hdr_source();
|
|
27165
27362
|
init_manifest();
|
|
27166
27363
|
CodecLoadError = class extends Error {
|
|
27167
27364
|
};
|
|
27365
|
+
SourceError = class extends Error {
|
|
27366
|
+
constructor(code, message) {
|
|
27367
|
+
super(message);
|
|
27368
|
+
this.code = code;
|
|
27369
|
+
}
|
|
27370
|
+
};
|
|
27371
|
+
megapixels = (texels) => (texels / 1e6).toFixed(1);
|
|
27168
27372
|
}
|
|
27169
27373
|
});
|
|
27170
27374
|
|