254 lines
8.2 KiB
Rust
254 lines
8.2 KiB
Rust
use image::{GrayImage, ImageBuffer, Luma, RgbImage};
|
|
use palette::FromColor;
|
|
use serde::{Deserialize, Serialize};
|
|
use tracing::instrument;
|
|
|
|
use image::Rgb as imgRgb;
|
|
use palette::color_difference::{Ciede2000, HyAb};
|
|
use palette::{cast::FromComponents, IntoColor, Lab, Srgb};
|
|
|
|
#[derive(
|
|
strum::EnumString, strum::Display, Serialize, Deserialize, PartialEq, Eq, Debug, Clone,
|
|
)]
|
|
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(NearestNeighbor {}),
|
|
Self::Atkinson => Box::new(ErrorDiffusion::new(ATKINSON_DITHER_POINTS)),
|
|
Self::FloydSteinberg => Box::new(ErrorDiffusion::new(FLOYD_STEINBERG_POINTS)),
|
|
Self::Stuki => Box::new(ErrorDiffusion::new(STUKI_DITHER_POINTS)),
|
|
Self::Sierra => Box::new(ErrorDiffusion::new(SIERRA_DITHER_POINTS)),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub enum ProcessingError {
|
|
DitherError,
|
|
PaletteIndexError(usize),
|
|
}
|
|
|
|
/// Buffer to be sent to the ``EInk`` display.
|
|
#[derive(Debug)]
|
|
pub struct DitheredImage {
|
|
buf: ImageBuffer<Luma<u8>, Vec<u8>>,
|
|
palette: Vec<Srgb>,
|
|
}
|
|
|
|
impl DitheredImage {
|
|
#[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
|
|
}
|
|
|
|
/// 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| {
|
|
let idx = self.buf.get_pixel(x, y).0[0];
|
|
let disp_color = self
|
|
.palette
|
|
.get(idx as usize)
|
|
.expect("Palette will never be out of bounds");
|
|
let arr: [u8; 3] = disp_color.into_format().into();
|
|
imgRgb(arr)
|
|
})
|
|
}
|
|
|
|
/// Constructs a new Dithered Image based on the given dimensions and color palette for
|
|
/// color indexing.
|
|
#[must_use]
|
|
pub fn new(width: u32, height: u32, palette: Vec<Srgb>) -> Self {
|
|
Self {
|
|
buf: GrayImage::new(width, height),
|
|
palette,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub trait Ditherer {
|
|
fn dither(&mut self, img: &RgbImage, output: &mut DitheredImage);
|
|
}
|
|
|
|
/// 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: &[Lab]) -> (u8, Lab) {
|
|
let (nearest, _, color_diff) = palette
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(idx, p_color)| {
|
|
(
|
|
idx,
|
|
input_color.difference(*p_color),
|
|
input_color - *p_color,
|
|
)
|
|
})
|
|
.min_by(|(_, a, _), (_, b, _)| a.total_cmp(b))
|
|
.expect("Should always find a color");
|
|
(nearest as u8, color_diff)
|
|
}
|
|
|
|
pub struct NearestNeighbor();
|
|
|
|
impl Ditherer for NearestNeighbor {
|
|
fn dither(&mut self, img: &RgbImage, output: &mut DitheredImage) {
|
|
// sRGB view into the given image. zero copy!
|
|
let srgb = <&[Srgb<u8>]>::from_components(&**img);
|
|
|
|
let lab_palette: Vec<Lab> = output.palette.iter().map(|c| Lab::from_color(*c)).collect();
|
|
|
|
for (idx, pix) in output.buf.iter_mut().enumerate() {
|
|
let (n, _) = nearest_neighbor(srgb[idx].into_format().into_color(), &lab_palette);
|
|
*pix = n;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 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
|
|
/// pixel multiplied by a scalar factor.
|
|
fn compute_error_adjusted_color(orig: &Lab, err: &Lab, weight: f32) -> Lab {
|
|
*orig + *err * weight
|
|
}
|
|
|
|
/// ``DiffusionPoint`` is part of the diffusion matrix, represented by a shift in x and y and an error
|
|
/// scaling factor.
|
|
#[derive(Debug)]
|
|
pub struct DiffusionPoint {
|
|
xshift: i32,
|
|
yshift: i32,
|
|
scale: f32,
|
|
}
|
|
|
|
impl DiffusionPoint {
|
|
/// Creates a new ``DiffusionPoint``
|
|
const fn new(xshift: i32, yshift: i32, scale: f32) -> Self {
|
|
Self {
|
|
xshift,
|
|
yshift,
|
|
scale,
|
|
}
|
|
}
|
|
}
|
|
|
|
static FLOYD_STEINBERG_POINTS: &[DiffusionPoint] = &[
|
|
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),
|
|
];
|
|
|
|
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),
|
|
];
|
|
|
|
pub type DiffusionMatrix<'a> = &'a [DiffusionPoint];
|
|
|
|
#[derive(Debug)]
|
|
pub struct ErrorDiffusion<'a>(&'a [DiffusionPoint]);
|
|
|
|
impl<'a> ErrorDiffusion<'a> {
|
|
#[must_use]
|
|
pub const fn new(dm: DiffusionMatrix<'a>) -> Self {
|
|
Self(dm)
|
|
}
|
|
}
|
|
|
|
impl<'a> Ditherer for ErrorDiffusion<'a> {
|
|
#[instrument]
|
|
fn dither(&mut self, img: &RgbImage, output: &mut DitheredImage) {
|
|
// 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());
|
|
}
|
|
let lab_palette: Vec<Lab> = output.palette.iter().map(|c| Lab::from_color(*c)).collect();
|
|
|
|
// TODO: rework this to make more sense.
|
|
for y in 0..ysize {
|
|
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, &lab_palette);
|
|
// set the color in the output buffer.
|
|
*output
|
|
.buf
|
|
.get_mut(index)
|
|
.expect("always in bounds of image") = nearest;
|
|
// take the error, and propagate it.
|
|
for point in self.0 {
|
|
// bounds checking.
|
|
let Some(target_x) = x.checked_add_signed(point.xshift) else {
|
|
continue;
|
|
};
|
|
let Some(target_y) = y.checked_add_signed(point.yshift) else {
|
|
continue;
|
|
};
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|