7 #include "ColorConstants.h" 8 #include "ColorFilter.h" 9 #include "EngaugeAssert.h" 22 const long MASK = 0xf0f0f0f0;
23 return (rgb1 & MASK) == (rgb2 & MASK);
27 QImage &imageFiltered,
28 ColorFilterMode colorFilterMode,
33 ENGAUGE_ASSERT (imageOriginal.width () == imageFiltered.width());
34 ENGAUGE_ASSERT (imageOriginal.height() == imageFiltered.height());
35 ENGAUGE_ASSERT (imageFiltered.format () == QImage::Format_RGB32);
37 for (
int x = 0; x < imageOriginal.width(); x++) {
38 for (
int y = 0; y < imageOriginal.height (); y++) {
40 QColor pixel = imageOriginal.pixel (x, y);
42 if (pixel.rgb() != rgbBackground) {
51 imageFiltered.setPixel (x, y, (isOn ?
52 QColor (Qt::black).rgb () :
53 QColor (Qt::white).rgb ()));
61 ColorList colorCounts;
62 for (
int x = 0; x < image->width (); x++) {
63 mergePixelIntoColorCounts (image->pixel (x, 0), colorCounts);
64 mergePixelIntoColorCounts (image->pixel (x, image->height () - 1), colorCounts);
66 for (
int y = 0; y < image->height (); y++) {
67 mergePixelIntoColorCounts (image->pixel (0, y), colorCounts);
68 mergePixelIntoColorCounts (image->pixel (image->width () - 1, y), colorCounts);
74 for (ColorList::const_iterator itr = colorCounts.begin (); itr != colorCounts.end (); itr++) {
75 if ((*itr).count > entryMax.
count) {
80 return entryMax.
color.rgb();
83 void ColorFilter::mergePixelIntoColorCounts (QRgb pixel,
84 ColorList &colorCounts)
const 92 for (ColorList::iterator itr = colorCounts.begin (); itr != colorCounts.end (); itr++) {
94 (*itr).color.rgb())) {
102 colorCounts.append (entry);
114 (x < image.width()) &&
115 (y < image.height())) {
119 const int BLACK_WHITE_THRESHOLD = 255 / 2;
120 int gray = qGray (pixelRGB (image, x, y));
121 rtn = (gray < BLACK_WHITE_THRESHOLD);
132 double high0To1)
const 140 if (low0To1 <= high0To1) {
143 rtn = (low0To1 <= s) && (s <= high0To1);
148 rtn = (s <= high0To1) || (low0To1 <= s);
158 QRgb rgbBackground)
const 162 switch (colorFilterMode) {
163 case COLOR_FILTER_MODE_FOREGROUND:
165 double distance = qSqrt (pow ((
double) pixel.red() - qRed (rgbBackground), 2) +
166 pow ((
double) pixel.green() - qGreen (rgbBackground), 2) +
167 pow ((
double) pixel.blue() - qBlue (rgbBackground), 2));
168 s = distance / qSqrt (255.0 * 255.0 + 255.0 * 255.0 + 255.0 * 255.0);
172 case COLOR_FILTER_MODE_HUE:
181 case COLOR_FILTER_MODE_INTENSITY:
183 double distance = qSqrt (pow ((
double) pixel.red(), 2) +
184 pow ((
double) pixel.green(), 2) +
185 pow ((
double) pixel.blue(), 2));
186 s = distance / qSqrt (255.0 * 255.0 + 255.0 * 255.0 + 255.0 * 255.0);
190 case COLOR_FILTER_MODE_SATURATION:
191 s = pixel.saturationF();
194 case COLOR_FILTER_MODE_VALUE:
199 ENGAUGE_ASSERT (
false);
210 switch (colorFilterMode) {
211 case COLOR_FILTER_MODE_FOREGROUND:
213 value = FOREGROUND_MIN + s * (FOREGROUND_MAX - FOREGROUND_MIN);
217 case COLOR_FILTER_MODE_HUE:
219 value = HUE_MIN + s * (HUE_MAX - HUE_MIN);
223 case COLOR_FILTER_MODE_INTENSITY:
225 value = INTENSITY_MIN + s * (INTENSITY_MAX - INTENSITY_MIN);
229 case COLOR_FILTER_MODE_SATURATION:
231 value = SATURATION_MIN + s * (SATURATION_MAX - SATURATION_MIN);
235 case COLOR_FILTER_MODE_VALUE:
237 value = VALUE_MIN + s * (VALUE_MAX - VALUE_MIN);
242 ENGAUGE_ASSERT (
false);
double pixelToZeroToOneOrMinusOne(ColorFilterMode colorFilterMode, const QColor &pixel, QRgb rgbBackground) const
Return pixel converted according to the current filter parameter, normalized to zero to one...
QColor color
Unique color entry.
int zeroToOneToValue(ColorFilterMode colorFilterMode, double s) const
Inverse of pixelToZeroToOneOrMinusOne.
QRgb marginColor(const QImage *image) const
Identify the margin color of the image, which is defined as the most common color in the four margins...
ColorFilter()
Single constructor.
unsigned int count
Number of times this color has appeared.
bool colorCompare(QRgb rgb1, QRgb rgb2) const
See if the two color values are close enough to be considered to be the same.
bool pixelFilteredIsOn(const QImage &image, int x, int y) const
Return true if specified filtered pixel is on.
Helper class so ColorFilter class can compute the background color.
bool pixelUnfilteredIsOn(ColorFilterMode colorFilterMode, const QColor &pixel, QRgb rgbBackground, double low0To1, double high0To1) const
Return true if specified unfiltered pixel is on.
void filterImage(const QImage &imageOriginal, QImage &imageFiltered, ColorFilterMode colorFilterMode, double low, double high, QRgb rgbBackground)
Filter the original image according to the specified filtering parameters.