Compare commits

..

No commits in common. "608ebe9ec57af6cd27206b3717332e71daa0b579" and "7e5d13849e98b62d031bc20d56b79a995df62605" have entirely different histories.

6 changed files with 126 additions and 136 deletions

24
Cargo.lock generated
View file

@ -1309,8 +1309,6 @@ dependencies = [
"linux-embedded-hal",
"minijinja",
"palette",
"serde",
"strum",
"thiserror",
"tokio",
"tower-http",
@ -1758,28 +1756,6 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "syn"
version = "2.0.72"

View file

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

View file

@ -7,7 +7,6 @@ use image::ImageReader;
use std::io::Cursor;
use std::time::Duration;
use tracing::{debug, error, info, instrument};
use serde::{Serialize, Deserialize};
use crate::display::EInkPanel;
use std::sync::Arc;
@ -37,6 +36,25 @@ 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`.
struct AppError(anyhow::Error);
@ -62,38 +80,11 @@ 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
/// Start with the basics: Send an image, crop it, dither, and upload.
/// we defer the upload to a separate task.
pub fn router() -> Router<Context> {
Router::new()
.route("/setimage", post(set_image))
.route("/process_image", post(process_image))
}
#[derive(Serialize, Deserialize)]
pub struct ImageRequest {
Router::new().route("/setimage", post(set_image))
}
#[instrument(skip(ctx))]
@ -112,8 +103,8 @@ async fn set_image(
debug!("Guessed format: {:?}", reader.format());
let image = reader.decode()?;
let mut buf = EInkImage::default();
let mut dither = ErrorDiffusionDither::new(DiffusionMatrix::Atkinson);
let mut buf = EInkImage::new(800, 480);
let dither = ErrorDiffusionDither::new(DiffusionMatrix::Atkinson);
dither.dither(&image.into(), &mut buf);
let cmd = DisplaySetCommand { img: Box::new(buf) };
ctx.display_channel
@ -123,7 +114,3 @@ async fn set_image(
}
Ok(())
}
async fn process_image(mut parts: Multipart) -> Result<impl IntoResponse, AppError> {
Ok(StatusCode::OK)
}

View file

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

View file

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

View file

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