imagescript 3.13.2 → 3.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/dist/animation-encoder.d.ts +1 -1
- package/dist/animation-encoder.js +6 -4
- package/dist/animation.d.ts +1 -1
- package/dist/animation.js +13 -8
- package/dist/codecs/jpeg.js +56 -5
- package/dist/codecs/jxl/acs-enc.js +32 -11
- package/dist/codecs/jxl/aq-enc.js +65 -44
- package/dist/codecs/jxl/dct.js +29 -9
- package/dist/codecs/jxl/entropy-enc.js +44 -35
- package/dist/codecs/jxl/fast-lossless.js +1 -1
- package/dist/codecs/jxl/modular-lossless.js +307 -0
- package/dist/codecs/jxl/modular.js +1 -1
- package/dist/codecs/jxl/patches-enc.js +75 -1
- package/dist/codecs/jxl/transforms-enc.js +99 -92
- package/dist/codecs/jxl/vardct-enc.js +4 -2
- package/dist/codecs/jxl.js +3 -1
- package/dist/codecs/vp8l.js +65 -27
- package/dist/codecs/webp.js +10 -15
- package/dist/frame.js +4 -0
- package/dist/image.d.ts +22 -14
- package/dist/image.js +27 -15
- package/dist/render/filter.js +6 -2
- package/dist/util/options.js +12 -0
- package/dist/util/resample.js +203 -61
- package/dist/util/worker.js +3 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -25,6 +25,11 @@ const token = (v) => {
|
|
|
25
25
|
const n = 31 - Math.clz32(v), m = v - (1 << n);
|
|
26
26
|
return SPLIT + ((n - SPLIT_EXPONENT) << (MSB + LSB)) + ((m >>> (n - MSB)) << LSB) + (m & ((1 << LSB) - 1));
|
|
27
27
|
};
|
|
28
|
+
export const MAX_TOKEN = token(0xffffffff) + 1;
|
|
29
|
+
export function countTokens(counts, ctx, value, from, to) {
|
|
30
|
+
for (let i = from; i < to; i++)
|
|
31
|
+
counts[ctx[i] * MAX_TOKEN + token(value[i])]++;
|
|
32
|
+
}
|
|
28
33
|
const ANS_LOG_TAB_SIZE = 12, ANS_TAB_SIZE = 1 << ANS_LOG_TAB_SIZE, LOG_ALPHABET_SIZE = 8;
|
|
29
34
|
const floorLog2 = (v) => 31 - Math.clz32(v);
|
|
30
35
|
const precision = (logcount, shift) => Math.max(0, Math.min(logcount, shift - ((ANS_LOG_TAB_SIZE - logcount) >> 1)));
|
|
@@ -224,21 +229,20 @@ export class EntropyCode {
|
|
|
224
229
|
ans;
|
|
225
230
|
constructor(streams, numContexts, { maxClusters = 64 } = {}) {
|
|
226
231
|
this.numContexts = numContexts;
|
|
227
|
-
let
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
}
|
|
235
|
-
const hist = Array.from({ length: numContexts }, () => new Float64Array(alphabet)), totals = new Float64Array(numContexts);
|
|
236
|
-
for (const t of streams) {
|
|
237
|
-
for (let i = 0; i < t.length; i++) {
|
|
238
|
-
hist[t.ctx[i]][token(t.value[i])]++;
|
|
239
|
-
totals[t.ctx[i]]++;
|
|
240
|
-
}
|
|
232
|
+
let counts;
|
|
233
|
+
if (streams instanceof Float64Array)
|
|
234
|
+
counts = streams;
|
|
235
|
+
else {
|
|
236
|
+
counts = new Float64Array(numContexts * MAX_TOKEN);
|
|
237
|
+
for (const t of streams)
|
|
238
|
+
countTokens(counts, t.ctx, t.value, 0, t.length);
|
|
241
239
|
}
|
|
240
|
+
let alphabet = 1;
|
|
241
|
+
for (let i = 0; i < counts.length; i++)
|
|
242
|
+
if (counts[i] && i % MAX_TOKEN + 1 > alphabet)
|
|
243
|
+
alphabet = i % MAX_TOKEN + 1;
|
|
244
|
+
const hist = Array.from({ length: numContexts }, (_, c) => counts.slice(c * MAX_TOKEN, c * MAX_TOKEN + alphabet));
|
|
245
|
+
const totals = Float64Array.from(hist, h => h.reduce((sum, v) => sum + v, 0));
|
|
242
246
|
this.ctxMap = new Uint8Array(numContexts);
|
|
243
247
|
const clusters = cluster(hist, totals, Math.min(maxClusters, 255, numContexts), this.ctxMap).map(h => {
|
|
244
248
|
let size = h.length;
|
|
@@ -263,28 +267,33 @@ export class EntropyCode {
|
|
|
263
267
|
c.write(w);
|
|
264
268
|
}
|
|
265
269
|
writeTokens(w, t) {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
270
|
+
writeTokens(w, this.tables(), t.ctx, t.value, 0, t.length);
|
|
271
|
+
}
|
|
272
|
+
tables() {
|
|
273
|
+
return { ctxMap: this.ctxMap, freq: this.ans.map(c => c.freq), reverse: this.ans.map(c => c.reverse) };
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
export function writeTokens(w, { ctxMap: map, freq: freqs, reverse }, ctx, value, from, to) {
|
|
277
|
+
const chunks = new Int32Array(to - from);
|
|
278
|
+
let state = 0x130000;
|
|
279
|
+
for (let i = to - 1; i >= from; i--) {
|
|
280
|
+
const c = map[ctx[i]], tk = token(value[i]), freq = freqs[c][tk];
|
|
281
|
+
chunks[i - from] = -1;
|
|
282
|
+
if (state >>> 20 >= freq) {
|
|
283
|
+
chunks[i - from] = state & 0xffff;
|
|
284
|
+
state >>>= 16;
|
|
278
285
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
286
|
+
const v = Math.floor(state / freq);
|
|
287
|
+
state = v * ANS_TAB_SIZE + reverse[c][tk][state - v * freq];
|
|
288
|
+
}
|
|
289
|
+
w.write(32, state);
|
|
290
|
+
for (let i = from; i < to; i++) {
|
|
291
|
+
if (chunks[i - from] >= 0)
|
|
292
|
+
w.write(16, chunks[i - from]);
|
|
293
|
+
const v = value[i];
|
|
294
|
+
if (v >= SPLIT) {
|
|
295
|
+
const nbits = 31 - Math.clz32(v) - MSB - LSB;
|
|
296
|
+
w.write(nbits, (v >>> LSB) & ((1 << nbits) - 1));
|
|
288
297
|
}
|
|
289
298
|
}
|
|
290
299
|
}
|
|
@@ -673,7 +673,7 @@ export function encode(px, width, height, numChans, chunkSize = 16) {
|
|
|
673
673
|
}
|
|
674
674
|
return w.bytes().slice();
|
|
675
675
|
}
|
|
676
|
-
function frameHeader(w, alpha) {
|
|
676
|
+
export function frameHeader(w, alpha) {
|
|
677
677
|
w.write(1, 0);
|
|
678
678
|
w.write(2, 0b00);
|
|
679
679
|
w.write(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;
|