write both color and depth images to file

This commit is contained in:
Brandon Thomas committed 2020-08-15 19:34:14 -04:00
1 parent e33bed276a
commit 0d3f67691d
5 files changed
+67 -25

No files matched your search

+2 -1
View File
@@ -1 +1,2 @@
pub mod read_file_string_contents;
pub mod read_file_string_contents;
pub mod write_to_file_from_byte_ptr;
+23
View File
@@ -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(())
}
+33 -2
View File
@@ -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<T> = std::result::Result<T, PointCloudComputeError>;
@@ -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::<u16>();
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::<DepthPixel>(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(())
}
}
+7 -21
View File
@@ -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<T> = std::result::Result<T, PointCloudRendererError>;
@@ -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(())
}
+2 -1
View File
@@ -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 {