10 releases

0.2.0-alpha.3 Aug 22, 2024
0.2.0-alpha.2 Aug 28, 2021
0.2.0-alpha.1 Jun 24, 2021
0.1.0 Mar 14, 2021
0.1.0-alpha.3 Mar 14, 2020

#195 in Images

Download history 31/week @ 2024-07-30 7/week @ 2024-08-06 7/week @ 2024-08-13 106/week @ 2024-08-20 30/week @ 2024-08-27 9/week @ 2024-09-03 23/week @ 2024-09-10 147/week @ 2024-09-17 150/week @ 2024-09-24 230/week @ 2024-10-01 146/week @ 2024-10-08 195/week @ 2024-10-15 228/week @ 2024-10-22 99/week @ 2024-10-29 325/week @ 2024-11-05 135/week @ 2024-11-12

799 downloads per month
Used in zune-png

MIT/Apache

380KB
10K SLoC

C 7K SLoC // 0.0% comments Rust 3K SLoC // 0.0% comments Shell 35 SLoC // 0.1% comments C++ 14 SLoC

spng-rs

crates.io docs.rs tests

Rust bindings to libspng.

Version

spng-rs libspng
Unreleased 0.7.4
0.2.0-alpha.2 0.7.0-rc2
0.2.0-alpha.1 0.7.0-rc2
0.1.0 0.6.3

Performance

This test image is decoded ~ 3-5x faster than with the png crate.

png_decode              time:   [1.7354 ms 1.7372 ms 1.7392 ms]
spng_decode             time:   [569.27 µs 570.86 µs 572.45 µs]
spng_decode             time:   [311.84 µs 312.45 µs 313.13 µs] (--features=zlib-ng)

Examples

A one-liner for simple use cases:

let file = File::open("image.png")?;
let (out_info, data) = spng::decode(file, spng::Format::Rgba8)?;

assert_eq!(300, out_info.width);
assert_eq!(300, out_info.height);
assert_eq!(8, out_info.bit_depth);
assert_eq!(4, out_info.color_type.samples());
assert_eq!(out_info.buffer_size, output_buffer_size);

The Decoder interface is modeled after the png crate:

let file = File::open("image.png")?;
let decoder = spng::Decoder::new(file)
    .with_output_format(spng::Format::Rgba8);
let (out_info, mut reader) = decoder.read_info()?;
let out_buffer_size = reader.output_buffer_size();
let mut data = vec![0; out_buffer_size];
reader.next_frame(&mut data)?;

assert_eq!(300, out_info.width);
assert_eq!(300, out_info.height);
assert_eq!(8, out_info.bit_depth);
assert_eq!(4, out_info.color_type.samples());
assert_eq!(out_info.buffer_size, out_buffer_size);

The RawContext interface is a safe and minimal wrapper over the full libspng C API.

let file = File::open("image.png")?;
let out_format = spng::Format::Rgba8;
let mut ctx = spng::raw::RawContext::new()?;
ctx.set_png_stream(file)?;
let ihdr = ctx.get_ihdr()?;
let out_buffer_size = ctx.decoded_image_size(out_format)?;
let mut data = vec![0; out_buffer_size];
ctx.decode_image(&mut data, out_format, spng::DecodeFlags::empty())?;

assert_eq!(300, ihdr.width);
assert_eq!(300, ihdr.height);
assert_eq!(8, ihdr.bit_depth);
assert_eq!(4, spng::ColorType::try_from(ihdr.color_type)?.samples());

Dependencies

~0.9–1.3MB
~26K SLoC