Compare commits

..

2 commits

Author SHA1 Message Date
saji 608ebe9ec5 add serde/strum, modify EIinkPanel API 2024-07-29 12:51:51 -05:00
saji fa2f5fc1a4 wip: add new EInk image impl 2024-07-28 09:28:06 -05:00
6 changed files with 136 additions and 126 deletions

24
Cargo.lock generated
View file

@ -1309,6 +1309,8 @@ dependencies = [
"linux-embedded-hal", "linux-embedded-hal",
"minijinja", "minijinja",
"palette", "palette",
"serde",
"strum",
"thiserror", "thiserror",
"tokio", "tokio",
"tower-http", "tower-http",
@ -1756,6 +1758,28 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "strum"
version = "0.26.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.26.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be"
dependencies = [
"heck 0.5.0",
"proc-macro2",
"quote",
"rustversion",
"syn",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.72" version = "2.0.72"

View file

@ -15,6 +15,8 @@ image = "0.25.1"
linux-embedded-hal = { version = "0.4.0"} linux-embedded-hal = { version = "0.4.0"}
minijinja = "2.1.0" minijinja = "2.1.0"
palette = "0.7.6" palette = "0.7.6"
serde = { version = "1.0.204", features = ["derive"] }
strum = { version = "0.26.3", features = ["derive"] }
thiserror = "1.0.63" thiserror = "1.0.63"
tokio = { version = "1.38.1", features = ["full"] } tokio = { version = "1.38.1", features = ["full"] }
tower-http = { version = "0.5.2", features = ["trace"] } tower-http = { version = "0.5.2", features = ["trace"] }

View file

@ -7,6 +7,7 @@ use image::ImageReader;
use std::io::Cursor; use std::io::Cursor;
use std::time::Duration; use std::time::Duration;
use tracing::{debug, error, info, instrument}; use tracing::{debug, error, info, instrument};
use serde::{Serialize, Deserialize};
use crate::display::EInkPanel; use crate::display::EInkPanel;
use std::sync::Arc; use std::sync::Arc;
@ -36,25 +37,6 @@ impl Context {
} }
} }
#[derive(Debug)]
pub struct DisplaySetCommand {
img: Box<EInkImage>,
}
#[instrument(skip_all)]
pub async fn display_task(
mut rx: Receiver<DisplaySetCommand>,
mut display: Box<dyn EInkPanel + Send>,
) {
while let Some(cmd) = rx.recv().await {
info!("Got a display set command");
let raw_buf = cmd.img.into_display_buffer();
if let Err(e) = display.display(&raw_buf) {
error!("Error displaying command {e}");
}
info!("Done setting display");
}
}
// Make our own error that wraps `anyhow::Error`. // Make our own error that wraps `anyhow::Error`.
struct AppError(anyhow::Error); struct AppError(anyhow::Error);
@ -80,11 +62,38 @@ where
} }
} }
#[derive(Debug)]
pub struct DisplaySetCommand {
img: Box<EInkImage>,
}
#[instrument(skip_all)]
pub async fn display_task(
mut rx: Receiver<DisplaySetCommand>,
mut display: Box<dyn EInkPanel + Send>,
) {
while let Some(cmd) = rx.recv().await {
info!("Got a display set command");
if let Err(e) = display.display(&cmd.img) {
error!("Error displaying command {e}");
}
info!("Done setting display");
}
}
/// API routes for axum /// API routes for axum
/// Start with the basics: Send an image, crop it, dither, and upload. /// Start with the basics: Send an image, crop it, dither, and upload.
/// we defer the upload to a separate task. /// we defer the upload to a separate task.
pub fn router() -> Router<Context> { pub fn router() -> Router<Context> {
Router::new().route("/setimage", post(set_image)) Router::new()
.route("/setimage", post(set_image))
.route("/process_image", post(process_image))
}
#[derive(Serialize, Deserialize)]
pub struct ImageRequest {
} }
#[instrument(skip(ctx))] #[instrument(skip(ctx))]
@ -103,8 +112,8 @@ async fn set_image(
debug!("Guessed format: {:?}", reader.format()); debug!("Guessed format: {:?}", reader.format());
let image = reader.decode()?; let image = reader.decode()?;
let mut buf = EInkImage::new(800, 480); let mut buf = EInkImage::default();
let dither = ErrorDiffusionDither::new(DiffusionMatrix::Atkinson); let mut dither = ErrorDiffusionDither::new(DiffusionMatrix::Atkinson);
dither.dither(&image.into(), &mut buf); dither.dither(&image.into(), &mut buf);
let cmd = DisplaySetCommand { img: Box::new(buf) }; let cmd = DisplaySetCommand { img: Box::new(buf) };
ctx.display_channel ctx.display_channel
@ -114,3 +123,7 @@ async fn set_image(
} }
Ok(()) Ok(())
} }
async fn process_image(mut parts: Multipart) -> Result<impl IntoResponse, AppError> {
Ok(StatusCode::OK)
}

View file

@ -8,8 +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;
pub trait EInkPanel { pub trait EInkPanel {
fn display(&mut self, buf: &[u8]) -> Result<()>; fn display(&mut self, buf: &EInkImage) -> Result<()>;
} }
pub struct Wrapper { pub struct Wrapper {
@ -64,9 +66,10 @@ impl Wrapper {
impl EInkPanel for Wrapper { impl EInkPanel for Wrapper {
#[instrument(skip_all)] #[instrument(skip_all)]
fn display(&mut self, buf: &[u8]) -> Result<()> { fn display(&mut self, img: &EInkImage) -> Result<()> {
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)?;
debug!("Finished updating frame"); debug!("Finished updating frame");
self.panel.sleep(&mut self.spi, &mut self.delay)?; self.panel.sleep(&mut self.spi, &mut self.delay)?;
debug!("Display entered sleep mode"); debug!("Display entered sleep mode");
@ -74,11 +77,16 @@ impl EInkPanel for Wrapper {
} }
} }
/// A Fake EInk display for testing purposes.
/// Saves the output as `display.bmp`
pub struct FakeEInk(); pub struct FakeEInk();
impl EInkPanel for FakeEInk { impl EInkPanel for FakeEInk {
fn display(&mut self, _buf: &[u8]) -> Result<()> { fn display(&mut self, img: &EInkImage) -> Result<()> {
// Do nothing.
warn!("Fake display was called"); warn!("Fake display was called: saving to display.bmp");
img.into_rgbimage().save("display.bmp");
Ok(()) Ok(())
} }
} }

View file

@ -1,4 +1,5 @@
use image::RgbImage; use image::{GrayImage, ImageBuffer, Luma, RgbImage};
use palette::FromColor;
use tracing::instrument; use tracing::instrument;
use image::Rgb as imgRgb; use image::Rgb as imgRgb;
@ -18,128 +19,88 @@ const DISPLAY_PALETTE: [Srgb; 7] = [
Srgb::new(0.757, 0.443, 0.165), // Orange Srgb::new(0.757, 0.443, 0.165), // Orange
]; ];
// TODO: support different color palettes. pub enum ProcessingError {
DitherError,
#[derive(Debug, Clone, Copy, PartialEq, Eq)] PaletteIndexError(usize),
pub enum DisplayColor {
Black,
White,
Green,
Blue,
Red,
Yellow,
Orange,
} }
impl From<DisplayColor> for Srgb {
fn from(value: DisplayColor) -> Self {
DISPLAY_PALETTE[value as usize]
}
}
impl DisplayColor {
fn from_u8(value: u8) -> Self {
match value {
0 => Self::Black,
1 => Self::White,
2 => Self::Green,
3 => Self::Blue,
4 => Self::Red,
5 => Self::Yellow,
6 => Self::Orange,
_ => panic!("unexpected DisplayColor {value}"),
}
}
fn into_byte(color1: Self, color2: Self) -> u8 {
let upper: u8 = color1.into();
let lower: u8 = color2.into();
upper << 4 | lower
}
}
impl From<DisplayColor> for u8 {
fn from(value: DisplayColor) -> Self {
value as Self
}
}
/// 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 EInkImage {
data: Vec<DisplayColor>, buf: ImageBuffer<Luma<u8>, Vec<u8>>,
width: u32, palette: Vec<Srgb>,
height: u32,
} }
impl EInkImage { impl EInkImage {
#[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.data.len() / 2); let mut buf = Vec::with_capacity(self.buf.len() / 2);
for pix in self.buf.chunks_exact(2) {
for colors in self.data.chunks_exact(2) { buf.push(pix[0] << 4 | pix[1]);
buf.push(DisplayColor::into_byte(colors[0], colors[1]));
} }
buf buf
} }
#[must_use]
pub fn new(width: u32, height: u32) -> Self {
let v = vec![DisplayColor::Black; (width * height) as usize];
Self {
data: v,
width,
height,
}
}
/// Produces a regular RGB image from this image buffer using the given /// Convert the EInk-palette image into an RGB image to be viewed on a regular screen.
/// color palette. #[must_use]
pub fn make_image(&self) -> RgbImage { pub fn into_rgbimage(&self) -> RgbImage {
RgbImage::from_fn(self.width, self.height, |x, y| { RgbImage::from_fn(self.buf.width(), self.buf.height(), |x, y| {
let srgb = Srgb::from(self.data[(y * self.width + x) as usize]); let idx = self.buf.get_pixel(x, y).0[0];
let arr: [u8; 3] = srgb.into_format().into(); let disp_color = self.palette.get(idx as usize).unwrap();
let arr: [u8; 3] = disp_color.into_format().into();
imgRgb(arr) imgRgb(arr)
}) })
} }
/// Returns the dimensions (width, height) of the image buffer. /// Constructs a new EInk Image based on the given color palette for
pub const fn dimensions(&self) -> (u32, u32) { /// color indexing.
(self.width, self.height) #[must_use]
pub fn new(palette: Vec<Srgb>) -> Self {
Self {
buf: GrayImage::new(800, 480),
palette,
}
}
}
impl Default for EInkImage {
fn default() -> Self {
Self::new(DISPLAY_PALETTE.to_vec())
} }
} }
pub trait Ditherer { pub trait Ditherer {
fn dither(&self, img: &RgbImage, output: &mut EInkImage); fn dither(&mut self, img: &RgbImage, output: &mut EInkImage);
} }
/// Find the closest approximate palette color to the given sRGB value. /// Find the closest approximate palette color to the given sRGB value.
/// This uses euclidian distance in linear space. /// This uses euclidian distance in linear space.
#[must_use] fn nearest_neighbor(input_color: Lab, palette: &[Srgb]) -> (u8, Lab) {
pub fn nearest_neighbor(input_color: Lab) -> (DisplayColor, Lab) { let (nearest, _, color_diff) = palette
let (nearest, _, color_diff) = DISPLAY_PALETTE
.iter() .iter()
.enumerate() .enumerate()
.map(|(idx, p_color)| { .map(|(idx, p_color)| {
let c: Lab = (*p_color).into_color(); let c: Lab = Lab::from_color(*p_color);
(idx, input_color.difference(c), input_color - c) (idx, input_color.difference(c), input_color - c)
}) })
.min_by(|(_, a, _), (_, b, _)| a.total_cmp(b)) .min_by(|(_, a, _), (_, b, _)| a.total_cmp(b))
.expect("could not find a color"); .expect("Should always find a color");
(DisplayColor::from_u8(nearest as u8), color_diff) (nearest as u8, color_diff)
} }
pub struct NNDither(); pub struct NNDither();
impl Ditherer for NNDither { impl Ditherer for NNDither {
fn dither(&self, img: &RgbImage, output: &mut EInkImage) { fn dither(&mut self, img: &RgbImage, output: &mut EInkImage) {
assert!(img.width() == 800); assert!(img.width() == 800);
assert!(img.height() == 480); assert!(img.height() == 480);
// sRGB view into the given image. zero copy! // sRGB view into the given image. zero copy!
let srgb = <&[Srgb<u8>]>::from_components(&**img); let srgb = <&[Srgb<u8>]>::from_components(&**img);
for (idx, pixel) in srgb.iter().enumerate() { for (idx, pix) in output.buf.iter_mut().enumerate() {
let (n, _) = nearest_neighbor(pixel.into_format().into_color()); let (n, _) = nearest_neighbor(srgb[idx].into_format().into_color(), &output.palette);
output.data[idx] = n; *pix = n;
} }
} }
} }
@ -152,13 +113,13 @@ const fn coord_to_idx(x: u32, y: u32, xsize: u32) -> usize {
/// Compute the error-adjusted new lab value based on the error value of the currently scanned /// Compute the error-adjusted new lab value based on the error value of the currently scanned
/// pixel multiplied by a scalar factor. /// pixel multiplied by a scalar factor.
fn get_error_adjusted(orig: &Lab, err: &Lab, scalar: f32) -> Lab { fn compute_error_adjusted_color(orig: &Lab, err: &Lab, weight: f32) -> Lab {
let (p_l, p_a, p_b) = orig.into_components(); let (orig_l, orig_a, orig_b) = orig.into_components();
let (err_l, err_a, err_b) = err.into_components(); let (err_l, err_a, err_b) = err.into_components();
Lab::from_components(( Lab::from_components((
p_l + err_l * scalar, err_l.mul_add(weight, orig_l), // scalar * err_l + p_l
p_a + err_a * scalar, err_a.mul_add(weight, orig_a),
p_b + err_b * scalar, err_b.mul_add(weight, orig_b),
)) ))
} }
@ -255,10 +216,12 @@ impl ErrorDiffusionDither {
impl Ditherer for ErrorDiffusionDither { impl Ditherer for ErrorDiffusionDither {
#[instrument] #[instrument]
fn dither(&self, img: &RgbImage, output: &mut EInkImage) { fn dither(&mut self, img: &RgbImage, output: &mut EInkImage) {
// 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
let srgb = <&[Srgb<u8>]>::from_components(&**img); let srgb = <&[Srgb<u8>]>::from_components(&**img);
let (xsize, ysize) = img.dimensions(); let (xsize, ysize) = img.dimensions();
// our destination buffer.
let mut temp_img: Vec<Lab> = Vec::with_capacity((xsize * ysize) as usize); let mut temp_img: Vec<Lab> = Vec::with_capacity((xsize * ysize) as usize);
for pix in srgb { for pix in srgb {
temp_img.push(pix.into_format().into_color()); temp_img.push(pix.into_format().into_color());
@ -269,9 +232,9 @@ impl Ditherer for ErrorDiffusionDither {
for x in 0..xsize { for x in 0..xsize {
let index = coord_to_idx(x, y, xsize); let index = coord_to_idx(x, y, xsize);
let curr_pix = temp_img[index]; let curr_pix = temp_img[index];
let (nearest, err) = nearest_neighbor(curr_pix); let (nearest, err) = nearest_neighbor(curr_pix, &output.palette);
// set the color in the output buffer. // set the color in the output buffer.
output.data[index] = nearest; *output.buf.get_mut(index).unwrap() = nearest;
// take the error, and propagate it. // take the error, and propagate it.
for point in self.0.value() { for point in self.0.value() {
let Some(target_x) = x.checked_add_signed(point.xshift) else { let Some(target_x) = x.checked_add_signed(point.xshift) else {
@ -282,7 +245,7 @@ impl Ditherer for ErrorDiffusionDither {
}; };
let target = coord_to_idx(target_x, target_y, xsize); let target = coord_to_idx(target_x, target_y, xsize);
if let Some(pix) = temp_img.get(target) { if let Some(pix) = temp_img.get(target) {
temp_img[target] = get_error_adjusted(pix, &err, point.scale); temp_img[target] = compute_error_adjusted_color(pix, &err, point.scale);
} }
} }
} }

View file

@ -3,13 +3,13 @@ pub mod display;
pub mod errors; pub mod errors;
pub mod imageproc; pub mod imageproc;
use crate::display::{EInkPanel, Wrapper}; use crate::display::{FakeEInk, EInkPanel, Wrapper};
use crate::imageproc::{DiffusionMatrix, Ditherer, EInkImage, ErrorDiffusionDither}; use crate::imageproc::{DiffusionMatrix, Ditherer, EInkImage, ErrorDiffusionDither};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use image::RgbImage; use image::RgbImage;
use tracing::info; use tracing::{error, info};
/// Display images over /// Display images on E-Ink Displays
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
#[command(version, about)] #[command(version, about)]
struct Cli { struct Cli {
@ -34,15 +34,15 @@ async fn main() -> anyhow::Result<()> {
println!("CLI {cli:?}"); println!("CLI {cli:?}");
if matches!(cli.command, Command::Show) { if matches!(cli.command, Command::Show) {
let img: RgbImage = image::ImageReader::open("myimage.png")?.decode()?.into(); let img: RgbImage = image::ImageReader::open("image.png")?.decode()?.into();
let mut display = Wrapper::new()?; error!("HI");
let mut display = FakeEInk {};
let mut eink_buf = EInkImage::new(800, 480); let mut eink_buf = EInkImage::default();
let dither = ErrorDiffusionDither::new(DiffusionMatrix::Atkinson); let mut dither = ErrorDiffusionDither::new(DiffusionMatrix::Atkinson);
dither.dither(&img, &mut eink_buf); dither.dither(&img, &mut eink_buf);
let raw_buf = eink_buf.into_display_buffer(); display.display(&eink_buf)?;
display.display(&raw_buf)?;
} }
if matches!(cli.command, Command::Test) { if matches!(cli.command, Command::Test) {
let mut display = Wrapper::new()?; let mut display = Wrapper::new()?;