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 ***************************************************************************/
15 The basis matrix for converting RGB to YUV is:
19 0.29900 0.58700 0.11400 1.00000
20 -0.14741 -0.28939 0.43680 x 0.00000
21 0.61478 -0.51480 -0.09998 0.00000
23 The resulting YUV value is then drawn on the circle
24 using U and V as coordinate values.
26 The maximum length of such an UV vector is reached
27 for the colors Red and Cyan: 0.632.
28 To make optimal use of space in the circle, this value
29 can be used for scaling.
31 As we are dealing with RGB values in a range of {0,...,255}
32 and the conversion values are made for [0,1], we already
33 divide the conversion values by 255 previously, e.g. in
37 http://de.wikipedia.org/wiki/Vektorskop
38 http://www.elektroniktutor.de/techno/vektskop.html
43 #include <QMouseEvent>
48 #include <qtconcurrentrun.h>
52 #include "vectorscope.h"
54 const float SCALING = 1/.7; // See class docs
55 const float P75 = .75;
56 const unsigned char DEFAULT_Y = 255;
58 const QPointF YUV_R(-.147, .615);
59 const QPointF YUV_G(-.289, -.515);
60 const QPointF YUV_B(.437, -.100);
61 const QPointF YUV_Cy(.147, -.615);
62 const QPointF YUV_Mg(.289, .515);
63 const QPointF YUV_Yl(-.437, .100);
65 const QPen penThick(QBrush(QColor(250,250,250)), 2, Qt::SolidLine);
66 const QPen penThin(QBrush(QColor(250,250,250)), 1, Qt::SolidLine);
67 const QPen penLight(QBrush(QColor(200,200,250,150)), 1, Qt::SolidLine);
68 const QPen penDark(QBrush(QColor(0,0,20,250)), 1, Qt::SolidLine);
71 Vectorscope::Vectorscope(Monitor *projMonitor, Monitor *clipMonitor, QWidget *parent) :
73 m_projMonitor(projMonitor),
74 m_clipMonitor(clipMonitor),
75 m_activeRender(clipMonitor->render),
78 initialDimensionUpdateDone(false)
82 m_colorTools = new ColorTools();
83 m_colorPlaneExport = new ColorPlaneExport(this);
85 paintMode->addItem(i18n("Green"), QVariant(PAINT_GREEN));
86 paintMode->addItem(i18n("Green 2"), QVariant(PAINT_GREEN2));
87 paintMode->addItem(i18n("Black"), QVariant(PAINT_BLACK));
88 paintMode->addItem(i18n("Modified YUV (Chroma)"), QVariant(PAINT_CHROMA));
89 paintMode->addItem(i18n("YUV"), QVariant(PAINT_YUV));
90 paintMode->addItem(i18n("Original Color"), QVariant(PAINT_ORIG));
92 backgroundMode->addItem(i18n("None"), QVariant(BG_NONE));
93 backgroundMode->addItem(i18n("YUV"), QVariant(BG_YUV));
94 backgroundMode->addItem(i18n("Modified YUV (Chroma)"), QVariant(BG_CHROMA));
96 cbAutoRefresh->setChecked(true);
98 connect(paintMode, SIGNAL(currentIndexChanged(int)), this, SLOT(slotPaintModeChanged(int)));
99 connect(backgroundMode, SIGNAL(currentIndexChanged(int)), this, SLOT(slotBackgroundChanged()));
100 connect(cbMagnify, SIGNAL(stateChanged(int)), this, SLOT(slotMagnifyChanged()));
101 connect(this, SIGNAL(signalScopeCalculationFinished()), this, SLOT(slotScopeCalculationFinished()));
102 connect(m_colorTools, SIGNAL(signalWheelCalculationFinished()), this, SLOT(slotWheelCalculationFinished()));
103 connect(paintMode, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateScope()));
104 connect(cbAutoRefresh, SIGNAL(stateChanged(int)), this, SLOT(slotUpdateScope()));
106 newFrames.fetchAndStoreRelaxed(0);
107 newChanges.fetchAndStoreRelaxed(0);
108 newWheelChanges.fetchAndStoreRelaxed(0);
111 ///// Build context menu /////
112 setContextMenuPolicy(Qt::ActionsContextMenu);
114 m_aExportBackground = new QAction(i18n("Export background"), this);
115 addAction(m_aExportBackground);
116 connect(m_aExportBackground, SIGNAL(triggered()), this, SLOT(slotExportBackground()));
118 m_a75PBox = new QAction(i18n("75% box"), this);
119 m_a75PBox->setCheckable(true);
120 m_a75PBox->setChecked(false);
121 addAction(m_a75PBox);
122 connect(m_a75PBox, SIGNAL(changed()), this, SLOT(update()));
124 m_aAxisEnabled = new QAction(i18n("Draw axis"), this);
125 m_aAxisEnabled->setCheckable(true);
126 m_aAxisEnabled->setChecked(false);
127 addAction(m_aAxisEnabled);
128 connect(m_aAxisEnabled, SIGNAL(changed()), this, SLOT(update()));
131 this->setMouseTracking(true);
136 Vectorscope::~Vectorscope()
138 delete m_colorPlaneExport;
143 Input point is on [-1,1]², 0 being at the center,
144 and positive directions are top/right.
146 Maps to a QRect «inside» which is using the
147 0 point in the top left corner. The coordinates of
148 the rect «inside» are relative to «parent». The
149 coordinates returned can be used in the parent.
153 +-------------------+
157 | | --0++ | | < point
161 +-------------------+
164 QPoint Vectorscope::mapToCanvas(QRect inside, QPointF point)
166 return QPoint(point.x()/2*inside.width() + inside.width()/2 + inside.x(),
167 -point.y()/2*inside.height() + inside.height()/2 + inside.y());
170 bool Vectorscope::prodCalcThread()
172 if (m_scopeCalcThread.isRunning()) {
173 qDebug() << "Calc thread still running.";
176 // See http://doc.qt.nokia.com/latest/qtconcurrentrun.html#run about
177 // running member functions in a thread
178 qDebug() << "Calc thread not running anymore, finished: " << m_scopeCalcThread.isFinished() << ", Starting new thread";
179 m_scopeCalcThread = QtConcurrent::run(this, &Vectorscope::calculateScope);
180 newFrames.fetchAndStoreRelease(0); // Reset number of new frames, as we just got the newest
181 newChanges.fetchAndStoreRelease(0); // Do the same with the external changes counter
186 bool Vectorscope::prodWheelThread()
188 if (m_wheelCalcThread.isRunning()) {
189 qDebug() << "Wheel thread still running.";
192 switch (backgroundMode->itemData(backgroundMode->currentIndex()).toInt()) {
194 qDebug() << "No background.";
199 qDebug() << "YUV background.";
200 m_wheelCalcThread = QtConcurrent::run(m_colorTools, &ColorTools::yuvColorWheel, m_scopeRect.size(), (unsigned char) 128, 1/SCALING, false, true);
203 m_wheelCalcThread = QtConcurrent::run(m_colorTools, &ColorTools::yuvColorWheel, m_scopeRect.size(), (unsigned char) 255, 1/SCALING, true, true);
206 newWheelChanges.fetchAndStoreRelaxed(0);
213 void Vectorscope::calculateScope()
215 // Prepare the vectorscope data
216 QImage scope(cw, cw, QImage::Format_ARGB32);
217 scope.fill(qRgba(0,0,0,0));
219 QImage img(m_activeRender->extractFrame(m_activeRender->seekFramePosition()));
220 const uchar *bits = img.bits();
223 double dy, dr, dg, db, dmax;
228 const QRect scopeRect(QPoint(0,0), scope.size());
230 for (int i = 0; i < img.byteCount(); i+= 4) {
231 QRgb *col = (QRgb *) bits;
237 y = (double) 0.001173 * r +0.002302 * g +0.0004471* b;
238 u = (double) -0.0005781* r -0.001135 * g +0.001713 * b;
239 v = (double) 0.002411 * r -0.002019 * g -0.0003921* b;
241 pt = mapToCanvas(scopeRect, QPointF(SCALING*m_scaling*u, SCALING*m_scaling*v));
243 if (!(pt.x() <= scopeRect.width() && pt.x() >= 0
244 && pt.y() <= scopeRect.height() && pt.y() >= 0)) {
245 // Point lies outside (because of scaling), don't plot it
249 // Draw the pixel using the chosen draw mode.
250 switch (paintMode->itemData(paintMode->currentIndex()).toInt()) {
253 dy = 128; // Default Y value. Lower = darker.
255 // Calculate the RGB values from YUV
257 dg = dy - 100.6*u - 148*v;
263 if (dr > 255) dr = 255;
264 if (dg > 255) dg = 255;
265 if (db > 255) db = 255;
267 scope.setPixel(pt, qRgba(dr, dg, db, 255));
271 dy = 200; // Default Y value. Lower = darker.
273 // Calculate the RGB values from YUV
275 dg = dy - 100.6*u - 148*v;
278 // Scale the RGB values back to max 255
280 if (dg > dmax) dmax = dg;
281 if (db > dmax) dmax = db;
288 scope.setPixel(pt, qRgba(dr, dg, db, 255));
291 scope.setPixel(pt, *col);
294 px = scope.pixel(pt);
295 scope.setPixel(pt, qRgba(qRed(px)+(255-qRed(px))/30, 255, qBlue(px)+(255-qBlue(px))/25, qAlpha(px)+(255-qAlpha(px))/20));
298 px = scope.pixel(pt);
299 scope.setPixel(pt, qRgba(qRed(px)+(255-qRed(px))/40+5, 255, qBlue(px)+(255-qBlue(px))/30+10, qAlpha(px)+(255-qAlpha(px))/20));
302 px = scope.pixel(pt);
303 scope.setPixel(pt, qRgba(0,0,0, qAlpha(px)+(255-qAlpha(px))/20));
313 qDebug() << "Scope rendered";
314 emit signalScopeCalculationFinished();
317 void Vectorscope::updateDimensions()
319 // Widget width/height
320 int ww = this->size().width();
321 int wh = this->size().height();
323 // Distance from top/left/right
326 // controlsArea contains the controls at the top;
327 // We want to paint below
328 QPoint topleft(offset, controlsArea->geometry().height()+offset);
330 // Circle Width: min of width and height
331 cw = wh - topleft.y();
332 if (ww < cw) { cw = ww; }
334 m_scopeRect = QRect(topleft, QPoint(cw, cw) + topleft);
336 centerPoint = mapToCanvas(m_scopeRect, QPointF(0,0));
337 pR75 = mapToCanvas(m_scopeRect, P75*SCALING*YUV_R);
338 pG75 = mapToCanvas(m_scopeRect, P75*SCALING*YUV_G);
339 pB75 = mapToCanvas(m_scopeRect, P75*SCALING*YUV_B);
340 pCy75 = mapToCanvas(m_scopeRect, P75*SCALING*YUV_Cy);
341 pMg75 = mapToCanvas(m_scopeRect, P75*SCALING*YUV_Mg);
342 pYl75 = mapToCanvas(m_scopeRect, P75*SCALING*YUV_Yl);
345 void Vectorscope::paintEvent(QPaintEvent *)
348 if (!initialDimensionUpdateDone) {
349 // This is a workaround.
350 // When updating the dimensions in the constructor, the size
351 // of the control items at the top are simply ignored! So do
354 initialDimensionUpdateDone = true;
357 // Draw the vectorscope circle
358 QPainter davinci(this);
362 davinci.setRenderHint(QPainter::Antialiasing, true);
363 davinci.fillRect(0, 0, this->size().width(), this->size().height(), QColor(25,25,23));
365 davinci.drawImage(m_scopeRect.topLeft(), m_wheel);
367 davinci.setPen(penThick);
368 davinci.drawEllipse(m_scopeRect);
370 // Draw RGB/CMY points with 100% chroma
371 vinciPoint = mapToCanvas(m_scopeRect, SCALING*YUV_R);
372 davinci.drawEllipse(vinciPoint, 4,4);
373 davinci.drawText(vinciPoint-QPoint(20, -10), "R");
375 vinciPoint = mapToCanvas(m_scopeRect, SCALING*YUV_G);
376 davinci.drawEllipse(vinciPoint, 4,4);
377 davinci.drawText(vinciPoint-QPoint(20, 0), "G");
379 vinciPoint = mapToCanvas(m_scopeRect, SCALING*YUV_B);
380 davinci.drawEllipse(vinciPoint, 4,4);
381 davinci.drawText(vinciPoint+QPoint(15, 10), "B");
383 vinciPoint = mapToCanvas(m_scopeRect, SCALING*YUV_Cy);
384 davinci.drawEllipse(vinciPoint, 4,4);
385 davinci.drawText(vinciPoint+QPoint(15, -5), "Cy");
387 vinciPoint = mapToCanvas(m_scopeRect, SCALING*YUV_Mg);
388 davinci.drawEllipse(vinciPoint, 4,4);
389 davinci.drawText(vinciPoint+QPoint(15, 10), "Mg");
391 vinciPoint = mapToCanvas(m_scopeRect, SCALING*YUV_Yl);
392 davinci.drawEllipse(vinciPoint, 4,4);
393 davinci.drawText(vinciPoint-QPoint(25, 0), "Yl");
395 switch (backgroundMode->itemData(backgroundMode->currentIndex()).toInt()) {
397 davinci.setPen(penLight);
400 davinci.setPen(penDark);
405 if (m_aAxisEnabled->isChecked()) {
406 davinci.drawLine(mapToCanvas(m_scopeRect, QPointF(0,-.9)), mapToCanvas(m_scopeRect, QPointF(0,.9)));
407 davinci.drawLine(mapToCanvas(m_scopeRect, QPointF(-.9,0)), mapToCanvas(m_scopeRect, QPointF(.9,0)));
411 switch (backgroundMode->itemData(backgroundMode->currentIndex()).toInt()) {
413 davinci.setPen(penDark);
416 davinci.setPen(penThin);
419 davinci.drawEllipse(centerPoint, 5,5);
423 if (m_a75PBox->isChecked()) {
424 davinci.drawLine(pR75, pYl75);
425 davinci.drawLine(pYl75, pG75);
426 davinci.drawLine(pG75, pCy75);
427 davinci.drawLine(pCy75, pB75);
428 davinci.drawLine(pB75, pMg75);
429 davinci.drawLine(pMg75, pR75);
432 // Draw RGB/CMY points with 75% chroma (for NTSC)
433 davinci.setPen(penThin);
434 davinci.drawEllipse(pR75, 3,3);
435 davinci.drawEllipse(pG75, 3,3);
436 davinci.drawEllipse(pB75, 3,3);
437 davinci.drawEllipse(pCy75, 3,3);
438 davinci.drawEllipse(pMg75, 3,3);
439 davinci.drawEllipse(pYl75, 3,3);
443 // Draw the scope data (previously calculated in a separate thread)
444 davinci.drawImage(m_scopeRect.topLeft(), m_scope);
448 // Mouse moved: Draw a circle over the scope
450 int dx = centerPoint.x()-mousePos.x();
451 int dy = centerPoint.y()-mousePos.y();
453 QPoint reference = mapToCanvas(m_scopeRect, QPointF(1,0));
455 int r = sqrt(dx*dx + dy*dy);
456 float percent = (float) 100*r/SCALING/m_scaling/(reference.x() - centerPoint.x());
458 switch (backgroundMode->itemData(backgroundMode->currentIndex()).toInt()) {
460 davinci.setPen(penLight);
463 davinci.setPen(penDark);
466 davinci.drawEllipse(centerPoint, r,r);
467 davinci.setPen(penThin);
468 davinci.drawText(m_scopeRect.bottomRight()-QPoint(40,0), QVariant((int)percent).toString().append(" %"));
470 circleEnabled = false;
479 void Vectorscope::slotMagnifyChanged()
481 if (cbMagnify->isChecked()) {
489 void Vectorscope::slotActiveMonitorChanged(bool isClipMonitor)
492 m_activeRender = m_clipMonitor->render;
493 disconnect(this, SLOT(slotRenderZoneUpdated()));
494 connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
496 m_activeRender = m_projMonitor->render;
497 disconnect(this, SLOT(slotRenderZoneUpdated()));
498 connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
502 void Vectorscope::slotRenderZoneUpdated()
504 qDebug() << "Monitor incoming. New frames total: " << newFrames;
505 // Monitor has shown a new frame
506 newFrames.fetchAndAddRelaxed(1);
507 if (cbAutoRefresh->isChecked()) {
512 void Vectorscope::slotScopeCalculationFinished()
514 if (!m_scopeCalcThread.isFinished()) {
515 // Wait for the thread to finish. Otherwise the scope might not get updated
516 // as prodCalcThread may see it still running.
517 QTime start = QTime::currentTime();
518 qDebug() << "Scope renderer has not finished yet, waiting ...";
519 m_scopeCalcThread.waitForFinished();
520 qDebug() << "Done. Waited for " << start.msecsTo(QTime::currentTime()) << " ms";
524 qDebug() << "Scope updated.";
526 // If auto-refresh is enabled and new frames are available,
527 // just start the next calculation.
528 if (newFrames > 0 && cbAutoRefresh->isChecked()) {
529 qDebug() << "More frames in the queue: " << newFrames;
531 } else if (newChanges > 0) {
532 qDebug() << newChanges << " changes (e.g. resize) in the meantime.";
535 qDebug() << newFrames << " new frames, " << newChanges << " new changes. Not updating.";
539 void Vectorscope::slotWheelCalculationFinished()
541 if (!m_wheelCalcThread.isFinished()) {
542 QTime start = QTime::currentTime();
543 qDebug() << "Wheel calc has not finished yet, waiting ...";
544 m_wheelCalcThread.waitForFinished();
545 qDebug() << "Done. Waited for " << start.msecsTo(QTime::currentTime()) << " ms";
548 qDebug() << "Wheel calculated. Updating.";
549 qDebug() << m_wheelCalcThread.resultCount() << " results from the Wheel thread.";
550 if (m_wheelCalcThread.resultCount() > 0) {
551 m_wheel = m_wheelCalcThread.resultAt(0);
554 if (newWheelChanges > 0) {
559 void Vectorscope::slotUpdateScope()
564 void Vectorscope::slotUpdateWheel()
569 void Vectorscope::slotExportBackground()
571 qDebug() << "Exporting background";
572 m_colorPlaneExport->show();
576 void Vectorscope::slotBackgroundChanged()
578 // Background changed, switch to a suitable color mode now
580 switch (backgroundMode->itemData(backgroundMode->currentIndex()).toInt()) {
582 index = paintMode->findData(QVariant(PAINT_BLACK));
584 paintMode->setCurrentIndex(index);
589 if (paintMode->itemData(paintMode->currentIndex()) == PAINT_BLACK) {
590 index = paintMode->findData(QVariant(PAINT_GREEN));
591 paintMode->setCurrentIndex(index);
595 newWheelChanges.fetchAndAddAcquire(1);
603 void Vectorscope::mousePressEvent(QMouseEvent *)
605 // Update the scope on mouse press
609 void Vectorscope::mouseMoveEvent(QMouseEvent *event)
611 // Draw a circle around the center,
612 // showing percentage number of the radius length
614 circleEnabled = true;
615 mousePos = event->pos();
619 void Vectorscope::resizeEvent(QResizeEvent *event)
621 qDebug() << "Resized.";
623 newChanges.fetchAndAddAcquire(1);
625 QWidget::resizeEvent(event);