histogram: implemented histogram widget
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user