From 9b7bd5696a21496fa0c38a17e69c5c0658acfe73 Mon Sep 17 00:00:00 2001 From: Genny Date: Mon, 11 Oct 2021 03:35:38 -0500 Subject: Implement images iterator --- src/colorimage.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 9 deletions(-) (limited to 'src/colorimage.rs') diff --git a/src/colorimage.rs b/src/colorimage.rs index 71f2bee..1bdb273 100644 --- a/src/colorimage.rs +++ b/src/colorimage.rs @@ -1,16 +1,61 @@ +use std::convert::TryFrom; + +use crate::{block::ColorTable, gif::Image, reader::DecodingError, Color}; + pub struct ColorImage { - width: u16, - height: u16, - data: Vec + width: u16, + height: u16, + data: Vec, } impl ColorImage { - pub fn new() { - - } + pub(crate) fn from_indicies( + width: u16, + height: u16, + indicies: &[u8], + table: &ColorTable, + transindex: Option, + ) -> Result { + let mut data = vec![Pixel::Transparent; (width * height) as usize]; + + for (image_index, color_index) in indicies.into_iter().enumerate() { + if let Some(trans) = transindex { + if trans == *color_index { + data[image_index] = Pixel::Transparent; + } + } else { + data[image_index] = Pixel::Color( + table + .get(*color_index) + .ok_or(DecodingError::ColorIndexOutOfBounds)?, + ); + } + } + + Ok(ColorImage { + width, + height, + data, + }) + } } +impl<'a> TryFrom> for ColorImage { + type Error = DecodingError; + + fn try_from(img: Image<'a>) -> Result { + ColorImage::from_indicies( + img.width, + img.height, + img.indicies, + img.palette, + img.transparent_index, + ) + } +} + +#[derive(Copy, Clone, Debug, PartialEq)] pub enum Pixel { - Color(Color), - Transparent -} \ No newline at end of file + Color(Color), + Transparent, +} -- cgit 1.4.1-3-g733a5