From 60aff508bd94d1e8c0d00342b53bf5bc03d656c8 Mon Sep 17 00:00:00 2001 From: Ethan Simmons Date: Mon, 15 Apr 2024 23:28:10 -0500 Subject: [PATCH] Added easier way to set colors + tests --- Cargo.lock | 1 + Cargo.toml | 1 + src/lib.rs | 7 --- src/main.rs | 18 +++++-- src/process_point.rs | 9 +++- src/wayland.rs | 19 +++++--- src/wayland/app.rs | 0 src/wayland/pixel.rs | 113 +++++++++++++++++++++++++++++++++++++++++++ src/wayland/ui.rs | 0 9 files changed, 148 insertions(+), 20 deletions(-) create mode 100644 src/wayland/app.rs create mode 100644 src/wayland/pixel.rs create mode 100644 src/wayland/ui.rs diff --git a/Cargo.lock b/Cargo.lock index 1d5b1fe..6b8a3f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1690,6 +1690,7 @@ dependencies = [ "rten", "rten-tensor", "smithay-client-toolkit", + "wayland-client", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index f936e04..2ee6cee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,4 @@ ocrs = "0.4.*" image = "0.24.9" anyhow = "1.0.81" smithay-client-toolkit = "0.18.1" +wayland-client = "0.31.2" diff --git a/src/lib.rs b/src/lib.rs index 7bd0e5d..8b13789 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1 @@ -pub mod process_point; -#[derive(Debug)] -pub struct Point { - pub x: f64, - pub y: f64, - pub new_line: bool, -} diff --git a/src/main.rs b/src/main.rs index 25fb8c9..c51f95e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,15 @@ use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt, OrientableExt, WidgetExt, Ge use gtk::cairo::{Context, Operator}; use relm4::drawing::DrawHandler; -use waywrite::process_point::print_words; -use waywrite::Point; +use std::error::Error; + +use anyhow::Result; + + +mod wayland; +mod process_point; + +use process_point::{Point, print_words}; #[derive(Debug)] enum AppInput { @@ -148,7 +155,8 @@ fn draw(cx: &Context, points: &[Point]) { } } -fn main() { - let app = RelmApp::new("simmer505.waywrite"); - app.run::(0); +fn main() -> Result<(), Box> { + + wayland::run()?; + Ok(()) } diff --git a/src/process_point.rs b/src/process_point.rs index 7513219..9192bbc 100644 --- a/src/process_point.rs +++ b/src/process_point.rs @@ -7,8 +7,6 @@ use rten::Model; use ocrs::{OcrEngine, OcrEngineParams}; use image::{ColorType, ImageFormat}; -use crate::Point; - const MATRIX_LEN: usize = 800; const TEXT_MATRIX_RATIO: f64 = 0.5; const LINE_WIDTH: f64 = 10.0; @@ -18,6 +16,13 @@ const TEXT_X_SIZE: f64 = (MATRIX_X_SIZE as f64) * TEXT_MATRIX_RATIO; const TEXT_X_OFFSET: f64 = (MATRIX_X_SIZE - TEXT_X_SIZE) / 2.0; const LINE_WIDTH_X_OFFSET: f64 = LINE_WIDTH / 2.0; +#[derive(Debug)] +pub struct Point { + pub x: f64, + pub y: f64, + pub new_line: bool, +} + pub fn print_words(points: &Vec) -> Result<(), Box> { let begin = Instant::now(); diff --git a/src/wayland.rs b/src/wayland.rs index 63af819..1cbfab7 100644 --- a/src/wayland.rs +++ b/src/wayland.rs @@ -13,8 +13,7 @@ use smithay_client_toolkit::{ }, WaylandSurface, }, - shm::{ - slot::{SlotPool, Buffer}, + shm::{ slot::{SlotPool, Buffer}, Shm, ShmHandler }, delegate_registry, delegate_compositor, delegate_seat, delegate_output, @@ -31,10 +30,15 @@ use std::time::Duration; use anyhow::Result; +mod ui; + +use ui::{Pixel, PixelEncoding}; + const WINDOW_HEIGHT: u32 = 256; const WINDOW_WIDTH: u32 = 512; -fn main() -> Result<()> { + +pub(super) fn run() -> Result<()> { let conn = Connection::connect_to_env()?; let (globals, event_queue) = registry_queue_init(&conn)?; @@ -249,10 +253,13 @@ impl SimpleWindow { for pix in canvas.chunks_exact_mut(4) { - let color: i32 = (255 << 24) + (255 << 16) + (0 << 8) + 0; + let pixel_color: &[u8; 4] = &Pixel::new(0.5, 0.5, 0.5, 1.0) + .as_u32(PixelEncoding::ARGB) + .to_le_bytes(); + + let pix: &mut [u8; 4] = pix.try_into().unwrap(); - let array: &mut [u8; 4] = pix.try_into().unwrap(); - *array = color.to_le_bytes(); + *pix = *pixel_color; } self.window.wl_surface().damage_buffer(0, 0, self.width as i32, self.height as i32); diff --git a/src/wayland/app.rs b/src/wayland/app.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/wayland/pixel.rs b/src/wayland/pixel.rs new file mode 100644 index 0000000..77dfb33 --- /dev/null +++ b/src/wayland/pixel.rs @@ -0,0 +1,113 @@ + +pub(super) enum PixelEncoding { + ARGB, + RGBA, +} + +struct ColorValue { + value: u8, +} + +impl ColorValue { + fn new(value: u8) -> Self { + Self { value } + } +} + +impl From for ColorValue { + fn from(value: u8) -> Self { + Self { value } + } +} + +impl From for ColorValue { + fn from(value: f32) -> Self { + assert!(value <= 1.0); + Self { value: (value * 255.0) as u8 } + } +} + +impl From for ColorValue { + fn from(value: f64) -> Self { + assert!(value <= 1.0); + Self { value: (value * 255.0) as u8 } + } +} + +pub(super) struct Pixel { + r: ColorValue, + g: ColorValue, + b: ColorValue, + a: ColorValue, +} + +impl Pixel { + pub(super) fn new>(r: T, g: T, b: T, a: T) -> Self { + Self { + r: r.into(), + g: g.into(), + b: b.into(), + a: a.into(), + } + } + + pub(super) fn as_u32(&self, encoding: PixelEncoding) -> u32 { + match encoding { + PixelEncoding::ARGB => { + let color = [self.a.value, self.r.value, self.g.value, self.b.value]; + u32::from_be_bytes(color) + }, + PixelEncoding::RGBA => { + let color = [self.r.value, self.g.value, self.b.value, self.a.value]; + u32::from_be_bytes(color) + }, + } + } +} + +#[cfg(test)] +mod tests { + use super::{ColorValue, Pixel, PixelEncoding}; + + #[test] + fn test_max_u8() { + assert_eq!(ColorValue::from(1.0).value, 255); + } + + #[test] + fn test_min_u8() { + assert_eq!(ColorValue::from(0.0).value, 0); + } + + #[test] + fn test_argb_u32() { + let pixel = Pixel::new(0x00, 0x00, 0x00, 0x00); + assert_eq!(pixel.as_u32(PixelEncoding::ARGB), 0x00); + + let pixel = Pixel::new(0xff, 0xff, 0xff, 0xff); + assert_eq!(pixel.as_u32(PixelEncoding::ARGB), u32::MAX); + + let pixel = Pixel::new(0xaa, 0xaa, 0xaa, 0xaa); + assert_eq!(pixel.as_u32(PixelEncoding::ARGB), 0xaaaaaaaa); + + let pixel = Pixel::new(0xfd, 0xcb, 0xa9, 0x87); + println!("{:x?}", pixel.as_u32(PixelEncoding::ARGB)); + assert_eq!(pixel.as_u32(PixelEncoding::ARGB), 0x87fdcba9); + } + + #[test] + fn test_rgba_u32() { + let pixel = Pixel::new(0x00, 0x00, 0x00, 0x00); + assert_eq!(pixel.as_u32(PixelEncoding::RGBA), 0x00); + + let pixel = Pixel::new(0xff, 0xff, 0xff, 0xff); + assert_eq!(pixel.as_u32(PixelEncoding::RGBA), u32::MAX); + + let pixel = Pixel::new(0xaa, 0xaa, 0xaa, 0xaa); + assert_eq!(pixel.as_u32(PixelEncoding::RGBA), 0xaaaaaaaa); + + let pixel = Pixel::new(0xfd, 0xcb, 0xa9, 0x87); + println!("{:x?}", pixel.as_u32(PixelEncoding::RGBA)); + assert_eq!(pixel.as_u32(PixelEncoding::RGBA), 0xfdcba987); + } +} diff --git a/src/wayland/ui.rs b/src/wayland/ui.rs new file mode 100644 index 0000000..e69de29