diff --git a/src/graphics_imgui.rs b/src/graphics_imgui.rs index 3850f28d9f..7329b06381 100644 --- a/src/graphics_imgui.rs +++ b/src/graphics_imgui.rs @@ -13,6 +13,7 @@ use point_cloud::point_cloud_visualiser::{ColorizationStrategy, PointCloudVisual use point_cloud::viewer_image::ViewerImage; use webcam::WebcamWriter; use kinect::capture::device_capturer::CaptureProvider; +use std::path::Path; pub fn run(capture_provider: Arc, calibration_data: k4a_sys::k4a_calibration_t, enable_webcam: bool) { let mut webcam_writer = None; @@ -211,6 +212,11 @@ pub fn run(capture_provider: Arc, calibration_data: k4a_sys::k4 window.gl_swap_window(); if let Some(mut captures) = capture_provider.get_captures() { + /*for (i, capture) in captures.iter().enumerate() { + let suffix = format!("{}", i); + capture.debug_save_files(Path::new("output/"), &suffix).unwrap(); + }*/ + visualizer.update_texture_id(texture.texture_id(), captures) .map(|_| {}) .map_err(|err| { diff --git a/src/kinect/capture/fake_device_capturer.rs b/src/kinect/capture/fake_device_capturer.rs index 1889fa7e73..013f8dc5f1 100644 --- a/src/kinect/capture/fake_device_capturer.rs +++ b/src/kinect/capture/fake_device_capturer.rs @@ -11,16 +11,14 @@ pub struct FakeDeviceCaptureProvider { impl FakeDeviceCaptureProvider { pub fn new() -> AnyhowResult { - // TODO: Dimensions are wrong - let color = ImageProxy::from_file("output/color_src_0", 0, 0, 0, ImageFormat::ColorBgra32)?; - let depth = ImageProxy::from_file("output/depth_src_0", 3840, 2160, 7680, ImageFormat::Depth16)?; - //let depth = ImageProxy::from_file("output/depth_src_0", 640, 576, 1280, ImageFormat::Depth16)?; + // TODO: Color dimensions are wrong + let color = ImageProxy::from_file("output/color_frame_0", 0, 0, 0, ImageFormat::ColorBgra32)?; + let depth = ImageProxy::from_file("output/depth_frame_0", 640, 576, 1280, ImageFormat::Depth16)?; let frame_1 = CaptureProxy::from_image_proxy_pair(color, depth); - // TODO: Dimensions are wrong - let color = ImageProxy::from_file("output/color_src_1", 0, 0, 0, ImageFormat::ColorBgra32)?; - let depth = ImageProxy::from_file("output/depth_src_1", 3840, 2160, 7680, ImageFormat::Depth16)?; - //let depth = ImageProxy::from_file("output/depth_src_1", 640, 576, 1280, ImageFormat::Depth16)?; + // TODO: Color dimensions are wrong + let color = ImageProxy::from_file("output/color_frame_1", 0, 0, 0, ImageFormat::ColorBgra32)?; + let depth = ImageProxy::from_file("output/depth_frame_1", 640, 576, 1280, ImageFormat::Depth16)?; let frame_2 = CaptureProxy::from_image_proxy_pair(color, depth); let mut captures = Vec::new(); diff --git a/src/main.rs b/src/main.rs index 7db9558275..a78e639b76 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,7 +70,7 @@ pub fn main() { //thread::spawn(move || capture_thread(capture_provider, Some(primary_device), false)); thread::spawn(move || start_capture_thread(multi_device)); - //let capture_provider = Arc::new(FakeDeviceCaptureProvider::new().unwrap()); + let capture_provider = Arc::new(FakeDeviceCaptureProvider::new().unwrap()); graphics_imgui::run(capture_provider, calibration, ENABLE_WEBCAM); } diff --git a/src/point_cloud/debug/capture_proxy.rs b/src/point_cloud/debug/capture_proxy.rs index 8aa98ef922..aa612b23a6 100644 --- a/src/point_cloud/debug/capture_proxy.rs +++ b/src/point_cloud/debug/capture_proxy.rs @@ -4,6 +4,7 @@ use point_cloud::debug::image_proxy::ImageProxy; use std::sync::{RwLock, Mutex, PoisonError, MutexGuard, Arc}; use winit::event::VirtualKeyCode::Mute; use kinect::k4a_sys_wrapper::CaptureError; +use std::path::Path; /// Store either a wrapped capture, or a k4a Capture. /// Useful for debugging. @@ -98,4 +99,24 @@ impl CaptureProxy { UnderlyingStorage::K4aCapture { capture, .. } => capture.clone(), } } + + pub fn debug_save_files(&self, path_prefix: &Path, suffix: &str) -> AnyhowResult<()> { + match &self.storage { + UnderlyingStorage::CameraImageBytes{ .. } => unimplemented!("No need to debug save for raw bytes"), + UnderlyingStorage::K4aCapture { maybe_color_image, maybe_depth_image, .. } => { + + if let Some(color_image) = maybe_color_image { + let filename = path_prefix.join(format!("./color_frame_{}", suffix)); + color_image.debug_save_file(filename.to_str().unwrap())?; + } + + if let Some(depth_image) = maybe_depth_image { + let filename = path_prefix.join(format!("./depth_frame_{}", suffix)); + depth_image.debug_save_file(filename.to_str().unwrap())?; + } + + Ok(()) + }, + } + } } diff --git a/src/point_cloud/debug/image_proxy.rs b/src/point_cloud/debug/image_proxy.rs index b58035d00c..58fa19414c 100644 --- a/src/point_cloud/debug/image_proxy.rs +++ b/src/point_cloud/debug/image_proxy.rs @@ -6,6 +6,7 @@ use std::fs; use std::io::Read; use kinect::k4a_sys_wrapper::ImageFormat; use std::ptr::null_mut; +use files::write_to_file_from_byte_ptr::write_to_file_from_byte_ptr; /// 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) @@ -22,7 +23,7 @@ enum UnderlyingStorage { bytes: Vec, width: usize, height: usize, - stride_bytes: i32, + stride_bytes: usize, format: ImageFormat, }, /// From the Kinect camera @@ -35,7 +36,7 @@ impl ImageProxy { filename: &str, width: usize, height: usize, - stride_bytes: i32, + stride_bytes: usize, format: ImageFormat) -> AnyhowResult { let mut file = File::open(filename)?; @@ -117,6 +118,20 @@ impl ImageProxy { } } + pub fn get_stride_bytes(&self) -> usize { + match &self.storage { + UnderlyingStorage::Bytes { stride_bytes, .. } => *stride_bytes, + UnderlyingStorage::K4aImage(image) => image.get_stride_bytes(), + } + } + + pub fn get_format(&self) -> ImageFormat { + match &self.storage { + UnderlyingStorage::Bytes { format, .. } => *format, + UnderlyingStorage::K4aImage(image) => image.get_format(), + } + } + pub fn get_handle(&self) -> k4a_sys::k4a_image_t { match &self.storage { UnderlyingStorage::Bytes { bytes, width, height, stride_bytes, format } => { @@ -131,7 +146,7 @@ impl ImageProxy { format.to_k4a(), *width as i32, *height as i32, - *stride_bytes, + *stride_bytes as i32, bytes.as_ptr() as *mut u8, // TODO: Technically we won't write. bytes.len(), None, @@ -148,4 +163,15 @@ impl ImageProxy { }, } } + + pub fn debug_save_file(&self, filename: &str) -> AnyhowResult<()> { + match &self.storage { + UnderlyingStorage::Bytes { .. } => unimplemented!("No need to debug save raw bytes"), + UnderlyingStorage::K4aImage(image) => { + let byte_src = image.get_buffer(); + let size_bytes = image.get_size(); + write_to_file_from_byte_ptr(filename, byte_src, size_bytes) + } + } + } } diff --git a/src/point_cloud/point_cloud_visualiser.rs b/src/point_cloud/point_cloud_visualiser.rs index c92b6b9be3..f06b5b80bb 100644 --- a/src/point_cloud/point_cloud_visualiser.rs +++ b/src/point_cloud/point_cloud_visualiser.rs @@ -393,6 +393,35 @@ impl PointCloudVisualizer { .as_mut() { unsafe { + /* + BROKEN FAKE DEVICE CAPTURE: + Depth image: 3840x2160 stride 7680 format Depth16 + Transformed depth image: 3840x2160 stride 7680 format Depth16 + [2020-08-16 02:32:59.296] [error] [t=22196] /__w/1/s/extern/Azure-Kinect-Sensor-SDK/src/transformation/rgbz.c (85): transformation_compare_image_descriptors(). + Unexpected image descriptor. + Expected width_pixels: 3840, height_pixels: 2160, stride_bytes: 7680, format: 4. + Actual width_pixels: 640, height_pixels: 576, stride_bytes: 1280, format: 4. + + + WORKING + Depth image: 640x576 stride 1280 format Depth16 + Transformed depth image: 3840x2160 stride 7680 format Depth16 + + */ + + println!("Depth image: {}x{} stride {} format {:?}", + depth_image.get_width_pixels(), + depth_image.get_height_pixels(), + depth_image.get_stride_bytes(), + depth_image.get_format()); + + // TODO: It's going to mutate this handle -- even if we fix the bug, that seems bad... + println!("Transformed depth image: {}x{} stride {} format {:?}", + transformed_depth_image.get_width_pixels(), + transformed_depth_image.get_height_pixels(), + transformed_depth_image.get_stride_bytes(), + transformed_depth_image.get_format()); + let result = k4a_sys::k4a_transformation_depth_image_to_color_camera( self.transformation.get_handle(), depth_image.get_handle(), @@ -400,6 +429,7 @@ impl PointCloudVisualizer { ); if result != k4a_sys::k4a_buffer_result_t_K4A_BUFFER_RESULT_SUCCEEDED { + println!(">>> THIS FAILS <<<"); return Err(PointCloudVisualizerError::DepthToColorConversionFailed); }