Adjusted update to image size

This commit is contained in:
Sebastian 2022-02-12 23:00:52 +01:00
parent 335dca9246
commit bb56972ec8
2 changed files with 22 additions and 30 deletions

View File

@ -1,8 +1,4 @@
use std::fs::File;
use std::io::prelude::*;
use std::path::Path; use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time;
use amdemod::SquaringAMDemodulator; use amdemod::SquaringAMDemodulator;
use aptsyncer::{APTSyncer, SyncedSample}; use aptsyncer::{APTSyncer, SyncedSample};
@ -81,7 +77,7 @@ const LOWPASS_COEFFS: [f32; 63] = [
pub fn decode<T>(input_file: &str, output_file: &str, progress_update: T) -> Result<(), String> pub fn decode<T>(input_file: &str, output_file: &str, progress_update: T) -> Result<(), String>
where where
T: Fn(f32, image::RgbaImage) -> bool, T: Fn(f32, image::RgbaImage) -> (bool, u32),
{ {
let mut reader = hound::WavReader::open(input_file) let mut reader = hound::WavReader::open(input_file)
.map_err(|err| format!("Could not open inputfile: {}", err))?; .map_err(|err| format!("Could not open inputfile: {}", err))?;
@ -91,7 +87,6 @@ where
} }
let sample_rate = reader.spec().sample_rate; let sample_rate = reader.spec().sample_rate;
println!("Samplerate: {}", sample_rate);
if sample_rate != 48000 { if sample_rate != 48000 {
return Err("Expected a 48kHz sample rate".to_owned()); return Err("Expected a 48kHz sample rate".to_owned());
} }
@ -99,7 +94,6 @@ where
let sample_count = reader.len(); let sample_count = reader.len();
let seconds = (sample_count as f32) / (sample_rate as f32); let seconds = (sample_count as f32) / (sample_rate as f32);
let lines = (seconds.ceil() as u32) * LINES_PER_SECOND; let lines = (seconds.ceil() as u32) * LINES_PER_SECOND;
println!("File contains {} seconds or {} lines", seconds, lines);
let mut img = image::DynamicImage::ImageLuma8(image::ImageBuffer::new(PIXELS_PER_LINE, lines)); let mut img = image::DynamicImage::ImageLuma8(image::ImageBuffer::new(PIXELS_PER_LINE, lines));
@ -119,20 +113,14 @@ where
let mut has_sync = false; let mut has_sync = false;
let mut progress = 0; let mut progress = 0;
let step = sample_count * 13 / 150 / 10; let pixel_count = sample_count * 13 / 150;
let mut update_step = 10;
let mut previous_sample = 0.0; let mut previous_sample = 0.0;
print!("0%");
std::io::stdout().flush().unwrap();
for synced_sample in syncer { for synced_sample in syncer {
progress += 1; progress += 1;
if progress % step == 0 {
print!("...{}%", progress / step * 10);
std::io::stdout().flush().unwrap();
}
let sample = match synced_sample { let sample = match synced_sample {
SyncedSample::Sample(s) => s, SyncedSample::Sample(s) => s,
SyncedSample::SyncA(s) => { SyncedSample::SyncA(s) => {
@ -179,20 +167,22 @@ where
previous_sample = sample; previous_sample = sample;
if progress % (PIXELS_PER_LINE * 4) == 0 { if progress % (PIXELS_PER_LINE * update_step) == 0 {
if !progress_update((progress as f32) / (step * 10) as f32, img.to_rgba8()) { let (cont, update_steps) =
progress_update((progress as f32) / (pixel_count as f32), img.to_rgba8());
if !cont {
return Ok(()); return Ok(());
} }
let line_count = pixel_count / PIXELS_PER_LINE;
update_step = line_count / update_steps;
} }
} }
println!("");
progress_update(1.0, img.to_rgba8()); progress_update(1.0, img.to_rgba8());
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))?;
println!("Done !");
Ok(()) Ok(())
} }

View File

@ -1,5 +1,4 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time;
use eframe::egui::text_edit::TextEdit; use eframe::egui::text_edit::TextEdit;
use eframe::egui::widgets::{Button, ProgressBar}; use eframe::egui::widgets::{Button, ProgressBar};
@ -16,8 +15,9 @@ enum DecoderRunState {
} }
struct DecoderJobState { struct DecoderJobState {
update_steps: u32,
progress: f32, progress: f32,
texture: Option<(egui::TextureId, egui::Vec2)>, texture: Option<egui::TextureId>,
run_state: DecoderRunState, run_state: DecoderRunState,
} }
@ -30,6 +30,7 @@ impl DecoderJobState {
impl Default for DecoderJobState { impl Default for DecoderJobState {
fn default() -> Self { fn default() -> Self {
Self { Self {
update_steps: 10,
progress: 0.0, progress: 0.0,
texture: None, texture: None,
run_state: DecoderRunState::DONE, run_state: DecoderRunState::DONE,
@ -56,7 +57,7 @@ impl Default for DecoderApp {
impl epi::App for DecoderApp { impl epi::App for DecoderApp {
fn name(&self) -> &str { fn name(&self) -> &str {
"eframe template" "APT Decoder"
} }
/// Called once before the first frame. /// Called once before the first frame.
@ -143,17 +144,15 @@ impl epi::App for DecoderApp {
size, size,
image.as_flat_samples().as_slice(), image.as_flat_samples().as_slice(),
); );
let size = egui::Vec2::new(size[0] as f32, size[1] as f32);
if let Some((old_texture, _)) = state.texture { if let Some(old_texture) = state.texture {
frame.free_texture(old_texture); frame.free_texture(old_texture);
} }
state.texture = Some(frame.alloc_texture(epi_img));
state.texture = Some((frame.alloc_texture(epi_img), size));
frame.request_repaint(); frame.request_repaint();
return state.is_running(); return (state.is_running(), state.update_steps);
}) })
.unwrap(); .unwrap();
@ -179,8 +178,11 @@ impl epi::App for DecoderApp {
ui.separator(); ui.separator();
if let Some((texture, size)) = state.texture { let image_size = ui.available_size();
ui.image(texture, ui.available_size()); state.update_steps = image_size[1] as u32;
if let Some(texture) = state.texture {
ui.image(texture, image_size);
} }
}); });
} }