init
This commit is contained in:
commit
eca5344c66
10 changed files with 445 additions and 0 deletions
115
encoder/src/util.rs
Normal file
115
encoder/src/util.rs
Normal file
|
@ -0,0 +1,115 @@
|
|||
use std::{
|
||||
fs::{self, File},
|
||||
io::BufReader,
|
||||
};
|
||||
|
||||
use image::{self, DynamicImage, GenericImageView, ImageFormat, Rgba};
|
||||
|
||||
pub const WIDTH: usize = 40;
|
||||
pub const HEIGHT: usize = 32;
|
||||
pub const 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];
|
||||
|
||||
fn convert_pixel(rgba: Rgba<u8>) -> u8 {
|
||||
(rgba.0[0] > 128) as u8
|
||||
}
|
||||
|
||||
pub fn convert_image(image: &DynamicImage) -> Frame {
|
||||
let mut frame = FRAME_0;
|
||||
for x in 0..WIDTH {
|
||||
for y in 0..HEIGHT {
|
||||
frame[x][y] = convert_pixel(image.get_pixel(x as u32, y as u32));
|
||||
}
|
||||
}
|
||||
frame
|
||||
}
|
||||
|
||||
pub fn get_all_frames(path: &str) -> Vec<Frame> {
|
||||
let frame_count = fs::read_dir(path).unwrap().count();
|
||||
let mut frames = Vec::new();
|
||||
for i in 0..frame_count {
|
||||
let path = format!("{}frame_{:04}.png", path, i + 1);
|
||||
let file = BufReader::new(File::open(path).unwrap());
|
||||
let image = image::load(file, ImageFormat::Png).unwrap();
|
||||
frames.push(convert_image(&image));
|
||||
}
|
||||
frames
|
||||
}
|
||||
|
||||
pub fn frame_error(real: &Frame, decoded: &Frame) -> usize {
|
||||
let mut error = 0;
|
||||
for x in 0..WIDTH {
|
||||
for y in 0..HEIGHT {
|
||||
if real[x][y] != decoded[x][y] {
|
||||
error += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
error
|
||||
}
|
||||
|
||||
fn render_pixel_pair(img: &Frame, x: usize, y: usize) {
|
||||
let char = match (img[x][y * 2], img[x][y * 2 + 1]) {
|
||||
(0, 0) => " ",
|
||||
(0, 1) => "▄",
|
||||
(1, 0) => "▀",
|
||||
(1, 1) => "█",
|
||||
_ => panic!("image contained nonbinary bytes"),
|
||||
};
|
||||
print!("{}", char);
|
||||
}
|
||||
|
||||
pub fn render_image(img: &Frame) {
|
||||
for y in 0..(HEIGHT / 2) {
|
||||
for x in 0..WIDTH {
|
||||
render_pixel_pair(img, x, y);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
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!(" ");
|
||||
render_pixel_pair(right, x, y);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rle_255_encode(raw: &[u8]) -> Vec<u8> {
|
||||
let mut encoded = Vec::new();
|
||||
let mut last_val = 0;
|
||||
let mut run = 0;
|
||||
for &val in raw {
|
||||
if val != last_val || run == 255 {
|
||||
encoded.push(run);
|
||||
if run == 255 {
|
||||
encoded.push(0);
|
||||
}
|
||||
run = 1;
|
||||
} else {
|
||||
run += 1;
|
||||
}
|
||||
last_val = val;
|
||||
}
|
||||
encoded.push(run);
|
||||
encoded
|
||||
}
|
||||
|
||||
pub fn rle_255_decode(encoded: &[u8]) -> Vec<u8> {
|
||||
let mut raw = Vec::new();
|
||||
let mut val = 0;
|
||||
for &run in encoded {
|
||||
for _ in 0..run {
|
||||
raw.push(val);
|
||||
}
|
||||
val = 1 - val;
|
||||
}
|
||||
raw
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue