static int Mask_evaluateSymbol(int width, unsigned char *frame) { int x, y; int demerit = 0; int runLength[QRSPEC_WIDTH_MAX + 1]; int length; demerit += Mask_calcN2(width, frame); for(y=0; y<width; y++) { length = Mask_calcRunLength(width, frame + y * width, 0, runLength); demerit += Mask_calcN1N3(length, runLength); } for(x=0; x<width; x++) { length = Mask_calcRunLength(width, frame + x, 1, runLength); demerit += Mask_calcN1N3(length, runLength); } return demerit; }
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) * expected[i][6]), "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) * expected[i][6]), "Run length does not match: pattern %d, vertical access.\n", i); } testFinish(); }
void test_calcN1N3(void) { int runLength[26]; int length; int demerit; int i; static unsigned char pattern[][16] = { {1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, N3}, {0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, N3}, {1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0}, {1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, N3}, {1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, N3}, {1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, N3 * 2}, }; static unsigned char pattern2[][19] = { {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, N3 + N1 + 1}, {0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, N3 + N1 + 1}, {1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, N1 + 1}, {1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, N3 + N1 + 1}, {1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, N3 + N1 + 1} }; testStart("Test N3 penalty calculation"); for(i=0; i<6; i++) { length = Mask_calcRunLength(15, pattern[i], 0, runLength); demerit = Mask_calcN1N3(length, runLength); assert_equal(pattern[i][15], demerit, "N3 penalty is wrong: %d, expected %d\n", demerit, pattern[i][15]); } for(i=0; i<5; i++) { length = Mask_calcRunLength(18, pattern2[i], 0, runLength); demerit = Mask_calcN1N3(length, runLength); assert_equal(pattern2[i][18], demerit, "N3 penalty is wrong: %d, expected %d\n", demerit, pattern2[i][18]); } testFinish(); }