00001
00012 #ifndef __HISTOGRAM_IMAGE_H__
00013 #define __HISTOGRAM_IMAGE_H__
00014
00015
00016 #include "../src/image.h"
00017 #include "../src/pixel.h"
00018 #include "../src/colorlookup.h"
00019 #include "../src/histogram.h"
00020
00021 namespace Plot {
00022
00023
00030 template<class PixelType=RGBPixel<>, class PixelMatrix=MatrixType<PixelType>::rectangular >
00031 class OneDHistogramImage : public AARImage<PixelType,PixelMatrix> {
00032 private:
00033
00035 typedef AARImage<PixelType,PixelMatrix>::PixelSubscript PixelSubscript;
00036
00038 typedef int Subscript;
00039
00041 PixelSubscript border;
00042
00044 double num_bins;
00045
00047 double size_scale;
00048
00050 bool vertical;
00051
00053 Bounded::Magnitude bin_fullness_range;
00054
00056 PixelSubscript min_pixelnum;
00057
00059 PixelSubscript zero_pixelnum;
00060
00062 PixelSubscript max_pixelnum;
00063
00065 PixelType bg;
00066
00068 PixelType fg;
00069
00071 bool markzero;
00072
00074 inline PixelSubscript bin_number_pixelnum(const Subscript b) const
00075 { return PixelSubscript(b*size_scale+border); }
00076
00078
00079 inline PixelSubscript bin_fullness_pixelnum(const Bounded::Magnitude bin_fullness) const {
00080 return Generic::crop(min_pixelnum,max_pixelnum,
00081 min_pixelnum+Subscript((1+max_pixelnum-min_pixelnum)*
00082 (bin_fullness/bin_fullness_range)));
00083 }
00084
00085 void draw_element(const Subscript bin_number, const Bounded::Magnitude bin_fullness, PixelType p=default_fg);
00086 void draw_element_vertical(const Subscript bin_number, const Bounded::Magnitude bin_fullness, PixelType p=default_fg);
00087
00088
00089 public:
00092
00094 OneDHistogramImage() { }
00095
00096 template <class Histogram>
00097 OneDHistogramImage(const Histogram &h, bool vertical=false,
00098 double size_scale_i=1.0, double aspect_ratio_i=1.0,
00099 Bounded::Magnitude bin_fullness_range_i=1.0, int borderwidth=0,
00100 const ColorLookup<PixelType>& L=HueColorLookup<PixelType>());
00102 };
00103
00104
00105
00114 template<class PixelType, class PixelMatrix>
00115 template <class Histogram>
00116 OneDHistogramImage<PixelType,PixelMatrix>::OneDHistogramImage
00117 (const Histogram &h, bool vertical, double size_scale_i, double aspect_ratio_i,
00118 Bounded::Magnitude bin_fullness_range_i, int borderwidth, const ColorLookup<PixelType>& L)
00119 : AARImage<PixelType>(2*borderwidth+Subscript(h.num_bins()*size_scale_i*(vertical?1.0:aspect_ratio_i)),
00120 2*borderwidth+Subscript(h.num_bins()*size_scale_i*(vertical?aspect_ratio_i:1.0))),
00121 size_scale(size_scale_i), bin_fullness_range(bin_fullness_range_i), border(borderwidth),
00122 min_pixelnum(borderwidth), zero_pixelnum(borderwidth), num_bins(h.num_bins()),
00123 max_pixelnum(borderwidth+PixelSubscript(h.num_bins()*size_scale_i*aspect_ratio_i)),
00124 bg(default_bg), fg(default_fg), markzero(false) {
00125
00126 draw_border(borderwidth);
00127 typedef typename Histogram::size_type Subscript;
00128
00129 if (vertical)
00130 for (Subscript b=0; b<h.num_bins(); b++)
00131 draw_element_vertical(b,h.value_percent(b)/100,L((b*1.0)/h.num_bins()));
00132 else
00133 for (Subscript b=0; b<h.num_bins(); b++)
00134 draw_element(b,h.value_percent(b)/100,L((b*1.0)/h.num_bins()));
00135 }
00136
00137
00138
00145 template<class PixelType, class PixelMatrix>
00146 void OneDHistogramImage<PixelType,PixelMatrix>::draw_element_vertical
00147 (const Subscript bin_number, const Bounded::Magnitude bin_fullness, PixelType p)
00148 {
00149 const PixelSubscript pivot = bin_fullness_pixelnum(bin_fullness);
00150 const PixelSubscript binpl = bin_number_pixelnum(bin_number);
00151 const PixelSubscript binph = bin_number_pixelnum(bin_number+1);
00152
00153
00154 draw_rectangle( binpl, zero_pixelnum, binph, pivot, p );
00155 draw_rectangle( binpl, pivot, binph, max_pixelnum, bg);
00156 if (markzero)
00157 draw_rectangle(binpl, zero_pixelnum, binph, zero_pixelnum+1, fg);
00158 }
00159
00160
00161
00168 template<class PixelType, class PixelMatrix>
00169 void OneDHistogramImage<PixelType,PixelMatrix>::draw_element
00170 (const Subscript bin_number, const Bounded::Magnitude bin_fullness, PixelType p)
00171 {
00172 const PixelSubscript pivot = bin_fullness_pixelnum(bin_fullness);
00173 const PixelSubscript binpl = bin_number_pixelnum(bin_number);
00174 const PixelSubscript binph = bin_number_pixelnum(bin_number+1);
00175
00176
00177 draw_rectangle( nrows()-zero_pixelnum, binpl, nrows()-pivot, binph, p );
00178 draw_rectangle( nrows()-pivot, binpl, nrows()-max_pixelnum, binph, bg);
00179 if (markzero)
00180 draw_rectangle(nrows()-zero_pixelnum, binpl, nrows()-zero_pixelnum+1, binph, fg);
00181 }
00182
00183
00184 }
00185 #endif