]> git.sesse.net Git - kdenlive/blob - src/colorcorrection/histogramgenerator.cpp
Histogram:
[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                                         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
34     int r[256], g[256], b[256], y[256];
35     // Initialize the values to zero
36     std::fill(r, r+255, 0);
37     std::fill(g, g+255, 0);
38     std::fill(b, b+255, 0);
39     std::fill(y, y+255, 0);
40
41     const uint iw = image.bytesPerLine();
42     const uint ih = image.height();
43     const uint ww = paradeSize.width();
44     const uint wh = paradeSize.height();
45     const uint byteCount = iw*ih;
46     const uint stepsize = 4*accelFactor;
47
48     const uchar *bits = image.bits();
49     QRgb *col;
50
51
52     // Read the stats from the input image
53     for (uint i = 0; i < byteCount; i += stepsize) {
54         col = (QRgb *)bits;
55
56         r[qRed(*col)]++;
57         g[qGreen(*col)]++;
58         b[qBlue(*col)]++;
59         y[(int)floor(.299*qRed(*col) + .587*qGreen(*col) + .114*qBlue(*col))]++;
60
61         bits += stepsize;
62     }
63
64
65     const int nParts = (drawY ? 1 : 0) + (drawR ? 1 : 0) + (drawG ? 1 : 0) + (drawB ? 1 : 0);
66     if (nParts == 0) {
67         // Nothing to draw
68         return QImage();
69     }
70
71     const int d = 20; // Distance for text
72     const int partH = (wh-nParts*d)/nParts;
73     const float scaling = (float)partH/(byteCount >> 7);
74     const int dist = 40;
75
76     int wy = 0; // Drawing position
77
78     QImage histogram(paradeSize, QImage::Format_ARGB32);
79     QPainter davinci(&histogram);
80     davinci.setPen(QColor(220, 220, 220, 255));
81     histogram.fill(qRgba(0, 0, 0, 0));
82
83     if (drawY) {
84 //        qDebug() << "Drawing Y at " << wy << " with height " << partH;
85         drawComponentFull(&davinci, y, scaling, QRect(0, wy, ww, partH + dist), QColor(220, 220, 210, 255), dist, unscaled);
86
87         wy += partH + d;
88     }
89
90     if (drawR) {
91 //        qDebug() << "Drawing R at " << wy << " with height " << partH;
92         drawComponentFull(&davinci, r, scaling, QRect(0, wy, ww, partH + dist), QColor(255, 128, 0, 255), dist, unscaled);
93
94         wy += partH + d;
95     }
96
97     if (drawG) {
98 //        qDebug() << "Drawing G at " << wy << " with height " << partH;
99         drawComponentFull(&davinci, g, scaling, QRect(0, wy, ww, partH + dist), QColor(128, 255, 0, 255), dist, unscaled);
100         wy += partH + d;
101     }
102
103     if (drawB) {
104 //        qDebug() << "Drawing B at " << wy << " with height " << partH;
105         drawComponentFull(&davinci, b, scaling, QRect(0, wy, ww, partH + dist), QColor(0, 128, 255, 255), dist, unscaled);
106
107         wy += partH + d;
108     }
109
110     return histogram;
111 }
112
113 QImage HistogramGenerator::drawComponent(const int *y, const QSize &size, const float &scaling, const QColor &color, const bool &unscaled) const
114 {
115     QImage component(256, size.height(), QImage::Format_ARGB32);
116     component.fill(qRgba(0, 0, 0, 0));
117     Q_ASSERT(scaling != INFINITY);
118
119     const int partH = size.height();
120     int partY;
121
122     for (int x = 0; x < 256; x++) {
123         // Calculate the height of the curve at position x
124         partY = scaling*y[x];
125
126         // Invert the y axis
127         if (partY > partH-1) { partY = partH-1; }
128         partY = partH-1 - partY;
129
130         for (int k = partH-1; k >= partY; k--) {
131             component.setPixel(x, k, color.rgba());
132         }
133     }
134     if (unscaled && size.width() >= component.width()) {
135         return component;
136     } else {
137         return component.scaled(size, Qt::IgnoreAspectRatio, Qt::FastTransformation);
138     }
139 }
140
141 void HistogramGenerator::drawComponentFull(QPainter *davinci, const int *y, const float &scaling, const QRect &rect,
142                                         const QColor &color, const int &textSpace, const bool &unscaled) const
143 {
144     QImage component = drawComponent(y, rect.size() - QSize(0, textSpace), scaling, color, unscaled);
145     davinci->drawImage(rect.topLeft(), component);
146
147     int min = 0;
148     for (int x = 0; x < 256; x++) {
149         min = x;
150         if (y[x] > 0) {
151             break;
152         }
153     }
154     int max = 255;
155     for (int x = 255; x >= 0; x--) {
156         max = x;
157         if (y[x] > 0) {
158             break;
159         }
160     }
161
162     const int textY = rect.bottom()-textSpace+15;
163     const int dist = 40;
164     const int cw = component.width();
165
166     davinci->drawText(0,            textY, "min");
167     davinci->drawText(dist,         textY, QString::number(min, 'f', 0));
168
169     davinci->drawText(cw-dist-30,   textY, "max");
170     davinci->drawText(cw-30,        textY, QString::number(max, 'f', 0));
171 }