imagescript 3.13.2 → 3.15.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 +13 -0
- package/dist/animation-encoder.d.ts +1 -1
- package/dist/animation-encoder.js +6 -4
- package/dist/animation.d.ts +1 -1
- package/dist/animation.js +13 -8
- package/dist/codecs/jpeg.js +56 -5
- package/dist/codecs/jxl/acs-enc.js +32 -11
- package/dist/codecs/jxl/aq-enc.js +65 -44
- package/dist/codecs/jxl/dct.js +29 -9
- package/dist/codecs/jxl/entropy-enc.js +44 -35
- package/dist/codecs/jxl/fast-lossless.js +1 -1
- package/dist/codecs/jxl/modular-lossless.js +307 -0
- package/dist/codecs/jxl/modular.js +1 -1
- package/dist/codecs/jxl/patches-enc.js +75 -1
- package/dist/codecs/jxl/transforms-enc.js +99 -92
- package/dist/codecs/jxl/vardct-enc.js +4 -2
- package/dist/codecs/jxl.js +3 -1
- package/dist/codecs/vp8l.js +65 -27
- package/dist/codecs/webp.js +10 -15
- package/dist/frame.js +4 -0
- package/dist/image.d.ts +22 -14
- package/dist/image.js +27 -15
- package/dist/render/filter.js +6 -2
- package/dist/util/options.js +12 -0
- package/dist/util/resample.js +203 -61
- package/dist/util/worker.js +3 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -1,61 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AFV_BASIS, dct2d, scaleF } from "./dct.js";
|
|
2
2
|
import { SIZES, single } from "./vardct.js";
|
|
3
3
|
import { fdct8, fdct16, fdct32, fdct64 } from "./fdct.js";
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
const cached = forwardMatrices.get(type);
|
|
7
|
-
if (cached)
|
|
8
|
-
return cached;
|
|
9
|
-
const m = new Float64Array(64 * 128), buf = new Float32Array(64);
|
|
10
|
-
for (let k = 0; k < 64; k++) {
|
|
11
|
-
buf.fill(0);
|
|
12
|
-
buf[k] = 1;
|
|
13
|
-
inverseTransform({ buf, offset: 0, stride: 8, w: 8, h: 8 }, type);
|
|
14
|
-
for (let p = 0; p < 64; p++)
|
|
15
|
-
m[p * 128 + k] = buf[p];
|
|
16
|
-
}
|
|
17
|
-
for (let i = 0; i < 64; i++)
|
|
18
|
-
m[i * 128 + 64 + i] = 1;
|
|
19
|
-
for (let col = 0; col < 64; col++) {
|
|
20
|
-
let pivot = col;
|
|
21
|
-
for (let r = col + 1; r < 64; r++)
|
|
22
|
-
if (Math.abs(m[r * 128 + col]) > Math.abs(m[pivot * 128 + col]))
|
|
23
|
-
pivot = r;
|
|
24
|
-
if (pivot !== col) {
|
|
25
|
-
for (let k = 0; k < 128; k++) {
|
|
26
|
-
const t = m[col * 128 + k];
|
|
27
|
-
m[col * 128 + k] = m[pivot * 128 + k];
|
|
28
|
-
m[pivot * 128 + k] = t;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
const inv = 1 / m[col * 128 + col];
|
|
32
|
-
for (let k = 0; k < 128; k++)
|
|
33
|
-
m[col * 128 + k] *= inv;
|
|
34
|
-
for (let r = 0; r < 64; r++) {
|
|
35
|
-
if (r === col)
|
|
36
|
-
continue;
|
|
37
|
-
const factor = m[r * 128 + col];
|
|
38
|
-
if (factor)
|
|
39
|
-
for (let k = 0; k < 128; k++)
|
|
40
|
-
m[r * 128 + k] -= factor * m[col * 128 + k];
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
const start = new Int32Array(65), column = [], value = [];
|
|
44
|
-
for (let r = 0; r < 64; r++) {
|
|
45
|
-
for (let k = 0; k < 64; k++) {
|
|
46
|
-
const v = m[r * 128 + 64 + k];
|
|
47
|
-
if (Math.abs(v) < 1e-9)
|
|
48
|
-
continue;
|
|
49
|
-
column.push(k);
|
|
50
|
-
value.push(v);
|
|
51
|
-
}
|
|
52
|
-
start[r + 1] = column.length;
|
|
53
|
-
}
|
|
54
|
-
const f = { start, column: Int32Array.from(column), value: Float64Array.from(value) };
|
|
55
|
-
forwardMatrices.set(type, f);
|
|
56
|
-
return f;
|
|
57
|
-
}
|
|
58
|
-
const px = new Float64Array(64), tmp = new Float32Array(64), block4 = new Float32Array(16);
|
|
4
|
+
const SEC0 = 0.5411961, SEC1 = 1.306563;
|
|
5
|
+
const tmp = new Float32Array(64), block4 = new Float32Array(16);
|
|
59
6
|
function dct2Top(src, so, stride, out, size) {
|
|
60
7
|
const n = size / 2;
|
|
61
8
|
for (let y = 0; y < n; y++) {
|
|
@@ -84,7 +31,7 @@ function special8x8(type, src, so, stride, c) {
|
|
|
84
31
|
dct2Top(src, so, stride, c, 8);
|
|
85
32
|
dct2Top(c, 0, 8, c, 4);
|
|
86
33
|
dct2Top(c, 0, 8, c, 2);
|
|
87
|
-
return
|
|
34
|
+
return;
|
|
88
35
|
}
|
|
89
36
|
if (type === 3) {
|
|
90
37
|
for (let y = 0; y < 2; y++) {
|
|
@@ -92,38 +39,34 @@ function special8x8(type, src, so, stride, c) {
|
|
|
92
39
|
for (let iy = 0; iy < 4; iy++)
|
|
93
40
|
for (let ix = 0; ix < 4; ix++)
|
|
94
41
|
block4[iy * 4 + ix] = src[so + (y * 4 + iy) * stride + x * 4 + ix];
|
|
95
|
-
|
|
42
|
+
dct4x4(block4);
|
|
96
43
|
for (let iy = 0; iy < 4; iy++)
|
|
97
44
|
for (let ix = 0; ix < 4; ix++)
|
|
98
45
|
c[(y + iy * 2) * 8 + x + ix * 2] = block4[ix * 4 + iy];
|
|
99
46
|
}
|
|
100
47
|
}
|
|
101
48
|
dct2x2Corner(c);
|
|
102
|
-
return
|
|
49
|
+
return;
|
|
103
50
|
}
|
|
104
|
-
|
|
105
|
-
for (let
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
for (let
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
for (let
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
c[(y + iy * 2) * 8 + x + ix * 2] = src[so + (y * 4 + iy) * stride + x * 4 + ix] - center;
|
|
117
|
-
}
|
|
51
|
+
for (let y = 0; y < 2; y++) {
|
|
52
|
+
for (let x = 0; x < 2; x++) {
|
|
53
|
+
let dc = 0;
|
|
54
|
+
for (let iy = 0; iy < 4; iy++)
|
|
55
|
+
for (let ix = 0; ix < 4; ix++)
|
|
56
|
+
dc += src[so + (y * 4 + iy) * stride + x * 4 + ix];
|
|
57
|
+
const center = src[so + (y * 4 + 1) * stride + x * 4 + 1];
|
|
58
|
+
for (let iy = 0; iy < 4; iy++) {
|
|
59
|
+
for (let ix = 0; ix < 4; ix++) {
|
|
60
|
+
if (ix === 1 && iy === 1)
|
|
61
|
+
continue;
|
|
62
|
+
c[(y + iy * 2) * 8 + x + ix * 2] = src[so + (y * 4 + iy) * stride + x * 4 + ix] - center;
|
|
118
63
|
}
|
|
119
|
-
c[(y + 2) * 8 + x + 2] = c[y * 8 + x];
|
|
120
|
-
c[y * 8 + x] = dc / 16;
|
|
121
64
|
}
|
|
65
|
+
c[(y + 2) * 8 + x + 2] = c[y * 8 + x];
|
|
66
|
+
c[y * 8 + x] = dc / 16;
|
|
122
67
|
}
|
|
123
|
-
dct2x2Corner(c);
|
|
124
|
-
return true;
|
|
125
68
|
}
|
|
126
|
-
|
|
69
|
+
dct2x2Corner(c);
|
|
127
70
|
}
|
|
128
71
|
function dct2(buf, w, h) {
|
|
129
72
|
if (w === 8 && h === 8) {
|
|
@@ -152,23 +95,87 @@ function fdct(buf, o, step, n) {
|
|
|
152
95
|
else
|
|
153
96
|
fdct64(buf, o, step);
|
|
154
97
|
}
|
|
98
|
+
const half = new Float32Array(32), quarter = new Float32Array(16), corner = new Float64Array(16);
|
|
99
|
+
function fdct4(buf, o, step) {
|
|
100
|
+
const i0 = buf[o], i1 = buf[o + step], i2 = buf[o + 2 * step], i3 = buf[o + 3 * step];
|
|
101
|
+
const sum03 = i0 + i3, sum12 = i1 + i2, t0 = (i0 - i3) * SEC0, t1 = (i1 - i2) * SEC1;
|
|
102
|
+
const out0 = (t0 + t1) / 4, out1 = (t0 - t1) / 4;
|
|
103
|
+
buf[o] = (sum03 + sum12) / 4;
|
|
104
|
+
buf[o + step] = out0 * Math.SQRT2 + out1;
|
|
105
|
+
buf[o + 2 * step] = (sum03 - sum12) / 4;
|
|
106
|
+
buf[o + 3 * step] = out1;
|
|
107
|
+
}
|
|
108
|
+
function dct8x4(buf) {
|
|
109
|
+
for (let y = 0; y < 32; y += 8)
|
|
110
|
+
fdct8(buf, y, 1);
|
|
111
|
+
for (let x = 0; x < 8; x++)
|
|
112
|
+
fdct4(buf, x, 8);
|
|
113
|
+
}
|
|
114
|
+
function dct4x4(buf) {
|
|
115
|
+
for (let y = 0; y < 16; y += 4)
|
|
116
|
+
fdct4(buf, y, 1);
|
|
117
|
+
for (let x = 0; x < 4; x++)
|
|
118
|
+
fdct4(buf, x, 4);
|
|
119
|
+
}
|
|
120
|
+
function dct4x8(src, so, stride, c, transposed) {
|
|
121
|
+
for (let i = 0; i < 2; i++) {
|
|
122
|
+
for (let iy = 0; iy < 4; iy++) {
|
|
123
|
+
for (let ix = 0; ix < 8; ix++)
|
|
124
|
+
half[iy * 8 + ix] = transposed ? src[so + ix * stride + i * 4 + iy] : src[so + (i * 4 + iy) * stride + ix];
|
|
125
|
+
}
|
|
126
|
+
dct8x4(half);
|
|
127
|
+
for (let iy = 0; iy < 4; iy++)
|
|
128
|
+
for (let ix = 0; ix < 8; ix++)
|
|
129
|
+
c[(iy * 2 + i) * 8 + ix] = half[iy * 8 + ix];
|
|
130
|
+
}
|
|
131
|
+
const d0 = c[0], d1 = c[8];
|
|
132
|
+
c[0] = (d0 + d1) * 0.5;
|
|
133
|
+
c[8] = (d0 - d1) * 0.5;
|
|
134
|
+
}
|
|
135
|
+
function afv(n, src, so, stride, c) {
|
|
136
|
+
const flipX = n & 1, flipY = n >> 1;
|
|
137
|
+
for (let iy = 0; iy < 4; iy++) {
|
|
138
|
+
const ay = flipY ? 3 - iy : iy;
|
|
139
|
+
for (let ix = 0; ix < 4; ix++)
|
|
140
|
+
corner[ay * 4 + (flipX ? 3 - ix : ix)] = src[so + (flipY * 4 + iy) * stride + flipX * 4 + ix];
|
|
141
|
+
}
|
|
142
|
+
for (let i = 1; i < 16; i++) {
|
|
143
|
+
let v = 0;
|
|
144
|
+
for (let k = 0; k < 16; k++)
|
|
145
|
+
v += AFV_BASIS[i * 16 + k] * corner[k];
|
|
146
|
+
c[2 * (i >> 2) * 8 + 2 * (i % 4)] = v;
|
|
147
|
+
}
|
|
148
|
+
let dc = 0;
|
|
149
|
+
for (let k = 0; k < 16; k++)
|
|
150
|
+
dc += AFV_BASIS[k] * corner[k];
|
|
151
|
+
for (let iy = 0; iy < 4; iy++)
|
|
152
|
+
for (let ix = 0; ix < 4; ix++)
|
|
153
|
+
quarter[iy * 4 + ix] = src[so + (flipY * 4 + iy) * stride + (1 - flipX) * 4 + ix];
|
|
154
|
+
dct4x4(quarter);
|
|
155
|
+
for (let iy = 0; iy < 4; iy++)
|
|
156
|
+
for (let ix = 0; ix < 4; ix++)
|
|
157
|
+
c[2 * iy * 8 + 2 * ix + 1] = quarter[ix * 4 + iy];
|
|
158
|
+
for (let iy = 0; iy < 4; iy++)
|
|
159
|
+
for (let ix = 0; ix < 8; ix++)
|
|
160
|
+
half[iy * 8 + ix] = src[so + ((1 - flipY) * 4 + iy) * stride + ix];
|
|
161
|
+
dct8x4(half);
|
|
162
|
+
for (let iy = 0; iy < 4; iy++)
|
|
163
|
+
for (let ix = 0; ix < 8; ix++)
|
|
164
|
+
c[(2 * iy + 1) * 8 + ix] = half[iy * 8 + ix];
|
|
165
|
+
const a = dc / 4, s = (a + quarter[0]) / 2;
|
|
166
|
+
c[1] = (a - quarter[0]) / 2;
|
|
167
|
+
c[0] = (s + half[0]) / 2;
|
|
168
|
+
c[8] = (s - half[0]) / 2;
|
|
169
|
+
}
|
|
155
170
|
export function forwardTransform(type, src, so, stride, coeffs, lf) {
|
|
156
171
|
const w8 = SIZES[2 * type], h8 = SIZES[2 * type + 1], w = w8 * 8, h = h8 * 8;
|
|
157
172
|
if (single(type) && type !== 0) {
|
|
158
|
-
if (
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
for (let x = 0; x < 8; x++)
|
|
165
|
-
px[y * 8 + x] = src[so + y * stride + x];
|
|
166
|
-
for (let r = 0; r < 64; r++) {
|
|
167
|
-
let v = 0;
|
|
168
|
-
for (let k = start[r]; k < start[r + 1]; k++)
|
|
169
|
-
v += value[k] * px[column[k]];
|
|
170
|
-
coeffs[r] = v;
|
|
171
|
-
}
|
|
173
|
+
if (type === 12 || type === 13)
|
|
174
|
+
dct4x8(src, so, stride, coeffs, type === 13);
|
|
175
|
+
else if (type >= 14)
|
|
176
|
+
afv(type - 14, src, so, stride, coeffs);
|
|
177
|
+
else
|
|
178
|
+
special8x8(type, src, so, stride, coeffs);
|
|
172
179
|
lf[0] = coeffs[0];
|
|
173
180
|
return;
|
|
174
181
|
}
|
|
@@ -136,8 +136,10 @@ export function coefficientChunks({ planes, pw, bw, bh, types, muls, globalScale
|
|
|
136
136
|
const w8 = SIZES[2 * type], h8 = SIZES[2 * type + 1], w = w8 * 8, h = h8 * 8, o = (by - ty) * 8 * pw + bx * 8;
|
|
137
137
|
for (let c = 0; c < 3; c++) {
|
|
138
138
|
forwardTransform(type, planes[c], by * 8 * pw + bx * 8, pw, coeffs, lfBlock);
|
|
139
|
+
const r = raw[c];
|
|
139
140
|
for (let y = 0; y < h; y++)
|
|
140
|
-
|
|
141
|
+
for (let x = 0; x < w; x++)
|
|
142
|
+
r[o + y * pw + x] = coeffs[y * w + x];
|
|
141
143
|
for (let y = 0; y < h8; y++)
|
|
142
144
|
for (let x = 0; x < w8; x++)
|
|
143
145
|
lfValues[c][(by + y) * bw + bx + x] = lfBlock[y * w8 + x];
|
|
@@ -168,7 +170,7 @@ export function coefficientChunks({ planes, pw, bw, bh, types, muls, globalScale
|
|
|
168
170
|
if (one ? k === 0 : x < w8 && y < h8)
|
|
169
171
|
continue;
|
|
170
172
|
const v = (c === 1 ? src[o + y * pw + x] : src[o + y * pw + x] - k0 * yDequant[k]) / m[k] * scale;
|
|
171
|
-
const q = Math.abs(v) >= thresholds[(y >= h / 2 ? 2 : 0) + (x >= w / 2 ? 1 : 0)] ? Math.
|
|
173
|
+
const q = Math.abs(v) >= thresholds[(y >= h / 2 ? 2 : 0) + (x >= w / 2 ? 1 : 0)] ? Math.floor(v + 0.5) : 0;
|
|
172
174
|
out[go + y * pw + x] = q;
|
|
173
175
|
if (c === 1) {
|
|
174
176
|
const d = q === 0 ? 0 : Math.abs(q) <= 1 ? q * QUANT_BIAS[1] : q - QUANT_BIAS_NUMERATOR / q;
|
package/dist/codecs/jxl.js
CHANGED
|
@@ -6,6 +6,7 @@ import { frameHeader, toc } from "./jxl/frame-header.js";
|
|
|
6
6
|
import { decodeFrame } from "./jxl/frame.js";
|
|
7
7
|
import { ColorOutput } from "./jxl/color.js";
|
|
8
8
|
import { encodeRGBA } from "./jxl/fast-lossless.js";
|
|
9
|
+
import { encodeModular } from "./jxl/modular-lossless.js";
|
|
9
10
|
import { encodeVarDCT } from "./jxl/vardct-enc.js";
|
|
10
11
|
import { concat } from "../util/mem.js";
|
|
11
12
|
import { srgbDecode } from "../util/icc.js";
|
|
@@ -16,7 +17,8 @@ export function isJXL(d) {
|
|
|
16
17
|
return d.length >= 12 && CONTAINER.every((v, i) => d[i] === v);
|
|
17
18
|
}
|
|
18
19
|
export async function encode(rgba, width, height, exif, distance = 0, effort = 1, threads = 1) {
|
|
19
|
-
const stream = distance > 0 ? await encodeVarDCT(rgba, width, height, { distance, effort, threads })
|
|
20
|
+
const stream = distance > 0 ? await encodeVarDCT(rgba, width, height, { distance, effort, threads })
|
|
21
|
+
: effort ? await encodeModular(rgba, width, height, threads) : encodeRGBA(rgba, width, height);
|
|
20
22
|
if (!exif?.length)
|
|
21
23
|
return stream;
|
|
22
24
|
const jxl = [0x6a, 0x78, 0x6c, 0x20];
|
package/dist/codecs/vp8l.js
CHANGED
|
@@ -732,7 +732,13 @@ function prefixEncode(value) {
|
|
|
732
732
|
const second = d >> h - 1 & 1;
|
|
733
733
|
return { symbol: 2 * h + second, bits: h - 1, extra: d & (1 << h - 1) - 1 };
|
|
734
734
|
}
|
|
735
|
-
|
|
735
|
+
const ALL_MODES = Array.from({ length: 14 }, (_, i) => i);
|
|
736
|
+
export const TUNING = [
|
|
737
|
+
{ chain: 4, predictedChain: 0, lazy: false, cacheSearch: false, groups: false, colorTransform: false, analyze: true, modes: [1, 2, 11], tryPredicted: false },
|
|
738
|
+
{ chain: 8, nearOnly: true, lazy: true, cacheSearch: true, groups: true, colorTransform: false, analyze: true, modes: ALL_MODES, tryPredicted: false },
|
|
739
|
+
{ chain: 24, nearOnly: true, lazy: true, cacheSearch: true, groups: true, colorTransform: true, analyze: true, modes: ALL_MODES, tryPredicted: true },
|
|
740
|
+
];
|
|
741
|
+
function tokenize(pixels, width, tuning = TUNING[2]) {
|
|
736
742
|
const n = pixels.length;
|
|
737
743
|
const ops = new Uint8Array(n), values = new Uint32Array(n), distances = new Uint32Array(n);
|
|
738
744
|
let count = 0;
|
|
@@ -743,7 +749,7 @@ function tokenize(pixels, width) {
|
|
|
743
749
|
shortCodes.set(d, c + 1);
|
|
744
750
|
}
|
|
745
751
|
const distanceCode = (d) => shortCodes.get(d) ?? d + 120;
|
|
746
|
-
const HASH_BITS = 15, CHAIN =
|
|
752
|
+
const HASH_BITS = 15, CHAIN = tuning.chain, MAX_LENGTH = 4096, MIN_LENGTH = 3, WINDOW = (1 << 20) - 120;
|
|
747
753
|
const head = new Int32Array(1 << HASH_BITS).fill(-1);
|
|
748
754
|
const chain = new Int32Array(n);
|
|
749
755
|
const hashes = new Int32Array(n);
|
|
@@ -801,7 +807,7 @@ function tokenize(pixels, width) {
|
|
|
801
807
|
for (let i = 0; i < n;) {
|
|
802
808
|
insertUpTo(i);
|
|
803
809
|
find(i);
|
|
804
|
-
if (bestLength >= MIN_LENGTH && bestLength < MAX_LENGTH && i + 1 < n) {
|
|
810
|
+
if (tuning.lazy && bestLength >= MIN_LENGTH && bestLength < MAX_LENGTH && i + 1 < n) {
|
|
805
811
|
const length = bestLength, distance = bestDistance;
|
|
806
812
|
insertUpTo(i + 1);
|
|
807
813
|
find(i + 1);
|
|
@@ -933,6 +939,15 @@ function bestCacheBits(stream, pixels) {
|
|
|
933
939
|
}
|
|
934
940
|
return best;
|
|
935
941
|
}
|
|
942
|
+
function streamBits(stream, pixels) {
|
|
943
|
+
const cacheBits = 10;
|
|
944
|
+
let bits = entropyBits(histograms(withCache(stream, pixels, cacheBits), cacheBits));
|
|
945
|
+
const { ops, values, distances, count } = stream;
|
|
946
|
+
for (let k = 0; k < count; k++)
|
|
947
|
+
if (ops[k] === 1)
|
|
948
|
+
bits += prefixEncode(values[k]).bits + prefixEncode(distances[k]).bits;
|
|
949
|
+
return bits;
|
|
950
|
+
}
|
|
936
951
|
function entropyBits(counts) {
|
|
937
952
|
let bits = 0;
|
|
938
953
|
for (const c of counts) {
|
|
@@ -1118,14 +1133,34 @@ export function costChunks({ entries, starts, used, logs, costs, nearest }) {
|
|
|
1118
1133
|
}
|
|
1119
1134
|
};
|
|
1120
1135
|
}
|
|
1121
|
-
async function writeImage(w, pixels, width, isMain, threads = null) {
|
|
1122
|
-
|
|
1123
|
-
|
|
1136
|
+
async function writeImage(w, pixels, width, isMain, threads = null, tuning = TUNING[2]) {
|
|
1137
|
+
let chain = tuning.chain, tokens = null;
|
|
1138
|
+
if (isMain && tuning.nearOnly && chain) {
|
|
1139
|
+
const near = { ...tuning, chain: 0 };
|
|
1140
|
+
if (pixels.length <= 1 << 20) {
|
|
1141
|
+
const a = tokenize(pixels, width, near), b = tokenize(pixels, width, tuning);
|
|
1142
|
+
tokens = streamBits(a, pixels) < streamBits(b, pixels) ? a : b;
|
|
1143
|
+
}
|
|
1144
|
+
else {
|
|
1145
|
+
const height = pixels.length / width, rows = Math.max(8, Math.floor(height / 24));
|
|
1146
|
+
let nearBits = 0, farBits = 0;
|
|
1147
|
+
for (let k = 0; k < 3; k++) {
|
|
1148
|
+
const top = Math.max(0, Math.min(height - rows, Math.floor((k + 0.5) * height / 3 - rows / 2)));
|
|
1149
|
+
const band = pixels.subarray(top * width, (top + rows) * width);
|
|
1150
|
+
nearBits += streamBits(tokenize(band, width, near), band);
|
|
1151
|
+
farBits += streamBits(tokenize(band, width, tuning), band);
|
|
1152
|
+
}
|
|
1153
|
+
if (nearBits < farBits)
|
|
1154
|
+
chain = 0;
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
tokens ??= tokenize(pixels, width, chain === tuning.chain ? tuning : { ...tuning, chain });
|
|
1158
|
+
const cacheBits = !isMain ? 0 : tuning.cacheSearch ? bestCacheBits(tokens, pixels) : pixels.length > 4096 ? 10 : 0;
|
|
1124
1159
|
const stream = withCache(tokens, pixels, cacheBits), counts = histograms(stream, cacheBits);
|
|
1125
1160
|
const best = entropyBits(counts);
|
|
1126
1161
|
const height = pixels.length / width;
|
|
1127
1162
|
let groups = null, groupCounts = [counts], prefixBits = 0;
|
|
1128
|
-
if (isMain && pixels.length > 4096) {
|
|
1163
|
+
if (isMain && tuning.groups && pixels.length > 4096) {
|
|
1129
1164
|
const r = await prefixGroups(stream, width, height, cacheBits, 4, threads);
|
|
1130
1165
|
if (r && r[2] < best) {
|
|
1131
1166
|
[groups, groupCounts] = r;
|
|
@@ -1142,7 +1177,7 @@ async function writeImage(w, pixels, width, isMain, threads = null) {
|
|
|
1142
1177
|
w.write(groups ? 1 : 0, 1);
|
|
1143
1178
|
if (groups) {
|
|
1144
1179
|
w.write(prefixBits - 2, 3);
|
|
1145
|
-
await writeImage(w, Uint32Array.from(groups, g => g << 8), subSize(width, prefixBits), false);
|
|
1180
|
+
await writeImage(w, Uint32Array.from(groups, g => g << 8), subSize(width, prefixBits), false, null, tuning);
|
|
1146
1181
|
}
|
|
1147
1182
|
}
|
|
1148
1183
|
const { ops, values, distances, count } = stream;
|
|
@@ -1375,17 +1410,17 @@ function analyze(argb, width, height, bits) {
|
|
|
1375
1410
|
const best = entropy.indexOf(Math.min(...entropy));
|
|
1376
1411
|
return { predict: best === 1 || best === 3, subtractGreen: best >= 2 };
|
|
1377
1412
|
}
|
|
1378
|
-
async function applyPredictor(p, width, height, bits, threads) {
|
|
1413
|
+
async function applyPredictor(p, width, height, bits, threads, candidates = ALL_MODES) {
|
|
1379
1414
|
const blocksX = subSize(width, bits), blocksY = subSize(height, bits);
|
|
1380
1415
|
const task = {
|
|
1381
|
-
task: 'vp8l-predictor', p, src: threads ? shared(p) : p.slice(), width, height, bits,
|
|
1416
|
+
task: 'vp8l-predictor', p, src: threads ? shared(p) : p.slice(), width, height, bits, candidates,
|
|
1382
1417
|
modes: new Uint32Array(threads ? new SharedArrayBuffer(4 * blocksX * blocksY) : new ArrayBuffer(4 * blocksX * blocksY)),
|
|
1383
1418
|
};
|
|
1384
1419
|
await runChunks(threads, task, Math.ceil(blocksY / PREDICTOR_ROWS), predictorChunks(task));
|
|
1385
1420
|
return task.modes;
|
|
1386
1421
|
}
|
|
1387
1422
|
const PREDICTOR_ROWS = 4;
|
|
1388
|
-
export function predictorChunks({ p, src, modes, width, height, bits }) {
|
|
1423
|
+
export function predictorChunks({ p, src, modes, width, height, bits, candidates }) {
|
|
1389
1424
|
const blocksX = subSize(width, bits), blocksY = subSize(height, bits), size = 1 << bits;
|
|
1390
1425
|
const predicted = new Uint32Array(size);
|
|
1391
1426
|
const predictRow = (mode, x0, x1, y) => {
|
|
@@ -1403,9 +1438,9 @@ export function predictorChunks({ p, src, modes, width, height, bits }) {
|
|
|
1403
1438
|
return chunk => {
|
|
1404
1439
|
for (let by = chunk * PREDICTOR_ROWS; by < Math.min(blocksY, (chunk + 1) * PREDICTOR_ROWS); by++)
|
|
1405
1440
|
for (let bx = 0; bx < blocksX; bx++) {
|
|
1406
|
-
let best = 0, bestCost = Infinity;
|
|
1441
|
+
let best = candidates[0], bestCost = Infinity;
|
|
1407
1442
|
const x0 = bx * size, y0 = by * size, x1 = Math.min(width, x0 + size), y1 = Math.min(height, y0 + size);
|
|
1408
|
-
for (
|
|
1443
|
+
for (const mode of candidates) {
|
|
1409
1444
|
let cost = 0;
|
|
1410
1445
|
for (let y = y0; y < y1 && cost < bestCost; y++) {
|
|
1411
1446
|
predictRow(mode, x0, x1, y);
|
|
@@ -1426,22 +1461,23 @@ export function predictorChunks({ p, src, modes, width, height, bits }) {
|
|
|
1426
1461
|
}
|
|
1427
1462
|
};
|
|
1428
1463
|
}
|
|
1429
|
-
export async function encode(argb, width, height, threads = null) {
|
|
1464
|
+
export async function encode(argb, width, height, threads = null, effort = 2) {
|
|
1430
1465
|
if (width < 1 || height < 1 || width > 16384 || height > 16384)
|
|
1431
1466
|
throw new RangeError('VP8L: dimensions must be 1-16384');
|
|
1467
|
+
const tuning = TUNING[Math.min(2, effort)];
|
|
1432
1468
|
const colors = new Map();
|
|
1433
1469
|
for (let i = 0; i < argb.length && colors.size <= 256; i++)
|
|
1434
1470
|
if (!colors.has(argb[i]))
|
|
1435
1471
|
colors.set(argb[i], colors.size);
|
|
1436
1472
|
if (colors.size > 256)
|
|
1437
|
-
return encodeWith(argb, width, height, null, threads);
|
|
1438
|
-
const indexed = await encodeWith(argb, width, height, colors, threads);
|
|
1439
|
-
if (colors.size <= 16)
|
|
1473
|
+
return encodeWith(argb, width, height, null, threads, tuning);
|
|
1474
|
+
const indexed = await encodeWith(argb, width, height, colors, threads, tuning);
|
|
1475
|
+
if (colors.size <= 16 || !tuning.tryPredicted)
|
|
1440
1476
|
return indexed;
|
|
1441
|
-
const predicted = await encodeWith(argb, width, height, null, threads);
|
|
1477
|
+
const predicted = await encodeWith(argb, width, height, null, threads, tuning);
|
|
1442
1478
|
return predicted.length < indexed.length ? predicted : indexed;
|
|
1443
1479
|
}
|
|
1444
|
-
async function encodeWith(argb, width, height, colors, threads) {
|
|
1480
|
+
async function encodeWith(argb, width, height, colors, threads, tuning) {
|
|
1445
1481
|
const w = new BitWriter();
|
|
1446
1482
|
let hasAlpha = false;
|
|
1447
1483
|
for (let i = 0; i < argb.length; i++)
|
|
@@ -1454,7 +1490,7 @@ async function encodeWith(argb, width, height, colors, threads) {
|
|
|
1454
1490
|
w.write(height - 1, 14);
|
|
1455
1491
|
w.write(hasAlpha ? 1 : 0, 1);
|
|
1456
1492
|
w.write(0, 3);
|
|
1457
|
-
let pixels, codedWidth = width;
|
|
1493
|
+
let pixels, codedWidth = width, predicted = false;
|
|
1458
1494
|
if (colors) {
|
|
1459
1495
|
const palette = Uint32Array.from([...colors.keys()].sort((a, b) => a - b));
|
|
1460
1496
|
palette.forEach((c, i) => colors.set(c, i));
|
|
@@ -1465,7 +1501,7 @@ async function encodeWith(argb, width, height, colors, threads) {
|
|
|
1465
1501
|
const deltas = palette.slice();
|
|
1466
1502
|
for (let i = palette.length - 1; i > 0; i--)
|
|
1467
1503
|
deltas[i] = subPixels(palette[i], palette[i - 1]);
|
|
1468
|
-
await writeImage(w, deltas, palette.length, false);
|
|
1504
|
+
await writeImage(w, deltas, palette.length, false, null, tuning);
|
|
1469
1505
|
codedWidth = subSize(width, bits);
|
|
1470
1506
|
pixels = new Uint32Array(codedWidth * height);
|
|
1471
1507
|
const perPixel = 8 >> bits;
|
|
@@ -1481,7 +1517,7 @@ async function encodeWith(argb, width, height, colors, threads) {
|
|
|
1481
1517
|
for (let i = 0; i < pixels.length && !redBlue; i++)
|
|
1482
1518
|
if (pixels[i] & 0x00ff00ff)
|
|
1483
1519
|
redBlue = true;
|
|
1484
|
-
const { predict, subtractGreen } = analyze(argb, width, height, 4);
|
|
1520
|
+
const { predict, subtractGreen } = tuning.analyze ? analyze(argb, width, height, 4) : { predict: true, subtractGreen: true };
|
|
1485
1521
|
if (redBlue && subtractGreen) {
|
|
1486
1522
|
w.write(1, 1);
|
|
1487
1523
|
w.write(2, 2);
|
|
@@ -1492,12 +1528,13 @@ async function encodeWith(argb, width, height, colors, threads) {
|
|
|
1492
1528
|
}
|
|
1493
1529
|
const bits = 4;
|
|
1494
1530
|
if (predict) {
|
|
1531
|
+
predicted = true;
|
|
1495
1532
|
w.write(1, 1);
|
|
1496
1533
|
w.write(0, 2);
|
|
1497
1534
|
w.write(bits - 2, 3);
|
|
1498
|
-
const modes = await applyPredictor(pixels, width, height, bits, threads);
|
|
1499
|
-
await writeImage(w, modes, subSize(width, bits), false);
|
|
1500
|
-
if (redBlue) {
|
|
1535
|
+
const modes = await applyPredictor(pixels, width, height, bits, threads, tuning.modes);
|
|
1536
|
+
await writeImage(w, modes, subSize(width, bits), false, null, tuning);
|
|
1537
|
+
if (redBlue && tuning.colorTransform) {
|
|
1501
1538
|
const colorBits = 5, before = pixels.slice(), multipliers = colorTransform(pixels, width, height, colorBits);
|
|
1502
1539
|
const changed = multipliers.reduce((n, m) => n + (m !== 0xff000000 ? 1 : 0), 0);
|
|
1503
1540
|
if (changed && redBlueBits(pixels) + 12 * changed >= redBlueBits(before))
|
|
@@ -1506,12 +1543,13 @@ async function encodeWith(argb, width, height, colors, threads) {
|
|
|
1506
1543
|
w.write(1, 1);
|
|
1507
1544
|
w.write(1, 2);
|
|
1508
1545
|
w.write(colorBits - 2, 3);
|
|
1509
|
-
await writeImage(w, multipliers, subSize(width, colorBits), false);
|
|
1546
|
+
await writeImage(w, multipliers, subSize(width, colorBits), false, null, tuning);
|
|
1510
1547
|
}
|
|
1511
1548
|
}
|
|
1512
1549
|
}
|
|
1513
1550
|
}
|
|
1514
1551
|
w.write(0, 1);
|
|
1515
|
-
|
|
1552
|
+
const chain = predicted && tuning.predictedChain !== undefined ? tuning.predictedChain : tuning.chain;
|
|
1553
|
+
await writeImage(w, pixels, codedWidth, true, threads, { ...tuning, chain });
|
|
1516
1554
|
return w.finish();
|
|
1517
1555
|
}
|
package/dist/codecs/webp.js
CHANGED
|
@@ -8,7 +8,7 @@ import { encodeVP8, encodeAlpha } from "./vp8-enc.js";
|
|
|
8
8
|
import { rgbaToYUV } from "./vp8-enc-dsp.js";
|
|
9
9
|
import { orientation as exifOrientation } from "./exif.js";
|
|
10
10
|
import { MAX_PIXELS, checkPixels } from "../util/limits.js";
|
|
11
|
-
import {
|
|
11
|
+
import { effort as checkEffort, quality as checkQuality } from "../util/options.js";
|
|
12
12
|
import { startThreads } from "../util/threads.js";
|
|
13
13
|
const fourCC = (s) => s.charCodeAt(0) | s.charCodeAt(1) << 8 | s.charCodeAt(2) << 16 | s.charCodeAt(3) << 24;
|
|
14
14
|
const RIFF = fourCC('RIFF'), WEBP = fourCC('WEBP');
|
|
@@ -262,17 +262,17 @@ const hasAlpha = (rgba) => {
|
|
|
262
262
|
return false;
|
|
263
263
|
};
|
|
264
264
|
const MIN_THREADED_PIXELS = 1536 << 10, PIXELS_PER_THREAD = 512 << 10;
|
|
265
|
-
export async function encode(rgba, width, height, exif, threads = 1) {
|
|
265
|
+
export async function encode(rgba, width, height, exif, threads = 1, effort = 2) {
|
|
266
266
|
const n = Math.min(threads, Math.floor(width * height / PIXELS_PER_THREAD));
|
|
267
267
|
const pool = n > 1 && width * height >= MIN_THREADED_PIXELS ? startThreads(n) : null;
|
|
268
268
|
let stream;
|
|
269
269
|
try {
|
|
270
|
-
stream = await vp8l.encode(toARGB(rgba, width * height), width, height, pool);
|
|
270
|
+
stream = await vp8l.encode(toARGB(rgba, width * height), width, height, pool, effort);
|
|
271
271
|
}
|
|
272
272
|
catch (e) {
|
|
273
273
|
if (!pool)
|
|
274
274
|
throw e;
|
|
275
|
-
stream = await vp8l.encode(toARGB(rgba, width * height), width, height);
|
|
275
|
+
stream = await vp8l.encode(toARGB(rgba, width * height), width, height, null, effort);
|
|
276
276
|
}
|
|
277
277
|
finally {
|
|
278
278
|
if (pool)
|
|
@@ -289,15 +289,10 @@ export async function encode(rgba, width, height, exif, threads = 1) {
|
|
|
289
289
|
return riff([riffChunk('VP8X', vp8x), bitstream, riffChunk('EXIF', exif)]);
|
|
290
290
|
}
|
|
291
291
|
export function lossyOptions(quality, effort, sharp, alphaQuality) {
|
|
292
|
+
checkEffort(effort, 6);
|
|
292
293
|
if (quality === undefined)
|
|
293
294
|
return undefined;
|
|
294
|
-
const q =
|
|
295
|
-
if (q < 0 || q > 100)
|
|
296
|
-
throw new RangeError(`quality must be 0-100, not ${q}`);
|
|
297
|
-
if (a < 0 || a > 100)
|
|
298
|
-
throw new RangeError(`alphaQuality must be 0-100, not ${a}`);
|
|
299
|
-
if (!Number.isInteger(effort) || effort < 0 || effort > 6)
|
|
300
|
-
throw new RangeError(`effort must be an integer 0-6, not ${effort}`);
|
|
295
|
+
const q = checkQuality(quality), a = checkQuality(alphaQuality, 'alphaQuality');
|
|
301
296
|
return { quality: q, method: effort, sharp, alphaQuality: Math.round(a) };
|
|
302
297
|
}
|
|
303
298
|
export async function encodeLossy(rgba, width, height, lossy, exif) {
|
|
@@ -326,7 +321,7 @@ export function lossyTolerance(quality) {
|
|
|
326
321
|
const value = Math.sqrt(quality / 100);
|
|
327
322
|
return Math.max(1, Math.round(31 * (1 - value) + value));
|
|
328
323
|
}
|
|
329
|
-
export async function encodeAnimation(frames, width, height, plays, exif, lossy) {
|
|
324
|
+
export async function encodeAnimation(frames, width, height, plays, exif, lossy, effort = 2) {
|
|
330
325
|
if (!frames.length)
|
|
331
326
|
throw new Error('WebP: an animation needs at least one frame');
|
|
332
327
|
if (width > 1 << 24 || height > 1 << 24)
|
|
@@ -336,16 +331,16 @@ export async function encodeAnimation(frames, width, height, plays, exif, lossy)
|
|
|
336
331
|
alpha ||= needsAlpha(f, i === 0, width, height);
|
|
337
332
|
const chunks = [];
|
|
338
333
|
for (const f of frames)
|
|
339
|
-
chunks.push(await frameChunk(f, lossy));
|
|
334
|
+
chunks.push(await frameChunk(f, lossy, effort));
|
|
340
335
|
return animationFile(chunks, width, height, plays, alpha, exif);
|
|
341
336
|
}
|
|
342
337
|
export function needsAlpha(f, first, width, height) {
|
|
343
338
|
return (first && (f.x > 0 || f.y > 0 || f.width < width || f.height < height)) || f.dispose || hasAlpha(f.pixels);
|
|
344
339
|
}
|
|
345
|
-
export async function frameChunk(f, lossy) {
|
|
340
|
+
export async function frameChunk(f, lossy, effort = 2) {
|
|
346
341
|
if (f.x & 1 || f.y & 1)
|
|
347
342
|
throw new RangeError('WebP: frame offsets have to be even');
|
|
348
|
-
const bitstream = !lossy ? riffChunk('VP8L', await vp8l.encode(toARGB(f.pixels, f.width * f.height), f.width, f.height))
|
|
343
|
+
const bitstream = !lossy ? riffChunk('VP8L', await vp8l.encode(toARGB(f.pixels, f.width * f.height), f.width, f.height, null, effort))
|
|
349
344
|
: concat((await lossyChunks(f.pixels, f.width, f.height, lossy)).chunks);
|
|
350
345
|
const header = new Uint8Array(16);
|
|
351
346
|
set24(header, 0, f.x >> 1);
|
package/dist/frame.js
CHANGED
|
@@ -75,4 +75,8 @@ export class Frame extends Image {
|
|
|
75
75
|
this.yOffset = Math.round(this.yOffset * this.height / originalHeight);
|
|
76
76
|
return this;
|
|
77
77
|
}
|
|
78
|
+
__rescaled__(width, height) {
|
|
79
|
+
this.xOffset = Math.round(this.xOffset * width / this.width);
|
|
80
|
+
this.yOffset = Math.round(this.yOffset * height / this.height);
|
|
81
|
+
}
|
|
78
82
|
}
|
package/dist/image.d.ts
CHANGED
|
@@ -74,9 +74,12 @@ export interface WebPEncodeOptions {
|
|
|
74
74
|
*/
|
|
75
75
|
quality?: number;
|
|
76
76
|
/**
|
|
77
|
-
* How much effort
|
|
77
|
+
* How much effort compression takes (0-6): higher is smaller but slower. Lossy (libwebp's method): 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
|
|
79
|
+
* quantized values; the default `2` takes about a quarter of the time of 4 (cwebp's default) for files about 5%
|
|
80
|
+
* larger. Lossless: 0 is a fast mode (photos in about a fifth of the time, graphics in about a third, files about
|
|
81
|
+
* 6-8% larger), 1 takes about two thirds of the time for files up to 2% larger, 2 and up compress the most.
|
|
82
|
+
* Default: `2`
|
|
80
83
|
*/
|
|
81
84
|
effort?: number;
|
|
82
85
|
/**
|
|
@@ -184,16 +187,19 @@ export interface JXLEncodeOptions {
|
|
|
184
187
|
*/
|
|
185
188
|
quality?: number;
|
|
186
189
|
/**
|
|
187
|
-
* How hard
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
+
* How hard compression works: 0-2, faster to smaller. Lossy: 0 uses 8x8 DCTs only (like `cjxl -e 3`), 1 and 2
|
|
191
|
+
* search their sizes like `cjxl -e 5` (2 tries more of the small ones), and 2 also stores text-like features that
|
|
192
|
+
* repeat (the glyphs of screenshots) once, as patches like `cjxl -e 7`. Lossless: 0 is `cjxl -e 1`'s fast mode
|
|
193
|
+
* (byte for byte), 1 and 2 like `cjxl -e 3` (about 15% smaller for photos, 13% for graphics; a few times slower).
|
|
194
|
+
* Default: `1`
|
|
190
195
|
*/
|
|
191
196
|
effort?: number;
|
|
192
197
|
/**
|
|
193
|
-
* At most this many worker threads compress
|
|
194
|
-
* 1920x1080 photo at effort 1,
|
|
195
|
-
* effort 0, 512x512 pixels at 1, 384x384 at 2
|
|
196
|
-
* cross-origin isolation)
|
|
198
|
+
* At most this many worker threads compress in parallel (the same file, faster: lossy 2.3 times with 8 on a
|
|
199
|
+
* 1920x1080 photo at effort 1, 3.8 times on 3840x2160; lossless 2.8 and 4.3 times), `1` for none. Small images
|
|
200
|
+
* (lossy under about 1.5 megapixels at effort 0, 512x512 pixels at 1, 384x384 at 2; lossless under 724x724 and
|
|
201
|
+
* always at effort 0), and runtimes whose workers can't share memory (browsers without cross-origin isolation)
|
|
202
|
+
* encode without them.
|
|
197
203
|
* Default: the processors available, at most 8
|
|
198
204
|
*/
|
|
199
205
|
threads?: number;
|
|
@@ -873,11 +879,13 @@ export declare class Image {
|
|
|
873
879
|
*/
|
|
874
880
|
encodeAVIF({ quality, alphaQuality, subsampling, effort, threads, metadata }?: AVIFEncodeOptions): Promise<Uint8Array>;
|
|
875
881
|
/**
|
|
876
|
-
* Encodes the image as JPEG XL: losslessly (the default)
|
|
877
|
-
*
|
|
878
|
-
*
|
|
879
|
-
*
|
|
880
|
-
*
|
|
882
|
+
* Encodes the image as JPEG XL: losslessly (the default) like libjxl's `cjxl -e 3` (a palette of up to 1024 colors,
|
|
883
|
+
* else palettes of channels using few of their values; YCoCg; the weighted predictor in contexts of its error;
|
|
884
|
+
* greyscale if the image is), at effort 0 the codestream byte for byte like `cjxl -e 1` on CPUs with AVX2 or NEON
|
|
885
|
+
* (its fast lossless mode: the gradient predictor, a palette if the image has at most 511 colors besides black,
|
|
886
|
+
* else YCoCg), or lossy with a quality (like `cjxl -q`: XYB, varblocks of libjxl's search, chroma from luma, the
|
|
887
|
+
* edge preserving filter). Alpha (if the image has transparency) is lossless either way. The codestream is in the
|
|
888
|
+
* container, with the metadata as Exif
|
|
881
889
|
* @param options See {@link JXLEncodeOptions}
|
|
882
890
|
*/
|
|
883
891
|
encodeJXL({ quality, effort, threads, metadata }?: JXLEncodeOptions): Promise<Uint8Array>;
|