Crates.io | minipng |
lib.rs | minipng |
version | 0.1.1 |
source | src |
created_at | 2023-09-06 02:45:09.188258 |
updated_at | 2023-09-21 03:44:08.038342 |
description | Tiny PNG decoder with no dependencies |
homepage | |
repository | https://github.com/pommicket/minipng |
max_upload_size | |
id | 964974 |
size | 4,529,647 |
Tiny Rust PNG decoder with no dependencies (not even std
or alloc
)
and tiny code size (e.g. >8 times smaller .wasm.gz
size compared to the png
crate — see check-size.sh
).
usize::MAX
).core
Basic usage:
let mut buffer = vec![0; 1 << 20]; // hope this is big enough!
let png = &include_bytes!("../examples/image.png")[..];
let image = minipng::decode_png(png, &mut buffer).expect("bad PNG");
println!("{}×{} image", image.width(), image.height());
let pixels = image.pixels();
println!("top-left pixel is #{:02x}{:02x}{:02x}", pixels[0], pixels[1], pixels[2]);
// (^ this only works for RGB(A) 8bpc images)
More complex example that allocates the right number of bytes and handles all color formats:
let png = &include_bytes!("../examples/image.png")[..];
let header = minipng::decode_png_header(png).expect("bad PNG");
let mut buffer = vec![0; header.required_bytes_rgba8bpc()];
let mut image = minipng::decode_png(png, &mut buffer).expect("bad PNG");
image.convert_to_rgba8bpc();
println!("{}×{} image", image.width(), image.height());
let pixels = image.pixels();
println!("top-left pixel is #{:02x}{:02x}{:02x}{:02x}", pixels[0], pixels[1], pixels[2], pixels[3]);
adler
(default: disabled) — check Adler-32 checksums. slightly increases code size and
slightly decreases performance but verifies integrity of PNG files.A pre-commit
git hook is provided to run cargo fmt
and cargo clippy
. You can install it with:
ln -s ../../pre-commit .git/hooks/
All PNG files in the test
directory are tested by cargo t
(NOTE: cargo test
doesn‘t log as much progress because there’s no way of dynamically generating
tests and no way of enabling --nocapture
by default grumble grumble).
Benchmarks (see cargo bench
) show that minipng
is about 50% slower than the png
crate
for large images, but faster than png
for small images.
Zero-Clause BSD
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.