#bindings #data #github #vector #fpzip #com-llnl-fpzip

sys fpzip-sys

Raw Rust bindings to FPZIP (https://github.com/LLNL/fpzip)

10 releases

0.2.0 Oct 24, 2024
0.1.9 Oct 18, 2024
0.1.8 Dec 17, 2023
0.1.7 May 10, 2023
0.1.3 Jun 19, 2020

#319 in Compression

MIT license

47KB
1.5K SLoC

C++ 1K SLoC // 0.1% comments C 368 SLoC // 0.1% comments Rust 68 SLoC // 0.1% comments

fpzip-sys

Raw Rust bindings to FPZIP (https://github.com/LLNL/fpzip).

An example compression function for a 1D f32 vector:

use fpzip_sys::*;

// ...

fn fpzip_compress(src: &Vec<f32>, high_quality: bool) -> Option<Vec<u8>> {
    let prec = if high_quality {
        24
    } else {
        16
    };
    
    /* allocate buffer for compressed data */
    let bufsize = 1024 + src.len() * std::mem::size_of::<f32>();
    let mut buffer: Vec<u8> = vec![0; bufsize];

    /* compress to memory */
    let fpz = unsafe {
        fpzip_write_to_buffer(buffer.as_mut_ptr() as *mut std::ffi::c_void, bufsize as u64)
    };

    unsafe {
        (*fpz).type_ = FPZIP_TYPE_FLOAT as i32;
        (*fpz).prec = prec;
        (*fpz).nx = src.len() as i32;
        (*fpz).ny = 1;
        (*fpz).nz = 1;
        (*fpz).nf = 1;
    }

    let stat = unsafe { fpzip_write_header(fpz) };

    if stat == 0 {
        unsafe { fpzip_write_close(fpz) };
        return None;
    };

    let outbytes = unsafe { fpzip_write(fpz, src.as_ptr() as *const std::ffi::c_void) };

    unsafe { fpzip_write_close(fpz) };

    if outbytes == 0 {
        return None;
    };

    println!("[fpzip::compress] {} reduced to {} bytes.", src.len() * std::mem::size_of::<f32>(), outbytes);

    return Some(buffer[0..outbytes as usize].to_vec());
}

No runtime deps

~0–2.2MB
~45K SLoC