1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released      *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission.     *
5  ******************************************************************************************************/
6 
7 #ifndef COLOR_FILTER_H
8 #define COLOR_FILTER_H
9 
10 #include "ColorFilterEntry.h"
11 #include "ColorFilterMode.h"
12 #include <QList>
13 #include <QMap>
14 #include <QRgb>
15 
16 class ColorFilterStrategyAbstractBase;
17 class QImage;
18 
19 /// Class for filtering image to remove unimportant information.
20 class ColorFilter
21 {
22 public:
23   /// Single constructor.
24   ColorFilter();
25 
26   /// Destructor deallocates memory
27   ~ColorFilter();
28 
29   /// See if the two color values are close enough to be considered to be the same.
30   bool colorCompare (QRgb rgb1,
31                      QRgb rgb2) const;
32 
33   /// Filter the original image according to the specified filtering parameters.
34   void filterImage (const QImage &imageOriginal,
35                     QImage &imageFiltered,
36                     ColorFilterMode colorFilterMode,
37                     double low,
38                     double high,
39                     QRgb rgbBackground);
40 
41   /// Identify the margin color of the image, which is defined as the most common color in the four margins. For speed,
42   /// only pixels in the four borders are examined, with the results from those borders safely representing the most
43   /// common color of the entire margin areas.
44   QRgb marginColor(const QImage *image) const;
45 
46   /// Return true if specified filtered pixel is on
47   bool pixelFilteredIsOn (const QImage &image,
48                           int x,
49                           int y) const;
50 
51   /// Return pixel converted according to the current filter parameter, normalized to zero to one. Special
52   /// case is -1 for a pixel that cannot be converted, like finding hue value for gray scale pixel
53   double pixelToZeroToOneOrMinusOne (ColorFilterMode colorFilterMode,
54                                      const QColor &pixel,
55                                      QRgb rgbBackground) const;
56 
57   /// Return true if specified unfiltered pixel is on
58   bool pixelUnfilteredIsOn (ColorFilterMode colorFilterMode,
59                             const QColor &pixel,
60                             QRgb rgbBackground,
61                             double low0To1,
62                             double high0To1) const;
63 
64   /// Inverse of pixelToZeroToOneOrMinusOne
65   int zeroToOneToValue (ColorFilterMode colorFilterMode,
66                         double s) const;
67 
68 private:
69 
70   void createStrategies ();
71 
72   typedef QList<ColorFilterEntry> ColorList;
73 
74   void mergePixelIntoColorCounts (QRgb pixel,
75                                   ColorList &colorCounts) const;
76 
77   // Strategies for mode-specific computations
78   QMap<ColorFilterMode, ColorFilterStrategyAbstractBase*> m_strategies;
79 
80 };
81 
82 #endif // COLOR_FILTER_H
83