From 242724d7fd2053acc76959846828d8e59f959615 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Fri, 12 Apr 2024 17:43:49 +0200 Subject: [PATCH] switch 8x8 cell diff to vertical --- .gitignore | 3 +++ encoder/src/dec.rs | 20 ++++++-------------- encoder/src/enc.rs | 23 +++++++++++++++-------- encoder/src/main.rs | 10 +++++----- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 93108d3..ba51260 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ *.map *.pdf target/ +*.png +*.mp4 +*.webm diff --git a/encoder/src/dec.rs b/encoder/src/dec.rs index cca9602..244c3ea 100644 --- a/encoder/src/dec.rs +++ b/encoder/src/dec.rs @@ -1,6 +1,6 @@ use crate::*; -pub fn cell_diff_8_horizontal(prev_frame: &Frame, encoded: &[u8], reader: &mut usize) -> Frame { +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; @@ -15,23 +15,15 @@ pub fn cell_diff_8_horizontal(prev_frame: &Frame, encoded: &[u8], reader: &mut u *reader += runs; let cells_x = WIDTH / 8; - let cells_y = HEIGHT / 8; let mut index = 0; - for celly in 0..cells_y { - for cellx in 0..cells_x { + 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 { - let pixels = &new_pixels[(index * 8 * 8)..((index + 1) * 8 * 8)]; - let mut i = 0; - for dx in 0..8 { - for dy in 0..8 { - let x = cellx * 8 + dx; - let y = celly * 8 + dy; - frame[x][y] = pixels[i]; - i += 1; - } - } + frame[x][y] = new_pixels[index]; index += 1; } } diff --git a/encoder/src/enc.rs b/encoder/src/enc.rs index 2c29754..c45caea 100644 --- a/encoder/src/enc.rs +++ b/encoder/src/enc.rs @@ -1,6 +1,6 @@ use crate::*; -pub fn cell_diff_8_horizontal(prev_frame: &Frame, frame: &Frame) -> EncodedFrame { +pub fn cell_diff_8_vertical(prev_frame: &Frame, frame: &Frame) -> EncodedFrame { let cells_x = WIDTH / 8; let cells_y = HEIGHT / 8; let loss = 0; @@ -8,17 +8,15 @@ pub fn cell_diff_8_horizontal(prev_frame: &Frame, frame: &Frame) -> EncodedFrame let mut bitmap: u32 = 0; let mut changed_pixels = Vec::new(); - for celly in 0..cells_y { - for cellx in 0..cells_x { + for cellx in 0..cells_x { + for celly in 0..cells_y { let mut changed = 0; - let mut cell_contents = Vec::new(); - // todo try encoding the cells in frame space instead of one cell at a time for dx in 0..8 { for dy in 0..8 { let x = cellx * 8 + dx; let y = celly * 8 + dy; let pixel = frame[x][y]; - cell_contents.push(pixel); + if pixel != prev_frame[x][y] { changed += 1; } @@ -26,14 +24,23 @@ pub fn cell_diff_8_horizontal(prev_frame: &Frame, frame: &Frame) -> EncodedFrame } if changed > loss { bitmap |= 1 << (cellx + cells_x * celly); - changed_pixels.extend_from_slice(&cell_contents); + } + } + } + for x in 0..WIDTH { + for y in 0..HEIGHT { + let cellx = x / 8; + let celly = y / 8; + let bit = 1 << (cellx + cells_x * celly); + if (bitmap & bit) != 0 { + changed_pixels.push(frame[x][y]); } } } let mut data = vec![(bitmap >> 16) as u8, (bitmap >> 8) as u8, bitmap as u8]; data.extend_from_slice(&rle_255_encode(&changed_pixels)); EncodedFrame { - encoding: Encoding::CellDiff8H, + encoding: Encoding::CellDiff8V, data, } } diff --git a/encoder/src/main.rs b/encoder/src/main.rs index 60e9e52..4eb64bc 100644 --- a/encoder/src/main.rs +++ b/encoder/src/main.rs @@ -57,7 +57,7 @@ fn encode(frames: &[Frame]) -> Vec { enc::rle_diff_horizontal, enc::rle_diff_vertical, enc::bg_strips_horizontal, - enc::cell_diff_8_horizontal, + enc::cell_diff_8_vertical, ]; let mut last_frame = FRAME_0; @@ -109,8 +109,8 @@ enum Encoding { // BGStripsV, // QuadTree, // DrawCommands, - CellDiff8H, - // CellDiff8V, + // CellDiff8H, + CellDiff8V, // CellDiff4HH, // CellDiff4HV, // CellDiff4VH, @@ -129,8 +129,8 @@ fn get_matching_decoder(encoding: Encoding) -> FrameDecoder { // Encoding::BGStripsV => todo!(), // Encoding::QuadTree => todo!(), // Encoding::DrawCommands => todo!(), - Encoding::CellDiff8H => dec::cell_diff_8_horizontal, - // Encoding::CellDiff8V => todo!(), + // Encoding::CellDiff8H => todo!(), + Encoding::CellDiff8V => dec::cell_diff_8_vertical, // Encoding::CellDiff4HH => todo!(), // Encoding::CellDiff4HV => todo!(), // Encoding::CellDiff4VH => todo!(),