Typstify /image endpoint
This commit is contained in:
parent
98071dfe32
commit
fa43074f3d
11 changed files with 179 additions and 189 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -2508,7 +2508,6 @@ dependencies = [
|
||||||
"image",
|
"image",
|
||||||
"jiff",
|
"jiff",
|
||||||
"mime_guess",
|
"mime_guess",
|
||||||
"palette",
|
|
||||||
"rand 0.9.0",
|
"rand 0.9.0",
|
||||||
"rust-embed",
|
"rust-embed",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ escpos = { workspace = true }
|
||||||
image = { workspace = true, default-features = true }
|
image = { workspace = true, default-features = true }
|
||||||
jiff = { workspace = true }
|
jiff = { workspace = true }
|
||||||
mime_guess = { workspace = true }
|
mime_guess = { workspace = true }
|
||||||
palette = { workspace = true }
|
|
||||||
rand = { workspace = true }
|
rand = { workspace = true }
|
||||||
rust-embed = { workspace = true }
|
rust-embed = { workspace = true }
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
use showbits_typst::Typst;
|
use showbits_typst::Typst;
|
||||||
|
|
||||||
pub use self::image::*;
|
pub mod image;
|
||||||
|
|
||||||
mod image;
|
|
||||||
pub mod text;
|
pub mod text;
|
||||||
|
|
||||||
fn typst_with_lib() -> Typst {
|
fn typst_with_lib() -> Typst {
|
||||||
|
|
|
||||||
6
showbits-thermal-printer/src/documents/image/data.json
Normal file
6
showbits-thermal-printer/src/documents/image/data.json
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"seamless": true,
|
||||||
|
"feed": true,
|
||||||
|
"bright": true,
|
||||||
|
"algo": "floyd-steinberg"
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,19 @@
|
||||||
#import "lib.typ";
|
#import "lib.typ";
|
||||||
#show: it => lib.init(it)
|
#show: it => lib.init(it)
|
||||||
|
|
||||||
#lib.dither("image.png")
|
#let data = json("data.json")
|
||||||
|
|
||||||
|
#let dithered = lib.dither(
|
||||||
|
"image.png",
|
||||||
|
bright: data.bright,
|
||||||
|
algorithm: data.algo,
|
||||||
|
)
|
||||||
|
|
||||||
|
#if data.seamless {
|
||||||
|
set page(margin: 0pt)
|
||||||
|
dithered
|
||||||
|
if data.feed { lib.feed }
|
||||||
|
} else {
|
||||||
|
dithered
|
||||||
|
if data.feed { lib.feed }
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,78 @@
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use image::{ImageFormat, RgbaImage};
|
use axum::{
|
||||||
use showbits_typst::Typst;
|
extract::{Multipart, State},
|
||||||
|
http::StatusCode,
|
||||||
|
response::{IntoResponse, Redirect, Response},
|
||||||
|
};
|
||||||
|
use image::ImageFormat;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
pub struct Image {
|
use crate::{
|
||||||
pub image: RgbaImage,
|
drawer::{Command, NewTypstDrawing},
|
||||||
|
server::{Server, somehow, statuscode::status_code},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct Data {
|
||||||
|
pub seamless: bool,
|
||||||
|
pub feed: bool,
|
||||||
|
pub bright: bool,
|
||||||
|
pub algo: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Image {
|
pub async fn post(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
|
||||||
pub fn into_typst(self) -> anyhow::Result<Typst> {
|
let mut image = None;
|
||||||
let mut bytes: Vec<u8> = Vec::new();
|
let mut data = Data {
|
||||||
self.image
|
seamless: false,
|
||||||
.write_to(&mut Cursor::new(&mut bytes), ImageFormat::Png)
|
feed: true,
|
||||||
.context("failed to encode image as png")?;
|
bright: true,
|
||||||
|
algo: "floyd-steinberg".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
let typst = super::typst_with_lib()
|
while let Some(field) = multipart.next_field().await? {
|
||||||
.with_file("/image.png", bytes)
|
match field.name() {
|
||||||
.with_main_file(include_str!("main.typ"));
|
Some("image") => {
|
||||||
|
let data = field.bytes().await?;
|
||||||
Ok(typst)
|
let decoded = image::load_from_memory(&data)?.into_rgba8();
|
||||||
|
image = Some(decoded);
|
||||||
|
}
|
||||||
|
Some("seamless") => {
|
||||||
|
data.seamless = !field.text().await?.is_empty();
|
||||||
|
}
|
||||||
|
Some("feed") => {
|
||||||
|
data.feed = !field.text().await?.is_empty();
|
||||||
|
}
|
||||||
|
Some("bright") => {
|
||||||
|
data.bright = !field.text().await?.is_empty();
|
||||||
|
}
|
||||||
|
Some("algo") => {
|
||||||
|
data.algo = field.text().await?;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let Some(image) = image else {
|
||||||
|
return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY));
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut bytes: Vec<u8> = Vec::new();
|
||||||
|
image
|
||||||
|
.write_to(&mut Cursor::new(&mut bytes), ImageFormat::Png)
|
||||||
|
.context("failed to encode image as png")
|
||||||
|
.map_err(somehow::Error)?;
|
||||||
|
|
||||||
|
let typst = super::typst_with_lib()
|
||||||
|
.with_json("/data.json", &data)
|
||||||
|
.with_file("/image.png", bytes)
|
||||||
|
.with_main_file(include_str!("main.typ"));
|
||||||
|
|
||||||
|
let _ = server
|
||||||
|
.tx
|
||||||
|
.send(Command::draw(NewTypstDrawing::new(typst)))
|
||||||
|
.await;
|
||||||
|
|
||||||
|
Ok(Redirect::to("image").into_response())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,18 +26,32 @@
|
||||||
|
|
||||||
#import plugin("plugin.wasm") as p
|
#import plugin("plugin.wasm") as p
|
||||||
|
|
||||||
#let _length_to_bytes(len) = {
|
#let _number_to_bytes(n) = int(n).to-bytes(size: 8)
|
||||||
let len = len.pt()
|
|
||||||
let n = if len > 10000 { -1 } else { int(len) }
|
#let _bool_to_bytes(b) = _number_to_bytes(if b { 1 } else { 0 })
|
||||||
n.to-bytes(size: 8)
|
|
||||||
|
#let _str_to_bytes(s) = {
|
||||||
|
bytes(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
#let dither(path) = layout(size => {
|
#let _length_to_bytes(l) = {
|
||||||
let bytes = read(path, encoding: none)
|
let l = l.pt()
|
||||||
|
let n = if l > 10000 { -1 } else { int(l) }
|
||||||
|
_number_to_bytes(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
#let dither(
|
||||||
|
path,
|
||||||
|
bright: true,
|
||||||
|
algorithm: "floyd-steinberg",
|
||||||
|
) = layout(size => {
|
||||||
|
let data = read(path, encoding: none)
|
||||||
let dithered = p.dither(
|
let dithered = p.dither(
|
||||||
bytes,
|
data,
|
||||||
_length_to_bytes(size.width),
|
_length_to_bytes(size.width),
|
||||||
_length_to_bytes(size.height),
|
_length_to_bytes(size.height),
|
||||||
|
_bool_to_bytes(bright),
|
||||||
|
_str_to_bytes(algorithm),
|
||||||
)
|
)
|
||||||
image(dithered)
|
image(dithered)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ mod calendar;
|
||||||
mod cells;
|
mod cells;
|
||||||
mod chat_message;
|
mod chat_message;
|
||||||
mod egg;
|
mod egg;
|
||||||
mod image;
|
|
||||||
mod new_typst;
|
mod new_typst;
|
||||||
mod photo;
|
mod photo;
|
||||||
mod tictactoe;
|
mod tictactoe;
|
||||||
|
|
@ -16,9 +15,8 @@ use crate::persistent_printer::PersistentPrinter;
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
backlog::BacklogDrawing, calendar::CalendarDrawing, cells::CellsDrawing,
|
backlog::BacklogDrawing, calendar::CalendarDrawing, cells::CellsDrawing,
|
||||||
chat_message::ChatMessageDrawing, egg::EggDrawing, image::ImageDrawing,
|
chat_message::ChatMessageDrawing, egg::EggDrawing, new_typst::NewTypstDrawing,
|
||||||
new_typst::NewTypstDrawing, photo::PhotoDrawing, tictactoe::TicTacToeDrawing,
|
photo::PhotoDrawing, tictactoe::TicTacToeDrawing, typst::TypstDrawing,
|
||||||
typst::TypstDrawing,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const FEED: f32 = 96.0;
|
pub const FEED: f32 = 96.0;
|
||||||
|
|
|
||||||
|
|
@ -1,53 +0,0 @@
|
||||||
use image::RgbaImage;
|
|
||||||
use palette::{FromColor, IntoColor, LinLumaa};
|
|
||||||
use showbits_common::{
|
|
||||||
Node, Tree, WidgetExt,
|
|
||||||
color::{self, BLACK, WHITE},
|
|
||||||
widgets::{DitherAlgorithm, Image},
|
|
||||||
};
|
|
||||||
use taffy::{AlignItems, Display, FlexDirection, prelude::length, style_helpers::percent};
|
|
||||||
|
|
||||||
use crate::persistent_printer::PersistentPrinter;
|
|
||||||
|
|
||||||
use super::{Context, Drawing, FEED};
|
|
||||||
|
|
||||||
pub struct ImageDrawing {
|
|
||||||
pub image: RgbaImage,
|
|
||||||
pub bright: bool,
|
|
||||||
pub algo: DitherAlgorithm,
|
|
||||||
pub scale: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drawing for ImageDrawing {
|
|
||||||
fn draw(&self, printer: &mut PersistentPrinter, 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])
|
|
||||||
.with_dither_algorithm(self.algo)
|
|
||||||
.with_scale(self.scale)
|
|
||||||
.node()
|
|
||||||
.register(&mut tree)?;
|
|
||||||
|
|
||||||
let root = Node::empty()
|
|
||||||
.with_size_width(percent(1.0))
|
|
||||||
.with_padding_bottom(length(FEED))
|
|
||||||
.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)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
mod somehow;
|
pub mod somehow;
|
||||||
mod r#static;
|
mod r#static;
|
||||||
mod statuscode;
|
pub mod statuscode;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
Form, Router,
|
Form, Router,
|
||||||
|
|
@ -10,14 +10,13 @@ use axum::{
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
};
|
};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use showbits_common::widgets::DitherAlgorithm;
|
|
||||||
use tokio::{net::TcpListener, sync::mpsc};
|
use tokio::{net::TcpListener, sync::mpsc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
documents::{self, Image},
|
documents,
|
||||||
drawer::{
|
drawer::{
|
||||||
CalendarDrawing, CellsDrawing, ChatMessageDrawing, Command, EggDrawing, ImageDrawing,
|
CalendarDrawing, CellsDrawing, ChatMessageDrawing, Command, EggDrawing, PhotoDrawing,
|
||||||
NewTypstDrawing, PhotoDrawing, TicTacToeDrawing, TypstDrawing,
|
TicTacToeDrawing, TypstDrawing,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -34,7 +33,10 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
|
||||||
.route("/cells", post(post_cells))
|
.route("/cells", post(post_cells))
|
||||||
.route("/chat_message", post(post_chat_message))
|
.route("/chat_message", post(post_chat_message))
|
||||||
.route("/egg", post(post_egg).fallback(get_static_file))
|
.route("/egg", post(post_egg).fallback(get_static_file))
|
||||||
.route("/image", post(post_image).fallback(get_static_file))
|
.route(
|
||||||
|
"/image",
|
||||||
|
post(documents::image::post).fallback(get_static_file),
|
||||||
|
)
|
||||||
.route("/photo", post(post_photo).fallback(get_static_file))
|
.route("/photo", post(post_photo).fallback(get_static_file))
|
||||||
.route(
|
.route(
|
||||||
"/text",
|
"/text",
|
||||||
|
|
@ -42,7 +44,6 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
|
||||||
)
|
)
|
||||||
.route("/tictactoe", post(post_tictactoe))
|
.route("/tictactoe", post(post_tictactoe))
|
||||||
.route("/typst", post(post_typst).fallback(get_static_file))
|
.route("/typst", post(post_typst).fallback(get_static_file))
|
||||||
.route("/test2", post(post_test2).fallback(get_static_file))
|
|
||||||
.fallback(get(get_static_file))
|
.fallback(get(get_static_file))
|
||||||
.layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB
|
.layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB
|
||||||
.with_state(Server { tx });
|
.with_state(Server { tx });
|
||||||
|
|
@ -115,52 +116,6 @@ async fn post_egg(server: State<Server>) -> impl IntoResponse {
|
||||||
Redirect::to("egg")
|
Redirect::to("egg")
|
||||||
}
|
}
|
||||||
|
|
||||||
// /image
|
|
||||||
|
|
||||||
async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
|
|
||||||
let mut image = None;
|
|
||||||
let mut bright = false;
|
|
||||||
let mut algo = DitherAlgorithm::FloydSteinberg;
|
|
||||||
let mut scale = 1_u32;
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
Some("bright") => {
|
|
||||||
bright = true;
|
|
||||||
}
|
|
||||||
Some("algo") => match &field.text().await? as &str {
|
|
||||||
"floyd-steinberg" => algo = DitherAlgorithm::FloydSteinberg,
|
|
||||||
"stucki" => algo = DitherAlgorithm::Stucki,
|
|
||||||
_ => {}
|
|
||||||
},
|
|
||||||
Some("scale") => {
|
|
||||||
scale = field.text().await?.parse::<u32>()?;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let Some(image) = image else {
|
|
||||||
return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY));
|
|
||||||
};
|
|
||||||
|
|
||||||
let _ = server
|
|
||||||
.tx
|
|
||||||
.send(Command::draw(ImageDrawing {
|
|
||||||
image,
|
|
||||||
bright,
|
|
||||||
algo,
|
|
||||||
scale,
|
|
||||||
}))
|
|
||||||
.await;
|
|
||||||
Ok(Redirect::to("image").into_response())
|
|
||||||
}
|
|
||||||
|
|
||||||
// /photo
|
// /photo
|
||||||
|
|
||||||
async fn post_photo(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
|
async fn post_photo(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
|
||||||
|
|
@ -215,33 +170,3 @@ async fn post_typst(server: State<Server>, request: Form<PostTypstForm>) {
|
||||||
.send(Command::draw(TypstDrawing(request.0.source)))
|
.send(Command::draw(TypstDrawing(request.0.source)))
|
||||||
.await;
|
.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())
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -4,18 +4,42 @@ use image::{
|
||||||
ImageFormat,
|
ImageFormat,
|
||||||
imageops::{self, FilterType},
|
imageops::{self, FilterType},
|
||||||
};
|
};
|
||||||
use mark::dither::{AlgoFloydSteinberg, Algorithm, DiffEuclid, Palette};
|
use mark::dither::{AlgoFloydSteinberg, AlgoStucki, Algorithm, DiffEuclid, Palette};
|
||||||
use palette::LinSrgb;
|
use palette::{FromColor, IntoColor, LinLumaa, LinSrgb, Srgba};
|
||||||
use wasm_minimal_protocol::{initiate_protocol, wasm_func};
|
use wasm_minimal_protocol::{initiate_protocol, wasm_func};
|
||||||
|
|
||||||
initiate_protocol!();
|
initiate_protocol!();
|
||||||
|
|
||||||
|
// Palette <-> image color conversions
|
||||||
|
|
||||||
|
fn image_to_palette(color: image::Rgba<u8>) -> Srgba {
|
||||||
|
let [r, g, b, a] = color.0;
|
||||||
|
Srgba::new(r, g, b, a).into_format()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn palette_to_image(color: Srgba) -> image::Rgba<u8> {
|
||||||
|
let color = color.into_format::<u8, u8>();
|
||||||
|
image::Rgba([color.red, color.green, color.blue, color.alpha])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Typst type conversions
|
||||||
|
|
||||||
fn i64_from_bytes(bytes: &[u8]) -> Result<i64, String> {
|
fn i64_from_bytes(bytes: &[u8]) -> Result<i64, String> {
|
||||||
let bytes: [u8; 8] = bytes.try_into().map_err(|it| format!("{it}"))?;
|
let bytes: [u8; 8] = bytes.try_into().map_err(|it| format!("{it}"))?;
|
||||||
Ok(i64::from_le_bytes(bytes))
|
Ok(i64::from_le_bytes(bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size_from_i64(size: i64) -> Result<Option<u32>, String> {
|
fn bool_from_bytes(bytes: &[u8]) -> Result<bool, String> {
|
||||||
|
Ok(i64_from_bytes(bytes)? != 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn str_from_bytes(bytes: &[u8]) -> Result<&str, String> {
|
||||||
|
std::str::from_utf8(bytes).map_err(|it| format!("{it}"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn size_from_bytes(bytes: &[u8]) -> Result<Option<u32>, String> {
|
||||||
|
let size = i64_from_bytes(bytes)?;
|
||||||
|
|
||||||
if size < 0 {
|
if size < 0 {
|
||||||
return Ok(None); // Unlimited width
|
return Ok(None); // Unlimited width
|
||||||
}
|
}
|
||||||
|
|
@ -24,10 +48,20 @@ fn size_from_i64(size: i64) -> Result<Option<u32>, String> {
|
||||||
Ok(Some(size))
|
Ok(Some(size))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Typst methods
|
||||||
|
|
||||||
#[wasm_func]
|
#[wasm_func]
|
||||||
pub fn dither(image: &[u8], max_width: &[u8], max_height: &[u8]) -> Result<Vec<u8>, String> {
|
pub fn dither(
|
||||||
let max_width = size_from_i64(i64_from_bytes(max_width)?)?;
|
image: &[u8],
|
||||||
let max_height = size_from_i64(i64_from_bytes(max_height)?)?;
|
max_width: &[u8],
|
||||||
|
max_height: &[u8],
|
||||||
|
bright: &[u8],
|
||||||
|
algorithm: &[u8],
|
||||||
|
) -> Result<Vec<u8>, String> {
|
||||||
|
let max_width = size_from_bytes(max_width)?;
|
||||||
|
let max_height = size_from_bytes(max_height)?;
|
||||||
|
let bright = bool_from_bytes(bright)?;
|
||||||
|
let algorithm = str_from_bytes(algorithm)?;
|
||||||
|
|
||||||
let mut image = image::load_from_memory(image)
|
let mut image = image::load_from_memory(image)
|
||||||
.map_err(|it| format!("Failed to read image: {it:?}"))?
|
.map_err(|it| format!("Failed to read image: {it:?}"))?
|
||||||
|
|
@ -52,12 +86,26 @@ pub fn dither(image: &[u8], max_width: &[u8], max_height: &[u8]) -> Result<Vec<u
|
||||||
image = imageops::resize(&image, target_width, target_height, FilterType::CatmullRom);
|
image = imageops::resize(&image, target_width, target_height, FilterType::CatmullRom);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if bright {
|
||||||
|
for pixel in image.pixels_mut() {
|
||||||
|
let mut color = LinLumaa::from_color(image_to_palette(*pixel));
|
||||||
|
color.luma = 1.0 - 0.4 * (1.0 - color.luma);
|
||||||
|
*pixel = palette_to_image(color.into_color());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let palette = Palette::new(vec![
|
let palette = Palette::new(vec![
|
||||||
LinSrgb::new(0.0, 0.0, 0.0),
|
LinSrgb::new(0.0, 0.0, 0.0),
|
||||||
LinSrgb::new(1.0, 1.0, 1.0),
|
LinSrgb::new(1.0, 1.0, 1.0),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let dithered = <AlgoFloydSteinberg as Algorithm<LinSrgb, DiffEuclid>>::run(image, &palette);
|
let dithered = match algorithm {
|
||||||
|
"floyd-steinberg" => {
|
||||||
|
<AlgoFloydSteinberg as Algorithm<LinSrgb, DiffEuclid>>::run(image, &palette)
|
||||||
|
}
|
||||||
|
"stucki" => <AlgoStucki as Algorithm<LinSrgb, DiffEuclid>>::run(image, &palette),
|
||||||
|
it => Err(format!("Unknown algorithm: {it}"))?,
|
||||||
|
};
|
||||||
|
|
||||||
let mut bytes: Vec<u8> = Vec::new();
|
let mut bytes: Vec<u8> = Vec::new();
|
||||||
dithered
|
dithered
|
||||||
|
|
@ -66,16 +114,3 @@ pub fn dither(image: &[u8], max_width: &[u8], max_height: &[u8]) -> Result<Vec<u
|
||||||
|
|
||||||
Ok(bytes)
|
Ok(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_func]
|
|
||||||
pub fn debug(value: &[u8]) -> Result<Vec<u8>, String> {
|
|
||||||
// let value: [u8; 4] = value
|
|
||||||
// .try_into()
|
|
||||||
// .map_err(|it| format!("incorrect number of bytes: {it}"))?;
|
|
||||||
|
|
||||||
// let be = u32::from_be_bytes(value);
|
|
||||||
// let le = u32::from_le_bytes(value);
|
|
||||||
|
|
||||||
// Ok(format!("be: {be}, le: {le}").into_bytes())
|
|
||||||
Ok(format!("{value:?}").into_bytes())
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue