refinery
RAW file processor
|
00001 #ifndef _REFINERY_IMAGE_H 00002 #define _REFINERY_IMAGE_H 00003 00004 #include <refinery/camera.h> 00005 00006 #include <vector> 00007 00008 namespace refinery { 00009 00010 class CameraData; 00011 00018 struct Point { 00019 int row; 00020 int col; 00025 Point() : row(0), col(0) {} 00032 Point(int row, int col) : row(row), col(col) {} 00038 Point(const Point& rhs) : row(rhs.row), col(rhs.col) {} 00039 00046 Point operator+(const Point& point) const { 00047 return Point(row + point.row, col + point.col); 00048 } 00049 00056 Point operator-(const Point& point) const { 00057 return Point(row - point.row, col - point.col); 00058 } 00059 00066 bool operator==(const Point& rhs) const { 00067 return this->row == rhs.row && this->col == rhs.col; 00068 } 00069 00076 bool operator!=(const Point& rhs) const { 00077 return !(this->operator==(rhs)); 00078 } 00079 }; 00080 00087 template<typename T, unsigned int N> 00088 struct Pixel { 00089 typedef unsigned int ColorType; 00090 static const ColorType NColors = N; 00091 typedef T ValueType; 00092 typedef T ArrayType[NColors]; 00093 ArrayType colors; 00098 Pixel() { 00099 for (ColorType c = 0; c < N; c++) { 00100 colors[c] = 0; 00101 } 00102 } 00109 template<typename U> Pixel(const U (&rhs)[N]) { 00110 for (ColorType c = 0; c < N; c++) { 00111 colors[c] = rhs[c]; 00112 } 00113 } 00120 template<typename U> Pixel(const Pixel<U, N>& rhs) { 00121 for (ColorType c = 0; c < N; c++) { 00122 colors[c] = rhs.colors[c]; 00123 } 00124 } 00134 inline ArrayType& array() { return colors; } 00142 inline const ArrayType& constArray() const { return colors; } 00149 inline T& operator[](const ColorType& index) { return colors[index]; } 00158 inline const T& at(const ColorType& index) const { return colors[index]; } 00159 }; 00160 00168 template<typename T> 00169 struct RGBPixel : public Pixel<T, 3> 00170 { 00174 RGBPixel() : Pixel<T, 3>() {} 00181 template<typename U> RGBPixel(const U (&rhs)[3]) : Pixel<T, 3>(rhs) {} 00188 template<typename U> RGBPixel(const RGBPixel<U>& rhs) : Pixel<T, 3>(rhs) {} 00192 inline const T& r() const { return this->at(0); } 00196 inline const T& g() const { return this->at(1); } 00200 inline const T& b() const { return this->at(2); } 00204 inline T& r() { return this->operator[](0); } 00208 inline T& g() { return this->operator[](1); } 00212 inline T& b() { return this->operator[](2); } 00213 }; 00214 00222 template<typename T> 00223 struct LABPixel : public Pixel<T, 3> 00224 { 00228 LABPixel() : Pixel<T, 3>() {} 00235 template<typename U> LABPixel(const U (&rhs)[3]) : Pixel<T, 3>(rhs) {} 00242 template<typename U> LABPixel(const LABPixel<U>& rhs) : Pixel<T, 3>(rhs) {} 00246 inline const T& l() const { return this->at(0); } 00250 inline const T& a() const { return this->at(1); } 00254 inline const T& b() const { return this->at(2); } 00258 inline T& l() { return this->operator[](0); } 00262 inline T& a() { return this->operator[](1); } 00266 inline T& b() { return this->operator[](2); } 00267 }; 00268 00274 template<typename T> 00275 struct GrayPixel : public Pixel<T, 1> { 00279 GrayPixel() : Pixel<T, 1>() {} 00286 template<typename U> GrayPixel(const U (&rhs)[1]) : Pixel<T, 1>(rhs) {} 00292 GrayPixel(const T& value) : Pixel<T, 1>() { this[0] = value; } 00299 template<typename U> GrayPixel(const GrayPixel<U>& rhs) : Pixel<T, 3>(rhs) {} 00300 00304 T& value() { return this->operator[](0); } 00308 const T& value() const { return this->at(0); } 00309 00317 template<typename U> GrayPixel& operator*(const U& rhs) { 00318 return GrayPixel(value() * rhs); 00319 } 00320 }; 00321 00330 template<typename T> 00331 class Image { 00332 public: 00333 typedef T PixelType; 00334 typedef typename T::ValueType ValueType; 00335 typedef typename T::ColorType ColorType; 00337 private: 00338 typedef std::vector<PixelType> PixelsType; 00339 CameraData mCameraData; 00340 int mWidth; 00341 int mHeight; 00342 int mFilters; 00343 PixelsType mPixels; 00344 00345 void allocate() 00346 { 00347 mPixels.assign(mWidth * mHeight, PixelType()); 00348 } 00349 00350 public: 00361 Image(const CameraData& cameraData, int width, int height) 00362 : mCameraData(cameraData), mWidth(width), mHeight(height), 00363 mFilters(mCameraData.filters()) 00364 { 00365 this->allocate(); 00366 } 00367 00371 const CameraData& cameraData() const { return mCameraData; } 00375 unsigned int width() const { return mWidth; } 00379 unsigned int height() const { return mHeight; } 00383 unsigned int nPixels() const { return mWidth * mHeight; } 00387 unsigned int filters() const { return mFilters; } 00393 void setFilters(unsigned int filters) { mFilters = filters; } 00394 00405 ColorType colorAtPoint(const Point& point) const { 00406 int row = point.row; 00407 int col = point.col; 00408 return (mFilters >> (((row << 1 & 14) | (col & 1)) << 1)) & 3; 00409 } 00421 ColorType colorAtPoint(unsigned int row, unsigned int col) const { 00422 return colorAtPoint(Point(row, col)); 00423 } 00424 00445 PixelType* pixels() { return &mPixels[0]; } 00449 PixelType* pixelsEnd() { return &mPixels[mWidth * mHeight]; } 00450 00454 const PixelType* constPixels() const { return &mPixels[0]; } 00458 const PixelType* constPixelsEnd() const { return &mPixels[mWidth * mHeight]; } 00459 00466 const PixelType* constPixelsAtRow(int row) const { 00467 return &mPixels[row * mWidth]; 00468 } 00475 PixelType* pixelsAtRow(int row) { 00476 return &mPixels[row * mWidth]; 00477 } 00478 00485 PixelType* pixelsAtPoint(const Point& point) { 00486 int row = point.row; 00487 int col = point.col; 00488 return &mPixels[row * mWidth + col]; 00489 } 00497 PixelType* pixelsAtPoint(unsigned int row, unsigned int col) { 00498 return pixelsAtPoint(Point(row, col)); 00499 } 00506 const PixelType* constPixelsAtPoint(const Point& point) const { 00507 int row = point.row; 00508 int col = point.col; 00509 return &mPixels[row * mWidth + col]; 00510 } 00518 const PixelType* constPixelsAtPoint( 00519 unsigned int row, unsigned int col) const { 00520 return constPixelsAtPoint(Point(row, col)); 00521 } 00522 00526 PixelType& pixelAtPoint(const Point& point) { 00527 return *pixelsAtPoint(point); 00528 } 00532 PixelType& pixelAtPoint(unsigned int row, unsigned int col) { 00533 return *pixelsAtPoint(row, col); 00534 } 00538 const PixelType& constPixelAtPoint(const Point& point) const { 00539 return *constPixelsAtPoint(point); 00540 } 00544 const PixelType& constPixelAtPoint(unsigned int row, unsigned int col) const { 00545 return *constPixelsAtPoint(row, col); 00546 } 00547 }; 00548 00549 typedef RGBPixel<unsigned short> u16RGBPixel; 00550 typedef LABPixel<short> s16LABPixel; 00551 typedef GrayPixel<unsigned short> u16GrayPixel; 00552 00553 typedef Image<u16RGBPixel> RGBImage; 00554 typedef Image<s16LABPixel> LABImage; 00555 typedef Image<u16GrayPixel> GrayImage; 00556 00557 }; /* namespace refinery */ 00558 00559 #endif /* REFINERY_IMAGE_H */