Skip to content

Commit

Permalink
Add methods to getting frames from animated images
Browse files Browse the repository at this point in the history
  • Loading branch information
emmabritton committed May 28, 2023
1 parent 706c425 commit b195691
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### Version 0.1.3
- Add `get_frame(idx)` and `as_images()` for `AnimatedIndexedImage`

### Version 0.1.2
- Add `width()` and `height()` for `IndexedImage`

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ici-files"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
authors = ["Emma Britton <emmabritton@pm.me>"]
description = "Encode/decode ici files"
Expand Down
24 changes: 22 additions & 2 deletions src/animated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::errors::IndexedImageError;
use crate::errors::IndexedImageError::*;
use crate::file::FileType::Animated;
use crate::file::{verify_format, HEADER};
use crate::image::IndexedImage;
use crate::palette::FilePalette;
use crate::{palette, IciColor};

Expand Down Expand Up @@ -243,6 +244,25 @@ impl AnimatedIndexedImage {
Ok(x as usize + y as usize * self.width as usize)
}

pub fn as_images(&self) -> Vec<IndexedImage> {
let mut output = vec![];
for i in 0..self.frame_count {
output.push(self.get_frame(i));
}
output
}

pub fn get_frame(&self, idx: usize) -> IndexedImage {
let pixels = self
.pixels
.iter()
.skip(self.frame_size * idx)
.take(self.frame_size)
.cloned()
.collect();
IndexedImage::new(self.width, self.height, self.palette.clone(), pixels).unwrap()
}

#[inline]
pub fn get_color(&self, idx: u8) -> Result<IciColor, IndexedImageError> {
if idx >= self.palette.len() as u8 {
Expand Down Expand Up @@ -489,8 +509,8 @@ impl AnimatedIndexedImage {
));
}
let pixels_start = start + 12;
let frame_size = width * height;
let frame_pixel_count = frame_size as usize * frame_count as usize;
let frame_size = width as usize * height as usize;
let frame_pixel_count = frame_size * frame_count as usize;
if bytes.len() < pixels_start + frame_pixel_count {
return Err(InvalidFileFormat(
pixels_start,
Expand Down

0 comments on commit b195691

Please sign in to comment.