Added rounding code to the C++ port as well.
[zxing.git] / cpp / magick / src / MagickBitmapSource.cpp
index 2008599..f805487 100644 (file)
@@ -2,8 +2,7 @@
  *  MagickBitmapSource.cpp
  *  zxing
  *
- *  Created by Ralf Kistner on 16/10/2009.
- *  Copyright 2008 ZXing authors All rights reserved.
+ *  Copyright 2010 ZXing authors All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,6 +23,8 @@
 
 using namespace Magick;
 
+namespace zxing {
+
 MagickBitmapSource::MagickBitmapSource(Image& image) : image_(image) {
   width = image.columns();
   height = image.rows();
@@ -35,25 +36,32 @@ MagickBitmapSource::~MagickBitmapSource() {
 
 }
 
-int MagickBitmapSource::getWidth() {
+int MagickBitmapSource::getWidth() const {
   return width;
 }
 
-int MagickBitmapSource::getHeight() {
+int MagickBitmapSource::getHeight() const {
   return height;
 }
 
-unsigned char MagickBitmapSource::getPixel(int x, int y) {
-  const PixelPacket* p = pixel_cache + y * width + x;
-  // We assume 16 bit values here
-
-  //return (unsigned char)((((int)p->red + (int)p->green + (int)p->blue) >> 8) / 3);
+unsigned char* MagickBitmapSource::getRow(int y, unsigned char* row) {
+  int width = getWidth();
+  if (row == NULL) {
+    row = new unsigned char[width];
+  }
+  for (int x = 0; x < width; x++) {
+    const PixelPacket* p = pixel_cache + y * width + x;
+    // We assume 16 bit values here
+    // 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;
 
-  return (unsigned char)((306 * ((int)p->red >> 8) + 601 * ((int)p->green >> 8) + 117 * ((int)p->blue >> 8)) >> 10);
 }
 
 /** This is a more efficient implementation. */
-unsigned char* MagickBitmapSource::copyMatrix() {
+unsigned char* MagickBitmapSource::getMatrix() {
   int width = getWidth();
   int height =  getHeight();
   unsigned char* matrix = new unsigned char[width*height];
@@ -61,7 +69,8 @@ unsigned char* MagickBitmapSource::copyMatrix() {
   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++;
     }
@@ -69,3 +78,22 @@ unsigned char* MagickBitmapSource::copyMatrix() {
   return matrix;
 }
 
+bool MagickBitmapSource::isRotateSupported() const {
+  return false;
+}
+
+Ref<LuminanceSource> MagickBitmapSource::rotateCounterClockwise() {
+    //TODO(flyashi): add rotated image support.
+  /* this segfaults. I tried a few things, none seemed to work. Perhaps the problem is elsewhere? */
+  /*
+  Magick::Image rotated(image_);
+  rotated.modifyImage();
+  rotated.rotate(90); // Image::rotate takes CCW degrees as an argument
+  rotated.syncPixels();
+  return Ref<MagickBitmapSource> (new MagickBitmapSource(rotated));
+  */
+  return Ref<MagickBitmapSource> (NULL);
+}
+
+}
+