imagescript 3.7.0 → 3.8.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 +46 -97
- package/THIRD_PARTY_NOTICES.md +101 -1
- package/dist/animation-encoder.d.ts +62 -0
- package/dist/animation-encoder.js +204 -0
- package/dist/animation.d.ts +32 -2
- package/dist/animation.js +14 -3
- package/dist/codecs/gif.js +37 -24
- package/dist/codecs/png.js +77 -37
- package/dist/codecs/sharpyuv.js +150 -0
- package/dist/codecs/vp8-enc-dsp.js +9 -1
- package/dist/codecs/vp8l.js +1 -1
- package/dist/codecs/webp.js +36 -26
- package/dist/codecs/zlib.js +20 -0
- package/dist/image.d.ts +27 -3
- package/dist/image.js +54 -15
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/render/filter.js +939 -0
- package/dist/render/shape.js +12 -6
- package/dist/render/svg.js +1140 -36
- package/dist/render/text.js +9 -9
- package/dist/util/optimize.js +208 -62
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/animation.js
CHANGED
|
@@ -10,6 +10,7 @@ import { optimize } from "./util/optimize.js";
|
|
|
10
10
|
import { resolveMetadata, pngText, exifData, gifComments } from "./metadata.js";
|
|
11
11
|
import { isOptions, finite } from "./util/options.js";
|
|
12
12
|
import { MAX_PIXELS, decoding } from "./util/limits.js";
|
|
13
|
+
import { AnimationEncoder } from "./animation-encoder.js";
|
|
13
14
|
const toPlays = (loopCount) => loopCount < 0 ? 0 : loopCount + 1;
|
|
14
15
|
const fromPlays = (plays) => plays === 0 ? -1 : plays - 1;
|
|
15
16
|
function canvasFrames(width, height, frames) {
|
|
@@ -124,7 +125,7 @@ export class Animation extends Array {
|
|
|
124
125
|
: this.__apngFrames__().map(({ blend: _, ...f }) => f);
|
|
125
126
|
return png.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), compression, pngText(resolveMetadata(metadata)));
|
|
126
127
|
}
|
|
127
|
-
async encodeWebP({ metadata = {}, optimize = true, quality, effort = 2 } = {}) {
|
|
128
|
+
async encodeWebP({ metadata = {}, optimize = true, quality, effort = 2, sharpYuv = false } = {}) {
|
|
128
129
|
this.__validate__();
|
|
129
130
|
if (!this.length)
|
|
130
131
|
throw new Error('An animation needs at least one frame');
|
|
@@ -135,8 +136,9 @@ export class Animation extends Array {
|
|
|
135
136
|
if (!Number.isInteger(effort) || effort < 0 || effort > 6)
|
|
136
137
|
throw new RangeError(`effort must be an integer 0-6, not ${effort}`);
|
|
137
138
|
}
|
|
138
|
-
const
|
|
139
|
-
|
|
139
|
+
const tolerance = quality === undefined ? undefined : webp.lossyTolerance(quality);
|
|
140
|
+
const frames = optimize ? this.__optimized__({ replace: true, even: true, alpha: 'frame', tolerance }) : this.__webpFrames__();
|
|
141
|
+
return webp.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), exifData(resolveMetadata(metadata)), quality, effort, sharpYuv);
|
|
140
142
|
}
|
|
141
143
|
__webpFrames__() {
|
|
142
144
|
const width = this.width, height = this.height;
|
|
@@ -177,6 +179,15 @@ export class Animation extends Array {
|
|
|
177
179
|
pixels: new Uint8Array(frame.bitmap.buffer, frame.bitmap.byteOffset, frame.bitmap.byteLength)
|
|
178
180
|
}));
|
|
179
181
|
}
|
|
182
|
+
static webpEncoder(width, height, options = {}) {
|
|
183
|
+
return AnimationEncoder.__webp__(width, height, options);
|
|
184
|
+
}
|
|
185
|
+
static gifEncoder(width, height, options = {}) {
|
|
186
|
+
return AnimationEncoder.__gif__(width, height, options);
|
|
187
|
+
}
|
|
188
|
+
static apngEncoder(width, height, options = {}) {
|
|
189
|
+
return AnimationEncoder.__apng__(width, height, options);
|
|
190
|
+
}
|
|
180
191
|
static async decode(input, onlyFirstFrameOrOptions = false) {
|
|
181
192
|
return decoding(() => Animation.__decode__(input, onlyFirstFrameOrOptions));
|
|
182
193
|
}
|
package/dist/codecs/gif.js
CHANGED
|
@@ -6,6 +6,20 @@ const DISPOSAL_FROM_GIF = [0, 1, 3, 2, 0, 0, 0, 0];
|
|
|
6
6
|
const MAX_CODE = 4095;
|
|
7
7
|
const UNEXPECTED_END = new Error('GIF: unexpected end of data');
|
|
8
8
|
export function encode({ width, height, loopCount = -1, frames }, quality = 95, comments = []) {
|
|
9
|
+
const restores = frames.some(frame => frame.dispose === 3);
|
|
10
|
+
const parts = [header(width, height, loopCount, comments)];
|
|
11
|
+
for (const [i, frame] of frames.entries())
|
|
12
|
+
parts.push(encodeFrame(frame, quality, frame.dispose === 3 || (i === 0 && restores)));
|
|
13
|
+
parts.push(new Uint8Array([0x3b]));
|
|
14
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
15
|
+
let offset = 0;
|
|
16
|
+
for (const p of parts) {
|
|
17
|
+
out.set(p, offset);
|
|
18
|
+
offset += p.length;
|
|
19
|
+
}
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
export function header(width, height, loopCount, comments) {
|
|
9
23
|
const out = new GIFWriter();
|
|
10
24
|
out.bytes(0x47, 0x49, 0x46, 0x38, 0x39, 0x61);
|
|
11
25
|
out.u16(width);
|
|
@@ -22,30 +36,29 @@ export function encode({ width, height, loopCount = -1, frames }, quality = 95,
|
|
|
22
36
|
out.bytes(0x21, 0xfe);
|
|
23
37
|
writeSubBlocks(out, comment);
|
|
24
38
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
out.bytes(0x3b);
|
|
39
|
+
return out.finish();
|
|
40
|
+
}
|
|
41
|
+
export function encodeFrame(frame, quality, transparent) {
|
|
42
|
+
const out = new GIFWriter();
|
|
43
|
+
const { palette, indices, transparentIndex } = quantize(frame.pixels, frame.width, { quality, transparent });
|
|
44
|
+
const entries = palette.length / 3;
|
|
45
|
+
const bits = Math.max(1, Math.ceil(Math.log2(entries)));
|
|
46
|
+
out.bytes(0x21, 0xf9, 0x04);
|
|
47
|
+
out.bytes(DISPOSAL_TO_GIF[frame.dispose] << 2 | (transparentIndex === -1 ? 0 : 1));
|
|
48
|
+
out.u16(Math.min(0xffff, Math.max(0, Math.round(frame.delay))));
|
|
49
|
+
out.bytes(transparentIndex === -1 ? 0 : transparentIndex, 0x00);
|
|
50
|
+
out.bytes(0x2c);
|
|
51
|
+
out.u16(frame.x);
|
|
52
|
+
out.u16(frame.y);
|
|
53
|
+
out.u16(frame.width);
|
|
54
|
+
out.u16(frame.height);
|
|
55
|
+
out.bytes(0x80 | bits - 1);
|
|
56
|
+
const table = new Uint8Array(3 << bits);
|
|
57
|
+
table.set(palette);
|
|
58
|
+
out.array(table);
|
|
59
|
+
const minCodeSize = Math.max(2, bits);
|
|
60
|
+
out.bytes(minCodeSize);
|
|
61
|
+
writeSubBlocks(out, lzwEncode(indices, minCodeSize));
|
|
49
62
|
return out.finish();
|
|
50
63
|
}
|
|
51
64
|
function lzwEncode(indices, minCodeSize) {
|
package/dist/codecs/png.js
CHANGED
|
@@ -107,9 +107,19 @@ function chunk(type, body) {
|
|
|
107
107
|
return out;
|
|
108
108
|
}
|
|
109
109
|
export function alphaCost(images) {
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
110
|
+
const scanner = new AlphaCost();
|
|
111
|
+
for (const rgba of images)
|
|
112
|
+
scanner.add(rgba);
|
|
113
|
+
return scanner.cost();
|
|
114
|
+
}
|
|
115
|
+
export class AlphaCost {
|
|
116
|
+
grey = true;
|
|
117
|
+
colors = new Set();
|
|
118
|
+
done = false;
|
|
119
|
+
add(rgba) {
|
|
120
|
+
if (this.done)
|
|
121
|
+
return;
|
|
122
|
+
const colors = this.colors;
|
|
113
123
|
const pixels = new Uint32Array(rgba.buffer, rgba.byteOffset, rgba.length >> 2);
|
|
114
124
|
let previous = -1;
|
|
115
125
|
for (let i = 0; i < pixels.length; i++) {
|
|
@@ -121,22 +131,34 @@ export function alphaCost(images) {
|
|
|
121
131
|
if (rgba[o + 3] === 0)
|
|
122
132
|
continue;
|
|
123
133
|
if (rgba[o] !== rgba[o + 1] || rgba[o] !== rgba[o + 2])
|
|
124
|
-
grey = false;
|
|
134
|
+
this.grey = false;
|
|
125
135
|
if (colors.size <= 256)
|
|
126
136
|
colors.add(value);
|
|
127
|
-
else if (!grey)
|
|
128
|
-
|
|
137
|
+
else if (!this.grey) {
|
|
138
|
+
this.done = true;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
129
141
|
}
|
|
130
142
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
143
|
+
cost() {
|
|
144
|
+
const { colors, grey } = this;
|
|
145
|
+
const palette = (entries) => (entries <= 2 ? 1 : entries <= 4 ? 2 : entries <= 16 ? 4 : 8) / 8;
|
|
146
|
+
const transparent = colors.size < 256 ? palette(colors.size + 1) : grey ? 2 : 4;
|
|
147
|
+
const opaque = colors.size <= 256 ? palette(colors.size) : grey ? 1 : 3;
|
|
148
|
+
return transparent / opaque;
|
|
149
|
+
}
|
|
135
150
|
}
|
|
136
151
|
function pixelFormat(images) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
152
|
+
const scanner = new FormatScanner();
|
|
153
|
+
for (const rgba of images)
|
|
154
|
+
scanner.add(rgba);
|
|
155
|
+
return scanner.format();
|
|
156
|
+
}
|
|
157
|
+
export class FormatScanner {
|
|
158
|
+
opaque = true;
|
|
159
|
+
grey = true;
|
|
160
|
+
colors = new Map();
|
|
161
|
+
add(rgba) {
|
|
140
162
|
const pixels = new Uint32Array(rgba.buffer, rgba.byteOffset, rgba.length >> 2);
|
|
141
163
|
let previous = -1;
|
|
142
164
|
for (let i = 0; i < pixels.length; i++) {
|
|
@@ -146,19 +168,24 @@ function pixelFormat(images) {
|
|
|
146
168
|
previous = value;
|
|
147
169
|
const o = i * 4;
|
|
148
170
|
if (rgba[o + 3] !== 255)
|
|
149
|
-
opaque = false;
|
|
171
|
+
this.opaque = false;
|
|
150
172
|
if (rgba[o] !== rgba[o + 1] || rgba[o] !== rgba[o + 2])
|
|
151
|
-
grey = false;
|
|
152
|
-
if (colors && !colors.has(value)) {
|
|
153
|
-
if (colors.size === 256)
|
|
154
|
-
colors = null;
|
|
173
|
+
this.grey = false;
|
|
174
|
+
if (this.colors && !this.colors.has(value)) {
|
|
175
|
+
if (this.colors.size === 256)
|
|
176
|
+
this.colors = null;
|
|
155
177
|
else
|
|
156
|
-
colors.set(value, colors.size);
|
|
178
|
+
this.colors.set(value, this.colors.size);
|
|
157
179
|
}
|
|
158
|
-
if (!colors && !opaque && !grey)
|
|
159
|
-
|
|
180
|
+
if (!this.colors && !this.opaque && !this.grey)
|
|
181
|
+
return;
|
|
160
182
|
}
|
|
161
183
|
}
|
|
184
|
+
format() {
|
|
185
|
+
return formatOf(this.colors, this.opaque, this.grey);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function formatOf(colors, opaque, grey) {
|
|
162
189
|
if (colors) {
|
|
163
190
|
const entries = [...colors.keys()].map(value => new Uint8Array(new Uint32Array([value]).buffer));
|
|
164
191
|
entries.sort((a, b) => a[3] - b[3]);
|
|
@@ -229,22 +256,36 @@ export async function encodeRGBA(rgba, width, height, level, text) {
|
|
|
229
256
|
export async function encodeAnimation(frames, width, height, plays, level, text) {
|
|
230
257
|
if (!frames.length)
|
|
231
258
|
throw new Error('PNG: an animation needs at least one frame');
|
|
232
|
-
|
|
233
|
-
if (first.x !== 0 || first.y !== 0 || first.width !== width || first.height !== height) {
|
|
234
|
-
const pixels = new Uint8Array(width * height * 4);
|
|
235
|
-
for (let y = 0; y < first.height && first.y + y < height; y++) {
|
|
236
|
-
const row = first.pixels.subarray(y * first.width * 4, (y * first.width + Math.min(first.width, width - first.x)) * 4);
|
|
237
|
-
pixels.set(row, ((first.y + y) * width + first.x) * 4);
|
|
238
|
-
}
|
|
239
|
-
frames = [{ ...first, x: 0, y: 0, width, height, pixels }, ...frames.slice(1)];
|
|
240
|
-
}
|
|
259
|
+
frames = [coverCanvas(frames[0], width, height), ...frames.slice(1)];
|
|
241
260
|
for (const f of frames)
|
|
242
|
-
|
|
243
|
-
throw new RangeError('PNG: APNG frames have to lie within the canvas');
|
|
261
|
+
checkFrame(f, width, height);
|
|
244
262
|
const images = frames.map(f => aligned(f.pixels.subarray(0, f.width * f.height * 4)));
|
|
245
263
|
const format = pixelFormat(images);
|
|
264
|
+
const encoded = [];
|
|
265
|
+
for (let i = 0; i < frames.length; i++)
|
|
266
|
+
encoded.push({ ...frames[i], data: await frameData(images[i], frames[i].width, frames[i].height, format, level) });
|
|
267
|
+
return animationFile(encoded, width, height, plays, format, text);
|
|
268
|
+
}
|
|
269
|
+
export const RGBA = { channels: 4, depth: 8, convert: rgba => rgba };
|
|
270
|
+
export function coverCanvas(first, width, height) {
|
|
271
|
+
if (first.x === 0 && first.y === 0 && first.width === width && first.height === height)
|
|
272
|
+
return first;
|
|
273
|
+
const pixels = new Uint8Array(width * height * 4);
|
|
274
|
+
for (let y = 0; y < first.height && first.y + y < height; y++) {
|
|
275
|
+
const row = first.pixels.subarray(y * first.width * 4, (y * first.width + Math.min(first.width, width - first.x)) * 4);
|
|
276
|
+
pixels.set(row, ((first.y + y) * width + first.x) * 4);
|
|
277
|
+
}
|
|
278
|
+
return { ...first, x: 0, y: 0, width, height, pixels };
|
|
279
|
+
}
|
|
280
|
+
export function checkFrame(f, width, height) {
|
|
281
|
+
if (f.x < 0 || f.y < 0 || f.x + f.width > width || f.y + f.height > height || !f.width || !f.height)
|
|
282
|
+
throw new RangeError('PNG: APNG frames have to lie within the canvas');
|
|
283
|
+
}
|
|
284
|
+
export function frameData(rgba, width, height, format, level) {
|
|
285
|
+
return compressRows(format.convert(rgba, width, height), width, height, format.channels, format.depth, level, level > 0 && !format.palette);
|
|
286
|
+
}
|
|
287
|
+
export function animationFile(frames, width, height, plays, format, text) {
|
|
246
288
|
const { channels, depth, palette, transparency } = format;
|
|
247
|
-
const filterRows = level > 0 && !palette;
|
|
248
289
|
const header = new Uint8Array(13);
|
|
249
290
|
const hv = new DataView(header.buffer);
|
|
250
291
|
hv.setUint32(0, width);
|
|
@@ -282,13 +323,12 @@ export async function encodeAnimation(frames, width, height, plays, level, text)
|
|
|
282
323
|
fctl[24] = f.dispose;
|
|
283
324
|
fctl[25] = f.blend === false ? 0 : 1;
|
|
284
325
|
chunks.push(chunk(fcTL, fctl));
|
|
285
|
-
const data = await compressRows(format.convert(images[i], f.width, f.height), f.width, f.height, channels, depth, level, filterRows);
|
|
286
326
|
if (i === 0)
|
|
287
|
-
chunks.push(chunk(IDAT, data));
|
|
327
|
+
chunks.push(chunk(IDAT, f.data));
|
|
288
328
|
else {
|
|
289
|
-
const body = new Uint8Array(data.length + 4);
|
|
329
|
+
const body = new Uint8Array(f.data.length + 4);
|
|
290
330
|
new DataView(body.buffer).setUint32(0, sequence++);
|
|
291
|
-
body.set(data, 4);
|
|
331
|
+
body.set(f.data, 4);
|
|
292
332
|
chunks.push(chunk(fdAT, body));
|
|
293
333
|
}
|
|
294
334
|
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* libwebp's sharp RGB to YUV 4:2:0 conversion (sharpyuv/sharpyuv.c, sharpyuv_dsp.c, sharpyuv_gamma.c), ported for 8
|
|
3
|
+
* bit RGB to 8 bit YUV with the WebP matrix: luma and chroma are chosen iteratively so that the picture upsampled
|
|
4
|
+
* again matches the original better, sharper at color edges than averaging the chroma. Copyright (c) 2022 Google
|
|
5
|
+
* Inc., BSD-3-Clause License (see THIRD_PARTY_NOTICES.md)
|
|
6
|
+
*/
|
|
7
|
+
const ITERATIONS = 4;
|
|
8
|
+
const SHIFT = 2, BITS = 10, MAX = (1 << BITS) - 1;
|
|
9
|
+
const TO_LINEAR = new Uint32Array(1024 + 2), FROM_LINEAR = new Uint32Array(512 + 2);
|
|
10
|
+
{
|
|
11
|
+
const a = 0.09929682680944, thresh = 0.018053968510807, gamma = 1 / 0.45;
|
|
12
|
+
for (let v = 0; v <= 1024; v++) {
|
|
13
|
+
const g = v / 1024;
|
|
14
|
+
TO_LINEAR[v] = Math.trunc((g <= thresh * 4.5 ? g / 4.5 : Math.pow((g + a) / (1 + a), gamma)) * 65536 + 0.5);
|
|
15
|
+
}
|
|
16
|
+
TO_LINEAR[1025] = TO_LINEAR[1024];
|
|
17
|
+
for (let v = 0; v <= 512; v++) {
|
|
18
|
+
const g = v / 512;
|
|
19
|
+
FROM_LINEAR[v] = Math.trunc(65536 * (g <= thresh ? 4.5 * g : (1 + a) * Math.pow(g, 1 / gamma) - a) + 0.5);
|
|
20
|
+
}
|
|
21
|
+
FROM_LINEAR[513] = FROM_LINEAR[512];
|
|
22
|
+
}
|
|
23
|
+
const toLinear = (v) => TO_LINEAR[v];
|
|
24
|
+
function fromLinear(value) {
|
|
25
|
+
const pos = value >>> 7, x = value - (pos << 7);
|
|
26
|
+
const v0 = FROM_LINEAR[pos] >>> 6, v1 = FROM_LINEAR[pos + 1] >>> 6;
|
|
27
|
+
return v0 + (((v1 - v0) * x + 64) >>> 7);
|
|
28
|
+
}
|
|
29
|
+
const gray = (r, g, b) => Math.floor((13933 * r + 46871 * g + 4732 * b + 32768) / 65536);
|
|
30
|
+
const clip = (v) => v < 0 ? 0 : v > MAX ? MAX : v;
|
|
31
|
+
function updateW(src, s, dst, d, w) {
|
|
32
|
+
for (let i = 0; i < w; i++)
|
|
33
|
+
dst[d + i] = fromLinear(gray(toLinear(src[s + i]), toLinear(src[s + w + i]), toLinear(src[s + 2 * w + i])));
|
|
34
|
+
}
|
|
35
|
+
const scaleDown = (a, b, c, d) => fromLinear((toLinear(a) + toLinear(b) + toLinear(c) + toLinear(d) + 2) >> 2);
|
|
36
|
+
function updateChroma(src, s1, s2, dst, d, uvWidth) {
|
|
37
|
+
const w = 2 * uvWidth;
|
|
38
|
+
for (let i = 0; i < uvWidth; i++) {
|
|
39
|
+
const o = 2 * i;
|
|
40
|
+
const r = scaleDown(src[s1 + o], src[s1 + o + 1], src[s2 + o], src[s2 + o + 1]);
|
|
41
|
+
const g = scaleDown(src[s1 + w + o], src[s1 + w + o + 1], src[s2 + w + o], src[s2 + w + o + 1]);
|
|
42
|
+
const b = scaleDown(src[s1 + 2 * w + o], src[s1 + 2 * w + o + 1], src[s2 + 2 * w + o], src[s2 + 2 * w + o + 1]);
|
|
43
|
+
const W = gray(r, g, b);
|
|
44
|
+
dst[d + i] = r - W;
|
|
45
|
+
dst[d + uvWidth + i] = g - W;
|
|
46
|
+
dst[d + 2 * uvWidth + i] = b - W;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function storeGray(rgb, s, y, d, w) {
|
|
50
|
+
for (let i = 0; i < w; i++)
|
|
51
|
+
y[d + i] = gray(rgb[s + i], rgb[s + w + i], rgb[s + 2 * w + i]);
|
|
52
|
+
}
|
|
53
|
+
function importRow(rgba, o, width, w, dst, d) {
|
|
54
|
+
for (let i = 0; i < width; i++, o += 4) {
|
|
55
|
+
dst[d + i] = rgba[o] << SHIFT;
|
|
56
|
+
dst[d + w + i] = rgba[o + 1] << SHIFT;
|
|
57
|
+
dst[d + 2 * w + i] = rgba[o + 2] << SHIFT;
|
|
58
|
+
}
|
|
59
|
+
if (width & 1)
|
|
60
|
+
for (let c = 0; c < 3; c++)
|
|
61
|
+
dst[d + c * w + width] = dst[d + c * w + width - 1];
|
|
62
|
+
}
|
|
63
|
+
function interpolateTwoRows(bestY, y, uv, prev, cur, next, w, out, o1, o2) {
|
|
64
|
+
const uvWidth = w >> 1, len = (w - 1) >> 1;
|
|
65
|
+
const filter2 = (a, b, w0) => clip(((a * 3 + b + 2) >> 2) + w0);
|
|
66
|
+
const filterRow = (a, b, by, o) => {
|
|
67
|
+
for (let i = 0; i < len; i++, a++, b++) {
|
|
68
|
+
const v0 = (uv[a] * 9 + uv[a + 1] * 3 + uv[b] * 3 + uv[b + 1] + 8) >> 4;
|
|
69
|
+
const v1 = (uv[a + 1] * 9 + uv[a] * 3 + uv[b + 1] * 3 + uv[b] + 8) >> 4;
|
|
70
|
+
out[o + 2 * i] = clip(bestY[by + 2 * i] + v0);
|
|
71
|
+
out[o + 2 * i + 1] = clip(bestY[by + 2 * i + 1] + v1);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
for (let k = 0; k < 3; k++) {
|
|
75
|
+
out[o1] = filter2(uv[cur], uv[prev], bestY[y]);
|
|
76
|
+
out[o2] = filter2(uv[cur], uv[next], bestY[y + w]);
|
|
77
|
+
filterRow(cur, prev, y + 1, o1 + 1);
|
|
78
|
+
filterRow(cur, next, y + w + 1, o2 + 1);
|
|
79
|
+
if (!(w & 1)) {
|
|
80
|
+
out[o1 + w - 1] = filter2(uv[cur + uvWidth - 1], uv[prev + uvWidth - 1], bestY[y + w - 1]);
|
|
81
|
+
out[o2 + w - 1] = filter2(uv[cur + uvWidth - 1], uv[next + uvWidth - 1], bestY[y + 2 * w - 1]);
|
|
82
|
+
}
|
|
83
|
+
o1 += w;
|
|
84
|
+
o2 += w;
|
|
85
|
+
prev += uvWidth;
|
|
86
|
+
cur += uvWidth;
|
|
87
|
+
next += uvWidth;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const yuv = (r, g, b, c0, c1, c2, offset) => (c0 * r + c1 * g + c2 * b + (offset << 18) + (1 << 17)) >> 18;
|
|
91
|
+
const clip8 = (v) => v < 0 ? 0 : v > 255 ? 255 : v;
|
|
92
|
+
export function sharpYUV(rgba, width, height, y, u, v) {
|
|
93
|
+
const w = (width + 1) & ~1, h = (height + 1) & ~1, uvWidth = w >> 1, uvHeight = h >> 1;
|
|
94
|
+
const tmp = new Uint16Array(w * 6), bestY = new Uint16Array(w * h), targetY = new Uint16Array(w * h), bestRGBY = new Uint16Array(w * 2);
|
|
95
|
+
const bestUV = new Int16Array(uvWidth * 3 * uvHeight), targetUV = new Int16Array(uvWidth * 3 * uvHeight), bestRGBUV = new Int16Array(uvWidth * 3);
|
|
96
|
+
const stride = width * 4;
|
|
97
|
+
for (let j = 0, uvRow = 0; j < height; j += 2, uvRow += 3 * uvWidth) {
|
|
98
|
+
importRow(rgba, j * stride, width, w, tmp, 0);
|
|
99
|
+
if (j < height - 1)
|
|
100
|
+
importRow(rgba, (j + 1) * stride, width, w, tmp, 3 * w);
|
|
101
|
+
else
|
|
102
|
+
tmp.copyWithin(3 * w, 0, 3 * w);
|
|
103
|
+
storeGray(tmp, 0, bestY, j * w, w);
|
|
104
|
+
storeGray(tmp, 3 * w, bestY, (j + 1) * w, w);
|
|
105
|
+
updateW(tmp, 0, targetY, j * w, w);
|
|
106
|
+
updateW(tmp, 3 * w, targetY, (j + 1) * w, w);
|
|
107
|
+
updateChroma(tmp, 0, 3 * w, targetUV, uvRow, uvWidth);
|
|
108
|
+
bestUV.set(targetUV.subarray(uvRow, uvRow + 3 * uvWidth), uvRow);
|
|
109
|
+
}
|
|
110
|
+
const threshold = Math.trunc(3 * w * h);
|
|
111
|
+
let previousSum = Infinity;
|
|
112
|
+
for (let iteration = 0; iteration < ITERATIONS; iteration++) {
|
|
113
|
+
let cur = 0, prev = 0, sum = 0;
|
|
114
|
+
for (let j = 0; j < h; j += 2) {
|
|
115
|
+
const next = cur + (j < h - 2 ? 3 * uvWidth : 0);
|
|
116
|
+
interpolateTwoRows(bestY, j * w, bestUV, prev, cur, next, w, tmp, 0, 3 * w);
|
|
117
|
+
prev = cur;
|
|
118
|
+
cur = next;
|
|
119
|
+
updateW(tmp, 0, bestRGBY, 0, w);
|
|
120
|
+
updateW(tmp, 3 * w, bestRGBY, w, w);
|
|
121
|
+
updateChroma(tmp, 0, 3 * w, bestRGBUV, 0, uvWidth);
|
|
122
|
+
for (let i = 0; i < 2 * w; i++) {
|
|
123
|
+
const diff = targetY[j * w + i] - bestRGBY[i];
|
|
124
|
+
bestY[j * w + i] = clip(bestY[j * w + i] + diff);
|
|
125
|
+
sum += Math.abs(diff);
|
|
126
|
+
}
|
|
127
|
+
const uvRow = (j >> 1) * 3 * uvWidth;
|
|
128
|
+
for (let i = 0; i < 3 * uvWidth; i++)
|
|
129
|
+
bestUV[uvRow + i] += targetUV[uvRow + i] - bestRGBUV[i];
|
|
130
|
+
}
|
|
131
|
+
if (iteration > 0 && (sum < threshold || sum > previousSum))
|
|
132
|
+
break;
|
|
133
|
+
previousSum = sum;
|
|
134
|
+
}
|
|
135
|
+
for (let j = 0; j < height; j++) {
|
|
136
|
+
const uvRow = (j >> 1) * 3 * uvWidth;
|
|
137
|
+
for (let i = 0; i < width; i++) {
|
|
138
|
+
const W = bestY[j * w + i], o = uvRow + (i >> 1);
|
|
139
|
+
const r = bestUV[o] + W, g = bestUV[o + uvWidth] + W, b = bestUV[o + 2 * uvWidth] + W;
|
|
140
|
+
y[j * width + i] = clip8(yuv(r, g, b, 16839, 33059, 6420, 16));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
for (let j = 0; j < uvHeight; j++)
|
|
144
|
+
for (let i = 0; i < uvWidth; i++) {
|
|
145
|
+
const o = j * 3 * uvWidth + i;
|
|
146
|
+
const r = bestUV[o], g = bestUV[o + uvWidth], b = bestUV[o + 2 * uvWidth];
|
|
147
|
+
u[j * uvWidth + i] = clip8(yuv(r, g, b, -9719, -19081, 28800, 128));
|
|
148
|
+
v[j * uvWidth + i] = clip8(yuv(r, g, b, 28800, -24116, -4684, 128));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { littleEndian } from "../util/alpha.js";
|
|
2
|
+
import { sharpYUV } from "./sharpyuv.js";
|
|
2
3
|
export const BPS = 32;
|
|
3
4
|
export const Y_OFF = 0, U_OFF = 16, V_OFF = 24;
|
|
4
5
|
export const YUV_SIZE = BPS * 16, PRED_SIZE = 56 * BPS;
|
|
@@ -43,7 +44,7 @@ function weighted(rgba, o, step, next, total) {
|
|
|
43
44
|
const sum = rgba[a] * g[rgba[o]] + rgba[a + step] * g[rgba[o + step]] + rgba[a + next] * g[rgba[o + next]] + rgba[a + next + step] * g[rgba[o + next + step]];
|
|
44
45
|
return linearToGamma((sum * INV_ALPHA[total]) >>> 17, 0);
|
|
45
46
|
}
|
|
46
|
-
export function rgbaToYUV(rgba, width, height) {
|
|
47
|
+
export function rgbaToYUV(rgba, width, height, sharp = false) {
|
|
47
48
|
const uvWidth = (width + 1) >> 1, uvHeight = (height + 1) >> 1;
|
|
48
49
|
const y = new Uint8Array(width * height), u = new Uint8Array(uvWidth * uvHeight), v = new Uint8Array(uvWidth * uvHeight);
|
|
49
50
|
let hasAlpha = false;
|
|
@@ -57,6 +58,13 @@ export function rgbaToYUV(rgba, width, height) {
|
|
|
57
58
|
if (a)
|
|
58
59
|
for (let j = 0; j < height; j++)
|
|
59
60
|
alphaRow(rgba, j * stride, a, j * width, width);
|
|
61
|
+
if (sharp && width >= 4 && height >= 4) {
|
|
62
|
+
sharpYUV(rgba, width, height, y, u, v);
|
|
63
|
+
const picture = { width, height, y, u, v, a, uvStride: uvWidth };
|
|
64
|
+
if (a)
|
|
65
|
+
cleanupTransparentArea(picture);
|
|
66
|
+
return picture;
|
|
67
|
+
}
|
|
60
68
|
const words = littleEndian && rgba.byteOffset % 4 === 0 ? new Uint32Array(rgba.buffer, rgba.byteOffset, width * height) : null;
|
|
61
69
|
for (let j = 0; j < uvHeight; j++) {
|
|
62
70
|
const row = 2 * j * stride;
|
package/dist/codecs/vp8l.js
CHANGED
|
@@ -719,7 +719,7 @@ function writeCode(w, counts) {
|
|
|
719
719
|
else if (t === 18)
|
|
720
720
|
w.write(extras[i], 7);
|
|
721
721
|
}
|
|
722
|
-
return code;
|
|
722
|
+
return used.length === 1 ? { lengths: code.lengths, codes: new Uint16Array(0) } : code;
|
|
723
723
|
}
|
|
724
724
|
function prefixEncode(value) {
|
|
725
725
|
const d = value - 1;
|
package/dist/codecs/webp.js
CHANGED
|
@@ -268,8 +268,8 @@ export function encode(rgba, width, height, exif) {
|
|
|
268
268
|
set24(vp8x, 7, height - 1);
|
|
269
269
|
return riff([riffChunk('VP8X', vp8x), bitstream, riffChunk('EXIF', exif)]);
|
|
270
270
|
}
|
|
271
|
-
export function encodeLossy(rgba, width, height, quality, exif, method = 4) {
|
|
272
|
-
const { alpha, chunks } = lossyChunks(rgba, width, height, quality, method);
|
|
271
|
+
export function encodeLossy(rgba, width, height, quality, exif, method = 4, sharp = false) {
|
|
272
|
+
const { alpha, chunks } = lossyChunks(rgba, width, height, quality, method, sharp);
|
|
273
273
|
if (!alpha && !exif)
|
|
274
274
|
return riff(chunks);
|
|
275
275
|
const vp8x = new Uint8Array(10);
|
|
@@ -278,10 +278,10 @@ export function encodeLossy(rgba, width, height, quality, exif, method = 4) {
|
|
|
278
278
|
set24(vp8x, 7, height - 1);
|
|
279
279
|
return riff([riffChunk('VP8X', vp8x), ...chunks, ...exif ? [riffChunk('EXIF', exif)] : []]);
|
|
280
280
|
}
|
|
281
|
-
function lossyChunks(rgba, width, height, quality, method) {
|
|
281
|
+
function lossyChunks(rgba, width, height, quality, method, sharp = false) {
|
|
282
282
|
if (width > 16383 || height > 16383)
|
|
283
283
|
throw new RangeError('WebP: lossy images can be at most 16383 pixels wide and high');
|
|
284
|
-
const picture = rgbaToYUV(rgba, width, height);
|
|
284
|
+
const picture = rgbaToYUV(rgba, width, height, sharp);
|
|
285
285
|
const frame = encodeVP8(picture, quality, method);
|
|
286
286
|
const padded = new Uint8Array(frame.length + (frame.length & 1));
|
|
287
287
|
padded.set(frame);
|
|
@@ -290,14 +290,41 @@ function lossyChunks(rgba, width, height, quality, method) {
|
|
|
290
290
|
chunks.unshift(riffChunk('ALPH', encodeAlpha(picture.a, width, height, method)));
|
|
291
291
|
return { alpha: !!picture.a, chunks };
|
|
292
292
|
}
|
|
293
|
-
export function
|
|
293
|
+
export function lossyTolerance(quality) {
|
|
294
|
+
const value = Math.sqrt(quality / 100);
|
|
295
|
+
return Math.max(1, Math.round(31 * (1 - value) + value));
|
|
296
|
+
}
|
|
297
|
+
export function encodeAnimation(frames, width, height, plays, exif, quality, method = 4, sharp = false) {
|
|
294
298
|
if (!frames.length)
|
|
295
299
|
throw new Error('WebP: an animation needs at least one frame');
|
|
296
300
|
if (width > 1 << 24 || height > 1 << 24)
|
|
297
301
|
throw new RangeError('WebP: canvas too large');
|
|
298
|
-
|
|
299
|
-
const
|
|
300
|
-
|
|
302
|
+
let alpha = false;
|
|
303
|
+
for (const [i, f] of frames.entries())
|
|
304
|
+
alpha ||= needsAlpha(f, i === 0, width, height);
|
|
305
|
+
return animationFile(frames.map(f => frameChunk(f, quality, method, sharp)), width, height, plays, alpha, exif);
|
|
306
|
+
}
|
|
307
|
+
export function needsAlpha(f, first, width, height) {
|
|
308
|
+
return (first && (f.x > 0 || f.y > 0 || f.width < width || f.height < height)) || f.dispose || hasAlpha(f.pixels);
|
|
309
|
+
}
|
|
310
|
+
export function frameChunk(f, quality, method = 4, sharp = false) {
|
|
311
|
+
if (f.x & 1 || f.y & 1)
|
|
312
|
+
throw new RangeError('WebP: frame offsets have to be even');
|
|
313
|
+
const bitstream = quality === undefined ? riffChunk('VP8L', vp8l.encode(toARGB(f.pixels, f.width * f.height), f.width, f.height))
|
|
314
|
+
: concat(lossyChunks(f.pixels, f.width, f.height, quality, method, sharp).chunks);
|
|
315
|
+
const header = new Uint8Array(16);
|
|
316
|
+
set24(header, 0, f.x >> 1);
|
|
317
|
+
set24(header, 3, f.y >> 1);
|
|
318
|
+
set24(header, 6, f.width - 1);
|
|
319
|
+
set24(header, 9, f.height - 1);
|
|
320
|
+
set24(header, 12, Math.min(0xffffff, Math.max(0, Math.round(f.duration))));
|
|
321
|
+
header[15] = (f.blend === false ? 2 : 0) | (f.dispose ? 1 : 0);
|
|
322
|
+
const body = new Uint8Array(16 + bitstream.length);
|
|
323
|
+
body.set(header);
|
|
324
|
+
body.set(bitstream, 16);
|
|
325
|
+
return riffChunk('ANMF', body);
|
|
326
|
+
}
|
|
327
|
+
export function animationFile(frames, width, height, plays, alpha, exif) {
|
|
301
328
|
const vp8x = new Uint8Array(10);
|
|
302
329
|
vp8x[0] = 2 | (alpha ? 16 : 0) | (exif ? 8 : 0);
|
|
303
330
|
set24(vp8x, 4, width - 1);
|
|
@@ -305,24 +332,7 @@ export function encodeAnimation(frames, width, height, plays, exif, quality, met
|
|
|
305
332
|
const anim = new Uint8Array(6);
|
|
306
333
|
anim[4] = plays & 255;
|
|
307
334
|
anim[5] = plays >> 8 & 255;
|
|
308
|
-
const chunks = [riffChunk('VP8X', vp8x), riffChunk('ANIM', anim)];
|
|
309
|
-
for (const f of frames) {
|
|
310
|
-
if (f.x & 1 || f.y & 1)
|
|
311
|
-
throw new RangeError('WebP: frame offsets have to be even');
|
|
312
|
-
const bitstream = quality === undefined ? riffChunk('VP8L', vp8l.encode(toARGB(f.pixels, f.width * f.height), f.width, f.height))
|
|
313
|
-
: concat(lossyChunks(f.pixels, f.width, f.height, quality, method).chunks);
|
|
314
|
-
const header = new Uint8Array(16);
|
|
315
|
-
set24(header, 0, f.x >> 1);
|
|
316
|
-
set24(header, 3, f.y >> 1);
|
|
317
|
-
set24(header, 6, f.width - 1);
|
|
318
|
-
set24(header, 9, f.height - 1);
|
|
319
|
-
set24(header, 12, Math.min(0xffffff, Math.max(0, Math.round(f.duration))));
|
|
320
|
-
header[15] = (f.blend === false ? 2 : 0) | (f.dispose ? 1 : 0);
|
|
321
|
-
const body = new Uint8Array(16 + bitstream.length);
|
|
322
|
-
body.set(header);
|
|
323
|
-
body.set(bitstream, 16);
|
|
324
|
-
chunks.push(riffChunk('ANMF', body));
|
|
325
|
-
}
|
|
335
|
+
const chunks = [riffChunk('VP8X', vp8x), riffChunk('ANIM', anim), ...frames];
|
|
326
336
|
if (exif)
|
|
327
337
|
chunks.push(riffChunk('EXIF', exif));
|
|
328
338
|
return riff(chunks);
|
package/dist/codecs/zlib.js
CHANGED
|
@@ -553,6 +553,26 @@ export function decompress(data, limit) {
|
|
|
553
553
|
throw new Error('zlib: preset dictionaries are not supported');
|
|
554
554
|
return inflate(data.subarray(2), new u8(limit));
|
|
555
555
|
}
|
|
556
|
+
export function gunzip(data, limit) {
|
|
557
|
+
if (data.length < 18 || data[0] !== 0x1f || data[1] !== 0x8b || data[2] !== 8)
|
|
558
|
+
throw new Error('gzip: invalid header');
|
|
559
|
+
const flags = data[3];
|
|
560
|
+
let o = 10;
|
|
561
|
+
if (flags & 4)
|
|
562
|
+
o += 2 + (data[o] | data[o + 1] << 8);
|
|
563
|
+
if (flags & 8)
|
|
564
|
+
while (o < data.length && data[o++])
|
|
565
|
+
;
|
|
566
|
+
if (flags & 16)
|
|
567
|
+
while (o < data.length && data[o++])
|
|
568
|
+
;
|
|
569
|
+
if (flags & 2)
|
|
570
|
+
o += 2;
|
|
571
|
+
const end = data.length - 4, size = (data[end] | data[end + 1] << 8 | data[end + 2] << 16 | data[end + 3] << 24) >>> 0;
|
|
572
|
+
if (o > end - 4 || size > limit)
|
|
573
|
+
throw new Error('gzip: invalid or too large');
|
|
574
|
+
return inflate(data.subarray(o, end - 4), new u8(size));
|
|
575
|
+
}
|
|
556
576
|
const { process: runtime, CompressionStream: NativeCompression, DecompressionStream: NativeDecompression, Blob: NativeBlob } = globalThis;
|
|
557
577
|
export const nodeZlib = (() => {
|
|
558
578
|
try {
|