1 #include "ColorConstants.h"
2 #include "ColorFilter.h"
3 #include "EngaugeAssert.h"
16 const long MASK = 0xf0f0f0f0;
17 return (rgb1 & MASK) == (rgb2 & MASK);
21 QImage &imageFiltered,
22 ColorFilterMode colorFilterMode,
27 ENGAUGE_ASSERT (imageOriginal.width () == imageFiltered.width());
28 ENGAUGE_ASSERT (imageOriginal.height() == imageFiltered.height());
29 ENGAUGE_ASSERT (imageFiltered.format () == QImage::Format_RGB32);
31 for (
int x = 0; x < imageOriginal.width(); x++) {
32 for (
int y = 0; y < imageOriginal.height (); y++) {
34 QColor pixel = imageOriginal.pixel (x, y);
36 if (pixel.rgb() != rgbBackground) {
45 imageFiltered.setPixel (x, y, (isOn ?
46 QColor (Qt::black).rgb () :
47 QColor (Qt::white).rgb ()));
55 ColorList colorCounts;
56 for (
int x = 0; x < image->width (); x++) {
57 mergePixelIntoColorCounts (image->pixel (x, 0), colorCounts);
58 mergePixelIntoColorCounts (image->pixel (x, image->height () - 1), colorCounts);
60 for (
int y = 0; y < image->height (); y++) {
61 mergePixelIntoColorCounts (image->pixel (0, y), colorCounts);
62 mergePixelIntoColorCounts (image->pixel (image->width () - 1, y), colorCounts);
68 for (ColorList::const_iterator itr = colorCounts.begin (); itr != colorCounts.end (); itr++) {
69 if ((*itr).count > entryMax.
count) {
74 return entryMax.
color.rgb();
77 void ColorFilter::mergePixelIntoColorCounts (QRgb pixel,
78 ColorList &colorCounts)
const
86 for (ColorList::iterator itr = colorCounts.begin (); itr != colorCounts.end (); itr++) {
88 (*itr).color.rgb())) {
96 colorCounts.append (entry);
108 (x < image.width()) &&
109 (y < image.height())) {
113 const int BLACK_WHITE_THRESHOLD = 255 / 2;
114 int gray = qGray (pixelRGB (image, x, y));
115 rtn = (gray < BLACK_WHITE_THRESHOLD);
126 double high0To1)
const
134 if (low0To1 <= high0To1) {
137 rtn = (low0To1 <= s) && (s <= high0To1);
142 rtn = (s <= high0To1) || (low0To1 <= s);
152 QRgb rgbBackground)
const
156 switch (colorFilterMode) {
157 case COLOR_FILTER_MODE_FOREGROUND:
159 double distance = qSqrt (pow (pixel.red() - qRed (rgbBackground), 2) +
160 pow (pixel.green() - qGreen (rgbBackground), 2) +
161 pow (pixel.blue() - qBlue (rgbBackground), 2));
162 s = distance / qSqrt (255.0 * 255.0 + 255.0 * 255.0 + 255.0 * 255.0);
166 case COLOR_FILTER_MODE_HUE:
175 case COLOR_FILTER_MODE_INTENSITY:
177 double distance = qSqrt (pow (pixel.red(), 2) +
178 pow (pixel.green(), 2) +
179 pow (pixel.blue(), 2));
180 s = distance / qSqrt (255 * 255 + 255 * 255 + 255 * 255);
184 case COLOR_FILTER_MODE_SATURATION:
185 s = pixel.saturationF();
188 case COLOR_FILTER_MODE_VALUE:
193 ENGAUGE_ASSERT (
false);
204 switch (colorFilterMode) {
205 case COLOR_FILTER_MODE_FOREGROUND:
207 value = FOREGROUND_MIN + s * (FOREGROUND_MAX - FOREGROUND_MIN);
211 case COLOR_FILTER_MODE_HUE:
213 value = HUE_MIN + s * (HUE_MAX - HUE_MIN);
217 case COLOR_FILTER_MODE_INTENSITY:
219 value = INTENSITY_MIN + s * (INTENSITY_MAX - INTENSITY_MIN);
223 case COLOR_FILTER_MODE_SATURATION:
225 value = SATURATION_MIN + s * (SATURATION_MAX - SATURATION_MIN);
229 case COLOR_FILTER_MODE_VALUE:
231 value = VALUE_MIN + s * (VALUE_MAX - VALUE_MIN);
236 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.