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,6 +1,6 @@
use std::{
fs::{self, File},
io::BufReader,
io::{stdin, BufReader},
};
use image::{self, DynamicImage, GenericImageView, ImageFormat, Rgba};
@ -14,7 +14,7 @@ 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();
stdin().read_line(&mut String::new()).unwrap();
}
fn convert_pixel(rgba: Rgba<u8>) -> u8 {
@ -55,6 +55,23 @@ pub fn frame_error(real: &Frame, decoded: &Frame) -> usize {
error
}
pub fn most_common_pixel(frame: &Frame) -> u8 {
let mut white_pixels = 0;
let mut black_pixels = 0;
for col in frame {
for &p in col {
let p = p as u32;
white_pixels += p;
black_pixels += 1 - p;
}
}
if white_pixels > black_pixels {
1
} else {
0
}
}
fn render_pixel_pair(img: &Frame, x: usize, y: usize) {
let char = match (img[x][y * 2], img[x][y * 2 + 1]) {
(0, 0) => " ",
@ -77,14 +94,15 @@ pub fn render_image(img: &Frame) {
pub fn render_images(left: &Frame, right: &Frame) {
for y in 0..(HEIGHT / 2) {
print!("|");
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!();
println!("|");
}
}
@ -95,7 +113,7 @@ pub fn rle_255_encode(raw: &[u8]) -> Vec<u8> {
for &val in raw {
if val != last_val || run == 255 {
encoded.push(run);
if run == 255 {
if run == 255 && val == last_val {
encoded.push(0);
}
run = 1;