Added easier way to set colors + tests
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1690,6 +1690,7 @@ dependencies = [
|
|||||||
"rten",
|
"rten",
|
||||||
"rten-tensor",
|
"rten-tensor",
|
||||||
"smithay-client-toolkit",
|
"smithay-client-toolkit",
|
||||||
|
"wayland-client",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -13,3 +13,4 @@ ocrs = "0.4.*"
|
|||||||
image = "0.24.9"
|
image = "0.24.9"
|
||||||
anyhow = "1.0.81"
|
anyhow = "1.0.81"
|
||||||
smithay-client-toolkit = "0.18.1"
|
smithay-client-toolkit = "0.18.1"
|
||||||
|
wayland-client = "0.31.2"
|
||||||
|
|||||||
@@ -1,8 +1 @@
|
|||||||
pub mod process_point;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Point {
|
|
||||||
pub x: f64,
|
|
||||||
pub y: f64,
|
|
||||||
pub new_line: bool,
|
|
||||||
}
|
|
||||||
|
|||||||
18
src/main.rs
18
src/main.rs
@@ -3,8 +3,15 @@ use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt, OrientableExt, WidgetExt, Ge
|
|||||||
use gtk::cairo::{Context, Operator};
|
use gtk::cairo::{Context, Operator};
|
||||||
use relm4::drawing::DrawHandler;
|
use relm4::drawing::DrawHandler;
|
||||||
|
|
||||||
use waywrite::process_point::print_words;
|
use std::error::Error;
|
||||||
use waywrite::Point;
|
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
|
||||||
|
mod wayland;
|
||||||
|
mod process_point;
|
||||||
|
|
||||||
|
use process_point::{Point, print_words};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum AppInput {
|
enum AppInput {
|
||||||
@@ -148,7 +155,8 @@ fn draw(cx: &Context, points: &[Point]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let app = RelmApp::new("simmer505.waywrite");
|
|
||||||
app.run::<AppModel>(0);
|
wayland::run()?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ use rten::Model;
|
|||||||
use ocrs::{OcrEngine, OcrEngineParams};
|
use ocrs::{OcrEngine, OcrEngineParams};
|
||||||
use image::{ColorType, ImageFormat};
|
use image::{ColorType, ImageFormat};
|
||||||
|
|
||||||
use crate::Point;
|
|
||||||
|
|
||||||
const MATRIX_LEN: usize = 800;
|
const MATRIX_LEN: usize = 800;
|
||||||
const TEXT_MATRIX_RATIO: f64 = 0.5;
|
const TEXT_MATRIX_RATIO: f64 = 0.5;
|
||||||
const LINE_WIDTH: f64 = 10.0;
|
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 TEXT_X_OFFSET: f64 = (MATRIX_X_SIZE - TEXT_X_SIZE) / 2.0;
|
||||||
const LINE_WIDTH_X_OFFSET: f64 = LINE_WIDTH / 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<Point>) -> Result<(), Box<dyn Error>> {
|
pub fn print_words(points: &Vec<Point>) -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
let begin = Instant::now();
|
let begin = Instant::now();
|
||||||
|
|||||||
@@ -13,8 +13,7 @@ use smithay_client_toolkit::{
|
|||||||
},
|
},
|
||||||
WaylandSurface,
|
WaylandSurface,
|
||||||
},
|
},
|
||||||
shm::{
|
shm::{ slot::{SlotPool, Buffer},
|
||||||
slot::{SlotPool, Buffer},
|
|
||||||
Shm, ShmHandler
|
Shm, ShmHandler
|
||||||
},
|
},
|
||||||
delegate_registry, delegate_compositor, delegate_seat, delegate_output,
|
delegate_registry, delegate_compositor, delegate_seat, delegate_output,
|
||||||
@@ -31,10 +30,15 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
|
mod ui;
|
||||||
|
|
||||||
|
use ui::{Pixel, PixelEncoding};
|
||||||
|
|
||||||
const WINDOW_HEIGHT: u32 = 256;
|
const WINDOW_HEIGHT: u32 = 256;
|
||||||
const WINDOW_WIDTH: u32 = 512;
|
const WINDOW_WIDTH: u32 = 512;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
|
||||||
|
pub(super) fn run() -> Result<()> {
|
||||||
let conn = Connection::connect_to_env()?;
|
let conn = Connection::connect_to_env()?;
|
||||||
|
|
||||||
let (globals, event_queue) = registry_queue_init(&conn)?;
|
let (globals, event_queue) = registry_queue_init(&conn)?;
|
||||||
@@ -249,10 +253,13 @@ impl SimpleWindow {
|
|||||||
|
|
||||||
for pix in canvas.chunks_exact_mut(4) {
|
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 array: &mut [u8; 4] = pix.try_into().unwrap();
|
let pix: &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);
|
self.window.wl_surface().damage_buffer(0, 0, self.width as i32, self.height as i32);
|
||||||
|
|||||||
0
src/wayland/app.rs
Normal file
0
src/wayland/app.rs
Normal file
113
src/wayland/pixel.rs
Normal file
113
src/wayland/pixel.rs
Normal file
@@ -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<u8> for ColorValue {
|
||||||
|
fn from(value: u8) -> Self {
|
||||||
|
Self { value }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<f32> for ColorValue {
|
||||||
|
fn from(value: f32) -> Self {
|
||||||
|
assert!(value <= 1.0);
|
||||||
|
Self { value: (value * 255.0) as u8 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<f64> 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<T: Into<ColorValue>>(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
0
src/wayland/ui.rs
Normal file
0
src/wayland/ui.rs
Normal file
Reference in New Issue
Block a user