diff options
Diffstat (limited to 'src/block')
-rw-r--r-- | src/block/colortable.rs | 101 | ||||
-rw-r--r-- | src/block/extension/application.rs | 15 | ||||
-rw-r--r-- | src/block/extension/graphiccontrol.rs | 131 | ||||
-rw-r--r-- | src/block/extension/mod.rs | 62 | ||||
-rw-r--r-- | src/block/imagedescriptor.rs | 100 | ||||
-rw-r--r-- | src/block/indexedimage.rs | 87 | ||||
-rw-r--r-- | src/block/mod.rs | 12 | ||||
-rw-r--r-- | src/block/screendescriptor.rs | 95 | ||||
-rw-r--r-- | src/block/version.rs | 29 |
9 files changed, 386 insertions, 246 deletions
diff --git a/src/block/colortable.rs b/src/block/colortable.rs index 97b428f..da9458c 100644 --- a/src/block/colortable.rs +++ b/src/block/colortable.rs @@ -1,67 +1,82 @@ -use std::ops::Deref; pub use crate::Color; +use std::{ + convert::{TryFrom, TryInto}, + ops::Deref, +}; pub struct ColorTable { - table: Vec<Color> + table: Vec<Color>, } impl ColorTable { - pub fn new() -> Self { - Self { - table: vec![] - } - } + 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 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() - } + /// 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); - } + /// Pushes a color on to the end of the table + pub fn push(&mut self, color: Color) { + self.table.push(color); + } } impl Deref for ColorTable { - type Target = [Color]; + type Target = [Color]; - fn deref(&self) -> &Self::Target { - &self.table - } + fn deref(&self) -> &Self::Target { + &self.table + } } impl From<Vec<Color>> for ColorTable { - fn from(table: Vec<Color>) -> Self{ - ColorTable { - table - } - } + fn from(table: Vec<Color>) -> Self { + ColorTable { table } + } } impl From<&ColorTable> for Box<[u8]> { - fn from(table: &ColorTable) -> Self { - let mut vec = vec![]; + fn from(table: &ColorTable) -> Self { + let mut vec = vec![]; - for color in table.iter() { - vec.extend_from_slice(&[color.r, color.g, color.b]); - } + for color in table.iter() { + vec.extend_from_slice(&[color.r, color.g, color.b]); + } - let packed_len = 2u8.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]); - } + 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() - } + vec.into_boxed_slice() + } } -//TODO: TryFrom Vec<u8> (must be multiple of 3 len) and From Vec<Color> \ No newline at end of file +//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>>(), + }) + } + } +} diff --git a/src/block/extension/application.rs b/src/block/extension/application.rs new file mode 100644 index 0000000..b3516d8 --- /dev/null +++ b/src/block/extension/application.rs @@ -0,0 +1,15 @@ +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 index 46f44fa..0d4edd1 100644 --- a/src/block/extension/graphiccontrol.rs +++ b/src/block/extension/graphiccontrol.rs @@ -1,59 +1,86 @@ +use std::convert::TryInto; + +#[derive(Clone, Debug)] pub struct GraphicControl { - pub(crate) packed: u8, - pub(crate) delay_time: u16, - pub(crate) transparency_index: u8 + 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.disposal_method(disposal_method); - ret.user_input(user_input_flag); - ret.transparency(transparency_flag); - - ret - } - - pub fn disposal_method(&mut self, method: DisposalMethod) { - match method { - DisposalMethod::Clear => 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 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(&mut self, hundreths: u16) { - self.delay_time = hundreths; - } - - //TODO: Transparency index setter + 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.disposal_method(disposal_method); + ret.user_input(user_input_flag); + ret.transparency(transparency_flag); + + ret + } + + pub fn disposal_method(&mut self, method: DisposalMethod) { + match method { + DisposalMethod::Clear => 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 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 + } + + //TODO: Transparency index setter +} + +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, + } + } } pub enum DisposalMethod { - Clear, - DoNotDispose, - RestoreBackground, - RestorePrevious -} \ No newline at end of file + Clear, + DoNotDispose, + RestoreBackground, + RestorePrevious, +} diff --git a/src/block/extension/mod.rs b/src/block/extension/mod.rs index 66a39b0..d0e57c6 100644 --- a/src/block/extension/mod.rs +++ b/src/block/extension/mod.rs @@ -1,39 +1,43 @@ +mod application; mod graphiccontrol; pub use graphiccontrol::{DisposalMethod, GraphicControl}; +pub use self::application::Application; + pub enum Extension { - GraphicControl(GraphicControl), - Looping(u16) - // Comment - // Plain Text - // Generic Application + 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 + 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()); - } - } + 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() - } -} \ No newline at end of file + vec.push(0x00); // Zero-length data block indicates end of extension + vec.into_boxed_slice() + } +} diff --git a/src/block/imagedescriptor.rs b/src/block/imagedescriptor.rs index c911baa..415bee7 100644 --- a/src/block/imagedescriptor.rs +++ b/src/block/imagedescriptor.rs @@ -1,45 +1,73 @@ +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 + // 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 color_table_present(&mut self, is_present: bool) { - if is_present { - self.packed |= 0b1000_0000; - } else { - self.packed &= 0b0111_1111; - } - } - - pub fn 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 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() - } + 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 \ No newline at end of file +//TODO: Impl to allow changing the packed field easier diff --git a/src/block/indexedimage.rs b/src/block/indexedimage.rs index ae2da06..52be3d5 100644 --- a/src/block/indexedimage.rs +++ b/src/block/indexedimage.rs @@ -1,45 +1,54 @@ -use crate::LZW; +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> + pub image_descriptor: ImageDescriptor, + pub local_color_table: Option<ColorTable>, + pub indicies: Vec<u8>, } impl IndexedImage { - 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 - }; - - 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() - } -} \ No newline at end of file + 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 BlockedImage { + 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 index 38b10ea..e3136bf 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -1,17 +1,19 @@ mod colortable; pub mod extension; -mod indexedimage; mod imagedescriptor; +mod indexedimage; mod screendescriptor; mod version; pub use colortable::ColorTable; -pub use indexedimage::IndexedImage; pub use imagedescriptor::ImageDescriptor; +pub use indexedimage::BlockedImage; +pub use indexedimage::IndexedImage; pub use screendescriptor::ScreenDescriptor; pub use version::Version; pub enum Block { - IndexedImage(IndexedImage), - Extension(extension::Extension) -} \ No newline at end of file + IndexedImage(IndexedImage), + BlockedImage(BlockedImage), + Extension(extension::Extension), +} diff --git a/src/block/screendescriptor.rs b/src/block/screendescriptor.rs index d53d252..ff70896 100644 --- a/src/block/screendescriptor.rs +++ b/src/block/screendescriptor.rs @@ -1,40 +1,69 @@ +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 + pub width: u16, + pub height: u16, + pub packed: u8, + pub background_color_index: u8, + pub pixel_aspect_ratio: u8, } impl ScreenDescriptor { - pub fn color_table_present(&mut self, is_present: bool) { - if is_present { - self.packed |= 0b1000_0000; - } else { - self.packed &= 0b0111_1111; - } - } - - pub fn 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 color resolution in packed field + 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() - } -} \ No newline at end of file + 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 index a5d688d..b785f27 100644 --- a/src/block/version.rs +++ b/src/block/version.rs @@ -1,13 +1,24 @@ +use std::fmt; + pub enum Version { - Gif87a, - Gif89a + Gif87a, + Gif89a, } impl From<&Version> for &[u8] { - fn from(version: &Version) -> Self { - match version { - Version::Gif87a => b"GIF87a", - Version::Gif89a => b"GIF89a" - } - } -} \ No newline at end of file + 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"), + } + } +} |