mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
Implement gam::PixelType
enables gam::Img to import different image formats: U8 & U8x3 for now
This commit is contained in:
parent
f7e216bb25
commit
409f696fde
5 changed files with 37 additions and 15 deletions
|
|
@ -290,6 +290,12 @@ impl From<&Img> for Bitmap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum PixelType {
|
||||||
|
U8,
|
||||||
|
U8x3,
|
||||||
|
U8x4,
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Image as a minimal flat buffer of grey u8 pixels; accessible by (x, y)
|
* Image as a minimal flat buffer of grey u8 pixels; accessible by (x, y)
|
||||||
*
|
*
|
||||||
|
|
@ -303,21 +309,33 @@ pub struct Img {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Img {
|
impl Img {
|
||||||
pub fn new(buf: Vec<u8>, width: usize) -> Self {
|
pub fn new(buf: Vec<u8>, width: usize, px_type: PixelType) -> Self {
|
||||||
const R: u32 = 2126;
|
const R: u32 = 2126;
|
||||||
const G: u32 = 7152;
|
const G: u32 = 7152;
|
||||||
const B: u32 = 722;
|
const B: u32 = 722;
|
||||||
const BLACK: u32 = R + G + B;
|
const BLACK: u32 = R + G + B;
|
||||||
let px_len = buf.len() / 3;
|
let px_len = match px_type {
|
||||||
|
PixelType::U8 => buf.len(),
|
||||||
|
PixelType::U8x3 => buf.len() / 3,
|
||||||
|
_ => 0,
|
||||||
|
};
|
||||||
let mut pixels: Vec<u8> = Vec::with_capacity(px_len);
|
let mut pixels: Vec<u8> = Vec::with_capacity(px_len);
|
||||||
for px in 0..px_len {
|
for px in 0..px_len {
|
||||||
let b = px * 3;
|
let pixel: u8 = match px_type {
|
||||||
let grey_r = R * buf[b] as u32;
|
PixelType::U8 => buf[px],
|
||||||
let grey_g = G * buf[b + 1] as u32;
|
PixelType::U8x3 => {
|
||||||
let grey_b = B * buf[b + 2] as u32;
|
let b = px * 3;
|
||||||
pixels.push(((grey_r + grey_g + grey_b) / BLACK)
|
let grey_r = R * buf[b] as u32;
|
||||||
.try_into()
|
let grey_g = G * buf[b + 1] as u32;
|
||||||
.unwrap());
|
let grey_b = B * buf[b + 2] as u32;
|
||||||
|
((grey_r + grey_g + grey_b) / BLACK).try_into().unwrap()
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
log::warn!("unsupported PixelType");
|
||||||
|
0
|
||||||
|
}
|
||||||
|
};
|
||||||
|
pixels.push(pixel);
|
||||||
}
|
}
|
||||||
Self { pixels, width }
|
Self { pixels, width }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ pub use apps::*;
|
||||||
#[cfg(feature="ditherpunk")]
|
#[cfg(feature="ditherpunk")]
|
||||||
pub mod bitmap;
|
pub mod bitmap;
|
||||||
#[cfg(feature="ditherpunk")]
|
#[cfg(feature="ditherpunk")]
|
||||||
pub use bitmap::{Bitmap, Img};
|
pub use bitmap::{Bitmap, Img, PixelType};
|
||||||
|
|
||||||
use graphics_server::api::{TextOp, TextView};
|
use graphics_server::api::{TextOp, TextView};
|
||||||
use graphics_server::api::{Gid, Line, Circle, RoundedRectangle, TokenClaim};
|
use graphics_server::api::{Gid, Line, Circle, RoundedRectangle, TokenClaim};
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ use num_traits::*;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
#[cfg(feature = "ditherpunk")]
|
#[cfg(feature = "ditherpunk")]
|
||||||
use std::num::NonZeroU32;
|
use std::num::NonZeroU32;
|
||||||
|
#[cfg(feature = "ditherpunk")]
|
||||||
|
use bitmap::PixelType;
|
||||||
use xous::{send_message, Message, CID};
|
use xous::{send_message, Message, CID};
|
||||||
use xous_ipc::Buffer;
|
use xous_ipc::Buffer;
|
||||||
|
|
||||||
|
|
@ -280,7 +282,7 @@ impl Modals {
|
||||||
resizer.resize(&fir_img.view(), &mut rsz_view).unwrap();
|
resizer.resize(&fir_img.view(), &mut rsz_view).unwrap();
|
||||||
|
|
||||||
let width: usize = resized.width().get().try_into().unwrap();
|
let width: usize = resized.width().get().try_into().unwrap();
|
||||||
Img::new(resized.into_vec(), width )
|
Img::new(resized.into_vec(), width, PixelType::U8x3)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_progress(
|
pub fn start_progress(
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ use super::*;
|
||||||
use gam::*;
|
use gam::*;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use xous_names::XousNames;
|
use xous_names::XousNames;
|
||||||
|
#[cfg(feature = "ditherpunk")]
|
||||||
|
use bitmap::PixelType;
|
||||||
|
|
||||||
const RADIO_TEST: [&'static str; 4] = ["zebra", "cow", "horse", "cat"];
|
const RADIO_TEST: [&'static str; 4] = ["zebra", "cow", "horse", "cat"];
|
||||||
|
|
||||||
|
|
@ -196,7 +198,7 @@ fn clifford() -> Img {
|
||||||
(rgb[j], rgb[j + 1], rgb[j + 2]) = (buf[i], buf[i], buf[i]);
|
(rgb[j], rgb[j + 1], rgb[j + 2]) = (buf[i], buf[i], buf[i]);
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
Img::new(rgb, SIZE.try_into().unwrap())
|
Img::new(rgb, SIZE.try_into().unwrap(), PixelType::U8x3)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_validator(input: TextEntryPayload) -> Option<xous_ipc::String<256>> {
|
fn test_validator(input: TextEntryPayload) -> Option<xous_ipc::String<256>> {
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ use dns::Dns; // necessary to work around https://github.com/rust-lang/rust/issu
|
||||||
#[cfg(feature="ditherpunk")]
|
#[cfg(feature="ditherpunk")]
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
#[cfg(feature="ditherpunk")]
|
#[cfg(feature="ditherpunk")]
|
||||||
use gam::Img;
|
use gam::{Img, PixelType};
|
||||||
|
|
||||||
pub struct NetCmd {
|
pub struct NetCmd {
|
||||||
callback_id: Option<u32>,
|
callback_id: Option<u32>,
|
||||||
|
|
@ -389,7 +389,7 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
|
||||||
let (width, height) = (info.width, info.height);
|
let (width, height) = (info.width, info.height);
|
||||||
log::info!("image is {} x {}", width, height);
|
log::info!("image is {} x {}", width, height);
|
||||||
log::info!("some image bytes: {:x?}", &buf[..64]);
|
log::info!("some image bytes: {:x?}", &buf[..64]);
|
||||||
let img = Img::new(buf, width as usize);
|
let img = Img::new(buf, width as usize, PixelType::U8x3);
|
||||||
|
|
||||||
modals.show_image(&img).expect("show image modal failed");
|
modals.show_image(&img).expect("show image modal failed");
|
||||||
write!(ret, "Image of {} bytes read successfully", content_length).unwrap();
|
write!(ret, "Image of {} bytes read successfully", content_length).unwrap();
|
||||||
|
|
@ -700,4 +700,4 @@ impl Worker {
|
||||||
thread: Some(thread),
|
thread: Some(thread),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue