Repository navigation
fix: write fixture images losslessly recompressed - #44
Merged
Merged
Conversation
kurkle
force-pushed
the
fix/smaller-fixture-images
branch
from
November 20, 2026 16:12
4016dc5 to
8d1b792
Compare
Member
Author
|
@etimberg thoughts on this? Could also use some library, but I'd rather not have extra deps. |
etimberg
approved these changes
Nov 20, 2026
etimberg
left a comment
Member
There was a problem hiding this comment.
This seems fine to me; we don't ship this beyond tests so there's no risk
|
🎉 This PR is included in version 1.1.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
createSaveFixtureImagewrites the reference image exactly as Chromium'scanvas.toDataURL('image/png')produced it. That encoder uses the fastest deflate setting (zlib header0x7801), so the images committed to consuming repos are several times larger than their pixels need. In chartjs-chart-sankey,issues/discussion108.pngwas 60,608 bytes when committed in 2024 and 112,223 bytes when regenerated with identical pixels;issues/multiple-flows.pngwent from 9.5 kB to 45 kB.What changes
createSaveFixtureImagepasses the PNG throughrecompressPng(newsrc/png.js, about 60 lines,node:zlibonly, 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.crc32needs Node 22.2 or later; CI runs Node 24 and the package has noenginesfield. 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-size57 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
toDataURLoutput of sankey fixtures (bytes, Node 24):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
Image.verify()passes. As the filters are untouched, equal inflated bytes are equal pixels, which the unit test also checks.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 testpasses (lint, typecheck, types, unit, Chromium and Firefox). With the repo's two reference images deleted,npm run fixtures:updaterewrote them smaller (10,421 to 7,424 bytes forline-sprited.png) and the suite passed against them. They are not changed in this PR.srcin a clone of chartjs-chart-sankey, five reference PNGs were deleted and regenerated; its browser suite passed in Chromium and Firefox (118 tests).