nanoodle 0.14.0 → 0.14.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 CHANGED
@@ -5,7 +5,7 @@
5
5
  and re-run them here — same graph, same [NanoGPT](https://nano-gpt.com) API,
6
6
  your own key.
7
7
 
8
- Zero runtime dependencies. Library + CLI in one install.
8
+ The library and CLI come in one npm install.
9
9
 
10
10
  Looking for Python? → **[nanoodle-py](https://github.com/nanoodlecom/nanoodle-py)**
11
11
 
@@ -35,7 +35,7 @@ agents.
35
35
  | | |
36
36
  |---|---|
37
37
  | **Package** | `nanoodle` on npm |
38
- | **Runtime** | Node 22.23.5+, 24.22.3+, or 26.13.1+ · built-in `fetch` · no deps |
38
+ | **Runtime** | Node 22.23.5+, 24.22.3+, or 26.13.1+ · built-in `fetch` · sharp and libavif for local image decoding |
39
39
  | **Sibling** | [Python package](https://github.com/nanoodlecom/nanoodle-py) (same graphs, same semantics) |
40
40
  | **Editor** | [nanoodle.com](https://nanoodle.com) — wire nodes, hit 💾, download the graph |
41
41
 
@@ -280,7 +280,7 @@ decisions, joins, retries, and an existing gate.
280
280
 
281
281
  `slice-3d` takes a `model` wire and an optional `paletteImage`. It reads static GLB or self-contained glTF 2 triangle meshes and uncompressed Gaussian PLY splats (ASCII or binary little-endian). Use the original Gaussian PLY for splats. Auto up is Y for meshes and Z for splats. The default is 48 layers of 48 × 48 pixels, 16 colors and an outline. Set `palette` to 0 to preserve source colors.
282
282
 
283
- The `sheet` output is a transparent PNG with square tiles ordered from ground at left to top at right. The `layout` text output gives the bounds, axis and sampling dimensions. The `image` output previews the stack at 35° yaw. Wire `sheet` into `sprite-stack-render.sheet` to change yaw, pitch, layer spacing or pixel scale. Enable `turntable` to add a local `video` output with configurable duration, fps, start yaw and end yaw. In the editor and `nanoodle/browser`, Canvas 2D draws the view and MediaRecorder exports MP4 or WebM. Node.js runs the same graph headlessly. Its CPU renderer reads ordinary PNG sheets directly and uses FFmpeg on PATH for additional image codecs and MP4 turntables.
283
+ The `sheet` output is a transparent PNG with square tiles ordered from ground at left to top at right. The `layout` text output gives the bounds, axis and sampling dimensions. The `image` output previews the stack at 35° yaw. Wire `sheet` into `sprite-stack-render.sheet` to change yaw, pitch, layer spacing or pixel scale. Enable `turntable` to add a local `video` output with configurable duration, fps, start yaw and end yaw. The editor draws with Canvas 2D. Node.js runs the same graph headlessly. Texture, palette and sheet inputs accept PNG, APNG, JPEG, GIF, WebP, AVIF, BMP, ICO and CUR. Animated inputs use their first displayed frame. A normal npm install supplies the image decoders. Both runtimes encode H.264 MP4 turntables in-process.
284
284
 
285
285
  † `decide` asks a NanoGPT decision model one typed question (`POST /api/v1/decisions`): pick the best of the wired `img1…` images, choose a label, score on a scale, or yes/no. Text-only decisions need nothing extra; wired images are shrunk to the model's limits with ffmpeg. A yes/no gate that answers no is not a failure: that node settles as `gated`, everything downstream is `skipped` unbilled, and the run succeeds (see [Gates](#gates-decide-said-no)).
286
286
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nanoodle",
3
- "version": "0.14.0",
4
- "description": "Run nanoodle visual AI workflows from JavaScript — zero-dependency executor for saved noodle-graph.json files",
3
+ "version": "0.14.1",
4
+ "description": "Run nanoodle visual AI workflows from JavaScript with a local executor for saved noodle-graph.json files",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
7
7
  "exports": {
@@ -46,9 +46,7 @@
46
46
  "automation",
47
47
  "tts",
48
48
  "transcription",
49
- "byok",
50
- "no-deps",
51
- "zero-dependencies"
49
+ "byok"
52
50
  ],
53
51
  "license": "MIT",
54
52
  "homepage": "https://nanoodle.com",
@@ -58,5 +56,9 @@
58
56
  },
59
57
  "bugs": {
60
58
  "url": "https://github.com/nanoodlecom/nanoodle-js/issues"
59
+ },
60
+ "dependencies": {
61
+ "@jsquash/avif": "^2.1.1",
62
+ "sharp": "^0.35.5"
61
63
  }
62
64
  }
package/src/graph.mjs CHANGED
@@ -124,6 +124,12 @@ function scrubMediaPlaceholders(n, warnings) {
124
124
  const keys = n.type === "mupload" ? MEDIA_FIELD_KEYS.concat(["model"]) : MEDIA_FIELD_KEYS;
125
125
  for (const k of keys) {
126
126
  const v = n.fields[k];
127
+ // Built-in editor examples store their same-origin assets as relative paths.
128
+ if (typeof v === "string" && /^examples\/[A-Za-z0-9_./-]+$/.test(v) && !v.includes("..")) {
129
+ const origin = globalThis.location?.origin;
130
+ n.fields[k] = (/^https?:\/\//.test(origin || "") ? origin : "https://nanoodle.com") + "/" + v;
131
+ continue;
132
+ }
127
133
  if (v == null || v === "" || (typeof v === "string" && MEDIA_URL_RE.test(v.trim()))) continue;
128
134
  const shown = typeof v === "string"
129
135
  ? `"${v.length > 60 ? v.slice(0, 57) + "…" : v}"`
@@ -13,7 +13,8 @@
13
13
  *
14
14
  * Outputs are data: URLs so they plug into the existing MediaRef / network-inline pipeline.
15
15
  */
16
- import { inflate, deflate } from "./zlib.mjs";
16
+ import { deflate } from "./zlib.mjs";
17
+ import { decodePngPixels } from "./png-decoder.mjs";
17
18
  import { NanoodleError } from "./errors.mjs";
18
19
  import { bytesToDataUrl, dataUrlBytes, downloadMedia, sniffMime, MEDIA_INLINE_MAX } from "./media.mjs";
19
20
  import { MP4CAT } from "./mp4cat.mjs";
@@ -217,99 +218,9 @@ function pngChunk(type, data) {
217
218
  return out;
218
219
  }
219
220
 
220
- /** Decode 8-bit RGB/RGBA/gray/gray+alpha PNG → { w, h, rgba:Uint8ClampedArray }. */
221
+ /** Decode a PNG texture to straight-alpha 8-bit RGBA. */
221
222
  async function decodePng(bytes, allowSpriteStrip = false) {
222
- const u8 = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
223
- if (u8.length < 8 || u8[0] !== 0x89 || u8[1] !== 0x50) {
224
- throw new NanoodleError("couldn't read that image to resize");
225
- }
226
- const sig = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
227
- for (let i = 0; i < 8; i++) if (u8[i] !== sig[i]) throw new NanoodleError("couldn't read that image to resize");
228
-
229
- let w = 0, h = 0, bitDepth = 8, colorType = 2;
230
- const idats = [];
231
- let p = 8;
232
- while (p + 8 <= u8.length) {
233
- // >>> 0: PNG chunk lengths are unsigned; JS << is signed 32-bit
234
- const len = ((u8[p] << 24) | (u8[p + 1] << 16) | (u8[p + 2] << 8) | u8[p + 3]) >>> 0;
235
- if (p + 12 + len > u8.length) throw new NanoodleError("couldn't read that image to resize");
236
- const type = String.fromCharCode(u8[p + 4], u8[p + 5], u8[p + 6], u8[p + 7]);
237
- const data = u8.subarray(p + 8, p + 8 + len);
238
- p += 12 + len;
239
- if (type === "IHDR") {
240
- w = ((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]) >>> 0;
241
- h = ((data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7]) >>> 0;
242
- bitDepth = data[8];
243
- colorType = data[9];
244
- if (data[10] !== 0 || data[11] !== 0 || data[12] !== 0) {
245
- throw new NanoodleError("couldn't read that image to resize"); // compressed/filter/interlace
246
- }
247
- } else if (type === "IDAT") {
248
- idats.push(data);
249
- } else if (type === "IEND") break;
250
- }
251
- if (!(w > 0) || !(h > 0) || bitDepth !== 8) {
252
- throw new NanoodleError("couldn't read that image to resize");
253
- }
254
- const maxWidth = allowSpriteStrip && h <= 128 ? 128 * 128 : MAX_IMAGE_DIM;
255
- if (w > maxWidth || h > MAX_IMAGE_DIM) {
256
- throw new NanoodleError(
257
- `image is too large to resize in-process (${w}×${h}; max ${MAX_IMAGE_DIM}px) — use smaller source dimensions`);
258
- }
259
- // colorType: 0 gray, 2 RGB, 4 gray+A, 6 RGBA (no palette)
260
- if (colorType !== 0 && colorType !== 2 && colorType !== 4 && colorType !== 6) {
261
- throw new NanoodleError("couldn't read that image to resize");
262
- }
263
- const cpp = colorType === 0 ? 1 : colorType === 2 ? 3 : colorType === 4 ? 2 : 4;
264
- const raw = await inflate(catBytes(idats));
265
- const stride = w * cpp;
266
- const expected = h * (1 + stride);
267
- if (raw.length < expected) throw new NanoodleError("couldn't read that image to resize");
268
-
269
- const unfiltered = new Uint8Array(h * stride);
270
- for (let y = 0; y < h; y++) {
271
- const ftype = raw[y * (1 + stride)];
272
- const row = raw.subarray(y * (1 + stride) + 1, y * (1 + stride) + 1 + stride);
273
- const dest = unfiltered.subarray(y * stride, (y + 1) * stride);
274
- const prev = y > 0 ? unfiltered.subarray((y - 1) * stride, y * stride) : null;
275
- for (let x = 0; x < stride; x++) {
276
- const left = x >= cpp ? dest[x - cpp] : 0;
277
- const up = prev ? prev[x] : 0;
278
- const upLeft = prev && x >= cpp ? prev[x - cpp] : 0;
279
- let v = row[x];
280
- if (ftype === 1) v = (v + left) & 255; // Sub
281
- else if (ftype === 2) v = (v + up) & 255; // Up
282
- else if (ftype === 3) v = (v + ((left + up) >> 1)) & 255; // Average
283
- else if (ftype === 4) { // Paeth
284
- const p0 = left + up - upLeft;
285
- const pa = Math.abs(p0 - left), pb = Math.abs(p0 - up), pc = Math.abs(p0 - upLeft);
286
- const pr = pa <= pb && pa <= pc ? left : pb <= pc ? up : upLeft;
287
- v = (v + pr) & 255;
288
- } else if (ftype !== 0) {
289
- throw new NanoodleError("couldn't read that image to resize");
290
- }
291
- dest[x] = v;
292
- }
293
- }
294
-
295
- const rgba = new Uint8ClampedArray(w * h * 4);
296
- for (let i = 0, px = 0; i < w * h; i++, px += cpp) {
297
- const o = i * 4;
298
- if (colorType === 0) {
299
- rgba[o] = rgba[o + 1] = rgba[o + 2] = unfiltered[px];
300
- rgba[o + 3] = 255;
301
- } else if (colorType === 2) {
302
- rgba[o] = unfiltered[px]; rgba[o + 1] = unfiltered[px + 1]; rgba[o + 2] = unfiltered[px + 2];
303
- rgba[o + 3] = 255;
304
- } else if (colorType === 4) {
305
- rgba[o] = rgba[o + 1] = rgba[o + 2] = unfiltered[px];
306
- rgba[o + 3] = unfiltered[px + 1];
307
- } else {
308
- rgba[o] = unfiltered[px]; rgba[o + 1] = unfiltered[px + 1];
309
- rgba[o + 2] = unfiltered[px + 2]; rgba[o + 3] = unfiltered[px + 3];
310
- }
311
- }
312
- return { w, h, rgba };
223
+ return decodePngPixels(bytes, {maxDim:MAX_IMAGE_DIM, allowSpriteStrip});
313
224
  }
314
225
 
315
226
  async function encodePngRgba(w, h, rgba) {
@@ -1096,4 +1007,4 @@ export async function muxSoundtrack(videoUrl, audioUrl, loop = false, { fetch: f
1096
1007
  export { MAX_FRAMES, MAX_IMAGE_DIM, MP4CAT };
1097
1008
  // PNG codec internals — used by the nanoodle repo's dual-engine parity harness
1098
1009
  // (pixel-level mask comparison + headless canvas shim). Not part of the stable API.
1099
- export { decodePng, encodePngRgba, withTemp, runProc };
1010
+ export { decodePng, encodePngRgba };
package/src/mp4cat.mjs CHANGED
@@ -171,6 +171,11 @@ function concatMp4(buffers, opts){
171
171
  outTracks.push(merged);
172
172
  }
173
173
 
174
+ return muxTracks(buffers, outTracks);
175
+ }
176
+
177
+ // Both a lossless concat and a newly encoded sprite track use the same muxer.
178
+ function muxTracks(buffers, outTracks, fastStart = false) {
174
179
  // Build mdat first so chunk offsets are known. Layout: ftyp + mdat + moov.
175
180
  const ftyp = box("ftyp", enc("isom"), u32(0x200), enc("isomiso2avc1mp41"));
176
181
  // assemble mdat data, recording each sample's absolute offset
@@ -184,10 +189,21 @@ function concatMp4(buffers, opts){
184
189
  cursor += s.size;
185
190
  }
186
191
  }
187
- const mdatData = concat(mdatParts);
188
- const mdat = concat([u32(mdatData.length + 8), enc("mdat"), mdatData]);
192
+ if(cursor > 0xffffffff) throw new Error("MP4 exceeds 32-bit chunk offsets");
193
+ const mdatHeader = concat([u32(cursor - ftyp.length), enc("mdat")]);
189
194
 
190
- // moov
195
+ let moov = movieBox(outTracks, 0);
196
+ if (cursor + moov.length > 0xffffffff) throw new Error("MP4 exceeds 32-bit file size");
197
+ if (fastStart) {
198
+ // All chunk offsets are fixed-width stco entries. Moving moov in
199
+ // front of mdat shifts each entry by exactly the size of moov.
200
+ moov = movieBox(outTracks, moov.length);
201
+ return concat([ftyp, moov, mdatHeader, ...mdatParts]);
202
+ }
203
+ return concat([ftyp, mdatHeader, ...mdatParts, moov]);
204
+ }
205
+
206
+ function movieBox(outTracks, chunkShift) {
191
207
  const mvTimescale = 1000;
192
208
  let maxDurMs = 0;
193
209
  const trakBoxes = [];
@@ -202,7 +218,7 @@ function concatMp4(buffers, opts){
202
218
  const stts = fullbox("stts", 0, 0, u32(sttsRuns.length), ...sttsRuns.map(r=>concat([u32(r[0]), u32(r[1])])));
203
219
  const stsz = fullbox("stsz", 0, 0, u32(0), u32(t.samples.length), ...t.samples.map(s=>u32(s.size)));
204
220
  const stsc = fullbox("stsc", 0, 0, u32(1), concat([u32(1), u32(1), u32(1)]));
205
- const stco = fullbox("stco", 0, 0, u32(t.samples.length), ...t.samples.map(s=>u32(s.newOffset)));
221
+ const stco = fullbox("stco", 0, 0, u32(t.samples.length), ...t.samples.map(s=>u32(s.newOffset + chunkShift)));
206
222
  const children = [t.stsdRaw, stts];
207
223
  if(t.kind==="video"){
208
224
  const anyCts = t.samples.some(s=>s.cts!==0);
@@ -242,9 +258,7 @@ function concatMp4(buffers, opts){
242
258
  u32(0x00010000), u16(0x0100), u16(0), new Uint8Array(8),
243
259
  concat([u32(0x00010000),u32(0),u32(0),u32(0),u32(0x00010000),u32(0),u32(0),u32(0),u32(0x40000000)]),
244
260
  new Uint8Array(24), u32(trackId));
245
- const moov = box("moov", mvhd, ...trakBoxes);
246
-
247
- return concat([ftyp, mdat, moov]);
261
+ return box("moov", mvhd, ...trakBoxes);
248
262
  }
249
263
 
250
264
  // quick sniff
@@ -276,7 +290,25 @@ function mp4ParamsMatch(bufs){
276
290
  return true;
277
291
  }catch(e){ return false; }
278
292
  }
279
- return { concatMp4, isMp4, mp4ParamsMatch, parseMp4 };
293
+ // A single AVC track, with one length-prefixed NAL per sample. Keep the
294
+ // visual sample entry and AVC decoder configuration in the sample table.
295
+ function muxAvc(samples, width, height, seconds, sps, pps) {
296
+ const avcC = box("avcC", Uint8Array.of(1, sps[1], sps[2], sps[3], 0xff, 0xe1),
297
+ u16(sps.length), sps, Uint8Array.of(1), u16(pps.length), pps);
298
+ const avc1 = box("avc1", new Uint8Array(6), u16(1), new Uint8Array(16),
299
+ u16(width), u16(height), u32(0x00480000), u32(0x00480000), u32(0),
300
+ u16(1), new Uint8Array(32), u16(24), u16(0xffff), avcC);
301
+ const timescale = 90000;
302
+ const total = Math.round(seconds * timescale);
303
+ const buffers = samples.map(s => s.bytes);
304
+ const entries = samples.map((s, i) => ({
305
+ bufIdx: i, offset: 0, size: s.bytes.length, cts: 0, sync: s.sync,
306
+ dur: Math.round((i + 1) * total / samples.length) - Math.round(i * total / samples.length)
307
+ }));
308
+ return muxTracks(buffers, [{kind:"video", width, height, timescale,
309
+ stsdRaw:fullbox("stsd", 0, 0, u32(1), avc1), samples:entries}], true);
310
+ }
311
+ return { concatMp4, isMp4, mp4ParamsMatch, parseMp4, muxAvc };
280
312
  })();
281
313
 
282
314
 
@@ -0,0 +1,226 @@
1
+ // PNG scanlines share their filtering and sample reconstruction across Adam7
2
+ // passes. The atlas can be wider than an ordinary texture (128 x 128 tiles).
3
+ import { inflate } from "./zlib.mjs";
4
+ import { NanoodleError } from "./errors.mjs";
5
+
6
+ const SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
7
+ const CHANNELS = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };
8
+ const DEPTHS = { 0: [1, 2, 4, 8, 16], 2: [8, 16], 3: [1, 2, 4, 8], 4: [8, 16], 6: [8, 16] };
9
+ const ADAM7 = [
10
+ [0, 0, 8, 8],
11
+ [4, 0, 8, 8],
12
+ [0, 4, 4, 8],
13
+ [2, 0, 4, 4],
14
+ [0, 2, 2, 4],
15
+ [1, 0, 2, 2],
16
+ [0, 1, 1, 2],
17
+ ];
18
+
19
+ function check(condition, detail) {
20
+ if (!condition) throw new NanoodleError("Invalid PNG: " + detail);
21
+ }
22
+
23
+ function unfilter(row, prev, step, mode) {
24
+ check(mode <= 4, "unknown scanline filter");
25
+ if (!mode) return;
26
+ for (let x = 0; x < row.length; x++) {
27
+ const a = x >= step ? row[x - step] : 0;
28
+ const b = prev ? prev[x] : 0;
29
+ if (mode === 1) row[x] += a;
30
+ else if (mode === 2) row[x] += b;
31
+ else if (mode === 3) row[x] += (a + b) >> 1;
32
+ else {
33
+ const c = prev && x >= step ? prev[x - step] : 0;
34
+ const da = Math.abs(b - c),
35
+ db = Math.abs(a - c),
36
+ dc = Math.abs(a + b - 2 * c);
37
+ row[x] += da <= db && da <= dc ? a : db <= dc ? b : c;
38
+ }
39
+ }
40
+ }
41
+
42
+ function sample(row, n, depth) {
43
+ if (depth === 8) return row[n];
44
+ if (depth === 16) return row[2 * n] * 256 + row[2 * n + 1];
45
+ const bit = n * depth;
46
+ return (row[bit >> 3] >> (8 - depth - (bit & 7))) & ((1 << depth) - 1);
47
+ }
48
+
49
+ export async function decodePngPixels(bytes, { maxDim, allowSpriteStrip, firstAnimationFrame = false }) {
50
+ const u = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
51
+ check(
52
+ SIGNATURE.every((v, i) => u[i] === v),
53
+ "signature",
54
+ );
55
+ const view = new DataView(u.buffer, u.byteOffset, u.byteLength);
56
+ let w,
57
+ h,
58
+ depth,
59
+ type,
60
+ interlace,
61
+ palette,
62
+ trns,
63
+ ended = false,
64
+ compressedSize = 0;
65
+ let animated = false,
66
+ sawIdat = false,
67
+ controlCount = 0,
68
+ firstFrame;
69
+ const chunks = [];
70
+ for (let p = 8; p + 12 <= u.length; ) {
71
+ const length = view.getUint32(p),
72
+ start = p + 8;
73
+ check(start + length + 4 <= u.length, "truncated chunk");
74
+ const name = String.fromCharCode(...u.subarray(p + 4, start));
75
+ const data = u.subarray(start, start + length);
76
+ check(name === "IHDR" || w, "missing header");
77
+ if (name === "IHDR") {
78
+ check(p === 8 && length === 13, "header");
79
+ w = view.getUint32(start);
80
+ h = view.getUint32(start + 4);
81
+ depth = data[8];
82
+ type = data[9];
83
+ interlace = data[12];
84
+ const maxWidth = allowSpriteStrip && h <= 128 ? 16384 : maxDim;
85
+ check(w > 0 && h > 0, "empty dimensions");
86
+ if (w > maxWidth || h > maxDim)
87
+ throw new NanoodleError(
88
+ `image is too large to resize in-process (${w}×${h}; max ${maxDim}px) — use smaller source dimensions`,
89
+ );
90
+ check(DEPTHS[type]?.includes(depth) && !data[10] && !data[11] && interlace <= 1, "header encoding");
91
+ } else if (name === "PLTE") {
92
+ check(!sawIdat && length > 0 && length <= 768 && length % 3 === 0, "palette");
93
+ palette = data;
94
+ } else if (name === "tRNS") {
95
+ check(
96
+ !sawIdat &&
97
+ ((type === 3 && palette && length <= palette.length / 3) ||
98
+ (type === 0 && length === 2) ||
99
+ (type === 2 && length === 6)),
100
+ "transparency",
101
+ );
102
+ trns = data;
103
+ } else if (firstAnimationFrame && name === "acTL") {
104
+ check(!sawIdat && !animated && length === 8 && view.getUint32(start), "animation control");
105
+ animated = true;
106
+ } else if (animated && name === "fcTL") {
107
+ check(length === 26, "animation frame control");
108
+ if (++controlCount === 1) {
109
+ const width = view.getUint32(start + 4),
110
+ height = view.getUint32(start + 8);
111
+ const x = view.getUint32(start + 12),
112
+ y = view.getUint32(start + 16);
113
+ check(
114
+ width && height && x + width <= w && y + height <= h && data[24] <= 2 && data[25] <= 1,
115
+ "animation frame bounds or operation",
116
+ );
117
+ check(sawIdat || (width === w && height === h && !x && !y), "animated IDAT dimensions");
118
+ firstFrame = { width, height, x, y, poster: sawIdat, over: data[25] === 1 };
119
+ // With fcTL after IDAT, IDAT is a separate poster outside the animation.
120
+ if (sawIdat) {
121
+ chunks.length = 0;
122
+ compressedSize = 0;
123
+ }
124
+ }
125
+ } else if (animated && name === "fdAT") {
126
+ check(firstFrame && sawIdat && length >= 4, "animation frame data");
127
+ if (controlCount === 1 && firstFrame.poster) {
128
+ chunks.push(data.subarray(4));
129
+ compressedSize += length - 4;
130
+ }
131
+ } else if (name === "IDAT") {
132
+ check(!firstFrame?.poster, "IDAT after animation frame data");
133
+ sawIdat = true;
134
+ compressedSize += length;
135
+ chunks.push(data);
136
+ } else if (name === "IEND") {
137
+ check(length === 0, "end chunk");
138
+ ended = true;
139
+ break;
140
+ } else check((u[p + 4] & 32) !== 0, "unsupported critical chunk " + name);
141
+ p = start + length + 4;
142
+ }
143
+ check(ended && chunks.length && (type !== 3 || palette), "missing image data or palette");
144
+ const frame = firstFrame || { width: w, height: h, x: 0, y: 0 };
145
+ const channels = CHANNELS[type],
146
+ bpp = channels * depth;
147
+ const passes = [];
148
+ let expected = 0;
149
+ for (const [x0, y0, dx, dy] of interlace ? ADAM7 : [[0, 0, 1, 1]]) {
150
+ const cols = Math.ceil((frame.width - x0) / dx),
151
+ rows = Math.ceil((frame.height - y0) / dy);
152
+ if (cols <= 0 || rows <= 0) continue;
153
+ const stride = Math.ceil((cols * bpp) / 8);
154
+ passes.push({ x0, y0, dx, dy, cols, rows, stride });
155
+ expected += rows * (stride + 1);
156
+ }
157
+ let compressed = chunks[0];
158
+ if (chunks.length > 1) {
159
+ compressed = new Uint8Array(compressedSize);
160
+ let at = 0;
161
+ for (const chunk of chunks) {
162
+ compressed.set(chunk, at);
163
+ at += chunk.length;
164
+ }
165
+ }
166
+ const raw = await inflate(compressed, expected);
167
+ check(raw.length === expected, "unexpected decompressed length");
168
+ const rgba = new Uint8ClampedArray(w * h * 4);
169
+ const max = (1 << depth) - 1;
170
+ const uint8 = depth === 8 ? (v) => v : (v) => Math.round((v * 255) / max);
171
+ const key = [];
172
+ if (trns && type !== 3) {
173
+ for (let i = 0; i < trns.length; i += 2) key.push(trns[i] * 256 + trns[i + 1]);
174
+ }
175
+ let p = 0;
176
+ for (const { x0, y0, dx, dy, cols, rows, stride } of passes) {
177
+ let prev;
178
+ for (let y = 0; y < rows; y++) {
179
+ const filter = raw[p++];
180
+ const row = raw.subarray(p, p + stride);
181
+ p += stride;
182
+ unfilter(row, prev, Math.ceil(bpp / 8), filter);
183
+ prev = row;
184
+ let dst = ((frame.y + y0 + y * dy) * w + frame.x + x0) * 4;
185
+ if (type === 6 && depth === 8 && dx === 1) {
186
+ rgba.set(row, dst);
187
+ continue;
188
+ }
189
+ for (let col = 0, n = 0; col < cols; col++, n += channels, dst += dx * 4) {
190
+ const first = sample(row, n, depth);
191
+ if (type === 3) {
192
+ check(3 * first + 2 < palette.length, "palette index");
193
+ rgba[dst] = palette[3 * first];
194
+ rgba[dst + 1] = palette[3 * first + 1];
195
+ rgba[dst + 2] = palette[3 * first + 2];
196
+ rgba[dst + 3] = trns?.[first] ?? 255;
197
+ } else if (type === 0 || type === 4) {
198
+ rgba[dst] = rgba[dst + 1] = rgba[dst + 2] = uint8(first);
199
+ rgba[dst + 3] = type === 4 ? uint8(sample(row, n + 1, depth)) : first === key[0] ? 0 : 255;
200
+ } else {
201
+ const g = sample(row, n + 1, depth),
202
+ b = sample(row, n + 2, depth);
203
+ rgba[dst] = uint8(first);
204
+ rgba[dst + 1] = uint8(g);
205
+ rgba[dst + 2] = uint8(b);
206
+ rgba[dst + 3] =
207
+ type === 6
208
+ ? uint8(sample(row, n + 3, depth))
209
+ : first === key[0] && g === key[1] && b === key[2]
210
+ ? 0
211
+ : 255;
212
+ }
213
+ }
214
+ }
215
+ }
216
+ // The first animation frame blends onto transparent black. Only fully
217
+ // transparent samples differ between SOURCE and OVER on an empty canvas.
218
+ if (frame.over)
219
+ for (let y = frame.y; y < frame.y + frame.height; y++) {
220
+ for (let x = frame.x; x < frame.x + frame.width; x++) {
221
+ const dst = (y * w + x) * 4;
222
+ if (!rgba[dst + 3]) rgba.fill(0, dst, dst + 3);
223
+ }
224
+ }
225
+ return { w, h, rgba };
226
+ }
@@ -0,0 +1,12 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import decode, { init } from "@jsquash/avif/decode.js";
3
+
4
+ // Resolve the decoder installed by npm without network access. Module
5
+ // initialization is shared by concurrent graphs and only runs on first use.
6
+ const wasmFile = new URL(import.meta.resolve("@jsquash/avif/codec/dec/avif_dec.wasm"));
7
+ await init(await WebAssembly.compile(await readFile(wasmFile)));
8
+
9
+ export async function decodeAvif(bytes) {
10
+ const { width, height, data } = await decode(bytes);
11
+ return { width, height, data };
12
+ }