Skip to content

Pixel Lab: a prototype page that turns any picture into an SNES PNG - #95

Merged
skerkewitz merged 1 commit into
developfrom
pixel-lab
Nov 29, 2026
Merged

skerkewitz merged 1 commit into
developfrom
pixel-lab

Conversation

@skerkewitz-agent

Copy link
Copy Markdown
Contributor

What

A new page, tools/pixel_lab.html, that turns any picture (a photo, a painting, a picture from an image model) into an indexed PNG the SNES can show. superdevkit png then cuts it into tiles as before.

Reducing the colours of a picture is easy, but the result looks like a photo with few colours, not like SNES graphics. What makes the retro look are choices an artist makes: palettes as ramps of a few steps with shadows shifted towards blue and lights towards yellow, dithering only inside a ramp and only in gradients, a dark outline, no lone pixels, tiles that repeat. Which of them suit a picture depends on the picture, so the page offers them all and a person chooses per picture, with the result next to the original and next to a kept result B.

It is a prototype, like the BRR Lab: a single page, no server, no network, no libraries.

How

The page works through five stages, recomputing only what a changed setting touches (the downscaled picture and the palette are cached):

  1. Size and crop: target width (and height, or from the picture), zoom and pan, the 8:7 pixels of the SNES (the picture is squeezed first so it looks right on a TV). Downscaling by nearest, average, median or the dominant colour of each cell, in OKLab.
  2. Colour adjustments and palette: brightness, contrast, saturation, gamma; then 2, 4 or 8 bits a pixel, index 0 transparent or a colour. The palette is either ramps (k-means on hue and chroma finds the materials, each gets a ramp of n steps over its lightness range, with hue shift and saturation) or plain k-means. Every colour is rounded to 15 bits.
  3. Dithering: checkerboard, Bayer 2/4/8 with strength and pattern levels, optionally only between neighbouring steps of a ramp and not across edges (Sobel on lightness); or Floyd-Steinberg and Atkinson.
  4. Clean-up: removing lone pixels, an outer outline next to transparency, inner edges on the darker side, pixel-perfect lines, merging tiles that are nearly the same (flips included).
  5. Output: the budget (colours, unique tiles, VRAM, a warning above 1024 tiles), the palette (each colour editable by a click), an indexed 8-bit PNG with index 0 transparent when the picture has transparency, a JSON recipe of the settings that loads again, a preview image.

Four presets show the range: "Technical" (k-means and Floyd-Steinberg, what a plain converter gives), "Retro: sprite", "Retro: background", "Retro: soft".

The tests are Playwright tests next to the BRR Lab's, in tools/lab_test/pixel_lab.spec.js; BrrLabTest runs every spec there, so it runs them too.

Verified

  • npx playwright test pixel_lab: 9 tests pass. They cover opening a picture through the file dialog, every preset (palette within its limit, every colour exactly 15 bits), bits per pixel, transparency and the outline, editing a colour, the snapshot and view options, and the recipe round trip. The last test saves the PNG, runs the installed superdevkit png tiles on it and compares the tile count and the palette words with what the page shows.
  • ./gradlew build: 1269 tests, 12 skipped as usual, BrrLabTest (both labs) passed in 19 s. One test failed: RandomProgramTest.loopProgramsMatchGcc, seed 20786007, today's random loop program, where the MIR backend's ROM differs from gcc. That is a dampf bug on develop that today's date happens to hit, not this change (no file of the compiler is touched); it gets a change of its own. ./gradlew build -x :compiler:test passes.
  • By hand: the user tried the German first version of the page with a picture of their own. Since then only the labels changed, to English like the BRR Lab.

Not checked: Firefox and Safari (the PNG export uses CompressionStream, which both have); CI does not install Playwright, so there the tests skip as the BRR Lab's do.

What follows

  • Generated code, the C guide and its StyleGuideTest rules: unchanged; dampf is not touched.
  • The game: nothing changes for it; docs/game-findings.md got no entry.
  • Documents: tools/README.md (a row for the page), tools/lab_test/README.md, docs/testing.md (the row is now "The labs"), README.md (requirements and the folder list).
  • The distribution ships tools/ already, so the page comes with the next release.
  • Book pages that will be stale at the release: guide/getting-started.md (its table of tools/) and guide/graphics-and-sound.md (which could say how to use the page).

Open

  • One palette for the whole picture. The next step with the biggest effect for full-screen backgrounds is a palette per tile (mode 1: 8 palettes of 15 colours), the packing problem docs/studio-editor-proposal.md describes.
  • Where the conversion should live in the end: the page saves the finished PNG, so the build needs nothing but Java as before (ADR-6F60Z9QJ), and the recipe only reopens the work. If the build should one day redo a picture from its recipe, the algorithm has to be in Java too. That decision belongs in a record once the studio takes shape; this prototype does not take it.
  • BrrLabTest now runs both labs' tests; a rename (for example to LabsTest) would be a separate small change.

🤖 Generated with Claude Code

tools/pixel_lab.html converts a photo or painting into an indexed PNG
for superdevkit png: crop and downscale (8:7 pixels), colour ramps with
a hue shift or k-means, ordered or error-diffusion dithering, outlines,
merging similar tiles. A person picks the steps per picture and compares
them side by side; the page shows the budget and saves the PNG and a
recipe of its settings. One palette per picture so far.

Playwright tests in tools/lab_test (pixel_lab.spec.js), run by
BrrLabTest; the last one converts the saved PNG with the installed
superdevkit png and compares the palette.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@skerkewitz
skerkewitz merged commit c66f0f7 into develop Nov 29, 2026
3 of 4 checks passed
@skerkewitz
skerkewitz deleted the pixel-lab branch November 29, 2026 19:46
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