3 #include <QDialogButtonBox>
8 #include <movit/resource_pool.h>
9 #include <movit/util.h>
14 #include "ui_analyzer.h"
16 // QCustomPlot includes qopenglfunctions.h, which #undefs all of the epoxy
17 // definitions (ugh) and doesn't put back any others (ugh). Add the ones we
20 #define glBindBuffer epoxy_glBindBuffer
21 #define glBindFramebuffer epoxy_glBindFramebuffer
22 #define glBufferData epoxy_glBufferData
23 #define glDeleteBuffers epoxy_glDeleteBuffers
24 #define glDisable epoxy_glDisable
25 #define glGenBuffers epoxy_glGenBuffers
26 #define glGetError epoxy_glGetError
27 #define glReadPixels epoxy_glReadPixels
28 #define glUnmapBuffer epoxy_glUnmapBuffer
29 #define glWaitSync epoxy_glWaitSync
34 : ui(new Ui::Analyzer),
35 grabbed_image(global_flags.width, global_flags.height, QImage::Format_ARGB32_Premultiplied)
39 //connect(ui->button_box, &QDialogButtonBox::accepted, [this]{ this->close(); });
41 ui->input_box->addItem("Live", Mixer::OUTPUT_LIVE);
42 ui->input_box->addItem("Preview", Mixer::OUTPUT_PREVIEW);
43 unsigned num_channels = global_mixer->get_num_channels();
44 for (unsigned channel_idx = 0; channel_idx < num_channels; ++channel_idx) {
45 Mixer::Output channel = static_cast<Mixer::Output>(Mixer::OUTPUT_INPUT0 + channel_idx);
46 string name = global_mixer->get_channel_name(channel);
47 ui->input_box->addItem(QString::fromStdString(name), channel);
50 connect(ui->grab_btn, &QPushButton::clicked, bind(&Analyzer::grab_clicked, this));
51 connect(ui->input_box, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), bind(&Analyzer::signal_changed, this));
53 ui->grabbed_frame_label->installEventFilter(this);
55 surface = create_surface(QSurfaceFormat::defaultFormat());
56 context = create_context(surface);
58 if (!make_current(context, surface)) {
63 glGenBuffers(1, &pbo);
64 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
65 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, global_flags.width * global_flags.height * 4, NULL, GL_STREAM_READ);
67 ui->histogram->xAxis->setVisible(true);
68 ui->histogram->yAxis->setVisible(false);
69 ui->histogram->xAxis->setRange(0, 255);
74 if (!make_current(context, surface)) {
78 glDeleteBuffers(1, &pbo);
80 if (resource_pool != nullptr) {
81 resource_pool->clean_context();
83 delete_context(context);
87 void Analyzer::grab_clicked()
89 Mixer::Output channel = static_cast<Mixer::Output>(ui->input_box->currentData().value<int>());
91 if (!make_current(context, surface)) {
96 Mixer::DisplayFrame frame;
97 if (!global_mixer->get_display_frame(channel, &frame)) {
98 printf("Not ready yet\n");
102 // Set up an FBO to render into.
103 if (resource_pool == nullptr) {
104 resource_pool = frame.chain->get_resource_pool();
106 assert(resource_pool == frame.chain->get_resource_pool());
108 GLuint fbo_tex = resource_pool->create_2d_texture(GL_RGBA8, global_flags.width, global_flags.height);
110 GLuint fbo = resource_pool->create_fbo(fbo_tex);
113 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
117 glDisable(GL_FRAMEBUFFER_SRGB);
119 frame.chain->render_to_fbo(fbo, global_flags.width, global_flags.height);
122 // Read back to memory.
123 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
125 glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
127 glReadPixels(0, 0, global_flags.width, global_flags.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
130 unsigned char *buf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
133 size_t pitch = global_flags.width * 4;
134 for (int y = 0; y < global_flags.height; ++y) {
135 memcpy(grabbed_image.scanLine(global_flags.height - y - 1), buf + y * pitch, pitch);
140 snprintf(buf, sizeof(buf), "Grabbed frame (%dx%d)", global_flags.width, global_flags.height);
141 ui->grabbed_frame_sublabel->setText(buf);
145 pixmap.convertFromImage(grabbed_image);
146 ui->grabbed_frame_label->setPixmap(pixmap);
148 int r_hist[256] = {0}, g_hist[256] = {0}, b_hist[256] = {0};
149 const unsigned char *ptr = buf;
150 for (int i = 0; i < global_flags.height * global_flags.width; ++i) {
161 glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
163 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
165 glBindFramebuffer(GL_FRAMEBUFFER, 0);
168 QVector<double> r_vec(256), g_vec(256), b_vec(256), x_vec(256);
170 for (unsigned i = 0; i < 256; ++i) {
172 r_vec[i] = log(r_hist[i] + 1.0);
173 g_vec[i] = log(g_hist[i] + 1.0);
174 b_vec[i] = log(b_hist[i] + 1.0);
176 max = std::max(max, r_vec[i]);
177 max = std::max(max, g_vec[i]);
178 max = std::max(max, b_vec[i]);
181 ui->histogram->clearGraphs();
182 ui->histogram->addGraph();
183 ui->histogram->graph(0)->setData(x_vec, r_vec);
184 ui->histogram->graph(0)->setPen(QPen(Qt::red));
185 ui->histogram->graph(0)->setBrush(QBrush(QColor(255, 127, 127, 80)));
186 ui->histogram->addGraph();
187 ui->histogram->graph(1)->setData(x_vec, g_vec);
188 ui->histogram->graph(1)->setPen(QPen(Qt::green));
189 ui->histogram->graph(1)->setBrush(QBrush(QColor(127, 255, 127, 80)));
190 ui->histogram->addGraph();
191 ui->histogram->graph(2)->setData(x_vec, b_vec);
192 ui->histogram->graph(2)->setPen(QPen(Qt::blue));
193 ui->histogram->graph(2)->setBrush(QBrush(QColor(127, 127, 255, 80)));
195 ui->histogram->xAxis->setVisible(true);
196 ui->histogram->yAxis->setVisible(false);
197 ui->histogram->xAxis->setRange(0, 255);
198 ui->histogram->yAxis->setRange(0, max);
199 ui->histogram->replot();
201 resource_pool->release_2d_texture(fbo_tex);
203 resource_pool->release_fbo(fbo);
207 void Analyzer::signal_changed()
209 Mixer::Output channel = static_cast<Mixer::Output>(ui->input_box->currentData().value<int>());
210 ui->display->set_output(channel);
213 bool Analyzer::eventFilter(QObject *watched, QEvent *event)
215 if (event->type() == QEvent::MouseMove &&
216 watched->isWidgetType()) {
217 const QMouseEvent *mouse_event = (QMouseEvent *)event;
218 const QPixmap *pixmap = ui->grabbed_frame_label->pixmap();
219 if (pixmap != nullptr) {
220 int x = lrint(mouse_event->x() * double(pixmap->width()) / ui->grabbed_frame_label->width());
221 int y = lrint(mouse_event->y() * double(pixmap->height()) / ui->grabbed_frame_label->height());
222 x = std::min(x, pixmap->width() - 1);
223 y = std::min(y, pixmap->height() - 1);
224 QRgb pixel = grabbed_image.pixel(x, y);
225 ui->red_label->setText(QString::fromStdString(to_string(qRed(pixel))));
226 ui->green_label->setText(QString::fromStdString(to_string(qGreen(pixel))));
227 ui->blue_label->setText(QString::fromStdString(to_string(qBlue(pixel))));
230 snprintf(buf, sizeof(buf), "#%02x%02x%02x", qRed(pixel), qGreen(pixel), qBlue(pixel));
231 ui->hex_label->setText(buf);