mirror of
https://gitlab.com/asus-linux/asusctl.git
synced 2026-02-06 00:15:04 +01:00
anime: discard frames if specified
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use glam::Vec2;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::{fs::File, path::Path, time::Duration};
|
||||
use glam::Vec2;
|
||||
|
||||
use crate::{AniMeDataBuffer, AniMeDiagonal, AniMeImage, error::AnimeError, Pixel};
|
||||
use crate::{error::AnimeError, AniMeDataBuffer, AniMeDiagonal, AniMeImage, Pixel};
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct AniMeFrame {
|
||||
@@ -27,7 +27,6 @@ pub struct AniMeGif(Vec<AniMeFrame>);
|
||||
|
||||
impl AniMeGif {
|
||||
pub fn create_diagonal_gif(file_name: &Path, brightness: f32) -> Result<Self, AnimeError> {
|
||||
let mut frames = Vec::new();
|
||||
let mut matrix = AniMeDiagonal::new();
|
||||
|
||||
let mut decoder = gif::DecodeOptions::new();
|
||||
@@ -37,8 +36,12 @@ impl AniMeGif {
|
||||
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 {
|
||||
@@ -68,25 +71,45 @@ impl AniMeGif {
|
||||
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(1.0, 1.0), 0.0, Vec2::new(0.0, 0.0),
|
||||
brightness, pixels, decoder.width() as u32);
|
||||
let pixels: Vec<Pixel> =
|
||||
vec![Pixel::default(); (decoder.width() as u32 * decoder.height() as u32) as usize];
|
||||
let mut image = AniMeImage::new(
|
||||
Vec2::new(1.0, 1.0),
|
||||
0.0,
|
||||
Vec2::new(0.0, 0.0),
|
||||
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(1.0, 1.0),
|
||||
0.0,
|
||||
Vec2::new(0.0, 0.0),
|
||||
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 {
|
||||
// should be t but not in some gifs? What, ASUS, what?
|
||||
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,
|
||||
};
|
||||
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();
|
||||
|
||||
@@ -55,11 +55,13 @@ impl AniMeSequence {
|
||||
Ok(Self::Image(Box::new(data)))
|
||||
}
|
||||
|
||||
pub fn png_gif(file: &Path,
|
||||
pub fn png_gif(
|
||||
file: &Path,
|
||||
// scale: Vec2,
|
||||
// angle: f32,
|
||||
// translation: Vec2,
|
||||
brightness: f32,) -> Result<Self, AnimeError> {
|
||||
brightness: f32,
|
||||
) -> Result<Self, AnimeError> {
|
||||
let frames = AniMeGif::create_png_gif(file, brightness)?;
|
||||
Ok(Self::Animation(frames))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user