Dither images using wasm plugin
This commit is contained in:
parent
179d0653bb
commit
92ec72ab4b
14 changed files with 116 additions and 15 deletions
|
|
@ -1,7 +1,8 @@
|
|||
use showbits_typst::Typst;
|
||||
|
||||
pub use self::text::*;
|
||||
pub use self::{image::*, text::*};
|
||||
|
||||
mod image;
|
||||
mod text;
|
||||
|
||||
fn typst_with_lib() -> Typst {
|
||||
|
|
|
|||
BIN
showbits-thermal-printer/src/documents/image/image.png
Normal file
BIN
showbits-thermal-printer/src/documents/image/image.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 370 KiB |
1
showbits-thermal-printer/src/documents/image/lib.typ
Symbolic link
1
showbits-thermal-printer/src/documents/image/lib.typ
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
../lib.typ
|
||||
4
showbits-thermal-printer/src/documents/image/main.typ
Normal file
4
showbits-thermal-printer/src/documents/image/main.typ
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
#import "lib.typ";
|
||||
#show: it => lib.init(it)
|
||||
|
||||
#lib.dither("image.png")
|
||||
24
showbits-thermal-printer/src/documents/image/mod.rs
Normal file
24
showbits-thermal-printer/src/documents/image/mod.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use std::io::Cursor;
|
||||
|
||||
use anyhow::Context;
|
||||
use image::{ImageFormat, RgbaImage};
|
||||
use showbits_typst::Typst;
|
||||
|
||||
pub struct Image {
|
||||
pub image: RgbaImage,
|
||||
}
|
||||
|
||||
impl Image {
|
||||
pub fn into_typst(self) -> anyhow::Result<Typst> {
|
||||
let mut bytes: Vec<u8> = Vec::new();
|
||||
self.image
|
||||
.write_to(&mut Cursor::new(&mut bytes), ImageFormat::Png)
|
||||
.context("failed to encode image as png")?;
|
||||
|
||||
let typst = super::typst_with_lib()
|
||||
.with_file("/image.png", bytes)
|
||||
.with_main_file(include_str!("main.typ"));
|
||||
|
||||
Ok(typst)
|
||||
}
|
||||
}
|
||||
1
showbits-thermal-printer/src/documents/image/plugin.wasm
Symbolic link
1
showbits-thermal-printer/src/documents/image/plugin.wasm
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
../plugin.wasm
|
||||
|
|
@ -20,4 +20,14 @@
|
|||
// the same size after tearing off the paper.
|
||||
#let feed = v(64pt + 32pt)
|
||||
|
||||
#import plugin("plugin.wasm") as plugin
|
||||
////////////
|
||||
// Plugin //
|
||||
////////////
|
||||
|
||||
#import plugin("plugin.wasm") as p
|
||||
|
||||
#let dither(path) = {
|
||||
let bytes = read(path, encoding: none)
|
||||
let dithered = p.dither(bytes)
|
||||
image(dithered)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
mod documents;
|
||||
mod drawer;
|
||||
mod persistent_printer;
|
||||
mod printer;
|
||||
mod server;
|
||||
mod documents;
|
||||
|
||||
use std::{path::PathBuf, time::Duration};
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ use showbits_common::widgets::DitherAlgorithm;
|
|||
use tokio::{net::TcpListener, sync::mpsc};
|
||||
|
||||
use crate::{
|
||||
documents::Text,
|
||||
documents::{Image, Text},
|
||||
drawer::{
|
||||
CalendarDrawing, CellsDrawing, ChatMessageDrawing, Command, EggDrawing, ImageDrawing,
|
||||
NewTypstDrawing, PhotoDrawing, TextDrawing, TicTacToeDrawing, TypstDrawing,
|
||||
|
|
@ -40,6 +40,7 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
|
|||
.route("/tictactoe", post(post_tictactoe))
|
||||
.route("/typst", post(post_typst).fallback(get_static_file))
|
||||
.route("/test", post(post_test).fallback(get_static_file))
|
||||
.route("/test2", post(post_test2).fallback(get_static_file))
|
||||
.fallback(get(get_static_file))
|
||||
.layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB
|
||||
.with_state(Server { tx });
|
||||
|
|
@ -235,3 +236,33 @@ async fn post_test(server: State<Server>, request: Form<Text>) {
|
|||
.send(Command::draw(NewTypstDrawing::new(request.0)))
|
||||
.await;
|
||||
}
|
||||
|
||||
// /test2
|
||||
|
||||
async fn post_test2(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
|
||||
let mut image = None;
|
||||
|
||||
while let Some(field) = multipart.next_field().await? {
|
||||
match field.name() {
|
||||
Some("image") => {
|
||||
let data = field.bytes().await?;
|
||||
let decoded = image::load_from_memory(&data)?.into_rgba8();
|
||||
image = Some(decoded);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let Some(image) = image else {
|
||||
return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY));
|
||||
};
|
||||
|
||||
let image = Image { image }.into_typst().map_err(somehow::Error)?;
|
||||
|
||||
let _ = server
|
||||
.tx
|
||||
.send(Command::draw(NewTypstDrawing::new(image)))
|
||||
.await;
|
||||
|
||||
Ok(().into_response())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue