png_codec 0.1.1

A minimal pure Rust PNG encoder
Documentation
//! Compression algorithms

use miniz_oxide::deflate::compress_to_vec;

// FIXME: Streaming API
pub(crate) fn compress(outv: &mut Vec<u8>, inp: &[u8], level: u8) {
    /*initially, *out must be NULL and outsize 0, if you just give some random *out
    that's pointing to a non allocated buffer, this'll crash*/
    /* zlib data: 1 byte CMF (cm+cinfo), 1 byte FLG, deflate data, 4 byte
     * adler32_val checksum of the Decompressed data */
    let cmf = 120;
    /* 0b01111000: CM 8, cinfo 7. With cinfo 7, any window size up to 32768
     * can be used. */
    let flevel = 0;
    let fdict = 0;
    let mut cmfflg = 256 * cmf + fdict * 32 + flevel * 64;
    let fcheck = 31 - cmfflg % 31;
    cmfflg += fcheck;
    /* Vec<u8>-controlled version of the output buffer, for dynamic array */
    outv.push((cmfflg >> 8) as u8);
    outv.push((cmfflg & 255) as u8);
    let deflated = compress_to_vec(inp, level);
    let adler32_val = adler32(inp);
    outv.extend_from_slice(&deflated);
    outv.extend(adler32_val.to_be_bytes().iter());
}

/// Return the Adler32 of the bytes data[0..len-1]
fn adler32(data: &[u8]) -> u32 {
    let mut adler = simd_adler32::Adler32::new();
    adler.write(data);
    adler.finish()
}