Added new integer constants to C code, updated existing code to make use of them.

This commit is contained in:
Project Nayuki 2017-04-19 22:17:07 +00:00
parent c81354658b
commit 26b4cf557c
3 changed files with 13 additions and 8 deletions

View File

@ -51,7 +51,8 @@ static void doBasicDemo() {
// Make and print the QR Code symbol
uint8_t qrcode[qrcodegen_BUFFER_LEN_MAX];
uint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];
int version = qrcodegen_encodeText(text, tempBuffer, qrcode, errCorLvl, 1, 40, qrcodegen_Mask_AUTO, true);
int version = qrcodegen_encodeText(text, tempBuffer, qrcode, errCorLvl,
qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
if (version != 0)
printQr(qrcode, version);
}

View File

@ -91,7 +91,7 @@ static const int PENALTY_N4 = 10;
// Public function - see documentation comment in header file.
int qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[],
enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) {
assert(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40);
assert(qrcodegen_VERSION_MIN <= minVersion && minVersion <= maxVersion && maxVersion <= qrcodegen_VERSION_MAX);
assert(0 <= (int)ecl && (int)ecl <= 3 && -1 <= (int)mask && (int)mask <= 7);
// Get text properties
@ -211,7 +211,7 @@ int qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[
// Public function - see documentation comment in header file.
int qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode[],
enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) {
assert(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40);
assert(qrcodegen_VERSION_MIN <= minVersion && minVersion <= maxVersion && maxVersion <= qrcodegen_VERSION_MAX);
assert(0 <= (int)ecl && (int)ecl <= 3 && -1 <= (int)mask && (int)mask <= 7);
int version;
@ -378,7 +378,7 @@ static void appendBitsToBuffer(uint16_t val, int numBits, uint8_t buffer[], int
// Returns the number of 8-bit codewords that can be used for storing data (not ECC),
// for the given version number and error correction level. The result is in the range [9, 2956].
static int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl) {
assert(0 <= (int)ecl && (int)ecl < 4 && 1 <= version && version <= 40);
assert(0 <= (int)ecl && (int)ecl < 4 && qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
return getNumRawDataModules(version) / 8 - NUM_ERROR_CORRECTION_CODEWORDS[(int)ecl][version];
}
@ -388,7 +388,7 @@ static int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl) {
// Public function - see documentation comment in header file.
int qrcodegen_getSize(int version) {
assert(1 <= version && version <= 40);
assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
return version * 4 + 17;
}
@ -605,7 +605,7 @@ static void fillRectangle(int left, int top, int width, int height, uint8_t qrco
// and will be clobbered by this function. The final answer is stored in result[0 : rawCodewords].
static void appendErrorCorrection(uint8_t data[], int version, enum qrcodegen_Ecc ecl, uint8_t result[]) {
// Calculate parameter numbers
assert(0 <= (int)ecl && (int)ecl < 4 && 1 <= version && version <= 40);
assert(0 <= (int)ecl && (int)ecl < 4 && qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[(int)ecl][version];
int totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[(int)ecl][version];
assert(totalEcc % numBlocks == 0);
@ -647,7 +647,7 @@ static void appendErrorCorrection(uint8_t data[], int version, enum qrcodegen_Ec
// Returns the number of data bits that can be stored in a QR Code of the given version number, after
// all function modules are excluded. This includes remainder bits, so it may not be a multiple of 8.
static int getNumRawDataModules(int version) {
assert(1 <= version && version <= 40);
assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
int result = (16 * version + 128) * version + 64;
if (version >= 2) {
int numAlign = version / 7 + 2;

View File

@ -60,6 +60,10 @@ enum qrcodegen_Mask {
/*---- Macro constants and functions ----*/
// The minimum and maximum defined QR Code version numbers.
#define qrcodegen_VERSION_MIN 1
#define qrcodegen_VERSION_MAX 40
// 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)];'
// can store any single QR Code from version 1 to 25, inclusive.
@ -67,7 +71,7 @@ enum qrcodegen_Mask {
// 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.
#define qrcodegen_BUFFER_LEN_MAX qrcodegen_BUFFER_LEN_FOR_VERSION(40)
#define qrcodegen_BUFFER_LEN_MAX qrcodegen_BUFFER_LEN_FOR_VERSION(qrcodegen_VERSION_MAX)