make einkimage non-eink specific
Replace it with DitheredImage, allow different resolutions.
This commit is contained in:
parent
599fb09503
commit
cee237cb32
20
src/api.rs
20
src/api.rs
|
@ -1,5 +1,5 @@
|
||||||
use crate::display::EInkPanel;
|
use crate::display::EInkPanel;
|
||||||
use crate::imageproc::{DitherMethod, DitherPalette, EInkImage};
|
use crate::imageproc::{DitherMethod, DitherPalette, DitheredImage};
|
||||||
use axum::async_trait;
|
use axum::async_trait;
|
||||||
use axum::extract::{FromRequest, Multipart, State};
|
use axum::extract::{FromRequest, Multipart, State};
|
||||||
use axum::http::{header, StatusCode};
|
use axum::http::{header, StatusCode};
|
||||||
|
@ -13,7 +13,7 @@ use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::sync::mpsc::{self, Receiver, Sender};
|
use tokio::sync::mpsc::{self, Receiver, Sender};
|
||||||
use tokio::task::JoinHandle;
|
use tokio::task::JoinHandle;
|
||||||
use tracing::{debug, error, info, instrument};
|
use tracing::{error, info, instrument};
|
||||||
|
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum ApiError {
|
pub enum ApiError {
|
||||||
|
@ -66,7 +66,7 @@ where
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct DisplaySetCommand {
|
pub struct DisplaySetCommand {
|
||||||
img: Box<EInkImage>,
|
img: Box<DitheredImage>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
|
@ -134,15 +134,16 @@ where
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(i) = img {
|
img.map_or_else(
|
||||||
|
|| Err(ApiError::MissingImage.into()),
|
||||||
|
|i| {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
image: i,
|
image: i,
|
||||||
dither_method: dither_method.unwrap_or(DitherMethod::NearestNeighbor),
|
dither_method: dither_method.unwrap_or(DitherMethod::NearestNeighbor),
|
||||||
palette: palette.unwrap_or(DitherPalette::Default),
|
palette: palette.unwrap_or(DitherPalette::Default),
|
||||||
})
|
})
|
||||||
} else {
|
},
|
||||||
Err(ApiError::MissingImage.into())
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,7 +152,8 @@ async fn set_image(
|
||||||
State(ctx): State<Context>,
|
State(ctx): State<Context>,
|
||||||
img_req: ImageRequest,
|
img_req: ImageRequest,
|
||||||
) -> Result<impl IntoResponse, AppError> {
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
let mut buf = EInkImage::new(img_req.palette.value().to_vec());
|
// 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 dither = img_req.dither_method.get_ditherer();
|
let mut dither = img_req.dither_method.get_ditherer();
|
||||||
dither.dither(&img_req.image, &mut buf);
|
dither.dither(&img_req.image, &mut buf);
|
||||||
|
@ -166,7 +168,7 @@ async fn set_image(
|
||||||
/// generates a dithered image based on the given image and the dithering parameters.
|
/// 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.
|
/// Can be used to see how the dithering and palette choices affect the result.
|
||||||
async fn preview_image(img_req: ImageRequest) -> Result<impl IntoResponse, AppError> {
|
async fn preview_image(img_req: ImageRequest) -> Result<impl IntoResponse, AppError> {
|
||||||
let mut buf = EInkImage::new(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();
|
let mut dither = img_req.dither_method.get_ditherer();
|
||||||
dither.dither(&img_req.image, &mut buf);
|
dither.dither(&img_req.image, &mut buf);
|
||||||
|
|
|
@ -8,10 +8,10 @@ use tracing::{debug, warn};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use linux_embedded_hal::gpio_cdev::{Chip, LineRequestFlags};
|
use linux_embedded_hal::gpio_cdev::{Chip, LineRequestFlags};
|
||||||
|
|
||||||
use crate::imageproc::EInkImage;
|
use crate::imageproc::DitheredImage;
|
||||||
|
|
||||||
pub trait EInkPanel {
|
pub trait EInkPanel {
|
||||||
fn display(&mut self, buf: &EInkImage) -> Result<()>;
|
fn display(&mut self, buf: &DitheredImage) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Wrapper {
|
pub struct Wrapper {
|
||||||
|
@ -66,7 +66,7 @@ impl Wrapper {
|
||||||
|
|
||||||
impl EInkPanel for Wrapper {
|
impl EInkPanel for Wrapper {
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
fn display(&mut self, img: &EInkImage) -> Result<()> {
|
fn display(&mut self, img: &DitheredImage) -> Result<()> {
|
||||||
let buf = img.into_display_buffer();
|
let buf = img.into_display_buffer();
|
||||||
self.panel
|
self.panel
|
||||||
.update_and_display_frame(&mut self.spi, &buf, &mut self.delay)?;
|
.update_and_display_frame(&mut self.spi, &buf, &mut self.delay)?;
|
||||||
|
@ -77,13 +77,11 @@ impl EInkPanel for Wrapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// A Fake EInk display for testing purposes.
|
/// A Fake EInk display for testing purposes.
|
||||||
/// Saves the output as `display.bmp`
|
/// Saves the output as `display.bmp`
|
||||||
pub struct FakeEInk();
|
pub struct FakeEInk();
|
||||||
impl EInkPanel for FakeEInk {
|
impl EInkPanel for FakeEInk {
|
||||||
fn display(&mut self, img: &EInkImage) -> Result<()> {
|
fn display(&mut self, img: &DitheredImage) -> Result<()> {
|
||||||
|
|
||||||
warn!("Fake display was called: saving to display.bmp");
|
warn!("Fake display was called: saving to display.bmp");
|
||||||
img.into_rgbimage().save("display.bmp");
|
img.into_rgbimage().save("display.bmp");
|
||||||
|
|
||||||
|
|
|
@ -75,12 +75,12 @@ pub enum ProcessingError {
|
||||||
|
|
||||||
/// Buffer to be sent to the ``EInk`` display.
|
/// Buffer to be sent to the ``EInk`` display.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct EInkImage {
|
pub struct DitheredImage {
|
||||||
buf: ImageBuffer<Luma<u8>, Vec<u8>>,
|
buf: ImageBuffer<Luma<u8>, Vec<u8>>,
|
||||||
palette: Vec<Srgb>,
|
palette: Vec<Srgb>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EInkImage {
|
impl DitheredImage {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn into_display_buffer(&self) -> Vec<u8> {
|
pub fn into_display_buffer(&self) -> Vec<u8> {
|
||||||
let mut buf = Vec::with_capacity(self.buf.len() / 2);
|
let mut buf = Vec::with_capacity(self.buf.len() / 2);
|
||||||
|
@ -90,7 +90,7 @@ impl EInkImage {
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert the EInk-palette image into an RGB image to be viewed on a regular screen.
|
/// Convert the index-based image back into an RGB image using the color palette.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn into_rgbimage(&self) -> RgbImage {
|
pub fn into_rgbimage(&self) -> RgbImage {
|
||||||
RgbImage::from_fn(self.buf.width(), self.buf.height(), |x, y| {
|
RgbImage::from_fn(self.buf.width(), self.buf.height(), |x, y| {
|
||||||
|
@ -104,25 +104,25 @@ impl EInkImage {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Constructs a new EInk Image based on the given color palette for
|
/// Constructs a new Dithered Image based on the given dimensions and color palette for
|
||||||
/// color indexing.
|
/// color indexing.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new(palette: Vec<Srgb>) -> Self {
|
pub fn new(width: u32, height: u32, palette: Vec<Srgb>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
buf: GrayImage::new(800, 480),
|
buf: GrayImage::new(width, height),
|
||||||
palette,
|
palette,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for EInkImage {
|
impl Default for DitheredImage {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new(DISPLAY_PALETTE.to_vec())
|
Self::new(800, 480, DISPLAY_PALETTE.to_vec())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Ditherer {
|
pub trait Ditherer {
|
||||||
fn dither(&mut self, img: &RgbImage, output: &mut EInkImage);
|
fn dither(&mut self, img: &RgbImage, output: &mut DitheredImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Find the closest approximate palette color to the given sRGB value.
|
/// Find the closest approximate palette color to the given sRGB value.
|
||||||
|
@ -143,7 +143,7 @@ fn nearest_neighbor(input_color: Lab, palette: &[Srgb]) -> (u8, Lab) {
|
||||||
pub struct NNDither();
|
pub struct NNDither();
|
||||||
|
|
||||||
impl Ditherer for NNDither {
|
impl Ditherer for NNDither {
|
||||||
fn dither(&mut self, img: &RgbImage, output: &mut EInkImage) {
|
fn dither(&mut self, img: &RgbImage, output: &mut DitheredImage) {
|
||||||
assert!(img.width() == 800);
|
assert!(img.width() == 800);
|
||||||
assert!(img.height() == 480);
|
assert!(img.height() == 480);
|
||||||
|
|
||||||
|
@ -253,7 +253,7 @@ impl<'a> ErrorDiffusionDither<'a> {
|
||||||
|
|
||||||
impl<'a> Ditherer for ErrorDiffusionDither<'a> {
|
impl<'a> Ditherer for ErrorDiffusionDither<'a> {
|
||||||
#[instrument]
|
#[instrument]
|
||||||
fn dither(&mut self, img: &RgbImage, output: &mut EInkImage) {
|
fn dither(&mut self, img: &RgbImage, output: &mut DitheredImage) {
|
||||||
// create a copy of the image in Lab space, mutable.
|
// create a copy of the image in Lab space, mutable.
|
||||||
// first, a view into the rgb components
|
// first, a view into the rgb components
|
||||||
let srgb = <&[Srgb<u8>]>::from_components(&**img);
|
let srgb = <&[Srgb<u8>]>::from_components(&**img);
|
||||||
|
|
|
@ -4,7 +4,7 @@ pub mod errors;
|
||||||
pub mod imageproc;
|
pub mod imageproc;
|
||||||
|
|
||||||
use crate::display::{EInkPanel, FakeEInk, Wrapper};
|
use crate::display::{EInkPanel, FakeEInk, Wrapper};
|
||||||
use crate::imageproc::{DitherMethod, EInkImage};
|
use crate::imageproc::{DitherMethod, DitheredImage};
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use image::RgbImage;
|
use image::RgbImage;
|
||||||
use tracing::{error, info};
|
use tracing::{error, info};
|
||||||
|
@ -38,7 +38,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
error!("HI");
|
error!("HI");
|
||||||
let mut display = FakeEInk {};
|
let mut display = FakeEInk {};
|
||||||
|
|
||||||
let mut eink_buf = EInkImage::default();
|
let mut eink_buf = DitheredImage::default();
|
||||||
let mut dither = DitherMethod::Atkinson.get_ditherer();
|
let mut dither = DitherMethod::Atkinson.get_ditherer();
|
||||||
|
|
||||||
dither.dither(&img, &mut eink_buf);
|
dither.dither(&img, &mut eink_buf);
|
||||||
|
@ -51,7 +51,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches!(cli.command, Command::Serve) {
|
if matches!(cli.command, Command::Serve) {
|
||||||
let display = Wrapper::new()?;
|
let display = FakeEInk {};
|
||||||
|
|
||||||
let ctx = api::Context::new(Box::new(display));
|
let ctx = api::Context::new(Box::new(display));
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue