Added rounding code to the C++ port as well.
authordswitkin@google.com <dswitkin@google.com@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Thu, 26 Aug 2010 14:13:33 +0000 (14:13 +0000)
committerdswitkin@google.com <dswitkin@google.com@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Thu, 26 Aug 2010 14:13:33 +0000 (14:13 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@1565 59b500cc-1b3d-0410-9834-0bbf25fbcc57

cpp/magick/src/MagickBitmapSource.cpp

index fd2980a..f805487 100644 (file)
@@ -52,7 +52,9 @@ unsigned char* MagickBitmapSource::getRow(int y, unsigned char* row) {
   for (int x = 0; x < width; x++) {
     const PixelPacket* p = pixel_cache + y * width + x;
     // We assume 16 bit values here
-    row[x] = (unsigned char)((306 * ((int)p->red >> 8) + 601 * ((int)p->green >> 8) + 117 * ((int)p->blue >> 8)) >> 10);
+    // 0x200 = 1<<9, half an lsb of the result to force rounding
+    row[x] = (unsigned char)((306 * ((int)p->red >> 8) + 601 * ((int)p->green >> 8) +
+        117 * ((int)p->blue >> 8) + 0x200) >> 10);
   }
   return row;
 
@@ -67,7 +69,8 @@ unsigned char* MagickBitmapSource::getMatrix() {
   const Magick::PixelPacket* p = pixel_cache;
   for (int y = 0; y < height; y++) {
     for (int x = 0; x < width; x++) {
-      *m = (unsigned char)((306 * ((int)p->red >> 8) + 601 * ((int)p->green >> 8) + 117 * ((int)p->blue >> 8)) >> 10);
+      *m = (unsigned char)((306 * ((int)p->red >> 8) + 601 * ((int)p->green >> 8) +
+          117 * ((int)p->blue >> 8) + 0x200) >> 10);
       m++;
       p++;
     }