imagescript 3.5.0 → 3.7.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 +4 -4
- package/THIRD_PARTY_NOTICES.md +3 -2
- package/dist/animation.d.ts +1 -1
- package/dist/animation.js +1 -1
- package/dist/codecs/av1/encode.js +186 -44
- package/dist/codecs/avif-enc.js +24 -10
- package/dist/codecs/jxl/acs-enc.js +294 -0
- package/dist/codecs/jxl/aq-enc.js +176 -0
- package/dist/codecs/jxl/bit-writer.js +46 -0
- package/dist/codecs/jxl/color.js +61 -1
- package/dist/codecs/jxl/dct.js +7 -7
- package/dist/codecs/jxl/entropy-enc.js +589 -0
- package/dist/codecs/jxl/entropy.js +1 -1
- package/dist/codecs/jxl/fast-lossless.js +2 -107
- package/dist/codecs/jxl/fdct.js +1114 -0
- package/dist/codecs/jxl/filters.js +49 -2
- package/dist/codecs/jxl/frame.js +4 -1
- package/dist/codecs/jxl/headers-enc.js +60 -0
- package/dist/codecs/jxl/idct.js +804 -2
- package/dist/codecs/jxl/modular-enc.js +100 -0
- package/dist/codecs/jxl/modular.js +3 -2
- package/dist/codecs/jxl/quant.js +1 -0
- package/dist/codecs/jxl/transforms-enc.js +188 -0
- package/dist/codecs/jxl/vardct-enc.js +397 -0
- package/dist/codecs/jxl/vardct.js +6 -6
- package/dist/codecs/jxl.js +14 -1
- package/dist/codecs/png.js +29 -26
- package/dist/codecs/vp8-enc-dsp.js +20 -13
- package/dist/codecs/vp8-enc.js +35 -14
- package/dist/codecs/vp8l.js +356 -58
- package/dist/image.d.ts +27 -7
- package/dist/image.js +16 -6
- package/dist/index.d.ts +1 -1
- package/dist/metadata.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import { BitWriter } from "./bit-writer.js";
|
|
2
|
+
import { imageHeader, u32 } from "./headers-enc.js";
|
|
3
|
+
import { EntropyCode, Tokens, packSigned } from "./entropy-enc.js";
|
|
4
|
+
import { MATree, PRED_GRADIENT, PRED_LEFT, PRED_WEIGHTED, PROP_CHANNEL, PROP_STREAM, PROP_WP_ERROR, channelTokens, fixedTree, streamHeader } from "./modular-enc.js";
|
|
5
|
+
import { forwardTransform } from "./transforms-enc.js";
|
|
6
|
+
import { initialQuantField } from "./aq-enc.js";
|
|
7
|
+
import { chooseVarblocks } from "./acs-enc.js";
|
|
8
|
+
import { defaultDequant, MATRIX_INDEX } from "./quant.js";
|
|
9
|
+
import { naturalOrder, DEFAULT_CTX_MAP, ORDER_ID, SIZES, single, transposed } from "./vardct.js";
|
|
10
|
+
const OPSIN = [
|
|
11
|
+
0.30, 0.622, 0.078,
|
|
12
|
+
0.23, 0.692, 0.078,
|
|
13
|
+
0.2434226892454782, 0.2047674442449682, 0.5518098665095535
|
|
14
|
+
];
|
|
15
|
+
const BIAS = 0.0037930732552754493, CBRT_BIAS = Math.cbrt(BIAS);
|
|
16
|
+
const LINEAR = Float64Array.from({ length: 256 }, (_, i) => {
|
|
17
|
+
const v = i / 255;
|
|
18
|
+
return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
|
|
19
|
+
});
|
|
20
|
+
function toXYB(rgba, width, height, pw, ph) {
|
|
21
|
+
const x = new Float32Array(pw * ph), y = new Float32Array(pw * ph), b = new Float32Array(pw * ph);
|
|
22
|
+
for (let py = 0; py < ph; py++) {
|
|
23
|
+
const sy = Math.min(py, height - 1);
|
|
24
|
+
for (let px = 0; px < pw; px++) {
|
|
25
|
+
const s = 4 * (sy * width + Math.min(px, width - 1));
|
|
26
|
+
const r = LINEAR[rgba[s]], g = LINEAR[rgba[s + 1]], bl = LINEAR[rgba[s + 2]];
|
|
27
|
+
const l = Math.cbrt(Math.max(OPSIN[0] * r + OPSIN[1] * g + OPSIN[2] * bl + BIAS, 0)) - CBRT_BIAS;
|
|
28
|
+
const m = Math.cbrt(Math.max(OPSIN[3] * r + OPSIN[4] * g + OPSIN[5] * bl + BIAS, 0)) - CBRT_BIAS;
|
|
29
|
+
const s3 = Math.cbrt(Math.max(OPSIN[6] * r + OPSIN[7] * g + OPSIN[8] * bl + BIAS, 0)) - CBRT_BIAS;
|
|
30
|
+
const i = py * pw + px;
|
|
31
|
+
x[i] = 0.5 * (l - m);
|
|
32
|
+
y[i] = 0.5 * (l + m);
|
|
33
|
+
b[i] = s3;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return [x, y, b];
|
|
37
|
+
}
|
|
38
|
+
function quantizer(distance) {
|
|
39
|
+
const dcDistance = Math.max(0.5 * distance, Math.min(distance, 0.3 * (distance / 0.3) ** 0.83));
|
|
40
|
+
const quantDc = Math.fround(Math.min(1.095924047623553 / dcDistance, 50));
|
|
41
|
+
const q = Math.fround(0.79 / distance);
|
|
42
|
+
let globalScale = Math.trunc(Math.min(Math.max(65536 * q / 5, 1), 1 << 15));
|
|
43
|
+
const scaledQuantDc = Math.trunc(quantDc * 4096 * 1.6);
|
|
44
|
+
if (globalScale > scaledQuantDc)
|
|
45
|
+
globalScale = Math.max(scaledQuantDc, 1);
|
|
46
|
+
const inv = 65536 / globalScale;
|
|
47
|
+
const quantLf = Math.trunc(Math.min(quantDc * inv + 0.5, 1 << 16));
|
|
48
|
+
const mul = Math.trunc(Math.max(1, Math.min(q * inv + 0.5, 256)));
|
|
49
|
+
return [globalScale, quantLf, mul];
|
|
50
|
+
}
|
|
51
|
+
const LF_DEQUANT = [1 / 32, 1 / 4, 1 / 2];
|
|
52
|
+
const QUANT_BIAS = [1 - 0.05465007330715401, 1 - 0.07005449891748593, 1 - 0.049935103337343655], QUANT_BIAS_NUMERATOR = 0.145;
|
|
53
|
+
const NUM_BLOCK_CTX = 15, MAX_COEFFS = 64 * 64;
|
|
54
|
+
const FREQ_CONTEXT = Uint8Array.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30);
|
|
55
|
+
const NUM_NONZERO_CONTEXT = Uint8Array.of(0, 31, 62, 62, 93, 93, 93, 93, 123, 123, 123, 123, 152, 152, 152, 152, 152, 152, 152, 152, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206);
|
|
56
|
+
const ceilLog2 = (x) => x <= 1 ? 0 : 32 - Math.clz32(x - 1);
|
|
57
|
+
const LF_CUTOFFS = [
|
|
58
|
+
-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, 63, 95,
|
|
59
|
+
127, 191, 255, 392, 500
|
|
60
|
+
];
|
|
61
|
+
function globalTree(numLfGroups, numPixels) {
|
|
62
|
+
const lf = () => fixedTree(PROP_WP_ERROR, LF_CUTOFFS, PRED_WEIGHTED, numPixels);
|
|
63
|
+
const byChannel = (n, leaf) => {
|
|
64
|
+
let node = leaf();
|
|
65
|
+
for (let c = n - 2; c >= 0; c--)
|
|
66
|
+
node = { property: PROP_CHANNEL, split: c, above: node, below: leaf() };
|
|
67
|
+
return node;
|
|
68
|
+
};
|
|
69
|
+
const hf = byChannel(4, () => ({ predictor: PRED_LEFT })), alpha = () => ({ predictor: PRED_GRADIENT });
|
|
70
|
+
const lfOrAlpha = { property: PROP_STREAM, split: 0, above: byChannel(3, lf), below: alpha() };
|
|
71
|
+
return new MATree({ property: PROP_STREAM, split: 3 * numLfGroups + 16, above: alpha(), below: { property: PROP_STREAM, split: numLfGroups, above: hf, below: lfOrAlpha } });
|
|
72
|
+
}
|
|
73
|
+
const inverses = new Map();
|
|
74
|
+
function inverseMatrices(type) {
|
|
75
|
+
let inv = inverses.get(type);
|
|
76
|
+
if (!inv)
|
|
77
|
+
inverses.set(type, inv = defaultDequant(MATRIX_INDEX[type])[transposed(type) ? 1 : 0].map(m => Float32Array.from(m, v => 1 / v)));
|
|
78
|
+
return inv;
|
|
79
|
+
}
|
|
80
|
+
function chromaFromLuma(raw, types, muls, pw, bw, bh, globalScale, xFromY, bFromY) {
|
|
81
|
+
const tilesPerRow = Math.ceil(bw / 8);
|
|
82
|
+
for (let ty = 0; ty < bh; ty += 8) {
|
|
83
|
+
for (let tx = 0; tx < bw; tx += 8) {
|
|
84
|
+
const sums = [0, 0, 0, 0];
|
|
85
|
+
let num = 0;
|
|
86
|
+
for (let by = ty; by < Math.min(ty + 8, bh); by++) {
|
|
87
|
+
for (let bx = tx; bx < Math.min(tx + 8, bw); bx++) {
|
|
88
|
+
const type = types[by * bw + bx];
|
|
89
|
+
if (type < 0)
|
|
90
|
+
continue;
|
|
91
|
+
const w8 = SIZES[2 * type], h8 = SIZES[2 * type + 1], w = w8 * 8, h = h8 * 8, o = by * 8 * pw + bx * 8, one = single(type);
|
|
92
|
+
const inv = inverseMatrices(type), q = globalScale / 65536 * 128 * muls[by * bw + bx];
|
|
93
|
+
num += w * h;
|
|
94
|
+
for (let y = 0; y < h; y++) {
|
|
95
|
+
for (let x = 0; x < w; x++) {
|
|
96
|
+
if (one ? x === 0 && y === 0 : x < w8 && y < h8)
|
|
97
|
+
continue;
|
|
98
|
+
const k = y * w + x, yv = raw[1][o + y * pw + x];
|
|
99
|
+
for (let c = 0; c < 2; c++) {
|
|
100
|
+
const ch = c ? 2 : 0, qm = q * inv[ch][k], base = c;
|
|
101
|
+
const a = yv * qm / 84, b = base * yv * qm - raw[ch][o + y * pw + x] * qm;
|
|
102
|
+
sums[2 * c] += a * a;
|
|
103
|
+
sums[2 * c + 1] += a * b;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const factor = (aa, ab) => {
|
|
110
|
+
if (!num)
|
|
111
|
+
return 0;
|
|
112
|
+
let x = -ab / (aa + num * 1e-9 * 0.5);
|
|
113
|
+
x = x >= 2.6 ? x - 2.6 : x <= -2.6 ? x + 2.6 : 0;
|
|
114
|
+
return Math.max(-128, Math.min(127, Math.round(x)));
|
|
115
|
+
};
|
|
116
|
+
const t = (ty >> 3) * tilesPerRow + (tx >> 3);
|
|
117
|
+
xFromY[t] = factor(sums[0], sums[1]);
|
|
118
|
+
bFromY[t] = factor(sums[2], sums[3]);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
export function encodeVarDCT(rgba, width, height, { distance, effort }) {
|
|
123
|
+
if (width < 1 || height < 1)
|
|
124
|
+
throw new RangeError('JPEG XL: dimensions must be at least 1');
|
|
125
|
+
const bw = Math.ceil(width / 8), bh = Math.ceil(height / 8), pw = bw * 8;
|
|
126
|
+
const planes = toXYB(rgba, width, height, pw, bh * 8);
|
|
127
|
+
let alpha = null;
|
|
128
|
+
for (let i = 3; i < rgba.length; i += 4) {
|
|
129
|
+
if (rgba[i] === 255)
|
|
130
|
+
continue;
|
|
131
|
+
alpha = new Int32Array(width * height);
|
|
132
|
+
for (let k = 0; k < alpha.length; k++)
|
|
133
|
+
alpha[k] = rgba[4 * k + 3];
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
const [globalScale, quantLf, mul] = quantizer(distance);
|
|
137
|
+
const xqmScale = 3 + [2.5, 5.5, 9.5].filter(t => distance > t).length, bqmScale = 2;
|
|
138
|
+
const qmMul = [1.25 ** (xqmScale - 2), 1, 1.25 ** (bqmScale - 2)];
|
|
139
|
+
const epfIters = [0.7, 1.5, 4].filter(t => distance >= t).length;
|
|
140
|
+
const lfStep = LF_DEQUANT.map(m => m * 512 / (globalScale * quantLf));
|
|
141
|
+
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
|
+
const muls = new Int32Array(bw * bh).fill(mul);
|
|
143
|
+
const lfValues = [0, 1, 2].map(() => new Float32Array(bw * bh));
|
|
144
|
+
const raw = [0, 1, 2].map(() => new Float32Array(pw * bh * 8));
|
|
145
|
+
const coeffs = new Float32Array(MAX_COEFFS), lfBlock = new Float32Array(MAX_COEFFS / 64);
|
|
146
|
+
for (let by = 0; by < bh; by++) {
|
|
147
|
+
for (let bx = 0; bx < bw; bx++) {
|
|
148
|
+
const type = types[by * bw + bx];
|
|
149
|
+
if (type < 0)
|
|
150
|
+
continue;
|
|
151
|
+
const w8 = SIZES[2 * type], h8 = SIZES[2 * type + 1], w = w8 * 8, h = h8 * 8, o = by * 8 * pw + bx * 8;
|
|
152
|
+
for (let c = 0; c < 3; c++) {
|
|
153
|
+
forwardTransform(type, planes[c], o, pw, coeffs, lfBlock);
|
|
154
|
+
for (let y = 0; y < h; y++)
|
|
155
|
+
raw[c].set(coeffs.subarray(y * w, (y + 1) * w), o + y * pw);
|
|
156
|
+
for (let y = 0; y < h8; y++)
|
|
157
|
+
for (let x = 0; x < w8; x++)
|
|
158
|
+
lfValues[c][(by + y) * bw + bx + x] = lfBlock[y * w8 + x];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const tilesPerRow = Math.ceil(bw / 8), tileRows = Math.ceil(bh / 8);
|
|
163
|
+
const xFromY = new Int32Array(tilesPerRow * tileRows), bFromY = new Int32Array(tilesPerRow * tileRows);
|
|
164
|
+
if (effort)
|
|
165
|
+
chromaFromLuma(raw, types, muls, pw, bw, bh, globalScale, xFromY, bFromY);
|
|
166
|
+
const hf = [0, 1, 2].map(() => new Int32Array(pw * bh * 8));
|
|
167
|
+
const yDequant = new Float32Array(MAX_COEFFS);
|
|
168
|
+
for (let by = 0; by < bh; by++) {
|
|
169
|
+
for (let bx = 0; bx < bw; bx++) {
|
|
170
|
+
const bi = by * bw + bx, type = types[bi];
|
|
171
|
+
if (type < 0)
|
|
172
|
+
continue;
|
|
173
|
+
const w8 = SIZES[2 * type], h8 = SIZES[2 * type + 1], w = w8 * 8, h = h8 * 8, o = by * 8 * pw + bx * 8;
|
|
174
|
+
const tile = (by >> 3) * tilesPerRow + (bx >> 3), cfl = [xFromY[tile] / 84, 0, 1 + bFromY[tile] / 84];
|
|
175
|
+
const [m0, m1] = defaultDequant(MATRIX_INDEX[type]), matrices = transposed(type) ? m1 : m0;
|
|
176
|
+
const hfStep = 65536 / (globalScale * muls[bi]), one = single(type);
|
|
177
|
+
for (const c of [1, 0, 2]) {
|
|
178
|
+
const src = raw[c], m = matrices[c], out = hf[c], k0 = cfl[c];
|
|
179
|
+
const thresholds = c === 1 ? [0.56, 0.62, 0.62, 0.62] : [0.58, 0.62, 0.62, 0.62];
|
|
180
|
+
if (c !== 1 && w8 * h8 >= 4)
|
|
181
|
+
for (let i = 0; i < 4; i++)
|
|
182
|
+
thresholds[i] = Math.max(0.5, thresholds[i] - 0.00744 * w8 * h8);
|
|
183
|
+
const scale = qmMul[c] / hfStep;
|
|
184
|
+
for (let y = 0; y < h; y++) {
|
|
185
|
+
for (let x = 0; x < w; x++) {
|
|
186
|
+
const k = y * w + x;
|
|
187
|
+
if (one ? k === 0 : x < w8 && y < h8)
|
|
188
|
+
continue;
|
|
189
|
+
const v = (c === 1 ? src[o + y * pw + x] : src[o + y * pw + x] - k0 * yDequant[k]) / m[k] * scale;
|
|
190
|
+
const q = Math.abs(v) >= thresholds[(y >= h / 2 ? 2 : 0) + (x >= w / 2 ? 1 : 0)] ? Math.round(v) : 0;
|
|
191
|
+
out[o + y * pw + x] = q;
|
|
192
|
+
if (c === 1) {
|
|
193
|
+
const d = q === 0 ? 0 : Math.abs(q) <= 1 ? q * QUANT_BIAS[1] : q - QUANT_BIAS_NUMERATOR / q;
|
|
194
|
+
yDequant[k] = d * m[k] * hfStep;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const lf = [0, 1, 2].map(() => new Int32Array(bw * bh));
|
|
202
|
+
for (let i = 0; i < bw * bh; i++) {
|
|
203
|
+
const ly = Math.round(lfValues[1][i] / lfStep[1]);
|
|
204
|
+
lf[1][i] = ly;
|
|
205
|
+
lf[0][i] = Math.round(lfValues[0][i] / lfStep[0]);
|
|
206
|
+
lf[2][i] = Math.round((lfValues[2][i] - ly * lfStep[1]) / lfStep[2]);
|
|
207
|
+
}
|
|
208
|
+
const groupsPerRow = Math.ceil(width / 256), numGroups = groupsPerRow * Math.ceil(height / 256);
|
|
209
|
+
const lfGroupsPerRow = Math.ceil(width / 2048), numLfGroups = lfGroupsPerRow * Math.ceil(height / 2048);
|
|
210
|
+
const tree = globalTree(numLfGroups, 3 * bw * bh);
|
|
211
|
+
const lfTokens = [], numVarblocks = [];
|
|
212
|
+
const lfGroupBlocks = (g) => {
|
|
213
|
+
const x0 = (g % lfGroupsPerRow) * 256, y0 = Math.floor(g / lfGroupsPerRow) * 256;
|
|
214
|
+
return [x0, y0, Math.min(256, bw - x0), Math.min(256, bh - y0)];
|
|
215
|
+
};
|
|
216
|
+
const crop = (src, x0, y0, w, h, stride) => {
|
|
217
|
+
const out = new Int32Array(w * h);
|
|
218
|
+
for (let y = 0; y < h; y++)
|
|
219
|
+
out.set(src.subarray((y0 + y) * stride + x0, (y0 + y) * stride + x0 + w), y * w);
|
|
220
|
+
return out;
|
|
221
|
+
};
|
|
222
|
+
for (let g = 0; g < numLfGroups; g++) {
|
|
223
|
+
const [x0, y0, w, h] = lfGroupBlocks(g), t = new Tokens();
|
|
224
|
+
[1, 0, 2].forEach((c, i) => channelTokens(t, tree, crop(lf[c], x0, y0, w, h, bw), w, h, i, 1 + g));
|
|
225
|
+
lfTokens.push(t);
|
|
226
|
+
const m = new Tokens(), tw = Math.ceil(w / 8), th = Math.ceil(h / 8), stream = 1 + 2 * numLfGroups + g;
|
|
227
|
+
channelTokens(m, tree, crop(xFromY, x0 / 8, y0 / 8, tw, th, tilesPerRow), tw, th, 0, stream);
|
|
228
|
+
channelTokens(m, tree, crop(bFromY, x0 / 8, y0 / 8, tw, th, tilesPerRow), tw, th, 1, stream);
|
|
229
|
+
const blocks = [];
|
|
230
|
+
for (let y = y0; y < y0 + h; y++)
|
|
231
|
+
for (let x = x0; x < x0 + w; x++)
|
|
232
|
+
if (types[y * bw + x] >= 0)
|
|
233
|
+
blocks.push(y * bw + x);
|
|
234
|
+
const info = new Int32Array(2 * blocks.length);
|
|
235
|
+
blocks.forEach((bi, k) => {
|
|
236
|
+
info[k] = types[bi];
|
|
237
|
+
info[blocks.length + k] = muls[bi] - 1;
|
|
238
|
+
});
|
|
239
|
+
numVarblocks.push(blocks.length);
|
|
240
|
+
channelTokens(m, tree, info, blocks.length, 2, 2, stream);
|
|
241
|
+
channelTokens(m, tree, new Int32Array(w * h).fill(4), w, h, 3, stream);
|
|
242
|
+
lfTokens.push(m);
|
|
243
|
+
}
|
|
244
|
+
const alphaTokens = [];
|
|
245
|
+
if (alpha && numGroups === 1) {
|
|
246
|
+
const t = new Tokens();
|
|
247
|
+
channelTokens(t, tree, alpha, width, height, 0, 0);
|
|
248
|
+
alphaTokens.push(t);
|
|
249
|
+
}
|
|
250
|
+
else if (alpha) {
|
|
251
|
+
for (let g = 0; g < numGroups; g++) {
|
|
252
|
+
const x0 = (g % groupsPerRow) * 256, y0 = Math.floor(g / groupsPerRow) * 256, w = Math.min(256, width - x0), h = Math.min(256, height - y0);
|
|
253
|
+
const t = new Tokens();
|
|
254
|
+
channelTokens(t, tree, crop(alpha, x0, y0, w, h, width), w, h, 0, 1 + 3 * numLfGroups + 17 + g);
|
|
255
|
+
alphaTokens.push(t);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const modularCode = new EntropyCode([...lfTokens, ...alphaTokens], tree.numLeaves);
|
|
259
|
+
const hfTokens = [];
|
|
260
|
+
for (let g = 0; g < numGroups; g++) {
|
|
261
|
+
const bx0 = (g % groupsPerRow) * 32, by0 = Math.floor(g / groupsPerRow) * 32;
|
|
262
|
+
const gw = Math.min(32, bw - bx0), gh = Math.min(32, bh - by0), t = new Tokens();
|
|
263
|
+
const nonZeros = [0, 1, 2].map(() => new Uint32Array(gw));
|
|
264
|
+
for (let y = 0; y < gh; y++) {
|
|
265
|
+
for (let x = 0; x < gw; x++) {
|
|
266
|
+
const type = types[(by0 + y) * bw + bx0 + x];
|
|
267
|
+
if (type < 0)
|
|
268
|
+
continue;
|
|
269
|
+
const w8 = SIZES[2 * type], numBlocks = w8 * SIZES[2 * type + 1], log = 31 - Math.clz32(numBlocks);
|
|
270
|
+
const orderId = ORDER_ID[type], order = naturalOrder(orderId), n = order.length / 2, tr = transposed(type);
|
|
271
|
+
const o = (by0 + y) * 8 * pw + (bx0 + x) * 8;
|
|
272
|
+
const at = (i) => tr ? o + order[2 * i] * pw + order[2 * i + 1] : o + order[2 * i + 1] * pw + order[2 * i];
|
|
273
|
+
for (let k = 0; k < 3; k++) {
|
|
274
|
+
const c = [1, 0, 2][k], q = hf[c], nz = nonZeros[c], blockCtx = DEFAULT_CTX_MAP[k * 13 + orderId];
|
|
275
|
+
let count = 0;
|
|
276
|
+
for (let i = numBlocks; i < n; i++)
|
|
277
|
+
if (q[at(i)])
|
|
278
|
+
count++;
|
|
279
|
+
const predicted = y === 0 ? (x === 0 ? 32 : nz[x - 1]) : x === 0 ? nz[x] : (nz[x] + nz[x - 1] + 1) >> 1;
|
|
280
|
+
t.push(blockCtx + (predicted >= 8 ? 4 + (predicted >> 1) : predicted) * NUM_BLOCK_CTX, count);
|
|
281
|
+
const value = (count + numBlocks - 1) >> log;
|
|
282
|
+
for (let dx = 0; dx < w8; dx++)
|
|
283
|
+
nz[x + dx] = value;
|
|
284
|
+
let prevNonZero = count <= numBlocks * 4 ? 1 : 0;
|
|
285
|
+
const ctxBase = blockCtx * 458 + 37 * NUM_BLOCK_CTX;
|
|
286
|
+
for (let i = numBlocks; i < n && count > 0; i++) {
|
|
287
|
+
const v = q[at(i)];
|
|
288
|
+
t.push(ctxBase + (NUM_NONZERO_CONTEXT[(count - 1) >> log] + FREQ_CONTEXT[(i - numBlocks) >> log]) * 2 + prevNonZero, packSigned(v));
|
|
289
|
+
prevNonZero = v ? 1 : 0;
|
|
290
|
+
if (v)
|
|
291
|
+
count--;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
hfTokens.push(t);
|
|
297
|
+
}
|
|
298
|
+
const hfCode = new EntropyCode(hfTokens, 495 * NUM_BLOCK_CTX);
|
|
299
|
+
const sections = [];
|
|
300
|
+
{
|
|
301
|
+
const w = new BitWriter();
|
|
302
|
+
w.write(1, 1);
|
|
303
|
+
u32(w, globalScale, [[11, 1], [11, 2049], [12, 4097], [16, 8193]]);
|
|
304
|
+
u32(w, quantLf, [[0, 16], [5, 1], [8, 1], [16, 1]]);
|
|
305
|
+
w.write(1, 1);
|
|
306
|
+
w.write(1, 1);
|
|
307
|
+
w.write(1, 1);
|
|
308
|
+
tree.write(w);
|
|
309
|
+
modularCode.writeHeader(w);
|
|
310
|
+
if (alpha) {
|
|
311
|
+
streamHeader(w);
|
|
312
|
+
if (numGroups === 1)
|
|
313
|
+
modularCode.writeTokens(w, alphaTokens[0]);
|
|
314
|
+
}
|
|
315
|
+
sections.push(w);
|
|
316
|
+
}
|
|
317
|
+
for (let g = 0; g < numLfGroups; g++) {
|
|
318
|
+
const w = new BitWriter(), [, , gw, gh] = lfGroupBlocks(g);
|
|
319
|
+
w.write(2, 0);
|
|
320
|
+
streamHeader(w);
|
|
321
|
+
modularCode.writeTokens(w, lfTokens[2 * g]);
|
|
322
|
+
w.write(ceilLog2(gw * gh), numVarblocks[g] - 1);
|
|
323
|
+
streamHeader(w);
|
|
324
|
+
modularCode.writeTokens(w, lfTokens[2 * g + 1]);
|
|
325
|
+
sections.push(w);
|
|
326
|
+
}
|
|
327
|
+
{
|
|
328
|
+
const w = new BitWriter();
|
|
329
|
+
w.write(1, 1);
|
|
330
|
+
w.write(ceilLog2(numGroups), 0);
|
|
331
|
+
w.write(2, 2);
|
|
332
|
+
hfCode.writeHeader(w);
|
|
333
|
+
sections.push(w);
|
|
334
|
+
}
|
|
335
|
+
for (let g = 0; g < numGroups; g++) {
|
|
336
|
+
const w = new BitWriter();
|
|
337
|
+
hfCode.writeTokens(w, hfTokens[g]);
|
|
338
|
+
if (alpha && numGroups > 1) {
|
|
339
|
+
streamHeader(w);
|
|
340
|
+
modularCode.writeTokens(w, alphaTokens[g]);
|
|
341
|
+
}
|
|
342
|
+
sections.push(w);
|
|
343
|
+
}
|
|
344
|
+
const out = new BitWriter();
|
|
345
|
+
imageHeader(out, width, height, alpha ? 4 : 3, true);
|
|
346
|
+
frameHeader(out, xqmScale, bqmScale, epfIters, alpha !== null);
|
|
347
|
+
if (numGroups === 1) {
|
|
348
|
+
const all = new BitWriter();
|
|
349
|
+
for (const s of sections)
|
|
350
|
+
all.append(s);
|
|
351
|
+
sections.length = 0;
|
|
352
|
+
sections.push(all);
|
|
353
|
+
}
|
|
354
|
+
for (const s of sections)
|
|
355
|
+
s.zeroPadToByte();
|
|
356
|
+
out.write(1, 0);
|
|
357
|
+
out.zeroPadToByte();
|
|
358
|
+
for (const s of sections)
|
|
359
|
+
u32(out, s.length, [[10, 0], [14, 1024], [22, 17408], [30, 4211712]]);
|
|
360
|
+
out.zeroPadToByte();
|
|
361
|
+
const total = out.length + sections.reduce((n, s) => n + s.length, 0), bytes = new Uint8Array(total);
|
|
362
|
+
bytes.set(out.bytes());
|
|
363
|
+
let at = out.length;
|
|
364
|
+
for (const s of sections) {
|
|
365
|
+
bytes.set(s.bytes(), at);
|
|
366
|
+
at += s.length;
|
|
367
|
+
}
|
|
368
|
+
return bytes;
|
|
369
|
+
}
|
|
370
|
+
function frameHeader(w, xqmScale, bqmScale, epfIters, alpha) {
|
|
371
|
+
w.write(1, 0);
|
|
372
|
+
w.write(2, 0);
|
|
373
|
+
w.write(1, 0);
|
|
374
|
+
w.write(2, 0);
|
|
375
|
+
w.write(2, 0);
|
|
376
|
+
if (alpha)
|
|
377
|
+
w.write(2, 0);
|
|
378
|
+
w.write(3, xqmScale);
|
|
379
|
+
w.write(3, bqmScale);
|
|
380
|
+
w.write(2, 0);
|
|
381
|
+
w.write(1, 0);
|
|
382
|
+
w.write(2, 0);
|
|
383
|
+
if (alpha)
|
|
384
|
+
w.write(2, 0);
|
|
385
|
+
w.write(1, 1);
|
|
386
|
+
w.write(2, 0);
|
|
387
|
+
w.write(1, 0);
|
|
388
|
+
w.write(1, 0);
|
|
389
|
+
w.write(2, epfIters);
|
|
390
|
+
if (epfIters) {
|
|
391
|
+
w.write(1, 0);
|
|
392
|
+
w.write(1, 0);
|
|
393
|
+
w.write(1, 0);
|
|
394
|
+
}
|
|
395
|
+
w.write(2, 0);
|
|
396
|
+
w.write(2, 0);
|
|
397
|
+
}
|
|
@@ -4,7 +4,7 @@ import { ModularImage, channel, decodeModular } from "./modular.js";
|
|
|
4
4
|
import { FLAG_SKIP_ADAPTIVE_LF_SMOOTHING } from "./frame-header.js";
|
|
5
5
|
import { readDequantMatrices, MATRIX_INDEX } from "./quant.js";
|
|
6
6
|
import { dct2d, inverseTransform, scaleF } from "./dct.js";
|
|
7
|
-
const DEFAULT_CTX_MAP = Uint8Array.of(0, 1, 2, 2, 3, 3, 4, 5, 6, 6, 6, 6, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 14, 14, 14, 14, 7, 8, 9, 9, 10, 11, 12, 13, 14, 14, 14, 14, 14);
|
|
7
|
+
export const DEFAULT_CTX_MAP = Uint8Array.of(0, 1, 2, 2, 3, 3, 4, 5, 6, 6, 6, 6, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 14, 14, 14, 14, 7, 8, 9, 9, 10, 11, 12, 13, 14, 14, 14, 14, 14);
|
|
8
8
|
export function readVarDCTGlobal(r) {
|
|
9
9
|
const globalScale = r.u32([1, 11], [2049, 11], [4097, 12], [8193, 16]), quantLf = r.u32(16, [1, 5], [1, 8], [1, 16]);
|
|
10
10
|
let ctx;
|
|
@@ -41,17 +41,17 @@ export function readVarDCTGlobal(r) {
|
|
|
41
41
|
}
|
|
42
42
|
return { globalScale, quantLf, ctx, colourFactor, baseX, baseB, xFactorLf, bFactorLf };
|
|
43
43
|
}
|
|
44
|
-
const SIZES = [
|
|
44
|
+
export const SIZES = [
|
|
45
45
|
1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 4, 4, 1, 2, 2, 1, 1, 4, 4, 1, 2, 4, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 8, 8, 4, 8, 8, 4,
|
|
46
46
|
16, 16, 8, 16, 16, 8, 32, 32, 16, 32, 32, 16
|
|
47
47
|
];
|
|
48
|
-
const ORDER_ID = [0, 1, 1, 1, 2, 3, 4, 4, 5, 5, 6, 6, 1, 1, 1, 1, 1, 1, 7, 8, 8, 9, 10, 10, 11, 12, 12];
|
|
48
|
+
export const ORDER_ID = [0, 1, 1, 1, 2, 3, 4, 4, 5, 5, 6, 6, 1, 1, 1, 1, 1, 1, 7, 8, 8, 9, 10, 10, 11, 12, 12];
|
|
49
49
|
const ORDER_SIZES = [[8, 8], [8, 8], [16, 16], [32, 32], [16, 8], [32, 8], [32, 16], [64, 64], [64, 32], [128, 128], [128, 64], [256, 256], [256, 128]];
|
|
50
50
|
const special = (t) => (t >= 1 && t <= 3) || (t >= 12 && t <= 17);
|
|
51
|
-
const transposed = (t) => !special(t) && SIZES[2 * t + 1] >= SIZES[2 * t];
|
|
52
|
-
const single = (t) => t === 0 || special(t);
|
|
51
|
+
export const transposed = (t) => !special(t) && SIZES[2 * t + 1] >= SIZES[2 * t];
|
|
52
|
+
export const single = (t) => t === 0 || special(t);
|
|
53
53
|
const naturalOrders = new Map();
|
|
54
|
-
function naturalOrder(id) {
|
|
54
|
+
export function naturalOrder(id) {
|
|
55
55
|
let o = naturalOrders.get(id);
|
|
56
56
|
if (o)
|
|
57
57
|
return o;
|
package/dist/codecs/jxl.js
CHANGED
|
@@ -4,13 +4,26 @@ import { imageHeader, skipICC } from "./jxl/headers.js";
|
|
|
4
4
|
import { frameHeader, toc } from "./jxl/frame-header.js";
|
|
5
5
|
import { decodeFrame } from "./jxl/frame.js";
|
|
6
6
|
import { ColorOutput } from "./jxl/color.js";
|
|
7
|
-
|
|
7
|
+
import { encodeRGBA } from "./jxl/fast-lossless.js";
|
|
8
|
+
import { encodeVarDCT } from "./jxl/vardct-enc.js";
|
|
9
|
+
import { concat } from "../util/mem.js";
|
|
8
10
|
const CONTAINER = [0, 0, 0, 12, 0x4a, 0x58, 0x4c, 0x20, 0x0d, 0x0a, 0x87, 0x0a];
|
|
9
11
|
export function isJXL(d) {
|
|
10
12
|
if (d.length >= 2 && d[0] === 0xff && d[1] === 0x0a)
|
|
11
13
|
return true;
|
|
12
14
|
return d.length >= 12 && CONTAINER.every((v, i) => d[i] === v);
|
|
13
15
|
}
|
|
16
|
+
export function encode(rgba, width, height, exif, distance = 0, effort = 1) {
|
|
17
|
+
const stream = distance > 0 ? encodeVarDCT(rgba, width, height, { distance, effort }) : encodeRGBA(rgba, width, height);
|
|
18
|
+
if (!exif?.length)
|
|
19
|
+
return stream;
|
|
20
|
+
const jxl = [0x6a, 0x78, 0x6c, 0x20];
|
|
21
|
+
return concat([CONTAINER, ...box('ftyp', jxl, [0, 0, 0, 0], jxl), ...box('Exif', [0, 0, 0, 0], exif), ...box('jxlc', stream)]);
|
|
22
|
+
}
|
|
23
|
+
const box = (type, ...content) => {
|
|
24
|
+
const size = 8 + content.reduce((n, c) => n + c.length, 0);
|
|
25
|
+
return [[size >>> 24, size >> 16 & 255, size >> 8 & 255, size & 255, ...Array.from(type, c => c.charCodeAt(0))], ...content];
|
|
26
|
+
};
|
|
14
27
|
const fourcc = (d, o) => String.fromCharCode(d[o], d[o + 1], d[o + 2], d[o + 3]);
|
|
15
28
|
function codestream(d) {
|
|
16
29
|
if (d[0] === 0xff)
|
package/dist/codecs/png.js
CHANGED
|
@@ -22,15 +22,6 @@ const CHANNELS_TO_COLOR_TYPE = { 1: 0, 2: 4, 3: 2, 4: 6 };
|
|
|
22
22
|
const VALID_DEPTHS = { 0: [1, 2, 4, 8, 16], 2: [8, 16], 3: [1, 2, 4, 8], 4: [8, 16], 6: [8, 16] };
|
|
23
23
|
const ADAM7 = [[0, 0, 8, 8], [4, 0, 8, 8], [0, 4, 4, 8], [2, 0, 4, 4], [0, 2, 2, 4], [1, 0, 2, 2], [0, 1, 1, 2]];
|
|
24
24
|
export async function encode(data, { text, width, height, channels, depth = 8, level = 0, palette, transparency, filter: filterRows = false }) {
|
|
25
|
-
const rowBytes = Math.ceil(width * channels * depth / 8);
|
|
26
|
-
let filtered;
|
|
27
|
-
if (filterRows)
|
|
28
|
-
filtered = filter(data, rowBytes, height, Math.max(1, channels * depth / 8));
|
|
29
|
-
else {
|
|
30
|
-
filtered = new Uint8Array(height * (rowBytes + 1));
|
|
31
|
-
for (let y = 0; y < height; y++)
|
|
32
|
-
filtered.set(data.subarray(y * rowBytes, (y + 1) * rowBytes), y * (rowBytes + 1) + 1);
|
|
33
|
-
}
|
|
34
25
|
const header = new Uint8Array(13);
|
|
35
26
|
const view = new DataView(header.buffer);
|
|
36
27
|
view.setUint32(0, width);
|
|
@@ -43,7 +34,7 @@ export async function encode(data, { text, width, height, channels, depth = 8, l
|
|
|
43
34
|
if (transparency)
|
|
44
35
|
chunks.push(chunk(tRNS, transparency));
|
|
45
36
|
chunks.push(...textChunks(text));
|
|
46
|
-
chunks.push(chunk(IDAT, await
|
|
37
|
+
chunks.push(chunk(IDAT, await compressRows(data, width, height, channels, depth, level, filterRows)));
|
|
47
38
|
chunks.push(chunk(IEND, new Uint8Array(0)));
|
|
48
39
|
return concat([HEAD, ...chunks]);
|
|
49
40
|
}
|
|
@@ -61,10 +52,10 @@ function textChunks(text) {
|
|
|
61
52
|
}
|
|
62
53
|
return chunks;
|
|
63
54
|
}
|
|
64
|
-
function filter(data, rowBytes,
|
|
65
|
-
const out = new Uint8Array(
|
|
66
|
-
for (let y =
|
|
67
|
-
const row = y * rowBytes, o = y * (rowBytes + 1) + 1;
|
|
55
|
+
function filter(data, rowBytes, y0, y1, bpp) {
|
|
56
|
+
const out = new Uint8Array((y1 - y0) * (rowBytes + 1));
|
|
57
|
+
for (let y = y0; y < y1; y++) {
|
|
58
|
+
const row = y * rowBytes, o = (y - y0) * (rowBytes + 1) + 1;
|
|
68
59
|
out[o - 1] = 4;
|
|
69
60
|
if (y === 0) {
|
|
70
61
|
for (let i = 0; i < rowBytes; i++)
|
|
@@ -82,6 +73,30 @@ function filter(data, rowBytes, height, bpp) {
|
|
|
82
73
|
}
|
|
83
74
|
return out;
|
|
84
75
|
}
|
|
76
|
+
function prefix(data, rowBytes, y0, y1) {
|
|
77
|
+
const out = new Uint8Array((y1 - y0) * (rowBytes + 1));
|
|
78
|
+
for (let y = y0; y < y1; y++)
|
|
79
|
+
out.set(data.subarray(y * rowBytes, (y + 1) * rowBytes), (y - y0) * (rowBytes + 1) + 1);
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
const SAMPLE_ROWS = 32, SAMPLE_STEP = 512;
|
|
83
|
+
async function compressRows(data, width, height, channels, depth, level, filterRows) {
|
|
84
|
+
const rowBytes = Math.ceil(width * channels * depth / 8), bpp = Math.max(1, channels * depth / 8);
|
|
85
|
+
if (!filterRows)
|
|
86
|
+
return compressAsync(prefix(data, rowBytes, 0, height), level);
|
|
87
|
+
if (height <= SAMPLE_ROWS) {
|
|
88
|
+
const [unfiltered, filtered] = await Promise.all([compressAsync(prefix(data, rowBytes, 0, height), level), compressAsync(filter(data, rowBytes, 0, height, bpp), level)]);
|
|
89
|
+
return unfiltered.length < filtered.length ? unfiltered : filtered;
|
|
90
|
+
}
|
|
91
|
+
const unfilteredSamples = [], filteredSamples = [];
|
|
92
|
+
for (let y = Math.min((SAMPLE_STEP - SAMPLE_ROWS) >> 1, (height - SAMPLE_ROWS) >> 1); y < height; y += SAMPLE_STEP) {
|
|
93
|
+
const end = Math.min(height, y + SAMPLE_ROWS);
|
|
94
|
+
unfilteredSamples.push(prefix(data, rowBytes, y, end));
|
|
95
|
+
filteredSamples.push(filter(data, rowBytes, y, end, bpp));
|
|
96
|
+
}
|
|
97
|
+
const [unfiltered, filtered] = await Promise.all([compressAsync(concat(unfilteredSamples), level), compressAsync(concat(filteredSamples), level)]);
|
|
98
|
+
return compressAsync(unfiltered.length < filtered.length ? prefix(data, rowBytes, 0, height) : filter(data, rowBytes, 0, height, bpp), level);
|
|
99
|
+
}
|
|
85
100
|
function chunk(type, body) {
|
|
86
101
|
const out = new Uint8Array(body.length + 12);
|
|
87
102
|
const view = new DataView(out.buffer);
|
|
@@ -211,18 +226,6 @@ export async function encodeRGBA(rgba, width, height, level, text) {
|
|
|
211
226
|
const { channels, depth, palette, transparency } = format;
|
|
212
227
|
return encode(format.convert(rgba, width, height), { width, height, level, text, channels, depth, palette, transparency, filter: level > 0 && !palette });
|
|
213
228
|
}
|
|
214
|
-
async function compressRows(data, width, height, channels, depth, level, filterRows) {
|
|
215
|
-
const rowBytes = Math.ceil(width * channels * depth / 8);
|
|
216
|
-
let filtered;
|
|
217
|
-
if (filterRows)
|
|
218
|
-
filtered = filter(data, rowBytes, height, Math.max(1, channels * depth / 8));
|
|
219
|
-
else {
|
|
220
|
-
filtered = new Uint8Array(height * (rowBytes + 1));
|
|
221
|
-
for (let y = 0; y < height; y++)
|
|
222
|
-
filtered.set(data.subarray(y * rowBytes, (y + 1) * rowBytes), y * (rowBytes + 1) + 1);
|
|
223
|
-
}
|
|
224
|
-
return compressAsync(filtered, level);
|
|
225
|
-
}
|
|
226
229
|
export async function encodeAnimation(frames, width, height, plays, level, text) {
|
|
227
230
|
if (!frames.length)
|
|
228
231
|
throw new Error('PNG: an animation needs at least one frame');
|
|
@@ -817,19 +817,26 @@ export function sse16(a, ao, b, bo, h) {
|
|
|
817
817
|
return count;
|
|
818
818
|
}
|
|
819
819
|
export function tTransform(input, i, w) {
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
820
|
+
const r00 = input[i], r01 = input[i + 1], r02 = input[i + 2], r03 = input[i + 3];
|
|
821
|
+
const a00 = r00 + r02, a01 = r01 + r03, a02 = r01 - r03, a03 = r00 - r02;
|
|
822
|
+
const h00 = a00 + a01, h01 = a03 + a02, h02 = a03 - a02, h03 = a00 - a01;
|
|
823
|
+
const r10 = input[i + 1 * BPS], r11 = input[i + 1 * BPS + 1], r12 = input[i + 1 * BPS + 2], r13 = input[i + 1 * BPS + 3];
|
|
824
|
+
const a10 = r10 + r12, a11 = r11 + r13, a12 = r11 - r13, a13 = r10 - r12;
|
|
825
|
+
const h10 = a10 + a11, h11 = a13 + a12, h12 = a13 - a12, h13 = a10 - a11;
|
|
826
|
+
const r20 = input[i + 2 * BPS], r21 = input[i + 2 * BPS + 1], r22 = input[i + 2 * BPS + 2], r23 = input[i + 2 * BPS + 3];
|
|
827
|
+
const a20 = r20 + r22, a21 = r21 + r23, a22 = r21 - r23, a23 = r20 - r22;
|
|
828
|
+
const h20 = a20 + a21, h21 = a23 + a22, h22 = a23 - a22, h23 = a20 - a21;
|
|
829
|
+
const r30 = input[i + 3 * BPS], r31 = input[i + 3 * BPS + 1], r32 = input[i + 3 * BPS + 2], r33 = input[i + 3 * BPS + 3];
|
|
830
|
+
const a30 = r30 + r32, a31 = r31 + r33, a32 = r31 - r33, a33 = r30 - r32;
|
|
831
|
+
const h30 = a30 + a31, h31 = a33 + a32, h32 = a33 - a32, h33 = a30 - a31;
|
|
832
|
+
const b00 = h00 + h20, b01 = h10 + h30, b02 = h10 - h30, b03 = h00 - h20;
|
|
833
|
+
const b10 = h01 + h21, b11 = h11 + h31, b12 = h11 - h31, b13 = h01 - h21;
|
|
834
|
+
const b20 = h02 + h22, b21 = h12 + h32, b22 = h12 - h32, b23 = h02 - h22;
|
|
835
|
+
const b30 = h03 + h23, b31 = h13 + h33, b32 = h13 - h33, b33 = h03 - h23;
|
|
836
|
+
return w[0] * Math.abs(b00 + b01) + w[4] * Math.abs(b03 + b02) + w[8] * Math.abs(b03 - b02) + w[12] * Math.abs(b00 - b01) +
|
|
837
|
+
w[1] * Math.abs(b10 + b11) + w[5] * Math.abs(b13 + b12) + w[9] * Math.abs(b13 - b12) + w[13] * Math.abs(b10 - b11) +
|
|
838
|
+
w[2] * Math.abs(b20 + b21) + w[6] * Math.abs(b23 + b22) + w[10] * Math.abs(b23 - b22) + w[14] * Math.abs(b20 - b21) +
|
|
839
|
+
w[3] * Math.abs(b30 + b31) + w[7] * Math.abs(b33 + b32) + w[11] * Math.abs(b33 - b32) + w[15] * Math.abs(b30 - b31);
|
|
833
840
|
}
|
|
834
841
|
export function tDisto4x4(source, b, bo, w) {
|
|
835
842
|
return Math.abs(tTransform(b, bo, w) - source) >> 5;
|