All-in-one PNG image encoder/decoder in pure Rust


License: Zlib

Language: Rust

Keywords: ffi-wrapper, lodepng, png, rust-bindings, rust-interface

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.

The easiest way to use LodePNG is to include the lodepng crate. To do so, add this to your Cargo.toml:

lodepng = "2.0.0"

See API reference for details. Requires Rust 1.21 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:

rgb = "0.8"
extern crate rgb;
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,u8,u8,u8)).


let mut state = lodepng::State::new();

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();

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

Project Statistics

Sourcerank 11
Repository Size 1.84 MB
Stars 59
Forks 13
Watchers 1
Open issues 5
Dependencies 2
Contributors 15
Tags 35
Last updated
Last pushed

Top Contributors See all

Kornel dependabot-bot Alex Burka Sergey "Shnatsel" Davidoff Ruud van Asseldonk Cassie Jones petevine Caspar Krieger Campbell Barton Josh Wright Markus Becker Mark Elias McDermott Danny Spencer Eduard-Mihai Burtescu

Packages Referencing this Repo

Reading and writing PNG files without system dependencies. Pure Rust port of LodePNG.
Latest release 2.5.0 - Updated - 59 stars

Recent Tags See all

v2.4.0 August 30, 2018
v2.3.0 July 16, 2018
v2.1.5 March 19, 2018
2.1.4 March 12, 2018
v2.1.2 December 22, 2017
v2.1.1 December 15, 2017
v1.2.2 December 14, 2017
v2.0.5 November 08, 2017
v2.0.4 November 04, 2017
v2.0.2 October 27, 2017
v2.0.1 October 20, 2017
v2.0.0 October 16, 2017
v1.2.1 October 15, 2017
v1.2.0 October 15, 2017
v1.1.3 July 23, 2017

Something wrong with this page? Make a suggestion

Last synced: 2019-08-27 22:14:21 UTC

Login to resync this repository