]> git.sesse.net Git - nageru/commitdiff
Make it possible to hover over the grabbed image to get out RGB values.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 9 May 2017 07:37:26 +0000 (09:37 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 9 May 2017 07:37:26 +0000 (09:37 +0200)
analyzer.cpp
analyzer.h
ui_analyzer.ui

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;
+}
index 3cefdc90991969c990e1385a93c6738e463aad99..68fb14e608511c4a88e7817ccef689644c59ba57 100644 (file)
@@ -2,6 +2,7 @@
 #define _ANALYZER_H 1
 
 #include <QDialog>
+#include <QImage>
 #include <QString>
 
 #include <epoxy/gl.h>
@@ -29,12 +30,14 @@ public:
 private:
        void grab_clicked();
        void signal_changed();
+       bool eventFilter(QObject *watched, QEvent *event) override;
 
        Ui::Analyzer *ui;
        QSurface *surface;
        QOpenGLContext *context;
        GLuint pbo;
        movit::ResourcePool *resource_pool = nullptr;
+       QImage grabbed_image;
 };
 
 #endif  // !defined(_ANALYZER_H)
index 0b46e1449f24737e022cb64556b3889bc5b7c4f6..ad50d3585595922768bc8e084bc8c15907b87d66 100644 (file)
      </widget>
     </item>
     <item row="1" column="1">
-     <widget class="QLabel" name="label_9">
+     <widget class="QLabel" name="green_label">
       <property name="text">
        <string>127</string>
       </property>
      </widget>
     </item>
     <item row="2" column="1">
-     <widget class="QLabel" name="label_10">
+     <widget class="QLabel" name="blue_label">
       <property name="text">
        <string>127</string>
       </property>
      </widget>
     </item>
     <item row="0" column="1">
-     <widget class="QLabel" name="label_8">
+     <widget class="QLabel" name="red_label">
       <property name="text">
        <string>127</string>
       </property>
      </widget>
     </item>
     <item row="3" column="1">
-     <widget class="QLabel" name="label_11">
+     <widget class="QLabel" name="hex_label">
       <property name="text">
        <string>#7f7f7f</string>
       </property>
      <height>270</height>
     </rect>
    </property>
+   <property name="cursor">
+    <cursorShape>CrossCursor</cursorShape>
+   </property>
+   <property name="mouseTracking">
+    <bool>true</bool>
+   </property>
    <property name="autoFillBackground">
     <bool>false</bool>
    </property>