libalvision  2.1.0.18
/home/opennao/work/master/sdk/libnaoqi/libalvision/alvision/alimage.h
Go to the documentation of this file.
00001 
00006 #pragma once
00007 #ifndef _LIBALVISION_ALVISION_ALIMAGE_H_
00008 #define _LIBALVISION_ALVISION_ALIMAGE_H_
00009 
00010 #include <string>
00011 #include <iostream>
00012 #include <vector>
00013 #include <cassert>
00014 
00015 #include <alvision/alvisiondefinitions.h>
00016 #include <qi/os.hpp>
00017 
00022 namespace AL
00023 {
00024 class ALValue;
00025 
00026 class ALImage
00027 {
00028 public:
00029   struct ROI {
00030     ROI(int left, int top, int width, int height);
00031     ROI(int left, int top, int width, int height,
00032         float leftAngle,  float topAngle,
00033         float rightAngle, float bottomAngle);
00034     int x;
00035     int y;
00036     int w;
00037     int h;
00038     float leftAngle;
00039     float topAngle;
00040     float rightAngle;
00041     float bottomAngle;
00042   };
00043 
00044   // .:: methods ::
00045 
00046 public:
00059   ALImage(int pWidth, int pHeight, int pColorSpace, bool pDataAreExternal = false,
00060           float pLeftAngle = 0.f, float pTopAngle = 0.f,
00061           float pRightAngle = 0.f, float pBottomAngle = 0.f);
00062 
00074   ALImage(int pResolution, int pColorSpace, bool pDataAreExternal = false,
00075           float pLeftAngle = 0.f, float pTopAngle = 0.f,
00076           float pRightAngle = 0.f, float pBottomAngle = 0.f);
00077 
00078   ~ALImage();
00079 
00096   ALValue toALValue();
00097 
00115   static ALImage* fromALValue(const ALValue& image);
00116 
00117   inline void setWidth( const int width )   { fWidth = width; }
00118   inline void setHeight( const int height ) { fHeight = height; }
00119   inline void setLeftAngle( const float leftAngle )     { fFOV.leftAngle = leftAngle; }
00120   inline void setTopAngle( const float topAngle )       { fFOV.topAngle = topAngle; }
00121   inline void setRightAngle( const float rightAngle )   { fFOV.rightAngle = rightAngle; }
00122   inline void setBottomAngle( const float bottomAngle ) { fFOV.bottomAngle = bottomAngle; }
00123   inline void setAngles( const float leftAngle,  const float topAngle,
00124                          const float rightAngle, const float bottomAngle )
00125   { fFOV.leftAngle = leftAngle;   fFOV.topAngle = topAngle;
00126     fFOV.rightAngle = rightAngle; fFOV.bottomAngle = bottomAngle; }
00127 
00131   bool setSize(int pResolution) { return setResolution(pResolution); }
00132 
00138   bool setResolution(int pResolution);
00139 
00145   bool setColorSpace(int pColorSpace);
00146 
00152   inline const unsigned char* getFrame() const
00153   { assert( fData != 0);
00154     std::cout << "getFrame() is deprecated. Please replace by getData()." << std::endl;
00155     return fData; }
00156 
00162   inline const unsigned char* getData() const { assert( fData != 0); return fData; }
00163 
00164   // for the camera
00170   inline unsigned char* getFrame()
00171   { assert( fData != 0);
00172     std::cout << "getFrame() is deprecated. Please replace by getData()." << std::endl;
00173     return fData; }
00174 
00180   inline unsigned char* getData() { assert( fData != 0); return fData; }
00181 
00187   inline void setData(unsigned char* pData) { fData = pData; }
00188 
00194   inline void setTimeStamp(const qi::os::timeval pTimeStamp)
00195   {
00196     if( (pTimeStamp.tv_usec < 0) || (pTimeStamp.tv_sec < 0) )
00197     {
00198       fTimeStamp = -1;
00199       return;
00200     }
00201     setTimeStamp(static_cast<int>(pTimeStamp.tv_sec), static_cast<int>(pTimeStamp.tv_usec));
00202   }
00203 
00209   inline void setTimeStamp(long long pTimeStamp) { fTimeStamp = pTimeStamp; }
00210 
00217   inline void setTimeStamp(int pSeconds, int pMicroSeconds)
00218   {
00219     fTimeStamp = (long long)pSeconds*1000000LL + (long long)pMicroSeconds;
00220   }
00221 
00222 
00228   inline void setCameraId(char pCameraId) { fCameraId = pCameraId; }
00229 
00230 
00235   inline unsigned int getSize() const { return fWidth*fHeight*fNbLayers; }
00236 
00237   /*
00238      * Accessor
00239      */
00240   inline int getWidth( void )  const { return fWidth; }
00241   inline int getHeight( void ) const { return fHeight; }
00242   inline int getResolution( void ) const { return getResolutionFromSize(fWidth, fHeight); }
00243   inline int getColorSpace( void ) const { return fColorSpace; }
00244   inline int getNbLayers( void ) const { return fNbLayers; }
00245   inline long long getTimeStamp( void ) const { return fTimeStamp; }
00246   inline char getCameraId() const { return fCameraId; }
00247   inline int getMaxResolution( void ) const { return fMaxResolution; }
00248   inline int getNbOfLayersMax( void ) const { return fMaxNumberOfLayers; }
00249   inline bool areDataExternal( void ) const { return fDataAreExternal; }
00250   int getAllocatedSize() const { return fAllocatedSize; }
00251   inline float getLeftAngle( void )   const { return fFOV.leftAngle; }
00252   inline float getTopAngle( void )    const { return fFOV.topAngle; }
00253   inline float getRightAngle( void )  const { return fFOV.rightAngle; }
00254   inline float getBottomAngle( void ) const { return fFOV.bottomAngle; }
00255   inline void getAngles( float& leftAngle, float& topAngle, float& rightAngle, float& bottomAngle )
00256   const { leftAngle = fFOV.leftAngle; topAngle = fFOV.topAngle;
00257     rightAngle = fFOV.rightAngle; bottomAngle = fFOV.bottomAngle; }
00258 
00259   /*
00260      * For debug purpose: print the object in an human format
00261      */
00262   std::string toString( void ) const;
00263 
00264   int getNumOfROIs() const { return (int)fROIs.size(); }
00265 
00266   const ROI* getROI(int index) const {
00267     if (index < 0 || index >= getNumOfROIs())
00268       return NULL;
00269     return &(fROIs[index]);
00270   }
00271 
00272   void addROI(const ROI& rect) {
00273     fROIs.push_back(rect);
00274   }
00275 
00276   void cleanROIs() {
00277     fROIs.clear();
00278   }
00279 
00280   void setEnableROIs(bool enable) {
00281     fROIEnabled = enable;
00282   }
00283 
00284   bool isROIEnabled() const {
00285     return fROIEnabled;
00286   }
00287 
00288 
00289   int writeFile(const char* _fileNameAndPath);
00290   int readFile(const char* _fileNameAndPath);
00291   int savePPM(const char* _fileNameAndPath);
00292 
00293   bool computeYUV422imageFromBGR(int height, int width, char *data);
00294   bool computeBGRimageFromYUV422(const unsigned char* _dest);
00295   bool computeYYYUUUVVVimageFromYUV422(const unsigned char* _dest);
00296   bool computeYYYYUUVVimageFromYUV422(const unsigned char* _dest);
00297 private:
00298   bool reallocateDataSize(const int resolution, const int nbLayers);
00299 
00300 
00301   // .:: members ::
00302 private:
00304   int fWidth;
00305 
00307   int fHeight;
00308 
00310   int fNbLayers;
00311 
00313   int fColorSpace;
00314 
00316   long long fTimeStamp;
00317 
00319   unsigned char* fData;
00320 
00322   char fCameraId;
00323 
00325   int fAllocatedSize;
00326 
00328   int fMaxResolution;
00329 
00331   int fMaxNumberOfLayers;
00332 
00335   bool fDataAreExternal;
00336 
00338   struct FOV {
00339     float leftAngle;
00340     float topAngle;
00341     float rightAngle;
00342     float bottomAngle;
00343   };
00344   struct FOV fFOV;
00345 
00346   std::vector<ROI> fROIs;
00347 
00348   bool fROIEnabled;
00349 };
00350 }
00351 
00352 // take a pixel in YUV and compute its RGB Value. RGB value are returned directly in params
00353 void computeRgbFromYuv( unsigned char * pYR, unsigned char * pUG, unsigned char * pVB );
00354 
00355 
00356 #endif  // _LIBALVISION_ALVISION_ALIMAGE_H_
 All Classes Namespaces Files Functions Variables Defines