Skip to content

Commit a87e142

Browse files
committed
saving sim image, the brightness is different idk why
1 parent ec9bdec commit a87e142

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

Cargo.lock

+44-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ bevy_file_dialog = "0.4.0"
1010
bevy_pixel_buffer = { version = "0.7.0", features = ["egui"] }
1111
colorgrad = "0.6.2"
1212
egui_plot = "0.26.2"
13+
lodepng = "3.10.1"
1314
rand = "0.8.5"
1415
rayon = "1.8.0"
1516
serde = { version = "1.0.197", features = ["serde_derive"] }

src/ui/draw.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use crate::components::wall::WallBlock;
1515
use crate::grid::grid::Grid;
1616
use crate::math::constants::*;
1717
use crate::math::fft::calc_mic_spectrum;
18-
use crate::math::transformations::f32_map_range;
18+
use crate::math::transformations::{coords_to_index, f32_map_range};
19+
use crate::render::draw::GradientResource;
1920
use crate::ui::state::*;
2021

2122
pub fn draw_egui(
@@ -26,6 +27,7 @@ pub fn draw_egui(
2627
mut egui_context: EguiContexts,
2728
mut ui_state: ResMut<UiState>,
2829
mut grid: ResMut<Grid>,
30+
gradient: Res<GradientResource>,
2931
mut wallblock_set: ParamSet<(
3032
Query<(Entity, &mut WallBlock), Without<Overlay>>,
3133
Query<(Entity, &mut WallBlock), (Without<Overlay>, With<Selected>)>,
@@ -139,6 +141,46 @@ pub fn draw_egui(
139141
.set_title("Select a file to load")
140142
.load_file::<SaveFileContents>();
141143
}
144+
145+
if ui
146+
.button("Save Image")
147+
.on_hover_text("Save the current iamge of the simulation")
148+
.clicked()
149+
{
150+
let mut pixels: Vec<[u8; 3]> = Vec::new();
151+
152+
for y in ui_state.e_al..(SIMULATION_WIDTH + ui_state.e_al) {
153+
for x in ui_state.e_al..(SIMULATION_HEIGHT + ui_state.e_al) {
154+
let pressure =
155+
grid.cells[coords_to_index(x, y, ui_state.e_al)].pressure;
156+
157+
let color = gradient.0.at((pressure) as f64);
158+
159+
pixels.push([
160+
(color.r * 255.) as u8,
161+
(color.g * 255.) as u8,
162+
(color.b * 255.) as u8,
163+
]);
164+
}
165+
}
166+
167+
let data = lodepng::encode_memory(
168+
&pixels,
169+
SIMULATION_WIDTH as usize,
170+
SIMULATION_HEIGHT as usize,
171+
lodepng::ColorType::RGB,
172+
8,
173+
)
174+
.expect("");
175+
176+
commands
177+
.dialog()
178+
.add_filter("Jpeg", &["jpeg"])
179+
.set_file_name("save.jpg")
180+
.set_directory("./")
181+
.set_title("Select a file to save to")
182+
.save_file::<SaveFileContents>(data);
183+
}
142184
});
143185

144186
// Sources

0 commit comments

Comments
 (0)