imagescript 3.6.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
  ```
@@ -17,7 +17,6 @@ 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;
@@ -258,7 +257,7 @@ class TileEncoder {
258
257
  }
259
258
  else {
260
259
  const none = this.searchBlock(bs, node.o);
261
- if (none.b.skip && this.params.effort < 2)
260
+ if ((none.b.skip || none.bits < 30) && this.params.effort < 2)
262
261
  return { node: { bp: PARTITION_NONE, b: none.b, kids: [] }, cost: none.cost + this.lambda };
263
262
  const noneArea = this.saveArea(bl);
264
263
  this.restoreArea(bl, saved);
@@ -419,9 +418,17 @@ class TileEncoder {
419
418
  }
420
419
  if (tdim(b.tx, MAX) <= TX_16X16 && this.params.effort && coded && !this.lossless) {
421
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
+ }
422
427
  for (const txtp of [ADST_ADST, ADST_DCT, DCT_ADST, IDTX]) {
423
428
  b.txtp = txtp;
424
- 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);
425
432
  const cost = d + lambda * (this.bits + 2.5 + (b.yMode === DC_PRED ? 2 : 4.5));
426
433
  if (cost < bestTxCost) {
427
434
  bestTxCost = cost;
@@ -514,7 +521,7 @@ class TileEncoder {
514
521
  }
515
522
  b.skip = coded ? 0 : 1;
516
523
  this.modeContexts(bs, b);
517
- return { b, cost: dist + lambda * (bits + 1 + 3) };
524
+ return { b, cost: dist + lambda * (bits + 1 + 3), bits };
518
525
  }
519
526
  paletteLuma(bs, edgeFlags, b, cost) {
520
527
  const f = this.f, stride = f.strides[0], src = this.src[0];
@@ -696,26 +703,43 @@ class TileEncoder {
696
703
  satd(p, bs) {
697
704
  const f = this.f, sx = p ? this.ssx : 0, sy = p ? this.ssy : 0, stride = f.strides[p];
698
705
  const w = bdim(bs, 0) * 4 >> sx, h = bdim(bs, 1) * 4 >> sy, x0 = this.bx * 4 >> sx, y0 = this.by * 4 >> sy;
699
- const pred = f.planes[p], src = this.src[p], d = SATD_BLOCK;
706
+ const pred = f.planes[p], src = this.src[p];
700
707
  let sum = 0;
701
708
  for (let by = 0; by < h; by += 4) {
702
709
  for (let bx = 0; bx < w; bx += 4) {
703
- for (let y = 0; y < 4; y++) {
704
- const o = (y0 + by + y) * stride + x0 + bx;
705
- for (let x = 0; x < 4; x++)
706
- d[y * 4 + x] = src[o + x] - pred[o + x];
707
- }
708
- for (let y = 0; y < 4; y++) {
709
- 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];
710
- d[y * 4] = a + c;
711
- d[y * 4 + 1] = b + e;
712
- d[y * 4 + 2] = a - c;
713
- d[y * 4 + 3] = b - e;
714
- }
715
- for (let x = 0; x < 4; x++) {
716
- 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];
717
- sum += Math.abs(a + c) + Math.abs(b + e) + Math.abs(a - c) + Math.abs(b - e);
718
- }
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);
719
743
  }
720
744
  }
721
745
  return sum / 4;
@@ -756,7 +780,7 @@ class TileEncoder {
756
780
  predictChroma(bs, edgeFlags, b) {
757
781
  this.codeChroma(bs, edgeFlags, b, true);
758
782
  }
759
- codeLuma(bs, edgeFlags, b, predictOnly = false) {
783
+ codeLuma(bs, edgeFlags, b, predictOnly = false, predicted = false) {
760
784
  const f = this.f, seq = this.seq;
761
785
  const bx = this.bx, by = this.by, bx4 = bx, by4 = by & 31;
762
786
  const bw4 = bdim(bs, 0), bh4 = bdim(bs, 1);
@@ -788,7 +812,7 @@ class TileEncoder {
788
812
  const tx4 = bx + x;
789
813
  const flags = (((y > initY || !sbHasTr) && x + tw >= subW4) ? 0 : EDGE_I444_TOP_HAS_RIGHT) |
790
814
  ((x > initX || (!sbHasBl && y + th >= subH4)) ? 0 : EDGE_I444_LEFT_HAS_BOTTOM);
791
- if (!palIdx) {
815
+ if (!palIdx && !predicted) {
792
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);
793
817
  predict(mode, pic, d, stride, tw * 4, th * 4, preparedAngle | intraFlags, 4 * f.bw - 4 * tx4, 4 * f.bh - 4 * ty, f.bitdepthMax);
794
818
  }
@@ -0,0 +1,294 @@
1
+ import { forwardTransform } from "./transforms-enc.js";
2
+ import { inverseTransform } from "./dct.js";
3
+ import { defaultDequant, MATRIX_INDEX } from "./quant.js";
4
+ import { SIZES, transposed } from "./vardct.js";
5
+ const DCT8 = 0, IDENTITY = 1, DCT2X2 = 2, DCT4X4 = 3, DCT16X16 = 4, DCT32X32 = 5, DCT16X8 = 6, DCT8X16 = 7;
6
+ const DCT32X16 = 10, DCT16X32 = 11, DCT4X8 = 12, DCT8X4 = 13, DCT64X64 = 18, DCT64X32 = 19, DCT32X64 = 20;
7
+ const AFV0 = 14, AFV1 = 15, AFV2 = 16, AFV3 = 17;
8
+ const ceilLog2 = (x) => x <= 1 ? 0 : 32 - Math.clz32(x - 1);
9
+ const CHANNEL_MUL = [10.2 ** 8, 1, 1.03 ** 8];
10
+ const scratch = [0, 1, 2].map(() => new Float32Array(64 * 64)), errors = new Float32Array(64 * 64), lfScratch = new Float32Array(64);
11
+ const dequant = (type) => defaultDequant(MATRIX_INDEX[type])[transposed(type) ? 1 : 0];
12
+ const inverses = new Map();
13
+ function inverseDequant(type) {
14
+ let inv = inverses.get(type);
15
+ if (!inv)
16
+ inverses.set(type, inv = dequant(type).map(m => Float32Array.from(m, v => 1 / v)));
17
+ return inv;
18
+ }
19
+ function estimateEntropy(type, entropyMul, x, y, cfg) {
20
+ const w8 = SIZES[2 * type], h8 = SIZES[2 * type + 1], w = w8 * 8, h = h8 * 8, numBlocks = w8 * h8, size = w * h;
21
+ for (let c = 0; c < 3; c++)
22
+ forwardTransform(type, cfg.planes[c], y * cfg.stride + x, cfg.stride, scratch[c], lfScratch);
23
+ const qf = cfg.field.quant, bw = cfg.bw, bx = x >> 3, by = y >> 3;
24
+ let quant = 0;
25
+ if (numBlocks === 1)
26
+ quant = qf[by * bw + bx];
27
+ else if (numBlocks === 2)
28
+ quant = h8 === 2 ? Math.max(qf[by * bw + bx], qf[(by + 1) * bw + bx]) : Math.max(qf[by * bw + bx], qf[by * bw + bx + 1]);
29
+ else {
30
+ for (let iy = 0; iy < h8; iy++) {
31
+ for (let ix = 0; ix < w8; ix++) {
32
+ const v = qf[(by + iy) * bw + bx + ix] ** 8;
33
+ quant += v * v;
34
+ }
35
+ }
36
+ quant = (quant / numBlocks) ** (1 / 16);
37
+ }
38
+ const matrices = dequant(type), inverses = inverseDequant(type);
39
+ const mask = cfg.field.mask1x1, stride = cfg.stride;
40
+ let entropy = 0, loss = 0;
41
+ for (let c = 0; c < 3; c++) {
42
+ const src = scratch[c], ys = scratch[1], m = matrices[c], im = inverses[c], cmap = c === 2 ? 1 : 0;
43
+ let sqrtSum = 0, nonZeros = 0;
44
+ for (let i = 0; i < size; i++) {
45
+ const v = (src[i] - ys[i] * cmap) * im[i] * quant, r = Math.round(v);
46
+ errors[i] = m[i] * (v - r);
47
+ const q = Math.abs(r);
48
+ sqrtSum += Math.sqrt(q);
49
+ if (q)
50
+ nonZeros++;
51
+ }
52
+ inverseTransform({ buf: errors, offset: 0, stride: w, w, h }, type);
53
+ let lossc = 0;
54
+ for (let py = 0; py < h; py++) {
55
+ const mo = (y + py) * stride + x;
56
+ for (let px = 0; px < w; px++) {
57
+ const e = Math.abs(mask[mo + px]) * errors[py * w + px], e2 = e * e, e4 = e2 * e2;
58
+ lossc += e4 * e4;
59
+ }
60
+ }
61
+ loss += CHANNEL_MUL[c] * lossc;
62
+ entropy += cfg.costDelta * sqrtSum;
63
+ const nbits = ceilLog2(nonZeros + 1) + 1;
64
+ entropy += cfg.zerosMul * (ceilLog2(nbits + 17) + nbits);
65
+ }
66
+ const lossScalar = (loss / (numBlocks * 64)) ** (1 / 8) * (numBlocks * 64) / quant;
67
+ return entropy * entropyMul + cfg.infoLossMul * lossScalar;
68
+ }
69
+ const TRANSFORMS_8X8 = [
70
+ [DCT8, 9, 0.8], [DCT4X4, 5, 1.08], [DCT2X2, 5, 0.95], [DCT4X8, 4, 0.85931637428340035], [DCT8X4, 4, 0.85931637428340035],
71
+ [IDENTITY, 5, 1.0427542510634957], [AFV0, 4, 0.81779489591359944], [AFV1, 4, 0.81779489591359944], [AFV2, 4, 0.81779489591359944],
72
+ [AFV3, 4, 0.81779489591359944]
73
+ ];
74
+ function best8x8(x, y, speed, cfg) {
75
+ let best = Infinity, bestType = DCT8;
76
+ const d = cfg.distance;
77
+ for (const [type, limit, mul] of TRANSFORMS_8X8) {
78
+ if (limit < speed)
79
+ continue;
80
+ let entropyMul = mul / TRANSFORMS_8X8[0][2];
81
+ if ((type === DCT2X2 || type === IDENTITY) && d < 5)
82
+ entropyMul -= 0.4 * ((5 - d) / 5) ** 2;
83
+ if (type !== DCT8 && type !== DCT2X2 && type !== IDENTITY && d > 4)
84
+ entropyMul += 0.5 * (d < 12 ? (12 - 4) / (d - 4) : 1);
85
+ const e = estimateEntropy(type, entropyMul, x, y, cfg);
86
+ if (e < best) {
87
+ best = e;
88
+ bestType = type;
89
+ }
90
+ }
91
+ return [bestType, best];
92
+ }
93
+ class Strategy {
94
+ bw;
95
+ bh;
96
+ types;
97
+ constructor(bw, bh) {
98
+ this.bw = bw;
99
+ this.bh = bh;
100
+ this.types = new Int8Array(bw * bh);
101
+ }
102
+ set(bx, by, type) {
103
+ const w8 = SIZES[2 * type], h8 = SIZES[2 * type + 1];
104
+ for (let y = 0; y < h8; y++)
105
+ for (let x = 0; x < w8; x++)
106
+ this.types[(by + y) * this.bw + bx + x] = -1;
107
+ this.types[by * this.bw + bx] = type;
108
+ }
109
+ type(bx, by) {
110
+ return this.types[by * this.bw + bx];
111
+ }
112
+ crossesHorizontal(x0, y, x1) {
113
+ if (x0 >= this.bw || y >= this.bh || y % 8 === 0)
114
+ return false;
115
+ x1 = Math.min(x1, this.bw);
116
+ const limit = x0 & ~7;
117
+ while (x0 !== limit && this.type(x0, y) < 0)
118
+ x0--;
119
+ for (let x = x0; x < x1;) {
120
+ const t = this.type(x, y);
121
+ if (t < 0)
122
+ return true;
123
+ x += SIZES[2 * t];
124
+ }
125
+ return false;
126
+ }
127
+ crossesVertical(x, y0, y1) {
128
+ if (x >= this.bw || y0 >= this.bh || x % 8 === 0)
129
+ return false;
130
+ y1 = Math.min(y1, this.bh);
131
+ const limit = y0 & ~7;
132
+ while (y0 !== limit && this.type(x, y0) < 0)
133
+ y0--;
134
+ for (let y = y0; y < y1;) {
135
+ const t = this.type(x, y);
136
+ if (t < 0)
137
+ return true;
138
+ y += SIZES[2 * t + 1];
139
+ }
140
+ return false;
141
+ }
142
+ }
143
+ const square = (blocks) => blocks === 2 ? DCT16X16 : blocks === 4 ? DCT32X32 : DCT64X64;
144
+ const verticalSplit = (blocks) => blocks === 2 ? DCT16X8 : blocks === 4 ? DCT32X16 : DCT64X32;
145
+ const horizontalSplit = (blocks) => blocks === 2 ? DCT8X16 : blocks === 4 ? DCT16X32 : DCT32X64;
146
+ function bestDivision(blocks, allowSquare, bx, by, cx, cy, cfg, s, mulJxK, mulJxJ, est) {
147
+ const half = blocks / 2, jxk = verticalSplit(blocks), kxj = horizontalSplit(blocks), jxj = square(blocks);
148
+ const x = bx + cx, y = by + cy;
149
+ if (s.crossesHorizontal(x, y, x + blocks) || s.crossesHorizontal(x, y + blocks, x + blocks) || s.crossesVertical(x, y, y + blocks) || s.crossesVertical(x + blocks, y, y + blocks))
150
+ return;
151
+ const allowJxK = !s.crossesVertical(x + half, y, y + blocks), allowKxJ = !s.crossesHorizontal(x, y + half, x + blocks);
152
+ const e = [[0, 0], [0, 0]];
153
+ for (let dy = 0; dy < blocks; dy++)
154
+ for (let dx = 0; dx < blocks; dx++)
155
+ e[Math.floor(dy / half)][Math.floor(dx / half)] += est[(cy + dy) * 8 + cx + dx];
156
+ let jxkLeft = Infinity, jxkRight = Infinity, kxjTop = Infinity, kxjBottom = Infinity, jxjCost = Infinity;
157
+ if (allowJxK) {
158
+ if (s.type(x, y) !== jxk)
159
+ jxkLeft = estimateEntropy(jxk, mulJxK, x * 8, y * 8, cfg);
160
+ if (s.type(x + half, y) !== jxk)
161
+ jxkRight = estimateEntropy(jxk, mulJxK, (x + half) * 8, y * 8, cfg);
162
+ }
163
+ if (allowKxJ) {
164
+ if (s.type(x, y) !== kxj)
165
+ kxjTop = estimateEntropy(kxj, mulJxK, x * 8, y * 8, cfg);
166
+ if (s.type(x, y + half) !== kxj)
167
+ kxjBottom = estimateEntropy(kxj, mulJxK, x * 8, (y + half) * 8, cfg);
168
+ }
169
+ if (allowSquare)
170
+ jxjCost = estimateEntropy(jxj, mulJxJ, x * 8, y * 8, cfg);
171
+ const costJxN = Math.min(jxkLeft, e[0][0] + e[1][0]) + Math.min(jxkRight, e[0][1] + e[1][1]);
172
+ const costNxJ = Math.min(kxjTop, e[0][0] + e[0][1]) + Math.min(kxjBottom, e[1][0] + e[1][1]);
173
+ const setEntropy = (ex, ey, type, entropy) => {
174
+ for (let dy = 0; dy < SIZES[2 * type + 1]; dy++)
175
+ for (let dx = 0; dx < SIZES[2 * type]; dx++)
176
+ est[(ey + dy) * 8 + ex + dx] = 0;
177
+ est[ey * 8 + ex] = entropy;
178
+ };
179
+ if (jxjCost < costJxN && jxjCost < costNxJ) {
180
+ s.set(x, y, jxj);
181
+ setEntropy(cx, cy, jxj, jxjCost);
182
+ }
183
+ else if (costJxN < costNxJ) {
184
+ if (jxkLeft < e[0][0] + e[1][0]) {
185
+ s.set(x, y, jxk);
186
+ setEntropy(cx, cy, jxk, jxkLeft);
187
+ }
188
+ if (jxkRight < e[0][1] + e[1][1]) {
189
+ s.set(x + half, y, jxk);
190
+ setEntropy(cx + half, cy, jxk, jxkRight);
191
+ }
192
+ }
193
+ else {
194
+ if (kxjTop < e[0][0] + e[0][1]) {
195
+ s.set(x, y, kxj);
196
+ setEntropy(cx, cy, kxj, kxjTop);
197
+ }
198
+ if (kxjBottom < e[1][0] + e[1][1]) {
199
+ s.set(x, y + half, kxj);
200
+ setEntropy(cx, cy + half, kxj, kxjBottom);
201
+ }
202
+ }
203
+ }
204
+ function tryMerge(type, bx, by, cx, cy, cfg, s, mul, prio, priority, est) {
205
+ const w8 = SIZES[2 * type], h8 = SIZES[2 * type + 1];
206
+ let current = 0;
207
+ for (let iy = 0; iy < h8; iy++) {
208
+ for (let ix = 0; ix < w8; ix++) {
209
+ if (priority[(cy + iy) * 8 + cx + ix] >= prio)
210
+ return;
211
+ current += est[(cy + iy) * 8 + cx + ix];
212
+ }
213
+ }
214
+ const candidate = estimateEntropy(type, mul, (bx + cx) * 8, (by + cy) * 8, cfg);
215
+ if (candidate >= current)
216
+ return;
217
+ for (let iy = 0; iy < h8; iy++) {
218
+ for (let ix = 0; ix < w8; ix++) {
219
+ est[(cy + iy) * 8 + cx + ix] = 0;
220
+ priority[(cy + iy) * 8 + cx + ix] = prio;
221
+ }
222
+ }
223
+ s.set(bx + cx, by + cy, type);
224
+ est[cy * 8 + cx] = candidate;
225
+ }
226
+ const MERGES = [
227
+ [DCT16X8, 2, 1.25], [DCT8X16, 2, 1.25], [DCT16X32, 4, 1.5], [DCT32X16, 4, 1.5], [DCT64X32, 6, 2.26], [DCT32X64, 6, 2.26]
228
+ ];
229
+ function processRect(bx, by, w, h, speed, cfg, s) {
230
+ const est = new Float32Array(64), priority = new Uint8Array(64);
231
+ const mul8x8 = 1 - 0.4 / (cfg.distance + 1.4);
232
+ for (let iy = 0; iy < h; iy++) {
233
+ for (let ix = 0; ix < w; ix++) {
234
+ const [type, entropy] = best8x8(8 * (bx + ix), 8 * (by + iy), speed, cfg);
235
+ s.set(bx + ix, by + iy, type);
236
+ est[iy * 8 + ix] = entropy * mul8x8;
237
+ }
238
+ }
239
+ for (const [type, prio, mul] of MERGES) {
240
+ const w8 = SIZES[2 * type], h8 = SIZES[2 * type + 1];
241
+ for (let cy = 0; cy + h8 - 1 < h; cy += h8) {
242
+ for (let cx = 0; cx + w8 - 1 < w; cx += w8) {
243
+ if (cy + 7 < h && cx + 7 < w) {
244
+ if (type === DCT32X64) {
245
+ if ((cy | cx) % 8 === 0)
246
+ bestDivision(8, true, bx, by, cx, cy, cfg, s, mul, 2.26, est);
247
+ continue;
248
+ }
249
+ else if (type === DCT64X32)
250
+ continue;
251
+ }
252
+ if ((type === DCT16X32 && cy % 4 !== 0) || (type === DCT32X16 && cx % 4 !== 0))
253
+ continue;
254
+ if (cy + 3 < h && cx + 3 < w) {
255
+ if (type === DCT16X32) {
256
+ if ((cy | cx) % 4 === 0)
257
+ bestDivision(4, true, bx, by, cx, cy, cfg, s, mul, 1.5, est);
258
+ continue;
259
+ }
260
+ else if (type === DCT32X16)
261
+ continue;
262
+ }
263
+ if ((type === DCT16X32 && cy % 4 !== 0) || (type === DCT32X16 && cx % 4 !== 0))
264
+ continue;
265
+ if (cy + 1 < h && cx + 1 < w) {
266
+ if (type === DCT8X16) {
267
+ if ((cy | cx) % 2 === 0)
268
+ bestDivision(2, true, bx, by, cx, cy, cfg, s, mul, 1.35, est);
269
+ continue;
270
+ }
271
+ else if (type === DCT16X8)
272
+ continue;
273
+ }
274
+ if ((type === DCT8X16 && cy % 2 === 1) || (type === DCT16X8 && cx % 2 === 1))
275
+ continue;
276
+ tryMerge(type, bx, by, cx, cy, cfg, s, mul, prio, priority, est);
277
+ }
278
+ }
279
+ }
280
+ }
281
+ export function chooseVarblocks(planes, stride, bw, bh, field, distance, speed) {
282
+ const ratio = (distance + 0.13731742964354549) / (1 + 0.13731742964354549);
283
+ const cfg = {
284
+ planes, stride, bw, field, distance,
285
+ infoLossMul: 1.2 * ratio ** 0.33677806662454718,
286
+ zerosMul: 9.3089059022677905 * ratio ** 0.50990926717963703,
287
+ costDelta: 10.833273317067883 * ratio ** 0.36702940662370243
288
+ };
289
+ const s = new Strategy(bw, bh);
290
+ for (let by = 0; by < bh; by += 8)
291
+ for (let bx = 0; bx < bw; bx += 8)
292
+ processRect(bx, by, Math.min(8, bw - bx), Math.min(8, bh - by), speed, cfg, s);
293
+ return s.types;
294
+ }
@@ -0,0 +1,176 @@
1
+ /*!
2
+ * The adaptive quantization field of the lossy JPEG XL encoder, for its varblock search: libjxl's initial quantization
3
+ * field and masking (lib/jxl/enc_adaptive_quantization.cc: InitialQuantField, AdaptiveQuantizationMap, FuzzyErosion,
4
+ * PerBlockModulations, Blur1x1Masking), on the whole image instead of tiles. Copyright (c) the JPEG XL Project Authors, BSD-3-Clause
5
+ * License (see THIRD_PARTY_NOTICES.md)
6
+ */
7
+ const SG_MUL = 226.77216153508914, SG_MUL2 = 1 / 73.377132366608819, LOG2 = 0.693147181;
8
+ const SG_RET_MUL = SG_MUL2 * 18.6580932135 * LOG2, SG_V_OFFSET = 7.7825991679894591;
9
+ const NUM_MUL = SG_RET_MUL * 3 * SG_MUL, V_OFFSET = SG_V_OFFSET * LOG2 + 1e-2, DEN_MUL = LOG2 * SG_MUL;
10
+ const gammaRatio = (v) => {
11
+ v = Math.max(v, 0);
12
+ const v2 = v * v;
13
+ return (DEN_MUL * v * v2 + V_OFFSET) / (NUM_MUL * v2 + 1e-2);
14
+ };
15
+ const gammaRatioInverse = (v) => {
16
+ v = Math.max(v, 0);
17
+ const v2 = v * v;
18
+ return (NUM_MUL * v2 + 1e-2) / (DEN_MUL * v * v2 + V_OFFSET);
19
+ };
20
+ function computeMask(v) {
21
+ const v1 = Math.max(v * 0.80061762862741759, 1e-3), offset3 = 3.7179635626140772;
22
+ return -0.7647 + 9.4708735624378946 / (v1 * v1 + 0.25 * offset3) + 17.35036561631863 / (v1 + 302.59587815579727) + 6.7943250517376494 / (v1 * v1 + offset3);
23
+ }
24
+ const maskingSqrt = (v) => 0.25 * Math.sqrt(v * Math.sqrt(211.66567973503678 * 1e8) + 27.505837037000106);
25
+ export function initialQuantField(distance, planes, w, h, scale) {
26
+ const [px, py, pb] = planes, bw = w / 8, bh = h / 8;
27
+ const MATCH_GAMMA_OFFSET = 0.019, LIMIT = 0.2;
28
+ const mask1x1 = new Float32Array(w * h), preErosion = new Float32Array((w / 4) * (h / 4)), rowSum = new Float32Array(w);
29
+ for (let y = 0; y < h; y++) {
30
+ const r = y * w, r1 = (y > 0 ? y - 1 : y) * w, r2 = (y + 1 < h ? y + 1 : y) * w;
31
+ for (let x = 0; x < w; x++) {
32
+ const x1 = x > 0 ? x - 1 : x, x2 = x + 1 < w ? x + 1 : x;
33
+ const base = 0.25 * (py[r2 + x] + py[r1 + x] + py[r + x1] + py[r + x2]);
34
+ const gammac = gammaRatio(py[r + x] + MATCH_GAMMA_OFFSET);
35
+ const d = gammac * (py[r + x] - base);
36
+ mask1x1[r + x] = 1 / (Math.log1p(Math.abs(d)) + 0.01);
37
+ const v = maskingSqrt(Math.min(d * d, LIMIT));
38
+ rowSum[x] = y % 4 ? rowSum[x] + v : v;
39
+ }
40
+ if (y % 4 === 3) {
41
+ const out = (y >> 2) * (w / 4);
42
+ for (let x = 0; x < w / 4; x++)
43
+ preErosion[out + x] = (rowSum[4 * x] + rowSum[4 * x + 1] + rowSum[4 * x + 2] + rowSum[4 * x + 3]) * 0.25;
44
+ }
45
+ }
46
+ const aq = fuzzyErosion(distance, preErosion, w / 4, h / 4);
47
+ const mask = Float32Array.from(aq, v => 1 / (v + 0.001));
48
+ perBlockModulations(distance, px, py, pb, w, bw, bh, scale, aq);
49
+ return { quant: aq, mask, mask1x1: blur1x1(mask1x1, w, h) };
50
+ }
51
+ function fuzzyErosion(distance, from, w, h) {
52
+ const mul = distance < 2 ? (2 - distance) / 2 : 0;
53
+ let k0 = 0.125, k1 = 0.10 - 0.10 * mul, k2 = 0.09 - 0.09 * mul, k3 = 0.06 - 0.06 * mul;
54
+ const norm = 0.29959705784054957 / (k0 + k1 + k2 + k3);
55
+ k0 *= norm;
56
+ k1 *= norm;
57
+ k2 *= norm;
58
+ k3 *= norm;
59
+ const out = new Float32Array((w / 2) * (h / 2)), ow = w / 2;
60
+ const v = new Float64Array(9);
61
+ for (let y = 0; y < h; y++) {
62
+ const t = (y > 0 ? y - 1 : y) * w, r = y * w, b = (y + 1 < h ? y + 1 : y) * w;
63
+ for (let x = 0; x < w; x++) {
64
+ const xm = x > 0 ? x - 1 : x, xp = x + 1 < w ? x + 1 : x;
65
+ v[0] = from[r + x];
66
+ v[1] = from[r + xm];
67
+ v[2] = from[r + xp];
68
+ v[3] = from[t + xm];
69
+ v[4] = from[t + x];
70
+ v[5] = from[t + xp];
71
+ v[6] = from[b + xm];
72
+ v[7] = from[b + x];
73
+ v[8] = from[b + xp];
74
+ let m0 = Infinity, m1 = Infinity, m2 = Infinity, m3 = Infinity;
75
+ for (let i = 0; i < 9; i++) {
76
+ const a = v[i];
77
+ if (a >= m3)
78
+ continue;
79
+ if (a < m0) {
80
+ m3 = m2;
81
+ m2 = m1;
82
+ m1 = m0;
83
+ m0 = a;
84
+ }
85
+ else if (a < m1) {
86
+ m3 = m2;
87
+ m2 = m1;
88
+ m1 = a;
89
+ }
90
+ else if (a < m2) {
91
+ m3 = m2;
92
+ m2 = a;
93
+ }
94
+ else
95
+ m3 = a;
96
+ }
97
+ const val = k0 * m0 + k1 * m1 + k2 * m2 + k3 * m3, o = (y >> 1) * ow + (x >> 1);
98
+ out[o] = x % 2 === 0 && y % 2 === 0 ? val : out[o] + val;
99
+ }
100
+ }
101
+ return out;
102
+ }
103
+ function perBlockModulations(distance, px, py, pb, w, bw, bh, scale, aq) {
104
+ const baseLevel = 0.48 * scale;
105
+ let dampen = 1;
106
+ if (distance >= 2)
107
+ dampen = Math.max(0, 1 - (distance - 2) / (14 - 2));
108
+ const mul = scale * dampen, add = (1 - dampen) * baseLevel;
109
+ for (let by = 0; by < bh; by++) {
110
+ for (let bx = 0; bx < bw; bx++) {
111
+ const o = by * 8 * w + bx * 8;
112
+ let v = computeMask(aq[by * bw + bx]);
113
+ let hf = 0;
114
+ for (let dy = 0; dy < 8; dy++) {
115
+ const r = o + dy * w, next = dy === 7 ? r : r + w;
116
+ for (let dx = 0; dx < 8; dx++) {
117
+ const p = py[r + dx];
118
+ if (dx < 7)
119
+ hf += Math.min(0.0206, Math.abs(p - py[r + dx + 1]));
120
+ hf += Math.min(0.0206, Math.abs(p - py[next + dx]));
121
+ }
122
+ }
123
+ v += hf * -0.38 + 0.42;
124
+ let ratio = 0;
125
+ for (let dy = 0; dy < 8; dy++) {
126
+ for (let dx = 0; dx < 8; dx++) {
127
+ const iny = py[o + dy * w + dx] + 0.16, inx = px[o + dy * w + dx];
128
+ ratio += gammaRatioInverse(iny - inx) + gammaRatioInverse(iny + inx);
129
+ }
130
+ }
131
+ v += 0.1005613337192697 * Math.log2(ratio * (0.5 / 64));
132
+ const LIMIT = 0.027121074570634722;
133
+ let blue = 0;
134
+ for (let dy = 0; dy < 8; dy++) {
135
+ for (let dx = 0; dx < 8; dx++) {
136
+ const i = o + dy * w + dx, eff = py[i] + 0.084381641171960495 + Math.abs(px[i]);
137
+ if (pb[i] > eff)
138
+ blue += Math.min(pb[i] - eff, LIMIT);
139
+ }
140
+ }
141
+ if (blue >= 32 * LIMIT)
142
+ blue = 64 * LIMIT - blue;
143
+ if (blue >= 15.398788439047934 * LIMIT)
144
+ blue = 15.398788439047934 * LIMIT;
145
+ v += blue * 0.14207000358439159;
146
+ aq[by * bw + bx] = Math.exp(v) * mul + add;
147
+ }
148
+ }
149
+ }
150
+ function symmetric5(m, w, h, [c, r, R, d, D, L]) {
151
+ const weights = [D, L, R, L, D, L, d, r, d, L, R, r, c, r, R, L, d, r, d, L, D, L, R, L, D];
152
+ const mirror = (v, n) => {
153
+ while (v < 0 || v >= n)
154
+ v = v < 0 ? -v - 1 : 2 * n - 1 - v;
155
+ return v;
156
+ };
157
+ const out = new Float32Array(w * h);
158
+ for (let y = 0; y < h; y++) {
159
+ const rows = [-2, -1, 0, 1, 2].map(dy => mirror(y + dy, h) * w);
160
+ for (let x = 0; x < w; x++) {
161
+ let sum = 0;
162
+ for (let dx = -2; dx <= 2; dx++) {
163
+ const col = x + dx >= 0 && x + dx < w ? x + dx : mirror(x + dx, w);
164
+ for (let k = 0; k < 5; k++)
165
+ sum += weights[k * 5 + dx + 2] * m[rows[k] + col];
166
+ }
167
+ out[y * w + x] = sum;
168
+ }
169
+ }
170
+ return out;
171
+ }
172
+ function blur1x1(m, w, h) {
173
+ const k = [0.25647067633737225, 0.2050056912354399, 0.1540820486684973, 0.08149576591362004, 0.05127501048123085];
174
+ const n = 1 / Math.max(1 + 4 * (k[0] + k[1] + k[2] + k[4] + 2 * k[3]), 1e-5);
175
+ return symmetric5(m, w, h, [n, n * k[0], n * k[2], n * k[1], n * k[4], n * k[3]]);
176
+ }