#[cfg(feature = "images")]
use std::path::Path;
use image;
use crate::shape::{Color, Shape};
use crate::error::Result;
pub struct Image {
image: image::RgbaImage
}
impl Image {
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self> {
Ok(Self { image: image::open(path)?.to_rgba() })
}
pub fn from_buffer(buffer: &[u8]) -> Result<Self> {
Ok(Self { image: image::load_from_memory(buffer)?.to_rgba() })
}
}
impl Shape for Image {
fn render(&self) -> Vec<Vec<Option<Color>>> {
self.image.rows().map(|row| {
row.map(|rgba| {
let [r, g, b, a] = rgba.0;
if a == 0 {
None
} else {
Some((r, g, b, a).into())
}
}).collect()
}).collect()
}
}