diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index 2d85b55..9e2314e 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -1,11 +1,11 @@ mod calendar; mod cells; +mod image; mod text; -use image::{Luma, Pixel, RgbaImage}; -use palette::{FromColor, IntoColor, LinLumaa}; +use ::image::{Luma, Pixel, RgbaImage}; use showbits_common::{ - color::{self, BLACK, WHITE}, + color::{BLACK, WHITE}, widgets::{Block, FontStuff, HasFontStuff, Image, Text}, Node, Tree, WidgetExt, }; @@ -17,7 +17,9 @@ use tokio::sync::mpsc; use crate::printer::Printer; -pub use self::{calendar::CalendarDrawing, cells::CellsDrawing, text::TextDrawing}; +pub use self::{ + calendar::CalendarDrawing, cells::CellsDrawing, image::ImageDrawing, text::TextDrawing, +}; #[derive(Default)] pub struct Context { @@ -33,7 +35,6 @@ pub struct BoxedDrawing(Box); pub enum Command { Draw(BoxedDrawing), - Image { image: RgbaImage, bright: bool }, Photo { image: RgbaImage, title: String }, ChatMessage { username: String, content: String }, } @@ -78,7 +79,6 @@ impl Drawer { match command { Command::Draw(drawing) => drawing.0.draw(&mut self.printer, &mut self.ctx)?, - Command::Image { image, bright } => self.on_image(image, bright)?, Command::Photo { image, title } => self.on_photo(image, title)?, Command::ChatMessage { username, content } => { self.on_chat_message(username, content)? @@ -87,35 +87,6 @@ impl Drawer { Ok(()) } - fn on_image(&mut self, mut image: RgbaImage, bright: bool) -> anyhow::Result<()> { - let mut tree = Tree::::new(WHITE); - - if bright { - for pixel in image.pixels_mut() { - let mut color = LinLumaa::from_color(color::from_image_color(*pixel)); - color.luma = 1.0 - 0.4 * (1.0 - color.luma); - *pixel = color::to_image_color(color.into_color()); - } - } - - let image = Image::new(image) - .with_dither_palette(&[BLACK, WHITE]) - .node() - .register(&mut tree)?; - - let root = Node::empty() - .with_size_width(percent(1.0)) - .with_padding_bottom(length(Self::FEED)) - .with_display(Display::Flex) - .with_flex_direction(FlexDirection::Column) - .with_align_items(Some(AlignItems::Center)) - .and_child(image) - .register(&mut tree)?; - - self.printer.print_tree(&mut tree, &mut self.ctx, root)?; - Ok(()) - } - fn on_photo(&mut self, mut image: RgbaImage, title: String) -> anyhow::Result<()> { println!( "Printing photo {title:?} ({}x{})", diff --git a/showbits-thermal-printer/src/drawer/image.rs b/showbits-thermal-printer/src/drawer/image.rs new file mode 100644 index 0000000..af84f71 --- /dev/null +++ b/showbits-thermal-printer/src/drawer/image.rs @@ -0,0 +1,49 @@ +use image::RgbaImage; +use palette::{FromColor, IntoColor, LinLumaa}; +use showbits_common::{ + color::{self, BLACK, WHITE}, + widgets::Image, + Node, Tree, WidgetExt, +}; +use taffy::{style_helpers::percent, AlignItems, Display, FlexDirection}; + +use crate::printer::Printer; + +use super::{Context, Drawing}; + +pub struct ImageDrawing { + pub image: RgbaImage, + pub bright: bool, +} + +impl Drawing for ImageDrawing { + fn draw(&self, printer: &mut Printer, ctx: &mut Context) -> anyhow::Result<()> { + let mut image = self.image.clone(); + if self.bright { + for pixel in image.pixels_mut() { + let mut color = LinLumaa::from_color(color::from_image_color(*pixel)); + color.luma = 1.0 - 0.4 * (1.0 - color.luma); + *pixel = color::to_image_color(color.into_color()); + } + } + + let mut tree = Tree::::new(WHITE); + + let image = Image::new(image) + .with_dither_palette(&[BLACK, WHITE]) + .node() + .register(&mut tree)?; + + let root = Node::empty() + .with_size_width(percent(1.0)) + .with_display(Display::Flex) + .with_flex_direction(FlexDirection::Column) + .with_align_items(Some(AlignItems::Center)) + .and_child(image) + .register(&mut tree)?; + + printer.print_tree(&mut tree, ctx, root)?; + printer.feed()?; + Ok(()) + } +} diff --git a/showbits-thermal-printer/src/server.rs b/showbits-thermal-printer/src/server.rs index e727419..0465d05 100644 --- a/showbits-thermal-printer/src/server.rs +++ b/showbits-thermal-printer/src/server.rs @@ -12,7 +12,7 @@ use axum::{ use serde::Deserialize; use tokio::{net::TcpListener, sync::mpsc}; -use crate::drawer::{CalendarDrawing, CellsDrawing, Command, TextDrawing}; +use crate::drawer::{CalendarDrawing, CellsDrawing, Command, ImageDrawing, TextDrawing}; use self::{r#static::get_static_file, statuscode::status_code}; @@ -72,7 +72,10 @@ async fn post_image(server: State, mut multipart: Multipart) -> somehow: return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY)); }; - let _ = server.tx.send(Command::Image { image, bright }).await; + let _ = server + .tx + .send(Command::draw(ImageDrawing { image, bright })) + .await; Ok(Redirect::to("image").into_response()) }