imagescript 3.4.0 → 3.6.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 +5 -1
- package/THIRD_PARTY_NOTICES.md +40 -3
- package/dist/animation.d.ts +1 -1
- package/dist/animation.js +1 -1
- package/dist/codecs/av1/cdef.js +17 -3
- package/dist/codecs/av1/encode.js +1630 -0
- package/dist/codecs/av1/fdct-lee.js +156 -0
- package/dist/codecs/av1/fdct.js +185 -0
- package/dist/codecs/av1/itx.js +15 -0
- package/dist/codecs/av1/msac-enc.js +126 -0
- package/dist/codecs/av1/obu-enc.js +153 -0
- package/dist/codecs/av1/palette.js +110 -0
- package/dist/codecs/avif-enc.js +224 -0
- package/dist/codecs/jxl/color.js +28 -10
- package/dist/codecs/jxl/dct.js +3 -38
- package/dist/codecs/jxl/entropy.js +27 -23
- package/dist/codecs/jxl/fast-lossless.js +805 -0
- package/dist/codecs/jxl/filters.js +66 -32
- package/dist/codecs/jxl/modular.js +69 -42
- package/dist/codecs/jxl.js +13 -0
- package/dist/codecs/png.js +29 -26
- package/dist/codecs/vp8-enc-dsp.js +20 -13
- package/dist/codecs/vp8-enc.js +35 -14
- package/dist/image.d.ts +29 -3
- package/dist/image.js +9 -1
- package/dist/metadata.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -5,10 +5,14 @@ export function gaborish(channels, w, h, weights) {
|
|
|
5
5
|
const out = new Float32Array(w * h);
|
|
6
6
|
for (let y = 0; y < h; y++) {
|
|
7
7
|
const t = mirror(y - 1, h) * w, m = y * w, b = mirror(y + 1, h) * w;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const at = (x, l, r) => (src[m + x] + (src[t + x] + src[b + x] + src[m + l] + src[m + r]) * w0 + (src[t + l] + src[t + r] + src[b + l] + src[b + r]) * w1) * norm;
|
|
9
|
+
out[m] = at(0, mirror(-1, w), mirror(1, w));
|
|
10
|
+
for (let x = 1; x < w - 1; x++) {
|
|
11
|
+
out[m + x] = (src[m + x] + (src[t + x] + src[b + x] + src[m + x - 1] + src[m + x + 1]) * w0 +
|
|
12
|
+
(src[t + x - 1] + src[t + x + 1] + src[b + x - 1] + src[b + x + 1]) * w1) * norm;
|
|
11
13
|
}
|
|
14
|
+
if (w > 1)
|
|
15
|
+
out[m + w - 1] = at(w - 1, w - 2, mirror(w, w));
|
|
12
16
|
}
|
|
13
17
|
return out;
|
|
14
18
|
});
|
|
@@ -41,43 +45,73 @@ function outputRow(cur, out, y, w, sums, p0, pw, u, rows, xs, sigma, sigmaRow, s
|
|
|
41
45
|
const [i0, i1, i2] = cur, [o0, o1, o2] = out, n = u.length / 2, yBorder = ((y + 1) & 6) === 0;
|
|
42
46
|
const innerRow = rows[0] === (y - 2) * w && rows[4] === (y + 2) * w;
|
|
43
47
|
const row = y * w;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
48
|
+
const sumOff = TAPS.subarray(0, n), srcOff = TAPS.subarray(8, 8 + n);
|
|
49
|
+
for (let k = 0; k < n; k++) {
|
|
50
|
+
sumOff[k] = u[2 * k + 1] * pw + u[2 * k];
|
|
51
|
+
srcOff[k] = u[2 * k + 1] * w + u[2 * k];
|
|
52
|
+
}
|
|
53
|
+
for (let x0 = 0; x0 < w; x0 += 8) {
|
|
54
|
+
const x1 = Math.min(x0 + 8, w);
|
|
55
|
+
const sg = sigma ? sigma[sigmaRow + (x0 >> 3)] : sigmaModular;
|
|
47
56
|
if (sg < 0.3) {
|
|
48
|
-
o0
|
|
49
|
-
o1
|
|
50
|
-
o2
|
|
57
|
+
o0.set(i0.subarray(row + x0, row + x1), row + x0);
|
|
58
|
+
o1.set(i1.subarray(row + x0, row + x1), row + x0);
|
|
59
|
+
o2.set(i2.subarray(row + x0, row + x1), row + x0);
|
|
51
60
|
continue;
|
|
52
61
|
}
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
const borderInvSigma = 6.6 * (Math.SQRT1_2 - 1) / sg * (stepMul * borderMul), innerInvSigma = 6.6 * (Math.SQRT1_2 - 1) / sg * stepMul;
|
|
63
|
+
for (let x = x0; x < x1; x++) {
|
|
64
|
+
const i = row + x;
|
|
65
|
+
const negInvSigma = yBorder || (x & 7) === 0 || (x & 7) === 7 ? borderInvSigma : innerInvSigma;
|
|
66
|
+
let sumW = 1, sum0 = i0[i], sum1 = i1[i], sum2 = i2[i];
|
|
67
|
+
const p = p0 + x;
|
|
68
|
+
if (innerRow && x >= 2 && x < w - 2) {
|
|
69
|
+
for (let k = 0; k < n; k++) {
|
|
70
|
+
const sum = sums[k], so = sumOff[k], to = srcOff[k];
|
|
71
|
+
let weight = 1 + sum[p] * negInvSigma;
|
|
72
|
+
if (weight > 0) {
|
|
73
|
+
sumW += weight;
|
|
74
|
+
sum0 += weight * i0[i + to];
|
|
75
|
+
sum1 += weight * i1[i + to];
|
|
76
|
+
sum2 += weight * i2[i + to];
|
|
77
|
+
}
|
|
78
|
+
weight = 1 + sum[p - so] * negInvSigma;
|
|
79
|
+
if (weight > 0) {
|
|
80
|
+
sumW += weight;
|
|
81
|
+
sum0 += weight * i0[i - to];
|
|
82
|
+
sum1 += weight * i1[i - to];
|
|
83
|
+
sum2 += weight * i2[i - to];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
66
86
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
87
|
+
else {
|
|
88
|
+
for (let k = 0; k < n; k++) {
|
|
89
|
+
const ux = u[2 * k], uy = u[2 * k + 1], sum = sums[k];
|
|
90
|
+
let weight = 1 + sum[p] * negInvSigma;
|
|
91
|
+
if (weight > 0) {
|
|
92
|
+
const t = rows[2 + uy] + xs[x + 5 + ux];
|
|
93
|
+
sumW += weight;
|
|
94
|
+
sum0 += weight * i0[t];
|
|
95
|
+
sum1 += weight * i1[t];
|
|
96
|
+
sum2 += weight * i2[t];
|
|
97
|
+
}
|
|
98
|
+
weight = 1 + sum[p - uy * pw - ux] * negInvSigma;
|
|
99
|
+
if (weight > 0) {
|
|
100
|
+
const t = rows[2 - uy] + xs[x + 5 - ux];
|
|
101
|
+
sumW += weight;
|
|
102
|
+
sum0 += weight * i0[t];
|
|
103
|
+
sum1 += weight * i1[t];
|
|
104
|
+
sum2 += weight * i2[t];
|
|
105
|
+
}
|
|
106
|
+
}
|
|
74
107
|
}
|
|
108
|
+
o0[i] = sum0 / sumW;
|
|
109
|
+
o1[i] = sum1 / sumW;
|
|
110
|
+
o2[i] = sum2 / sumW;
|
|
75
111
|
}
|
|
76
|
-
o0[i] = sum0 / sumW;
|
|
77
|
-
o1[i] = sum1 / sumW;
|
|
78
|
-
o2[i] = sum2 / sumW;
|
|
79
112
|
}
|
|
80
113
|
}
|
|
114
|
+
const TAPS = new Int32Array(16);
|
|
81
115
|
export function epf(channels, w, h, e, sigma, bw) {
|
|
82
116
|
let cur = channels;
|
|
83
117
|
const steps = e.iters === 3 ? [0, 1, 2] : e.iters === 2 ? [1, 2] : [1];
|
|
@@ -13,31 +13,43 @@ function mulShift24(a, b) {
|
|
|
13
13
|
return Math.floor(a * b / 16777216);
|
|
14
14
|
return Number((BigInt(a) * BigInt(b)) >> 24n);
|
|
15
15
|
}
|
|
16
|
+
function weight(w, e, posN, posNE, posNW) {
|
|
17
|
+
const sum = (e[posN] + e[posNE] + e[posNW]) >>> 0;
|
|
18
|
+
const shift = sum === 0xffffffff ? 27 : Math.max(floorLog2(sum + 1) - 5, 0);
|
|
19
|
+
return 4 + ((w * DIV_LOOKUP[sum >>> shift]) >>> shift);
|
|
20
|
+
}
|
|
16
21
|
class WeightedPredictor {
|
|
17
22
|
header;
|
|
18
23
|
xsize;
|
|
19
|
-
|
|
24
|
+
e0;
|
|
25
|
+
e1;
|
|
26
|
+
e2;
|
|
27
|
+
e3;
|
|
20
28
|
error;
|
|
21
|
-
|
|
29
|
+
w0;
|
|
30
|
+
w1;
|
|
31
|
+
w2;
|
|
32
|
+
w3;
|
|
33
|
+
p0 = 0;
|
|
34
|
+
p1 = 0;
|
|
35
|
+
p2 = 0;
|
|
36
|
+
p3 = 0;
|
|
22
37
|
pred = 0;
|
|
23
38
|
maxError = 0;
|
|
24
39
|
constructor(header, xsize) {
|
|
25
40
|
this.header = header;
|
|
26
41
|
this.xsize = xsize;
|
|
27
|
-
|
|
28
|
-
this.
|
|
42
|
+
const n = (xsize + 2) * 2;
|
|
43
|
+
[this.e0, this.e1, this.e2, this.e3] = [0, 1, 2, 3].map(() => new Uint32Array(n));
|
|
44
|
+
this.error = new Int32Array(n);
|
|
45
|
+
[this.w0, this.w1, this.w2, this.w3] = header.w;
|
|
29
46
|
}
|
|
30
47
|
predict(x, y, N, W, NE, NW, NN) {
|
|
31
48
|
const h = this.header, xsize = this.xsize;
|
|
32
49
|
const cur = y & 1 ? 0 : xsize + 2, prev = y & 1 ? xsize + 2 : 0;
|
|
33
50
|
const posN = prev + x, posNE = x < xsize - 1 ? posN + 1 : posN, posNW = x > 0 ? posN - 1 : posN;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const e = this.predErrors[i];
|
|
37
|
-
const sum = (e[posN] + e[posNE] + e[posNW]) >>> 0;
|
|
38
|
-
const shift = Math.max((sum === 0xffffffff ? 32 : floorLog2(sum + 1)) - 5, 0);
|
|
39
|
-
weights[i] = 4 + ((h.w[i] * DIV_LOOKUP[Math.floor(sum / 2 ** shift)]) >>> shift);
|
|
40
|
-
}
|
|
51
|
+
let w0 = weight(this.w0, this.e0, posN, posNE, posNW), w1 = weight(this.w1, this.e1, posN, posNE, posNW);
|
|
52
|
+
let w2 = weight(this.w2, this.e2, posN, posNE, posNW), w3 = weight(this.w3, this.e3, posN, posNE, posNW);
|
|
41
53
|
N *= 8;
|
|
42
54
|
W *= 8;
|
|
43
55
|
NE *= 8;
|
|
@@ -53,35 +65,41 @@ class WeightedPredictor {
|
|
|
53
65
|
if (Math.abs(teNE) > Math.abs(p))
|
|
54
66
|
p = teNE;
|
|
55
67
|
this.maxError = p;
|
|
56
|
-
const sumWN = teN + teW
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
sum += pr[i] * weights[i];
|
|
68
|
+
const sumWN = teN + teW;
|
|
69
|
+
const p0 = this.p0 = W + NE - N;
|
|
70
|
+
const p1 = this.p1 = N - Math.floor((sumWN + teNE) * h.p1 / 32);
|
|
71
|
+
const p2 = this.p2 = W - Math.floor((sumWN + teNW) * h.p2 / 32);
|
|
72
|
+
const p3 = this.p3 = N - Math.floor((teNW * h.p3a + teN * h.p3b + teNE * h.p3c + (NN - N) * h.p3d + (NW - W) * h.p3e) / 32);
|
|
73
|
+
const logWeight = floorLog2(w0 + w1 + w2 + w3) - 4;
|
|
74
|
+
w0 >>= logWeight;
|
|
75
|
+
w1 >>= logWeight;
|
|
76
|
+
w2 >>= logWeight;
|
|
77
|
+
w3 >>= logWeight;
|
|
78
|
+
const weightSum = w0 + w1 + w2 + w3;
|
|
79
|
+
const sum = (weightSum >> 1) - 1 + p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3;
|
|
69
80
|
let pred = mulShift24(sum, DIV_LOOKUP[weightSum - 1]);
|
|
70
|
-
if (((teN ^ teW) | (teN ^ teNW)) <= 0)
|
|
71
|
-
|
|
81
|
+
if (((teN ^ teW) | (teN ^ teNW)) <= 0) {
|
|
82
|
+
const lo = W < NE ? (W < N ? W : N) : (NE < N ? NE : N), hi = W > NE ? (W > N ? W : N) : (NE > N ? NE : N);
|
|
83
|
+
pred = pred < lo ? lo : pred > hi ? hi : pred;
|
|
84
|
+
}
|
|
72
85
|
this.pred = pred;
|
|
73
86
|
return Math.floor((pred + 3) / 8);
|
|
74
87
|
}
|
|
75
88
|
update(value, x, y) {
|
|
76
|
-
const xsize = this.xsize, cur = y & 1 ? 0 : xsize + 2,
|
|
89
|
+
const xsize = this.xsize, cur = y & 1 ? 0 : xsize + 2, i = cur + x, j = (y & 1 ? xsize + 2 : 0) + x + 1;
|
|
77
90
|
const val = value * 8;
|
|
78
|
-
this.error[
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
91
|
+
this.error[i] = this.pred - val;
|
|
92
|
+
const e0 = Math.floor((Math.abs(this.p0 - val) + 3) / 8) >>> 0, e1 = Math.floor((Math.abs(this.p1 - val) + 3) / 8) >>> 0;
|
|
93
|
+
const e2 = Math.floor((Math.abs(this.p2 - val) + 3) / 8) >>> 0, e3 = Math.floor((Math.abs(this.p3 - val) + 3) / 8) >>> 0;
|
|
94
|
+
const E0 = this.e0, E1 = this.e1, E2 = this.e2, E3 = this.e3;
|
|
95
|
+
E0[i] = e0;
|
|
96
|
+
E0[j] = (E0[j] + e0) >>> 0;
|
|
97
|
+
E1[i] = e1;
|
|
98
|
+
E1[j] = (E1[j] + e1) >>> 0;
|
|
99
|
+
E2[i] = e2;
|
|
100
|
+
E2[j] = (E2[j] + e2) >>> 0;
|
|
101
|
+
E3[i] = e3;
|
|
102
|
+
E3[j] = (E3[j] + e3) >>> 0;
|
|
85
103
|
}
|
|
86
104
|
}
|
|
87
105
|
export const clampedGradient = (n, w, l) => {
|
|
@@ -591,6 +609,21 @@ export function decodeModular(r, image, stream, global, maxChanSize, undo) {
|
|
|
591
609
|
if (undo)
|
|
592
610
|
image.undo();
|
|
593
611
|
}
|
|
612
|
+
function decodeZero(r, dec, cluster, data, mult, offset, distMultiplier) {
|
|
613
|
+
for (let i = 0; i < data.length; i++)
|
|
614
|
+
data[i] = (Math.imul(unpackSigned(dec.readClustered(r, cluster, distMultiplier)), mult) + offset) | 0;
|
|
615
|
+
}
|
|
616
|
+
function decodeGradient(r, dec, cluster, data, w, h, distMultiplier) {
|
|
617
|
+
for (let x = 0; x < w; x++)
|
|
618
|
+
data[x] = (unpackSigned(dec.readClustered(r, cluster, distMultiplier)) + (x ? data[x - 1] : 0)) | 0;
|
|
619
|
+
for (let y = 1, i = w; y < h; y++) {
|
|
620
|
+
data[i] = (unpackSigned(dec.readClustered(r, cluster, distMultiplier)) + data[i - w]) | 0;
|
|
621
|
+
i++;
|
|
622
|
+
for (let x = 1; x < w; x++, i++) {
|
|
623
|
+
data[i] = (unpackSigned(dec.readClustered(r, cluster, distMultiplier)) + clampedGradient(data[i - w], data[i - 1], data[i - w - 1])) | 0;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
}
|
|
594
627
|
function decodeChannel(r, dec, globalTree, image, chan, stream, distMultiplier) {
|
|
595
628
|
const c = image.channels[chan], { w, h } = c, data = c.data;
|
|
596
629
|
const t = specialize(globalTree, chan, stream);
|
|
@@ -614,17 +647,11 @@ function decodeChannel(r, dec, globalTree, image, chan, stream, distMultiplier)
|
|
|
614
647
|
data.fill((Math.imul(unpackSigned(single), mult) + offset) | 0);
|
|
615
648
|
return;
|
|
616
649
|
}
|
|
617
|
-
|
|
618
|
-
data[i] = (Math.imul(unpackSigned(dec.readClustered(r, cluster, distMultiplier)), mult) + offset) | 0;
|
|
650
|
+
decodeZero(r, dec, cluster, data, mult, offset, distMultiplier);
|
|
619
651
|
return;
|
|
620
652
|
}
|
|
621
653
|
if (pred === 5 && mult === 1 && offset === 0) {
|
|
622
|
-
|
|
623
|
-
for (let x = 0; x < w; x++, i++) {
|
|
624
|
-
const left = x ? data[i - 1] : y ? data[i - w] : 0, top = y ? data[i - w] : left, topleft = x && y ? data[i - w - 1] : left;
|
|
625
|
-
data[i] = (unpackSigned(dec.readClustered(r, cluster, distMultiplier)) + clampedGradient(top, left, topleft)) | 0;
|
|
626
|
-
}
|
|
627
|
-
}
|
|
654
|
+
decodeGradient(r, dec, cluster, data, w, h, distMultiplier);
|
|
628
655
|
return;
|
|
629
656
|
}
|
|
630
657
|
}
|
package/dist/codecs/jxl.js
CHANGED
|
@@ -4,12 +4,25 @@ import { imageHeader, skipICC } from "./jxl/headers.js";
|
|
|
4
4
|
import { frameHeader, toc } from "./jxl/frame-header.js";
|
|
5
5
|
import { decodeFrame } from "./jxl/frame.js";
|
|
6
6
|
import { ColorOutput } from "./jxl/color.js";
|
|
7
|
+
import { encodeRGBA } from "./jxl/fast-lossless.js";
|
|
8
|
+
import { concat } from "../util/mem.js";
|
|
7
9
|
const CONTAINER = [0, 0, 0, 12, 0x4a, 0x58, 0x4c, 0x20, 0x0d, 0x0a, 0x87, 0x0a];
|
|
8
10
|
export function isJXL(d) {
|
|
9
11
|
if (d.length >= 2 && d[0] === 0xff && d[1] === 0x0a)
|
|
10
12
|
return true;
|
|
11
13
|
return d.length >= 12 && CONTAINER.every((v, i) => d[i] === v);
|
|
12
14
|
}
|
|
15
|
+
export function encode(rgba, width, height, exif) {
|
|
16
|
+
const stream = encodeRGBA(rgba, width, height);
|
|
17
|
+
if (!exif?.length)
|
|
18
|
+
return stream;
|
|
19
|
+
const jxl = [0x6a, 0x78, 0x6c, 0x20];
|
|
20
|
+
return concat([CONTAINER, ...box('ftyp', jxl, [0, 0, 0, 0], jxl), ...box('Exif', [0, 0, 0, 0], exif), ...box('jxlc', stream)]);
|
|
21
|
+
}
|
|
22
|
+
const box = (type, ...content) => {
|
|
23
|
+
const size = 8 + content.reduce((n, c) => n + c.length, 0);
|
|
24
|
+
return [[size >>> 24, size >> 16 & 255, size >> 8 & 255, size & 255, ...Array.from(type, c => c.charCodeAt(0))], ...content];
|
|
25
|
+
};
|
|
13
26
|
const fourcc = (d, o) => String.fromCharCode(d[o], d[o + 1], d[o + 2], d[o + 3]);
|
|
14
27
|
function codestream(d) {
|
|
15
28
|
if (d[0] === 0xff)
|
package/dist/codecs/png.js
CHANGED
|
@@ -22,15 +22,6 @@ const CHANNELS_TO_COLOR_TYPE = { 1: 0, 2: 4, 3: 2, 4: 6 };
|
|
|
22
22
|
const VALID_DEPTHS = { 0: [1, 2, 4, 8, 16], 2: [8, 16], 3: [1, 2, 4, 8], 4: [8, 16], 6: [8, 16] };
|
|
23
23
|
const ADAM7 = [[0, 0, 8, 8], [4, 0, 8, 8], [0, 4, 4, 8], [2, 0, 4, 4], [0, 2, 2, 4], [1, 0, 2, 2], [0, 1, 1, 2]];
|
|
24
24
|
export async function encode(data, { text, width, height, channels, depth = 8, level = 0, palette, transparency, filter: filterRows = false }) {
|
|
25
|
-
const rowBytes = Math.ceil(width * channels * depth / 8);
|
|
26
|
-
let filtered;
|
|
27
|
-
if (filterRows)
|
|
28
|
-
filtered = filter(data, rowBytes, height, Math.max(1, channels * depth / 8));
|
|
29
|
-
else {
|
|
30
|
-
filtered = new Uint8Array(height * (rowBytes + 1));
|
|
31
|
-
for (let y = 0; y < height; y++)
|
|
32
|
-
filtered.set(data.subarray(y * rowBytes, (y + 1) * rowBytes), y * (rowBytes + 1) + 1);
|
|
33
|
-
}
|
|
34
25
|
const header = new Uint8Array(13);
|
|
35
26
|
const view = new DataView(header.buffer);
|
|
36
27
|
view.setUint32(0, width);
|
|
@@ -43,7 +34,7 @@ export async function encode(data, { text, width, height, channels, depth = 8, l
|
|
|
43
34
|
if (transparency)
|
|
44
35
|
chunks.push(chunk(tRNS, transparency));
|
|
45
36
|
chunks.push(...textChunks(text));
|
|
46
|
-
chunks.push(chunk(IDAT, await
|
|
37
|
+
chunks.push(chunk(IDAT, await compressRows(data, width, height, channels, depth, level, filterRows)));
|
|
47
38
|
chunks.push(chunk(IEND, new Uint8Array(0)));
|
|
48
39
|
return concat([HEAD, ...chunks]);
|
|
49
40
|
}
|
|
@@ -61,10 +52,10 @@ function textChunks(text) {
|
|
|
61
52
|
}
|
|
62
53
|
return chunks;
|
|
63
54
|
}
|
|
64
|
-
function filter(data, rowBytes,
|
|
65
|
-
const out = new Uint8Array(
|
|
66
|
-
for (let y =
|
|
67
|
-
const row = y * rowBytes, o = y * (rowBytes + 1) + 1;
|
|
55
|
+
function filter(data, rowBytes, y0, y1, bpp) {
|
|
56
|
+
const out = new Uint8Array((y1 - y0) * (rowBytes + 1));
|
|
57
|
+
for (let y = y0; y < y1; y++) {
|
|
58
|
+
const row = y * rowBytes, o = (y - y0) * (rowBytes + 1) + 1;
|
|
68
59
|
out[o - 1] = 4;
|
|
69
60
|
if (y === 0) {
|
|
70
61
|
for (let i = 0; i < rowBytes; i++)
|
|
@@ -82,6 +73,30 @@ function filter(data, rowBytes, height, bpp) {
|
|
|
82
73
|
}
|
|
83
74
|
return out;
|
|
84
75
|
}
|
|
76
|
+
function prefix(data, rowBytes, y0, y1) {
|
|
77
|
+
const out = new Uint8Array((y1 - y0) * (rowBytes + 1));
|
|
78
|
+
for (let y = y0; y < y1; y++)
|
|
79
|
+
out.set(data.subarray(y * rowBytes, (y + 1) * rowBytes), (y - y0) * (rowBytes + 1) + 1);
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
const SAMPLE_ROWS = 32, SAMPLE_STEP = 512;
|
|
83
|
+
async function compressRows(data, width, height, channels, depth, level, filterRows) {
|
|
84
|
+
const rowBytes = Math.ceil(width * channels * depth / 8), bpp = Math.max(1, channels * depth / 8);
|
|
85
|
+
if (!filterRows)
|
|
86
|
+
return compressAsync(prefix(data, rowBytes, 0, height), level);
|
|
87
|
+
if (height <= SAMPLE_ROWS) {
|
|
88
|
+
const [unfiltered, filtered] = await Promise.all([compressAsync(prefix(data, rowBytes, 0, height), level), compressAsync(filter(data, rowBytes, 0, height, bpp), level)]);
|
|
89
|
+
return unfiltered.length < filtered.length ? unfiltered : filtered;
|
|
90
|
+
}
|
|
91
|
+
const unfilteredSamples = [], filteredSamples = [];
|
|
92
|
+
for (let y = Math.min((SAMPLE_STEP - SAMPLE_ROWS) >> 1, (height - SAMPLE_ROWS) >> 1); y < height; y += SAMPLE_STEP) {
|
|
93
|
+
const end = Math.min(height, y + SAMPLE_ROWS);
|
|
94
|
+
unfilteredSamples.push(prefix(data, rowBytes, y, end));
|
|
95
|
+
filteredSamples.push(filter(data, rowBytes, y, end, bpp));
|
|
96
|
+
}
|
|
97
|
+
const [unfiltered, filtered] = await Promise.all([compressAsync(concat(unfilteredSamples), level), compressAsync(concat(filteredSamples), level)]);
|
|
98
|
+
return compressAsync(unfiltered.length < filtered.length ? prefix(data, rowBytes, 0, height) : filter(data, rowBytes, 0, height, bpp), level);
|
|
99
|
+
}
|
|
85
100
|
function chunk(type, body) {
|
|
86
101
|
const out = new Uint8Array(body.length + 12);
|
|
87
102
|
const view = new DataView(out.buffer);
|
|
@@ -211,18 +226,6 @@ export async function encodeRGBA(rgba, width, height, level, text) {
|
|
|
211
226
|
const { channels, depth, palette, transparency } = format;
|
|
212
227
|
return encode(format.convert(rgba, width, height), { width, height, level, text, channels, depth, palette, transparency, filter: level > 0 && !palette });
|
|
213
228
|
}
|
|
214
|
-
async function compressRows(data, width, height, channels, depth, level, filterRows) {
|
|
215
|
-
const rowBytes = Math.ceil(width * channels * depth / 8);
|
|
216
|
-
let filtered;
|
|
217
|
-
if (filterRows)
|
|
218
|
-
filtered = filter(data, rowBytes, height, Math.max(1, channels * depth / 8));
|
|
219
|
-
else {
|
|
220
|
-
filtered = new Uint8Array(height * (rowBytes + 1));
|
|
221
|
-
for (let y = 0; y < height; y++)
|
|
222
|
-
filtered.set(data.subarray(y * rowBytes, (y + 1) * rowBytes), y * (rowBytes + 1) + 1);
|
|
223
|
-
}
|
|
224
|
-
return compressAsync(filtered, level);
|
|
225
|
-
}
|
|
226
229
|
export async function encodeAnimation(frames, width, height, plays, level, text) {
|
|
227
230
|
if (!frames.length)
|
|
228
231
|
throw new Error('PNG: an animation needs at least one frame');
|
|
@@ -817,19 +817,26 @@ export function sse16(a, ao, b, bo, h) {
|
|
|
817
817
|
return count;
|
|
818
818
|
}
|
|
819
819
|
export function tTransform(input, i, w) {
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
820
|
+
const r00 = input[i], r01 = input[i + 1], r02 = input[i + 2], r03 = input[i + 3];
|
|
821
|
+
const a00 = r00 + r02, a01 = r01 + r03, a02 = r01 - r03, a03 = r00 - r02;
|
|
822
|
+
const h00 = a00 + a01, h01 = a03 + a02, h02 = a03 - a02, h03 = a00 - a01;
|
|
823
|
+
const r10 = input[i + 1 * BPS], r11 = input[i + 1 * BPS + 1], r12 = input[i + 1 * BPS + 2], r13 = input[i + 1 * BPS + 3];
|
|
824
|
+
const a10 = r10 + r12, a11 = r11 + r13, a12 = r11 - r13, a13 = r10 - r12;
|
|
825
|
+
const h10 = a10 + a11, h11 = a13 + a12, h12 = a13 - a12, h13 = a10 - a11;
|
|
826
|
+
const r20 = input[i + 2 * BPS], r21 = input[i + 2 * BPS + 1], r22 = input[i + 2 * BPS + 2], r23 = input[i + 2 * BPS + 3];
|
|
827
|
+
const a20 = r20 + r22, a21 = r21 + r23, a22 = r21 - r23, a23 = r20 - r22;
|
|
828
|
+
const h20 = a20 + a21, h21 = a23 + a22, h22 = a23 - a22, h23 = a20 - a21;
|
|
829
|
+
const r30 = input[i + 3 * BPS], r31 = input[i + 3 * BPS + 1], r32 = input[i + 3 * BPS + 2], r33 = input[i + 3 * BPS + 3];
|
|
830
|
+
const a30 = r30 + r32, a31 = r31 + r33, a32 = r31 - r33, a33 = r30 - r32;
|
|
831
|
+
const h30 = a30 + a31, h31 = a33 + a32, h32 = a33 - a32, h33 = a30 - a31;
|
|
832
|
+
const b00 = h00 + h20, b01 = h10 + h30, b02 = h10 - h30, b03 = h00 - h20;
|
|
833
|
+
const b10 = h01 + h21, b11 = h11 + h31, b12 = h11 - h31, b13 = h01 - h21;
|
|
834
|
+
const b20 = h02 + h22, b21 = h12 + h32, b22 = h12 - h32, b23 = h02 - h22;
|
|
835
|
+
const b30 = h03 + h23, b31 = h13 + h33, b32 = h13 - h33, b33 = h03 - h23;
|
|
836
|
+
return w[0] * Math.abs(b00 + b01) + w[4] * Math.abs(b03 + b02) + w[8] * Math.abs(b03 - b02) + w[12] * Math.abs(b00 - b01) +
|
|
837
|
+
w[1] * Math.abs(b10 + b11) + w[5] * Math.abs(b13 + b12) + w[9] * Math.abs(b13 - b12) + w[13] * Math.abs(b10 - b11) +
|
|
838
|
+
w[2] * Math.abs(b20 + b21) + w[6] * Math.abs(b23 + b22) + w[10] * Math.abs(b23 - b22) + w[14] * Math.abs(b20 - b21) +
|
|
839
|
+
w[3] * Math.abs(b30 + b31) + w[7] * Math.abs(b33 + b32) + w[11] * Math.abs(b33 - b32) + w[15] * Math.abs(b30 - b31);
|
|
833
840
|
}
|
|
834
841
|
export function tDisto4x4(source, b, bo, w) {
|
|
835
842
|
return Math.abs(tTransform(b, bo, w) - source) >> 5;
|
package/dist/codecs/vp8-enc.js
CHANGED
|
@@ -1146,7 +1146,17 @@ function reconstructIntra4(it, levels, lo, src, s, out, o, mode) {
|
|
|
1146
1146
|
fTransform(src, s, it.yuvP, r, tmpLevels, 0);
|
|
1147
1147
|
const nz = it.doTrellis ? trellisQuantize(it.enc, tmpLevels, 0, levels, lo, it.topNz[it.i4 & 3] + it.leftNz[it.i4 >> 2], TYPE_I4_AC, dqm.y1, dqm.lambdaTrellisI4)
|
|
1148
1148
|
: quantizeBlock(tmpLevels, 0, levels, lo, dqm.y1) ? 1 : 0;
|
|
1149
|
-
|
|
1149
|
+
if (nz)
|
|
1150
|
+
iTransform(it.yuvP, r, tmpLevels, 0, out, o, false);
|
|
1151
|
+
else {
|
|
1152
|
+
const p = it.yuvP;
|
|
1153
|
+
for (let k = 0; k < 4 * BPS; k += BPS) {
|
|
1154
|
+
out[o + k] = p[r + k];
|
|
1155
|
+
out[o + k + 1] = p[r + k + 1];
|
|
1156
|
+
out[o + k + 2] = p[r + k + 2];
|
|
1157
|
+
out[o + k + 3] = p[r + k + 3];
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1150
1160
|
return nz;
|
|
1151
1161
|
}
|
|
1152
1162
|
function quantizeSingle(v, i, m) {
|
|
@@ -1219,16 +1229,19 @@ function pickBestIntra16(it, rd) {
|
|
|
1219
1229
|
rdCur.modeI16 = mode;
|
|
1220
1230
|
rdCur.nz = reconstructIntra16(it, rdCur, dst, Y_OFF, mode);
|
|
1221
1231
|
rdCur.D = sse16(it.yuvIn, Y_OFF, dst, Y_OFF, 16);
|
|
1222
|
-
rdCur.SD = tlambda ? (tlambda * tDisto16x16(sourceSpectra, dst, Y_OFF, WEIGHT_Y) + 128) >> 8 : 0;
|
|
1223
1232
|
rdCur.H = FIXED_COSTS_I16[mode];
|
|
1224
|
-
|
|
1225
|
-
if (flat) {
|
|
1233
|
+
if (flat)
|
|
1226
1234
|
flat = isFlat(rdCur.yAcLevels, 0, 16, FLATNESS_LIMIT_I16);
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1235
|
+
if (flat)
|
|
1236
|
+
rdCur.D *= 2;
|
|
1237
|
+
rdCur.SD = rdCur.R = 0;
|
|
1238
|
+
rdCur.setRDScore(lambda);
|
|
1239
|
+
if (mode > 0 && rdCur.score >= rdBest.score)
|
|
1240
|
+
continue;
|
|
1241
|
+
rdCur.SD = tlambda ? (tlambda * tDisto16x16(sourceSpectra, dst, Y_OFF, WEIGHT_Y) + 128) >> 8 : 0;
|
|
1242
|
+
if (flat)
|
|
1243
|
+
rdCur.SD *= 2;
|
|
1244
|
+
rdCur.R = costLuma16(it, rdCur);
|
|
1232
1245
|
rdCur.setRDScore(lambda);
|
|
1233
1246
|
if (mode === 0 || rdCur.score < rdBest.score) {
|
|
1234
1247
|
const swap = rdCur;
|
|
@@ -1277,11 +1290,17 @@ function pickBestIntra4(it, rd) {
|
|
|
1277
1290
|
const spectrum = tlambda ? tTransform(it.yuvIn, src, WEIGHT_Y) : 0;
|
|
1278
1291
|
for (let mode = 0; mode < 10; mode++) {
|
|
1279
1292
|
const r = rdTmpI4;
|
|
1293
|
+
r.H = costs[modeCosts + mode];
|
|
1294
|
+
if (bestMode >= 0 && r.H * lambda >= rdI4.score)
|
|
1295
|
+
continue;
|
|
1280
1296
|
r.nz = reconstructIntra4(it, tmpLevels2, 0, it.yuvIn, src, tmpDst, td, mode) << it.i4;
|
|
1281
1297
|
r.D = sse4x4(it.yuvIn, src, tmpDst, td);
|
|
1282
|
-
r.SD = tlambda ? (tlambda * tDisto4x4(spectrum, tmpDst, td, WEIGHT_Y) + 128) >> 8 : 0;
|
|
1283
|
-
r.H = costs[modeCosts + mode];
|
|
1284
1298
|
r.R = mode > 0 && isFlat(tmpLevels2, 0, 1, FLATNESS_LIMIT_I4) ? FLATNESS_PENALTY : 0;
|
|
1299
|
+
r.SD = 0;
|
|
1300
|
+
r.setRDScore(lambda);
|
|
1301
|
+
if (bestMode >= 0 && r.score >= rdI4.score)
|
|
1302
|
+
continue;
|
|
1303
|
+
r.SD = tlambda ? (tlambda * tDisto4x4(spectrum, tmpDst, td, WEIGHT_Y) + 128) >> 8 : 0;
|
|
1285
1304
|
r.setRDScore(lambda);
|
|
1286
1305
|
if (bestMode >= 0 && r.score >= rdI4.score)
|
|
1287
1306
|
continue;
|
|
@@ -1337,9 +1356,11 @@ function pickBestUv(it, rd) {
|
|
|
1337
1356
|
r.D = sse16(it.yuvIn, U_OFF, tmpDst, U_OFF, 8);
|
|
1338
1357
|
r.SD = 0;
|
|
1339
1358
|
r.H = FIXED_COSTS_UV[mode];
|
|
1340
|
-
r.R =
|
|
1341
|
-
|
|
1342
|
-
|
|
1359
|
+
r.R = mode > 0 && isFlat(r.uvLevels, 0, 8, FLATNESS_LIMIT_UV) ? FLATNESS_PENALTY * 8 : 0;
|
|
1360
|
+
r.setRDScore(lambda);
|
|
1361
|
+
if (mode > 0 && r.score >= rdBestUv.score)
|
|
1362
|
+
continue;
|
|
1363
|
+
r.R += costUv(it, r);
|
|
1343
1364
|
r.setRDScore(lambda);
|
|
1344
1365
|
if (mode === 0 || r.score < rdBestUv.score) {
|
|
1345
1366
|
rdBestUv.copyScore(r);
|
package/dist/image.d.ts
CHANGED
|
@@ -70,19 +70,19 @@ export interface JPEGEncodeOptions {
|
|
|
70
70
|
export interface WebPEncodeOptions {
|
|
71
71
|
/**
|
|
72
72
|
* Lossy compression (0-100, like libwebp's quality: 75 is its default): the image is encoded like cwebp encodes it
|
|
73
|
-
* with
|
|
73
|
+
* (with `-m` set to the effort), much smaller than lossless. Without it the image is encoded losslessly
|
|
74
74
|
*/
|
|
75
75
|
quality?: number;
|
|
76
76
|
/**
|
|
77
77
|
* How much effort lossy compression takes (0-6, libwebp's method): higher is smaller but slower. 0-2 pick the
|
|
78
78
|
* predictions by how close they are, 3 and up also by how many bits they take, 5 and 6 also optimize the
|
|
79
|
-
* quantized values. Default: `4
|
|
79
|
+
* quantized values. Default: `2`: about a quarter of the time of 4 (cwebp's default) for files about 5% larger
|
|
80
80
|
*/
|
|
81
81
|
effort?: number;
|
|
82
82
|
/** Metadata to include, see {@link Metadata} */
|
|
83
83
|
metadata?: Metadata;
|
|
84
84
|
}
|
|
85
|
-
/** Options of {@link Image.encodeTGA} */
|
|
85
|
+
/** Options of {@link Image.encodeTGA} and {@link Image.encodeJXL} */
|
|
86
86
|
export interface EncodeOptions {
|
|
87
87
|
/** Metadata to include, see {@link Metadata} */
|
|
88
88
|
metadata?: Metadata;
|
|
@@ -132,6 +132,19 @@ export interface DecodeOptions {
|
|
|
132
132
|
*/
|
|
133
133
|
toneMapping?: ToneMapping;
|
|
134
134
|
}
|
|
135
|
+
/** Options of {@link Image.encodeAVIF} */
|
|
136
|
+
export interface AVIFEncodeOptions {
|
|
137
|
+
/** 0-100, like avifenc's: higher is better and larger, 100 is lossless. Default: `60` */
|
|
138
|
+
quality?: number;
|
|
139
|
+
/** The quality of the alpha channel (if the image has transparency), like avifenc's. Default: `100` (lossless) */
|
|
140
|
+
alphaQuality?: number;
|
|
141
|
+
/** Chroma subsampling: `'4:4:4'` keeps the full color resolution, `'4:2:0'` halves it (smaller). Default: `'4:4:4'` */
|
|
142
|
+
subsampling?: '4:4:4' | '4:2:0';
|
|
143
|
+
/** How hard the encoder searches for the best modes: 0-2, faster to smaller. Default: `1` */
|
|
144
|
+
effort?: number;
|
|
145
|
+
/** Metadata to include, see {@link Metadata} */
|
|
146
|
+
metadata?: Metadata;
|
|
147
|
+
}
|
|
135
148
|
/** Text style options of {@link Image.renderText} */
|
|
136
149
|
export interface TextStyle {
|
|
137
150
|
/** Width of an outline around the glyphs in pixels. Default: `0` */
|
|
@@ -754,6 +767,19 @@ export declare class Image {
|
|
|
754
767
|
* Encodes the image as QOI (3 channels if the image is opaque)
|
|
755
768
|
*/
|
|
756
769
|
encodeQOI(): Promise<Uint8Array>;
|
|
770
|
+
/**
|
|
771
|
+
* Encodes the image as AVIF (AV1, with an alpha image if the image has transparency)
|
|
772
|
+
* @param options See {@link AVIFEncodeOptions}
|
|
773
|
+
*/
|
|
774
|
+
encodeAVIF({ quality, alphaQuality, subsampling, effort, metadata }?: AVIFEncodeOptions): Promise<Uint8Array>;
|
|
775
|
+
/**
|
|
776
|
+
* Encodes the image as lossless JPEG XL, the codestream byte for byte like libjxl's `cjxl -e 1` on CPUs with AVX2
|
|
777
|
+
* or NEON (its fast lossless mode: the gradient predictor, a palette if the image has at most 511 colors besides
|
|
778
|
+
* black, else YCoCg); greyscale if the image is, with alpha if it has transparency. The codestream is in the
|
|
779
|
+
* container, with the metadata as Exif
|
|
780
|
+
* @param options See {@link EncodeOptions}
|
|
781
|
+
*/
|
|
782
|
+
encodeJXL({ metadata }?: EncodeOptions): Promise<Uint8Array>;
|
|
757
783
|
/**
|
|
758
784
|
* Encodes the image as the smallest fitting Netpbm format: PBM for black and white images, PGM for greyscale ones,
|
|
759
785
|
* PPM for others, PAM if the image has transparency
|
package/dist/image.js
CHANGED
|
@@ -9,6 +9,7 @@ import * as qoi from "./codecs/qoi.js";
|
|
|
9
9
|
import * as netpbm from "./codecs/netpbm.js";
|
|
10
10
|
import * as hdr from "./codecs/hdr.js";
|
|
11
11
|
import * as avif from "./codecs/avif.js";
|
|
12
|
+
import * as avifEnc from "./codecs/avif-enc.js";
|
|
12
13
|
import * as jxl from "./codecs/jxl.js";
|
|
13
14
|
import * as quantize from "./codecs/quantize.js";
|
|
14
15
|
import * as textRenderer from "./render/text.js";
|
|
@@ -1003,7 +1004,7 @@ export class Image {
|
|
|
1003
1004
|
const m = resolveMetadata(metadata);
|
|
1004
1005
|
return jpeg.encode(this.__bytes__(), this.width, this.height, finite(quality, 'quality'), { exif: exifData(m), comment: m.comment ? utf8(m.comment) : undefined });
|
|
1005
1006
|
}
|
|
1006
|
-
async encodeWebP({ quality, effort =
|
|
1007
|
+
async encodeWebP({ quality, effort = 2, metadata = {} } = {}) {
|
|
1007
1008
|
const exif = exifData(resolveMetadata(metadata));
|
|
1008
1009
|
if (quality === undefined)
|
|
1009
1010
|
return webp.encode(this.__bytes__(), this.width, this.height, exif);
|
|
@@ -1024,6 +1025,13 @@ export class Image {
|
|
|
1024
1025
|
async encodeQOI() {
|
|
1025
1026
|
return qoi.encode(this.__bytes__(), this.width, this.height);
|
|
1026
1027
|
}
|
|
1028
|
+
async encodeAVIF({ quality = 60, alphaQuality = 100, subsampling = '4:4:4', effort = 1, metadata = {} } = {}) {
|
|
1029
|
+
const exif = exifData(resolveMetadata(metadata));
|
|
1030
|
+
return avifEnc.encode(this.__bytes__(), this.width, this.height, { quality: finite(quality, 'quality'), alphaQuality: finite(alphaQuality, 'alphaQuality'), subsampling, effort, exif });
|
|
1031
|
+
}
|
|
1032
|
+
async encodeJXL({ metadata = {} } = {}) {
|
|
1033
|
+
return jxl.encode(this.__bytes__(), this.width, this.height, exifData(resolveMetadata(metadata)));
|
|
1034
|
+
}
|
|
1027
1035
|
async encodePNM() {
|
|
1028
1036
|
return netpbm.encode(this.__bytes__(), this.width, this.height);
|
|
1029
1037
|
}
|
package/dist/metadata.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* |---|---|
|
|
6
6
|
* | PNG, APNG | all (as text chunks) |
|
|
7
7
|
* | JPEG | author, description, copyright, creation time, software (Exif); comment (comment marker) |
|
|
8
|
-
* | WebP | author, description, copyright, creation time, software (Exif) |
|
|
8
|
+
* | WebP, AVIF, JPEG XL | author, description, copyright, creation time, software (Exif) |
|
|
9
9
|
* | GIF | software, comment (comment extensions) |
|
|
10
10
|
* | TGA | author, comment, creation time, software (extension area) |
|
|
11
11
|
*
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '3.
|
|
1
|
+
export const VERSION = '3.6.0';
|