Mike Fleming | 3933d92 | 2018-04-02 10:53:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * QR Code generator library (C++) |
| 3 | * |
| 4 | * Copyright (c) Project Nayuki. (MIT License) |
| 5 | * https://www.nayuki.io/page/qr-code-generator-library |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 8 | * this software and associated documentation files (the "Software"), to deal in |
| 9 | * the Software without restriction, including without limitation the rights to |
| 10 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 11 | * the Software, and to permit persons to whom the Software is furnished to do so, |
| 12 | * subject to the following conditions: |
| 13 | * - The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * - The Software is provided "as is", without warranty of any kind, express or |
| 16 | * implied, including but not limited to the warranties of merchantability, |
| 17 | * fitness for a particular purpose and noninfringement. In no event shall the |
| 18 | * authors or copyright holders be liable for any claim, damages or other |
| 19 | * liability, whether in an action of contract, tort or otherwise, arising from, |
| 20 | * out of or in connection with the Software or the use or other dealings in the |
| 21 | * Software. |
| 22 | */ |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <climits> |
| 26 | #include <cstddef> |
| 27 | #include <cstdlib> |
| 28 | #include <sstream> |
| 29 | #include <utility> |
| 30 | #include "BitBuffer.hpp" |
| 31 | #include "QrCode.hpp" |
| 32 | |
Chad Duffin | ac9ac06 | 2019-07-23 10:06:45 -0700 | [diff] [blame] | 33 | #include "starboard/common/log.h" |
Andrew Top | b626c97 | 2018-04-19 09:11:54 -0700 | [diff] [blame] | 34 | |
| 35 | #define throw SB_CHECK(false) << |
| 36 | |
Mike Fleming | 3933d92 | 2018-04-02 10:53:08 -0700 | [diff] [blame] | 37 | using std::int8_t; |
| 38 | using std::uint8_t; |
| 39 | using std::size_t; |
| 40 | using std::vector; |
| 41 | |
| 42 | |
| 43 | namespace qrcodegen { |
| 44 | |
| 45 | int QrCode::getFormatBits(Ecc ecl) { |
| 46 | switch (ecl) { |
| 47 | case Ecc::LOW : return 1; |
| 48 | case Ecc::MEDIUM : return 0; |
| 49 | case Ecc::QUARTILE: return 3; |
| 50 | case Ecc::HIGH : return 2; |
| 51 | default: throw "Assertion error"; |
| 52 | } |
Andrew Top | b626c97 | 2018-04-19 09:11:54 -0700 | [diff] [blame] | 53 | return 0; |
Mike Fleming | 3933d92 | 2018-04-02 10:53:08 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
Andrew Top | a7b1cfa | 2019-12-18 19:15:07 -0800 | [diff] [blame] | 57 | QrCode QrCode::encodeText(const char *text, Ecc ecl, int minVersion) { |
Mike Fleming | 3933d92 | 2018-04-02 10:53:08 -0700 | [diff] [blame] | 58 | vector<QrSegment> segs = QrSegment::makeSegments(text); |
Andrew Top | a7b1cfa | 2019-12-18 19:15:07 -0800 | [diff] [blame] | 59 | return encodeSegments(segs, ecl, minVersion); |
Mike Fleming | 3933d92 | 2018-04-02 10:53:08 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | QrCode QrCode::encodeBinary(const vector<uint8_t> &data, Ecc ecl) { |
| 64 | vector<QrSegment> segs{QrSegment::makeBytes(data)}; |
| 65 | return encodeSegments(segs, ecl); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, Ecc ecl, |
| 70 | int minVersion, int maxVersion, int mask, bool boostEcl) { |
| 71 | if (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7) |
| 72 | throw "Invalid value"; |
| 73 | |
| 74 | // Find the minimal version number to use |
| 75 | int version, dataUsedBits; |
| 76 | for (version = minVersion; ; version++) { |
| 77 | int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; // Number of data bits available |
| 78 | dataUsedBits = QrSegment::getTotalBits(segs, version); |
| 79 | if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits) |
| 80 | break; // This version number is found to be suitable |
| 81 | if (version >= maxVersion) // All versions in the range could not fit the given data |
| 82 | throw "Data too long"; |
| 83 | } |
| 84 | if (dataUsedBits == -1) |
| 85 | throw "Assertion error"; |
| 86 | |
| 87 | // Increase the error correction level while the data still fits in the current version number |
| 88 | for (Ecc newEcl : vector<Ecc>{Ecc::MEDIUM, Ecc::QUARTILE, Ecc::HIGH}) { |
| 89 | if (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8) |
| 90 | ecl = newEcl; |
| 91 | } |
| 92 | |
| 93 | // Create the data bit string by concatenating all segments |
| 94 | size_t dataCapacityBits = getNumDataCodewords(version, ecl) * 8; |
| 95 | BitBuffer bb; |
| 96 | for (const QrSegment &seg : segs) { |
| 97 | bb.appendBits(seg.getMode().getModeBits(), 4); |
| 98 | bb.appendBits(seg.getNumChars(), seg.getMode().numCharCountBits(version)); |
| 99 | bb.insert(bb.end(), seg.getData().begin(), seg.getData().end()); |
| 100 | } |
| 101 | |
| 102 | // Add terminator and pad up to a byte if applicable |
| 103 | bb.appendBits(0, std::min<size_t>(4, dataCapacityBits - bb.size())); |
| 104 | bb.appendBits(0, (8 - bb.size() % 8) % 8); |
| 105 | |
| 106 | // Pad with alternate bytes until data capacity is reached |
| 107 | for (uint8_t padByte = 0xEC; bb.size() < dataCapacityBits; padByte ^= 0xEC ^ 0x11) |
| 108 | bb.appendBits(padByte, 8); |
| 109 | if (bb.size() % 8 != 0) |
| 110 | throw "Assertion error"; |
| 111 | |
| 112 | // Create the QR Code symbol |
| 113 | return QrCode(version, ecl, bb.getBytes(), mask); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | QrCode::QrCode(int ver, Ecc ecl, const vector<uint8_t> &dataCodewords, int mask) : |
| 118 | // Initialize fields |
| 119 | version(ver), |
| 120 | size(MIN_VERSION <= ver && ver <= MAX_VERSION ? ver * 4 + 17 : -1), // Avoid signed overflow undefined behavior |
| 121 | errorCorrectionLevel(ecl), |
| 122 | modules(size, vector<bool>(size)), // Entirely white grid |
| 123 | isFunction(size, vector<bool>(size)) { |
| 124 | |
| 125 | // Check arguments |
| 126 | if (ver < MIN_VERSION || ver > MAX_VERSION || mask < -1 || mask > 7) |
| 127 | throw "Value out of range"; |
| 128 | |
| 129 | // Draw function patterns, draw all codewords, do masking |
| 130 | drawFunctionPatterns(); |
| 131 | const vector<uint8_t> allCodewords = appendErrorCorrection(dataCodewords); |
| 132 | drawCodewords(allCodewords); |
| 133 | this->mask = handleConstructorMasking(mask); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | int QrCode::getVersion() const { |
| 138 | return version; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | int QrCode::getSize() const { |
| 143 | return size; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | QrCode::Ecc QrCode::getErrorCorrectionLevel() const { |
| 148 | return errorCorrectionLevel; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | int QrCode::getMask() const { |
| 153 | return mask; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | bool QrCode::getModule(int x, int y) const { |
| 158 | return 0 <= x && x < size && 0 <= y && y < size && module(x, y); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | std::string QrCode::toSvgString(int border) const { |
| 163 | if (border < 0) |
| 164 | throw "Border must be non-negative"; |
| 165 | if (border > INT_MAX / 2 || border * 2 > INT_MAX - size) |
| 166 | throw "Border too large"; |
| 167 | |
| 168 | std::ostringstream sb; |
| 169 | sb << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
| 170 | sb << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"; |
| 171 | sb << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 "; |
| 172 | sb << (size + border * 2) << " " << (size + border * 2) << "\" stroke=\"none\">\n"; |
| 173 | sb << "\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n"; |
| 174 | sb << "\t<path d=\""; |
| 175 | bool head = true; |
| 176 | for (int y = -border; y < size + border; y++) { |
| 177 | for (int x = -border; x < size + border; x++) { |
| 178 | if (getModule(x, y)) { |
| 179 | if (head) |
| 180 | head = false; |
| 181 | else |
| 182 | sb << " "; |
| 183 | sb << "M" << (x + border) << "," << (y + border) << "h1v1h-1z"; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | sb << "\" fill=\"#000000\"/>\n"; |
| 188 | sb << "</svg>\n"; |
| 189 | return sb.str(); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void QrCode::drawFunctionPatterns() { |
| 194 | // Draw horizontal and vertical timing patterns |
| 195 | for (int i = 0; i < size; i++) { |
| 196 | setFunctionModule(6, i, i % 2 == 0); |
| 197 | setFunctionModule(i, 6, i % 2 == 0); |
| 198 | } |
| 199 | |
| 200 | // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules) |
| 201 | drawFinderPattern(3, 3); |
| 202 | drawFinderPattern(size - 4, 3); |
| 203 | drawFinderPattern(3, size - 4); |
| 204 | |
| 205 | // Draw numerous alignment patterns |
| 206 | const vector<int> alignPatPos = getAlignmentPatternPositions(version); |
| 207 | int numAlign = alignPatPos.size(); |
| 208 | for (int i = 0; i < numAlign; i++) { |
| 209 | for (int j = 0; j < numAlign; j++) { |
| 210 | if ((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0)) |
| 211 | continue; // Skip the three finder corners |
| 212 | else |
| 213 | drawAlignmentPattern(alignPatPos.at(i), alignPatPos.at(j)); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Draw configuration data |
| 218 | drawFormatBits(0); // Dummy mask value; overwritten later in the constructor |
| 219 | drawVersion(); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | void QrCode::drawFormatBits(int mask) { |
| 224 | // Calculate error correction code and pack bits |
| 225 | int data = getFormatBits(errorCorrectionLevel) << 3 | mask; // errCorrLvl is uint2, mask is uint3 |
| 226 | int rem = data; |
| 227 | for (int i = 0; i < 10; i++) |
| 228 | rem = (rem << 1) ^ ((rem >> 9) * 0x537); |
| 229 | data = data << 10 | rem; |
| 230 | data ^= 0x5412; // uint15 |
| 231 | if (data >> 15 != 0) |
| 232 | throw "Assertion error"; |
| 233 | |
| 234 | // Draw first copy |
| 235 | for (int i = 0; i <= 5; i++) |
| 236 | setFunctionModule(8, i, ((data >> i) & 1) != 0); |
| 237 | setFunctionModule(8, 7, ((data >> 6) & 1) != 0); |
| 238 | setFunctionModule(8, 8, ((data >> 7) & 1) != 0); |
| 239 | setFunctionModule(7, 8, ((data >> 8) & 1) != 0); |
| 240 | for (int i = 9; i < 15; i++) |
| 241 | setFunctionModule(14 - i, 8, ((data >> i) & 1) != 0); |
| 242 | |
| 243 | // Draw second copy |
| 244 | for (int i = 0; i <= 7; i++) |
| 245 | setFunctionModule(size - 1 - i, 8, ((data >> i) & 1) != 0); |
| 246 | for (int i = 8; i < 15; i++) |
| 247 | setFunctionModule(8, size - 15 + i, ((data >> i) & 1) != 0); |
| 248 | setFunctionModule(8, size - 8, true); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | void QrCode::drawVersion() { |
| 253 | if (version < 7) |
| 254 | return; |
| 255 | |
| 256 | // Calculate error correction code and pack bits |
| 257 | int rem = version; // version is uint6, in the range [7, 40] |
| 258 | for (int i = 0; i < 12; i++) |
| 259 | rem = (rem << 1) ^ ((rem >> 11) * 0x1F25); |
| 260 | long data = (long)version << 12 | rem; // uint18 |
| 261 | if (data >> 18 != 0) |
| 262 | throw "Assertion error"; |
| 263 | |
| 264 | // Draw two copies |
| 265 | for (int i = 0; i < 18; i++) { |
| 266 | bool bit = ((data >> i) & 1) != 0; |
| 267 | int a = size - 11 + i % 3, b = i / 3; |
| 268 | setFunctionModule(a, b, bit); |
| 269 | setFunctionModule(b, a, bit); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | |
| 274 | void QrCode::drawFinderPattern(int x, int y) { |
| 275 | for (int i = -4; i <= 4; i++) { |
| 276 | for (int j = -4; j <= 4; j++) { |
| 277 | int dist = std::max(std::abs(i), std::abs(j)); // Chebyshev/infinity norm |
| 278 | int xx = x + j, yy = y + i; |
| 279 | if (0 <= xx && xx < size && 0 <= yy && yy < size) |
| 280 | setFunctionModule(xx, yy, dist != 2 && dist != 4); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | |
| 286 | void QrCode::drawAlignmentPattern(int x, int y) { |
| 287 | for (int i = -2; i <= 2; i++) { |
| 288 | for (int j = -2; j <= 2; j++) |
| 289 | setFunctionModule(x + j, y + i, std::max(std::abs(i), std::abs(j)) != 1); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | |
| 294 | void QrCode::setFunctionModule(int x, int y, bool isBlack) { |
| 295 | modules.at(y).at(x) = isBlack; |
| 296 | isFunction.at(y).at(x) = true; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | bool QrCode::module(int x, int y) const { |
| 301 | return modules.at(y).at(x); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | vector<uint8_t> QrCode::appendErrorCorrection(const vector<uint8_t> &data) const { |
| 306 | if (data.size() != static_cast<unsigned int>(getNumDataCodewords(version, errorCorrectionLevel))) |
| 307 | throw "Invalid argument"; |
| 308 | |
| 309 | // Calculate parameter numbers |
| 310 | int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(errorCorrectionLevel)][version]; |
| 311 | int blockEccLen = ECC_CODEWORDS_PER_BLOCK[static_cast<int>(errorCorrectionLevel)][version]; |
| 312 | int rawCodewords = getNumRawDataModules(version) / 8; |
| 313 | int numShortBlocks = numBlocks - rawCodewords % numBlocks; |
| 314 | int shortBlockLen = rawCodewords / numBlocks; |
| 315 | |
| 316 | // Split data into blocks and append ECC to each block |
| 317 | vector<vector<uint8_t> > blocks; |
| 318 | const ReedSolomonGenerator rs(blockEccLen); |
| 319 | for (int i = 0, k = 0; i < numBlocks; i++) { |
| 320 | vector<uint8_t> dat(data.cbegin() + k, data.cbegin() + (k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1))); |
| 321 | k += dat.size(); |
| 322 | const vector<uint8_t> ecc = rs.getRemainder(dat); |
| 323 | if (i < numShortBlocks) |
| 324 | dat.push_back(0); |
| 325 | dat.insert(dat.end(), ecc.cbegin(), ecc.cend()); |
| 326 | blocks.push_back(std::move(dat)); |
| 327 | } |
| 328 | |
| 329 | // Interleave (not concatenate) the bytes from every block into a single sequence |
| 330 | vector<uint8_t> result; |
| 331 | for (int i = 0; static_cast<unsigned int>(i) < blocks.at(0).size(); i++) { |
| 332 | for (int j = 0; static_cast<unsigned int>(j) < blocks.size(); j++) { |
| 333 | // Skip the padding byte in short blocks |
| 334 | if (i != shortBlockLen - blockEccLen || j >= numShortBlocks) |
| 335 | result.push_back(blocks.at(j).at(i)); |
| 336 | } |
| 337 | } |
| 338 | if (result.size() != static_cast<unsigned int>(rawCodewords)) |
| 339 | throw "Assertion error"; |
| 340 | return result; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | void QrCode::drawCodewords(const vector<uint8_t> &data) { |
| 345 | if (data.size() != static_cast<unsigned int>(getNumRawDataModules(version) / 8)) |
| 346 | throw "Invalid argument"; |
| 347 | |
| 348 | size_t i = 0; // Bit index into the data |
| 349 | // Do the funny zigzag scan |
| 350 | for (int right = size - 1; right >= 1; right -= 2) { // Index of right column in each column pair |
| 351 | if (right == 6) |
| 352 | right = 5; |
| 353 | for (int vert = 0; vert < size; vert++) { // Vertical counter |
| 354 | for (int j = 0; j < 2; j++) { |
| 355 | int x = right - j; // Actual x coordinate |
| 356 | bool upward = ((right + 1) & 2) == 0; |
| 357 | int y = upward ? size - 1 - vert : vert; // Actual y coordinate |
| 358 | if (!isFunction.at(y).at(x) && i < data.size() * 8) { |
| 359 | modules.at(y).at(x) = ((data.at(i >> 3) >> (7 - (i & 7))) & 1) != 0; |
| 360 | i++; |
| 361 | } |
| 362 | // If there are any remainder bits (0 to 7), they are already |
| 363 | // set to 0/false/white when the grid of modules was initialized |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | if (static_cast<unsigned int>(i) != data.size() * 8) |
| 368 | throw "Assertion error"; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | void QrCode::applyMask(int mask) { |
| 373 | if (mask < 0 || mask > 7) |
| 374 | throw "Mask value out of range"; |
| 375 | for (int y = 0; y < size; y++) { |
| 376 | for (int x = 0; x < size; x++) { |
| 377 | bool invert; |
| 378 | switch (mask) { |
| 379 | case 0: invert = (x + y) % 2 == 0; break; |
| 380 | case 1: invert = y % 2 == 0; break; |
| 381 | case 2: invert = x % 3 == 0; break; |
| 382 | case 3: invert = (x + y) % 3 == 0; break; |
| 383 | case 4: invert = (x / 3 + y / 2) % 2 == 0; break; |
| 384 | case 5: invert = x * y % 2 + x * y % 3 == 0; break; |
| 385 | case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break; |
| 386 | case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break; |
| 387 | default: throw "Assertion error"; |
| 388 | } |
| 389 | modules.at(y).at(x) = modules.at(y).at(x) ^ (invert & !isFunction.at(y).at(x)); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | |
| 395 | int QrCode::handleConstructorMasking(int mask) { |
| 396 | if (mask == -1) { // Automatically choose best mask |
| 397 | long minPenalty = LONG_MAX; |
| 398 | for (int i = 0; i < 8; i++) { |
| 399 | drawFormatBits(i); |
| 400 | applyMask(i); |
| 401 | long penalty = getPenaltyScore(); |
| 402 | if (penalty < minPenalty) { |
| 403 | mask = i; |
| 404 | minPenalty = penalty; |
| 405 | } |
| 406 | applyMask(i); // Undoes the mask due to XOR |
| 407 | } |
| 408 | } |
| 409 | if (mask < 0 || mask > 7) |
| 410 | throw "Assertion error"; |
| 411 | drawFormatBits(mask); // Overwrite old format bits |
| 412 | applyMask(mask); // Apply the final choice of mask |
| 413 | return mask; // The caller shall assign this value to the final-declared field |
| 414 | } |
| 415 | |
| 416 | |
| 417 | long QrCode::getPenaltyScore() const { |
| 418 | long result = 0; |
| 419 | |
| 420 | // Adjacent modules in row having same color |
| 421 | for (int y = 0; y < size; y++) { |
| 422 | bool colorX = false; |
| 423 | for (int x = 0, runX = -1; x < size; x++) { |
| 424 | if (x == 0 || module(x, y) != colorX) { |
| 425 | colorX = module(x, y); |
| 426 | runX = 1; |
| 427 | } else { |
| 428 | runX++; |
| 429 | if (runX == 5) |
| 430 | result += PENALTY_N1; |
| 431 | else if (runX > 5) |
| 432 | result++; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | // Adjacent modules in column having same color |
| 437 | for (int x = 0; x < size; x++) { |
| 438 | bool colorY = false; |
| 439 | for (int y = 0, runY = -1; y < size; y++) { |
| 440 | if (y == 0 || module(x, y) != colorY) { |
| 441 | colorY = module(x, y); |
| 442 | runY = 1; |
| 443 | } else { |
| 444 | runY++; |
| 445 | if (runY == 5) |
| 446 | result += PENALTY_N1; |
| 447 | else if (runY > 5) |
| 448 | result++; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | // 2*2 blocks of modules having same color |
| 454 | for (int y = 0; y < size - 1; y++) { |
| 455 | for (int x = 0; x < size - 1; x++) { |
| 456 | bool color = module(x, y); |
| 457 | if ( color == module(x + 1, y) && |
| 458 | color == module(x, y + 1) && |
| 459 | color == module(x + 1, y + 1)) |
| 460 | result += PENALTY_N2; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // Finder-like pattern in rows |
| 465 | for (int y = 0; y < size; y++) { |
| 466 | for (int x = 0, bits = 0; x < size; x++) { |
| 467 | bits = ((bits << 1) & 0x7FF) | (module(x, y) ? 1 : 0); |
| 468 | if (x >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated |
| 469 | result += PENALTY_N3; |
| 470 | } |
| 471 | } |
| 472 | // Finder-like pattern in columns |
| 473 | for (int x = 0; x < size; x++) { |
| 474 | for (int y = 0, bits = 0; y < size; y++) { |
| 475 | bits = ((bits << 1) & 0x7FF) | (module(x, y) ? 1 : 0); |
| 476 | if (y >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated |
| 477 | result += PENALTY_N3; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | // Balance of black and white modules |
| 482 | int black = 0; |
| 483 | for (const vector<bool> &row : modules) { |
| 484 | for (bool color : row) { |
| 485 | if (color) |
| 486 | black++; |
| 487 | } |
| 488 | } |
| 489 | int total = size * size; |
| 490 | // Find smallest k such that (45-5k)% <= dark/total <= (55+5k)% |
| 491 | for (int k = 0; black*20L < (9L-k)*total || black*20L > (11L+k)*total; k++) |
| 492 | result += PENALTY_N4; |
| 493 | return result; |
| 494 | } |
| 495 | |
| 496 | |
| 497 | vector<int> QrCode::getAlignmentPatternPositions(int ver) { |
Andrew Top | b626c97 | 2018-04-19 09:11:54 -0700 | [diff] [blame] | 498 | if (ver < MIN_VERSION || ver > MAX_VERSION) { |
Mike Fleming | 3933d92 | 2018-04-02 10:53:08 -0700 | [diff] [blame] | 499 | throw "Version number out of range"; |
Mike Fleming | 3933d92 | 2018-04-02 10:53:08 -0700 | [diff] [blame] | 500 | return vector<int>(); |
Andrew Top | b626c97 | 2018-04-19 09:11:54 -0700 | [diff] [blame] | 501 | } else if (ver == 1) { |
| 502 | return vector<int>(); |
| 503 | } else { |
Mike Fleming | 3933d92 | 2018-04-02 10:53:08 -0700 | [diff] [blame] | 504 | int numAlign = ver / 7 + 2; |
| 505 | int step; |
| 506 | if (ver != 32) { |
| 507 | // ceil((size - 13) / (2*numAlign - 2)) * 2 |
| 508 | step = (ver * 4 + numAlign * 2 + 1) / (2 * numAlign - 2) * 2; |
| 509 | } else // C-C-C-Combo breaker! |
| 510 | step = 26; |
| 511 | |
| 512 | vector<int> result; |
| 513 | for (int i = 0, pos = ver * 4 + 10; i < numAlign - 1; i++, pos -= step) |
| 514 | result.insert(result.begin(), pos); |
| 515 | result.insert(result.begin(), 6); |
| 516 | return result; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | |
| 521 | int QrCode::getNumRawDataModules(int ver) { |
| 522 | if (ver < MIN_VERSION || ver > MAX_VERSION) |
| 523 | throw "Version number out of range"; |
| 524 | int result = (16 * ver + 128) * ver + 64; |
| 525 | if (ver >= 2) { |
| 526 | int numAlign = ver / 7 + 2; |
| 527 | result -= (25 * numAlign - 10) * numAlign - 55; |
| 528 | if (ver >= 7) |
| 529 | result -= 18 * 2; // Subtract version information |
| 530 | } |
| 531 | return result; |
| 532 | } |
| 533 | |
| 534 | |
| 535 | int QrCode::getNumDataCodewords(int ver, Ecc ecl) { |
| 536 | if (ver < MIN_VERSION || ver > MAX_VERSION) |
| 537 | throw "Version number out of range"; |
| 538 | return getNumRawDataModules(ver) / 8 |
| 539 | - ECC_CODEWORDS_PER_BLOCK[static_cast<int>(ecl)][ver] |
| 540 | * NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(ecl)][ver]; |
| 541 | } |
| 542 | |
| 543 | |
| 544 | /*---- Tables of constants ----*/ |
| 545 | |
| 546 | const int QrCode::PENALTY_N1 = 3; |
| 547 | const int QrCode::PENALTY_N2 = 3; |
| 548 | const int QrCode::PENALTY_N3 = 40; |
| 549 | const int QrCode::PENALTY_N4 = 10; |
| 550 | |
| 551 | |
| 552 | const int8_t QrCode::ECC_CODEWORDS_PER_BLOCK[4][41] = { |
| 553 | // Version: (note that index 0 is for padding, and is set to an illegal value) |
| 554 | //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level |
| 555 | {-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Low |
| 556 | {-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28}, // Medium |
| 557 | {-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Quartile |
| 558 | {-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // High |
| 559 | }; |
| 560 | |
| 561 | const int8_t QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = { |
| 562 | // Version: (note that index 0 is for padding, and is set to an illegal value) |
| 563 | //0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level |
| 564 | {-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low |
| 565 | {-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium |
| 566 | {-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile |
| 567 | {-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High |
| 568 | }; |
| 569 | |
| 570 | |
| 571 | QrCode::ReedSolomonGenerator::ReedSolomonGenerator(int degree) : |
| 572 | coefficients() { |
| 573 | if (degree < 1 || degree > 255) |
| 574 | throw "Degree out of range"; |
| 575 | |
| 576 | // Start with the monomial x^0 |
| 577 | coefficients.resize(degree); |
| 578 | coefficients.at(degree - 1) = 1; |
| 579 | |
| 580 | // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}), |
| 581 | // drop the highest term, and store the rest of the coefficients in order of descending powers. |
| 582 | // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D). |
| 583 | uint8_t root = 1; |
| 584 | for (int i = 0; i < degree; i++) { |
| 585 | // Multiply the current product by (x - r^i) |
| 586 | for (size_t j = 0; j < coefficients.size(); j++) { |
| 587 | coefficients.at(j) = multiply(coefficients.at(j), root); |
| 588 | if (j + 1 < coefficients.size()) |
| 589 | coefficients.at(j) ^= coefficients.at(j + 1); |
| 590 | } |
| 591 | root = multiply(root, 0x02); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | |
| 596 | vector<uint8_t> QrCode::ReedSolomonGenerator::getRemainder(const vector<uint8_t> &data) const { |
| 597 | // Compute the remainder by performing polynomial division |
| 598 | vector<uint8_t> result(coefficients.size()); |
| 599 | for (uint8_t b : data) { |
| 600 | uint8_t factor = b ^ result.at(0); |
| 601 | result.erase(result.begin()); |
| 602 | result.push_back(0); |
| 603 | for (size_t j = 0; j < result.size(); j++) |
| 604 | result.at(j) ^= multiply(coefficients.at(j), factor); |
| 605 | } |
| 606 | return result; |
| 607 | } |
| 608 | |
| 609 | |
| 610 | uint8_t QrCode::ReedSolomonGenerator::multiply(uint8_t x, uint8_t y) { |
| 611 | // Russian peasant multiplication |
| 612 | int z = 0; |
| 613 | for (int i = 7; i >= 0; i--) { |
| 614 | z = (z << 1) ^ ((z >> 7) * 0x11D); |
| 615 | z ^= ((y >> i) & 1) * x; |
| 616 | } |
| 617 | if (z >> 8 != 0) |
| 618 | throw "Assertion error"; |
| 619 | return static_cast<uint8_t>(z); |
| 620 | } |
| 621 | |
| 622 | } |