imagescript 3.9.0 → 3.10.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 +16 -14
- package/dist/animation-encoder.js +3 -3
- package/dist/animation.d.ts +2 -0
- package/dist/animation.js +5 -2
- package/dist/codecs/av1/encode.js +149 -44
- package/dist/codecs/avif-enc.js +35 -9
- package/dist/codecs/avif.js +152 -81
- package/dist/codecs/jxl/acs-enc.js +18 -6
- package/dist/codecs/jxl/aq-enc.js +38 -37
- package/dist/codecs/jxl/color.js +14 -3
- package/dist/codecs/jxl/filters.js +44 -34
- package/dist/codecs/jxl/frame.js +218 -37
- package/dist/codecs/jxl/modular.js +40 -29
- package/dist/codecs/jxl/patches-enc.js +15 -8
- package/dist/codecs/jxl/vardct-enc.js +184 -134
- package/dist/codecs/jxl/vardct.js +53 -19
- package/dist/codecs/jxl.js +32 -21
- package/dist/codecs/vp8-enc.js +2 -2
- package/dist/codecs/vp8l.js +94 -62
- package/dist/codecs/webp.js +32 -11
- package/dist/image.d.ts +32 -3
- package/dist/image.js +19 -9
- package/dist/render/svg.js +99 -25
- package/dist/util/threads.js +101 -0
- package/dist/util/worker.js +52 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/codecs/vp8l.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { ByteWriter } from "../util/bytes.js";
|
|
7
7
|
import { MAX_PIXELS, checkPixels } from "../util/limits.js";
|
|
8
|
+
import { runChunks, shared } from "../util/threads.js";
|
|
8
9
|
const SIGNATURE = 0x2f;
|
|
9
10
|
const CODE_LENGTH_ORDER = [17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
|
10
11
|
const DISTANCE_MAP = Int8Array.of(0, 1, 1, 0, 1, 1, -1, 1, 0, 2, 2, 0, 1, 2, -1, 2, 2, 1, -2, 1, 2, 2, -2, 2, 0, 3, 3, 0, 1, 3, -1, 3, 3, 1, -3, 1, 2, 3, -2, 3, 3, 2, -3, 2, 0, 4, 4, 0, 1, 4, -1, 4, 4, 1, -4, 1, 3, 3, -3, 3, 2, 4, -2, 4, 4, 2, -4, 2, 0, 5, 3, 4, -3, 4, 4, 3, -4, 3, 5, 0, 1, 5, -1, 5, 5, 1, -5, 1, 2, 5, -2, 5, 5, 2, -5, 2, 4, 4, -4, 4, 3, 5, -3, 5, 5, 3, -5, 3, 0, 6, 6, 0, 1, 6, -1, 6, 6, 1, -6, 1, 2, 6, -2, 6, 6, 2, -6, 2, 4, 5, -4, 5, 5, 4, -5, 4, 3, 6, -3, 6, 6, 3, -6, 3, 0, 7, 7, 0, 1, 7, -1, 7, 5, 5, -5, 5, 7, 1, -7, 1, 4, 6, -4, 6, 6, 4, -6, 4, 2, 7, -2, 7, 7, 2, -7, 2, 3, 7, -3, 7, 7, 3, -7, 3, 5, 6, -5, 6, 6, 5, -6, 5, 8, 0, 4, 7, -4, 7, 7, 4, -7, 4, 8, 1, 8, 2, 6, 6, -6, 6, 8, 3, 5, 7, -5, 7, 7, 5, -7, 5, 8, 4, 6, 7, -6, 7, 7, 6, -7, 6, 8, 5, 7, 7, -7, 7, 8, 6, 8, 7);
|
|
@@ -958,7 +959,7 @@ function tokenSymbols(stream, k, out) {
|
|
|
958
959
|
else
|
|
959
960
|
out[0] = NUM_LITERAL_CODES + NUM_LENGTH_CODES + v;
|
|
960
961
|
}
|
|
961
|
-
function prefixGroups(stream, width, height, cacheBits, bits) {
|
|
962
|
+
async function prefixGroups(stream, width, height, cacheBits, bits, threads) {
|
|
962
963
|
const bw = subSize(width, bits), bh = subSize(height, bits), nb = bw * bh;
|
|
963
964
|
const greenSize = NUM_LITERAL_CODES + NUM_LENGTH_CODES + (cacheBits ? 1 << cacheBits : 0);
|
|
964
965
|
const sizes = [greenSize, 256, 256, 256, NUM_DISTANCE_CODES], offsets = [0, greenSize, greenSize + 256, greenSize + 512, greenSize + 768];
|
|
@@ -988,7 +989,10 @@ function prefixGroups(stream, width, height, cacheBits, bits) {
|
|
|
988
989
|
}
|
|
989
990
|
while (blk < nb)
|
|
990
991
|
starts[++blk] = sparse.length;
|
|
991
|
-
const
|
|
992
|
+
const array = (Type, bytes, length) => new Type(threads ? new SharedArrayBuffer(bytes * length) : new ArrayBuffer(bytes * length));
|
|
993
|
+
const entries = array(Float64Array, 8, sparse.length);
|
|
994
|
+
entries.set(sparse);
|
|
995
|
+
const sharedStarts = threads ? shared(starts) : starts;
|
|
992
996
|
const logs = (cl) => {
|
|
993
997
|
const t = totals(cl), out = new Float64Array(total);
|
|
994
998
|
for (let c = 0; c < 5; c++) {
|
|
@@ -998,12 +1002,6 @@ function prefixGroups(stream, width, height, cacheBits, bits) {
|
|
|
998
1002
|
}
|
|
999
1003
|
return out;
|
|
1000
1004
|
};
|
|
1001
|
-
const cost = (blk, log) => {
|
|
1002
|
-
let bitsSum = 0;
|
|
1003
|
-
for (let e = starts[blk]; e < starts[blk + 1]; e += 2)
|
|
1004
|
-
bitsSum += entries[e + 1] * log[entries[e]];
|
|
1005
|
-
return bitsSum;
|
|
1006
|
-
};
|
|
1007
1005
|
const totals = (cl) => Float64Array.from(offsets, (o, c) => {
|
|
1008
1006
|
let t = 0;
|
|
1009
1007
|
for (let s = 0; s < sizes[c]; s++)
|
|
@@ -1022,7 +1020,13 @@ function prefixGroups(stream, width, height, cacheBits, bits) {
|
|
|
1022
1020
|
}
|
|
1023
1021
|
return b;
|
|
1024
1022
|
};
|
|
1025
|
-
const used = Array.from({ length: nb }, (_, i) => i).filter(i => starts[i + 1] > starts[i]);
|
|
1023
|
+
const used = Int32Array.from(Array.from({ length: nb }, (_, i) => i).filter(i => starts[i + 1] > starts[i]));
|
|
1024
|
+
const sharedUsed = threads ? shared(used) : used;
|
|
1025
|
+
const costs = array(Float64Array, 8, used.length), nearest = array(Int32Array, 4, used.length);
|
|
1026
|
+
const blockCosts = async (logs) => {
|
|
1027
|
+
const task = { task: 'vp8l-costs', entries, starts: sharedStarts, used: sharedUsed, logs, costs, nearest };
|
|
1028
|
+
await runChunks(threads, task, Math.ceil(used.length / COST_BLOCKS), costChunks(task));
|
|
1029
|
+
};
|
|
1026
1030
|
const single = new Float64Array(total);
|
|
1027
1031
|
for (const blk of used)
|
|
1028
1032
|
for (let e = starts[blk]; e < starts[blk + 1]; e += 2)
|
|
@@ -1041,10 +1045,11 @@ function prefixGroups(stream, width, height, cacheBits, bits) {
|
|
|
1041
1045
|
const centers = [blockHistogram(used[0])];
|
|
1042
1046
|
const dist = new Float64Array(nb).fill(Infinity);
|
|
1043
1047
|
while (centers.length < k) {
|
|
1044
|
-
|
|
1048
|
+
await blockCosts([logs(centers[centers.length - 1])]);
|
|
1045
1049
|
let far = -1;
|
|
1046
|
-
for (
|
|
1047
|
-
|
|
1050
|
+
for (let u = 0; u < used.length; u++) {
|
|
1051
|
+
const blk = used[u];
|
|
1052
|
+
dist[blk] = Math.min(dist[blk], costs[u]);
|
|
1048
1053
|
if (far < 0 || dist[blk] > dist[far])
|
|
1049
1054
|
far = blk;
|
|
1050
1055
|
}
|
|
@@ -1052,18 +1057,9 @@ function prefixGroups(stream, width, height, cacheBits, bits) {
|
|
|
1052
1057
|
}
|
|
1053
1058
|
let clusters = centers;
|
|
1054
1059
|
for (let iter = 0; iter < 3; iter++) {
|
|
1055
|
-
|
|
1056
|
-
for (
|
|
1057
|
-
|
|
1058
|
-
for (let j = 0; j < ls.length; j++) {
|
|
1059
|
-
const v = cost(blk, ls[j]);
|
|
1060
|
-
if (v < bv) {
|
|
1061
|
-
bv = v;
|
|
1062
|
-
bc = j;
|
|
1063
|
-
}
|
|
1064
|
-
}
|
|
1065
|
-
assign[blk] = bc;
|
|
1066
|
-
}
|
|
1060
|
+
await blockCosts(clusters.map(logs));
|
|
1061
|
+
for (let u = 0; u < used.length; u++)
|
|
1062
|
+
assign[used[u]] = nearest[u];
|
|
1067
1063
|
clusters = clusters.map(() => new Float64Array(total));
|
|
1068
1064
|
for (const blk of used) {
|
|
1069
1065
|
const cl = clusters[assign[blk]];
|
|
@@ -1093,7 +1089,34 @@ function prefixGroups(stream, width, height, cacheBits, bits) {
|
|
|
1093
1089
|
const [groups, kept] = best;
|
|
1094
1090
|
return [groups, kept.map(cl => sizes.map((size, c) => Int32Array.from(cl.subarray(offsets[c], offsets[c] + size)))), bestBits];
|
|
1095
1091
|
}
|
|
1096
|
-
|
|
1092
|
+
const COST_BLOCKS = 1024;
|
|
1093
|
+
export function costChunks({ entries, starts, used, logs, costs, nearest }) {
|
|
1094
|
+
const cost = (blk, log) => {
|
|
1095
|
+
let bitsSum = 0;
|
|
1096
|
+
for (let e = starts[blk]; e < starts[blk + 1]; e += 2)
|
|
1097
|
+
bitsSum += entries[e + 1] * log[entries[e]];
|
|
1098
|
+
return bitsSum;
|
|
1099
|
+
};
|
|
1100
|
+
return chunk => {
|
|
1101
|
+
for (let u = chunk * COST_BLOCKS; u < Math.min(used.length, (chunk + 1) * COST_BLOCKS); u++) {
|
|
1102
|
+
const blk = used[u];
|
|
1103
|
+
if (logs.length === 1) {
|
|
1104
|
+
costs[u] = cost(blk, logs[0]);
|
|
1105
|
+
continue;
|
|
1106
|
+
}
|
|
1107
|
+
let bc = 0, bv = Infinity;
|
|
1108
|
+
for (let j = 0; j < logs.length; j++) {
|
|
1109
|
+
const v = cost(blk, logs[j]);
|
|
1110
|
+
if (v < bv) {
|
|
1111
|
+
bv = v;
|
|
1112
|
+
bc = j;
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
nearest[u] = bc;
|
|
1116
|
+
}
|
|
1117
|
+
};
|
|
1118
|
+
}
|
|
1119
|
+
async function writeImage(w, pixels, width, isMain, threads = null) {
|
|
1097
1120
|
const tokens = tokenize(pixels, width);
|
|
1098
1121
|
const cacheBits = isMain ? bestCacheBits(tokens, pixels) : 0;
|
|
1099
1122
|
const stream = withCache(tokens, pixels, cacheBits), counts = histograms(stream, cacheBits);
|
|
@@ -1101,7 +1124,7 @@ function writeImage(w, pixels, width, isMain) {
|
|
|
1101
1124
|
const height = pixels.length / width;
|
|
1102
1125
|
let groups = null, groupCounts = [counts], prefixBits = 0;
|
|
1103
1126
|
if (isMain && pixels.length > 4096) {
|
|
1104
|
-
const r = prefixGroups(stream, width, height, cacheBits, 4);
|
|
1127
|
+
const r = await prefixGroups(stream, width, height, cacheBits, 4, threads);
|
|
1105
1128
|
if (r && r[2] < best) {
|
|
1106
1129
|
[groups, groupCounts] = r;
|
|
1107
1130
|
prefixBits = 4;
|
|
@@ -1117,7 +1140,7 @@ function writeImage(w, pixels, width, isMain) {
|
|
|
1117
1140
|
w.write(groups ? 1 : 0, 1);
|
|
1118
1141
|
if (groups) {
|
|
1119
1142
|
w.write(prefixBits - 2, 3);
|
|
1120
|
-
writeImage(w, Uint32Array.from(groups, g => g << 8), subSize(width, prefixBits), false);
|
|
1143
|
+
await writeImage(w, Uint32Array.from(groups, g => g << 8), subSize(width, prefixBits), false);
|
|
1121
1144
|
}
|
|
1122
1145
|
}
|
|
1123
1146
|
const { ops, values, distances, count } = stream;
|
|
@@ -1154,11 +1177,19 @@ function writeImage(w, pixels, width, isMain) {
|
|
|
1154
1177
|
const subPixels = (a, b) => ((a | 0x00ff00ff) - (b & 0xff00ff00) & 0xff00ff00 | (a | 0xff00ff00) - (b & 0x00ff00ff) & 0x00ff00ff) >>> 0;
|
|
1155
1178
|
const BYTE_COST = Uint8Array.from({ length: 256 }, (_, c) => c < 128 ? c : 256 - c);
|
|
1156
1179
|
const residualCost = (r) => BYTE_COST[r & 255] + BYTE_COST[r >>> 8 & 255] + BYTE_COST[r >>> 16 & 255] + BYTE_COST[r >>> 24];
|
|
1157
|
-
function applyPredictor(p, width, height, bits) {
|
|
1180
|
+
async function applyPredictor(p, width, height, bits, threads) {
|
|
1158
1181
|
const blocksX = subSize(width, bits), blocksY = subSize(height, bits);
|
|
1159
|
-
const
|
|
1160
|
-
|
|
1161
|
-
|
|
1182
|
+
const task = {
|
|
1183
|
+
task: 'vp8l-predictor', p, src: threads ? shared(p) : p.slice(), width, height, bits,
|
|
1184
|
+
modes: new Uint32Array(threads ? new SharedArrayBuffer(4 * blocksX * blocksY) : new ArrayBuffer(4 * blocksX * blocksY)),
|
|
1185
|
+
};
|
|
1186
|
+
await runChunks(threads, task, Math.ceil(blocksY / PREDICTOR_ROWS), predictorChunks(task));
|
|
1187
|
+
return task.modes;
|
|
1188
|
+
}
|
|
1189
|
+
const PREDICTOR_ROWS = 4;
|
|
1190
|
+
export function predictorChunks({ p, src, modes, width, height, bits }) {
|
|
1191
|
+
const blocksX = subSize(width, bits), blocksY = subSize(height, bits), size = 1 << bits;
|
|
1192
|
+
const predicted = new Uint32Array(size);
|
|
1162
1193
|
const predictRow = (mode, x0, x1, y) => {
|
|
1163
1194
|
const row = y * width;
|
|
1164
1195
|
if (y === 0) {
|
|
@@ -1171,32 +1202,33 @@ function applyPredictor(p, width, height, bits) {
|
|
|
1171
1202
|
predicted[x++] = src[row - width];
|
|
1172
1203
|
predictRun(mode, src, row + x, x1 - x, width, predicted, x - x0);
|
|
1173
1204
|
};
|
|
1174
|
-
|
|
1175
|
-
for (let
|
|
1176
|
-
let
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
let
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1205
|
+
return chunk => {
|
|
1206
|
+
for (let by = chunk * PREDICTOR_ROWS; by < Math.min(blocksY, (chunk + 1) * PREDICTOR_ROWS); by++)
|
|
1207
|
+
for (let bx = 0; bx < blocksX; bx++) {
|
|
1208
|
+
let best = 0, bestCost = Infinity;
|
|
1209
|
+
const x0 = bx * size, y0 = by * size, x1 = Math.min(width, x0 + size), y1 = Math.min(height, y0 + size);
|
|
1210
|
+
for (let mode = 0; mode < 14; mode++) {
|
|
1211
|
+
let cost = 0;
|
|
1212
|
+
for (let y = y0; y < y1 && cost < bestCost; y++) {
|
|
1213
|
+
predictRow(mode, x0, x1, y);
|
|
1214
|
+
for (let x = x0, i = y * width + x0; x < x1; x++, i++)
|
|
1215
|
+
cost += residualCost(subPixels(src[i], predicted[x - x0]));
|
|
1216
|
+
}
|
|
1217
|
+
if (cost < bestCost) {
|
|
1218
|
+
bestCost = cost;
|
|
1219
|
+
best = mode;
|
|
1220
|
+
}
|
|
1184
1221
|
}
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
best
|
|
1222
|
+
modes[by * blocksX + bx] = (0xff000000 | best << 8) >>> 0;
|
|
1223
|
+
for (let y = y0; y < y1; y++) {
|
|
1224
|
+
predictRow(best, x0, x1, y);
|
|
1225
|
+
for (let x = x0, i = y * width + x0; x < x1; x++, i++)
|
|
1226
|
+
p[i] = subPixels(src[i], predicted[x - x0]);
|
|
1188
1227
|
}
|
|
1189
1228
|
}
|
|
1190
|
-
|
|
1191
|
-
for (let y = y0; y < y1; y++) {
|
|
1192
|
-
predictRow(best, x0, x1, y);
|
|
1193
|
-
for (let x = x0, i = y * width + x0; x < x1; x++, i++)
|
|
1194
|
-
p[i] = subPixels(src[i], predicted[x - x0]);
|
|
1195
|
-
}
|
|
1196
|
-
}
|
|
1197
|
-
return modes;
|
|
1229
|
+
};
|
|
1198
1230
|
}
|
|
1199
|
-
export function encode(argb, width, height) {
|
|
1231
|
+
export async function encode(argb, width, height, threads = null) {
|
|
1200
1232
|
if (width < 1 || height < 1 || width > 16384 || height > 16384)
|
|
1201
1233
|
throw new RangeError('VP8L: dimensions must be 1-16384');
|
|
1202
1234
|
const colors = new Map();
|
|
@@ -1204,14 +1236,14 @@ export function encode(argb, width, height) {
|
|
|
1204
1236
|
if (!colors.has(argb[i]))
|
|
1205
1237
|
colors.set(argb[i], colors.size);
|
|
1206
1238
|
if (colors.size > 256)
|
|
1207
|
-
return encodeWith(argb, width, height, null);
|
|
1208
|
-
const indexed = encodeWith(argb, width, height, colors);
|
|
1239
|
+
return encodeWith(argb, width, height, null, threads);
|
|
1240
|
+
const indexed = await encodeWith(argb, width, height, colors, threads);
|
|
1209
1241
|
if (colors.size <= 16)
|
|
1210
1242
|
return indexed;
|
|
1211
|
-
const predicted = encodeWith(argb, width, height, null);
|
|
1243
|
+
const predicted = await encodeWith(argb, width, height, null, threads);
|
|
1212
1244
|
return predicted.length < indexed.length ? predicted : indexed;
|
|
1213
1245
|
}
|
|
1214
|
-
function encodeWith(argb, width, height, colors) {
|
|
1246
|
+
async function encodeWith(argb, width, height, colors, threads) {
|
|
1215
1247
|
const w = new BitWriter();
|
|
1216
1248
|
let hasAlpha = false;
|
|
1217
1249
|
for (let i = 0; i < argb.length; i++)
|
|
@@ -1235,7 +1267,7 @@ function encodeWith(argb, width, height, colors) {
|
|
|
1235
1267
|
const deltas = palette.slice();
|
|
1236
1268
|
for (let i = palette.length - 1; i > 0; i--)
|
|
1237
1269
|
deltas[i] = subPixels(palette[i], palette[i - 1]);
|
|
1238
|
-
writeImage(w, deltas, palette.length, false);
|
|
1270
|
+
await writeImage(w, deltas, palette.length, false);
|
|
1239
1271
|
codedWidth = subSize(width, bits);
|
|
1240
1272
|
pixels = new Uint32Array(codedWidth * height);
|
|
1241
1273
|
const perPixel = 8 >> bits;
|
|
@@ -1246,7 +1278,7 @@ function encodeWith(argb, width, height, colors) {
|
|
|
1246
1278
|
}
|
|
1247
1279
|
}
|
|
1248
1280
|
else {
|
|
1249
|
-
pixels = argb.slice();
|
|
1281
|
+
pixels = threads ? shared(argb) : argb.slice();
|
|
1250
1282
|
let redBlue = false;
|
|
1251
1283
|
for (let i = 0; i < pixels.length && !redBlue; i++)
|
|
1252
1284
|
if (pixels[i] & 0x00ff00ff)
|
|
@@ -1263,10 +1295,10 @@ function encodeWith(argb, width, height, colors) {
|
|
|
1263
1295
|
w.write(1, 1);
|
|
1264
1296
|
w.write(0, 2);
|
|
1265
1297
|
w.write(bits - 2, 3);
|
|
1266
|
-
const modes = applyPredictor(pixels, width, height, bits);
|
|
1267
|
-
writeImage(w, modes, subSize(width, bits), false);
|
|
1298
|
+
const modes = await applyPredictor(pixels, width, height, bits, threads);
|
|
1299
|
+
await writeImage(w, modes, subSize(width, bits), false);
|
|
1268
1300
|
}
|
|
1269
1301
|
w.write(0, 1);
|
|
1270
|
-
writeImage(w, pixels, codedWidth, true);
|
|
1302
|
+
await writeImage(w, pixels, codedWidth, true, threads);
|
|
1271
1303
|
return w.finish();
|
|
1272
1304
|
}
|
package/dist/codecs/webp.js
CHANGED
|
@@ -9,6 +9,7 @@ import { rgbaToYUV } from "./vp8-enc-dsp.js";
|
|
|
9
9
|
import { orientation as exifOrientation } from "./exif.js";
|
|
10
10
|
import { MAX_PIXELS, checkPixels } from "../util/limits.js";
|
|
11
11
|
import { finite } from "../util/options.js";
|
|
12
|
+
import { startThreads } from "../util/threads.js";
|
|
12
13
|
const fourCC = (s) => s.charCodeAt(0) | s.charCodeAt(1) << 8 | s.charCodeAt(2) << 16 | s.charCodeAt(3) << 24;
|
|
13
14
|
const RIFF = fourCC('RIFF'), WEBP = fourCC('WEBP');
|
|
14
15
|
const VP8 = fourCC('VP8 '), VP8L = fourCC('VP8L'), VP8X = fourCC('VP8X');
|
|
@@ -260,8 +261,25 @@ const hasAlpha = (rgba) => {
|
|
|
260
261
|
return true;
|
|
261
262
|
return false;
|
|
262
263
|
};
|
|
263
|
-
|
|
264
|
-
|
|
264
|
+
const MIN_THREADED_PIXELS = 1536 << 10, PIXELS_PER_THREAD = 512 << 10;
|
|
265
|
+
export async function encode(rgba, width, height, exif, threads = 1) {
|
|
266
|
+
const n = Math.min(threads, Math.floor(width * height / PIXELS_PER_THREAD));
|
|
267
|
+
const pool = n > 1 && width * height >= MIN_THREADED_PIXELS ? startThreads(n) : null;
|
|
268
|
+
let stream;
|
|
269
|
+
try {
|
|
270
|
+
stream = await vp8l.encode(toARGB(rgba, width * height), width, height, pool);
|
|
271
|
+
}
|
|
272
|
+
catch (e) {
|
|
273
|
+
if (!pool)
|
|
274
|
+
throw e;
|
|
275
|
+
stream = await vp8l.encode(toARGB(rgba, width * height), width, height);
|
|
276
|
+
}
|
|
277
|
+
finally {
|
|
278
|
+
if (pool)
|
|
279
|
+
for (const t of pool)
|
|
280
|
+
t.terminate();
|
|
281
|
+
}
|
|
282
|
+
const bitstream = riffChunk('VP8L', stream);
|
|
265
283
|
if (!exif)
|
|
266
284
|
return riff([bitstream]);
|
|
267
285
|
const vp8x = new Uint8Array(10);
|
|
@@ -282,8 +300,8 @@ export function lossyOptions(quality, effort, sharp, alphaQuality) {
|
|
|
282
300
|
throw new RangeError(`effort must be an integer 0-6, not ${effort}`);
|
|
283
301
|
return { quality: q, method: effort, sharp, alphaQuality: Math.round(a) };
|
|
284
302
|
}
|
|
285
|
-
export function encodeLossy(rgba, width, height, lossy, exif) {
|
|
286
|
-
const { alpha, chunks } = lossyChunks(rgba, width, height, lossy);
|
|
303
|
+
export async function encodeLossy(rgba, width, height, lossy, exif) {
|
|
304
|
+
const { alpha, chunks } = await lossyChunks(rgba, width, height, lossy);
|
|
287
305
|
if (!alpha && !exif)
|
|
288
306
|
return riff(chunks);
|
|
289
307
|
const vp8x = new Uint8Array(10);
|
|
@@ -292,7 +310,7 @@ export function encodeLossy(rgba, width, height, lossy, exif) {
|
|
|
292
310
|
set24(vp8x, 7, height - 1);
|
|
293
311
|
return riff([riffChunk('VP8X', vp8x), ...chunks, ...exif ? [riffChunk('EXIF', exif)] : []]);
|
|
294
312
|
}
|
|
295
|
-
function lossyChunks(rgba, width, height, { quality, method, sharp, alphaQuality }) {
|
|
313
|
+
async function lossyChunks(rgba, width, height, { quality, method, sharp, alphaQuality }) {
|
|
296
314
|
if (width > 16383 || height > 16383)
|
|
297
315
|
throw new RangeError('WebP: lossy images can be at most 16383 pixels wide and high');
|
|
298
316
|
const picture = rgbaToYUV(rgba, width, height, sharp);
|
|
@@ -301,14 +319,14 @@ function lossyChunks(rgba, width, height, { quality, method, sharp, alphaQuality
|
|
|
301
319
|
padded.set(frame);
|
|
302
320
|
const chunks = [riffChunk('VP8 ', padded)];
|
|
303
321
|
if (picture.a)
|
|
304
|
-
chunks.unshift(riffChunk('ALPH', encodeAlpha(picture.a, width, height, method, alphaQuality)));
|
|
322
|
+
chunks.unshift(riffChunk('ALPH', await encodeAlpha(picture.a, width, height, method, alphaQuality)));
|
|
305
323
|
return { alpha: !!picture.a, chunks };
|
|
306
324
|
}
|
|
307
325
|
export function lossyTolerance(quality) {
|
|
308
326
|
const value = Math.sqrt(quality / 100);
|
|
309
327
|
return Math.max(1, Math.round(31 * (1 - value) + value));
|
|
310
328
|
}
|
|
311
|
-
export function encodeAnimation(frames, width, height, plays, exif, lossy) {
|
|
329
|
+
export async function encodeAnimation(frames, width, height, plays, exif, lossy) {
|
|
312
330
|
if (!frames.length)
|
|
313
331
|
throw new Error('WebP: an animation needs at least one frame');
|
|
314
332
|
if (width > 1 << 24 || height > 1 << 24)
|
|
@@ -316,16 +334,19 @@ export function encodeAnimation(frames, width, height, plays, exif, lossy) {
|
|
|
316
334
|
let alpha = false;
|
|
317
335
|
for (const [i, f] of frames.entries())
|
|
318
336
|
alpha ||= needsAlpha(f, i === 0, width, height);
|
|
319
|
-
|
|
337
|
+
const chunks = [];
|
|
338
|
+
for (const f of frames)
|
|
339
|
+
chunks.push(await frameChunk(f, lossy));
|
|
340
|
+
return animationFile(chunks, width, height, plays, alpha, exif);
|
|
320
341
|
}
|
|
321
342
|
export function needsAlpha(f, first, width, height) {
|
|
322
343
|
return (first && (f.x > 0 || f.y > 0 || f.width < width || f.height < height)) || f.dispose || hasAlpha(f.pixels);
|
|
323
344
|
}
|
|
324
|
-
export function frameChunk(f, lossy) {
|
|
345
|
+
export async function frameChunk(f, lossy) {
|
|
325
346
|
if (f.x & 1 || f.y & 1)
|
|
326
347
|
throw new RangeError('WebP: frame offsets have to be even');
|
|
327
|
-
const bitstream = !lossy ? riffChunk('VP8L', vp8l.encode(toARGB(f.pixels, f.width * f.height), f.width, f.height))
|
|
328
|
-
: concat(lossyChunks(f.pixels, f.width, f.height, lossy).chunks);
|
|
348
|
+
const bitstream = !lossy ? riffChunk('VP8L', await vp8l.encode(toARGB(f.pixels, f.width * f.height), f.width, f.height))
|
|
349
|
+
: concat((await lossyChunks(f.pixels, f.width, f.height, lossy)).chunks);
|
|
329
350
|
const header = new Uint8Array(16);
|
|
330
351
|
set24(header, 0, f.x >> 1);
|
|
331
352
|
set24(header, 3, f.y >> 1);
|
package/dist/image.d.ts
CHANGED
|
@@ -92,6 +92,13 @@ export interface WebPEncodeOptions {
|
|
|
92
92
|
* Default: `100`
|
|
93
93
|
*/
|
|
94
94
|
alphaQuality?: number;
|
|
95
|
+
/**
|
|
96
|
+
* For lossless compression, at most this many worker threads search the predictors and prefix codes in parallel
|
|
97
|
+
* (the same file, faster), `1` for none. Images under about 1.5 megapixels, and runtimes whose workers can't share
|
|
98
|
+
* memory (browsers without cross-origin isolation), encode without them. Default: the processors available, at
|
|
99
|
+
* most 8
|
|
100
|
+
*/
|
|
101
|
+
threads?: number;
|
|
95
102
|
/** Metadata to include, see {@link Metadata} */
|
|
96
103
|
metadata?: Metadata;
|
|
97
104
|
}
|
|
@@ -161,6 +168,13 @@ export interface DecodeOptions {
|
|
|
161
168
|
* brighter than white. Default: `'reinhard'`
|
|
162
169
|
*/
|
|
163
170
|
toneMapping?: ToneMapping;
|
|
171
|
+
/**
|
|
172
|
+
* At most this many worker threads decode in parallel (lossy JPEG XLs from 4 megapixels on, one per 2 megapixels
|
|
173
|
+
* at most, lossless ones from 1.5 megapixels and 512 KiB on; AVIFs from 4 megapixels on: the conversion to RGB,
|
|
174
|
+
* one per megapixel, and the tiles of grids), `1` for none. Default: the processors available, at most 8 (1 where
|
|
175
|
+
* workers can't share memory)
|
|
176
|
+
*/
|
|
177
|
+
threads?: number;
|
|
164
178
|
}
|
|
165
179
|
/** Options of {@link Image.encodeJXL} */
|
|
166
180
|
export interface JXLEncodeOptions {
|
|
@@ -175,6 +189,14 @@ export interface JXLEncodeOptions {
|
|
|
175
189
|
* text-like features that repeat (the glyphs of screenshots) once, as patches like `cjxl -e 7`. Default: `1`
|
|
176
190
|
*/
|
|
177
191
|
effort?: number;
|
|
192
|
+
/**
|
|
193
|
+
* At most this many worker threads compress lossily in parallel (the same file, faster: 2.6 times with 8 on a
|
|
194
|
+
* 1920x1080 photo at effort 1, 4.2 times on 3840x2160), `1` for none. Small images (under about 1.5 megapixels at
|
|
195
|
+
* effort 0, 512x512 pixels at 1, 384x384 at 2), and runtimes whose workers can't share memory (browsers without
|
|
196
|
+
* cross-origin isolation) encode without them.
|
|
197
|
+
* Default: the processors available, at most 8
|
|
198
|
+
*/
|
|
199
|
+
threads?: number;
|
|
178
200
|
/** Metadata to include, see {@link Metadata} */
|
|
179
201
|
metadata?: Metadata;
|
|
180
202
|
}
|
|
@@ -188,6 +210,13 @@ export interface AVIFEncodeOptions {
|
|
|
188
210
|
subsampling?: '4:4:4' | '4:2:0';
|
|
189
211
|
/** How hard the encoder searches for the best modes: 0-2, faster to smaller. Default: `1` */
|
|
190
212
|
effort?: number;
|
|
213
|
+
/**
|
|
214
|
+
* At most this many worker threads encode in parallel (the same file, faster: 2.9 times with 8 on a 1920x1080
|
|
215
|
+
* photo at effort 1), `1` for none. Small images (under about 640x640 pixels at effort 0, 448x448 at 1, 320x320
|
|
216
|
+
* at 2), and runtimes whose workers can't share memory (browsers without cross-origin isolation), encode without
|
|
217
|
+
* them. Default: the processors available, at most 8
|
|
218
|
+
*/
|
|
219
|
+
threads?: number;
|
|
191
220
|
/** Metadata to include, see {@link Metadata} */
|
|
192
221
|
metadata?: Metadata;
|
|
193
222
|
}
|
|
@@ -814,7 +843,7 @@ export declare class Image {
|
|
|
814
843
|
* quality
|
|
815
844
|
* @param options See {@link WebPEncodeOptions}
|
|
816
845
|
*/
|
|
817
|
-
encodeWebP({ quality, effort, sharpYuv, alphaQuality, metadata }?: WebPEncodeOptions): Promise<Uint8Array>;
|
|
846
|
+
encodeWebP({ quality, effort, sharpYuv, alphaQuality, threads, metadata }?: WebPEncodeOptions): Promise<Uint8Array>;
|
|
818
847
|
/**
|
|
819
848
|
* Encodes the image as run length encoded TGA (32 bit, or 24 bit if the image is opaque)
|
|
820
849
|
* @param options See {@link EncodeOptions}
|
|
@@ -833,7 +862,7 @@ export declare class Image {
|
|
|
833
862
|
* Encodes the image as AVIF (AV1, with an alpha image if the image has transparency)
|
|
834
863
|
* @param options See {@link AVIFEncodeOptions}
|
|
835
864
|
*/
|
|
836
|
-
encodeAVIF({ quality, alphaQuality, subsampling, effort, metadata }?: AVIFEncodeOptions): Promise<Uint8Array>;
|
|
865
|
+
encodeAVIF({ quality, alphaQuality, subsampling, effort, threads, metadata }?: AVIFEncodeOptions): Promise<Uint8Array>;
|
|
837
866
|
/**
|
|
838
867
|
* Encodes the image as JPEG XL: losslessly (the default), the codestream byte for byte like libjxl's `cjxl -e 1` on
|
|
839
868
|
* CPUs with AVX2 or NEON (its fast lossless mode: the gradient predictor, a palette if the image has at most 511
|
|
@@ -842,7 +871,7 @@ export declare class Image {
|
|
|
842
871
|
* transparency) is lossless either way. The codestream is in the container, with the metadata as Exif
|
|
843
872
|
* @param options See {@link JXLEncodeOptions}
|
|
844
873
|
*/
|
|
845
|
-
encodeJXL({ quality, effort, metadata }?: JXLEncodeOptions): Promise<Uint8Array>;
|
|
874
|
+
encodeJXL({ quality, effort, threads, metadata }?: JXLEncodeOptions): Promise<Uint8Array>;
|
|
846
875
|
/**
|
|
847
876
|
* Encodes the image as the smallest fitting Netpbm format: PBM for black and white images, PGM for greyscale ones,
|
|
848
877
|
* PPM for others, PAM if the image has transparency
|
package/dist/image.js
CHANGED
|
@@ -11,6 +11,12 @@ import * as netpbm from "./codecs/netpbm.js";
|
|
|
11
11
|
import * as hdr from "./codecs/hdr.js";
|
|
12
12
|
import * as avif from "./codecs/avif.js";
|
|
13
13
|
import * as avifEnc from "./codecs/avif-enc.js";
|
|
14
|
+
import { availableThreads } from "./util/threads.js";
|
|
15
|
+
const defaultThreads = () => Math.max(1, Math.min(availableThreads(), 8));
|
|
16
|
+
const checkThreads = (threads) => {
|
|
17
|
+
if (!Number.isInteger(threads) || threads < 1)
|
|
18
|
+
throw new RangeError(`threads must be a positive integer, not ${threads}`);
|
|
19
|
+
};
|
|
14
20
|
import * as jxl from "./codecs/jxl.js";
|
|
15
21
|
import * as quantize from "./codecs/quantize.js";
|
|
16
22
|
import * as textRenderer from "./render/text.js";
|
|
@@ -1026,11 +1032,12 @@ export class Image {
|
|
|
1026
1032
|
const m = resolveMetadata(metadata);
|
|
1027
1033
|
return jpeg.encode(this.__bytes__(), this.width, this.height, finite(quality, 'quality'), { exif: exifData(m), comment: m.comment ? utf8(m.comment) : undefined });
|
|
1028
1034
|
}
|
|
1029
|
-
async encodeWebP({ quality, effort = 2, sharpYuv = false, alphaQuality = 100, metadata = {} } = {}) {
|
|
1035
|
+
async encodeWebP({ quality, effort = 2, sharpYuv = false, alphaQuality = 100, threads = defaultThreads(), metadata = {} } = {}) {
|
|
1036
|
+
checkThreads(threads);
|
|
1030
1037
|
const exif = exifData(resolveMetadata(metadata));
|
|
1031
1038
|
const lossy = webp.lossyOptions(quality, effort, sharpYuv, alphaQuality);
|
|
1032
1039
|
if (!lossy)
|
|
1033
|
-
return webp.encode(this.__bytes__(), this.width, this.height, exif);
|
|
1040
|
+
return webp.encode(this.__bytes__(), this.width, this.height, exif, threads);
|
|
1034
1041
|
return webp.encodeLossy(this.__bytes__(), this.width, this.height, lossy, exif);
|
|
1035
1042
|
}
|
|
1036
1043
|
async encodeTGA({ metadata = {} } = {}) {
|
|
@@ -1043,13 +1050,15 @@ export class Image {
|
|
|
1043
1050
|
async encodeQOI() {
|
|
1044
1051
|
return qoi.encode(this.__bytes__(), this.width, this.height);
|
|
1045
1052
|
}
|
|
1046
|
-
async encodeAVIF({ quality = 60, alphaQuality = 100, subsampling = '4:4:4', effort = 1, metadata = {} } = {}) {
|
|
1053
|
+
async encodeAVIF({ quality = 60, alphaQuality = 100, subsampling = '4:4:4', effort = 1, threads = defaultThreads(), metadata = {} } = {}) {
|
|
1054
|
+
checkThreads(threads);
|
|
1047
1055
|
const exif = exifData(resolveMetadata(metadata));
|
|
1048
|
-
return avifEnc.encode(this.__bytes__(), this.width, this.height, { quality: finite(quality, 'quality'), alphaQuality: finite(alphaQuality, 'alphaQuality'), subsampling, effort, exif });
|
|
1056
|
+
return avifEnc.encode(this.__bytes__(), this.width, this.height, { quality: finite(quality, 'quality'), alphaQuality: finite(alphaQuality, 'alphaQuality'), subsampling, effort, exif, threads });
|
|
1049
1057
|
}
|
|
1050
|
-
async encodeJXL({ quality, effort = 1, metadata = {} } = {}) {
|
|
1058
|
+
async encodeJXL({ quality, effort = 1, threads = defaultThreads(), metadata = {} } = {}) {
|
|
1051
1059
|
if (!Number.isInteger(effort) || effort < 0 || effort > 2)
|
|
1052
1060
|
throw new RangeError(`effort must be an integer 0-2, not ${effort}`);
|
|
1061
|
+
checkThreads(threads);
|
|
1053
1062
|
let distance = 0;
|
|
1054
1063
|
if (quality !== undefined) {
|
|
1055
1064
|
const q = finite(quality, 'quality');
|
|
@@ -1057,13 +1066,14 @@ export class Image {
|
|
|
1057
1066
|
throw new RangeError(`quality must be 0-100, not ${q}`);
|
|
1058
1067
|
distance = q >= 100 ? 0 : q >= 30 ? 0.1 + (100 - q) * 0.09 : 53 / 3000 * q * q - 23 / 20 * q + 25;
|
|
1059
1068
|
}
|
|
1060
|
-
return jxl.encode(this.__bytes__(), this.width, this.height, exifData(resolveMetadata(metadata)), distance, effort);
|
|
1069
|
+
return jxl.encode(this.__bytes__(), this.width, this.height, exifData(resolveMetadata(metadata)), distance, effort, threads);
|
|
1061
1070
|
}
|
|
1062
1071
|
async encodePNM() {
|
|
1063
1072
|
return netpbm.encode(this.__bytes__(), this.width, this.height);
|
|
1064
1073
|
}
|
|
1065
1074
|
static async decode(input, options = {}) {
|
|
1066
|
-
const { applyOrientation = true, applyColorProfile = true, maxPixels = MAX_PIXELS, toneMapping = 'reinhard' } = options;
|
|
1075
|
+
const { applyOrientation = true, applyColorProfile = true, maxPixels = MAX_PIXELS, toneMapping = 'reinhard', threads = defaultThreads() } = options;
|
|
1076
|
+
checkThreads(threads);
|
|
1067
1077
|
if (toneMapping !== 'reinhard' && toneMapping !== 'clamp')
|
|
1068
1078
|
throw new TypeError(`toneMapping must be 'reinhard' or 'clamp', not ${String(toneMapping)}`);
|
|
1069
1079
|
const data = view(input);
|
|
@@ -1087,9 +1097,9 @@ export class Image {
|
|
|
1087
1097
|
return { width, height, pixels };
|
|
1088
1098
|
};
|
|
1089
1099
|
if (ImageType.isAVIF(dataView))
|
|
1090
|
-
return avif.decode(data, maxPixels, toneMapping);
|
|
1100
|
+
return avif.decode(data, maxPixels, toneMapping, threads);
|
|
1091
1101
|
if (ImageType.isJXL(dataView))
|
|
1092
|
-
return jxl.decode(data, maxPixels, toneMapping);
|
|
1102
|
+
return jxl.decode(data, maxPixels, toneMapping, threads);
|
|
1093
1103
|
if (ImageType.isBMP(dataView))
|
|
1094
1104
|
return bitmap(bmp.decode(data, maxPixels));
|
|
1095
1105
|
if (ImageType.isICO(dataView))
|