[PR #5101] [MERGED] feat(sprites): deterministic pixelize post-processing for AI-generated pixel art #6627

Closed
opened 2026-10-23 04:12:12 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5101
Author: @kolacheee
Created: 8/15/2026
Status: ✅ Merged
Merged: 8/15/2026
Merged by: @kolacheee

Base: staging ← Head: feat/5096-sprite-pixelize


📝 Commits (4)

  • 8d68d8a feat(sprites): deterministic pixelize post-processing route (#5096)
  • 7ec1ff0 Merge branch 'staging' into feat/5096-sprite-pixelize
  • c6b045d fix(sprites): harden pixelize against bad input and oversized derived height
  • 4ccb3dc fix(sprites): keep server resource failures out of the 400 path; test HTTP contract

📊 Changes

5 files changed (+468 additions, -1 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 package.json (+2 -1)
📝 packages/server/src/routes/sprites.routes.ts (+54 -0)
➕ packages/server/src/services/image/pixelize.service.ts (+220 -0)
➕ scripts/regressions/pixelize.regression.ts (+191 -0)

📄 Description

Closes #5096

Why

Image models asked for pixel art return approximately pixel art — anti-aliased edges, off-grid pixels, soft alpha, drifting palettes. Nothing turned that probabilistic output into an asset that can sit beside authored pixel art. (The surrounding machinery already exists: strict sheet-layout prompt contracts, real sharp.extract() slicing, provider-agnostic chroma-matte removal — this adds the missing deterministic final step, the enabler for a pixel Experience's AI art tier.)

What changed (server-only, additive)

New packages/server/src/services/image/pixelize.service.ts — pixelizeImage(input, options):

  • nearest-kernel downscale to the target cell size (aspect preserved when targetHeight is omitted);
  • palette quantization against a caller-supplied #rrggbb ramp (≤256 entries; channel-distance nearest, deliberately no perceptual weighting or dithering — determinism first);
  • binary alpha at a configurable threshold, with fully-transparent pixels normalized to rgba(0,0,0,0) so identical shapes hash identically;
  • wrap-around seam scores per axis + a tileable verdict;
  • deterministic: identical input and options always produce identical bytes (regression-pinned).
  • Input bounds enforced from the image header before any pixel allocation (4096 px/side, 16 MP — phone-memory pressure class); typed PixelizeInputError for every validation failure.
  • sharp obtained exclusively via the lazy getSharp() loader.

POST /api/sprites/pixelize (sprites.routes.ts): accepts a data-URL or raw base64 (reusing the router's existing reference-image normalizer), returns { imageBase64, report }. PixelizeInputError → 400; the sharp-unavailable case (Android/Termux without a native prebuild) → the documented 503 with the loader's actionable message, never a bare 500. Standalone like the cleanup routes so client-only capability packages can call it over REST.

Encoder gotcha, found by the regression during development: sharp's png() with effort/quality options implicitly enables palette encoding, which rewrites the RGB under fully-transparent pixels to an arbitrary shared palette entry — silently breaking the normalization guarantee. palette: false is now explicit, with a comment and a regression assertion pinning it.

New tracked lane regression:pixelize (wired into regression:sprites): byte determinism, ramp-only opaque pixels, strict binary alpha, normalized transparency, tileable/split seam verdicts, and typed rejections (oversized input, bad targetWidth, malformed palette entries).

Validation

  • pnpm check
  • pnpm regression:sprites (includes the new regression:pixelize)
  • pnpm version:check (root package.json diff is scripts-only)
  • Manually verify a generated sprite/tile pixelizes: POST an image with a ramp and confirm the output is on-grid, palette-locked, hard-alpha, and the seam report matches visual tileability.

Summary by CodeRabbit

  • New Features

    • Added an image pixelization endpoint that converts images into deterministic pixel art.
    • Supports target dimensions, optional color palettes, alpha thresholds, and wrap-around tileability metrics.
    • Returns the processed PNG with dimensions, palette, seam, and tileability details.
  • Bug Fixes

    • Added validation and clear errors for invalid images, dimensions, palettes, and oversized inputs.
  • Tests

    • Added regression coverage for deterministic output, transparency handling, palette quantization, error handling, and tileability scoring.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/5101 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/15/2026 **Status:** ✅ Merged **Merged:** 8/15/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `feat/5096-sprite-pixelize` --- ### 📝 Commits (4) - [`8d68d8a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8d68d8a0a57b861547c81d8ddea9cba8585b3af2) feat(sprites): deterministic pixelize post-processing route (#5096) - [`7ec1ff0`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7ec1ff0610d433909ec1581240a3a3c0d11c798c) Merge branch 'staging' into feat/5096-sprite-pixelize - [`c6b045d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c6b045d9fff766e54118c5bb987694957e374b83) fix(sprites): harden pixelize against bad input and oversized derived height - [`4ccb3dc`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4ccb3dc04de7f833b231d18f58cfffbfd9922331) fix(sprites): keep server resource failures out of the 400 path; test HTTP contract ### 📊 Changes **5 files changed** (+468 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `package.json` (+2 -1) 📝 `packages/server/src/routes/sprites.routes.ts` (+54 -0) ➕ `packages/server/src/services/image/pixelize.service.ts` (+220 -0) ➕ `scripts/regressions/pixelize.regression.ts` (+191 -0) </details> ### 📄 Description Closes #5096 ## Why Image models asked for pixel art return *approximately* pixel art — anti-aliased edges, off-grid pixels, soft alpha, drifting palettes. Nothing turned that probabilistic output into an asset that can sit beside authored pixel art. (The surrounding machinery already exists: strict sheet-layout prompt contracts, real `sharp.extract()` slicing, provider-agnostic chroma-matte removal — this adds the missing deterministic final step, the enabler for a pixel Experience's AI art tier.) ## What changed (server-only, additive) **New `packages/server/src/services/image/pixelize.service.ts`** — `pixelizeImage(input, options)`: - nearest-kernel downscale to the target cell size (aspect preserved when `targetHeight` is omitted); - palette quantization against a caller-supplied `#rrggbb` ramp (≤256 entries; channel-distance nearest, deliberately no perceptual weighting or dithering — determinism first); - binary alpha at a configurable threshold, with fully-transparent pixels normalized to `rgba(0,0,0,0)` so identical shapes hash identically; - wrap-around seam scores per axis + a `tileable` verdict; - **deterministic**: identical input and options always produce identical bytes (regression-pinned). - Input bounds enforced from the image header **before** any pixel allocation (4096 px/side, 16 MP — phone-memory pressure class); typed `PixelizeInputError` for every validation failure. - sharp obtained exclusively via the lazy `getSharp()` loader. **`POST /api/sprites/pixelize`** (`sprites.routes.ts`): accepts a data-URL or raw base64 (reusing the router's existing reference-image normalizer), returns `{ imageBase64, report }`. `PixelizeInputError` → 400; the sharp-unavailable case (Android/Termux without a native prebuild) → the documented 503 with the loader's actionable message, never a bare 500. Standalone like the cleanup routes so client-only capability packages can call it over REST. **Encoder gotcha, found by the regression during development**: sharp's `png()` with `effort`/`quality` options implicitly enables **palette encoding**, which rewrites the RGB under fully-transparent pixels to an arbitrary shared palette entry — silently breaking the normalization guarantee. `palette: false` is now explicit, with a comment and a regression assertion pinning it. **New tracked lane** `regression:pixelize` (wired into `regression:sprites`): byte determinism, ramp-only opaque pixels, strict binary alpha, normalized transparency, tileable/split seam verdicts, and typed rejections (oversized input, bad `targetWidth`, malformed palette entries). ## Validation - [x] `pnpm check` - [x] `pnpm regression:sprites` (includes the new `regression:pixelize`) - [x] `pnpm version:check` (root `package.json` diff is scripts-only) - [x] Manually verify a generated sprite/tile pixelizes: POST an image with a ramp and confirm the output is on-grid, palette-locked, hard-alpha, and the seam report matches visual tileability. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added an image pixelization endpoint that converts images into deterministic pixel art. * Supports target dimensions, optional color palettes, alpha thresholds, and wrap-around tileability metrics. * Returns the processed PNG with dimensions, palette, seam, and tileability details. * **Bug Fixes** * Added validation and clear errors for invalid images, dimensions, palettes, and oversized inputs. * **Tests** * Added regression coverage for deterministic output, transparency handling, palette quantization, error handling, and tileability scoring. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:12:12 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#6627
No description provided.