diff options
Diffstat (limited to 'src/block/extension')
-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 |
3 files changed, 137 insertions, 137 deletions
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) + } } |