Make bright printing optional

This commit is contained in:
Joscha 2024-03-15 23:19:17 +01:00
parent 9d4ecaaf72
commit 4f3d66f6a0
3 changed files with 26 additions and 15 deletions

View file

@ -1,8 +1,9 @@
mod calendar; mod calendar;
use image::{Luma, Pixel, RgbaImage}; use image::{Luma, Pixel, RgbaImage};
use palette::{FromColor, IntoColor, LinLumaa};
use showbits_common::{ use showbits_common::{
color::{BLACK, WHITE}, color::{self, BLACK, WHITE},
widgets::{Block, FontStuff, HasFontStuff, Image, Text}, widgets::{Block, FontStuff, HasFontStuff, Image, Text},
Node, Tree, WidgetExt, Node, Tree, WidgetExt,
}; };
@ -19,7 +20,7 @@ pub enum Command {
Rip, Rip,
Test, Test,
Text(String), Text(String),
Image(RgbaImage), Image { image: RgbaImage, bright: bool },
Photo { image: RgbaImage, title: String }, Photo { image: RgbaImage, title: String },
ChatMessage { username: String, content: String }, ChatMessage { username: String, content: String },
Calendar { year: i32, month: u8 }, Calendar { year: i32, month: u8 },
@ -70,7 +71,7 @@ impl Drawer {
Command::Rip => self.printer.rip()?, Command::Rip => self.printer.rip()?,
Command::Test => self.on_test()?, Command::Test => self.on_test()?,
Command::Text(text) => self.on_text(text)?, Command::Text(text) => self.on_text(text)?,
Command::Image(image) => self.on_image(image)?, Command::Image { image, bright } => self.on_image(image, bright)?,
Command::Photo { image, title } => self.on_photo(image, title)?, Command::Photo { image, title } => self.on_photo(image, title)?,
Command::ChatMessage { username, content } => { Command::ChatMessage { username, content } => {
self.on_chat_message(username, content)? self.on_chat_message(username, content)?
@ -131,15 +132,15 @@ impl Drawer {
Ok(()) Ok(())
} }
fn on_image(&mut self, mut image: RgbaImage) -> anyhow::Result<()> { fn on_image(&mut self, mut image: RgbaImage, bright: bool) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new(WHITE); let mut tree = Tree::<Context>::new(WHITE);
for pixel in image.pixels_mut() { if bright {
let [l] = pixel.to_luma().0; for pixel in image.pixels_mut() {
let l = l as f32 / 255.0; // Convert to [0, 1] let mut color = LinLumaa::from_color(color::from_image_color(*pixel));
let l = 1.0 - (0.4 * (1.0 - l)); // Lerp to [0.6, 1] color.luma = 1.0 - 0.4 * (1.0 - color.luma);
let l = (l.clamp(0.0, 1.0) * 255.0) as u8; // Convert back to [0, 255] *pixel = color::to_image_color(color.into_color());
*pixel = Luma([l]).to_rgba(); }
} }
let image = Image::new(image) let image = Image::new(image)

View file

@ -63,12 +63,19 @@ async fn post_text(server: State<Server>, request: Form<PostTextForm>) {
async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> { async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
let mut image = None; let mut image = None;
let mut bright = false;
while let Some(field) = multipart.next_field().await? { while let Some(field) = multipart.next_field().await? {
if let Some("image") = field.name() { match field.name() {
let data = field.bytes().await?; Some("image") => {
let decoded = image::load_from_memory(&data)?.into_rgba8(); let data = field.bytes().await?;
image = Some(decoded); let decoded = image::load_from_memory(&data)?.into_rgba8();
image = Some(decoded);
}
Some("bright") => {
bright = true;
}
_ => {}
} }
} }
@ -76,7 +83,7 @@ async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow:
return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY)); return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY));
}; };
let _ = server.tx.send(Command::Image(image)).await; let _ = server.tx.send(Command::Image { image, bright }).await;
Ok(Redirect::to("image").into_response()) Ok(Redirect::to("image").into_response())
} }

View file

@ -10,6 +10,9 @@
<form method="post" enctype="multipart/form-data"> <form method="post" enctype="multipart/form-data">
<ol> <ol>
<li><input type="file" name="image" /></li> <li><input type="file" name="image" /></li>
<li>
<label><input type="checkbox" name="bright" checked /> Bright</label>
</li>
<li><button>Print!</button></li> <li><button>Print!</button></li>
</ol> </ol>
</form> </form>