Repository navigation
Add pure-Rust PNG encoder (encode_png / save_image) - #11
Merged
Merged
Conversation
- 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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]u8tensor to a.pngfile.crc32/adler32are promoted from the test module to shared helpers (as suggested in the issue).adler32now reduces modulo once per 5552 bytes instead of per byte.Indexed-palette (
PLTE+tRNS) and 16-bit decoding are already handled bydecode_pngand covered bydecodes_palette_low_bit_depth_and_16_bit_pngs, so no decoder changes were needed.Tests
New unit tests in
image_codec:encode_png->decode_pnground trip for 1-4 channelssave_image->load_imageround trip, plus rejection of a non-[C, H, W]tensorI also checked that Pillow opens an RGBA PNG produced by
encode_pngand reads back the same pixels.🤖 Generated with Claude Code