52 lines
1.4 KiB
Rust
52 lines
1.4 KiB
Rust
use image::{
|
|
DynamicImage, GrayImage, Luma,
|
|
imageops::{self, BiLevel, colorops, colorops::ColorMap, index_colors},
|
|
};
|
|
|
|
struct CustomLevel {
|
|
threshold: u8,
|
|
}
|
|
|
|
impl ColorMap for CustomLevel {
|
|
type Color = Luma<u8>;
|
|
|
|
fn index_of(&self, color: &Self::Color) -> usize {
|
|
let v = color.0[0];
|
|
if v >= self.threshold { 1 } else { 0 }
|
|
}
|
|
|
|
fn map_color(&self, color: &mut Self::Color) {
|
|
color.0[0] = if color.0[0] >= self.threshold { 255 } else { 0 };
|
|
}
|
|
}
|
|
|
|
pub fn auto_brighten(gray: &mut GrayImage) -> () {
|
|
let avg = gray.pixels().map(|p| p.0[0] as u32).sum::<u32>() / gray.len() as u32;
|
|
|
|
if avg < 100 {
|
|
colorops::brighten_in_place(gray, 40);
|
|
colorops::contrast_in_place(gray, 25.0);
|
|
} else if avg > 180 {
|
|
colorops::brighten_in_place(gray, -15);
|
|
}
|
|
}
|
|
|
|
pub fn dither(gray: &mut GrayImage) -> Vec<u8> {
|
|
let level = CustomLevel { threshold: 175 };
|
|
let bw: image::ImageBuffer<Luma<u8>, Vec<u8>> = index_colors(&gray, &level);
|
|
|
|
let (w, h) = (gray.width() as usize, gray.height() as usize);
|
|
let pitch = (w + 7) >> 3;
|
|
let mut bits = vec![0u8; pitch * h];
|
|
|
|
for y in 0..h {
|
|
for x in 0..w {
|
|
let byte = y * pitch + (x >> 3);
|
|
let bit = 7 - (x & 7);
|
|
if bw[(x as u32, y as u32)].0[0] == 0 {
|
|
bits[byte] |= 1 << bit;
|
|
}
|
|
}
|
|
}
|
|
bits
|
|
}
|