libalvision  2.5.7.1
 All Classes Namespaces Files Functions Variables Friends 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 
15 #include <boost/shared_ptr.hpp>
16 
18 #include <qi/os.hpp>
19 
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 
88  ALImage deepCopy();
89 
106  ALValue toALValue();
107 
125  static ALImage* fromALValue(const ALValue& image);
126 
127  inline void setWidth( const int width ) { fWidth = width; }
128  inline void setHeight( const int height ) { fHeight = height; }
129  inline void setLeftAngle( const float leftAngle ) { fFOV.leftAngle = leftAngle; }
130  inline void setTopAngle( const float topAngle ) { fFOV.topAngle = topAngle; }
131  inline void setRightAngle( const float rightAngle ) { fFOV.rightAngle = rightAngle; }
132  inline void setBottomAngle( const float bottomAngle ) { fFOV.bottomAngle = bottomAngle; }
133  inline void setAngles( const float leftAngle, const float topAngle,
134  const float rightAngle, const float bottomAngle )
135  { fFOV.leftAngle = leftAngle; fFOV.topAngle = topAngle;
136  fFOV.rightAngle = rightAngle; fFOV.bottomAngle = bottomAngle; }
137 
141  bool setSize(int pResolution) { return setResolution(pResolution); }
142 
148  bool setResolution(int pResolution);
149 
155  bool setColorSpace(int pColorSpace);
156 
162  inline const unsigned char* getFrame() const
163  { std::cout << "getFrame() is deprecated. Please replace by getData()." << std::endl;
164  return rawData(); }
165 
171  inline const unsigned char* getData() const { return rawData(); }
172 
173  // for the camera
179  inline unsigned char* getFrame()
180  { std::cout << "getFrame() is deprecated. Please replace by getData()." << std::endl;
181  return rawData(); }
182 
188  inline unsigned char* getData() { return rawData(); }
189 
196  inline void setData(unsigned char* pData) { fData = externalData(pData); }
197 
203  inline void setTimeStamp(const qi::os::timeval pTimeStamp)
204  {
205  if( (pTimeStamp.tv_usec < 0) || (pTimeStamp.tv_sec < 0) )
206  {
207  fTimeStamp = -1;
208  return;
209  }
210  setTimeStamp(static_cast<int>(pTimeStamp.tv_sec), static_cast<int>(pTimeStamp.tv_usec));
211  }
212 
218  inline void setTimeStamp(long long pTimeStamp) { fTimeStamp = pTimeStamp; }
219 
226  inline void setTimeStamp(int pSeconds, int pMicroSeconds)
227  {
228  fTimeStamp = (long long)pSeconds*1000000LL + (long long)pMicroSeconds;
229  }
230 
231 
237  inline void setCameraId(char pCameraId) { fCameraId = pCameraId; }
238 
239 
244  inline unsigned int getSize() const { return fWidth*fHeight*fNbLayers; }
245 
246  /*
247  * Accessor
248  */
249  inline int getWidth( void ) const { return fWidth; }
250  inline int getHeight( void ) const { return fHeight; }
251  inline int getResolution( void ) const { return getResolutionFromSize(fWidth, fHeight); }
252  inline int getColorSpace( void ) const { return fColorSpace; }
253  inline int getNbLayers( void ) const { return fNbLayers; }
254  inline long long getTimeStamp( void ) const { return fTimeStamp; }
255  inline char getCameraId() const { return fCameraId; }
256  inline int getMaxResolution( void ) const { return fMaxResolution; }
257  inline int getNbOfLayersMax( void ) const { return fMaxNumberOfLayers; }
258  inline bool areDataExternal( void ) const { return !fData || getDeleter().external; }
259  int getAllocatedSize() const { return fAllocatedSize; }
260  inline float getLeftAngle( void ) const { return fFOV.leftAngle; }
261  inline float getTopAngle( void ) const { return fFOV.topAngle; }
262  inline float getRightAngle( void ) const { return fFOV.rightAngle; }
263  inline float getBottomAngle( void ) const { return fFOV.bottomAngle; }
264  inline void getAngles( float& leftAngle, float& topAngle, float& rightAngle, float& bottomAngle )
265  const { leftAngle = fFOV.leftAngle; topAngle = fFOV.topAngle;
266  rightAngle = fFOV.rightAngle; bottomAngle = fFOV.bottomAngle; }
267 
268  /*
269  * For debug purpose: print the object in an human format
270  */
271  std::string toString( void ) const;
272 
273  int getNumOfROIs() const { return (int)fROIs.size(); }
274 
275  const ROI* getROI(int index) const {
276  if (index < 0 || index >= getNumOfROIs())
277  return NULL;
278  return &(fROIs[index]);
279  }
280 
281  void addROI(const ROI& rect) {
282  fROIs.push_back(rect);
283  }
284 
285  void cleanROIs() {
286  fROIs.clear();
287  }
288 
289  void setEnableROIs(bool enable) {
290  fROIEnabled = enable;
291  }
292 
293  bool isROIEnabled() const {
294  return fROIEnabled;
295  }
296 
297 
298  int writeFile(const char* _fileNameAndPath);
299  int readFile(const char* _fileNameAndPath);
300  int savePPM(const char* _fileNameAndPath);
301 
302  bool computeYUV422imageFromBGR(int height, int width, char *data);
303  bool computeBGRimageFromYUV422(const unsigned char* _dest);
304  bool computeYYYUUUVVVimageFromYUV422(const unsigned char* _dest);
305  bool computeYYYYUUVVimageFromYUV422(const unsigned char* _dest);
306 private:
327  class deleter
328  {
329  public:
330  deleter(bool external) : external(external) {}
331  void operator()(unsigned char* p)
332  {
333  if (!external && p != NULL)
334  delete[] p;
335  }
336  bool external;
337  };
338  friend class deleter;
339 
340  bool reallocateDataSize(const int resolution, const int nbLayers);
341  inline unsigned char* rawData() const { assert( fData ); return fData.get(); }
349  inline ALImage::deleter& getDeleter() const
350  {
351  assert( fData );
352  assert( boost::get_deleter<ALImage::deleter>(fData) != NULL );
353  return *boost::get_deleter<ALImage::deleter>(fData);
354  }
355  static boost::shared_ptr<unsigned char[]> internalDataUninitialized(size_t size);
356  static boost::shared_ptr<unsigned char[]> internalDataZeroInitialized(size_t size);
357  static boost::shared_ptr<unsigned char[]> externalData(unsigned char[]);
358  static boost::shared_ptr<unsigned char[]> emptyData(bool external);
359 
360 
361  // .:: members ::
362 private:
364  int fWidth;
365 
367  int fHeight;
368 
370  int fNbLayers;
371 
373  int fColorSpace;
374 
376  long long fTimeStamp;
377 
379  boost::shared_ptr<unsigned char[]> fData;
380 
382  char fCameraId;
383 
385  int fAllocatedSize;
386 
388  int fMaxResolution;
389 
391  int fMaxNumberOfLayers;
392 
394  struct FOV {
395  float leftAngle;
396  float topAngle;
397  float rightAngle;
398  float bottomAngle;
399  };
400  struct FOV fFOV;
401 
402  std::vector<ROI> fROIs;
403 
404  bool fROIEnabled;
405 };
406 }
407 
408 // take a pixel in YUV and compute its RGB Value. RGB value are returned directly in params
409 void computeRgbFromYuv( unsigned char * pYR, unsigned char * pUG, unsigned char * pVB );
410 
411 
412 #endif // _LIBALVISION_ALVISION_ALIMAGE_H_
void computeRgbFromYuv(unsigned char *pYR, unsigned char *pUG, unsigned char *pVB)
void setData(unsigned char *pData)
set the image data pointer to point to the specified buffer.
Definition: alimage.h:196
bool computeYYYUUUVVVimageFromYUV422(const unsigned char *_dest)
bool setResolution(int pResolution)
set the Resolution of the image without changing the allocation size.
unsigned int getSize() const
Definition: alimage.h:244
float rightAngle
Definition: alimage.h:42
bool setColorSpace(int pColorSpace)
set the ColorSpace of the image without changing the allocation size.
void setTimeStamp(int pSeconds, int pMicroSeconds)
set the image timestamp
Definition: alimage.h:226
ROI(int left, int top, int width, int height)
ALImage deepCopy()
data-ownership copy creation
void setBottomAngle(const float bottomAngle)
Definition: alimage.h:132
int getResolutionFromSize(const int width, const int height)
Utility function that takes width and height as inputs and returns the corresponding resolution index...
float getRightAngle(void) const
Definition: alimage.h:262
int getMaxResolution(void) const
Definition: alimage.h:256
void setCameraId(char pCameraId)
set the ID of the camera that shot the picture
Definition: alimage.h:237
bool setSize(int pResolution)
set the Resolution of the image without changing the allocation size.
Definition: alimage.h:141
vision defines
bool computeYYYYUUVVimageFromYUV422(const unsigned char *_dest)
void setAngles(const float leftAngle, const float topAngle, const float rightAngle, const float bottomAngle)
Definition: alimage.h:133
void setTopAngle(const float topAngle)
Definition: alimage.h:130
float topAngle
Definition: alimage.h:41
void setTimeStamp(const qi::os::timeval pTimeStamp)
set the image timestamp.
Definition: alimage.h:203
const unsigned char * getData() const
return the reference to the image data.
Definition: alimage.h:171
int readFile(const char *_fileNameAndPath)
void setWidth(const int width)
Definition: alimage.h:127
int writeFile(const char *_fileNameAndPath)
void getAngles(float &leftAngle, float &topAngle, float &rightAngle, float &bottomAngle) const
Definition: alimage.h:264
int getNbOfLayersMax(void) const
Definition: alimage.h:257
const ROI * getROI(int index) const
Definition: alimage.h:275
float getLeftAngle(void) const
Definition: alimage.h:260
float getBottomAngle(void) const
Definition: alimage.h:263
ALValue toALValue()
return an ALValue containing image structure
void setHeight(const int height)
Definition: alimage.h:128
int savePPM(const char *_fileNameAndPath)
void setRightAngle(const float rightAngle)
Definition: alimage.h:131
void cleanROIs()
Definition: alimage.h:285
int getResolution(void) const
Definition: alimage.h:251
float bottomAngle
Definition: alimage.h:43
friend class deleter
Definition: alimage.h:338
int getWidth(void) const
Definition: alimage.h:249
bool computeYUV422imageFromBGR(int height, int width, char *data)
bool areDataExternal(void) const
Definition: alimage.h:258
int getNumOfROIs() const
Definition: alimage.h:273
int getHeight(void) const
Definition: alimage.h:250
std::string toString(void) const
void setTimeStamp(long long pTimeStamp)
set the image timestamp
Definition: alimage.h:218
int getColorSpace(void) const
Definition: alimage.h:252
bool isROIEnabled() const
Definition: alimage.h:293
ALImage(int pWidth, int pHeight, int pColorSpace, bool pDataAreExternal=false, float pLeftAngle=0.f, float pTopAngle=0.f, float pRightAngle=0.f, float pBottomAngle=0.f)
constructor
void setLeftAngle(const float leftAngle)
Definition: alimage.h:129
static ALImage * fromALValue(const ALValue &image)
Allocate an ALImage and return a pointer on it using an ALValue to fill it.
unsigned char * getData()
return the pointer to the image data.
Definition: alimage.h:188
float getTopAngle(void) const
Definition: alimage.h:261
const unsigned char * getFrame() const
return the reference to the image data.
Definition: alimage.h:162
int getAllocatedSize() const
Definition: alimage.h:259
bool computeBGRimageFromYUV422(const unsigned char *_dest)
float leftAngle
Definition: alimage.h:40
int getNbLayers(void) const
Definition: alimage.h:253
char getCameraId() const
Definition: alimage.h:255
long long getTimeStamp(void) const
Definition: alimage.h:254
unsigned char * getFrame()
return the pointer to the image data.
Definition: alimage.h:179
void setEnableROIs(bool enable)
Definition: alimage.h:289
void addROI(const ROI &rect)
Definition: alimage.h:281