]> git.sesse.net Git - kdenlive/blob - src/colorcorrection/rgbparadegenerator.cpp
aee6fa65f60e95bc9e3c5a6154f9111069367b21
[kdenlive] / src / colorcorrection / rgbparadegenerator.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 "rgbparadegenerator.h"
12
13 #include <QColor>
14 #include <QPainter>
15
16 #define CHOP255(a) ((255) < (a) ? (255) : (a))
17 #define CHOP1255(a) ((a) < (1) ? (1) : ((a) > (255) ? (255) : (a)))
18
19 const QColor RGBParadeGenerator::colHighlight(255, 245, 235, 255);
20 const QColor RGBParadeGenerator::colLight(200, 200, 200, 255);
21 const QColor RGBParadeGenerator::colSoft(150, 150, 150, 255);
22
23
24 const uchar RGBParadeGenerator::distRight(40);
25 const uchar RGBParadeGenerator::distBottom(40);
26
27 struct StructRGB {
28     uint r;
29     uint g;
30     uint b;
31 };
32
33 RGBParadeGenerator::RGBParadeGenerator()
34 {
35 }
36
37 QImage RGBParadeGenerator::calculateRGBParade(const QSize &paradeSize, const QImage &image,
38                                               const RGBParadeGenerator::PaintMode paintMode, bool drawAxis,
39                                               bool drawGradientRef, uint accelFactor)
40 {
41     Q_ASSERT(accelFactor >= 1);
42
43     if (paradeSize.width() <= 0 || paradeSize.height() <= 0 || image.width() <= 0 || image.height() <= 0) {
44         return QImage();
45
46     } else {
47         QImage parade(paradeSize, QImage::Format_ARGB32);
48         parade.fill(Qt::transparent);
49
50         QRgb *col;
51         QPainter davinci(&parade);
52
53         double dx, dy;
54
55         const uint ww = paradeSize.width();
56         const uint wh = paradeSize.height();
57         const uint iw = image.bytesPerLine();
58         const uint ih = image.height();
59         const uint byteCount = iw*ih;   // Note that 1 px = 4 B
60
61         const uchar offset = 10;
62         const uint partW = (ww - 2*offset - distRight) / 3;
63         const uint partH = wh - distBottom;
64
65         // Statistics
66         uchar minR = 255, minG = 255, minB = 255, maxR = 0, maxG = 0, maxB = 0, r, g, b;
67
68
69         // Number of input pixels that will fall on one scope pixel.
70         // Must be a float because the acceleration factor can be high, leading to <1 expected px per px.
71         const float pixelDepth = (float)((byteCount>>2) / accelFactor)/(partW*255);
72         const float gain = 255/(8*pixelDepth);
73 //        qDebug() << "Pixel depth: expected " << pixelDepth << "; Gain: using " << gain << " (acceleration: " << accelFactor << "x)";
74
75         QImage unscaled(ww-distRight, 256, QImage::Format_ARGB32);
76         unscaled.fill(qRgba(0, 0, 0, 0));
77
78         const float wPrediv = (float)(partW-1)/(iw-1);
79
80         StructRGB paradeVals[partW][256];
81         for (uint i = 0; i < partW; ++i) {
82             for (uint j = 0; j < 256; j++) {
83                 paradeVals[i][j].r = 0;
84                 paradeVals[i][j].g = 0;
85                 paradeVals[i][j].b = 0;
86             }
87         }
88
89         const uchar *bits = image.bits();
90         const uint stepsize = image.depth() / 8 *accelFactor;
91
92         for (uint i = 0, x = 0; i < byteCount; i += stepsize) {
93             col = (QRgb *)bits;
94             r = qRed(*col);
95             g = qGreen(*col);
96             b = qBlue(*col);
97
98             dx = x*wPrediv;
99
100             paradeVals[(int)dx][r].r++;
101             paradeVals[(int)dx][g].g++;
102             paradeVals[(int)dx][b].b++;
103
104
105             if (r < minR) { minR = r; }
106             if (g < minG) { minG = g; }
107             if (b < minB) { minB = b; }
108             if (r > maxR) { maxR = r; }
109             if (g > maxG) { maxG = g; }
110             if (b > maxB) { maxB = b; }
111
112             bits += stepsize;
113             x += stepsize;
114             x %= iw; // Modulo image width, to represent the current x position in the image
115         }
116
117
118         const uint offset1 = partW + offset;
119         const uint offset2 = 2*partW + 2*offset;
120         switch(paintMode) {
121         case PaintMode_RGB:
122             for (uint i = 0; i < partW; ++i) {
123                 for (uint j = 0; j < 256; j++) {
124                     unscaled.setPixel(i,         j, qRgba(255,10,10, CHOP255(gain*paradeVals[i][j].r)));
125                     unscaled.setPixel(i+offset1, j, qRgba(10,255,10, CHOP255(gain*paradeVals[i][j].g)));
126                     unscaled.setPixel(i+offset2, j, qRgba(10,10,255, CHOP255(gain*paradeVals[i][j].b)));
127                 }
128             }
129             break;
130         default:
131             for (uint i = 0; i < partW; ++i) {
132                 for (uint j = 0; j < 256; j++) {
133                     unscaled.setPixel(i,         j, qRgba(255,255,255, CHOP255(gain*paradeVals[i][j].r)));
134                     unscaled.setPixel(i+offset1, j, qRgba(255,255,255, CHOP255(gain*paradeVals[i][j].g)));
135                     unscaled.setPixel(i+offset2, j, qRgba(255,255,255, CHOP255(gain*paradeVals[i][j].b)));
136                 }
137             }
138             break;
139         }
140
141         // Scale the image to the target height. Scaling is not accomplished before because
142         // there are only 255 different values which would lead to gaps if the height is not exactly 255.
143         // Don't use bilinear transformation because the fast transformation meets the goal better.
144         davinci.drawImage(0, 0, unscaled.mirrored(false, true).scaled(unscaled.width(), partH, Qt::IgnoreAspectRatio, Qt::FastTransformation));
145
146         if (drawAxis) {
147             QRgb opx;
148             for (uint i = 0; i <= 10; ++i) {
149                 dy = (float)i/10 * (partH-1);
150                 for (uint x = 0; x < ww-distRight; x++) {
151                     opx = parade.pixel(x, dy);
152                     parade.setPixel(x,dy, qRgba(CHOP255(150+qRed(opx)), 255,
153                                               CHOP255(200+qBlue(opx)), CHOP255(32+qAlpha(opx))));
154                 }
155             }
156         }
157         
158         if (drawGradientRef) {
159             davinci.setPen(colLight);
160             davinci.drawLine(0                 ,partH,   partW,           0);
161             davinci.drawLine(  partW +   offset,partH, 2*partW +   offset,0);
162             davinci.drawLine(2*partW + 2*offset,partH, 3*partW + 2*offset,0);
163         }
164
165
166         const int d = 50;
167
168         // Show numerical minimum
169         if (minR == 0) { davinci.setPen(colHighlight); } else { davinci.setPen(colSoft); }
170         davinci.drawText(0,                     wh, "min: ");
171         if (minG == 0) { davinci.setPen(colHighlight); } else { davinci.setPen(colSoft); }
172         davinci.drawText(partW + offset,        wh, "min: ");
173         if (minB == 0) { davinci.setPen(colHighlight); } else { davinci.setPen(colSoft); }
174         davinci.drawText(2*partW + 2*offset,    wh, "min: ");
175
176         // Show numerical maximum
177         if (maxR == 255) { davinci.setPen(colHighlight); } else { davinci.setPen(colSoft); }
178         davinci.drawText(0,                     wh-20, "max: ");
179         if (maxG == 255) { davinci.setPen(colHighlight); } else { davinci.setPen(colSoft); }
180         davinci.drawText(partW + offset,        wh-20, "max: ");
181         if (maxB == 255) { davinci.setPen(colHighlight); } else { davinci.setPen(colSoft); }
182         davinci.drawText(2*partW + 2*offset,    wh-20, "max: ");
183
184         davinci.setPen(colLight);
185         davinci.drawText(d,                        wh, QString::number(minR, 'f', 0));
186         davinci.drawText(partW + offset + d,       wh, QString::number(minG, 'f', 0));
187         davinci.drawText(2*partW + 2*offset + d,   wh, QString::number(minB, 'f', 0));
188
189         davinci.drawText(d,                        wh-20, QString::number(maxR, 'f', 0));
190         davinci.drawText(partW + offset + d,       wh-20, QString::number(maxG, 'f', 0));
191         davinci.drawText(2*partW + 2*offset + d,   wh-20, QString::number(maxB, 'f', 0));
192
193
194         return parade;
195     }
196 }
197
198 #undef CHOP255