www.pudn.com > colortracker.rar > histogram_hue.h


#ifndef __HISTOGRAM_HUE__ 
#define __HISTOGRAM_HUE__ 
 
#include "preprocessor.h" 
 
#include "Images/color.h" 
 
////////////////////////////////////// 
 
template class histogram_hue { 
private: 
	double m_hist[BINSIZE];	 
	int m_totalMemSize; 
	int m_numBins; 
	int m_numColors; 
	MPColorTools m_color; 
 
	inline double & get_prob(const int & bin) { 
		return(m_hist[bin]); 
	} 
 
	inline void rgb2bin(const RGBTRIPLE & rgb, int & bin) { 
		const double alpha = 0.0;	// -0.5 left edge of distribution 
		const double binwidth = static_cast(m_numColors)/static_cast(BINSIZE); 
		float h = 0.0, s = 0.0, v = 0.0; 
		m_color.RGBtoHSV(&rgb, h, s, v); 
		bin = (int) (h*m_numBins/360.0); 
	} 
 
  inline void addtobin(const int & bin, double value = 1.0) { 
		m_hist[bin] += value; 
	} 
 
public: 
	histogram_hue() { 
		m_numBins = BINSIZE; 
		m_totalMemSize = m_numBins * sizeof(double); 
		m_numColors = 256; 
		clear(); 
	} 
	 
	inline double *begin () { 
		return (&(m_hist[0])); 
	} 
 
	inline const double *end () { 
		return(&(m_hist[BINSIZE])); 
	} 
 
	inline double & operator[] (const int & bin) { 
		return(m_hist[bin]); 
	} 
 
  inline void addtobin(const RGBTRIPLE & rgb, const double value = 1.0) { 
		int bin; 
		rgb2bin(rgb, bin); 
		addtobin(bin, value); 
	} 
 
	inline double & get_prob(const RGBTRIPLE & rgb) { 
		int bin; 
		rgb2bin(rgb, bin); 
		return(get_prob(bin)); 
	} 
 
	void clear () { 
		memset(m_hist, 0, m_totalMemSize); 
	} 
 
	histogram_hue & copy(histogram_hue & hist) { 
		memcpy(hist.m_hist, m_hist, m_totalMemSize); 
		return(hist); 
	} 
 
	void normalize() { 
		double s = sum(); 
		for (double *it = begin(); it != end(); it++) { 
			if (*it) 
				*it /= s; 
		} 
	} 
 
	double sum() { 
		double s = 0.0; 
		for (double *it = begin(); it != end(); it++) 
			s += *it; 
		return (s); 
	} 
 
	void add_uniform (double weight = 1.0) { 
		const double uniform = weight*(1.0/static_cast(m_numBins)); 
		for (double *it = begin(); it != end(); it++) 
			*it = uniform + *it*(1-weight); 
	} 
 
	void weighted_add (histogram_hue & hist, const double & weight) { 
		for (double *toptr = begin(), *fromptr = hist.begin(); toptr != end(); toptr++, fromptr++) 
			*toptr = *toptr*weight + *fromptr*(1-weight); 
	} 
 
	void print_values (std::ostream &os) { 
		int count = 1; 
		for (double *it = begin(); it != end(); it++) { 
			count++; 
			os << *it << endl; 
		} 
		count; 
	} 
 
	void load_from_file (ifstream &is) { 
		clear(); 
		for (double *it = begin(); it != end(); it++) 
			is >> *it; 
	} 
 
	void save_to_file (ofstream &os) { 
		print_values (os); 
	} 
}; 
 
////////////////////////////////////// 
 
#endif __HISTOGRAM_HUE__