Engauge Digitizer
2
Toggle main menu visibility
Loading...
Searching...
No Matches
Color
ColorFilterHistogram.cpp
Go to the documentation of this file.
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
#include "
ColorFilter.h
"
8
#include "
ColorFilterHistogram.h
"
9
#include "
EngaugeAssert.h
"
10
#include <QImage>
11
#include <qmath.h>
12
13
ColorFilterHistogram::ColorFilterHistogram
()
14
{
15
}
16
17
int
ColorFilterHistogram::binFromPixel
(
const
ColorFilter
&filter,
18
ColorFilterMode
colorFilterMode,
19
const
QColor &pixel,
20
const
QRgb &rgbBackground)
const
21
{
22
// Instead of mapping from s=0 through 1 to bin=0 through HISTOGRAM_BINS-1, we
23
// map it to bin=1 through HISTOGRAM_BINS-2 so first and last bin are zero. The
24
// result is a peak at the start or end is complete and easier to read
25
double
s = filter.
pixelToZeroToOneOrMinusOne
(colorFilterMode,
26
pixel,
27
rgbBackground);
28
ENGAUGE_ASSERT
(s <= 1.0);
29
30
int
bin = -1;
31
32
if
(s >= 0) {
33
34
bin = qFloor (FIRST_NON_EMPTY_BIN_AT_START () + s * (LAST_NON_EMPTY_BIN_AT_END () - FIRST_NON_EMPTY_BIN_AT_START ()));
35
36
}
37
38
return
bin;
39
}
40
41
void
ColorFilterHistogram::generate
(
const
ColorFilter
&filter,
42
double
histogramBins [],
43
ColorFilterMode
colorFilterMode,
44
const
QImage &image,
45
int
&maxBinCount)
const
46
{
47
// Initialize histogram bins
48
int
bin;
49
for
(bin = 0; bin <
HISTOGRAM_BINS
(); bin++) {
50
histogramBins [bin] = 0;
51
}
52
53
QRgb rgbBackground = filter.
marginColor
(&image);
54
55
// Populate histogram bins
56
maxBinCount = 0;
57
for
(
int
x = 0; x < image.width(); x++) {
58
for
(
int
y = 0; y < image.height(); y++) {
59
60
QColor pixel (image.pixel (x, y));
61
int
bin =
binFromPixel
(filter,
62
colorFilterMode,
63
pixel,
64
rgbBackground);
65
if
(bin >= 0) {
66
67
ENGAUGE_ASSERT
((FIRST_NON_EMPTY_BIN_AT_START () <= bin) &&
68
(LAST_NON_EMPTY_BIN_AT_END () >= bin));
69
++(histogramBins [bin]);
70
71
if
(histogramBins [bin] > maxBinCount) {
72
maxBinCount = qFloor (histogramBins [bin]);
73
}
74
}
75
}
76
}
77
}
78
79
int
ColorFilterHistogram::valueFromBin
(
const
ColorFilter
&filter,
80
ColorFilterMode
colorFilterMode,
81
int
bin)
82
{
83
// Just do everything in binFromPixel backwards
84
double
s = double (bin - FIRST_NON_EMPTY_BIN_AT_START ()) / double (LAST_NON_EMPTY_BIN_AT_END () - FIRST_NON_EMPTY_BIN_AT_START ());
85
s = qMin (qMax (s, 0.0), 1.0);
86
87
return
filter.
zeroToOneToValue
(colorFilterMode,
88
s);
89
}
ColorFilterHistogram.h
ColorFilterMode
ColorFilterMode
Definition
ColorFilterMode.h:12
ColorFilter.h
EngaugeAssert.h
ENGAUGE_ASSERT
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT.
Definition
EngaugeAssert.h:17
ColorFilterHistogram::generate
void generate(const ColorFilter &filter, double histogramBins[], ColorFilterMode colorFilterMode, const QImage &image, int &maxBinCount) const
Generate the histogram.
Definition
ColorFilterHistogram.cpp:41
ColorFilterHistogram::ColorFilterHistogram
ColorFilterHistogram()
Single constructor.
Definition
ColorFilterHistogram.cpp:13
ColorFilterHistogram::valueFromBin
int valueFromBin(const ColorFilter &filter, ColorFilterMode colorFilterMode, int bin)
Inverse of binFromPixel.
Definition
ColorFilterHistogram.cpp:79
ColorFilterHistogram::HISTOGRAM_BINS
static int HISTOGRAM_BINS()
Number of histogram bins.
Definition
ColorFilterHistogram.h:39
ColorFilterHistogram::binFromPixel
int binFromPixel(const ColorFilter &filter, ColorFilterMode colorFilterMode, const QColor &pixel, const QRgb &rgbBackground) const
Compute histogram bin number from pixel according to filter.
Definition
ColorFilterHistogram.cpp:17
ColorFilter
Class for filtering image to remove unimportant information.
Definition
ColorFilter.h:21
ColorFilter::marginColor
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...
Definition
ColorFilter.cpp:79
ColorFilter::zeroToOneToValue
int zeroToOneToValue(ColorFilterMode colorFilterMode, double s) const
Inverse of pixelToZeroToOneOrMinusOne.
Definition
ColorFilter.cpp:197
ColorFilter::pixelToZeroToOneOrMinusOne
double pixelToZeroToOneOrMinusOne(ColorFilterMode colorFilterMode, const QColor &pixel, QRgb rgbBackground) const
Return pixel converted according to the current filter parameter, normalized to zero to one.
Definition
ColorFilter.cpp:177
Generated on
for Engauge Digitizer by
1.17.0