imagescript 3.8.1 → 3.9.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 +3 -1
- package/THIRD_PARTY_NOTICES.md +62 -3
- package/dist/animation-encoder.d.ts +2 -0
- package/dist/animation-encoder.js +4 -10
- package/dist/animation.d.ts +5 -1
- package/dist/animation.js +17 -15
- package/dist/codecs/avif.js +15 -1
- package/dist/codecs/jpeg.js +7 -1
- package/dist/codecs/jxl/patches-enc.js +342 -0
- package/dist/codecs/jxl/vardct-enc.js +13 -3
- package/dist/codecs/png.js +48 -12
- package/dist/codecs/tiff.js +5 -3
- package/dist/codecs/vp8-enc.js +59 -3
- package/dist/codecs/webp.js +28 -14
- package/dist/image.d.ts +28 -4
- package/dist/image.js +10 -11
- package/dist/render/blend.js +102 -0
- package/dist/render/filter.js +6 -90
- package/dist/render/hinting.js +118 -75
- package/dist/render/svg-text.js +854 -0
- package/dist/render/svg.js +767 -77
- package/dist/render/text.js +1 -1
- package/dist/util/icc.js +143 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* JPEG XL patches for the lossy encoder: text-like features of screenshots stored once in a reference frame and added
|
|
3
|
+
* where they repeat, ported from libjxl (lib/jxl/enc_patch_dictionary.cc: FindTextLikePatches, FindBestPatchDictionary,
|
|
4
|
+
* PatchDictionaryEncoder; enc_modular.cc: the XYB quantization of Modular frames). Copyright (c) the JPEG XL Project
|
|
5
|
+
* Authors, BSD-3-Clause License (see THIRD_PARTY_NOTICES.md)
|
|
6
|
+
*/
|
|
7
|
+
import { BitWriter } from "./bit-writer.js";
|
|
8
|
+
import { Tokens, EntropyCode, packSigned } from "./entropy-enc.js";
|
|
9
|
+
import { MATree, channelTokens, streamHeader, PRED_GRADIENT, PROP_CHANNEL } from "./modular-enc.js";
|
|
10
|
+
import { u32 } from "./headers-enc.js";
|
|
11
|
+
const DEQUANT = [0.01615, 0.08875, 0.1922], WEIGHTS = [30, 3, 1];
|
|
12
|
+
const PATCH_SIDE = 4, EXTRA_SIDE = 4, MAX_PATCH_SIZE = 32, DISTANCE_LIMIT = 50;
|
|
13
|
+
const SIMILAR = 0.8, VERY_SIMILAR = 0.03, HAS_SIMILAR = 0.03, MIN_PEAK = 2, HAS_SIMILAR_RADIUS = 2, MIN_MAX_PATCH_SIZE = 20;
|
|
14
|
+
const REFERENCE = 3;
|
|
15
|
+
function f16(v) {
|
|
16
|
+
const f = new Float32Array(1), u = new Uint32Array(f.buffer);
|
|
17
|
+
f[0] = v;
|
|
18
|
+
const x = u[0], sign = x >>> 16 & 0x8000, e = (x >>> 23 & 255) - 127 + 15;
|
|
19
|
+
let m = x & 0x7fffff;
|
|
20
|
+
if (e <= 0)
|
|
21
|
+
return sign;
|
|
22
|
+
if (e >= 31)
|
|
23
|
+
return sign | 0x7bff;
|
|
24
|
+
m += 0x1000;
|
|
25
|
+
if (m & 0x800000)
|
|
26
|
+
return e + 1 >= 31 ? sign | 0x7bff : sign | (e + 1) << 10;
|
|
27
|
+
return sign | e << 10 | m >> 13;
|
|
28
|
+
}
|
|
29
|
+
function fromF16(bits) {
|
|
30
|
+
const e = bits >> 10 & 31, m = bits & 1023, s = bits & 0x8000 ? -1 : 1;
|
|
31
|
+
return e === 0 ? s * m / 1024 / 16384 : s * (1 + m / 1024) * 2 ** (e - 15);
|
|
32
|
+
}
|
|
33
|
+
function candidates(planes, stride, width, height) {
|
|
34
|
+
const [p0, p1, p2] = planes;
|
|
35
|
+
const at = (c, x, y) => planes[c][y * stride + x];
|
|
36
|
+
const same = (x1, y1, x2, y2) => {
|
|
37
|
+
const i = y1 * stride + x1, j = y2 * stride + x2;
|
|
38
|
+
return Math.abs(p0[i] - p0[j]) <= 1e-4 && Math.abs(p1[i] - p1[j]) <= 1e-4 && Math.abs(p2[i] - p2[j]) <= 1e-4;
|
|
39
|
+
};
|
|
40
|
+
const similar = (a, i, b, j, threshold) => Math.abs(a[0][i] - b[0][j]) * WEIGHTS[0] + Math.abs(a[1][i] - b[1][j]) * WEIGHTS[1] + Math.abs(a[2][i] - b[2][j]) * WEIGHTS[2] <= threshold;
|
|
41
|
+
const sw = Math.ceil(width / PATCH_SIDE), screenshot = new Uint8Array(sw * Math.ceil(height / PATCH_SIDE));
|
|
42
|
+
let any = false;
|
|
43
|
+
for (let by = 0; by < Math.floor(height / PATCH_SIDE); by++) {
|
|
44
|
+
for (let bx = 0; bx < Math.floor(width / PATCH_SIDE); bx++) {
|
|
45
|
+
const x0 = bx * PATCH_SIDE, y0 = by * PATCH_SIDE;
|
|
46
|
+
let allSame = true;
|
|
47
|
+
for (let iy = 0; iy < PATCH_SIDE && allSame; iy++)
|
|
48
|
+
for (let ix = 0; ix < PATCH_SIDE; ix++)
|
|
49
|
+
if (!same(x0 + ix, y0 + iy, x0, y0)) {
|
|
50
|
+
allSame = false;
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
if (!allSame)
|
|
54
|
+
continue;
|
|
55
|
+
let n = 0, nSame = 0;
|
|
56
|
+
for (let iy = -EXTRA_SIDE; iy < EXTRA_SIDE + PATCH_SIDE; iy++) {
|
|
57
|
+
for (let ix = -EXTRA_SIDE; ix < EXTRA_SIDE + PATCH_SIDE; ix++) {
|
|
58
|
+
const x = x0 + ix, y = y0 + iy;
|
|
59
|
+
if (x < 0 || x >= width || y < 0 || y >= height)
|
|
60
|
+
continue;
|
|
61
|
+
n++;
|
|
62
|
+
if (same(x, y, x0, y0))
|
|
63
|
+
nSame++;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (nSame * 8 < n * 7)
|
|
67
|
+
continue;
|
|
68
|
+
screenshot[by * sw + bx] = 1;
|
|
69
|
+
any = true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (!any)
|
|
73
|
+
return [];
|
|
74
|
+
const isBackground = new Uint8Array(width * height), queued = new Uint8Array(width * height), background = [0, 1, 2].map(() => new Float32Array(width * height));
|
|
75
|
+
const queue = new Int32Array(width * height * 4);
|
|
76
|
+
let end = 0;
|
|
77
|
+
for (let y = 0; y < height; y++) {
|
|
78
|
+
for (let x = 0; x < width; x++) {
|
|
79
|
+
if (!screenshot[(y / PATCH_SIDE | 0) * sw + (x / PATCH_SIDE | 0)])
|
|
80
|
+
continue;
|
|
81
|
+
queue[end++] = x;
|
|
82
|
+
queue[end++] = y;
|
|
83
|
+
queue[end++] = x;
|
|
84
|
+
queue[end++] = y;
|
|
85
|
+
queued[y * width + x] = 1;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
for (let q = 0; q < end; q += 4) {
|
|
89
|
+
const cx = queue[q], cy = queue[q + 1], sx = queue[q + 2], sy = queue[q + 3];
|
|
90
|
+
isBackground[cy * width + cx] = 1;
|
|
91
|
+
for (let c = 0; c < 3; c++)
|
|
92
|
+
background[c][cy * width + cx] = at(c, sx, sy);
|
|
93
|
+
const src = sy * stride + sx;
|
|
94
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
95
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
96
|
+
if (!dx && !dy)
|
|
97
|
+
continue;
|
|
98
|
+
const nx = cx + dx, ny = cy + dy;
|
|
99
|
+
if (nx < 0 || ny < 0 || nx >= width || ny >= height || Math.abs(nx - sx) + Math.abs(ny - sy) > DISTANCE_LIMIT)
|
|
100
|
+
continue;
|
|
101
|
+
const i = ny * width + nx;
|
|
102
|
+
if (!queued[i] && similar(planes, src, planes, ny * stride + nx, SIMILAR) && (!screenshot[(ny / PATCH_SIDE | 0) * sw + (nx / PATCH_SIDE | 0)] || same(sx, sy, nx, ny))) {
|
|
103
|
+
queued[i] = 1;
|
|
104
|
+
queue[end++] = nx;
|
|
105
|
+
queue[end++] = ny;
|
|
106
|
+
queue[end++] = sx;
|
|
107
|
+
queue[end++] = sy;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const visited = new Uint8Array(width * height), found = [];
|
|
113
|
+
for (let y = 0; y < height; y++) {
|
|
114
|
+
for (let x = 0; x < width; x++) {
|
|
115
|
+
if (isBackground[y * width + x] || visited[y * width + x])
|
|
116
|
+
continue;
|
|
117
|
+
const stack = [x, y];
|
|
118
|
+
let minX = x, maxX = x, minY = y, maxY = y, reference = -1, allSimilar = true;
|
|
119
|
+
while (stack.length) {
|
|
120
|
+
const cy = stack.pop(), cx = stack.pop();
|
|
121
|
+
if (visited[cy * width + cx])
|
|
122
|
+
continue;
|
|
123
|
+
visited[cy * width + cx] = 1;
|
|
124
|
+
minX = Math.min(minX, cx);
|
|
125
|
+
maxX = Math.max(maxX, cx);
|
|
126
|
+
minY = Math.min(minY, cy);
|
|
127
|
+
maxY = Math.max(maxY, cy);
|
|
128
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
129
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
130
|
+
if (!dx && !dy)
|
|
131
|
+
continue;
|
|
132
|
+
const nx = cx + dx, ny = cy + dy;
|
|
133
|
+
if (nx < 0 || ny < 0 || nx >= width || ny >= height)
|
|
134
|
+
continue;
|
|
135
|
+
const i = ny * width + nx;
|
|
136
|
+
if (!isBackground[i]) {
|
|
137
|
+
if (!visited[i])
|
|
138
|
+
stack.push(nx, ny);
|
|
139
|
+
}
|
|
140
|
+
else if (reference < 0)
|
|
141
|
+
reference = i;
|
|
142
|
+
else if (!similar(background, i, background, reference, VERY_SIMILAR))
|
|
143
|
+
allSimilar = false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (reference < 0 || !allSimilar || maxX - minX >= MAX_PATCH_SIZE || maxY - minY >= MAX_PATCH_SIZE)
|
|
148
|
+
continue;
|
|
149
|
+
const ref = [background[0][reference], background[1][reference], background[2][reference]];
|
|
150
|
+
let hasSimilar = false;
|
|
151
|
+
for (let iy = Math.max(minY - HAS_SIMILAR_RADIUS, 0); iy < Math.min(maxY + HAS_SIMILAR_RADIUS + 1, height) && !hasSimilar; iy++)
|
|
152
|
+
for (let ix = Math.max(minX - HAS_SIMILAR_RADIUS, 0); ix < Math.min(maxX + HAS_SIMILAR_RADIUS + 1, width); ix++)
|
|
153
|
+
if (similar(background, reference, planes, iy * stride + ix, HAS_SIMILAR)) {
|
|
154
|
+
hasSimilar = true;
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
if (!hasSimilar)
|
|
158
|
+
continue;
|
|
159
|
+
const w = maxX - minX + 1, h = maxY - minY + 1;
|
|
160
|
+
const fpixels = [0, 1, 2].map(() => new Float32Array(w * h)), quantized = new Int8Array(3 * w * h);
|
|
161
|
+
let peak = 0;
|
|
162
|
+
for (const c of [1, 0, 2]) {
|
|
163
|
+
for (let iy = 0; iy < h; iy++) {
|
|
164
|
+
for (let ix = 0; ix < w; ix++) {
|
|
165
|
+
const v = fpixels[c][iy * w + ix] = at(c, minX + ix, minY + iy) - ref[c];
|
|
166
|
+
const q = Math.trunc(Math.fround(v) / DEQUANT[c]);
|
|
167
|
+
quantized[c * w * h + iy * w + ix] = q;
|
|
168
|
+
peak = Math.max(peak, Math.abs(q));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (peak < MIN_PEAK)
|
|
173
|
+
continue;
|
|
174
|
+
found.push({ w, h, fpixels, key: `${w}x${h}:${quantized.join(',')}`, positions: [[minX, minY]] });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const unique = new Map();
|
|
178
|
+
for (const c of found) {
|
|
179
|
+
const u = unique.get(c.key);
|
|
180
|
+
if (u)
|
|
181
|
+
u.positions.push(...c.positions);
|
|
182
|
+
else
|
|
183
|
+
unique.set(c.key, c);
|
|
184
|
+
}
|
|
185
|
+
const repeated = [...unique.values()].filter(c => c.positions.length >= 2);
|
|
186
|
+
if (!repeated.length || Math.max(...repeated.map(c => c.w * c.h)) < MIN_MAX_PATCH_SIZE)
|
|
187
|
+
return [];
|
|
188
|
+
return repeated;
|
|
189
|
+
}
|
|
190
|
+
export function findPatches(planes, stride, width, height, distance, numExtra) {
|
|
191
|
+
const info = candidates(planes, stride, width, height);
|
|
192
|
+
if (!info.length)
|
|
193
|
+
return null;
|
|
194
|
+
info.sort((a, b) => b.w * b.h - a.w * a.h);
|
|
195
|
+
let total = 0, maxW = 0, maxH = 0;
|
|
196
|
+
for (const p of info) {
|
|
197
|
+
total += p.w * p.h;
|
|
198
|
+
maxW = Math.max(maxW, p.w);
|
|
199
|
+
maxH = Math.max(maxH, p.h);
|
|
200
|
+
}
|
|
201
|
+
let refW = Math.max(maxW, Math.sqrt(total)), refH = Math.max(maxH, Math.sqrt(total));
|
|
202
|
+
let spots = [], usedH = 0;
|
|
203
|
+
for (;;) {
|
|
204
|
+
refW = Math.floor(refW * 1.05 + 1);
|
|
205
|
+
refH = Math.floor(refH * 1.05 + 1);
|
|
206
|
+
const occupied = new Uint8Array(refW * refH);
|
|
207
|
+
spots = [];
|
|
208
|
+
usedH = 0;
|
|
209
|
+
let ok = true;
|
|
210
|
+
for (const p of info) {
|
|
211
|
+
let spot = null;
|
|
212
|
+
for (let y0 = 0; y0 + p.h <= refH && !spot; y0++) {
|
|
213
|
+
for (let x0 = 0; x0 + p.w <= refW;) {
|
|
214
|
+
let blocked = -1;
|
|
215
|
+
for (let y = y0; y < y0 + p.h && blocked < 0; y++)
|
|
216
|
+
for (let x = x0; x < x0 + p.w; x++)
|
|
217
|
+
if (occupied[y * refW + x]) {
|
|
218
|
+
blocked = x;
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
if (blocked < 0) {
|
|
222
|
+
spot = [x0, y0];
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
x0 = blocked + 1;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (!spot) {
|
|
229
|
+
ok = false;
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
for (let y = spot[1]; y < spot[1] + p.h; y++)
|
|
233
|
+
occupied.fill(1, y * refW + spot[0], y * refW + spot[0] + p.w);
|
|
234
|
+
spots.push(spot);
|
|
235
|
+
usedH = Math.max(usedH, spot[1] + p.h);
|
|
236
|
+
}
|
|
237
|
+
if (ok)
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
refH = usedH;
|
|
241
|
+
if (refW > 1024 || refH > 1024)
|
|
242
|
+
return null;
|
|
243
|
+
const factors = [65536 / (1 + 23 * distance), 4096 / (1 + 14 * distance), 4096 / (1 + 14 * distance)];
|
|
244
|
+
const lfBits = factors.map(f => f16(128 / f)), steps = lfBits.map(b => fromF16(b) / 128);
|
|
245
|
+
const n = refW * refH, ints = [0, 1, 2].map(() => new Int32Array(n)), decoded = [0, 1, 2].map(() => new Float32Array(n));
|
|
246
|
+
info.forEach((p, k) => {
|
|
247
|
+
const [x0, y0] = spots[k];
|
|
248
|
+
for (let y = 0; y < p.h; y++) {
|
|
249
|
+
for (let x = 0; x < p.w; x++) {
|
|
250
|
+
const i = (y0 + y) * refW + x0 + x, j = y * p.w + x;
|
|
251
|
+
for (let c = 0; c < 3; c++)
|
|
252
|
+
ints[c][i] = Math.round(p.fpixels[c][j] / steps[c]);
|
|
253
|
+
for (let c = 0; c < 3; c++)
|
|
254
|
+
decoded[c][i] = Math.fround(ints[c][i] * steps[c]);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
info.forEach((p, k) => {
|
|
259
|
+
const [x0, y0] = spots[k];
|
|
260
|
+
for (const [px, py] of p.positions)
|
|
261
|
+
for (let y = 0; y < p.h; y++)
|
|
262
|
+
for (let x = 0; x < p.w; x++)
|
|
263
|
+
for (let c = 0; c < 3; c++)
|
|
264
|
+
planes[c][(py + y) * stride + px + x] -= decoded[c][(y0 + y) * refW + x0 + x];
|
|
265
|
+
});
|
|
266
|
+
const frame = new BitWriter();
|
|
267
|
+
frame.write(1, 0);
|
|
268
|
+
frame.write(2, 2);
|
|
269
|
+
frame.write(1, 1);
|
|
270
|
+
frame.write(2, 0);
|
|
271
|
+
frame.write(2, 0);
|
|
272
|
+
for (let e = 0; e < numExtra; e++)
|
|
273
|
+
frame.write(2, 0);
|
|
274
|
+
frame.write(2, 3);
|
|
275
|
+
frame.write(1, 1);
|
|
276
|
+
const sizes = [[8, 0], [11, 256], [14, 2304], [30, 18688]];
|
|
277
|
+
u32(frame, refW, sizes);
|
|
278
|
+
u32(frame, refH, sizes);
|
|
279
|
+
frame.write(2, REFERENCE);
|
|
280
|
+
frame.write(1, 1);
|
|
281
|
+
u32(frame, 0, [[0, 0], [4, 0], [5, 16], [10, 48]]);
|
|
282
|
+
frame.write(1, 0);
|
|
283
|
+
frame.write(1, 0);
|
|
284
|
+
frame.write(2, 0);
|
|
285
|
+
frame.write(2, 0);
|
|
286
|
+
frame.write(2, 0);
|
|
287
|
+
const section = new BitWriter();
|
|
288
|
+
section.write(1, 0);
|
|
289
|
+
for (const b of [lfBits[0], lfBits[1], lfBits[2]])
|
|
290
|
+
section.write(16, b);
|
|
291
|
+
const channels = [ints[1], ints[0], ints[2].map((v, i) => v - ints[1][i])];
|
|
292
|
+
for (let e = 0; e < numExtra; e++)
|
|
293
|
+
channels.push(new Int32Array(n));
|
|
294
|
+
let root = { predictor: PRED_GRADIENT };
|
|
295
|
+
for (let c = channels.length - 2; c >= 0; c--)
|
|
296
|
+
root = { property: PROP_CHANNEL, split: c, above: root, below: { predictor: PRED_GRADIENT } };
|
|
297
|
+
const tree = new MATree(root), tokens = new Tokens();
|
|
298
|
+
channels.forEach((data, c) => channelTokens(tokens, tree, data, refW, refH, c, 0));
|
|
299
|
+
const code = new EntropyCode([tokens], tree.numLeaves);
|
|
300
|
+
section.write(1, 1);
|
|
301
|
+
tree.write(section);
|
|
302
|
+
code.writeHeader(section);
|
|
303
|
+
streamHeader(section);
|
|
304
|
+
code.writeTokens(section, tokens);
|
|
305
|
+
section.zeroPadToByte();
|
|
306
|
+
frame.write(1, 0);
|
|
307
|
+
frame.zeroPadToByte();
|
|
308
|
+
u32(frame, section.length, [[10, 0], [14, 1024], [22, 17408], [30, 4211712]]);
|
|
309
|
+
frame.zeroPadToByte();
|
|
310
|
+
frame.append(section);
|
|
311
|
+
const dictionary = new Tokens();
|
|
312
|
+
dictionary.push(0, info.length);
|
|
313
|
+
info.forEach((p, k) => {
|
|
314
|
+
dictionary.push(1, REFERENCE);
|
|
315
|
+
dictionary.push(3, spots[k][0]);
|
|
316
|
+
dictionary.push(3, spots[k][1]);
|
|
317
|
+
dictionary.push(2, p.w - 1);
|
|
318
|
+
dictionary.push(2, p.h - 1);
|
|
319
|
+
dictionary.push(7, p.positions.length - 1);
|
|
320
|
+
p.positions.forEach(([x, y], i) => {
|
|
321
|
+
if (i) {
|
|
322
|
+
dictionary.push(6, packSigned(x - p.positions[i - 1][0]));
|
|
323
|
+
dictionary.push(6, packSigned(y - p.positions[i - 1][1]));
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
dictionary.push(4, x);
|
|
327
|
+
dictionary.push(4, y);
|
|
328
|
+
}
|
|
329
|
+
dictionary.push(5, 2);
|
|
330
|
+
for (let e = 0; e < numExtra; e++)
|
|
331
|
+
dictionary.push(5, 0);
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
const dictionaryCode = new EntropyCode([dictionary], 10);
|
|
335
|
+
return {
|
|
336
|
+
frame,
|
|
337
|
+
writeDictionary: (w) => {
|
|
338
|
+
dictionaryCode.writeHeader(w);
|
|
339
|
+
dictionaryCode.writeTokens(w, dictionary);
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { findPatches } from "./patches-enc.js";
|
|
1
2
|
import { BitWriter } from "./bit-writer.js";
|
|
2
3
|
import { imageHeader, u32 } from "./headers-enc.js";
|
|
3
4
|
import { EntropyCode, Tokens, packSigned } from "./entropy-enc.js";
|
|
@@ -138,6 +139,7 @@ export function encodeVarDCT(rgba, width, height, { distance, effort }) {
|
|
|
138
139
|
const qmMul = [1.25 ** (xqmScale - 2), 1, 1.25 ** (bqmScale - 2)];
|
|
139
140
|
const epfIters = [0.7, 1.5, 4].filter(t => distance >= t).length;
|
|
140
141
|
const lfStep = LF_DEQUANT.map(m => m * 512 / (globalScale * quantLf));
|
|
142
|
+
const patches = effort >= 2 ? findPatches(planes, pw, width, height, distance, alpha ? 1 : 0) : null;
|
|
141
143
|
const types = effort ? chooseVarblocks(planes, pw, bw, bh, initialQuantField(distance * 0.62, planes, pw, bh * 8, 0.725 / (distance * 0.62)), distance, effort === 1 ? 5 : 4) : new Int8Array(bw * bh);
|
|
142
144
|
const muls = new Int32Array(bw * bh).fill(mul);
|
|
143
145
|
const lfValues = [0, 1, 2].map(() => new Float32Array(bw * bh));
|
|
@@ -299,6 +301,7 @@ export function encodeVarDCT(rgba, width, height, { distance, effort }) {
|
|
|
299
301
|
const sections = [];
|
|
300
302
|
{
|
|
301
303
|
const w = new BitWriter();
|
|
304
|
+
patches?.writeDictionary(w);
|
|
302
305
|
w.write(1, 1);
|
|
303
306
|
u32(w, globalScale, [[11, 1], [11, 2049], [12, 4097], [16, 8193]]);
|
|
304
307
|
u32(w, quantLf, [[0, 16], [5, 1], [8, 1], [16, 1]]);
|
|
@@ -343,7 +346,9 @@ export function encodeVarDCT(rgba, width, height, { distance, effort }) {
|
|
|
343
346
|
}
|
|
344
347
|
const out = new BitWriter();
|
|
345
348
|
imageHeader(out, width, height, alpha ? 4 : 3, true);
|
|
346
|
-
|
|
349
|
+
if (patches)
|
|
350
|
+
out.append(patches.frame);
|
|
351
|
+
frameHeader(out, xqmScale, bqmScale, epfIters, alpha !== null, !!patches);
|
|
347
352
|
if (numGroups === 1) {
|
|
348
353
|
const all = new BitWriter();
|
|
349
354
|
for (const s of sections)
|
|
@@ -367,11 +372,16 @@ export function encodeVarDCT(rgba, width, height, { distance, effort }) {
|
|
|
367
372
|
}
|
|
368
373
|
return bytes;
|
|
369
374
|
}
|
|
370
|
-
function frameHeader(w, xqmScale, bqmScale, epfIters, alpha) {
|
|
375
|
+
function frameHeader(w, xqmScale, bqmScale, epfIters, alpha, patches) {
|
|
371
376
|
w.write(1, 0);
|
|
372
377
|
w.write(2, 0);
|
|
373
378
|
w.write(1, 0);
|
|
374
|
-
|
|
379
|
+
if (patches) {
|
|
380
|
+
w.write(2, 1);
|
|
381
|
+
w.write(4, 1);
|
|
382
|
+
}
|
|
383
|
+
else
|
|
384
|
+
w.write(2, 0);
|
|
375
385
|
w.write(2, 0);
|
|
376
386
|
if (alpha)
|
|
377
387
|
w.write(2, 0);
|
package/dist/codecs/png.js
CHANGED
|
@@ -4,7 +4,7 @@ import { concat } from "../util/mem.js";
|
|
|
4
4
|
import { littleEndian } from "../util/alpha.js";
|
|
5
5
|
import { encode as utf8, decode as utf8Decode } from "../util/utf8.js";
|
|
6
6
|
import { orientation as exifOrientation, xmpOrientation } from "./exif.js";
|
|
7
|
-
import { compressAsync, decompressAsync } from "./zlib.js";
|
|
7
|
+
import { compressAsync, decompressAsync, decompress } from "./zlib.js";
|
|
8
8
|
const HEAD = Uint8Array.of(137, 80, 78, 71, 13, 10, 26, 10);
|
|
9
9
|
const IHDR = 0x49484452;
|
|
10
10
|
const PLTE = 0x504c5445;
|
|
@@ -12,6 +12,8 @@ const tRNS = 0x74524e53;
|
|
|
12
12
|
const IDAT = 0x49444154;
|
|
13
13
|
const IEND = 0x49454e44;
|
|
14
14
|
const eXIf = 0x65584966;
|
|
15
|
+
const iCCP = 0x69434350;
|
|
16
|
+
const sRGB = 0x73524742;
|
|
15
17
|
const tEXt = 0x74455874;
|
|
16
18
|
const zTXt = 0x7a545874;
|
|
17
19
|
const iTXt = 0x69545874;
|
|
@@ -346,6 +348,7 @@ function parse(array) {
|
|
|
346
348
|
let transparency = null;
|
|
347
349
|
const idat = [];
|
|
348
350
|
let exif = null;
|
|
351
|
+
let iccp = null, srgb = false;
|
|
349
352
|
const text = [];
|
|
350
353
|
let animation = null;
|
|
351
354
|
const frames = [];
|
|
@@ -429,6 +432,10 @@ function parse(array) {
|
|
|
429
432
|
}
|
|
430
433
|
else if (type === eXIf)
|
|
431
434
|
exif ??= chunk;
|
|
435
|
+
else if (type === iCCP)
|
|
436
|
+
iccp ??= chunk;
|
|
437
|
+
else if (type === sRGB)
|
|
438
|
+
srgb = true;
|
|
432
439
|
else if (type === tEXt || type === zTXt || type === iTXt)
|
|
433
440
|
text.push({ type, chunk });
|
|
434
441
|
else if (type === IEND)
|
|
@@ -455,23 +462,30 @@ function parse(array) {
|
|
|
455
462
|
if (!frames.length || frames.length < animation.frames || frames.some(f => !f.data.length))
|
|
456
463
|
animationError = true;
|
|
457
464
|
}
|
|
458
|
-
return { header, palette, transparency, idat, exif, text, animation, frames, animationError };
|
|
465
|
+
return { header, palette, transparency, idat, exif, iccp: srgb ? null : iccp, text, animation, frames, animationError };
|
|
459
466
|
}
|
|
460
|
-
|
|
461
|
-
const
|
|
462
|
-
const
|
|
463
|
-
const bitsPerPixel = channels * depth;
|
|
464
|
-
const bpp = Math.max(1, bitsPerPixel >> 3);
|
|
465
|
-
const passes = interlace ? ADAM7.map(([xs, ys, dx, dy]) => [xs, ys, dx, dy, Math.ceil((width - xs) / dx), Math.ceil((height - ys) / dy)]) : [[0, 0, 1, 1, width, height]];
|
|
467
|
+
function passesOf(p, width, height) {
|
|
468
|
+
const bitsPerPixel = COLOR_TYPE_CHANNELS[p.header.colorType] * p.header.depth;
|
|
469
|
+
const passes = p.header.interlace ? ADAM7.map(([xs, ys, dx, dy]) => [xs, ys, dx, dy, Math.ceil((width - xs) / dx), Math.ceil((height - ys) / dy)]) : [[0, 0, 1, 1, width, height]];
|
|
466
470
|
let expected = 0;
|
|
467
471
|
for (const pass of passes) {
|
|
468
472
|
const [, , , , pw, ph] = pass;
|
|
469
473
|
if (pw > 0 && ph > 0)
|
|
470
474
|
expected += ph * (1 + Math.ceil(pw * bitsPerPixel / 8));
|
|
471
475
|
}
|
|
476
|
+
return { passes, expected };
|
|
477
|
+
}
|
|
478
|
+
async function decodeImageData(p, chunks, width, height) {
|
|
479
|
+
const { expected } = passesOf(p, width, height);
|
|
472
480
|
if (!chunks.length)
|
|
473
481
|
throw new Error('PNG: missing image data');
|
|
474
|
-
|
|
482
|
+
return imagePixels(p, await decompressAsync(chunks.length === 1 ? chunks[0] : concat(chunks), expected), width, height);
|
|
483
|
+
}
|
|
484
|
+
function imagePixels(p, data, width, height) {
|
|
485
|
+
const { depth, colorType, interlace } = p.header;
|
|
486
|
+
const bitsPerPixel = COLOR_TYPE_CHANNELS[colorType] * depth;
|
|
487
|
+
const bpp = Math.max(1, bitsPerPixel >> 3);
|
|
488
|
+
const { passes, expected } = passesOf(p, width, height);
|
|
475
489
|
if (data.length < expected)
|
|
476
490
|
throw new Error('PNG: image data is truncated');
|
|
477
491
|
const rgba = new Uint8Array(width * height * 4);
|
|
@@ -493,7 +507,29 @@ export async function decode(array, maxPixels = MAX_PIXELS) {
|
|
|
493
507
|
const { width, height } = p.header;
|
|
494
508
|
checkPixels(width, height, maxPixels);
|
|
495
509
|
const pixels = await decodeImageData(p, p.idat, width, height);
|
|
496
|
-
return { width, height, pixels, orientation: p.exif ? exifOrientation(p.exif) : await textOrientation(p.text) };
|
|
510
|
+
return { width, height, pixels, orientation: p.exif ? exifOrientation(p.exif) : await textOrientation(p.text), icc: await profile(p.iccp) };
|
|
511
|
+
}
|
|
512
|
+
async function profile(chunk) {
|
|
513
|
+
if (!chunk)
|
|
514
|
+
return undefined;
|
|
515
|
+
const separator = chunk.indexOf(0);
|
|
516
|
+
if (separator < 1 || separator > 79 || chunk[separator + 1] !== 0)
|
|
517
|
+
return undefined;
|
|
518
|
+
try {
|
|
519
|
+
return await decompressAsync(chunk.subarray(separator + 2), 1 << 26);
|
|
520
|
+
}
|
|
521
|
+
catch {
|
|
522
|
+
return undefined;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
export function decodeSync(array, maxPixels = MAX_PIXELS) {
|
|
526
|
+
const p = parse(array);
|
|
527
|
+
const { width, height } = p.header;
|
|
528
|
+
checkPixels(width, height, maxPixels);
|
|
529
|
+
if (!p.idat.length)
|
|
530
|
+
throw new Error('PNG: missing image data');
|
|
531
|
+
const { expected } = passesOf(p, width, height);
|
|
532
|
+
return { width, height, pixels: imagePixels(p, decompress(p.idat.length === 1 ? p.idat[0] : concat(p.idat), expected), width, height) };
|
|
497
533
|
}
|
|
498
534
|
export function isAnimated(array) {
|
|
499
535
|
try {
|
|
@@ -510,7 +546,7 @@ export async function decodeAnimation(array, onlyFirstFrame = false, maxPixels =
|
|
|
510
546
|
checkPixels(width, height, maxPixels, !p.animation || p.animationError || onlyFirstFrame ? 1 : Math.max(1, p.frames.length));
|
|
511
547
|
if (!p.animation || p.animationError) {
|
|
512
548
|
const pixels = await decodeImageData(p, p.idat, width, height);
|
|
513
|
-
return { width, height, plays: 0, frames: [{ x: 0, y: 0, width, height, duration: 0, dispose: 0, blend: 0, pixels }] };
|
|
549
|
+
return { width, height, plays: 0, frames: [{ x: 0, y: 0, width, height, duration: 0, dispose: 0, blend: 0, pixels }], icc: await profile(p.iccp) };
|
|
514
550
|
}
|
|
515
551
|
const frames = [];
|
|
516
552
|
for (const { control: c, data } of p.frames) {
|
|
@@ -525,7 +561,7 @@ export async function decodeAnimation(array, onlyFirstFrame = false, maxPixels =
|
|
|
525
561
|
if (onlyFirstFrame)
|
|
526
562
|
break;
|
|
527
563
|
}
|
|
528
|
-
return { width, height, plays: p.animation.plays, frames };
|
|
564
|
+
return { width, height, plays: p.animation.plays, frames, icc: await profile(p.iccp) };
|
|
529
565
|
}
|
|
530
566
|
export function compositeAnimation({ width, height, frames }) {
|
|
531
567
|
const canvas = new Uint8Array(width * height * 4);
|
package/dist/codecs/tiff.js
CHANGED
|
@@ -107,6 +107,8 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
|
|
|
107
107
|
const v = values(tag);
|
|
108
108
|
return v && v.length ? v[0] : fallback;
|
|
109
109
|
}
|
|
110
|
+
const profileTag = tags.get(34675);
|
|
111
|
+
const icc = profileTag && TYPE_SIZES[profileTag.type] === 1 ? data.subarray(profileTag.offset, profileTag.offset + profileTag.count) : undefined;
|
|
110
112
|
const orientationTag = value(TAGS.Orientation, 1);
|
|
111
113
|
const orientation = orientationTag >= 1 && orientationTag <= 8 ? orientationTag : 1;
|
|
112
114
|
const width = value(TAGS.ImageWidth);
|
|
@@ -174,7 +176,7 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
|
|
|
174
176
|
const decoded = jpeg.decode(stream, { transform });
|
|
175
177
|
blitRGBA(decoded.pixels, decoded.width, decoded.height, pixels, width, height, (s % segmentsAcross) * segmentWidth, (s / segmentsAcross | 0) * segmentHeight);
|
|
176
178
|
}
|
|
177
|
-
return { width, height, pixels, orientation };
|
|
179
|
+
return { width, height, pixels, orientation, icc };
|
|
178
180
|
}
|
|
179
181
|
const samplesPerRow = planar ? 1 : samplesPerPixel;
|
|
180
182
|
const planes = planar ? samplesPerPixel : 1;
|
|
@@ -195,7 +197,7 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
|
|
|
195
197
|
written += expected;
|
|
196
198
|
}
|
|
197
199
|
ycbcrUnitsToRGBA(units, width, height, h, v, ycbcrConversion(values), pixels);
|
|
198
|
-
return { width, height, pixels, orientation };
|
|
200
|
+
return { width, height, pixels, orientation, icc };
|
|
199
201
|
}
|
|
200
202
|
const rowBytes = Math.ceil(width * samplesPerRow * bps / 8);
|
|
201
203
|
const planeBytes = rowBytes * height;
|
|
@@ -274,7 +276,7 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
|
|
|
274
276
|
}
|
|
275
277
|
}
|
|
276
278
|
}
|
|
277
|
-
return { width, height, pixels, orientation };
|
|
279
|
+
return { width, height, pixels, orientation, icc };
|
|
278
280
|
}
|
|
279
281
|
function sampleReader(raw, bps, sampleFormat, little, range) {
|
|
280
282
|
const view = new DataView(raw.buffer, raw.byteOffset, raw.byteLength);
|
package/dist/codecs/vp8-enc.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* Lossy WebP (VP8) encoding, ported from libwebp's encoder (src/enc: analysis_enc.c, quant_enc.c, frame_enc.c,
|
|
3
3
|
* iterator_enc.c, cost_enc.c, token_enc.c, tree_enc.c, syntax_enc.c, filter_enc.c, alpha_enc.c; src/utils:
|
|
4
|
-
* bit_writer_utils.c, filters_utils.c) with its default settings and any method (effort: 0-2 decide modes by
|
|
4
|
+
* bit_writer_utils.c, filters_utils.c, quant_levels_utils.c) with its default settings and any method (effort: 0-2 decide modes by
|
|
5
5
|
* distortion, 3-6 by rate and distortion, 5-6 with trellis quantization), so the output is the same as cwebp's.
|
|
6
6
|
* Copyright (c) 2010 Google Inc., BSD-3-Clause License (see THIRD_PARTY_NOTICES.md)
|
|
7
7
|
*/
|
|
@@ -1849,6 +1849,57 @@ function partition0(enc) {
|
|
|
1849
1849
|
codeIntraModes(enc, bw);
|
|
1850
1850
|
return bw.finish();
|
|
1851
1851
|
}
|
|
1852
|
+
function quantizeLevels(a, levels) {
|
|
1853
|
+
const freq = new Int32Array(256), qLevel = new Int32Array(256), invQLevel = new Float64Array(256);
|
|
1854
|
+
let minS = 255, maxS = 0, levelsIn = 0;
|
|
1855
|
+
for (let n = 0; n < a.length; n++) {
|
|
1856
|
+
const v = a[n];
|
|
1857
|
+
if (!freq[v])
|
|
1858
|
+
levelsIn++;
|
|
1859
|
+
if (v < minS)
|
|
1860
|
+
minS = v;
|
|
1861
|
+
if (v > maxS)
|
|
1862
|
+
maxS = v;
|
|
1863
|
+
freq[v]++;
|
|
1864
|
+
}
|
|
1865
|
+
if (levelsIn <= levels)
|
|
1866
|
+
return;
|
|
1867
|
+
for (let i = 0; i < levels; i++)
|
|
1868
|
+
invQLevel[i] = minS + (maxS - minS) * i / (levels - 1);
|
|
1869
|
+
qLevel[maxS] = levels - 1;
|
|
1870
|
+
const threshold = 1e-4 * a.length;
|
|
1871
|
+
const qSum = new Float64Array(256), qCount = new Float64Array(256);
|
|
1872
|
+
let lastErr = 1e38;
|
|
1873
|
+
for (let iter = 0; iter < 6; iter++) {
|
|
1874
|
+
qSum.fill(0);
|
|
1875
|
+
qCount.fill(0);
|
|
1876
|
+
for (let s = minS, slot = 0; s <= maxS; s++) {
|
|
1877
|
+
while (slot < levels - 1 && 2 * s > invQLevel[slot] + invQLevel[slot + 1])
|
|
1878
|
+
slot++;
|
|
1879
|
+
if (freq[s] > 0) {
|
|
1880
|
+
qSum[slot] += s * freq[s];
|
|
1881
|
+
qCount[slot] += freq[s];
|
|
1882
|
+
}
|
|
1883
|
+
qLevel[s] = slot;
|
|
1884
|
+
}
|
|
1885
|
+
for (let slot = 1; slot < levels - 1; slot++)
|
|
1886
|
+
if (qCount[slot] > 0)
|
|
1887
|
+
invQLevel[slot] = qSum[slot] / qCount[slot];
|
|
1888
|
+
let err = 0;
|
|
1889
|
+
for (let s = minS; s <= maxS; s++) {
|
|
1890
|
+
const e = s - invQLevel[qLevel[s]];
|
|
1891
|
+
err += freq[s] * e * e;
|
|
1892
|
+
}
|
|
1893
|
+
if (lastErr - err < threshold)
|
|
1894
|
+
break;
|
|
1895
|
+
lastErr = err;
|
|
1896
|
+
}
|
|
1897
|
+
const map = new Uint8Array(256);
|
|
1898
|
+
for (let s = minS; s <= maxS; s++)
|
|
1899
|
+
map[s] = Math.trunc(invQLevel[qLevel[s]] + 0.5);
|
|
1900
|
+
for (let n = 0; n < a.length; n++)
|
|
1901
|
+
a[n] = map[a[n]];
|
|
1902
|
+
}
|
|
1852
1903
|
function filterAlpha(a, width, height, filter) {
|
|
1853
1904
|
const out = new Uint8Array(a.length);
|
|
1854
1905
|
for (let y = 0; y < height; y++) {
|
|
@@ -1899,7 +1950,12 @@ function estimateBestFilter(a, width, height) {
|
|
|
1899
1950
|
}
|
|
1900
1951
|
return best;
|
|
1901
1952
|
}
|
|
1902
|
-
export function encodeAlpha(a, width, height, method = 4) {
|
|
1953
|
+
export function encodeAlpha(a, width, height, method = 4, quality = 100) {
|
|
1954
|
+
const reduce = quality < 100;
|
|
1955
|
+
if (reduce) {
|
|
1956
|
+
a = a.slice();
|
|
1957
|
+
quantizeLevels(a, quality <= 70 ? 2 + Math.trunc(quality / 5) : 16 + (quality - 70) * 8);
|
|
1958
|
+
}
|
|
1903
1959
|
let colors = 0;
|
|
1904
1960
|
const seen = new Uint8Array(256);
|
|
1905
1961
|
for (let i = 0; i < a.length; i++)
|
|
@@ -1922,7 +1978,7 @@ export function encodeAlpha(a, width, height, method = 4) {
|
|
|
1922
1978
|
method = 0;
|
|
1923
1979
|
}
|
|
1924
1980
|
const out = new Uint8Array(1 + stream.length);
|
|
1925
|
-
out[0] = method | filter << 2;
|
|
1981
|
+
out[0] = method | filter << 2 | (reduce ? 1 << 4 : 0);
|
|
1926
1982
|
out.set(stream, 1);
|
|
1927
1983
|
if (!best || out.length < best.length)
|
|
1928
1984
|
best = out;
|