only single frame so far

This commit is contained in:
gallant 2023-07-09 08:16:47 -05:00
commit ac616c724a
5 changed files with 2170 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

2084
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

12
Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "ai-gif"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
image = "0.24.6"
pixels = "0.13.0"
wgpu = "0.16.1"
winit = "0.28.6"

BIN
image.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 MiB

73
src/main.rs Normal file
View file

@ -0,0 +1,73 @@
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use pixels::{Error, Pixels, SurfaceTexture};
use image::GenericImageView;
use std::path::PathBuf;
use std::time::Instant;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create an event loop
let event_loop = EventLoop::new();
// Create a window with the desired properties
let window = WindowBuilder::new()
.with_title("GIF Viewer")
.build(&event_loop)?;
// Load the GIF image
let image_path = PathBuf::from("image.gif");
let image = image::open(&image_path).expect("Failed to load image");
// Get the dimensions of the image
let (image_width, image_height) = image.dimensions();
// Create a surface texture to draw onto
let mut pixels = {
let window_size = window.inner_size();
let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);
Pixels::new(image_width, image_height, surface_texture)?
};
// Run the event loop
let mut last_frame = Instant::now();
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => {
*control_flow = ControlFlow::Exit;
}
Event::RedrawRequested(_) => {
let elapsed = last_frame.elapsed();
if elapsed.as_secs_f32() >= 1.0 / 60.0 {
// Update the pixel buffer with the GIF image
let mut frame = image.to_rgba8();
pixels
.frame_mut()
.copy_from_slice(&mut frame);
// Render the updated pixel buffer to the window
if let Err(e) = pixels.render() {
eprintln!("Failed to render: {}", e);
*control_flow = ControlFlow::Exit;
return;
}
last_frame = Instant::now();
}
}
Event::MainEventsCleared => {
// Request a redraw when the main events have been processed
window.request_redraw();
}
_ => {}
}
});
}