Skip to content

Commit

Permalink
Extract u16 reads into function
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jan 15, 2024
1 parent f9d6591 commit 9201683
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions src/reader/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,43 +550,6 @@ impl StreamingDecoder {
} else {
Err(DecodingError::format("malformed GIF header"))
},
U16(next) => goto!(U16Byte1(next, b)),
U16Byte1(next, value) => {
use self::U16Value::*;
let value = (u16::from(b) << 8) | u16::from(value);
match (next, value) {
(ScreenWidth, width) => {
self.width = width;
goto!(U16(U16Value::ScreenHeight))
},
(ScreenHeight, height) => {
self.height = height;
goto!(Byte(ByteValue::GlobalFlags))
},
(Delay, delay) => {
self.try_current_frame()?.delay = delay;
self.ext.data.push(value as u8);
self.ext.data.push(b);
goto!(Byte(ByteValue::TransparentIdx))
},
(ImageLeft, left) => {
self.try_current_frame()?.left = left;
goto!(U16(U16Value::ImageTop))
},
(ImageTop, top) => {
self.try_current_frame()?.top = top;
goto!(U16(U16Value::ImageWidth))
},
(ImageWidth, width) => {
self.try_current_frame()?.width = width;
goto!(U16(U16Value::ImageHeight))
},
(ImageHeight, height) => {
self.try_current_frame()?.height = height;
goto!(Byte(ByteValue::ImageFlags))
}
}
}
Byte(value) => {
use self::ByteValue::*;
match value {
Expand Down Expand Up @@ -837,6 +800,10 @@ impl StreamingDecoder {
}
}
}
U16(next) => goto!(U16Byte1(next, b)),
U16Byte1(next, value) => {
goto!(self.read_second_byte(next, value, b)?)
}
FrameDecoded => {
// end of image data reached
self.current = None;
Expand All @@ -847,6 +814,43 @@ impl StreamingDecoder {
}
}

fn read_second_byte(&mut self, next: U16Value, value: u8, b: u8) -> Result<State, DecodingError> {
use self::U16Value::*;
let value = (u16::from(b) << 8) | u16::from(value);
Ok(match (next, value) {
(ScreenWidth, width) => {
self.width = width;
U16(U16Value::ScreenHeight)
},
(ScreenHeight, height) => {
self.height = height;
Byte(ByteValue::GlobalFlags)
},
(Delay, delay) => {
self.try_current_frame()?.delay = delay;
self.ext.data.push(value as u8);
self.ext.data.push(b);
Byte(ByteValue::TransparentIdx)
},
(ImageLeft, left) => {
self.try_current_frame()?.left = left;
U16(U16Value::ImageTop)
},
(ImageTop, top) => {
self.try_current_frame()?.top = top;
U16(U16Value::ImageWidth)
},
(ImageWidth, width) => {
self.try_current_frame()?.width = width;
U16(U16Value::ImageHeight)
},
(ImageHeight, height) => {
self.try_current_frame()?.height = height;
Byte(ByteValue::ImageFlags)
}
})
}

fn read_control_extension(&mut self, b: u8) -> Result<State, DecodingError> {
self.add_frame();
self.ext.data.push(b);
Expand Down

0 comments on commit 9201683

Please sign in to comment.