@hiroosy/baileys-lite 0.4.2 → 0.4.4
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 +8 -17
- package/package.json +1 -1
- package/src/ffmpeg/LICENSE +502 -0
- package/src/ffmpeg/index.js +64 -0
- package/src/ffmpeg/lib/bits.js +150 -0
- package/src/ffmpeg/lib/bsf.js +242 -0
- package/src/ffmpeg/lib/codec/flac.js +801 -0
- package/src/ffmpeg/lib/codec/index.js +108 -0
- package/src/ffmpeg/lib/codec/mp3.js +768 -0
- package/src/ffmpeg/lib/codec/mpa.js +65 -0
- package/src/ffmpeg/lib/codec/mpa_tables.js +27 -0
- package/src/ffmpeg/lib/codec/opus.js +140 -0
- package/src/ffmpeg/lib/codec/pcm.js +198 -0
- package/src/ffmpeg/lib/compat.js +42 -0
- package/src/ffmpeg/lib/crc.js +60 -0
- package/src/ffmpeg/lib/demux/adts.js +151 -0
- package/src/ffmpeg/lib/demux/flac.js +150 -0
- package/src/ffmpeg/lib/demux/image.js +47 -0
- package/src/ffmpeg/lib/demux/index.js +60 -0
- package/src/ffmpeg/lib/demux/matroska.js +525 -0
- package/src/ffmpeg/lib/demux/merge.js +106 -0
- package/src/ffmpeg/lib/demux/mp3.js +199 -0
- package/src/ffmpeg/lib/demux/mp4.js +622 -0
- package/src/ffmpeg/lib/demux/ogg.js +250 -0
- package/src/ffmpeg/lib/demux/ts.js +354 -0
- package/src/ffmpeg/lib/demux/wav.js +156 -0
- package/src/ffmpeg/lib/errors.js +27 -0
- package/src/ffmpeg/lib/filter/gain.js +69 -0
- package/src/ffmpeg/lib/filter/mix.js +95 -0
- package/src/ffmpeg/lib/filter/resample.js +187 -0
- package/src/ffmpeg/lib/filter/video.js +575 -0
- package/src/ffmpeg/lib/image/bmp.js +123 -0
- package/src/ffmpeg/lib/image/frame.js +216 -0
- package/src/ffmpeg/lib/image/gif.js +215 -0
- package/src/ffmpeg/lib/image/index.js +70 -0
- package/src/ffmpeg/lib/image/jpeg.js +964 -0
- package/src/ffmpeg/lib/image/png.js +256 -0
- package/src/ffmpeg/lib/image/run.js +67 -0
- package/src/ffmpeg/lib/io.js +298 -0
- package/src/ffmpeg/lib/mux/adts.js +75 -0
- package/src/ffmpeg/lib/mux/flac.js +79 -0
- package/src/ffmpeg/lib/mux/mp3.js +85 -0
- package/src/ffmpeg/lib/mux/mp4.js +450 -0
- package/src/ffmpeg/lib/mux/ogg.js +146 -0
- package/src/ffmpeg/lib/mux/wav.js +94 -0
- package/src/ffmpeg/lib/net.js +270 -0
- package/src/ffmpeg/lib/pipeline.js +579 -0
- package/src/ffmpeg/lib/probe.js +45 -0
- package/src/ffmpeg/lib/tags.js +307 -0
- package/src/ffmpeg/lib/util.js +104 -0
- package/src/foundation/image-thumb.js +4 -56
- package/src/socket/messages-recv.js +5 -6
- package/src/socket/socket-core.js +4 -0
- package/src/utils/media.js +8 -85
- package/src/utils/message-compose.js +19 -11
- package/src/utils/wa-protocol-core.js +5 -3
- package/src/voip/index.js +16 -50
- package/src/voip/media/WaAudioEngine.js +8 -216
- package/src/voip/media/WaVideoEngine.js +8 -237
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { badArg } from '../errors.js';
|
|
2
|
+
|
|
3
|
+
function clamp8(v) {
|
|
4
|
+
return v < 0 ? 0 : v > 255 ? 255 : v;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function planeSize(width, height, hsub, vsub) {
|
|
8
|
+
return { width: Math.ceil(width / hsub), height: Math.ceil(height / vsub) };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function createRgba(width, height) {
|
|
12
|
+
if (!(width > 0 && height > 0)) throw badArg('invalid image size');
|
|
13
|
+
return { format: 'rgba', width, height, hsub: 1, vsub: 1, planes: [{ data: new Uint8Array(width * height * 4), width, height }] };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function createGray(width, height) {
|
|
17
|
+
if (!(width > 0 && height > 0)) throw badArg('invalid image size');
|
|
18
|
+
return { format: 'gray', width, height, hsub: 1, vsub: 1, planes: [{ data: new Uint8Array(width * height), width, height }] };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function createYuv(width, height, hsub, vsub) {
|
|
22
|
+
if (!(width > 0 && height > 0)) throw badArg('invalid image size');
|
|
23
|
+
const c = planeSize(width, height, hsub, vsub);
|
|
24
|
+
return {
|
|
25
|
+
format: 'yuv',
|
|
26
|
+
width,
|
|
27
|
+
height,
|
|
28
|
+
hsub,
|
|
29
|
+
vsub,
|
|
30
|
+
planes: [
|
|
31
|
+
{ data: new Uint8Array(width * height), width, height },
|
|
32
|
+
{ data: new Uint8Array(c.width * c.height), width: c.width, height: c.height },
|
|
33
|
+
{ data: new Uint8Array(c.width * c.height), width: c.width, height: c.height },
|
|
34
|
+
],
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function channelsOf(frame) {
|
|
39
|
+
return frame.format === 'rgba' ? 4 : 1;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function upsamplePlane(plane, width, height) {
|
|
43
|
+
if (plane.width === width && plane.height === height) return plane.data;
|
|
44
|
+
const out = new Uint8Array(width * height);
|
|
45
|
+
const sw = plane.width;
|
|
46
|
+
const sh = plane.height;
|
|
47
|
+
const src = plane.data;
|
|
48
|
+
const xs = new Int32Array(width);
|
|
49
|
+
const xw = new Uint8Array(width);
|
|
50
|
+
const xn = new Int32Array(width);
|
|
51
|
+
for (let x = 0; x < width; x++) {
|
|
52
|
+
const f = ((x + 0.5) * sw) / width - 0.5;
|
|
53
|
+
let x0 = Math.floor(f);
|
|
54
|
+
const w = f - x0;
|
|
55
|
+
let x1 = x0 + 1;
|
|
56
|
+
if (x0 < 0) x0 = 0;
|
|
57
|
+
if (x1 < 0) x1 = 0;
|
|
58
|
+
if (x0 > sw - 1) x0 = sw - 1;
|
|
59
|
+
if (x1 > sw - 1) x1 = sw - 1;
|
|
60
|
+
xs[x] = x0;
|
|
61
|
+
xn[x] = x1;
|
|
62
|
+
xw[x] = Math.round(w * 256 > 255 ? 255 : w * 256);
|
|
63
|
+
}
|
|
64
|
+
for (let y = 0; y < height; y++) {
|
|
65
|
+
const f = ((y + 0.5) * sh) / height - 0.5;
|
|
66
|
+
let y0 = Math.floor(f);
|
|
67
|
+
const wy = Math.round((f - y0) * 256);
|
|
68
|
+
let y1 = y0 + 1;
|
|
69
|
+
if (y0 < 0) y0 = 0;
|
|
70
|
+
if (y1 < 0) y1 = 0;
|
|
71
|
+
if (y0 > sh - 1) y0 = sh - 1;
|
|
72
|
+
if (y1 > sh - 1) y1 = sh - 1;
|
|
73
|
+
const r0 = y0 * sw;
|
|
74
|
+
const r1 = y1 * sw;
|
|
75
|
+
const row = y * width;
|
|
76
|
+
for (let x = 0; x < width; x++) {
|
|
77
|
+
const wx = xw[x];
|
|
78
|
+
const a = src[r0 + xs[x]] * (256 - wx) + src[r0 + xn[x]] * wx;
|
|
79
|
+
const b = src[r1 + xs[x]] * (256 - wx) + src[r1 + xn[x]] * wx;
|
|
80
|
+
out[row + x] = (a * (256 - wy) + b * wy + 32768) >> 16;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function toRgba(frame) {
|
|
87
|
+
if (frame.format === 'rgba') return frame;
|
|
88
|
+
const { width, height } = frame;
|
|
89
|
+
const out = createRgba(width, height);
|
|
90
|
+
const dst = out.planes[0].data;
|
|
91
|
+
if (frame.format === 'gray') {
|
|
92
|
+
const g = frame.planes[0].data;
|
|
93
|
+
for (let i = 0, j = 0; i < g.length; i++, j += 4) {
|
|
94
|
+
dst[j] = dst[j + 1] = dst[j + 2] = g[i];
|
|
95
|
+
dst[j + 3] = 255;
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
const y = frame.planes[0].data;
|
|
100
|
+
const cb = upsamplePlane(frame.planes[1], width, height);
|
|
101
|
+
const cr = upsamplePlane(frame.planes[2], width, height);
|
|
102
|
+
for (let i = 0, j = 0; i < y.length; i++, j += 4) {
|
|
103
|
+
const yy = y[i];
|
|
104
|
+
const u = cb[i] - 128;
|
|
105
|
+
const v = cr[i] - 128;
|
|
106
|
+
dst[j] = clamp8((yy + 1.402 * v + 0.5) | 0);
|
|
107
|
+
dst[j + 1] = clamp8((yy - 0.344136 * u - 0.714136 * v + 0.5) | 0);
|
|
108
|
+
dst[j + 2] = clamp8((yy + 1.772 * u + 0.5) | 0);
|
|
109
|
+
dst[j + 3] = 255;
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function toGray(frame) {
|
|
115
|
+
if (frame.format === 'gray') return frame;
|
|
116
|
+
const out = createGray(frame.width, frame.height);
|
|
117
|
+
if (frame.format === 'yuv') {
|
|
118
|
+
out.planes[0].data.set(frame.planes[0].data);
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
const src = frame.planes[0].data;
|
|
122
|
+
const dst = out.planes[0].data;
|
|
123
|
+
for (let i = 0, j = 0; i < dst.length; i++, j += 4) {
|
|
124
|
+
dst[i] = (src[j] * 77 + src[j + 1] * 150 + src[j + 2] * 29 + 128) >> 8;
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function toYuv(frame, hsub, vsub) {
|
|
130
|
+
if (frame.format === 'yuv' && frame.hsub === hsub && frame.vsub === vsub) return frame;
|
|
131
|
+
const rgba = frame.format === 'yuv' ? toRgba(frame) : frame;
|
|
132
|
+
const { width, height } = rgba;
|
|
133
|
+
const out = createYuv(width, height, hsub, vsub);
|
|
134
|
+
const src = rgba.planes[0].data;
|
|
135
|
+
const yp = out.planes[0].data;
|
|
136
|
+
const cbp = out.planes[1];
|
|
137
|
+
const crp = out.planes[2];
|
|
138
|
+
const cw = cbp.width;
|
|
139
|
+
const ch = cbp.height;
|
|
140
|
+
const sumCb = new Float32Array(cw * ch);
|
|
141
|
+
const sumCr = new Float32Array(cw * ch);
|
|
142
|
+
const count = new Uint16Array(cw * ch);
|
|
143
|
+
const gray = frame.format === 'gray';
|
|
144
|
+
const gsrc = gray ? frame.planes[0].data : null;
|
|
145
|
+
for (let y = 0; y < height; y++) {
|
|
146
|
+
const cy = Math.floor(y / vsub);
|
|
147
|
+
for (let x = 0; x < width; x++) {
|
|
148
|
+
const i = y * width + x;
|
|
149
|
+
let r;
|
|
150
|
+
let g;
|
|
151
|
+
let b;
|
|
152
|
+
if (gray) {
|
|
153
|
+
r = g = b = gsrc[i];
|
|
154
|
+
} else {
|
|
155
|
+
const j = i * 4;
|
|
156
|
+
r = src[j];
|
|
157
|
+
g = src[j + 1];
|
|
158
|
+
b = src[j + 2];
|
|
159
|
+
}
|
|
160
|
+
yp[i] = (0.299 * r + 0.587 * g + 0.114 * b + 0.5) | 0;
|
|
161
|
+
const k = cy * cw + Math.floor(x / hsub);
|
|
162
|
+
sumCb[k] += -0.168736 * r - 0.331264 * g + 0.5 * b + 128;
|
|
163
|
+
sumCr[k] += 0.5 * r - 0.418688 * g - 0.081312 * b + 128;
|
|
164
|
+
count[k]++;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
for (let k = 0; k < count.length; k++) {
|
|
168
|
+
cbp.data[k] = clamp8((sumCb[k] / count[k] + 0.5) | 0);
|
|
169
|
+
crp.data[k] = clamp8((sumCr[k] / count[k] + 0.5) | 0);
|
|
170
|
+
}
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function flatten(frame, background) {
|
|
175
|
+
const rgba = toRgba(frame);
|
|
176
|
+
const src = rgba.planes[0].data;
|
|
177
|
+
const out = createRgba(rgba.width, rgba.height);
|
|
178
|
+
const dst = out.planes[0].data;
|
|
179
|
+
const bg = background || [0, 0, 0];
|
|
180
|
+
for (let i = 0; i < src.length; i += 4) {
|
|
181
|
+
const a = src[i + 3];
|
|
182
|
+
if (a === 255) {
|
|
183
|
+
dst[i] = src[i];
|
|
184
|
+
dst[i + 1] = src[i + 1];
|
|
185
|
+
dst[i + 2] = src[i + 2];
|
|
186
|
+
} else {
|
|
187
|
+
dst[i] = (src[i] * a + bg[0] * (255 - a) + 127) / 255;
|
|
188
|
+
dst[i + 1] = (src[i + 1] * a + bg[1] * (255 - a) + 127) / 255;
|
|
189
|
+
dst[i + 2] = (src[i + 2] * a + bg[2] * (255 - a) + 127) / 255;
|
|
190
|
+
}
|
|
191
|
+
dst[i + 3] = 255;
|
|
192
|
+
}
|
|
193
|
+
return out;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function hasAlpha(frame) {
|
|
197
|
+
if (frame.format !== 'rgba') return false;
|
|
198
|
+
const d = frame.planes[0].data;
|
|
199
|
+
for (let i = 3; i < d.length; i += 4) if (d[i] !== 255) return true;
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export {
|
|
204
|
+
clamp8,
|
|
205
|
+
planeSize,
|
|
206
|
+
createRgba,
|
|
207
|
+
createGray,
|
|
208
|
+
createYuv,
|
|
209
|
+
channelsOf,
|
|
210
|
+
upsamplePlane,
|
|
211
|
+
toRgba,
|
|
212
|
+
toGray,
|
|
213
|
+
toYuv,
|
|
214
|
+
flatten,
|
|
215
|
+
hasAlpha,
|
|
216
|
+
};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { badData, noSupport } from '../errors.js';
|
|
2
|
+
import { createRgba } from './frame.js';
|
|
3
|
+
|
|
4
|
+
function isGif(buf) {
|
|
5
|
+
return buf.length >= 10 && buf.toString('latin1', 0, 3) === 'GIF' && (buf.toString('latin1', 3, 6) === '87a' || buf.toString('latin1', 3, 6) === '89a');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function probeGif(buf) {
|
|
9
|
+
if (!isGif(buf)) return null;
|
|
10
|
+
return { width: buf.readUInt16LE(6), height: buf.readUInt16LE(8) };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function lzwDecode(data, minCode, pixelCount) {
|
|
14
|
+
const out = new Uint8Array(pixelCount);
|
|
15
|
+
const clear = 1 << minCode;
|
|
16
|
+
const eoi = clear + 1;
|
|
17
|
+
const prefix = new Int16Array(4096);
|
|
18
|
+
const suffix = new Uint8Array(4096);
|
|
19
|
+
const stack = new Uint8Array(4097);
|
|
20
|
+
let codeSize = minCode + 1;
|
|
21
|
+
let next = eoi + 1;
|
|
22
|
+
let prev = -1;
|
|
23
|
+
let first = 0;
|
|
24
|
+
let bits = 0;
|
|
25
|
+
let acc = 0;
|
|
26
|
+
let pos = 0;
|
|
27
|
+
let o = 0;
|
|
28
|
+
for (let i = 0; i < clear; i++) suffix[i] = i;
|
|
29
|
+
while (o < pixelCount) {
|
|
30
|
+
while (bits < codeSize) {
|
|
31
|
+
if (pos >= data.length) return out;
|
|
32
|
+
acc |= data[pos++] << bits;
|
|
33
|
+
bits += 8;
|
|
34
|
+
}
|
|
35
|
+
let code = acc & ((1 << codeSize) - 1);
|
|
36
|
+
acc >>>= codeSize;
|
|
37
|
+
bits -= codeSize;
|
|
38
|
+
if (code === clear) {
|
|
39
|
+
codeSize = minCode + 1;
|
|
40
|
+
next = eoi + 1;
|
|
41
|
+
prev = -1;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (code === eoi) break;
|
|
45
|
+
if (prev === -1) {
|
|
46
|
+
out[o++] = suffix[code];
|
|
47
|
+
prev = code;
|
|
48
|
+
first = code;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const incoming = code;
|
|
52
|
+
let sp = 0;
|
|
53
|
+
if (code >= next) {
|
|
54
|
+
stack[sp++] = first;
|
|
55
|
+
code = prev;
|
|
56
|
+
}
|
|
57
|
+
while (code >= clear) {
|
|
58
|
+
stack[sp++] = suffix[code];
|
|
59
|
+
code = prefix[code];
|
|
60
|
+
}
|
|
61
|
+
first = suffix[code];
|
|
62
|
+
stack[sp++] = first;
|
|
63
|
+
if (next < 4096) {
|
|
64
|
+
prefix[next] = prev;
|
|
65
|
+
suffix[next] = first;
|
|
66
|
+
next++;
|
|
67
|
+
if ((next & (next - 1)) === 0 && next < 4096) codeSize++;
|
|
68
|
+
}
|
|
69
|
+
prev = incoming;
|
|
70
|
+
while (sp > 0 && o < pixelCount) out[o++] = stack[--sp];
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function forEachGifFrame(buf, visit) {
|
|
76
|
+
if (!isGif(buf)) throw badData('not a gif');
|
|
77
|
+
const width = buf.readUInt16LE(6);
|
|
78
|
+
const height = buf.readUInt16LE(8);
|
|
79
|
+
if (!width || !height) throw badData('invalid gif size');
|
|
80
|
+
if (width * height > 100000000) throw noSupport('gif is too large');
|
|
81
|
+
const flags = buf[10];
|
|
82
|
+
const bgIndex = buf[11];
|
|
83
|
+
let pos = 13;
|
|
84
|
+
let globalPalette = null;
|
|
85
|
+
if (flags & 0x80) {
|
|
86
|
+
const n = 3 << ((flags & 7) + 1);
|
|
87
|
+
globalPalette = buf.subarray(pos, pos + n);
|
|
88
|
+
pos += n;
|
|
89
|
+
}
|
|
90
|
+
const d = createRgba(width, height).planes[0].data;
|
|
91
|
+
const fillAll = (rgba) => {
|
|
92
|
+
for (let i = 0; i < d.length; i += 4) {
|
|
93
|
+
d[i] = rgba[0];
|
|
94
|
+
d[i + 1] = rgba[1];
|
|
95
|
+
d[i + 2] = rgba[2];
|
|
96
|
+
d[i + 3] = rgba[3];
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const transparentWhite = [255, 255, 255, 0];
|
|
100
|
+
const bgColor = globalPalette && bgIndex * 3 + 2 < globalPalette.length ? [globalPalette[bgIndex * 3], globalPalette[bgIndex * 3 + 1], globalPalette[bgIndex * 3 + 2], 255] : transparentWhite;
|
|
101
|
+
let started = false;
|
|
102
|
+
let gce = null;
|
|
103
|
+
let loop = 1;
|
|
104
|
+
let visited = 0;
|
|
105
|
+
while (pos < buf.length) {
|
|
106
|
+
const b = buf[pos++];
|
|
107
|
+
if (b === 0x3b) break;
|
|
108
|
+
if (b === 0x21) {
|
|
109
|
+
const label = buf[pos++];
|
|
110
|
+
if (label === 0xf9 && buf[pos] >= 4) {
|
|
111
|
+
gce = { disposal: (buf[pos + 1] >> 2) & 7, transparent: buf[pos + 1] & 1 ? buf[pos + 4] : -1, delay: buf.readUInt16LE(pos + 2) * 10 };
|
|
112
|
+
} else if (label === 0xff && buf.toString('latin1', pos + 1, pos + 12) === 'NETSCAPE2.0') {
|
|
113
|
+
loop = buf.readUInt16LE(pos + 14);
|
|
114
|
+
}
|
|
115
|
+
while (pos < buf.length && buf[pos] !== 0) pos += buf[pos] + 1;
|
|
116
|
+
pos++;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (b !== 0x2c) throw badData('invalid gif block');
|
|
120
|
+
const fx = buf.readUInt16LE(pos);
|
|
121
|
+
const fy = buf.readUInt16LE(pos + 2);
|
|
122
|
+
const fw = buf.readUInt16LE(pos + 4);
|
|
123
|
+
const fh = buf.readUInt16LE(pos + 6);
|
|
124
|
+
const fflags = buf[pos + 8];
|
|
125
|
+
pos += 9;
|
|
126
|
+
let palette = globalPalette;
|
|
127
|
+
if (fflags & 0x80) {
|
|
128
|
+
const n = 3 << ((fflags & 7) + 1);
|
|
129
|
+
palette = buf.subarray(pos, pos + n);
|
|
130
|
+
pos += n;
|
|
131
|
+
}
|
|
132
|
+
const minCode = buf[pos++];
|
|
133
|
+
if (minCode < 1 || minCode > 11) throw badData('invalid gif code size');
|
|
134
|
+
const chunks = [];
|
|
135
|
+
while (pos < buf.length && buf[pos] !== 0) {
|
|
136
|
+
chunks.push(buf.subarray(pos + 1, pos + 1 + buf[pos]));
|
|
137
|
+
pos += buf[pos] + 1;
|
|
138
|
+
}
|
|
139
|
+
pos++;
|
|
140
|
+
if (!palette) throw badData('gif has no palette');
|
|
141
|
+
const indices = lzwDecode(Buffer.concat(chunks), minCode, fw * fh);
|
|
142
|
+
const info = gce || { disposal: 0, transparent: -1, delay: 0 };
|
|
143
|
+
gce = null;
|
|
144
|
+
if (!started) {
|
|
145
|
+
started = true;
|
|
146
|
+
fillAll(info.transparent === -1 && globalPalette ? bgColor : transparentWhite);
|
|
147
|
+
}
|
|
148
|
+
const saved = info.disposal === 3 ? Uint8Array.from(d) : null;
|
|
149
|
+
const interlaced = (fflags & 0x40) !== 0;
|
|
150
|
+
const rowMap = new Int32Array(fh);
|
|
151
|
+
if (interlaced) {
|
|
152
|
+
let r = 0;
|
|
153
|
+
for (const [start, step] of [[0, 8], [4, 8], [2, 4], [1, 2]]) for (let y = start; y < fh; y += step) rowMap[r++] = y;
|
|
154
|
+
} else for (let y = 0; y < fh; y++) rowMap[y] = y;
|
|
155
|
+
for (let r = 0; r < fh; r++) {
|
|
156
|
+
const y = fy + rowMap[r];
|
|
157
|
+
if (y >= height) continue;
|
|
158
|
+
for (let x = 0; x < fw; x++) {
|
|
159
|
+
const px = fx + x;
|
|
160
|
+
if (px >= width) continue;
|
|
161
|
+
const idx = indices[r * fw + x];
|
|
162
|
+
if (idx === info.transparent) continue;
|
|
163
|
+
const o = (y * width + px) * 4;
|
|
164
|
+
d[o] = palette[idx * 3];
|
|
165
|
+
d[o + 1] = palette[idx * 3 + 1];
|
|
166
|
+
d[o + 2] = palette[idx * 3 + 2];
|
|
167
|
+
d[o + 3] = 255;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
visited++;
|
|
171
|
+
if (visit(d, info.delay, width, height)) break;
|
|
172
|
+
if (info.disposal === 2) {
|
|
173
|
+
for (let y = fy; y < Math.min(height, fy + fh); y++) {
|
|
174
|
+
for (let x = fx; x < Math.min(width, fx + fw); x++) {
|
|
175
|
+
const o = (y * width + x) * 4;
|
|
176
|
+
const c = info.transparent >= 0 ? transparentWhite : bgColor;
|
|
177
|
+
d[o] = c[0];
|
|
178
|
+
d[o + 1] = c[1];
|
|
179
|
+
d[o + 2] = c[2];
|
|
180
|
+
d[o + 3] = c[3];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
} else if (info.disposal === 3 && saved) d.set(saved);
|
|
184
|
+
}
|
|
185
|
+
if (!visited) throw badData('gif has no frames');
|
|
186
|
+
return { width, height, loop, background: bgIndex };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function decodeGif(buf, options = {}) {
|
|
190
|
+
const frames = [];
|
|
191
|
+
return {
|
|
192
|
+
...forEachGifFrame(buf, (pixels, delay, width, height) => {
|
|
193
|
+
const frame = createRgba(width, height);
|
|
194
|
+
frame.planes[0].data.set(pixels);
|
|
195
|
+
frames.push({ frame, delay });
|
|
196
|
+
return frames.length >= (options.maxFrames || Infinity);
|
|
197
|
+
}),
|
|
198
|
+
frames,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function decodeGifFrame(buf, startSec) {
|
|
203
|
+
let elapsed = 0;
|
|
204
|
+
let picked = null;
|
|
205
|
+
forEachGifFrame(buf, (pixels, delay, width, height) => {
|
|
206
|
+
picked ??= createRgba(width, height);
|
|
207
|
+
picked.planes[0].data.set(pixels);
|
|
208
|
+
if (elapsed / 1000 >= startSec - 1e-6) return true;
|
|
209
|
+
elapsed += delay || 100;
|
|
210
|
+
return false;
|
|
211
|
+
});
|
|
212
|
+
return picked;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export { isGif, probeGif, decodeGif, decodeGifFrame };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { badData, noSupport, badArg } from '../errors.js';
|
|
2
|
+
import { decodeJpeg, encodeJpeg, probeJpeg } from './jpeg.js';
|
|
3
|
+
import { decodePng, encodePng, probePng, isPng } from './png.js';
|
|
4
|
+
import { decodeGif, decodeGifFrame, probeGif, isGif } from './gif.js';
|
|
5
|
+
import { decodeBmp, encodeBmp, probeBmp, isBmp } from './bmp.js';
|
|
6
|
+
|
|
7
|
+
const codecNames = { jpeg: 'mjpeg', png: 'png', gif: 'gif', bmp: 'bmp' };
|
|
8
|
+
|
|
9
|
+
const outputNames = {
|
|
10
|
+
jpg: 'jpeg',
|
|
11
|
+
jpeg: 'jpeg',
|
|
12
|
+
mjpeg: 'jpeg',
|
|
13
|
+
png: 'png',
|
|
14
|
+
bmp: 'bmp',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function detectImage(buf) {
|
|
18
|
+
if (buf.length >= 3 && buf[0] === 0xff && buf[1] === 0xd8 && buf[2] === 0xff) return 'jpeg';
|
|
19
|
+
if (isPng(buf)) return 'png';
|
|
20
|
+
if (isGif(buf)) return 'gif';
|
|
21
|
+
if (isBmp(buf)) return 'bmp';
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function probeImage(buf) {
|
|
26
|
+
const format = detectImage(buf);
|
|
27
|
+
if (!format) return null;
|
|
28
|
+
let info = null;
|
|
29
|
+
if (format === 'jpeg') info = probeJpeg(buf);
|
|
30
|
+
else if (format === 'png') info = probePng(buf);
|
|
31
|
+
else if (format === 'gif') info = probeGif(buf);
|
|
32
|
+
else info = probeBmp(buf);
|
|
33
|
+
if (!info) return null;
|
|
34
|
+
return { format, codec: codecNames[format], width: info.width, height: info.height };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function decodeImage(buf, options = {}) {
|
|
38
|
+
const format = detectImage(buf);
|
|
39
|
+
if (!format) throw badData('unrecognized image format');
|
|
40
|
+
if (format === 'jpeg') return decodeJpeg(buf);
|
|
41
|
+
if (format === 'png') return decodePng(buf);
|
|
42
|
+
if (format === 'bmp') return decodeBmp(buf);
|
|
43
|
+
const gif = decodeGif(buf, { maxFrames: options.maxFrames || 1 });
|
|
44
|
+
return gif.frames[0].frame;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function encodeImage(frame, format, options = {}) {
|
|
48
|
+
if (format === 'jpeg') return encodeJpeg(frame, options);
|
|
49
|
+
if (format === 'png') return encodePng(frame, options);
|
|
50
|
+
if (format === 'bmp') return encodeBmp(frame);
|
|
51
|
+
throw noSupport('no image encoder for ' + format);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function outputFormatOf(name) {
|
|
55
|
+
if (!name) return null;
|
|
56
|
+
return outputNames[String(name).toLowerCase()] || null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function subsamplingOf(pixFmt) {
|
|
60
|
+
if (!pixFmt) return null;
|
|
61
|
+
const p = String(pixFmt).toLowerCase();
|
|
62
|
+
if (p === 'gray' || p === 'gray8' || p === 'y8') return { gray: true };
|
|
63
|
+
if (/^yuvj?420p$/.test(p)) return { hsub: 2, vsub: 2 };
|
|
64
|
+
if (/^yuvj?422p$/.test(p)) return { hsub: 2, vsub: 1 };
|
|
65
|
+
if (/^yuvj?444p$/.test(p)) return { hsub: 1, vsub: 1 };
|
|
66
|
+
if (/^yuvj?440p$/.test(p)) return { hsub: 1, vsub: 2 };
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export { detectImage, probeImage, decodeImage, decodeGifFrame, encodeImage, outputFormatOf, subsamplingOf, codecNames };
|