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
38 #include <QMouseEvent>
42 #include <qtconcurrentrun.h>
45 #include "vectorscope.h"
47 const float SCALING = 1/.7; // See class docs
48 const float P75 = .75;
49 const QPointF YUV_R(-.147, .615);
50 const QPointF YUV_G(-.289, -.515);
51 const QPointF YUV_B(.437, -.100);
52 const QPointF YUV_Cy(.147, -.615);
53 const QPointF YUV_Mg(.289, .515);
54 const QPointF YUV_Yl(-.437, .100);
56 const QPen penThick(QBrush(QColor(250,250,250)), 2, Qt::SolidLine);
57 const QPen penThin(QBrush(QColor(250,250,250)), 1, Qt::SolidLine);
58 const QPen penLight(QBrush(QColor(144,255,100,50)), 1, Qt::SolidLine);
61 Vectorscope::Vectorscope(Monitor *projMonitor, Monitor *clipMonitor, QWidget *parent) :
63 m_projMonitor(projMonitor),
64 m_clipMonitor(clipMonitor),
65 m_activeRender(clipMonitor->render),
69 initialDimensionUpdateDone(false)
73 paintMode->insertItem(GREEN, i18n("Green"));
74 paintMode->insertItem(CHROMA, i18n("Chroma"));
75 paintMode->insertItem(ORIG, i18n("Original Color"));
77 connect(paintMode, SIGNAL(currentIndexChanged(int)), this, SLOT(slotPaintModeChanged(int)));
78 connect(cbMagnify, SIGNAL(stateChanged(int)), this, SLOT(slotMagnifyChanged()));
79 connect(this, SIGNAL(signalScopeCalculationFinished()), this, SLOT(slotScopeCalculationFinished()));
80 connect(cbMagnify, SIGNAL(stateChanged(int)), this, SLOT(slotRenderZoneUpdated()));
82 newFrames.fetchAndStoreRelaxed(0);
84 this->setMouseTracking(true);
88 Vectorscope::~Vectorscope()
94 Input point is on [-1,1]², 0 being at the center,
95 and positive directions are top/right.
97 Maps to a QRect «inside» which is using the
98 0 point in the top left corner. The coordinates of
99 the rect «inside» are relative to «parent». The
100 coordinates returned can be used in the parent.
104 +-------------------+
108 | | --0++ | | < point
112 +-------------------+
115 QPoint Vectorscope::mapToCanvas(QRect inside, QPointF point)
117 return QPoint(point.x()/2*inside.width() + inside.width()/2 + inside.x(),
118 -point.y()/2*inside.height() + inside.height()/2 + inside.y());
121 bool Vectorscope::prodCalcThread()
123 if (m_scopeCalcThread.isRunning()) {
124 qDebug() << "Calc thread still running.";
127 // See http://doc.qt.nokia.com/latest/qtconcurrentrun.html#run about
128 // running member functions in a thread
129 qDebug() << "Calc thread not running anymore, finished: " << m_scopeCalcThread.isFinished() << ", Starting new thread";
130 m_scopeCalcThread = QtConcurrent::run(this, &Vectorscope::calculateScope);
135 void Vectorscope::calculateScope()
137 // Prepare the vectorscope data
138 QImage scope(cw, cw, QImage::Format_ARGB32);
139 scope.fill(qRgba(0,0,0,0));
141 QImage img(m_activeRender->extractFrame(m_activeRender->seekFramePosition()));
142 newFrames.fetchAndStoreRelease(0); // Reset number of new frames, as we just got the newest
143 uchar *bits = img.bits();
146 double dy, dr, dg, db, dmax;
151 for (int i = 0; i < img.byteCount(); i+= 4) {
152 QRgb *col = (QRgb *) bits;
158 y = (double) 0.001173 * r + 0.002302 * g + 0.0004471 * b;
159 u = (double) -0.0005781*r -0.001135*g +0.001713*b;
160 v = (double) 0.002411*r -0.002019*g -0.0003921*b;
162 pt = mapToCanvas(QRect(QPoint(0,0), scope.size()), QPointF(SCALING*scaling*u, SCALING*scaling*v));
164 if (!(pt.x() <= scopeRect.width() && pt.x() >= 0
165 && pt.y() <= scopeRect.height() && pt.y() >= 0)) {
166 // Point lies outside (because of scaling), don't plot it
170 // Draw the pixel using the chosen draw mode
171 switch (iPaintMode) {
175 dg = dy - 100.6*u - 148*v;
179 if (dg > dmax) dmax = dg;
180 if (db > dmax) dmax = db;
187 scope.setPixel(pt, qRgba(dr, dg, db, 255));
190 scope.setPixel(pt, *col);
193 px = scope.pixel(pt);
194 scope.setPixel(pt, qRgba(qRed(px)+(255-qRed(px))/40, 255, qBlue(px)+(255-qBlue(px))/40, qAlpha(px)+(255-qAlpha(px))/20));
203 qDebug() << "Scope rendered";
204 emit signalScopeCalculationFinished();
207 void Vectorscope::updateDimensions()
209 // Widget width/height
210 int ww = this->size().width();
211 int wh = this->size().height();
213 // Distance from top/left/right
216 // controlsArea contains the controls at the top;
217 // We want to paint below
218 QPoint topleft(offset, controlsArea->geometry().height()+offset);
220 // Circle Width: min of width and height
221 cw = wh - topleft.y();
222 if (ww < cw) { cw = ww; }
224 scopeRect = QRect(topleft, QPoint(cw, cw) + topleft);
227 void Vectorscope::paintEvent(QPaintEvent *)
230 if (!initialDimensionUpdateDone) {
231 // This is a workaround.
232 // When updating the dimensions in the constructor, the size
233 // of the control items at the top are simply ignored! So do
236 initialDimensionUpdateDone = true;
239 // Draw the vectorscope circle
240 QPainter davinci(this);
242 QPoint centerPoint = mapToCanvas(scopeRect, QPointF(0,0));
243 davinci.setRenderHint(QPainter::Antialiasing, true);
244 davinci.fillRect(0, 0, this->size().width(), this->size().height(), QColor(25,25,23));
246 davinci.setPen(penThick);
247 davinci.drawEllipse(scopeRect);
249 // Draw RGB/CMY points with 100% chroma
250 vinciPoint = mapToCanvas(scopeRect, SCALING*YUV_R);
251 davinci.drawEllipse(vinciPoint, 4,4);
252 davinci.drawText(vinciPoint-QPoint(20, -10), "R");
254 vinciPoint = mapToCanvas(scopeRect, SCALING*YUV_G);
255 davinci.drawEllipse(vinciPoint, 4,4);
256 davinci.drawText(vinciPoint-QPoint(20, 0), "G");
258 vinciPoint = mapToCanvas(scopeRect, SCALING*YUV_B);
259 davinci.drawEllipse(vinciPoint, 4,4);
260 davinci.drawText(vinciPoint+QPoint(15, 10), "B");
262 vinciPoint = mapToCanvas(scopeRect, SCALING*YUV_Cy);
263 davinci.drawEllipse(vinciPoint, 4,4);
264 davinci.drawText(vinciPoint+QPoint(15, -5), "Cy");
266 vinciPoint = mapToCanvas(scopeRect, SCALING*YUV_Mg);
267 davinci.drawEllipse(vinciPoint, 4,4);
268 davinci.drawText(vinciPoint+QPoint(15, 10), "Mg");
270 vinciPoint = mapToCanvas(scopeRect, SCALING*YUV_Yl);
271 davinci.drawEllipse(vinciPoint, 4,4);
272 davinci.drawText(vinciPoint-QPoint(25, 0), "Yl");
274 // Draw RGB/CMY points with 75% chroma (for NTSC)
275 davinci.setPen(penThin);
276 davinci.drawEllipse(centerPoint, 4,4);
277 davinci.drawEllipse(mapToCanvas(scopeRect, P75*SCALING*YUV_R), 3,3);
278 davinci.drawEllipse(mapToCanvas(scopeRect, P75*SCALING*YUV_G), 3,3);
279 davinci.drawEllipse(mapToCanvas(scopeRect, P75*SCALING*YUV_B), 3,3);
280 davinci.drawEllipse(mapToCanvas(scopeRect, P75*SCALING*YUV_Cy), 3,3);
281 davinci.drawEllipse(mapToCanvas(scopeRect, P75*SCALING*YUV_Mg), 3,3);
282 davinci.drawEllipse(mapToCanvas(scopeRect, P75*SCALING*YUV_Yl), 3,3);
287 davinci.setPen(penLight);
290 // Draw the scope data (previously calculated in a separate thread)
291 davinci.drawImage(scopeRect.topLeft(), m_scope);
295 // Mouse moved: Draw a circle over the scope
297 int dx = centerPoint.x()-mousePos.x();
298 int dy = centerPoint.y()-mousePos.y();
300 QPoint reference = mapToCanvas(scopeRect, QPointF(1,0));
302 int r = sqrt(dx*dx + dy*dy);
303 float percent = (float) 100*r/SCALING/scaling/(reference.x() - centerPoint.x());
305 davinci.drawEllipse(centerPoint, r,r);
306 davinci.setPen(penThin);
307 davinci.drawText(scopeRect.bottomRight()-QPoint(40,0), QVariant((int)percent).toString().append(" %"));
309 circleEnabled = false;
313 void Vectorscope::slotPaintModeChanged(int index)
319 void Vectorscope::slotMagnifyChanged()
321 if (cbMagnify->isChecked()) {
329 void Vectorscope::slotActiveMonitorChanged(bool isClipMonitor)
332 m_activeRender = m_clipMonitor->render;
333 disconnect(this, SLOT(slotRenderZoneUpdated()));
334 connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
336 m_activeRender = m_projMonitor->render;
337 disconnect(this, SLOT(slotRenderZoneUpdated()));
338 connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
342 void Vectorscope::slotRenderZoneUpdated()
344 qDebug() << "Monitor incoming. New frames total: " << newFrames;
345 // Monitor has shown a new frame
346 newFrames.fetchAndAddRelaxed(1);
347 if (cbAutoRefresh->isChecked()) {
352 void Vectorscope::mousePressEvent(QMouseEvent *)
354 // Update the scope on mouse press
358 void Vectorscope::mouseMoveEvent(QMouseEvent *event)
360 // Draw a circle around the center,
361 // showing percentage number of the radius length
363 circleEnabled = true;
364 mousePos = event->pos();
368 void Vectorscope::resizeEvent(QResizeEvent *event)
370 qDebug() << "Resized.";
372 QWidget::resizeEvent(event);
375 void Vectorscope::slotScopeCalculationFinished()
378 qDebug() << "Scope updated.";
380 // If auto-refresh is enabled and new frames are available,
381 // just start the next calculation.
382 if (newFrames > 0 && cbAutoRefresh->isChecked()) {
383 qDebug() << "More frames in the queue: " << newFrames;