about summary refs log tree commit diff
path: root/gifed/src/block
diff options
context:
space:
mode:
Diffstat (limited to 'gifed/src/block')
-rw-r--r--gifed/src/block/imagedescriptor.rs4
-rw-r--r--gifed/src/block/indexedimage.rs9
-rw-r--r--gifed/src/block/mod.rs4
-rw-r--r--gifed/src/block/palette.rs (renamed from gifed/src/block/colortable.rs)37
-rw-r--r--gifed/src/block/screendescriptor.rs4
5 files changed, 21 insertions, 37 deletions
diff --git a/gifed/src/block/imagedescriptor.rs b/gifed/src/block/imagedescriptor.rs
index 76efe6c..6bc0792 100644
--- a/gifed/src/block/imagedescriptor.rs
+++ b/gifed/src/block/imagedescriptor.rs
@@ -1,6 +1,6 @@
 use std::convert::TryInto;
 
-use super::{packed::ImagePacked, ColorTable};
+use super::{packed::ImagePacked, Palette};
 
 pub struct ImageDescriptor {
 	pub left: u16,
@@ -13,7 +13,7 @@ pub struct ImageDescriptor {
 impl ImageDescriptor {
 	/// This data structure **does not** contain the color table, only a flag to
 	/// indicate if one is present and it's size.
-	pub fn set_color_table_metadata<T: AsRef<ColorTable>>(&mut self, table: Option<T>) {
+	pub fn set_color_table_metadata<T: AsRef<Palette>>(&mut self, table: Option<T>) {
 		if let Some(table) = table {
 			let table = table.as_ref();
 			self.packed.set_color_table(true);
diff --git a/gifed/src/block/indexedimage.rs b/gifed/src/block/indexedimage.rs
index 730a02c..4e72ea0 100644
--- a/gifed/src/block/indexedimage.rs
+++ b/gifed/src/block/indexedimage.rs
@@ -2,12 +2,12 @@ use std::convert::TryFrom;
 
 use weezl::encode::Encoder;
 
-use super::{ColorTable, ImageDescriptor};
+use super::{ImageDescriptor, Palette};
 use crate::LZW;
 
 pub struct IndexedImage {
 	pub image_descriptor: ImageDescriptor,
-	pub local_color_table: Option<ColorTable>,
+	pub local_color_table: Option<Palette>,
 	pub indicies: Vec<u8>,
 }
 
@@ -36,8 +36,7 @@ impl IndexedImage {
 
 		// Get the mcs while we write out the color table
 		let mut mcs = if let Some(lct) = &self.local_color_table {
-			boxed = lct.into();
-			out.extend_from_slice(&*boxed);
+			out.extend_from_slice(&lct.as_bytes());
 
 			lct.packed_len() + 1
 		} else {
@@ -70,7 +69,7 @@ impl IndexedImage {
 
 pub struct CompressedImage {
 	pub image_descriptor: ImageDescriptor,
-	pub local_color_table: Option<ColorTable>,
+	pub local_color_table: Option<Palette>,
 	pub lzw_minimum_code_size: u8,
 	pub blocks: Vec<Vec<u8>>,
 }
diff --git a/gifed/src/block/mod.rs b/gifed/src/block/mod.rs
index 28f8c86..06f3d9a 100644
--- a/gifed/src/block/mod.rs
+++ b/gifed/src/block/mod.rs
@@ -1,15 +1,15 @@
-mod colortable;
 pub mod extension;
 mod imagedescriptor;
 mod indexedimage;
 pub mod packed;
+mod palette;
 mod screendescriptor;
 mod version;
 
-pub use colortable::ColorTable;
 pub use imagedescriptor::ImageDescriptor;
 pub use indexedimage::CompressedImage;
 pub use indexedimage::IndexedImage;
+pub use palette::Palette;
 pub use screendescriptor::ScreenDescriptor;
 pub use version::Version;
 
diff --git a/gifed/src/block/colortable.rs b/gifed/src/block/palette.rs
index 0d076c9..f43a67c 100644
--- a/gifed/src/block/colortable.rs
+++ b/gifed/src/block/palette.rs
@@ -6,11 +6,11 @@ use std::{
 };
 
 #[derive(Clone, Debug)]
-pub struct ColorTable {
+pub struct Palette {
 	table: Vec<Color>,
 }
 
-impl ColorTable {
+impl Palette {
 	pub fn new() -> Self {
 		Self { table: vec![] }
 	}
@@ -18,6 +18,9 @@ impl ColorTable {
 	/// Returns the number of colors in the color table as used by the packed
 	/// fields in the Logical Screen Descriptor and Image Descriptor. You can
 	/// get the actual size with the [`len`](struct.ColorTable.html#method.len) method.
+	///
+	/// This value is equal to `log2([Palette::len]) - 1`. In other words, 2^(n + 1) will
+	/// give you the same value as [Palette::len]. (where `n` is the value returned)
 	pub fn packed_len(&self) -> u8 {
 		((self.table.len() as f32).log2().ceil() - 1f32) as u8
 	}
@@ -55,7 +58,7 @@ impl ColorTable {
 	}
 }
 
-impl Deref for ColorTable {
+impl Deref for Palette {
 	type Target = [Color];
 
 	fn deref(&self) -> &Self::Target {
@@ -63,32 +66,14 @@ impl Deref for ColorTable {
 	}
 }
 
-impl AsRef<ColorTable> for ColorTable {
-	fn as_ref(&self) -> &ColorTable {
+impl AsRef<Palette> for Palette {
+	fn as_ref(&self) -> &Palette {
 		self
 	}
 }
 
-impl From<&ColorTable> for Box<[u8]> {
-	fn from(table: &ColorTable) -> Self {
-		let mut vec = vec![];
-
-		for color in table.iter() {
-			vec.extend_from_slice(&[color.r, color.g, color.b]);
-		}
-
-		let packed_len = 2usize.pow(table.packed_len() as u32 + 1);
-		let padding = (packed_len as usize - table.len()) * 3;
-		if padding > 0 {
-			vec.extend_from_slice(&vec![0; padding]);
-		}
-
-		vec.into_boxed_slice()
-	}
-}
-
 //TODO: TryFrom Vec<u8> (must be multiple of 3 len) and From Vec<Color>
-impl TryFrom<&[u8]> for ColorTable {
+impl TryFrom<&[u8]> for Palette {
 	type Error = ();
 
 	fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
@@ -105,7 +90,7 @@ impl TryFrom<&[u8]> for ColorTable {
 	}
 }
 
-impl TryFrom<Vec<Color>> for ColorTable {
+impl TryFrom<Vec<Color>> for Palette {
 	type Error = EncodingError;
 
 	fn try_from(value: Vec<Color>) -> Result<Self, Self::Error> {
@@ -117,7 +102,7 @@ impl TryFrom<Vec<Color>> for ColorTable {
 	}
 }
 
-impl TryFrom<Vec<(u8, u8, u8)>> for ColorTable {
+impl TryFrom<Vec<(u8, u8, u8)>> for Palette {
 	type Error = EncodingError;
 
 	fn try_from(value: Vec<(u8, u8, u8)>) -> Result<Self, Self::Error> {
diff --git a/gifed/src/block/screendescriptor.rs b/gifed/src/block/screendescriptor.rs
index 6b318b5..766ad66 100644
--- a/gifed/src/block/screendescriptor.rs
+++ b/gifed/src/block/screendescriptor.rs
@@ -1,6 +1,6 @@
 use std::convert::TryInto;
 
-use super::{packed::ScreenPacked, ColorTable};
+use super::{packed::ScreenPacked, Palette};
 
 pub struct ScreenDescriptor {
 	pub width: u16,
@@ -23,7 +23,7 @@ impl ScreenDescriptor {
 
 	/// This data structure **does not** contain the color table, only a flag to
 	/// indicate if one is present and it's size.
-	pub fn set_color_table_metadata<T: AsRef<ColorTable>>(&mut self, table: Option<T>) {
+	pub fn set_color_table_metadata<T: AsRef<Palette>>(&mut self, table: Option<T>) {
 		if let Some(table) = table {
 			let table = table.as_ref();
 			self.packed.set_color_table(true);