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