imagescript 3.7.0 → 3.8.1
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 +58 -64
- package/dist/codecs/jpeg.js +54 -32
- package/dist/codecs/png.js +135 -43
- package/dist/codecs/sharpyuv.js +150 -0
- package/dist/codecs/vp8-enc-dsp.js +9 -1
- package/dist/codecs/vp8.js +21 -17
- 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) {
|
|
@@ -258,17 +271,11 @@ function lzwDecode(data, minCodeSize, pixelCount) {
|
|
|
258
271
|
return output;
|
|
259
272
|
const clear = 1 << minCodeSize;
|
|
260
273
|
const eoi = clear + 1;
|
|
261
|
-
const
|
|
262
|
-
const
|
|
263
|
-
const first = new Uint8Array(4096);
|
|
264
|
-
const lengths = new Uint16Array(4096);
|
|
265
|
-
for (let i = 0; i < clear; i++) {
|
|
266
|
-
suffix[i] = first[i] = i;
|
|
267
|
-
lengths[i] = 1;
|
|
268
|
-
}
|
|
274
|
+
const positions = new Int32Array(4096);
|
|
275
|
+
const lengths = new Uint16Array(4096).fill(1, 0, clear);
|
|
269
276
|
let codeSize = minCodeSize + 1;
|
|
270
277
|
let next = eoi + 1;
|
|
271
|
-
let previous = -1;
|
|
278
|
+
let previous = -1, previousOut = 0;
|
|
272
279
|
let buffer = 0, bufferBits = 0, position = 0, out = 0;
|
|
273
280
|
while (out < pixelCount) {
|
|
274
281
|
while (bufferBits < codeSize) {
|
|
@@ -288,49 +295,36 @@ function lzwDecode(data, minCodeSize, pixelCount) {
|
|
|
288
295
|
}
|
|
289
296
|
if (code === eoi)
|
|
290
297
|
break;
|
|
291
|
-
|
|
292
|
-
if (code < next)
|
|
293
|
-
entry = code;
|
|
294
|
-
else if (code === next && previous !== -1)
|
|
295
|
-
entry = previous;
|
|
296
|
-
else
|
|
298
|
+
if (code > next || (code === next && previous === -1))
|
|
297
299
|
break;
|
|
298
300
|
if (previous === -1 && code >= clear)
|
|
299
301
|
break;
|
|
300
|
-
const
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
if (end === out + length) {
|
|
304
|
-
for (let i = end - 1; i >= out; i--) {
|
|
305
|
-
output[i] = suffix[c];
|
|
306
|
-
c = prefix[c];
|
|
307
|
-
}
|
|
308
|
-
}
|
|
302
|
+
const start = out;
|
|
303
|
+
if (code < clear)
|
|
304
|
+
output[out++] = code;
|
|
309
305
|
else {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
306
|
+
const kwk = code === next;
|
|
307
|
+
const from = kwk ? previousOut : positions[code];
|
|
308
|
+
const length = kwk ? lengths[previous] + 1 : lengths[code];
|
|
309
|
+
const count = Math.min(length, pixelCount - out) - (kwk ? 1 : 0);
|
|
310
|
+
if (count < 16)
|
|
311
|
+
for (let i = 0; i < count; i++)
|
|
312
|
+
output[out + i] = output[from + i];
|
|
313
|
+
else
|
|
314
|
+
output.copyWithin(out, from, from + count);
|
|
315
|
+
if (kwk && out + count < pixelCount)
|
|
316
|
+
output[out + count] = output[from];
|
|
317
|
+
out = Math.min(out + length, pixelCount);
|
|
322
318
|
}
|
|
323
|
-
out += written;
|
|
324
319
|
if (previous !== -1 && next <= MAX_CODE) {
|
|
325
|
-
|
|
326
|
-
suffix[next] = first[code === next ? previous : code];
|
|
327
|
-
first[next] = first[previous];
|
|
320
|
+
positions[next] = previousOut;
|
|
328
321
|
lengths[next] = lengths[previous] + 1;
|
|
329
322
|
next++;
|
|
330
323
|
if (next === 1 << codeSize && codeSize < 12)
|
|
331
324
|
codeSize++;
|
|
332
325
|
}
|
|
333
326
|
previous = code;
|
|
327
|
+
previousOut = start;
|
|
334
328
|
}
|
|
335
329
|
return output;
|
|
336
330
|
}
|
package/dist/codecs/jpeg.js
CHANGED
|
@@ -8,6 +8,7 @@ import { ByteWriter } from "../util/bytes.js";
|
|
|
8
8
|
import { MAX_PIXELS, checkPixels } from "../util/limits.js";
|
|
9
9
|
import { orientation as exifOrientation } from "./exif.js";
|
|
10
10
|
import { concat } from "../util/mem.js";
|
|
11
|
+
import { littleEndian } from "../util/alpha.js";
|
|
11
12
|
const ZIGZAG = Int32Array.of(0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63);
|
|
12
13
|
function clamp8(v) {
|
|
13
14
|
return v < 0 ? 0 : v > 255 ? 255 : v;
|
|
@@ -610,6 +611,9 @@ for (let i = 0; i < 256; i++) {
|
|
|
610
611
|
crG[i] = -46802 * x;
|
|
611
612
|
cbG[i] = -22554 * x + 32768;
|
|
612
613
|
}
|
|
614
|
+
const CLAMP_OFFSET = 256, CLAMP = new Uint8Array(768);
|
|
615
|
+
for (let i = 0; i < 768; i++)
|
|
616
|
+
CLAMP[i] = clamp8(i - CLAMP_OFFSET);
|
|
613
617
|
function output(frame, adobe, jfif, override) {
|
|
614
618
|
const { width, height, components } = frame;
|
|
615
619
|
const planes = components.map(component => upsample(component, frame));
|
|
@@ -633,27 +637,45 @@ function output(frame, adobe, jfif, override) {
|
|
|
633
637
|
else
|
|
634
638
|
throw new Error(`JPEG: ${count} color components are not supported`);
|
|
635
639
|
const [p0, p1, p2, p3] = planes;
|
|
640
|
+
if (transform === 'grey' || transform === 'rgb' || transform === 'ycc') {
|
|
641
|
+
const s0 = p0.samples, s1 = (p1 ?? p0).samples, s2 = (p2 ?? p0).samples;
|
|
642
|
+
const grey = transform === 'grey', ycc = transform === 'ycc';
|
|
643
|
+
const words = littleEndian ? new Uint32Array(pixels.buffer) : null;
|
|
644
|
+
for (let y = 0, o = 0; y < height; y++, o = y * width * 4) {
|
|
645
|
+
const r0 = y * p0.stride, r1 = p1 ? y * p1.stride : r0, r2 = p2 ? y * p2.stride : r0;
|
|
646
|
+
if (grey)
|
|
647
|
+
for (let x = 0; x < width; x++, o += 4) {
|
|
648
|
+
pixels[o] = pixels[o + 1] = pixels[o + 2] = s0[r0 + x];
|
|
649
|
+
pixels[o + 3] = 0xff;
|
|
650
|
+
}
|
|
651
|
+
else if (ycc && words)
|
|
652
|
+
for (let x = 0, w = o >> 2; x < width; x++, w++) {
|
|
653
|
+
const a = s0[r0 + x] + CLAMP_OFFSET, b = s1[r1 + x], c = s2[r2 + x];
|
|
654
|
+
words[w] = (CLAMP[a + crR[c]] | CLAMP[a + (cbG[b] + crG[c] >> 16)] << 8 | CLAMP[a + cbB[b]] << 16 | 0xff000000) >>> 0;
|
|
655
|
+
}
|
|
656
|
+
else if (ycc)
|
|
657
|
+
for (let x = 0; x < width; x++, o += 4) {
|
|
658
|
+
const a = s0[r0 + x] + CLAMP_OFFSET, b = s1[r1 + x], c = s2[r2 + x];
|
|
659
|
+
pixels[o] = CLAMP[a + crR[c]];
|
|
660
|
+
pixels[o + 1] = CLAMP[a + (cbG[b] + crG[c] >> 16)];
|
|
661
|
+
pixels[o + 2] = CLAMP[a + cbB[b]];
|
|
662
|
+
pixels[o + 3] = 0xff;
|
|
663
|
+
}
|
|
664
|
+
else
|
|
665
|
+
for (let x = 0; x < width; x++, o += 4) {
|
|
666
|
+
pixels[o] = s0[r0 + x];
|
|
667
|
+
pixels[o + 1] = s1[r1 + x];
|
|
668
|
+
pixels[o + 2] = s2[r2 + x];
|
|
669
|
+
pixels[o + 3] = 0xff;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
return pixels;
|
|
673
|
+
}
|
|
636
674
|
for (let y = 0, o = 0; y < height; y++) {
|
|
637
|
-
const r0 = y * p0.stride, r1 =
|
|
675
|
+
const r0 = y * p0.stride, r1 = y * p1.stride, r2 = y * p2.stride, r3 = y * p3.stride;
|
|
638
676
|
for (let x = 0; x < width; x++, o += 4) {
|
|
639
677
|
pixels[o + 3] = 0xff;
|
|
640
|
-
if (transform === 'grey') {
|
|
641
|
-
pixels[o] = pixels[o + 1] = pixels[o + 2] = p0.samples[r0 + x];
|
|
642
|
-
continue;
|
|
643
|
-
}
|
|
644
678
|
const a = p0.samples[r0 + x], b = p1.samples[r1 + x], c = p2.samples[r2 + x];
|
|
645
|
-
if (transform === 'rgb') {
|
|
646
|
-
pixels[o] = a;
|
|
647
|
-
pixels[o + 1] = b;
|
|
648
|
-
pixels[o + 2] = c;
|
|
649
|
-
continue;
|
|
650
|
-
}
|
|
651
|
-
if (transform === 'ycc') {
|
|
652
|
-
pixels[o] = clamp8(a + crR[c]);
|
|
653
|
-
pixels[o + 1] = clamp8(a + (cbG[b] + crG[c] >> 16));
|
|
654
|
-
pixels[o + 2] = clamp8(a + cbB[b]);
|
|
655
|
-
continue;
|
|
656
|
-
}
|
|
657
679
|
let cyan = a, magenta = b, yellow = c, k = p3.samples[r3 + x];
|
|
658
680
|
if (transform === 'ycck') {
|
|
659
681
|
cyan = 255 - clamp8(a + crR[c]);
|
|
@@ -745,7 +767,7 @@ export function encode(pixels, width, height, quality = 90, { exif, comment } =
|
|
|
745
767
|
const blocksPerMcu = hmax * hmax + 2;
|
|
746
768
|
const count = mcusPerLine * mcusPerColumn * blocksPerMcu;
|
|
747
769
|
const coefficients = new Int16Array(count * 64);
|
|
748
|
-
const owners = new Uint8Array(count);
|
|
770
|
+
const owners = new Uint8Array(count), ends = new Uint8Array(count);
|
|
749
771
|
const block = new Float64Array(64);
|
|
750
772
|
let n = 0;
|
|
751
773
|
for (let my = 0; my < mcusPerColumn; my++)
|
|
@@ -763,12 +785,18 @@ export function encode(pixels, width, height, quality = 90, { exif, comment } =
|
|
|
763
785
|
}
|
|
764
786
|
fdct(block);
|
|
765
787
|
const o = n * 64;
|
|
766
|
-
|
|
767
|
-
|
|
788
|
+
let end = 0;
|
|
789
|
+
for (let k = 0; k < 64; k++) {
|
|
790
|
+
const i = ZIGZAG[k], value = (block[i] * d[i] + 16384.5 | 0) - 16384;
|
|
791
|
+
coefficients[o + k] = value;
|
|
792
|
+
if (value)
|
|
793
|
+
end = k;
|
|
794
|
+
}
|
|
768
795
|
owners[n] = c;
|
|
796
|
+
ends[n] = end;
|
|
769
797
|
}
|
|
770
798
|
}
|
|
771
|
-
const blocks = { coefficients, owners, count };
|
|
799
|
+
const blocks = { coefficients, owners, ends, count };
|
|
772
800
|
const frequencies = [new Int32Array(257), new Int32Array(257), new Int32Array(257), new Int32Array(257)];
|
|
773
801
|
encodeBlocks(blocks, (table, symbol) => frequencies[table][symbol]++, () => { });
|
|
774
802
|
const huffman = frequencies.map(buildOptimalTable);
|
|
@@ -809,7 +837,7 @@ export function encode(pixels, width, height, quality = 90, { exif, comment } =
|
|
|
809
837
|
writer.bytes(0xff, 0xd9);
|
|
810
838
|
return writer.finish();
|
|
811
839
|
}
|
|
812
|
-
function encodeBlocks({ coefficients, owners, count }, symbol, bits) {
|
|
840
|
+
function encodeBlocks({ coefficients, owners, ends, count }, symbol, bits) {
|
|
813
841
|
const pred = [0, 0, 0];
|
|
814
842
|
for (let n = 0; n < count; n++) {
|
|
815
843
|
const component = owners[n];
|
|
@@ -823,8 +851,8 @@ function encodeBlocks({ coefficients, owners, count }, symbol, bits) {
|
|
|
823
851
|
if (size)
|
|
824
852
|
bits(diff < 0 ? diff - 1 & (1 << size) - 1 : diff, size);
|
|
825
853
|
let run = 0;
|
|
826
|
-
for (let k = 1; k
|
|
827
|
-
const value = coefficients[o +
|
|
854
|
+
for (let k = 1, end = ends[n]; k <= end; k++) {
|
|
855
|
+
const value = coefficients[o + k];
|
|
828
856
|
if (value === 0) {
|
|
829
857
|
run++;
|
|
830
858
|
continue;
|
|
@@ -838,18 +866,12 @@ function encodeBlocks({ coefficients, owners, count }, symbol, bits) {
|
|
|
838
866
|
bits(value < 0 ? value - 1 & (1 << s) - 1 : value, s);
|
|
839
867
|
run = 0;
|
|
840
868
|
}
|
|
841
|
-
if (
|
|
869
|
+
if (ends[n] < 63)
|
|
842
870
|
symbol(acTable, 0x00);
|
|
843
871
|
}
|
|
844
872
|
}
|
|
845
873
|
function magnitude(value) {
|
|
846
|
-
|
|
847
|
-
let size = 0;
|
|
848
|
-
while (value) {
|
|
849
|
-
size++;
|
|
850
|
-
value >>= 1;
|
|
851
|
-
}
|
|
852
|
-
return size;
|
|
874
|
+
return 32 - Math.clz32(Math.abs(value));
|
|
853
875
|
}
|
|
854
876
|
function buildOptimalTable(frequencies) {
|
|
855
877
|
const freq = Int32Array.from(frequencies);
|
package/dist/codecs/png.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { crc32 } from "./crc.js";
|
|
2
2
|
import { MAX_PIXELS, checkPixels } from "../util/limits.js";
|
|
3
3
|
import { concat } from "../util/mem.js";
|
|
4
|
+
import { littleEndian } from "../util/alpha.js";
|
|
4
5
|
import { encode as utf8, decode as utf8Decode } from "../util/utf8.js";
|
|
5
6
|
import { orientation as exifOrientation, xmpOrientation } from "./exif.js";
|
|
6
7
|
import { compressAsync, decompressAsync } from "./zlib.js";
|
|
@@ -107,9 +108,19 @@ function chunk(type, body) {
|
|
|
107
108
|
return out;
|
|
108
109
|
}
|
|
109
110
|
export function alphaCost(images) {
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
111
|
+
const scanner = new AlphaCost();
|
|
112
|
+
for (const rgba of images)
|
|
113
|
+
scanner.add(rgba);
|
|
114
|
+
return scanner.cost();
|
|
115
|
+
}
|
|
116
|
+
export class AlphaCost {
|
|
117
|
+
grey = true;
|
|
118
|
+
colors = new Set();
|
|
119
|
+
done = false;
|
|
120
|
+
add(rgba) {
|
|
121
|
+
if (this.done)
|
|
122
|
+
return;
|
|
123
|
+
const colors = this.colors;
|
|
113
124
|
const pixels = new Uint32Array(rgba.buffer, rgba.byteOffset, rgba.length >> 2);
|
|
114
125
|
let previous = -1;
|
|
115
126
|
for (let i = 0; i < pixels.length; i++) {
|
|
@@ -121,22 +132,34 @@ export function alphaCost(images) {
|
|
|
121
132
|
if (rgba[o + 3] === 0)
|
|
122
133
|
continue;
|
|
123
134
|
if (rgba[o] !== rgba[o + 1] || rgba[o] !== rgba[o + 2])
|
|
124
|
-
grey = false;
|
|
135
|
+
this.grey = false;
|
|
125
136
|
if (colors.size <= 256)
|
|
126
137
|
colors.add(value);
|
|
127
|
-
else if (!grey)
|
|
128
|
-
|
|
138
|
+
else if (!this.grey) {
|
|
139
|
+
this.done = true;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
129
142
|
}
|
|
130
143
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
144
|
+
cost() {
|
|
145
|
+
const { colors, grey } = this;
|
|
146
|
+
const palette = (entries) => (entries <= 2 ? 1 : entries <= 4 ? 2 : entries <= 16 ? 4 : 8) / 8;
|
|
147
|
+
const transparent = colors.size < 256 ? palette(colors.size + 1) : grey ? 2 : 4;
|
|
148
|
+
const opaque = colors.size <= 256 ? palette(colors.size) : grey ? 1 : 3;
|
|
149
|
+
return transparent / opaque;
|
|
150
|
+
}
|
|
135
151
|
}
|
|
136
152
|
function pixelFormat(images) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
153
|
+
const scanner = new FormatScanner();
|
|
154
|
+
for (const rgba of images)
|
|
155
|
+
scanner.add(rgba);
|
|
156
|
+
return scanner.format();
|
|
157
|
+
}
|
|
158
|
+
export class FormatScanner {
|
|
159
|
+
opaque = true;
|
|
160
|
+
grey = true;
|
|
161
|
+
colors = new Map();
|
|
162
|
+
add(rgba) {
|
|
140
163
|
const pixels = new Uint32Array(rgba.buffer, rgba.byteOffset, rgba.length >> 2);
|
|
141
164
|
let previous = -1;
|
|
142
165
|
for (let i = 0; i < pixels.length; i++) {
|
|
@@ -146,19 +169,24 @@ function pixelFormat(images) {
|
|
|
146
169
|
previous = value;
|
|
147
170
|
const o = i * 4;
|
|
148
171
|
if (rgba[o + 3] !== 255)
|
|
149
|
-
opaque = false;
|
|
172
|
+
this.opaque = false;
|
|
150
173
|
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;
|
|
174
|
+
this.grey = false;
|
|
175
|
+
if (this.colors && !this.colors.has(value)) {
|
|
176
|
+
if (this.colors.size === 256)
|
|
177
|
+
this.colors = null;
|
|
155
178
|
else
|
|
156
|
-
colors.set(value, colors.size);
|
|
179
|
+
this.colors.set(value, this.colors.size);
|
|
157
180
|
}
|
|
158
|
-
if (!colors && !opaque && !grey)
|
|
159
|
-
|
|
181
|
+
if (!this.colors && !this.opaque && !this.grey)
|
|
182
|
+
return;
|
|
160
183
|
}
|
|
161
184
|
}
|
|
185
|
+
format() {
|
|
186
|
+
return formatOf(this.colors, this.opaque, this.grey);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function formatOf(colors, opaque, grey) {
|
|
162
190
|
if (colors) {
|
|
163
191
|
const entries = [...colors.keys()].map(value => new Uint8Array(new Uint32Array([value]).buffer));
|
|
164
192
|
entries.sort((a, b) => a[3] - b[3]);
|
|
@@ -229,22 +257,36 @@ export async function encodeRGBA(rgba, width, height, level, text) {
|
|
|
229
257
|
export async function encodeAnimation(frames, width, height, plays, level, text) {
|
|
230
258
|
if (!frames.length)
|
|
231
259
|
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
|
-
}
|
|
260
|
+
frames = [coverCanvas(frames[0], width, height), ...frames.slice(1)];
|
|
241
261
|
for (const f of frames)
|
|
242
|
-
|
|
243
|
-
throw new RangeError('PNG: APNG frames have to lie within the canvas');
|
|
262
|
+
checkFrame(f, width, height);
|
|
244
263
|
const images = frames.map(f => aligned(f.pixels.subarray(0, f.width * f.height * 4)));
|
|
245
264
|
const format = pixelFormat(images);
|
|
265
|
+
const encoded = [];
|
|
266
|
+
for (let i = 0; i < frames.length; i++)
|
|
267
|
+
encoded.push({ ...frames[i], data: await frameData(images[i], frames[i].width, frames[i].height, format, level) });
|
|
268
|
+
return animationFile(encoded, width, height, plays, format, text);
|
|
269
|
+
}
|
|
270
|
+
export const RGBA = { channels: 4, depth: 8, convert: rgba => rgba };
|
|
271
|
+
export function coverCanvas(first, width, height) {
|
|
272
|
+
if (first.x === 0 && first.y === 0 && first.width === width && first.height === height)
|
|
273
|
+
return first;
|
|
274
|
+
const pixels = new Uint8Array(width * height * 4);
|
|
275
|
+
for (let y = 0; y < first.height && first.y + y < height; y++) {
|
|
276
|
+
const row = first.pixels.subarray(y * first.width * 4, (y * first.width + Math.min(first.width, width - first.x)) * 4);
|
|
277
|
+
pixels.set(row, ((first.y + y) * width + first.x) * 4);
|
|
278
|
+
}
|
|
279
|
+
return { ...first, x: 0, y: 0, width, height, pixels };
|
|
280
|
+
}
|
|
281
|
+
export function checkFrame(f, width, height) {
|
|
282
|
+
if (f.x < 0 || f.y < 0 || f.x + f.width > width || f.y + f.height > height || !f.width || !f.height)
|
|
283
|
+
throw new RangeError('PNG: APNG frames have to lie within the canvas');
|
|
284
|
+
}
|
|
285
|
+
export function frameData(rgba, width, height, format, level) {
|
|
286
|
+
return compressRows(format.convert(rgba, width, height), width, height, format.channels, format.depth, level, level > 0 && !format.palette);
|
|
287
|
+
}
|
|
288
|
+
export function animationFile(frames, width, height, plays, format, text) {
|
|
246
289
|
const { channels, depth, palette, transparency } = format;
|
|
247
|
-
const filterRows = level > 0 && !palette;
|
|
248
290
|
const header = new Uint8Array(13);
|
|
249
291
|
const hv = new DataView(header.buffer);
|
|
250
292
|
hv.setUint32(0, width);
|
|
@@ -282,13 +324,12 @@ export async function encodeAnimation(frames, width, height, plays, level, text)
|
|
|
282
324
|
fctl[24] = f.dispose;
|
|
283
325
|
fctl[25] = f.blend === false ? 0 : 1;
|
|
284
326
|
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
327
|
if (i === 0)
|
|
287
|
-
chunks.push(chunk(IDAT, data));
|
|
328
|
+
chunks.push(chunk(IDAT, f.data));
|
|
288
329
|
else {
|
|
289
|
-
const body = new Uint8Array(data.length + 4);
|
|
330
|
+
const body = new Uint8Array(f.data.length + 4);
|
|
290
331
|
new DataView(body.buffer).setUint32(0, sequence++);
|
|
291
|
-
body.set(data, 4);
|
|
332
|
+
body.set(f.data, 4);
|
|
292
333
|
chunks.push(chunk(fdAT, body));
|
|
293
334
|
}
|
|
294
335
|
}
|
|
@@ -608,12 +649,15 @@ function unfilter(data, offset, height, rowBytes, bpp, inPlace = false) {
|
|
|
608
649
|
else {
|
|
609
650
|
for (let i = row; i < row + first; i++)
|
|
610
651
|
out[i] = data[src + i] + out[i - rowBytes];
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
652
|
+
if (bpp === 3 || bpp === 4)
|
|
653
|
+
paethRow(data, src, out, row, rowBytes, bpp);
|
|
654
|
+
else
|
|
655
|
+
for (let i = row + bpp; i < row + rowBytes; i++) {
|
|
656
|
+
const a = out[i - bpp], b = out[i - rowBytes], c = out[i - rowBytes - bpp];
|
|
657
|
+
const pa = Math.abs(b - c), pb = Math.abs(a - c), pc = Math.abs(a + b - 2 * c);
|
|
658
|
+
const notA = (pb - pa | pc - pa) >> 31, takeC = (pc - pb) >> 31;
|
|
659
|
+
out[i] = data[src + i] + (a ^ (a ^ b ^ (b ^ c) & takeC) & notA);
|
|
660
|
+
}
|
|
617
661
|
}
|
|
618
662
|
}
|
|
619
663
|
else
|
|
@@ -622,6 +666,47 @@ function unfilter(data, offset, height, rowBytes, bpp, inPlace = false) {
|
|
|
622
666
|
}
|
|
623
667
|
return out;
|
|
624
668
|
}
|
|
669
|
+
function paethRow(data, src, out, row, rowBytes, bpp) {
|
|
670
|
+
const up = row - rowBytes, end = row + rowBytes;
|
|
671
|
+
let a0 = out[row], a1 = out[row + 1], a2 = out[row + 2], a3 = bpp === 4 ? out[row + 3] : 0;
|
|
672
|
+
let c0 = out[up], c1 = out[up + 1], c2 = out[up + 2], c3 = bpp === 4 ? out[up + 3] : 0;
|
|
673
|
+
for (let i = row + bpp; i < end; i += bpp) {
|
|
674
|
+
let b = out[i - rowBytes], pa = Math.abs(b - c0), pb = Math.abs(a0 - c0), pc = Math.abs(a0 + b - 2 * c0);
|
|
675
|
+
let notA = (pb - pa | pc - pa) >> 31, takeC = (pc - pb) >> 31;
|
|
676
|
+
a0 = data[src + i] + (a0 ^ (a0 ^ b ^ (b ^ c0) & takeC) & notA) & 255;
|
|
677
|
+
out[i] = a0;
|
|
678
|
+
c0 = b;
|
|
679
|
+
b = out[i + 1 - rowBytes];
|
|
680
|
+
pa = Math.abs(b - c1);
|
|
681
|
+
pb = Math.abs(a1 - c1);
|
|
682
|
+
pc = Math.abs(a1 + b - 2 * c1);
|
|
683
|
+
notA = (pb - pa | pc - pa) >> 31;
|
|
684
|
+
takeC = (pc - pb) >> 31;
|
|
685
|
+
a1 = data[src + i + 1] + (a1 ^ (a1 ^ b ^ (b ^ c1) & takeC) & notA) & 255;
|
|
686
|
+
out[i + 1] = a1;
|
|
687
|
+
c1 = b;
|
|
688
|
+
b = out[i + 2 - rowBytes];
|
|
689
|
+
pa = Math.abs(b - c2);
|
|
690
|
+
pb = Math.abs(a2 - c2);
|
|
691
|
+
pc = Math.abs(a2 + b - 2 * c2);
|
|
692
|
+
notA = (pb - pa | pc - pa) >> 31;
|
|
693
|
+
takeC = (pc - pb) >> 31;
|
|
694
|
+
a2 = data[src + i + 2] + (a2 ^ (a2 ^ b ^ (b ^ c2) & takeC) & notA) & 255;
|
|
695
|
+
out[i + 2] = a2;
|
|
696
|
+
c2 = b;
|
|
697
|
+
if (bpp === 4) {
|
|
698
|
+
b = out[i + 3 - rowBytes];
|
|
699
|
+
pa = Math.abs(b - c3);
|
|
700
|
+
pb = Math.abs(a3 - c3);
|
|
701
|
+
pc = Math.abs(a3 + b - 2 * c3);
|
|
702
|
+
notA = (pb - pa | pc - pa) >> 31;
|
|
703
|
+
takeC = (pc - pb) >> 31;
|
|
704
|
+
a3 = data[src + i + 3] + (a3 ^ (a3 ^ b ^ (b ^ c3) & takeC) & notA) & 255;
|
|
705
|
+
out[i + 3] = a3;
|
|
706
|
+
c3 = b;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
625
710
|
function createConverter(colorType, depth, palette, transparency) {
|
|
626
711
|
const maxValue = (1 << depth) - 1;
|
|
627
712
|
const sample = depth === 8 ? (row, n) => row[n]
|
|
@@ -677,6 +762,13 @@ function createConverter(colorType, depth, palette, transparency) {
|
|
|
677
762
|
if (depth === 8) {
|
|
678
763
|
const [kr, kg, kb] = key || [-1, -1, -1];
|
|
679
764
|
return (pixels, rowBytes, w, h, out, place) => {
|
|
765
|
+
if (!key && place.step === 4 && littleEndian && !(out.byteOffset & 3)) {
|
|
766
|
+
const words = new Uint32Array(out.buffer, out.byteOffset, out.length >> 2);
|
|
767
|
+
for (let y = 0; y < h; y++)
|
|
768
|
+
for (let x = 0, s = y * rowBytes, o = place.row(y) >> 2; x < w; x++, s += 3, o++)
|
|
769
|
+
words[o] = (pixels[s] | pixels[s + 1] << 8 | pixels[s + 2] << 16 | 0xff000000) >>> 0;
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
680
772
|
for (let y = 0; y < h; y++) {
|
|
681
773
|
for (let x = 0, s = y * rowBytes, o = place.row(y), step = place.step; x < w; x++, s += 3, o += step) {
|
|
682
774
|
const r = pixels[s], g = pixels[s + 1], b = pixels[s + 2];
|