png_pong 0.5.0

A pure Rust PNG/APNG encoder & decoder

PNG Pong

A pure Rust PNG/APNG encoder & decoder

Build Status Docs

This is a pure Rust PNG image decoder and encoder based on lodepng. This crate allows easy reading and writing of PNG files without any system dependencies.

Why another PNG crate?

These are the 3 Rust PNG encoder/decoder crates I know of:

  • png - The one everyone uses, is very limited in which PNGs it can open.
  • lodepng - Lots of features, code is ported from C, therefore code is hard read & maintain, also uses slow implementation of deflate/inflate algorithm.
  • imagefmt - Abandoned, just as limited as png, but with a lot less lines of code.

Originally I made the aci_png based on imagefmt, and intended to add more features. That task seemed possible at first, but just became daunting after a while. That's why I decided to take lodepng which has more features (and more low level features) and clean up the code, upgrade to 2018 edition of Rust, depend on the miniz_oxide crate (because it can do it faster than lodepng) and get rid of the libc dependency so it actually becomes pure Rust (lodepng claims to be, but calls C's malloc and free). I also decided to model the API after the gift crate, so I'm using pix instead of rgb.


  • Forbid unsafe.
  • APNG support as iterator.
  • Fast.
  • Compatible with pix / gift-style API.
  • Load all PNG files crushed with pngcrush.
  • Save crushed PNG files.
  • Clean, well-documented, concise code.


  • Implement APNG reading.
  • Implement Chunk reading (with all the different chunk structs).
  • RasterDecoder should wrap ChunkDecoder & RasterEncoder should wrap ChunkEncoder
  • Replace ParseError with Rust-style enum instead of having a C integer.
  • More test cases to test against.

Table of Contents

Getting Started

Add the following to your Cargo.toml.

version = "0.5"


// Saving raster as a PNG file
let raster = pix::Raster::with_pixels(1, 1, &[
    pix::rgb::SRgba8::new(0, 0, 0, 0)][..]
let mut out_data = Vec::new();
let mut encoder = png_pong::FrameEncoder::<_, pix::rgb::SRgba8>::new(
    &mut out_data
let frame = png_pong::Frame{ raster, delay: 0 };
encoder.encode(&frame).expect("Failed to add frame");
std::fs::write("graphic.png", out_data).expect("Failed to save image");

// Loading PNG file into a Raster
let data = std::fs::read("graphic.png").expect("Failed to open PNG");
let data = std::io::Cursor::new(data);
let decoder = png_pong::FrameDecoder::<_, pix::rgb::SRgba8>::new(data);
let png_pong::Frame { raster, delay } = decoder
    .expect("No frames in PNG")
    .expect("PNG parsing error");


API documentation can be found on


There is one optional feature "flate" which is enabled by default, allowing png_pong to read compressed PNG files (which is most of them). This pulls in the miniz_oxide dependency.


You can use the changelog to facilitate upgrading this crate as a dependency.


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Before contributing, check out the contribution guidelines, and, as always, make sure to always follow the code of conduct.