lossy compression
This commit is contained in:
parent
242724d7fd
commit
8bfeb6d2f3
4 changed files with 194 additions and 37 deletions
|
@ -107,13 +107,17 @@ pub fn render_images(left: &Frame, right: &Frame) {
|
|||
}
|
||||
|
||||
pub fn rle_255_encode(raw: &[u8]) -> Vec<u8> {
|
||||
rle_encode(raw, 255)
|
||||
}
|
||||
|
||||
pub fn rle_encode(raw: &[u8], max: u8) -> Vec<u8> {
|
||||
let mut encoded = Vec::new();
|
||||
let mut last_val = 0;
|
||||
let mut run = 0;
|
||||
for &val in raw {
|
||||
if val != last_val || run == 255 {
|
||||
if val != last_val || run == max {
|
||||
encoded.push(run);
|
||||
if run == 255 && val == last_val {
|
||||
if run == max && val == last_val {
|
||||
encoded.push(0);
|
||||
}
|
||||
run = 1;
|
||||
|
@ -126,7 +130,31 @@ pub fn rle_255_encode(raw: &[u8]) -> Vec<u8> {
|
|||
encoded
|
||||
}
|
||||
|
||||
pub fn rle_255_decode_until(encoded: &[u8], max_size: usize) -> (usize, Vec<u8>) {
|
||||
pub fn pack_nybbles(mut nybbles: Vec<u8>) -> Vec<u8> {
|
||||
if nybbles.len() % 2 == 1 {
|
||||
nybbles.push(0);
|
||||
}
|
||||
assert!(nybbles.iter().all(|&n| n < 16));
|
||||
let mut packed = Vec::with_capacity(nybbles.len() / 2);
|
||||
for i in 0..(nybbles.len() / 2) {
|
||||
let upper = nybbles[i * 2] << 4;
|
||||
let lower = nybbles[i * 2 + 1] & 15;
|
||||
let byte = upper | lower;
|
||||
packed.push(byte);
|
||||
}
|
||||
packed
|
||||
}
|
||||
|
||||
pub fn unpack_nybbles(packed: &[u8]) -> Vec<u8> {
|
||||
let mut nybbles = Vec::with_capacity(packed.len() * 2);
|
||||
for &p in packed {
|
||||
nybbles.push(p >> 4);
|
||||
nybbles.push(p & 15);
|
||||
}
|
||||
nybbles
|
||||
}
|
||||
|
||||
pub fn rle_255_decode(encoded: &[u8], max_size: usize) -> (usize, Vec<u8>) {
|
||||
let mut raw = Vec::new();
|
||||
let mut val = 0;
|
||||
let mut consumed_bytes = 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue