From f5271cd25f3002279a4fee57d09d12102645d66b Mon Sep 17 00:00:00 2001 From: Tommy Gilligan <7865781+tommy-gilligan@users.noreply.github.com> Date: Wed, 7 Feb 2024 18:19:24 +1100 Subject: [PATCH] update dither (broken) --- Cargo.lock | 45 ++++++++++++++++++++++++-- Cargo.toml | 3 +- examples/inkplate_image.rs | 66 ++++++++++++-------------------------- 3 files changed, 65 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c8dd3a4..0c37b38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,6 +16,7 @@ dependencies = [ "esp-backtrace", "esp-println 0.7.1", "esp32-hal", + "fixed", "num_enum", "tinybmp", ] @@ -86,6 +87,12 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "bytemuck" +version = "1.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea31d69bda4949c1c1562c1e6f042a1caefac98cdc8a298260a2ff41c1e2d42b" + [[package]] name = "byteorder" version = "1.5.0" @@ -117,6 +124,12 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7059fff8937831a9ae6f0fe4d658ffabf58f2ca96aa9dec1c889f936f705f216" +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "darling" version = "0.20.5" @@ -187,10 +200,10 @@ dependencies = [ [[package]] name = "dither" version = "0.1.0" -source = "git+https://github.com/tommy-gilligan/dither.git#dc278ec7bc337148114ee8aa4e3b6f656e983f2d" +source = "git+https://github.com/tommy-gilligan/dither.git?branch=separate-e-g#4af16deadb807320b7512ed67bbc70490d6baf1c" dependencies = [ "embedded-graphics-core", - "heapless", + "fixed", ] [[package]] @@ -506,6 +519,18 @@ dependencies = [ "xtensa-lx", ] +[[package]] +name = "fixed" +version = "1.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e29e5681dc8556fb9df1409e95eae050e12e8776394313da3546dcb8cf390c73" +dependencies = [ + "az", + "bytemuck", + "half", + "typenum", +] + [[package]] name = "float-cmp" version = "0.9.0" @@ -536,6 +561,16 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d758ba1b47b00caf47f24925c0074ecb20d6dfcffe7f6d53395c0465674841a" +[[package]] +name = "half" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc52e53916c08643f1b56ec082790d1e86a32e58dc5268f897f313fbae7b4872" +dependencies = [ + "cfg-if", + "crunchy", +] + [[package]] name = "hash32" version = "0.3.1" @@ -988,6 +1023,12 @@ dependencies = [ "winnow", ] +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-ident" version = "1.0.12" diff --git a/Cargo.toml b/Cargo.toml index 56d0156..79bef13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ categories = ["embedded", "no-std", "hardware-support"] embed-doc-image = "0.1.4" embedded-graphics-core = { version = "0.4.0", optional = true } embedded-hal = "1.0.0" +fixed = "1.25.1" num_enum = { version = "0.7.2", default-features = false } [dev-dependencies] @@ -29,7 +30,7 @@ embedded-hal-mock = { git = "https://github.com/tommy-gilligan/embedded-hal-mock esp-backtrace = { version = "0.10.0", features = ["esp32", "panic-handler", "exception-handler", "print-uart"] } esp-println = { version = "0.7.0", features = ["esp32"] } hal = { package = "esp32-hal", version = "0.18.0", features = ["eh1"] } -dither = { git = "https://github.com/tommy-gilligan/dither.git", features = ["color_cube"] } +dither = { git = "https://github.com/tommy-gilligan/dither.git", branch = "separate-e-g" } embedded-hal-bus = "0.1.0" [features] diff --git a/examples/inkplate_image.rs b/examples/inkplate_image.rs index ed48bf8..77ee2b7 100644 --- a/examples/inkplate_image.rs +++ b/examples/inkplate_image.rs @@ -10,7 +10,7 @@ //! use ab1024_ega::color::Color; -use dither::DitherTarget; +use dither::embedded_graphics::DitherTarget; use embedded_graphics::{pixelcolor::Rgb888, prelude::*}; use embedded_hal_bus::spi::ExclusiveDevice; use esp_backtrace as _; @@ -23,41 +23,31 @@ use hal::{ Delay, Rtc, }; use tinybmp::Bmp; -use dither::color_cube; -use dither::color_cube::ColorCube; +use dither::vector::Vector; +use fixed::types::extra::U16; +type Num = fixed::FixedI32; -const RGB_DISPLAY_PAIRS: [(Rgb888, Color); 7] = [ - (Rgb888::new(0x00, 0x00, 0x00), Color::BLACK), - (Rgb888::new(0xFF, 0xFF, 0xFF), Color::WHITE), - (Rgb888::new(0x10, 0xcb, 0x10), Color::GREEN), - (Rgb888::new(0x20, 0x20, 0xff), Color::BLUE), - (Rgb888::new(0xff, 0x30, 0x20), Color::RED), - (Rgb888::new(0xff, 0xff, 0x50), Color::YELLOW), - (Rgb888::new(0xf0, 0x70, 0x20), Color::ORANGE), +const RGB_DISPLAY_PAIRS: [(Color, Rgb888); 7] = [ + (Color::BLACK, Rgb888::new(0x00, 0x00, 0x00)), + (Color::WHITE, Rgb888::new(0xFF, 0xFF, 0xFF)), + (Color::GREEN, Rgb888::new(0x10, 0xcb, 0x10)), + (Color::BLUE, Rgb888::new(0x20, 0x20, 0xff)), + (Color::RED, Rgb888::new(0xff, 0x30, 0x20)), + (Color::YELLOW, Rgb888::new(0xff, 0xff, 0x50)), + (Color::ORANGE, Rgb888::new(0xf0, 0x70, 0x20)), ]; -fn rgb_to_epd(color: Rgb888) -> (Color, (i16, i16, i16)) { +fn conversion(source: Vector) -> (Color, Vector) { let pair = RGB_DISPLAY_PAIRS .into_iter() - .min_by_key(|(rgb, _): &(Rgb888, Color)| { - let r: u16 = - (>::into(color.r())).abs_diff(>::into(rgb.r())); - let g: u16 = - (>::into(color.g())).abs_diff(>::into(rgb.g())); - let b: u16 = - (>::into(color.b())).abs_diff(>::into(rgb.b())); - r + g + b + .min_by_key(|(_, rgb): &(Color, Rgb888)| { + (source.0).abs_diff(Num::from_num(rgb.r())) + + (source.1).abs_diff(Num::from_num(rgb.g())) + + (source.2).abs_diff(Num::from_num(rgb.b())) }) .unwrap(); - ( - pair.1, - ( - color.r() as i16 - pair.0.r() as i16, - color.g() as i16 - pair.0.g() as i16, - color.b() as i16 - pair.0.b() as i16, - ) - ) + (pair.0, source - Vector::from_rgb888(pair.1)) } #[entry] @@ -82,25 +72,9 @@ fn main() -> ! { let bmp: Bmp = Bmp::from_slice(include_bytes!("starry-night.bmp")).unwrap(); let mut display = ab1024_ega::Display::new(spi, rst, dc, busy, delay); + let mut ed: DitherTarget<'_, _, _, { ab1024_ega::WIDTH } > = + DitherTarget::new(&mut display, &conversion); - let color_cube: color_cube::ColorCube = - color_cube::ColorCube::from(&|r, g, b| { - let pair = RGB_DISPLAY_PAIRS - .into_iter() - .min_by_key(|(rgb, _): &(Rgb888, Color)| { - (>::into(r)).abs_diff(>::into(rgb.r())) + - (>::into(g)).abs_diff(>::into(rgb.g())) + - (>::into(b)).abs_diff(>::into(rgb.b())) - }) - .unwrap(); - pair.1 - }) - .unwrap(); - - let binding = |rgb| color_cube.with_error(rgb); - - let mut ed: DitherTarget<'_, _, _, { ab1024_ega::WIDTH + 1 }> = - DitherTarget::new(&mut display, &binding); bmp.draw(&mut ed).unwrap(); display.init().unwrap();