Changed C code to prepend a size info byte to beginning of QR Code arrays. This has no visible behavioral change when using qrcodegen APIs, but the array data will be different and all buffers need one extra byte. Old code using the API needs to be recompiled because the buffer length calculation macros changed.

This commit is contained in:
Project Nayuki 2017-04-25 05:31:15 +00:00
parent d6080663b9
commit 339efc46b8
2 changed files with 6 additions and 5 deletions

View File

@ -460,7 +460,8 @@ testable uint8_t finiteFieldMultiply(uint8_t x, uint8_t y) {
static void initializeFunctionModules(int version, uint8_t qrcode[]) { static void initializeFunctionModules(int version, uint8_t qrcode[]) {
// Initialize QR Code // Initialize QR Code
int qrsize = qrcodegen_getSize(version); int qrsize = qrcodegen_getSize(version);
memset(qrcode, 0, (qrsize * qrsize + 7) / 8 * sizeof(qrcode[0])); memset(qrcode, 0, ((qrsize * qrsize + 7) / 8 + 1) * sizeof(qrcode[0]));
qrcode[0] = (uint8_t)qrsize;
// Fill horizontal and vertical timing patterns // Fill horizontal and vertical timing patterns
fillRectangle(6, 0, 1, qrsize, qrcode, qrsize); fillRectangle(6, 0, 1, qrsize, qrcode, qrsize);
@ -785,7 +786,7 @@ static bool getModule(const uint8_t qrcode[], int qrsize, int x, int y) {
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;
int byteIndex = index >> 3; int byteIndex = (index >> 3) + 1;
return ((qrcode[byteIndex] >> bitIndex) & 1) != 0; return ((qrcode[byteIndex] >> bitIndex) & 1) != 0;
} }
@ -795,7 +796,7 @@ static void setModule(uint8_t qrcode[], int qrsize, int x, int y, bool isBlack)
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;
int byteIndex = index >> 3; int byteIndex = (index >> 3) + 1;
if (isBlack) if (isBlack)
qrcode[byteIndex] |= 1 << bitIndex; qrcode[byteIndex] |= 1 << bitIndex;
else else

View File

@ -66,7 +66,7 @@ enum qrcodegen_Mask {
// Calculates the number of bytes needed to store any QR Code up to and including the given version number, // Calculates the number of bytes needed to store any QR Code up to and including the given version number,
// as a compile-time constant. For example, 'uint8_t buffer[qrcodegen_BUFFER_LEN_FOR_VERSION(25)];' // as a compile-time constant. For example, 'uint8_t buffer[qrcodegen_BUFFER_LEN_FOR_VERSION(25)];'
// can store any single QR Code from version 1 to 25, inclusive. // can store any single QR Code from version 1 to 25, inclusive.
#define qrcodegen_BUFFER_LEN_FOR_VERSION(n) ((((n) * 4 + 17) * ((n) * 4 + 17) + 7) / 8) #define qrcodegen_BUFFER_LEN_FOR_VERSION(n) ((((n) * 4 + 17) * ((n) * 4 + 17) + 7) / 8 + 1)
// The worst-case number of bytes needed to store one QR Code, up to and including // The worst-case number of bytes needed to store one QR Code, up to and including
// version 40. This value equals 3917, which is just under 4 kilobytes. // version 40. This value equals 3917, which is just under 4 kilobytes.
@ -105,7 +105,7 @@ int qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode
* Returns the side length of any QR Code of the given version number. * Returns the side length of any QR Code of the given version number.
* The version must be in the range [1, 40]. The result is in the range [21, 177]. * The version must be in the range [1, 40]. The result is in the range [21, 177].
* Note that every 'uint8_t qrcode[]' buffer must have a length of at least * Note that every 'uint8_t qrcode[]' buffer must have a length of at least
* ceil(size^2 / 8), which also equals qrcodegen_BUFFER_LEN_FOR_VERSION(version). * ceil(size^2 / 8 + 1), which also equals qrcodegen_BUFFER_LEN_FOR_VERSION(version).
*/ */
int qrcodegen_getSize(int version); int qrcodegen_getSize(int version);