refinery
RAW file processor
|
00001 #ifndef _REFINERY_HISTOGRAM_H 00002 #define _REFINERY_HISTOGRAM_H 00003 00004 #include <cstddef> 00005 #include <limits> 00006 00007 namespace refinery { 00008 00023 template<typename ImageType, unsigned int TCoarseness = 0> 00024 class Histogram { 00025 public: 00027 typedef typename ImageType::ValueType ValueType; 00029 typedef typename ImageType::PixelType PixelType; 00031 typedef typename ImageType::ColorType ColorType; 00033 static const unsigned int Coarseness = TCoarseness; 00035 static const ColorType NColors = PixelType::NColors; 00036 00037 private: 00038 typedef std::vector<unsigned int> CurveType; 00039 00040 CurveType mCurves[NColors]; 00041 unsigned int mNPixels; 00042 00043 void init(const ImageType& image) 00044 { 00045 // XXX only works for unsigned integer types 00046 const std::size_t nSlots 00047 = (std::numeric_limits<ValueType>::max() >> Coarseness) + 1; 00048 00049 for (ColorType c = 0; c < NColors; c++) { 00050 mCurves[c].assign(nSlots, 0); 00051 } 00052 00053 const PixelType* pixel(image.constPixels()); 00054 const PixelType* endPixel(image.constPixelsEnd()); 00055 00056 for (;pixel < endPixel; pixel++) { 00057 for (ColorType c = 0; c < NColors; c++) { 00058 mCurves[c][pixel->at(c) >> Coarseness]++; 00059 } 00060 } 00061 00062 this->mNPixels = image.nPixels(); 00063 } 00064 00065 public: 00073 Histogram(const ImageType& image) 00074 { 00075 this->init(image); 00076 } 00077 00086 inline unsigned int nSlots() const { return this->mCurves[0].size(); } 00087 00096 inline unsigned int nPixels() const { return this->mNPixels; } 00097 00107 inline unsigned short count(const ColorType& color, std::size_t slot) const 00108 { 00109 return mCurves[color][slot]; 00110 } 00111 }; 00112 00113 } // namespace refinery 00114 00115 #endif /* _REFINERY_HISTOGRAM_H */