From a033a55098e26c81bbedd91cdb3f09b415d3618c Mon Sep 17 00:00:00 2001 From: saji Date: Tue, 30 Jul 2024 10:45:29 -0500 Subject: [PATCH] rename imageproc to dither.rs --- src/api.rs | 14 +++++++++++--- src/display.rs | 4 ++-- src/{imageproc.rs => dither.rs} | 4 ++++ src/main.rs | 4 ++-- 4 files changed, 19 insertions(+), 7 deletions(-) rename src/{imageproc.rs => dither.rs} (98%) diff --git a/src/api.rs b/src/api.rs index 6a58820..42ada33 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,5 +1,5 @@ use crate::display::EInkPanel; -use crate::imageproc::{DitherMethod, DitherPalette, DitheredImage}; +use crate::dither::{DitherMethod, DitherPalette, DitheredImage}; use axum::async_trait; use axum::extract::{FromRequest, Multipart, State}; use axum::http::{header, StatusCode}; @@ -153,7 +153,11 @@ async fn set_image( img_req: ImageRequest, ) -> Result { // FIXME: resize image to 800x480 to match the eink panel. - let mut buf = DitheredImage::new(img_req.image.width(), img_req.image.height(), img_req.palette.value().to_vec()); + let mut buf = DitheredImage::new( + img_req.image.width(), + img_req.image.height(), + img_req.palette.value().to_vec(), + ); { let mut dither = img_req.dither_method.get_ditherer(); dither.dither(&img_req.image, &mut buf); @@ -168,7 +172,11 @@ async fn set_image( /// generates a dithered image based on the given image and the dithering parameters. /// Can be used to see how the dithering and palette choices affect the result. async fn preview_image(img_req: ImageRequest) -> Result { - let mut buf = DitheredImage::new(img_req.image.width(), img_req.image.height(), img_req.palette.value().to_vec()); + let mut buf = DitheredImage::new( + img_req.image.width(), + img_req.image.height(), + img_req.palette.value().to_vec(), + ); { let mut dither = img_req.dither_method.get_ditherer(); dither.dither(&img_req.image, &mut buf); diff --git a/src/display.rs b/src/display.rs index 08cc5b8..79121f6 100644 --- a/src/display.rs +++ b/src/display.rs @@ -8,7 +8,7 @@ use tracing::{debug, warn}; use anyhow::Result; use linux_embedded_hal::gpio_cdev::{Chip, LineRequestFlags}; -use crate::imageproc::DitheredImage; +use crate::dither::DitheredImage; pub trait EInkPanel { fn display(&mut self, buf: &DitheredImage) -> Result<()>; @@ -83,7 +83,7 @@ pub struct FakeEInk(); impl EInkPanel for FakeEInk { fn display(&mut self, img: &DitheredImage) -> Result<()> { warn!("Fake display was called: saving to display.bmp"); - img.into_rgbimage().save("display.bmp"); + img.into_rgbimage().save("display.bmp")?; Ok(()) } diff --git a/src/imageproc.rs b/src/dither.rs similarity index 98% rename from src/imageproc.rs rename to src/dither.rs index 40da8fc..ee6cbbe 100644 --- a/src/imageproc.rs +++ b/src/dither.rs @@ -91,6 +91,10 @@ impl DitheredImage { } /// Convert the index-based image back into an RGB image using the color palette. + /// + /// # Panics + /// May panic if the given palette is somehow not the one used to actually dither, and the + /// image has color indexes that are out of bounds of the palette. #[must_use] pub fn into_rgbimage(&self) -> RgbImage { RgbImage::from_fn(self.buf.width(), self.buf.height(), |x, y| { diff --git a/src/main.rs b/src/main.rs index 4156b70..8d8df80 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,10 @@ pub mod api; pub mod display; pub mod errors; -pub mod imageproc; +pub mod dither; use crate::display::{EInkPanel, FakeEInk, Wrapper}; -use crate::imageproc::{DitherMethod, DitheredImage}; +use crate::dither::{DitherMethod, DitheredImage}; use clap::{Parser, Subcommand}; use image::RgbImage; use tracing::{error, info};