#!/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"] } atty = { version = "0.2.14" } */ use image::GenericImageView; use image::ImageReader; use image::Pixel; use std::env; use std::error::Error; use std::io::{stdout, Error as IoError, ErrorKind, Write}; /// Example script description fn main() -> Result<(), Box> { let file = env::args().skip(1).next().ok_or(IoError::new( ErrorKind::NotFound, "expected PNG 128x32 file path", ))?; eprintln!("Loading {}", file); let img = ImageReader::open(file)?.decode()?; let (w, h) = img.dimensions(); eprintln!("Loaded file {}x{}", w, h); if w != 128 && h != 32 { return Err(Into::into(IoError::new( ErrorKind::InvalidData, "bad immage dimensions", ))); } let out_hex = atty::is(atty::Stream::Stdout); 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; } } if out_hex { print!("{:02X}", byte); } else { stdout().write(&[byte]).unwrap(); } } } Ok(()) } // vim: ft=rust