Skip to content

Commit 256dc9d

Browse files
authored
Merge pull request #2716 from rayguo17/loop_cnt
Implement loop_count for AnimationDecoder Trait.
2 parents 43d7534 + a14387c commit 256dc9d

5 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎src/codecs/gif.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use std::io::{self, BufRead, Cursor, Read, Seek, Write};
3131
use std::marker::PhantomData;
3232
use std::mem;
33+
use std::num::NonZeroU32;
3334

3435
use gif::ColorOutput;
3536
use gif::{DisposalMethod, Frame};
@@ -40,6 +41,7 @@ use crate::error::{
4041
DecodingError, EncodingError, ImageError, ImageResult, LimitError, LimitErrorKind,
4142
ParameterError, ParameterErrorKind, UnsupportedError, UnsupportedErrorKind,
4243
};
44+
use crate::metadata::LoopCount;
4345
use crate::traits::Pixel;
4446
use crate::{
4547
AnimationDecoder, ExtendedColorType, ImageBuffer, ImageDecoder, ImageEncoder, ImageFormat,
@@ -422,6 +424,14 @@ impl<R: Read> Iterator for GifFrameIterator<R> {
422424
}
423425

424426
impl<'a, R: BufRead + Seek + 'a> AnimationDecoder<'a> for GifDecoder<R> {
427+
fn loop_count(&self) -> LoopCount {
428+
match self.reader.repeat() {
429+
gif::Repeat::Finite(n) => LoopCount::Finite(
430+
NonZeroU32::new(n.into()).expect("Repeat::Finite should be non-zero"),
431+
),
432+
gif::Repeat::Infinite => LoopCount::Infinite,
433+
}
434+
}
425435
fn into_frames(self) -> animation::Frames<'a> {
426436
animation::Frames::new(Box::new(GifFrameIterator::new(self)))
427437
}

‎src/codecs/png.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
use std::borrow::Cow;
99
use std::io::{BufRead, Seek, Write};
10+
use std::num::NonZeroU32;
1011

1112
use png::{BlendOp, DeflateCompression, DisposeOp};
1213

@@ -17,6 +18,7 @@ use crate::error::{
1718
ParameterErrorKind, UnsupportedError, UnsupportedErrorKind,
1819
};
1920
use crate::math::Rect;
21+
use crate::metadata::LoopCount;
2022
use crate::utils::vec_try_with_capacity;
2123
use crate::{
2224
AnimationDecoder, DynamicImage, GenericImage, GenericImageView, ImageBuffer, ImageDecoder,
@@ -519,6 +521,16 @@ impl<R: BufRead + Seek> ApngDecoder<R> {
519521
}
520522

521523
impl<'a, R: BufRead + Seek + 'a> AnimationDecoder<'a> for ApngDecoder<R> {
524+
fn loop_count(&self) -> LoopCount {
525+
match self.inner.reader.info().animation_control() {
526+
None => LoopCount::Infinite,
527+
Some(actl) if actl.num_plays == 0 => LoopCount::Infinite,
528+
Some(actl) => LoopCount::Finite(
529+
NonZeroU32::new(actl.num_plays).expect("num_plays should be non-zero"),
530+
),
531+
}
532+
}
533+
522534
fn into_frames(self) -> Frames<'a> {
523535
struct FrameIterator<R: BufRead + Seek>(ApngDecoder<R>);
524536

‎src/codecs/webp/decoder.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
use std::io::{BufRead, Read, Seek};
2+
use std::num::NonZeroU32;
3+
4+
use image_webp::LoopCount;
25

36
use crate::buffer::ConvertBuffer;
47
use crate::error::{DecodingError, ImageError, ImageResult};
@@ -100,6 +103,15 @@ impl<R: BufRead + Seek> ImageDecoder for WebPDecoder<R> {
100103
}
101104

102105
impl<'a, R: 'a + BufRead + Seek> AnimationDecoder<'a> for WebPDecoder<R> {
106+
fn loop_count(&self) -> crate::metadata::LoopCount {
107+
match self.inner.loop_count() {
108+
LoopCount::Forever => crate::metadata::LoopCount::Infinite,
109+
LoopCount::Times(n) => crate::metadata::LoopCount::Finite(
110+
NonZeroU32::new(n.get().into()).expect("LoopCount::Times should be non-zero"),
111+
),
112+
}
113+
}
114+
103115
fn into_frames(self) -> Frames<'a> {
104116
struct FramesInner<R: Read + Seek> {
105117
decoder: WebPDecoder<R>,

‎src/io/decoder.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::animation::Frames;
22
use crate::color::{ColorType, ExtendedColorType};
33
use crate::error::ImageResult;
4-
use crate::metadata::Orientation;
4+
use crate::metadata::{LoopCount, Orientation};
55

66
/// The trait that all decoders implement
77
pub trait ImageDecoder {
@@ -179,6 +179,8 @@ impl<T: ?Sized + ImageDecoder> ImageDecoder for Box<T> {
179179
pub trait AnimationDecoder<'a> {
180180
/// Consume the decoder producing a series of frames.
181181
fn into_frames(self) -> Frames<'a>;
182+
/// Loop count of the animated image.
183+
fn loop_count(&self) -> LoopCount;
182184
}
183185

184186
#[cfg(test)]

‎src/metadata.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! Types describing image metadata
22
pub(crate) mod cicp;
33

4-
use std::io::{Cursor, Read};
4+
use std::{
5+
io::{Cursor, Read},
6+
num::NonZeroU32,
7+
};
58

69
use byteorder_lite::{BigEndian, LittleEndian, ReadBytesExt, WriteBytesExt};
710

@@ -158,6 +161,15 @@ enum ExifEndian {
158161
Little,
159162
}
160163

164+
/// The number of times animated image should loop over.
165+
#[derive(Clone, Copy)]
166+
pub enum LoopCount {
167+
/// Loop the image Infinitely
168+
Infinite,
169+
/// Loop the image within Finite times.
170+
Finite(NonZeroU32),
171+
}
172+
161173
#[cfg(all(test, feature = "jpeg"))]
162174
mod tests {
163175
use crate::{codecs::jpeg::JpegDecoder, ImageDecoder as _};

0 commit comments

Comments
 (0)