7 #include "ColorConstants.h" 8 #include "ColorFilter.h" 9 #include "ColorFilterStrategyForeground.h" 10 #include "ColorFilterStrategyHue.h" 11 #include "ColorFilterStrategyIntensity.h" 12 #include "ColorFilterStrategySaturation.h" 13 #include "ColorFilterStrategyValue.h" 14 #include "EngaugeAssert.h" 28 const long MASK = 0xf0f0f0f0;
29 return (rgb1 & MASK) == (rgb2 & MASK);
32 void ColorFilter::createStrategies ()
42 QImage &imageFiltered,
43 ColorFilterMode colorFilterMode,
48 ENGAUGE_ASSERT (imageOriginal.width () == imageFiltered.width());
49 ENGAUGE_ASSERT (imageOriginal.height() == imageFiltered.height());
50 ENGAUGE_ASSERT (imageFiltered.format () == QImage::Format_RGB32);
52 for (
int x = 0; x < imageOriginal.width(); x++) {
53 for (
int y = 0; y < imageOriginal.height (); y++) {
55 QColor pixel = imageOriginal.pixel (x, y);
57 if (pixel.rgb() != rgbBackground) {
66 imageFiltered.setPixel (x, y, (isOn ?
67 QColor (Qt::black).rgb () :
68 QColor (Qt::white).rgb ()));
76 ColorList colorCounts;
77 for (
int x = 0; x < image->width (); x++) {
78 mergePixelIntoColorCounts (image->pixel (x, 0), colorCounts);
79 mergePixelIntoColorCounts (image->pixel (x, image->height () - 1), colorCounts);
81 for (
int y = 0; y < image->height (); y++) {
82 mergePixelIntoColorCounts (image->pixel (0, y), colorCounts);
83 mergePixelIntoColorCounts (image->pixel (image->width () - 1, y), colorCounts);
89 for (ColorList::const_iterator itr = colorCounts.begin (); itr != colorCounts.end (); itr++) {
90 if ((*itr).count > entryMax.
count) {
95 return entryMax.
color.rgb();
98 void ColorFilter::mergePixelIntoColorCounts (QRgb pixel,
99 ColorList &colorCounts)
const 107 for (ColorList::iterator itr = colorCounts.begin (); itr != colorCounts.end (); itr++) {
109 (*itr).color.rgb())) {
117 colorCounts.append (entry);
129 (x < image.width()) &&
130 (y < image.height())) {
134 const int BLACK_WHITE_THRESHOLD = 255 / 2;
135 int gray = qGray (pixelRGB (image, x, y));
136 rtn = (gray < BLACK_WHITE_THRESHOLD);
147 double high0To1)
const 155 if (low0To1 <= high0To1) {
158 rtn = (low0To1 <= s) && (s <= high0To1);
163 rtn = (s <= high0To1) || (low0To1 <= s);
173 QRgb rgbBackground)
const 175 if (m_strategies.contains (colorFilterMode)) {
184 ENGAUGE_ASSERT (
false);
193 if (m_strategies.contains (colorFilterMode)) {
200 ENGAUGE_ASSERT (
false);
Leaf class for foreground strategy for ColorFilter.
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...
bool colorCompare(QRgb rgb1, QRgb rgb2) const
See if the two color values are close enough to be considered to be the same.
virtual int zeroToOneToValue(double s) const =0
Return the low value normalized to 0 to 1.
bool pixelFilteredIsOn(const QImage &image, int x, int y) const
Return true if specified filtered pixel is on.
double pixelToZeroToOneOrMinusOne(ColorFilterMode colorFilterMode, const QColor &pixel, QRgb rgbBackground) const
Return pixel converted according to the current filter parameter, normalized to zero to one...
int zeroToOneToValue(ColorFilterMode colorFilterMode, double s) const
Inverse of pixelToZeroToOneOrMinusOne.
QColor color
Unique color entry.
Base class for strategy pattern whose subclasses process the different color filter settings modes (o...
ColorFilter()
Single constructor.
unsigned int count
Number of times this color has appeared.
Leaf class for value strategy for ColorFilter.
Leaf class for intensity strategy for ColorFilter.
Leaf class for hue strategy for ColorFilter.
virtual double pixelToZeroToOne(const QColor &pixel, QRgb rgbBackground) const =0
Return a normalized value of 0 to 1 given input pixel.
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.
Leaf class for saturation strategy for ColorFilter.
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.