]> git.sesse.net Git - kdenlive/blob - src/vectorscope.cpp
* Scopes: Configuration is saved now.
[kdenlive] / src / vectorscope.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
12 #include <QAction>
13 #include <QColor>
14 #include <QDebug>
15 #include <QMouseEvent>
16 #include <QMenu>
17 #include <QPainter>
18
19 #include <QTime>
20
21 #include "colorplaneexport.h"
22 #include "colortools.h"
23 #include "renderer.h"
24 #include "vectorscope.h"
25 #include "vectorscopegenerator.h"
26
27 const float P75 = .75;
28 const unsigned char DEFAULT_Y = 255;
29
30 const QPointF YUV_R(-.147,  .615);
31 const QPointF YUV_G(-.289, -.515);
32 const QPointF YUV_B(.437, -.100);
33 const QPointF YUV_Cy(.147, -.615);
34 const QPointF YUV_Mg(.289,  .515);
35 const QPointF YUV_Yl(-.437,  .100);
36
37 const QPen penThick(QBrush(QColor(250,250,250)), 2, Qt::SolidLine);
38 const QPen penThin(QBrush(QColor(250,250,250)), 1, Qt::SolidLine);
39 const QPen penLight(QBrush(QColor(200,200,250,150)), 1, Qt::SolidLine);
40 const QPen penDark(QBrush(QColor(0,0,20,250)), 1, Qt::SolidLine);
41
42
43 Vectorscope::Vectorscope(Monitor *projMonitor, Monitor *clipMonitor, QWidget *parent) :
44     AbstractScopeWidget(projMonitor, clipMonitor, parent),
45     m_gain(1),
46     m_circleEnabled(false)
47 {
48     ui = new Ui::Vectorscope_UI();
49     ui->setupUi(this);
50
51     m_colorTools = new ColorTools();
52     m_colorPlaneExport = new ColorPlaneExport(this);
53     m_vectorscopeGenerator = new VectorscopeGenerator();
54
55     ui->paintMode->addItem(i18n("Green 2"), QVariant(VectorscopeGenerator::PaintMode_Green2));
56     ui->paintMode->addItem(i18n("Green"), QVariant(VectorscopeGenerator::PaintMode_Green));
57     ui->paintMode->addItem(i18n("Black"), QVariant(VectorscopeGenerator::PaintMode_Black));
58     ui->paintMode->addItem(i18n("Modified YUV (Chroma)"), QVariant(VectorscopeGenerator::PaintMode_Chroma));
59     ui->paintMode->addItem(i18n("YUV"), QVariant(VectorscopeGenerator::PaintMode_YUV));
60     ui->paintMode->addItem(i18n("Original Color"), QVariant(VectorscopeGenerator::PaintMode_Original));
61
62     ui->backgroundMode->addItem(i18n("None"), QVariant(BG_NONE));
63     ui->backgroundMode->addItem(i18n("YUV"), QVariant(BG_YUV));
64     ui->backgroundMode->addItem(i18n("Modified YUV (Chroma)"), QVariant(BG_CHROMA));
65
66     ui->sliderGain->setMinimum(0);
67     ui->sliderGain->setMaximum(40);
68
69     bool b = true;
70     b &= connect(ui->backgroundMode, SIGNAL(currentIndexChanged(int)), this, SLOT(slotBackgroundChanged()));
71     b &= connect(ui->sliderGain, SIGNAL(valueChanged(int)), this, SLOT(slotGainChanged(int)));
72     b &= connect(ui->paintMode, SIGNAL(currentIndexChanged(int)), this, SLOT(forceUpdateScope()));
73     ui->sliderGain->setValue(0);
74
75
76     ///// Build context menu /////
77
78     m_menu->addSeparator();
79
80     m_aExportBackground = new QAction(i18n("Export background"), this);
81     m_menu->addAction(m_aExportBackground);
82     b &= connect(m_aExportBackground, SIGNAL(triggered()), this, SLOT(slotExportBackground()));
83
84     m_a75PBox = new QAction(i18n("75% box"), this);
85     m_a75PBox->setCheckable(true);
86     m_a75PBox->setChecked(false);
87     m_menu->addAction(m_a75PBox);
88     b &= connect(m_a75PBox, SIGNAL(changed()), this, SLOT(forceUpdateBackground()));
89
90     m_aAxisEnabled = new QAction(i18n("Draw axis"), this);
91     m_aAxisEnabled->setCheckable(true);
92     m_aAxisEnabled->setChecked(false);
93     m_menu->addAction(m_aAxisEnabled);
94     b &= connect(m_aAxisEnabled, SIGNAL(changed()), this, SLOT(forceUpdateBackground()));
95
96     Q_ASSERT(b);
97
98
99     this->setMouseTracking(true);
100     slotGainChanged(ui->sliderGain->value());
101
102     init();
103 }
104
105 Vectorscope::~Vectorscope()
106 {
107     writeConfig();
108
109     delete m_colorTools;
110     delete m_colorPlaneExport;
111     delete m_vectorscopeGenerator;
112
113     delete m_aExportBackground;
114     delete m_aAxisEnabled;
115     delete m_a75PBox;
116 }
117
118 QString Vectorscope::widgetName() const { return QString("Vectorscope"); }
119
120 void Vectorscope::readConfig()
121 {
122     AbstractScopeWidget::readConfig();
123
124     KSharedConfigPtr config = KGlobal::config();
125     KConfigGroup scopeConfig(config, configName());
126     m_a75PBox->setChecked(scopeConfig.readEntry("75PBox", false));
127     m_aAxisEnabled->setChecked(scopeConfig.readEntry("axis", false));
128 }
129
130 void Vectorscope::writeConfig()
131 {
132     KSharedConfigPtr config = KGlobal::config();
133     KConfigGroup scopeConfig(config, configName());
134     scopeConfig.writeEntry("75PBox", m_a75PBox->isChecked());
135     scopeConfig.writeEntry("axis", m_aAxisEnabled->isChecked());
136     scopeConfig.sync();
137 }
138
139 QRect Vectorscope::scopeRect()
140 {
141     // Distance from top/left/right
142     int offset = 6;
143
144     // We want to paint below the controls area. The line is the lowest element.
145     QPoint topleft(offset, ui->verticalSpacer->geometry().y()+offset);
146     QPoint bottomright(ui->horizontalSpacer->geometry().right()-offset, this->size().height()-offset);
147
148     QRect scopeRect(topleft, bottomright);
149
150     // Circle Width: min of width and height
151     cw = (scopeRect.height() < scopeRect.width()) ? scopeRect.height() : scopeRect.width();
152     scopeRect.setWidth(cw);
153     scopeRect.setHeight(cw);
154
155
156     m_centerPoint = m_vectorscopeGenerator->mapToCircle(scopeRect.size(), QPointF(0,0));
157     pR75 = m_vectorscopeGenerator->mapToCircle(scopeRect.size(), P75*VectorscopeGenerator::scaling*YUV_R);
158     pG75 = m_vectorscopeGenerator->mapToCircle(scopeRect.size(), P75*VectorscopeGenerator::scaling*YUV_G);
159     pB75 = m_vectorscopeGenerator->mapToCircle(scopeRect.size(), P75*VectorscopeGenerator::scaling*YUV_B);
160     pCy75 = m_vectorscopeGenerator->mapToCircle(scopeRect.size(), P75*VectorscopeGenerator::scaling*YUV_Cy);
161     pMg75 = m_vectorscopeGenerator->mapToCircle(scopeRect.size(), P75*VectorscopeGenerator::scaling*YUV_Mg);
162     pYl75 = m_vectorscopeGenerator->mapToCircle(scopeRect.size(), P75*VectorscopeGenerator::scaling*YUV_Yl);
163
164     return scopeRect;
165 }
166
167 bool Vectorscope::isHUDDependingOnInput() const { return false; }
168 bool Vectorscope::isScopeDependingOnInput() const { return true; }
169 bool Vectorscope::isBackgroundDependingOnInput() const { return false; }
170
171 QImage Vectorscope::renderHUD(uint)
172 {
173
174     QImage hud;
175
176     if (m_circleEnabled) {
177         // Mouse moved: Draw a circle over the scope
178
179         hud = QImage(m_scopeRect.size(), QImage::Format_ARGB32);
180         hud.fill(qRgba(0, 0, 0, 0));
181
182         QPainter davinci(&hud);
183         QPoint widgetCenterPoint = m_scopeRect.topLeft() + m_centerPoint;
184
185         int dx = -widgetCenterPoint.x()+m_mousePos.x();
186         int dy =  widgetCenterPoint.y()-m_mousePos.y();
187
188         QPoint reference = m_vectorscopeGenerator->mapToCircle(m_scopeRect.size(), QPointF(1,0));
189
190         float r = sqrt(dx*dx + dy*dy);
191         float percent = (float) 100*r/VectorscopeGenerator::scaling/m_gain/(reference.x() - widgetCenterPoint.x());
192
193         switch (ui->backgroundMode->itemData(ui->backgroundMode->currentIndex()).toInt()) {
194         case BG_NONE:
195             davinci.setPen(penLight);
196             break;
197         default:
198             davinci.setPen(penDark);
199             break;
200         }
201         davinci.drawEllipse(m_centerPoint, (int)r, (int)r);
202         davinci.setPen(penThin);
203         davinci.drawText(QPoint(m_scopeRect.width()-40, m_scopeRect.height()), i18n("%1 \%", QString::number(percent, 'f', 0)));
204
205         float angle = copysign(acos(dx/r)*180/M_PI, dy);
206         davinci.drawText(QPoint(10, m_scopeRect.height()), i18n("%1°", QString::number(angle, 'f', 1)));
207
208         m_circleEnabled = false;
209     } else {
210         hud = QImage(0, 0, QImage::Format_ARGB32);
211     }
212     emit signalHUDRenderingFinished(0, 1);
213     return hud;
214 }
215
216 QImage Vectorscope::renderScope(uint accelerationFactor, QImage qimage)
217 {
218     QTime start = QTime::currentTime();
219     QImage scope;
220
221     if (cw <= 0) {
222         qDebug() << "Scope size not known yet. Aborting.";
223     } else {
224
225         scope = m_vectorscopeGenerator->calculateVectorscope(m_scopeRect.size(),
226                                                              qimage,
227                                                              m_gain, (VectorscopeGenerator::PaintMode) ui->paintMode->itemData(ui->paintMode->currentIndex()).toInt(),
228                                                              m_aAxisEnabled->isChecked(), accelerationFactor);
229
230     }
231
232     unsigned int mseconds = start.msecsTo(QTime::currentTime());
233     emit signalScopeRenderingFinished(mseconds, accelerationFactor);
234     return scope;
235 }
236
237 QImage Vectorscope::renderBackground(uint)
238 {
239     QTime start = QTime::currentTime();
240     start.start();
241
242     QImage bg;
243     switch (ui->backgroundMode->itemData(ui->backgroundMode->currentIndex()).toInt()) {
244     case BG_YUV:
245         qDebug() << "YUV background.";
246         bg = m_colorTools->yuvColorWheel(m_scopeRect.size(), (unsigned char) 128, 1/VectorscopeGenerator::scaling, false, true);
247         break;
248     case BG_CHROMA:
249         bg = m_colorTools->yuvColorWheel(m_scopeRect.size(), (unsigned char) 255, 1/VectorscopeGenerator::scaling, true, true);
250         break;
251     default:
252         qDebug() << "No background.";
253         bg = QImage(cw, cw, QImage::Format_ARGB32);
254         bg.fill(qRgba(0,0,0,0));
255         break;
256     }
257
258
259     // Draw the vectorscope circle
260     QPainter davinci(&bg);
261     QPoint vinciPoint;
262
263
264     davinci.setRenderHint(QPainter::Antialiasing, true);
265
266     davinci.setPen(penThick);
267     davinci.drawEllipse(0, 0, cw, cw);
268
269     // Draw RGB/CMY points with 100% chroma
270     vinciPoint = m_vectorscopeGenerator->mapToCircle(m_scopeRect.size(), VectorscopeGenerator::scaling*YUV_R);
271     davinci.drawEllipse(vinciPoint, 4,4);
272     davinci.drawText(vinciPoint-QPoint(20, -10), "R");
273
274     vinciPoint = m_vectorscopeGenerator->mapToCircle(m_scopeRect.size(), VectorscopeGenerator::scaling*YUV_G);
275     davinci.drawEllipse(vinciPoint, 4,4);
276     davinci.drawText(vinciPoint-QPoint(20, 0), "G");
277
278     vinciPoint = m_vectorscopeGenerator->mapToCircle(m_scopeRect.size(), VectorscopeGenerator::scaling*YUV_B);
279     davinci.drawEllipse(vinciPoint, 4,4);
280     davinci.drawText(vinciPoint+QPoint(15, 10), "B");
281
282     vinciPoint = m_vectorscopeGenerator->mapToCircle(m_scopeRect.size(), VectorscopeGenerator::scaling*YUV_Cy);
283     davinci.drawEllipse(vinciPoint, 4,4);
284     davinci.drawText(vinciPoint+QPoint(15, -5), "Cy");
285
286     vinciPoint = m_vectorscopeGenerator->mapToCircle(m_scopeRect.size(), VectorscopeGenerator::scaling*YUV_Mg);
287     davinci.drawEllipse(vinciPoint, 4,4);
288     davinci.drawText(vinciPoint+QPoint(15, 10), "Mg");
289
290     vinciPoint = m_vectorscopeGenerator->mapToCircle(m_scopeRect.size(), VectorscopeGenerator::scaling*YUV_Yl);
291     davinci.drawEllipse(vinciPoint, 4,4);
292     davinci.drawText(vinciPoint-QPoint(25, 0), "Yl");
293
294     switch (ui->backgroundMode->itemData(ui->backgroundMode->currentIndex()).toInt()) {
295     case BG_NONE:
296         davinci.setPen(penLight);
297         break;
298     default:
299         davinci.setPen(penDark);
300         break;
301     }
302
303     // Draw axis
304     if (m_aAxisEnabled->isChecked()) {
305         davinci.drawLine(m_vectorscopeGenerator->mapToCircle(m_scopeRect.size(), QPointF(0,-.9)), m_vectorscopeGenerator->mapToCircle(m_scopeRect.size(), QPointF(0,.9)));
306         davinci.drawLine(m_vectorscopeGenerator->mapToCircle(m_scopeRect.size(), QPointF(-.9,0)), m_vectorscopeGenerator->mapToCircle(m_scopeRect.size(), QPointF(.9,0)));
307     }
308
309     // Draw center point
310     switch (ui->backgroundMode->itemData(ui->backgroundMode->currentIndex()).toInt()) {
311     case BG_CHROMA:
312         davinci.setPen(penDark);
313         break;
314     default:
315         davinci.setPen(penThin);
316         break;
317     }
318     davinci.drawEllipse(m_centerPoint, 5,5);
319
320
321     // Draw 75% box
322     if (m_a75PBox->isChecked()) {
323         davinci.drawLine(pR75, pYl75);
324         davinci.drawLine(pYl75, pG75);
325         davinci.drawLine(pG75, pCy75);
326         davinci.drawLine(pCy75, pB75);
327         davinci.drawLine(pB75, pMg75);
328         davinci.drawLine(pMg75, pR75);
329     }
330
331     // Draw RGB/CMY points with 75% chroma (for NTSC)
332     davinci.setPen(penThin);
333     davinci.drawEllipse(pR75, 3,3);
334     davinci.drawEllipse(pG75, 3,3);
335     davinci.drawEllipse(pB75, 3,3);
336     davinci.drawEllipse(pCy75, 3,3);
337     davinci.drawEllipse(pMg75, 3,3);
338     davinci.drawEllipse(pYl75, 3,3);
339
340     // Draw realtime factor (number of skipped pixels)
341     if (m_aRealtime->isChecked()) {
342         davinci.setPen(penThin);
343         davinci.drawText(QPoint(m_scopeRect.width()-40, m_scopeRect.height()-15), QVariant(m_accelFactorScope).toString().append("x"));
344     }
345
346     emit signalBackgroundRenderingFinished(start.elapsed(), 1);
347     return bg;
348 }
349
350
351
352
353 ///// Slots /////
354
355 void Vectorscope::slotGainChanged(int newval)
356 {
357     m_gain = 1 + (float)newval/10;
358     ui->lblGain->setText(QString::number(m_gain, 'f', 1) + "x");
359     forceUpdateScope();
360 }
361
362 void Vectorscope::slotExportBackground()
363 {
364     qDebug() << "Exporting background";
365     m_colorPlaneExport->show();
366
367 }
368
369 void Vectorscope::slotBackgroundChanged()
370 {
371     // Background changed, switch to a suitable color mode now
372     int index;
373     switch (ui->backgroundMode->itemData(ui->backgroundMode->currentIndex()).toInt()) {
374     case BG_YUV:
375         index = ui->paintMode->findData(QVariant(VectorscopeGenerator::PaintMode_Black));
376         if (index >= 0) {
377             ui->paintMode->setCurrentIndex(index);
378         }
379         break;
380
381     case BG_NONE:
382         if (ui->paintMode->itemData(ui->paintMode->currentIndex()).toInt() == VectorscopeGenerator::PaintMode_Black) {
383             index = ui->paintMode->findData(QVariant(VectorscopeGenerator::PaintMode_Green2));
384             ui->paintMode->setCurrentIndex(index);
385         }
386         break;
387     }
388     forceUpdateBackground();
389 }
390
391
392
393 ///// Events /////
394
395 void Vectorscope::mouseMoveEvent(QMouseEvent *event)
396 {
397     // Draw a circle around the center,
398     // showing percentage number of the radius length
399
400     m_circleEnabled = true;
401     m_mousePos = event->pos();
402     forceUpdateHUD();
403 }
404
405 void Vectorscope::leaveEvent(QEvent *event)
406 {
407     // Repaint the HUD without the circle
408
409     m_circleEnabled = false;
410     QWidget::leaveEvent(event);
411     forceUpdateHUD();
412 }