]> git.sesse.net Git - kdenlive/blob - src/colorcorrection/waveformgenerator.cpp
a1d2162f8dcf38fd985fdf6186222a16e660a261
[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 <cmath>
12
13 #include <QColor>
14 #include <QImage>
15 #include <QPainter>
16 #include <QSize>
17 #include <QTime>
18
19 #include "waveformgenerator.h"
20
21 #define CHOP255(a) ((255) < (a) ? (255) : (a))
22
23 WaveformGenerator::WaveformGenerator()
24 {
25 }
26
27 WaveformGenerator::~WaveformGenerator()
28 {
29 }
30
31 QImage WaveformGenerator::calculateWaveform(const QSize &waveformSize, const QImage &image, WaveformGenerator::PaintMode paintMode,
32                                             const bool &drawAxis, WaveformGenerator::Rec rec, const uint &accelFactor)
33 {
34     Q_ASSERT(accelFactor >= 1);
35
36     //QTime time;
37     //time.start();
38
39     QImage wave(waveformSize, QImage::Format_ARGB32);
40
41     if (waveformSize.width() <= 0 || waveformSize.height() <= 0 || image.width() <= 0 || image.height() <= 0) {
42         return QImage();
43
44     } else {
45
46         // Fill with transparent color
47         wave.fill(qRgba(0,0,0,0));
48
49         QRgb *col;
50
51         double dY, dx, dy;
52
53         const uint ww = waveformSize.width();
54         const uint wh = waveformSize.height();
55         const uint iw = image.bytesPerLine();
56         const uint ih = image.height();
57         const uint byteCount = iw*ih;
58
59         uint waveValues[waveformSize.width()][waveformSize.height()];
60         for (int i = 0; i < waveformSize.width(); i++) {
61             for (int j = 0; j < waveformSize.height(); j++) {
62                 waveValues[i][j] = 0;
63             }
64         }
65
66         // Number of input pixels that will fall on one scope pixel.
67         // Must be a float because the acceleration factor can be high, leading to <1 expected px per px.
68         const float pixelDepth = (float)((byteCount>>2) / accelFactor)/(ww*wh);
69         const float gain = 255/(8*pixelDepth);
70         //qDebug() << "Pixel depth: expected " << pixelDepth << "; Gain: using " << gain << " (acceleration: " << accelFactor << "x)";
71
72
73         // Subtract 1 from sizes because we start counting from 0.
74         // Not doing it would result in attempts to paint outside of the image.
75         const float hPrediv = (float)(wh-1)/255;
76         const float wPrediv = (float)(ww-1)/(iw-1);
77
78         const uchar *bits = image.bits();
79         const int bpp = image.depth() / 8;
80
81         for (uint i = 0, x = 0; i < byteCount; i += bpp) {
82
83             Q_ASSERT(bits < image.bits() + byteCount);
84
85             col = (QRgb *)bits;
86
87             if (rec == WaveformGenerator::Rec_601) {
88                 // CIE 601 Luminance
89                 dY = .299*qRed(*col) + .587*qGreen(*col) + .114*qBlue(*col);
90             } else {
91                 // CIE 709 Luminance
92                 dY = .2125*qRed(*col) + .7154*qGreen(*col) + .0721*qBlue(*col);
93             }
94             // dY is on [0,255] now.
95
96             dy = dY*hPrediv;
97             dx = x*wPrediv;
98             waveValues[(int)dx][(int)dy]++;
99
100             bits += bpp;
101             x += bpp;
102             if (x > iw) {
103                 x -= iw;
104                 if (accelFactor > 1) {
105                     bits += bpp*iw*(accelFactor-1);
106                     i += bpp*iw*(accelFactor-1);
107                 }
108             }
109         }
110
111         switch (paintMode) {
112         case PaintMode_Green:
113             for (int i = 0; i < waveformSize.width(); i++) {
114                 for (int j = 0; j < waveformSize.height(); j++) {
115                     // Logarithmic scale. Needs fine tuning by hand, but looks great.
116                     wave.setPixel(i, waveformSize.height()-j-1, qRgba(CHOP255(52*log(0.1*gain*waveValues[i][j])),
117                                                                       CHOP255(52*log(gain*waveValues[i][j])),
118                                                                       CHOP255(52*log(.25*gain*waveValues[i][j])),
119                                                                       CHOP255(64*log(gain*waveValues[i][j]))));
120                 }
121             }
122             break;
123         case PaintMode_Yellow:
124             for (int i = 0; i < waveformSize.width(); i++) {
125                 for (int j = 0; j < waveformSize.height(); j++) {
126                     wave.setPixel(i, waveformSize.height()-j-1, qRgba(255,242,0,   CHOP255(gain*waveValues[i][j])));
127                 }
128             }
129             break;
130         default:
131             for (int i = 0; i < waveformSize.width(); i++) {
132                 for (int j = 0; j < waveformSize.height(); j++) {
133                     wave.setPixel(i, waveformSize.height()-j-1, qRgba(255,255,255, CHOP255(2*gain*waveValues[i][j])));
134                 }
135             }
136             break;
137         }
138
139         if (drawAxis) {
140             QPainter davinci(&wave);
141             QRgb opx;
142             davinci.setPen(qRgba(150,255,200,32));
143             davinci.setCompositionMode(QPainter::CompositionMode_Overlay);
144             for (uint i = 0; i <= 10; i++) {
145                 dy = (float)i/10 * (wh-1);
146                 for (uint x = 0; x < ww; x++) {
147                     opx = wave.pixel(x, dy);
148                     wave.setPixel(x,dy, qRgba(CHOP255(150+qRed(opx)), 255,
149                                               CHOP255(200+qBlue(opx)), CHOP255(32+qAlpha(opx))));
150                 }
151             }
152         }
153
154     }
155
156     //uint diff = time.elapsed();
157     //emit signalCalculationFinished(wave, diff);
158
159     return wave;
160 }
161 #undef CHOP255