add horizontal strip encoding type

This commit is contained in:
Crispy 2024-04-11 23:28:49 +02:00
parent 3af9be328c
commit 1f61cebd50
4 changed files with 136 additions and 30 deletions

View file

@ -1,5 +1,58 @@
use crate::*;
pub fn bg_strips_horizontal(_prev_frame: &Frame, frame: &Frame) -> EncodedFrame {
let bg = most_common_pixel(frame);
fn pack_strip(x: usize, y: usize, width: usize) -> [u8; 2] {
// Y is 0..31 so will only need 5 bits
// x is 0..42 so needs 6 bits
// 5 bits remain for width
// y: 1111100000000000
let mut y_x_w: u16 = (y as u16) << 11;
// x: 0000011111100000
y_x_w |= (x as u16) << 5;
// w: 0000000000011111
y_x_w |= width as u16;
[(y_x_w >> 8) as u8, (y_x_w as u8)]
}
let mut strips = Vec::new();
'outer: for y in 0..HEIGHT {
let mut strip_start = 0;
let mut in_strip = false;
for x in 0..WIDTH {
let pixel = frame[x][y];
if !in_strip && pixel != bg {
in_strip = true;
strip_start = x;
}
if in_strip {
if pixel == bg {
strips.push((strip_start, y, x - strip_start));
in_strip = false;
} else if x - strip_start == 31 {
strips.push((strip_start, y, x - strip_start));
strip_start = x;
}
if strips.len() == 127 {
break 'outer;
}
}
}
if in_strip {
strips.push((strip_start, y, WIDTH - strip_start));
}
}
let mut frame_bytes = Vec::with_capacity(1 + strips.len() * 2);
frame_bytes.push(bg << 7 | (strips.len() as u8));
for (x, y, width) in strips {
frame_bytes.extend_from_slice(&pack_strip(x, y, width));
}
EncodedFrame {
encoding: Encoding::BGStripsH,
data: frame_bytes,
}
}
pub fn rle_diff_horizontal(prev_frame: &Frame, frame: &Frame) -> EncodedFrame {
let mut pixels = Vec::new();
for y in 0..HEIGHT {