From e6a712bb96282c62923af8033f609e47a3aa85a5 Mon Sep 17 00:00:00 2001 From: Sofia Date: Sat, 13 Sep 2025 14:10:05 +0300 Subject: [PATCH] Update API again, rename QOI to graphics --- src/display.rs | 12 +++--- src/{qoi.rs => graphics.rs} | 75 ++++++++++++++++++++++--------------- src/main.rs | 10 ++--- 3 files changed, 56 insertions(+), 41 deletions(-) rename src/{qoi.rs => graphics.rs} (84%) diff --git a/src/display.rs b/src/display.rs index c187808..ce852e1 100644 --- a/src/display.rs +++ b/src/display.rs @@ -61,7 +61,7 @@ impl Mul for Rgb565 { } #[derive(Default, Clone, Copy)] -pub struct Position { +pub struct Vec2 { pub x: u16, pub y: u16, } @@ -179,8 +179,8 @@ impl Display { self.set_inversion(true); self.write(Writeable::Command(Command::NoRon)); self.draw_rect( - Position { x: 0, y: 0 }, - Position { x: 240, y: 240 }, + Vec2 { x: 0, y: 0 }, + Vec2 { x: 240, y: 240 }, Color::default(), ); @@ -246,18 +246,18 @@ impl Display { self.write(Writeable::Data(&[start1, start2, end1, end2])); } - pub fn set_window(&mut self, pos0: Position, pos1: Position) { + pub fn set_window(&mut self, pos0: Vec2, pos1: Vec2) { self.set_columns(pos0.x, pos1.x); self.set_rows(pos0.y, pos1.y); self.write(Writeable::Command(Command::RamWR)); } - pub fn pixel(&mut self, position: Position, color: Color) { + pub fn pixel(&mut self, position: Vec2, color: Color) { self.set_window(position, position); self.write(Writeable::Data(&color.bytes)); } - pub fn draw_rect(&mut self, pos0: Position, pos1: Position, color: Color) { + pub fn draw_rect(&mut self, pos0: Vec2, pos1: Vec2, color: Color) { self.set_window(pos0, pos1); self.dc.set_high(); let width = pos1.x - pos0.x; diff --git a/src/qoi.rs b/src/graphics.rs similarity index 84% rename from src/qoi.rs rename to src/graphics.rs index 1f2dc53..4730323 100644 --- a/src/qoi.rs +++ b/src/graphics.rs @@ -10,7 +10,7 @@ use ufmt::derive::uDebug; use crate::{ CoreClock, - display::{Display, Position, Rgb565, Writeable}, + display::{Display, Rgb565, Vec2, Writeable}, }; // https://qoiformat.org/qoi-specification.pdf @@ -79,7 +79,7 @@ pub fn draw_image( serial: &mut Usart, Pin, CoreClock>, iter: &mut dyn Iterator, display: &mut Display, - position: Position, + position: Vec2, ) -> Result<(), QoiErr> { if let (Some(113), Some(111), Some(105), Some(102)) = (iter.next(), iter.next(), iter.next(), iter.next()) @@ -98,40 +98,55 @@ pub fn draw_image( let scale_factor = 240 / width; - display.set_window( + let mut qoi_iter = QoiIterator::from(iter, width * height); + draw_stream( + &mut qoi_iter, + display, position, - Position { - x: position.x + (width * scale_factor) - 1, - y: position.y + (height * scale_factor) - 1, + Vec2 { + x: width, + y: height, }, - ); - - let qoi_iter = QoiIterator::from(iter, width * height); - let scale_iter = ScaleIterator { - qoi: qoi_iter, - last_row: [Rgb565::yellow(); 120], - scale_factor: scale_factor as usize, - width: width as usize, - counter: 0, - index: 0, - }; - - let mut counter = 0u32; - for pixel in scale_iter { - let [c1, c2] = pixel.as_color().bytes; - display.write(Writeable::Data(&[c1, c2])); - counter += 1; - } - ufmt::uwriteln!(serial, "Counter: {}", counter).unwrap(); - - Ok(()) + scale_factor, + ) } else { Err(QoiErr::InvalidMagicNumber) } } +pub fn draw_stream( + stream: &mut dyn Iterator, + display: &mut Display, + position: Vec2, + scale: Vec2, + scale_factor: u16, +) -> Result<(), QoiErr> { + let scale_iter = ScaleIterator { + iter: stream, + last_row: [Rgb565::yellow(); 120], + scale_factor: scale_factor as usize, + width: scale.x as usize, + counter: 0, + index: 0, + }; + + display.set_window( + position, + Vec2 { + x: position.x + (scale.x * scale_factor) - 1, + y: position.y + (scale.y * scale_factor) - 1, + }, + ); + + for pixel in scale_iter { + let [c1, c2] = pixel.as_color().bytes; + display.write(Writeable::Data(&[c1, c2])); + } + + Ok(()) +} struct ScaleIterator<'a> { - qoi: QoiIterator<'a>, + iter: &'a mut dyn Iterator, last_row: [Rgb565; 120], width: usize, scale_factor: usize, @@ -144,7 +159,7 @@ impl<'a> Iterator for ScaleIterator<'a> { fn next(&mut self) -> Option { if self.scale_factor == 1 { - return self.qoi.next(); + return self.iter.next(); } if self.index >= self.width * self.scale_factor { @@ -155,7 +170,7 @@ impl<'a> Iterator for ScaleIterator<'a> { if self.counter % self.scale_factor == 0 { if (self.index % self.scale_factor) == 0 { - if let Some(pixel) = self.qoi.next() { + if let Some(pixel) = self.iter.next() { self.last_row[index_div] = pixel; self.index += 1; Some(pixel) diff --git a/src/main.rs b/src/main.rs index bc27706..e4f234f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,15 +18,15 @@ use atmega_hal::{ use panic_halt as _; use crate::{ - display::{Display, Position}, + display::{Display, Vec2}, + graphics::{Image, LARGE_CAT_UNSAFE, PRESS_BTN_UNSAFE, draw_image}, peripherals::Button, - qoi::{Image, LARGE_CAT_UNSAFE, PRESS_BTN_UNSAFE, draw_image}, }; mod display; mod font; +mod graphics; mod peripherals; -mod qoi; type CoreClock = atmega_hal::clock::MHz8; @@ -85,7 +85,7 @@ fn main() -> ! { &mut serial, &mut Image::from(addr_of!(PRESS_BTN_UNSAFE)).iter(), &mut display, - Position { x: 0, y: 0 }, + Vec2 { x: 0, y: 0 }, ) { Ok(_) => ufmt::uwriteln!(serial, "Successfully read QOI").unwrap(), Err(e) => ufmt::uwriteln!(serial, "Error: {:?}", e).unwrap(), @@ -97,7 +97,7 @@ fn main() -> ! { &mut serial, &mut images[idx].iter(), &mut display, - Position { x: 0, y: 0 }, + Vec2 { x: 0, y: 0 }, ) { Ok(_) => ufmt::uwriteln!(serial, "Successfully read QOI").unwrap(), Err(e) => ufmt::uwriteln!(serial, "Error: {:?}", e).unwrap(),