Turn Image into drawing
This commit is contained in:
parent
0bc2942b2b
commit
aa02b42f5d
3 changed files with 60 additions and 37 deletions
|
|
@ -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<dyn Drawing + Send>);
|
|||
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::<Context>::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{})",
|
||||
|
|
|
|||
49
showbits-thermal-printer/src/drawer/image.rs
Normal file
49
showbits-thermal-printer/src/drawer/image.rs
Normal file
|
|
@ -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::<Context>::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(())
|
||||
}
|
||||
}
|
||||
|
|
@ -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<Server>, 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())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue