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 ***************************************************************************/
16 #include "levelsgenerator.h"
18 LevelsGenerator::LevelsGenerator()
22 QImage LevelsGenerator::calculateLevels(const QSize ¶deSize, const QImage &image, const int &components, const uint &accelFactor) const
24 qDebug() << "Levels rect size is: " << paradeSize.width() << "/" << paradeSize.height();
25 if (paradeSize.height() <= 0 || paradeSize.width() <= 0) {
29 bool drawY = (components & LevelsGenerator::ComponentY) != 0;
30 bool drawR = (components & LevelsGenerator::ComponentR) != 0;
31 bool drawG = (components & LevelsGenerator::ComponentG) != 0;
32 bool drawB = (components & LevelsGenerator::ComponentB) != 0;
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);
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;
48 const uchar *bits = image.bits();
52 // Read the stats from the input image
53 for (uint i = 0; i < byteCount; i += stepsize) {
59 y[(int)floor(.299*qRed(*col) + .587*qGreen(*col) + .114*qBlue(*col))]++;
65 const int nParts = (drawY ? 1 : 0) + (drawR ? 1 : 0) + (drawG ? 1 : 0) + (drawB ? 1 : 0);
71 const int d = 20; // Distance for text
72 const int partH = (wh-4*d)/nParts;
73 const float scaling = (float)partH/(byteCount >> 7);
75 int wy = 0; // Drawing position
76 int partY; // Vertical position for the dots
78 QImage levels(paradeSize, QImage::Format_ARGB32);
79 QImage component(256, partH, QImage::Format_ARGB32);
80 QPainter davinci(&levels);
81 levels.fill(qRgba(0, 0, 0, 0));
84 qDebug() << "Drawing Y at " << wy << " with height " << partH;
85 component.fill(qRgba(0, 0, 0, 0));
87 for (int x = 0; x < 256; x++) {
88 // Calculate the height of the curve at position x
92 if (partY > partH-1) { partY = partH-1; }
93 partY = partH-1 - partY;
95 for (int k = partH-1; k >= partY; k--) {
96 component.setPixel(x, k, qRgba(220, 220, 210, 255));
100 davinci.drawImage(0, wy, component.scaled(ww, component.height(), Qt::IgnoreAspectRatio, Qt::FastTransformation));
106 qDebug() << "Drawing R at " << wy << " with height " << partH;
107 component.fill(qRgba(0, 0, 0, 0));
109 for (int x = 0; x < 256; x++) {
110 // Calculate the height of the curve at position x
111 partY = scaling*r[x];
114 if (partY > partH-1) { partY = partH-1; }
115 partY = partH-1 - partY;
117 for (int k = partH-1; k >= partY; k--) {
118 component.setPixel(x, k, qRgba(255, 128, 0, 255));
122 davinci.drawImage(0, wy, component.scaled(ww, component.height(), Qt::IgnoreAspectRatio, Qt::FastTransformation));
128 qDebug() << "Drawing G at " << wy << " with height " << partH;
129 component.fill(qRgba(0, 0, 0, 0));
131 for (int x = 0; x < 256; x++) {
132 // Calculate the height of the curve at position x
133 partY = scaling*g[x];
136 if (partY > partH-1) { partY = partH-1; }
137 partY = partH-1 - partY;
139 for (int k = partH-1; k >= partY; k--) {
140 component.setPixel(x, k, qRgba(128, 255, 0, 255));
144 davinci.drawImage(0, wy, component.scaled(ww, component.height(), Qt::IgnoreAspectRatio, Qt::FastTransformation));
150 qDebug() << "Drawing B at " << wy << " with height " << partH;
151 component.fill(qRgba(0, 0, 0, 0));
153 for (int x = 0; x < 256; x++) {
154 // Calculate the height of the curve at position x
155 partY = scaling*b[x];
158 if (partY > partH-1) { partY = partH-1; }
159 partY = partH-1 - partY;
161 for (int k = partH-1; k >= partY; k--) {
162 component.setPixel(x, k, qRgba(0, 128, 255, 255));
166 davinci.drawImage(0, wy, component.scaled(ww, component.height(), Qt::IgnoreAspectRatio, Qt::FastTransformation));