diff options
Diffstat (limited to 'src/block')
-rw-r--r-- | src/block/colortable.rs | 117 | ||||
-rw-r--r-- | src/block/extension/application.rs | 15 | ||||
-rw-r--r-- | src/block/extension/graphiccontrol.rs | 116 | ||||
-rw-r--r-- | src/block/extension/mod.rs | 49 | ||||
-rw-r--r-- | src/block/imagedescriptor.rs | 73 | ||||
-rw-r--r-- | src/block/indexedimage.rs | 70 | ||||
-rw-r--r-- | src/block/mod.rs | 25 | ||||
-rw-r--r-- | src/block/screendescriptor.rs | 79 | ||||
-rw-r--r-- | src/block/version.rs | 25 |
9 files changed, 0 insertions, 569 deletions
diff --git a/src/block/colortable.rs b/src/block/colortable.rs deleted file mode 100644 index 01fe00b..0000000 --- a/src/block/colortable.rs +++ /dev/null @@ -1,117 +0,0 @@ -pub use crate::Color; -use crate::EncodingError; -use std::{ - convert::{TryFrom, TryInto}, - ops::Deref, -}; - -#[derive(Clone, Debug)] -pub struct ColorTable { - table: Vec<Color>, -} - -impl ColorTable { - pub fn new() -> Self { - Self { table: vec![] } - } - - /// 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. - pub fn packed_len(&self) -> u8 { - ((self.table.len() as f32).log2().ceil() - 1f32) as u8 - } - - /// Returns the number of items in the table - pub fn len(&self) -> usize { - self.table.len() - } - - /// Pushes a color on to the end of the table - pub fn push(&mut self, color: Color) { - self.table.push(color); - } - - pub fn get(&self, index: u8) -> Option<Color> { - self.table.get(index as usize).map(|v| v.clone()) - } - - pub fn from_color(&self, color: Color) -> Option<u8> { - for (i, &c) in self.table.iter().enumerate() { - if c == color { - return Some(i as u8); - } - } - None - } -} - -impl Deref for ColorTable { - type Target = [Color]; - - fn deref(&self) -> &Self::Target { - &self.table - } -} - -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 { - type Error = (); - - fn try_from(value: &[u8]) -> Result<Self, Self::Error> { - if value.len() % 3 != 0 { - return Err(()); - } else { - Ok(Self { - table: value - .chunks(3) - .map(|slice| Color::from(TryInto::<[u8; 3]>::try_into(slice).unwrap())) - .collect::<Vec<Color>>(), - }) - } - } -} - -impl TryFrom<Vec<Color>> for ColorTable { - type Error = EncodingError; - - fn try_from(value: Vec<Color>) -> Result<Self, Self::Error> { - if value.len() > 256 { - Err(EncodingError::TooManyColors) - } else { - Ok(Self { table: value }) - } - } -} - -impl TryFrom<Vec<(u8, u8, u8)>> for ColorTable { - type Error = EncodingError; - - fn try_from(value: Vec<(u8, u8, u8)>) -> Result<Self, Self::Error> { - if value.len() > 256 { - Err(EncodingError::TooManyColors) - } else { - Ok(Self { - table: value.into_iter().map(|c| c.into()).collect(), - }) - } - } -} diff --git a/src/block/extension/application.rs b/src/block/extension/application.rs deleted file mode 100644 index 9ec1814..0000000 --- a/src/block/extension/application.rs +++ /dev/null @@ -1,15 +0,0 @@ -pub struct Application { - pub(crate) identifier: String, // max len 8 - pub(crate) authentication_code: [u8; 3], - pub(crate) data: Vec<u8>, -} - -impl Application { - pub fn identifier(&self) -> &str { - &self.identifier - } - - pub fn authentication_code(&self) -> &[u8] { - &self.authentication_code - } -} diff --git a/src/block/extension/graphiccontrol.rs b/src/block/extension/graphiccontrol.rs deleted file mode 100644 index b595554..0000000 --- a/src/block/extension/graphiccontrol.rs +++ /dev/null @@ -1,116 +0,0 @@ -use std::{convert::TryInto, fmt}; - -#[derive(Clone, Debug)] -pub struct GraphicControl { - pub(crate) packed: u8, - pub(crate) delay_time: u16, - pub(crate) transparency_index: u8, -} - -impl GraphicControl { - pub fn new( - disposal_method: DisposalMethod, - user_input_flag: bool, - transparency_flag: bool, - delay_time: u16, - transparency_index: u8, - ) -> Self { - let mut ret = Self { - packed: 0, - delay_time, - transparency_index, - }; - - ret.set_disposal_method(disposal_method); - ret.user_input(user_input_flag); - ret.transparency(transparency_flag); - - ret - } - - pub fn disposal_method(&self) -> Option<DisposalMethod> { - match self.packed & 0b000_111_00 { - 0b000_000_00 => Some(DisposalMethod::NoAction), - 0b000_100_00 => Some(DisposalMethod::DoNotDispose), - 0b000_010_00 => Some(DisposalMethod::RestoreBackground), - 0b000_110_00 => Some(DisposalMethod::RestorePrevious), - _ => None, - } - } - - pub fn set_disposal_method(&mut self, method: DisposalMethod) { - match method { - DisposalMethod::NoAction => self.packed &= 0b111_000_1_1, - DisposalMethod::DoNotDispose => self.packed |= 0b000_100_0_0, - DisposalMethod::RestoreBackground => self.packed |= 0b000_010_0_0, - DisposalMethod::RestorePrevious => self.packed |= 0b000_110_0_0, - }; - } - - pub fn transparency_index(&self) -> u8 { - self.transparency_index - } - - pub fn user_input(&mut self, flag: bool) { - if flag { - self.packed |= 0b000_000_1_0; - } else { - self.packed &= 0b111_111_0_1; - } - } - - pub fn transparency(&mut self, flag: bool) { - if flag { - self.packed |= 0b000_000_0_1; - } else { - self.packed &= 0b111_111_1_0; - } - } - - pub fn delay_time(&self) -> u16 { - self.delay_time - } - - pub fn delay_time_mut(&mut self) -> &mut u16 { - &mut self.delay_time - } - - pub fn is_transparent(&self) -> bool { - self.packed & 0b000_000_0_1 > 0 - } -} - -impl From<[u8; 4]> for GraphicControl { - fn from(arr: [u8; 4]) -> Self { - let packed = arr[0]; - let delay_time = u16::from_le_bytes(arr[1..3].try_into().unwrap()); - let transparency_index = arr[3]; - - Self { - packed, - delay_time, - transparency_index, - } - } -} - -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum DisposalMethod { - NoAction, - DoNotDispose, - RestoreBackground, - RestorePrevious, -} - -impl fmt::Display for DisposalMethod { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let st = match self { - DisposalMethod::NoAction => "Dispose as Normal", - DisposalMethod::DoNotDispose => "No Dispose", - DisposalMethod::RestoreBackground => "Restore to background", - DisposalMethod::RestorePrevious => "Restore previous image", - }; - - write!(f, "{}", st) - } -} diff --git a/src/block/extension/mod.rs b/src/block/extension/mod.rs deleted file mode 100644 index fb5eb20..0000000 --- a/src/block/extension/mod.rs +++ /dev/null @@ -1,49 +0,0 @@ -mod application; -mod graphiccontrol; - -pub use graphiccontrol::{DisposalMethod, GraphicControl}; - -pub use self::application::Application; - -pub enum Extension { - GraphicControl(GraphicControl), - Looping(u16), - Comment(Vec<u8>), // Plain Text - Application(Application), -} - -impl From<&Extension> for Box<[u8]> { - fn from(ext: &Extension) -> Self { - let mut vec = vec![]; - vec.push(0x21); // Push the extension introducer - - match ext { - Extension::GraphicControl(gc) => { - vec.push(0xF9); // Graphic control label - vec.push(0x04); // Block size for this extension is always 4 - vec.push(gc.packed); - vec.extend_from_slice(&gc.delay_time.to_le_bytes()); - vec.push(gc.transparency_index); - } - Extension::Looping(count) => { - vec.push(0xFF); // Application extension label - vec.push(0x0B); // 11 bytes in this block - vec.extend_from_slice(b"NETSCAPE2.0"); // App. ident. and "auth code" - vec.push(0x03); // Sub-block length - vec.push(0x01); // Identifies netscape looping extension - vec.extend_from_slice(&count.to_le_bytes()); - } - Extension::Comment(_) => todo!(), - Extension::Application(_) => todo!(), - } - - vec.push(0x00); // Zero-length data block indicates end of extension - vec.into_boxed_slice() - } -} - -impl From<GraphicControl> for Extension { - fn from(gce: GraphicControl) -> Self { - Extension::GraphicControl(gce) - } -} diff --git a/src/block/imagedescriptor.rs b/src/block/imagedescriptor.rs deleted file mode 100644 index 25567b2..0000000 --- a/src/block/imagedescriptor.rs +++ /dev/null @@ -1,73 +0,0 @@ -use std::convert::TryInto; - -pub struct ImageDescriptor { - // Image Seperator 0x2C is the first byte // - pub left: u16, - pub top: u16, - pub width: u16, - pub height: u16, - pub packed: u8, -} - -impl ImageDescriptor { - pub fn set_color_table_present(&mut self, is_present: bool) { - if is_present { - self.packed |= 0b1000_0000; - } else { - self.packed &= 0b0111_1111; - } - } - - pub fn set_color_table_size(&mut self, size: u8) { - // GCT size is calulated by raising two to this number plus one, - // so we have to work backwards. - let size = (size as f32).log2().ceil() - 1f32; - self.packed |= size as u8; - } - - //TODO: Setter for sort flag in packed field - //TODO: Setter for interlace flag in packed field - - pub fn color_table_present(&self) -> bool { - self.packed & 0b1000_0000 != 0 - } - - pub fn color_table_size(&self) -> usize { - crate::packed_to_color_table_length(self.packed & 0b0000_0111) - } -} - -impl From<&ImageDescriptor> for Box<[u8]> { - fn from(desc: &ImageDescriptor) -> Self { - let mut vec = vec![]; - - vec.push(0x2C); // Image Seperator - vec.extend_from_slice(&desc.left.to_le_bytes()); - vec.extend_from_slice(&desc.top.to_le_bytes()); - vec.extend_from_slice(&desc.width.to_le_bytes()); - vec.extend_from_slice(&desc.height.to_le_bytes()); - vec.push(desc.packed); - - vec.into_boxed_slice() - } -} - -impl From<[u8; 9]> for ImageDescriptor { - fn from(arr: [u8; 9]) -> Self { - let left = u16::from_le_bytes(arr[0..2].try_into().unwrap()); - let top = u16::from_le_bytes(arr[2..4].try_into().unwrap()); - let width = u16::from_le_bytes(arr[4..6].try_into().unwrap()); - let height = u16::from_le_bytes(arr[6..8].try_into().unwrap()); - let packed = arr[8]; - - Self { - left, - top, - width, - height, - packed, - } - } -} - -//TODO: Impl to allow changing the packed field easier diff --git a/src/block/indexedimage.rs b/src/block/indexedimage.rs deleted file mode 100644 index 8ed0319..0000000 --- a/src/block/indexedimage.rs +++ /dev/null @@ -1,70 +0,0 @@ -use std::convert::TryFrom; - -use super::{ColorTable, ImageDescriptor}; -use crate::LZW; - -pub struct IndexedImage { - pub image_descriptor: ImageDescriptor, - pub local_color_table: Option<ColorTable>, - pub indicies: Vec<u8>, -} - -impl IndexedImage { - pub fn left(&self) -> u16 { - self.image_descriptor.left - } - - pub fn top(&self) -> u16 { - self.image_descriptor.left - } - - pub fn width(&self) -> u16 { - self.image_descriptor.width - } - - pub fn height(&self) -> u16 { - self.image_descriptor.height - } - - pub fn as_boxed_slice(&self, minimum_code_size: u8) -> Box<[u8]> { - let mut out = vec![]; - - let mut boxed: Box<[u8]> = (&self.image_descriptor).into(); - out.extend_from_slice(&*boxed); - - // 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); - - lct.packed_len() - } else { - minimum_code_size + 1 - }; - - if mcs < 2 { - mcs = 2; // Must be true: 0 <= mcs <= 8 - } - - // First write out the MCS - out.push(mcs); - - let compressed = LZW::encode(mcs, &self.indicies); - - for chunk in compressed.chunks(255) { - out.push(chunk.len() as u8); - out.extend_from_slice(chunk); - } - // Data block length 0 to indicate an end - out.push(0x00); - - out.into_boxed_slice() - } -} - -pub struct CompressedImage { - pub image_descriptor: ImageDescriptor, - pub local_color_table: Option<ColorTable>, - pub lzw_minimum_code_size: u8, - pub blocks: Vec<Vec<u8>>, -} diff --git a/src/block/mod.rs b/src/block/mod.rs deleted file mode 100644 index e35224b..0000000 --- a/src/block/mod.rs +++ /dev/null @@ -1,25 +0,0 @@ -mod colortable; -pub mod extension; -mod imagedescriptor; -mod indexedimage; -mod screendescriptor; -mod version; - -pub use colortable::ColorTable; -pub use imagedescriptor::ImageDescriptor; -pub use indexedimage::CompressedImage; -pub use indexedimage::IndexedImage; -pub use screendescriptor::ScreenDescriptor; -pub use version::Version; - -use crate::writer::ImageBuilder; - -pub enum Block { - IndexedImage(IndexedImage), - Extension(extension::Extension), -} - -enum WriteBlock { - ImageBuilder(ImageBuilder), - Block(Block), -} diff --git a/src/block/screendescriptor.rs b/src/block/screendescriptor.rs deleted file mode 100644 index dc0257d..0000000 --- a/src/block/screendescriptor.rs +++ /dev/null @@ -1,79 +0,0 @@ -use std::convert::TryInto; - -pub struct ScreenDescriptor { - pub width: u16, - pub height: u16, - pub packed: u8, - pub background_color_index: u8, - pub pixel_aspect_ratio: u8, -} - -impl ScreenDescriptor { - pub fn new(width: u16, height: u16) -> Self { - Self { - width, - height, - packed: 0, - background_color_index: 0, - pixel_aspect_ratio: 0, - } - } - - pub fn set_color_table_present(&mut self, is_present: bool) { - if is_present { - self.packed |= 0b1000_0000; - } else { - self.packed &= 0b0111_1111; - } - } - - pub fn set_color_table_size(&mut self, size: u8) { - println!("scts: {}", size); - // GCT size is calulated by raising two to this number plus one, - // so we have to work backwards. - let size = (size as f32).log2().ceil() - 1f32; - self.packed |= size as u8; - } - - //TODO: Setter for sort flag in packed field - //TODO: Setter for color resolution in packed field - - pub fn color_table_present(&self) -> bool { - self.packed & 0b1000_0000 != 0 - } - - pub fn color_table_len(&self) -> usize { - crate::packed_to_color_table_length(self.packed & 0b0000_0111) - } -} - -impl From<&ScreenDescriptor> for Box<[u8]> { - fn from(lsd: &ScreenDescriptor) -> Self { - let mut vec = vec![]; - vec.extend_from_slice(&lsd.width.to_le_bytes()); - vec.extend_from_slice(&lsd.height.to_le_bytes()); - vec.push(lsd.packed); - vec.push(lsd.background_color_index); - vec.push(lsd.pixel_aspect_ratio); - - vec.into_boxed_slice() - } -} - -impl From<[u8; 7]> for ScreenDescriptor { - fn from(arr: [u8; 7]) -> Self { - let width = u16::from_le_bytes(arr[0..2].try_into().unwrap()); - let height = u16::from_le_bytes(arr[2..4].try_into().unwrap()); - let packed = arr[4]; - let background_color_index = arr[5]; - let pixel_aspect_ratio = arr[6]; - - Self { - width, - height, - packed, - background_color_index, - pixel_aspect_ratio, - } - } -} diff --git a/src/block/version.rs b/src/block/version.rs deleted file mode 100644 index 0171ad4..0000000 --- a/src/block/version.rs +++ /dev/null @@ -1,25 +0,0 @@ -use std::fmt; - -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum Version { - Gif87a, - Gif89a, -} - -impl From<&Version> for &[u8] { - fn from(version: &Version) -> Self { - match version { - Version::Gif87a => b"GIF87a", - Version::Gif89a => b"GIF89a", - } - } -} - -impl fmt::Display for Version { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Version::Gif87a => write!(f, "GIF87a"), - Version::Gif89a => write!(f, "GIF89a"), - } - } -} |