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 ***************************************************************************/
18 #include "waveformgenerator.h"
20 #define CHOP255(a) ((255) < (a) ? (255) : (a))
22 WaveformGenerator::WaveformGenerator()
26 WaveformGenerator::~WaveformGenerator()
30 QImage WaveformGenerator::calculateWaveform(const QSize &waveformSize, const QImage &image, const bool &drawAxis, const uint &accelFactor)
32 Q_ASSERT(accelFactor >= 1);
37 QImage wave(waveformSize, QImage::Format_ARGB32);
39 if (waveformSize.width() <= 0 || waveformSize.height() <= 0) {
44 qDebug() << "Waveform calculation started.";
46 // Fill with transparent color
47 wave.fill(qRgba(0,0,0,0));
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;
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);
66 const uchar *bits = image.bits();
67 const uint stepsize = 4*accelFactor;
69 for (uint i = 0, x = 0; i < byteCount; i += stepsize) {
74 // dY is on [0,255] now.
75 dY = .299*qRed(*col) + .587*qGreen(*col) + .114*qBlue(*col);
79 wavePoint = QPoint((int)dx, (int)(wh-1 - dy));
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));
87 x %= iw; // Modulo image width, to represent the current x position in the image
91 QPainter davinci(&wave);
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))));
102 //davinci.drawLine(0, dy, ww-1, dy);
108 uint diff = time.elapsed();
109 // qDebug() << "Waveform calculation ended. Time taken: " << diff << " ms. Sending signal now.";
110 emit signalCalculationFinished(wave, diff);