]> git.sesse.net Git - kdenlive/blobdiff - src/colorcorrection/waveformgenerator.cpp
Waveform changes:
[kdenlive] / src / colorcorrection / waveformgenerator.cpp
index 633a2211135326eb99ea398a0acfeb234f0ebcf2..05d278d1d571c934ef4b96ead4a7c6375049c710 100644 (file)
@@ -8,6 +8,8 @@
  *   (at your option) any later version.                                   *
  ***************************************************************************/
 
+#include <cmath>
+
 #include <QColor>
 #include <QDebug>
 #include <QImage>
@@ -27,7 +29,8 @@ WaveformGenerator::~WaveformGenerator()
 {
 }
 
-QImage WaveformGenerator::calculateWaveform(const QSize &waveformSize, const QImage &image, const bool &drawAxis, const uint &accelFactor)
+QImage WaveformGenerator::calculateWaveform(const QSize &waveformSize, const QImage &image, WaveformGenerator::PaintMode paintMode,
+                                            const bool &drawAxis, WaveformGenerator::Rec rec, const uint &accelFactor)
 {
     Q_ASSERT(accelFactor >= 1);
 
@@ -41,14 +44,10 @@ QImage WaveformGenerator::calculateWaveform(const QSize &waveformSize, const QIm
 
     } else {
 
-        qDebug() << "Waveform calculation started.";
-
         // Fill with transparent color
         wave.fill(qRgba(0,0,0,0));
 
         QRgb *col;
-        QRgb waveCol;
-        QPoint wavePoint;
 
         double dY, dx, dy;
 
@@ -58,6 +57,20 @@ QImage WaveformGenerator::calculateWaveform(const QSize &waveformSize, const QIm
         const uint ih = image.height();
         const uint byteCount = iw*ih;
 
+        uint waveValues[waveformSize.width()][waveformSize.height()];
+        for (int i = 0; i < waveformSize.width(); i++) {
+            for (int j = 0; j < waveformSize.height(); j++) {
+                waveValues[i][j] = 0;
+            }
+        }
+
+        // Number of input pixels that will fall on one scope pixel.
+        // Must be a float because the acceleration factor can be high, leading to <1 expected px per px.
+        const float pixelDepth = (float)((byteCount>>2) / accelFactor)/(ww*wh);
+        const float gain = 255/(8*pixelDepth);
+        qDebug() << "Pixel depth: expected " << pixelDepth << "; Gain: using " << gain << " (acceleration: " << accelFactor << "x)";
+
+
         // Subtract 1 from sizes because we start counting from 0.
         // Not doing it would result in attempts to paint outside of the image.
         const float hPrediv = (float)(wh-1)/255;
@@ -70,23 +83,52 @@ QImage WaveformGenerator::calculateWaveform(const QSize &waveformSize, const QIm
 
             col = (QRgb *)bits;
 
-            // CIE 601 Luminance
+            if (rec == WaveformGenerator::Rec_601) {
+                // CIE 601 Luminance
+                dY = .299*qRed(*col) + .587*qGreen(*col) + .114*qBlue(*col);
+            } else {
+                // CIE 709 Luminance
+                dY = .2125*qRed(*col) + .7154*qGreen(*col) + .0721*qBlue(*col);
+            }
             // dY is on [0,255] now.
-            dY = .299*qRed(*col) + .587*qGreen(*col) + .114*qBlue(*col);
 
             dy = dY*hPrediv;
             dx = x*wPrediv;
-            wavePoint = QPoint((int)dx, (int)(wh-1 - dy));
-
-            waveCol = QRgb(wave.pixel(wavePoint));
-            wave.setPixel(wavePoint, qRgba(CHOP255(9 + qRed(waveCol)), CHOP255(36 + qGreen(waveCol)),
-                                           CHOP255(18 + qBlue(waveCol)), 255));
+            waveValues[(int)dx][(int)dy]++;
 
             bits += stepsize;
             x += stepsize;
             x %= iw; // Modulo image width, to represent the current x position in the image
         }
 
+        switch (paintMode) {
+        case PaintMode_Green:
+            for (int i = 0; i < waveformSize.width(); i++) {
+                for (int j = 0; j < waveformSize.height(); j++) {
+                    // Logarithmic scale. Needs fine tuning by hand, but looks great.
+                    wave.setPixel(i, waveformSize.height()-j-1, qRgba(CHOP255(52*log(0.1*gain*waveValues[i][j])),
+                                                                      CHOP255(52*log(gain*waveValues[i][j])),
+                                                                      CHOP255(52*log(.25*gain*waveValues[i][j])),
+                                                                      CHOP255(64*log(gain*waveValues[i][j]))));
+                }
+            }
+            break;
+        case PaintMode_Yellow:
+            for (int i = 0; i < waveformSize.width(); i++) {
+                for (int j = 0; j < waveformSize.height(); j++) {
+                    wave.setPixel(i, waveformSize.height()-j-1, qRgba(255,242,0,   CHOP255(gain*waveValues[i][j])));
+                }
+            }
+            break;
+        default:
+            for (int i = 0; i < waveformSize.width(); i++) {
+                for (int j = 0; j < waveformSize.height(); j++) {
+                    wave.setPixel(i, waveformSize.height()-j-1, qRgba(255,255,255, CHOP255(2*gain*waveValues[i][j])));
+                }
+            }
+            break;
+        }
+
         if (drawAxis) {
             QPainter davinci(&wave);
             QRgb opx;
@@ -99,14 +141,12 @@ QImage WaveformGenerator::calculateWaveform(const QSize &waveformSize, const QIm
                     wave.setPixel(x,dy, qRgba(CHOP255(150+qRed(opx)), 255,
                                               CHOP255(200+qBlue(opx)), CHOP255(32+qAlpha(opx))));
                 }
-                //davinci.drawLine(0, dy, ww-1, dy);
             }
         }
 
     }
 
     uint diff = time.elapsed();
-//    qDebug() << "Waveform calculation ended. Time taken: " << diff << " ms. Sending signal now.";
     emit signalCalculationFinished(wave, diff);
 
     return wave;