Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
DlgFilterWorker.cpp
1 #include "ColorFilter.h"
2 #include "DlgFilterWorker.h"
3 #include "Logger.h"
4 #include <QImage>
5 
6 const int NO_DELAY = 0;
7 const int COLUMNS_PER_PIECE = 5;
8 
9 DlgFilterWorker::DlgFilterWorker(const QPixmap &pixmapOriginal,
10  QRgb rgbBackground) :
11  m_imageOriginal (pixmapOriginal.toImage()),
12  m_rgbBackground (rgbBackground),
13  m_colorFilterMode (NUM_COLOR_FILTER_MODES),
14  m_low (-1.0),
15  m_high (-1.0)
16 {
17  m_restartTimer.setSingleShot (false);
18  connect (&m_restartTimer, SIGNAL (timeout ()), this, SLOT (slotRestartTimeout()));
19 }
20 
21 void DlgFilterWorker::slotNewParameters (ColorFilterMode colorFilterMode,
22  double low,
23  double high)
24 {
25  LOG4CPP_INFO_S ((*mainCat)) << "DlgFilterWorker::slotNewParameters filterMode=" << colorFilterMode
26  << " low=" << low
27  << " high=" << high;
28 
29  // Push onto queue
30  DlgFilterCommand command (colorFilterMode,
31  low,
32  high);
33  m_inputCommandQueue.push_back (command);
34 
35  if (!m_restartTimer.isActive()) {
36 
37  // Timer is not currently active so start it up
38  m_restartTimer.start (NO_DELAY);
39  }
40 }
41 
42 void DlgFilterWorker::slotRestartTimeout ()
43 {
44  if (m_inputCommandQueue.count() > 0) {
45 
46  DlgFilterCommand command = m_inputCommandQueue.last();
47  m_inputCommandQueue.clear ();
48 
49  // Start over from the left side
50  m_colorFilterMode = command.colorFilterMode();
51  m_low = command.low0To1();
52  m_high = command.high0To1();
53 
54  m_xLeft = 0;
55 
56  // Start timer to process first piece
57  m_restartTimer.start (NO_DELAY);
58 
59  } else if (m_xLeft < m_imageOriginal.width ()) {
60 
61  // To to process a new piece, starting at m_xLeft
62  int xStop = m_xLeft + COLUMNS_PER_PIECE;
63  if (xStop >= m_imageOriginal.width()) {
64  xStop = m_imageOriginal.width();
65  }
66 
67  // From here on, if a new command gets pushed onto the queue then we immediately stop processing
68  // and do nothing except start the timer so we can start over after the next timeout. The goal is
69  // to not tie up the gui by emitting signalTransferPiece unnecessarily.
70  //
71  // This code is basically a heavily customized version of ColorFilter::filterImage
72  ColorFilter filter;
73  int processedWidth = xStop - m_xLeft;
74  QImage imageProcessed (processedWidth,
75  m_imageOriginal.height(),
76  QImage::Format_RGB32);
77  for (int xFrom = m_xLeft, xTo = 0; (xFrom < xStop) && (m_inputCommandQueue.count() == 0); xFrom++, xTo++) {
78  for (int y = 0; (y < m_imageOriginal.height ()) && (m_inputCommandQueue.count() == 0); y++) {
79  QColor pixel = m_imageOriginal.pixel (xFrom, y);
80  bool isOn = false;
81  if (pixel.rgb() != m_rgbBackground) {
82 
83  isOn = filter.pixelUnfilteredIsOn (m_colorFilterMode,
84  pixel,
85  m_rgbBackground,
86  m_low,
87  m_high);
88  }
89 
90  imageProcessed.setPixel (xTo, y, (isOn ?
91  QColor (Qt::black).rgb () :
92  QColor (Qt::white).rgb ()));
93  }
94  }
95 
96  if (m_inputCommandQueue.count() == 0) {
97  emit signalTransferPiece (m_xLeft,
98  imageProcessed);
99  m_xLeft += processedWidth;
100  }
101 
102  if ((xStop < m_imageOriginal.width()) ||
103  (m_inputCommandQueue.count () > 0)) {
104 
105  // Restart timer to process next piece
106  m_restartTimer.start (NO_DELAY);
107  }
108  }
109 }
Command pattern object for receiving new parameters in DlgFilterWorker from GUI thread.
double low0To1() const
Get method for low value.
Class for filtering image to remove unimportant information.
Definition: ColorFilter.h:12
void slotNewParameters(ColorFilterMode colorFilterMode, double low, double high)
Start processing with a new set of parameters. Any ongoing processing is interrupted when m_filterMod...
void signalTransferPiece(int xLeft, QImage image)
Send a processed vertical piece of the original pixmap. The destination is between xLeft and xLeft+pi...
double high0To1() const
Get method for high value.
bool pixelUnfilteredIsOn(ColorFilterMode colorFilterMode, const QColor &pixel, QRgb rgbBackground, double low0To1, double high0To1) const
Return true if specified unfiltered pixel is on.
ColorFilterMode colorFilterMode() const
Get method for filter mode.
DlgFilterWorker(const QPixmap &pixmapOriginal, QRgb m_rgbBackground)
Single constructor.