]> git.sesse.net Git - kdenlive/blob - src/colorcorrection/histogramgenerator.cpp
Histogram: Fixed max display
[kdenlive] / src / colorcorrection / histogramgenerator.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com)      *
3  *   This file is part of kdenlive. See www.kdenlive.org.                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10
11 #include <algorithm>
12 #include <math.h>
13 #include <QImage>
14 #include <QPainter>
15 #include "histogramgenerator.h"
16
17 HistogramGenerator::HistogramGenerator()
18 {
19 }
20
21 QImage HistogramGenerator::calculateHistogram(const QSize &paradeSize, const QImage &image, const int &components,
22                                               HistogramGenerator::Rec rec, const bool &unscaled, const uint &accelFactor) const
23 {
24 //    qDebug() << "Histogram rect size is: " << paradeSize.width() << "/" << paradeSize.height();
25     if (paradeSize.height() <= 0 || paradeSize.width() <= 0 || image.width() <= 0 || image.height() <= 0) {
26         return QImage();
27     }
28
29     bool drawY = (components & HistogramGenerator::ComponentY) != 0;
30     bool drawR = (components & HistogramGenerator::ComponentR) != 0;
31     bool drawG = (components & HistogramGenerator::ComponentG) != 0;
32     bool drawB = (components & HistogramGenerator::ComponentB) != 0;
33     bool drawSum = (components & HistogramGenerator::ComponentSum) != 0;
34
35     int r[256], g[256], b[256], y[256], s[766];
36     // Initialize the values to zero
37     std::fill(r, r+256, 0);
38     std::fill(g, g+256, 0);
39     std::fill(b, b+256, 0);
40     std::fill(y, y+256, 0);
41     std::fill(s, s+766, 0);
42
43     const uint iw = image.bytesPerLine();
44     const uint ih = image.height();
45     const uint ww = paradeSize.width();
46     const uint wh = paradeSize.height();
47     const uint byteCount = iw*ih;
48     const uint stepsize = 4*accelFactor;
49
50     const uchar *bits = image.bits();
51     QRgb *col;
52
53
54     // Read the stats from the input image
55     for (uint i = 0; i < byteCount; i += stepsize) {
56         col = (QRgb *)bits;
57
58         r[qRed(*col)]++;
59         g[qGreen(*col)]++;
60         b[qBlue(*col)]++;
61         if (drawY) {
62             // Use if branch to avoid expensive multiplication if Y disabled
63             if (rec == HistogramGenerator::Rec_601) {
64                 y[(int)floor(.299*qRed(*col) + .587*qGreen(*col) + .114*qBlue(*col))]++;
65             } else {
66                 y[(int)floor(.2125*qRed(*col) + .7154*qGreen(*col) + .0721*qBlue(*col))]++;
67             }
68         }
69         if (drawSum) {
70             // Use an if branch here because the sum takes more operations than rgb
71             s[qRed(*col)]++;
72             s[qGreen(*col)]++;
73             s[qBlue(*col)]++;
74         }
75
76         bits += stepsize;
77     }
78
79
80     const int nParts = (drawY ? 1 : 0) + (drawR ? 1 : 0) + (drawG ? 1 : 0) + (drawB ? 1 : 0) + (drawSum ? 1 : 0);
81     if (nParts == 0) {
82         // Nothing to draw
83         return QImage();
84     }
85
86     const int d = 20; // Distance for text
87     const int partH = (wh-nParts*d)/nParts;
88     const float scaling = (float)partH/(byteCount >> 7);
89     const int dist = 40;
90
91     int wy = 0; // Drawing position
92
93     QImage histogram(paradeSize, QImage::Format_ARGB32);
94     QPainter davinci(&histogram);
95     davinci.setPen(QColor(220, 220, 220, 255));
96     histogram.fill(qRgba(0, 0, 0, 0));
97
98     if (drawY) {
99 //        qDebug() << "Drawing Y at " << wy << " with height " << partH;
100         drawComponentFull(&davinci, y, scaling, QRect(0, wy, ww, partH + dist), QColor(220, 220, 210, 255), dist, unscaled, 256);
101
102         wy += partH + d;
103     }
104
105     if (drawSum) {
106 //        qDebug() << "Drawing S at " << wy << " with height " << partH;
107         drawComponentFull(&davinci, s, scaling/3, QRect(0, wy, ww, partH + dist), QColor(220, 220, 210, 255), dist, unscaled, 256);
108
109         wy += partH + d;
110     }
111
112     if (drawR) {
113 //        qDebug() << "Drawing R at " << wy << " with height " << partH;
114         drawComponentFull(&davinci, r, scaling, QRect(0, wy, ww, partH + dist), QColor(255, 128, 0, 255), dist, unscaled, 256);
115
116         wy += partH + d;
117     }
118
119     if (drawG) {
120 //        qDebug() << "Drawing G at " << wy << " with height " << partH;
121         drawComponentFull(&davinci, g, scaling, QRect(0, wy, ww, partH + dist), QColor(128, 255, 0, 255), dist, unscaled, 256);
122         wy += partH + d;
123     }
124
125     if (drawB) {
126 //        qDebug() << "Drawing B at " << wy << " with height " << partH;
127         drawComponentFull(&davinci, b, scaling, QRect(0, wy, ww, partH + dist), QColor(0, 128, 255, 255), dist, unscaled, 256);
128
129         wy += partH + d;
130     }
131
132     return histogram;
133 }
134
135 QImage HistogramGenerator::drawComponent(const int *y, const QSize &size, const float &scaling, const QColor &color,
136                                          const bool &unscaled, const uint &max) const
137 {
138     QImage component(max, size.height(), QImage::Format_ARGB32);
139     component.fill(qRgba(0, 0, 0, 0));
140     Q_ASSERT(scaling != INFINITY);
141
142     const int partH = size.height();
143     int partY;
144
145     for (uint x = 0; x < max; x++) {
146         // Calculate the height of the curve at position x
147         partY = scaling*y[x];
148
149         // Invert the y axis
150         if (partY > partH-1) { partY = partH-1; }
151         partY = partH-1 - partY;
152
153         for (int k = partH-1; k >= partY; k--) {
154             component.setPixel(x, k, color.rgba());
155         }
156     }
157     if (unscaled && size.width() >= component.width()) {
158         return component;
159     } else {
160         return component.scaled(size, Qt::IgnoreAspectRatio, Qt::FastTransformation);
161     }
162 }
163
164 void HistogramGenerator::drawComponentFull(QPainter *davinci, const int *y, const float &scaling, const QRect &rect,
165                                         const QColor &color, const int &textSpace, const bool &unscaled, const uint &max) const
166 {
167     QImage component = drawComponent(y, rect.size() - QSize(0, textSpace), scaling, color, unscaled, max);
168     davinci->drawImage(rect.topLeft(), component);
169
170     int min = 0;
171     for (uint x = 0; x < max; x++) {
172         min = x;
173         if (y[x] > 0) {
174             break;
175         }
176     }
177     int maxVal = max-1;
178     for (int x = max-1; x >= 0; x--) {
179         maxVal = x;
180         if (y[x] > 0) {
181             break;
182         }
183     }
184
185     const int textY = rect.bottom()-textSpace+15;
186     const int dist = 40;
187     const int cw = component.width();
188
189     davinci->drawText(0,            textY, "min");
190     davinci->drawText(dist,         textY, QString::number(min, 'f', 0));
191
192     davinci->drawText(cw-dist-30,   textY, "max");
193     davinci->drawText(cw-30,        textY, QString::number(maxVal, 'f', 0));
194 }