refinery
RAW file processor

include/refinery/image_tile.h

00001 #ifndef _REFINERY_IMAGE_TILE_H
00002 #define _REFINERY_IMAGE_TILE_H
00003 
00004 namespace refinery {
00005 
00011 template<typename T>
00012 class ImageTile {
00013 public:
00014   typedef T ImageType; 
00015   typedef typename T::PixelType PixelType; 
00016   typedef typename T::ValueType ValueType; 
00017   typedef typename T::ColorType ColorType; 
00019 private:
00020   typedef std::vector<PixelType> PixelsType;
00021   Point mImageSize;
00022   Point mTopLeft;
00023   Point mSize;
00024   unsigned int mEdgeSize;
00025   PixelsType mPixels;
00026 
00027   void allocate()
00028   {
00029     mPixels.assign(mSize.row * mSize.col, PixelType());
00030   }
00031 
00032   ptrdiff_t offsetForImagePoint(const Point& imagePoint) const
00033   {
00034     Point tilePoint(imagePoint - mTopLeft);
00035     return tilePoint.row * mSize.col + tilePoint.col;
00036   }
00037 
00038 public:
00050   ImageTile(
00051       const Point& imageSize, const Point& topLeft, const Point& size,
00052       unsigned int border, unsigned int margin)
00053     : mImageSize(imageSize), mTopLeft(topLeft), mSize(size),
00054       mEdgeSize(static_cast<unsigned int>(border - margin))
00055   {
00056     this->allocate();
00057   }
00058 
00062   unsigned int top() const {
00063     return std::max<unsigned int>(mTopLeft.row, mEdgeSize);
00064   }
00068   unsigned int left() const {
00069     return std::max<unsigned int>(mTopLeft.col, mEdgeSize);
00070   }
00074   unsigned int height() const {
00075     return mSize.row;
00076   }
00080   unsigned int width() const {
00081     return mSize.col;
00082   }
00086   unsigned int bottom() const {
00087     return std::min<unsigned int>(
00088         mImageSize.row - mEdgeSize,
00089         mTopLeft.row + mSize.row);
00090   }
00094   unsigned int right() const {
00095     return std::min<unsigned int>(
00096         mImageSize.col - mEdgeSize,
00097         mTopLeft.col + mSize.col);
00098   }
00099 
00103   void setTopLeft(const Point& topLeft) { mTopLeft = topLeft; }
00104 
00108   PixelType* pixelsAtImageCoords(const Point& point) {
00109     const ptrdiff_t offset(offsetForImagePoint(point));
00110     return &mPixels[offset];
00111   }
00115   PixelType* pixelsAtImageCoords(int row, int col) {
00116     return pixelsAtImageCoords(Point(row, col));
00117   }
00121   const PixelType* constPixelsAtImageCoords(const Point& point) const {
00122     const ptrdiff_t offset(offsetForImagePoint(point));
00123     return &mPixels[offset];
00124   }
00128   const PixelType* constPixelsAtImageCoords(int row, int col) const {
00129     return constPixelsAtImageCoords(Point(row, col));
00130   }
00131 };
00132 
00133 }; /* namespace refinery */
00134 
00135 #endif /* _REFINERY_IMAGE_TILE_H */
 All Classes Functions Variables Typedefs Enumerations Enumerator