imagescript 3.2.1 → 3.4.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 +35 -10
- package/THIRD_PARTY_NOTICES.md +298 -2
- package/dist/animation.d.ts +25 -8
- package/dist/animation.js +44 -13
- package/dist/codecs/av1/cdef.js +197 -0
- package/dist/codecs/av1/data.js +141 -0
- package/dist/codecs/av1/decode.js +1769 -0
- package/dist/codecs/av1/filmgrain.js +219 -0
- package/dist/codecs/av1/frame.js +114 -0
- package/dist/codecs/av1/ipred.js +491 -0
- package/dist/codecs/av1/itx.js +950 -0
- package/dist/codecs/av1/loopfilter.js +149 -0
- package/dist/codecs/av1/msac.js +161 -0
- package/dist/codecs/av1/obu.js +516 -0
- package/dist/codecs/av1/postfilter.js +28 -0
- package/dist/codecs/av1/restoration.js +193 -0
- package/dist/codecs/av1/tables.js +160 -0
- package/dist/codecs/avif.js +581 -0
- package/dist/codecs/bmp.js +317 -0
- package/dist/codecs/crc.js +5 -0
- package/dist/codecs/gif.js +10 -35
- package/dist/codecs/hdr.d.ts +25 -0
- package/dist/codecs/hdr.js +201 -0
- package/dist/codecs/ico.js +48 -0
- package/dist/codecs/jpeg.js +11 -28
- package/dist/codecs/jxl/bits.js +126 -0
- package/dist/codecs/jxl/blend.js +117 -0
- package/dist/codecs/jxl/color.js +116 -0
- package/dist/codecs/jxl/dct.js +404 -0
- package/dist/codecs/jxl/entropy.js +556 -0
- package/dist/codecs/jxl/features.js +325 -0
- package/dist/codecs/jxl/filters.js +119 -0
- package/dist/codecs/jxl/frame-header.js +195 -0
- package/dist/codecs/jxl/frame.js +319 -0
- package/dist/codecs/jxl/headers.js +249 -0
- package/dist/codecs/jxl/idct.js +484 -0
- package/dist/codecs/jxl/modular.js +688 -0
- package/dist/codecs/jxl/quant.js +219 -0
- package/dist/codecs/jxl/upsample.js +52 -0
- package/dist/codecs/jxl/vardct.js +472 -0
- package/dist/codecs/jxl.js +89 -0
- package/dist/codecs/netpbm.js +239 -0
- package/dist/codecs/png.js +22 -10
- package/dist/codecs/qoi.js +150 -0
- package/dist/codecs/tga.js +3 -1
- package/dist/codecs/tiff.js +7 -2
- package/dist/codecs/vp8-enc-dsp.js +932 -0
- package/dist/codecs/vp8-enc-tables.js +78 -0
- package/dist/codecs/vp8-enc.js +1930 -0
- package/dist/codecs/vp8.js +3 -1
- package/dist/codecs/vp8l.js +126 -58
- package/dist/codecs/webp.js +47 -11
- package/dist/codecs/zlib.js +81 -69
- package/dist/frame.d.ts +1 -0
- package/dist/frame.js +4 -2
- package/dist/gif.d.ts +1 -0
- package/dist/gif.js +2 -2
- package/dist/image-type.d.ts +15 -1
- package/dist/image-type.js +45 -2
- package/dist/image.d.ts +89 -13
- package/dist/image.js +266 -113
- package/dist/index.d.ts +3 -2
- package/dist/index.js +4 -4
- package/dist/render/bidi.js +350 -0
- package/dist/render/buffer.js +362 -0
- package/dist/render/color.js +164 -0
- package/dist/render/colr.js +523 -0
- package/dist/render/font.js +173 -241
- package/dist/render/hinting.js +38 -22
- package/dist/render/layout.js +1553 -0
- package/dist/render/linebreak.js +210 -0
- package/dist/render/segment.js +106 -0
- package/dist/render/shape.js +984 -0
- package/dist/render/shapers/arabic.js +284 -0
- package/dist/render/shapers/hangul.js +142 -0
- package/dist/render/shapers/hebrew.js +49 -0
- package/dist/render/shapers/index.js +52 -0
- package/dist/render/shapers/indic.js +579 -0
- package/dist/render/shapers/khmer.js +104 -0
- package/dist/render/shapers/machine.js +129 -0
- package/dist/render/shapers/myanmar.js +121 -0
- package/dist/render/shapers/syllabic.js +100 -0
- package/dist/render/shapers/thai.js +125 -0
- package/dist/render/shapers/use.js +235 -0
- package/dist/render/svg.js +102 -23
- package/dist/render/text.js +373 -103
- package/dist/render/unicode-data.js +63 -0
- package/dist/render/unicode.js +50 -0
- package/dist/text-layout.d.ts +4 -1
- package/dist/text-layout.js +2 -2
- package/dist/util/alpha.js +43 -0
- package/dist/util/blur.js +20 -33
- package/dist/util/bytes.js +40 -0
- package/dist/util/limits.js +34 -0
- package/dist/util/options.js +7 -0
- package/dist/util/resample.js +126 -7
- package/dist/util/rotate.js +43 -0
- package/dist/version.js +1 -1
- package/licenses/HarfBuzz.txt +42 -0
- package/licenses/Unicode-3.0.txt +39 -0
- package/package.json +25 -2
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import { idct16, idct32 } from "./idct.js";
|
|
2
|
+
const halfSecants = new Map();
|
|
3
|
+
function secHalf(n) {
|
|
4
|
+
let t = halfSecants.get(n);
|
|
5
|
+
if (!t) {
|
|
6
|
+
t = new Float64Array(n / 2);
|
|
7
|
+
for (let k = 0; k < n / 2; k++)
|
|
8
|
+
t[k] = 1 / Math.cos((2 * k + 1) / (2 * n) * Math.PI) / 2;
|
|
9
|
+
halfSecants.set(n, t);
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
}
|
|
13
|
+
const SEC0 = 0.5411961, SEC1 = 1.306563;
|
|
14
|
+
function dct4(io, o, inverse) {
|
|
15
|
+
const i0 = io[o], i1 = io[o + 1], i2 = io[o + 2], i3 = io[o + 3];
|
|
16
|
+
if (!inverse) {
|
|
17
|
+
const sum03 = i0 + i3, sum12 = i1 + i2, t0 = (i0 - i3) * SEC0, t1 = (i1 - i2) * SEC1;
|
|
18
|
+
const out0 = (t0 + t1) / 4, out1 = (t0 - t1) / 4;
|
|
19
|
+
io[o] = (sum03 + sum12) / 4;
|
|
20
|
+
io[o + 1] = out0 * Math.SQRT2 + out1;
|
|
21
|
+
io[o + 2] = (sum03 - sum12) / 4;
|
|
22
|
+
io[o + 3] = out1;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
const t0 = i1 * Math.SQRT2, t1 = i1 + i3, out0 = (t0 + t1) * SEC0, out1 = (t0 - t1) * SEC1;
|
|
26
|
+
const sum02 = i0 + i2, sub02 = i0 - i2;
|
|
27
|
+
io[o] = sum02 + out0;
|
|
28
|
+
io[o + 1] = sub02 + out1;
|
|
29
|
+
io[o + 2] = sub02 - out1;
|
|
30
|
+
io[o + 3] = sum02 - out0;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function dct(io, o, n, scratch, s, inverse) {
|
|
34
|
+
if (n <= 1)
|
|
35
|
+
return;
|
|
36
|
+
if (n === 2) {
|
|
37
|
+
const a = io[o] + io[o + 1], b = io[o] - io[o + 1];
|
|
38
|
+
io[o] = inverse ? a : a / 2;
|
|
39
|
+
io[o + 1] = inverse ? b : b / 2;
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (n === 4) {
|
|
43
|
+
dct4(io, o, inverse);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (inverse && (n === 16 || n === 32)) {
|
|
47
|
+
(n === 16 ? idct16 : idct32)(io, o, 1);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const half = n / 2, sec = secHalf(n);
|
|
51
|
+
if (!inverse) {
|
|
52
|
+
for (let i = 0; i < half; i++) {
|
|
53
|
+
scratch[s + i] = (io[o + i] + io[o + n - i - 1]) / 2;
|
|
54
|
+
scratch[s + half + i] = (io[o + i] - io[o + n - i - 1]) / 2 * sec[i];
|
|
55
|
+
}
|
|
56
|
+
dct(scratch, s, half, io, o, false);
|
|
57
|
+
dct(scratch, s + half, half, io, o + half, false);
|
|
58
|
+
scratch[s + half] *= Math.SQRT2;
|
|
59
|
+
for (let i = 0; i < half - 1; i++)
|
|
60
|
+
scratch[s + half + i] += scratch[s + half + i + 1];
|
|
61
|
+
for (let i = 0; i < half; i++) {
|
|
62
|
+
io[o + 2 * i] = scratch[s + i];
|
|
63
|
+
io[o + 2 * i + 1] = scratch[s + half + i];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
for (let i = 0; i < half; i++) {
|
|
68
|
+
scratch[s + i] = io[o + 2 * i];
|
|
69
|
+
scratch[s + half + i] = io[o + 2 * i + 1];
|
|
70
|
+
}
|
|
71
|
+
for (let i = 1; i < half; i++)
|
|
72
|
+
scratch[s + n - i] += scratch[s + n - i - 1];
|
|
73
|
+
scratch[s + half] *= Math.SQRT2;
|
|
74
|
+
dct(scratch, s, half, io, o, true);
|
|
75
|
+
dct(scratch, s + half, half, io, o + half, true);
|
|
76
|
+
for (let i = 0; i < half; i++) {
|
|
77
|
+
const r = scratch[s + half + i] * sec[i];
|
|
78
|
+
io[o + i] = scratch[s + i] + r;
|
|
79
|
+
io[o + n - i - 1] = scratch[s + i] - r;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const work = { row: new Float64Array(256), scratch: new Float64Array(256), col: new Float64Array(256 * 2) };
|
|
84
|
+
const SEC8 = secHalf(8);
|
|
85
|
+
function idct8(buf, o, step) {
|
|
86
|
+
const v0 = buf[o], v1 = buf[o + step], v2 = buf[o + 2 * step], v3 = buf[o + 3 * step];
|
|
87
|
+
const v4 = buf[o + 4 * step], v5 = buf[o + 5 * step], v6 = buf[o + 6 * step], v7 = buf[o + 7 * step];
|
|
88
|
+
let t0 = v2 * Math.SQRT2, t1 = v2 + v6;
|
|
89
|
+
const e0 = (t0 + t1) * SEC0, e1 = (t0 - t1) * SEC1, s02 = v0 + v4, d02 = v0 - v4;
|
|
90
|
+
const a0 = s02 + e0, a1 = d02 + e1, a2 = d02 - e1, a3 = s02 - e0;
|
|
91
|
+
const o0 = v1 * Math.SQRT2, o1 = v3 + v1, o2 = v5 + v3, o3 = v7 + v5;
|
|
92
|
+
t0 = o1 * Math.SQRT2;
|
|
93
|
+
t1 = o1 + o3;
|
|
94
|
+
const f0 = (t0 + t1) * SEC0, f1 = (t0 - t1) * SEC1, s13 = o0 + o2, d13 = o0 - o2;
|
|
95
|
+
const b0 = (s13 + f0) * SEC8[0], b1 = (d13 + f1) * SEC8[1], b2 = (d13 - f1) * SEC8[2], b3 = (s13 - f0) * SEC8[3];
|
|
96
|
+
buf[o] = a0 + b0;
|
|
97
|
+
buf[o + 7 * step] = a0 - b0;
|
|
98
|
+
buf[o + step] = a1 + b1;
|
|
99
|
+
buf[o + 6 * step] = a1 - b1;
|
|
100
|
+
buf[o + 2 * step] = a2 + b2;
|
|
101
|
+
buf[o + 5 * step] = a2 - b2;
|
|
102
|
+
buf[o + 3 * step] = a3 + b3;
|
|
103
|
+
buf[o + 4 * step] = a3 - b3;
|
|
104
|
+
}
|
|
105
|
+
function dctRow(buf, o, n, inverse) {
|
|
106
|
+
if (inverse && n <= 32 && n >= 8) {
|
|
107
|
+
(n === 8 ? idct8 : n === 16 ? idct16 : idct32)(buf, o, 1);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const row = work.row;
|
|
111
|
+
for (let x = 0; x < n; x++)
|
|
112
|
+
row[x] = buf[o + x];
|
|
113
|
+
dct(row, 0, n, work.scratch, 0, inverse);
|
|
114
|
+
for (let x = 0; x < n; x++)
|
|
115
|
+
buf[o + x] = row[x];
|
|
116
|
+
}
|
|
117
|
+
function dctColumn(buf, o, stride, n, inverse) {
|
|
118
|
+
if (inverse && n <= 32 && n >= 8) {
|
|
119
|
+
(n === 8 ? idct8 : n === 16 ? idct16 : idct32)(buf, o, stride);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const row = work.row;
|
|
123
|
+
for (let y = 0; y < n; y++)
|
|
124
|
+
row[y] = buf[o + y * stride];
|
|
125
|
+
dct(row, 0, n, work.scratch, 0, inverse);
|
|
126
|
+
for (let y = 0; y < n; y++)
|
|
127
|
+
buf[o + y * stride] = row[y];
|
|
128
|
+
}
|
|
129
|
+
export function dct2d(b, inverse) {
|
|
130
|
+
const { buf, offset, stride, w, h } = b;
|
|
131
|
+
if (w * h <= 1)
|
|
132
|
+
return;
|
|
133
|
+
const mul = inverse ? 1 : 0.5;
|
|
134
|
+
const at = (x, y) => offset + y * stride + x;
|
|
135
|
+
if ((w === 2 && h === 1) || (w === 1 && h === 2)) {
|
|
136
|
+
const p = at(0, 0), q = w === 2 ? at(1, 0) : at(0, 1), v0 = buf[p], v1 = buf[q];
|
|
137
|
+
buf[p] = (v0 + v1) * mul;
|
|
138
|
+
buf[q] = (v0 - v1) * mul;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (w === 2 && h === 2) {
|
|
142
|
+
const v00 = buf[at(0, 0)], v01 = buf[at(1, 0)], v10 = buf[at(0, 1)], v11 = buf[at(1, 1)], m = mul * mul;
|
|
143
|
+
buf[at(0, 0)] = (v00 + v01 + v10 + v11) * m;
|
|
144
|
+
buf[at(1, 0)] = (v00 - v01 + v10 - v11) * m;
|
|
145
|
+
buf[at(0, 1)] = (v00 + v01 - v10 - v11) * m;
|
|
146
|
+
buf[at(1, 1)] = (v00 - v01 - v10 + v11) * m;
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (inverse && w === 8 && h === 8) {
|
|
150
|
+
for (let y = 0; y < 8; y++)
|
|
151
|
+
idct8(buf, offset + y * stride, 1);
|
|
152
|
+
for (let x = 0; x < 8; x++)
|
|
153
|
+
idct8(buf, offset + x, stride);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const rowDct = (y, n, x0 = 0) => dctRow(buf, offset + y * stride + x0, n, inverse);
|
|
157
|
+
const colDct = (x, n) => dctColumn(buf, offset + x, stride, n, inverse);
|
|
158
|
+
if (h === 1) {
|
|
159
|
+
rowDct(0, w);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (w === 1) {
|
|
163
|
+
colDct(0, h);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (h === 2) {
|
|
167
|
+
for (let x = 0; x < w; x++) {
|
|
168
|
+
const p = at(x, 0), q = at(x, 1), v0 = buf[p], v1 = buf[q];
|
|
169
|
+
buf[p] = (v0 + v1) * mul;
|
|
170
|
+
buf[q] = (v0 - v1) * mul;
|
|
171
|
+
}
|
|
172
|
+
rowDct(0, w);
|
|
173
|
+
rowDct(1, w);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (w === 2) {
|
|
177
|
+
for (let y = 0; y < h; y++) {
|
|
178
|
+
const p = at(0, y), q = at(1, y), v0 = buf[p], v1 = buf[q];
|
|
179
|
+
buf[p] = (v0 + v1) * mul;
|
|
180
|
+
buf[q] = (v0 - v1) * mul;
|
|
181
|
+
}
|
|
182
|
+
colDct(0, h);
|
|
183
|
+
colDct(1, h);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
for (let y = 0; y < h; y++)
|
|
187
|
+
rowDct(y, w);
|
|
188
|
+
const size = Math.min(w, h);
|
|
189
|
+
const transposeSquares = () => {
|
|
190
|
+
for (let by = 0; by < h; by += size) {
|
|
191
|
+
for (let bx = 0; bx < w; bx += size) {
|
|
192
|
+
for (let dy = 0; dy < size; dy++) {
|
|
193
|
+
for (let dx = dy + 1; dx < size; dx++) {
|
|
194
|
+
const p = at(bx + dx, by + dy), q = at(bx + dy, by + dx), t = buf[p];
|
|
195
|
+
buf[p] = buf[q];
|
|
196
|
+
buf[q] = t;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
transposeSquares();
|
|
203
|
+
if (size === h) {
|
|
204
|
+
for (let y = 0; y < h; y++)
|
|
205
|
+
for (let x0 = 0; x0 < w; x0 += h)
|
|
206
|
+
rowDct(y, h, x0);
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
const col = work.col, scratch = work.scratch;
|
|
210
|
+
for (let y = 0; y < w; y++) {
|
|
211
|
+
for (let k = 0; k < h / size; k++) {
|
|
212
|
+
const p = at(0, y + k * size);
|
|
213
|
+
for (let x = 0; x < w; x++)
|
|
214
|
+
col[k * w + x] = buf[p + x];
|
|
215
|
+
}
|
|
216
|
+
dct(col, 0, h, scratch, 0, inverse);
|
|
217
|
+
for (let k = 0; k < h / size; k++) {
|
|
218
|
+
const p = at(0, y + k * size);
|
|
219
|
+
for (let x = 0; x < w; x++)
|
|
220
|
+
buf[p + x] = col[k * w + x];
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
transposeSquares();
|
|
225
|
+
}
|
|
226
|
+
const SCALE_F = [
|
|
227
|
+
1.0000000000000000, 0.9996047255830407, 0.9984194528776054, 0.9964458326264695, 0.9936866130906366,
|
|
228
|
+
0.9901456355893141, 0.9858278282666936, 0.9807391980963174, 0.9748868211368796, 0.9682788310563117,
|
|
229
|
+
0.9609244059440204, 0.9528337534340876, 0.9440180941651672, 0.9344896436056892, 0.9242615922757944,
|
|
230
|
+
0.9133480844001980, 0.9017641950288744, 0.8895259056651056, 0.8766500784429904, 0.8631544288990163,
|
|
231
|
+
0.8490574973847023, 0.8343786191696513, 0.8191378932865928, 0.8033561501721485, 0.7870549181591013,
|
|
232
|
+
0.7702563888779096, 0.7529833816270532, 0.7352593067735488, 0.7171081282466044, 0.6985543251889097,
|
|
233
|
+
0.6796228528314652, 0.6603391026591464
|
|
234
|
+
];
|
|
235
|
+
export const scaleF = (c, logb) => SCALE_F[c << logb];
|
|
236
|
+
const sub = (b, x, y, w, h) => ({ buf: b.buf, offset: b.offset + y * b.stride + x, stride: b.stride, w, h });
|
|
237
|
+
const get = (b, x, y) => b.buf[b.offset + y * b.stride + x];
|
|
238
|
+
const set = (b, x, y, v) => {
|
|
239
|
+
b.buf[b.offset + y * b.stride + x] = v;
|
|
240
|
+
};
|
|
241
|
+
const tmp = { a: new Float32Array(64), b: new Float32Array(64) };
|
|
242
|
+
const blockOf = (buf, offset, w, h, stride = w) => ({ buf, offset, stride, w, h });
|
|
243
|
+
function idct2InPlace(b, size) {
|
|
244
|
+
const half = size / 2, s = tmp.a;
|
|
245
|
+
for (let y = 0; y < half; y++) {
|
|
246
|
+
for (let x = 0; x < half; x++) {
|
|
247
|
+
const c00 = get(b, x, y), c01 = get(b, x + half, y), c10 = get(b, x, y + half), c11 = get(b, x + half, y + half);
|
|
248
|
+
s[2 * y * 8 + 2 * x] = c00 + c01 + c10 + c11;
|
|
249
|
+
s[2 * y * 8 + 2 * x + 1] = c00 + c01 - c10 - c11;
|
|
250
|
+
s[(2 * y + 1) * 8 + 2 * x] = c00 - c01 + c10 - c11;
|
|
251
|
+
s[(2 * y + 1) * 8 + 2 * x + 1] = c00 - c01 - c10 + c11;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
for (let y = 0; y < size; y++)
|
|
255
|
+
for (let x = 0; x < size; x++)
|
|
256
|
+
set(b, x, y, s[y * 8 + x]);
|
|
257
|
+
}
|
|
258
|
+
function dct2(b) {
|
|
259
|
+
idct2InPlace(b, 2);
|
|
260
|
+
idct2InPlace(b, 4);
|
|
261
|
+
idct2InPlace(b, 8);
|
|
262
|
+
}
|
|
263
|
+
function dct4x4(b) {
|
|
264
|
+
idct2InPlace(b, 2);
|
|
265
|
+
const s = tmp.b;
|
|
266
|
+
for (let y = 0; y < 2; y++) {
|
|
267
|
+
for (let x = 0; x < 2; x++) {
|
|
268
|
+
const o = (y * 2 + x) * 16;
|
|
269
|
+
for (let iy = 0; iy < 4; iy++)
|
|
270
|
+
for (let ix = 0; ix < 4; ix++)
|
|
271
|
+
s[o + ix * 4 + iy] = get(b, x + ix * 2, y + iy * 2);
|
|
272
|
+
dct2d(blockOf(s, o, 4, 4), true);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
for (let y = 0; y < 2; y++)
|
|
276
|
+
for (let x = 0; x < 2; x++)
|
|
277
|
+
for (let iy = 0; iy < 4; iy++)
|
|
278
|
+
for (let ix = 0; ix < 4; ix++)
|
|
279
|
+
set(b, x * 4 + ix, y * 4 + iy, s[(y * 2 + x) * 16 + iy * 4 + ix]);
|
|
280
|
+
}
|
|
281
|
+
function hornuss(b) {
|
|
282
|
+
idct2InPlace(b, 2);
|
|
283
|
+
const s = tmp.b;
|
|
284
|
+
for (let y = 0; y < 2; y++) {
|
|
285
|
+
for (let x = 0; x < 2; x++) {
|
|
286
|
+
const o = (y * 2 + x) * 16;
|
|
287
|
+
for (let iy = 0; iy < 4; iy++)
|
|
288
|
+
for (let ix = 0; ix < 4; ix++)
|
|
289
|
+
s[o + iy * 4 + ix] = get(b, x + ix * 2, y + iy * 2);
|
|
290
|
+
let residual = 0;
|
|
291
|
+
for (let i = 1; i < 16; i++)
|
|
292
|
+
residual += s[o + i];
|
|
293
|
+
const avg = s[o] - residual / 16;
|
|
294
|
+
s[o] = s[o + 5];
|
|
295
|
+
s[o + 5] = 0;
|
|
296
|
+
for (let i = 0; i < 16; i++)
|
|
297
|
+
s[o + i] += avg;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
for (let y = 0; y < 2; y++)
|
|
301
|
+
for (let x = 0; x < 2; x++)
|
|
302
|
+
for (let iy = 0; iy < 4; iy++)
|
|
303
|
+
for (let ix = 0; ix < 4; ix++)
|
|
304
|
+
set(b, x * 4 + ix, y * 4 + iy, s[(y * 2 + x) * 16 + iy * 4 + ix]);
|
|
305
|
+
}
|
|
306
|
+
function dct4x8(b, transposed) {
|
|
307
|
+
const c0 = get(b, 0, 0), c1 = get(b, 0, 1);
|
|
308
|
+
set(b, 0, 0, c0 + c1);
|
|
309
|
+
set(b, 0, 1, c0 - c1);
|
|
310
|
+
const s = tmp.b;
|
|
311
|
+
for (let i = 0; i < 2; i++) {
|
|
312
|
+
for (let iy = 0; iy < 4; iy++)
|
|
313
|
+
for (let ix = 0; ix < 8; ix++)
|
|
314
|
+
s[i * 32 + iy * 8 + ix] = get(b, ix, iy * 2 + i);
|
|
315
|
+
dct2d(blockOf(s, i * 32, 8, 4), true);
|
|
316
|
+
}
|
|
317
|
+
for (let y = 0; y < 8; y++)
|
|
318
|
+
for (let x = 0; x < 8; x++) {
|
|
319
|
+
if (transposed)
|
|
320
|
+
set(b, y, x, s[y * 8 + x]);
|
|
321
|
+
else
|
|
322
|
+
set(b, x, y, s[y * 8 + x]);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
function afv(b, n) {
|
|
326
|
+
const flipX = n % 2, flipY = n >> 1;
|
|
327
|
+
const coeffs = new Float64Array(16), samples = new Float64Array(16);
|
|
328
|
+
coeffs[0] = (get(b, 0, 0) + get(b, 1, 0) + get(b, 0, 1)) * 4;
|
|
329
|
+
for (let i = 1; i < 16; i++)
|
|
330
|
+
coeffs[i] = get(b, 2 * (i % 4), 2 * (i >> 2));
|
|
331
|
+
for (let i = 0; i < 16; i++)
|
|
332
|
+
for (let k = 0; k < 16; k++)
|
|
333
|
+
samples[k] += coeffs[i] * AFV_BASIS[i * 16 + k];
|
|
334
|
+
const s4 = new Float32Array(16), s8 = new Float32Array(32);
|
|
335
|
+
s4[0] = get(b, 0, 0) - get(b, 1, 0) + get(b, 0, 1);
|
|
336
|
+
for (let iy = 0; iy < 4; iy++)
|
|
337
|
+
for (let ix = 0; ix < 4; ix++)
|
|
338
|
+
if (ix | iy)
|
|
339
|
+
s4[ix * 4 + iy] = get(b, 2 * ix + 1, 2 * iy);
|
|
340
|
+
dct2d(blockOf(s4, 0, 4, 4), true);
|
|
341
|
+
s8[0] = get(b, 0, 0) - get(b, 0, 1);
|
|
342
|
+
for (let iy = 0; iy < 4; iy++)
|
|
343
|
+
for (let ix = 0; ix < 8; ix++)
|
|
344
|
+
if (ix | iy)
|
|
345
|
+
s8[iy * 8 + ix] = get(b, ix, 2 * iy + 1);
|
|
346
|
+
dct2d(blockOf(s8, 0, 8, 4), true);
|
|
347
|
+
for (let iy = 0; iy < 4; iy++) {
|
|
348
|
+
const ay = flipY ? 3 - iy : iy;
|
|
349
|
+
for (let ix = 0; ix < 4; ix++)
|
|
350
|
+
set(b, flipX * 4 + ix, flipY * 4 + iy, samples[ay * 4 + (flipX ? 3 - ix : ix)]);
|
|
351
|
+
}
|
|
352
|
+
for (let iy = 0; iy < 4; iy++)
|
|
353
|
+
for (let ix = 0; ix < 4; ix++)
|
|
354
|
+
set(b, (1 - flipX) * 4 + ix, flipY * 4 + iy, s4[iy * 4 + ix]);
|
|
355
|
+
for (let iy = 0; iy < 4; iy++)
|
|
356
|
+
for (let ix = 0; ix < 8; ix++)
|
|
357
|
+
set(b, ix, (1 - flipY) * 4 + iy, s8[iy * 8 + ix]);
|
|
358
|
+
}
|
|
359
|
+
export const DCT8 = 0, HORNUSS = 1, DCT2 = 2, DCT4 = 3, DCT4X8 = 12, DCT8X4 = 13, AFV0 = 14;
|
|
360
|
+
export function inverseTransform(b, type) {
|
|
361
|
+
switch (type) {
|
|
362
|
+
case DCT2:
|
|
363
|
+
dct2(b);
|
|
364
|
+
break;
|
|
365
|
+
case DCT4:
|
|
366
|
+
dct4x4(b);
|
|
367
|
+
break;
|
|
368
|
+
case HORNUSS:
|
|
369
|
+
hornuss(b);
|
|
370
|
+
break;
|
|
371
|
+
case DCT4X8:
|
|
372
|
+
dct4x8(b, false);
|
|
373
|
+
break;
|
|
374
|
+
case DCT8X4:
|
|
375
|
+
dct4x8(b, true);
|
|
376
|
+
break;
|
|
377
|
+
case 14:
|
|
378
|
+
case 15:
|
|
379
|
+
case 16:
|
|
380
|
+
case 17:
|
|
381
|
+
afv(b, type - 14);
|
|
382
|
+
break;
|
|
383
|
+
default: dct2d(b, true);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
export { sub as subBlock };
|
|
387
|
+
const AFV_BASIS = [
|
|
388
|
+
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
|
|
389
|
+
0.876902929799142, 0.2206518106944235, -0.10140050393753763, -0.1014005039375375, 0.2206518106944236, -0.10140050393753777, -0.10140050393753772, -0.10140050393753763, -0.10140050393753758, -0.10140050393753769, -0.1014005039375375, -0.10140050393753768, -0.10140050393753768, -0.10140050393753759, -0.10140050393753763, -0.10140050393753741,
|
|
390
|
+
0.0, 0.0, 0.40670075830260755, 0.44444816619734445, 0.0, 0.0, 0.19574399372042936, 0.2929100136981264, -0.40670075830260716, -0.19574399372042872, 0.0, 0.11379074460448091, -0.44444816619734384, -0.29291001369812636, -0.1137907446044814, 0.0,
|
|
391
|
+
0.0, 0.0, -0.21255748058288748, 0.3085497062849767, 0.0, 0.4706702258572536, -0.1621205195722993, 0.0, -0.21255748058287047, -0.16212051957228327, -0.47067022585725277, -0.1464291867126764, 0.3085497062849487, 0.0, -0.14642918671266536, 0.4251149611657548,
|
|
392
|
+
0.0, -0.7071067811865474, 0.0, 0.0, 0.7071067811865476, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
|
393
|
+
-0.4105377591765233, 0.6235485373547691, -0.06435071657946274, -0.06435071657946266, 0.6235485373547694, -0.06435071657946284, -0.0643507165794628, -0.06435071657946274, -0.06435071657946272, -0.06435071657946279, -0.06435071657946266, -0.06435071657946277, -0.06435071657946277, -0.06435071657946273, -0.06435071657946274, -0.0643507165794626,
|
|
394
|
+
0.0, 0.0, -0.4517556589999482, 0.15854503551840063, 0.0, -0.04038515160822202, 0.0074182263792423875, 0.39351034269210167, -0.45175565899994635, 0.007418226379244351, 0.1107416575309343, 0.08298163094882051, 0.15854503551839705, 0.3935103426921022, 0.0829816309488214, -0.45175565899994796,
|
|
395
|
+
0.0, 0.0, -0.304684750724869, 0.5112616136591823, 0.0, 0.0, -0.290480129728998, -0.06578701549142804, 0.304684750724884, 0.2904801297290076, 0.0, -0.23889773523344604, -0.5112616136592012, 0.06578701549142545, 0.23889773523345467, 0.0,
|
|
396
|
+
0.0, 0.0, 0.3017929516615495, 0.25792362796341184, 0.0, 0.16272340142866204, 0.09520022653475037, 0.0, 0.3017929516615503, 0.09520022653475055, -0.16272340142866173, -0.35312385449816297, 0.25792362796341295, 0.0, -0.3531238544981624, -0.6035859033230976,
|
|
397
|
+
0.0, 0.0, 0.40824829046386274, 0.0, 0.0, 0.0, 0.0, -0.4082482904638628, -0.4082482904638635, 0.0, 0.0, -0.40824829046386296, 0.0, 0.4082482904638634, 0.408248290463863, 0.0,
|
|
398
|
+
0.0, 0.0, 0.1747866975480809, 0.0812611176717539, 0.0, 0.0, -0.3675398009862027, -0.307882213957909, -0.17478669754808135, 0.3675398009862011, 0.0, 0.4826689115059883, -0.08126111767175039, 0.30788221395790305, -0.48266891150598584, 0.0,
|
|
399
|
+
0.0, 0.0, -0.21105601049335784, 0.18567180916109802, 0.0, 0.0, 0.49215859013738733, -0.38525013709251915, 0.21105601049335806, -0.49215859013738905, 0.0, 0.17419412659916217, -0.18567180916109904, 0.3852501370925211, -0.1741941265991621, 0.0,
|
|
400
|
+
0.0, 0.0, -0.14266084808807264, -0.3416446842253372, 0.0, 0.7367497537172237, 0.24627107722075148, -0.08574019035519306, -0.14266084808807344, 0.24627107722075137, 0.14883399227113567, -0.04768680350229251, -0.3416446842253373, -0.08574019035519267, -0.047686803502292804, -0.14266084808807242,
|
|
401
|
+
0.0, 0.0, -0.13813540350758585, 0.3302282550303788, 0.0, 0.08755115000587084, -0.07946706605909573, -0.4613374887461511, -0.13813540350758294, -0.07946706605910261, 0.49724647109535086, 0.12538059448563663, 0.3302282550303805, -0.4613374887461554, 0.12538059448564315, -0.13813540350758452,
|
|
402
|
+
0.0, 0.0, -0.17437602599651067, 0.0702790691196284, 0.0, -0.2921026642334881, 0.3623817333531167, 0.0, -0.1743760259965108, 0.36238173335311646, 0.29210266423348785, -0.4326608024727445, 0.07027906911962818, 0.0, -0.4326608024727457, 0.34875205199302267,
|
|
403
|
+
0.0, 0.0, 0.11354987314994337, -0.07417504595810355, 0.0, 0.19402893032594343, -0.435190496523228, 0.21918684838857466, 0.11354987314994257, -0.4351904965232251, 0.5550443808910661, -0.25468277124066463, -0.07417504595810233, 0.2191868483885728, -0.25468277124066413, 0.1135498731499429
|
|
404
|
+
];
|