9 releases

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.5 Jun 14, 2020
0.1.0-alpha.3 Mar 14, 2020

#291 in Images

Download history 84/week @ 2023-12-15 324/week @ 2023-12-22 75/week @ 2023-12-29 140/week @ 2024-01-05 62/week @ 2024-01-12 48/week @ 2024-01-19 27/week @ 2024-01-26 6/week @ 2024-02-02 65/week @ 2024-02-09 38/week @ 2024-02-16 28/week @ 2024-02-23 40/week @ 2024-03-01 63/week @ 2024-03-08 84/week @ 2024-03-15 31/week @ 2024-03-22 234/week @ 2024-03-29

418 downloads per month
Used in zune-png

MIT/Apache

290KB
7.5K SLoC

C 4.5K SLoC // 0.1% comments Rust 3K SLoC // 0.0% comments Shell 30 SLoC // 0.1% comments C++ 13 SLoC Bitbake 5 SLoC

spng-rs

crates.io docs.rs build status tests

Rust bindings to libspng.

Version

spng-rs libspng
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:   [2.1378 ms 2.1410 ms 2.1446 ms]
spng_decode             time:   [778.51 us 780.36 us 782.33 us]
spng_decode             time:   [420.45 us 421.26 us 422.12 us] (--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

~4MB
~61K SLoC