base
Frame.hpp
Go to the documentation of this file.
1 
5 #ifndef BASE_SAMPLES_FRAME_H__
6 #define BASE_SAMPLES_FRAME_H__
7 
8 #include <stdint.h>
9 #include <vector>
10 #include <sstream>
11 #include <stdexcept>
12 #include <base/Time.hpp>
13 
14 
15 namespace base { namespace samples { namespace frame {
17  {
18  std::string data_;
19  std::string name_;
20  void set(const std::string &name,const std::string &data);
21  };
22 
23  struct frame_size_t {
24  frame_size_t() : width(0), height(0) {}
25  frame_size_t(uint16_t w, uint16_t h) : width(w), height(h) {}
26 
27  bool operator==(const frame_size_t &other) const;
28 
29 
30  bool operator!=(const frame_size_t &other) const;
31 
32  uint16_t width;
33  uint16_t height;
34  };
35 
36  enum frame_mode_t {
39  MODE_RGB = 2,
40  MODE_UYVY = 3,
41  MODE_BGR = 4,
43  RAW_MODES = 128,
49  COMPRESSED_MODES = 256, //if an image is compressed it has no relationship
50  //between number of pixels and number of bytes
54  };
55 
60  };
61 
62  /* A single image frame */
63  struct Frame
64  {
65  public:
73  Frame();
74 
75  //@depth number of bits per pixel and channel
76  Frame(uint16_t width, uint16_t height, uint8_t depth=8, frame_mode_t mode=MODE_GRAYSCALE, uint8_t const val = 0,size_t sizeInBytes=0);
77 
78  //makes a copy of other
79  Frame(const Frame &other,bool bcopy = true);
80 
81  //copies all attributes which are independent from size and mode
82  //if an attribute already exists the old value is over written
83  void copyImageIndependantAttributes(const Frame &other);
84 
85  //makes a copy of other
86  void init(const Frame &other,bool bcopy = true);
87 
88  void init(uint16_t width, uint16_t height, uint8_t depth=8, frame_mode_t mode=MODE_GRAYSCALE, const uint8_t val = 0, size_t sizeInBytes=0);
89 
90  // if val is negative the image will not be initialized
91  void reset(int const val = 0);
92 
93  void swap(Frame &frame);
94 
95  bool isHDR()const;
96 
97  void setHDR(bool value);
98 
99  bool isCompressed()const;
100 
101  bool isGrayscale()const;
102 
103  bool isRGB()const;
104 
105  bool isBayer()const;
106 
107  void setStatus(const frame_status_t value);
108 
109  frame_status_t getStatus()const;
110 
111  uint32_t getChannelCount() const;
112 
113  static uint32_t getChannelCount(frame_mode_t mode);
114 
115  //qt ruby does not support enums as slot parameters
116  //therefore frame_mode_t is passed as string
117  static frame_mode_t toFrameMode(const std::string &str);
118 
119  frame_mode_t getFrameMode() const;
120 
126  uint32_t getPixelSize() const;
127 
134  uint32_t getRowSize() const;
135 
139  uint32_t getNumberOfBytes() const;
140 
145  uint32_t getPixelCount() const;
146 
147  uint32_t getDataDepth() const;
148 
149  void setDataDepth(uint32_t value);
150 
151  void setFrameMode(frame_mode_t mode);
152 
153  frame_size_t getSize() const;
154 
155  uint16_t getWidth() const;
156 
157  uint16_t getHeight() const;
158 
159  const std::vector<uint8_t> &getImage() const;
160 
161  void validateImageSize(size_t sizeToValidate) const;
162 
163  void setImage(const std::vector<uint8_t> &newImage);
164 
167  void setImage(const char *data, size_t newImageSize);
168 
169  void setImage(const uint8_t *data, size_t newImageSize);
170 
171  uint8_t *getImagePtr();
172 
173  const uint8_t *getImageConstPtr() const;
174 
175  uint8_t* getLastByte();
176 
177  const uint8_t* getLastConstByte()const;
178 
179  bool hasAttribute(const std::string &name)const;
180 
181  template<typename T>
182  inline T getAttribute(const std::string &name)const
183  {
184  static T default_value;
185  std::stringstream strstr;
186 
187  std::vector<frame_attrib_t>::const_iterator _iter = attributes.begin();
188  for (;_iter != attributes.end();_iter++)
189  {
190  if (_iter->name_ == name)
191  {
192  T data;
193  strstr << _iter->data_;
194  strstr >> data;
195  return data;
196  }
197  }
198  return default_value;
199  }
200 
201  bool deleteAttribute(const std::string &name);
202 
203  template<typename T>
204  inline void setAttribute(const std::string &name,const T &data)
205  {
206  //if attribute exists
207  std::stringstream strstr;
208  strstr << data;
209  std::vector<frame_attrib_t>::iterator _iter = attributes.begin();
210  for (;_iter != attributes.end();_iter++)
211  {
212  if (_iter->name_ == name)
213  {
214  _iter->set(name,strstr.str());
215  return;
216  }
217  }
218  //if attribute does not exist
219  attributes.push_back(frame_attrib_t());
220  attributes.back().set(name,strstr.str());
221  return ;
222  }
223 
224  template <typename Tp> Tp& at(unsigned int column,unsigned int row)
225  {
226  if(column >= size.width || row >= size.height )
227  throw std::runtime_error("out of index");
228  return *((Tp*)(getImagePtr()+row*getRowSize()+column*getPixelSize()));
229  }
230 
238 
240  std::vector<uint8_t> image;
242  std::vector<frame_attrib_t> attributes;
243 
246 
253  uint32_t data_depth;
260  uint32_t pixel_size;
261 
264  uint32_t row_size;
265 
269 
272  };
273 
274  struct FramePair
275  {
279  uint32_t id;
280  };
281 }}}
282 
283 #endif
bool operator!=(const Motion2D &lhs, const Motion2D &rhs)
Definition: Motion2D.cpp:10
Definition: Frame.hpp:39
uint16_t width
Definition: Frame.hpp:32
std::string name_
Definition: Frame.hpp:19
Definition: Frame.hpp:40
frame_size_t(uint16_t w, uint16_t h)
Definition: Frame.hpp:25
Tp & at(unsigned int column, unsigned int row)
Definition: Frame.hpp:224
frame_status_t
Definition: Frame.hpp:56
Definition: Frame.hpp:57
std::string data_
Definition: Frame.hpp:18
void setAttribute(const std::string &name, const T &data)
Definition: Frame.hpp:204
Definition: Frame.hpp:43
std::vector< frame_attrib_t > attributes
Definition: Frame.hpp:242
Definition: Frame.hpp:44
uint32_t data_depth
Definition: Frame.hpp:253
T getAttribute(const std::string &name) const
Definition: Frame.hpp:182
uint32_t pixel_size
Definition: Frame.hpp:260
Definition: Frame.hpp:41
frame_status_t frame_status
Definition: Frame.hpp:271
uint32_t row_size
Definition: Frame.hpp:264
Definition: Time.hpp:11
Definition: Frame.hpp:53
Definition: Frame.hpp:52
Definition: Frame.hpp:42
frame_mode_t frame_mode
Definition: Frame.hpp:268
frame_size_t size
Definition: Frame.hpp:245
base::Time time
Definition: Frame.hpp:235
Definition: Frame.hpp:51
Definition: Frame.hpp:63
base::Time time
Definition: Frame.hpp:276
Definition: Frame.hpp:59
std::vector< uint8_t > image
Definition: Frame.hpp:240
Definition: Frame.hpp:23
Definition: Frame.hpp:38
bool operator==(const Motion2D &lhs, const Motion2D &rhs)
Definition: Motion2D.cpp:5
frame_mode_t
Definition: Frame.hpp:36
uint16_t height
Definition: Frame.hpp:33
Definition: LinearAngular6DCommand.hpp:8
Definition: Frame.hpp:58
frame_size_t()
Definition: Frame.hpp:24
Definition: Frame.hpp:274
Definition: Frame.hpp:16
Frame second
Definition: Frame.hpp:278
uint32_t id
Definition: Frame.hpp:279
Frame first
Definition: Frame.hpp:277
Definition: Frame.hpp:37
base::Time received_time
Definition: Frame.hpp:237