Add dither algorithm option

This commit is contained in:
Joscha 2025-02-22 21:39:11 +01:00
parent f833a5fea5
commit f8ff567a00
3 changed files with 22 additions and 2 deletions

View file

@ -3,7 +3,7 @@ use palette::{FromColor, IntoColor, LinLumaa};
use showbits_common::{
Node, Tree, WidgetExt,
color::{self, BLACK, WHITE},
widgets::Image,
widgets::{DitherAlgorithm, Image},
};
use taffy::{AlignItems, Display, FlexDirection, style_helpers::percent};
@ -14,6 +14,7 @@ use super::{Context, Drawing};
pub struct ImageDrawing {
pub image: RgbaImage,
pub bright: bool,
pub algo: DitherAlgorithm,
}
impl Drawing for ImageDrawing {
@ -31,6 +32,7 @@ impl Drawing for ImageDrawing {
let image = Image::new(image)
.with_dither_palette(&[BLACK, WHITE])
.with_dither_algorithm(self.algo)
.node()
.register(&mut tree)?;

View file

@ -10,6 +10,7 @@ use axum::{
routing::{get, post},
};
use serde::Deserialize;
use showbits_common::widgets::DitherAlgorithm;
use tokio::{net::TcpListener, sync::mpsc};
use crate::drawer::{
@ -112,6 +113,7 @@ async fn post_egg(server: State<Server>) -> impl IntoResponse {
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;
while let Some(field) = multipart.next_field().await? {
match field.name() {
@ -123,6 +125,11 @@ async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow:
Some("bright") => {
bright = true;
}
Some("algo") => match &field.text().await? as &str {
"floyd-steinberg" => algo = DitherAlgorithm::FloydSteinberg,
"stucki" => algo = DitherAlgorithm::Stucki,
_ => {}
},
_ => {}
}
}
@ -133,7 +140,11 @@ async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow:
let _ = server
.tx
.send(Command::draw(ImageDrawing { image, bright }))
.send(Command::draw(ImageDrawing {
image,
bright,
algo,
}))
.await;
Ok(Redirect::to("image").into_response())
}