GreyscaleRotatedLuminanceSource: implemented getMatrix()

BitMatrix: implemented getRow


git-svn-id: https://zxing.googlecode.com/svn/trunk@1551 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
flyashi 2010-08-20 20:24:20 +00:00
parent 124ef03ca9
commit ca1cf153d6
5 changed files with 91 additions and 2 deletions

View 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_;
}

View file

@ -21,6 +21,7 @@
*/
#include <zxing/common/Counted.h>
#include <zxing/common/BitArray.h>
#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);
Ref<BitArray> getRow(int y, Ref<BitArray> row);
size_t getDimension() const;
size_t getWidth() const;

View file

@ -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

View file

@ -20,12 +20,17 @@
#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);
@ -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<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));
}
}
}
}

View file

@ -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);
};
}