#PNG #encoder #decoder #lodepng

lodepng

Reading and writing PNG files without system dependencies. Pure Rust port of LodePNG.

59 releases (36 stable)

3.2.1 Aug 16, 2020
3.0.0 Jul 6, 2020
2.7.3 Aug 16, 2020
2.7.2 Jul 6, 2020
0.3.1 Feb 8, 2015

#3 in Images

Download history 1148/week @ 2020-05-29 1154/week @ 2020-06-05 1396/week @ 2020-06-12 1040/week @ 2020-06-19 1102/week @ 2020-06-26 1513/week @ 2020-07-03 1210/week @ 2020-07-10 1079/week @ 2020-07-17 1061/week @ 2020-07-24 1468/week @ 2020-07-31 1631/week @ 2020-08-07 1203/week @ 2020-08-14 1056/week @ 2020-08-21 1323/week @ 2020-08-28 1268/week @ 2020-09-04 1193/week @ 2020-09-11

5,472 downloads per month
Used in 25 crates (19 directly)

BSD-3-Clause

195KB
4K SLoC

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:

[dependencies]
lodepng = "2.6.0"

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

Loading image example

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:

[dependencies]
rgb = "0.8"
use rgb::*;let bytes: &[u8] = image.buffer.as_bytes();

Saving image example

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

let mut state = lodepng::Decoder::new();
state.remember_unknown_chunks(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.info_png().unknown_chunks() {
    println!("{:?} = {:?}", chunk.name(), chunk.data());
}

// Color profile (to be used with e.g. LCMS2)
let icc_data = state.info_png().get_icc();

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

Upgrading from 2.x

  • C FFI still exists, but is no longer ABI-compatible with the original C lodepng due to layout changes in structs.
  • Structs use bool where appropriate instead of 0/1 int.
  • Custom zlib callbacks use io::Write instead of malloc-ed buffers (remember to use write_all, not write!)
  • ffi::Error has been renamed to ffi::ErrorCode.
  • Compression level is set via set_level() function instead of individual CompressConfig fields.

Upgrading from 1.x

  • CVec has been replaced with a regular Vec. Delete extra .as_ref() that the compiler may complain about.
  • LCT_* constants have been changed to ColorType::*.
  • Chunk/Chunks renamed to ChunkRef/ChunksIter.
  • auto_convert is a boolean.
  • bitdepth has a getter/setter.
  • There is no C any more!

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.

Dependencies

~360–520KB