Skip to content

fix: write fixture images losslessly recompressed - #44

Merged
kurkle merged 1 commit into
chartjs:masterfrom
kurkle:fix/smaller-fixture-images
Nov 20, 2026
Merged

kurkle merged 1 commit into
chartjs:masterfrom
kurkle:fix/smaller-fixture-images

Conversation

@kurkle

@kurkle kurkle commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Problem

createSaveFixtureImage writes the reference image exactly as Chromium's canvas.toDataURL('image/png') produced it. That encoder uses the fastest deflate setting (zlib header 0x7801), so the images committed to consuming repos are several times larger than their pixels need. In chartjs-chart-sankey, issues/discussion108.png was 60,608 bytes when committed in 2024 and 112,223 bytes when regenerated with identical pixels; issues/multiple-flows.png went from 9.5 kB to 45 kB.

What changes

createSaveFixtureImage passes the PNG through recompressPng (new src/png.js, about 60 lines, node:zlib only, no new dependency) before writing it. It inflates the IDAT data and deflates it again at level 9. The scanline filters are not touched, so bit depth, colour type and interlacing do not matter. The IDAT chunks become one chunk with a correct CRC; every other chunk is copied through byte for byte in its original order.

Fallback: a bad signature, a truncated chunk, an inflate error, or a result that is not smaller returns the original bytes. It never throws.

zlib.crc32 needs Node 22.2 or later; CI runs Node 24 and the package has no engines field. The README sentence states the requirement.

Level 9 was chosen over 6 to 8 because level 8 is up to 19% larger on some images (node-min-size 57 kB against 48 kB). It costs 10 to 40 ms on typical charts and about 0.5 s on a 1024x1024 image of noise.

Measurements

Fresh Chromium toDataURL output of sankey fixtures (bytes, Node 24):

image browser now time
basic 107,004 23,395 39 ms
border 107,366 23,256 37 ms
labels 121,988 25,491 32 ms
cash 30,064 24,264 14 ms
multiple-flows 45,379 13,617 10 ms
discussion108 75,950 65,827 33 ms
node-min-size 461,780 47,982 82 ms
issue99 482,613 238,202 268 ms
energy 890,881 438,839 377 ms
issue25 663,342 466,276 389 ms
issue72 1,953,584 1,766,110 503 ms

Re-filtering the rows as well (an earlier version of this PR) would shrink flat charts roughly another 1.5 to 2 times, at the price of several hundred lines of PNG code. It was left out on purpose.

Verification

  • Pixel identity: all images above plus six committed sankey fixtures were decoded with Pillow before and after and compared byte for byte in RGBA (17 of 17 identical); Image.verify() passes. As the filters are untouched, equal inflated bytes are equal pixels, which the unit test also checks.
  • Unit tests (test/unit/png.test.js): the rows survive, several IDAT chunks in give one out with a correct CRC, other chunks preserved in order, and fallbacks (not a PNG, truncated, corrupt IDAT, not smaller) return the same buffer.
  • npm test passes (lint, typecheck, types, unit, Chromium and Firefox). With the repo's two reference images deleted, npm run fixtures:update rewrote them smaller (10,421 to 7,424 bytes for line-sprited.png) and the suite passed against them. They are not changed in this PR.
  • Consumer check: with this src in a clone of chartjs-chart-sankey, five reference PNGs were deleted and regenerated; its browser suite passed in Chromium and Firefox (118 tests).

@kurkle
kurkle force-pushed the fix/smaller-fixture-images branch from 4016dc5 to 8d1b792 Compare November 20, 2026 16:12
@kurkle

kurkle commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

@etimberg thoughts on this? Could also use some library, but I'd rather not have extra deps.

@etimberg etimberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine to me; we don't ship this beyond tests so there's no risk

@kurkle
kurkle merged commit 12417a7 into chartjs:master Nov 20, 2026
3 of 4 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants