From cdcad8e2af288e66a22a247235bc0f6081e95215 Mon Sep 17 00:00:00 2001 From: flyashi Date: Fri, 20 Aug 2010 20:24:20 +0000 Subject: [PATCH] GreyscaleRotatedLuminanceSource: implemented getMatrix() 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 | 33 +++++++++++++++ cpp/core/src/zxing/common/BitMatrix.h | 2 + .../GreyscaleRotatedLuminanceSource.cpp | 10 ++++- cpp/core/tests/src/common/BitMatrixTest.cpp | 40 +++++++++++++++++++ cpp/core/tests/src/common/BitMatrixTest.h | 8 ++++ 5 files changed, 91 insertions(+), 2 deletions(-) diff --git a/cpp/core/src/zxing/common/BitMatrix.cpp b/cpp/core/src/zxing/common/BitMatrix.cpp index 8c137f2a..6fea1049 100644 --- a/cpp/core/src/zxing/common/BitMatrix.cpp +++ b/cpp/core/src/zxing/common/BitMatrix.cpp @@ -113,6 +113,39 @@ void BitMatrix::setRegion(size_t left, size_t top, size_t width, size_t height) } } +Ref BitMatrix::getRow(int y, Ref 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::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_; } diff --git a/cpp/core/src/zxing/common/BitMatrix.h b/cpp/core/src/zxing/common/BitMatrix.h index 33ed8f4f..2b1267b7 100644 --- a/cpp/core/src/zxing/common/BitMatrix.h +++ b/cpp/core/src/zxing/common/BitMatrix.h @@ -21,6 +21,7 @@ */ #include +#include #include 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); + Ref getRow(int y, Ref row); size_t getDimension() const; size_t getWidth() const; diff --git a/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp b/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp index 4e3aec81..25c09db9 100644 --- a/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp +++ b/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp @@ -55,8 +55,14 @@ unsigned char* GreyscaleRotatedLuminanceSource::getRow(int y, unsigned char* row } 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 diff --git a/cpp/core/tests/src/common/BitMatrixTest.cpp b/cpp/core/tests/src/common/BitMatrixTest.cpp index c75fa149..c2488a96 100644 --- a/cpp/core/tests/src/common/BitMatrixTest.cpp +++ b/cpp/core/tests/src/common/BitMatrixTest.cpp @@ -20,12 +20,17 @@ #include "BitMatrixTest.h" #include +#include namespace zxing { using namespace std; CPPUNIT_TEST_SUITE_REGISTRATION(BitMatrixTest); +BitMatrixTest::BitMatrixTest() { + srand(getpid()); +} + void BitMatrixTest::testGetSet() { size_t bits = numeric_limits::digits; BitMatrix matrix(bits + 1); @@ -64,4 +69,39 @@ void BitMatrixTest::testGetBits() { 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 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)); + } + } +} } diff --git a/cpp/core/tests/src/common/BitMatrixTest.h b/cpp/core/tests/src/common/BitMatrixTest.h index b94c235a..c5841d95 100644 --- a/cpp/core/tests/src/common/BitMatrixTest.h +++ b/cpp/core/tests/src/common/BitMatrixTest.h @@ -30,16 +30,24 @@ class BitMatrixTest : public CPPUNIT_NS::TestFixture { CPPUNIT_TEST(testGetSet); CPPUNIT_TEST(testSetRegion); CPPUNIT_TEST(testGetBits); + CPPUNIT_TEST(testGetRow1); + CPPUNIT_TEST(testGetRow2); + CPPUNIT_TEST(testGetRow3); CPPUNIT_TEST_SUITE_END(); public: + BitMatrixTest(); protected: void testGetSet(); void testSetRegion(); void testGetBits(); + void testGetRow1(); + void testGetRow2(); + void testGetRow3(); private: + void runBitMatrixGetRowTest(int width, int height); }; } -- 2.20.1