@joycostudio/chopper 0.4.0 → 0.5.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 +3 -5
- package/codecs/manifest.json +1 -2
- package/dist/browser/{chunk-M7J64NCO.js → chunk-OLPLOAWX.js} +3 -4
- package/dist/browser/chunk-OLPLOAWX.js.map +1 -0
- package/dist/browser/{in-process-7XITYSC4.js → in-process-EWL5QP56.js} +214 -15
- package/dist/browser/in-process-EWL5QP56.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 +405 -59
- package/dist/cli/index.js.map +1 -1
- package/dist/config/index.d.ts +29 -5
- package/dist/config/index.js +20 -1
- 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 +30 -9
- package/dist/node/index.js +218 -19
- package/dist/node/index.js.map +1 -1
- package/package.json +3 -2
- package/dist/browser/chunk-M7J64NCO.js.map +0 -1
- package/dist/browser/in-process-7XITYSC4.js.map +0 -1
package/dist/node/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'> & {
|
|
@@ -130,7 +142,9 @@ interface ChopperSuccess<T> {
|
|
|
130
142
|
}
|
|
131
143
|
type ChopperResult<T> = ChopperSuccess<T> | ChopperFailure;
|
|
132
144
|
|
|
133
|
-
type
|
|
145
|
+
type HdrSourceMimeType = 'image/x-exr' | 'image/vnd.radiance';
|
|
146
|
+
|
|
147
|
+
type SourceImageMimeType = 'image/jpeg' | 'image/png' | HdrSourceMimeType;
|
|
134
148
|
interface SourceImage {
|
|
135
149
|
bytes: Uint8Array;
|
|
136
150
|
mimeType: SourceImageMimeType;
|
|
@@ -164,7 +178,7 @@ interface AssetInput {
|
|
|
164
178
|
}
|
|
165
179
|
interface ImageInspection {
|
|
166
180
|
kind: 'image';
|
|
167
|
-
mimeType: 'image/png' | 'image/jpeg';
|
|
181
|
+
mimeType: 'image/png' | 'image/jpeg' | 'image/x-exr' | 'image/vnd.radiance';
|
|
168
182
|
width: number;
|
|
169
183
|
height: number;
|
|
170
184
|
byteLength: number;
|
|
@@ -286,6 +300,13 @@ interface ChopperRule {
|
|
|
286
300
|
}
|
|
287
301
|
interface ChopperConfig {
|
|
288
302
|
output: string;
|
|
303
|
+
/**
|
|
304
|
+
* Every rule matches independently, so one file can be claimed by several of
|
|
305
|
+
* them. Where two rules resolve the same output path for the same input, the
|
|
306
|
+
* later rule wins: state the general strategy first and narrow it with
|
|
307
|
+
* exceptions after. Rules that resolve to different output paths all run, so
|
|
308
|
+
* one input can still fan out to several encodings.
|
|
309
|
+
*/
|
|
289
310
|
rules: readonly ChopperRule[];
|
|
290
311
|
concurrency?: number;
|
|
291
312
|
}
|
package/dist/node/index.js
CHANGED
|
@@ -24,7 +24,6 @@ import { glob } from 'tinyglobby';
|
|
|
24
24
|
// src/node/image-engine.ts
|
|
25
25
|
|
|
26
26
|
// src/core/recipes.ts
|
|
27
|
-
var RECIPE_SCHEMA_VERSION = 1;
|
|
28
27
|
function outputExtension(encoding) {
|
|
29
28
|
if (encoding.codec === "ktx2") return ".ktx2";
|
|
30
29
|
if (encoding.codec === "jpeg") return ".jpg";
|
|
@@ -60,13 +59,11 @@ function normalizeRecipe(value) {
|
|
|
60
59
|
const recipe = validated.data;
|
|
61
60
|
if (recipe.kind === "image") {
|
|
62
61
|
return success({
|
|
63
|
-
schemaVersion: RECIPE_SCHEMA_VERSION,
|
|
64
62
|
kind: "image",
|
|
65
63
|
encode: normalizeEncoding(recipe.encode)
|
|
66
64
|
});
|
|
67
65
|
}
|
|
68
66
|
return success({
|
|
69
|
-
schemaVersion: RECIPE_SCHEMA_VERSION,
|
|
70
67
|
kind: "glb",
|
|
71
68
|
methods: recipe.methods ?? [],
|
|
72
69
|
textures: (recipe.textures ?? []).map((texture) => ({
|
|
@@ -120,15 +117,15 @@ function encodingShapeIssue(encoding) {
|
|
|
120
117
|
if (!["webp", "avif", "jpeg", "png", "ktx2"].includes(String(candidate.codec))) {
|
|
121
118
|
return `Unsupported image codec: ${String(candidate.codec)}`;
|
|
122
119
|
}
|
|
123
|
-
if (candidate.codec === "ktx2" && candidate.mode !== "uastc" && candidate.mode !== "etc1s") {
|
|
120
|
+
if (candidate.codec === "ktx2" && candidate.mode !== "uastc" && candidate.mode !== "etc1s" && candidate.mode !== "uastc-hdr") {
|
|
124
121
|
return `Unsupported KTX2 mode: ${String(candidate.mode)}`;
|
|
125
122
|
}
|
|
123
|
+
if (candidate.codec === "ktx2" && candidate.mode === "uastc-hdr" && candidate.maxSize !== void 0) {
|
|
124
|
+
return "UASTC HDR does not support maxSize; resizing requires a decoded 8-bit image. Author the source at its target resolution.";
|
|
125
|
+
}
|
|
126
126
|
return void 0;
|
|
127
127
|
}
|
|
128
128
|
function recipeIssue(recipe) {
|
|
129
|
-
if (recipe.schemaVersion !== RECIPE_SCHEMA_VERSION) {
|
|
130
|
-
return `Unsupported recipe schema version: ${String(recipe.schemaVersion)}`;
|
|
131
|
-
}
|
|
132
129
|
if (recipe.kind === "image") return encodingIssue(recipe.encode);
|
|
133
130
|
const labels = /* @__PURE__ */ new Set();
|
|
134
131
|
const selectors = /* @__PURE__ */ new Set();
|
|
@@ -150,6 +147,9 @@ function recipeIssue(recipe) {
|
|
|
150
147
|
}
|
|
151
148
|
selectors.add(key);
|
|
152
149
|
}
|
|
150
|
+
if (texture.encode.codec === "ktx2" && texture.encode.mode === "uastc-hdr") {
|
|
151
|
+
return `Texture recipe "${texture.label}" uses UASTC HDR, which KHR_texture_basisu does not cover; HDR is only supported for standalone image recipes`;
|
|
152
|
+
}
|
|
153
153
|
const issue = encodingIssue(texture.encode);
|
|
154
154
|
if (issue) return issue;
|
|
155
155
|
}
|
|
@@ -171,7 +171,7 @@ function selectorValues(selector) {
|
|
|
171
171
|
return success(selector.indices);
|
|
172
172
|
}
|
|
173
173
|
function encodingIssue(encoding) {
|
|
174
|
-
if (encoding.maxSize) {
|
|
174
|
+
if ("maxSize" in encoding && encoding.maxSize) {
|
|
175
175
|
const [width, height] = encoding.maxSize;
|
|
176
176
|
if (!Number.isInteger(width) || width < 1 || !Number.isInteger(height) || height < 1) {
|
|
177
177
|
return "Texture maxSize must contain positive integers";
|
|
@@ -200,6 +200,9 @@ function encodingIssue(encoding) {
|
|
|
200
200
|
return "UASTC RDO lambda must be between 0.001 and 10";
|
|
201
201
|
}
|
|
202
202
|
}
|
|
203
|
+
if (encoding.codec === "ktx2" && encoding.mode === "uastc-hdr" && encoding.level !== void 0 && (!Number.isInteger(encoding.level) || encoding.level < 0 || encoding.level > 4)) {
|
|
204
|
+
return "UASTC HDR level must be an integer between 0 and 4";
|
|
205
|
+
}
|
|
203
206
|
if (encoding.codec === "ktx2" && encoding.mode === "etc1s" && encoding.compression !== void 0 && (!Number.isInteger(encoding.compression) || encoding.compression < 0 || encoding.compression > 6)) {
|
|
204
207
|
return "ETC1S compression must be an integer between 0 and 6";
|
|
205
208
|
}
|
|
@@ -232,6 +235,14 @@ function normalizeEncoding(encoding) {
|
|
|
232
235
|
case "png":
|
|
233
236
|
return { ...encoding };
|
|
234
237
|
case "ktx2":
|
|
238
|
+
if (encoding.mode === "uastc-hdr") {
|
|
239
|
+
return {
|
|
240
|
+
...encoding,
|
|
241
|
+
level: encoding.level ?? 1,
|
|
242
|
+
mipmaps: encoding.mipmaps ?? true,
|
|
243
|
+
zstd: encoding.zstd ?? true
|
|
244
|
+
};
|
|
245
|
+
}
|
|
235
246
|
if (encoding.mode === "uastc") {
|
|
236
247
|
return {
|
|
237
248
|
...encoding,
|
|
@@ -253,9 +264,133 @@ function normalizeEncoding(encoding) {
|
|
|
253
264
|
}
|
|
254
265
|
}
|
|
255
266
|
|
|
267
|
+
// src/codecs/hdr-source.ts
|
|
268
|
+
var HDR_SOURCE_MIME_TYPES = {
|
|
269
|
+
exr: "image/x-exr",
|
|
270
|
+
hdr: "image/vnd.radiance"
|
|
271
|
+
};
|
|
272
|
+
function isHdrSourceMimeType(mimeType) {
|
|
273
|
+
return mimeType === "image/x-exr" || mimeType === "image/vnd.radiance";
|
|
274
|
+
}
|
|
275
|
+
var EXR_MAGIC = [118, 47, 49, 1];
|
|
276
|
+
var VERSION_NUMBER_FIELD = 255;
|
|
277
|
+
var NON_IMAGE_FLAG = 2048;
|
|
278
|
+
var MULTI_PART_FILE_FLAG = 4096;
|
|
279
|
+
var EXR_COMPRESSION = [
|
|
280
|
+
{ name: "NO_COMPRESSION", supported: true },
|
|
281
|
+
{ name: "RLE", supported: true },
|
|
282
|
+
{ name: "ZIPS", supported: true },
|
|
283
|
+
{ name: "ZIP", supported: true },
|
|
284
|
+
{ name: "PIZ", supported: true },
|
|
285
|
+
{ name: "PXR24", supported: false },
|
|
286
|
+
{ name: "B44", supported: false },
|
|
287
|
+
{ name: "B44A", supported: false },
|
|
288
|
+
{ name: "DWAA", supported: false },
|
|
289
|
+
{ name: "DWAB", supported: false }
|
|
290
|
+
];
|
|
291
|
+
var RADIANCE_MAGIC = [35, 63];
|
|
292
|
+
var RADIANCE_HEADER_LIMIT = 8192;
|
|
293
|
+
var MAX_HDR_SOURCE_TEXELS = 12 * 1024 * 1024;
|
|
294
|
+
function detectHdrContainer(bytes) {
|
|
295
|
+
if (bytes.length >= 8 && EXR_MAGIC.every((byte, index) => bytes[index] === byte)) return "exr";
|
|
296
|
+
if (bytes.length >= 8 && RADIANCE_MAGIC.every((byte, index) => bytes[index] === byte)) return "hdr";
|
|
297
|
+
return void 0;
|
|
298
|
+
}
|
|
299
|
+
function readHdrSource(bytes) {
|
|
300
|
+
const container = detectHdrContainer(bytes);
|
|
301
|
+
if (!container) return { error: "Not an OpenEXR or Radiance HDR file" };
|
|
302
|
+
return container === "exr" ? readExr(bytes) : readRadiance(bytes);
|
|
303
|
+
}
|
|
304
|
+
function readExr(bytes) {
|
|
305
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
306
|
+
const version = view.getInt32(4, true);
|
|
307
|
+
if ((version & VERSION_NUMBER_FIELD) !== 2) {
|
|
308
|
+
return { error: `Unsupported EXR version: ${version & VERSION_NUMBER_FIELD}` };
|
|
309
|
+
}
|
|
310
|
+
if (version & MULTI_PART_FILE_FLAG) return { error: "Multi-part EXR files are not supported" };
|
|
311
|
+
if (version & NON_IMAGE_FLAG) return { error: "Deep EXR files are not supported" };
|
|
312
|
+
let size;
|
|
313
|
+
let compressionIssue;
|
|
314
|
+
let offset = 8;
|
|
315
|
+
while (offset < bytes.length) {
|
|
316
|
+
const name = readNullTerminated(bytes, offset);
|
|
317
|
+
if (name === void 0) return { error: "Malformed EXR header" };
|
|
318
|
+
if (name.value === "") break;
|
|
319
|
+
offset = name.next;
|
|
320
|
+
const type = readNullTerminated(bytes, offset);
|
|
321
|
+
if (type === void 0) return { error: "Malformed EXR header" };
|
|
322
|
+
offset = type.next;
|
|
323
|
+
if (offset + 4 > bytes.length) return { error: "Malformed EXR header" };
|
|
324
|
+
const length = view.getInt32(offset, true);
|
|
325
|
+
offset += 4;
|
|
326
|
+
if (length < 0 || offset + length > bytes.length) return { error: "Malformed EXR header" };
|
|
327
|
+
if (name.value === "dataWindow") {
|
|
328
|
+
if (type.value !== "box2i" || length !== 16) return { error: "Malformed EXR dataWindow" };
|
|
329
|
+
const xMin = view.getInt32(offset, true);
|
|
330
|
+
const yMin = view.getInt32(offset + 4, true);
|
|
331
|
+
const xMax = view.getInt32(offset + 8, true);
|
|
332
|
+
const yMax = view.getInt32(offset + 12, true);
|
|
333
|
+
const width = xMax - xMin + 1;
|
|
334
|
+
const height = yMax - yMin + 1;
|
|
335
|
+
if (width < 1 || height < 1) return { error: "EXR dataWindow is empty" };
|
|
336
|
+
size = { container: "exr", width, height };
|
|
337
|
+
}
|
|
338
|
+
if (name.value === "compression") {
|
|
339
|
+
if (length !== 1) return { error: "Malformed EXR compression attribute" };
|
|
340
|
+
compressionIssue = exrCompressionIssue(bytes[offset]);
|
|
341
|
+
}
|
|
342
|
+
offset += length;
|
|
343
|
+
}
|
|
344
|
+
if (compressionIssue) return { error: compressionIssue };
|
|
345
|
+
return size ?? { error: "EXR header has no dataWindow attribute" };
|
|
346
|
+
}
|
|
347
|
+
function exrCompressionIssue(code) {
|
|
348
|
+
const codec = EXR_COMPRESSION[code];
|
|
349
|
+
if (!codec) return `Unsupported EXR compression: unknown codec ${code}`;
|
|
350
|
+
if (codec.supported) return void 0;
|
|
351
|
+
const readable = EXR_COMPRESSION.filter((entry) => entry.supported).map((entry) => entry.name).join(", ");
|
|
352
|
+
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.`;
|
|
353
|
+
}
|
|
354
|
+
function readRadiance(bytes) {
|
|
355
|
+
const text = latin1(bytes.subarray(0, RADIANCE_HEADER_LIMIT));
|
|
356
|
+
const lines = text.split("\n");
|
|
357
|
+
let index = 0;
|
|
358
|
+
while (index < lines.length && lines[index] !== "" && lines[index] !== "\r") index++;
|
|
359
|
+
index++;
|
|
360
|
+
while (index < lines.length && (lines[index] === "" || lines[index] === "\r")) index++;
|
|
361
|
+
if (index >= lines.length) return { error: "Radiance file has no resolution line" };
|
|
362
|
+
const resolution = /^([+-][XY])\s+(\d+)\s+([+-][XY])\s+(\d+)\s*$/.exec(lines[index].replace(/\r$/, ""));
|
|
363
|
+
if (!resolution) return { error: `Malformed Radiance resolution line: ${lines[index].slice(0, 40)}` };
|
|
364
|
+
const extents = /* @__PURE__ */ new Map([
|
|
365
|
+
[resolution[1][1], Number(resolution[2])],
|
|
366
|
+
[resolution[3][1], Number(resolution[4])]
|
|
367
|
+
]);
|
|
368
|
+
const width = extents.get("X");
|
|
369
|
+
const height = extents.get("Y");
|
|
370
|
+
if (width === void 0 || height === void 0) {
|
|
371
|
+
return { error: "Radiance resolution line must pair an X and a Y axis" };
|
|
372
|
+
}
|
|
373
|
+
if (width < 1 || height < 1) return { error: "Radiance image is empty" };
|
|
374
|
+
return { container: "hdr", width, height };
|
|
375
|
+
}
|
|
376
|
+
function readNullTerminated(bytes, start) {
|
|
377
|
+
const limit = Math.min(bytes.length, start + 256);
|
|
378
|
+
for (let index = start; index < limit; index++) {
|
|
379
|
+
if (bytes[index] === 0) {
|
|
380
|
+
return { value: latin1(bytes.subarray(start, index)), next: index + 1 };
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return void 0;
|
|
384
|
+
}
|
|
385
|
+
function latin1(bytes) {
|
|
386
|
+
let value = "";
|
|
387
|
+
for (const byte of bytes) value += String.fromCharCode(byte);
|
|
388
|
+
return value;
|
|
389
|
+
}
|
|
390
|
+
|
|
256
391
|
// package.json
|
|
257
392
|
var package_default = {
|
|
258
|
-
version: "0.
|
|
393
|
+
version: "0.5.1"};
|
|
259
394
|
|
|
260
395
|
// src/codecs/manifest.ts
|
|
261
396
|
var CODEC_MANIFEST_HASH = "2046d60deb0f9f224743f4404f3c6e8ba5399f81b2811fa50466e93fce3bd322";
|
|
@@ -345,12 +480,23 @@ var ENGINE_ID = `chopper@${package_default.version}:${CODEC_MANIFEST_HASH}`;
|
|
|
345
480
|
// src/codecs/image-engine.ts
|
|
346
481
|
var CodecLoadError = class extends Error {
|
|
347
482
|
};
|
|
483
|
+
var SourceError = class extends Error {
|
|
484
|
+
constructor(code, message) {
|
|
485
|
+
super(message);
|
|
486
|
+
this.code = code;
|
|
487
|
+
}
|
|
488
|
+
};
|
|
348
489
|
function createWasmImageEngine(loader) {
|
|
349
490
|
const adapters = createAdapterCache(loader);
|
|
350
491
|
return {
|
|
351
492
|
id: ENGINE_ID,
|
|
352
493
|
manifestHash: CODEC_MANIFEST_HASH,
|
|
353
494
|
async inspect(source) {
|
|
495
|
+
if (isHdrSourceMimeType(source.mimeType)) {
|
|
496
|
+
const header = readHdrSource(source.bytes);
|
|
497
|
+
if ("error" in header) return failure("decode_failed", header.error);
|
|
498
|
+
return success({ width: header.width, height: header.height });
|
|
499
|
+
}
|
|
354
500
|
try {
|
|
355
501
|
const image = await decodeSource(source, adapters);
|
|
356
502
|
return success({ width: image.width, height: image.height });
|
|
@@ -359,10 +505,17 @@ function createWasmImageEngine(loader) {
|
|
|
359
505
|
}
|
|
360
506
|
},
|
|
361
507
|
async encode(source, encoding, signal) {
|
|
362
|
-
const valid = validateRecipe({
|
|
508
|
+
const valid = validateRecipe({ kind: "image", encode: encoding });
|
|
363
509
|
if (!valid.ok) return valid;
|
|
510
|
+
const mismatch = sourcePairingIssue(source, encoding);
|
|
511
|
+
if (mismatch) return mismatch;
|
|
364
512
|
try {
|
|
365
513
|
assertNotAborted(signal);
|
|
514
|
+
if (isHdrEncoding(encoding)) {
|
|
515
|
+
const encoded2 = await encodeKtx2Hdr(source, encoding, await loader.basisEncoderUrl());
|
|
516
|
+
assertNotAborted(signal);
|
|
517
|
+
return success(encoded2);
|
|
518
|
+
}
|
|
366
519
|
let image = await decodeSource(source, adapters);
|
|
367
520
|
assertNotAborted(signal);
|
|
368
521
|
if (encoding.maxSize) {
|
|
@@ -517,6 +670,45 @@ async function encodeRaster(image, encoding, adapters) {
|
|
|
517
670
|
}
|
|
518
671
|
return { bytes: toUint8Array(bytes), mimeType: outputMimeType(encoding) };
|
|
519
672
|
}
|
|
673
|
+
var megapixels = (texels) => (texels / 1e6).toFixed(1);
|
|
674
|
+
function isHdrEncoding(encoding) {
|
|
675
|
+
return encoding.codec === "ktx2" && encoding.mode === "uastc-hdr";
|
|
676
|
+
}
|
|
677
|
+
function sourcePairingIssue(source, encoding) {
|
|
678
|
+
const isHdrSource = isHdrSourceMimeType(source.mimeType);
|
|
679
|
+
if (isHdrEncoding(encoding) && !isHdrSource) {
|
|
680
|
+
return failure("invalid_recipe", `UASTC HDR requires an EXR or Radiance HDR source; received ${source.mimeType}`);
|
|
681
|
+
}
|
|
682
|
+
if (isHdrSource && !isHdrEncoding(encoding)) {
|
|
683
|
+
return failure(
|
|
684
|
+
"invalid_recipe",
|
|
685
|
+
"HDR sources can only be encoded as KTX2 with mode 'uastc-hdr'; chopper does not decode HDR pixels"
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
return void 0;
|
|
689
|
+
}
|
|
690
|
+
async function encodeKtx2Hdr(source, encoding, wasmUrl) {
|
|
691
|
+
const header = readHdrSource(source.bytes);
|
|
692
|
+
if ("error" in header) throw new SourceError("decode_failed", header.error);
|
|
693
|
+
const texels = header.width * header.height;
|
|
694
|
+
if (texels > MAX_HDR_SOURCE_TEXELS) {
|
|
695
|
+
throw new SourceError(
|
|
696
|
+
"unsupported_asset",
|
|
697
|
+
`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.`
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
const bytes = await encodeToKTX2(source.bytes, {
|
|
701
|
+
isHDR: true,
|
|
702
|
+
imageType: header.container,
|
|
703
|
+
isUASTC: true,
|
|
704
|
+
hdrQualityLevel: encoding.level ?? 1,
|
|
705
|
+
needSupercompression: encoding.zstd ?? true,
|
|
706
|
+
generateMipmap: encoding.mipmaps ?? true,
|
|
707
|
+
isKTX2File: true,
|
|
708
|
+
wasmUrl
|
|
709
|
+
});
|
|
710
|
+
return { bytes, mimeType: outputMimeType(encoding), width: header.width, height: header.height };
|
|
711
|
+
}
|
|
520
712
|
async function encodeKtx2(source, image, encoding, wasmUrl) {
|
|
521
713
|
const srgb = encoding.colorSpace !== "linear";
|
|
522
714
|
const bytes = await encodeToKTX2(source.bytes, {
|
|
@@ -542,7 +734,9 @@ async function encodeKtx2(source, image, encoding, wasmUrl) {
|
|
|
542
734
|
return { bytes, mimeType: outputMimeType(encoding) };
|
|
543
735
|
}
|
|
544
736
|
function operationFailure(code, message, cause) {
|
|
545
|
-
|
|
737
|
+
if (cause instanceof CodecLoadError) return failure("codec_load_failed", cause.message, cause.cause);
|
|
738
|
+
if (cause instanceof SourceError) return failure(cause.code, cause.message);
|
|
739
|
+
return failure(code, message, cause);
|
|
546
740
|
}
|
|
547
741
|
function assertNotAborted(signal) {
|
|
548
742
|
if (signal?.aborted) throw new DOMException("Optimization was aborted", "AbortError");
|
|
@@ -988,8 +1182,11 @@ function detectAsset(input) {
|
|
|
988
1182
|
if (input.bytes.length >= 4 && input.bytes[0] === 103 && input.bytes[1] === 108 && input.bytes[2] === 84 && input.bytes[3] === 70) {
|
|
989
1183
|
return success("glb");
|
|
990
1184
|
}
|
|
991
|
-
if (isPng(input.bytes) || isJpeg(input.bytes)) return success("image");
|
|
992
|
-
return failure(
|
|
1185
|
+
if (isPng(input.bytes) || isJpeg(input.bytes) || detectHdrContainer(input.bytes)) return success("image");
|
|
1186
|
+
return failure(
|
|
1187
|
+
"unsupported_asset",
|
|
1188
|
+
`Unsupported asset: ${input.fileName}; expected GLB, PNG, JPEG, EXR, or Radiance HDR`
|
|
1189
|
+
);
|
|
993
1190
|
}
|
|
994
1191
|
async function inspectImage(input, mimeType, engine) {
|
|
995
1192
|
const dimensions = await engine.inspect({ bytes: input.bytes, mimeType });
|
|
@@ -1003,6 +1200,8 @@ async function inspectImage(input, mimeType, engine) {
|
|
|
1003
1200
|
});
|
|
1004
1201
|
}
|
|
1005
1202
|
function imageMimeType(input) {
|
|
1203
|
+
const hdr = detectHdrContainer(input.bytes);
|
|
1204
|
+
if (hdr) return HDR_SOURCE_MIME_TYPES[hdr];
|
|
1006
1205
|
return isPng(input.bytes) ? "image/png" : "image/jpeg";
|
|
1007
1206
|
}
|
|
1008
1207
|
function isPng(bytes) {
|
|
@@ -1149,22 +1348,22 @@ async function planAssets(config, cwd) {
|
|
|
1149
1348
|
});
|
|
1150
1349
|
}
|
|
1151
1350
|
}
|
|
1152
|
-
const
|
|
1351
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
1153
1352
|
const inputs = new Set(planned.map(({ input }) => input));
|
|
1154
1353
|
for (const asset of planned) {
|
|
1155
|
-
const owner =
|
|
1156
|
-
if (owner && owner !== asset.input) {
|
|
1354
|
+
const owner = resolved.get(asset.output);
|
|
1355
|
+
if (owner && owner.input !== asset.input) {
|
|
1157
1356
|
return failure(
|
|
1158
1357
|
"invalid_recipe",
|
|
1159
|
-
`Output collision: ${relative(cwd, owner)} and ${relative(cwd, asset.input)} both write ${relative(cwd, asset.output)}`
|
|
1358
|
+
`Output collision: ${relative(cwd, owner.input)} and ${relative(cwd, asset.input)} both write ${relative(cwd, asset.output)}`
|
|
1160
1359
|
);
|
|
1161
1360
|
}
|
|
1162
1361
|
if (asset.output !== asset.input && inputs.has(asset.output)) {
|
|
1163
1362
|
return failure("invalid_recipe", `Output loop: ${relative(cwd, asset.output)} is also a matched input`);
|
|
1164
1363
|
}
|
|
1165
|
-
|
|
1364
|
+
resolved.set(asset.output, asset);
|
|
1166
1365
|
}
|
|
1167
|
-
return success(
|
|
1366
|
+
return success([...resolved.values()]);
|
|
1168
1367
|
}
|
|
1169
1368
|
async function executeAsset(asset, mode, cwd, chopper2) {
|
|
1170
1369
|
try {
|