use crate::gl;
#[cfg(feature = "png")]
use png;
#[cfg(feature = "png")]
use std::error::Error;
#[derive(Clone, Debug)]
pub struct Image {
pub pixels: Vec<u8>,
pub width: i32,
pub height: i32,
pub format: u32,
}
impl Image {
#[cfg(feature = "png")]
pub fn from_png(bytes: &[u8]) -> Result<Image, Box<Error>> {
let decoder = png::Decoder::new(bytes);
let (info, mut reader) = decoder.read_info()?;
let mut pixels = vec![0; info.buffer_size()];
reader.next_frame(&mut pixels)?;
Ok(Image {
pixels,
width: info.width as i32,
height: info.height as i32,
format: gl::RGBA,
})
}
pub fn from_color(width: i32, height: i32, color: &[u8]) -> Image {
let mut pixels = vec![0; (width * height) as usize * color.len()];
for i in 0..pixels.len() {
pixels[i] = color[i % color.len()];
}
Image {
pixels,
width,
height,
format: gl::RGBA,
}
}
pub fn format(mut self, format: u32) -> Image {
self.format = format;
self
}
}