Merge pull request #190 from nworbnhoj/main

fix gam::Tile render RHS
This commit is contained in:
bunnie 2022-06-17 21:40:28 +08:00 committed by GitHub
commit d829839c28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 62 deletions

View file

@ -235,8 +235,8 @@ impl From<[Option<Tile>; 6]> for Bitmap {
} }
} }
impl From<Img> for Bitmap { impl From<&Img> for Bitmap {
fn from(image: Img) -> Self { fn from(image: &Img) -> Self {
let (bm_width, bm_height, _) = image.size(); let (bm_width, bm_height, _) = image.size();
let bm_bottom = bm_height - 1; let bm_bottom = bm_height - 1;
let bm_right = bm_width - 1; let bm_right = bm_width - 1;
@ -251,7 +251,7 @@ impl From<Img> for Bitmap {
}; };
let mut mosaic: Vec<Tile> = Vec::new(); let mut mosaic: Vec<Tile> = Vec::new();
let pixels = Dither::new(BURKES.to_vec()).dither(image); let pixels = Dither::new(BURKES.to_vec()).dither(&image);
let mut px_index = 0; let mut px_index = 0;
let bits_per_word: i16 = BITS_PER_WORD.try_into().unwrap(); let bits_per_word: i16 = BITS_PER_WORD.try_into().unwrap();
for t in 0..tile_count { for t in 0..tile_count {
@ -456,7 +456,7 @@ impl Dither {
PixelColor::Light PixelColor::Light
} }
} }
pub fn dither(&mut self, image: Img) -> Vec<PixelColor> { pub fn dither(&mut self, image: &Img) -> Vec<PixelColor> {
let (width, height, length) = image.size(); let (width, height, length) = image.size();
self.provision(width.try_into().unwrap()); self.provision(width.try_into().unwrap());
let mut pixels: Vec<PixelColor> = Vec::with_capacity(length); let mut pixels: Vec<PixelColor> = Vec::with_capacity(length);

View file

@ -1,6 +1,4 @@
use crate::api::{ use crate::api::{Circle, DrawStyle, Line, Pixel, PixelColor, Point, Rectangle, RoundedRectangle};
Circle, DrawStyle, Line, Pixel, PixelColor, Point, Rectangle, RoundedRectangle,
};
/// LCD Frame buffer bounds /// LCD Frame buffer bounds
pub const LCD_WORDS_PER_LINE: usize = 11; pub const LCD_WORDS_PER_LINE: usize = 11;
@ -593,46 +591,47 @@ pub fn rounded_rectangle(fb: &mut LcdFB, rr: RoundedRectangle, clip: Option<Rect
use crate::api::Tile; use crate::api::Tile;
#[cfg(feature = "ditherpunk")] #[cfg(feature = "ditherpunk")]
pub fn tile(fb: &mut LcdFB, tile: Tile, clip: Option<Rectangle>) { pub fn tile(fb: &mut LcdFB, tile: Tile, clip: Option<Rectangle>) {
use std::cmp::min; use std::cmp::{max, min};
// prepare the clip boundries: min (LCD, clip, tile) let fb_size: Point = Point::new((LCD_PX_PER_LINE - 1) as i16, (LCD_LINES - 1) as i16);
let bound = tile.bound(); const FB_WORDS_PER_LINE: usize = LCD_WORDS_PER_LINE;
let tl_x = bound.tl.x as usize; let clip = match clip {
let tl_y = bound.tl.y as usize; // alter clip to work around an odd use (bug?) of the clip origin
log::info!("bound: {:?}, clip: {:?}", bound, clip); // clip.tl appears to relate to the the usable screen area as origin
let (mut br_x, mut br_y) = match clip { // clip.br appears to relate to the clip.tl as origin
Some(clip) => { Some(clip) => Rectangle::new(Point::new(0, 0), clip.br),
let x = min(bound.br.x, clip.br.x) as usize; // default to the LcdFB dimensions
let y = min(bound.br.y, clip.br.y) as usize; None => Rectangle::new(Point::new(0, 0), fb_size),
(x, y)
}
None => (bound.br.x as usize, bound.br.y as usize),
}; };
br_x = min(br_x, LCD_PX_PER_LINE - 1); // determine the sub-region of the tile to transfer to the fb (in Tile coords)
br_y = min(br_y, LCD_LINES - 1); let (tl_x, tl_y, br_x, br_y): (usize, usize, usize, usize) = (
max(tile.bound().tl.x, clip.tl.x) as usize,
max(tile.bound().tl.y, clip.tl.y) as usize,
min(tile.bound().br.x, clip.br.x) as usize,
min(tile.bound().br.y, clip.br.y) as usize,
);
// prepare to cut and mask misaligned words // prepare to cut and mask misaligned words
let r_cut = tl_x % 32; let r_cut = tl_x % 32;
let l_cut = 32 - r_cut; let l_cut = 32 - r_cut;
let mask = (1 << r_cut) - 1; let mask = (1 << r_cut) - 1;
// prepare to clip a word spanning br_y // prepare to clip a word spanning br_x
let c_clip = br_x % 32; let c_clip = br_x % 32;
let c_mask = (1 << c_clip) - 1; let c_mask = (1 << c_clip) - 1;
let first_word = tl_x / 32; let first_wd = tl_x / 32;
let last_word = LCD_WORDS_PER_LINE - 1; let clip_wd = br_x / 32;
log::info!("tl_y {}, br_y {}, clip: {:?}", tl_y, br_y, clip); let last_wd = min(clip_wd, FB_WORDS_PER_LINE - 1);
for tile_ln in tl_y..=br_y { for tile_ln in tl_y..=br_y {
let fb_ln = tile_ln * LCD_WORDS_PER_LINE; let fb_ln = tile_ln * FB_WORDS_PER_LINE;
let tile_line = tile.get_line(Point::new(tl_x as i16, tile_ln as i16)); let tile_line = tile.get_line(Point::new(tl_x as i16, tile_ln as i16));
// log::info!("tile_line: {:x?}", tile_line); let mut fb_wd = first_wd;
let mut fb_wd = first_word;
let mut tile_wd = 0; let mut tile_wd = 0;
let mut fb_word = fb[fb_ln + fb_wd]; let mut fb_word = fb[fb_ln + fb_wd];
let mut left_part = fb_word & mask; let mut left_part = fb_word & mask;
let mut right_part; let mut right_part;
let mut tile_word: u32; let mut tile_word: u32;
while (fb_wd <= last_word) & (tile_wd < tile_line.len()) { while (fb_wd <= last_wd) & (tile_wd < tile_line.len()) {
fb_word = fb[fb_ln + fb_wd]; fb_word = fb[fb_ln + fb_wd];
tile_word = tile_line[tile_wd]; tile_word = tile_line[tile_wd];
fb[fb_ln + fb_wd] = match r_cut { fb[fb_ln + fb_wd] = match r_cut {
@ -648,12 +647,20 @@ pub fn tile(fb: &mut LcdFB, tile: Tile, clip: Option<Rectangle>) {
fb_wd += 1; fb_wd += 1;
tile_wd += 1; tile_wd += 1;
} }
// clip the last word in the line (ie word containing br_x) // complete the right side of the tile
left_part = fb[fb_ln + last_word] & c_mask; if fb_wd > last_wd {
// encountered clip before end of tile
left_part = fb[fb_ln + fb_wd - 1] & c_mask;
right_part = fb_word & !c_mask; right_part = fb_word & !c_mask;
fb[fb_ln + last_word] = left_part | right_part; fb[fb_ln + fb_wd - 1] = left_part | right_part;
} else {
// encountered end of tile before clip
left_part = left_part & c_mask;
right_part = fb[fb_ln + fb_wd] & !c_mask;
fb[fb_ln + fb_wd] = left_part | right_part;
}
// set the dirty bit on the line // set the dirty bit on the line
fb[fb_ln + (LCD_WORDS_PER_LINE - 1)] |= 0x1_0000; fb[fb_ln + (FB_WORDS_PER_LINE - 1)] |= 0x1_0000;
} }
} }

View file

@ -195,58 +195,49 @@ impl Modals {
} }
#[cfg(feature = "ditherpunk")] #[cfg(feature = "ditherpunk")]
const MODAL_WIDTH: u32 = 300; const MODAL_WIDTH: u32 = 296;
#[allow(dead_code)] #[allow(dead_code)]
#[cfg(feature = "ditherpunk")] #[cfg(feature = "ditherpunk")]
const MODAL_HEIGHT: u32 = 370; const MODAL_HEIGHT: u32 = 300;
/// this blocks until the image has been dismissed. /// this blocks until the image has been dismissed.
#[cfg(feature = "ditherpunk")] #[cfg(feature = "ditherpunk")]
pub fn show_image(&self, img: &Img) -> Result<(), xous::Error> { pub fn show_image(&self, img: &Img) -> Result<(), xous::Error> {
self.lock(); self.lock();
// resize and/or rotate // resize and/or rotate
/*
let (modal_width, modal_height) = (Modals::MODAL_WIDTH as f32, Modals::MODAL_HEIGHT as f32); let (modal_width, modal_height) = (Modals::MODAL_WIDTH as f32, Modals::MODAL_HEIGHT as f32);
let (w, h, _) = img.size(); let (w, h, _) = img.size();
let (img_width, img_height) = (w as f32, h as f32); let (img_width, img_height) = (w as f32, h as f32);
let portrait_scale = (modal_width / img_width).min(modal_height / img_height); let portrait_scale = (modal_width / img_width).min(modal_height / img_height);
let landscape_scale = (modal_width / img_height).min(modal_height / img_width); let landscape_scale = (modal_width / img_height).min(modal_height / img_width);
let mut rotate = false; let mut bm = if portrait_scale >= 1.0 {
let modal_img; Bitmap::from(img)
if portrait_scale >= 1.0 {
modal_img = img.clone();
} else if landscape_scale >= 1.0 { } else if landscape_scale >= 1.0 {
modal_img = img.clone(); Bitmap::from(img).rotate90()
rotate = true;
} else if portrait_scale >= landscape_scale { } else if portrait_scale >= landscape_scale {
modal_img = Modals::resize_image(img.clone(), portrait_scale) let resized = Modals::resize_image(img, portrait_scale);
Bitmap::from(&resized)
} else { } else {
rotate = true; let resized = Modals::resize_image(img, landscape_scale);
modal_img = Modals::resize_image(img.clone(), landscape_scale) Bitmap::from(&resized).rotate90()
}; };
let mut bm = Bitmap::from(modal_img);
bm = if rotate { bm.rotate90() } else { bm };
let (bm_width, bm_height) = bm.size(); let (bm_width, bm_height) = bm.size();
let (bm_width, bm_height) = (bm_width as u32, bm_height as u32); let (bm_width, bm_height) = (bm_width as u32, bm_height as u32);
*/
let bm = Bitmap::from(img.clone());
// center image in modal // center image in modal
/*
let center = Point::new( let center = Point::new(
((Modals::MODAL_WIDTH - bm_width) / 2).try_into().unwrap(), ((Modals::MODAL_WIDTH - bm_width) / 2).try_into().unwrap(),
((Modals::MODAL_HEIGHT - bm_height) / 2).try_into().unwrap(), ((Modals::MODAL_HEIGHT - bm_height) / 2).try_into().unwrap(),
);*/ );
bm.translate(center);
let mut tiles: [Option<Tile>; 6] = [None; 6]; let mut tiles: [Option<Tile>; 6] = [None; 6];
for (t, tile) in bm.iter().enumerate() { for (t, tile) in bm.iter().enumerate() {
if t >= tiles.len() { if t >= tiles.len() {
continue; continue;
} }
let copy = tile.clone(); tiles[t] = Some(*tile);
//copy.translate(center);
tiles[t] = Some(copy);
} }
let spec = ManagedImage { let spec = ManagedImage {
@ -262,7 +253,7 @@ impl Modals {
#[allow(dead_code)] #[allow(dead_code)]
#[cfg(feature = "ditherpunk")] #[cfg(feature = "ditherpunk")]
fn resize_image(img: Img, scale: f32) -> Img { fn resize_image(img: &Img, scale: f32) -> Img {
let (w, h, _) = img.size(); let (w, h, _) = img.size();
let (img_width, img_height) = (w as f32, h as f32); let (img_width, img_height) = (w as f32, h as f32);
let height: u32 = (scale * img_height).floor() as u32; let height: u32 = (scale * img_height).floor() as u32;

View file

@ -173,9 +173,9 @@ pub fn spawn_test() {
// https://sequelaencollection.home.blog/2d-chaotic-attractors/ // https://sequelaencollection.home.blog/2d-chaotic-attractors/
#[cfg(feature = "ditherpunk")] #[cfg(feature = "ditherpunk")]
fn clifford() -> Img { fn clifford() -> Img {
const SIZE: u32 = Modals::MODAL_WIDTH; const SIZE: u32 = Modals::MODAL_WIDTH - 10;
const CENTER: f32 = (SIZE / 2) as f32; const CENTER: f32 = (SIZE / 2) as f32;
const SCALE: f32 = 70.0; const SCALE: f32 = 60.0;
let mut buf = vec![255u8; (SIZE * SIZE).try_into().unwrap()]; let mut buf = vec![255u8; (SIZE * SIZE).try_into().unwrap()];
let (a, b, c, d) = (-2.0, -2.4, 1.1, -0.9); let (a, b, c, d) = (-2.0, -2.4, 1.1, -0.9);
let (mut x, mut y): (f32, f32) = (0.0, 0.0); let (mut x, mut y): (f32, f32) = (0.0, 0.0);