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

View file

@ -10,6 +10,7 @@ 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::drawer::{ 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> { async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
let mut image = None; let mut image = None;
let mut bright = false; let mut bright = false;
let mut algo = DitherAlgorithm::FloydSteinberg;
while let Some(field) = multipart.next_field().await? { while let Some(field) = multipart.next_field().await? {
match field.name() { match field.name() {
@ -123,6 +125,11 @@ async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow:
Some("bright") => { Some("bright") => {
bright = true; 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 let _ = server
.tx .tx
.send(Command::draw(ImageDrawing { image, bright })) .send(Command::draw(ImageDrawing {
image,
bright,
algo,
}))
.await; .await;
Ok(Redirect::to("image").into_response()) Ok(Redirect::to("image").into_response())
} }

View file

@ -13,6 +13,13 @@
<li> <li>
<label><input type="checkbox" name="bright" checked /> Bright</label> <label><input type="checkbox" name="bright" checked /> Bright</label>
</li> </li>
<li>
Dithering algorithm:
<select name="algo">
<option value="floyd-steinberg">Floyd-Steinberg</option>
<option value="stucki">Stucki</option>
</select>
</li>
<li><button>Print!</button></li> <li><button>Print!</button></li>
</ol> </ol>
</form> </form>