diff --git a/.gitignore b/.gitignore index 0f4fba1..25c44c4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ target/ Cargo.lock +**/test_images + # ggml binaries & other binaries in general *.bin # local splat files diff --git a/crates/bevy_frame_capture/src/scene.rs b/crates/bevy_frame_capture/src/scene.rs index 5df8dc9..b1dd9b3 100644 --- a/crates/bevy_frame_capture/src/scene.rs +++ b/crates/bevy_frame_capture/src/scene.rs @@ -161,35 +161,29 @@ // } // } -// fn image_to_browser_base64(img: &ImageBuffer, Vec>) -> Result { -// let mut image_data: Vec = Vec::new(); -// img.write_to(&mut Cursor::new(&mut image_data), ImageOutputFormat::Png)?; -// let res_base64 = general_purpose::STANDARD.encode(image_data); -// Ok(format!("data:image/png;base64,{}", res_base64)) -// } -// pub fn white_img_placeholder(w: u32, h: u32) -> String { -// let img = RgbaImage::new(w, h); -// // img.iter_mut().for_each(|pixel| *pixel = 255); -// image_to_browser_base64(&img).unwrap() -// } - use std::path::PathBuf; + use std::{io::Cursor, path::PathBuf}; + use base64::{engine::general_purpose, Engine}; use bevy::{ app::AppExit, prelude::*, render::{camera::RenderTarget, renderer::RenderDevice}, }; + use image::{ImageBuffer, ImageOutputFormat, Rgba, RgbaImage}; use wgpu::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages}; use super::image_copy::ImageCopier; #[derive(Component, Default)] pub struct CaptureCamera; + + #[derive(Resource)] + pub struct CurrImageBase64(pub String); #[derive(Component, Deref, DerefMut)] struct ImageToSave(Handle); @@ -211,7 +205,7 @@ } impl SceneController { - pub fn new(width: u32, height: u32, single_image: bool) -> SceneController { + pub fn new(width: u32, height: u32) -> SceneController { SceneController { state: SceneState::BuildScene, name: String::from(""), @@ -297,7 +291,6 @@ images_to_save: Query<&ImageToSave>, mut images: ResMut>, mut scene_controller: ResMut, - mut app_exit_writer: EventWriter, ) { if let SceneState::Render(n) = scene_controller.state { if n < 1 { @@ -311,7 +304,7 @@ let images_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_images"); - print!("Saving image to: {:?}\n", images_dir); + println!("Saving image to: {images_dir:?}"); std::fs::create_dir_all(&images_dir).unwrap(); let uuid = bevy::utils::Uuid::new_v4(); @@ -325,3 +318,17 @@ } } } + +// useful utils +fn image_to_browser_base64(img: &ImageBuffer, Vec>) -> anyhow::Result { + let mut image_data: Vec = Vec::new(); + img.write_to(&mut Cursor::new(&mut image_data), ImageOutputFormat::Png)?; + let res_base64 = general_purpose::STANDARD.encode(image_data); + Ok(format!("data:image/png;base64,{}", res_base64)) +} + +pub fn white_img_placeholder(w: u32, h: u32) -> String { + let img = RgbaImage::new(w, h); + // img.iter_mut().for_each(|pixel| *pixel = 255); + image_to_browser_base64(&img).unwrap() +} \ No newline at end of file diff --git a/crates/minimal/src/main.rs b/crates/minimal/src/main.rs index f5ad070..d5a880c 100644 --- a/crates/minimal/src/main.rs +++ b/crates/minimal/src/main.rs @@ -21,7 +21,6 @@ fn headless_app() { app.insert_resource(bevy_frame_capture::scene::SceneController::new( config.width, config.height, - true )); app.insert_resource(ClearColor(Color::rgb_u8(0, 0, 0)));