imagescript 3.13.2 → 3.14.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/dist/animation-encoder.js +5 -3
- package/dist/animation.js +12 -7
- 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/webp.js +3 -8
- package/dist/image.d.ts +17 -12
- package/dist/image.js +16 -10
- package/dist/render/filter.js +6 -2
- package/dist/util/options.js +12 -0
- package/dist/util/worker.js +3 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { BitWriter } from "./bit-writer.js";
|
|
2
|
+
import { imageHeader, u32 } from "./headers-enc.js";
|
|
3
|
+
import { EntropyCode, MAX_TOKEN, Tokens, countTokens, writeTokens } from "./entropy-enc.js";
|
|
4
|
+
import { MATree, PRED_WEIGHTED, PROP_WP_ERROR, channelTokens, fixedTree, streamHeader } from "./modular-enc.js";
|
|
5
|
+
import { paletteValue, TR_PALETTE, TR_RCT } from "./modular.js";
|
|
6
|
+
import { frameHeader } from "./fast-lossless.js";
|
|
7
|
+
import { runChunks, shared, startThreads } from "../../util/threads.js";
|
|
8
|
+
const WP_CUTOFFS = [
|
|
9
|
+
-500, -392, -255, -191, -127, -95, -63, -47, -31, -23, -15, -11, -7, -4, -3, -1, 0, 1, 3, 5, 7, 11, 15, 23, 31, 47,
|
|
10
|
+
63, 95, 127, 191, 255, 392, 500
|
|
11
|
+
];
|
|
12
|
+
const PALETTE_COLORS = 1024, CHANNEL_COLORS_PERCENT = 95;
|
|
13
|
+
const IMPLICIT_SIZE = 64 + 125, GROUP_DIM = 256;
|
|
14
|
+
const NO_PALETTE = { w: 0, h: 0, hshift: 0, vshift: 0, data: new Int32Array(0) };
|
|
15
|
+
function palette(ch, begin, nb, maxColors, transforms) {
|
|
16
|
+
if (maxColors < 2)
|
|
17
|
+
return false;
|
|
18
|
+
const { w, h } = ch[begin], n = w * h, src = ch.slice(begin, begin + nb).map(c => c.data);
|
|
19
|
+
const key = (i) => {
|
|
20
|
+
let k = 0;
|
|
21
|
+
for (let c = 0; c < nb; c++)
|
|
22
|
+
k = k * 256 + src[c][i];
|
|
23
|
+
return k;
|
|
24
|
+
};
|
|
25
|
+
const colorOf = (k) => {
|
|
26
|
+
const color = new Array(nb);
|
|
27
|
+
for (let c = nb - 1; c >= 0; c--) {
|
|
28
|
+
color[c] = k % 256;
|
|
29
|
+
k = Math.floor(k / 256);
|
|
30
|
+
}
|
|
31
|
+
return color;
|
|
32
|
+
};
|
|
33
|
+
const implicitKeys = [];
|
|
34
|
+
for (let k = 0; k < IMPLICIT_SIZE; k++) {
|
|
35
|
+
let v = 0;
|
|
36
|
+
for (let c = 0; c < nb; c++)
|
|
37
|
+
v = v * 256 + paletteValue(NO_PALETTE, k, c, 8);
|
|
38
|
+
implicitKeys.push(v);
|
|
39
|
+
}
|
|
40
|
+
const implicit = new Set(implicitKeys);
|
|
41
|
+
const counts = new Map(), order = [];
|
|
42
|
+
for (let i = 0; i < n; i++) {
|
|
43
|
+
const k = key(i), count = counts.get(k);
|
|
44
|
+
if (count !== undefined) {
|
|
45
|
+
counts.set(k, count + 1);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
counts.set(k, 1);
|
|
49
|
+
if (implicit.has(k))
|
|
50
|
+
continue;
|
|
51
|
+
order.push(k);
|
|
52
|
+
if (order.length > maxColors)
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
let nbColors = order.length;
|
|
56
|
+
if (nbColors + (counts.size - nbColors) === 1)
|
|
57
|
+
return false;
|
|
58
|
+
for (const k of implicitKeys) {
|
|
59
|
+
if ((counts.get(k) ?? 0) > 10) {
|
|
60
|
+
nbColors++;
|
|
61
|
+
order.push(k);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const index = new Map();
|
|
65
|
+
implicitKeys.forEach((k, i) => index.set(k, nbColors + i));
|
|
66
|
+
if (nb >= 3) {
|
|
67
|
+
const luma = new Map();
|
|
68
|
+
for (const k of order) {
|
|
69
|
+
const [r, g, b, a] = colorOf(k);
|
|
70
|
+
let y = 0.299 * r + 0.587 * g + 0.114 * b + 0.1;
|
|
71
|
+
if (nb > 3)
|
|
72
|
+
y *= 1 + a;
|
|
73
|
+
luma.set(k, (counts.get(k) ?? 0) > 4 ? -y : y);
|
|
74
|
+
}
|
|
75
|
+
order.sort((a, b) => luma.get(a) - luma.get(b));
|
|
76
|
+
}
|
|
77
|
+
const meta = { w: nbColors, h: nb, data: new Int32Array(nbColors * nb) };
|
|
78
|
+
order.forEach((k, i) => {
|
|
79
|
+
colorOf(k).forEach((v, c) => meta.data[c * nbColors + i] = v);
|
|
80
|
+
index.set(k, i);
|
|
81
|
+
});
|
|
82
|
+
const indices = new Int32Array(n);
|
|
83
|
+
for (let i = 0; i < n; i++)
|
|
84
|
+
indices[i] = index.get(key(i));
|
|
85
|
+
ch.splice(begin, nb, { w, h, data: indices });
|
|
86
|
+
ch.unshift(meta);
|
|
87
|
+
transforms.push({ id: TR_PALETTE, beginC: begin, numC: nb, nbColors });
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
function channelPalette(ch, begin, maxColors, transforms) {
|
|
91
|
+
if (maxColors < 2)
|
|
92
|
+
return false;
|
|
93
|
+
const { w, h, data } = ch[begin];
|
|
94
|
+
let min = Infinity, max = -Infinity;
|
|
95
|
+
for (let i = 0; i < data.length; i++) {
|
|
96
|
+
if (data[i] < min)
|
|
97
|
+
min = data[i];
|
|
98
|
+
if (data[i] > max)
|
|
99
|
+
max = data[i];
|
|
100
|
+
}
|
|
101
|
+
const lookup = new Int32Array(max - min + 1);
|
|
102
|
+
let count = 0;
|
|
103
|
+
for (let i = 0; i < data.length; i++) {
|
|
104
|
+
if (lookup[data[i] - min])
|
|
105
|
+
continue;
|
|
106
|
+
lookup[data[i] - min] = 1;
|
|
107
|
+
if (++count > maxColors)
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
const meta = { w: count, h: 1, data: new Int32Array(count) };
|
|
111
|
+
for (let v = 0, k = 0; v < lookup.length; v++) {
|
|
112
|
+
if (!lookup[v])
|
|
113
|
+
continue;
|
|
114
|
+
meta.data[k] = v + min;
|
|
115
|
+
lookup[v] = k++;
|
|
116
|
+
}
|
|
117
|
+
const indices = new Int32Array(data.length);
|
|
118
|
+
for (let i = 0; i < data.length; i++)
|
|
119
|
+
indices[i] = lookup[data[i] - min];
|
|
120
|
+
ch[begin] = { w, h, data: indices };
|
|
121
|
+
ch.unshift(meta);
|
|
122
|
+
transforms.push({ id: TR_PALETTE, beginC: begin, numC: 1, nbColors: count });
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
function ycocg(ch, begin, transforms) {
|
|
126
|
+
const [r, g, b] = ch.slice(begin, begin + 3).map(c => c.data);
|
|
127
|
+
for (let i = 0; i < r.length; i++) {
|
|
128
|
+
const co = r[i] - b[i], tmp = b[i] + (co >> 1), cg = g[i] - tmp;
|
|
129
|
+
r[i] = tmp + (cg >> 1);
|
|
130
|
+
g[i] = co;
|
|
131
|
+
b[i] = cg;
|
|
132
|
+
}
|
|
133
|
+
transforms.push({ id: TR_RCT, beginC: begin });
|
|
134
|
+
}
|
|
135
|
+
export function modularGroupChunks({ channels, first, width, numLfGroups, numPixels, offsets, ctx, value }) {
|
|
136
|
+
const tree = new MATree(fixedTree(PROP_WP_ERROR, WP_CUTOFFS, PRED_WEIGHTED, numPixels));
|
|
137
|
+
const groupsPerRow = Math.ceil(width / GROUP_DIM);
|
|
138
|
+
return g => {
|
|
139
|
+
const x0 = (g % groupsPerRow) * GROUP_DIM, y0 = Math.floor(g / groupsPerRow) * GROUP_DIM, t = new Tokens();
|
|
140
|
+
t.ctx = ctx.subarray(offsets[g], offsets[g + 1]);
|
|
141
|
+
t.value = value.subarray(offsets[g], offsets[g + 1]);
|
|
142
|
+
channels.slice(first).forEach((c, i) => {
|
|
143
|
+
const w = Math.min(GROUP_DIM, c.w - x0), h = Math.min(GROUP_DIM, c.h - y0), part = new Int32Array(w * h);
|
|
144
|
+
for (let y = 0; y < h; y++)
|
|
145
|
+
part.set(c.data.subarray((y0 + y) * c.w + x0, (y0 + y) * c.w + x0 + w), y * w);
|
|
146
|
+
channelTokens(t, tree, part, w, h, first + i, 1 + 3 * numLfGroups + 17 + g);
|
|
147
|
+
});
|
|
148
|
+
const counts = new Float64Array(tree.numLeaves * MAX_TOKEN);
|
|
149
|
+
countTokens(counts, ctx, value, offsets[g], offsets[g + 1]);
|
|
150
|
+
return counts;
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
export function modularWriteChunks({ offsets, ctx, value, tables }) {
|
|
154
|
+
return g => {
|
|
155
|
+
const w = new BitWriter();
|
|
156
|
+
streamHeader(w);
|
|
157
|
+
writeTokens(w, tables, ctx, value, offsets[g], offsets[g + 1]);
|
|
158
|
+
w.zeroPadToByte();
|
|
159
|
+
return w.bytes().slice();
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
const MIN_THREADED_PIXELS = 1 << 19, PIXELS_PER_THREAD = 1 << 17;
|
|
163
|
+
export async function encodeModular(rgba, width, height, threads = 1) {
|
|
164
|
+
if (width < 1 || height < 1)
|
|
165
|
+
throw new RangeError('JPEG XL: dimensions must be at least 1');
|
|
166
|
+
const n = width * height;
|
|
167
|
+
let grey = true, opaque = true;
|
|
168
|
+
for (let i = 0; i < 4 * n && (grey || opaque); i += 4) {
|
|
169
|
+
if (rgba[i] !== rgba[i + 1] || rgba[i] !== rgba[i + 2])
|
|
170
|
+
grey = false;
|
|
171
|
+
if (rgba[i + 3] !== 255)
|
|
172
|
+
opaque = false;
|
|
173
|
+
}
|
|
174
|
+
const numColor = grey ? 1 : 3, numChans = numColor + (opaque ? 0 : 1);
|
|
175
|
+
const samples = grey ? [0] : [0, 1, 2];
|
|
176
|
+
if (!opaque)
|
|
177
|
+
samples.push(3);
|
|
178
|
+
const ch = samples.map(o => {
|
|
179
|
+
const data = new Int32Array(n);
|
|
180
|
+
for (let i = 0; i < n; i++)
|
|
181
|
+
data[i] = rgba[4 * i + o];
|
|
182
|
+
return { w: width, h: height, data };
|
|
183
|
+
});
|
|
184
|
+
const transforms = [];
|
|
185
|
+
let didPalette = 0;
|
|
186
|
+
if (numChans > 1) {
|
|
187
|
+
const maxColors = Math.min(Math.trunc(n * 0.2 * 8 * numChans * 0.0005 + n / 128 + 128), PALETTE_COLORS);
|
|
188
|
+
if (palette(ch, 0, numChans, maxColors, transforms))
|
|
189
|
+
didPalette = 1;
|
|
190
|
+
else if (numChans > 3 && palette(ch, 0, numChans - 1, maxColors, transforms))
|
|
191
|
+
didPalette = 1;
|
|
192
|
+
}
|
|
193
|
+
let nbMeta = didPalette;
|
|
194
|
+
{
|
|
195
|
+
const count = ch.length - nbMeta - didPalette;
|
|
196
|
+
for (let i = didPalette; i < count + didPalette; i++) {
|
|
197
|
+
const begin = i + nbMeta, c = ch[begin];
|
|
198
|
+
let min = Infinity, max = -Infinity;
|
|
199
|
+
for (let k = 0; k < c.data.length; k++) {
|
|
200
|
+
if (c.data[k] < min)
|
|
201
|
+
min = c.data[k];
|
|
202
|
+
if (c.data[k] > max)
|
|
203
|
+
max = c.data[k];
|
|
204
|
+
}
|
|
205
|
+
const maxColors = Math.min(Math.trunc(n / 16), Math.trunc(CHANNEL_COLORS_PERCENT / 100 * (max - min + 1)));
|
|
206
|
+
if (channelPalette(ch, begin, maxColors, transforms))
|
|
207
|
+
nbMeta++;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (numColor === 3 && ch.length - nbMeta >= 3)
|
|
211
|
+
ycocg(ch, nbMeta, transforms);
|
|
212
|
+
let first = nbMeta;
|
|
213
|
+
while (first < ch.length && ch[first].w <= GROUP_DIM && ch[first].h <= GROUP_DIM)
|
|
214
|
+
first++;
|
|
215
|
+
const groupsPerRow = Math.ceil(width / GROUP_DIM), groupRows = Math.ceil(height / GROUP_DIM), numGroups = groupsPerRow * groupRows;
|
|
216
|
+
const numLfGroups = Math.ceil(width / 2048) * Math.ceil(height / 2048);
|
|
217
|
+
const numPixels = ch.reduce((sum, c) => sum + c.w * c.h, 0);
|
|
218
|
+
const tree = new MATree(fixedTree(PROP_WP_ERROR, WP_CUTOFFS, PRED_WEIGHTED, numPixels));
|
|
219
|
+
const globalTokens = new Tokens();
|
|
220
|
+
for (let c = 0; c < first; c++)
|
|
221
|
+
channelTokens(globalTokens, tree, ch[c].data, ch[c].w, ch[c].h, c, 0);
|
|
222
|
+
const counts = new Float64Array(tree.numLeaves * MAX_TOKEN);
|
|
223
|
+
countTokens(counts, globalTokens.ctx, globalTokens.value, 0, globalTokens.length);
|
|
224
|
+
const count = Math.min(threads, numGroups, Math.max(2, Math.floor(n / PIXELS_PER_THREAD)));
|
|
225
|
+
let pool = first < ch.length && count > 1 && n >= MIN_THREADED_PIXELS ? startThreads(count) : null;
|
|
226
|
+
const parallel = async (task, local) => {
|
|
227
|
+
if (pool) {
|
|
228
|
+
try {
|
|
229
|
+
return await runChunks(pool, task, numGroups, local);
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
for (const t of pool)
|
|
233
|
+
t.terminate();
|
|
234
|
+
pool = null;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return runChunks(null, task, numGroups, local);
|
|
238
|
+
};
|
|
239
|
+
const offsets = new Int32Array(numGroups + 1);
|
|
240
|
+
for (let g = 0; g < numGroups; g++) {
|
|
241
|
+
const x0 = (g % groupsPerRow) * GROUP_DIM, y0 = Math.floor(g / groupsPerRow) * GROUP_DIM;
|
|
242
|
+
offsets[g + 1] = offsets[g] + (ch.length - first) * Math.min(GROUP_DIM, width - x0) * Math.min(GROUP_DIM, height - y0);
|
|
243
|
+
}
|
|
244
|
+
const tokenArray = () => new Uint32Array(pool ? new SharedArrayBuffer(4 * offsets[numGroups]) : new ArrayBuffer(4 * offsets[numGroups]));
|
|
245
|
+
const groups = { offsets, ctx: tokenArray(), value: tokenArray() };
|
|
246
|
+
let code, groupSections = [];
|
|
247
|
+
try {
|
|
248
|
+
if (first < ch.length) {
|
|
249
|
+
const channels = pool ? ch.map(c => ({ ...c, data: shared(c.data) })) : ch;
|
|
250
|
+
const task = { task: 'jxl-modular-groups', channels, first, width, numLfGroups, numPixels, ...groups };
|
|
251
|
+
for (const c of await parallel(task, modularGroupChunks(task)))
|
|
252
|
+
for (let i = 0; i < c.length; i++)
|
|
253
|
+
counts[i] += c[i];
|
|
254
|
+
}
|
|
255
|
+
code = new EntropyCode(counts, tree.numLeaves);
|
|
256
|
+
if (first < ch.length) {
|
|
257
|
+
const task = { task: 'jxl-modular-write', ...groups, tables: code.tables() };
|
|
258
|
+
groupSections = await parallel(task, modularWriteChunks(task));
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
finally {
|
|
262
|
+
if (pool)
|
|
263
|
+
for (const t of pool)
|
|
264
|
+
t.terminate();
|
|
265
|
+
}
|
|
266
|
+
const lfGlobal = new BitWriter();
|
|
267
|
+
lfGlobal.write(1, 1);
|
|
268
|
+
lfGlobal.write(1, 1);
|
|
269
|
+
tree.write(lfGlobal);
|
|
270
|
+
code.writeHeader(lfGlobal);
|
|
271
|
+
lfGlobal.write(1, 1);
|
|
272
|
+
lfGlobal.write(1, 1);
|
|
273
|
+
u32(lfGlobal, transforms.length, [[0, 0], [0, 1], [4, 2], [8, 18]]);
|
|
274
|
+
const begin = [[3, 0], [6, 8], [10, 72], [13, 1096]];
|
|
275
|
+
for (const t of transforms) {
|
|
276
|
+
lfGlobal.write(2, t.id);
|
|
277
|
+
u32(lfGlobal, t.beginC, begin);
|
|
278
|
+
if (t.id === TR_RCT)
|
|
279
|
+
lfGlobal.write(2, 0);
|
|
280
|
+
else {
|
|
281
|
+
u32(lfGlobal, t.numC, [[0, 1], [0, 3], [0, 4], [13, 1]]);
|
|
282
|
+
u32(lfGlobal, t.nbColors, [[8, 0], [10, 256], [12, 1280], [16, 5376]]);
|
|
283
|
+
lfGlobal.write(2, 0);
|
|
284
|
+
lfGlobal.write(4, 0);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (first > 0)
|
|
288
|
+
code.writeTokens(lfGlobal, globalTokens);
|
|
289
|
+
lfGlobal.zeroPadToByte();
|
|
290
|
+
const sections = [lfGlobal.bytes()];
|
|
291
|
+
if (numGroups > 1)
|
|
292
|
+
sections.push(...Array.from({ length: numLfGroups + 1 }, () => new Uint8Array(0)), ...groupSections);
|
|
293
|
+
const out = new BitWriter();
|
|
294
|
+
imageHeader(out, width, height, numChans);
|
|
295
|
+
frameHeader(out, !opaque);
|
|
296
|
+
for (const s of sections)
|
|
297
|
+
u32(out, s.length, [[10, 0], [14, 1024], [22, 17408], [30, 4211712]]);
|
|
298
|
+
out.zeroPadToByte();
|
|
299
|
+
const bytes = new Uint8Array(out.length + sections.reduce((sum, s) => sum + s.length, 0));
|
|
300
|
+
bytes.set(out.bytes());
|
|
301
|
+
let at = out.length;
|
|
302
|
+
for (const s of sections) {
|
|
303
|
+
bytes.set(s, at);
|
|
304
|
+
at += s.length;
|
|
305
|
+
}
|
|
306
|
+
return bytes;
|
|
307
|
+
}
|
|
@@ -490,7 +490,7 @@ const DELTA_PALETTE = [
|
|
|
490
490
|
0, 0, -96, 0, 128, 128, 0, 96, 0, 45, 24, -45, -128, 0, 0, 24, -45, 24, -45, 24, -45, 64, 0, -64, 64, -64, -64, 96, 0,
|
|
491
491
|
96, 45, -45, 24, 24, 45, -45, 64, 64, -64, 128, 128, 0, 0, 0, -128, -24, 45, -45
|
|
492
492
|
];
|
|
493
|
-
function paletteValue(palette, index, c, bitDepth) {
|
|
493
|
+
export function paletteValue(palette, index, c, bitDepth) {
|
|
494
494
|
const size = palette.w;
|
|
495
495
|
if (index < 0) {
|
|
496
496
|
if (c >= 3)
|
|
@@ -30,6 +30,79 @@ function fromF16(bits) {
|
|
|
30
30
|
const e = bits >> 10 & 31, m = bits & 1023, s = bits & 0x8000 ? -1 : 1;
|
|
31
31
|
return e === 0 ? s * m / 1024 / 16384 : s * (1 + m / 1024) * 2 ** (e - 15);
|
|
32
32
|
}
|
|
33
|
+
function largeComponents(isBackground, width, height) {
|
|
34
|
+
const labels = new Int32Array(width * height).fill(-1);
|
|
35
|
+
let parent = new Int32Array(1024), count = 0;
|
|
36
|
+
const find = (l) => {
|
|
37
|
+
while (parent[l] !== l)
|
|
38
|
+
l = parent[l] = parent[parent[l]];
|
|
39
|
+
return l;
|
|
40
|
+
};
|
|
41
|
+
const union = (a, b) => {
|
|
42
|
+
a = find(a);
|
|
43
|
+
b = find(b);
|
|
44
|
+
if (a === b)
|
|
45
|
+
return a;
|
|
46
|
+
if (a < b) {
|
|
47
|
+
parent[b] = a;
|
|
48
|
+
return a;
|
|
49
|
+
}
|
|
50
|
+
parent[a] = b;
|
|
51
|
+
return b;
|
|
52
|
+
};
|
|
53
|
+
for (let y = 0; y < height; y++) {
|
|
54
|
+
for (let x = 0; x < width; x++) {
|
|
55
|
+
const i = y * width + x;
|
|
56
|
+
if (isBackground[i])
|
|
57
|
+
continue;
|
|
58
|
+
let l = x > 0 ? labels[i - 1] : -1;
|
|
59
|
+
if (y > 0) {
|
|
60
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
61
|
+
if (x + dx < 0 || x + dx >= width)
|
|
62
|
+
continue;
|
|
63
|
+
const n = labels[i - width + dx];
|
|
64
|
+
if (n >= 0)
|
|
65
|
+
l = l < 0 ? n : union(l, n);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (l < 0) {
|
|
69
|
+
if (count === parent.length) {
|
|
70
|
+
const grown = new Int32Array(2 * count);
|
|
71
|
+
grown.set(parent);
|
|
72
|
+
parent = grown;
|
|
73
|
+
}
|
|
74
|
+
parent[count] = count;
|
|
75
|
+
l = count++;
|
|
76
|
+
}
|
|
77
|
+
labels[i] = l;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const box = new Int32Array(4 * count);
|
|
81
|
+
for (let l = 0; l < count; l++) {
|
|
82
|
+
box[4 * l] = box[4 * l + 2] = 0x7fffffff;
|
|
83
|
+
box[4 * l + 1] = box[4 * l + 3] = -1;
|
|
84
|
+
}
|
|
85
|
+
for (let y = 0; y < height; y++) {
|
|
86
|
+
for (let x = 0; x < width; x++) {
|
|
87
|
+
const l = labels[y * width + x];
|
|
88
|
+
if (l < 0)
|
|
89
|
+
continue;
|
|
90
|
+
const r = 4 * find(l);
|
|
91
|
+
if (x < box[r])
|
|
92
|
+
box[r] = x;
|
|
93
|
+
if (x > box[r + 1])
|
|
94
|
+
box[r + 1] = x;
|
|
95
|
+
if (y < box[r + 2])
|
|
96
|
+
box[r + 2] = y;
|
|
97
|
+
if (y > box[r + 3])
|
|
98
|
+
box[r + 3] = y;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return pixel => {
|
|
102
|
+
const r = 4 * find(labels[pixel]);
|
|
103
|
+
return box[r + 1] - box[r] >= MAX_PATCH_SIZE || box[r + 3] - box[r + 2] >= MAX_PATCH_SIZE;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
33
106
|
function candidates(planes, stride, width, height) {
|
|
34
107
|
const [p0, p1, p2] = planes;
|
|
35
108
|
const at = (c, x, y) => planes[c][y * stride + x];
|
|
@@ -109,11 +182,12 @@ function candidates(planes, stride, width, height) {
|
|
|
109
182
|
}
|
|
110
183
|
}
|
|
111
184
|
}
|
|
185
|
+
const large = largeComponents(isBackground, width, height);
|
|
112
186
|
const visited = new Uint8Array(width * height), found = [];
|
|
113
187
|
let stack = new Int32Array(1024);
|
|
114
188
|
for (let y = 0; y < height; y++) {
|
|
115
189
|
for (let x = 0; x < width; x++) {
|
|
116
|
-
if (isBackground[y * width + x] || visited[y * width + x])
|
|
190
|
+
if (isBackground[y * width + x] || visited[y * width + x] || large(y * width + x))
|
|
117
191
|
continue;
|
|
118
192
|
stack[0] = y * width + x;
|
|
119
193
|
let top = 1, minX = x, maxX = x, minY = y, maxY = y, reference = -1, allSimilar = true;
|
|
@@ -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];
|