Fixed texture leak
This commit is contained in:
parent
cc8afc1b07
commit
70a8b41578
|
@ -175,12 +175,16 @@ where
|
||||||
|
|
||||||
previous_sample = sample;
|
previous_sample = sample;
|
||||||
|
|
||||||
if !progress_update((progress as f32) / (step * 10) as f32, img.clone()) {
|
if progress % LINES_PER_SECOND == 0 {
|
||||||
return Ok(());
|
if !progress_update((progress as f32) / (step * 10) as f32, img.clone()) {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("");
|
println!("");
|
||||||
|
|
||||||
|
progress_update(1.0, img.clone());
|
||||||
|
|
||||||
img.save_with_format(&Path::new(output_file), image::ImageFormat::Png)
|
img.save_with_format(&Path::new(output_file), image::ImageFormat::Png)
|
||||||
.map_err(|err| format!("Could not save outputfile: {}", err))?;
|
.map_err(|err| format!("Could not save outputfile: {}", err))?;
|
||||||
|
|
||||||
|
|
39
src/ui.rs
39
src/ui.rs
|
@ -17,7 +17,7 @@ enum DecoderRunState {
|
||||||
|
|
||||||
struct DecoderJobState {
|
struct DecoderJobState {
|
||||||
progress: f32,
|
progress: f32,
|
||||||
image: image::GrayImage,
|
image: Option<image::GrayImage>,
|
||||||
run_state: DecoderRunState,
|
run_state: DecoderRunState,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ impl Default for DecoderJobState {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
progress: 0.0,
|
progress: 0.0,
|
||||||
image: image::GrayImage::new(1, 1),
|
image: None,
|
||||||
run_state: DecoderRunState::DONE,
|
run_state: DecoderRunState::DONE,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,7 @@ pub struct DecoderApp {
|
||||||
input_path: String,
|
input_path: String,
|
||||||
output_path: String,
|
output_path: String,
|
||||||
decoding_state: Arc<Mutex<DecoderJobState>>,
|
decoding_state: Arc<Mutex<DecoderJobState>>,
|
||||||
|
texture: Option<(egui::TextureId, egui::Vec2)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DecoderApp {
|
impl Default for DecoderApp {
|
||||||
|
@ -50,6 +51,7 @@ impl Default for DecoderApp {
|
||||||
input_path: "input.wav".to_owned(),
|
input_path: "input.wav".to_owned(),
|
||||||
output_path: "output.png".to_owned(),
|
output_path: "output.png".to_owned(),
|
||||||
decoding_state: Arc::new(Mutex::new(DecoderJobState::default())),
|
decoding_state: Arc::new(Mutex::new(DecoderJobState::default())),
|
||||||
|
texture: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,6 +77,7 @@ impl epi::App for DecoderApp {
|
||||||
input_path,
|
input_path,
|
||||||
output_path,
|
output_path,
|
||||||
decoding_state,
|
decoding_state,
|
||||||
|
texture,
|
||||||
} = self;
|
} = self;
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -135,10 +138,12 @@ impl epi::App for DecoderApp {
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
decoder::decode(&input_path, &output_path, |progress, image| {
|
decoder::decode(&input_path, &output_path, |progress, image| {
|
||||||
let mut state = decoding_state.lock().unwrap();
|
let mut state = decoding_state.lock().unwrap();
|
||||||
|
|
||||||
state.progress = progress;
|
state.progress = progress;
|
||||||
state.image = image;
|
state.image = Some(image);
|
||||||
|
|
||||||
frame.request_repaint();
|
frame.request_repaint();
|
||||||
|
|
||||||
return state.is_running();
|
return state.is_running();
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -163,16 +168,28 @@ impl epi::App for DecoderApp {
|
||||||
|
|
||||||
ui.separator();
|
ui.separator();
|
||||||
|
|
||||||
let size = [state.image.width() as _, state.image.height() as _];
|
if let Some(image) = state.image.take() {
|
||||||
let image_buffer =
|
let image = image::DynamicImage::ImageLuma8(image);
|
||||||
image::DynamicImage::ImageLuma8(state.image.clone()).into_rgba8();
|
let size = [image.width() as _, image.height() as _];
|
||||||
let pixels = image_buffer.as_flat_samples();
|
let pixels = image.into_rgba8();
|
||||||
let epi_img = epi::Image::from_rgba_unmultiplied(size, pixels.as_slice());
|
let epi_img = epi::Image::from_rgba_unmultiplied(
|
||||||
|
size,
|
||||||
|
pixels.as_flat_samples().as_slice(),
|
||||||
|
);
|
||||||
|
let size = egui::Vec2::new(size[0] as f32, size[1] as f32);
|
||||||
|
|
||||||
let texture = frame.alloc_texture(epi_img);
|
if let Some((old_texture, _)) = texture {
|
||||||
let size = egui::Vec2::new(size[0] as f32, size[1] as f32);
|
frame.free_texture(*old_texture);
|
||||||
|
}
|
||||||
|
|
||||||
ui.image(texture, size);
|
*texture = Some((frame.alloc_texture(epi_img), size));
|
||||||
|
|
||||||
|
state.image = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some((texture, size)) = texture {
|
||||||
|
ui.image(*texture, *size);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue