Skip to content

Leave a palette with no opaque entry unrefined, instead of panicking - #9

Open
lndgalante wants to merge 1 commit into
heydeer:mainfrom
lndgalante:refine-without-opaque-entries
Open

lndgalante wants to merge 1 commit into
heydeer:mainfrom
lndgalante:refine-without-opaque-entries

Conversation

@lndgalante

Copy link
Copy Markdown

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 --target of 62 or more:

$ slimdeer fade.png --target 70
thread 'main' panicked at src/color.rs:215:20:
index out of bounds: the len is 0 but the index is 0
   3: <slimdeer::color::Nearest>::find
   4: … slimdeer::palette::refine_palette::{closure#3} …
   5: <slimdeer::search::Search>::candidate
   6: <slimdeer::search::Search>::evaluate
   7: <slimdeer::search::Search>::upgrade
   8: <slimdeer::search::Search>::at_top

On a 320×240 fade (script at the end), at main (eafae5e):

--target PNG output
automatic (58.32), 59 hit: 15,326 bytes, d50, score 59.122
60, 61 miss, the same file
62, 70, 80, 90 panic

WebP 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_palette looks up each opaque color's nearest entry among the palette's opaque entries. This image's 256-color plain palette has none, so Nearest is built from an empty list and find indexes order[0].

The refined palette is one of the extras upgrade tries 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_palette returns 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:

--target PNG output
automatic, 59, 60, 61 as before (the same bytes at automatic and 60, the two I compared)
62, 70 hit: 20,675 bytes, d100, score 70.454
80, 90 miss, the same file

WebP 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_opaque in tests/compress.rs: a 160×120 fade at target 70, through compress.

Both fail on main with the panic above. cargo fmt --all --check, cargo clippy --release --all-targets -- -D warnings (also with each optional feature on its own) and cargo test --release pass on macOS arm64 with Rust 1.100.0.

make-fade.mjs, which writes the 320×240 image (153,609 bytes)
// node make-fade.mjs  ->  fade.png
import { writeFileSync } from "node:fs";
import { deflateSync, crc32 } from "node:zlib";
const chunk = (type, data) => {
  const out = Buffer.alloc(12 + data.length);
  out.writeUInt32BE(data.length, 0);
  out.write(type, 4, "latin1");
  data.copy(out, 8);
  out.writeUInt32BE(crc32(out.subarray(4, 8 + data.length)), 8 + data.length);
  return out;
};
const [width, height] = [320, 240];
const raw = Buffer.alloc((width * 4 + 1) * height);
for (let y = 0; y < height; y++) {
  for (let x = 0; x < width; x++) {
    raw.set([(x * 255) / width, (y * 255) / height, 128, 255 - (x * 255) / width], y * (width * 4 + 1) + 1 + x * 4);
  }
}
const header = Buffer.alloc(13);
header.writeUInt32BE(width, 0);
header.writeUInt32BE(height, 4);
header.set([8, 6], 8);
writeFileSync("fade.png", Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk("IHDR", header), chunk("IDAT", deflateSync(raw)), chunk("IEND", Buffer.alloc(0))]));

🤖 Generated with Claude Code

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>
@vercel

vercel Bot commented Nov 18, 2026

Copy link
Copy Markdown

@lndgalante is attempting to deploy a commit to the Adastralab AI Team on Vercel.

A member of the Team first needs to authorize it.

@heydeer heydeer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔎 Review completed. 1 finding needs attention.

Fix with HeyDeer Walkthrough

ℹ️ About this review
  • Risk level: low. The PR adds a small early return in refine_palette that 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 →

Comment thread src/palette.rs
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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 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

Fix with HeyDeer

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

lndgalante added a commit to lndgalante/oxlint-plugin-slimdeer that referenced this pull request Nov 18, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant