bad-apple/encoder/src/dec.rs

122 lines
2.9 KiB
Rust

use crate::*;
pub fn cell_diff_8_vertical(prev_frame: &Frame, encoded: &[u8], reader: &mut usize) -> Frame {
let bitmap = {
*reader += 3;
let mut bitmap = (encoded[0] as u32) << 16;
bitmap |= (encoded[1] as u32) << 8;
bitmap | encoded[2] as u32
};
let encoded = &encoded[3..];
let mut frame = *prev_frame;
let changed_pixel_count = bitmap.count_ones() as usize * 8 * 8;
let (runs, new_pixels) = rle_255_decode_until(encoded, changed_pixel_count);
*reader += runs;
let cells_x = WIDTH / 8;
let mut index = 0;
for x in 0..WIDTH {
for y in 0..HEIGHT {
let cellx = x / 8;
let celly = y / 8;
let is_changed = (bitmap >> (cellx + cells_x * celly)) & 1;
if is_changed == 1 {
frame[x][y] = new_pixels[index];
index += 1;
}
}
}
frame
}
pub fn bg_strips_horizontal(_prev_frame: &Frame, encoded: &[u8], reader: &mut usize) -> Frame {
*reader += 1;
let bg = encoded[0] >> 7;
let fg = 1 - bg;
let mut frame = [[bg; HEIGHT]; WIDTH];
let count = (encoded[0] & 0x7f) as usize;
*reader += count * 2;
for i in 0..count {
let upper = (encoded[i * 2 + 1] as u16) << 8;
let lower = encoded[i * 2 + 2] as u16;
let y_x_w: u16 = upper | lower;
let y = (y_x_w >> 11) as usize;
let x_start = ((y_x_w >> 5) & 0x3f) as usize;
let w = (y_x_w & 0x1f) as usize;
for x in x_start..(x_start + w) {
frame[x][y] = fg;
}
}
frame
}
pub fn rle_diff_horizontal(prev_frame: &Frame, encoded: &[u8], reader: &mut usize) -> Frame {
let (runs, decoded) = rle_255_decode_until(encoded, FRAME_SIZE);
*reader += runs;
let mut frame = *prev_frame;
let mut i = 0;
for y in 0..HEIGHT {
for x in 0..WIDTH {
frame[x][y] ^= decoded[i];
i += 1;
}
}
frame
}
pub fn rle_diff_vertical(prev_frame: &Frame, encoded: &[u8], reader: &mut usize) -> Frame {
let (runs, decoded) = rle_255_decode_until(encoded, FRAME_SIZE);
*reader += runs;
let mut frame = *prev_frame;
let mut dbg_frame = FRAME_0;
let mut i = 0;
for x in 0..WIDTH {
for y in 0..HEIGHT {
frame[x][y] ^= decoded[i];
frame[x][y] &= 1;
dbg_frame[x][y] ^= decoded[i];
dbg_frame[x][y] &= 1;
i += 1;
}
}
frame
}
pub fn rle_horizontal(_prev_frame: &Frame, encoded: &[u8], reader: &mut usize) -> Frame {
let (runs, pixels) = rle_255_decode_until(encoded, FRAME_SIZE);
*reader += runs;
let mut frame = FRAME_0;
let mut i = 0;
for y in 0..HEIGHT {
for x in 0..WIDTH {
frame[x][y] = pixels[i];
i += 1;
}
}
frame
}
pub fn rle_vertical(_prev_frame: &Frame, encoded: &[u8], reader: &mut usize) -> Frame {
let (runs, pixels) = rle_255_decode_until(encoded, FRAME_SIZE);
*reader += runs;
let mut frame = FRAME_0;
let mut i = 0;
for x in 0..WIDTH {
for y in 0..HEIGHT {
frame[x][y] = pixels[i];
i += 1;
}
}
frame
}
pub fn fill_white(_prev_frame: &Frame, _encoded: &[u8], _reader: &mut usize) -> Frame {
FRAME_1
}
pub fn fill_black(_prev_frame: &Frame, _encoded: &[u8], _reader: &mut usize) -> Frame {
FRAME_0
}