imagescript 3.14.0 → 3.15.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 +13 -0
- package/dist/animation-encoder.d.ts +1 -1
- package/dist/animation-encoder.js +1 -1
- package/dist/animation.d.ts +1 -1
- package/dist/animation.js +1 -1
- package/dist/codecs/jpeg.js +56 -5
- package/dist/codecs/vp8l.js +65 -27
- package/dist/codecs/webp.js +7 -7
- package/dist/frame.js +4 -0
- package/dist/image.d.ts +5 -2
- package/dist/image.js +11 -5
- package/dist/util/resample.js +203 -61
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,6 +23,19 @@ npm install imagescript # Node.js, Bun and bundlers
|
|
|
23
23
|
deno add jsr:@matmen/imagescript # Deno
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
+
Without a bundler, import it from jsDelivr in the browser:
|
|
27
|
+
|
|
28
|
+
```html
|
|
29
|
+
<script type="module">
|
|
30
|
+
import { Image } from 'https://cdn.jsdelivr.net/npm/imagescript@3/+esm';
|
|
31
|
+
|
|
32
|
+
const image = await Image.decode(await fetch('photo.jpg').then(r => r.arrayBuffer()));
|
|
33
|
+
image.resize(256, Image.RESIZE_AUTO);
|
|
34
|
+
const url = URL.createObjectURL(new Blob([await image.encode()], { type: 'image/png' }));
|
|
35
|
+
document.body.append(Object.assign(document.createElement('img'), { src: url }));
|
|
36
|
+
</script>
|
|
37
|
+
```
|
|
38
|
+
|
|
26
39
|
---
|
|
27
40
|
|
|
28
41
|
### Example
|
|
@@ -17,7 +17,7 @@ export interface AnimationEncoderOptions {
|
|
|
17
17
|
export interface WebPAnimationEncoderOptions extends AnimationEncoderOptions {
|
|
18
18
|
/** Lossy frames (0-100), see {@link WebPAnimationEncodeOptions.quality}. Without it the frames are lossless */
|
|
19
19
|
quality?: number;
|
|
20
|
-
/** How much effort
|
|
20
|
+
/** How much effort the frames take (0-6, lossy and lossless), see {@link WebPEncodeOptions.effort}. Default: `2` */
|
|
21
21
|
effort?: number;
|
|
22
22
|
/** Lossy frames: libwebp's sharp YUV conversion, see {@link WebPEncodeOptions.sharpYuv}. Default: `false` */
|
|
23
23
|
sharpYuv?: boolean;
|
|
@@ -77,7 +77,7 @@ export class AnimationEncoder {
|
|
|
77
77
|
let full = false;
|
|
78
78
|
const add = async (f) => {
|
|
79
79
|
alpha ||= webp.needsAlpha(f, !chunks.length, width, height);
|
|
80
|
-
chunks.push(await webp.frameChunk(f, lossy));
|
|
80
|
+
chunks.push(await webp.frameChunk(f, lossy, effort));
|
|
81
81
|
};
|
|
82
82
|
return new AnimationEncoder(width, height, {
|
|
83
83
|
optimize: { replace: true, even: true, alpha: 'frame', tolerance: lossy ? webp.lossyTolerance(lossy.quality) : undefined },
|
package/dist/animation.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ export interface WebPAnimationEncodeOptions extends AnimationEncodeOptions {
|
|
|
42
42
|
* up to 3 per channel at quality 85, 1 at 100)
|
|
43
43
|
*/
|
|
44
44
|
quality?: number;
|
|
45
|
-
/** How much effort
|
|
45
|
+
/** How much effort the frames take (0-6, lossy and lossless), see {@link WebPEncodeOptions.effort}. Default: `2` */
|
|
46
46
|
effort?: number;
|
|
47
47
|
/** Lossy frames: libwebp's sharp YUV conversion, see {@link WebPEncodeOptions.sharpYuv}. Default: `false` */
|
|
48
48
|
sharpYuv?: boolean;
|
package/dist/animation.js
CHANGED
|
@@ -134,7 +134,7 @@ export class Animation extends Array {
|
|
|
134
134
|
const lossy = webp.lossyOptions(quality, effort, sharpYuv, alphaQuality);
|
|
135
135
|
const tolerance = lossy ? webp.lossyTolerance(lossy.quality) : undefined;
|
|
136
136
|
const frames = optimize ? this.__optimized__({ replace: true, even: true, alpha: 'frame', tolerance }) : this.__webpFrames__();
|
|
137
|
-
return webp.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), exifData(resolveMetadata(metadata)), lossy);
|
|
137
|
+
return webp.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), exifData(resolveMetadata(metadata)), lossy, effort);
|
|
138
138
|
}
|
|
139
139
|
__webpFrames__() {
|
|
140
140
|
const width = this.width, height = this.height;
|
package/dist/codecs/jpeg.js
CHANGED
|
@@ -187,7 +187,9 @@ function createFrame(progressive, height, width, componentData) {
|
|
|
187
187
|
blocksPerColumn: Math.ceil(componentHeight / 8),
|
|
188
188
|
blocksPerLineForMcu,
|
|
189
189
|
blocksPerColumnForMcu,
|
|
190
|
-
coefficients: new Int16Array(blocksPerLineForMcu * blocksPerColumnForMcu * 64),
|
|
190
|
+
coefficients: new Int16Array(blocksPerLineForMcu * (progressive ? blocksPerColumnForMcu : c.v) * 64),
|
|
191
|
+
plane: progressive ? undefined : new Uint8Array(blocksPerLineForMcu * 8 * blocksPerColumnForMcu * 8),
|
|
192
|
+
done: progressive ? undefined : new Uint8Array(blocksPerColumnForMcu),
|
|
191
193
|
pred: 0
|
|
192
194
|
};
|
|
193
195
|
});
|
|
@@ -374,6 +376,15 @@ function decodeScan(data, offset, frame, components, restartInterval, ss, se, ah
|
|
|
374
376
|
? components[0].blocksPerLine * components[0].blocksPerColumn
|
|
375
377
|
: frame.mcusPerLine * frame.mcusPerColumn;
|
|
376
378
|
const interval = restartInterval || total;
|
|
379
|
+
const sequential = !frame.progressive;
|
|
380
|
+
let pending = -1;
|
|
381
|
+
const finish = () => {
|
|
382
|
+
if (pending < 0)
|
|
383
|
+
return;
|
|
384
|
+
for (const component of components)
|
|
385
|
+
render(component, single ? pending : pending * component.v, single ? 1 : component.v);
|
|
386
|
+
pending = -1;
|
|
387
|
+
};
|
|
377
388
|
for (const component of components)
|
|
378
389
|
component.pred = 0;
|
|
379
390
|
for (let unit = 0; unit < total;) {
|
|
@@ -382,18 +393,29 @@ function decodeScan(data, offset, frame, components, restartInterval, ss, se, ah
|
|
|
382
393
|
const component = components[0];
|
|
383
394
|
const row = unit / component.blocksPerLine | 0;
|
|
384
395
|
const column = unit % component.blocksPerLine;
|
|
385
|
-
|
|
396
|
+
if (sequential) {
|
|
397
|
+
pending = row;
|
|
398
|
+
decodeBlock(component, column * 64);
|
|
399
|
+
if (column === component.blocksPerLine - 1)
|
|
400
|
+
finish();
|
|
401
|
+
}
|
|
402
|
+
else
|
|
403
|
+
decodeBlock(component, (row * component.blocksPerLineForMcu + column) * 64);
|
|
386
404
|
}
|
|
387
405
|
else {
|
|
388
406
|
const mcuRow = unit / frame.mcusPerLine | 0;
|
|
389
407
|
const mcuColumn = unit % frame.mcusPerLine;
|
|
408
|
+
if (sequential)
|
|
409
|
+
pending = mcuRow;
|
|
390
410
|
for (const component of components)
|
|
391
411
|
for (let v = 0; v < component.v; v++)
|
|
392
412
|
for (let h = 0; h < component.h; h++) {
|
|
393
|
-
const row = mcuRow * component.v + v;
|
|
413
|
+
const row = sequential ? v : mcuRow * component.v + v;
|
|
394
414
|
const column = mcuColumn * component.h + h;
|
|
395
415
|
decodeBlock(component, (row * component.blocksPerLineForMcu + column) * 64);
|
|
396
416
|
}
|
|
417
|
+
if (sequential && mcuColumn === frame.mcusPerLine - 1)
|
|
418
|
+
finish();
|
|
397
419
|
}
|
|
398
420
|
}
|
|
399
421
|
if (unit >= total)
|
|
@@ -401,19 +423,40 @@ function decodeScan(data, offset, frame, components, restartInterval, ss, se, ah
|
|
|
401
423
|
bits = 0;
|
|
402
424
|
buffer = 0;
|
|
403
425
|
while (pos + 1 < data.length && !(data[pos] === 0xff && data[pos + 1] >= 0xd0 && data[pos + 1] <= 0xd7)) {
|
|
404
|
-
if (data[pos] === 0xff && data[pos + 1] !== 0x00 && data[pos + 1] !== 0xff)
|
|
426
|
+
if (data[pos] === 0xff && data[pos + 1] !== 0x00 && data[pos + 1] !== 0xff) {
|
|
427
|
+
finish();
|
|
405
428
|
return pos;
|
|
429
|
+
}
|
|
406
430
|
pos++;
|
|
407
431
|
}
|
|
408
|
-
if (pos + 1 >= data.length)
|
|
432
|
+
if (pos + 1 >= data.length) {
|
|
433
|
+
finish();
|
|
409
434
|
return pos;
|
|
435
|
+
}
|
|
410
436
|
pos += 2;
|
|
411
437
|
eobrun = 0;
|
|
412
438
|
for (const component of components)
|
|
413
439
|
component.pred = 0;
|
|
414
440
|
}
|
|
441
|
+
finish();
|
|
415
442
|
return pos;
|
|
416
443
|
}
|
|
444
|
+
function render(component, row, count) {
|
|
445
|
+
const { plane, done, coefficients, blocksPerLineForMcu: line } = component;
|
|
446
|
+
if (!plane || !done)
|
|
447
|
+
return;
|
|
448
|
+
const stride = line * 8, workspace = new Int32Array(64);
|
|
449
|
+
const quantization = component.quantization || new Int32Array(64);
|
|
450
|
+
for (let r = 0; r < count; r++) {
|
|
451
|
+
const blockRow = row + r;
|
|
452
|
+
if (blockRow < component.blocksPerColumn) {
|
|
453
|
+
for (let column = 0; column < component.blocksPerLine; column++)
|
|
454
|
+
idct(coefficients, (r * line + column) * 64, quantization, workspace, plane, blockRow * 8 * stride + column * 8, stride);
|
|
455
|
+
done[blockRow] = 1;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
coefficients.fill(0, 0, count * line * 64);
|
|
459
|
+
}
|
|
417
460
|
const CONST_BITS = 13;
|
|
418
461
|
const PASS1_BITS = 2;
|
|
419
462
|
const FIX_0_298631336 = 2446;
|
|
@@ -527,6 +570,14 @@ function idct(coefficients, offset, quantization, workspace, out, outOffset, str
|
|
|
527
570
|
}
|
|
528
571
|
function buildPlane(component) {
|
|
529
572
|
const stride = component.blocksPerLineForMcu * 8;
|
|
573
|
+
if (component.plane && component.done) {
|
|
574
|
+
const { plane, done } = component;
|
|
575
|
+
for (let row = 0; row < component.blocksPerColumn; row++)
|
|
576
|
+
if (!done[row])
|
|
577
|
+
for (let y = row * 8; y < row * 8 + 8; y++)
|
|
578
|
+
plane.fill(128, y * stride, y * stride + component.blocksPerLine * 8);
|
|
579
|
+
return { plane, stride };
|
|
580
|
+
}
|
|
530
581
|
const plane = new Uint8Array(stride * component.blocksPerColumnForMcu * 8);
|
|
531
582
|
const workspace = new Int32Array(64);
|
|
532
583
|
const quantization = component.quantization || new Int32Array(64);
|
package/dist/codecs/vp8l.js
CHANGED
|
@@ -732,7 +732,13 @@ function prefixEncode(value) {
|
|
|
732
732
|
const second = d >> h - 1 & 1;
|
|
733
733
|
return { symbol: 2 * h + second, bits: h - 1, extra: d & (1 << h - 1) - 1 };
|
|
734
734
|
}
|
|
735
|
-
|
|
735
|
+
const ALL_MODES = Array.from({ length: 14 }, (_, i) => i);
|
|
736
|
+
export const TUNING = [
|
|
737
|
+
{ chain: 4, predictedChain: 0, lazy: false, cacheSearch: false, groups: false, colorTransform: false, analyze: true, modes: [1, 2, 11], tryPredicted: false },
|
|
738
|
+
{ chain: 8, nearOnly: true, lazy: true, cacheSearch: true, groups: true, colorTransform: false, analyze: true, modes: ALL_MODES, tryPredicted: false },
|
|
739
|
+
{ chain: 24, nearOnly: true, lazy: true, cacheSearch: true, groups: true, colorTransform: true, analyze: true, modes: ALL_MODES, tryPredicted: true },
|
|
740
|
+
];
|
|
741
|
+
function tokenize(pixels, width, tuning = TUNING[2]) {
|
|
736
742
|
const n = pixels.length;
|
|
737
743
|
const ops = new Uint8Array(n), values = new Uint32Array(n), distances = new Uint32Array(n);
|
|
738
744
|
let count = 0;
|
|
@@ -743,7 +749,7 @@ function tokenize(pixels, width) {
|
|
|
743
749
|
shortCodes.set(d, c + 1);
|
|
744
750
|
}
|
|
745
751
|
const distanceCode = (d) => shortCodes.get(d) ?? d + 120;
|
|
746
|
-
const HASH_BITS = 15, CHAIN =
|
|
752
|
+
const HASH_BITS = 15, CHAIN = tuning.chain, MAX_LENGTH = 4096, MIN_LENGTH = 3, WINDOW = (1 << 20) - 120;
|
|
747
753
|
const head = new Int32Array(1 << HASH_BITS).fill(-1);
|
|
748
754
|
const chain = new Int32Array(n);
|
|
749
755
|
const hashes = new Int32Array(n);
|
|
@@ -801,7 +807,7 @@ function tokenize(pixels, width) {
|
|
|
801
807
|
for (let i = 0; i < n;) {
|
|
802
808
|
insertUpTo(i);
|
|
803
809
|
find(i);
|
|
804
|
-
if (bestLength >= MIN_LENGTH && bestLength < MAX_LENGTH && i + 1 < n) {
|
|
810
|
+
if (tuning.lazy && bestLength >= MIN_LENGTH && bestLength < MAX_LENGTH && i + 1 < n) {
|
|
805
811
|
const length = bestLength, distance = bestDistance;
|
|
806
812
|
insertUpTo(i + 1);
|
|
807
813
|
find(i + 1);
|
|
@@ -933,6 +939,15 @@ function bestCacheBits(stream, pixels) {
|
|
|
933
939
|
}
|
|
934
940
|
return best;
|
|
935
941
|
}
|
|
942
|
+
function streamBits(stream, pixels) {
|
|
943
|
+
const cacheBits = 10;
|
|
944
|
+
let bits = entropyBits(histograms(withCache(stream, pixels, cacheBits), cacheBits));
|
|
945
|
+
const { ops, values, distances, count } = stream;
|
|
946
|
+
for (let k = 0; k < count; k++)
|
|
947
|
+
if (ops[k] === 1)
|
|
948
|
+
bits += prefixEncode(values[k]).bits + prefixEncode(distances[k]).bits;
|
|
949
|
+
return bits;
|
|
950
|
+
}
|
|
936
951
|
function entropyBits(counts) {
|
|
937
952
|
let bits = 0;
|
|
938
953
|
for (const c of counts) {
|
|
@@ -1118,14 +1133,34 @@ export function costChunks({ entries, starts, used, logs, costs, nearest }) {
|
|
|
1118
1133
|
}
|
|
1119
1134
|
};
|
|
1120
1135
|
}
|
|
1121
|
-
async function writeImage(w, pixels, width, isMain, threads = null) {
|
|
1122
|
-
|
|
1123
|
-
|
|
1136
|
+
async function writeImage(w, pixels, width, isMain, threads = null, tuning = TUNING[2]) {
|
|
1137
|
+
let chain = tuning.chain, tokens = null;
|
|
1138
|
+
if (isMain && tuning.nearOnly && chain) {
|
|
1139
|
+
const near = { ...tuning, chain: 0 };
|
|
1140
|
+
if (pixels.length <= 1 << 20) {
|
|
1141
|
+
const a = tokenize(pixels, width, near), b = tokenize(pixels, width, tuning);
|
|
1142
|
+
tokens = streamBits(a, pixels) < streamBits(b, pixels) ? a : b;
|
|
1143
|
+
}
|
|
1144
|
+
else {
|
|
1145
|
+
const height = pixels.length / width, rows = Math.max(8, Math.floor(height / 24));
|
|
1146
|
+
let nearBits = 0, farBits = 0;
|
|
1147
|
+
for (let k = 0; k < 3; k++) {
|
|
1148
|
+
const top = Math.max(0, Math.min(height - rows, Math.floor((k + 0.5) * height / 3 - rows / 2)));
|
|
1149
|
+
const band = pixels.subarray(top * width, (top + rows) * width);
|
|
1150
|
+
nearBits += streamBits(tokenize(band, width, near), band);
|
|
1151
|
+
farBits += streamBits(tokenize(band, width, tuning), band);
|
|
1152
|
+
}
|
|
1153
|
+
if (nearBits < farBits)
|
|
1154
|
+
chain = 0;
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
tokens ??= tokenize(pixels, width, chain === tuning.chain ? tuning : { ...tuning, chain });
|
|
1158
|
+
const cacheBits = !isMain ? 0 : tuning.cacheSearch ? bestCacheBits(tokens, pixels) : pixels.length > 4096 ? 10 : 0;
|
|
1124
1159
|
const stream = withCache(tokens, pixels, cacheBits), counts = histograms(stream, cacheBits);
|
|
1125
1160
|
const best = entropyBits(counts);
|
|
1126
1161
|
const height = pixels.length / width;
|
|
1127
1162
|
let groups = null, groupCounts = [counts], prefixBits = 0;
|
|
1128
|
-
if (isMain && pixels.length > 4096) {
|
|
1163
|
+
if (isMain && tuning.groups && pixels.length > 4096) {
|
|
1129
1164
|
const r = await prefixGroups(stream, width, height, cacheBits, 4, threads);
|
|
1130
1165
|
if (r && r[2] < best) {
|
|
1131
1166
|
[groups, groupCounts] = r;
|
|
@@ -1142,7 +1177,7 @@ async function writeImage(w, pixels, width, isMain, threads = null) {
|
|
|
1142
1177
|
w.write(groups ? 1 : 0, 1);
|
|
1143
1178
|
if (groups) {
|
|
1144
1179
|
w.write(prefixBits - 2, 3);
|
|
1145
|
-
await writeImage(w, Uint32Array.from(groups, g => g << 8), subSize(width, prefixBits), false);
|
|
1180
|
+
await writeImage(w, Uint32Array.from(groups, g => g << 8), subSize(width, prefixBits), false, null, tuning);
|
|
1146
1181
|
}
|
|
1147
1182
|
}
|
|
1148
1183
|
const { ops, values, distances, count } = stream;
|
|
@@ -1375,17 +1410,17 @@ function analyze(argb, width, height, bits) {
|
|
|
1375
1410
|
const best = entropy.indexOf(Math.min(...entropy));
|
|
1376
1411
|
return { predict: best === 1 || best === 3, subtractGreen: best >= 2 };
|
|
1377
1412
|
}
|
|
1378
|
-
async function applyPredictor(p, width, height, bits, threads) {
|
|
1413
|
+
async function applyPredictor(p, width, height, bits, threads, candidates = ALL_MODES) {
|
|
1379
1414
|
const blocksX = subSize(width, bits), blocksY = subSize(height, bits);
|
|
1380
1415
|
const task = {
|
|
1381
|
-
task: 'vp8l-predictor', p, src: threads ? shared(p) : p.slice(), width, height, bits,
|
|
1416
|
+
task: 'vp8l-predictor', p, src: threads ? shared(p) : p.slice(), width, height, bits, candidates,
|
|
1382
1417
|
modes: new Uint32Array(threads ? new SharedArrayBuffer(4 * blocksX * blocksY) : new ArrayBuffer(4 * blocksX * blocksY)),
|
|
1383
1418
|
};
|
|
1384
1419
|
await runChunks(threads, task, Math.ceil(blocksY / PREDICTOR_ROWS), predictorChunks(task));
|
|
1385
1420
|
return task.modes;
|
|
1386
1421
|
}
|
|
1387
1422
|
const PREDICTOR_ROWS = 4;
|
|
1388
|
-
export function predictorChunks({ p, src, modes, width, height, bits }) {
|
|
1423
|
+
export function predictorChunks({ p, src, modes, width, height, bits, candidates }) {
|
|
1389
1424
|
const blocksX = subSize(width, bits), blocksY = subSize(height, bits), size = 1 << bits;
|
|
1390
1425
|
const predicted = new Uint32Array(size);
|
|
1391
1426
|
const predictRow = (mode, x0, x1, y) => {
|
|
@@ -1403,9 +1438,9 @@ export function predictorChunks({ p, src, modes, width, height, bits }) {
|
|
|
1403
1438
|
return chunk => {
|
|
1404
1439
|
for (let by = chunk * PREDICTOR_ROWS; by < Math.min(blocksY, (chunk + 1) * PREDICTOR_ROWS); by++)
|
|
1405
1440
|
for (let bx = 0; bx < blocksX; bx++) {
|
|
1406
|
-
let best = 0, bestCost = Infinity;
|
|
1441
|
+
let best = candidates[0], bestCost = Infinity;
|
|
1407
1442
|
const x0 = bx * size, y0 = by * size, x1 = Math.min(width, x0 + size), y1 = Math.min(height, y0 + size);
|
|
1408
|
-
for (
|
|
1443
|
+
for (const mode of candidates) {
|
|
1409
1444
|
let cost = 0;
|
|
1410
1445
|
for (let y = y0; y < y1 && cost < bestCost; y++) {
|
|
1411
1446
|
predictRow(mode, x0, x1, y);
|
|
@@ -1426,22 +1461,23 @@ export function predictorChunks({ p, src, modes, width, height, bits }) {
|
|
|
1426
1461
|
}
|
|
1427
1462
|
};
|
|
1428
1463
|
}
|
|
1429
|
-
export async function encode(argb, width, height, threads = null) {
|
|
1464
|
+
export async function encode(argb, width, height, threads = null, effort = 2) {
|
|
1430
1465
|
if (width < 1 || height < 1 || width > 16384 || height > 16384)
|
|
1431
1466
|
throw new RangeError('VP8L: dimensions must be 1-16384');
|
|
1467
|
+
const tuning = TUNING[Math.min(2, effort)];
|
|
1432
1468
|
const colors = new Map();
|
|
1433
1469
|
for (let i = 0; i < argb.length && colors.size <= 256; i++)
|
|
1434
1470
|
if (!colors.has(argb[i]))
|
|
1435
1471
|
colors.set(argb[i], colors.size);
|
|
1436
1472
|
if (colors.size > 256)
|
|
1437
|
-
return encodeWith(argb, width, height, null, threads);
|
|
1438
|
-
const indexed = await encodeWith(argb, width, height, colors, threads);
|
|
1439
|
-
if (colors.size <= 16)
|
|
1473
|
+
return encodeWith(argb, width, height, null, threads, tuning);
|
|
1474
|
+
const indexed = await encodeWith(argb, width, height, colors, threads, tuning);
|
|
1475
|
+
if (colors.size <= 16 || !tuning.tryPredicted)
|
|
1440
1476
|
return indexed;
|
|
1441
|
-
const predicted = await encodeWith(argb, width, height, null, threads);
|
|
1477
|
+
const predicted = await encodeWith(argb, width, height, null, threads, tuning);
|
|
1442
1478
|
return predicted.length < indexed.length ? predicted : indexed;
|
|
1443
1479
|
}
|
|
1444
|
-
async function encodeWith(argb, width, height, colors, threads) {
|
|
1480
|
+
async function encodeWith(argb, width, height, colors, threads, tuning) {
|
|
1445
1481
|
const w = new BitWriter();
|
|
1446
1482
|
let hasAlpha = false;
|
|
1447
1483
|
for (let i = 0; i < argb.length; i++)
|
|
@@ -1454,7 +1490,7 @@ async function encodeWith(argb, width, height, colors, threads) {
|
|
|
1454
1490
|
w.write(height - 1, 14);
|
|
1455
1491
|
w.write(hasAlpha ? 1 : 0, 1);
|
|
1456
1492
|
w.write(0, 3);
|
|
1457
|
-
let pixels, codedWidth = width;
|
|
1493
|
+
let pixels, codedWidth = width, predicted = false;
|
|
1458
1494
|
if (colors) {
|
|
1459
1495
|
const palette = Uint32Array.from([...colors.keys()].sort((a, b) => a - b));
|
|
1460
1496
|
palette.forEach((c, i) => colors.set(c, i));
|
|
@@ -1465,7 +1501,7 @@ async function encodeWith(argb, width, height, colors, threads) {
|
|
|
1465
1501
|
const deltas = palette.slice();
|
|
1466
1502
|
for (let i = palette.length - 1; i > 0; i--)
|
|
1467
1503
|
deltas[i] = subPixels(palette[i], palette[i - 1]);
|
|
1468
|
-
await writeImage(w, deltas, palette.length, false);
|
|
1504
|
+
await writeImage(w, deltas, palette.length, false, null, tuning);
|
|
1469
1505
|
codedWidth = subSize(width, bits);
|
|
1470
1506
|
pixels = new Uint32Array(codedWidth * height);
|
|
1471
1507
|
const perPixel = 8 >> bits;
|
|
@@ -1481,7 +1517,7 @@ async function encodeWith(argb, width, height, colors, threads) {
|
|
|
1481
1517
|
for (let i = 0; i < pixels.length && !redBlue; i++)
|
|
1482
1518
|
if (pixels[i] & 0x00ff00ff)
|
|
1483
1519
|
redBlue = true;
|
|
1484
|
-
const { predict, subtractGreen } = analyze(argb, width, height, 4);
|
|
1520
|
+
const { predict, subtractGreen } = tuning.analyze ? analyze(argb, width, height, 4) : { predict: true, subtractGreen: true };
|
|
1485
1521
|
if (redBlue && subtractGreen) {
|
|
1486
1522
|
w.write(1, 1);
|
|
1487
1523
|
w.write(2, 2);
|
|
@@ -1492,12 +1528,13 @@ async function encodeWith(argb, width, height, colors, threads) {
|
|
|
1492
1528
|
}
|
|
1493
1529
|
const bits = 4;
|
|
1494
1530
|
if (predict) {
|
|
1531
|
+
predicted = true;
|
|
1495
1532
|
w.write(1, 1);
|
|
1496
1533
|
w.write(0, 2);
|
|
1497
1534
|
w.write(bits - 2, 3);
|
|
1498
|
-
const modes = await applyPredictor(pixels, width, height, bits, threads);
|
|
1499
|
-
await writeImage(w, modes, subSize(width, bits), false);
|
|
1500
|
-
if (redBlue) {
|
|
1535
|
+
const modes = await applyPredictor(pixels, width, height, bits, threads, tuning.modes);
|
|
1536
|
+
await writeImage(w, modes, subSize(width, bits), false, null, tuning);
|
|
1537
|
+
if (redBlue && tuning.colorTransform) {
|
|
1501
1538
|
const colorBits = 5, before = pixels.slice(), multipliers = colorTransform(pixels, width, height, colorBits);
|
|
1502
1539
|
const changed = multipliers.reduce((n, m) => n + (m !== 0xff000000 ? 1 : 0), 0);
|
|
1503
1540
|
if (changed && redBlueBits(pixels) + 12 * changed >= redBlueBits(before))
|
|
@@ -1506,12 +1543,13 @@ async function encodeWith(argb, width, height, colors, threads) {
|
|
|
1506
1543
|
w.write(1, 1);
|
|
1507
1544
|
w.write(1, 2);
|
|
1508
1545
|
w.write(colorBits - 2, 3);
|
|
1509
|
-
await writeImage(w, multipliers, subSize(width, colorBits), false);
|
|
1546
|
+
await writeImage(w, multipliers, subSize(width, colorBits), false, null, tuning);
|
|
1510
1547
|
}
|
|
1511
1548
|
}
|
|
1512
1549
|
}
|
|
1513
1550
|
}
|
|
1514
1551
|
w.write(0, 1);
|
|
1515
|
-
|
|
1552
|
+
const chain = predicted && tuning.predictedChain !== undefined ? tuning.predictedChain : tuning.chain;
|
|
1553
|
+
await writeImage(w, pixels, codedWidth, true, threads, { ...tuning, chain });
|
|
1516
1554
|
return w.finish();
|
|
1517
1555
|
}
|
package/dist/codecs/webp.js
CHANGED
|
@@ -262,17 +262,17 @@ const hasAlpha = (rgba) => {
|
|
|
262
262
|
return false;
|
|
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) {
|
|
265
|
+
export async function encode(rgba, width, height, exif, threads = 1, effort = 2) {
|
|
266
266
|
const n = Math.min(threads, Math.floor(width * height / PIXELS_PER_THREAD));
|
|
267
267
|
const pool = n > 1 && width * height >= MIN_THREADED_PIXELS ? startThreads(n) : null;
|
|
268
268
|
let stream;
|
|
269
269
|
try {
|
|
270
|
-
stream = await vp8l.encode(toARGB(rgba, width * height), width, height, pool);
|
|
270
|
+
stream = await vp8l.encode(toARGB(rgba, width * height), width, height, pool, effort);
|
|
271
271
|
}
|
|
272
272
|
catch (e) {
|
|
273
273
|
if (!pool)
|
|
274
274
|
throw e;
|
|
275
|
-
stream = await vp8l.encode(toARGB(rgba, width * height), width, height);
|
|
275
|
+
stream = await vp8l.encode(toARGB(rgba, width * height), width, height, null, effort);
|
|
276
276
|
}
|
|
277
277
|
finally {
|
|
278
278
|
if (pool)
|
|
@@ -321,7 +321,7 @@ export function lossyTolerance(quality) {
|
|
|
321
321
|
const value = Math.sqrt(quality / 100);
|
|
322
322
|
return Math.max(1, Math.round(31 * (1 - value) + value));
|
|
323
323
|
}
|
|
324
|
-
export async function encodeAnimation(frames, width, height, plays, exif, lossy) {
|
|
324
|
+
export async function encodeAnimation(frames, width, height, plays, exif, lossy, effort = 2) {
|
|
325
325
|
if (!frames.length)
|
|
326
326
|
throw new Error('WebP: an animation needs at least one frame');
|
|
327
327
|
if (width > 1 << 24 || height > 1 << 24)
|
|
@@ -331,16 +331,16 @@ export async function encodeAnimation(frames, width, height, plays, exif, lossy)
|
|
|
331
331
|
alpha ||= needsAlpha(f, i === 0, width, height);
|
|
332
332
|
const chunks = [];
|
|
333
333
|
for (const f of frames)
|
|
334
|
-
chunks.push(await frameChunk(f, lossy));
|
|
334
|
+
chunks.push(await frameChunk(f, lossy, effort));
|
|
335
335
|
return animationFile(chunks, width, height, plays, alpha, exif);
|
|
336
336
|
}
|
|
337
337
|
export function needsAlpha(f, first, width, height) {
|
|
338
338
|
return (first && (f.x > 0 || f.y > 0 || f.width < width || f.height < height)) || f.dispose || hasAlpha(f.pixels);
|
|
339
339
|
}
|
|
340
|
-
export async function frameChunk(f, lossy) {
|
|
340
|
+
export async function frameChunk(f, lossy, effort = 2) {
|
|
341
341
|
if (f.x & 1 || f.y & 1)
|
|
342
342
|
throw new RangeError('WebP: frame offsets have to be even');
|
|
343
|
-
const bitstream = !lossy ? riffChunk('VP8L', await vp8l.encode(toARGB(f.pixels, f.width * f.height), f.width, f.height))
|
|
343
|
+
const bitstream = !lossy ? riffChunk('VP8L', await vp8l.encode(toARGB(f.pixels, f.width * f.height), f.width, f.height, null, effort))
|
|
344
344
|
: concat((await lossyChunks(f.pixels, f.width, f.height, lossy)).chunks);
|
|
345
345
|
const header = new Uint8Array(16);
|
|
346
346
|
set24(header, 0, f.x >> 1);
|
package/dist/frame.js
CHANGED
|
@@ -75,4 +75,8 @@ export class Frame extends Image {
|
|
|
75
75
|
this.yOffset = Math.round(this.yOffset * this.height / originalHeight);
|
|
76
76
|
return this;
|
|
77
77
|
}
|
|
78
|
+
__rescaled__(width, height) {
|
|
79
|
+
this.xOffset = Math.round(this.xOffset * width / this.width);
|
|
80
|
+
this.yOffset = Math.round(this.yOffset * height / this.height);
|
|
81
|
+
}
|
|
78
82
|
}
|
package/dist/image.d.ts
CHANGED
|
@@ -74,9 +74,12 @@ export interface WebPEncodeOptions {
|
|
|
74
74
|
*/
|
|
75
75
|
quality?: number;
|
|
76
76
|
/**
|
|
77
|
-
* How much effort
|
|
77
|
+
* How much effort compression takes (0-6): higher is smaller but slower. Lossy (libwebp's method): 0-2 pick the
|
|
78
78
|
* predictions by how close they are, 3 and up also by how many bits they take, 5 and 6 also optimize the
|
|
79
|
-
* quantized values
|
|
79
|
+
* quantized values; the default `2` takes about a quarter of the time of 4 (cwebp's default) for files about 5%
|
|
80
|
+
* larger. Lossless: 0 is a fast mode (photos in about a fifth of the time, graphics in about a third, files about
|
|
81
|
+
* 6-8% larger), 1 takes about two thirds of the time for files up to 2% larger, 2 and up compress the most.
|
|
82
|
+
* Default: `2`
|
|
80
83
|
*/
|
|
81
84
|
effort?: number;
|
|
82
85
|
/**
|
package/dist/image.js
CHANGED
|
@@ -285,10 +285,16 @@ export class Image {
|
|
|
285
285
|
width = Math.floor(width);
|
|
286
286
|
height = Math.floor(height);
|
|
287
287
|
const scale = Math.max(width / this.width, height / this.height);
|
|
288
|
-
|
|
288
|
+
const resizedWidth = Math.max(width, Math.round(this.width * scale)), resizedHeight = Math.max(height, Math.round(this.height * scale));
|
|
289
|
+
if (options.mode !== Image.RESIZE_NEAREST_NEIGHBOR && width >= 1 && height >= 1) {
|
|
290
|
+
const region = { x: ~~((resizedWidth - width) / 2), y: ~~((resizedHeight - height) / 2), width, height };
|
|
291
|
+
this.__rescaled__?.(resizedWidth, resizedHeight);
|
|
292
|
+
return this.__apply__(this.__resize__(resizedWidth, resizedHeight, options.mode, options.fast, region));
|
|
293
|
+
}
|
|
294
|
+
this.resize(resizedWidth, resizedHeight, options);
|
|
289
295
|
return this.crop((this.width - width) / 2, (this.height - height) / 2, width, height);
|
|
290
296
|
}
|
|
291
|
-
__resize__(width, height, mode = Image.RESIZE_BILINEAR, fast = true) {
|
|
297
|
+
__resize__(width, height, mode = Image.RESIZE_BILINEAR, fast = true, region) {
|
|
292
298
|
if (width === Image.RESIZE_AUTO && height === Image.RESIZE_AUTO)
|
|
293
299
|
throw new Error('RESIZE_AUTO can only be used for either width or height, not for both');
|
|
294
300
|
else if (width === Image.RESIZE_AUTO)
|
|
@@ -306,8 +312,8 @@ export class Image {
|
|
|
306
312
|
const filter = mode === Image.RESIZE_BILINEAR ? TRIANGLE : mode === Image.RESIZE_BICUBIC ? CUBIC : mode === Image.RESIZE_AREA ? BOX : null;
|
|
307
313
|
if (!filter)
|
|
308
314
|
throw new Error('Invalid resize mode');
|
|
309
|
-
const image = new Image(width, height);
|
|
310
|
-
resample(this.bitmap, this.width, this.height, width, height, filter, image.bitmap, fast);
|
|
315
|
+
const image = new Image(region?.width ?? width, region?.height ?? height);
|
|
316
|
+
resample(this.bitmap, this.width, this.height, width, height, filter, image.bitmap, fast, region);
|
|
311
317
|
return image;
|
|
312
318
|
}
|
|
313
319
|
__resize_nearest_neighbor__(width, height) {
|
|
@@ -1036,7 +1042,7 @@ export class Image {
|
|
|
1036
1042
|
const exif = exifData(resolveMetadata(metadata));
|
|
1037
1043
|
const lossy = webp.lossyOptions(quality, effort, sharpYuv, alphaQuality);
|
|
1038
1044
|
if (!lossy)
|
|
1039
|
-
return webp.encode(this.__bytes__(), this.width, this.height, exif, threads);
|
|
1045
|
+
return webp.encode(this.__bytes__(), this.width, this.height, exif, threads, effort);
|
|
1040
1046
|
return webp.encodeLossy(this.__bytes__(), this.width, this.height, lossy, exif);
|
|
1041
1047
|
}
|
|
1042
1048
|
async encodeTGA({ metadata = {} } = {}) {
|
package/dist/util/resample.js
CHANGED
|
@@ -57,44 +57,66 @@ function coefficients(inSize, outSize, filter, bits, pad = false) {
|
|
|
57
57
|
}
|
|
58
58
|
return { start, weights, taps };
|
|
59
59
|
}
|
|
60
|
+
function sliced(inSize, outSize, filter, bits, pad, from, count) {
|
|
61
|
+
const c = coefficients(inSize, outSize, filter, bits, pad);
|
|
62
|
+
if (from === 0 && count === outSize)
|
|
63
|
+
return c;
|
|
64
|
+
return { start: c.start.slice(from, from + count), weights: c.weights.slice(from * c.taps, (from + count) * c.taps), taps: c.taps };
|
|
65
|
+
}
|
|
66
|
+
function window(horizontal, region) {
|
|
67
|
+
if (!horizontal)
|
|
68
|
+
return { x0: region.x, x1: region.x + region.width };
|
|
69
|
+
const { start, taps } = horizontal, x0 = start[0], x1 = start[start.length - 1] + taps;
|
|
70
|
+
for (let i = 0; i < start.length; i++)
|
|
71
|
+
start[i] -= x0;
|
|
72
|
+
return { x0, x1 };
|
|
73
|
+
}
|
|
60
74
|
const FAST_TAPS = 4;
|
|
61
|
-
export function resample(src, width, height, outWidth, outHeight, filter, out, fast = false) {
|
|
75
|
+
export function resample(src, width, height, outWidth, outHeight, filter, out, fast = false, region = { x: 0, y: 0, width: outWidth, height: outHeight }) {
|
|
76
|
+
const partial = region.width !== outWidth || region.height !== outHeight;
|
|
77
|
+
const opaque = partial ? isOpaque(src) : undefined;
|
|
62
78
|
if (littleEndian && filter.support <= 1) {
|
|
63
|
-
if (fast && resampleFast(src, width, height, outWidth, outHeight, filter, out))
|
|
79
|
+
if (fast && resampleFast(src, width, height, outWidth, outHeight, filter, out, region, opaque))
|
|
64
80
|
return;
|
|
65
|
-
if (resampleOpaque(src, width, height, outWidth, outHeight, filter, out))
|
|
81
|
+
if (opaque !== false && resampleOpaque(src, width, height, outWidth, outHeight, filter, out, region))
|
|
66
82
|
return;
|
|
67
83
|
}
|
|
68
|
-
|
|
84
|
+
else if (opaque !== false && resampleOpaqueSigned(src, width, height, outWidth, outHeight, filter, out, region))
|
|
85
|
+
return;
|
|
86
|
+
resamplePremultiplied(src, width, height, outWidth, outHeight, filter, out, region);
|
|
69
87
|
}
|
|
70
|
-
function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
88
|
+
function resampleOpaque(src, width, height, outWidth, outHeight, filter, out, region) {
|
|
71
89
|
const pixels = new Int32Array(src.buffer, src.byteOffset, width * height);
|
|
72
90
|
let all = -1;
|
|
73
91
|
for (let x = 0, last = (height - 1) * width; x < width; x++)
|
|
74
92
|
all &= pixels[x] & pixels[last + x];
|
|
75
93
|
if (all >>> 24 !== 255)
|
|
76
94
|
return false;
|
|
77
|
-
const
|
|
95
|
+
const horizontal = outWidth === width ? null : sliced(width, outWidth, filter, BITS, false, region.x, region.width);
|
|
96
|
+
const { x0, x1 } = window(horizontal, region), cw = x1 - x0;
|
|
97
|
+
const rh = region.height;
|
|
98
|
+
const lanes = new Int32Array(cw * rh * 2);
|
|
78
99
|
if (outHeight === height) {
|
|
79
|
-
for (let
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
100
|
+
for (let y = 0, o = 0; y < rh; y++)
|
|
101
|
+
for (let i = (region.y + y) * width + x0, end = i + cw; i < end; i++, o += 2) {
|
|
102
|
+
const p = pixels[i];
|
|
103
|
+
all &= p;
|
|
104
|
+
lanes[o] = (p & 0xff00ff) << 8;
|
|
105
|
+
lanes[o + 1] = (p & 0xff00) | 0;
|
|
106
|
+
}
|
|
85
107
|
}
|
|
86
108
|
else {
|
|
87
|
-
let coefficientsV =
|
|
109
|
+
let coefficientsV = sliced(height, outHeight, filter, 8, false, region.y, rh);
|
|
88
110
|
const swar = coefficientsV.taps <= 4;
|
|
89
111
|
if (!swar)
|
|
90
|
-
coefficientsV =
|
|
112
|
+
coefficientsV = sliced(height, outHeight, filter, BITS, false, region.y, rh);
|
|
91
113
|
const { start, weights, taps } = coefficientsV;
|
|
92
|
-
for (let y = 0, o = 0; y <
|
|
93
|
-
const w = y * taps, r0 = start[y] * width;
|
|
114
|
+
for (let y = 0, o = 0; y < rh; y++) {
|
|
115
|
+
const w = y * taps, r0 = start[y] * width + x0;
|
|
94
116
|
if (swar && taps === 4) {
|
|
95
117
|
const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
|
|
96
118
|
const r1 = r0 + width, r2 = r1 + width, r3 = r2 + width;
|
|
97
|
-
for (let x = 0; x <
|
|
119
|
+
for (let x = 0; x < cw; x++, o += 2) {
|
|
98
120
|
const p0 = pixels[r0 + x], p1 = pixels[r1 + x], p2 = pixels[r2 + x], p3 = pixels[r3 + x];
|
|
99
121
|
all &= p0 & p1 & p2 & p3;
|
|
100
122
|
lanes[o] = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2) + Math.imul(p3 & 0xff00ff, w3);
|
|
@@ -103,7 +125,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
103
125
|
}
|
|
104
126
|
else if (swar && taps === 3) {
|
|
105
127
|
const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], r1 = r0 + width, r2 = r1 + width;
|
|
106
|
-
for (let x = 0; x <
|
|
128
|
+
for (let x = 0; x < cw; x++, o += 2) {
|
|
107
129
|
const p0 = pixels[r0 + x], p1 = pixels[r1 + x], p2 = pixels[r2 + x];
|
|
108
130
|
all &= p0 & p1 & p2;
|
|
109
131
|
lanes[o] = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2);
|
|
@@ -112,7 +134,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
112
134
|
}
|
|
113
135
|
else if (swar && taps === 2) {
|
|
114
136
|
const w0 = weights[w], w1 = weights[w + 1], r1 = r0 + width;
|
|
115
|
-
for (let x = 0; x <
|
|
137
|
+
for (let x = 0; x < cw; x++, o += 2) {
|
|
116
138
|
const p0 = pixels[r0 + x], p1 = pixels[r1 + x];
|
|
117
139
|
all &= p0 & p1;
|
|
118
140
|
lanes[o] = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1);
|
|
@@ -120,7 +142,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
120
142
|
}
|
|
121
143
|
}
|
|
122
144
|
else if (swar) {
|
|
123
|
-
for (let x = 0; x <
|
|
145
|
+
for (let x = 0; x < cw; x++, o += 2) {
|
|
124
146
|
let rb = 0, g = 0;
|
|
125
147
|
for (let k = 0, s = r0 + x; k < taps; k++, s += width) {
|
|
126
148
|
const p = pixels[s], wk = weights[w + k];
|
|
@@ -133,7 +155,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
133
155
|
}
|
|
134
156
|
}
|
|
135
157
|
else {
|
|
136
|
-
for (let x = 0; x <
|
|
158
|
+
for (let x = 0; x < cw; x++, o += 2) {
|
|
137
159
|
let r = 0, g = 0, b = 0, k = 0, s = r0 + x;
|
|
138
160
|
for (; k + 1 < taps; k += 2, s += width * 2) {
|
|
139
161
|
const p = pixels[s], q = pixels[s + width], w0 = weights[w + k], w1 = weights[w + k + 1];
|
|
@@ -157,8 +179,8 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
157
179
|
}
|
|
158
180
|
if (all >>> 24 !== 255)
|
|
159
181
|
return false;
|
|
160
|
-
const result = new Int32Array(out.buffer, out.byteOffset,
|
|
161
|
-
if (
|
|
182
|
+
const result = new Int32Array(out.buffer, out.byteOffset, region.width * rh);
|
|
183
|
+
if (!horizontal) {
|
|
162
184
|
for (let i = 0, o = 0; i < result.length; i++, o += 2) {
|
|
163
185
|
const rb = lanes[o], g = lanes[o + 1];
|
|
164
186
|
result[i] = ((rb & 0xffff) + 128 >> 8) | (g + 128 >> 8) << 8 | ((rb >>> 16) + 128 >> 8) << 16 | 0xff000000;
|
|
@@ -166,11 +188,11 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
166
188
|
return true;
|
|
167
189
|
}
|
|
168
190
|
const R = 1 << 7 + BITS, S = 8 + BITS;
|
|
169
|
-
const { start, weights, taps } =
|
|
170
|
-
for (let y = 0, o = 0; y <
|
|
171
|
-
const row = y *
|
|
191
|
+
const { start, weights, taps } = horizontal, rw = region.width;
|
|
192
|
+
for (let y = 0, o = 0; y < rh; y++) {
|
|
193
|
+
const row = y * cw * 2;
|
|
172
194
|
if (taps === 4) {
|
|
173
|
-
for (let x = 0; x <
|
|
195
|
+
for (let x = 0; x < rw; x++, o++) {
|
|
174
196
|
const s = row + start[x] * 2, w = x * 4;
|
|
175
197
|
const a0 = lanes[s], a1 = lanes[s + 2], a2 = lanes[s + 4], a3 = lanes[s + 6];
|
|
176
198
|
const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
|
|
@@ -181,7 +203,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
181
203
|
}
|
|
182
204
|
}
|
|
183
205
|
else if (taps === 2) {
|
|
184
|
-
for (let x = 0; x <
|
|
206
|
+
for (let x = 0; x < rw; x++, o++) {
|
|
185
207
|
const s = row + start[x] * 2, w = x * 2;
|
|
186
208
|
const a0 = lanes[s], a1 = lanes[s + 2], w0 = weights[w], w1 = weights[w + 1];
|
|
187
209
|
const r = (a0 & 0xffff) * w0 + (a1 & 0xffff) * w1;
|
|
@@ -191,7 +213,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
191
213
|
}
|
|
192
214
|
}
|
|
193
215
|
else {
|
|
194
|
-
for (let x = 0; x <
|
|
216
|
+
for (let x = 0; x < rw; x++, o++) {
|
|
195
217
|
let r = 0, g = 0, b = 0, k = 0, s = row + start[x] * 2;
|
|
196
218
|
const w = x * taps;
|
|
197
219
|
for (; k + 1 < taps; k += 2, s += 4) {
|
|
@@ -221,14 +243,14 @@ function packedSum(pixels, s, step, weights, w, taps) {
|
|
|
221
243
|
}
|
|
222
244
|
return (rb >>> 8 & 0xff00ff) | (ga & 0xff00ff00);
|
|
223
245
|
}
|
|
224
|
-
function verticalFast(pixels, rows, width,
|
|
246
|
+
function verticalFast(pixels, rows, width, x0, cw, { start, weights, taps }) {
|
|
225
247
|
let all = -1;
|
|
226
|
-
for (let y = 0, o = 0; y <
|
|
227
|
-
const w = y * taps, r0 = start[y] * width;
|
|
248
|
+
for (let y = 0, o = 0; y < start.length; y++) {
|
|
249
|
+
const w = y * taps, r0 = start[y] * width + x0;
|
|
228
250
|
if (taps === 4) {
|
|
229
251
|
const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
|
|
230
252
|
const r1 = r0 + width, r2 = r1 + width, r3 = r2 + width;
|
|
231
|
-
for (let x = 0; x <
|
|
253
|
+
for (let x = 0; x < cw; x++, o++) {
|
|
232
254
|
const p0 = pixels[r0 + x], p1 = pixels[r1 + x], p2 = pixels[r2 + x], p3 = pixels[r3 + x];
|
|
233
255
|
all &= p0 & p1 & p2 & p3;
|
|
234
256
|
const rb = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2) + Math.imul(p3 & 0xff00ff, w3) + 0x800080;
|
|
@@ -238,7 +260,7 @@ function verticalFast(pixels, rows, width, outHeight, { start, weights, taps })
|
|
|
238
260
|
}
|
|
239
261
|
else if (taps === 2) {
|
|
240
262
|
const w0 = weights[w], w1 = weights[w + 1], r1 = r0 + width;
|
|
241
|
-
for (let x = 0; x <
|
|
263
|
+
for (let x = 0; x < cw; x++, o++) {
|
|
242
264
|
const p0 = pixels[r0 + x], p1 = pixels[r1 + x];
|
|
243
265
|
all &= p0 & p1;
|
|
244
266
|
const rb = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + 0x800080;
|
|
@@ -247,7 +269,7 @@ function verticalFast(pixels, rows, width, outHeight, { start, weights, taps })
|
|
|
247
269
|
}
|
|
248
270
|
}
|
|
249
271
|
else {
|
|
250
|
-
for (let x = 0; x <
|
|
272
|
+
for (let x = 0; x < cw; x++, o++) {
|
|
251
273
|
for (let k = 0, s = r0 + x; k < taps; k++, s += width)
|
|
252
274
|
all &= pixels[s];
|
|
253
275
|
rows[o] = packedSum(pixels, r0 + x, width, weights, w, taps);
|
|
@@ -284,14 +306,17 @@ function horizontalFast(rows, result, width, outWidth, outHeight, { start, weigh
|
|
|
284
306
|
}
|
|
285
307
|
}
|
|
286
308
|
}
|
|
287
|
-
function resampleFast(src, width, height, outWidth, outHeight, filter, out) {
|
|
288
|
-
const
|
|
289
|
-
|
|
309
|
+
function resampleFast(src, width, height, outWidth, outHeight, filter, out, region, known) {
|
|
310
|
+
const { width: rw, height: rh } = region;
|
|
311
|
+
const vertical = sliced(height, outHeight, filter, 8, true, region.y, rh);
|
|
312
|
+
const horizontal = outWidth === width ? null : sliced(width, outWidth, filter, 8, true, region.x, rw);
|
|
313
|
+
if (vertical.taps > FAST_TAPS || horizontal && horizontal.taps > FAST_TAPS)
|
|
290
314
|
return false;
|
|
315
|
+
const { x0, x1 } = window(horizontal, region), cw = x1 - x0;
|
|
291
316
|
let pixels = new Int32Array(src.buffer, src.byteOffset, width * height), all = -1;
|
|
292
317
|
for (let x = 0, last = (height - 1) * width; x < width; x++)
|
|
293
318
|
all &= pixels[x] & pixels[last + x];
|
|
294
|
-
let opaque = all >>> 24 === 255 && (outHeight !== height || isOpaque(src));
|
|
319
|
+
let opaque = known ?? (all >>> 24 === 255 && (outHeight !== height || isOpaque(src)));
|
|
295
320
|
const premultiplied = () => {
|
|
296
321
|
const bitmap = new Uint8ClampedArray(src.length);
|
|
297
322
|
premultiply8(src, bitmap);
|
|
@@ -301,38 +326,155 @@ function resampleFast(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
301
326
|
pixels = premultiplied();
|
|
302
327
|
let rows = pixels;
|
|
303
328
|
if (outHeight !== height) {
|
|
304
|
-
rows = new Int32Array(
|
|
305
|
-
if (verticalFast(pixels, rows, width,
|
|
329
|
+
rows = new Int32Array(cw * rh);
|
|
330
|
+
if (verticalFast(pixels, rows, width, x0, cw, vertical) >>> 24 !== 255 && opaque) {
|
|
306
331
|
opaque = false;
|
|
307
|
-
verticalFast(premultiplied(), rows, width,
|
|
332
|
+
verticalFast(premultiplied(), rows, width, x0, cw, vertical);
|
|
308
333
|
}
|
|
309
334
|
}
|
|
310
|
-
|
|
311
|
-
|
|
335
|
+
else if (cw !== width || rh !== height) {
|
|
336
|
+
rows = new Int32Array(cw * rh);
|
|
337
|
+
for (let y = 0; y < rh; y++)
|
|
338
|
+
rows.set(pixels.subarray((region.y + y) * width + x0, (region.y + y) * width + x1), y * cw);
|
|
339
|
+
}
|
|
340
|
+
const result = new Int32Array(out.buffer, out.byteOffset, rw * rh);
|
|
341
|
+
if (!horizontal)
|
|
312
342
|
result.set(rows);
|
|
313
343
|
else
|
|
314
|
-
horizontalFast(rows, result,
|
|
344
|
+
horizontalFast(rows, result, cw, rw, rh, horizontal);
|
|
315
345
|
if (!opaque)
|
|
316
346
|
unpremultiply8(out);
|
|
317
347
|
return true;
|
|
318
348
|
}
|
|
319
|
-
function
|
|
320
|
-
const
|
|
349
|
+
function resampleOpaqueSigned(src, width, height, outWidth, outHeight, filter, out, region) {
|
|
350
|
+
const pixels = new Int32Array(src.buffer, src.byteOffset, width * height);
|
|
351
|
+
let all = -1;
|
|
352
|
+
for (let x = 0, last = (height - 1) * width; x < width; x++)
|
|
353
|
+
all &= pixels[x] & pixels[last + x];
|
|
354
|
+
if (all >>> 24 !== 255)
|
|
355
|
+
return false;
|
|
356
|
+
const horizontal = outWidth === width ? null : sliced(width, outWidth, filter, BITS, false, region.x, region.width);
|
|
357
|
+
const { x0, x1 } = window(horizontal, region), cw = x1 - x0, rh = region.height;
|
|
358
|
+
const R = new Int32Array(cw * rh), G = new Int32Array(cw * rh), B = new Int32Array(cw * rh);
|
|
321
359
|
if (outHeight === height) {
|
|
322
|
-
for (let
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
360
|
+
for (let y = 0, o = 0; y < rh; y++)
|
|
361
|
+
for (let i = (region.y + y) * width + x0, end = i + cw; i < end; i++, o++) {
|
|
362
|
+
const p = pixels[i];
|
|
363
|
+
all &= p;
|
|
364
|
+
R[o] = (p & 255) * 255;
|
|
365
|
+
G[o] = (p >> 8 & 255) * 255;
|
|
366
|
+
B[o] = (p >> 16 & 255) * 255;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
const { start, weights, taps } = sliced(height, outHeight, filter, BITS, false, region.y, rh);
|
|
371
|
+
const H = ONE >> 1;
|
|
372
|
+
for (let y = 0, o = 0; y < rh; y++, o += cw) {
|
|
373
|
+
const w = y * taps;
|
|
374
|
+
for (let k = 0; k + 1 < taps; k += 2) {
|
|
375
|
+
const wa = weights[w + k], wb = weights[w + k + 1], ra = (start[y] + k) * width + x0, rb = ra + width;
|
|
376
|
+
if (k === 0) {
|
|
377
|
+
for (let x = 0; x < cw; x++) {
|
|
378
|
+
const p = pixels[ra + x], q = pixels[rb + x];
|
|
379
|
+
all &= p & q;
|
|
380
|
+
R[o + x] = (p & 255) * wa + (q & 255) * wb;
|
|
381
|
+
G[o + x] = (p >> 8 & 255) * wa + (q >> 8 & 255) * wb;
|
|
382
|
+
B[o + x] = (p >> 16 & 255) * wa + (q >> 16 & 255) * wb;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
for (let x = 0; x < cw; x++) {
|
|
387
|
+
const p = pixels[ra + x], q = pixels[rb + x];
|
|
388
|
+
all &= p & q;
|
|
389
|
+
R[o + x] += (p & 255) * wa + (q & 255) * wb;
|
|
390
|
+
G[o + x] += (p >> 8 & 255) * wa + (q >> 8 & 255) * wb;
|
|
391
|
+
B[o + x] += (p >> 16 & 255) * wa + (q >> 16 & 255) * wb;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (taps & 1) {
|
|
396
|
+
const k = taps - 1, wk = weights[w + k], r = (start[y] + k) * width + x0;
|
|
397
|
+
for (let x = 0; x < cw; x++) {
|
|
398
|
+
const p = pixels[r + x];
|
|
399
|
+
all &= p;
|
|
400
|
+
if (k === 0) {
|
|
401
|
+
R[o + x] = (p & 255) * wk;
|
|
402
|
+
G[o + x] = (p >> 8 & 255) * wk;
|
|
403
|
+
B[o + x] = (p >> 16 & 255) * wk;
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
R[o + x] += (p & 255) * wk;
|
|
407
|
+
G[o + x] += (p >> 8 & 255) * wk;
|
|
408
|
+
B[o + x] += (p >> 16 & 255) * wk;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
for (let x = o, end = o + cw; x < end; x++) {
|
|
413
|
+
R[x] = R[x] * 255 + H >> BITS;
|
|
414
|
+
G[x] = G[x] * 255 + H >> BITS;
|
|
415
|
+
B[x] = B[x] * 255 + H >> BITS;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (all >>> 24 !== 255)
|
|
420
|
+
return false;
|
|
421
|
+
const f = 255 / (65025 * ONE), rw = region.width;
|
|
422
|
+
const { start, weights, taps } = horizontal ?? { start: null, weights: null, taps: 0 };
|
|
423
|
+
for (let y = 0, o = 0; y < rh; y++) {
|
|
424
|
+
const row = y * cw;
|
|
425
|
+
if (!start || !weights) {
|
|
426
|
+
for (let x = 0; x < rw; x++, o += 4) {
|
|
427
|
+
const s = row + x;
|
|
428
|
+
out[o] = R[s] * ONE * f;
|
|
429
|
+
out[o + 1] = G[s] * ONE * f;
|
|
430
|
+
out[o + 2] = B[s] * ONE * f;
|
|
431
|
+
out[o + 3] = 255;
|
|
432
|
+
}
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
for (let x = 0; x < rw; x++, o += 4) {
|
|
436
|
+
let r = 0, g = 0, b = 0, k = 0, s = row + start[x];
|
|
437
|
+
const w = x * taps;
|
|
438
|
+
for (; k + 1 < taps; k += 2, s += 2) {
|
|
439
|
+
const w0 = weights[w + k], w1 = weights[w + k + 1];
|
|
440
|
+
r += R[s] * w0 + R[s + 1] * w1;
|
|
441
|
+
g += G[s] * w0 + G[s + 1] * w1;
|
|
442
|
+
b += B[s] * w0 + B[s + 1] * w1;
|
|
443
|
+
}
|
|
444
|
+
if (k < taps) {
|
|
445
|
+
const w0 = weights[w + k];
|
|
446
|
+
r += R[s] * w0;
|
|
447
|
+
g += G[s] * w0;
|
|
448
|
+
b += B[s] * w0;
|
|
449
|
+
}
|
|
450
|
+
out[o] = r * f;
|
|
451
|
+
out[o + 1] = g * f;
|
|
452
|
+
out[o + 2] = b * f;
|
|
453
|
+
out[o + 3] = 255;
|
|
328
454
|
}
|
|
329
455
|
}
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
function resamplePremultiplied(src, width, height, outWidth, outHeight, filter, out, region) {
|
|
459
|
+
const horizontal = outWidth === width ? null : sliced(width, outWidth, filter, BITS, false, region.x, region.width);
|
|
460
|
+
const { x0, x1 } = window(horizontal, region), cw = x1 - x0, rh = region.height;
|
|
461
|
+
const plane = new Int32Array(cw * rh * 4);
|
|
462
|
+
if (outHeight === height) {
|
|
463
|
+
for (let y = 0, o = 0; y < rh; y++)
|
|
464
|
+
for (let s = ((region.y + y) * width + x0) * 4, end = s + cw * 4; s < end; s += 4, o += 4) {
|
|
465
|
+
const a = src[s + 3];
|
|
466
|
+
plane[o] = src[s] * a;
|
|
467
|
+
plane[o + 1] = src[s + 1] * a;
|
|
468
|
+
plane[o + 2] = src[s + 2] * a;
|
|
469
|
+
plane[o + 3] = a * 255;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
330
472
|
else {
|
|
331
|
-
const { start, weights, taps } =
|
|
473
|
+
const { start, weights, taps } = sliced(height, outHeight, filter, BITS, false, region.y, rh);
|
|
332
474
|
const stride = width * 4, H = ONE >> 1;
|
|
333
|
-
for (let y = 0, o = 0; y <
|
|
334
|
-
const w = y * taps, r0 = start[y] * stride;
|
|
335
|
-
for (let x = 0; x <
|
|
475
|
+
for (let y = 0, o = 0; y < rh; y++) {
|
|
476
|
+
const w = y * taps, r0 = start[y] * stride + x0 * 4;
|
|
477
|
+
for (let x = 0; x < cw * 4; x += 4, o += 4) {
|
|
336
478
|
let r = 0, g = 0, b = 0, a = 0, k = 0, s = r0 + x;
|
|
337
479
|
for (; k + 1 < taps; k += 2, s += stride * 2) {
|
|
338
480
|
const t = s + stride, wa = weights[w + k] * src[s + 3], wb = weights[w + k + 1] * src[t + 3];
|
|
@@ -355,10 +497,10 @@ function resamplePremultiplied(src, width, height, outWidth, outHeight, filter,
|
|
|
355
497
|
}
|
|
356
498
|
}
|
|
357
499
|
}
|
|
358
|
-
const { start, weights, taps } =
|
|
359
|
-
for (let y = 0, o = 0; y <
|
|
360
|
-
const row = y *
|
|
361
|
-
for (let x = 0; x <
|
|
500
|
+
const { start, weights, taps } = horizontal ?? { start: null, weights: null, taps: 0 };
|
|
501
|
+
for (let y = 0, o = 0; y < rh; y++) {
|
|
502
|
+
const row = y * cw * 4;
|
|
503
|
+
for (let x = 0; x < region.width; x++, o += 4) {
|
|
362
504
|
let r = 0, g = 0, b = 0, a = 0;
|
|
363
505
|
if (!start || !weights) {
|
|
364
506
|
const s = row + x * 4;
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '3.
|
|
1
|
+
export const VERSION = '3.15.0';
|