Skip to content
This repository has been archived by the owner on Sep 4, 2019. It is now read-only.

Commit

Permalink
Mask_calcRunLength() has been refactored out from Mask_evaluateSymbol…
Browse files Browse the repository at this point in the history
…(). New test code for Mask_calcRunLength has been added.
  • Loading branch information
fukuchi committed Oct 16, 2011
1 parent acfd9a2 commit 296e792
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 41 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
@@ -1,3 +1,9 @@
2011.10.16 Kentaro FUKUCHI <kentaro@fukuchi.org>
* mask.[ch]:
- Mask_calcRunLength() has been refactored out from Mask_evaluateSymbol().
* test/test_mask.c:
- New test code for Mask_calcRunLength has been added.

2011.10.13 Kentaro FUKUCHI <kentaro@fukuchi.org>
* mask.[ch]:
- Mask_calcN2() has been refactored out from Mask_evaluateSymbol().
Expand Down
80 changes: 39 additions & 41 deletions mask.c
Expand Up @@ -185,7 +185,7 @@ unsigned char *Mask_makeMask(int width, unsigned char *frame, int mask, QRecLeve
//static int n3;
//static int n4;

static int Mask_calcN1N3(int length, int *runLength)
__STATIC int Mask_calcN1N3(int length, int *runLength)
{
int i;
int demerit = 0;
Expand Down Expand Up @@ -241,58 +241,56 @@ __STATIC int Mask_calcN2(int width, unsigned char *frame)
return demerit;
}

__STATIC int Mask_calcRunLength(int width, unsigned char *frame, int dir, int *runLength)
{
int head;
int i;
unsigned char *p;
int pitch;

pitch = (dir==0)?1:width;
for(i=0; i<width+1; i++) {
runLength[i] = 0;
}
if(frame[0] & 1) {
runLength[0] = -1;
head = 1;
} else {
head = 0;
}
runLength[head] = 1;
p = frame + pitch;

for(i=1; i<width; i++) {
if((p[0] ^ p[-pitch]) & 1) {
head++;
runLength[head] = 1;
} else {
runLength[head]++;
}
p += pitch;
}

return head + 1;
}

__STATIC int Mask_evaluateSymbol(int width, unsigned char *frame)
{
int x, y;
unsigned char *p;
int head;
int demerit = 0;
int runLength[QRSPEC_WIDTH_MAX + 1];
int length;

demerit += Mask_calcN2(width, frame);

p = frame;
for(y=0; y<width; y++) {
head = 0;
runLength[0] = 1;
for(x=0; x<width; x++) {
if(x == 0 && (p[0] & 1)) {
runLength[0] = -1;
head = 1;
runLength[head] = 1;
} else if(x > 0) {
if((p[0] ^ p[-1]) & 1) {
head++;
runLength[head] = 1;
} else {
runLength[head]++;
}
}
p++;
}
demerit += Mask_calcN1N3(head+1, runLength);
length = Mask_calcRunLength(width, frame + y * width, 0, runLength);
demerit += Mask_calcN1N3(length, runLength);
}

for(x=0; x<width; x++) {
head = 0;
runLength[0] = 1;
p = frame + x;
for(y=0; y<width; y++) {
if(y == 0 && (p[0] & 1)) {
runLength[0] = -1;
head = 1;
runLength[head] = 1;
} else if(y > 0) {
if((p[0] ^ p[-width]) & 1) {
head++;
runLength[head] = 1;
} else {
runLength[head]++;
}
}
p+=width;
}
demerit += Mask_calcN1N3(head+1, runLength);
length = Mask_calcRunLength(width, frame + x, 1, runLength);
demerit += Mask_calcN1N3(length, runLength);
}

return demerit;
Expand Down
1 change: 1 addition & 0 deletions mask.h
Expand Up @@ -27,6 +27,7 @@ extern unsigned char *Mask_mask(int width, unsigned char *frame, QRecLevel level

#ifdef WITH_TESTS
extern int Mask_calcN2(int width, unsigned char *frame);
extern int Mask_calcRunLength(int width, unsigned char *frame, int dir, int *runLength);
extern int Mask_evaluateSymbol(int width, unsigned char *frame);
extern int Mask_writeFormatInformation(int width, unsigned char *frame, int mask, QRecLevel level);
extern unsigned char *Mask_makeMaskedFrame(int width, unsigned char *frame, int mask);
Expand Down
40 changes: 40 additions & 0 deletions tests/test_mask.c
Expand Up @@ -303,6 +303,45 @@ void test_format(void)
testFinish();
}

void test_calcRunLength(void)
{
int width = 5;
unsigned char frame[width * width];
int runLength[width + 1];
int i, j;
int length;
static unsigned char pattern[6][5] = {
{0, 1, 0, 1, 0},
{1, 0, 1, 0, 1},
{0, 0, 0, 0, 0},
{1, 1, 1, 1, 1},
{0, 0, 1, 1, 1},
{1, 1, 0, 0, 0}
};
static int expected[6][7] = {
{ 1, 1, 1, 1, 1, 0, 5},
{-1, 1, 1, 1, 1, 1, 6},
{ 5, 0, 0, 0, 0, 0, 1},
{-1, 5, 0, 0, 0, 0, 2},
{ 2, 3, 0, 0, 0, 0, 2},
{-1, 2, 3, 0, 0, 0, 3}
};

testStart("Test runlength calc function");
for(i=0; i<6; i++) {
length = Mask_calcRunLength(width, pattern[i], 0, runLength);
assert_equal(expected[i][6], length, "Length incorrect: %d, expected %d.\n", length, expected[i][6]);
assert_zero(memcmp(runLength, expected[i], sizeof(int) * (width + 1)), "Run length does not match: pattern %d, horizontal access.\n", i);
for(j=0; j<width; j++) {
frame[j * width] = pattern[i][j];
}
length = Mask_calcRunLength(width, frame, 1, runLength);
assert_equal(expected[i][6], length, "Length incorrect: %d, expected %d.\n", length, expected[i][6]);
assert_zero(memcmp(runLength, expected[i], sizeof(int) * (width + 1)), "Run length does not match: pattern %d, vertical access.\n", i);
}
testFinish();
}

int main(void)
{
//print_masks();
Expand All @@ -312,6 +351,7 @@ int main(void)
test_eval3();
test_format();
test_calcN2();
test_calcRunLength();

report();

Expand Down

0 comments on commit 296e792

Please sign in to comment.