A pure Rust PNG/APNG encoder & decoder
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.
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.
ParseError
with Rust-style enum instead of having a C integer.Add the following to your Cargo.toml
.
toml
[dependencies.png_pong]
version = "0.2"
```rust // Saving raster as a PNG file let raster = pix::RasterBuilder::new().withpixels(1, 1, &[ pix::SRgba8::new(0, 0, 0, 0)][..] ); let mut outdata = Vec::new(); let mut encoder = pngpong::FrameEncoder::<_, pix::SRgba8>::new( &mut outdata ); let frame = pngpong::Frame{ raster, delay: 0 }; encoder.encode(&frame).expect("Failed to add frame"); std::fs::write("graphic.png", outdata).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 = pngpong::FrameDecoder::<_, pix::SRgba8>::new(data); let pngpong::Frame { raster, delay } = decoder .last() .expect("No frames in PNG") .expect("PNG parsing error"); ```
API documentation can be found on docs.rs.
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 - Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0) - Zlib License, (LICENSE-ZLIB or https://opensource.org/licenses/Zlib)
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.