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 {
20  std::string _filePath;
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  int 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:
63  converter_bin(const std::string& filePath, rs2_stream streamType = rs2_stream::RS2_STREAM_ANY)
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;
91  filename << _filePath
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
GLint y
std::string stream_name() const
Definition: rs_frame.hpp:113
void convert(rs2::frame &frame) override
Definition: animated.h:9
double get_timestamp() const
Definition: rs_frame.hpp:476
std::string name() const override
converter_bin(const std::string &filePath, rs2_stream streamType=rs2_stream::RS2_STREAM_ANY)
unsigned char uint8_t
Definition: stdint.h:78
GLenum GLfloat * buffer
float get_distance(int x, int y) const
Definition: rs_frame.hpp:837
bool frames_map_get_and_set(rs2_stream streamType, frame_number_t frameNumber)
Definition: converter.cpp:31
GLdouble x
unsigned int uint32_t
Definition: stdint.h:80
T as() const
Definition: rs_frame.hpp:582
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:42
int get_height() const
Definition: rs_frame.hpp:673
unsigned long long get_frame_number() const
Definition: rs_frame.hpp:523
void metadata_to_txtfile(const rs2::frame &frm, const std::string &filename)
Definition: converter.cpp:9
int get_width() const
Definition: rs_frame.hpp:661
static void * to_ieee754_32(float f, uint8_t *buffer)
GLdouble f
rs2_stream stream_type() const
Definition: rs_frame.hpp:39
stream_profile get_profile() const
Definition: rs_frame.hpp:559


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Thu Dec 22 2022 03:43:16