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