Compare commits
No commits in common. "brightness-cutoff" and "master" have entirely different histories.
brightness
...
master
4 changed files with 5 additions and 24 deletions
BIN
out.png
Normal file
BIN
out.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
BIN
small.png
Normal file
BIN
small.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 475 KiB |
|
|
@ -1,25 +1,7 @@
|
||||||
use image::{
|
use image::{
|
||||||
DynamicImage, GrayImage, Luma,
|
DynamicImage, GrayImage,
|
||||||
imageops::{self, BiLevel, colorops, colorops::ColorMap, index_colors},
|
imageops::{self, colorops},
|
||||||
};
|
};
|
||||||
|
|
||||||
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) -> () {
|
pub fn auto_brighten(gray: &mut GrayImage) -> () {
|
||||||
let avg = gray.pixels().map(|p| p.0[0] as u32).sum::<u32>() / gray.len() as u32;
|
let avg = gray.pixels().map(|p| p.0[0] as u32).sum::<u32>() / gray.len() as u32;
|
||||||
|
|
||||||
|
|
@ -32,8 +14,7 @@ pub fn auto_brighten(gray: &mut GrayImage) -> () {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dither(gray: &mut GrayImage) -> Vec<u8> {
|
pub fn dither(gray: &mut GrayImage) -> Vec<u8> {
|
||||||
let level = CustomLevel { threshold: 175 };
|
imageops::dither(gray, &imageops::colorops::BiLevel);
|
||||||
let bw: image::ImageBuffer<Luma<u8>, Vec<u8>> = index_colors(&gray, &level);
|
|
||||||
|
|
||||||
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 pitch = (w + 7) >> 3;
|
||||||
|
|
@ -43,7 +24,7 @@ pub fn dither(gray: &mut GrayImage) -> Vec<u8> {
|
||||||
for x in 0..w {
|
for x in 0..w {
|
||||||
let byte = y * pitch + (x >> 3);
|
let byte = y * pitch + (x >> 3);
|
||||||
let bit = 7 - (x & 7);
|
let bit = 7 - (x & 7);
|
||||||
if bw[(x as u32, y as u32)].0[0] == 0 {
|
if gray[(x as u32, y as u32)].0[0] == 0 {
|
||||||
bits[byte] |= 1 << bit;
|
bits[byte] |= 1 << bit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ pub fn escpos_raster(
|
||||||
let img = img.resize(w, h, FilterType::Triangle);
|
let img = img.resize(w, h, FilterType::Triangle);
|
||||||
|
|
||||||
let mut gray = img.to_luma8();
|
let mut gray = img.to_luma8();
|
||||||
// auto_brighten(&mut gray);
|
auto_brighten(&mut gray);
|
||||||
|
|
||||||
let mono = dither(&mut gray);
|
let mono = dither(&mut gray);
|
||||||
let (width_px, height_px) = (img.width() as u16, img.height() as u16);
|
let (width_px, height_px) = (img.width() as u16, img.height() as u16);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue