Refactored private C function getModule() to read size from QR Code array and remove parameter.

This commit is contained in:
Project Nayuki 2017-04-25 05:41:26 +00:00
parent 2b4bd9ee24
commit 12bd5401ab
1 changed files with 20 additions and 19 deletions

View File

@ -66,7 +66,7 @@ static void drawCodewords(const uint8_t data[], int dataLen, uint8_t qrcode[], i
static void applyMask(const uint8_t functionModules[], uint8_t qrcode[], int qrsize, enum qrcodegen_Mask mask); static void applyMask(const uint8_t functionModules[], uint8_t qrcode[], int qrsize, enum qrcodegen_Mask mask);
static long getPenaltyScore(const uint8_t qrcode[], int qrsize); static long getPenaltyScore(const uint8_t qrcode[], int qrsize);
static bool getModule(const uint8_t qrcode[], int qrsize, int x, int y); static bool getModule(const uint8_t qrcode[], int x, int y);
static void setModule(uint8_t qrcode[], int x, int y, bool isBlack); static void setModule(uint8_t qrcode[], int x, int y, bool isBlack);
static void setModuleBounded(uint8_t qrcode[], int x, int y, bool isBlack); static void setModuleBounded(uint8_t qrcode[], int x, int y, bool isBlack);
@ -639,7 +639,7 @@ static void drawCodewords(const uint8_t data[], int dataLen, uint8_t qrcode[], i
int x = right - j; // Actual x coordinate int x = right - j; // Actual x coordinate
bool upward = ((right + 1) & 2) == 0; bool upward = ((right + 1) & 2) == 0;
int y = upward ? qrsize - 1 - vert : vert; // Actual y coordinate int y = upward ? qrsize - 1 - vert : vert; // Actual y coordinate
if (!getModule(qrcode, qrsize, x, y) && i < dataLen * 8) { if (!getModule(qrcode, x, y) && i < dataLen * 8) {
bool black = ((data[i >> 3] >> (7 - (i & 7))) & 1) != 0; bool black = ((data[i >> 3] >> (7 - (i & 7))) & 1) != 0;
setModule(qrcode, x, y, black); setModule(qrcode, x, y, black);
i++; i++;
@ -661,7 +661,7 @@ static void applyMask(const uint8_t functionModules[], uint8_t qrcode[], int qrs
assert(0 <= (int)mask && (int)mask <= 7); // Disallows qrcodegen_Mask_AUTO assert(0 <= (int)mask && (int)mask <= 7); // Disallows qrcodegen_Mask_AUTO
for (int y = 0; y < qrsize; y++) { for (int y = 0; y < qrsize; y++) {
for (int x = 0; x < qrsize; x++) { for (int x = 0; x < qrsize; x++) {
if (getModule(functionModules, qrsize, x, y)) if (getModule(functionModules, x, y))
continue; continue;
bool invert; bool invert;
switch ((int)mask) { switch ((int)mask) {
@ -675,7 +675,7 @@ static void applyMask(const uint8_t functionModules[], uint8_t qrcode[], int qrs
case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break; case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;
default: assert(false); default: assert(false);
} }
bool val = getModule(qrcode, qrsize, x, y); bool val = getModule(qrcode, x, y);
setModule(qrcode, x, y, val ^ invert); setModule(qrcode, x, y, val ^ invert);
} }
} }
@ -689,10 +689,10 @@ static long getPenaltyScore(const uint8_t qrcode[], int qrsize) {
// Adjacent modules in row having same color // Adjacent modules in row having same color
for (int y = 0; y < qrsize; y++) { for (int y = 0; y < qrsize; y++) {
bool colorX = getModule(qrcode, qrsize, 0, y); bool colorX = getModule(qrcode, 0, y);
for (int x = 1, runX = 1; x < qrsize; x++) { for (int x = 1, runX = 1; x < qrsize; x++) {
if (getModule(qrcode, qrsize, x, y) != colorX) { if (getModule(qrcode, x, y) != colorX) {
colorX = getModule(qrcode, qrsize, x, y); colorX = getModule(qrcode, x, y);
runX = 1; runX = 1;
} else { } else {
runX++; runX++;
@ -705,10 +705,10 @@ static long getPenaltyScore(const uint8_t qrcode[], int qrsize) {
} }
// Adjacent modules in column having same color // Adjacent modules in column having same color
for (int x = 0; x < qrsize; x++) { for (int x = 0; x < qrsize; x++) {
bool colorY = getModule(qrcode, qrsize, x, 0); bool colorY = getModule(qrcode, x, 0);
for (int y = 1, runY = 1; y < qrsize; y++) { for (int y = 1, runY = 1; y < qrsize; y++) {
if (getModule(qrcode, qrsize, x, y) != colorY) { if (getModule(qrcode, x, y) != colorY) {
colorY = getModule(qrcode, qrsize, x, y); colorY = getModule(qrcode, x, y);
runY = 1; runY = 1;
} else { } else {
runY++; runY++;
@ -723,10 +723,10 @@ static long getPenaltyScore(const uint8_t qrcode[], int qrsize) {
// 2*2 blocks of modules having same color // 2*2 blocks of modules having same color
for (int y = 0; y < qrsize - 1; y++) { for (int y = 0; y < qrsize - 1; y++) {
for (int x = 0; x < qrsize - 1; x++) { for (int x = 0; x < qrsize - 1; x++) {
bool color = getModule(qrcode, qrsize, x, y); bool color = getModule(qrcode, x, y);
if ( color == getModule(qrcode, qrsize, x + 1, y) && if ( color == getModule(qrcode, x + 1, y) &&
color == getModule(qrcode, qrsize, x, y + 1) && color == getModule(qrcode, x, y + 1) &&
color == getModule(qrcode, qrsize, x + 1, y + 1)) color == getModule(qrcode, x + 1, y + 1))
result += PENALTY_N2; result += PENALTY_N2;
} }
} }
@ -734,7 +734,7 @@ static long getPenaltyScore(const uint8_t qrcode[], int qrsize) {
// Finder-like pattern in rows // Finder-like pattern in rows
for (int y = 0; y < qrsize; y++) { for (int y = 0; y < qrsize; y++) {
for (int x = 0, bits = 0; x < qrsize; x++) { for (int x = 0, bits = 0; x < qrsize; x++) {
bits = ((bits << 1) & 0x7FF) | (getModule(qrcode, qrsize, x, y) ? 1 : 0); bits = ((bits << 1) & 0x7FF) | (getModule(qrcode, x, y) ? 1 : 0);
if (x >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated if (x >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated
result += PENALTY_N3; result += PENALTY_N3;
} }
@ -742,7 +742,7 @@ static long getPenaltyScore(const uint8_t qrcode[], int qrsize) {
// Finder-like pattern in columns // Finder-like pattern in columns
for (int x = 0; x < qrsize; x++) { for (int x = 0; x < qrsize; x++) {
for (int y = 0, bits = 0; y < qrsize; y++) { for (int y = 0, bits = 0; y < qrsize; y++) {
bits = ((bits << 1) & 0x7FF) | (getModule(qrcode, qrsize, x, y) ? 1 : 0); bits = ((bits << 1) & 0x7FF) | (getModule(qrcode, x, y) ? 1 : 0);
if (y >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated if (y >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated
result += PENALTY_N3; result += PENALTY_N3;
} }
@ -752,7 +752,7 @@ static long getPenaltyScore(const uint8_t qrcode[], int qrsize) {
int black = 0; int black = 0;
for (int y = 0; y < qrsize; y++) { for (int y = 0; y < qrsize; y++) {
for (int x = 0; x < qrsize; x++) { for (int x = 0; x < qrsize; x++) {
if (getModule(qrcode, qrsize, x, y)) if (getModule(qrcode, x, y))
black++; black++;
} }
} }
@ -777,12 +777,13 @@ int qrcodegen_getSize(int version) {
// Public function - see documentation comment in header file. // Public function - see documentation comment in header file.
bool qrcodegen_getModule(const uint8_t qrcode[], int version, int x, int y) { bool qrcodegen_getModule(const uint8_t qrcode[], int version, int x, int y) {
int qrsize = qrcodegen_getSize(version); int qrsize = qrcodegen_getSize(version);
return (0 <= x && x < qrsize && 0 <= y && y < qrsize) && getModule(qrcode, qrsize, x, y); return (0 <= x && x < qrsize && 0 <= y && y < qrsize) && getModule(qrcode, x, y);
} }
// Gets the module at the given coordinates, which must be in bounds. // Gets the module at the given coordinates, which must be in bounds.
static bool getModule(const uint8_t qrcode[], int qrsize, int x, int y) { static bool getModule(const uint8_t qrcode[], int x, int y) {
int qrsize = qrcode[0];
assert(21 <= qrsize && qrsize <= 177 && 0 <= x && x < qrsize && 0 <= y && y < qrsize); assert(21 <= qrsize && qrsize <= 177 && 0 <= x && x < qrsize && 0 <= y && y < qrsize);
int index = y * qrsize + x; int index = y * qrsize + x;
int bitIndex = index & 7; int bitIndex = index & 7;