Skip to content

Commit

Permalink
use rayon for processing gifs to be faster
Browse files Browse the repository at this point in the history
  • Loading branch information
Toyz committed Mar 22, 2024
1 parent c22ddcd commit 5e541e7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ rand = "0.9.0-alpha.1"
once_cell = "1.19.0"
open = "5.1.2"
ansiterm = "0.12.2"
rayon = "1.9.0"
17 changes: 14 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use image::{
};
use std::io::{BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use rayon::prelude::*;

mod bounds;
mod eval;
Expand Down Expand Up @@ -210,17 +212,26 @@ fn main() -> anyhow::Result<()> {
let mut encoder = Encoder::new(&mut writer, w as u16, h as u16, &[])?;
encoder.set_repeat(Repeat::Infinite)?;

for frame in &frames {
let new_frames = Mutex::new(Vec::with_capacity(frames.len()));

(0..frames.len()).into_par_iter().for_each(|i| {
let frame = frames.get(i).expect("Failed to get frame");
let frame = frame.clone();
let delay = frame.delay().numer_denom_ms().0 as u16;
let img = frame.into_buffer();
let out = process(img.into(), parsed.clone())?;
let out = process(img.into(), parsed.clone()).expect("Failed to process frame");
let mut bytes = out.as_bytes().to_vec();

let mut new_frame = gif::Frame::from_rgba_speed(w as u16, h as u16, &mut bytes, 10);

new_frame.delay = delay / 10;
encoder.write_frame(&new_frame)?;
new_frames.lock().expect("failed to unlock").push((i, new_frame));
});

let mut frames = new_frames.into_inner().expect("Failed to get frames");
frames.sort_by(|a, b| a.0.cmp(&b.0));
for (_, frame) in frames {
encoder.write_frame(&frame)?;
}
}
_ => return Err(anyhow::anyhow!("Unsupported file format\n")),
Expand Down

0 comments on commit 5e541e7

Please sign in to comment.