]> git.sesse.net Git - nageru/blob - nageru/rgb_histogram.cpp
fb1a0ca0783ed2e6d7079fb50d12ba93f7e95350
[nageru] / nageru / rgb_histogram.cpp
1 #include "rgb_histogram.h"
2
3 #include <QPainter>
4 #include <QRect>
5
6 class QPaintEvent;
7 class QResizeEvent;
8
9 using namespace std;
10
11 static constexpr int margin_left = 16;
12 static constexpr int margin_top = 14;
13 static constexpr int margin_right = 14;
14 static constexpr int margin_bottom = 25;
15
16 RGBHistogram::RGBHistogram(QWidget *parent)
17         : QWidget(parent), red(256, 0.0), green(256, 0.0), blue(256, 0.0)
18 {
19 }
20
21 void RGBHistogram::set_histograms(std::vector<double> red, std::vector<double> green, std::vector<double> blue)
22 {
23         assert(red.size() == 256);
24         assert(green.size() == 256);
25         assert(blue.size() == 256);
26         this->red = std::move(red);
27         this->green = std::move(green);
28         this->blue = std::move(blue);
29         repaint();
30 }
31
32 void RGBHistogram::paintEvent(QPaintEvent *event)
33 {
34         QPainter painter(this);
35         painter.fillRect(0, 0, width(), height(), Qt::white);
36         painter.setRenderHints(QPainter::Antialiasing);
37
38         double max = 0.0;
39         for (unsigned i = 0; i < 256; ++i) {
40                 max = std::max(max, red[i]);
41                 max = std::max(max, green[i]);
42                 max = std::max(max, blue[i]);
43         }
44
45         // Draw the graphs. (We'd like to get rid of the bottom line, but that's just cosmetic.)
46         painter.setPen(QPen(Qt::red));
47         painter.setBrush(QBrush(QColor(255, 127, 127, 80)));
48         draw(red, max, Qt::GlobalColor::red, &painter);
49
50         painter.setPen(QPen(Qt::green));
51         painter.setBrush(QBrush(QColor(127, 255, 127, 80)));
52         draw(green, max, Qt::GlobalColor::green, &painter);
53
54         painter.setPen(QPen(Qt::blue));
55         painter.setBrush(QBrush(QColor(127, 127, 255, 80)));
56         draw(blue, max, Qt::GlobalColor::blue, &painter);
57
58         // Draw the tick lines.
59         int w = (width() - 1) - margin_left - margin_right;
60         painter.setPen(QPen(Qt::black));
61         painter.setBrush(QBrush());
62         for (int i = 0; i < 256; i += 10) {
63                 int th = (i % 50 == 0) ? 5 : 2;
64                 double x = (i / 255.0) * w + margin_left;
65                 double y = (height() - 1) - margin_top - margin_bottom;
66                 painter.drawLine(QPointF(x, y + margin_top), QPointF(x, y + margin_top - th));
67         }
68
69         // And the axis labels.
70         for (int i = 0; i < 256; i += 50) {
71                 char buf[16];
72                 snprintf(buf, sizeof(buf), "%d", i);
73
74                 double x = (i / 255.0) * w + margin_left;
75                 double y = (height() - 1) - margin_top - margin_bottom;
76                 painter.drawText(QRectF(x - 20, y + margin_top, 40, margin_bottom), Qt::AlignCenter | Qt::AlignTop, QString(buf));
77         }
78 }
79
80 void RGBHistogram::draw(const vector<double> &v, double max, Qt::GlobalColor color, QPainter *painter)
81 {
82         int w = (width() - 1) - margin_left - margin_right;
83         int h = (height() - 1) - margin_top - margin_bottom;
84
85         QPointF points[258];
86         points[0] = QPointF(margin_left, margin_top + h);
87         for (int i = 0; i < 256; ++i) {
88                 double x = (i / 255.0) * w + margin_left;
89                 double y = (1.0 - (v[i] / max)) * h + margin_top;
90                 points[i + 1] = QPointF(x, y);
91         }
92         points[257] = QPointF(margin_left + w, margin_top + h);
93         painter->drawPolygon(points, 258);
94 }