Migrate from pngjs and pixelmatch
Visual regression tools often use pngjs to
read and write PNG files and pixelmatch
to compare the pixels. @swc/anyformat-image replaces both packages:
- The decoded pixels are the same as the pixels from pngjs 7.0.0, except for the differences in Decoding.
- The diff count and the diff image are the same as the results of
pixelmatch 8.0.0, 7.2.0, 6.0.0, or 5.3.0 with a
Uint8Arrayoutput (see Differences from pixelmatch). - One call can decode two files, compare them, and encode the diff image.
- Every function that decodes, encodes, or compares has an asynchronous form that does not block the event loop.
See the image API reference for the full API. The examples
on this page use Node.js. In Python, use decode, encode, and
compare_images. The options have the same names in snake case. Give encode
options as PngEncodeOptions(...), PNG decode options as
PngDecodeOptions(...), and the decode option of compare_images as
DecodeOptions(...), not as dictionaries.
Compare two PNG files
With pngjs and pixelmatch:
With @swc/anyformat-image:
The pixels of the two files stay in native memory. To compare images that
are already in memory, pass raw images. A pngjs PNG object is a valid raw
image, so you can migrate one step at a time:
API mapping
The comparison options have the same names and defaults as pixelmatch 8.
The values are checked more strictly than in pixelmatch. See
Compare options. An option that is null
has its default value here, but pixelmatch uses null as the value: for
example, threshold: null means 0 in pixelmatch.
pixelmatch draws only the pixels that differ when diffMask is set, and
keeps the other bytes of the output buffer. diffImage is transparent at
these pixels, so a program that fills the output first, for example with a
copy of the first image, must draw the diff image over that copy itself.
These features of pngjs have no replacement:
- The stream API (
parse,pack, and the events),bitblt, andadjustGamma. - 16-bit output:
skipRescaleofPNG.sync.readandbitDepth: 16ofPNG.sync.write. - Input that is not RGBA:
inputColorTypeandinputHasAlphaofPNG.sync.write. - Metadata on write: pngjs writes the
gAMAchunk ofpng.gamma.encodewrites no metadata. deflateChunkSize,deflateFactory, thefilloption ofnew PNG(), andpng.palette.- The
pixelmatchcommand-line tool.
png.depth maps to info.bitDepth, and png.colorType (a number) maps to
info.colorType (a name, for example 'rgba').
Decoding
decode and pngjs give different results in these cases:
- EXIF orientation.
decodeapplies the orientation of aneXIfchunk by default, as browsers do, so the width and the height can change. pngjs ignores it. Passorientation: 'ignore'to get the pixels of pngjs, also in thedecodeoption ofcompareImages. - Gamma. pngjs sets
png.gammato0when the file has nogAMAchunk.info.gammaisnullin this case.gamma,bitDepth, and the other PNG fields exist only wheninfo.formatis'png'; check it first in TypeScript. - Chunks after the image data. pngjs applies a
tRNSchunk that comes after the image data.decodeignores it, as the PNG specification requires, so the pixels that it makes transparent stay opaque. - Files that are not valid. pngjs rejects a bad CRC in the chunks that it
reads, for example
gAMA,tRNS, andIEND.decodechecks the CRC ofIHDR,PLTE, andIDATonly, and ignores an ancillary chunk with a bad CRC. See Conversion torgba8for the other cases.
PNG compression
The encoded bytes are not identical to the bytes of pngjs, because the deflate implementations are different. The decoded pixels are identical.
The default of pngjs is not comparable to a zlib level. pngjs uses
deflateLevel: 9 with deflateStrategy: 3 (Z_RLE). With Z_RLE, zlib
finds only matches at distance 1, and the level has almost no effect: levels
1 and 9 give the same file. So the pngjs default makes large files quickly.
- Do not map the pngjs default to
'high'.'high'is zlib level 9 with the default strategy. In the measurement below, it was about twice as slow as the pngjs default and made a file that was about 6 times smaller. The ratios depend on the image: on other images,'high'was faster than the pngjs default, or not smaller than'balanced'. 'fastest'is much faster than the pngjs default (3.9 ms against 68.4 ms below), but its file can be larger than the file of pngjs, for example for line art.'balanced'is usually smaller than the pngjs default. On images with large flat areas, the pngjs default can be smaller. Measure with your own images if the file size matters.- With
deflateStrategy: 0, the levels in the table above are the same zlib levels. The files are not identical, because zlib-rs and the zlib of Node.js are different implementations.'fastest'uses the'up'filter by default, and pngjs selects the filter of each row. Addfilter: 'adaptive'to select the filter of each row.
Performance
A measurement on one Apple Silicon Mac with Node.js 24, with one synthetic 1920 × 1080 screenshot and a changed copy. pngjs wrote the PNG input files. Both packages use one thread.
The diff counts were identical. pngjs writes with the RLE strategy, which gives many short matches and makes the inflate slow. A file from this encoder decodes in 2.6 ms; pngjs decodes the same file in 17 ms.
The encoder with each compression value:
pngjs with the same image. The time also includes the filters, which pngjs runs in JavaScript, so even the fastest setting takes 55 ms.
At the same zlib level and with the adaptive filter, @swc/anyformat-image
was faster than pngjs: level 6 took 22 ms against 84 ms, and level 9 took
133 ms against 311 ms. On this image, its files were larger (208 KiB against
183 KiB, and 175 KiB against 165 KiB). On other images, they were smaller.