about summary refs log tree commit diff
path: root/src/colorimage.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/colorimage.rs')
-rw-r--r--src/colorimage.rs63
1 files changed, 54 insertions, 9 deletions
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<Pixel>
+    width: u16,
+    height: u16,
+    data: Vec<Pixel>,
 }
 
 impl ColorImage {
-	pub fn new() {
-		
-	}
+    pub(crate) fn from_indicies(
+        width: u16,
+        height: u16,
+        indicies: &[u8],
+        table: &ColorTable,
+        transindex: Option<u8>,
+    ) -> Result<Self, DecodingError> {
+        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<Image<'a>> for ColorImage {
+    type Error = DecodingError;
+
+    fn try_from(img: Image<'a>) -> Result<Self, Self::Error> {
+        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,
+}