export data to c header file

This commit is contained in:
Crispy 2024-04-12 19:12:10 +02:00
parent 8bfeb6d2f3
commit 5dcf495744

View file

@ -1,3 +1,5 @@
use std::{fs::File, io::Write};
use enum_map::{Enum, EnumMap}; use enum_map::{Enum, EnumMap};
use num_enum::TryFromPrimitive; use num_enum::TryFromPrimitive;
@ -11,6 +13,20 @@ const INSPECT_DEC: bool = false;
const MAX_ERROR: usize = 4; // max wrong pixels const MAX_ERROR: usize = 4; // max wrong pixels
const MAX_LOSS: usize = 16; // highest "loss" value tried for all lossy encodings 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() { fn main() {
let frames = get_all_frames("../video/frames/"); let frames = get_all_frames("../video/frames/");
let encoded = encode(&frames); let encoded = encode(&frames);
@ -45,28 +61,36 @@ fn main() {
encoded.len(), encoded.len(),
encoded.len() / frames.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<u8> { fn encode(frames: &[Frame]) -> Vec<u8> {
let mut out = Vec::new(); let mut out = Vec::new();
let lossless_encodings: Vec<FrameEncoder> = vec![
enc::rle_horizontal,
enc::rle_vertical,
enc::rle_diff_horizontal,
enc::rle_diff_vertical,
enc::bg_strips_horizontal,
];
let lossy_encodings: Vec<FrameEncoderLossy> = vec![
enc::fill_white,
enc::fill_black,
enc::cell_diff_8_vertical,
enc::cell_diff_4_vertical,
];
let mut last_frame = FRAME_0; let mut last_frame = FRAME_0;
for (_i, frame) in frames.iter().enumerate() { for (_i, frame) in frames.iter().enumerate() {
let mut options = Vec::new(); let mut options = Vec::new();
for encode in &lossless_encodings { for encode in LOSSLESS_ENCODINGS.iter() {
let encoded = encode(&last_frame, frame); let encoded = encode(&last_frame, frame);
let decode = get_matching_decoder(encoded.encoding); let decode = get_matching_decoder(encoded.encoding);
let decoded = decode(&last_frame, &encoded.data, &mut 0); let decoded = decode(&last_frame, &encoded.data, &mut 0);
@ -80,7 +104,7 @@ fn encode(frames: &[Frame]) -> Vec<u8> {
panic!("error in lossless compression"); panic!("error in lossless compression");
} }
} }
for encode in &lossy_encodings { for encode in LOSSY_ENCODINGS.iter() {
for loss in 0..MAX_LOSS { for loss in 0..MAX_LOSS {
let encoded = encode(&last_frame, frame, loss); let encoded = encode(&last_frame, frame, loss);
let decode = get_matching_decoder(encoded.encoding); let decode = get_matching_decoder(encoded.encoding);
@ -124,10 +148,10 @@ fn encode(frames: &[Frame]) -> Vec<u8> {
#[derive(Debug, TryFromPrimitive, Enum, Copy, Clone)] #[derive(Debug, TryFromPrimitive, Enum, Copy, Clone)]
#[repr(u8)] #[repr(u8)]
enum Encoding { enum Encoding {
FillBlack = 0, FillBlack,
FillWhite = 1, FillWhite,
RLEHorizontal = 2, RLEHorizontal,
RLEVertical = 3, RLEVertical,
RLEDiffHorizontal, RLEDiffHorizontal,
RLEDiffVertical, RLEDiffVertical,
BGStripsH, BGStripsH,