pi-frame-server/src/imageproc.rs

174 lines
4.9 KiB
Rust
Raw Normal View History

2024-06-29 16:55:37 +00:00
use image::RgbImage;
2024-07-16 23:55:59 +00:00
use palette::{cast::FromComponents, color_difference::Ciede2000, IntoColor, Lab, Oklch, 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
];
// fn octcolor_rgb(color: &OctColor) -> &Srgb<u8> {
// match color {
// OctColor::Black => &DISPLAY_PALETTE[0],
// OctColor::White => &DISPLAY_PALETTE[1],
// OctColor::Green => &DISPLAY_PALETTE[2],
// OctColor::Blue => &DISPLAY_PALETTE[3],
// OctColor::Red => &DISPLAY_PALETTE[4],
// OctColor::Yellow => &DISPLAY_PALETTE[5],
// OctColor::Orange => &DISPLAY_PALETTE[6],
// OctColor::HiZ => &DISPLAY_PALETTE[1],
// }
// }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DisplayColor {
Black,
White,
Green,
Blue,
Red,
Yellow,
Orange,
}
2024-07-02 15:57:29 +00:00
impl From<DisplayColor> for Srgb {
fn from(value: DisplayColor) -> Self {
DISPLAY_PALETTE[value as usize]
2024-06-29 16:55:37 +00:00
}
}
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,
2024-07-16 23:55:59 +00:00
_ => panic!("unexpected DisplayColor {value}"),
2024-06-29 16:55:37 +00:00
}
}
2024-07-02 15:57:29 +00:00
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 {
2024-07-16 23:55:59 +00:00
value as Self
2024-07-02 15:57:29 +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-02 15:57:29 +00:00
pub struct EInkBuffer(Vec<DisplayColor>);
2024-06-29 16:55:37 +00:00
impl EInkBuffer {
2024-07-16 23:55:59 +00:00
#[must_use]
2024-07-02 15:57:29 +00:00
pub fn into_display_buffer(&self) -> Vec<u8> {
2024-07-16 23:55:59 +00:00
let mut buf = Vec::with_capacity(self.0.len() / 2);
2024-06-29 16:55:37 +00:00
2024-07-02 15:57:29 +00:00
for colors in self.0.chunks_exact(2) {
buf.push(DisplayColor::into_byte(colors[0], colors[1]));
2024-06-29 16:55:37 +00:00
}
2024-07-02 15:57:29 +00:00
buf
}
2024-07-16 23:55:59 +00:00
#[must_use]
2024-07-02 15:57:29 +00:00
pub fn new(width: usize, height: usize) -> Self {
let v = vec![DisplayColor::Black; width * height];
2024-07-16 23:55:59 +00:00
Self(v)
2024-06-29 16:55:37 +00:00
}
}
2024-07-02 15:57:29 +00:00
// impl EInkBuffer {
// /// Converts the EInkBuffer into data that can be sent over the SPI API
// /// Bin-packs the two 4-bit colors into bytes.
// pub fn into_buffer(&self) -> Vec<u8> {
// vec![]
// }
//
// pub fn new(width: usize, height: usize) -> EInkBuffer {
// EInkBuffer {
// data: vec![DisplayColor::Black; width * height],
// width,
// height,
// }
// }
// pub fn set(&mut self, x: usize, y: usize, value: DisplayColor) {
// self.data[x + y * self.width] = value;
// }
// pub fn get(&self, x:usize, y:usize) -> DisplayColor {
2024-07-16 23:55:59 +00:00
// self.data[x
2024-07-02 15:57:29 +00:00
// }
// }
2024-06-29 16:55:37 +00:00
pub trait Ditherer {
2024-07-16 23:55:59 +00:00
fn dither(&mut self, img: &RgbImage, output: &mut EInkBuffer);
2024-06-29 16:55:37 +00:00
}
// fn color_distance(c1: LinSrgb, c2: LinSrgb) -> f32 {
// let r2 = (c2.red - c1.red).powf(2.0);
// let g2 = (c2.green - c1.green).powf(2.0);
// let b2 = (c2.blue - c1.blue).powf(2.0);
//
// (r2 + g2 + b2).sqrt()
// }
/// Find the closest approximate palette color to the given sRGB value.
/// This uses euclidian distance in linear space.
2024-07-16 23:55:59 +00:00
pub fn nearest_neighbor(input_color: Lab) -> (DisplayColor, Lab) {
let (nearest, _, color_diff) = DISPLAY_PALETTE
2024-06-29 16:55:37 +00:00
.iter()
.enumerate()
.map(|(idx, p_color)| {
2024-07-02 15:57:29 +00:00
let c: Lab = (*p_color).into_color();
2024-07-16 23:55:59 +00:00
(idx, input_color.difference(c), input_color - c)
2024-06-29 16:55:37 +00:00
})
2024-07-16 23:55:59 +00:00
.min_by(|(_, a, _), (_, b, _)| a.total_cmp(b))
2024-06-29 16:55:37 +00:00
.unwrap();
2024-07-16 23:55:59 +00:00
(DisplayColor::from_u8(nearest as u8), color_diff)
2024-06-29 16:55:37 +00:00
}
pub struct NNDither();
impl Ditherer for NNDither {
2024-07-16 23:55:59 +00:00
fn dither(&mut self, img: &RgbImage, output: &mut EInkBuffer) {
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
for (idx, pixel) in srgb.iter().enumerate() {
2024-07-16 23:55:59 +00:00
let (n, _) = nearest_neighbor(pixel.into_format().into_color());
2024-07-02 15:57:29 +00:00
output.0[idx] = n;
}
2024-06-29 16:55:37 +00:00
}
}
2024-07-02 15:57:29 +00:00
2024-07-16 23:55:59 +00:00
pub struct FloydSteinbergDither();
impl Ditherer for FloydSteinbergDither {
fn dither(&mut self, img: &RgbImage, output: &mut EInkBuffer) {
// create a copy of the image in Lab space, mutable.
let srgb = <&[Srgb<u8>]>::from_components(&**img);
let mut temp_img: Vec<Lab> = Vec::new();
for pix in srgb {
temp_img.push(pix.into_format().into_color());
}
}
}