Introducing SWC anyformat
Hi, I'm DongYun Kang (@kdy1), the creator of SWC.
When I named SWC, I wanted it to support a large part of what the web uses. I had wanted to build that since before 2018. But it needed far too much code, so it was never realistic.
That has changed. The Rust ecosystem is now mature, so I don't have to build everything from scratch. And now there is AI. So I am finally working on the big dream I've had since I was young, and SWC anyformat is how I'm doing it.
What is SWC anyformat?
SWC anyformat processes the file formats that are common on the web, with native performance. These include image, audio, video, and font formats, and also formats such as PDF and Office documents.
It is a separate project from SWC itself.
- Maintained by the creator of SWC. I maintain it myself, and I plan to maintain it for a long time. As with SWC, I expect to keep working on it for as long as I live.
- Node.js and Python. These are the first two languages. I will add more languages when there is demand for them.
- Demand first. I build the features that people need first.
Image support comes first
At e2e.com, where I work, we needed image processing. So image support is the first part that is ready. The image package can:
- Decode and encode PNG.
- Compare images pixel by pixel, with the same results as pixelmatch 8.0.0, 7.2.0, 6.0.0, or 5.3.0.
- Resize and crop images.
It can replace pngjs and pixelmatch. See Migrate from pngjs and pixelmatch.
How fast is it?
I compared it with pngjs and pixelmatch on an Apple Silicon Mac with Node.js 24. The input was a synthetic 1920 × 1080 screenshot and a changed copy. Both packages used one thread.
| Operation | pngjs and pixelmatch 8 | @swc/anyformat-image |
|---|---|---|
| Decode PNG | 33.0 ms | 8.3 ms |
Encode PNG ('balanced') |
70.3 ms | 21.1 ms |
| Compare raw images with a diff image | 14.2 ms | 3.6 ms |
| Decode two files, compare, encode the diff | 132.0 ms | 41.1 ms |
Both gave the same diff counts.
Since then, the comparison has become much faster. It now runs in parallel on all CPU cores, and it skips pixels that are provably within the threshold. On an 18-core Apple M5 Max, comparing a 5600 × 3200 image with the pixelmatch8 algorithm went from about 200 ms on one thread to about 6 ms. The diff counts and the diff images are still byte-identical to pixelmatch.
Built not to block your app
- Node.js: Functions such as
decodeandcompareImagesreturn a promise and run on the libuv thread pool, so they do not block the event loop. Each one also has a synchronous form with theSyncsuffix. - Python: The functions release the GIL while the Rust code runs, so you can run several calls in parallel with threads.
- Parallel comparison: One call of
compareImagesuses all CPU cores.
WebAssembly build
Each Node.js package also ships a WebAssembly build. You can use it where the native binary cannot load, for example in StackBlitz WebContainers or on a platform that has no native package. It needs Node.js 20.19, 22.13, 23.5, or later. See WebAssembly build.
Getting started
Install the package of the format family that you need:
npm install @swc/anyformat-image # Node.js
pip install anyformat-image # Python
Compare two screenshots and write a diff image:
import { readFile, writeFile } from 'node:fs/promises'
import { compareImages } from '@swc/anyformat-image'
const result = await compareImages(
await readFile('expected.png'),
await readFile('actual.png'),
{ threshold: 0.1, diff: { format: 'png' } },
)
if (result.diffPixels > 0) await writeFile('diff.png', result.diffEncoded)
from pathlib import Path
import anyformat_image
result = anyformat_image.compare_images(
Path("expected.png").read_bytes(),
Path("actual.png").read_bytes(),
threshold=0.1,
diff=anyformat_image.PngEncodeOptions(),
)
if result.diff_pixels > 0 and result.diff_encoded is not None:
Path("diff.png").write_bytes(result.diff_encoded)
The documentation has the full API.
Tell me what you need
Features come in order of demand, so your requests decide what I build next. Open an issue on GitHub. If the demand is clear and the feature clearly makes things better, I will implement it.
About the author
- CEO of Delino. I created SWC, an open-source compiler project, and previously worked at Deno and Vercel.
Comments
Join the discussionNo comments yet. Be the first to comment.