]> git.sesse.net Git - kdenlive/blobdiff - src/colorpickerwidget.cpp
Add settings to new geometry widget:
[kdenlive] / src / colorpickerwidget.cpp
index 44ae3f0c12658b7893e70efc5cdb4ffe54d877f4..acd1d26982a8be5378c146f2395f5582abaa6b66 100644 (file)
@@ -59,8 +59,8 @@ ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
 {
 #ifdef Q_WS_X11
     m_filter = 0;
+    m_image = NULL;
 #endif
-    m_image = 0;
 
     QHBoxLayout *layout = new QHBoxLayout(this);
 
@@ -92,10 +92,17 @@ ColorPickerWidget::~ColorPickerWidget()
 QColor ColorPickerWidget::averagePickedColor(const QPoint pos)
 {
     int size = m_size->value();
-    int x0 = qMax(0, pos.x() - size / 2); 
+    int x0 = qMax(0, pos.x() - size / 2);
     int y0 = qMax(0, pos.y() - size / 2);
     int x1 = qMin(qApp->desktop()->geometry().width(), pos.x() + size / 2);
     int y1 = qMin(qApp->desktop()->geometry().height(), pos.y() + size / 2);
+
+    // take care of loss when dividing odd sizes
+    if (size % 2 != 0) {
+        if (x1 < qApp->desktop()->geometry().width()) ++x1;
+        if (y1 < qApp->desktop()->geometry().height()) ++y1;
+    }
+
     int numPixel = (x1 - x0) * (y1 - y0);
 
     int sumR = 0;
@@ -118,8 +125,8 @@ QColor ColorPickerWidget::averagePickedColor(const QPoint pos)
     m_image = QPixmap::grabWindow(desktop->winId(), x0, y0, x1 - x0, y1 - y0).toImage();
 #endif
 
-    for (int i = x0; i <= x1; ++i) {
-        for (int j = y0; j <= y1; ++j) {
+    for (int i = x0; i < x1; ++i) {
+        for (int j = y0; j < y1; ++j) {
             QColor color;
             color = grabColor(QPoint(i - x0, j - y0), false);
             sumR += color.red();
@@ -134,8 +141,8 @@ QColor ColorPickerWidget::averagePickedColor(const QPoint pos)
 
 #ifdef Q_WS_X11
     XDestroyImage(m_image);
+    m_image = NULL;
 #endif
-    m_image = 0;
 
     if (size > 200)
         emit displayMessage(i18n("Calculated average color for rectangle."), -1);
@@ -217,7 +224,7 @@ QColor ColorPickerWidget::grabColor(const QPoint &p, bool destroyImage)
     if( !qApp->desktop()->geometry().contains( p ))
         return QColor();
     unsigned long xpixel;
-    if (m_image == 0) {
+    if (m_image == NULL) {
         Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
         m_image = XGetImage(QX11Info::display(), root, p.x(), p.y(), 1, 1, -1, ZPixmap);
         xpixel = XGetPixel(m_image, 0, 0);
@@ -236,7 +243,7 @@ QColor ColorPickerWidget::grabColor(const QPoint &p, bool destroyImage)
                 &xcol);
     return QColor::fromRgbF(xcol.red / 65535.0, xcol.green / 65535.0, xcol.blue / 65535.0);
 #else
-    if (m_image == 0) {
+    if (m_image.isNull()) {
         QWidget *desktop = QApplication::desktop();
         QPixmap pm = QPixmap::grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
         QImage i = pm.toImage();