Skip to content

Commit 7715987

Browse files
committed
composite a transparent image onto the JPEG background row by row
1 parent dcc4cb4 commit 7715987

4 files changed

Lines changed: 101 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ Upgrading from 3.x? Upgrade to the latest 3.x release first, fix the deprecation
136136

137137
### Fixes
138138

139+
- `encodeJPEG()` of an image with transparency composites it onto the background row by row instead of copying the image first: the peak memory of a 6000x6000 image is 378 MB instead of 485 MB, the same as without transparency, and the file is the same (images with a gain map still take the copy).
139140
- `encodeWebP({ optimize: false })` makes the same file as `webpEncoder()` for animations with frames WebP can't store as they are (stored as the canvas from the first such frame on, not all of them), and `Colors.gradient()` with 3 or more stops has its first and last stops' exact colors before and after them (a transparent end stop gave `0x00000000`) and takes 200,000 stops without overflowing the call stack.
140141
- `resize({ fit: 'inside' })` throws for a box side below 1 pixel, like the other fits, instead of making a 1x1 image; `pixels()` goes over the pixels the image has when it's called, also when it's resized before the first value is taken.
141142
- `Image.renderText()` checks `strokeColor` without a `strokeWidth` too (a wrong color passed until a stroke was set), and an animation encoder whose `finish()` found no frame can still be given frames.

‎src/codecs/jpeg.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,9 +1030,14 @@ export interface EncodeMetadata {
10301030
fullChroma?: boolean;
10311031
/** the chroma subsampling, overriding the default by quality (4:2:0 up to 90, 4:4:4 above) */
10321032
subsampling?: '4:4:4' | '4:2:0';
1033+
/**
1034+
* with it, `pixels` is the one row of scratch pixels (width * 4 bytes): `rows(y)` fills it with row y before the
1035+
* row is read, so the pixels need not be in memory whole (the image composited onto a background, row by row)
1036+
*/
1037+
rows?: (y: number) => void;
10331038
}
10341039

1035-
export function encode(pixels: Uint8Array, width: number, height: number, quality = 90, { exif, comment, segments, gray = false, fullChroma = false, subsampling }: EncodeMetadata = {}): Uint8Array<ArrayBuffer> {
1040+
export function encode(pixels: Uint8Array, width: number, height: number, quality = 90, { exif, comment, segments, gray = false, fullChroma = false, subsampling, rows }: EncodeMetadata = {}): Uint8Array<ArrayBuffer> {
10361041
if (!width || !height) throw new RangeError('JPEG: invalid dimensions');
10371042
checkFormatSize('JPEG', width, height, 0xffff);
10381043

@@ -1062,10 +1067,12 @@ export function encode(pixels: Uint8Array, width: number, height: number, qualit
10621067
const cbRows = subsample ? new Uint8Array(2 * rowWidth) : Cb, crRows = subsample ? new Uint8Array(2 * rowWidth) : Cr;
10631068

10641069
// (pixels a 32-bit word at a time where they're aligned, little endian)
1065-
const words = littleEndian && pixels.byteOffset % 4 === 0 ? new Uint32Array(pixels.buffer, pixels.byteOffset, width * height) : null;
1070+
const words = littleEndian && pixels.byteOffset % 4 === 0 ? new Uint32Array(pixels.buffer, pixels.byteOffset, (rows ? width : width * height)) : null;
10661071
for (let y = 0; y < height; y++) {
1072+
// (with `rows`, the pixels are the scratch row, filled first)
1073+
if (rows) rows(y);
10671074
const row = subsample ? (y & 1) * rowWidth : y * CW;
1068-
let i = y * width * 4, o = y * W, c = row;
1075+
let i = rows ? 0 : y * width * 4, o = y * W, c = row;
10691076

10701077
if (words) for (let x = 0, w = i >> 2; x < width; x++, w++, o++, c++) {
10711078
const p = words[w], r = p & 255, g = p >> 8 & 255, b = p >> 16 & 255;

‎src/image.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2681,6 +2681,15 @@ export class Image {
26812681
// eslint-disable-next-line no-restricted-syntax -- a limit of the format, not a range of the option
26822682
if (exif && exif.length > 65527) throw new RangeError(`JPEG: the metadata's description, author, copyright, software and creation time must be at most 65533 bytes together in Exif, not ${exif.length + 6}`);
26832683

2684+
// without a gain map: the transparent pixels are composited onto the background row by row as the encoder reads
2685+
// them (no copy of the image: __flattened__'s copy of 4 bytes a pixel was the peak memory)
2686+
if (!this.__gain_map__) {
2687+
const flat = this.__flatRows__(backgroundColor);
2688+
const base = { exif, comment, subsampling };
2689+
if (!flat) return jpeg.encode(this.__bytes__(), this.width, this.height, quality, { ...base, gray: this.__is_gray__() });
2690+
return jpeg.encode(flat.pixels, this.width, this.height, quality, { ...base, gray: flat.gray(), rows: flat.row });
2691+
}
2692+
26842693
// transparent pixels: composited onto the opaque background (with the gain map, so the HDR light dims alike)
26852694
const source = this.__flattened__(backgroundColor, embed !== null) ?? this;
26862695
const gainMap = source.__gain_map__;
@@ -2712,6 +2721,52 @@ export class Image {
27122721
return true;
27132722
}
27142723

2724+
/**
2725+
* The image composited onto an opaque color, one row at a time, as `__flattened__` does (the same arithmetic, so the
2726+
* same pixels), or null if it is opaque already. `pixels` is the scratch row that `row(y)` fills; `gray()` says
2727+
* whether every composited pixel is gray (streamed, stops at the first colored one)
2728+
* @internal
2729+
* @ignore
2730+
*/
2731+
protected __flatRows__(background: Color): { pixels: Uint8Array, row: (y: number) => void, gray: () => boolean } | null {
2732+
const src = this.bitmap;
2733+
let opaque = true;
2734+
for (let i = 3; i < src.length && opaque; i += 4) opaque = src[i] === 255;
2735+
if (opaque) return null;
2736+
const width = this.width, bgR = background >>> 24, bgG = background >>> 16 & 255, bgB = background >>> 8 & 255;
2737+
const scratch = new Uint8ClampedArray(width * 4);
2738+
const row = (y: number): void => {
2739+
// (source over an opaque background, the arithmetic of composite(): a clamped byte per channel)
2740+
for (let x = 0, s = 4 * y * width, d = 0; x < width; x++, s += 4, d += 4) {
2741+
const sa = src[s + 3];
2742+
if (sa === 0) {
2743+
scratch[d] = bgR;
2744+
scratch[d + 1] = bgG;
2745+
scratch[d + 2] = bgB;
2746+
continue;
2747+
}
2748+
if (sa === 255) {
2749+
scratch[d] = src[s];
2750+
scratch[d + 1] = src[s + 1];
2751+
scratch[d + 2] = src[s + 2];
2752+
continue;
2753+
}
2754+
const fa = sa / 255, ba = 1 - fa, a = fa + ba;
2755+
scratch[d] = (src[s] * fa + bgR * ba) / a;
2756+
scratch[d + 1] = (src[s + 1] * fa + bgG * ba) / a;
2757+
scratch[d + 2] = (src[s + 2] * fa + bgB * ba) / a;
2758+
}
2759+
};
2760+
const gray = (): boolean => {
2761+
for (let y = 0; y < this.height; y++) {
2762+
row(y);
2763+
for (let d = 0; d < scratch.length; d += 4) if (scratch[d] !== scratch[d + 1] || scratch[d] !== scratch[d + 2]) return false;
2764+
}
2765+
return true;
2766+
};
2767+
return { pixels: new Uint8Array(scratch.buffer), row, gray };
2768+
}
2769+
27152770
/**
27162771
* The image composited onto an opaque color, or null if it is opaque already (formats without alpha)
27172772
* @internal

‎tests/jpeg-flatten.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const {Image} = require('..');
2+
3+
const panic = message => {
4+
console.error(message);
5+
process.exit(1);
6+
};
7+
let seed = 11;
8+
const random = () => (seed = (seed * 1103515245 + 12345) >>> 0) / 4294967296;
9+
10+
// encodeJPEG composites transparent pixels onto the background row by row, without a copy of the image: the file is the
11+
// one of the image composited the plain way (composite() onto a filled image), for every kind of alpha
12+
(async () => {
13+
const alphas = {
14+
'random alpha': () => random() * 256 | 0,
15+
'mostly opaque': () => random() < 0.9 ? 255 : random() * 256 | 0,
16+
'mostly clear': () => random() < 0.9 ? 0 : random() * 256 | 0,
17+
'two levels': () => random() < 0.5 ? 0 : 128
18+
};
19+
for (const [name, alpha] of Object.entries(alphas)) for (const [width, height] of [[17, 9], [1, 1], [64, 33]]) for (const gray of [false, true]) {
20+
const image = new Image(width, height).fill(() => {
21+
const v = random() * 256 | 0;
22+
return gray ? (v << 24 | v << 16 | v << 8 | alpha()) >>> 0 : ((random() * 2 ** 24) << 8 | alpha()) >>> 0;
23+
});
24+
for (const background of [0x000000ff, 0xff8800ff, 0x808080ff]) for (const quality of [50, 90, 95]) {
25+
const flat = new Image(width, height).fill(background).composite(image);
26+
const expected = await flat.encodeJPEG({quality, subsampling: quality > 90 ? '4:4:4' : '4:2:0'});
27+
const got = await image.encodeJPEG({quality, background, subsampling: quality > 90 ? '4:4:4' : '4:2:0'});
28+
if (!Buffer.from(expected).equals(Buffer.from(got))) panic(`${name} ${width}x${height} ${gray ? 'gray' : 'color'} background ${background.toString(16)} q${quality}: ${got.length} bytes, expected ${expected.length}`);
29+
}
30+
}
31+
// the image isn't changed
32+
const image = new Image(4, 4).fill(0xff000080), before = Buffer.from(image.bitmap);
33+
await image.encodeJPEG();
34+
if (!Buffer.from(image.bitmap).equals(before)) panic('encodeJPEG changed the image');
35+
})();

0 commit comments

Comments
 (0)