]> git.sesse.net Git - nageru/blobdiff - analyzer.cpp
Make it possible to hover over the grabbed image to get out RGB values.
[nageru] / analyzer.cpp
index eb1f5170be9b617ffe69173988e1f87b7ce812af..08de86fd535a520376a4413ebed0b3a38a236336 100644 (file)
@@ -1,6 +1,7 @@
 #include "analyzer.h"
 
 #include <QDialogButtonBox>
+#include <QMouseEvent>
 #include <QSurface>
 
 #include <movit/resource_pool.h>
@@ -14,7 +15,8 @@
 using namespace std;
 
 Analyzer::Analyzer()
-       : ui(new Ui::Analyzer)
+       : ui(new Ui::Analyzer),
+         grabbed_image(global_flags.width, global_flags.height, QImage::Format_ARGB32_Premultiplied)
 {
        ui->setupUi(this);
 
@@ -32,6 +34,7 @@ Analyzer::Analyzer()
        connect(ui->grab_btn, &QPushButton::clicked, bind(&Analyzer::grab_clicked, this));
        connect(ui->input_box, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), bind(&Analyzer::signal_changed, this));
        signal_changed();
+       ui->grabbed_frame_label->installEventFilter(this);
 
        surface = create_surface(QSurfaceFormat::defaultFormat());
        context = create_context(surface);
@@ -107,14 +110,13 @@ void Analyzer::grab_clicked()
        unsigned char *buf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
        check_error();
 
-       QImage img(global_flags.width, global_flags.height, QImage::Format_ARGB32_Premultiplied);
        size_t pitch = global_flags.width * 4;
        for (int y = 0; y < global_flags.height; ++y) {
-               memcpy(img.scanLine(global_flags.height - y - 1), buf + y * pitch, pitch);
+               memcpy(grabbed_image.scanLine(global_flags.height - y - 1), buf + y * pitch, pitch);
        }
 
        QPixmap pixmap;
-       pixmap.convertFromImage(QImage(img));
+       pixmap.convertFromImage(grabbed_image);
        ui->grabbed_frame_label->setPixmap(pixmap);
 
        int r_hist[256] = {0}, g_hist[256] = {0}, b_hist[256] = {0};
@@ -158,3 +160,27 @@ void Analyzer::signal_changed()
        Mixer::Output channel = static_cast<Mixer::Output>(ui->input_box->currentData().value<int>());
        ui->display->set_output(channel);
 }
+
+bool Analyzer::eventFilter(QObject *watched, QEvent *event)
+{
+       if (event->type() == QEvent::MouseMove &&
+           watched->isWidgetType()) {
+               const QMouseEvent *mouse_event = (QMouseEvent *)event;
+               const QPixmap *pixmap = ui->grabbed_frame_label->pixmap();
+               if (pixmap != nullptr) {
+                       int x = lrint(mouse_event->x() * double(pixmap->width()) / ui->grabbed_frame_label->width());
+                       int y = lrint(mouse_event->y() * double(pixmap->height()) / ui->grabbed_frame_label->height());
+                       x = std::min(x, pixmap->width() - 1);
+                       y = std::min(y, pixmap->height() - 1);
+                       QRgb pixel = grabbed_image.pixel(x, y);
+                       ui->red_label->setText(QString::fromStdString(to_string(qRed(pixel))));
+                       ui->green_label->setText(QString::fromStdString(to_string(qGreen(pixel))));
+                       ui->blue_label->setText(QString::fromStdString(to_string(qBlue(pixel))));
+
+                       char buf[256];
+                       snprintf(buf, sizeof(buf), "#%02x%02x%02x", qRed(pixel), qGreen(pixel), qBlue(pixel));
+                       ui->hex_label->setText(buf);
+               }
+        }
+        return false;
+}