Rust version of LodePNG

This is a pure Rust PNG image decoder and encoder. Allows easy reading and writing of PNG files without any system dependencies.

To use the lodepng crate, add to your Cargo.toml:

toml [dependencies] lodepng = "2.6.0"

See API reference for details. Requires Rust 1.44 or later.

Loading image example

rust let image = lodepng::decode32_file("in.png")?;

returns image of type lodepng::Bitmap<lodepng::RGBA<u8>> with fields .width, .height, and .buffer (the buffer is a Vec).

The RGB/RGBA pixel types are from the RGB crate, which you can import separately to use the same pixel struct throughout the program, without casting. But if you want to read the image buffer as bunch of raw bytes, ignoring the RGB(A) pixel structure, use:

toml [dependencies] rgb = "0.8"

rust use rgb::*; … let bytes: &[u8] = image.buffer.as_bytes();

Saving image example

rust lodepng::encode32_file("out.png", &buffer, width, height)

The buffer can be a slice of any type as long as it has 4 bytes per element (e.g. struct RGBA or [u8; 4]).

Advanced

```rust let mut state = lodepng::Decoder::new(); state.rememberunknownchunks(true);

match state.decode("in.png") { Ok(lodepng::Image::RGB(image)) => {…} Ok(lodepng::Image::RGBA(image)) => {…} Ok(lodepng::Image::RGBA16(image)) => {…} Ok(lodepng::Image::Gray(image)) => {…} Ok(_) => {…} Err(err) => {…} }

for chunk in state.infopng().unknownchunks() { println!("{:?} = {:?}", chunk.name(), chunk.data()); }

// Color profile (to be used with e.g. LCMS2) let iccdata = state.infopng().get_icc(); ```

See load_image crate for an example how to use lodepng with color profiles.

Upgrading from 2.x

Upgrading from 1.x

Origin of the Rust version

This codebase is derived from C LodePNG by Lode Vandevenne. It has been converted to Rust using Citrus C to Rust converter and manual refactorings.