GreyscaleRotatedLuminanceSource: implemented getMatrix()
authorflyashi <flyashi@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Fri, 20 Aug 2010 20:24:20 +0000 (20:24 +0000)
committerflyashi <flyashi@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Fri, 20 Aug 2010 20:24:20 +0000 (20:24 +0000)
BitMatrix: implemented getRow

git-svn-id: http://zxing.googlecode.com/svn/trunk@1551 59b500cc-1b3d-0410-9834-0bbf25fbcc57

cpp/core/src/zxing/common/BitMatrix.cpp
cpp/core/src/zxing/common/BitMatrix.h
cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp
cpp/core/tests/src/common/BitMatrixTest.cpp
cpp/core/tests/src/common/BitMatrixTest.h

index 8c137f2..6fea104 100644 (file)
@@ -113,6 +113,39 @@ void BitMatrix::setRegion(size_t left, size_t top, size_t width, size_t height)
   }
 }
 
   }
 }
 
+Ref<BitArray> BitMatrix::getRow(int y, Ref<BitArray> row) {
+  if (row.empty() || row->getSize() < width_) {
+    row = new BitArray(width_);
+  } else {
+    row->clear();
+  }
+  size_t start = y * width_;
+  size_t end = start + width_ - 1; // end is inclusive
+  size_t firstWord = start >> logBits;
+  size_t lastWord = end >> logBits;
+  size_t bitOffset = start & bitsMask;
+  for (size_t i = firstWord; i <= lastWord; i++) {
+    size_t firstBit = i > firstWord ? 0 : start & bitsMask;
+    size_t lastBit = i < lastWord ? bitsPerWord - 1 : end & bitsMask;
+    unsigned int mask;
+    if (firstBit == 0 && lastBit == logBits) {
+      mask = numeric_limits<unsigned int>::max();
+    } else {
+      mask = 0;
+      for (size_t j = firstBit; j <= lastBit; j++) {
+        mask |= 1 << j;
+      }
+    }
+    row->setBulk((i - firstWord) << logBits, (bits_[i] & mask) >> bitOffset);
+    if (firstBit == 0 && bitOffset != 0) {
+      unsigned int prevBulk = row->getBitArray()[i - firstWord - 1];
+      prevBulk |= (bits_[i] & mask) << (bitsPerWord - bitOffset);
+      row->setBulk((i - firstWord - 1) << logBits, prevBulk);
+    }
+  }
+  return row;
+}
+
 size_t BitMatrix::getWidth() const {
   return width_;
 }
 size_t BitMatrix::getWidth() const {
   return width_;
 }
index 33ed8f4..2b1267b 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include <zxing/common/Counted.h>
  */
 
 #include <zxing/common/Counted.h>
+#include <zxing/common/BitArray.h>
 #include <limits>
 
 namespace zxing {
 #include <limits>
 
 namespace zxing {
@@ -43,6 +44,7 @@ public:
   void flip(size_t x, size_t y);
   void clear();
   void setRegion(size_t left, size_t top, size_t width, size_t height);
   void flip(size_t x, size_t y);
   void clear();
   void setRegion(size_t left, size_t top, size_t width, size_t height);
+  Ref<BitArray> getRow(int y, Ref<BitArray> row);
 
   size_t getDimension() const;
   size_t getWidth() const;
 
   size_t getDimension() const;
   size_t getWidth() const;
index 4e3aec8..25c09db 100644 (file)
@@ -55,8 +55,14 @@ unsigned char* GreyscaleRotatedLuminanceSource::getRow(int y, unsigned char* row
 }
 
 unsigned char* GreyscaleRotatedLuminanceSource::getMatrix() {
 }
 
 unsigned char* GreyscaleRotatedLuminanceSource::getMatrix() {
-  // FIXME(flyashi): fine for 1D scanning, need to implement for 2D scanning
-  return NULL;
+  unsigned char* result = new unsigned char[width_ * height_];
+  unsigned char* row = new unsigned char[width_];
+  for (int y = 0; y < height_; y++) {
+    row = getRow(y, row);
+    memcpy(result + y * width_, row, width_);
+  }
+  delete [] row;
+  return result;
 }
 
 } // namespace
 }
 
 } // namespace
index c75fa14..c2488a9 100644 (file)
 
 #include "BitMatrixTest.h"
 #include <limits>
 
 #include "BitMatrixTest.h"
 #include <limits>
+#include <stdlib.h>
 
 namespace zxing {
 using namespace std;
 
 CPPUNIT_TEST_SUITE_REGISTRATION(BitMatrixTest);
 
 
 namespace zxing {
 using namespace std;
 
 CPPUNIT_TEST_SUITE_REGISTRATION(BitMatrixTest);
 
+BitMatrixTest::BitMatrixTest() {
+  srand(getpid());
+}
+
 void BitMatrixTest::testGetSet() {
   size_t bits = numeric_limits<unsigned int>::digits;
   BitMatrix matrix(bits + 1);
 void BitMatrixTest::testGetSet() {
   size_t bits = numeric_limits<unsigned int>::digits;
   BitMatrix matrix(bits + 1);
@@ -64,4 +69,39 @@ void BitMatrixTest::testGetBits() {
   CPPUNIT_ASSERT_EQUAL(8u, bits[1]);
 }
 
   CPPUNIT_ASSERT_EQUAL(8u, bits[1]);
 }
 
+void BitMatrixTest::testGetRow1() {
+  const int width = 98;
+  const int height = 76;
+  runBitMatrixGetRowTest(width, height);
+}
+
+void BitMatrixTest::testGetRow2() {
+  const int width = 320;
+  const int height = 320;
+  runBitMatrixGetRowTest(width, height);
+}
+
+void BitMatrixTest::testGetRow3() {
+  const int width = 17;
+  const int height = 23;
+  runBitMatrixGetRowTest(width, height);
+}
+
+void BitMatrixTest::runBitMatrixGetRowTest(int width, int height) {
+  BitMatrix mat(width, height);
+  for (int y = 0; y < height; y++) {
+    for (int x = 0; x < width; x++) {
+      if ((rand() & 0x01) != 0) {
+        mat.set(x, y);
+      }
+    }
+  }
+  Ref<BitArray> row(new BitArray(width));
+  for (int y = 0; y < height; y++) {
+    row = mat.getRow(y, row);
+    for (int x = 0; x < width; x++) {
+      CPPUNIT_ASSERT_EQUAL(row->get(x), mat.get(x,y));
+    }
+  }
+}
 }
 }
index b94c235..c5841d9 100644 (file)
@@ -30,16 +30,24 @@ class BitMatrixTest : public CPPUNIT_NS::TestFixture {
   CPPUNIT_TEST(testGetSet);
   CPPUNIT_TEST(testSetRegion);
   CPPUNIT_TEST(testGetBits);
   CPPUNIT_TEST(testGetSet);
   CPPUNIT_TEST(testSetRegion);
   CPPUNIT_TEST(testGetBits);
+  CPPUNIT_TEST(testGetRow1);
+  CPPUNIT_TEST(testGetRow2);
+  CPPUNIT_TEST(testGetRow3);
   CPPUNIT_TEST_SUITE_END();
 
 public:
   CPPUNIT_TEST_SUITE_END();
 
 public:
+  BitMatrixTest();
 
 protected:
   void testGetSet();
   void testSetRegion();
   void testGetBits();
 
 protected:
   void testGetSet();
   void testSetRegion();
   void testGetBits();
+  void testGetRow1();
+  void testGetRow2();
+  void testGetRow3();
 
 private:
 
 private:
+  void runBitMatrixGetRowTest(int width, int height);
 };
 }
 
 };
 }