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