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