imagescript 3.8.0 → 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/gif.js +21 -40
- package/dist/codecs/jpeg.js +61 -33
- package/dist/codecs/jxl/patches-enc.js +342 -0
- package/dist/codecs/jxl/vardct-enc.js +13 -3
- package/dist/codecs/png.js +106 -18
- package/dist/codecs/tiff.js +5 -3
- package/dist/codecs/vp8-enc.js +59 -3
- package/dist/codecs/vp8.js +21 -17
- 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
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { crc32 } from "./crc.js";
|
|
2
2
|
import { MAX_PIXELS, checkPixels } from "../util/limits.js";
|
|
3
3
|
import { concat } from "../util/mem.js";
|
|
4
|
+
import { littleEndian } from "../util/alpha.js";
|
|
4
5
|
import { encode as utf8, decode as utf8Decode } from "../util/utf8.js";
|
|
5
6
|
import { orientation as exifOrientation, xmpOrientation } from "./exif.js";
|
|
6
|
-
import { compressAsync, decompressAsync } from "./zlib.js";
|
|
7
|
+
import { compressAsync, decompressAsync, decompress } from "./zlib.js";
|
|
7
8
|
const HEAD = Uint8Array.of(137, 80, 78, 71, 13, 10, 26, 10);
|
|
8
9
|
const IHDR = 0x49484452;
|
|
9
10
|
const PLTE = 0x504c5445;
|
|
@@ -11,6 +12,8 @@ const tRNS = 0x74524e53;
|
|
|
11
12
|
const IDAT = 0x49444154;
|
|
12
13
|
const IEND = 0x49454e44;
|
|
13
14
|
const eXIf = 0x65584966;
|
|
15
|
+
const iCCP = 0x69434350;
|
|
16
|
+
const sRGB = 0x73524742;
|
|
14
17
|
const tEXt = 0x74455874;
|
|
15
18
|
const zTXt = 0x7a545874;
|
|
16
19
|
const iTXt = 0x69545874;
|
|
@@ -345,6 +348,7 @@ function parse(array) {
|
|
|
345
348
|
let transparency = null;
|
|
346
349
|
const idat = [];
|
|
347
350
|
let exif = null;
|
|
351
|
+
let iccp = null, srgb = false;
|
|
348
352
|
const text = [];
|
|
349
353
|
let animation = null;
|
|
350
354
|
const frames = [];
|
|
@@ -428,6 +432,10 @@ function parse(array) {
|
|
|
428
432
|
}
|
|
429
433
|
else if (type === eXIf)
|
|
430
434
|
exif ??= chunk;
|
|
435
|
+
else if (type === iCCP)
|
|
436
|
+
iccp ??= chunk;
|
|
437
|
+
else if (type === sRGB)
|
|
438
|
+
srgb = true;
|
|
431
439
|
else if (type === tEXt || type === zTXt || type === iTXt)
|
|
432
440
|
text.push({ type, chunk });
|
|
433
441
|
else if (type === IEND)
|
|
@@ -454,23 +462,30 @@ function parse(array) {
|
|
|
454
462
|
if (!frames.length || frames.length < animation.frames || frames.some(f => !f.data.length))
|
|
455
463
|
animationError = true;
|
|
456
464
|
}
|
|
457
|
-
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 };
|
|
458
466
|
}
|
|
459
|
-
|
|
460
|
-
const
|
|
461
|
-
const
|
|
462
|
-
const bitsPerPixel = channels * depth;
|
|
463
|
-
const bpp = Math.max(1, bitsPerPixel >> 3);
|
|
464
|
-
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]];
|
|
465
470
|
let expected = 0;
|
|
466
471
|
for (const pass of passes) {
|
|
467
472
|
const [, , , , pw, ph] = pass;
|
|
468
473
|
if (pw > 0 && ph > 0)
|
|
469
474
|
expected += ph * (1 + Math.ceil(pw * bitsPerPixel / 8));
|
|
470
475
|
}
|
|
476
|
+
return { passes, expected };
|
|
477
|
+
}
|
|
478
|
+
async function decodeImageData(p, chunks, width, height) {
|
|
479
|
+
const { expected } = passesOf(p, width, height);
|
|
471
480
|
if (!chunks.length)
|
|
472
481
|
throw new Error('PNG: missing image data');
|
|
473
|
-
|
|
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);
|
|
474
489
|
if (data.length < expected)
|
|
475
490
|
throw new Error('PNG: image data is truncated');
|
|
476
491
|
const rgba = new Uint8Array(width * height * 4);
|
|
@@ -492,7 +507,29 @@ export async function decode(array, maxPixels = MAX_PIXELS) {
|
|
|
492
507
|
const { width, height } = p.header;
|
|
493
508
|
checkPixels(width, height, maxPixels);
|
|
494
509
|
const pixels = await decodeImageData(p, p.idat, width, height);
|
|
495
|
-
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) };
|
|
496
533
|
}
|
|
497
534
|
export function isAnimated(array) {
|
|
498
535
|
try {
|
|
@@ -509,7 +546,7 @@ export async function decodeAnimation(array, onlyFirstFrame = false, maxPixels =
|
|
|
509
546
|
checkPixels(width, height, maxPixels, !p.animation || p.animationError || onlyFirstFrame ? 1 : Math.max(1, p.frames.length));
|
|
510
547
|
if (!p.animation || p.animationError) {
|
|
511
548
|
const pixels = await decodeImageData(p, p.idat, width, height);
|
|
512
|
-
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) };
|
|
513
550
|
}
|
|
514
551
|
const frames = [];
|
|
515
552
|
for (const { control: c, data } of p.frames) {
|
|
@@ -524,7 +561,7 @@ export async function decodeAnimation(array, onlyFirstFrame = false, maxPixels =
|
|
|
524
561
|
if (onlyFirstFrame)
|
|
525
562
|
break;
|
|
526
563
|
}
|
|
527
|
-
return { width, height, plays: p.animation.plays, frames };
|
|
564
|
+
return { width, height, plays: p.animation.plays, frames, icc: await profile(p.iccp) };
|
|
528
565
|
}
|
|
529
566
|
export function compositeAnimation({ width, height, frames }) {
|
|
530
567
|
const canvas = new Uint8Array(width * height * 4);
|
|
@@ -648,12 +685,15 @@ function unfilter(data, offset, height, rowBytes, bpp, inPlace = false) {
|
|
|
648
685
|
else {
|
|
649
686
|
for (let i = row; i < row + first; i++)
|
|
650
687
|
out[i] = data[src + i] + out[i - rowBytes];
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
688
|
+
if (bpp === 3 || bpp === 4)
|
|
689
|
+
paethRow(data, src, out, row, rowBytes, bpp);
|
|
690
|
+
else
|
|
691
|
+
for (let i = row + bpp; i < row + rowBytes; i++) {
|
|
692
|
+
const a = out[i - bpp], b = out[i - rowBytes], c = out[i - rowBytes - bpp];
|
|
693
|
+
const pa = Math.abs(b - c), pb = Math.abs(a - c), pc = Math.abs(a + b - 2 * c);
|
|
694
|
+
const notA = (pb - pa | pc - pa) >> 31, takeC = (pc - pb) >> 31;
|
|
695
|
+
out[i] = data[src + i] + (a ^ (a ^ b ^ (b ^ c) & takeC) & notA);
|
|
696
|
+
}
|
|
657
697
|
}
|
|
658
698
|
}
|
|
659
699
|
else
|
|
@@ -662,6 +702,47 @@ function unfilter(data, offset, height, rowBytes, bpp, inPlace = false) {
|
|
|
662
702
|
}
|
|
663
703
|
return out;
|
|
664
704
|
}
|
|
705
|
+
function paethRow(data, src, out, row, rowBytes, bpp) {
|
|
706
|
+
const up = row - rowBytes, end = row + rowBytes;
|
|
707
|
+
let a0 = out[row], a1 = out[row + 1], a2 = out[row + 2], a3 = bpp === 4 ? out[row + 3] : 0;
|
|
708
|
+
let c0 = out[up], c1 = out[up + 1], c2 = out[up + 2], c3 = bpp === 4 ? out[up + 3] : 0;
|
|
709
|
+
for (let i = row + bpp; i < end; i += bpp) {
|
|
710
|
+
let b = out[i - rowBytes], pa = Math.abs(b - c0), pb = Math.abs(a0 - c0), pc = Math.abs(a0 + b - 2 * c0);
|
|
711
|
+
let notA = (pb - pa | pc - pa) >> 31, takeC = (pc - pb) >> 31;
|
|
712
|
+
a0 = data[src + i] + (a0 ^ (a0 ^ b ^ (b ^ c0) & takeC) & notA) & 255;
|
|
713
|
+
out[i] = a0;
|
|
714
|
+
c0 = b;
|
|
715
|
+
b = out[i + 1 - rowBytes];
|
|
716
|
+
pa = Math.abs(b - c1);
|
|
717
|
+
pb = Math.abs(a1 - c1);
|
|
718
|
+
pc = Math.abs(a1 + b - 2 * c1);
|
|
719
|
+
notA = (pb - pa | pc - pa) >> 31;
|
|
720
|
+
takeC = (pc - pb) >> 31;
|
|
721
|
+
a1 = data[src + i + 1] + (a1 ^ (a1 ^ b ^ (b ^ c1) & takeC) & notA) & 255;
|
|
722
|
+
out[i + 1] = a1;
|
|
723
|
+
c1 = b;
|
|
724
|
+
b = out[i + 2 - rowBytes];
|
|
725
|
+
pa = Math.abs(b - c2);
|
|
726
|
+
pb = Math.abs(a2 - c2);
|
|
727
|
+
pc = Math.abs(a2 + b - 2 * c2);
|
|
728
|
+
notA = (pb - pa | pc - pa) >> 31;
|
|
729
|
+
takeC = (pc - pb) >> 31;
|
|
730
|
+
a2 = data[src + i + 2] + (a2 ^ (a2 ^ b ^ (b ^ c2) & takeC) & notA) & 255;
|
|
731
|
+
out[i + 2] = a2;
|
|
732
|
+
c2 = b;
|
|
733
|
+
if (bpp === 4) {
|
|
734
|
+
b = out[i + 3 - rowBytes];
|
|
735
|
+
pa = Math.abs(b - c3);
|
|
736
|
+
pb = Math.abs(a3 - c3);
|
|
737
|
+
pc = Math.abs(a3 + b - 2 * c3);
|
|
738
|
+
notA = (pb - pa | pc - pa) >> 31;
|
|
739
|
+
takeC = (pc - pb) >> 31;
|
|
740
|
+
a3 = data[src + i + 3] + (a3 ^ (a3 ^ b ^ (b ^ c3) & takeC) & notA) & 255;
|
|
741
|
+
out[i + 3] = a3;
|
|
742
|
+
c3 = b;
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
665
746
|
function createConverter(colorType, depth, palette, transparency) {
|
|
666
747
|
const maxValue = (1 << depth) - 1;
|
|
667
748
|
const sample = depth === 8 ? (row, n) => row[n]
|
|
@@ -717,6 +798,13 @@ function createConverter(colorType, depth, palette, transparency) {
|
|
|
717
798
|
if (depth === 8) {
|
|
718
799
|
const [kr, kg, kb] = key || [-1, -1, -1];
|
|
719
800
|
return (pixels, rowBytes, w, h, out, place) => {
|
|
801
|
+
if (!key && place.step === 4 && littleEndian && !(out.byteOffset & 3)) {
|
|
802
|
+
const words = new Uint32Array(out.buffer, out.byteOffset, out.length >> 2);
|
|
803
|
+
for (let y = 0; y < h; y++)
|
|
804
|
+
for (let x = 0, s = y * rowBytes, o = place.row(y) >> 2; x < w; x++, s += 3, o++)
|
|
805
|
+
words[o] = (pixels[s] | pixels[s + 1] << 8 | pixels[s + 2] << 16 | 0xff000000) >>> 0;
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
720
808
|
for (let y = 0; y < h; y++) {
|
|
721
809
|
for (let x = 0, s = y * rowBytes, o = place.row(y), step = place.step; x < w; x++, s += 3, o += step) {
|
|
722
810
|
const r = pixels[s], g = pixels[s + 1], b = pixels[s + 2];
|
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);
|