From 0d3f67691df8cf5c6ccdb387bc17be483dd5042e Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sat, 15 Aug 2020 19:34:14 -0400 Subject: [PATCH] write both color and depth images to file --- src/files/mod.rs | 3 +- src/files/write_to_file_from_byte_ptr.rs | 23 +++++++++++++ src/point_cloud/gpu_point_cloud_converter.rs | 35 ++++++++++++++++++-- src/point_cloud/point_cloud_renderer.rs | 28 ++++------------ src/point_cloud/point_cloud_visualiser.rs | 3 +- 5 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 src/files/write_to_file_from_byte_ptr.rs diff --git a/src/files/mod.rs b/src/files/mod.rs index 9ba4e750e9..1fd10f23eb 100644 --- a/src/files/mod.rs +++ b/src/files/mod.rs @@ -1 +1,2 @@ -pub mod read_file_string_contents; \ No newline at end of file +pub mod read_file_string_contents; +pub mod write_to_file_from_byte_ptr; diff --git a/src/files/write_to_file_from_byte_ptr.rs b/src/files/write_to_file_from_byte_ptr.rs new file mode 100644 index 0000000000..eaa99f523d --- /dev/null +++ b/src/files/write_to_file_from_byte_ptr.rs @@ -0,0 +1,23 @@ +use anyhow::Result as AnyhowResult; +use std::fs::OpenOptions; +use std::io::Write; +use std::ptr; + +/// Raw memory copy into a file +pub fn write_to_file_from_byte_ptr(filename: &str, byte_src: *const u8, size_bytes: usize) -> AnyhowResult<()> { + let mut file = OpenOptions::new() + .write(true) + .create(true) + .open(filename)?; + + let mut vec = vec![0u8; size_bytes]; + + unsafe { + let vec_ptr = vec.as_mut_ptr(); + //ptr::write_bytes(vec_ptr, color_src, size_bytes); + ptr::copy_nonoverlapping(byte_src, vec_ptr, size_bytes); + } + + file.write(&vec)?; + Ok(()) +} diff --git a/src/point_cloud/gpu_point_cloud_converter.rs b/src/point_cloud/gpu_point_cloud_converter.rs index 38c06a82dd..1bf8ffa095 100644 --- a/src/point_cloud/gpu_point_cloud_converter.rs +++ b/src/point_cloud/gpu_point_cloud_converter.rs @@ -21,6 +21,7 @@ use opengl::opengl_wrapper::Texture; 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; pub type Result = std::result::Result; @@ -148,8 +149,9 @@ impl GpuPointCloudConverter { /// pub fn convert(&self, depth_image: &k4a_sys_wrapper::Image, - output_texture: &mut Texture) -> Result<()> - { + output_texture: &mut Texture, + camera_index: usize + ) -> Result<()> { if !self.xy_table_texture.is_initialized() { return Err(PointCloudComputeError::UnknownError); } @@ -206,6 +208,11 @@ impl GpuPointCloudConverter { } let depth_image_buffer = depth_image.get_buffer(); + + let filename = format!("output/depth_src_{}", camera_index); + let size = (width * height) as usize * size_of::(); + write_to_file_from_byte_ptr(&filename, depth_image_buffer, size).unwrap(); + let typed_depth_image_buffer = depth_image_buffer as *const DepthPixel; std::ptr::copy::(typed_depth_image_buffer, texture_mapped_buffer, length as usize); @@ -286,6 +293,30 @@ impl GpuPointCloudConverter { // This is needed despite using glGetInteger(GL_ACTIVE_TEXTURE) and resetting that value. //gl::ActiveTexture(gl::TEXTURE0); + /* + // Try to save the texture buffer to file... (not working yet) + let size_bytes = (width * height * 4); + let mut vec = vec![0u8; size_bytes as usize]; + let mut pointer = vec.as_mut_ptr() as *mut c_void; + + // Pull the texture out as bytes. + //gl::ActiveTexture(gl::TEXTURE1); + gl::BindTexture(gl::TEXTURE_2D, output_texture.id()); + + gl_get_error().unwrap(); + + gl::GetTextureImage( + output_texture.id(), + 0, // level + gl::RGBA32F, // format + gl::UNSIGNED_BYTE, // type + size_bytes, // buffer size + pointer, + ); + + gl_get_error().unwrap(); + */ + Ok(()) } } diff --git a/src/point_cloud/point_cloud_renderer.rs b/src/point_cloud/point_cloud_renderer.rs index f62a0550d1..123ac7fee0 100644 --- a/src/point_cloud/point_cloud_renderer.rs +++ b/src/point_cloud/point_cloud_renderer.rs @@ -28,6 +28,7 @@ use std::io::{Read, Write}; use opengl::link_program::link_shader_program; use point_cloud::debug::color_image_bytes::ColorImageBytes; use files::read_file_string_contents::read_file_string_contents; +use files::write_to_file_from_byte_ptr::write_to_file_from_byte_ptr; pub type Result = std::result::Result; @@ -269,8 +270,8 @@ impl PointCloudRenderer { let mut debug_static_color_frames = Vec::new(); - debug_static_color_frames.push(ColorImageBytes::from_file("output/color_src_0").unwrap()); - debug_static_color_frames.push(ColorImageBytes::from_file("output/color_src_1").unwrap()); + //debug_static_color_frames.push(ColorImageBytes::from_file("output/color_src_0").unwrap()); + //debug_static_color_frames.push(ColorImageBytes::from_file("output/color_src_1").unwrap()); Self { num_cameras, @@ -354,6 +355,10 @@ impl PointCloudRenderer { } else { let color_image = color_images.get(i).unwrap(); let color_image_bytes = ColorImageBytes::from_k4a_image(&color_image); + + let filename = format!("output/color_src_{}", i); + write_to_file_from_byte_ptr(&filename, color_image_bytes.as_ptr(), color_image_bytes.len()).unwrap(); + color_image_storage = Some(color_image_bytes); color_image_storage.as_ref().unwrap() }; @@ -557,22 +562,3 @@ impl PointCloudRenderer { self.enable_shading = enable_shading; } } - -/// Raw memory copy into a file -fn write_file_from_byte_ptr(filename: &str, byte_src: *const u8, size_bytes: usize) -> AnyhowResult<()> { - let mut file = OpenOptions::new() - .write(true) - .create(true) - .open(filename)?; - - let mut vec = vec![0u8; size_bytes]; - - unsafe { - let vec_ptr = vec.as_mut_ptr(); - //ptr::write_bytes(vec_ptr, color_src, size_bytes); - ptr::copy_nonoverlapping(byte_src, vec_ptr, size_bytes); - } - - file.write(&vec)?; - Ok(()) -} diff --git a/src/point_cloud/point_cloud_visualiser.rs b/src/point_cloud/point_cloud_visualiser.rs index 216f9a0a82..657b761379 100644 --- a/src/point_cloud/point_cloud_visualiser.rs +++ b/src/point_cloud/point_cloud_visualiser.rs @@ -386,7 +386,8 @@ impl PointCloudVisualizer { let result = self.point_cloud_converters.get(camera_index).unwrap().convert( &depth_image, - &mut self.xyz_textures.get_mut(camera_index).unwrap() // TODO: TEMP MULTI CAMERA SUPPORT + &mut self.xyz_textures.get_mut(camera_index).unwrap(), // TODO: TEMP MULTI CAMERA SUPPORT + camera_index ); if let Err(err) = result {