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 ***************************************************************************/
17 #include "waveformgenerator.h"
19 #define CHOP255(a) ((255) < (a) ? (255) : (a))
21 WaveformGenerator::WaveformGenerator()
25 WaveformGenerator::~WaveformGenerator()
29 QImage WaveformGenerator::calculateWaveform(const QSize &waveformSize, const QImage &image, WaveformGenerator::PaintMode paintMode,
30 const bool &drawAxis, WaveformGenerator::Rec rec, const uint &accelFactor)
32 Q_ASSERT(accelFactor >= 1);
37 QImage wave(waveformSize, QImage::Format_ARGB32);
39 if (waveformSize.width() <= 0 || waveformSize.height() <= 0 || image.width() <= 0 || image.height() <= 0) {
44 // Fill with transparent color
45 wave.fill(qRgba(0,0,0,0));
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;
59 // Subtract 1 from sizes because we start counting from 0.
60 // Not doing it would result in attempts to paint outside of the image.
61 const float hPrediv = (float)(wh-1)/255;
62 const float wPrediv = (float)(ww-1)/(iw-1);
64 const float brightnessAdjustment = accelFactor * ((float) ww*wh/(byteCount>>3));
66 const uchar *bits = image.bits();
67 const uint stepsize = 4*accelFactor;
69 for (uint i = 0, x = 0; i < byteCount; i += stepsize) {
73 if (rec == WaveformGenerator::Rec_601) {
75 dY = .299*qRed(*col) + .587*qGreen(*col) + .114*qBlue(*col);
78 dY = .2125*qRed(*col) + .7154*qGreen(*col) + .0721*qBlue(*col);
80 // dY is on [0,255] now.
84 wavePoint = QPoint((int)dx, (int)(wh-1 - dy));
86 waveCol = QRgb(wave.pixel(wavePoint));
89 wave.setPixel(wavePoint, qRgba(CHOP255(9 + qRed(waveCol)), CHOP255(36 + qGreen(waveCol)),
90 CHOP255(18 + qBlue(waveCol)), 255));
92 case PaintMode_Yellow:
93 wave.setPixel(wavePoint, qRgba(255, 242,
94 0, CHOP255(brightnessAdjustment*10+qAlpha(waveCol))));
97 wave.setPixel(wavePoint, qRgba(255,255,255,
98 CHOP255(brightnessAdjustment*32+qAlpha(waveCol))));
104 x %= iw; // Modulo image width, to represent the current x position in the image
108 QPainter davinci(&wave);
110 davinci.setPen(qRgba(150,255,200,32));
111 davinci.setCompositionMode(QPainter::CompositionMode_Overlay);
112 for (uint i = 0; i <= 10; i++) {
113 dy = (float)i/10 * (wh-1);
114 for (uint x = 0; x < ww; x++) {
115 opx = wave.pixel(x, dy);
116 wave.setPixel(x,dy, qRgba(CHOP255(150+qRed(opx)), 255,
117 CHOP255(200+qBlue(opx)), CHOP255(32+qAlpha(opx))));
124 uint diff = time.elapsed();
125 emit signalCalculationFinished(wave, diff);