TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/host/attribute/Image.hpp
Go to the documentation of this file.
00001 #ifndef TUTTLE_HOST_CORE_IMAGE_HPP
00002 #define TUTTLE_HOST_CORE_IMAGE_HPP
00003 
00004 #include <tuttle/host/ofx/OfxhImage.hpp>
00005 #include <tuttle/common/ofx/imageEffect.hpp>
00006 
00007 #include <tuttle/host/memory/IMemoryPool.hpp>
00008 
00009 /// @tuttle: remove include dependencies to gil
00010 #include <boost/gil/channel_algorithm.hpp> // force include boostHack first
00011 #include <boost/gil/image_view.hpp>
00012 #include <boost/gil/image_view_factory.hpp>
00013 
00014 #include <boost/cstdint.hpp>
00015 
00016 namespace tuttle {
00017 namespace host {
00018 namespace attribute {
00019 
00020 class ClipImage;
00021 
00022 /**
00023  * make an image up
00024  */
00025 class Image : public tuttle::host::ofx::imageEffect::OfxhImage
00026 {
00027 public:
00028         enum EImageOrientation
00029         {
00030                 eImageOrientationFromTopToBottom, //< Use image orientation from top to bottom
00031                 eImageOrientationFromBottomToTop //< Use image orientation from bottom to top
00032         };
00033         
00034 protected:
00035         std::size_t _memorySize; ///< memory size
00036         std::size_t _pixelBytes; ///< pixel memory size
00037         int _rowAbsDistanceBytes; ///< positive memory size for the distance between rows
00038         OfxRectI _bounds;
00039         EImageOrientation _orientation;
00040         std::string _fullname;
00041         memory::IPoolDataPtr _data; ///< where we are keeping our image data
00042 
00043 public:
00044         Image( ClipImage& clip, const OfxTime time, const OfxRectD& bounds, const EImageOrientation orientation, const int rowDistanceBytes );
00045         virtual ~Image();
00046 
00047 #ifndef SWIG
00048         memory::IPoolDataPtr& getPoolData() { return _data; }
00049         const memory::IPoolDataPtr& getPoolData() const { return _data; }
00050 
00051         void setPoolData( const memory::IPoolDataPtr& pData )
00052         {
00053                 _data = pData;
00054                 setPointerProperty( kOfxImagePropData, getOrientedPixelData( eImageOrientationFromBottomToTop ) ); // OpenFX standard use BottomToTop
00055         }
00056 #endif
00057         
00058         std::string getFullName() const { return _fullname; }
00059 
00060         std::size_t getMemorySize() const { return _memorySize; }
00061         /**
00062          * @brief Positive/Absolute distance rows.
00063          */
00064         int getRowAbsDistanceBytes() const { return _rowAbsDistanceBytes; }
00065         
00066         EImageOrientation getOrientation() const { return _orientation; }
00067         
00068         std::size_t getNbComponents() const { return ofx::imageEffect::numberOfComponents( getComponentsType() ); }
00069         
00070         /**
00071          * @brief Get distance between rows depending on the requested orientation.
00072          */
00073         int getOrientedRowDistanceBytes( const EImageOrientation orientation ) { return _rowAbsDistanceBytes * ( _orientation != orientation ? -1 : 1); }
00074         
00075         boost::uint8_t* getPixelData();
00076         void* getVoidPixelData();
00077         char* getCharPixelData();
00078         boost::uint8_t* getOrientedPixelData( const EImageOrientation orientation );
00079 
00080         boost::uint8_t* pixel( const int x, const int y );
00081         
00082         /**
00083          * @todo clean this!
00084          * move outside from class or use copyFrom (don't specify dst)
00085          * use ref, change order, etc.
00086          */
00087         static void     copy( Image* dst, Image* src, const OfxPointI& dstCorner,
00088                               const OfxPointI& srcCorner, const OfxPointI& count );
00089 
00090         void getImage( boost::uint8_t*& outData, int& outWidth, int& outHeight, int& outRowSizeBytes, ofx::imageEffect::EBitDepth& outBitDepth, ofx::imageEffect::EPixelComponent& outComponents )
00091         {
00092                 outData = getPixelData();
00093                 const OfxRectI bounds = getBounds();
00094                 outWidth = bounds.x2 - bounds.x1;
00095                 outHeight = bounds.y2 - bounds.y1;
00096                 outRowSizeBytes = getRowBytes();
00097                 outBitDepth = getBitDepth();
00098                 outComponents = getComponentsType();
00099         }
00100 
00101         
00102         template < class VIEW_T >
00103         VIEW_T getGilView( const EImageOrientation orientation );
00104         
00105         template < class VIEW_T >
00106         VIEW_T getGilView();
00107 
00108 public:
00109         #if(TUTTLE_PNG_EXPORT_BETWEEN_NODES)
00110         void debugSaveAsPng( const std::string& filename );
00111         #endif
00112 
00113 private:
00114         template < class S_VIEW >
00115         static void copy( Image* dst, S_VIEW& src, const OfxPointI& dstCorner,
00116                           const OfxPointI& srcCorner, const OfxPointI& count );
00117         template < class D_VIEW, class S_VIEW >
00118         static void copy( D_VIEW& dst, S_VIEW& src, const OfxPointI& dstCorner,
00119                           const OfxPointI& srcCorner, const OfxPointI& count );
00120 };
00121 
00122 
00123 template < class VIEW_T >
00124 VIEW_T Image::getGilView( const EImageOrientation orientation )
00125 {
00126         //const OfxRectI rod = this->getROD();
00127         const OfxRectI bounds = this->getBounds();
00128 
00129         TUTTLE_TLOG_VAR( TUTTLE_TRACE, bounds );
00130         TUTTLE_TLOG_VAR( TUTTLE_TRACE, std::abs( bounds.x2 - bounds.x1 ) );
00131         TUTTLE_TLOG_VAR( TUTTLE_TRACE, std::abs( bounds.y2 - bounds.y1 ) );
00132         TUTTLE_TLOG_VAR( TUTTLE_TRACE, this->getRowBytes() );
00133         
00134         typedef typename VIEW_T::value_type Pixel;
00135         return boost::gil::interleaved_view( std::abs( bounds.x2 - bounds.x1 ),
00136                                  std::abs( bounds.y2 - bounds.y1 ),
00137                                  ( Pixel* )( this->getOrientedPixelData( orientation ) ),
00138                                  this->getOrientedRowDistanceBytes( orientation ) );
00139 }
00140 
00141 template < class VIEW_T >
00142 VIEW_T Image::getGilView()
00143 {
00144         //const OfxRectI rod = this->getROD();
00145         const OfxRectI bounds = this->getBounds();
00146 
00147         typedef typename VIEW_T::value_type Pixel;
00148         return boost::gil::interleaved_view( std::abs( bounds.x2 - bounds.x1 ),
00149                                  std::abs( bounds.y2 - bounds.y1 ),
00150                                  ( Pixel* )( this->getPixelData() ),
00151                                  this->getRowAbsDistanceBytes() );
00152 }
00153 
00154 }
00155 }
00156 }
00157 
00158 #endif