Skip to content

Commit 5e7894d

Browse files
committed
expand palette and gray PNG samples to RGBA through a word table
1 parent 9d87b3b commit 5e7894d

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Lossy WebP decodes 3 to 14% faster (more at higher qualities): the coefficients are read with the boolean decoder's state in local variables, like libwebp does
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
15+
- 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
1516
- `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
1617
- 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
1718

‎src/codecs/png.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,23 @@ function createConverter(colorType: number, depth: number, palette: Uint8Array |
10131013
lut[i * 4 + 3] = transparency && i < transparency.length ? transparency[i] : 0xff;
10141014
}
10151015

1016+
// whole rows: a pixel at a time as a 32-bit word (copying the entry's four bytes, in any byte order)
1017+
const words = new Uint32Array(lut.buffer);
1018+
10161019
return (pixels, rowBytes, w, h, out, place) => {
1020+
if (place.step === 4 && !(out.byteOffset & 3)) {
1021+
const target = new Uint32Array(out.buffer, out.byteOffset, out.length >> 2);
1022+
for (let y = 0; y < h; y++) {
1023+
const s = y * rowBytes, o = place.row(y) >> 2;
1024+
if (depth === 8) for (let x = 0; x < w; x++) target[o + x] = words[pixels[s + x]];
1025+
else {
1026+
const row = pixels.subarray(s, s + rowBytes);
1027+
for (let x = 0; x < w; x++) target[o + x] = words[sample(row, x)];
1028+
}
1029+
}
1030+
return;
1031+
}
1032+
10171033
for (let y = 0; y < h; y++) {
10181034
const row = pixels.subarray(y * rowBytes, (y + 1) * rowBytes);
10191035

@@ -1031,7 +1047,32 @@ function createConverter(colorType: number, depth: number, palette: Uint8Array |
10311047
if (colorType === 0) {
10321048
const key = transparency && transparency.length >= 2 ? (transparency[0] << 8 | transparency[1]) & maxValue : -1;
10331049

1050+
// up to 8 bits: every sample's pixel from a table, a 32-bit word at a time (as for palettes)
1051+
let words: Uint32Array | null = null;
1052+
if (depth <= 8) {
1053+
const lut = new Uint8Array(256 * 4);
1054+
for (let raw = 0; raw <= maxValue; raw++) {
1055+
const v = scale(raw);
1056+
lut[raw * 4] = lut[raw * 4 + 1] = lut[raw * 4 + 2] = v;
1057+
lut[raw * 4 + 3] = raw === key ? 0 : 0xff;
1058+
}
1059+
words = new Uint32Array(lut.buffer);
1060+
}
1061+
10341062
return (pixels, rowBytes, w, h, out, place) => {
1063+
if (words && place.step === 4 && !(out.byteOffset & 3)) {
1064+
const target = new Uint32Array(out.buffer, out.byteOffset, out.length >> 2);
1065+
for (let y = 0; y < h; y++) {
1066+
const s = y * rowBytes, o = place.row(y) >> 2;
1067+
if (depth === 8) for (let x = 0; x < w; x++) target[o + x] = words[pixels[s + x]];
1068+
else {
1069+
const row = pixels.subarray(s, s + rowBytes);
1070+
for (let x = 0; x < w; x++) target[o + x] = words[sample(row, x)];
1071+
}
1072+
}
1073+
return;
1074+
}
1075+
10351076
for (let y = 0; y < h; y++) {
10361077
const row = pixels.subarray(y * rowBytes, (y + 1) * rowBytes);
10371078

0 commit comments

Comments
 (0)