breakout mod Img RGB raster

This commit is contained in:
nworbnhoj 2022-07-28 06:03:22 +10:00
parent 98586ffc6c
commit afef00a028
2 changed files with 72 additions and 65 deletions

View file

@ -5,6 +5,8 @@ use std::convert::TryInto;
use std::io::Read;
use std::ops::Deref;
mod img;
pub use img::*;
mod decode_png;
pub use decode_png::*;
mod greyscale;
@ -362,71 +364,6 @@ impl<'a> From<&Img> for Bitmap {
}
}
// **********************************************************************
#[derive(Debug, Clone, Copy)]
pub enum PixelType {
U0, // Error
U8,
U8x2,
U8x3,
U8x4,
U16,
U16x2,
U16x3,
U16x4,
}
/*
* Image as a minimal flat buffer of u8; accessible by (x, y)
*
* author: nworbnhoj
*/
//#[derive(Debug)]
pub struct Img {
pub pixels: Vec<u8>,
pub width: usize,
pub px_type: PixelType,
}
impl Img {
pub fn new(pixels: Vec<u8>, width: usize, px_type: PixelType) -> Self {
Self {
pixels,
width,
px_type,
}
}
pub fn width(&self) -> usize {
self.width
}
pub fn height(&self) -> usize {
match self.px_type {
PixelType::U8 => self.pixels.len() / self.width,
PixelType::U8x2 => self.pixels.len() / (self.width * 2),
PixelType::U8x3 => self.pixels.len() / (self.width * 3),
PixelType::U8x4 => self.pixels.len() / (self.width * 4),
PixelType::U16 => self.pixels.len() / (self.width * 2),
PixelType::U16x2 => self.pixels.len() / (self.width * 4),
PixelType::U16x3 => self.pixels.len() / (self.width * 6),
PixelType::U16x4 => self.pixels.len() / (self.width * 8),
_ => {
log::warn!("PixelType not implemented");
0
}
}
}
}
impl Deref for Img {
type Target = Vec<u8>;
fn deref(&self) -> &Self::Target {
&self.pixels
}
}
// **********************************************************************

View file

@ -0,0 +1,70 @@
/*
* Img is a minimal structure to hold an RGB raster image
*
* author: nworbnhoj
*/
use std::ops::Deref;
#[derive(Debug, Clone, Copy)]
pub enum PixelType {
U0, // Error
U8,
U8x2,
U8x3,
U8x4,
U16,
U16x2,
U16x3,
U16x4,
}
/*
* Image as a minimal flat buffer of u8; accessible by (x, y)
*
* author: nworbnhoj
*/
//#[derive(Debug)]
pub struct Img {
pub pixels: Vec<u8>,
pub width: usize,
pub px_type: PixelType,
}
impl Img {
pub fn new(pixels: Vec<u8>, width: usize, px_type: PixelType) -> Self {
Self {
pixels,
width,
px_type,
}
}
pub fn width(&self) -> usize {
self.width
}
pub fn height(&self) -> usize {
match self.px_type {
PixelType::U8 => self.pixels.len() / self.width,
PixelType::U8x2 => self.pixels.len() / (self.width * 2),
PixelType::U8x3 => self.pixels.len() / (self.width * 3),
PixelType::U8x4 => self.pixels.len() / (self.width * 4),
PixelType::U16 => self.pixels.len() / (self.width * 2),
PixelType::U16x2 => self.pixels.len() / (self.width * 4),
PixelType::U16x3 => self.pixels.len() / (self.width * 6),
PixelType::U16x4 => self.pixels.len() / (self.width * 8),
_ => {
log::warn!("PixelType not implemented");
0
}
}
}
}
impl Deref for Img {
type Target = Vec<u8>;
fn deref(&self) -> &Self::Target {
&self.pixels
}
}