use glam::Vec2;
use serde_derive::{Deserialize, Serialize};
use std::{fs::File, path::Path, time::Duration};
use crate::{error::AnimeError, AnimeDataBuffer, AnimeDiagonal, AnimeImage, Pixel};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AnimeFrame {
data: AnimeDataBuffer,
delay: Duration,
}
impl AnimeFrame {
#[inline]
pub fn frame(&self) -> &AnimeDataBuffer {
&self.data
}
#[inline]
pub fn delay(&self) -> Duration {
self.delay
}
}
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
pub enum AnimTime {
Time(Duration),
Count(u32),
Infinite,
Fade(Fade),
}
impl Default for AnimTime {
#[inline]
fn default() -> Self {
Self::Infinite
}
}
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
pub struct Fade {
fade_in: Duration,
show_for: Option<Duration>,
fade_out: Duration,
}
impl Fade {
pub fn new(fade_in: Duration, show_for: Option<Duration>, fade_out: Duration) -> Self {
Self {
fade_in,
show_for,
fade_out,
}
}
pub fn fade_in(&self) -> Duration {
self.fade_in
}
pub fn show_for(&self) -> Option<Duration> {
self.show_for
}
pub fn fade_out(&self) -> Duration {
self.fade_out
}
pub fn total_fade_time(&self) -> Duration {
self.fade_in + self.fade_out
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AnimeGif(Vec<AnimeFrame>, AnimTime);
impl AnimeGif {
#[inline]
pub fn from_diagonal_gif(
file_name: &Path,
duration: AnimTime,
brightness: f32,
) -> Result<Self, AnimeError> {
let mut matrix = AnimeDiagonal::new(None);
let mut decoder = gif::DecodeOptions::new();
decoder.set_color_output(gif::ColorOutput::RGBA);
let file = File::open(file_name)?;
let mut decoder = decoder.read_info(file)?;
let mut frames = Vec::with_capacity(decoder.buffer_size());
while let Some(frame) = decoder.read_next_frame()? {
let wait = frame.delay * 10;
if matches!(frame.dispose, gif::DisposalMethod::Background) {
frames = Vec::new();
}
for (y, row) in frame.buffer.chunks(frame.width as usize * 4).enumerate() {
for (x, px) in row.chunks(4).enumerate() {
if px[3] != 255 {
continue;
}
matrix.get_mut()[y + frame.top as usize][x + frame.left as usize] =
(px[0] as f32 * brightness) as u8;
}
}
frames.push(AnimeFrame {
data: <AnimeDataBuffer>::from(&matrix),
delay: Duration::from_millis(wait as u64),
});
}
Ok(Self(frames, duration))
}
#[inline]
pub fn from_diagonal_png(
file_name: &Path,
duration: AnimTime,
brightness: f32,
) -> Result<Self, AnimeError> {
let image = AnimeDiagonal::from_png(file_name, None, brightness)?;
let mut total = Duration::from_millis(1000);
if let AnimTime::Fade(fade) = duration {
total = fade.total_fade_time();
if let Some(middle) = fade.show_for {
total += middle;
}
}
let frame_count = total.as_millis() / 30;
let single = AnimeFrame {
data: <AnimeDataBuffer>::from(&image),
delay: Duration::from_millis(30),
};
let frames = vec![single; frame_count as usize];
Ok(Self(frames, duration))
}
#[inline]
pub fn from_gif(
file_name: &Path,
scale: f32,
angle: f32,
translation: Vec2,
duration: AnimTime,
brightness: f32,
) -> Result<Self, AnimeError> {
let mut frames = Vec::new();
let mut decoder = gif::DecodeOptions::new();
decoder.set_color_output(gif::ColorOutput::RGBA);
let file = File::open(file_name)?;
let mut decoder = decoder.read_info(file)?;
let height = decoder.height();
let width = decoder.width();
let pixels: Vec<Pixel> =
vec![Pixel::default(); (decoder.width() as u32 * decoder.height() as u32) as usize];
let mut image = AnimeImage::new(
Vec2::new(scale, scale),
angle,
translation,
brightness,
pixels,
decoder.width() as u32,
);
while let Some(frame) = decoder.read_next_frame()? {
let wait = frame.delay * 10;
if matches!(frame.dispose, gif::DisposalMethod::Background) {
let pixels: Vec<Pixel> =
vec![Pixel::default(); (width as u32 * height as u32) as usize];
image = AnimeImage::new(
Vec2::new(scale, scale),
angle,
translation,
brightness,
pixels,
width as u32,
);
}
for (y, row) in frame.buffer.chunks(frame.width as usize * 4).enumerate() {
for (x, px) in row.chunks(4).enumerate() {
if px[3] != 255 {
continue;
}
let pos =
(x + frame.left as usize) + ((y + frame.top as usize) * width as usize);
image.get_mut()[pos] = Pixel {
color: ((px[0] as u32 + px[1] as u32 + px[2] as u32) / 3),
alpha: 1.0,
};
}
}
image.update();
frames.push(AnimeFrame {
data: <AnimeDataBuffer>::from(&image),
delay: Duration::from_millis(wait as u64),
});
}
Ok(Self(frames, duration))
}
#[inline]
pub fn from_png(
file_name: &Path,
scale: f32,
angle: f32,
translation: Vec2,
duration: AnimTime,
brightness: f32,
) -> Result<Self, AnimeError> {
let image = AnimeImage::from_png(file_name, scale, angle, translation, brightness)?;
let mut total = Duration::from_millis(1000);
if let AnimTime::Fade(fade) = duration {
total = fade.total_fade_time();
if let Some(middle) = fade.show_for {
total += middle;
}
}
let frame_count = total.as_millis() / 30;
let single = AnimeFrame {
data: <AnimeDataBuffer>::from(&image),
delay: Duration::from_millis(30),
};
let frames = vec![single; frame_count as usize];
Ok(Self(frames, duration))
}
#[inline]
pub fn frames(&self) -> &[AnimeFrame] {
&self.0
}
#[inline]
pub fn duration(&self) -> AnimTime {
self.1
}
pub fn frame_count(&self) -> usize {
self.0.len()
}
pub fn total_frame_time(&self) -> Duration {
let mut time = 0;
self.0.iter().for_each(|f| time += f.delay.as_millis());
Duration::from_millis(time as u64)
}
}