libalvision  1.14
 All Classes Namespaces Files Functions Variables Macros Pages
alimage.h
Go to the documentation of this file.
1 
6 #pragma once
7 #ifndef _LIBALVISION_ALVISION_ALIMAGE_H_
8 #define _LIBALVISION_ALVISION_ALIMAGE_H_
9 
10 #include <string>
11 #include <iostream>
12 #include <vector>
13 #include <cassert>
14 
16 #include <qi/os.hpp>
17 
21 struct _IplImage;
22 typedef struct _IplImage IplImage;
23 
24 namespace AL
25 {
26 class ALValue;
27 
28 class ALImage
29 {
30 public:
31  struct ROI {
32  ROI(int left, int top, int width, int height);
33  ROI(int left, int top, int width, int height,
34  float leftAngle, float topAngle,
35  float rightAngle, float bottomAngle);
36  int x;
37  int y;
38  int w;
39  int h;
40  float leftAngle;
41  float topAngle;
42  float rightAngle;
43  float bottomAngle;
44  };
45 
46  // .:: methods ::
47 
48 public:
61  ALImage(int pWidth, int pHeight, int pColorSpace, bool pDataAreExternal = false,
62  float pLeftAngle = 0.f, float pTopAngle = 0.f,
63  float pRightAngle = 0.f, float pBottomAngle = 0.f);
64 
76  ALImage(int pResolution, int pColorSpace, bool pDataAreExternal = false,
77  float pLeftAngle = 0.f, float pTopAngle = 0.f,
78  float pRightAngle = 0.f, float pBottomAngle = 0.f);
79 
80  ~ALImage();
81 
98  ALValue toALValue();
99 
100  static ALImage* fromALValue(const ALValue& image);
101 
102  inline void setWidth( const int width ) { fWidth = width; }
103  inline void setHeight( const int height ) { fHeight = height; }
104  inline void setLeftAngle( const float leftAngle ) { fFOV.leftAngle = leftAngle; }
105  inline void setTopAngle( const float topAngle ) { fFOV.topAngle = topAngle; }
106  inline void setRightAngle( const float rightAngle ) { fFOV.rightAngle = rightAngle; }
107  inline void setBottomAngle( const float bottomAngle ) { fFOV.bottomAngle = bottomAngle; }
108  inline void setAngles( const float leftAngle, const float topAngle,
109  const float rightAngle, const float bottomAngle )
110  { fFOV.leftAngle = leftAngle; fFOV.topAngle = topAngle;
111  fFOV.rightAngle = rightAngle; fFOV.bottomAngle = bottomAngle; }
112 
118  bool setSize(int pResolution) { return setResolution(pResolution); }
119 
125  bool setResolution(int pResolution);
126 
132  bool setColorSpace(int pColorSpace);
133 
139  inline const unsigned char* getFrame() const
140  { assert( fData != 0);
141  std::cout << "getFrame() is deprecated. Please replace by getData()." << std::endl;
142  return fData; }
143 
149  inline const unsigned char* getData() const { assert( fData != 0); return fData; }
150 
151  // for the camera
157  inline unsigned char* getFrame()
158  { assert( fData != 0);
159  std::cout << "getFrame() is deprecated. Please replace by getData()." << std::endl;
160  return fData; }
161 
167  inline unsigned char* getData() { assert( fData != 0); return fData; }
168 
174  inline void setData(unsigned char* pData) { fData = pData; }
175 
181  inline void setTimeStamp(const qi::os::timeval pTimeStamp)
182  {
183  if( (pTimeStamp.tv_usec < 0) || (pTimeStamp.tv_sec < 0) )
184  {
185  fTimeStamp = -1;
186  }
187  setTimeStamp(pTimeStamp.tv_sec, pTimeStamp.tv_usec);
188  }
189 
195  inline void setTimeStamp(long long pTimeStamp) { fTimeStamp = pTimeStamp; }
196 
203  inline void setTimeStamp(int pSeconds, int pMicroSeconds)
204  {
205  fTimeStamp = (long long)pSeconds*1000000LL + (long long)pMicroSeconds;
206  }
207 
208 
214  inline void setCameraId(char pCameraId) { fCameraId = pCameraId; }
215 
216 
221  inline unsigned int getSize() const { return fWidth*fHeight*fNbLayers; }
222 
223  /*
224  * Accessor
225  */
226  inline int getWidth( void ) const { return fWidth; }
227  inline int getHeight( void ) const { return fHeight; }
228  inline int getResolution( void ) const { return (fWidth==640) ? kVGA : (fWidth==320)?kQVGA:kQQVGA; }
229  inline int getColorSpace( void ) const { return fColorSpace; }
230  inline int getNbLayers( void ) const { return fNbLayers; }
231  inline long long getTimeStamp( void ) const { return fTimeStamp; }
232  inline char getCameraId() const { return fCameraId; }
233  inline int getMaxResolution( void ) const { return fMaxResolution; }
234  inline int getNbOfLayersMax( void ) const { return fMaxNumberOfLayers; }
235  inline bool areDataExternal( void ) const { return fDataAreExternal; }
236  int getAllocatedSize() const { return fAllocatedSize; }
237  inline float getLeftAngle( void ) const { return fFOV.leftAngle; }
238  inline float getTopAngle( void ) const { return fFOV.topAngle; }
239  inline float getRightAngle( void ) const { return fFOV.rightAngle; }
240  inline float getBottomAngle( void ) const { return fFOV.bottomAngle; }
241  inline void getAngles( float& leftAngle, float& topAngle, float& rightAngle, float& bottomAngle )
242  { leftAngle = fFOV.leftAngle; topAngle = fFOV.topAngle;
243  rightAngle = fFOV.rightAngle; bottomAngle = fFOV.bottomAngle; }
244 
245  /*
246  * For debug purpose: print the object in an human format
247  */
248  std::string toString( void ) const;
249 
250  int getNumOfROIs() const { return (int)fROIs.size(); }
251 
252  const ROI* getROI(int index) const {
253  if (index < 0 || index >= getNumOfROIs())
254  return NULL;
255  return &(fROIs[index]);
256  }
257 
258  void addROI(const ROI& rect) {
259  fROIs.push_back(rect);
260  }
261 
262  void cleanROIs() {
263  fROIs.clear();
264  }
265 
266  void setEnableROIs(bool enable) {
267  fROIEnabled = enable;
268  }
269 
270  bool isROIEnabled() const {
271  return fROIEnabled;
272  }
273 
274 
275  int writeFile(const char* _fileNameAndPath);
276  int readFile(const char* _fileNameAndPath);
277  int savePPM(const char* _fileNameAndPath);
278 
279  //deprecated
280  bool computeYUV422imageFromBGR(IplImage* _src); //TODO(pev): replace argument for compatibility with ALImage by data pointer and sizes
281  bool computeYUV422imageFromBGR(int height, int width, char *data);
282  bool computeBGRimageFromYUV422(const unsigned char* _dest);
283  bool computeYYYUUUVVVimageFromYUV422(const unsigned char* _dest);
284  bool computeYYYYUUVVimageFromYUV422(const unsigned char* _dest);
285 private:
286  bool reallocateDataSize(const int resolution, const int nbLayers);
287 
288 
289  // .:: members ::
290 private:
292  int fWidth;
293 
295  int fHeight;
296 
298  int fNbLayers;
299 
301  int fColorSpace;
302 
304  long long fTimeStamp;
305 
307  unsigned char* fData;
308 
310  char fCameraId;
311 
313  int fAllocatedSize;
314 
316  int fMaxResolution;
317 
319  int fMaxNumberOfLayers;
320 
323  bool fDataAreExternal;
324 
326  struct FOV {
327  float leftAngle;
328  float topAngle;
329  float rightAngle;
330  float bottomAngle;
331  };
332  struct FOV fFOV;
333 
334  std::vector<ROI> fROIs;
335 
336  bool fROIEnabled;
337 };
338 }
339 
340 // take a pixel in YUV and compute its RGB Value. RGB value are returned directly in params
341 void computeRgbFromYuv( unsigned char * pYR, unsigned char * pUG, unsigned char * pVB );
342 
343 
344 #endif // _LIBALVISION_ALVISION_ALIMAGE_H_