From be2e9e7a3af742daca0ccdc8f189c09bc9025a6a Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Wed, 19 Apr 2017 20:46:32 +0000 Subject: [PATCH] Continued implementing C library by adding logic to support boosting ECC level. --- c/qrcodegen.c | 18 +++++++++++++++--- c/qrcodegen.h | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/c/qrcodegen.c b/c/qrcodegen.c index 35e5e61..f6e0505 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -88,7 +88,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) { + enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) { assert(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40); assert(0 <= (int)ecl && (int)ecl <= 3 && -1 <= (int)mask && (int)mask <= 7); @@ -126,7 +126,7 @@ int qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[ return 0; for (int i = 0; i < textLen; i++) tempBuffer[i] = (uint8_t)text[i]; - return qrcodegen_encodeBinary(tempBuffer, (size_t)textLen, qrcode, ecl, minVersion, maxVersion, mask); + return qrcodegen_encodeBinary(tempBuffer, (size_t)textLen, qrcode, ecl, minVersion, maxVersion, mask, boostEcl); } int version; @@ -153,6 +153,12 @@ int qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[ return 0; } assert(dataUsedBits >= 0 && dataCapacityBits >= 0); + if (boostEcl) { + if (dataUsedBits <= getNumDataCodewords(version, qrcodegen_Ecc_MEDIUM ) * 8) ecl = qrcodegen_Ecc_MEDIUM ; + if (dataUsedBits <= getNumDataCodewords(version, qrcodegen_Ecc_QUARTILE) * 8) ecl = qrcodegen_Ecc_QUARTILE; + if (dataUsedBits <= getNumDataCodewords(version, qrcodegen_Ecc_HIGH ) * 8) ecl = qrcodegen_Ecc_HIGH ; + dataCapacityBits = getNumDataCodewords(version, ecl) * 8; + } memset(qrcode, 0, qrcodegen_BUFFER_LEN_FOR_VERSION(version) * sizeof(qrcode[0])); int bitLen = 0; @@ -223,7 +229,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) { + enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) { assert(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40); assert(0 <= (int)ecl && (int)ecl <= 3 && -1 <= (int)mask && (int)mask <= 7); @@ -244,6 +250,12 @@ int qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode return 0; } assert(dataUsedBits >= 0 && dataCapacityBits >= 0); + if (boostEcl) { + if (dataUsedBits <= getNumDataCodewords(version, qrcodegen_Ecc_MEDIUM ) * 8) ecl = qrcodegen_Ecc_MEDIUM ; + if (dataUsedBits <= getNumDataCodewords(version, qrcodegen_Ecc_QUARTILE) * 8) ecl = qrcodegen_Ecc_QUARTILE; + if (dataUsedBits <= getNumDataCodewords(version, qrcodegen_Ecc_HIGH ) * 8) ecl = qrcodegen_Ecc_HIGH ; + dataCapacityBits = getNumDataCodewords(version, ecl) * 8; + } memset(qrcode, 0, qrcodegen_BUFFER_LEN_FOR_VERSION(version) * sizeof(qrcode[0])); int bitLen = 0; diff --git a/c/qrcodegen.h b/c/qrcodegen.h index 1a21e7e..4d3bebe 100644 --- a/c/qrcodegen.h +++ b/c/qrcodegen.h @@ -71,7 +71,7 @@ enum qrcodegen_Mask { * qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion). */ int qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[], - enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask); + enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl); /* @@ -82,7 +82,7 @@ int qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[ * must have length at least qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion). */ int qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode[], - enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask); + enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl); /*