Crate png[−][src]
Expand description
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. An APNG might contain multiple frames.
reader.next_frame(&mut buf).unwrap();
// Inspect more details of the last read frame.
let in_animation = reader.info().frame_control.is_some();
Encoder
Using the encoder
// For reading and opening files
use std::path::Path;
use std::fs::File;
use std::io::BufWriter;
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_color(png::ColorType::RGBA);
encoder.set_depth(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
Modules
Chunk types and functions
Structs
Animation control information
PNG Decoder
PNG Encoder
Frame control information
PNG info struct
Limits on the resources the Decoder
is allowed too use
Output info
Pixel dimensions information
PNG reader (mostly high-level interface)
Streaming png writer
PNG StreamingDecoder (low-level interface)
Output transformations
PNG writer
Enums
Bit depth of the png file
How pixels are written into the buffer.
Describes the layout of samples in a pixel
The type and strength of applied compression.
Result of the decoding process
How to reset buffer of an animated png (APNG) at the end of a frame.
The byte level filter applied to scanlines to prepare them for compression.
Physical unit of the pixel dimensions