#png #decoding

png

PNG decoding and encoding library in pure Rust

20 releases (11 breaking)

0.12.0 Apr 24, 2018
0.11.0 Oct 13, 2017
0.10.0 Sep 23, 2017
0.9.0 Jun 25, 2017
0.3.1 Jun 15, 2015

#7 in Images

Download history 3878/week @ 2018-05-27 5446/week @ 2018-06-03 5833/week @ 2018-06-10 4207/week @ 2018-06-17 3645/week @ 2018-06-24 4702/week @ 2018-07-01 4818/week @ 2018-07-08 5095/week @ 2018-07-15 4283/week @ 2018-07-22 3571/week @ 2018-07-29 5132/week @ 2018-08-05 4658/week @ 2018-08-12 5050/week @ 2018-08-19

15,828 downloads per month
Used in 52 crates (22 directly)

MIT/Apache

76KB
2K SLoC

PNG Decoder/Encoder Build Status

PNG decoder/encoder in pure Rust. Currently pngcheck is the most usable part. Build with cargo build --release --example pngcheck.

License

Licensed under either of

at your option.

Contribution

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.


lib.rs:

PNG encoder and decoder

This crate contains a PNG encoder and decoder. It supports reading of single lines or whole frames.

The decoder

The most important types for decoding purposes are Decoder and Reader. They both wrap a std::io::Read. Decoder serves as a builder for Reader. Calling Decoder::read_info reads from the Read until the image data is reached.

Using the decoder

 use std::fs::File;

 // The decoder is a build for reader and can be used to set various decoding options
 // via `Transformations`. The default output transformation is `Transformations::EXPAND
 // | Transformations::STRIP_ALPHA`.
 let decoder = png::Decoder::new(File::open("tests/pngsuite/basi0g01.png").unwrap());
 let (info, mut reader) = decoder.read_info().unwrap();
 // Allocate the output buffer.
 let mut buf = vec![0; info.buffer_size()];
 // Read the next frame. Currently this function should only called once.
 // The default options
 reader.next_frame(&mut buf).unwrap();

Encoder

Using the encoder

    // For reading and opening files
    use std::path::Path;
    use std::fs::File;
    use std::io::BufWriter;
    // To use encoder.set()
    use png::HasParameters;

    let path = Path::new(r"/path/to/image.png");
    let file = File::create(path).unwrap();
    let ref mut w = BufWriter::new(file);

    let mut encoder = png::Encoder::new(w, 2, 1); // Width is 2 pixels and height is 1.
    encoder.set(png::ColorType::RGBA).set(png::BitDepth::Eight);
     let mut writer = encoder.write_header().unwrap();

    let data = [255, 0, 0, 255, 0, 0, 0, 255]; // An array containing a RGBA sequence. First pixel is red and second pixel is black.
    writer.write_image_data(&data).unwrap(); // Save

Dependencies

~374KB