From 859c2f41544de45ee1699e5d105c10cff24a6ec1 Mon Sep 17 00:00:00 2001 From: Avii Date: Fri, 7 Aug 2026 21:36:53 +0200 Subject: [PATCH] initial commit --- src/rgba.rs | 125 -------------------------------------------------- src/screen.rs | 88 ----------------------------------- 2 files changed, 213 deletions(-) delete mode 100644 src/rgba.rs delete mode 100644 src/screen.rs diff --git a/src/rgba.rs b/src/rgba.rs deleted file mode 100644 index 2ba1669..0000000 --- a/src/rgba.rs +++ /dev/null @@ -1,125 +0,0 @@ -use embedded_graphics_core::pixelcolor::*; -use linfb::shape::Color; -use slint::platform::software_renderer::TargetPixel; - -/// Simple RGBA color wrapper. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Rgba(C, u8); - -#[allow(unused)] -#[inline(always)] -fn mul_blend_u8(delta: u32, a: u32) -> u32 { - // Exact (delta * a) / 255 using the div255 trick (no slow integer division). - // Valid for 0..=65535 inputs; see Hacker's Delight 10-16. - let t = delta * a + 128; - (t + (t >> 8)) >> 8 -} - -#[allow(unused)] -impl Rgba { - /// Create a new RGBA color. - pub const fn new(color: C, alpha: u8) -> Self { - Self(color, alpha) - } - - /// Get the color component. - pub const fn rgb(&self) -> C { - self.0 - } - - pub fn r(&self) -> u8 { - self.0.r() - } - - pub fn g(&self) -> u8 { - self.0.g() - } - - pub fn b(&self) -> u8 { - self.0.b() - } - - /// Get the alpha component (0..=255). - pub const fn a(&self) -> u8 { - self.1 - } -} - -impl PixelColor for Rgba { - type Raw = C::Raw; -} - -#[allow(unused)] -pub trait Blend { - fn blend(&self, bg: T) -> T; -} - -impl Blend for Rgba { - #[inline(always)] - fn blend(&self, bg: Rgb888) -> Rgb888 { - let a = self.a() as u32; - if a == 0 { - return bg; - } - if a == 255 { - return self.rgb(); - } - - let fr = self.rgb().r() as u32; - let fg = self.rgb().g() as u32; - let fb = self.rgb().b() as u32; - - let br = bg.r() as u32; - let bgc = bg.g() as u32; - let bb = bg.b() as u32; - - let r = (br + mul_blend_u8(fr.wrapping_sub(br), a)) as u8; - let g = (bgc + mul_blend_u8(fg.wrapping_sub(bgc), a)) as u8; - let b = (bb + mul_blend_u8(fb.wrapping_sub(bb), a)) as u8; - - Rgb888::new(r, g, b) - } -} - -impl TargetPixel for Rgba { - fn blend(&mut self, color: slint::platform::software_renderer::PremultipliedRgbaColor) { - let a = self.a() as u32; - // if a == 0 { - // return bg; - // } - // if a == 255 { - // return self.rgb(); - // } - - let fr = self.rgb().r() as u32; - let fg = self.rgb().g() as u32; - let fb = self.rgb().b() as u32; - - let br = color.red as u32; - let bgc = color.green as u32; - let bb = color.blue as u32; - - let r = (br + mul_blend_u8(fr.wrapping_sub(br), a)) as u8; - let g = (bgc + mul_blend_u8(fg.wrapping_sub(bgc), a)) as u8; - let b = (bb + mul_blend_u8(fb.wrapping_sub(bb), a)) as u8; - - self.0 = Rgb888::new(r, g, b); - self.1 = a as u8; - } - - fn from_rgb(red: u8, green: u8, blue: u8) -> Self { - Self(Rgb888::new(red, green, blue), 255) - } -} - -impl From<(u8, u8, u8, u8)> for Rgba { - fn from(value: (u8, u8, u8, u8)) -> Self { - Self(Rgb888::new(value.0, value.1, value.2), value.3) - } -} - -impl From for Rgba { - fn from(value: Color) -> Self { - Self(Rgb888::new(value.red, value.green, value.blue), value.alpha) - } -} diff --git a/src/screen.rs b/src/screen.rs deleted file mode 100644 index 60f8f23..0000000 --- a/src/screen.rs +++ /dev/null @@ -1,88 +0,0 @@ -use embedded_graphics::{pixelcolor::Rgb888, prelude::*, primitives::Rectangle}; -use linfb::{Framebuffer, shape::Color}; - -pub struct Screen { - pub framebuffer: Framebuffer, - width: u32, - height: u32, -} - -#[allow(unused)] -impl Screen { - pub fn new() -> Self { - let framebuffer = Framebuffer::open().expect("Failed to open framebuffer"); - - let width = framebuffer.screen_info.xres; - let height = framebuffer.screen_info.yres; - - Self { - framebuffer, - width, - height, - } - } - - pub fn flush(&mut self) { - self.framebuffer.flush(); - } - - #[inline] - pub fn width(&self) -> u32 { - self.width - } - - #[inline] - pub fn height(&self) -> u32 { - self.height - } - - #[inline] - pub fn rect(&self) -> Rectangle { - Rectangle { - top_left: Point::new(0, 0), - size: Size { - width: self.width(), - height: self.height(), - }, - } - } -} - -impl Dimensions for Screen { - fn bounding_box(&self) -> Rectangle { - Rectangle::new(Point::new(0, 0), Size::new(self.width, self.height)) - } -} - -impl DrawTarget for Screen { - type Color = Rgba; - type Error = Box; - - fn draw_iter(&mut self, pixels: I) -> Result<(), Self::Error> - where - I: IntoIterator>, - { - let width = self.bounding_box().size.width; - let height = self.bounding_box().size.height; - - for p in pixels { - let x = p.0.x as u32; - let y = p.0.y as u32; - - let color = Color { - red: p.1.r(), - green: p.1.g(), - blue: p.1.b(), - alpha: p.1.a(), - }; - - if !(0..width).contains(&x) || !(0..height).contains(&y) { - continue; - } - - self.framebuffer.set_pixel(x, y, color); - } - - Ok(()) - } -}