Print images lighter
This commit is contained in:
parent
e7d3d2b638
commit
9d4ecaaf72
2 changed files with 25 additions and 13 deletions
|
|
@ -3,7 +3,7 @@ use image::{
|
||||||
RgbaImage,
|
RgbaImage,
|
||||||
};
|
};
|
||||||
use mark::dither::{AlgoFloydSteinberg, AlgoStucki, Algorithm, DiffEuclid, Palette};
|
use mark::dither::{AlgoFloydSteinberg, AlgoStucki, Algorithm, DiffEuclid, Palette};
|
||||||
use palette::{IntoColor, Srgb, Srgba};
|
use palette::{IntoColor, LinSrgb, Srgba};
|
||||||
use taffy::prelude::{AvailableSpace, Layout, Size};
|
use taffy::prelude::{AvailableSpace, Layout, Size};
|
||||||
|
|
||||||
use crate::Widget;
|
use crate::Widget;
|
||||||
|
|
@ -15,12 +15,12 @@ pub enum DitherAlgorithm {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DitherAlgorithm {
|
impl DitherAlgorithm {
|
||||||
fn dither(self, image: RgbaImage, palette: &Palette<Srgb>) -> RgbaImage {
|
fn dither(self, image: RgbaImage, palette: &Palette<LinSrgb>) -> RgbaImage {
|
||||||
match self {
|
match self {
|
||||||
Self::FloydSteinberg => {
|
Self::FloydSteinberg => {
|
||||||
<AlgoFloydSteinberg as Algorithm<Srgb, DiffEuclid>>::run(image, palette)
|
<AlgoFloydSteinberg as Algorithm<LinSrgb, DiffEuclid>>::run(image, palette)
|
||||||
}
|
}
|
||||||
Self::Stucki => <AlgoStucki as Algorithm<Srgb, DiffEuclid>>::run(image, palette),
|
Self::Stucki => <AlgoStucki as Algorithm<LinSrgb, DiffEuclid>>::run(image, palette),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -31,11 +31,7 @@ pub struct Image {
|
||||||
grow: bool,
|
grow: bool,
|
||||||
filter: FilterType,
|
filter: FilterType,
|
||||||
|
|
||||||
// The palette uses Srgb instead of LinSrgb because dithering in Srgb
|
dither_palette: Option<Palette<LinSrgb>>,
|
||||||
// produces better results when printed, even though color distance
|
|
||||||
// calculations are objectively wrong. Maybe the printer's black ink
|
|
||||||
// behaviour makes them correct again.
|
|
||||||
dither_palette: Option<Palette<Srgb>>,
|
|
||||||
dither_algorithm: DitherAlgorithm,
|
dither_algorithm: DitherAlgorithm,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -70,7 +66,7 @@ impl Image {
|
||||||
let palette = palette
|
let palette = palette
|
||||||
.iter()
|
.iter()
|
||||||
.map(|c| c.color.into_color())
|
.map(|c| c.color.into_color())
|
||||||
.collect::<Vec<Srgb>>();
|
.collect::<Vec<LinSrgb>>();
|
||||||
|
|
||||||
self.dither_palette = Some(Palette::new(palette));
|
self.dither_palette = Some(Palette::new(palette));
|
||||||
self
|
self
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
mod calendar;
|
mod calendar;
|
||||||
|
|
||||||
use image::RgbaImage;
|
use image::{Luma, Pixel, RgbaImage};
|
||||||
use showbits_common::{
|
use showbits_common::{
|
||||||
color::{BLACK, WHITE},
|
color::{BLACK, WHITE},
|
||||||
widgets::{Block, FontStuff, HasFontStuff, Image, Text},
|
widgets::{Block, FontStuff, HasFontStuff, Image, Text},
|
||||||
|
|
@ -131,9 +131,17 @@ impl Drawer {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_image(&mut self, image: RgbaImage) -> anyhow::Result<()> {
|
fn on_image(&mut self, mut image: RgbaImage) -> anyhow::Result<()> {
|
||||||
let mut tree = Tree::<Context>::new(WHITE);
|
let mut tree = Tree::<Context>::new(WHITE);
|
||||||
|
|
||||||
|
for pixel in image.pixels_mut() {
|
||||||
|
let [l] = pixel.to_luma().0;
|
||||||
|
let l = l as f32 / 255.0; // Convert to [0, 1]
|
||||||
|
let l = 1.0 - (0.4 * (1.0 - l)); // Lerp to [0.6, 1]
|
||||||
|
let l = (l.clamp(0.0, 1.0) * 255.0) as u8; // Convert back to [0, 255]
|
||||||
|
*pixel = Luma([l]).to_rgba();
|
||||||
|
}
|
||||||
|
|
||||||
let image = Image::new(image)
|
let image = Image::new(image)
|
||||||
.with_dither_palette(&[BLACK, WHITE])
|
.with_dither_palette(&[BLACK, WHITE])
|
||||||
.node()
|
.node()
|
||||||
|
|
@ -152,9 +160,17 @@ impl Drawer {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_photo(&mut self, image: RgbaImage, title: String) -> anyhow::Result<()> {
|
fn on_photo(&mut self, mut image: RgbaImage, title: String) -> anyhow::Result<()> {
|
||||||
let mut tree = Tree::<Context>::new(WHITE);
|
let mut tree = Tree::<Context>::new(WHITE);
|
||||||
|
|
||||||
|
for pixel in image.pixels_mut() {
|
||||||
|
let [l] = pixel.to_luma().0;
|
||||||
|
let l = l as f32 / 255.0; // Convert to [0, 1]
|
||||||
|
let l = 1.0 - (0.4 * (1.0 - l)); // Lerp to [0.6, 1]
|
||||||
|
let l = (l.clamp(0.0, 1.0) * 255.0) as u8; // Convert back to [0, 255]
|
||||||
|
*pixel = Luma([l]).to_rgba();
|
||||||
|
}
|
||||||
|
|
||||||
let image = Image::new(image)
|
let image = Image::new(image)
|
||||||
.with_dither_palette(&[BLACK, WHITE])
|
.with_dither_palette(&[BLACK, WHITE])
|
||||||
.node()
|
.node()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue