about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2024-01-02 19:11:20 -0600
committergennyble <gen@nyble.dev>2024-01-02 19:11:20 -0600
commit939eb07c2962a8dc20eb8963277f3f7359db50fb (patch)
tree20fd7c8c3e4e42e8cba4a7d9db93f0a0ba87ba73
parent96878711b03ee026f57d69648953d603ed503f34 (diff)
downloadgifed-939eb07c2962a8dc20eb8963277f3f7359db50fb.tar.gz
gifed-939eb07c2962a8dc20eb8963277f3f7359db50fb.zip
VideoGif will now loop
-rw-r--r--gifed/src/videogif.rs22
1 files changed, 20 insertions, 2 deletions
diff --git a/gifed/src/videogif.rs b/gifed/src/videogif.rs
index a5475cc..d066019 100644
--- a/gifed/src/videogif.rs
+++ b/gifed/src/videogif.rs
@@ -1,15 +1,23 @@
-use crate::{block::Palette, writer::ImageBuilder, Color, EncodeError, Gif};
+use crate::{
+	block::{LoopCount, Palette},
+	writer::ImageBuilder,
+	Color, EncodeError, Gif,
+};
 
 use color_quant::NeuQuant;
 use rgb::{ComponentBytes, FromSlice};
 
 use std::convert::TryFrom;
 
+/// A Video-like GIF.
+///
+/// All images must have the same dimensions.
 pub struct VideoGif {
 	width: u16,
 	height: u16,
 	framerate: Option<u16>,
 	frames: Vec<Frame>,
+	looping: LoopCount,
 }
 
 impl VideoGif {
@@ -19,6 +27,7 @@ impl VideoGif {
 			height,
 			framerate: None,
 			frames: vec![],
+			looping: LoopCount::Forever,
 		}
 	}
 
@@ -30,6 +39,13 @@ impl VideoGif {
 		self.framerate = Some(100 / framerate);
 	}
 
+	/// Set the number of times this gif should loop. Defaults to forever.
+	///
+	/// See [LoopCount]
+	pub fn set_looping(&mut self, count: LoopCount) {
+		self.looping = count;
+	}
+
 	/// Adds a frame to the gif.
 	///
 	/// # Panic
@@ -53,10 +69,12 @@ impl VideoGif {
 
 	#[rustfmt::skip] // it was doing things i did not like
 	pub fn build(self) -> Result<Gif, EncodeError> {
-		let Self { width, height, framerate, frames } = self;
+		let Self { width, height, framerate, frames, looping } = self;
 
 		let mut gif = Gif::new(width, height);
 
+		gif.push(looping);
+
 		for Frame { image_indices, interval, palette } in frames {
 			//TODO: return error instead of defaulting to 10? or print warning?
 			// printing in a library is bad but perhaps so is assuming 10 fps?