$OpenBSD$
ISSUE: http://code.google.com/p/yagf/issues/detail?id=45
--- src/core/qipblackandwhiteimage.cpp.orig	Wed Jan 28 18:02:16 2015
+++ src/core/qipblackandwhiteimage.cpp	Thu Jul 30 15:24:04 2015
@@ -81,7 +81,7 @@ bool QIPBlackAndWhiteImage::compareElements(quint8 **s
 
 IntRect QIPBlackAndWhiteImage::cropInternal(bool upperLeft) const
 {
-    IntRect result;
+    IntRect result = { 0, 0, w, h };
     if (upperLeft) {
         for (int y = 0; y < h; y++) {
             quint32 stepCount = 0;
@@ -146,9 +146,16 @@ IntRect QIPBlackAndWhiteImage::cropInternal(bool upper
 
 void QIPBlackAndWhiteImage::copyInternal(quint8 * data, int x1, int x2, int y1, int y2) const
 {
+    Q_ASSERT(x1 >= 0);
+    Q_ASSERT(x1 <  x2);
+    Q_ASSERT(x2 <= width());
+    Q_ASSERT(y1 >= 0);
+    Q_ASSERT(y1 <  y2);
+    Q_ASSERT(y2 <= height());
+
     int wr = x2 - x1;
     for (int y = y1; y < y2; y++)
-        memcpy(&(data[wr*(y-y1)]), &(scanLine(y)[x1]), wr);
+        memmove(&(data[wr*(y-y1)]), &(scanLine(y)[x1]), wr);
 }
 
 QImage QIPBlackAndWhiteImage::toImage() const
@@ -171,6 +178,13 @@ QImage QIPBlackAndWhiteImage::toImage() const
 
 QIPBlackAndWhiteImage QIPBlackAndWhiteImage::copy(quint32 x1, quint32 x2, quint32 y1, quint32 y2) const
 {
+    Q_ASSERT(x1 >= 0);
+    Q_ASSERT(x1 <  x2);
+    Q_ASSERT(x2 <= width());
+    Q_ASSERT(y1 >= 0);
+    Q_ASSERT(y1 <  y2);
+    Q_ASSERT(y2 <= height());
+
     QIPBlackAndWhiteImage result(x2-x1, y2-y1);
 #ifndef IPRIT_MULTITHREADING
     copyInternal(result.data.data(), x1, x2, y1, y2);
@@ -186,12 +200,12 @@ QIPBlackAndWhiteImage QIPBlackAndWhiteImage::copy(quin
     return result;
 }
 
-quint32 QIPBlackAndWhiteImage::width()
+quint32 QIPBlackAndWhiteImage::width() const
 {
     return w;
 }
 
-quint32 QIPBlackAndWhiteImage::height()
+quint32 QIPBlackAndWhiteImage::height() const
 {
     return h;
 }
@@ -360,7 +374,7 @@ QRect QIPBlackAndWhiteImage::cropGrayScaleImage(const 
        // QIPGrayscaleImage tmp = image->copy(r1.x1, r2.x2, r1.y1, r2.y2);
        // delete *image;
        // (*image) = tmp;
-        return QRect(r1.x1,r1.y1, r2.x2-r1.x1,r2.y2-r2.y1);
+        return QRect(r1.x1,r1.y1, r2.x2-r1.x1,r2.y2-r1.y1);
 }
 
 
