00001
00002
00003
00004
00005
00006
00007 #include "ColorFilter.h"
00008 #include "ColorFilterHistogram.h"
00009 #include "EngaugeAssert.h"
00010 #include <QImage>
00011
00012 ColorFilterHistogram::ColorFilterHistogram()
00013 {
00014 }
00015
00016 int ColorFilterHistogram::binFromPixel (const ColorFilter &filter,
00017 ColorFilterMode colorFilterMode,
00018 const QColor &pixel,
00019 const QRgb &rgbBackground) const
00020 {
00021
00022
00023
00024 double s = filter.pixelToZeroToOneOrMinusOne (colorFilterMode,
00025 pixel,
00026 rgbBackground);
00027 ENGAUGE_ASSERT (s <= 1.0);
00028
00029 int bin = -1;
00030
00031 if (s >= 0) {
00032
00033 bin = FIRST_NON_EMPTY_BIN_AT_START () + s * (LAST_NON_EMPTY_BIN_AT_END () - FIRST_NON_EMPTY_BIN_AT_START ());
00034
00035 }
00036
00037 return bin;
00038 }
00039
00040 void ColorFilterHistogram::generate (const ColorFilter &filter,
00041 double histogramBins [],
00042 ColorFilterMode colorFilterMode,
00043 const QImage &image,
00044 int &maxBinCount) const
00045 {
00046
00047 int bin;
00048 for (bin = 0; bin < HISTOGRAM_BINS (); bin++) {
00049 histogramBins [bin] = 0;
00050 }
00051
00052 QRgb rgbBackground = filter.marginColor(&image);
00053
00054
00055 maxBinCount = 0;
00056 for (int x = 0; x < image.width(); x++) {
00057 for (int y = 0; y < image.height(); y++) {
00058
00059 QColor pixel (image.pixel (x, y));
00060 int bin = binFromPixel (filter,
00061 colorFilterMode,
00062 pixel,
00063 rgbBackground);
00064 if (bin >= 0) {
00065
00066 ENGAUGE_ASSERT ((FIRST_NON_EMPTY_BIN_AT_START () <= bin) &&
00067 (LAST_NON_EMPTY_BIN_AT_END () >= bin));
00068 ++(histogramBins [bin]);
00069
00070 if (histogramBins [bin] > maxBinCount) {
00071 maxBinCount = histogramBins [bin];
00072 }
00073 }
00074 }
00075 }
00076 }
00077
00078 int ColorFilterHistogram::valueFromBin (const ColorFilter &filter,
00079 ColorFilterMode colorFilterMode,
00080 int bin)
00081 {
00082
00083 double s = (double) (bin - FIRST_NON_EMPTY_BIN_AT_START ()) / (double) (LAST_NON_EMPTY_BIN_AT_END () - FIRST_NON_EMPTY_BIN_AT_START ());
00084 s = qMin (qMax (s, 0.0), 1.0);
00085
00086 return filter.zeroToOneToValue (colorFilterMode,
00087 s);
00088 }