1 /***************************************************************************
2 * Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com) *
3 * This file is part of kdenlive. See www.kdenlive.org. *
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 ***************************************************************************/
11 #include "rgbparadegenerator.h"
19 #define CHOP255(a) ((255) < (a) ? (255) : (a))
21 const QColor RGBParadeGenerator::colHighlight(255, 245, 235, 255);
22 const QColor RGBParadeGenerator::colLight(200, 200, 200, 255);
23 const QColor RGBParadeGenerator::colSoft(150, 150, 150, 255);
25 RGBParadeGenerator::RGBParadeGenerator()
29 QImage RGBParadeGenerator::calculateRGBParade(const QSize ¶deSize, const QImage &image, const bool &drawAxis, const uint &accelFactor)
31 Q_ASSERT(accelFactor >= 1);
33 QImage parade(paradeSize, QImage::Format_ARGB32);
35 if (paradeSize.width() <= 0 || paradeSize.height() <= 0) {
36 qCritical("Wave size should not be 0.");
40 qDebug() << "Wave calculation started.";
42 // Fill with transparent color
43 parade.fill(qRgba(0,0,0,0));
48 QPainter davinci(¶de);
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;
58 const uchar offset = 10;
59 const uchar right = 40;
60 const uchar bottom = 40;
61 const int partW = (ww - 2*offset - right) / 3;
62 const int partH = wh - bottom;
64 // To get constant brightness, independant of acceleration factor and input image size
65 // Must be a float because the acceleration factor can be high, leading to <1 expected px per px.
66 const float avgPxPerPx = ((float)(image.width() * image.height()) / (500*partW*accelFactor));
67 const float weaken = (avgPxPerPx == 0) ? 1 : (float)4/avgPxPerPx;
68 const int vh = weaken*27;
69 const int vm = weaken*18;
70 const int vl = weaken*9;
72 uchar minR = 255, minG = 255, minB = 255, maxR = 0, maxG = 0, maxB = 0, r, g, b;
73 qDebug() << "Expecting about " << avgPxPerPx << " pixels per pixel in the RGB parade. Weakening by " << weaken
74 << " with an acceleration factor of " << accelFactor;
77 QImage unscaled(ww-right, 256, QImage::Format_ARGB32);
78 unscaled.fill(qRgba(0, 0, 0, 0));
80 const float wPrediv = (float)(partW-1)/(iw-1);
82 const uchar *bits = image.bits();
83 const uint stepsize = 4*accelFactor;
85 for (uint i = 0, x = 0; i < byteCount; i += stepsize) {
94 paradePoint = QPoint((int)dx, r);
95 paradeCol = QRgb(unscaled.pixel(paradePoint));
96 unscaled.setPixel(paradePoint, qRgba(CHOP255(vh + qRed(paradeCol)), CHOP255(vm + qGreen(paradeCol)),
97 CHOP255(vl + qBlue(paradeCol)), 255));
99 paradePoint = QPoint((int) (dx + partW + offset), g);
100 paradeCol = QRgb(unscaled.pixel(paradePoint));
101 unscaled.setPixel(paradePoint, qRgba(CHOP255(vl + qRed(paradeCol)), CHOP255(vh + qGreen(paradeCol)),
102 CHOP255(vm + qBlue(paradeCol)), 255));
104 paradePoint = QPoint((int) (dx + 2*partW + 2*offset), b);
105 paradeCol = QRgb(unscaled.pixel(paradePoint));
106 unscaled.setPixel(paradePoint, qRgba(CHOP255(vm + qRed(paradeCol)), CHOP255(vl + qGreen(paradeCol)),
107 CHOP255(vh + qBlue(paradeCol)), 255));
110 if (r < minR) { minR = r; }
111 if (g < minG) { minG = g; }
112 if (b < minB) { minB = b; }
113 if (r > maxR) { maxR = r; }
114 if (g > maxG) { maxG = g; }
115 if (b > maxB) { maxB = b; }
119 x %= iw; // Modulo image width, to represent the current x position in the image
121 // Scale the image to the target height. Scaling is not accomplished before because
122 // there are only 255 different values which would lead to gaps if the height is not exactly 255.
123 // Don't use bilinear transformation because the fast transformation meets the goal better.
124 davinci.drawImage(0, 0, unscaled.mirrored(false, true).scaled(unscaled.width(), partH, Qt::IgnoreAspectRatio, Qt::FastTransformation));
128 for (uint i = 0; i <= 10; i++) {
129 dy = (float)i/10 * (partH-1);
130 for (uint x = 0; x < ww-right; x++) {
131 opx = parade.pixel(x, dy);
132 parade.setPixel(x,dy, qRgba(CHOP255(150+qRed(opx)), 255,
133 CHOP255(200+qBlue(opx)), CHOP255(32+qAlpha(opx))));
141 // Show numerical minimum
142 if (minR == 0) { davinci.setPen(colHighlight); } else { davinci.setPen(colSoft); }
143 davinci.drawText(0, wh, "min: ");
144 if (minG == 0) { davinci.setPen(colHighlight); } else { davinci.setPen(colSoft); }
145 davinci.drawText(partW + offset, wh, "min: ");
146 if (minB == 0) { davinci.setPen(colHighlight); } else { davinci.setPen(colSoft); }
147 davinci.drawText(2*partW + 2*offset, wh, "min: ");
149 // Show numerical maximum
150 if (maxR == 255) { davinci.setPen(colHighlight); } else { davinci.setPen(colSoft); }
151 davinci.drawText(0, wh-20, "max: ");
152 if (maxG == 255) { davinci.setPen(colHighlight); } else { davinci.setPen(colSoft); }
153 davinci.drawText(partW + offset, wh-20, "max: ");
154 if (maxB == 255) { davinci.setPen(colHighlight); } else { davinci.setPen(colSoft); }
155 davinci.drawText(2*partW + 2*offset, wh-20, "max: ");
157 davinci.setPen(colLight);
158 davinci.drawText(d, wh, QString::number(minR, 'f', 0));
159 davinci.drawText(partW + offset + d, wh, QString::number(minG, 'f', 0));
160 davinci.drawText(2*partW + 2*offset + d, wh, QString::number(minB, 'f', 0));
162 davinci.drawText(d, wh-20, QString::number(maxR, 'f', 0));
163 davinci.drawText(partW + offset + d, wh-20, QString::number(maxG, 'f', 0));
164 davinci.drawText(2*partW + 2*offset + d, wh-20, QString::number(maxB, 'f', 0));
166 davinci.drawText(ww-right+5, 10, "255");
167 davinci.drawText(ww-right+5, partH+5, "0");