initial commit
This commit is contained in:
58
oled-to-hex
Executable file
58
oled-to-hex
Executable file
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env -S denim
|
||||
/* Cargo.toml
|
||||
[package]
|
||||
name = "oled-to-hex"
|
||||
version = "0.1.0"
|
||||
authors = ["Anonymous"]
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
image = { version = "0.25.8", default-features = false, features = ["png"] }
|
||||
*/
|
||||
|
||||
use image::GenericImageView;
|
||||
use image::ImageReader;
|
||||
use image::Pixel;
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::io::{Error as IoError, ErrorKind};
|
||||
|
||||
/// Example script description
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let file = env::args().skip(1).next().ok_or(IoError::new(
|
||||
ErrorKind::NotFound,
|
||||
"expected PNG 128x32 file path",
|
||||
))?;
|
||||
println!("Loading {}", file);
|
||||
|
||||
let img = ImageReader::open(file)?.decode()?;
|
||||
let (w, h) = img.dimensions();
|
||||
println!("Loaded file {}x{}", w, h);
|
||||
if w != 128 && h != 32 {
|
||||
return Err(Into::into(IoError::new(
|
||||
ErrorKind::InvalidData,
|
||||
"bad immage dimensions",
|
||||
)));
|
||||
}
|
||||
|
||||
for row in 0..4 {
|
||||
for column in 0..128 {
|
||||
let mut byte = 0u8;
|
||||
for bit in 0..8 {
|
||||
byte = byte >> 1;
|
||||
let x = column;
|
||||
let y = row * 8 + bit;
|
||||
let pixel = img.get_pixel(x, y);
|
||||
let luma = pixel.to_luma()[0];
|
||||
if luma > 128 {
|
||||
byte = byte | 128;
|
||||
}
|
||||
}
|
||||
print!("{:02X}", byte);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// vim: ft=rust
|
||||
Reference in New Issue
Block a user