add diff rle and run decoding on final byte array

This commit is contained in:
Crispy 2024-04-11 20:52:12 +02:00
parent eca5344c66
commit 3af9be328c
6 changed files with 308 additions and 48 deletions

View file

@ -7,12 +7,16 @@ use image::{self, DynamicImage, GenericImageView, ImageFormat, Rgba};
pub const WIDTH: usize = 40;
pub const HEIGHT: usize = 32;
pub const SIZE: usize = WIDTH * HEIGHT;
pub const FRAME_SIZE: usize = WIDTH * HEIGHT;
pub type Frame = [[u8; HEIGHT]; WIDTH];
pub const FRAME_0: Frame = [[0; HEIGHT]; WIDTH];
pub const FRAME_1: Frame = [[1; HEIGHT]; WIDTH];
pub fn wait_for_input() {
std::io::stdin().read_line(&mut String::new()).unwrap();
}
fn convert_pixel(rgba: Rgba<u8>) -> u8 {
(rgba.0[0] > 128) as u8
}
@ -75,7 +79,9 @@ pub fn render_images(left: &Frame, right: &Frame) {
for y in 0..(HEIGHT / 2) {
for x in 0..WIDTH {
render_pixel_pair(left, x, y);
print!(" ");
}
print!(" ");
for x in 0..WIDTH {
render_pixel_pair(right, x, y);
}
println!();
@ -102,14 +108,19 @@ pub fn rle_255_encode(raw: &[u8]) -> Vec<u8> {
encoded
}
pub fn rle_255_decode(encoded: &[u8]) -> Vec<u8> {
pub fn rle_255_decode_until(encoded: &[u8], max_size: usize) -> (usize, Vec<u8>) {
let mut raw = Vec::new();
let mut val = 0;
let mut consumed_bytes = 0;
for &run in encoded {
consumed_bytes += 1;
for _ in 0..run {
raw.push(val);
}
if raw.len() >= max_size {
break;
}
val = 1 - val;
}
raw
(consumed_bytes, raw)
}