Repository navigation
Leave a palette with no opaque entry unrefined, instead of panicking - #9
lndgalante wants to merge 1 commit into
Conversation
refine_palette looks up each opaque color's nearest entry among the palette's opaque entries. An image that is translucent nearly everywhere (one opaque column, then a fade to transparent) gets a 256-color palette with no opaque entry, so the lookup ran on an empty list and Nearest::find indexed past it: "index out of bounds: the len is 0 but the index is 0" at src/color.rs:215. The refined palette is an extra the search tries when the target is far out of reach at 256 colors, so it took a fixed --target to get there: on a 320×240 fade, 62 and up panicked, 61 and the automatic target didn't. WebP output of a PNG runs the same search, and panicked with it. With the palette returned as it is, 62 and 70 are hits on that image (full dithering scores 70.45) and 90 is a miss. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
@lndgalante is attempting to deploy a commit to the Adastralab AI Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
🔎 Review completed. 1 finding needs attention.
ℹ️ About this review
- Risk level: low. The PR adds a small early return in
refine_palettethat hands back the palette unchanged when it has no opaque entry, replacing a panic, so a missed mistake would only affect how one edge-case image is quantized and is easy to revert. - Review depth: Standard
- Trigger source: Automatic (PR opened)
- View in HeyDeer · Config
HeyDeer reviews public repositories for free. Set it up for your open-source project →
| let mut centers: Vec<Color> = palette.iter().filter(|c| c.a == 255).map(|&c| to_color(c)).collect(); | ||
| if centers.is_empty() { | ||
| // nothing to refine: an image translucent nearly everywhere gets no opaque entry | ||
| return palette.to_vec(); |
There was a problem hiding this comment.
🔵 Low (Performance): Skip evaluating an unchanged refined palette
When a plain palette has no opaque entry and the search reaches the refined extra, Search::candidate repeats the full-image remap and scoring for an identical palette. This adds avoidable processing without improving the selected output.
Learn more
PLAIN and REFINED both use dither: 100 and rdo: None. The new return preserves the plain palette exactly, but evaluations are cached separately by variant, so candidate remaps it again and evaluate invokes score_of again.
upgrade considers PLAIN first and requires extra_score > best.1, so the identical refined result cannot replace it. This duplicates image-wide color conversion, dithering and SSIMULACRA2 comparison, including per-frame work for animations. Encoding is not necessarily repeated: the strict score comparison short-circuits before encoded for the tied result.
Suggested fix: Reuse the plain evaluation or skip the refined extra when refinement returns an unchanged palette, while preserving the guard against an empty opaque palette.
Affected code: src/search.rs:932–938
There was a problem hiding this comment.
That's right: with no opaque entry, the refined extra repeats the plain one's remap and scoring, and it can't win. It only happens on images that panicked before, so I kept this change to refine_palette alone. I can add a skip for that case here if you'd like it.
The four engine bugs the docs describe are now reported upstream: the fade panic (heydeer/slimdeer#9) and the flat-color JPEGs (#10) as pull requests with a fix, the dropped eXIf orientation (#11) and the gray profile on a palette PNG (#12) as issues. The 0.6.0 notes' known limits and the rule page's skip list now link to them. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>


Found while testing oxlint-plugin-slimdeer, which runs slimdeer's wasm build.
The bug
A PNG that is translucent nearly everywhere (one opaque column, then a fade to transparent) panics at a fixed
--targetof 62 or more:On a 320×240 fade (script at the end), at
main(eafae5e):--targetd50, score 59.122WebP output of the same PNG panics at the same targets, since it runs the PNG search too. The wasm build throws
RuntimeError: unreachable.The cause
refine_palettelooks up each opaque color's nearest entry among the palette's opaque entries. This image's 256-color plain palette has none, soNearestis built from an empty list andfindindexesorder[0].The refined palette is one of the extras
upgradetries when the target is out of reach at 256 colors, which is why it takes a fixed target. At 60 and 61 the plain extra is already over the allowance, and that rules the others out before they are made.The fix
refine_palettereturns the palette as it is when it has no opaque entry: there is nothing for it to move. A palette with an opaque entry takes the same path as before.On the same image, with the fix:
--targetd100, score 70.454WebP output works at 62, 70, 80 and 90 too.
Tests
palette::tests::refining_needs_an_opaque_entry: the function with and without an opaque entry.misses_where_hardly_a_pixel_is_opaqueintests/compress.rs: a 160×120 fade at target 70, throughcompress.Both fail on
mainwith the panic above.cargo fmt --all --check,cargo clippy --release --all-targets -- -D warnings(also with each optional feature on its own) andcargo test --releasepass on macOS arm64 with Rust 1.100.0.make-fade.mjs, which writes the 320×240 image (153,609 bytes)
🤖 Generated with Claude Code