Basic POC cli working

This commit is contained in:
Jurn Wubben 2025-09-23 21:13:21 +02:00
commit 410e203f9e
9 changed files with 1433 additions and 0 deletions

46
src/dithering.rs Normal file
View file

@ -0,0 +1,46 @@
use image::DynamicImage;
pub fn atkinson_mono(img: &DynamicImage) -> Vec<u8> {
let mut gray = img.to_luma8();
let (w, h) = (gray.width() as usize, gray.height() as usize);
let pitch = (w + 7) >> 3;
let mut bits = vec![0u8; pitch * h];
let buf = gray.as_mut();
for y in 0..h {
let row = y * w;
for x in 0..w {
let i = row + x;
let old = buf[i];
let new = if old < 128 { 0 } else { 255 };
buf[i] = new;
let bit = new == 0;
if bit {
let byte = y * pitch + (x >> 3);
bits[byte] |= 128 >> (x & 7);
}
let err = ((old as i16) - (new as i16)) / 8;
if x + 1 < w {
buf[i + 1] = (buf[i + 1] as i16 + err).clamp(0, 255) as u8;
}
if x + 2 < w {
buf[i + 2] = (buf[i + 2] as i16 + err).clamp(0, 255) as u8;
}
if y + 1 < h {
let down = i + w;
if x > 0 {
buf[down - 1] = (buf[down - 1] as i16 + err).clamp(0, 255) as u8;
}
buf[down] = (buf[down] as i16 + err).clamp(0, 255) as u8;
if x + 1 < w {
buf[down + 1] = (buf[down + 1] as i16 + err).clamp(0, 255) as u8;
}
if y + 2 < h {
buf[down + w] = (buf[down + w] as i16 + err).clamp(0, 255) as u8;
}
}
}
}
bits
}

46
src/escpos.rs Normal file
View file

@ -0,0 +1,46 @@
use crate::dithering::atkinson_mono;
use image::{DynamicImage, imageops::FilterType};
const MAX_WIDTH: u32 = 384;
pub enum ImageOrientation {
Preserve,
Largest,
}
pub fn escpos_raster(
img: &DynamicImage,
orientation: Option<ImageOrientation>,
mode: Option<u8>,
) -> Vec<u8> {
let mode = mode.unwrap_or(0);
assert!(mode <= 3 || (48..=51).contains(&mode));
let (w, h) = (img.width(), img.height());
let swap = matches!(orientation, Some(ImageOrientation::Largest))
&& h.min(MAX_WIDTH) * w > w.min(MAX_WIDTH) * h;
let (w, h) = if swap { (h, w) } else { (w, h) };
let (w, h) = (MAX_WIDTH, h.saturating_mul(MAX_WIDTH).div_ceil(w));
let img = if swap { &img.rotate90() } else { &img };
let img = img.resize(w, h, FilterType::Triangle);
let mono = atkinson_mono(&img);
let (width_px, height_px) = (img.width() as u16, img.height() as u16);
let width_bytes = ((width_px + 7) >> 3) as u16;
let mut out = Vec::with_capacity(8 + mono.len());
out.extend_from_slice(&[
0x1D,
0x76,
0x30,
mode,
width_bytes as u8,
(width_bytes >> 8) as u8,
height_px as u8,
(height_px >> 8) as u8,
]);
out.extend_from_slice(&mono);
out
}

27
src/main.rs Normal file
View file

@ -0,0 +1,27 @@
mod dithering;
mod escpos;
use image::{ImageError, ImageReader};
use std::{env, fs, io, io::Write, process};
fn main() {
let args: Vec<String> = env::args().collect();
let len = args.len();
if len < 2 || len > 2 {
println!("Please provide a path to the image.");
process::exit(1);
}
let img = ImageReader::open(&args[1])
.map_err(|err| ImageError::IoError(err))
.and_then(|v| v.decode())
.unwrap();
let mut escpos = escpos::escpos_raster(&img, Some(escpos::ImageOrientation::Largest), Some(0));
for _ in 0..2 {
escpos.push('\n' as u8);
}
io::stdout().write(&escpos).unwrap();
}