diff --git a/src/analysis.rs b/src/analysis.rs index ea74ffa..c21da6a 100644 --- a/src/analysis.rs +++ b/src/analysis.rs @@ -12,7 +12,7 @@ use scrap::{Capturer, Display}; use crate::{ capture, config::{Config, LearnedConfig}, - image_processing::{self, extract_and_filter, hash_image, Region}, + image_processing::{self, extract_and_filter, hash_image, Region, to_png_bytes}, ocr, state::{AppState, DebugOcrFrame, LapState, RaceState, SharedAppState}, }; @@ -111,7 +111,7 @@ fn handle_new_frame(state: &mut AppState, frame: LapState, image: RgbImage) { if let Some(lap) = &merged.lap { merged.lap = Some(lap - 1); } - merged.screenshot = Some(image); + merged.screenshot = Some(to_png_bytes(&image)); if let Some(race) = state.current_race.as_mut() { if let Some(prev_lap) = race.laps.last() { diff --git a/src/image_processing.rs b/src/image_processing.rs index 535e886..4f0a5d7 100644 --- a/src/image_processing.rs +++ b/src/image_processing.rs @@ -1,4 +1,4 @@ -use image::{codecs::png::PngEncoder, ColorType, ImageEncoder, Rgb, RgbImage}; +use image::{codecs::png::{PngEncoder, PngDecoder}, ColorType, ImageEncoder, Rgb, RgbImage, DynamicImage}; use serde::{Deserialize, Serialize}; @@ -74,6 +74,11 @@ pub fn to_png_bytes(image: &RgbImage) -> Vec { buffer } +pub fn from_png_bytes(bytes: &[u8]) -> RgbImage { + let image = image::load_from_memory(bytes).expect("expected valid image"); + image.to_rgb8() +} + pub fn hash_image(image: &RgbImage) -> String { let hasher = img_hash::HasherConfig::new().hash_alg(img_hash::HashAlg::Mean).hash_size(16, 16).to_hasher(); let have_to_use_other_image_library_version = diff --git a/src/main.rs b/src/main.rs index 35ee4d4..b3b1c40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ use eframe::{ epaint::Color32, }; use egui_extras::RetainedImage; -use image_processing::to_png_bytes; +use image_processing::{to_png_bytes, from_png_bytes}; use state::{AppState, DebugOcrFrame, LapState, OcrCache, RaceState, SharedAppState}; use stats_writer::export_race_stats; @@ -296,12 +296,13 @@ fn open_debug_lap( learned: Arc, ocr_cache: Arc, ) { - if let Some(screenshot) = &lap.screenshot { - let ocr_results = ocr::ocr_all_regions(screenshot, config.clone(), learned, ocr_cache); + if let Some(screenshot_bytes) = &lap.screenshot { + let screenshot = from_png_bytes(screenshot_bytes); + let ocr_results = ocr::ocr_all_regions(&screenshot, config.clone(), learned, ocr_cache); let debug_lap = DebugLap { - screenshot: RetainedImage::from_image_bytes("debug-lap", &to_png_bytes(screenshot)) + screenshot: RetainedImage::from_image_bytes("debug-lap", &to_png_bytes(&screenshot)) .unwrap(), - debug_regions: save_frames_from(screenshot, &*config, &ocr_results), + debug_regions: save_frames_from(&screenshot, &*config, &ocr_results), }; ui_state.debug_lap = Some(debug_lap); diff --git a/src/state.rs b/src/state.rs index 9ad24f3..86b83ba 100644 --- a/src/state.rs +++ b/src/state.rs @@ -19,7 +19,7 @@ pub struct LapState { pub lap_time: Option, pub striked: bool, - pub screenshot: Option, + pub screenshot: Option>, pub debug: bool, }