diff options
author | Genny <gen@nyble.dev> | 2021-10-11 03:36:38 -0500 |
---|---|---|
committer | Genny <gen@nyble.dev> | 2021-10-11 03:36:38 -0500 |
commit | 757eab88d67a425728b87286c763387f52367196 (patch) | |
tree | f1d61edb0a838787f6984accdd92df68a69d567a /src/block | |
parent | 9b7bd5696a21496fa0c38a17e69c5c0658acfe73 (diff) | |
download | gifed-757eab88d67a425728b87286c763387f52367196.tar.gz gifed-757eab88d67a425728b87286c763387f52367196.zip |
Run rustfmt
Diffstat (limited to 'src/block')
-rw-r--r-- | src/block/colortable.rs | 172 | ||||
-rw-r--r-- | src/block/extension/application.rs | 18 | ||||
-rw-r--r-- | src/block/extension/graphiccontrol.rs | 192 | ||||
-rw-r--r-- | src/block/extension/mod.rs | 64 | ||||
-rw-r--r-- | src/block/imagedescriptor.rs | 102 | ||||
-rw-r--r-- | src/block/indexedimage.rs | 90 | ||||
-rw-r--r-- | src/block/mod.rs | 8 | ||||
-rw-r--r-- | src/block/screendescriptor.rs | 118 | ||||
-rw-r--r-- | src/block/version.rs | 28 |
9 files changed, 396 insertions, 396 deletions
diff --git a/src/block/colortable.rs b/src/block/colortable.rs index d8241f7..01fe00b 100644 --- a/src/block/colortable.rs +++ b/src/block/colortable.rs @@ -1,117 +1,117 @@ pub use crate::Color; use crate::EncodingError; use std::{ - convert::{TryFrom, TryInto}, - ops::Deref, + convert::{TryFrom, TryInto}, + ops::Deref, }; #[derive(Clone, Debug)] pub struct ColorTable { - table: Vec<Color>, + 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 - } + 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]; + type Target = [Color]; - fn deref(&self) -> &Self::Target { - &self.table - } + fn deref(&self) -> &Self::Target { + &self.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 = 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]); - } + 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> 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>>(), - }) - } - } + 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 }) - } - } + 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(), - }) - } - } + 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 index b3516d8..9ec1814 100644 --- a/src/block/extension/application.rs +++ b/src/block/extension/application.rs @@ -1,15 +1,15 @@ pub struct Application { - pub(crate) identifier: String, // max len 8 - pub(crate) authentication_code: [u8; 3], - pub(crate) data: Vec<u8>, + 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 identifier(&self) -> &str { + &self.identifier + } - pub fn authentication_code(&self) -> &[u8] { - &self.authentication_code - } + pub fn authentication_code(&self) -> &[u8] { + &self.authentication_code + } } diff --git a/src/block/extension/graphiccontrol.rs b/src/block/extension/graphiccontrol.rs index 830dff4..6bbe4ea 100644 --- a/src/block/extension/graphiccontrol.rs +++ b/src/block/extension/graphiccontrol.rs @@ -2,113 +2,113 @@ 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, + 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 - } - - //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.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 + } + + //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, - } - } + 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, + 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) - } + 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 index fa712a6..fb5eb20 100644 --- a/src/block/extension/mod.rs +++ b/src/block/extension/mod.rs @@ -6,44 +6,44 @@ pub use graphiccontrol::{DisposalMethod, GraphicControl}; pub use self::application::Application; pub enum Extension { - GraphicControl(GraphicControl), - Looping(u16), - Comment(Vec<u8>), // Plain Text - Application(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()); - } - Extension::Comment(_) => todo!(), - Extension::Application(_) => todo!(), - } + 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() - } + 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) - } + fn from(gce: GraphicControl) -> Self { + Extension::GraphicControl(gce) + } } diff --git a/src/block/imagedescriptor.rs b/src/block/imagedescriptor.rs index 415bee7..25567b2 100644 --- a/src/block/imagedescriptor.rs +++ b/src/block/imagedescriptor.rs @@ -1,73 +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 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_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; - } + 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 + //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_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) - } + 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![]; + 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.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() - } + 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]; + 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, - } - } + 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 index 0be066f..8ed0319 100644 --- a/src/block/indexedimage.rs +++ b/src/block/indexedimage.rs @@ -4,67 +4,67 @@ 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 left(&self) -> u16 { - self.image_descriptor.left - } + pub fn left(&self) -> u16 { + self.image_descriptor.left + } - pub fn top(&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 width(&self) -> u16 { + self.image_descriptor.width + } - pub fn height(&self) -> u16 { - self.image_descriptor.height - } + 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![]; + 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); + 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); + // 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 - }; + lct.packed_len() + } else { + minimum_code_size + 1 + }; - if mcs < 2 { - mcs = 2; // Must be true: 0 <= mcs <= 8 - } + if mcs < 2 { + mcs = 2; // Must be true: 0 <= mcs <= 8 + } - // First write out the MCS - out.push(mcs); + // First write out the MCS + out.push(mcs); - let compressed = LZW::encode(mcs, &self.indicies); + 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); + 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() - } + 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>>, + 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 645c11d..e35224b 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -15,11 +15,11 @@ pub use version::Version; use crate::writer::ImageBuilder; pub enum Block { - IndexedImage(IndexedImage), - Extension(extension::Extension), + IndexedImage(IndexedImage), + Extension(extension::Extension), } enum WriteBlock { - ImageBuilder(ImageBuilder), - Block(Block), + ImageBuilder(ImageBuilder), + Block(Block), } diff --git a/src/block/screendescriptor.rs b/src/block/screendescriptor.rs index 444b44f..dc0257d 100644 --- a/src/block/screendescriptor.rs +++ b/src/block/screendescriptor.rs @@ -1,79 +1,79 @@ 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 new(width: u16, height: u16) -> Self { - Self { - width, - height, - packed: 0, - background_color_index: 0, - pixel_aspect_ratio: 0, - } - } + 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_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; - } + 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 + //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_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) - } + 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); + 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() - } + 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]; + 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, - } - } + Self { + width, + height, + packed, + background_color_index, + pixel_aspect_ratio, + } + } } diff --git a/src/block/version.rs b/src/block/version.rs index c52439c..0171ad4 100644 --- a/src/block/version.rs +++ b/src/block/version.rs @@ -2,24 +2,24 @@ use std::fmt; #[derive(Clone, Copy, Debug, PartialEq)] 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", - } - } + 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"), - } - } + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Version::Gif87a => write!(f, "GIF87a"), + Version::Gif89a => write!(f, "GIF89a"), + } + } } |