Skip to content

Commit b856b4d

Browse files
committed
copy opaque composite() source pixels as words
1 parent 5e7894d commit b856b4d

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Encoding opaque truecolor PNGs is 4 to 8% faster: once only the opacity of the remaining pixels is unknown, their alpha is checked a word at a time
1414
- Lossless WebP decodes 18 to 29% faster: the pixels are read with the bit reader's state in local variables, as libwebp does, the predictors restore pixels in loops of their own, and the color transform unpacks its multipliers once per block
1515
- Palette and grayscale PNGs (up to 8 bits) decode 1.2 to 1.7 times faster: each pixel is copied from a table as a 32-bit word
16+
- `composite()` is up to 1.5 times faster for opaque sources: their pixels are copied as 32-bit words
1617
- `quantize()` is up to 2.3 times faster without dithering, 1.4 times with `'ordered'` and 1.2 times with `'atkinson'`: the nearest palette entry of each color and Bayer cell is remembered, the palette is searched in sorted arrays, and the spread errors aren't put in an array per pixel
1718
- PNG encoding is 2 to 4 times faster for large images under Node.js and Bun: more than 2 MiB of image data is compressed in 1 MiB pieces on zlib's thread pool at the same time, the way pigz does (a 4000x2667 photo: 655 to 265 ms in Node.js, 601 to 157 ms in Bun). The files are a few bytes larger or smaller and no longer byte for byte the same as from 4.0.0; the pixels are
1819

‎src/image.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,11 +1651,19 @@ export class Image {
16511651
const dst = this.bitmap;
16521652
// (a source sharing the destination's memory, itself or through the bitmap setter, is read before it is written)
16531653
const src = source.bitmap.buffer === dst.buffer ? source.bitmap.slice() : source.bitmap;
1654+
// opaque source pixels at full opacity are copied as 32-bit words (their four bytes, in any byte order)
1655+
const words = opacity === 1 && !(src.byteOffset & 3) && !(dst.byteOffset & 3);
1656+
const srcWords = words ? new Uint32Array(src.buffer, src.byteOffset, src.length >> 2) : null;
1657+
const dstWords = words ? new Uint32Array(dst.buffer, dst.byteOffset, dst.length >> 2) : null;
16541658

16551659
for (let row = top; row < bottom; row++) {
16561660
for (let column = left, s = 4 * ((row - y) * source.width + left - x), d = 4 * (row * this.width + left); column < right; column++, s += 4, d += 4) {
16571661
const sa = src[s + 3];
16581662
if (sa === 0) continue;
1663+
if (sa === 255 && dstWords && srcWords) {
1664+
dstWords[d >> 2] = srcWords[s >> 2];
1665+
continue;
1666+
}
16591667

16601668
const da = dst[d + 3];
16611669
if ((sa === 255 && opacity === 1) || da === 0) {

0 commit comments

Comments
 (0)