diff --git a/bitmap_to_asm/src/main.rs b/bitmap_to_asm/src/main.rs index 94207ac..7a0cc8a 100644 --- a/bitmap_to_asm/src/main.rs +++ b/bitmap_to_asm/src/main.rs @@ -1,3 +1,4 @@ +use image::GenericImageView; use regex::Regex; use std::{borrow::Cow, env::args, process::exit}; @@ -14,9 +15,10 @@ fn main() { } } }; - let (data, name, width, height) = path_to_img(path.as_str()); + let (data, name, width, height, color) = path_to_img(path.as_str()); println!("{name}:"); if !nosize { + println!(" D 0x{:08x}", color); println!(" D {width}"); println!(" D {height}"); } @@ -29,9 +31,9 @@ fn remove_non_alphanumeric(input: &str) -> Cow<'_, str> { re.replace_all(input, "") } -fn path_to_img(path: &str) -> (Vec, String, u32, u32) { - let img = match image::open(path) { - Ok(img) => img.to_luma8(), +fn path_to_img(path: &str) -> (Vec, String, u32, u32, u32) { + let (base_img, img) = match image::open(path) { + Ok(img) => (img.clone(), img.to_luma8()), Err(e) => panic!("failed to open image {path}: {e}"), }; @@ -41,11 +43,16 @@ fn path_to_img(path: &str) -> (Vec, String, u32, u32) { let mut bytes = Vec::new(); let mut bit = 0; let mut byte = 0; + let mut color = None; + for y in 0..height { for x in 0..width { let pix = img.get_pixel(x, y)[0]; - if pix >= 127 { - byte |= 1 << bit + if pix >= 5 { + byte |= 1 << bit; + if color == None { + color = Some(base_img.get_pixel(x, y)); + } } bit += 1; if bit == 32 { @@ -64,5 +71,12 @@ fn path_to_img(path: &str) -> (Vec, String, u32, u32) { "picture_{}", remove_non_alphanumeric(&split[0..split.len() - 1].join("_")).to_lowercase() ); - (bytes, name, width, height) + let color = color.map_or(0, |c| { + let mut r = 0; + r |= (c[0] as u32) << 24; + r |= (c[1] as u32) << 16; + r |= (c[2] as u32) << 8; + r + }); + (bytes, name, width, height, color) }