about summary refs log tree commit diff
path: root/gifed/src/block/extension
diff options
context:
space:
mode:
authorGenevieve Alfirevic <gen@nyble.dev>2022-02-11 14:13:36 -0600
committerGenevieve Alfirevic <gen@nyble.dev>2022-02-11 14:13:36 -0600
commit7a7da6b0fe15fa20ec10544fb0f0d8177dbb9ca9 (patch)
tree1cf43a4d3bb4a8d304d0de1b22623c38eb3ae708 /gifed/src/block/extension
parent64dc74a04eedd550f1c72af1caa88d2b81ab70e8 (diff)
downloadgifed-7a7da6b0fe15fa20ec10544fb0f0d8177dbb9ca9.tar.gz
gifed-7a7da6b0fe15fa20ec10544fb0f0d8177dbb9ca9.zip
Start moving packed fields to their own structs
Diffstat (limited to 'gifed/src/block/extension')
-rw-r--r--gifed/src/block/extension/application.rs4
-rw-r--r--gifed/src/block/extension/graphiccontrol.rs91
2 files changed, 34 insertions, 61 deletions
diff --git a/gifed/src/block/extension/application.rs b/gifed/src/block/extension/application.rs
index 3d46277..2244c35 100644
--- a/gifed/src/block/extension/application.rs
+++ b/gifed/src/block/extension/application.rs
@@ -12,4 +12,8 @@ impl Application {
 	pub fn authentication_code(&self) -> &[u8] {
 		&self.authentication_code
 	}
+
+	pub fn data(&self) -> &[u8] {
+		&self.data
+	}
 }
diff --git a/gifed/src/block/extension/graphiccontrol.rs b/gifed/src/block/extension/graphiccontrol.rs
index a6abbdc..e9662b6 100644
--- a/gifed/src/block/extension/graphiccontrol.rs
+++ b/gifed/src/block/extension/graphiccontrol.rs
@@ -1,14 +1,16 @@
 use std::{convert::TryInto, fmt, time::Duration};
 
+use crate::block::packed::GraphicPacked;
+
 #[derive(Clone, Debug)]
 pub struct GraphicControl {
-	pub(crate) packed: u8,
+	pub(crate) packed: GraphicPacked,
 	pub(crate) delay: u16,
 	pub(crate) transparency_index: u8,
 }
 
 impl GraphicControl {
-	pub fn new(
+	pub(crate) fn new(
 		disposal_method: DisposalMethod,
 		user_input_flag: bool,
 		transparency_flag: bool,
@@ -16,47 +18,50 @@ impl GraphicControl {
 		transparency_index: u8,
 	) -> Self {
 		let mut ret = Self {
-			packed: 0,
+			packed: GraphicPacked { raw: 0 },
 			delay,
 			transparency_index,
 		};
 
 		ret.set_disposal_method(disposal_method);
-		ret.set_user_input(user_input_flag);
-		ret.set_transparent(transparency_flag);
+		ret.packed.set_user_input(user_input_flag);
+		ret.packed.set_transparent_color(transparency_flag);
 
 		ret
 	}
 
+	pub fn packed(&self) -> &GraphicPacked {
+		&self.packed
+	}
+
 	/// Get the disposal method that should be used for the associated image.
 	///
 	/// # Returns
 	/// This method will return `Some([DisposalMethod])` if the disposal method
 	/// is recognized, or None if it was set to a reserved value.
 	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),
+		match self.packed.disposal_method() {
+			0 => Some(DisposalMethod::NoAction),
+			1 => Some(DisposalMethod::DoNotDispose),
+			2 => Some(DisposalMethod::RestoreBackground),
+			3 => Some(DisposalMethod::RestorePrevious),
 			_ => None,
 		}
 	}
 
-	/// Set the disposal method that shoudl be used for the associated image.
-	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 set_disposal_method(&mut self, dispose: DisposalMethod) {
+		match dispose {
+			DisposalMethod::NoAction => self.packed.set_disposal_method(0),
+			DisposalMethod::DoNotDispose => self.packed.set_disposal_method(1),
+			DisposalMethod::RestoreBackground => self.packed.set_disposal_method(2),
+			DisposalMethod::RestorePrevious => self.packed.set_disposal_method(4),
+		}
 	}
 
 	/// Returns the index that should be replaced by a fully transparent pixel
 	/// if the transparency flag is set, or None if it's not set.
 	pub fn transparent_index(&self) -> Option<u8> {
-		if self.transparent() {
+		if self.packed.transparent_color() {
 			Some(self.transparency_index)
 		} else {
 			None
@@ -64,68 +69,32 @@ impl GraphicControl {
 	}
 
 	/// Returns the transparency index regardless if the transparency flag is set.
-	/// You probably want [GraphicControl::transparency_index] instead.s
+	/// You probably want [GraphicControl::transparency_index] instead.
 	pub fn transparent_index_unchecked(&self) -> u8 {
 		self.transparency_index
 	}
 
-	/// Sets the transparent index flag to the value provided. This will change
-	/// the index value in any way and should be used with caution. You probably
-	/// want [GraphicControl::set_transparent_index] instead.
-	pub fn set_transparent(&mut self, flag: bool) {
-		if flag {
-			self.packed |= 0b000_000_0_1;
-		} else {
-			self.packed &= 0b111_111_1_0;
-		}
-	}
-
 	/// Sets the transparent index and flips the flag to indicate a transparent
-	/// index is present.
+	/// index is present if `index` is `Some`.
 	pub fn set_transparent_index(&mut self, index: Option<u8>) {
-		self.set_transparent(index.is_some());
+		self.packed.set_transparent_color(index.is_some());
 
 		if let Some(index) = index {
 			self.transparency_index = index;
 		}
 	}
 
-	/// Get the value of the transparency flag
-	pub fn transparent(&self) -> bool {
-		self.packed & 0b000_000_0_1 > 0
-	}
-
-	pub fn user_input(&self) -> bool {
-		self.packed & 0b000_000_1_0 > 0
-	}
-
-	pub fn set_user_input(&mut self, flag: bool) {
-		if flag {
-			self.packed |= 0b000_000_1_0;
-		} else {
-			self.packed &= 0b111_111_0_1;
-		}
+	pub fn delay_duration(&self) -> Duration {
+		Duration::from_millis(self.delay as u64 * 10)
 	}
 
 	pub fn delay(&self) -> u16 {
 		self.delay
 	}
 
-	pub fn delay_duration(&self) -> Duration {
-		Duration::from_millis(self.delay as u64 * 10)
-	}
-
 	pub fn delay_mut(&mut self) -> &mut u16 {
 		&mut self.delay
 	}
-
-	pub fn packed(&self) -> u8 {
-		self.packed
-	}
-
-	pub fn packed_mut(&mut self) -> &mut u8 {
-		&mut self.packed
-	}
 }
 
 impl From<[u8; 4]> for GraphicControl {
@@ -135,7 +104,7 @@ impl From<[u8; 4]> for GraphicControl {
 		let transparency_index = arr[3];
 
 		Self {
-			packed,
+			packed: GraphicPacked { raw: packed },
 			delay,
 			transparency_index,
 		}