Skip to content

Commit 312d4db

Browse files
committed
Avoid cloning every frame metadata
1 parent f348b62 commit 312d4db

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

src/common.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,24 @@ impl Frame<'static> {
338338
}
339339
Frame::from_rgba_speed(width, height, &mut vec, speed)
340340
}
341+
342+
/// Leaves empty buffer and empty palette behind
343+
#[inline]
344+
pub(crate) fn take(&mut self) -> Self {
345+
Frame {
346+
delay: self.delay,
347+
dispose: self.dispose,
348+
transparent: self.transparent,
349+
needs_user_input: self.needs_user_input,
350+
top: self.top,
351+
left: self.left,
352+
width: self.width,
353+
height: self.height,
354+
interlaced: self.interlaced,
355+
palette: std::mem::take(&mut self.palette),
356+
buffer: std::mem::replace(&mut self.buffer, Cow::Borrowed(&[])),
357+
}
358+
}
341359
}
342360

343361
#[test]

src/reader/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub enum Decoded<'a> {
141141
/// Decoded all information of the next frame, except the image data.
142142
///
143143
/// The returned frame does **not** contain any owned image data.
144-
FrameMetadata(&'a Frame<'static>, FrameDataType),
144+
FrameMetadata(&'a mut Frame<'static>, FrameDataType),
145145
/// Decoded some data of the current frame.
146146
BytesDecoded(usize),
147147
/// Copied (or consumed and discarded) compressed data of the current frame. In bytes.

src/reader/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ impl<R> Decoder<R> where R: Read {
308308
loop {
309309
match self.decoder.decode_next(&mut OutputBuffer::None)? {
310310
Some(Decoded::FrameMetadata(frame, frame_data_type)) => {
311-
self.current_frame = frame.clone();
311+
self.current_frame = frame.take();
312312
self.current_frame_data_type = frame_data_type;
313-
if frame.palette.is_none() && self.global_palette.is_none() {
313+
if self.current_frame.palette.is_none() && self.global_palette.is_none() {
314314
return Err(DecodingError::format(
315315
"no color table available for current frame",
316316
));

tests/decode.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,13 @@ fn check_skip_frame_data_decode_frame_error() {
107107
let mut skipping_decoder = options.read_info(image).unwrap();
108108
let mut normal_decoder = DecodeOptions::new().read_info(image).unwrap();
109109

110-
while let Ok(Some(_normal_frame)) = normal_decoder.read_next_frame() {
111-
let _compressed_frame = skipping_decoder.read_next_frame().unwrap().unwrap();
110+
while let Ok(Some(normal_frame)) = normal_decoder.read_next_frame() {
111+
let compressed_frame = skipping_decoder.read_next_frame().unwrap().unwrap();
112+
assert_eq!(normal_frame.width, compressed_frame.width);
113+
assert_eq!(normal_frame.height, compressed_frame.height);
114+
assert_eq!(normal_frame.delay, compressed_frame.delay);
115+
assert!(!normal_frame.buffer.is_empty());
116+
assert!(!compressed_frame.buffer.is_empty());
112117
}
113118
assert!(skipping_decoder.read_next_frame().unwrap().is_none());
114119

0 commit comments

Comments
 (0)