implement gam bitmap.rotate90()

This commit is contained in:
nworbnhoj 2022-06-02 14:14:50 +10:00
parent b1ff16dee7
commit beb2778ce6
2 changed files with 74 additions and 24 deletions

View file

@ -82,8 +82,7 @@ impl Bitmap {
(x * y) as u32
}
#[allow(dead_code)]
fn size(&self) -> (usize, usize) {
pub fn size(&self) -> (usize, usize) {
(self.bound.br.x as usize, self.bound.br.y as usize)
}
@ -137,11 +136,14 @@ impl Bitmap {
self.get_tile(point).get_line(point)
}
#[allow(dead_code)]
fn get_word(&self, point: Point) -> Word {
self.get_tile(point).get_word(point)
}
fn set_word(&mut self, point: Point, word: Word) {
self.get_mut_tile(point).set_word(point, word);
}
pub fn get_pixel(&self, point: Point) -> PixelColor {
self.get_tile(point).get_pixel(point)
}
@ -155,6 +157,53 @@ impl Bitmap {
tile.translate(offset);
}
}
pub fn rotate90(&mut self) -> Self {
let bits_per_word: i16 = BITS_PER_WORD.try_into().unwrap();
let (size_x, size_y) = self.size();
let size_x: i16 = size_x.try_into().unwrap();
let size_y: i16 = size_y.try_into().unwrap();
let mut r90 = Bitmap::new(Point::new(size_y, size_x));
let (_, r90_size_y) = r90.size();
let mut x: i16 = 0;
let mut r90_y = 0;
let mut block: [Word; BITS_PER_WORD] = [0; BITS_PER_WORD];
while x < size_x {
let mut y = size_y - 1;
let mut r90_x = 0;
// extract a square block of bits - ie 32 x u32 words
// beginning from bottom-left, and progressing up in strips, from left to right
while y >= 0 {
let mut b = 0;
while b < block.len() {
block[b] = if y >= 0 {
self.get_word(Point::new(x, y))
} else {
0
};
y -= 1;
b += 1;
}
// rotate the block and write to r90
// beginning from top-left, and progressing right in strips, from top to bottom
for w in 0..bits_per_word {
if r90_y + w >= r90_size_y.try_into().unwrap() {
continue;
}
let mut word: Word = 0;
for b in 0..block.len() {
word = word | (((block[b] >> w) & 1) << b);
}
r90.set_word(Point::new(r90_x, r90_y + w), word);
}
r90_x = r90_x + bits_per_word;
}
x = x + bits_per_word;
r90_y = r90_y + bits_per_word;
}
r90
}
}
impl Deref for Bitmap {

View file

@ -6,18 +6,18 @@ pub mod tests;
use bit_field::BitField;
use core::cell::Cell;
#[cfg(feature = "ditherpunk")]
use fast_image_resize as fir;
use gam::*;
#[cfg(feature = "ditherpunk")]
use image::RgbImage;
use num_traits::*;
#[cfg(feature = "ditherpunk")]
use std::convert::TryInto;
#[cfg(feature = "ditherpunk")]
use std::num::NonZeroU32;
use xous::{send_message, Message, CID};
use xous_ipc::Buffer;
#[cfg(feature="ditherpunk")]
use fast_image_resize as fir;
#[cfg(feature="ditherpunk")]
use image::{imageops, RgbImage};
#[cfg(feature="ditherpunk")]
use std::convert::TryInto;
#[cfg(feature="ditherpunk")]
use std::num::NonZeroU32;
pub type TextValidationFn = fn(TextEntryPayload) -> Option<ValidatorErr>;
@ -198,12 +198,12 @@ impl Modals {
Ok(())
}
#[cfg(feature="ditherpunk")]
#[cfg(feature = "ditherpunk")]
const MODAL_WIDTH: u32 = 300;
#[cfg(feature="ditherpunk")]
#[cfg(feature = "ditherpunk")]
const MODAL_HEIGHT: u32 = 370;
/// this blocks until the image has been dismissed.
#[cfg(feature="ditherpunk")]
#[cfg(feature = "ditherpunk")]
pub fn show_image(&self, img: &RgbImage) -> Result<(), xous::Error> {
self.lock();
@ -214,28 +214,29 @@ impl Modals {
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 mut modal_img: RgbImage = img.clone();
let mut rotate = false;
if portrait_scale >= 1.0 {
//noop
} else if landscape_scale >= 1.0 {
modal_img = imageops::rotate90(&modal_img);
rotate = true;
} else if portrait_scale >= landscape_scale {
modal_img = Modals::resize_image(modal_img, portrait_scale);
} else {
modal_img = Modals::resize_image(modal_img, landscape_scale);
modal_img = imageops::rotate90(&modal_img);
rotate = true;
}
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_width as u32, bm_height as u32);
// center image in modal
let center = Point::new(
((Modals::MODAL_WIDTH - modal_img.width()) / 2)
.try_into()
.unwrap(),
((Modals::MODAL_HEIGHT - modal_img.height()) / 2)
.try_into()
.unwrap(),
((Modals::MODAL_WIDTH - bm_width) / 2).try_into().unwrap(),
((Modals::MODAL_HEIGHT - bm_height) / 2).try_into().unwrap(),
);
let bm = Bitmap::from(&modal_img);
let mut tiles: [Option<Tile>; 6] = [None; 6];
for (t, tile) in bm.iter().enumerate() {
if t >= tiles.len() {
@ -257,7 +258,7 @@ impl Modals {
Ok(())
}
#[cfg(feature="ditherpunk")]
#[cfg(feature = "ditherpunk")]
fn resize_image(img: RgbImage, scale: f32) -> RgbImage {
let width: u32 = (img.width() as f32 * scale).floor() as u32;
let height: u32 = (img.height() as f32 * scale).floor() as u32;