Skip to content

Add pure-Rust PNG encoder (encode_png / save_image) - #11

Merged
voidstackloop merged 1 commit into
voidstackloop:mainfrom
emreocell:feat/png-encoder
Nov 24, 2026
Merged

voidstackloop merged 1 commit into
voidstackloop:mainfrom
emreocell:feat/png-encoder

Conversation

@emreocell

Copy link
Copy Markdown
Contributor

Closes #4

What

  • encode_png(pixels, width, height, channels) -> Result<Vec<u8>>: encodes 8-bit Grayscale / Grayscale+Alpha / RGB / RGBA into a valid PNG (IHDR / IDAT / IEND) using stored DEFLATE blocks, so it stays zero-dependency. It validates the channel count, non-zero dimensions, size overflow and buffer length.
  • save_image(&Tensor, path): writes a [C, H, W] u8 tensor to a .png file.
  • crc32 / adler32 are promoted from the test module to shared helpers (as suggested in the issue). adler32 now reduces modulo once per 5552 bytes instead of per byte.

Indexed-palette (PLTE + tRNS) and 16-bit decoding are already handled by decode_png and covered by decodes_palette_low_bit_depth_and_16_bit_pngs, so no decoder changes were needed.

Tests

New unit tests in image_codec:

  • encode_png -> decode_png round trip for 1-4 channels
  • images large enough to need several stored blocks (>65535 bytes)
  • invalid input is rejected
  • save_image -> load_image round trip, plus rejection of a non-[C, H, W] tensor
cargo test -p oxideon-core --lib image_codec   # 7 passed
cargo clippy -p oxideon-core --all-targets -- -D warnings   # clean

I also checked that Pillow opens an RGBA PNG produced by encode_png and reads back the same pixels.

🤖 Generated with Claude Code

- encode_png: 8-bit gray / gray+alpha / RGB / RGBA -> valid PNG using
  stored DEFLATE blocks (zero dependencies), with input validation
- save_image: writes a [C, H, W] u8 Tensor to a PNG file
- promote crc32/adler32 from the test module to shared helpers
- tests: encode -> decode round trips for 1-4 channels, multi-block
  streams, invalid input, and save_image -> load_image

Palette (PLTE/tRNS) and 16-bit decoding were already supported by
decode_png and are covered by existing tests.

Refs voidstackloop#4

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 15:02

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@voidstackloop
voidstackloop merged commit 2f10c8f into voidstackloop:main Nov 24, 2026
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.

Add pure-Rust PNG encoding (encode_png / save_image) and 16-bit / palette PNG decoding

3 participants