converter-bin.hpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2018 Intel Corporation. All Rights Reserved.
3 
4 #ifndef __RS_CONVERTER_CONVERTER_BIN_H
5 #define __RS_CONVERTER_CONVERTER_BIN_H
6 
7 
8 #include <fstream>
9 #include <cmath>
10 
11 #include "../converter.hpp"
12 
13 
14 namespace rs2 {
15  namespace tools {
16  namespace converter {
17 
18  class converter_bin : public converter_base {
21 
22  protected:
23  static void* to_ieee754_32(float f, uint8_t* buffer)
24  {
25  uint32_t ieee754 = 0;
26 
27  if (f != 0.) {
28  uint32_t sign = 0;
29 
30  if (f < 0) {
31  sign = 1;
32  f = -f;
33  }
34 
35  int shift = 0;
36 
37  while (f >= 2.0) {
38  f /= 2.0;
39  shift++;
40  }
41 
42  while (f < 1.0) {
43  f *= 2.0;
44  shift--;
45  }
46 
47  f = f - 1.f;
48  uint32_t mantissa = uint32_t(f * ((1U << 23) + 0.5f));
49  uint32_t exponent = shift + ((1 << 7) - 1);
50 
51  ieee754 = (sign << 31) | (exponent << 23) | mantissa;
52  }
53 
54  buffer[0] = ieee754 & 0xff;
55  buffer[1] = (ieee754 >> 8) & 0xff;
56  buffer[2] = (ieee754 >> 16) & 0xff;
57  buffer[3] = (ieee754 >> 24) & 0xff;
58 
59  return buffer;
60  }
61 
62  public:
64  : _filePath(filePath)
65  , _streamType(streamType)
66  {
67  }
68 
69  std::string name() const override
70  {
71  return "BIN converter";
72  }
73 
74  void convert(rs2::frame& frame) override
75  {
76  rs2::depth_frame depthframe = frame.as<rs2::depth_frame>();
77 
78  if (!depthframe || !(_streamType == rs2_stream::RS2_STREAM_ANY || depthframe.get_profile().stream_type() == _streamType)) {
79  return;
80  }
81 
82  if (frames_map_get_and_set(depthframe.get_profile().stream_type(), depthframe.get_frame_number())) {
83  return;
84  }
85 
87  [this, &frame] {
88  rs2::depth_frame depthframe = frame.as<rs2::depth_frame>();
89 
90  std::stringstream filename;
92  << "_" << depthframe.get_profile().stream_name()
93  << "_" << std::setprecision(14) << std::fixed << depthframe.get_timestamp()
94  << ".bin";
95 
96  std::stringstream metadata_file;
97  metadata_file << _filePath
98  << "_" << depthframe.get_profile().stream_name()
99  << "_metadata_" << std::setprecision(14) << std::fixed << depthframe.get_timestamp()
100  << ".txt";
101 
102  std::string filenameS = filename.str();
103  std::string metadataS = metadata_file.str();
104 
106  [filenameS, metadataS, depthframe] {
107  std::ofstream fs(filenameS, std::ios::binary | std::ios::trunc);
108 
109  if (fs) {
110  uint8_t buffer[4];
111 
112  for (int y = 0; y < depthframe.get_height(); y++) {
113  for (int x = 0; x < depthframe.get_width(); x++) {
114  fs.write(
115  static_cast<const char *>(to_ieee754_32(depthframe.get_distance(x, y), buffer))
116  , sizeof buffer);
117  }
118  }
119 
120  fs.flush();
121  }
122 
123  metadata_to_txtfile(depthframe, metadataS);
124  });
125 
127  });
128  }
129  };
130 
131  }
132  }
133 }
134 
135 
136 #endif
rs2::tools::converter::converter_bin
Definition: converter-bin.hpp:18
uint8_t
unsigned char uint8_t
Definition: stdint.h:78
rs2::frame
Definition: rs_frame.hpp:345
rs2::depth_frame::get_distance
float get_distance(int x, int y) const
Definition: rs_frame.hpp:837
rs2::frame::get_frame_number
unsigned long long get_frame_number() const
Definition: rs_frame.hpp:523
string
GLsizei const GLchar *const * string
Definition: glad/glad/glad.h:2861
rs2::stream_profile::stream_name
std::string stream_name() const
Definition: rs_frame.hpp:113
rs2::stream_profile::stream_type
rs2_stream stream_type() const
Definition: rs_frame.hpp:39
rs2::tools::converter::converter_base::wait_sub_workers
void wait_sub_workers()
Definition: converter.cpp:47
rs2::tools::converter::converter_bin::converter_bin
converter_bin(const std::string &filePath, rs2_stream streamType=rs2_stream::RS2_STREAM_ANY)
Definition: converter-bin.hpp:63
rs2::tools::converter::converter_base::add_sub_worker
void add_sub_worker(const F &f)
Definition: converter.hpp:40
uint32_t
unsigned int uint32_t
Definition: stdint.h:80
rs2::frame::get_timestamp
double get_timestamp() const
Definition: rs_frame.hpp:476
buffer
GLenum GLfloat * buffer
Definition: glad/glad/glad.h:2066
f
GLdouble f
Definition: glad/glad/glad.h:1517
rs2
Definition: animated.h:9
rs2::tools::converter::converter_bin::to_ieee754_32
static void * to_ieee754_32(float f, uint8_t *buffer)
Definition: converter-bin.hpp:23
rs2::tools::converter::converter_base::frames_map_get_and_set
bool frames_map_get_and_set(rs2_stream streamType, frame_number_t frameNumber)
Definition: converter.cpp:31
rs2::video_frame::get_width
int get_width() const
Definition: rs_frame.hpp:661
rs2::tools::converter::converter_base
Definition: converter.hpp:25
RS2_STREAM_ANY
@ RS2_STREAM_ANY
Definition: rs_sensor.h:45
rs2::tools::converter::converter_bin::name
std::string name() const override
Definition: converter-bin.hpp:69
rs2::tools::converter::converter_bin::_streamType
rs2_stream _streamType
Definition: converter-bin.hpp:19
tools
Definition: lrs-device-controller.h:26
test-projection-from-recording.filename
filename
Definition: test-projection-from-recording.py:15
rs2::tools::converter::metadata_to_txtfile
void metadata_to_txtfile(const rs2::frame &frm, const std::string &filename)
Definition: converter.cpp:9
rs2::depth_frame
Definition: rs_frame.hpp:813
rs2::frame::as
T as() const
Definition: rs_frame.hpp:582
rs2::tools::converter::converter_base::start_worker
void start_worker(const F &f)
Definition: converter.hpp:35
rs2::frame::get_profile
stream_profile get_profile() const
Definition: rs_frame.hpp:559
rs2::tools::converter::converter_bin::convert
void convert(rs2::frame &frame) override
Definition: converter-bin.hpp:74
x
GLdouble x
Definition: glad/glad/glad.h:2279
test-ts-same-fps.fs
fs
Definition: test-ts-same-fps.py:57
rs2_stream
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:43
rs2::tools::converter::converter_bin::_filePath
std::string _filePath
Definition: converter-bin.hpp:20
rs2::configurations::ply::binary
@ binary
Definition: device-model.h:174
rs2::video_frame::get_height
int get_height() const
Definition: rs_frame.hpp:673
y
GLint y
Definition: glad/glad/glad.h:1397


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:01