]> git.sesse.net Git - kdenlive/blob - src/colorcorrection/waveformgenerator.cpp
RGB Parade added.
[kdenlive] / src / colorcorrection / waveformgenerator.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 <QColor>
12 #include <QDebug>
13 #include <QImage>
14 #include <QPainter>
15 #include <QSize>
16 #include <QTime>
17
18 #include "waveformgenerator.h"
19
20 #define CHOP255(a) ((255) < (a) ? (255) : (a))
21
22 WaveformGenerator::WaveformGenerator()
23 {
24 }
25
26 WaveformGenerator::~WaveformGenerator()
27 {
28 }
29
30 QImage WaveformGenerator::calculateWaveform(const QSize &waveformSize, const QImage &image, const bool &drawAxis, const uint &accelFactor)
31 {
32     Q_ASSERT(accelFactor >= 1);
33
34     QTime time;
35     time.start();
36
37     QImage wave(waveformSize, QImage::Format_ARGB32);
38
39     if (waveformSize.width() <= 0 || waveformSize.height() <= 0) {
40         qCritical("Waveform size should not be 0.");
41
42     } else {
43
44         qDebug() << "Waveform calculation started.";
45
46         // Fill with transparent color
47         wave.fill(qRgba(0,0,0,0));
48
49         QRgb *col;
50         QRgb waveCol;
51         QPoint wavePoint;
52
53         double dY, dx, dy;
54
55         const uint ww = waveformSize.width();
56         const uint wh = waveformSize.height();
57         const uint iw = image.bytesPerLine();
58         const uint ih = image.height();
59         const uint byteCount = iw*ih;
60
61         // Subtract 1 from sizes because we start counting from 0.
62         // Not doing it would result in attempts to paint outside of the image.
63         const float hPrediv = (float)(wh-1)/255;
64         const float wPrediv = (float)(ww-1)/(iw-1);
65
66         const uchar *bits = image.bits();
67         const uint stepsize = 4*accelFactor;
68
69         for (uint i = 0, x = 0; i < byteCount; i += stepsize) {
70
71             col = (QRgb *)bits;
72
73             // CIE 601 Luminance
74             // dY is on [0,255] now.
75             dY = .299*qRed(*col) + .587*qGreen(*col) + .114*qBlue(*col);
76
77             dy = dY*hPrediv;
78             dx = x*wPrediv;
79             wavePoint = QPoint((int)dx, (int)(wh-1 - dy));
80
81             waveCol = QRgb(wave.pixel(wavePoint));
82             wave.setPixel(wavePoint, qRgba(CHOP255(9 + qRed(waveCol)), CHOP255(36 + qGreen(waveCol)),
83                                            CHOP255(18 + qBlue(waveCol)), 255));
84
85             bits += stepsize;
86             x += stepsize;
87             x %= iw; // Modulo image width, to represent the current x position in the image
88         }
89
90         if (drawAxis) {
91             QPainter davinci(&wave);
92             QRgb opx;
93             davinci.setPen(qRgba(150,255,200,32));
94             davinci.setCompositionMode(QPainter::CompositionMode_Overlay);
95             for (uint i = 0; i <= 10; i++) {
96                 dy = (float)i/10 * (wh-1);
97                 for (uint x = 0; x < ww; x++) {
98                     opx = wave.pixel(x, dy);
99                     wave.setPixel(x,dy, qRgba(CHOP255(150+qRed(opx)), 255,
100                                               CHOP255(200+qBlue(opx)), CHOP255(32+qAlpha(opx))));
101                 }
102                 //davinci.drawLine(0, dy, ww-1, dy);
103             }
104         }
105
106     }
107
108     uint diff = time.elapsed();
109 //    qDebug() << "Waveform calculation ended. Time taken: " << diff << " ms. Sending signal now.";
110     emit signalCalculationFinished(wave, diff);
111
112     return wave;
113 }
114 #undef CHOP255