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

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
testing.png
.direnv/

1130
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

7
Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "printserv"
version = "0.1.0"
edition = "2024"
[dependencies]
image = {version = "0.25.8", features = ["png"]}

136
flake.lock generated Normal file
View file

@ -0,0 +1,136 @@
{
"nodes": {
"fenix": {
"inputs": {
"nixpkgs": [
"naersk",
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1752475459,
"narHash": "sha256-z6QEu4ZFuHiqdOPbYss4/Q8B0BFhacR8ts6jO/F/aOU=",
"owner": "nix-community",
"repo": "fenix",
"rev": "bf0d6f70f4c9a9cf8845f992105652173f4b617f",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"naersk": {
"inputs": {
"fenix": "fenix",
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1752689277,
"narHash": "sha256-uldUBFkZe/E7qbvxa3mH1ItrWZyT6w1dBKJQF/3ZSsc=",
"owner": "nix-community",
"repo": "naersk",
"rev": "0e72363d0938b0208d6c646d10649164c43f4d64",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1752077645,
"narHash": "sha256-HM791ZQtXV93xtCY+ZxG1REzhQenSQO020cu6rHtAPk=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "be9e214982e20b8310878ac2baa063a961c1bdf6",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1758446476,
"narHash": "sha256-5rdAi7CTvM/kSs6fHe1bREIva5W3TbImsto+dxG4mBo=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "a1f79a1770d05af18111fbbe2a3ab2c42c0f6cd0",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"naersk": "naersk",
"nixpkgs": "nixpkgs_2"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1752428706,
"narHash": "sha256-EJcdxw3aXfP8Ex1Nm3s0awyH9egQvB2Gu+QEnJn2Sfg=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "591e3b7624be97e4443ea7b5542c191311aa141d",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

37
flake.nix Normal file
View file

@ -0,0 +1,37 @@
{
inputs = {
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk"; #https://github.com/nix-community/naersk
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = {
self,
flake-utils,
naersk,
nixpkgs,
}:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = (import nixpkgs) {
inherit system;
};
naersk' = pkgs.callPackage naersk {};
in let
deps = [pkgs.pkg-config pkgs.openssl];
in {
# For `nix build` & `nix run`:
defaultPackage = naersk'.buildPackage {
name = "somcli";
src = ./.;
buildInputs = deps;
};
# For `nix develop` (optional, can be skipped):
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [rustc cargo rustfmt] ++ deps;
};
}
);
}

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();
}