Turn Photo into drawing
This commit is contained in:
parent
aa02b42f5d
commit
13ee9c5267
3 changed files with 72 additions and 53 deletions
|
|
@ -1,24 +1,25 @@
|
||||||
mod calendar;
|
mod calendar;
|
||||||
mod cells;
|
mod cells;
|
||||||
mod image;
|
mod image;
|
||||||
|
mod photo;
|
||||||
mod text;
|
mod text;
|
||||||
|
|
||||||
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, Text},
|
||||||
Node, Tree, WidgetExt,
|
Node, Tree, WidgetExt,
|
||||||
};
|
};
|
||||||
use taffy::{
|
use taffy::{
|
||||||
style_helpers::{length, percent},
|
style_helpers::{length, percent},
|
||||||
AlignItems, Display, FlexDirection,
|
AlignItems, FlexDirection,
|
||||||
};
|
};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
use crate::printer::Printer;
|
use crate::printer::Printer;
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
calendar::CalendarDrawing, cells::CellsDrawing, image::ImageDrawing, text::TextDrawing,
|
calendar::CalendarDrawing, cells::CellsDrawing, image::ImageDrawing, photo::PhotoDrawing,
|
||||||
|
text::TextDrawing,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
@ -35,7 +36,6 @@ pub struct BoxedDrawing(Box<dyn Drawing + Send>);
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
Draw(BoxedDrawing),
|
Draw(BoxedDrawing),
|
||||||
|
|
||||||
Photo { image: RgbaImage, title: String },
|
|
||||||
ChatMessage { username: String, content: String },
|
ChatMessage { username: String, content: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,8 +58,6 @@ pub struct Drawer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drawer {
|
impl Drawer {
|
||||||
const FEED: f32 = 64.0;
|
|
||||||
|
|
||||||
pub fn new(rx: mpsc::Receiver<Command>, printer: Printer) -> Self {
|
pub fn new(rx: mpsc::Receiver<Command>, printer: Printer) -> Self {
|
||||||
Self {
|
Self {
|
||||||
rx,
|
rx,
|
||||||
|
|
@ -79,7 +77,6 @@ impl Drawer {
|
||||||
match command {
|
match command {
|
||||||
Command::Draw(drawing) => drawing.0.draw(&mut self.printer, &mut self.ctx)?,
|
Command::Draw(drawing) => drawing.0.draw(&mut self.printer, &mut self.ctx)?,
|
||||||
|
|
||||||
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)?
|
||||||
}
|
}
|
||||||
|
|
@ -87,49 +84,6 @@ impl Drawer {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_photo(&mut self, mut image: RgbaImage, title: String) -> anyhow::Result<()> {
|
|
||||||
println!(
|
|
||||||
"Printing photo {title:?} ({}x{})",
|
|
||||||
image.width(),
|
|
||||||
image.height()
|
|
||||||
);
|
|
||||||
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)
|
|
||||||
.with_dither_palette(&[BLACK, WHITE])
|
|
||||||
.node()
|
|
||||||
.register(&mut tree)?;
|
|
||||||
|
|
||||||
let title = Text::new()
|
|
||||||
.with_metrics(Text::default_metrics().scale(2.0))
|
|
||||||
.and_plain(title)
|
|
||||||
.widget(&mut self.ctx.font_stuff)
|
|
||||||
.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))
|
|
||||||
.with_gap(length(8.0))
|
|
||||||
.and_child(image)
|
|
||||||
.and_child(title)
|
|
||||||
.register(&mut tree)?;
|
|
||||||
|
|
||||||
self.printer.print_tree(&mut tree, &mut self.ctx, root)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_chat_message(&mut self, username: String, content: String) -> anyhow::Result<()> {
|
fn on_chat_message(&mut self, username: String, content: String) -> anyhow::Result<()> {
|
||||||
let mut tree = Tree::<Context>::new(WHITE);
|
let mut tree = Tree::<Context>::new(WHITE);
|
||||||
|
|
||||||
|
|
|
||||||
60
showbits-thermal-printer/src/drawer/photo.rs
Normal file
60
showbits-thermal-printer/src/drawer/photo.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
use image::{Luma, Pixel, RgbaImage};
|
||||||
|
use showbits_common::{
|
||||||
|
color::{BLACK, WHITE},
|
||||||
|
widgets::{Image, Text},
|
||||||
|
Node, Tree, WidgetExt,
|
||||||
|
};
|
||||||
|
use taffy::{
|
||||||
|
style_helpers::{length, percent},
|
||||||
|
AlignItems, Display, FlexDirection,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::printer::Printer;
|
||||||
|
|
||||||
|
use super::{Context, Drawing};
|
||||||
|
|
||||||
|
pub struct PhotoDrawing {
|
||||||
|
pub image: RgbaImage,
|
||||||
|
pub title: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drawing for PhotoDrawing {
|
||||||
|
fn draw(&self, printer: &mut Printer, ctx: &mut Context) -> anyhow::Result<()> {
|
||||||
|
let mut tree = Tree::<Context>::new(WHITE);
|
||||||
|
|
||||||
|
let mut image = self.image.clone();
|
||||||
|
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)
|
||||||
|
.with_dither_palette(&[BLACK, WHITE])
|
||||||
|
.node()
|
||||||
|
.register(&mut tree)?;
|
||||||
|
|
||||||
|
let title = Text::new()
|
||||||
|
.with_metrics(Text::default_metrics().scale(2.0))
|
||||||
|
.and_plain(&self.title)
|
||||||
|
.widget(&mut ctx.font_stuff)
|
||||||
|
.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))
|
||||||
|
.with_gap(length(8.0))
|
||||||
|
.and_child(image)
|
||||||
|
.and_child(title)
|
||||||
|
.register(&mut tree)?;
|
||||||
|
|
||||||
|
printer.print_tree(&mut tree, ctx, root)?;
|
||||||
|
printer.feed()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -12,7 +12,9 @@ use axum::{
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use tokio::{net::TcpListener, sync::mpsc};
|
use tokio::{net::TcpListener, sync::mpsc};
|
||||||
|
|
||||||
use crate::drawer::{CalendarDrawing, CellsDrawing, Command, ImageDrawing, TextDrawing};
|
use crate::drawer::{
|
||||||
|
CalendarDrawing, CellsDrawing, Command, ImageDrawing, PhotoDrawing, TextDrawing,
|
||||||
|
};
|
||||||
|
|
||||||
use self::{r#static::get_static_file, statuscode::status_code};
|
use self::{r#static::get_static_file, statuscode::status_code};
|
||||||
|
|
||||||
|
|
@ -105,7 +107,10 @@ async fn post_photo(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::Photo { image, title }).await;
|
let _ = server
|
||||||
|
.tx
|
||||||
|
.send(Command::draw(PhotoDrawing { image, title }))
|
||||||
|
.await;
|
||||||
Ok(Redirect::to("photo").into_response())
|
Ok(Redirect::to("photo").into_response())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue