Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ compare-codecs <image-or-dir...> [options]
--max-iterations <n> Max search steps per codec (default: 10)
--effort <0..10> Matched encoder effort across codecs (jxl/avif/webp); higher = slower
--time-runs <n> Time each codec's encode n times, report the median (default: 1)
--target-bytes <size> Budget mode: search every codec for the largest file that
fits this size (50KB = 50,000 bytes, 50KiB = 51,200, or a
bare byte count) and compare the ssimulacra2 each reaches.
Replaces --target; not available with --rd or --html
--rd <list> Rate-distortion mode: search every codec at each target in the
list, report bpp per target and BD-rate vs mozjpeg; with
--html, the report plots these curves
Expand All @@ -96,6 +100,9 @@ compare-codecs images/ --target 90 --csv results.csv
# Interactive report: wipe, format toggle, difference view, rate–distortion chart, quality slider
compare-codecs images/ --target 90 --html report.html

# Budget: the best quality each codec reaches within 30 KB (equal size, compare quality)
compare-codecs images/ --target-bytes 30KB --csv budget.csv

# Rate-distortion: bpp at several quality targets and BD-rate vs mozjpeg
compare-codecs images/ --rd 70,80,85,90,95 --csv rd.csv

Expand Down
5 changes: 3 additions & 2 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ This is a lightweight list of ideas under consideration. Nothing here is committ

## Status

The current state is stable: `compare-codecs` supports six codecs (JPEG XL, AVIF, WebP, jpegli, mozjpeg, HEIC) with equal-quality search, batch mode, CSV output, per-codec encode timing, matched encoder effort (`--effort`), a rate-distortion mode (`--rd`) with BD-rate against mozjpeg, and a self-contained interactive HTML report with a difference view and a rate–distortion chart that, with `--rd`, plots the equal-quality curves and BD-rates. All three test layers (unit, gated integration, and the image E2E build) run in CI, alongside a size-regression gate against a committed baseline that comments on pull requests, and a weekly comparison report on the demo images, a set of seven covering photographs, a flat illustration, a high-detail texture, a UI screenshot and a diagram. The comparison and the regression gate are also packaged as a GitHub Action, [Compare image codecs](https://github.com/nucliweb/compare-codecs-action), for use on other repositories' images.
The current state is stable: `compare-codecs` supports six codecs (JPEG XL, AVIF, WebP, jpegli, mozjpeg, HEIC) with equal-quality search, batch mode, CSV output, per-codec encode timing, matched encoder effort (`--effort`), a rate-distortion mode (`--rd`) with BD-rate against mozjpeg, a budget mode (`--target-bytes`) that compares quality at equal file size, and a self-contained interactive HTML report with a difference view and a rate–distortion chart that, with `--rd`, plots the equal-quality curves and BD-rates. All three test layers (unit, gated integration, and the image E2E build) run in CI, alongside a size-regression gate against a committed baseline that comments on pull requests, and a weekly comparison report on the demo images, a set of seven covering photographs, a flat illustration, a high-detail texture, a UI screenshot and a diagram. The comparison and the regression gate are also packaged as a GitHub Action, [Compare image codecs](https://github.com/nucliweb/compare-codecs-action), for use on other repositories' images.

## Ideas

### Features

- **Target-size mode.** Answer the inverse question of the equal-quality search: given a byte budget (`--target-bytes 50KB`), which codec looks best? The search runs on each codec's quality knob towards a file size instead of a score, then reports the ssimulacra2 each one reaches at that size. This is the question a web performance budget asks. The budget should state its unit explicitly, KB (1000 bytes) or KiB (1024 bytes), and the result should say whether it was met. A later step could add downscaling as a second lever, for budgets that quality alone cannot reach.
- **Rate-distortion curves by default in the HTML report.** Without `--rd`, the report's chart is still a coarse fixed-knob sweep. Making the equal-quality curves the default would drop the sweep, at the cost of one search per target instead of five fixed encodes per codec.
- **More codecs.** Extend beyond the current six as encoders mature or new ones become relevant. Low effort per codec, since the equal-quality search already generalises across quality knobs. A candidate is [jixel](https://github.com/awxkee/jixel), a second JPEG XL encoder (Rust, BSD-3-Clause, ported from libjxl-tiny) that [JPEG XL vs AVIF for web use-cases](https://jakearchibald.com/2026/jpeg-xl-vs-avif/) finds much closer to AVIF than libjxl on photographs; as an opt-in variant it would repeat the rav1e and SVT-AV1 lesson for JPEG XL. It has no CLI, so the image would build a small wrapper.
- **Tool versions in the results.** Write the encoder, decoder and metric versions into the CSV and the HTML report, so a result read outside the Docker image still says which builds produced it, together with the SHA-256 of each input image. A JSON output with a `schemaVersion` field would carry the same provenance for scripts and the GitHub Action, which today parse the CSV.
- **Output format check.** Verify each encoder's output by its magic bytes (the `ftyp` brand for AVIF and HEIC, `RIFF`/`WEBP`, the JPEG XL signature, `FF D8 FF`), so an encoder that silently writes another container fails with a clear message instead of a decoder error.
- **An independent second metric.** Optionally score every result with [ColorVideoVDP](https://github.com/gfxdisp/ColorVideoVDP), from a lab with no tie to any codec, as a cross-check on ssimulacra2 where JPEG XL and another format are close. It is a Python and PyTorch install of about 1 GB, so it would stay outside the default image.
- **Decode with platform decoders.** Measure what users actually see: decode with the platform's own decoder (Apple ImageIO through `sips`, browsers) as well as the reference decoders, and time the decode. The HEIC 4:2:0 lesson already shows a 1.5–8.4 point gap between libheif and ImageIO, and decode speed is part of the trade-off ([JPEG XL vs AVIF for web use-cases](https://jakearchibald.com/2026/jpeg-xl-vs-avif/) measures it in Firefox, Chrome and Safari).

- **Budget mode in the HTML report.** `--target-bytes` prints tables and a CSV; the report compares codecs at equal quality, so budget runs have no visual comparison yet. A later step could add downscaling as a second lever, for budgets that quality alone cannot reach.

### Demo material

- **An image with alpha.** The seven demo images are all opaque RGB. Transparency changes the comparison (some encoders store alpha losslessly, which [JPEG XL vs AVIF for web use-cases](https://jakearchibald.com/2026/jpeg-xl-vs-avif/) shows costing jixel 136% against AVIF), so the set should include one image with an alpha channel.
Expand Down
36 changes: 36 additions & 0 deletions compare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Options:
| `--max-iterations <n>` | `10` | Maximum search steps per codec |
| `--effort <0..10>` | codec default | Matched encoder effort across codecs that have the knob (higher = slower) |
| `--time-runs <n>` | `1` | Time each codec's encode n times and report the median |
| `--target-bytes <size>` | — | [Budget mode](#budget-mode): search each codec for the largest file that fits this size (`50KB` = 50,000 bytes, `50KiB` = 51,200, or a bare byte count) and compare quality; replaces `--target`, not available with `--rd` or `--html` |
| `--rd <list>` | — | Rate-distortion mode: search each codec at every target in the list; with `--html`, the report shows the encodes at the target closest to `--target` |
| `--csv <path>` | — | Also write the results as CSV |
| `--html <path>` | — | Write an interactive, self-contained HTML report |
Expand Down Expand Up @@ -84,6 +85,39 @@ compare-codecs images/ --rd 70,80,85,90,95 --html rd.html

Its chart dots are the equal-quality encodes at each target, the same points as the table and the CSV, and the readout under the chart and the across-all-images summary add each codec's BD-rate and mean BD-rate. The viewer shows each codec's encode at the target closest to `--target` (90 by default), so pass a list that includes the quality you want to inspect.

## Budget mode

The default comparison fixes the quality and compares sizes. A web performance budget asks the inverse question: with **50 KB to spend**, which codec looks best? `--target-bytes` searches each codec's quality knob for the largest file that fits the budget, then compares the ssimulacra2 each one reaches there:

```bash
compare-codecs images/ --target-bytes 30KB --csv budget.csv
```

The budget takes its unit explicitly: `KB` and `MB` are decimal (`30KB` is 30,000 bytes), `KiB` and `MiB` are binary (`30KiB` is 30,720 bytes), and a bare number is a byte count. Each table states the budget both ways, gives sizes in exact bytes, and ranks codecs by quality, best first:

```
Budget: 30 KB = 30,000 bytes (equal size; compare quality, higher ssimulacra2 is better).

+---------+--------------+----------+-----------+-------+-------------+---------+-------+--------+--------+----------+-----------+-----------+
| Codec | Setting | Size | of budget | bpp | ssimulacra2 | dssim | iters | encode | cpu | memory | enc total | cpu total |
+---------+--------------+----------+-----------+-------+-------------+---------+-------+--------+--------+----------+-----------+-----------+
| AVIF | --min/max 31 | 29,224 B | 97.4% | 0.244 | 76.52 | 0.00105 | 6 | 530 ms | 660 ms | 135.4 MB | 5.92 s | 7.45 s |
| HEIC | -q 29 | 29,263 B | 97.5% | 0.244 | 61.80 | 0.00259 | 5 | 290 ms | 280 ms | 42.2 MB | 3.17 s | 3.13 s |
| JPEG XL | -d 8.95 | 29,817 B | 99.4% | 0.248 | 54.81 | 0.00407 | 7 | 90 ms | 500 ms | 95.3 MB | 740 ms | 3.80 s |
| WebP | -q 33 | 29,934 B | 99.8% | 0.249 | 48.69 | 0.00486 | 6 | 40 ms | 40 ms | 8.6 MB | 280 ms | 250 ms |
| mozjpeg | -quality 27 | 29,885 B | 99.6% | 0.249 | 31.16 | 0.01084 | 6 | 40 ms | 30 ms | 7.9 MB | 270 ms | 240 ms |
| jpegli | -d 14.97 | 30,395 B | 101.3% * | 0.253 | 23.55 | 0.01330 | 9 | 20 ms | 10 ms | 13.6 MB | 210 ms | 130 ms |
+---------+--------------+----------+-----------+-------+-------------+---------+-------+--------+--------+----------+-----------+-----------+

* cannot fit the budget even at its lowest quality setting; smallest file shown.
```

That is `report-ui.png`, the 1200×800 UI screenshot, in the Docker image. At 30 KB, AVIF keeps it at ssimulacra2 76.5, while mozjpeg falls to 31.2. `of budget` is the share of the budget the file uses: the search stops within 1% under the budget, but a codec whose quality knob moves in coarse steps can land lower, since the next step up would exceed it. A file over the budget is never chosen while one under it exists. When even the codec's lowest quality setting is over the budget, as for jpegli here, the row shows that smallest file, flagged `*`, and ranks after every codec that fits.

With more than one image, a **budget summary** follows: each codec's average ssimulacra2, its wins (the best quality among the codecs that fit an image), its mean difference in ssimulacra2 points from mozjpeg, and on how many images it fits. The averages and the difference cover only the images where **every** codec fits, the same images for all, and the summary says how many those are: averaging each codec over its own images would rank them on different sets and reward a codec for failing to fit the hardest ones. `--csv` writes one row per image and codec (`image,codec,setting,budget_bytes,bytes,bpp,ssimulacra2,dssim,fits,iterations`, then the timing columns and `error`).

The search only needs the file size, so it encodes without decoding or measuring, and decodes and scores once, at the chosen setting; on the seven demo images, a 30 KB budget run takes about half as long as an equal-quality run at the default target (29 s against 63 s on an Apple Silicon laptop). `--codecs`, `--effort`, `--time-runs` and `--max-iterations` apply as usual; `--tolerance` is a ssimulacra2 tolerance and does not apply. The HTML report compares codecs at equal quality, so `--html` is not available in budget mode.

## Interactive HTML report

`--html report.html` produces a single self-contained file (images embedded, no
Expand Down Expand Up @@ -374,6 +408,8 @@ the PNG to its critical chunks (so every codec's output is readable by ssimulacr
measure, and adjust. If the target is outside a codec's reachable range, it settles at
the closest bound (visible as `iters` hitting the max with the knob pinned).

In [budget mode](#budget-mode) the same knob is searched against the encoded size instead, which grows with quality in the same direction as the score: encode, read the size, and adjust towards the largest file under the budget.

## Scope and assumptions

- Input is a PNG in sRGB. Ancillary chunks (ICC profile, chromaticities) are stripped
Expand Down
Loading