imagescript 3.5.0 → 3.7.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 CHANGED
@@ -68,8 +68,8 @@ deno add jsr:@matmen/imagescript # Deno (npm:imagescript works too)
68
68
  , lossless and lossy [WebPs](https://imagescript.matmen.dev/classes/Image.html#encodewebp) (lossy ones byte for
69
69
  byte like libwebp's cwebp)
70
70
  , [AVIFs](https://imagescript.matmen.dev/classes/Image.html#encodeavif) (lossy and lossless, with alpha)
71
- , lossless [JPEG XLs](https://imagescript.matmen.dev/classes/Image.html#encodejxl) (byte for byte like libjxl's
72
- `cjxl -e 1`)
71
+ , [JPEG XLs](https://imagescript.matmen.dev/classes/Image.html#encodejxl) (lossless byte for byte like libjxl's
72
+ `cjxl -e 1`, lossy about as small as `cjxl -e 5`)
73
73
  , [BMPs](https://imagescript.matmen.dev/classes/Image.html#encodebmp)
74
74
  , [QOIs](https://imagescript.matmen.dev/classes/Image.html#encodeqoi)
75
75
  , [Netpbm images](https://imagescript.matmen.dev/classes/Image.html#encodepnm) (PBM, PGM, PPM or PAM, whichever is
@@ -129,8 +129,8 @@ the text shaping is ported from [HarfBuzz](https://github.com/harfbuzz/harfbuzz)
129
129
  [Unicode Character Database](https://www.unicode.org/ucd/). The AV1 decoder is ported from
130
130
  [dav1d](https://code.videolan.org/videolan/dav1d), AVIFs are converted to RGB like
131
131
  [libavif](https://github.com/AOMediaCodec/libavif) does, the JPEG XL decoder is ported from
132
- [jxl-oxide](https://github.com/tirr-c/jxl-oxide) and [libjxl](https://github.com/libjxl/libjxl), the JPEG XL encoder
133
- from libjxl's fast lossless encoder, the AV1 encoder mirrors the decoder and uses libaom's symbol encoder.
132
+ [jxl-oxide](https://github.com/tirr-c/jxl-oxide) and [libjxl](https://github.com/libjxl/libjxl), the JPEG XL encoders
133
+ follow libjxl's fast lossless encoder and its lossy heuristics, the AV1 encoder mirrors the decoder and uses libaom's symbol encoder.
134
134
  See [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md) for their licenses.
135
135
 
136
136
  ---
@@ -290,8 +290,9 @@ DEALINGS IN THE SOFTWARE.
290
290
  ## libjxl, JPEG XL conformance
291
291
 
292
292
  The Modular decoding, the rendering pipeline, blending, the XYB conversion and the rendering of patches, splines and
293
- noise follow libjxl, the JPEG XL reference implementation, and the lossless encoder is ported from its fast lossless
294
- encoder; tests/jxl includes files of its conformance suite. JPEG XL
293
+ noise follow libjxl, the JPEG XL reference implementation, the lossless encoder is ported from its fast lossless
294
+ encoder, the lossy encoder follows its heuristics (the varblock search with its adaptive quantization field,
295
+ chroma from luma, quantization, the ANS histograms, fixed trees and clustering, the forward transforms); tests/jxl includes files of its conformance suite. JPEG XL
295
296
  is covered by Google's patent grant for libjxl (the "Additional IP Rights Grant" in libjxl's PATENTS file).
296
297
 
297
298
  ```
@@ -36,7 +36,7 @@ export interface WebPAnimationEncodeOptions extends AnimationEncodeOptions {
36
36
  * frames are lossless
37
37
  */
38
38
  quality?: number;
39
- /** How much effort lossy frames take (0-6), see {@link WebPEncodeOptions.effort}. Default: `4` */
39
+ /** How much effort lossy frames take (0-6), see {@link WebPEncodeOptions.effort}. Default: `2` */
40
40
  effort?: number;
41
41
  }
42
42
  /** Options of {@link Animation.encodeGIF} */
package/dist/animation.js CHANGED
@@ -124,7 +124,7 @@ export class Animation extends Array {
124
124
  : this.__apngFrames__().map(({ blend: _, ...f }) => f);
125
125
  return png.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), compression, pngText(resolveMetadata(metadata)));
126
126
  }
127
- async encodeWebP({ metadata = {}, optimize = true, quality, effort = 4 } = {}) {
127
+ async encodeWebP({ metadata = {}, optimize = true, quality, effort = 2 } = {}) {
128
128
  this.__validate__();
129
129
  if (!this.length)
130
130
  throw new Error('An animation needs at least one frame');
@@ -12,12 +12,11 @@ import { paletteColors, colorRanks, colorIndexBits } from "./palette.js";
12
12
  const ADST_DCT = 1, DCT_ADST = 2, ADST_ADST = 3;
13
13
  const bdim = (bs, i) => BLOCK_DIMENSIONS[bs * 4 + i];
14
14
  const tdim = (tx, i) => TXFM_DIMENSIONS[tx * 8 + i];
15
- const W = 0, H = 1, LW = 2, LH = 3, MIN = 4, MAX = 5, CTX = 7;
15
+ const W = 0, H = 1, LW = 2, LH = 3, MIN = 4, MAX = 5, SUB = 6, CTX = 7;
16
16
  const isDirectional = (mode) => mode >= VERT_PRED && mode <= VERT_LEFT_PRED;
17
17
  const smFlag = (mode) => mode === SMOOTH_PRED || mode === SMOOTH_H_PRED || mode === SMOOTH_V_PRED ? ANGLE_SMOOTH_EDGE : 0;
18
18
  const ZERO_BITS = 0.6;
19
19
  const coefBits = (q) => q === 0 ? ZERO_BITS : q === 1 ? 2.2 : q === 2 ? 3.4 : q < 15 ? 4.6 + Math.log2(q - 2) : 9 + 2 * Math.log2(q - 14);
20
- const SATD_BLOCK = new Int32Array(16);
21
20
  const mostlyFew = (counts, n) => {
22
21
  if (counts.length <= 8)
23
22
  return true;
@@ -63,6 +62,8 @@ class TileEncoder {
63
62
  lPalSzUv = new Uint8Array(32);
64
63
  aPal;
65
64
  lPal = new Uint16Array(32 * 24);
65
+ aTxIntra;
66
+ lTxIntra = new Int8Array(32);
66
67
  cf = new Int32Array(32 * 32);
67
68
  coefs = new Float64Array(32 * 32);
68
69
  q = new Int32Array(32 * 32);
@@ -96,6 +97,7 @@ class TileEncoder {
96
97
  this.aPalSz = new Uint8Array(n);
97
98
  this.aPalSzUv = new Uint8Array(n);
98
99
  this.aPal = new Uint16Array(n * 24);
100
+ this.aTxIntra = new Int8Array(n);
99
101
  this.cdefChoice = new Int8Array(f.cdefIdx.length);
100
102
  this.lossless = this.hdr.allLossless;
101
103
  const q = this.hdr.quant;
@@ -113,6 +115,7 @@ class TileEncoder {
113
115
  this.aCcoef[1].fill(0x40);
114
116
  this.aPalSz.fill(0);
115
117
  this.aPalSzUv.fill(0);
118
+ this.aTxIntra.fill(-1);
116
119
  }
117
120
  resetLeft() {
118
121
  this.lUvmode.fill(DC_PRED);
@@ -124,6 +127,7 @@ class TileEncoder {
124
127
  this.lCcoef[1].fill(0x40);
125
128
  this.lPalSz.fill(0);
126
129
  this.lPalSzUv.fill(0);
130
+ this.lTxIntra.fill(-1);
127
131
  }
128
132
  tile(tileRow, tileCol) {
129
133
  const f = this.f, t = this.hdr.tiles;
@@ -152,14 +156,28 @@ class TileEncoder {
152
156
  }
153
157
  }
154
158
  walk(bl, n) {
159
+ const f = this.f, hsz = 16 >> bl;
155
160
  if (n.bp === PARTITION_NONE) {
156
- const b = n.b, f = this.f;
161
+ const b = n.b;
157
162
  this.storeBlock(BLOCK_SIZES[(bl * 10 + PARTITION_NONE) * 2], b);
158
163
  if (!b.skip)
159
164
  f.cdefIdx[(this.by >> 4) * (f.b4Stride >> 4) + (this.bx >> 4)] = 0;
160
165
  return;
161
166
  }
162
- const hsz = 16 >> bl;
167
+ if (n.bp === PARTITION_H || n.bp === PARTITION_V) {
168
+ const bs = BLOCK_SIZES[(bl * 10 + n.bp) * 2], dx = n.bp === PARTITION_V ? hsz : 0, dy = n.bp === PARTITION_H ? hsz : 0;
169
+ for (let k = 0; k < 2; k++) {
170
+ const b = n.kids[k].b;
171
+ this.storeBlock(bs, b);
172
+ if (!b.skip)
173
+ f.cdefIdx[(this.by >> 4) * (f.b4Stride >> 4) + (this.bx >> 4)] = 0;
174
+ this.bx += dx;
175
+ this.by += dy;
176
+ }
177
+ this.bx -= 2 * dx;
178
+ this.by -= 2 * dy;
179
+ return;
180
+ }
163
181
  for (let k = 0; k < 4; k++) {
164
182
  if (!n.kids[k])
165
183
  continue;
@@ -217,7 +235,8 @@ class TileEncoder {
217
235
  const r = this.searchBlock(bs, node.o);
218
236
  return { node: { bp: PARTITION_NONE, b: r.b, kids: [] }, cost: r.cost };
219
237
  }
220
- const saved = this.saveArea(bl);
238
+ const saved = this.saveArea(bl), entryContexts = this.saveContexts(bs);
239
+ let result, noneCost, splitCost;
221
240
  if (bl <= BL_64X64 + 1) {
222
241
  const sp = split([0, 1, 2, 3]);
223
242
  const deeper = sp.node.kids.filter(k => k && k.bp === PARTITION_SPLIT).length;
@@ -226,24 +245,78 @@ class TileEncoder {
226
245
  const spArea = this.saveArea(bl), contexts = this.saveContexts(bs);
227
246
  this.restoreArea(bl, saved);
228
247
  const none = this.searchBlock(bs, node.o);
229
- if (none.cost + this.lambda < sp.cost + this.lambda * 2)
248
+ noneCost = none.cost + this.lambda;
249
+ splitCost = sp.cost + this.lambda * 2;
250
+ if (noneCost < splitCost)
251
+ result = { node: { bp: PARTITION_NONE, b: none.b, kids: [] }, cost: noneCost };
252
+ else {
253
+ this.restoreArea(bl, spArea);
254
+ this.restoreContexts(contexts);
255
+ result = { node: sp.node, cost: splitCost };
256
+ }
257
+ }
258
+ else {
259
+ const none = this.searchBlock(bs, node.o);
260
+ if ((none.b.skip || none.bits < 30) && this.params.effort < 2)
230
261
  return { node: { bp: PARTITION_NONE, b: none.b, kids: [] }, cost: none.cost + this.lambda };
231
- this.restoreArea(bl, spArea);
232
- this.restoreContexts(contexts);
233
- return { node: sp.node, cost: sp.cost + this.lambda * 2 };
234
- }
235
- const none = this.searchBlock(bs, node.o);
236
- if (none.b.skip && this.params.effort < 2)
237
- return { node: { bp: PARTITION_NONE, b: none.b, kids: [] }, cost: none.cost + this.lambda };
238
- const noneArea = this.saveArea(bl);
239
- this.restoreArea(bl, saved);
240
- const sp = split([0, 1, 2, 3]);
241
- if (none.cost + this.lambda < sp.cost + this.lambda * 2) {
242
- this.restoreArea(bl, noneArea);
243
- this.modeContexts(bs, none.b);
244
- return { node: { bp: PARTITION_NONE, b: none.b, kids: [] }, cost: none.cost + this.lambda };
245
- }
246
- return { node: sp.node, cost: sp.cost + this.lambda * 2 };
262
+ const noneArea = this.saveArea(bl);
263
+ this.restoreArea(bl, saved);
264
+ const sp = split([0, 1, 2, 3]);
265
+ noneCost = none.cost + this.lambda;
266
+ splitCost = sp.cost + this.lambda * 2;
267
+ if (noneCost < splitCost) {
268
+ this.restoreArea(bl, noneArea);
269
+ this.modeContexts(bs, none.b);
270
+ result = { node: { bp: PARTITION_NONE, b: none.b, kids: [] }, cost: noneCost };
271
+ }
272
+ else
273
+ result = { node: sp.node, cost: splitCost };
274
+ }
275
+ const ratio = noneCost / splitCost, deeper = result.node.bp === PARTITION_SPLIT ? result.node.kids.filter(k => k && k.bp !== PARTITION_NONE).length : 0;
276
+ if (this.params.effort && ratio >= 0.9 && ratio < 1.3 && deeper <= 1) {
277
+ let bestArea = this.saveArea(bl), bestContexts = this.saveContexts(bs);
278
+ for (const bp of this.params.effort > 1 ? [PARTITION_H, PARTITION_V] : [this.halvesDirection(bs)]) {
279
+ this.restoreArea(bl, saved);
280
+ this.restoreContexts(entryContexts);
281
+ const r = this.halves(bl, bp, node);
282
+ if (r.cost < result.cost) {
283
+ result = r;
284
+ bestArea = this.saveArea(bl);
285
+ bestContexts = this.saveContexts(bs);
286
+ }
287
+ }
288
+ this.restoreArea(bl, bestArea);
289
+ this.restoreContexts(bestContexts);
290
+ }
291
+ return result;
292
+ }
293
+ halvesDirection(bs) {
294
+ const f = this.f, stride = f.strides[0], src = this.src[0];
295
+ const w = Math.min(bdim(bs, 0), f.bw - this.bx) * 4, h = Math.min(bdim(bs, 1), f.bh - this.by) * 4, o = this.by * 4 * stride + this.bx * 4;
296
+ let top = 0, left = 0, all = 0;
297
+ for (let y = 0; y < h; y++) {
298
+ for (let x = 0; x < w; x++) {
299
+ const v = src[o + y * stride + x];
300
+ all += v;
301
+ if (y < h >> 1)
302
+ top += v;
303
+ if (x < w >> 1)
304
+ left += v;
305
+ }
306
+ }
307
+ return Math.abs(2 * top - all) >= Math.abs(2 * left - all) ? PARTITION_H : PARTITION_V;
308
+ }
309
+ halves(bl, bp, node) {
310
+ const hsz = 16 >> bl, bs = BLOCK_SIZES[(bl * 10 + bp) * 2], edges = bp === PARTITION_H ? node.h : node.v;
311
+ const dx = bp === PARTITION_V ? hsz : 0, dy = bp === PARTITION_H ? hsz : 0;
312
+ const first = this.searchBlock(bs, edges[0]);
313
+ this.bx += dx;
314
+ this.by += dy;
315
+ const second = this.searchBlock(bs, edges[1]);
316
+ this.bx -= dx;
317
+ this.by -= dy;
318
+ const leaf = (b) => ({ bp: PARTITION_NONE, b, kids: [] });
319
+ return { node: { bp, b: null, kids: [leaf(first.b), leaf(second.b)] }, cost: first.cost + second.cost + this.lambda * 3 };
247
320
  }
248
321
  saveArea(bl) {
249
322
  const f = this.f, n = 64 >> (bl - 1);
@@ -319,11 +392,43 @@ class TileEncoder {
319
392
  b.yAngle = bestMode.angle;
320
393
  if (kept)
321
394
  this.restorePlane(0, bs, kept);
395
+ if (this.hdr.txMode === 2 && tdim(b.tx, MAX) > TX_4X4) {
396
+ const modeBits = b.yMode === DC_PRED ? 2 : 4.5, maxDepth = Math.min(tdim(b.tx, MAX), 2);
397
+ let keptDepth = null, bestDepthCost = best + lambda, bestTxSize = b.tx;
398
+ for (let depth = 1, tx = b.tx; depth <= maxDepth; depth++) {
399
+ tx = tdim(tx, SUB);
400
+ if (!keptDepth)
401
+ keptDepth = this.savePlane(0, bs);
402
+ b.tx = tx;
403
+ const d = this.codeLuma(bs, edgeFlags, b);
404
+ const cost = d + lambda * (this.bits + modeBits + 2 + depth);
405
+ if (cost < bestDepthCost) {
406
+ bestDepthCost = best = cost;
407
+ dist = d;
408
+ bits = this.bits + 1;
409
+ coded = this.coded;
410
+ bestTxSize = tx;
411
+ if (depth < maxDepth)
412
+ keptDepth = this.savePlane(0, bs);
413
+ }
414
+ else if (depth === maxDepth && keptDepth)
415
+ this.restorePlane(0, bs, keptDepth);
416
+ }
417
+ b.tx = bestTxSize;
418
+ }
322
419
  if (tdim(b.tx, MAX) <= TX_16X16 && this.params.effort && coded && !this.lossless) {
323
420
  let keptTx = this.savePlane(0, bs), bestTxCost = best + lambda;
421
+ const single = tdim(b.tx, W) >= Math.min(bdim(bs, 0), this.f.bw - this.bx) && tdim(b.tx, H) >= Math.min(bdim(bs, 1), this.f.bh - this.by);
422
+ let prediction = null;
423
+ if (single) {
424
+ this.codeLuma(bs, edgeFlags, b, true);
425
+ prediction = this.savePlane(0, bs);
426
+ }
324
427
  for (const txtp of [ADST_ADST, ADST_DCT, DCT_ADST, IDTX]) {
325
428
  b.txtp = txtp;
326
- const d = this.codeLuma(bs, edgeFlags, b);
429
+ if (prediction)
430
+ this.restorePlane(0, bs, prediction);
431
+ const d = this.codeLuma(bs, edgeFlags, b, false, prediction !== null);
327
432
  const cost = d + lambda * (this.bits + 2.5 + (b.yMode === DC_PRED ? 2 : 4.5));
328
433
  if (cost < bestTxCost) {
329
434
  bestTxCost = cost;
@@ -416,7 +521,7 @@ class TileEncoder {
416
521
  }
417
522
  b.skip = coded ? 0 : 1;
418
523
  this.modeContexts(bs, b);
419
- return { b, cost: dist + lambda * (bits + 1 + 3) };
524
+ return { b, cost: dist + lambda * (bits + 1 + 3), bits };
420
525
  }
421
526
  paletteLuma(bs, edgeFlags, b, cost) {
422
527
  const f = this.f, stride = f.strides[0], src = this.src[0];
@@ -598,26 +703,43 @@ class TileEncoder {
598
703
  satd(p, bs) {
599
704
  const f = this.f, sx = p ? this.ssx : 0, sy = p ? this.ssy : 0, stride = f.strides[p];
600
705
  const w = bdim(bs, 0) * 4 >> sx, h = bdim(bs, 1) * 4 >> sy, x0 = this.bx * 4 >> sx, y0 = this.by * 4 >> sy;
601
- const pred = f.planes[p], src = this.src[p], d = SATD_BLOCK;
706
+ const pred = f.planes[p], src = this.src[p];
602
707
  let sum = 0;
603
708
  for (let by = 0; by < h; by += 4) {
604
709
  for (let bx = 0; bx < w; bx += 4) {
605
- for (let y = 0; y < 4; y++) {
606
- const o = (y0 + by + y) * stride + x0 + bx;
607
- for (let x = 0; x < 4; x++)
608
- d[y * 4 + x] = src[o + x] - pred[o + x];
609
- }
610
- for (let y = 0; y < 4; y++) {
611
- const a = d[y * 4] + d[y * 4 + 1], b = d[y * 4] - d[y * 4 + 1], c = d[y * 4 + 2] + d[y * 4 + 3], e = d[y * 4 + 2] - d[y * 4 + 3];
612
- d[y * 4] = a + c;
613
- d[y * 4 + 1] = b + e;
614
- d[y * 4 + 2] = a - c;
615
- d[y * 4 + 3] = b - e;
616
- }
617
- for (let x = 0; x < 4; x++) {
618
- const a = d[x] + d[4 + x], b = d[x] - d[4 + x], c = d[8 + x] + d[12 + x], e = d[8 + x] - d[12 + x];
619
- sum += Math.abs(a + c) + Math.abs(b + e) + Math.abs(a - c) + Math.abs(b - e);
620
- }
710
+ let o = (y0 + by) * stride + x0 + bx;
711
+ const d00 = src[o] - pred[o], d01 = src[o + 1] - pred[o + 1], d02 = src[o + 2] - pred[o + 2], d03 = src[o + 3] - pred[o + 3];
712
+ o += stride;
713
+ const d10 = src[o] - pred[o], d11 = src[o + 1] - pred[o + 1], d12 = src[o + 2] - pred[o + 2], d13 = src[o + 3] - pred[o + 3];
714
+ o += stride;
715
+ const d20 = src[o] - pred[o], d21 = src[o + 1] - pred[o + 1], d22 = src[o + 2] - pred[o + 2], d23 = src[o + 3] - pred[o + 3];
716
+ o += stride;
717
+ const d30 = src[o] - pred[o], d31 = src[o + 1] - pred[o + 1], d32 = src[o + 2] - pred[o + 2], d33 = src[o + 3] - pred[o + 3];
718
+ const a0 = d00 + d01, b0 = d00 - d01, c0 = d02 + d03, e0 = d02 - d03;
719
+ const r00 = a0 + c0, r01 = b0 + e0, r02 = a0 - c0, r03 = b0 - e0;
720
+ const a1 = d10 + d11, b1 = d10 - d11, c1 = d12 + d13, e1 = d12 - d13;
721
+ const r10 = a1 + c1, r11 = b1 + e1, r12 = a1 - c1, r13 = b1 - e1;
722
+ const a2 = d20 + d21, b2 = d20 - d21, c2 = d22 + d23, e2 = d22 - d23;
723
+ const r20 = a2 + c2, r21 = b2 + e2, r22 = a2 - c2, r23 = b2 - e2;
724
+ const a3 = d30 + d31, b3 = d30 - d31, c3 = d32 + d33, e3 = d32 - d33;
725
+ const r30 = a3 + c3, r31 = b3 + e3, r32 = a3 - c3, r33 = b3 - e3;
726
+ let a = r00 + r10, b = r00 - r10, c = r20 + r30, e = r20 - r30;
727
+ sum += Math.abs(a + c) + Math.abs(b + e) + Math.abs(a - c) + Math.abs(b - e);
728
+ a = r01 + r11;
729
+ b = r01 - r11;
730
+ c = r21 + r31;
731
+ e = r21 - r31;
732
+ sum += Math.abs(a + c) + Math.abs(b + e) + Math.abs(a - c) + Math.abs(b - e);
733
+ a = r02 + r12;
734
+ b = r02 - r12;
735
+ c = r22 + r32;
736
+ e = r22 - r32;
737
+ sum += Math.abs(a + c) + Math.abs(b + e) + Math.abs(a - c) + Math.abs(b - e);
738
+ a = r03 + r13;
739
+ b = r03 - r13;
740
+ c = r23 + r33;
741
+ e = r23 - r33;
742
+ sum += Math.abs(a + c) + Math.abs(b + e) + Math.abs(a - c) + Math.abs(b - e);
621
743
  }
622
744
  }
623
745
  return sum / 4;
@@ -658,7 +780,7 @@ class TileEncoder {
658
780
  predictChroma(bs, edgeFlags, b) {
659
781
  this.codeChroma(bs, edgeFlags, b, true);
660
782
  }
661
- codeLuma(bs, edgeFlags, b, predictOnly = false) {
783
+ codeLuma(bs, edgeFlags, b, predictOnly = false, predicted = false) {
662
784
  const f = this.f, seq = this.seq;
663
785
  const bx = this.bx, by = this.by, bx4 = bx, by4 = by & 31;
664
786
  const bw4 = bdim(bs, 0), bh4 = bdim(bs, 1);
@@ -690,7 +812,7 @@ class TileEncoder {
690
812
  const tx4 = bx + x;
691
813
  const flags = (((y > initY || !sbHasTr) && x + tw >= subW4) ? 0 : EDGE_I444_TOP_HAS_RIGHT) |
692
814
  ((x > initX || (!sbHasBl && y + th >= subH4)) ? 0 : EDGE_I444_LEFT_HAS_BOTTOM);
693
- if (!palIdx) {
815
+ if (!palIdx && !predicted) {
694
816
  const mode = prepareEdges(tx4, tx4 > this.colStart, ty, ty > this.rowStart, this.colEnd, this.rowEnd, flags, pic, d, stride, b.yMode, b.yAngle, tw, th, seq.intraEdgeFilter, f.bitdepth);
695
817
  predict(mode, pic, d, stride, tw * 4, th * 4, preparedAngle | intraFlags, 4 * f.bw - 4 * tx4, 4 * f.bh - 4 * ty, f.bitdepthMax);
696
818
  }
@@ -932,6 +1054,16 @@ class TileEncoder {
932
1054
  w.symbol(cdf, pc, PARTITION_TYPE_COUNT[bl], bp);
933
1055
  if (bp === PARTITION_NONE)
934
1056
  this.writeB(BLOCK_SIZES[(bl * 10 + bp) * 2], node.o, n.b);
1057
+ else if (bp === PARTITION_H || bp === PARTITION_V) {
1058
+ const bs = BLOCK_SIZES[(bl * 10 + bp) * 2], edges = bp === PARTITION_H ? node.h : node.v;
1059
+ const dx = bp === PARTITION_V ? hsz : 0, dy = bp === PARTITION_H ? hsz : 0;
1060
+ this.writeB(bs, edges[0], n.kids[0].b);
1061
+ this.bx += dx;
1062
+ this.by += dy;
1063
+ this.writeB(bs, edges[1], n.kids[1].b);
1064
+ this.bx -= dx;
1065
+ this.by -= dy;
1066
+ }
935
1067
  else
936
1068
  for (let k = 0; k < 4; k++)
937
1069
  quarter(k);
@@ -1011,6 +1143,14 @@ class TileEncoder {
1011
1143
  this.writeColorIndices(b.palIdx, b.palSz[0], 0, w4 * 4, h4 * 4, bw4 * 4);
1012
1144
  if (hasChroma && b.palSz[1])
1013
1145
  this.writeColorIndices(b.palIdxUv, b.palSz[1], 1, (w4 + ssx >> ssx) * 4, (h4 + ssy >> ssy) * 4, cbw4 * 4);
1146
+ const maxTx = MAX_TXFM_SIZE_FOR_BS[bs * 4];
1147
+ if (this.hdr.txMode === 2 && tdim(maxTx, MAX) > TX_4X4) {
1148
+ const tctx = (this.lTxIntra[by4] >= tdim(maxTx, LH) ? 1 : 0) + (this.aTxIntra[bx4] >= tdim(maxTx, LW) ? 1 : 0);
1149
+ let depth = 0;
1150
+ for (let tx = maxTx; tx !== b.tx; tx = tdim(tx, SUB))
1151
+ depth++;
1152
+ w.symbol(cdf, CDF.txSize + ((tdim(maxTx, MAX) - 1) * 3 + tctx) * 4, Math.min(tdim(maxTx, MAX), 2), depth);
1153
+ }
1014
1154
  this.codeLuma(bs, edgeFlags, b);
1015
1155
  if (hasChroma)
1016
1156
  this.codeChroma(bs, edgeFlags, b);
@@ -1022,6 +1162,8 @@ class TileEncoder {
1022
1162
  this.lSkip.fill(b.skip, by4, by4 + lh);
1023
1163
  this.aPalSz.fill(b.palSz[0], bx4, bx4 + aw);
1024
1164
  this.lPalSz.fill(b.palSz[0], by4, by4 + lh);
1165
+ this.aTxIntra.fill(tdim(b.tx, LW), bx4, bx4 + aw);
1166
+ this.lTxIntra.fill(tdim(b.tx, LH), by4, by4 + lh);
1025
1167
  this.aPalSzUv.fill(hasChroma ? b.palSz[1] : 0, bx4, bx4 + aw);
1026
1168
  this.lPalSzUv.fill(hasChroma ? b.palSz[1] : 0, by4, by4 + lh);
1027
1169
  for (let pl = 0; pl < 3; pl++) {
@@ -1347,7 +1489,7 @@ export function encodeAV1(image, params) {
1347
1489
  loopFilter: { levelY: [0, 0], levelU: 0, levelV: 0, sharpness: 0, modeRefDeltaEnabled: false, refDeltas: [1, 0, 0, 0, -1, 0, -1, -1], modeDeltas: [0, 0] },
1348
1490
  cdef: { damping: 3, bits: 0, yStrengths: [0], uvStrengths: [0] },
1349
1491
  restoration: { types: [RESTORATION_NONE, RESTORATION_NONE, RESTORATION_NONE], unitSizeLog2: [8, 8] },
1350
- txMode: qidx ? 1 : 0, reducedTxSet: false, filmGrain: null
1492
+ txMode: qidx ? params.effort > 1 ? 2 : 1 : 0, reducedTxSet: false, filmGrain: null
1351
1493
  };
1352
1494
  const f = new Frame(seq, hdr);
1353
1495
  const src = f.planes.map((plane, p) => {
@@ -149,7 +149,7 @@ function seqMonochrome(d, o) {
149
149
  read(1);
150
150
  return !!read(1);
151
151
  }
152
- export function encode(rgba, width, height, { quality, alphaQuality, subsampling, effort }) {
152
+ export function encode(rgba, width, height, { quality, alphaQuality, subsampling, effort, exif }) {
153
153
  if (width < 1 || height < 1)
154
154
  throw new RangeError('AVIF: dimensions must be at least 1');
155
155
  if (width > 65536 || height > 65536)
@@ -183,11 +183,18 @@ export function encode(rgba, width, height, { quality, alphaQuality, subsampling
183
183
  }
184
184
  const profile = subsampled || gray ? 'MA1B' : 'MA1A';
185
185
  const ftyp = box('ftyp', ascii('avif'), u32(0), ascii('avif'), ascii('mif1'), ascii('miaf'), ascii(profile));
186
- const hdlr = fullBox('hdlr', 0, 0, u32(0), ascii('pict'), u32(0), u32(0), u32(0), ascii('ImageScript'), [0]);
186
+ const hdlr = fullBox('hdlr', 0, 0, u32(0), ascii('pict'), u32(0), u32(0), u32(0), [0]);
187
187
  const pitm = fullBox('pitm', 0, 0, u16(1));
188
- const infe = (id, name) => fullBox('infe', 2, 0, u16(id), u16(0), ascii('av01'), ascii(name), [0]);
189
- const iinf = fullBox('iinf', 0, 0, u16(alpha ? 2 : 1), infe(1, 'Color'), alpha ? infe(2, 'Alpha') : []);
190
- const iref = alpha ? fullBox('iref', 0, 0, box('auxl', u16(2), u16(1), u16(1))) : [];
188
+ const items = [{ type: 'av01', name: 'Color', data: [color] }];
189
+ if (alpha)
190
+ items.push({ type: 'av01', name: 'Alpha', data: [alpha] });
191
+ if (exif?.length)
192
+ items.push({ type: 'Exif', name: 'Exif', data: [u32(0), exif] });
193
+ const sizes = items.map(item => item.data.reduce((n, d) => n + d.length, 0));
194
+ const infe = (id, type, name) => fullBox('infe', 2, 0, u16(id), u16(0), ascii(type), ascii(name), [0]);
195
+ const iinf = fullBox('iinf', 0, 0, u16(items.length), ...items.map((item, i) => infe(i + 1, item.type, item.name)));
196
+ const exifId = exif?.length ? items.length : 0;
197
+ const iref = alpha || exifId ? fullBox('iref', 0, 0, alpha ? box('auxl', u16(2), u16(1), u16(1)) : [], exifId ? box('cdsc', u16(exifId), u16(1), u16(1)) : []) : [];
191
198
  const ispe = fullBox('ispe', 0, 0, u32(width), u32(height));
192
199
  const colr = box('colr', ascii('nclx'), u16(cicp[0]), u16(cicp[1]), u16(cicp[2]), [0x80]);
193
200
  const pixi = (channels) => fullBox('pixi', 0, 0, [channels], new Array(channels).fill(8));
@@ -195,16 +202,23 @@ export function encode(rgba, width, height, { quality, alphaQuality, subsampling
195
202
  const ipco = box('ipco', ispe, av1C(color), colr, pixi(gray ? 1 : 3), alpha ? av1C(alpha) : [], alpha ? pixi(1) : [], alpha ? auxC : []);
196
203
  const ipma = fullBox('ipma', 0, 0, u32(alpha ? 2 : 1), u16(1), [4, 1, 0x82, 3, 4], alpha ? [...u16(2), 4, 1, 0x85, 6, 7] : []);
197
204
  const iprp = box('iprp', ipco, ipma);
198
- const ilocFor = (offset) => fullBox('iloc', 0, 0, [0x44, 0x00], u16(alpha ? 2 : 1), u16(1), u16(0), u16(1), u32(offset), u32(color.length), alpha ? [...u16(2), ...u16(0), ...u16(1), ...u32(offset + color.length), ...u32(alpha.length)] : []);
205
+ const ilocFor = (offset) => fullBox('iloc', 0, 0, [0x44, 0x00], u16(items.length), ...items.map((_, i) => {
206
+ const at = offset + sizes.slice(0, i).reduce((n, size) => n + size, 0);
207
+ return [...u16(i + 1), ...u16(0), ...u16(1), ...u32(at), ...u32(sizes[i])];
208
+ }));
199
209
  const metaFor = (offset) => fullBox('meta', 0, 0, hdlr, pitm, ilocFor(offset), iinf, iref, iprp);
200
210
  const headerSize = ftyp.length + metaFor(0).length + 8;
201
211
  const head = [...ftyp, ...metaFor(headerSize)];
202
- const mdatSize = 8 + color.length + (alpha ? alpha.length : 0);
212
+ const mdatSize = 8 + sizes.reduce((n, size) => n + size, 0);
203
213
  const out = new Uint8Array(head.length + mdatSize);
204
214
  out.set(head);
205
215
  out.set([...u32(mdatSize), ...ascii('mdat')], head.length);
206
- out.set(color, head.length + 8);
207
- if (alpha)
208
- out.set(alpha, head.length + 8 + color.length);
216
+ let at = head.length + 8;
217
+ for (const item of items) {
218
+ for (const d of item.data) {
219
+ out.set(d, at);
220
+ at += d.length;
221
+ }
222
+ }
209
223
  return out;
210
224
  }