diff --git a/cpp/QrCode.cpp b/cpp/QrCode.cpp index de23a7f..1b82517 100644 --- a/cpp/QrCode.cpp +++ b/cpp/QrCode.cpp @@ -139,11 +139,31 @@ QrCode::QrCode(int ver, Ecc ecl, const vector &dataCodewords, int mask) modules = vector >(size, vector(size)); // Initially all white isFunction = vector >(size, vector(size)); - // Compute ECC, draw modules, do masking + // Compute ECC, draw modules drawFunctionPatterns(); const vector allCodewords = addEccAndInterleave(dataCodewords); drawCodewords(allCodewords); - this->mask = handleConstructorMasking(mask); + + // Do masking + if (mask == -1) { // Automatically choose best mask + long minPenalty = LONG_MAX; + for (int i = 0; i < 8; i++) { + drawFormatBits(i); + applyMask(i); + long penalty = getPenaltyScore(); + if (penalty < minPenalty) { + mask = i; + minPenalty = penalty; + } + applyMask(i); // Undoes the mask due to XOR + } + } + if (mask < 0 || mask > 7) + throw std::logic_error("Assertion error"); + this->mask = mask; + drawFormatBits(mask); // Overwrite old format bits + applyMask(mask); // Apply the final choice of mask + isFunction.clear(); isFunction.shrink_to_fit(); } @@ -403,28 +423,6 @@ void QrCode::applyMask(int mask) { } -int QrCode::handleConstructorMasking(int mask) { - if (mask == -1) { // Automatically choose best mask - long minPenalty = LONG_MAX; - for (int i = 0; i < 8; i++) { - drawFormatBits(i); - applyMask(i); - long penalty = getPenaltyScore(); - if (penalty < minPenalty) { - mask = i; - minPenalty = penalty; - } - applyMask(i); // Undoes the mask due to XOR - } - } - if (mask < 0 || mask > 7) - throw std::logic_error("Assertion error"); - drawFormatBits(mask); // Overwrite old format bits - applyMask(mask); // Apply the final choice of mask - return mask; // The caller shall assign this value to the final-declared field -} - - long QrCode::getPenaltyScore() const { long result = 0; diff --git a/cpp/QrCode.hpp b/cpp/QrCode.hpp index 2f69843..8e4970e 100644 --- a/cpp/QrCode.hpp +++ b/cpp/QrCode.hpp @@ -247,12 +247,6 @@ class QrCode final { private: void applyMask(int mask); - // A messy helper function for the constructors. This QR Code must be in an unmasked state when this - // method is called. The given argument is the requested mask, which is -1 for auto or 0 to 7 for fixed. - // This method applies and returns the actual mask chosen, from 0 to 7. - private: int handleConstructorMasking(int mask); - - // Calculates and returns the penalty score based on state of this QR Code's current modules. // This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score. private: long getPenaltyScore() const;