Git Product home page Git Product logo

spng-rs's Introduction

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());

spng-rs's People

Contributors

alextmjugador avatar aloucks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

comunidadaylas

spng-rs's Issues

Unsound issue for uninitizalized memory access

Hi,

Thanks for your time & patience for reviewing this issue.

We are doing Rust ffi safety analysis and found unsound issues in this repo. There are multiple dataflows triggers the unsoundness(read of uninitalized memory) problem in safe Rust, the PoC code is shown as:

#[forbid(unsafe_code)]
use std::fs::File;
use std::io::Read;

struct A{
    f: File
}

impl Read for A {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        println!("buf should be inited {}", buf[0]);
        return self.f.read(buf);
    }
}

fn main(){
    if let Ok(png) =File::open("test.png") {
        let a = A{f:png};
        if let Ok((info,_)) = spng::decode(a, spng::Format::Rgba8){
            println!("{:?}", info);
        }
    }
}

Running with command:

RUSTFLAGS='-Zsanitizer=memory -Zsanitizer-memory-track-origins' cargo run -Zbuild-std --target x86_64-unknown-linux-gnu

The dataflow we found are:

  1. The uninit chunk is created in Rust and accessed in C

spng-rs/spng/src/raw.rs

Lines 165 to 167 in 129822c

let mut chunk = MaybeUninit::uninit();
check_err(sys::spng_get_ihdr(self.raw, chunk.as_mut_ptr()))?;
Ok(chunk.assume_init())

  1. The out buffer is created in Rust and accessed in C

spng-rs/spng/src/lib.rs

Lines 395 to 399 in 129822c

out.reserve_exact(reader.output_buffer_size());
unsafe {
out.set_len(reader.output_buffer_size());
}
let out_info = reader.next_frame(&mut out)?;

  1. The buffer is alloced in C and accessed in Rust

https://github.com/randy408/libspng/blob/8919deb455be635f862fbb459af1aa5ad1f32b11/spng/spng.c#L3512

Reading uninitialized memory is regarded as a kind of undefined behavior in Rust/C, and safe Rust is guaranteed for not triggering undefined behaviors.

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.