Make bright printing optional

This commit is contained in:
Joscha 2024-03-15 23:19:17 +01:00
parent 9d4ecaaf72
commit 4f3d66f6a0
3 changed files with 26 additions and 15 deletions

View file

@ -1,8 +1,9 @@
mod calendar;
use image::{Luma, Pixel, RgbaImage};
use palette::{FromColor, IntoColor, LinLumaa};
use showbits_common::{
color::{BLACK, WHITE},
color::{self, BLACK, WHITE},
widgets::{Block, FontStuff, HasFontStuff, Image, Text},
Node, Tree, WidgetExt,
};
@ -19,7 +20,7 @@ pub enum Command {
Rip,
Test,
Text(String),
Image(RgbaImage),
Image { image: RgbaImage, bright: bool },
Photo { image: RgbaImage, title: String },
ChatMessage { username: String, content: String },
Calendar { year: i32, month: u8 },
@ -70,7 +71,7 @@ impl Drawer {
Command::Rip => self.printer.rip()?,
Command::Test => self.on_test()?,
Command::Text(text) => self.on_text(text)?,
Command::Image(image) => self.on_image(image)?,
Command::Image { image, bright } => self.on_image(image, bright)?,
Command::Photo { image, title } => self.on_photo(image, title)?,
Command::ChatMessage { username, content } => {
self.on_chat_message(username, content)?
@ -131,15 +132,15 @@ impl Drawer {
Ok(())
}
fn on_image(&mut self, mut image: RgbaImage) -> anyhow::Result<()> {
fn on_image(&mut self, mut image: RgbaImage, bright: bool) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new(WHITE);
if bright {
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 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 image = Image::new(image)

View file

@ -63,20 +63,27 @@ async fn post_text(server: State<Server>, request: Form<PostTextForm>) {
async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
let mut image = None;
let mut bright = false;
while let Some(field) = multipart.next_field().await? {
if let Some("image") = field.name() {
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;
}
_ => {}
}
}
let Some(image) = image else {
return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY));
};
let _ = server.tx.send(Command::Image(image)).await;
let _ = server.tx.send(Command::Image { image, bright }).await;
Ok(Redirect::to("image").into_response())
}

View file

@ -10,6 +10,9 @@
<form method="post" enctype="multipart/form-data">
<ol>
<li><input type="file" name="image" /></li>
<li>
<label><input type="checkbox" name="bright" checked /> Bright</label>
</li>
<li><button>Print!</button></li>
</ol>
</form>