bitmap to asm moved to the other repo
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "bitmap_to_asm"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
image = "0.25.10"
|
||||
regex = "1.12.3"
|
||||
@@ -1,82 +0,0 @@
|
||||
use image::GenericImageView;
|
||||
use regex::Regex;
|
||||
use std::{borrow::Cow, env::args, process::exit};
|
||||
|
||||
fn main() {
|
||||
let path = args().nth(1).expect("usage: file [--nosize]");
|
||||
let nosize = match args().nth(2) {
|
||||
None => false,
|
||||
Some(t) => {
|
||||
if t == "--nosize" {
|
||||
true
|
||||
} else {
|
||||
eprint!("usage: file [--nosize]");
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
};
|
||||
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}");
|
||||
}
|
||||
for d in data {
|
||||
println!(" D 0x{d:08x}");
|
||||
}
|
||||
}
|
||||
fn remove_non_alphanumeric(input: &str) -> Cow<'_, str> {
|
||||
let re = Regex::new(r"[^a-zA-Z0-9_]+").unwrap();
|
||||
re.replace_all(input, "")
|
||||
}
|
||||
|
||||
fn path_to_img(path: &str) -> (Vec<u32>, 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}"),
|
||||
};
|
||||
|
||||
let width = img.width();
|
||||
let height = img.height();
|
||||
|
||||
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 >= 5 {
|
||||
byte |= 1 << bit;
|
||||
if color == None {
|
||||
color = Some(base_img.get_pixel(x, y));
|
||||
}
|
||||
}
|
||||
bit += 1;
|
||||
if bit == 32 {
|
||||
bytes.push(byte);
|
||||
byte = 0;
|
||||
bit = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if bit != 0 {
|
||||
bytes.push(byte);
|
||||
}
|
||||
let path = path.split('/').next_back().unwrap();
|
||||
let split: Vec<_> = path.split('.').collect();
|
||||
let name = format!(
|
||||
"picture_{}",
|
||||
remove_non_alphanumeric(&split[0..split.len() - 1].join("_")).to_lowercase()
|
||||
);
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user