2016-04-15 04:26:37 +00:00
|
|
|
/*
|
|
|
|
* QR Code generator library (C++)
|
|
|
|
*
|
2017-04-24 20:41:54 +00:00
|
|
|
* Copyright (c) Project Nayuki. (MIT License)
|
2016-04-15 04:26:37 +00:00
|
|
|
* https://www.nayuki.io/page/qr-code-generator-library
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
* - The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
* - The Software is provided "as is", without warranty of any kind, express or
|
|
|
|
* implied, including but not limited to the warranties of merchantability,
|
|
|
|
* fitness for a particular purpose and noninfringement. In no event shall the
|
|
|
|
* authors or copyright holders be liable for any claim, damages or other
|
|
|
|
* liability, whether in an action of contract, tort or otherwise, arising from,
|
|
|
|
* out of or in connection with the Software or the use or other dealings in the
|
|
|
|
* Software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <climits>
|
|
|
|
#include <cstddef>
|
2017-05-05 20:57:38 +00:00
|
|
|
#include <cstdlib>
|
2016-04-15 04:26:37 +00:00
|
|
|
#include <sstream>
|
2018-08-21 23:12:42 +00:00
|
|
|
#include <stdexcept>
|
2017-08-18 03:39:51 +00:00
|
|
|
#include <utility>
|
2016-04-15 04:26:37 +00:00
|
|
|
#include "BitBuffer.hpp"
|
|
|
|
#include "QrCode.hpp"
|
|
|
|
|
2017-05-05 20:57:13 +00:00
|
|
|
using std::int8_t;
|
|
|
|
using std::uint8_t;
|
|
|
|
using std::size_t;
|
2017-05-05 21:03:08 +00:00
|
|
|
using std::vector;
|
2017-05-05 20:57:13 +00:00
|
|
|
|
2016-04-15 04:26:37 +00:00
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
namespace qrcodegen {
|
|
|
|
|
2018-02-26 19:53:57 +00:00
|
|
|
int QrCode::getFormatBits(Ecc ecl) {
|
|
|
|
switch (ecl) {
|
|
|
|
case Ecc::LOW : return 1;
|
|
|
|
case Ecc::MEDIUM : return 0;
|
|
|
|
case Ecc::QUARTILE: return 3;
|
|
|
|
case Ecc::HIGH : return 2;
|
2018-08-21 23:12:42 +00:00
|
|
|
default: throw std::logic_error("Assertion error");
|
2018-02-26 19:53:57 +00:00
|
|
|
}
|
2017-09-06 03:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-06 04:21:56 +00:00
|
|
|
QrCode QrCode::encodeText(const char *text, Ecc ecl) {
|
2018-02-26 20:22:56 +00:00
|
|
|
vector<QrSegment> segs = QrSegment::makeSegments(text);
|
2016-04-15 04:26:37 +00:00
|
|
|
return encodeSegments(segs, ecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-06 04:21:56 +00:00
|
|
|
QrCode QrCode::encodeBinary(const vector<uint8_t> &data, Ecc ecl) {
|
2017-08-18 00:17:11 +00:00
|
|
|
vector<QrSegment> segs{QrSegment::makeBytes(data)};
|
2016-04-15 04:26:37 +00:00
|
|
|
return encodeSegments(segs, ecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-06 04:21:56 +00:00
|
|
|
QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, Ecc ecl,
|
2016-04-16 03:53:58 +00:00
|
|
|
int minVersion, int maxVersion, int mask, bool boostEcl) {
|
2017-10-23 04:42:53 +00:00
|
|
|
if (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::invalid_argument("Invalid value");
|
2016-04-16 03:53:58 +00:00
|
|
|
|
2016-04-15 04:26:37 +00:00
|
|
|
// Find the minimal version number to use
|
2016-04-16 03:53:58 +00:00
|
|
|
int version, dataUsedBits;
|
|
|
|
for (version = minVersion; ; version++) {
|
|
|
|
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; // Number of data bits available
|
|
|
|
dataUsedBits = QrSegment::getTotalBits(segs, version);
|
|
|
|
if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)
|
2016-04-15 04:26:37 +00:00
|
|
|
break; // This version number is found to be suitable
|
2016-04-16 03:53:58 +00:00
|
|
|
if (version >= maxVersion) // All versions in the range could not fit the given data
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::length_error("Data too long");
|
2016-04-16 03:53:58 +00:00
|
|
|
}
|
|
|
|
if (dataUsedBits == -1)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::logic_error("Assertion error");
|
2016-04-16 03:53:58 +00:00
|
|
|
|
|
|
|
// Increase the error correction level while the data still fits in the current version number
|
2018-08-22 19:22:00 +00:00
|
|
|
for (Ecc newEcl : vector<Ecc>{Ecc::MEDIUM, Ecc::QUARTILE, Ecc::HIGH}) { // From low to high
|
2017-09-06 04:21:56 +00:00
|
|
|
if (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8)
|
|
|
|
ecl = newEcl;
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 19:22:00 +00:00
|
|
|
// Concatenate all segments to create the data bit string
|
2017-09-06 04:21:56 +00:00
|
|
|
size_t dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
|
2016-04-15 04:26:37 +00:00
|
|
|
BitBuffer bb;
|
2017-08-17 21:04:21 +00:00
|
|
|
for (const QrSegment &seg : segs) {
|
2017-09-06 03:56:06 +00:00
|
|
|
bb.appendBits(seg.getMode().getModeBits(), 4);
|
|
|
|
bb.appendBits(seg.getNumChars(), seg.getMode().numCharCountBits(version));
|
|
|
|
bb.insert(bb.end(), seg.getData().begin(), seg.getData().end());
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add terminator and pad up to a byte if applicable
|
2017-08-18 00:17:11 +00:00
|
|
|
bb.appendBits(0, std::min<size_t>(4, dataCapacityBits - bb.size()));
|
2017-08-17 21:18:31 +00:00
|
|
|
bb.appendBits(0, (8 - bb.size() % 8) % 8);
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Pad with alternate bytes until data capacity is reached
|
2017-08-17 21:18:31 +00:00
|
|
|
for (uint8_t padByte = 0xEC; bb.size() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
|
2016-04-15 04:26:37 +00:00
|
|
|
bb.appendBits(padByte, 8);
|
2017-08-17 21:18:31 +00:00
|
|
|
if (bb.size() % 8 != 0)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::logic_error("Assertion error");
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Create the QR Code symbol
|
2017-09-06 04:21:56 +00:00
|
|
|
return QrCode(version, ecl, bb.getBytes(), mask);
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-06 04:21:56 +00:00
|
|
|
QrCode::QrCode(int ver, Ecc ecl, const vector<uint8_t> &dataCodewords, int mask) :
|
2017-09-06 04:24:19 +00:00
|
|
|
// Initialize fields
|
2016-04-15 04:26:37 +00:00
|
|
|
version(ver),
|
2017-10-23 04:42:53 +00:00
|
|
|
size(MIN_VERSION <= ver && ver <= MAX_VERSION ? ver * 4 + 17 : -1), // Avoid signed overflow undefined behavior
|
2017-09-06 04:24:19 +00:00
|
|
|
errorCorrectionLevel(ecl),
|
|
|
|
modules(size, vector<bool>(size)), // Entirely white grid
|
|
|
|
isFunction(size, vector<bool>(size)) {
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Check arguments
|
2017-10-23 04:42:53 +00:00
|
|
|
if (ver < MIN_VERSION || ver > MAX_VERSION || mask < -1 || mask > 7)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::domain_error("Value out of range");
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Draw function patterns, draw all codewords, do masking
|
|
|
|
drawFunctionPatterns();
|
2018-02-26 20:22:56 +00:00
|
|
|
const vector<uint8_t> allCodewords = appendErrorCorrection(dataCodewords);
|
2016-04-15 04:26:37 +00:00
|
|
|
drawCodewords(allCodewords);
|
|
|
|
this->mask = handleConstructorMasking(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-06 04:03:52 +00:00
|
|
|
int QrCode::getVersion() const {
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int QrCode::getSize() const {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QrCode::Ecc QrCode::getErrorCorrectionLevel() const {
|
|
|
|
return errorCorrectionLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
int QrCode::getMask() const {
|
2016-04-15 04:26:37 +00:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-31 20:39:29 +00:00
|
|
|
bool QrCode::getModule(int x, int y) const {
|
|
|
|
return 0 <= x && x < size && 0 <= y && y < size && module(x, y);
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
std::string QrCode::toSvgString(int border) const {
|
2016-04-15 04:26:37 +00:00
|
|
|
if (border < 0)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::domain_error("Border must be non-negative");
|
2018-02-26 20:29:25 +00:00
|
|
|
if (border > INT_MAX / 2 || border * 2 > INT_MAX - size)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::overflow_error("Border too large");
|
2018-02-26 20:29:25 +00:00
|
|
|
|
2016-04-15 04:26:37 +00:00
|
|
|
std::ostringstream sb;
|
|
|
|
sb << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
|
|
|
sb << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
|
|
|
|
sb << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 ";
|
2017-07-26 20:34:29 +00:00
|
|
|
sb << (size + border * 2) << " " << (size + border * 2) << "\" stroke=\"none\">\n";
|
|
|
|
sb << "\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n";
|
2016-04-15 04:26:37 +00:00
|
|
|
sb << "\t<path d=\"";
|
|
|
|
bool head = true;
|
2018-08-22 19:32:03 +00:00
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
for (int x = 0; x < size; x++) {
|
2017-08-31 20:39:29 +00:00
|
|
|
if (getModule(x, y)) {
|
2016-04-15 04:26:37 +00:00
|
|
|
if (head)
|
|
|
|
head = false;
|
|
|
|
else
|
|
|
|
sb << " ";
|
|
|
|
sb << "M" << (x + border) << "," << (y + border) << "h1v1h-1z";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-26 20:34:29 +00:00
|
|
|
sb << "\" fill=\"#000000\"/>\n";
|
2016-04-15 04:26:37 +00:00
|
|
|
sb << "</svg>\n";
|
|
|
|
return sb.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
void QrCode::drawFunctionPatterns() {
|
2017-04-20 06:39:59 +00:00
|
|
|
// Draw horizontal and vertical timing patterns
|
2016-04-15 04:26:37 +00:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
setFunctionModule(6, i, i % 2 == 0);
|
|
|
|
setFunctionModule(i, 6, i % 2 == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
|
|
|
|
drawFinderPattern(3, 3);
|
|
|
|
drawFinderPattern(size - 4, 3);
|
|
|
|
drawFinderPattern(3, size - 4);
|
|
|
|
|
2017-04-20 06:39:59 +00:00
|
|
|
// Draw numerous alignment patterns
|
2018-02-26 20:22:56 +00:00
|
|
|
const vector<int> alignPatPos = getAlignmentPatternPositions(version);
|
2016-04-15 04:26:37 +00:00
|
|
|
int numAlign = alignPatPos.size();
|
|
|
|
for (int i = 0; i < numAlign; i++) {
|
|
|
|
for (int j = 0; j < numAlign; j++) {
|
|
|
|
if ((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0))
|
|
|
|
continue; // Skip the three finder corners
|
|
|
|
else
|
|
|
|
drawAlignmentPattern(alignPatPos.at(i), alignPatPos.at(j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw configuration data
|
|
|
|
drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
|
|
|
|
drawVersion();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
void QrCode::drawFormatBits(int mask) {
|
2016-04-15 04:26:37 +00:00
|
|
|
// Calculate error correction code and pack bits
|
2018-02-26 19:53:57 +00:00
|
|
|
int data = getFormatBits(errorCorrectionLevel) << 3 | mask; // errCorrLvl is uint2, mask is uint3
|
2016-04-15 04:26:37 +00:00
|
|
|
int rem = data;
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
rem = (rem << 1) ^ ((rem >> 9) * 0x537);
|
2018-08-22 19:48:29 +00:00
|
|
|
int bits = (data << 10 | rem) ^ 0x5412; // uint15
|
|
|
|
if (bits >> 15 != 0)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::logic_error("Assertion error");
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Draw first copy
|
|
|
|
for (int i = 0; i <= 5; i++)
|
2018-08-22 19:48:29 +00:00
|
|
|
setFunctionModule(8, i, getBit(bits, i));
|
|
|
|
setFunctionModule(8, 7, getBit(bits, 6));
|
|
|
|
setFunctionModule(8, 8, getBit(bits, 7));
|
|
|
|
setFunctionModule(7, 8, getBit(bits, 8));
|
2016-04-15 04:26:37 +00:00
|
|
|
for (int i = 9; i < 15; i++)
|
2018-08-22 19:48:29 +00:00
|
|
|
setFunctionModule(14 - i, 8, getBit(bits, i));
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Draw second copy
|
|
|
|
for (int i = 0; i <= 7; i++)
|
2018-08-22 19:48:29 +00:00
|
|
|
setFunctionModule(size - 1 - i, 8, getBit(bits, i));
|
2016-04-15 04:26:37 +00:00
|
|
|
for (int i = 8; i < 15; i++)
|
2018-08-22 19:48:29 +00:00
|
|
|
setFunctionModule(8, size - 15 + i, getBit(bits, i));
|
2018-08-22 19:22:00 +00:00
|
|
|
setFunctionModule(8, size - 8, true); // Always black
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
void QrCode::drawVersion() {
|
2016-04-15 04:26:37 +00:00
|
|
|
if (version < 7)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Calculate error correction code and pack bits
|
|
|
|
int rem = version; // version is uint6, in the range [7, 40]
|
|
|
|
for (int i = 0; i < 12; i++)
|
|
|
|
rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
|
2018-08-22 19:52:50 +00:00
|
|
|
long bits = (long)version << 12 | rem; // uint18
|
|
|
|
if (bits >> 18 != 0)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::logic_error("Assertion error");
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Draw two copies
|
|
|
|
for (int i = 0; i < 18; i++) {
|
2018-08-22 19:52:50 +00:00
|
|
|
bool bit = getBit(bits, i);
|
2018-08-22 19:56:24 +00:00
|
|
|
int a = size - 11 + i % 3;
|
|
|
|
int b = i / 3;
|
2016-04-15 04:26:37 +00:00
|
|
|
setFunctionModule(a, b, bit);
|
|
|
|
setFunctionModule(b, a, bit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
void QrCode::drawFinderPattern(int x, int y) {
|
2016-04-15 04:26:37 +00:00
|
|
|
for (int i = -4; i <= 4; i++) {
|
|
|
|
for (int j = -4; j <= 4; j++) {
|
|
|
|
int dist = std::max(std::abs(i), std::abs(j)); // Chebyshev/infinity norm
|
|
|
|
int xx = x + j, yy = y + i;
|
|
|
|
if (0 <= xx && xx < size && 0 <= yy && yy < size)
|
|
|
|
setFunctionModule(xx, yy, dist != 2 && dist != 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
void QrCode::drawAlignmentPattern(int x, int y) {
|
2016-04-15 04:26:37 +00:00
|
|
|
for (int i = -2; i <= 2; i++) {
|
|
|
|
for (int j = -2; j <= 2; j++)
|
|
|
|
setFunctionModule(x + j, y + i, std::max(std::abs(i), std::abs(j)) != 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
void QrCode::setFunctionModule(int x, int y, bool isBlack) {
|
2016-04-15 04:26:37 +00:00
|
|
|
modules.at(y).at(x) = isBlack;
|
|
|
|
isFunction.at(y).at(x) = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-17 23:41:01 +00:00
|
|
|
bool QrCode::module(int x, int y) const {
|
|
|
|
return modules.at(y).at(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-05 21:03:08 +00:00
|
|
|
vector<uint8_t> QrCode::appendErrorCorrection(const vector<uint8_t> &data) const {
|
2016-04-15 04:26:37 +00:00
|
|
|
if (data.size() != static_cast<unsigned int>(getNumDataCodewords(version, errorCorrectionLevel)))
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::invalid_argument("Invalid argument");
|
2016-04-21 01:57:58 +00:00
|
|
|
|
|
|
|
// Calculate parameter numbers
|
2018-02-26 19:53:57 +00:00
|
|
|
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(errorCorrectionLevel)][version];
|
|
|
|
int blockEccLen = ECC_CODEWORDS_PER_BLOCK[static_cast<int>(errorCorrectionLevel)][version];
|
2017-05-06 17:19:00 +00:00
|
|
|
int rawCodewords = getNumRawDataModules(version) / 8;
|
|
|
|
int numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
|
|
|
int shortBlockLen = rawCodewords / numBlocks;
|
2016-04-15 04:26:37 +00:00
|
|
|
|
2016-04-21 01:57:58 +00:00
|
|
|
// Split data into blocks and append ECC to each block
|
2017-05-05 21:03:08 +00:00
|
|
|
vector<vector<uint8_t> > blocks;
|
2016-04-21 01:57:58 +00:00
|
|
|
const ReedSolomonGenerator rs(blockEccLen);
|
2016-04-15 04:26:37 +00:00
|
|
|
for (int i = 0, k = 0; i < numBlocks; i++) {
|
2017-08-18 00:17:11 +00:00
|
|
|
vector<uint8_t> dat(data.cbegin() + k, data.cbegin() + (k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1)));
|
2016-04-15 04:26:37 +00:00
|
|
|
k += dat.size();
|
2018-02-26 20:22:56 +00:00
|
|
|
const vector<uint8_t> ecc = rs.getRemainder(dat);
|
2016-04-15 04:26:37 +00:00
|
|
|
if (i < numShortBlocks)
|
|
|
|
dat.push_back(0);
|
2017-08-18 00:17:11 +00:00
|
|
|
dat.insert(dat.end(), ecc.cbegin(), ecc.cend());
|
|
|
|
blocks.push_back(std::move(dat));
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 01:57:58 +00:00
|
|
|
// Interleave (not concatenate) the bytes from every block into a single sequence
|
2017-05-05 21:03:08 +00:00
|
|
|
vector<uint8_t> result;
|
2016-04-15 04:26:37 +00:00
|
|
|
for (int i = 0; static_cast<unsigned int>(i) < blocks.at(0).size(); i++) {
|
|
|
|
for (int j = 0; static_cast<unsigned int>(j) < blocks.size(); j++) {
|
2016-04-21 01:57:58 +00:00
|
|
|
// Skip the padding byte in short blocks
|
|
|
|
if (i != shortBlockLen - blockEccLen || j >= numShortBlocks)
|
2016-04-15 04:26:37 +00:00
|
|
|
result.push_back(blocks.at(j).at(i));
|
|
|
|
}
|
|
|
|
}
|
2017-05-06 17:19:00 +00:00
|
|
|
if (result.size() != static_cast<unsigned int>(rawCodewords))
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::logic_error("Assertion error");
|
2016-04-15 04:26:37 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-05 21:03:08 +00:00
|
|
|
void QrCode::drawCodewords(const vector<uint8_t> &data) {
|
2016-04-15 04:26:37 +00:00
|
|
|
if (data.size() != static_cast<unsigned int>(getNumRawDataModules(version) / 8))
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::invalid_argument("Invalid argument");
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
size_t i = 0; // Bit index into the data
|
|
|
|
// Do the funny zigzag scan
|
|
|
|
for (int right = size - 1; right >= 1; right -= 2) { // Index of right column in each column pair
|
|
|
|
if (right == 6)
|
|
|
|
right = 5;
|
|
|
|
for (int vert = 0; vert < size; vert++) { // Vertical counter
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
int x = right - j; // Actual x coordinate
|
2017-04-20 04:18:22 +00:00
|
|
|
bool upward = ((right + 1) & 2) == 0;
|
2017-04-20 04:06:46 +00:00
|
|
|
int y = upward ? size - 1 - vert : vert; // Actual y coordinate
|
2016-04-15 04:26:37 +00:00
|
|
|
if (!isFunction.at(y).at(x) && i < data.size() * 8) {
|
2018-04-13 19:48:59 +00:00
|
|
|
modules.at(y).at(x) = getBit(data.at(i >> 3), 7 - static_cast<int>(i & 7));
|
2016-04-15 04:26:37 +00:00
|
|
|
i++;
|
|
|
|
}
|
2016-05-07 19:02:11 +00:00
|
|
|
// If there are any remainder bits (0 to 7), they are already
|
|
|
|
// set to 0/false/white when the grid of modules was initialized
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (static_cast<unsigned int>(i) != data.size() * 8)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::logic_error("Assertion error");
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
void QrCode::applyMask(int mask) {
|
2016-04-15 04:26:37 +00:00
|
|
|
if (mask < 0 || mask > 7)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::domain_error("Mask value out of range");
|
2016-04-15 04:26:37 +00:00
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
for (int x = 0; x < size; x++) {
|
|
|
|
bool invert;
|
|
|
|
switch (mask) {
|
|
|
|
case 0: invert = (x + y) % 2 == 0; break;
|
|
|
|
case 1: invert = y % 2 == 0; break;
|
|
|
|
case 2: invert = x % 3 == 0; break;
|
|
|
|
case 3: invert = (x + y) % 3 == 0; break;
|
|
|
|
case 4: invert = (x / 3 + y / 2) % 2 == 0; break;
|
|
|
|
case 5: invert = x * y % 2 + x * y % 3 == 0; break;
|
|
|
|
case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break;
|
|
|
|
case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;
|
2018-08-21 23:12:42 +00:00
|
|
|
default: throw std::logic_error("Assertion error");
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
modules.at(y).at(x) = modules.at(y).at(x) ^ (invert & !isFunction.at(y).at(x));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
int QrCode::handleConstructorMasking(int mask) {
|
2016-04-15 04:26:37 +00:00
|
|
|
if (mask == -1) { // Automatically choose best mask
|
2017-04-17 16:41:20 +00:00
|
|
|
long minPenalty = LONG_MAX;
|
2016-04-15 04:26:37 +00:00
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
drawFormatBits(i);
|
|
|
|
applyMask(i);
|
2017-04-17 16:41:20 +00:00
|
|
|
long penalty = getPenaltyScore();
|
2016-04-15 04:26:37 +00:00
|
|
|
if (penalty < minPenalty) {
|
|
|
|
mask = i;
|
|
|
|
minPenalty = penalty;
|
|
|
|
}
|
|
|
|
applyMask(i); // Undoes the mask due to XOR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mask < 0 || mask > 7)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::logic_error("Assertion error");
|
2016-04-15 04:26:37 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
long QrCode::getPenaltyScore() const {
|
2017-04-17 16:41:20 +00:00
|
|
|
long result = 0;
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Adjacent modules in row having same color
|
|
|
|
for (int y = 0; y < size; y++) {
|
2017-12-10 16:36:02 +00:00
|
|
|
bool colorX = false;
|
|
|
|
for (int x = 0, runX = -1; x < size; x++) {
|
2017-08-17 23:41:01 +00:00
|
|
|
if (x == 0 || module(x, y) != colorX) {
|
|
|
|
colorX = module(x, y);
|
2016-04-15 04:26:37 +00:00
|
|
|
runX = 1;
|
|
|
|
} else {
|
|
|
|
runX++;
|
|
|
|
if (runX == 5)
|
|
|
|
result += PENALTY_N1;
|
|
|
|
else if (runX > 5)
|
|
|
|
result++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Adjacent modules in column having same color
|
|
|
|
for (int x = 0; x < size; x++) {
|
2017-12-10 16:36:02 +00:00
|
|
|
bool colorY = false;
|
|
|
|
for (int y = 0, runY = -1; y < size; y++) {
|
2017-08-17 23:41:01 +00:00
|
|
|
if (y == 0 || module(x, y) != colorY) {
|
|
|
|
colorY = module(x, y);
|
2016-04-15 04:26:37 +00:00
|
|
|
runY = 1;
|
|
|
|
} else {
|
|
|
|
runY++;
|
|
|
|
if (runY == 5)
|
|
|
|
result += PENALTY_N1;
|
|
|
|
else if (runY > 5)
|
|
|
|
result++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2*2 blocks of modules having same color
|
|
|
|
for (int y = 0; y < size - 1; y++) {
|
|
|
|
for (int x = 0; x < size - 1; x++) {
|
2017-08-17 23:41:01 +00:00
|
|
|
bool color = module(x, y);
|
|
|
|
if ( color == module(x + 1, y) &&
|
|
|
|
color == module(x, y + 1) &&
|
|
|
|
color == module(x + 1, y + 1))
|
2016-04-15 04:26:37 +00:00
|
|
|
result += PENALTY_N2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finder-like pattern in rows
|
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
for (int x = 0, bits = 0; x < size; x++) {
|
2017-08-17 23:41:01 +00:00
|
|
|
bits = ((bits << 1) & 0x7FF) | (module(x, y) ? 1 : 0);
|
2016-04-15 04:26:37 +00:00
|
|
|
if (x >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated
|
|
|
|
result += PENALTY_N3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Finder-like pattern in columns
|
|
|
|
for (int x = 0; x < size; x++) {
|
|
|
|
for (int y = 0, bits = 0; y < size; y++) {
|
2017-08-17 23:41:01 +00:00
|
|
|
bits = ((bits << 1) & 0x7FF) | (module(x, y) ? 1 : 0);
|
2016-04-15 04:26:37 +00:00
|
|
|
if (y >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated
|
|
|
|
result += PENALTY_N3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Balance of black and white modules
|
|
|
|
int black = 0;
|
2017-08-18 00:17:11 +00:00
|
|
|
for (const vector<bool> &row : modules) {
|
|
|
|
for (bool color : row) {
|
|
|
|
if (color)
|
2016-04-15 04:26:37 +00:00
|
|
|
black++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int total = size * size;
|
|
|
|
// Find smallest k such that (45-5k)% <= dark/total <= (55+5k)%
|
2017-04-20 04:08:26 +00:00
|
|
|
for (int k = 0; black*20L < (9L-k)*total || black*20L > (11L+k)*total; k++)
|
2016-04-15 04:26:37 +00:00
|
|
|
result += PENALTY_N4;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-05 21:03:08 +00:00
|
|
|
vector<int> QrCode::getAlignmentPatternPositions(int ver) {
|
2017-10-23 04:42:53 +00:00
|
|
|
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::domain_error("Version number out of range");
|
2016-04-15 04:26:37 +00:00
|
|
|
else if (ver == 1)
|
2017-05-05 21:03:08 +00:00
|
|
|
return vector<int>();
|
2016-04-15 04:26:37 +00:00
|
|
|
else {
|
|
|
|
int numAlign = ver / 7 + 2;
|
2018-08-22 20:58:16 +00:00
|
|
|
int step = (ver == 32) ? 26 :
|
|
|
|
(ver*4 + numAlign*2 + 1) / (numAlign*2 - 2) * 2;
|
2016-04-15 04:26:37 +00:00
|
|
|
|
2017-05-05 21:03:08 +00:00
|
|
|
vector<int> result;
|
2017-08-18 00:44:24 +00:00
|
|
|
for (int i = 0, pos = ver * 4 + 10; i < numAlign - 1; i++, pos -= step)
|
2016-06-14 17:28:32 +00:00
|
|
|
result.insert(result.begin(), pos);
|
|
|
|
result.insert(result.begin(), 6);
|
2016-04-15 04:26:37 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
int QrCode::getNumRawDataModules(int ver) {
|
2017-10-23 04:42:53 +00:00
|
|
|
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::domain_error("Version number out of range");
|
2016-04-15 04:26:37 +00:00
|
|
|
int result = (16 * ver + 128) * ver + 64;
|
|
|
|
if (ver >= 2) {
|
|
|
|
int numAlign = ver / 7 + 2;
|
|
|
|
result -= (25 * numAlign - 10) * numAlign - 55;
|
|
|
|
if (ver >= 7)
|
|
|
|
result -= 18 * 2; // Subtract version information
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-06 04:21:56 +00:00
|
|
|
int QrCode::getNumDataCodewords(int ver, Ecc ecl) {
|
2017-10-23 04:42:53 +00:00
|
|
|
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::domain_error("Version number out of range");
|
2017-08-31 20:19:41 +00:00
|
|
|
return getNumRawDataModules(ver) / 8
|
2018-02-26 19:53:57 +00:00
|
|
|
- ECC_CODEWORDS_PER_BLOCK[static_cast<int>(ecl)][ver]
|
|
|
|
* NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(ecl)][ver];
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-13 19:48:59 +00:00
|
|
|
bool QrCode::getBit(long x, int i) {
|
|
|
|
return ((x >> i) & 1) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-15 04:26:37 +00:00
|
|
|
/*---- Tables of constants ----*/
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
const int QrCode::PENALTY_N1 = 3;
|
|
|
|
const int QrCode::PENALTY_N2 = 3;
|
|
|
|
const int QrCode::PENALTY_N3 = 40;
|
|
|
|
const int QrCode::PENALTY_N4 = 10;
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
const int8_t QrCode::ECC_CODEWORDS_PER_BLOCK[4][41] = {
|
2016-04-15 04:26:37 +00:00
|
|
|
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
2017-04-19 23:06:40 +00:00
|
|
|
//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
|
|
|
|
{-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
|
|
|
|
{-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
|
|
|
|
{-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
|
|
|
|
{-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
|
2016-04-15 04:26:37 +00:00
|
|
|
};
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
const int8_t QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
|
2016-04-15 04:26:37 +00:00
|
|
|
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
|
|
|
//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
|
|
|
|
{-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
|
|
|
|
{-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
|
|
|
|
{-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
|
|
|
|
{-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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
QrCode::ReedSolomonGenerator::ReedSolomonGenerator(int degree) :
|
2016-04-15 04:26:37 +00:00
|
|
|
coefficients() {
|
|
|
|
if (degree < 1 || degree > 255)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::domain_error("Degree out of range");
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Start with the monomial x^0
|
|
|
|
coefficients.resize(degree);
|
|
|
|
coefficients.at(degree - 1) = 1;
|
|
|
|
|
|
|
|
// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
|
|
|
|
// drop the highest term, and store the rest of the coefficients in order of descending powers.
|
|
|
|
// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
|
2017-05-08 05:21:05 +00:00
|
|
|
uint8_t root = 1;
|
2016-04-15 04:26:37 +00:00
|
|
|
for (int i = 0; i < degree; i++) {
|
|
|
|
// Multiply the current product by (x - r^i)
|
|
|
|
for (size_t j = 0; j < coefficients.size(); j++) {
|
2017-05-08 05:21:05 +00:00
|
|
|
coefficients.at(j) = multiply(coefficients.at(j), root);
|
2016-04-15 04:26:37 +00:00
|
|
|
if (j + 1 < coefficients.size())
|
|
|
|
coefficients.at(j) ^= coefficients.at(j + 1);
|
|
|
|
}
|
2017-05-08 05:21:05 +00:00
|
|
|
root = multiply(root, 0x02);
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-05 21:03:08 +00:00
|
|
|
vector<uint8_t> QrCode::ReedSolomonGenerator::getRemainder(const vector<uint8_t> &data) const {
|
2016-04-15 04:26:37 +00:00
|
|
|
// Compute the remainder by performing polynomial division
|
2017-05-05 21:03:08 +00:00
|
|
|
vector<uint8_t> result(coefficients.size());
|
2017-08-17 21:04:21 +00:00
|
|
|
for (uint8_t b : data) {
|
|
|
|
uint8_t factor = b ^ result.at(0);
|
2016-04-15 04:26:37 +00:00
|
|
|
result.erase(result.begin());
|
|
|
|
result.push_back(0);
|
|
|
|
for (size_t j = 0; j < result.size(); j++)
|
|
|
|
result.at(j) ^= multiply(coefficients.at(j), factor);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
uint8_t QrCode::ReedSolomonGenerator::multiply(uint8_t x, uint8_t y) {
|
2016-04-15 04:26:37 +00:00
|
|
|
// Russian peasant multiplication
|
|
|
|
int z = 0;
|
|
|
|
for (int i = 7; i >= 0; i--) {
|
|
|
|
z = (z << 1) ^ ((z >> 7) * 0x11D);
|
|
|
|
z ^= ((y >> i) & 1) * x;
|
|
|
|
}
|
|
|
|
if (z >> 8 != 0)
|
2018-08-21 23:12:42 +00:00
|
|
|
throw std::logic_error("Assertion error");
|
2016-04-15 04:26:37 +00:00
|
|
|
return static_cast<uint8_t>(z);
|
|
|
|
}
|
2017-04-21 05:30:59 +00:00
|
|
|
|
|
|
|
}
|