#[derive(Debug)]
pub enum GraphicDecodeErr {
IncorrectFormat,
BadNum,
GrayscaleNYI,
IndexedNYI,
AGrayscaleNYI,
BitsNYI,
}
impl ::std::fmt::Display for GraphicDecodeErr {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "Couldn't parse PNG because: {}", match *self {
GraphicDecodeErr::IncorrectFormat => "Bad header",
GraphicDecodeErr::BadNum => "Dimensions aren't numbers",
GraphicDecodeErr::GrayscaleNYI => "NYI: Grayscale",
GraphicDecodeErr::IndexedNYI => "NYI: Indexed",
GraphicDecodeErr::AGrayscaleNYI => "NYI: AGrayscale",
GraphicDecodeErr::BitsNYI => "NYI: bad bits",
})
}
}
pub struct GraphicBuilder;
impl GraphicBuilder {
pub fn new() -> GraphicBuilder {
GraphicBuilder
}
pub fn rgba(self, w: u32, h: u32, data: Vec<u32>) -> Graphic {
let bgra = false;
assert!(data.len() as u32 / w == h
&& data.len() as u32 % w == 0);
Graphic { bgra, w, h, data }
}
pub fn bgra(self, w: u32, h: u32, data: Vec<u32>) -> Graphic {
let bgra = true;
assert!(data.len() as u32 / w == h
&& data.len() as u32 % w == 0);
Graphic { bgra, w, h, data }
}
}
#[derive(Clone)]
pub struct Graphic {
bgra: bool,
w: u32,
h: u32,
data: Vec<u32>,
}
impl Graphic {
pub fn bgra(&mut self) {
if !self.bgra {
for i in self.data.as_mut_slice() {
*i = i.swap_bytes().rotate_right(8);
}
}
self.bgra = true;
}
pub fn rgba(&mut self) {
if self.bgra {
for i in self.data.as_mut_slice() {
*i = i.swap_bytes().rotate_right(8);
}
}
self.bgra = false;
}
pub fn as_slice(&self) -> (u32, u32, &[u32]) {
(self.w, self.h, self.data.as_slice())
}
}