}
}
+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_;
}
*/
#include <zxing/common/Counted.h>
+#include <zxing/common/BitArray.h>
#include <limits>
namespace zxing {
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;
}
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
#include "BitMatrixTest.h"
#include <limits>
+#include <stdlib.h>
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);
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));
+ }
+ }
+}
}
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);
};
}