imagescript 3.8.1 → 3.10.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 +16 -12
- package/THIRD_PARTY_NOTICES.md +62 -3
- package/dist/animation-encoder.d.ts +2 -0
- package/dist/animation-encoder.js +6 -12
- package/dist/animation.d.ts +7 -1
- package/dist/animation.js +21 -16
- package/dist/codecs/av1/encode.js +149 -44
- package/dist/codecs/avif-enc.js +35 -9
- package/dist/codecs/avif.js +161 -76
- package/dist/codecs/jpeg.js +7 -1
- package/dist/codecs/jxl/acs-enc.js +18 -6
- package/dist/codecs/jxl/aq-enc.js +38 -37
- package/dist/codecs/jxl/color.js +14 -3
- package/dist/codecs/jxl/filters.js +44 -34
- package/dist/codecs/jxl/frame.js +218 -37
- package/dist/codecs/jxl/modular.js +40 -29
- package/dist/codecs/jxl/patches-enc.js +349 -0
- package/dist/codecs/jxl/vardct-enc.js +197 -137
- package/dist/codecs/jxl/vardct.js +53 -19
- package/dist/codecs/jxl.js +32 -21
- package/dist/codecs/png.js +48 -12
- package/dist/codecs/tiff.js +5 -3
- package/dist/codecs/vp8-enc.js +60 -4
- package/dist/codecs/vp8l.js +94 -62
- package/dist/codecs/webp.js +51 -16
- package/dist/image.d.ts +59 -6
- package/dist/image.js +27 -18
- 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 +866 -102
- package/dist/render/text.js +1 -1
- package/dist/util/icc.js +143 -0
- package/dist/util/threads.js +101 -0
- package/dist/util/worker.js +52 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/codecs/jxl/color.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { TF_PQ, TF_HLG, PRI_BT2100, PRI_P3, EXTRA_ALPHA, EXTRA_SPOT, EXTRA_BLACK } from "./headers.js";
|
|
2
2
|
import { toSRGB, hdrToLinear, REFERENCE_WHITE } from "../hdr.js";
|
|
3
|
-
const byte = (v) => v <= 0 ? 0 : v >= 1 ? 255 : Math.round(v * 255);
|
|
3
|
+
export const byte = (v) => v <= 0 ? 0 : v >= 1 ? 255 : Math.round(v * 255);
|
|
4
4
|
const [P0, P1, P2, P3, P4] = [-5.135152395e-04, 5.287254571e-03, 3.903842876e-01, 1.474205315e+00, 7.352629620e-01].map(Math.fround);
|
|
5
5
|
const [Q0, Q1, Q2, Q3, Q4] = [1.004519624e-02, 3.036675394e-01, 1.340816930e+00, 9.258482155e-01, 2.424867759e-02].map(Math.fround);
|
|
6
6
|
const srgbEncode = (v) => {
|
|
@@ -43,10 +43,10 @@ const linearByte = (v) => {
|
|
|
43
43
|
k++;
|
|
44
44
|
return k;
|
|
45
45
|
};
|
|
46
|
-
function xybToBytes(x, y, b, m, bias, k, out) {
|
|
46
|
+
export function xybToBytes(x, y, b, m, bias, k, out, from = 0, to = x.length) {
|
|
47
47
|
const m0 = m[0], m1 = m[1], m2 = m[2], m3 = m[3], m4 = m[4], m5 = m[5], m6 = m[6], m7 = m[7], m8 = m[8];
|
|
48
48
|
const b0 = bias[0], b1 = bias[1], b2 = bias[2], c0 = Math.cbrt(b0), c1 = Math.cbrt(b1), c2 = Math.cbrt(b2);
|
|
49
|
-
for (let i =
|
|
49
|
+
for (let i = from, o = 4 * from; i < to; i++, o += 4) {
|
|
50
50
|
const gr = y[i] + x[i] - c0, gg = y[i] - x[i] - c1, gb = b[i] - c2;
|
|
51
51
|
const mr = gr * gr * gr + b0, mg = gg * gg * gg + b1, mb = gb * gb * gb + b2;
|
|
52
52
|
out[o] = linearByte((m0 * mr + m1 * mg + m2 * mb) * k);
|
|
@@ -69,6 +69,8 @@ export class ColorOutput {
|
|
|
69
69
|
this.scale = this.linear ? image.toneMapping.intensityTarget / REFERENCE_WHITE : 1;
|
|
70
70
|
}
|
|
71
71
|
transform(p, ycbcr, final = false) {
|
|
72
|
+
if (p.rgba)
|
|
73
|
+
return p;
|
|
72
74
|
const image = this.image, n = p.width * p.height, ch = p.channels.slice();
|
|
73
75
|
if (ycbcr) {
|
|
74
76
|
const [cb, y, cr] = ch, r = new Float32Array(n), g = new Float32Array(n), b = new Float32Array(n);
|
|
@@ -106,6 +108,15 @@ export class ColorOutput {
|
|
|
106
108
|
}
|
|
107
109
|
return { width: p.width, height: p.height, channels: ch };
|
|
108
110
|
}
|
|
111
|
+
bytesOfSamples() {
|
|
112
|
+
return !this.image.xybEncoded && !this.hdrSignal;
|
|
113
|
+
}
|
|
114
|
+
bytesParams(ycbcr, final) {
|
|
115
|
+
const image = this.image;
|
|
116
|
+
if (ycbcr || !image.xybEncoded || !final || this.linear)
|
|
117
|
+
return null;
|
|
118
|
+
return { m: image.opsinInverse, bias: image.opsinBias, k: 255 / image.toneMapping.intensityTarget * this.scale };
|
|
119
|
+
}
|
|
109
120
|
toRGBA(p) {
|
|
110
121
|
const n = p.width * p.height, ch = p.channels, image = this.image;
|
|
111
122
|
if (p.rgba) {
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { mirror } from "./upsample.js";
|
|
2
2
|
export function gaborish(channels, w, h, weights) {
|
|
3
|
-
|
|
3
|
+
const out = channels.map(() => new Float32Array(w * h));
|
|
4
|
+
gaborishRows(channels, out, w, h, weights, 0, h);
|
|
5
|
+
return out;
|
|
6
|
+
}
|
|
7
|
+
export function gaborishRows(channels, outs, w, h, weights, y0, y1) {
|
|
8
|
+
channels.forEach((src, c) => {
|
|
4
9
|
const w0 = weights[2 * c], w1 = weights[2 * c + 1], norm = 1 / (1 + 4 * w0 + 4 * w1);
|
|
5
|
-
const out =
|
|
6
|
-
for (let y =
|
|
10
|
+
const out = outs[c];
|
|
11
|
+
for (let y = y0; y < y1; y++) {
|
|
7
12
|
const t = mirror(y - 1, h) * w, m = y * w, b = mirror(y + 1, h) * w;
|
|
8
13
|
const at = (x, l, r) => (src[m + x] + (src[t + x] + src[b + x] + src[m + l] + src[m + r]) * w0 + (src[t + l] + src[t + r] + src[b + l] + src[b + r]) * w1) * norm;
|
|
9
14
|
out[m] = at(0, mirror(-1, w), mirror(1, w));
|
|
@@ -21,7 +26,6 @@ export function gaborish(channels, w, h, weights) {
|
|
|
21
26
|
if (w > 1)
|
|
22
27
|
out[m + w - 1] = at(w - 1, w - 2, mirror(w, w));
|
|
23
28
|
}
|
|
24
|
-
return out;
|
|
25
29
|
});
|
|
26
30
|
}
|
|
27
31
|
const STEPS = [
|
|
@@ -159,42 +163,48 @@ function outputRow(cur, out, y, w, sums, p0, pw, u, rows, xs, sigma, sigmaRow, s
|
|
|
159
163
|
}
|
|
160
164
|
}
|
|
161
165
|
const TAPS = new Int32Array(16);
|
|
162
|
-
export function
|
|
163
|
-
let cur = channels;
|
|
164
|
-
const steps = e.iters === 3 ? [0, 1, 2] : e.iters === 2 ? [1, 2] : [1];
|
|
166
|
+
export function epfScratch(w) {
|
|
165
167
|
const pw = w + 6, ph = STRIP + 6;
|
|
166
|
-
const diffs = Array.from({ length: 6 }, () => new Float64Array(pw * ph)), sums = Array.from({ length: 6 }, () => new Float64Array(pw * ph));
|
|
167
168
|
const xs = new Int32Array(w + 10);
|
|
168
169
|
for (let x = 0; x < xs.length; x++)
|
|
169
170
|
xs[x] = mirror(x - 5, w);
|
|
170
|
-
|
|
171
|
-
|
|
171
|
+
return { diffs: Array.from({ length: 6 }, () => new Float64Array(pw * ph)), sums: Array.from({ length: 6 }, () => new Float64Array(pw * ph)), xs, rows: new Int32Array(5) };
|
|
172
|
+
}
|
|
173
|
+
export const epfSteps = (e) => e.iters === 3 ? [0, 1, 2] : e.iters === 2 ? [1, 2] : [1];
|
|
174
|
+
export const epfStrips = (h) => Math.ceil(h / STRIP);
|
|
175
|
+
export function epf(channels, w, h, e, sigma, bw) {
|
|
176
|
+
let cur = channels;
|
|
177
|
+
const scratch = epfScratch(w);
|
|
178
|
+
for (const step of epfSteps(e)) {
|
|
172
179
|
const out = [new Float32Array(w * h), new Float32Array(w * h), new Float32Array(w * h)];
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
const [s0, s1, s2] = e.channelScale;
|
|
176
|
-
const [i0, i1, i2] = cur;
|
|
177
|
-
for (let y0 = 0; y0 < h; y0 += STRIP) {
|
|
178
|
-
const strip = Math.min(STRIP, h - y0);
|
|
179
|
-
for (let k = 0; k < n; k++) {
|
|
180
|
-
const ux = u[2 * k], uy = u[2 * k + 1], d = diffs[k], sum = sums[k];
|
|
181
|
-
for (let py = 0; py < strip + 6; py++) {
|
|
182
|
-
diffRow(i0, i1, i2, mirror(y0 + py - 3, h) * w, mirror(y0 + py - 3 + uy, h) * w, xs, ux, d, py * pw, pw, s0, s1, s2);
|
|
183
|
-
}
|
|
184
|
-
if (!plus)
|
|
185
|
-
sum.set(d);
|
|
186
|
-
else
|
|
187
|
-
for (let py = 1; py < strip + 5; py++)
|
|
188
|
-
plusRow(d, sum, py * pw, pw);
|
|
189
|
-
}
|
|
190
|
-
for (let ry = 0; ry < strip; ry++) {
|
|
191
|
-
const y = y0 + ry;
|
|
192
|
-
for (let k = 0; k < 5; k++)
|
|
193
|
-
rows[k] = mirror(y + k - 2, h) * w;
|
|
194
|
-
outputRow(cur, out, y, w, sums, (ry + 3) * pw + 3, pw, u, rows, xs, sigma, (y >> 3) * bw, e.sigmaForModular, stepMul, e.borderSadMul);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
180
|
+
for (let strip = 0; strip < epfStrips(h); strip++)
|
|
181
|
+
epfStrip(cur, out, w, h, e, sigma, bw, step, strip, scratch);
|
|
197
182
|
cur = out;
|
|
198
183
|
}
|
|
199
184
|
return cur;
|
|
200
185
|
}
|
|
186
|
+
export function epfStrip(cur, out, w, h, e, sigma, bw, step, index, scratch) {
|
|
187
|
+
const pw = w + 6, { diffs, sums, xs, rows } = scratch;
|
|
188
|
+
const { u, plus } = STEPS[step], n = u.length / 2;
|
|
189
|
+
const stepMul = step === 0 ? e.pass0SigmaScale : step === 2 ? e.pass2SigmaScale : 1;
|
|
190
|
+
const [s0, s1, s2] = e.channelScale;
|
|
191
|
+
const [i0, i1, i2] = cur;
|
|
192
|
+
const y0 = index * STRIP, strip = Math.min(STRIP, h - y0);
|
|
193
|
+
for (let k = 0; k < n; k++) {
|
|
194
|
+
const ux = u[2 * k], uy = u[2 * k + 1], d = diffs[k], sum = sums[k];
|
|
195
|
+
for (let py = 0; py < strip + 6; py++) {
|
|
196
|
+
diffRow(i0, i1, i2, mirror(y0 + py - 3, h) * w, mirror(y0 + py - 3 + uy, h) * w, xs, ux, d, py * pw, pw, s0, s1, s2);
|
|
197
|
+
}
|
|
198
|
+
if (!plus)
|
|
199
|
+
sum.set(d);
|
|
200
|
+
else
|
|
201
|
+
for (let py = 1; py < strip + 5; py++)
|
|
202
|
+
plusRow(d, sum, py * pw, pw);
|
|
203
|
+
}
|
|
204
|
+
for (let ry = 0; ry < strip; ry++) {
|
|
205
|
+
const y = y0 + ry;
|
|
206
|
+
for (let k = 0; k < 5; k++)
|
|
207
|
+
rows[k] = mirror(y + k - 2, h) * w;
|
|
208
|
+
outputRow(cur, out, y, w, sums, (ry + 3) * pw + 3, pw, u, rows, xs, sigma, (y >> 3) * bw, e.sigmaForModular, stepMul, e.borderSadMul);
|
|
209
|
+
}
|
|
210
|
+
}
|
package/dist/codecs/jxl/frame.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { BitReader } from "./bits.js";
|
|
2
2
|
import { EXTRA_ALPHA } from "./headers.js";
|
|
3
3
|
import { frameHeader, toc, passShifts, FRAME_LF, FRAME_REGULAR, FRAME_SKIP_PROGRESSIVE, BLEND_REPLACE, BLEND_ADD, BLEND_BLEND, BLEND_MULADD, BLEND_MUL, FLAG_PATCHES, FLAG_SPLINES, FLAG_NOISE, FLAG_USE_LF_FRAME } from "./frame-header.js";
|
|
4
|
-
import { ModularImage, channel, readTree, decodeModular } from "./modular.js";
|
|
4
|
+
import { ModularImage, TR_RCT, channel, readTree, decodeModular, rctOrder, rctSamples } from "./modular.js";
|
|
5
5
|
import { upsample } from "./upsample.js";
|
|
6
|
-
import { gaborish, epf } from "./filters.js";
|
|
6
|
+
import { gaborish, gaborishRows, epf, epfStrip, epfScratch, epfSteps, epfStrips } from "./filters.js";
|
|
7
7
|
import { VarDCT, readVarDCTGlobal } from "./vardct.js";
|
|
8
|
+
import { runChunks, shared, sharedOf } from "../../util/threads.js";
|
|
8
9
|
import { readPatches, applyPatches, readSplines, renderSplines, readNoise, renderNoise } from "./features.js";
|
|
10
|
+
import { byte, xybToBytes } from "./color.js";
|
|
11
|
+
import { littleEndian } from "../../util/alpha.js";
|
|
9
12
|
import { performBlending, PB_REPLACE, PB_ADD, PB_BLEND_ABOVE, PB_ADD_ABOVE, PB_MUL } from "./blend.js";
|
|
10
13
|
const NUM_QUANT_TABLES = 17;
|
|
11
14
|
class Sections {
|
|
@@ -27,7 +30,8 @@ class Sections {
|
|
|
27
30
|
return new BitReader(this.d, this.t.offsets[i], end);
|
|
28
31
|
}
|
|
29
32
|
}
|
|
30
|
-
|
|
33
|
+
const MIN_THREADED_PIXELS = 1024 * 1024;
|
|
34
|
+
export async function decodeFrame(d, r, state, at, pool) {
|
|
31
35
|
const image = state.image;
|
|
32
36
|
const f = frameHeader(r, image);
|
|
33
37
|
const t = toc(r, f);
|
|
@@ -50,25 +54,56 @@ export function decodeFrame(d, r, state) {
|
|
|
50
54
|
decodeModularGroup(gr, lf, f, x, y, lfGroupDim, 3, 1000, 1 + f.numLfGroups + g);
|
|
51
55
|
vardct?.readHfMetadata(gr, g, lf.tree);
|
|
52
56
|
}
|
|
53
|
-
if (vardct)
|
|
57
|
+
if (vardct) {
|
|
54
58
|
vardct.readHfGlobal(sections.get(1 + f.numLfGroups), lf.tree);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
const lfFrame = useLfFrame ? state.lfFrames[f.lfLevel] : null;
|
|
60
|
+
if (useLfFrame && !lfFrame)
|
|
61
|
+
throw new Error('JPEG XL: no LF frame');
|
|
62
|
+
const many = pool.threads && f.numGroups > 1 && f.width * f.height >= MIN_THREADED_PIXELS;
|
|
63
|
+
vardct.prepare(image.quantBias, image.quantBiasNumerator, lfFrame ? lfFrame.channels.slice(0, 3) : null, many ? [0, 1, 2].map(c => new Float32Array(new SharedArrayBuffer(4 * vardct.stride(c) * (vardct.bh >> vardct.vs[c]) * 8))) : undefined);
|
|
64
|
+
}
|
|
65
|
+
const coeffs = vardct ? [0, 1, 2].map(() => new Int32Array(f.groupDim * f.groupDim)) : [];
|
|
66
|
+
const local = (g) => decodeGroup(g, f, sections, lf, vardct, coeffs);
|
|
67
|
+
let groupsDone = false;
|
|
68
|
+
if (pool.threads && f.numGroups > 1 && f.width * f.height >= MIN_THREADED_PIXELS) {
|
|
69
|
+
if (!pool.data)
|
|
70
|
+
pool.data = shared(d);
|
|
71
|
+
const planes = vardct ? vardct.rendered : null;
|
|
72
|
+
for (const c of lf.modular.channels)
|
|
73
|
+
c.data = shared(c.data);
|
|
74
|
+
const task = {
|
|
75
|
+
task: 'jxl-groups', data: pool.data, at, image, channels: lf.modular.channels, nbMeta: lf.modular.nbMeta,
|
|
76
|
+
vardct: vardct ? vardct.share() : null, planes,
|
|
77
|
+
};
|
|
78
|
+
try {
|
|
79
|
+
await runChunks(pool.threads, task, f.numGroups, local);
|
|
80
|
+
if (vardct && planes)
|
|
81
|
+
vardct.adopt(vardct.share(), planes);
|
|
82
|
+
groupsDone = true;
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
for (const t of pool.threads)
|
|
86
|
+
t.terminate();
|
|
87
|
+
pool.threads = null;
|
|
88
|
+
if (vardct)
|
|
89
|
+
vardct.rendered.forEach(p => p.fill(0));
|
|
62
90
|
}
|
|
63
91
|
}
|
|
64
|
-
|
|
65
|
-
|
|
92
|
+
if (!groupsDone)
|
|
93
|
+
for (let g = 0; g < f.numGroups; g++)
|
|
94
|
+
local(g);
|
|
95
|
+
const bytes = !vardct && f.modular && !f.doYCbCr && !f.gab && !f.epf && !lf.patches && !lf.splines && !lf.noise && f.upsampling === 1
|
|
96
|
+
&& f.ecUpsampling.every(u => u === 1) && shownAsIs(f, image) && state.output.bytesOfSamples() && !image.bitDepth.float;
|
|
97
|
+
let planes;
|
|
98
|
+
if (bytes)
|
|
99
|
+
planes = await modularToBytes(lf, f, image, pool);
|
|
100
|
+
else {
|
|
101
|
+
lf.modular.undo();
|
|
102
|
+
planes = modularToPlanes(lf, f, image);
|
|
103
|
+
}
|
|
66
104
|
let sigma = null;
|
|
67
105
|
if (vardct) {
|
|
68
|
-
const
|
|
69
|
-
if (useLfFrame && !lfFrame)
|
|
70
|
-
throw new Error('JPEG XL: no LF frame');
|
|
71
|
-
const xyb = vardct.render(image.quantBias, image.quantBiasNumerator, lfFrame ? lfFrame.channels.slice(0, 3) : null);
|
|
106
|
+
const xyb = vardct.rendered;
|
|
72
107
|
const color = xyb.map((plane, c) => {
|
|
73
108
|
const hs = vardct.hs[c], vs = vardct.vs[c], w = Math.ceil(f.sampleWidth / (1 << hs)), h = Math.ceil(f.sampleHeight / (1 << vs));
|
|
74
109
|
const out = crop(plane, vardct.stride(c), w, h);
|
|
@@ -77,7 +112,7 @@ export function decodeFrame(d, r, state) {
|
|
|
77
112
|
planes = { ...planes, channels: [...color, ...planes.channels] };
|
|
78
113
|
sigma = vardct.sigma;
|
|
79
114
|
}
|
|
80
|
-
planes = filter(planes, f, image, sigma, vardct ? vardct.bw : 0);
|
|
115
|
+
planes = await threaded(pool, planes.width * planes.height, threads => filterThreaded(planes, f, image, sigma, vardct ? vardct.bw : 0, threads), () => filter(planes, f, image, sigma, vardct ? vardct.bw : 0));
|
|
81
116
|
const numColor = planes.channels.length - image.extraChannels.length;
|
|
82
117
|
if (lf.patches)
|
|
83
118
|
applyPatches(planes, numColor, lf.patches, state.references, image.extraChannels);
|
|
@@ -87,9 +122,32 @@ export function decodeFrame(d, r, state) {
|
|
|
87
122
|
planes = upsamplePlanes(planes, f, image);
|
|
88
123
|
if (lf.noise && numColor === 3)
|
|
89
124
|
renderNoise(planes, lf.noise, f.groupDim, state.visible, state.invisible, baseX, baseB);
|
|
90
|
-
const frame = render(planes, f, state);
|
|
125
|
+
const frame = await render(planes, f, state, pool);
|
|
91
126
|
return { ...frame, end: t.end };
|
|
92
127
|
}
|
|
128
|
+
function decodeGroup(g, f, sections, lf, vardct, coeffs) {
|
|
129
|
+
for (const c of coeffs)
|
|
130
|
+
c.fill(0);
|
|
131
|
+
for (let p = 0; p < f.numPasses; p++) {
|
|
132
|
+
const [min, max] = passShifts(f, p);
|
|
133
|
+
const gr = sections.get(2 + f.numLfGroups + p * f.numGroups + g);
|
|
134
|
+
vardct?.readPassGroup(gr, p, g, coeffs);
|
|
135
|
+
const x = (g % f.groupsPerRow) * f.groupDim, y = Math.floor(g / f.groupsPerRow) * f.groupDim;
|
|
136
|
+
decodeModularGroup(gr, lf, f, x, y, f.groupDim, min, max, 1 + 3 * f.numLfGroups + NUM_QUANT_TABLES + f.numGroups * p + g);
|
|
137
|
+
}
|
|
138
|
+
vardct?.renderGroup(g, coeffs);
|
|
139
|
+
}
|
|
140
|
+
export function groupChunks(task) {
|
|
141
|
+
const r = new BitReader(task.data, task.at), f = frameHeader(r, task.image), sections = new Sections(task.data, toc(r, f));
|
|
142
|
+
const lf = lfGlobal(sections.get(0), f, task.image, task);
|
|
143
|
+
const vardct = lf.vardct ? new VarDCT(f, lf.vardct, lf.lfDequant, task.image.bitDepth.bits) : null;
|
|
144
|
+
if (vardct) {
|
|
145
|
+
vardct.adopt(task.vardct, task.planes);
|
|
146
|
+
vardct.readHfGlobal(sections.get(1 + f.numLfGroups), lf.tree);
|
|
147
|
+
}
|
|
148
|
+
const coeffs = vardct ? [0, 1, 2].map(() => new Int32Array(f.groupDim * f.groupDim)) : [];
|
|
149
|
+
return g => decodeGroup(g, f, sections, lf, vardct, coeffs);
|
|
150
|
+
}
|
|
93
151
|
function crop(src, stride, w, h) {
|
|
94
152
|
if (stride === w && src.length === w * h)
|
|
95
153
|
return src;
|
|
@@ -122,7 +180,7 @@ function chromaUpsample(src, sw, sh, w, h) {
|
|
|
122
180
|
}
|
|
123
181
|
return cur;
|
|
124
182
|
}
|
|
125
|
-
function lfGlobal(r, f, image) {
|
|
183
|
+
function lfGlobal(r, f, image, given) {
|
|
126
184
|
const patches = f.flags & FLAG_PATCHES ? readPatches(r, image.extraChannels, f.width, f.height) : null;
|
|
127
185
|
const splines = f.flags & FLAG_SPLINES ? readSplines(r, f.width, f.height) : null;
|
|
128
186
|
const noise = f.flags & FLAG_NOISE ? readNoise(r) : null;
|
|
@@ -134,6 +192,11 @@ function lfGlobal(r, f, image) {
|
|
|
134
192
|
let tree = null;
|
|
135
193
|
if (r.bool())
|
|
136
194
|
tree = readTree(r, Math.min(1 << 22, 1024 + Math.floor(f.width * f.height * (f.encodedColorChannels + numExtra) / 16)));
|
|
195
|
+
if (given) {
|
|
196
|
+
const modular = new ModularImage(given.channels, image.bitDepth.bits);
|
|
197
|
+
modular.nbMeta = given.nbMeta;
|
|
198
|
+
return { patches, splines, noise, lfDequant, vardct, tree, modular };
|
|
199
|
+
}
|
|
137
200
|
const w = f.sampleWidth, h = f.sampleHeight, list = [];
|
|
138
201
|
if (f.modular) {
|
|
139
202
|
if (f.doYCbCr) {
|
|
@@ -185,21 +248,22 @@ function decodeModularGroup(r, lf, f, x0, y0, dim, minShift, maxShift, stream) {
|
|
|
185
248
|
fc.data.set(g.data.subarray(row * g.w, (row + 1) * g.w), (y + row) * fc.w + x);
|
|
186
249
|
});
|
|
187
250
|
}
|
|
251
|
+
function samplesToFloat(c, depth) {
|
|
252
|
+
const o = new Float32Array(c.w * c.h);
|
|
253
|
+
if (depth.float)
|
|
254
|
+
for (let i = 0; i < o.length; i++)
|
|
255
|
+
o[i] = floatSample(c.data[i], depth.bits, depth.expBits);
|
|
256
|
+
else {
|
|
257
|
+
const scale = 1 / (2 ** depth.bits - 1);
|
|
258
|
+
for (let i = 0; i < o.length; i++)
|
|
259
|
+
o[i] = c.data[i] * scale;
|
|
260
|
+
}
|
|
261
|
+
return o;
|
|
262
|
+
}
|
|
188
263
|
function modularToPlanes(lf, f, image) {
|
|
189
264
|
const ch = lf.modular.channels.slice(lf.modular.nbMeta);
|
|
190
265
|
const out = [];
|
|
191
|
-
const toFloat = (c, depth = image.bitDepth) =>
|
|
192
|
-
const o = new Float32Array(c.w * c.h);
|
|
193
|
-
if (depth.float)
|
|
194
|
-
for (let i = 0; i < o.length; i++)
|
|
195
|
-
o[i] = floatSample(c.data[i], depth.bits, depth.expBits);
|
|
196
|
-
else {
|
|
197
|
-
const scale = 1 / (2 ** depth.bits - 1);
|
|
198
|
-
for (let i = 0; i < o.length; i++)
|
|
199
|
-
o[i] = c.data[i] * scale;
|
|
200
|
-
}
|
|
201
|
-
return o;
|
|
202
|
-
};
|
|
266
|
+
const toFloat = (c, depth = image.bitDepth) => samplesToFloat(c, depth);
|
|
203
267
|
if (!f.modular) {
|
|
204
268
|
image.extraChannels.forEach((ec, i) => out.push(toFloat(ch[i], ec.bitDepth)));
|
|
205
269
|
return { width: f.sampleWidth, height: f.sampleHeight, channels: out };
|
|
@@ -222,6 +286,64 @@ function modularToPlanes(lf, f, image) {
|
|
|
222
286
|
image.extraChannels.forEach((ec, i) => out.push(toFloat(ch[numColor + i], ec.bitDepth)));
|
|
223
287
|
return { width: f.sampleWidth, height: f.sampleHeight, channels: out };
|
|
224
288
|
}
|
|
289
|
+
async function modularToBytes(lf, f, image, pool) {
|
|
290
|
+
const m = lf.modular, numColor = f.encodedColorChannels, n = f.width * f.height, first = m.transforms[0];
|
|
291
|
+
const rct = numColor === 3 && first?.id === TR_RCT && first.beginC === m.nbMeta ? first : null;
|
|
292
|
+
m.undo(rct ? 1 : 0);
|
|
293
|
+
const color = rct ? m.rctChannels(rct) : [0, 1, 2].map(c => m.channels[m.nbMeta + (numColor === 1 ? 0 : c)].data);
|
|
294
|
+
const task = (threaded) => ({
|
|
295
|
+
task: 'jxl-post', kind: 'samples', w: f.width, h: f.height, inputs: [], outputs: [], samples: threaded ? color.map(sharedOf) : color,
|
|
296
|
+
rctType: rct ? rct.rctType : -1, bits: image.bitDepth.bits, rgba: new Uint8Array(threaded ? new SharedArrayBuffer(4 * n) : new ArrayBuffer(4 * n)),
|
|
297
|
+
});
|
|
298
|
+
const rgba = await threaded(pool, n, async (threads) => {
|
|
299
|
+
const t = task(true);
|
|
300
|
+
await runChunks(threads, t, Math.ceil(f.height / BAND), postChunks(t));
|
|
301
|
+
if (rct)
|
|
302
|
+
for (let c = 0; c < 3; c++)
|
|
303
|
+
m.channels[m.nbMeta + c].data = t.samples[c];
|
|
304
|
+
return new Uint8Array(t.rgba);
|
|
305
|
+
}, () => {
|
|
306
|
+
const t = task(false);
|
|
307
|
+
samplesToBytes(t.samples, t.rctType, t.bits, t.rgba, 0, n);
|
|
308
|
+
return t.rgba;
|
|
309
|
+
});
|
|
310
|
+
if (rct) {
|
|
311
|
+
m.permuteRCT(rct);
|
|
312
|
+
m.transforms.length = 0;
|
|
313
|
+
}
|
|
314
|
+
const ch = m.channels.slice(m.nbMeta);
|
|
315
|
+
return { width: f.width, height: f.height, channels: image.extraChannels.map((ec, i) => samplesToFloat(ch[numColor + i], ec.bitDepth)), rgba };
|
|
316
|
+
}
|
|
317
|
+
function samplesToBytes(samples, rctType, bits, rgba, i0, i1) {
|
|
318
|
+
if (rctType >= 0)
|
|
319
|
+
rctSamples(samples, rctType, i0, i1);
|
|
320
|
+
const order = rctType >= 0 ? rctOrder(rctType) : [0, 1, 2], [r, g, b] = [0, 1, 2].map(c => samples[order.indexOf(c)]);
|
|
321
|
+
const max = 2 ** bits - 1, scale = 1 / max;
|
|
322
|
+
const table = bits <= 16 ? byteTable(bits) : null;
|
|
323
|
+
if (table && littleEndian) {
|
|
324
|
+
const words = new Uint32Array(rgba.buffer, rgba.byteOffset, rgba.length >> 2);
|
|
325
|
+
const at = (v) => v >>> 0 <= max ? table[v] : v < 0 ? 0 : 255;
|
|
326
|
+
for (let i = i0; i < i1; i++)
|
|
327
|
+
words[i] = at(r[i]) | at(g[i]) << 8 | at(b[i]) << 16 | 0xff000000;
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
[r, g, b].forEach((data, c) => {
|
|
331
|
+
for (let i = i0, o = 4 * i0 + c; i < i1; i++, o += 4)
|
|
332
|
+
rgba[o] = table ? (data[i] >>> 0 <= max ? table[data[i]] : data[i] < 0 ? 0 : 255) : byte(Math.fround(data[i] * scale));
|
|
333
|
+
});
|
|
334
|
+
for (let o = 4 * i0 + 3; o < 4 * i1; o += 4)
|
|
335
|
+
rgba[o] = 255;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const byteTables = new Map();
|
|
339
|
+
function byteTable(bits) {
|
|
340
|
+
let table = byteTables.get(bits);
|
|
341
|
+
if (!table) {
|
|
342
|
+
const scale = 1 / (2 ** bits - 1);
|
|
343
|
+
byteTables.set(bits, table = Uint8Array.from({ length: 2 ** bits }, (_, v) => byte(Math.fround(v * scale))));
|
|
344
|
+
}
|
|
345
|
+
return table;
|
|
346
|
+
}
|
|
225
347
|
function filter(p, f, image, sigma, sigmaStride = 0) {
|
|
226
348
|
if (!f.gab && !f.epf)
|
|
227
349
|
return p;
|
|
@@ -233,7 +355,57 @@ function filter(p, f, image, sigma, sigmaStride = 0) {
|
|
|
233
355
|
color = epf(color, p.width, p.height, f.epf, sigma, sigmaStride);
|
|
234
356
|
return { ...p, channels: [...color.slice(0, numColor), ...p.channels.slice(numColor)] };
|
|
235
357
|
}
|
|
358
|
+
async function threaded(pool, pixels, inThreads, here) {
|
|
359
|
+
if (pool.threads && pixels >= MIN_THREADED_PIXELS) {
|
|
360
|
+
try {
|
|
361
|
+
return await inThreads(pool.threads);
|
|
362
|
+
}
|
|
363
|
+
catch {
|
|
364
|
+
for (const t of pool.threads)
|
|
365
|
+
t.terminate();
|
|
366
|
+
pool.threads = null;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return here();
|
|
370
|
+
}
|
|
371
|
+
const sharedPlanes = (n, w, h) => Array.from({ length: n }, () => new Float32Array(new SharedArrayBuffer(4 * w * h)));
|
|
372
|
+
const BAND = 64;
|
|
373
|
+
export function postChunks(t) {
|
|
374
|
+
const { w, h, inputs, outputs } = t;
|
|
375
|
+
if (t.kind === 'gaborish')
|
|
376
|
+
return chunk => gaborishRows(inputs, outputs, w, h, t.weights, chunk * BAND, Math.min(h, (chunk + 1) * BAND));
|
|
377
|
+
if (t.kind === 'epf') {
|
|
378
|
+
const scratch = epfScratch(w);
|
|
379
|
+
return chunk => epfStrip(inputs, outputs, w, h, t.epf, t.sigma ?? null, t.bw, t.step, chunk, scratch);
|
|
380
|
+
}
|
|
381
|
+
if (t.kind === 'samples')
|
|
382
|
+
return chunk => samplesToBytes(t.samples, t.rctType, t.bits, t.rgba, chunk * BAND * w, Math.min(h, (chunk + 1) * BAND) * w);
|
|
383
|
+
const [x, y, b] = inputs;
|
|
384
|
+
return chunk => xybToBytes(x, y, b, t.m, t.bias, t.k, t.rgba, chunk * BAND * w, Math.min(h, (chunk + 1) * BAND) * w);
|
|
385
|
+
}
|
|
386
|
+
async function filterThreaded(p, f, image, sigma, sigmaStride, threads) {
|
|
387
|
+
if (!f.gab && !f.epf)
|
|
388
|
+
return p;
|
|
389
|
+
const numColor = p.channels.length - image.extraChannels.length, w = p.width, h = p.height;
|
|
390
|
+
let color = (numColor === 1 ? [p.channels[0], p.channels[0], p.channels[0]] : p.channels.slice(0, 3)).map(sharedOf);
|
|
391
|
+
if (f.gab) {
|
|
392
|
+
const task = { task: 'jxl-post', kind: 'gaborish', w, h, inputs: color, outputs: sharedPlanes(3, w, h), weights: f.gab };
|
|
393
|
+
await runChunks(threads, task, Math.ceil(h / BAND), postChunks(task));
|
|
394
|
+
color = task.outputs;
|
|
395
|
+
}
|
|
396
|
+
if (f.epf) {
|
|
397
|
+
const s = sigma && sharedOf(sigma);
|
|
398
|
+
for (const step of epfSteps(f.epf)) {
|
|
399
|
+
const task = { task: 'jxl-post', kind: 'epf', w, h, inputs: color, outputs: sharedPlanes(3, w, h), epf: f.epf, sigma: s, bw: sigmaStride, step };
|
|
400
|
+
await runChunks(threads, task, epfStrips(h), postChunks(task));
|
|
401
|
+
color = task.outputs;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return { ...p, channels: [...color.slice(0, numColor), ...p.channels.slice(numColor)] };
|
|
405
|
+
}
|
|
236
406
|
function upsamplePlanes(p, f, image) {
|
|
407
|
+
if (f.upsampling === 1 && f.ecUpsampling.every(u => u === 1))
|
|
408
|
+
return p;
|
|
237
409
|
const numColor = p.channels.length - image.extraChannels.length, W = f.upsampledWidth, H = f.upsampledHeight;
|
|
238
410
|
const channels = p.channels.map((c, i) => {
|
|
239
411
|
const up = i < numColor ? f.upsampling : f.ecUpsampling[i - numColor];
|
|
@@ -258,22 +430,31 @@ function floatSample(v, bits, expBits) {
|
|
|
258
430
|
value = (1 + mantissa / 2 ** mantissaBits) * 2 ** (exp - bias);
|
|
259
431
|
return sign ? -value : value;
|
|
260
432
|
}
|
|
261
|
-
|
|
433
|
+
const referenced = (f) => !f.isLast && (f.duration === 0 || f.saveAsReference !== 0) && f.type !== FRAME_LF;
|
|
434
|
+
const shownAsIs = (f, image) => (f.type === FRAME_REGULAR || f.type === FRAME_SKIP_PROGRESSIVE) && !f.haveCrop
|
|
435
|
+
&& f.blending.mode === BLEND_REPLACE && f.ecBlending.every(b => b.mode === BLEND_REPLACE) && f.width === image.width && f.height === image.height
|
|
436
|
+
&& !referenced(f) && image.extraChannels.every(e => e.type === EXTRA_ALPHA && !e.alphaAssociated);
|
|
437
|
+
async function render(p, f, state, pool) {
|
|
262
438
|
const image = state.image;
|
|
263
439
|
let planes = p;
|
|
264
440
|
if (f.type === FRAME_LF) {
|
|
265
441
|
state.lfFrames[f.lfLevel - 1] = planes;
|
|
266
442
|
return { header: f, canvas: null };
|
|
267
443
|
}
|
|
268
|
-
const canReference =
|
|
444
|
+
const canReference = referenced(f);
|
|
269
445
|
if (canReference && f.saveBeforeCT) {
|
|
270
446
|
state.references[f.saveAsReference] = planes;
|
|
271
447
|
state.referenceIsXYB[f.saveAsReference] = image.xybEncoded;
|
|
272
448
|
}
|
|
273
449
|
const normal = f.type === FRAME_REGULAR || f.type === FRAME_SKIP_PROGRESSIVE;
|
|
274
|
-
const
|
|
275
|
-
const
|
|
276
|
-
planes =
|
|
450
|
+
const final = shownAsIs(f, image) && p.width === image.width && p.height === image.height;
|
|
451
|
+
const bytes = state.output.bytesParams(f.doYCbCr, final), input = planes;
|
|
452
|
+
planes = bytes ? await threaded(pool, planes.width * planes.height, async (threads) => {
|
|
453
|
+
const { width: w, height: h, channels } = input, rgba = new Uint8Array(new SharedArrayBuffer(4 * w * h));
|
|
454
|
+
const task = { task: 'jxl-post', kind: 'xyb', w, h, inputs: channels.slice(0, 3).map(sharedOf), outputs: [], rgba, ...bytes };
|
|
455
|
+
await runChunks(threads, task, Math.ceil(h / BAND), postChunks(task));
|
|
456
|
+
return { width: w, height: h, channels: channels.slice(3), rgba: new Uint8Array(rgba) };
|
|
457
|
+
}, () => state.output.transform(input, f.doYCbCr, final)) : state.output.transform(planes, f.doYCbCr, final);
|
|
277
458
|
const canvas = normal ? blend(planes, f, state) : planes;
|
|
278
459
|
if (canReference && !f.saveBeforeCT) {
|
|
279
460
|
state.references[f.saveAsReference] = canvas;
|
|
@@ -263,6 +263,34 @@ function transform(r) {
|
|
|
263
263
|
}
|
|
264
264
|
return t;
|
|
265
265
|
}
|
|
266
|
+
export function rctOrder(rctType) {
|
|
267
|
+
const perm = Math.floor(rctType / 7);
|
|
268
|
+
return [perm % 3, (perm + 1 + Math.floor(perm / 3)) % 3, (perm + 2 - Math.floor(perm / 3)) % 3];
|
|
269
|
+
}
|
|
270
|
+
export function rctSamples([a, b, c], rctType, i0, i1) {
|
|
271
|
+
const type = rctType % 7;
|
|
272
|
+
if (type === 6) {
|
|
273
|
+
for (let i = i0; i < i1; i++) {
|
|
274
|
+
const Y = a[i], Co = b[i], Cg = c[i];
|
|
275
|
+
const tmp = (Y - (Cg >> 1)) | 0, G = (Cg + tmp) | 0, B = (tmp - (Co >> 1)) | 0;
|
|
276
|
+
a[i] = (B + Co) | 0;
|
|
277
|
+
b[i] = G;
|
|
278
|
+
c[i] = B;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
else if (type !== 0) {
|
|
282
|
+
const second = type >> 1, third = type & 1;
|
|
283
|
+
for (let i = i0; i < i1; i++) {
|
|
284
|
+
const first = a[i];
|
|
285
|
+
if (third)
|
|
286
|
+
c[i] = (c[i] + first) | 0;
|
|
287
|
+
if (second === 1)
|
|
288
|
+
b[i] = (b[i] + first) | 0;
|
|
289
|
+
else if (second === 2)
|
|
290
|
+
b[i] = (b[i] + (((first + c[i]) | 0) >> 1)) | 0;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
266
294
|
export class ModularImage {
|
|
267
295
|
channels;
|
|
268
296
|
bitDepth;
|
|
@@ -363,8 +391,8 @@ export class ModularImage {
|
|
|
363
391
|
}
|
|
364
392
|
return out;
|
|
365
393
|
}
|
|
366
|
-
undo() {
|
|
367
|
-
while (this.transforms.length) {
|
|
394
|
+
undo(keep = 0) {
|
|
395
|
+
while (this.transforms.length > keep) {
|
|
368
396
|
const t = this.transforms.pop();
|
|
369
397
|
if (t.id === TR_RCT)
|
|
370
398
|
this.inverseRCT(t);
|
|
@@ -376,34 +404,17 @@ export class ModularImage {
|
|
|
376
404
|
}
|
|
377
405
|
}
|
|
378
406
|
inverseRCT(t) {
|
|
407
|
+
const c = this.channels[t.beginC];
|
|
408
|
+
rctSamples(this.rctChannels(t), t.rctType, 0, c.w * c.h);
|
|
409
|
+
this.permuteRCT(t);
|
|
410
|
+
}
|
|
411
|
+
rctChannels(t) {
|
|
379
412
|
this.checkEqual(t.beginC, t.beginC + 2);
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
if (type === 6) {
|
|
386
|
-
for (let i = 0; i < n; i++) {
|
|
387
|
-
const Y = a[i], Co = b[i], Cg = c[i];
|
|
388
|
-
const tmp = (Y - (Cg >> 1)) | 0, G = (Cg + tmp) | 0, B = (tmp - (Co >> 1)) | 0;
|
|
389
|
-
a[i] = (B + Co) | 0;
|
|
390
|
-
b[i] = G;
|
|
391
|
-
c[i] = B;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
else {
|
|
395
|
-
const second = type >> 1, third = type & 1;
|
|
396
|
-
for (let i = 0; i < n; i++) {
|
|
397
|
-
const first = a[i];
|
|
398
|
-
if (third)
|
|
399
|
-
c[i] = (c[i] + first) | 0;
|
|
400
|
-
if (second === 1)
|
|
401
|
-
b[i] = (b[i] + first) | 0;
|
|
402
|
-
else if (second === 2)
|
|
403
|
-
b[i] = (b[i] + (((first + c[i]) | 0) >> 1)) | 0;
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
}
|
|
413
|
+
return [0, 1, 2].map(i => this.channels[t.beginC + i].data);
|
|
414
|
+
}
|
|
415
|
+
permuteRCT(t) {
|
|
416
|
+
const m = t.beginC, ch = this.channels, [c0, c1, c2] = ch.slice(m, m + 3);
|
|
417
|
+
const [o0, o1, o2] = rctOrder(t.rctType).map(o => m + o);
|
|
407
418
|
ch[o0] = c0;
|
|
408
419
|
ch[o1] = c1;
|
|
409
420
|
ch[o2] = c2;
|