tahoma2d/toonz/sources/toonzqt/pickrgbutils.cpp

110 lines
3.1 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "tgl.h"
2016-06-15 18:43:10 +12:00
// Qt includes
2016-03-19 06:57:51 +13:00
#include <QPixmap>
#include <QImage>
#include <QOpenGLWidget>
2016-03-19 06:57:51 +13:00
#include <QDesktopWidget>
#include <QApplication>
#include "toonzqt/pickrgbutils.h"
//*************************************************************************************
// Local namespace
//*************************************************************************************
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
QRgb meanColor(const QImage &img, const QRect &rect) {
if (rect.width() == 1 && rect.height() == 1)
return img.pixel(rect.left(), rect.top());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int r, g, b, m;
int x, y, x1 = rect.right(), y1 = rect.bottom(),
count = rect.width() * rect.height();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
r = g = b = m = 0;
for (y = rect.top(); y <= y1; ++y)
for (x = rect.left(); x <= x1; ++x) {
QRgb color = img.pixel(x, y);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
r += (color & 0xff);
g += ((color >> 8) & 0xff);
b += ((color >> 16) & 0xff);
m += ((color >> 24) & 0xff);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
r = r / count;
g = g / count;
b = b / count;
m = m / count;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return r | (g << 8) | (b << 16) | (m << 24);
2016-03-19 06:57:51 +13:00
}
}
//==============================================================================
2016-06-15 18:43:10 +12:00
QRgb pickRGB(QWidget *widget, const QRect &rect) {
QImage img(QPixmap::grabWidget(widget, rect.x(), rect.y(), rect.width(),
rect.height())
.toImage());
return meanColor(img, img.rect());
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
QRgb pickScreenRGB(const QRect &rect) {
QWidget *widget = QApplication::desktop();
2016-03-19 06:57:51 +13:00
#ifdef MACOSX
2016-06-15 18:43:10 +12:00
// #Bugzilla 6514, possibly related to #QTBUG 23516
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Screen grabbing on a secondary screen seems to be broken on MAC
// (see what happens using PixelTool), when no piece of the *primary*
// screen is involved in the grab. As it seems to be very Qt-based,
// the workaround is to trivially grab the smallest rect including the
// requested one and a part of the primary screen.
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const QRect &screen0Geom = QApplication::desktop()->screenGeometry(0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int left = std::min(rect.left(), screen0Geom.right());
int top = std::min(rect.top(), screen0Geom.bottom());
int right = std::max(rect.right(), screen0Geom.left());
int bottom = std::max(rect.bottom(), screen0Geom.right());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
QRect theRect(QPoint(left, top), QPoint(right, bottom));
2016-03-19 06:57:51 +13:00
#else
2016-06-15 18:43:10 +12:00
const QRect &theRect = rect;
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
QImage img(QPixmap::grabWindow(widget->winId(), theRect.x(), theRect.y(),
theRect.width(), theRect.height())
.toImage());
return meanColor(
img, QRect(rect.left() - theRect.left(), rect.top() - theRect.top(),
rect.width(), rect.height()));
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
QRgb pickRGB(QOpenGLWidget *widget, const QRect &rect) {
widget->makeCurrent();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
glReadBuffer(GL_FRONT);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
QImage img(rect.size(), QImage::Format_ARGB32);
glReadPixels(rect.x(), widget->height() - rect.y(), rect.width(),
rect.height(), GL_BGRA_EXT, GL_UNSIGNED_BYTE, img.bits());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
widget->doneCurrent();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return meanColor(img, img.rect());
2016-03-19 06:57:51 +13:00
}