2024-07-29 17:51:51 +00:00
|
|
|
use image::{GrayImage, ImageBuffer, Luma, RgbImage};
|
2024-07-28 14:28:06 +00:00
|
|
|
use palette::FromColor;
|
2024-07-29 21:39:49 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-07-28 04:01:57 +00:00
|
|
|
use tracing::instrument;
|
2024-06-29 16:55:37 +00:00
|
|
|
|
2024-07-17 18:27:31 +00:00
|
|
|
use image::Rgb as imgRgb;
|
2024-07-18 20:51:07 +00:00
|
|
|
use palette::color_difference::Ciede2000;
|
|
|
|
use palette::{cast::FromComponents, IntoColor, Lab, Srgb};
|
2024-06-29 16:55:37 +00:00
|
|
|
|
|
|
|
/// Palette used on the display; pixels can be one of these colors.
|
|
|
|
///
|
|
|
|
/// The RGB values are slightly adjusted to improve accuracy.
|
|
|
|
const DISPLAY_PALETTE: [Srgb; 7] = [
|
|
|
|
Srgb::new(0.047, 0.047, 0.055), // Black
|
|
|
|
Srgb::new(0.824, 0.824, 0.816), // White
|
|
|
|
Srgb::new(0.118, 0.376, 0.122), // Green
|
|
|
|
Srgb::new(0.114, 0.118, 0.667), // Blue
|
|
|
|
Srgb::new(0.549, 0.106, 0.114), // Red
|
|
|
|
Srgb::new(0.827, 0.788, 0.239), // Yellow
|
|
|
|
Srgb::new(0.757, 0.443, 0.165), // Orange
|
|
|
|
];
|
|
|
|
|
2024-07-30 05:35:43 +00:00
|
|
|
const SIMPLE_PALETTE: [Srgb; 7] = [
|
2024-07-30 13:53:47 +00:00
|
|
|
Srgb::new(0.0, 0.0, 0.0), // Black
|
|
|
|
Srgb::new(1.0, 1.0, 1.0), // White
|
|
|
|
Srgb::new(0.0, 1.0, 0.0), // Green
|
|
|
|
Srgb::new(0.0, 0.0, 1.0), // Blue
|
|
|
|
Srgb::new(1.0, 0.0, 0.0), // Red
|
|
|
|
Srgb::new(1.0, 1.0, 0.0), // Yellow
|
2024-07-30 05:35:43 +00:00
|
|
|
Srgb::new(0.757, 0.443, 0.165), // Orange
|
|
|
|
];
|
|
|
|
|
|
|
|
#[derive(strum::EnumString, Serialize, Deserialize, PartialEq, Eq, Debug)]
|
|
|
|
pub enum DitherPalette {
|
|
|
|
Default,
|
|
|
|
Simple,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DitherPalette {
|
2024-07-30 13:53:47 +00:00
|
|
|
#[must_use]
|
|
|
|
pub const fn value(&self) -> &[Srgb] {
|
2024-07-30 05:35:43 +00:00
|
|
|
match self {
|
|
|
|
Self::Default => &DISPLAY_PALETTE,
|
2024-07-30 13:53:47 +00:00
|
|
|
Self::Simple => &SIMPLE_PALETTE, // FIXME: use simple pallete based on binary.
|
2024-07-30 05:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-29 21:39:49 +00:00
|
|
|
|
2024-07-30 05:35:43 +00:00
|
|
|
#[derive(strum::EnumString, Serialize, Deserialize, PartialEq, Eq, Debug)]
|
2024-07-29 21:39:49 +00:00
|
|
|
pub enum DitherMethod {
|
|
|
|
NearestNeighbor,
|
|
|
|
FloydSteinberg,
|
|
|
|
Atkinson,
|
|
|
|
Stuki,
|
|
|
|
Sierra,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DitherMethod {
|
|
|
|
#[must_use]
|
|
|
|
pub fn get_ditherer(&self) -> Box<dyn Ditherer> {
|
|
|
|
match self {
|
|
|
|
Self::NearestNeighbor => Box::new(NNDither {}),
|
|
|
|
Self::Atkinson => Box::new(ErrorDiffusionDither::new(ATKINSON_DITHER_POINTS)),
|
|
|
|
Self::FloydSteinberg => Box::new(ErrorDiffusionDither::new(FLOYD_STEINBERG_POINTS)),
|
|
|
|
Self::Stuki => Box::new(ErrorDiffusionDither::new(STUKI_DITHER_POINTS)),
|
|
|
|
Self::Sierra => Box::new(ErrorDiffusionDither::new(SIERRA_DITHER_POINTS)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-29 17:51:51 +00:00
|
|
|
pub enum ProcessingError {
|
2024-07-28 14:28:06 +00:00
|
|
|
DitherError,
|
|
|
|
PaletteIndexError(usize),
|
|
|
|
}
|
2024-06-29 16:55:37 +00:00
|
|
|
|
2024-07-16 23:55:59 +00:00
|
|
|
/// Buffer to be sent to the ``EInk`` display.
|
2024-06-29 16:55:37 +00:00
|
|
|
#[derive(Debug)]
|
2024-07-17 18:27:31 +00:00
|
|
|
pub struct EInkImage {
|
2024-07-28 14:28:06 +00:00
|
|
|
buf: ImageBuffer<Luma<u8>, Vec<u8>>,
|
|
|
|
palette: Vec<Srgb>,
|
|
|
|
}
|
2024-07-29 17:51:51 +00:00
|
|
|
|
|
|
|
impl EInkImage {
|
|
|
|
#[must_use]
|
2024-07-28 14:28:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-29 17:51:51 +00:00
|
|
|
/// Convert the EInk-palette image into an RGB image to be viewed on a regular screen.
|
|
|
|
#[must_use]
|
2024-07-28 14:28:06 +00:00
|
|
|
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];
|
2024-07-30 13:53:47 +00:00
|
|
|
let disp_color = self
|
|
|
|
.palette
|
|
|
|
.get(idx as usize)
|
|
|
|
.expect("Palette will never be out of bounds");
|
2024-07-28 14:28:06 +00:00
|
|
|
let arr: [u8; 3] = disp_color.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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-29 16:55:37 +00:00
|
|
|
|
2024-07-29 17:51:51 +00:00
|
|
|
impl Default for EInkImage {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new(DISPLAY_PALETTE.to_vec())
|
2024-07-17 18:27:31 +00:00
|
|
|
}
|
2024-06-29 16:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Ditherer {
|
2024-07-28 14:28:06 +00:00
|
|
|
fn dither(&mut self, img: &RgbImage, output: &mut EInkImage);
|
2024-06-29 16:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Find the closest approximate palette color to the given sRGB value.
|
|
|
|
/// This uses euclidian distance in linear space.
|
2024-07-29 17:51:51 +00:00
|
|
|
fn nearest_neighbor(input_color: Lab, palette: &[Srgb]) -> (u8, Lab) {
|
2024-07-28 14:28:06 +00:00
|
|
|
let (nearest, _, color_diff) = palette
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(idx, p_color)| {
|
|
|
|
let c: Lab = Lab::from_color(*p_color);
|
|
|
|
(idx, input_color.difference(c), input_color - c)
|
|
|
|
})
|
|
|
|
.min_by(|(_, a, _), (_, b, _)| a.total_cmp(b))
|
|
|
|
.expect("Should always find a color");
|
2024-07-29 17:51:51 +00:00
|
|
|
(nearest as u8, color_diff)
|
2024-06-29 16:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NNDither();
|
|
|
|
|
|
|
|
impl Ditherer for NNDither {
|
2024-07-28 14:28:06 +00:00
|
|
|
fn dither(&mut self, img: &RgbImage, output: &mut EInkImage) {
|
2024-06-29 16:55:37 +00:00
|
|
|
assert!(img.width() == 800);
|
|
|
|
assert!(img.height() == 480);
|
2024-07-16 23:55:59 +00:00
|
|
|
|
2024-07-02 15:57:29 +00:00
|
|
|
// sRGB view into the given image. zero copy!
|
2024-06-29 16:55:37 +00:00
|
|
|
let srgb = <&[Srgb<u8>]>::from_components(&**img);
|
2024-07-02 15:57:29 +00:00
|
|
|
|
2024-07-29 17:51:51 +00:00
|
|
|
for (idx, pix) in output.buf.iter_mut().enumerate() {
|
|
|
|
let (n, _) = nearest_neighbor(srgb[idx].into_format().into_color(), &output.palette);
|
|
|
|
*pix = n;
|
2024-07-02 15:57:29 +00:00
|
|
|
}
|
2024-06-29 16:55:37 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-02 15:57:29 +00:00
|
|
|
|
2024-07-17 05:24:11 +00:00
|
|
|
/// Compute the vector index for a given image by using the size of rows. Assumes that images
|
|
|
|
/// are indexed in row-major order.
|
|
|
|
const fn coord_to_idx(x: u32, y: u32, xsize: u32) -> usize {
|
|
|
|
(y * xsize + x) as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compute the error-adjusted new lab value based on the error value of the currently scanned
|
2024-07-17 17:37:43 +00:00
|
|
|
/// pixel multiplied by a scalar factor.
|
2024-07-28 14:28:06 +00:00
|
|
|
fn compute_error_adjusted_color(orig: &Lab, err: &Lab, weight: f32) -> Lab {
|
|
|
|
let (orig_l, orig_a, orig_b) = orig.into_components();
|
2024-07-17 05:24:11 +00:00
|
|
|
let (err_l, err_a, err_b) = err.into_components();
|
|
|
|
Lab::from_components((
|
2024-07-28 14:28:06 +00:00
|
|
|
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),
|
2024-07-17 05:24:11 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2024-07-17 17:37:43 +00:00
|
|
|
/// ``DiffusionPoint`` is part of the diffusion matrix, represented by a shift in x and y and an error
|
|
|
|
/// scaling factor.
|
2024-07-29 21:39:49 +00:00
|
|
|
#[derive(Debug)]
|
2024-07-30 13:53:47 +00:00
|
|
|
pub struct DiffusionPoint {
|
2024-07-17 05:24:11 +00:00
|
|
|
xshift: i32,
|
|
|
|
yshift: i32,
|
|
|
|
scale: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiffusionPoint {
|
2024-07-17 17:37:43 +00:00
|
|
|
/// Creates a new ``DiffusionPoint``
|
2024-07-17 05:24:11 +00:00
|
|
|
const fn new(xshift: i32, yshift: i32, scale: f32) -> Self {
|
|
|
|
Self {
|
|
|
|
xshift,
|
|
|
|
yshift,
|
|
|
|
scale,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 17:37:43 +00:00
|
|
|
static FLOYD_STEINBERG_POINTS: &[DiffusionPoint] = &[
|
2024-07-17 05:24:11 +00:00
|
|
|
DiffusionPoint::new(1, 0, 7.0 / 16.0),
|
|
|
|
DiffusionPoint::new(-1, 1, 3.0 / 16.0),
|
|
|
|
DiffusionPoint::new(0, 1, 5.0 / 16.0),
|
|
|
|
DiffusionPoint::new(1, 1, 1.0 / 16.0),
|
|
|
|
];
|
2024-07-17 17:37:43 +00:00
|
|
|
|
|
|
|
static ATKINSON_DITHER_POINTS: &[DiffusionPoint] = &[
|
|
|
|
DiffusionPoint::new(1, 0, 1.0 / 8.0),
|
|
|
|
DiffusionPoint::new(2, 0, 1.0 / 8.0),
|
|
|
|
DiffusionPoint::new(-1, 1, 1.0 / 8.0),
|
|
|
|
DiffusionPoint::new(0, 1, 1.0 / 8.0),
|
|
|
|
DiffusionPoint::new(1, 1, 1.0 / 8.0),
|
|
|
|
DiffusionPoint::new(0, 2, 1.0 / 8.0),
|
|
|
|
];
|
|
|
|
|
|
|
|
static SIERRA_DITHER_POINTS: &[DiffusionPoint] = &[
|
|
|
|
DiffusionPoint::new(1, 0, 5.0 / 32.0),
|
|
|
|
DiffusionPoint::new(2, 0, 3.0 / 32.0),
|
|
|
|
DiffusionPoint::new(-2, 1, 2.0 / 32.0),
|
|
|
|
DiffusionPoint::new(-1, 1, 4.0 / 32.0),
|
|
|
|
DiffusionPoint::new(0, 1, 5.0 / 32.0),
|
|
|
|
DiffusionPoint::new(1, 1, 4.0 / 32.0),
|
|
|
|
DiffusionPoint::new(2, 1, 2.0 / 32.0),
|
|
|
|
DiffusionPoint::new(-1, 2, 2.0 / 32.0),
|
|
|
|
DiffusionPoint::new(0, 2, 3.0 / 32.0),
|
|
|
|
DiffusionPoint::new(1, 2, 2.0 / 32.0),
|
|
|
|
];
|
|
|
|
|
|
|
|
static STUKI_DITHER_POINTS: &[DiffusionPoint] = &[
|
|
|
|
DiffusionPoint::new(1, 0, 8.0 / 42.0),
|
|
|
|
DiffusionPoint::new(2, 0, 4.0 / 42.0),
|
|
|
|
DiffusionPoint::new(-2, 1, 2.0 / 42.0),
|
|
|
|
DiffusionPoint::new(-1, 1, 4.0 / 42.0),
|
|
|
|
DiffusionPoint::new(0, 1, 8.0 / 42.0),
|
|
|
|
DiffusionPoint::new(1, 1, 4.0 / 42.0),
|
|
|
|
DiffusionPoint::new(2, 1, 2.0 / 42.0),
|
|
|
|
DiffusionPoint::new(-2, 2, 1.0 / 42.0),
|
|
|
|
DiffusionPoint::new(-1, 2, 2.0 / 42.0),
|
|
|
|
DiffusionPoint::new(0, 2, 4.0 / 42.0),
|
|
|
|
DiffusionPoint::new(1, 2, 2.0 / 42.0),
|
|
|
|
DiffusionPoint::new(1, 2, 1.0 / 42.0),
|
2024-07-17 15:20:06 +00:00
|
|
|
];
|
2024-07-17 05:24:11 +00:00
|
|
|
|
2024-07-29 21:39:49 +00:00
|
|
|
pub type DiffusionMatrix<'a> = &'a [DiffusionPoint];
|
2024-07-17 17:37:43 +00:00
|
|
|
|
2024-07-27 19:11:44 +00:00
|
|
|
#[derive(Debug)]
|
2024-07-29 21:39:49 +00:00
|
|
|
pub struct ErrorDiffusionDither<'a>(&'a [DiffusionPoint]);
|
|
|
|
|
|
|
|
impl<'a> ErrorDiffusionDither<'a> {
|
2024-07-17 17:37:43 +00:00
|
|
|
#[must_use]
|
2024-07-29 21:39:49 +00:00
|
|
|
pub const fn new(dm: DiffusionMatrix<'a>) -> Self {
|
2024-07-17 17:37:43 +00:00
|
|
|
Self(dm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-29 21:39:49 +00:00
|
|
|
impl<'a> Ditherer for ErrorDiffusionDither<'a> {
|
2024-07-27 19:11:44 +00:00
|
|
|
#[instrument]
|
2024-07-28 14:28:06 +00:00
|
|
|
fn dither(&mut self, img: &RgbImage, output: &mut EInkImage) {
|
2024-07-16 23:55:59 +00:00
|
|
|
// create a copy of the image in Lab space, mutable.
|
2024-07-28 14:28:06 +00:00
|
|
|
// first, a view into the rgb components
|
2024-07-16 23:55:59 +00:00
|
|
|
let srgb = <&[Srgb<u8>]>::from_components(&**img);
|
2024-07-17 05:24:11 +00:00
|
|
|
let (xsize, ysize) = img.dimensions();
|
2024-07-28 14:28:06 +00:00
|
|
|
// our destination buffer.
|
2024-07-17 05:24:11 +00:00
|
|
|
let mut temp_img: Vec<Lab> = Vec::with_capacity((xsize * ysize) as usize);
|
2024-07-16 23:55:59 +00:00
|
|
|
for pix in srgb {
|
|
|
|
temp_img.push(pix.into_format().into_color());
|
|
|
|
}
|
2024-07-17 05:24:11 +00:00
|
|
|
|
2024-07-29 21:39:49 +00:00
|
|
|
// TODO: rework this to make more sense.
|
2024-07-17 05:24:11 +00:00
|
|
|
for y in 0..ysize {
|
|
|
|
for x in 0..xsize {
|
|
|
|
let index = coord_to_idx(x, y, xsize);
|
|
|
|
let curr_pix = temp_img[index];
|
2024-07-29 17:51:51 +00:00
|
|
|
let (nearest, err) = nearest_neighbor(curr_pix, &output.palette);
|
2024-07-17 05:24:11 +00:00
|
|
|
// set the color in the output buffer.
|
2024-07-29 17:51:51 +00:00
|
|
|
*output.buf.get_mut(index).unwrap() = nearest;
|
2024-07-17 05:24:11 +00:00
|
|
|
// take the error, and propagate it.
|
2024-07-29 21:39:49 +00:00
|
|
|
for point in self.0 {
|
|
|
|
// bounds checking.
|
2024-07-17 05:24:11 +00:00
|
|
|
let Some(target_x) = x.checked_add_signed(point.xshift) else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
let Some(target_y) = y.checked_add_signed(point.yshift) else {
|
|
|
|
continue;
|
|
|
|
};
|
2024-07-17 17:37:43 +00:00
|
|
|
let target = coord_to_idx(target_x, target_y, xsize);
|
2024-07-17 05:24:11 +00:00
|
|
|
if let Some(pix) = temp_img.get(target) {
|
2024-07-28 14:28:06 +00:00
|
|
|
temp_img[target] = compute_error_adjusted_color(pix, &err, point.scale);
|
2024-07-17 05:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-16 23:55:59 +00:00
|
|
|
}
|
|
|
|
}
|