From fb78650fbbbfa8cb2cc8e9392881b1724fe0014f Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sat, 15 Aug 2020 22:54:15 -0400 Subject: [PATCH] rename awkward CameraImageBytes to ImageProxy --- src/point_cloud/debug/capture_proxy.rs | 23 +++++++++++-------- .../{camera_image_bytes.rs => image_proxy.rs} | 4 ++-- src/point_cloud/debug/mod.rs | 2 +- src/point_cloud/gpu_point_cloud_converter.rs | 4 ++-- src/point_cloud/point_cloud_renderer.rs | 4 ++-- src/point_cloud/point_cloud_visualiser.rs | 22 +++++++++--------- 6 files changed, 31 insertions(+), 28 deletions(-) rename src/point_cloud/debug/{camera_image_bytes.rs => image_proxy.rs} (98%) diff --git a/src/point_cloud/debug/capture_proxy.rs b/src/point_cloud/debug/capture_proxy.rs index 4bd263e0ed..0ef162ad80 100644 --- a/src/point_cloud/debug/capture_proxy.rs +++ b/src/point_cloud/debug/capture_proxy.rs @@ -1,6 +1,6 @@ use anyhow::Result as AnyhowResult; use kinect::k4a_sys_wrapper; -use point_cloud::debug::camera_image_bytes::CameraImageBytes; +use point_cloud::debug::image_proxy::ImageProxy; use std::sync::{RwLock, Mutex, PoisonError, MutexGuard, Arc}; use winit::event::VirtualKeyCode::Mute; @@ -14,19 +14,19 @@ pub struct CaptureProxy { #[derive(Clone)] enum UnderlyingStorage { CameraImageBytes { - color_image: CameraImageBytes, - depth_image: CameraImageBytes, + color_image: ImageProxy, + depth_image: ImageProxy, }, K4aCapture { capture: k4a_sys_wrapper::Capture, - color_image: CameraImageBytes, - depth_image: CameraImageBytes, + color_image: ImageProxy, + depth_image: ImageProxy, } } impl CaptureProxy { - pub fn from_image_proxy_pair(color_image: CameraImageBytes, depth_image: CameraImageBytes) -> Self { + pub fn from_image_proxy_pair(color_image: ImageProxy, depth_image: ImageProxy) -> Self { Self { storage: UnderlyingStorage::CameraImageBytes { color_image, @@ -39,14 +39,17 @@ impl CaptureProxy { // NB: We need to increase the refcount. // K4a manages the memory under the hood. let capture = capture.clone(); + Self::consume_k4a_image(capture) + } + pub fn consume_k4a_image(capture: k4a_sys_wrapper::Capture) -> AnyhowResult { let color_image = capture.get_color_image()?; let depth_image = capture.get_color_image()?; // NB: I tried to lazily unpack these, but interior mutability Sync/Send was a nightmare. // The poor ergonomics were not worth it. - let color_image_wrapper = CameraImageBytes::from_k4a_image(&color_image); - let depth_image_wrapper = CameraImageBytes::from_k4a_image(&depth_image); + let color_image_wrapper = ImageProxy::from_k4a_image(&color_image); + let depth_image_wrapper = ImageProxy::from_k4a_image(&depth_image); Ok(Self { storage: UnderlyingStorage::K4aCapture { @@ -64,14 +67,14 @@ impl CaptureProxy { } } - pub fn get_color_image(&self) -> AnyhowResult<&CameraImageBytes> { + pub fn get_color_image(&self) -> AnyhowResult<&ImageProxy> { match &self.storage { UnderlyingStorage::CameraImageBytes { color_image, .. } => Ok(color_image), UnderlyingStorage::K4aCapture{ color_image, .. } => Ok(color_image), } } - pub fn get_depth_image(&self) -> AnyhowResult<&CameraImageBytes> { + pub fn get_depth_image(&self) -> AnyhowResult<&ImageProxy> { match &self.storage { UnderlyingStorage::CameraImageBytes { depth_image, .. } => Ok(depth_image), UnderlyingStorage::K4aCapture{ depth_image, .. } => Ok(depth_image), diff --git a/src/point_cloud/debug/camera_image_bytes.rs b/src/point_cloud/debug/image_proxy.rs similarity index 98% rename from src/point_cloud/debug/camera_image_bytes.rs rename to src/point_cloud/debug/image_proxy.rs index 5f38574434..3919135cdd 100644 --- a/src/point_cloud/debug/camera_image_bytes.rs +++ b/src/point_cloud/debug/image_proxy.rs @@ -7,7 +7,7 @@ use kinect::k4a_sys_wrapper; /// Store either raw bytes, or wrap a k4a::Image /// This is meant to be plumbed through the system instead of a k4a::Image (depth or color image) #[derive(Clone)] -pub struct CameraImageBytes { +pub struct ImageProxy { storage: UnderlyingStorage, } @@ -24,7 +24,7 @@ enum UnderlyingStorage { K4aImage(k4a_sys_wrapper::Image), } -impl CameraImageBytes { +impl ImageProxy { pub fn from_file(filename: &str, width: usize, height: usize) -> AnyhowResult { let mut file = File::open(filename)?; let metadata = fs::metadata(&filename)?; diff --git a/src/point_cloud/debug/mod.rs b/src/point_cloud/debug/mod.rs index 2ae688d69c..e4df8b8377 100644 --- a/src/point_cloud/debug/mod.rs +++ b/src/point_cloud/debug/mod.rs @@ -1,2 +1,2 @@ -pub mod camera_image_bytes; +pub mod image_proxy; pub mod capture_proxy; diff --git a/src/point_cloud/gpu_point_cloud_converter.rs b/src/point_cloud/gpu_point_cloud_converter.rs index e4d03da068..2d56e948ff 100644 --- a/src/point_cloud/gpu_point_cloud_converter.rs +++ b/src/point_cloud/gpu_point_cloud_converter.rs @@ -22,7 +22,7 @@ use opengl::opengl_wrapper::{Buffer, gl_get_error}; use point_cloud::pixel_structs::DepthPixel; use files::read_file_string_contents::read_file_string_contents; use files::write_to_file_from_byte_ptr::write_to_file_from_byte_ptr; -use point_cloud::debug::camera_image_bytes::CameraImageBytes; +use point_cloud::debug::image_proxy::ImageProxy; pub type Result = std::result::Result; @@ -150,7 +150,7 @@ impl GpuPointCloudConverter { /// pub fn convert(&self, //depth_image: &k4a_sys_wrapper::Image, - depth_image: &CameraImageBytes, + depth_image: &ImageProxy, output_texture: &mut Texture, camera_index: usize ) -> Result<()> { diff --git a/src/point_cloud/point_cloud_renderer.rs b/src/point_cloud/point_cloud_renderer.rs index 5e3c2ffb76..73e8ceb199 100644 --- a/src/point_cloud/point_cloud_renderer.rs +++ b/src/point_cloud/point_cloud_renderer.rs @@ -26,7 +26,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; use std::fs::{File, OpenOptions}; use std::io::{Read, Write}; use opengl::link_program::link_shader_program; -use point_cloud::debug::camera_image_bytes::CameraImageBytes; +use point_cloud::debug::image_proxy::ImageProxy; use files::read_file_string_contents::read_file_string_contents; use files::write_to_file_from_byte_ptr::write_to_file_from_byte_ptr; @@ -311,7 +311,7 @@ impl PointCloudRenderer { /// /// pub fn update_point_clouds(&mut self, - color_images: &Vec, + color_images: &Vec, point_cloud_textures: &Vec ) -> Result<()> { let time = SystemTime::now(); diff --git a/src/point_cloud/point_cloud_visualiser.rs b/src/point_cloud/point_cloud_visualiser.rs index 7bdbee038f..ecae2c4b30 100644 --- a/src/point_cloud/point_cloud_visualiser.rs +++ b/src/point_cloud/point_cloud_visualiser.rs @@ -20,7 +20,7 @@ use point_cloud::pixel_structs::DepthPixel; use point_cloud::point_cloud_renderer::{PointCloudRenderer, PointCloudRendererError}; use point_cloud::util::{colorize_depth_blue_to_red, get_depth_mode_range, ValueRange}; use point_cloud::viewer_image::ViewerImage; -use point_cloud::debug::camera_image_bytes::CameraImageBytes; +use point_cloud::debug::image_proxy::ImageProxy; pub type Result = std::result::Result; @@ -137,8 +137,8 @@ pub struct PointCloudVisualizer { /// For debugging /// If present, use these instead of camera captures. - debug_static_color_frames: Vec, - debug_static_depth_frames: Vec, + debug_static_color_frames: Vec, + debug_static_depth_frames: Vec, } // TODO: Dedup @@ -210,12 +210,12 @@ impl PointCloudVisualizer { } let mut debug_static_color_frames = Vec::new(); - debug_static_color_frames.push(CameraImageBytes::from_file("output/color_src_0", 0, 0).unwrap()); - debug_static_color_frames.push(CameraImageBytes::from_file("output/color_src_1", 0, 0).unwrap()); + debug_static_color_frames.push(ImageProxy::from_file("output/color_src_0", 0, 0).unwrap()); + debug_static_color_frames.push(ImageProxy::from_file("output/color_src_1", 0, 0).unwrap()); let mut debug_static_depth_frames = Vec::new(); - debug_static_depth_frames.push(CameraImageBytes::from_file("output/depth_src_0", 3840, 2160).unwrap()); - debug_static_depth_frames.push(CameraImageBytes::from_file("output/depth_src_1", 3840, 2160).unwrap()); + debug_static_depth_frames.push(ImageProxy::from_file("output/depth_src_0", 3840, 2160).unwrap()); + debug_static_depth_frames.push(ImageProxy::from_file("output/depth_src_1", 3840, 2160).unwrap()); let mut visualizer = Self { num_cameras, @@ -352,9 +352,9 @@ impl PointCloudVisualizer { } else { // TODO: wrt k4a::Image.clone(), k4a should use ref counting. (I did this one other place a few commits ago. // Hope it's not ref leaking.) - let colorizations: Vec = self.point_cloud_colorizations.iter() + let colorizations: Vec = self.point_cloud_colorizations.iter() .map(|img| img.as_ref().map(|img| img.clone()).unwrap()) // Potential ref leak #1 - .map(|image| CameraImageBytes::from_k4a_image(&image)) // Potential ref leak #2 + .map(|image| ImageProxy::from_k4a_image(&image)) // Potential ref leak #2 .collect(); color_captures = colorizations; @@ -411,12 +411,12 @@ impl PointCloudVisualizer { // // Hack to hold onto the K4A ColorImageBytes wrapper. - let mut depth_image_storage : Option = None; + let mut depth_image_storage : Option = None; let depth_image = if let Some(image) = self.debug_static_depth_frames.get(camera_index) { image } else { - let camera_image = CameraImageBytes::from_k4a_image(&depth_image); + let camera_image = ImageProxy::from_k4a_image(&depth_image); depth_image_storage = Some(camera_image); depth_image_storage.as_ref().unwrap() };