histogram: implemented histogram widget
This commit is contained in:
@@ -57,7 +57,8 @@ ScopePanel::ScopePanel(QWidget* parent) :
|
||||
stack_->addWidget(waveform_view_);
|
||||
|
||||
// Create histogram
|
||||
stack_->addWidget(new QWidget());
|
||||
histogram_ = new HistogramScope();
|
||||
stack_->addWidget(histogram_);
|
||||
|
||||
connect(scope_type_combobox_, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), stack_, &QStackedWidget::setCurrentIndex);
|
||||
|
||||
@@ -88,6 +89,11 @@ void ScopePanel::DrewManagedTexture(OpenGLTexture *texture)
|
||||
waveform_view_->SetTexture(texture);
|
||||
}
|
||||
|
||||
void ScopePanel::SetBuffer(Frame *frame)
|
||||
{
|
||||
histogram_->SetBuffer(frame);
|
||||
}
|
||||
|
||||
void ScopePanel::Retranslate()
|
||||
{
|
||||
SetTitle(tr("Scope"));
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <QStackedWidget>
|
||||
|
||||
#include "widget/panel/panel.h"
|
||||
#include "widget/scope/histogram/histogram.h"
|
||||
#include "widget/scope/waveform/waveform.h"
|
||||
|
||||
OLIVE_NAMESPACE_ENTER
|
||||
@@ -51,6 +52,8 @@ public:
|
||||
public slots:
|
||||
void DrewManagedTexture(OpenGLTexture* texture);
|
||||
|
||||
void SetBuffer(Frame* frame);
|
||||
|
||||
protected:
|
||||
virtual void Retranslate() override;
|
||||
|
||||
@@ -63,6 +66,8 @@ private:
|
||||
|
||||
WaveformScope* waveform_view_;
|
||||
|
||||
HistogramScope* histogram_;
|
||||
|
||||
};
|
||||
|
||||
OLIVE_NAMESPACE_EXIT
|
||||
|
||||
@@ -106,6 +106,7 @@ void ViewerPanelBase::CreateScopePanel(ScopePanel::Type type)
|
||||
|
||||
// Connect viewer widget texture drawing to scope panel
|
||||
connect(vw, &ViewerWidget::DrewManagedTexture, p, &ScopePanel::DrewManagedTexture);
|
||||
connect(vw, &ViewerWidget::LoadedBuffer, p, &ScopePanel::SetBuffer);
|
||||
|
||||
if (!scope_panel_count_) {
|
||||
vw->SetEmitDrewManagedTextureEnabled(true);
|
||||
|
||||
@@ -20,23 +20,169 @@
|
||||
|
||||
#include "histogram.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QtMath>
|
||||
|
||||
#include "common/clamp.h"
|
||||
#include "common/functiontimer.h"
|
||||
|
||||
OLIVE_NAMESPACE_ENTER
|
||||
|
||||
HistogramScope::HistogramScope(QWidget* parent) :
|
||||
QOpenGLWidget(parent)
|
||||
QOpenGLWidget(parent),
|
||||
buffer_(nullptr)
|
||||
{
|
||||
connect(&worker_, &HistogramScopeWorker::Finished, this, &HistogramScope::FinishedProcessing, Qt::QueuedConnection);
|
||||
worker_.start(QThread::IdlePriority);
|
||||
}
|
||||
|
||||
HistogramScope::~HistogramScope()
|
||||
{
|
||||
worker_.Cancel();
|
||||
worker_.quit();
|
||||
worker_.wait();
|
||||
}
|
||||
|
||||
void HistogramScope::SetBuffer(Frame* frame)
|
||||
{
|
||||
// We'll be multithreading this, so we make a copy to prevent collisions
|
||||
buffer_ = frame;
|
||||
|
||||
StartUpdate();
|
||||
}
|
||||
|
||||
void HistogramScope::FinishedProcessing(QVector<double> red, QVector<double> green, QVector<double> blue)
|
||||
{
|
||||
red_val_ = red;
|
||||
green_val_ = green;
|
||||
blue_val_ = blue;
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
void HistogramScope::paintGL()
|
||||
{
|
||||
//QPainter p(this);
|
||||
QVector<QLine> red_lines(red_val_.size());
|
||||
QVector<QLine> green_lines(green_val_.size());
|
||||
QVector<QLine> blue_lines(blue_val_.size());
|
||||
|
||||
for (int i=0;i<red_lines.size();i++) {
|
||||
red_lines.replace(i, QLine(i, height() * (1.0 - red_val_.at(i)), i, height()));
|
||||
green_lines.replace(i, QLine(i, height() * (1.0 - green_val_.at(i)), i, height()));
|
||||
blue_lines.replace(i, QLine(i, height() * (1.0 - blue_val_.at(i)), i, height()));
|
||||
}
|
||||
|
||||
QPainter p(this);
|
||||
p.setCompositionMode(QPainter::CompositionMode_Plus);
|
||||
|
||||
p.setPen(Qt::red);
|
||||
p.drawLines(red_lines);
|
||||
|
||||
p.setPen(Qt::green);
|
||||
p.drawLines(green_lines);
|
||||
|
||||
p.setPen(Qt::blue);
|
||||
p.drawLines(blue_lines);
|
||||
}
|
||||
|
||||
void HistogramScope::resizeEvent(QResizeEvent *e)
|
||||
{
|
||||
QOpenGLWidget::resizeEvent(e);
|
||||
|
||||
if (buffer_){
|
||||
StartUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
void HistogramScope::StartUpdate()
|
||||
{
|
||||
worker_.QueueNext(*buffer_, width());
|
||||
}
|
||||
|
||||
HistogramScopeWorker::HistogramScopeWorker() :
|
||||
cancelled_(false)
|
||||
{
|
||||
}
|
||||
|
||||
void HistogramScopeWorker::run()
|
||||
{
|
||||
while (!cancelled_) {
|
||||
next_lock_.lock();
|
||||
while (!next_.is_allocated()) {
|
||||
next_wait_.wait(&next_lock_);
|
||||
|
||||
if (cancelled_){
|
||||
next_lock_.unlock();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Copy values
|
||||
Frame f = next_;
|
||||
int w = next_width_;
|
||||
next_.destroy();
|
||||
|
||||
next_lock_.unlock();
|
||||
|
||||
QVector<int> data(w * kRGBChannels, 0);
|
||||
|
||||
int max_w = w-1;
|
||||
|
||||
for (int x=0;x<f.width();x++) {
|
||||
for (int y=0;y<f.height();y++) {
|
||||
Color c = f.get_pixel(x, y);
|
||||
|
||||
data[qFloor(clamp(c.red(), 0.0f, 1.0f) * max_w)]++;
|
||||
data[qFloor(clamp(c.green(), 0.0f, 1.0f) * max_w) + w]++;
|
||||
data[qFloor(clamp(c.blue(), 0.0f, 1.0f) * max_w) + w * 2]++;
|
||||
}
|
||||
}
|
||||
|
||||
int max_val = 0;
|
||||
|
||||
foreach (const int& i, data) {
|
||||
if (i > max_val) {
|
||||
max_val = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (!max_val) {
|
||||
// Prevent divide by zero
|
||||
return;
|
||||
}
|
||||
|
||||
QVector<double> red_lines(w);
|
||||
QVector<double> green_lines(w);
|
||||
QVector<double> blue_lines(w);
|
||||
|
||||
for (int i=0;i<w;i++) {
|
||||
red_lines.replace(i, static_cast<double>(data.at(i)) / static_cast<double>(max_val));
|
||||
green_lines.replace(i, static_cast<double>(data.at(i + w)) / static_cast<double>(max_val));
|
||||
blue_lines.replace(i, static_cast<double>(data.at(i + w * 2)) / static_cast<double>(max_val));
|
||||
}
|
||||
|
||||
emit Finished(red_lines, green_lines, blue_lines);
|
||||
}
|
||||
}
|
||||
|
||||
void HistogramScopeWorker::QueueNext(const Frame &f, int width)
|
||||
{
|
||||
next_lock_.lock();
|
||||
|
||||
next_ = f;
|
||||
next_width_ = width;
|
||||
|
||||
next_wait_.wakeOne();
|
||||
|
||||
next_lock_.unlock();
|
||||
}
|
||||
|
||||
void HistogramScopeWorker::Cancel()
|
||||
{
|
||||
cancelled_ = true;
|
||||
next_lock_.lock();
|
||||
next_wait_.wakeOne();
|
||||
next_lock_.unlock();
|
||||
}
|
||||
|
||||
OLIVE_NAMESPACE_EXIT
|
||||
|
||||
@@ -21,27 +21,74 @@
|
||||
#ifndef HISTOGRAMSCOPE_H
|
||||
#define HISTOGRAMSCOPE_H
|
||||
|
||||
#include <QMutex>
|
||||
#include <QOpenGLWidget>
|
||||
#include <QThread>
|
||||
#include <QWaitCondition>
|
||||
|
||||
#include "codec/frame.h"
|
||||
|
||||
OLIVE_NAMESPACE_ENTER
|
||||
|
||||
class HistogramScopeWorker : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
HistogramScopeWorker();
|
||||
|
||||
virtual void run() override;
|
||||
|
||||
// Thread-safe
|
||||
void QueueNext(const Frame& f, int width);
|
||||
|
||||
void Cancel();
|
||||
|
||||
signals:
|
||||
void Finished(QVector<double> red, QVector<double> green, QVector<double> blue);
|
||||
|
||||
private:
|
||||
QAtomicInt cancelled_;
|
||||
|
||||
QMutex next_lock_;
|
||||
QWaitCondition next_wait_;
|
||||
Frame next_;
|
||||
int next_width_;
|
||||
|
||||
};
|
||||
|
||||
class HistogramScope : public QOpenGLWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
HistogramScope(QWidget* parent = nullptr);
|
||||
|
||||
virtual ~HistogramScope() override;
|
||||
|
||||
public slots:
|
||||
void SetBuffer(Frame* frame);
|
||||
|
||||
protected:
|
||||
// virtual void paintEvent(QPaintEvent* e) override;
|
||||
virtual void paintGL() override;
|
||||
|
||||
virtual void resizeEvent(QResizeEvent* e) override;
|
||||
|
||||
private:
|
||||
void StartUpdate();
|
||||
|
||||
Frame* buffer_;
|
||||
|
||||
QVector<double> red_val_;
|
||||
|
||||
QVector<double> green_val_;
|
||||
|
||||
QVector<double> blue_val_;
|
||||
|
||||
HistogramScopeWorker worker_;
|
||||
|
||||
private slots:
|
||||
void FinishedProcessing(QVector<double> red, QVector<double> green, QVector<double> blue);
|
||||
|
||||
};
|
||||
|
||||
OLIVE_NAMESPACE_EXIT
|
||||
|
||||
Reference in New Issue
Block a user