imagescript 3.20.3 → 4.0.0-rc.2
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/LICENSE +16 -12
- package/README.md +21 -12
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/dist/animation-encoder.d.ts +1 -34
- package/dist/animation-encoder.js +35 -34
- package/dist/animation.d.ts +108 -122
- package/dist/animation.js +285 -121
- package/dist/codecs/av1/decode.js +1 -1
- package/dist/codecs/av1/encode.js +1 -1
- package/dist/codecs/gif.js +11 -9
- package/dist/codecs/hdr.d.ts +1 -1
- package/dist/codecs/jpeg.js +13 -13
- package/dist/codecs/jxl/headers.js +3 -3
- package/dist/codecs/jxl/patches-enc.js +1 -1
- package/dist/codecs/jxl.js +5 -5
- package/dist/codecs/netpbm.js +7 -7
- package/dist/codecs/png.js +15 -15
- package/dist/codecs/quantize.js +34 -8
- package/dist/codecs/tiff.js +1 -1
- package/dist/codecs/webp.js +3 -3
- package/dist/codecs/zlib.js +1 -1
- package/dist/colors.d.ts +8 -7
- package/dist/colors.js +23 -3
- package/dist/frame.d.ts +94 -100
- package/dist/frame.js +89 -65
- package/dist/gainmap.d.ts +88 -46
- package/dist/gainmap.js +75 -51
- package/dist/image-type.d.ts +6 -1
- package/dist/image-type.js +4 -1
- package/dist/image.d.ts +317 -738
- package/dist/image.js +308 -438
- package/dist/index.d.ts +19 -25
- package/dist/index.js +15 -14
- package/dist/metadata.d.ts +4 -3
- package/dist/metadata.js +9 -7
- package/dist/render/colr.js +1 -1
- package/dist/render/font.js +1 -1
- package/dist/render/layout.js +1 -1
- package/dist/render/segment.js +1 -1
- package/dist/render/shapers/hangul.js +1 -1
- package/dist/render/svg.js +68 -20
- package/dist/render/text.js +7 -9
- package/dist/text-layout.d.ts +5 -35
- package/dist/text-layout.js +11 -12
- package/dist/util/gain.js +12 -12
- package/dist/util/mem.d.ts +1 -1
- package/dist/util/optimize.js +3 -1
- package/dist/util/options.js +46 -7
- package/package.json +15 -10
- package/LICENSE.AGPL-3.0 +0 -619
- package/LICENSE.MIT +0 -19
- package/dist/gif.d.ts +0 -36
- package/dist/gif.js +0 -22
- package/dist/util/rotate.js +0 -43
- package/dist/version.js +0 -1
package/dist/codecs/gif.js
CHANGED
|
@@ -5,11 +5,11 @@ const DISPOSAL_TO_GIF = [0, 1, 3, 2];
|
|
|
5
5
|
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
|
-
export function encode({ width, height, loopCount =
|
|
8
|
+
export function encode({ width, height, loopCount = Infinity, frames }, palette, comments = []) {
|
|
9
9
|
const restores = frames.some(frame => frame.dispose === 3);
|
|
10
10
|
const parts = [header(width, height, loopCount, comments)];
|
|
11
11
|
for (const [i, frame] of frames.entries())
|
|
12
|
-
parts.push(encodeFrame(frame,
|
|
12
|
+
parts.push(encodeFrame(frame, palette, frame.dispose === 3 || (i === 0 && restores)));
|
|
13
13
|
parts.push(new Uint8Array([0x3b]));
|
|
14
14
|
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
15
15
|
let offset = 0;
|
|
@@ -29,7 +29,7 @@ export function header(width, height, loopCount, comments) {
|
|
|
29
29
|
out.bytes(0x21, 0xff, 0x0b);
|
|
30
30
|
out.string('NETSCAPE2.0');
|
|
31
31
|
out.bytes(0x03, 0x01);
|
|
32
|
-
out.u16(loopCount ===
|
|
32
|
+
out.u16(loopCount === Infinity ? 0 : loopCount);
|
|
33
33
|
out.bytes(0x00);
|
|
34
34
|
}
|
|
35
35
|
for (const comment of comments) {
|
|
@@ -38,9 +38,9 @@ export function header(width, height, loopCount, comments) {
|
|
|
38
38
|
}
|
|
39
39
|
return out.finish();
|
|
40
40
|
}
|
|
41
|
-
export function encodeFrame(frame,
|
|
41
|
+
export function encodeFrame(frame, colors, transparent) {
|
|
42
42
|
const out = new GIFWriter();
|
|
43
|
-
const { palette, indices, transparentIndex } = quantize(frame.pixels, frame.width, {
|
|
43
|
+
const { palette, indices, transparentIndex } = quantize(frame.pixels, frame.width, { maxColors: colors.colors, dither: colors.dither, ditherStrength: colors.strength, transparent });
|
|
44
44
|
const entries = palette.length / 3;
|
|
45
45
|
const bits = Math.max(1, Math.ceil(Math.log2(entries)));
|
|
46
46
|
out.bytes(0x21, 0xf9, 0x04);
|
|
@@ -125,7 +125,7 @@ function writeSubBlocks(out, data) {
|
|
|
125
125
|
}
|
|
126
126
|
out.byte(0x00);
|
|
127
127
|
}
|
|
128
|
-
export function decode(data,
|
|
128
|
+
export function decode(data, firstFrameOnly = false, maxPixels = MAX_PIXELS) {
|
|
129
129
|
const reader = new ByteReader(data);
|
|
130
130
|
const signature = reader.string(6);
|
|
131
131
|
if (signature !== 'GIF87a' && signature !== 'GIF89a')
|
|
@@ -137,6 +137,7 @@ export function decode(data, onlyFirstFrame = false, maxPixels = MAX_PIXELS) {
|
|
|
137
137
|
const globalTable = flags & 0x80 ? reader.slice(3 << (flags & 7) + 1) : null;
|
|
138
138
|
const frames = [];
|
|
139
139
|
let loopCount = 0;
|
|
140
|
+
let hasLoopExtension = false;
|
|
140
141
|
let control = null;
|
|
141
142
|
const budget = new PixelBudget(maxPixels);
|
|
142
143
|
try {
|
|
@@ -147,7 +148,7 @@ export function decode(data, onlyFirstFrame = false, maxPixels = MAX_PIXELS) {
|
|
|
147
148
|
throw err;
|
|
148
149
|
}
|
|
149
150
|
checkPixels(width, height, maxPixels, Math.max(1, frames.length));
|
|
150
|
-
return { width, height, loopCount, frames };
|
|
151
|
+
return { width, height, loopCount, hasLoopExtension, frames };
|
|
151
152
|
function parse() {
|
|
152
153
|
while (reader.offset < data.length) {
|
|
153
154
|
const block = reader.u8();
|
|
@@ -170,11 +171,12 @@ export function decode(data, onlyFirstFrame = false, maxPixels = MAX_PIXELS) {
|
|
|
170
171
|
const size = reader.u8();
|
|
171
172
|
const id = reader.string(size);
|
|
172
173
|
if (id === 'NETSCAPE2.0' || id === 'ANIMEXTS1.0') {
|
|
174
|
+
hasLoopExtension = true;
|
|
173
175
|
for (let length; (length = reader.u8());) {
|
|
174
176
|
const sub = reader.slice(length);
|
|
175
177
|
if (sub[0] === 1 && length >= 3) {
|
|
176
178
|
const count = sub[1] | sub[2] << 8;
|
|
177
|
-
loopCount = count === 0 ?
|
|
179
|
+
loopCount = count === 0 ? Infinity : count;
|
|
178
180
|
}
|
|
179
181
|
}
|
|
180
182
|
}
|
|
@@ -220,7 +222,7 @@ export function decode(data, onlyFirstFrame = false, maxPixels = MAX_PIXELS) {
|
|
|
220
222
|
ordered.set(indices.subarray(row * fw, (row + 1) * fw), rows[row] * fw);
|
|
221
223
|
}
|
|
222
224
|
frames.push({ x, y, width: fw, height: fh, delay, dispose, indices: ordered, colors, transparentIndex });
|
|
223
|
-
if (
|
|
225
|
+
if (firstFrameOnly)
|
|
224
226
|
break;
|
|
225
227
|
}
|
|
226
228
|
}
|
package/dist/codecs/hdr.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export type ToneMapping = 'reinhard' | 'clamp';
|
|
3
3
|
/**
|
|
4
4
|
* Linear light RGB (3 values per pixel) as sRGB RGBA. Reinhard's extended operator ("Photographic Tone Reproduction
|
|
5
|
-
* for Digital Images", 2002, equation 4) without its automatic exposure, which makes dark scenes like nights
|
|
5
|
+
* for Digital Images", 2002, equation 4) without its automatic exposure, which makes dark scenes like nights gray:
|
|
6
6
|
* the luminance L becomes L (1 + L / Lw²) / (1 + L), the brightest pixel's (at least 1) Lw white, and the channels are
|
|
7
7
|
* scaled by that, keeping their hue; images no brighter than white stay as they are
|
|
8
8
|
*/
|
package/dist/codecs/jpeg.js
CHANGED
|
@@ -678,7 +678,7 @@ function output(frame, adobe, jfif, override) {
|
|
|
678
678
|
const count = components.length;
|
|
679
679
|
let transform;
|
|
680
680
|
if (count === 1)
|
|
681
|
-
transform = '
|
|
681
|
+
transform = 'gray';
|
|
682
682
|
else if (override && (count === 4) === (override === 'cmyk'))
|
|
683
683
|
transform = override;
|
|
684
684
|
else if (count === 3) {
|
|
@@ -694,15 +694,15 @@ function output(frame, adobe, jfif, override) {
|
|
|
694
694
|
else
|
|
695
695
|
throw new Error(`JPEG: ${count} color components are not supported`);
|
|
696
696
|
const [p0, p1, p2, p3] = planes;
|
|
697
|
-
if (transform === '
|
|
697
|
+
if (transform === 'gray' || transform === 'rgb' || transform === 'ycc') {
|
|
698
698
|
const s0 = p0.samples, s1 = (p1 ?? p0).samples, s2 = (p2 ?? p0).samples;
|
|
699
|
-
const
|
|
699
|
+
const gray = transform === 'gray', ycc = transform === 'ycc';
|
|
700
700
|
const words = littleEndian ? new Uint32Array(pixels.buffer) : null;
|
|
701
701
|
for (let y = 0, o = 0; y < height; y++, o = y * width * 4) {
|
|
702
702
|
for (const p of planes)
|
|
703
703
|
p.fill?.(y);
|
|
704
704
|
const r0 = y * p0.stride, r1 = p1 ? y * p1.stride : r0, r2 = p2 ? y * p2.stride : r0;
|
|
705
|
-
if (
|
|
705
|
+
if (gray)
|
|
706
706
|
for (let x = 0; x < width; x++, o += 4) {
|
|
707
707
|
pixels[o] = pixels[o + 1] = pixels[o + 2] = s0[r0 + x];
|
|
708
708
|
pixels[o + 3] = 0xff;
|
|
@@ -807,7 +807,7 @@ function reciprocals(table) {
|
|
|
807
807
|
}
|
|
808
808
|
return r;
|
|
809
809
|
}
|
|
810
|
-
export function encode(pixels, width, height, quality = 90, { exif, comment, segments,
|
|
810
|
+
export function encode(pixels, width, height, quality = 90, { exif, comment, segments, gray = false, fullChroma = false, subsampling } = {}) {
|
|
811
811
|
if (!width || !height || width > 0xffff || height > 0xffff)
|
|
812
812
|
throw new RangeError('JPEG: invalid dimensions');
|
|
813
813
|
quality = Math.min(100, Math.max(1, Math.round(quality)));
|
|
@@ -815,7 +815,7 @@ export function encode(pixels, width, height, quality = 90, { exif, comment, seg
|
|
|
815
815
|
const scaleTable = (base) => base.map(v => Math.min(255, Math.max(1, Math.floor((v * scale + 50) / 100))));
|
|
816
816
|
const tables = [scaleTable(LUMA_QUANTIZATION), scaleTable(CHROMA_QUANTIZATION)];
|
|
817
817
|
const divisors = tables.map(reciprocals);
|
|
818
|
-
const subsample =
|
|
818
|
+
const subsample = !gray && !fullChroma && (subsampling ? subsampling === '4:2:0' : quality <= 90);
|
|
819
819
|
const hmax = subsample ? 2 : 1;
|
|
820
820
|
const mcuSize = 8 * hmax;
|
|
821
821
|
const mcusPerLine = Math.ceil(width / mcuSize);
|
|
@@ -870,8 +870,8 @@ export function encode(pixels, width, height, quality = 90, { exif, comment, seg
|
|
|
870
870
|
{ plane: Cb, stride: CW, h: 1, v: 1, blocksX: chromaBlocksX, blocksY: chromaBlocksY, table: 1 },
|
|
871
871
|
{ plane: Cr, stride: CW, h: 1, v: 1, blocksX: chromaBlocksX, blocksY: chromaBlocksY, table: 1 }
|
|
872
872
|
];
|
|
873
|
-
const componentCount =
|
|
874
|
-
const blocksPerMcu =
|
|
873
|
+
const componentCount = gray ? 1 : 3;
|
|
874
|
+
const blocksPerMcu = gray ? 1 : hmax * hmax + 2;
|
|
875
875
|
const count = mcusPerLine * mcusPerColumn * blocksPerMcu;
|
|
876
876
|
const coefficients = new Int16Array(count * 64);
|
|
877
877
|
const owners = new Uint8Array(count), ends = new Uint8Array(count);
|
|
@@ -911,7 +911,7 @@ export function encode(pixels, width, height, quality = 90, { exif, comment, seg
|
|
|
911
911
|
const blocks = { coefficients, owners, ends, count };
|
|
912
912
|
const frequencies = [new Int32Array(257), new Int32Array(257), new Int32Array(257), new Int32Array(257)];
|
|
913
913
|
countSymbols(blocks, frequencies);
|
|
914
|
-
const huffman = frequencies.slice(0,
|
|
914
|
+
const huffman = frequencies.slice(0, gray ? 2 : 4).map(buildOptimalTable);
|
|
915
915
|
const writer = new BitWriter(width * height);
|
|
916
916
|
writer.bytes(0xff, 0xd8);
|
|
917
917
|
writer.bytes(0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00);
|
|
@@ -930,18 +930,18 @@ export function encode(pixels, width, height, quality = 90, { exif, comment, seg
|
|
|
930
930
|
for (const bytes of segments)
|
|
931
931
|
for (let i = 0; i < bytes.length; i++)
|
|
932
932
|
writer.bytes(bytes[i]);
|
|
933
|
-
for (let t = 0; t < (
|
|
933
|
+
for (let t = 0; t < (gray ? 1 : 2); t++) {
|
|
934
934
|
writer.bytes(0xff, 0xdb, 0x00, 0x43, t);
|
|
935
935
|
for (let k = 0; k < 64; k++)
|
|
936
936
|
writer.bytes(tables[t][ZIGZAG[k]]);
|
|
937
937
|
}
|
|
938
|
-
if (
|
|
938
|
+
if (gray)
|
|
939
939
|
writer.bytes(0xff, 0xc0, 0x00, 0x0b, 0x08, height >> 8, height & 0xff, width >> 8, width & 0xff, 0x01, 1, 0x11, 0);
|
|
940
940
|
else {
|
|
941
941
|
writer.bytes(0xff, 0xc0, 0x00, 0x11, 0x08, height >> 8, height & 0xff, width >> 8, width & 0xff, 0x03);
|
|
942
942
|
writer.bytes(1, hmax << 4 | hmax, 0, 2, 0x11, 1, 3, 0x11, 1);
|
|
943
943
|
}
|
|
944
|
-
for (const [index, id] of [[0, 0x00], [1, 0x10], [2, 0x01], [3, 0x11]].slice(0,
|
|
944
|
+
for (const [index, id] of [[0, 0x00], [1, 0x10], [2, 0x01], [3, 0x11]].slice(0, gray ? 2 : 4)) {
|
|
945
945
|
const { bits, values } = huffman[index];
|
|
946
946
|
const length = 19 + values.length;
|
|
947
947
|
writer.bytes(0xff, 0xc4, length >> 8, length & 0xff, id);
|
|
@@ -950,7 +950,7 @@ export function encode(pixels, width, height, quality = 90, { exif, comment, seg
|
|
|
950
950
|
for (const value of values)
|
|
951
951
|
writer.bytes(value);
|
|
952
952
|
}
|
|
953
|
-
if (
|
|
953
|
+
if (gray)
|
|
954
954
|
writer.bytes(0xff, 0xda, 0x00, 0x08, 0x01, 1, 0x00, 0x00, 0x3f, 0x00);
|
|
955
955
|
else
|
|
956
956
|
writer.bytes(0xff, 0xda, 0x00, 0x0c, 0x03, 1, 0x00, 2, 0x11, 3, 0x11, 0x00, 0x3f, 0x00);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { extensions, unpackSigned } from "./bits.js";
|
|
2
2
|
import { EntropyDecoder } from "./entropy.js";
|
|
3
3
|
export const EXTRA_ALPHA = 0, EXTRA_SPOT = 2, EXTRA_BLACK = 4;
|
|
4
|
-
export const CS_RGB = 0,
|
|
4
|
+
export const CS_RGB = 0, CS_GRAY = 1, CS_XYB = 2;
|
|
5
5
|
export const WP_D65 = 1, WP_CUSTOM = 2, WP_E = 10, WP_DCI = 11;
|
|
6
6
|
export const PRI_SRGB = 1, PRI_CUSTOM = 2, PRI_BT2100 = 9, PRI_P3 = 11;
|
|
7
7
|
export const TF_BT709 = 1, TF_UNKNOWN = 2, TF_LINEAR = 8, TF_SRGB = 13, TF_PQ = 16, TF_DCI = 17, TF_HLG = 18;
|
|
@@ -94,7 +94,7 @@ export function colourEncoding(r) {
|
|
|
94
94
|
else if (c.whitePoint !== WP_D65 && c.whitePoint !== WP_E && c.whitePoint !== WP_DCI)
|
|
95
95
|
throw new Error('JPEG XL: invalid white point');
|
|
96
96
|
}
|
|
97
|
-
if (c.colourSpace !== CS_XYB && c.colourSpace !==
|
|
97
|
+
if (c.colourSpace !== CS_XYB && c.colourSpace !== CS_GRAY) {
|
|
98
98
|
c.primaries = r.enum();
|
|
99
99
|
if (c.primaries === PRI_CUSTOM)
|
|
100
100
|
for (let i = 0; i < 6; i++)
|
|
@@ -214,7 +214,7 @@ export function readICC(r) {
|
|
|
214
214
|
const COMMON_TAGS = ['rTRC', 'rXYZ', 'cprt', 'wtpt', 'bkpt', 'rXYZ', 'gXYZ', 'bXYZ', 'kXYZ', 'rTRC', 'gTRC', 'bTRC', 'kTRC', 'chad', 'desc', 'chrm', 'dmnd', 'dmdd', 'lumi'];
|
|
215
215
|
const COMMON_DATA = ['XYZ ', 'desc', 'text', 'mluc', 'para', 'curv', 'sf32', 'gbd '];
|
|
216
216
|
export function decodeICC(stream) {
|
|
217
|
-
let at
|
|
217
|
+
let at;
|
|
218
218
|
const fail = () => {
|
|
219
219
|
throw new RangeError('invalid ICC stream');
|
|
220
220
|
};
|
|
@@ -280,7 +280,7 @@ export function findPatches(planes, stride, width, height, distance, numExtra) {
|
|
|
280
280
|
maxH = Math.max(maxH, p.h);
|
|
281
281
|
}
|
|
282
282
|
let refW = Math.max(maxW, Math.sqrt(total)), refH = Math.max(maxH, Math.sqrt(total));
|
|
283
|
-
let spots = [], usedH
|
|
283
|
+
let spots = [], usedH;
|
|
284
284
|
for (;;) {
|
|
285
285
|
refW = Math.floor(refW * 1.05 + 1);
|
|
286
286
|
refH = Math.floor(refH * 1.05 + 1);
|
package/dist/codecs/jxl.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { startThreads } from "../util/threads.js";
|
|
2
2
|
import { MAX_PIXELS, checkPixels } from "../util/limits.js";
|
|
3
3
|
import { BitReader } from "./jxl/bits.js";
|
|
4
|
-
import {
|
|
4
|
+
import { CS_GRAY, CS_RGB, decodeICC, imageHeader, PRI_BT2100, PRI_CUSTOM, PRI_P3, readICC, TF_DCI, TF_HLG, TF_LINEAR, TF_PQ, WP_CUSTOM, WP_DCI, WP_E } from "./jxl/headers.js";
|
|
5
5
|
import { frameHeader, toc } from "./jxl/frame-header.js";
|
|
6
6
|
import { decodeFrame } from "./jxl/frame.js";
|
|
7
7
|
import { ColorOutput } from "./jxl/color.js";
|
|
@@ -85,16 +85,16 @@ const PRIMARIES = {
|
|
|
85
85
|
};
|
|
86
86
|
export function colorSpace(image) {
|
|
87
87
|
const c = image.colour;
|
|
88
|
-
if (image.xybEncoded || c.wantICC || (c.colourSpace !== CS_RGB && c.colourSpace !==
|
|
88
|
+
if (image.xybEncoded || c.wantICC || (c.colourSpace !== CS_RGB && c.colourSpace !== CS_GRAY))
|
|
89
89
|
return;
|
|
90
90
|
if (c.transfer === TF_PQ || c.transfer === TF_HLG)
|
|
91
91
|
return;
|
|
92
92
|
const curve = c.transfer === -1 ? (c.gamma > 0 ? v => v ** (1 / c.gamma) : null)
|
|
93
93
|
: c.transfer === TF_LINEAR ? v => v : c.transfer === TF_DCI ? v => v ** 2.6 : null;
|
|
94
94
|
const white = c.whitePoint === WP_CUSTOM ? c.white : c.whitePoint === WP_E ? [1 / 3, 1 / 3] : c.whitePoint === WP_DCI ? [0.314, 0.351] : null;
|
|
95
|
-
const primaries = c.colourSpace ===
|
|
95
|
+
const primaries = c.colourSpace === CS_GRAY ? null : c.primaries === PRI_CUSTOM
|
|
96
96
|
? [0, 2, 4].map(i => c.primariesXY.slice(i, i + 2)) : PRIMARIES[c.primaries] ?? null;
|
|
97
|
-
if (c.colourSpace ===
|
|
97
|
+
if (c.colourSpace === CS_GRAY)
|
|
98
98
|
return curve ? { xy: null, curve } : undefined;
|
|
99
99
|
if (!curve && !white && !primaries)
|
|
100
100
|
return;
|
|
@@ -136,7 +136,7 @@ export async function* frames(data, maxPixels = MAX_PIXELS, toneMapping = 'reinh
|
|
|
136
136
|
shown++;
|
|
137
137
|
yield {
|
|
138
138
|
width: image.width, height: image.height, pixels: state.output.toRGBA(frame.canvas), duration: frame.header.duration * tick,
|
|
139
|
-
orientation: image.orientation, loopCount: a ? a.numLoops : 1, icc, colorSpace: space
|
|
139
|
+
orientation: image.orientation, loopCount: a ? a.numLoops : 1, animated: !!a, icc, colorSpace: space
|
|
140
140
|
};
|
|
141
141
|
}
|
|
142
142
|
if (frame.header.isLast)
|
package/dist/codecs/netpbm.js
CHANGED
|
@@ -186,31 +186,31 @@ export function encode(rgba, width, height) {
|
|
|
186
186
|
if (width < 1 || height < 1)
|
|
187
187
|
throw new RangeError('Netpbm: dimensions must be at least 1');
|
|
188
188
|
const count = width * height;
|
|
189
|
-
let opaque = true,
|
|
189
|
+
let opaque = true, gray = true, bitmap = true;
|
|
190
190
|
for (let p = 0; p < count * 4; p += 4) {
|
|
191
191
|
const r = rgba[p], g = rgba[p + 1], b = rgba[p + 2], a = rgba[p + 3];
|
|
192
192
|
if (a !== 255)
|
|
193
193
|
opaque = false;
|
|
194
194
|
if (r !== g || g !== b)
|
|
195
|
-
|
|
195
|
+
gray = false;
|
|
196
196
|
if (r !== 0 && r !== 255 || r !== g || g !== b)
|
|
197
197
|
bitmap = false;
|
|
198
|
-
if (!opaque && !
|
|
198
|
+
if (!opaque && !gray)
|
|
199
199
|
break;
|
|
200
200
|
}
|
|
201
201
|
const ascii = (text) => Uint8Array.from(text, c => c.charCodeAt(0));
|
|
202
202
|
let header, depth;
|
|
203
203
|
if (!opaque) {
|
|
204
|
-
depth =
|
|
205
|
-
header = ascii(`P7\nWIDTH ${width}\nHEIGHT ${height}\nDEPTH ${depth}\nMAXVAL 255\nTUPLTYPE ${
|
|
204
|
+
depth = gray ? 2 : 4;
|
|
205
|
+
header = ascii(`P7\nWIDTH ${width}\nHEIGHT ${height}\nDEPTH ${depth}\nMAXVAL 255\nTUPLTYPE ${gray ? 'GRAYSCALE_ALPHA' : 'RGB_ALPHA'}\nENDHDR\n`);
|
|
206
206
|
}
|
|
207
207
|
else if (bitmap) {
|
|
208
208
|
depth = 0;
|
|
209
209
|
header = ascii(`P4\n${width} ${height}\n`);
|
|
210
210
|
}
|
|
211
211
|
else {
|
|
212
|
-
depth =
|
|
213
|
-
header = ascii(`P${
|
|
212
|
+
depth = gray ? 1 : 3;
|
|
213
|
+
header = ascii(`P${gray ? 5 : 6}\n${width} ${height}\n255\n`);
|
|
214
214
|
}
|
|
215
215
|
const stride = depth ? width * depth : width + 7 >> 3;
|
|
216
216
|
const out = new Uint8Array(header.length + stride * height);
|
package/dist/codecs/png.js
CHANGED
|
@@ -116,7 +116,7 @@ export function alphaCost(images) {
|
|
|
116
116
|
return scanner.cost();
|
|
117
117
|
}
|
|
118
118
|
export class AlphaCost {
|
|
119
|
-
|
|
119
|
+
gray = true;
|
|
120
120
|
colors = new Set();
|
|
121
121
|
done = false;
|
|
122
122
|
add(rgba) {
|
|
@@ -134,20 +134,20 @@ export class AlphaCost {
|
|
|
134
134
|
if (rgba[o + 3] === 0)
|
|
135
135
|
continue;
|
|
136
136
|
if (rgba[o] !== rgba[o + 1] || rgba[o] !== rgba[o + 2])
|
|
137
|
-
this.
|
|
137
|
+
this.gray = false;
|
|
138
138
|
if (colors.size <= 256)
|
|
139
139
|
colors.add(value);
|
|
140
|
-
else if (!this.
|
|
140
|
+
else if (!this.gray) {
|
|
141
141
|
this.done = true;
|
|
142
142
|
return;
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
146
|
cost() {
|
|
147
|
-
const { colors,
|
|
147
|
+
const { colors, gray } = this;
|
|
148
148
|
const palette = (entries) => (entries <= 2 ? 1 : entries <= 4 ? 2 : entries <= 16 ? 4 : 8) / 8;
|
|
149
|
-
const transparent = colors.size < 256 ? palette(colors.size + 1) :
|
|
150
|
-
const opaque = colors.size <= 256 ? palette(colors.size) :
|
|
149
|
+
const transparent = colors.size < 256 ? palette(colors.size + 1) : gray ? 2 : 4;
|
|
150
|
+
const opaque = colors.size <= 256 ? palette(colors.size) : gray ? 1 : 3;
|
|
151
151
|
return transparent / opaque;
|
|
152
152
|
}
|
|
153
153
|
}
|
|
@@ -159,7 +159,7 @@ function pixelFormat(images) {
|
|
|
159
159
|
}
|
|
160
160
|
export class FormatScanner {
|
|
161
161
|
opaque = true;
|
|
162
|
-
|
|
162
|
+
gray = true;
|
|
163
163
|
colors = new Map();
|
|
164
164
|
add(rgba) {
|
|
165
165
|
const pixels = new Uint32Array(rgba.buffer, rgba.byteOffset, rgba.length >> 2);
|
|
@@ -173,22 +173,22 @@ export class FormatScanner {
|
|
|
173
173
|
if (rgba[o + 3] !== 255)
|
|
174
174
|
this.opaque = false;
|
|
175
175
|
if (rgba[o] !== rgba[o + 1] || rgba[o] !== rgba[o + 2])
|
|
176
|
-
this.
|
|
176
|
+
this.gray = false;
|
|
177
177
|
if (this.colors && !this.colors.has(value)) {
|
|
178
178
|
if (this.colors.size === 256)
|
|
179
179
|
this.colors = null;
|
|
180
180
|
else
|
|
181
181
|
this.colors.set(value, this.colors.size);
|
|
182
182
|
}
|
|
183
|
-
if (!this.colors && !this.opaque && !this.
|
|
183
|
+
if (!this.colors && !this.opaque && !this.gray)
|
|
184
184
|
return;
|
|
185
185
|
}
|
|
186
186
|
}
|
|
187
187
|
format() {
|
|
188
|
-
return formatOf(this.colors, this.opaque, this.
|
|
188
|
+
return formatOf(this.colors, this.opaque, this.gray);
|
|
189
189
|
}
|
|
190
190
|
}
|
|
191
|
-
function formatOf(colors, opaque,
|
|
191
|
+
function formatOf(colors, opaque, gray) {
|
|
192
192
|
if (colors) {
|
|
193
193
|
const entries = [...colors.keys()].map(value => new Uint8Array(new Uint32Array([value]).buffer));
|
|
194
194
|
entries.sort((a, b) => a[3] - b[3]);
|
|
@@ -221,7 +221,7 @@ function formatOf(colors, opaque, grey) {
|
|
|
221
221
|
}
|
|
222
222
|
};
|
|
223
223
|
}
|
|
224
|
-
const channels = (
|
|
224
|
+
const channels = (gray ? 1 : 3) + (opaque ? 0 : 1);
|
|
225
225
|
return {
|
|
226
226
|
channels, depth: 8,
|
|
227
227
|
convert(rgba, width, height) {
|
|
@@ -540,11 +540,11 @@ export function isAnimated(array) {
|
|
|
540
540
|
return false;
|
|
541
541
|
}
|
|
542
542
|
}
|
|
543
|
-
export async function decodeAnimation(array,
|
|
543
|
+
export async function decodeAnimation(array, firstFrameOnly = false, maxPixels = MAX_PIXELS) {
|
|
544
544
|
const p = parse(array);
|
|
545
545
|
const { width, height } = p.header;
|
|
546
546
|
const orientation = p.exif ? exifOrientation(p.exif) : await textOrientation(p.text);
|
|
547
|
-
checkPixels(width, height, maxPixels, !p.animation || p.animationError ||
|
|
547
|
+
checkPixels(width, height, maxPixels, !p.animation || p.animationError || firstFrameOnly ? 1 : Math.max(1, p.frames.length));
|
|
548
548
|
if (!p.animation || p.animationError) {
|
|
549
549
|
const pixels = await decodeImageData(p, p.idat, width, height);
|
|
550
550
|
return { width, height, plays: 0, frames: [{ x: 0, y: 0, width, height, duration: 0, dispose: 0, blend: 0, pixels }], icc: await profile(p.iccp), orientation };
|
|
@@ -559,7 +559,7 @@ export async function decodeAnimation(array, onlyFirstFrame = false, maxPixels =
|
|
|
559
559
|
blend: c.blend,
|
|
560
560
|
pixels: await decodeImageData(p, data, c.width, c.height)
|
|
561
561
|
});
|
|
562
|
-
if (
|
|
562
|
+
if (firstFrameOnly)
|
|
563
563
|
break;
|
|
564
564
|
}
|
|
565
565
|
return { width, height, plays: p.animation.plays, frames, icc: await profile(p.iccp), orientation };
|
package/dist/codecs/quantize.js
CHANGED
|
@@ -1,4 +1,22 @@
|
|
|
1
1
|
const WR = 3, WG = 4, WB = 2;
|
|
2
|
+
const DITHER = { 'none': true, 'floyd-steinberg': true, 'atkinson': true, 'ordered': true };
|
|
3
|
+
export const DITHER_METHODS = Object.keys(DITHER);
|
|
4
|
+
export function paletteOptions(colors, dither, ditherStrength) {
|
|
5
|
+
let count;
|
|
6
|
+
if (colors !== undefined) {
|
|
7
|
+
count = Number(colors);
|
|
8
|
+
if (!Number.isInteger(count) || count < 2 || count > 256)
|
|
9
|
+
throw new RangeError(`colors must be an integer from 2 to 256, not ${colors}`);
|
|
10
|
+
}
|
|
11
|
+
if (!DITHER_METHODS.includes(dither))
|
|
12
|
+
throw new TypeError(`dither must be one of ${DITHER_METHODS.map(name => `'${name}'`).join(', ')}, not ${String(dither)}`);
|
|
13
|
+
const strength = Number(ditherStrength);
|
|
14
|
+
if (!Number.isFinite(strength))
|
|
15
|
+
throw new RangeError(`Invalid ditherStrength: ${ditherStrength}`);
|
|
16
|
+
if (strength < 0 || strength > 1)
|
|
17
|
+
throw new RangeError(`ditherStrength must be 0-1, not ${ditherStrength}`);
|
|
18
|
+
return { colors: count, dither: dither, strength };
|
|
19
|
+
}
|
|
2
20
|
export function dominantColor(pixels, include, clusters = 8) {
|
|
3
21
|
const { palette, weights } = medianCut(pixels, clusters, 4, include);
|
|
4
22
|
if (!weights.length)
|
|
@@ -9,7 +27,7 @@ export function dominantColor(pixels, include, clusters = 8) {
|
|
|
9
27
|
best = i;
|
|
10
28
|
return [Math.round(palette[best * 3]), Math.round(palette[best * 3 + 1]), Math.round(palette[best * 3 + 2])];
|
|
11
29
|
}
|
|
12
|
-
export function quantize(pixels, width, { maxColors = 256,
|
|
30
|
+
export function quantize(pixels, width, { maxColors = 256, dither = 'floyd-steinberg', ditherStrength = 0.85, transparent = false } = {}) {
|
|
13
31
|
const length = pixels.length >> 2;
|
|
14
32
|
let hasTransparency = transparent;
|
|
15
33
|
for (let i = 3; i < pixels.length; i += 4)
|
|
@@ -17,16 +35,14 @@ export function quantize(pixels, width, { maxColors = 256, quality = 95, transpa
|
|
|
17
35
|
hasTransparency = true;
|
|
18
36
|
break;
|
|
19
37
|
}
|
|
20
|
-
|
|
21
|
-
const limit = Math.min(maxColors, Math.round(8 * 32 ** (Math.min(quality, 90) / 90)));
|
|
22
|
-
const colors = hasTransparency ? limit - 1 : limit;
|
|
38
|
+
const colors = hasTransparency ? maxColors - 1 : maxColors;
|
|
23
39
|
const exact = exactPalette(pixels, colors);
|
|
24
40
|
let palette, indices;
|
|
25
41
|
if (exact)
|
|
26
42
|
({ palette, indices } = exact);
|
|
27
43
|
else {
|
|
28
|
-
palette = Uint8Array.from(medianCut(pixels, colors,
|
|
29
|
-
indices = ditherMap(pixels, width, palette, palette.length / 3);
|
|
44
|
+
palette = Uint8Array.from(medianCut(pixels, colors, 4).palette, Math.round);
|
|
45
|
+
indices = dither === 'floyd-steinberg' ? ditherMap(pixels, width, palette, palette.length / 3, ditherStrength) : paletteIndices(pixels, width, palette, dither, ditherStrength);
|
|
30
46
|
}
|
|
31
47
|
let transparentIndex = -1;
|
|
32
48
|
if (hasTransparency) {
|
|
@@ -340,13 +356,23 @@ function createLookup(palette, entries) {
|
|
|
340
356
|
return index;
|
|
341
357
|
};
|
|
342
358
|
}
|
|
343
|
-
function
|
|
359
|
+
function paletteIndices(pixels, width, palette, dither, strength) {
|
|
360
|
+
const mapped = pixels.slice();
|
|
361
|
+
applyPalette(mapped, width, (pixels.length >> 2) / width, palette, dither, strength);
|
|
362
|
+
const lookup = new Map();
|
|
363
|
+
for (let i = palette.length / 3 - 1; i >= 0; i--)
|
|
364
|
+
lookup.set(palette[i * 3] << 16 | palette[i * 3 + 1] << 8 | palette[i * 3 + 2], i);
|
|
365
|
+
const indices = new Uint8Array(pixels.length >> 2);
|
|
366
|
+
for (let p = 0; p < indices.length; p++)
|
|
367
|
+
indices[p] = lookup.get(mapped[p * 4] << 16 | mapped[p * 4 + 1] << 8 | mapped[p * 4 + 2]) ?? 0;
|
|
368
|
+
return indices;
|
|
369
|
+
}
|
|
370
|
+
function ditherMap(pixels, width, palette, entries, strength) {
|
|
344
371
|
const height = (pixels.length >> 2) / width;
|
|
345
372
|
const lookup = createLookup(palette, entries);
|
|
346
373
|
const indices = new Uint8Array(pixels.length >> 2);
|
|
347
374
|
let current = new Float32Array((width + 2) * 3);
|
|
348
375
|
let next = new Float32Array((width + 2) * 3);
|
|
349
|
-
const strength = 0.85;
|
|
350
376
|
for (let y = 0; y < height; y++) {
|
|
351
377
|
const reverse = y & 1;
|
|
352
378
|
for (let step = 0; step < width; step++) {
|
package/dist/codecs/tiff.js
CHANGED
|
@@ -173,7 +173,7 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
|
|
|
173
173
|
joined.set(stream.subarray(2), tables.length - 2);
|
|
174
174
|
stream = joined;
|
|
175
175
|
}
|
|
176
|
-
const decoded = jpeg.decode(stream, { transform });
|
|
176
|
+
const decoded = jpeg.decode(stream, { transform, maxPixels });
|
|
177
177
|
blitRGBA(decoded.pixels, decoded.width, decoded.height, pixels, width, height, (s % segmentsAcross) * segmentWidth, (s / segmentsAcross | 0) * segmentHeight);
|
|
178
178
|
}
|
|
179
179
|
return { width, height, pixels, orientation, icc };
|
package/dist/codecs/webp.js
CHANGED
|
@@ -136,7 +136,7 @@ export function decode(data, maxPixels = MAX_PIXELS) {
|
|
|
136
136
|
}
|
|
137
137
|
return { ...image, orientation, icc };
|
|
138
138
|
}
|
|
139
|
-
export function decodeAnimation(data,
|
|
139
|
+
export function decodeAnimation(data, firstFrameOnly = false, maxPixels = MAX_PIXELS) {
|
|
140
140
|
const list = container(data);
|
|
141
141
|
const header = list[0];
|
|
142
142
|
if (!header || header.type !== VP8X || !list.some(c => c.type === ANMF)) {
|
|
@@ -146,7 +146,7 @@ export function decodeAnimation(data, onlyFirstFrame = false, maxPixels = MAX_PI
|
|
|
146
146
|
if (header.data.length < 10)
|
|
147
147
|
throw new Error('WebP: invalid VP8X chunk');
|
|
148
148
|
const width = u24(header.data, 4) + 1, height = u24(header.data, 7) + 1;
|
|
149
|
-
checkPixels(width, height, maxPixels,
|
|
149
|
+
checkPixels(width, height, maxPixels, firstFrameOnly ? 1 : list.filter(c => c.type === ANMF).length);
|
|
150
150
|
let loopCount = 0, background = 0;
|
|
151
151
|
const frames = [];
|
|
152
152
|
for (const chunk of list) {
|
|
@@ -170,7 +170,7 @@ export function decodeAnimation(data, onlyFirstFrame = false, maxPixels = MAX_PI
|
|
|
170
170
|
dispose: (d[15] & 1) === 1,
|
|
171
171
|
pixels: frame.pixels
|
|
172
172
|
});
|
|
173
|
-
if (
|
|
173
|
+
if (firstFrameOnly)
|
|
174
174
|
break;
|
|
175
175
|
}
|
|
176
176
|
}
|
package/dist/codecs/zlib.js
CHANGED
package/dist/colors.d.ts
CHANGED
|
@@ -37,7 +37,7 @@ export declare class Colors {
|
|
|
37
37
|
static fromRGB(r: number, g: number, b: number): Color;
|
|
38
38
|
/**
|
|
39
39
|
* Converts HSLA colors to RGBA colors
|
|
40
|
-
* @param h hue (0..
|
|
40
|
+
* @param h hue in degrees (0..360, like CSS' `hsl()`: 0 red, 120 green, 240 blue; other values wrap around)
|
|
41
41
|
* @param s saturation (0..1)
|
|
42
42
|
* @param l lightness (0..1)
|
|
43
43
|
* @param a opacity (0..1)
|
|
@@ -45,8 +45,8 @@ export declare class Colors {
|
|
|
45
45
|
*/
|
|
46
46
|
static fromHSLA(h: number, s: number, l: number, a: number): Color;
|
|
47
47
|
/**
|
|
48
|
-
* Converts HSL colors to RGBA colors (
|
|
49
|
-
* @param h hue (0..
|
|
48
|
+
* Converts HSL colors to RGBA colors (fully opaque)
|
|
49
|
+
* @param h hue in degrees (0..360, like CSS' `hsl()`; other values wrap around)
|
|
50
50
|
* @param s saturation (0..1)
|
|
51
51
|
* @param l lightness (0..1)
|
|
52
52
|
* @returns color
|
|
@@ -59,7 +59,7 @@ export declare class Colors {
|
|
|
59
59
|
*/
|
|
60
60
|
static toRGBA(color: Color): [r: number, g: number, b: number, a: number];
|
|
61
61
|
/**
|
|
62
|
-
* Converts a color value to an array of RGB values (ignoring the
|
|
62
|
+
* Converts a color value to an array of RGB values (ignoring the color's alpha)
|
|
63
63
|
* @param color The color value to convert
|
|
64
64
|
* @returns The RGB values ([R, G, B])
|
|
65
65
|
*/
|
|
@@ -67,13 +67,14 @@ export declare class Colors {
|
|
|
67
67
|
/**
|
|
68
68
|
* Converts a color to an array of HSLA values
|
|
69
69
|
* @param color The color value to convert
|
|
70
|
-
* @returns The HSLA values ([H, S, L, A])
|
|
70
|
+
* @returns The HSLA values ([H, S, L, A]): H in degrees (0..360, like CSS' `hsl()`), S, L and A in 0..1
|
|
71
71
|
*/
|
|
72
72
|
static toHSLA(color: Color): [h: number, s: number, l: number, a: number];
|
|
73
73
|
/**
|
|
74
74
|
* Creates a multi-point gradient generator
|
|
75
|
-
* @param
|
|
75
|
+
* @param stops The colors along the gradient: their positions (0..1) and colors (`{ 0: 0xff0000ff, 1: 0x00ff00ff }`),
|
|
76
|
+
* or just the colors, evenly spaced (`[0xff0000ff, 0x00ff00ff]`), like {@link GradientOptions.stops}
|
|
76
77
|
* @return The gradient generator. The function argument is the position in the gradient (0..1).
|
|
77
78
|
*/
|
|
78
|
-
static gradient(
|
|
79
|
+
static gradient(stops: Record<number, Color> | Color[]): (position: number) => Color;
|
|
79
80
|
}
|
package/dist/colors.js
CHANGED
|
@@ -78,7 +78,7 @@ export class Colors {
|
|
|
78
78
|
return Colors.fromRGBA(r, g, b, 0xff);
|
|
79
79
|
}
|
|
80
80
|
static fromHSLA(h, s, l, a) {
|
|
81
|
-
h = wrapHue(finite(h, 'hue'));
|
|
81
|
+
h = wrapHue(finite(h, 'hue') / 360);
|
|
82
82
|
s = finite(s, 'saturation');
|
|
83
83
|
l = finite(l, 'lightness');
|
|
84
84
|
a = finite(a, 'alpha');
|
|
@@ -111,9 +111,19 @@ export class Colors {
|
|
|
111
111
|
}
|
|
112
112
|
static toHSLA(color) {
|
|
113
113
|
const [r, g, b, a] = Colors.toRGBA(color);
|
|
114
|
-
|
|
114
|
+
const [h, s, l, alpha] = rgbaToHSLA(r, g, b, a);
|
|
115
|
+
return [h * 360, s, l, alpha];
|
|
115
116
|
}
|
|
116
|
-
static gradient(
|
|
117
|
+
static gradient(stops) {
|
|
118
|
+
let colors;
|
|
119
|
+
if (Array.isArray(stops)) {
|
|
120
|
+
if (!stops.length)
|
|
121
|
+
throw new RangeError('Invalid gradient point count');
|
|
122
|
+
const list = stops.length === 1 ? [stops[0], stops[0]] : stops;
|
|
123
|
+
colors = Object.fromEntries(list.map((color, i) => [i / (list.length - 1), color]));
|
|
124
|
+
}
|
|
125
|
+
else
|
|
126
|
+
colors = stops;
|
|
117
127
|
const entries = Object.entries(colors).sort((a, b) => Number(a[0]) - Number(b[0]));
|
|
118
128
|
const positions = entries.map(e => finite(parseFloat(e[0]), 'gradient position'));
|
|
119
129
|
const values = entries.map(e => checkColor(e[1]));
|
|
@@ -125,6 +135,8 @@ export class Colors {
|
|
|
125
135
|
else if (positions.length === 2) {
|
|
126
136
|
const gradient = gradientBetween(values[0], values[1]);
|
|
127
137
|
return position => {
|
|
138
|
+
if (Number.isNaN(position))
|
|
139
|
+
throw new RangeError(`Invalid gradient position: ${position}`);
|
|
128
140
|
if (position <= positions[0])
|
|
129
141
|
return values[0];
|
|
130
142
|
if (position >= positions[1])
|
|
@@ -140,6 +152,8 @@ export class Colors {
|
|
|
140
152
|
if (minPos === undefined)
|
|
141
153
|
continue;
|
|
142
154
|
const maxPos = positions[i];
|
|
155
|
+
if (maxPos === minPos)
|
|
156
|
+
continue;
|
|
143
157
|
let minVal = values[i - 1];
|
|
144
158
|
if (minVal === undefined)
|
|
145
159
|
minVal = values[i];
|
|
@@ -147,6 +161,12 @@ export class Colors {
|
|
|
147
161
|
const gradient = gradientBetween(minVal, maxVal);
|
|
148
162
|
gradients.push({ min: minPos, max: maxPos, gradient });
|
|
149
163
|
}
|
|
164
|
+
if (!gradients.length)
|
|
165
|
+
return position => {
|
|
166
|
+
if (Number.isNaN(position))
|
|
167
|
+
throw new RangeError(`Invalid gradient position: ${position}`);
|
|
168
|
+
return position <= minDef ? values[0] : values[values.length - 1];
|
|
169
|
+
};
|
|
150
170
|
return position => {
|
|
151
171
|
if (position <= minDef)
|
|
152
172
|
return gradients[0].gradient(0);
|