From 5dcf4957443d773c53ec2162a73d602e38cf4294 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Fri, 12 Apr 2024 19:12:10 +0200 Subject: [PATCH] export data to c header file --- encoder/src/main.rs | 64 +++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/encoder/src/main.rs b/encoder/src/main.rs index 3577eb8..c07803c 100644 --- a/encoder/src/main.rs +++ b/encoder/src/main.rs @@ -1,3 +1,5 @@ +use std::{fs::File, io::Write}; + use enum_map::{Enum, EnumMap}; use num_enum::TryFromPrimitive; @@ -11,6 +13,20 @@ const INSPECT_DEC: bool = false; const MAX_ERROR: usize = 4; // max wrong pixels const MAX_LOSS: usize = 16; // highest "loss" value tried for all lossy encodings +const LOSSLESS_ENCODINGS: &[FrameEncoder] = &[ + enc::rle_horizontal, + enc::rle_vertical, + // enc::rle_diff_horizontal, + // enc::rle_diff_vertical, + enc::bg_strips_horizontal, +]; +const LOSSY_ENCODINGS: &[FrameEncoderLossy] = &[ + enc::fill_white, + enc::fill_black, + enc::cell_diff_8_vertical, + // enc::cell_diff_4_vertical, +]; + fn main() { let frames = get_all_frames("../video/frames/"); let encoded = encode(&frames); @@ -45,28 +61,36 @@ fn main() { encoded.len(), encoded.len() / frames.len() ); + + let mut export_string = String::from("// Generated by the `encoder` rust app\n"); + export_string += "typedef enum {\n"; + for (encoding, count) in stats { + if count > 0 { + export_string += &format!("\tEncoding_{encoding:?} = {},\n", encoding as u8); + } + } + export_string += "} Encoding_t;\n\n"; + export_string += "const unsigned char video[] = {"; + let mut i = 99; + for byte in encoded { + if i > 15 { + export_string += "\n\t"; + i = 0; + } + i += 1; + export_string += &format!("{byte},"); + } + export_string += "\n};\n"; + let mut file = File::create("../data.h").unwrap(); + file.write_all(export_string.as_bytes()).unwrap(); } fn encode(frames: &[Frame]) -> Vec { let mut out = Vec::new(); - let lossless_encodings: Vec = vec![ - enc::rle_horizontal, - enc::rle_vertical, - enc::rle_diff_horizontal, - enc::rle_diff_vertical, - enc::bg_strips_horizontal, - ]; - let lossy_encodings: Vec = vec![ - enc::fill_white, - enc::fill_black, - enc::cell_diff_8_vertical, - enc::cell_diff_4_vertical, - ]; - let mut last_frame = FRAME_0; for (_i, frame) in frames.iter().enumerate() { let mut options = Vec::new(); - for encode in &lossless_encodings { + for encode in LOSSLESS_ENCODINGS.iter() { let encoded = encode(&last_frame, frame); let decode = get_matching_decoder(encoded.encoding); let decoded = decode(&last_frame, &encoded.data, &mut 0); @@ -80,7 +104,7 @@ fn encode(frames: &[Frame]) -> Vec { panic!("error in lossless compression"); } } - for encode in &lossy_encodings { + for encode in LOSSY_ENCODINGS.iter() { for loss in 0..MAX_LOSS { let encoded = encode(&last_frame, frame, loss); let decode = get_matching_decoder(encoded.encoding); @@ -124,10 +148,10 @@ fn encode(frames: &[Frame]) -> Vec { #[derive(Debug, TryFromPrimitive, Enum, Copy, Clone)] #[repr(u8)] enum Encoding { - FillBlack = 0, - FillWhite = 1, - RLEHorizontal = 2, - RLEVertical = 3, + FillBlack, + FillWhite, + RLEHorizontal, + RLEVertical, RLEDiffHorizontal, RLEDiffVertical, BGStripsH,