Changing the dithering algorithm

This commit is contained in:
Jurn Wubben 2025-09-25 21:26:07 +02:00
parent 410e203f9e
commit 322a951d51

View file

@ -1,45 +1,35 @@
use image::DynamicImage; use image::{DynamicImage, imageops};
pub fn atkinson_mono(img: &DynamicImage) -> Vec<u8> { pub fn atkinson_mono(img: &DynamicImage) -> Vec<u8> {
let mut gray = img.to_luma8(); let mut gray = img.to_luma8();
imageops::dither(&mut gray, &imageops::colorops::BiLevel);
let (w, h) = (gray.width() as usize, gray.height() as usize); let (w, h) = (gray.width() as usize, gray.height() as usize);
let pitch = (w + 7) >> 3; let stride = (w + 7) >> 3;
let mut bits = vec![0u8; pitch * h]; let mut bits = vec![0u8; stride * h];
let buf = gray.as_mut(); let pix = gray.as_raw();
for y in 0..h { for y in 0..h {
let row = y * w; let row_off = y * w;
for x in 0..w { let bit_off = y * stride;
let i = row + x; let chunks = w >> 3;
let old = buf[i]; let tail = w & 7;
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 { for x8 in 0..chunks {
buf[i + 1] = (buf[i + 1] as i16 + err).clamp(0, 255) as u8; let p = &pix[row_off + (x8 << 3)..][..8];
let mut byte = 0u8;
for (bit, &v) in p.iter().enumerate() {
byte |= ((v < 128) as u8) << (7 - bit);
} }
if x + 2 < w { bits[bit_off + x8] = byte;
buf[i + 2] = (buf[i + 2] as i16 + err).clamp(0, 255) as u8; }
}
if y + 1 < h { if tail != 0 {
let down = i + w; let mut last = 0u8;
if x > 0 { for (bit, &v) in pix[row_off + (chunks << 3)..][..tail].iter().enumerate() {
buf[down - 1] = (buf[down - 1] as i16 + err).clamp(0, 255) as u8; last |= ((v < 128) as u8) << (7 - bit);
}
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[bit_off + chunks] = last;
} }
} }
bits bits