34qrcodegen::QrCode::Ecc::Ecc(
int ord,
int fb) :
52 std::vector<QrSegment> segs;
54 return encodeSegments(segs, ecl);
59 int minVersion,
int maxVersion,
int mask,
bool boostEcl) {
60 if (!(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40) || mask < -1 || mask > 7)
61 throw "Invalid value";
64 int version, dataUsedBits;
65 for (version = minVersion; ; version++) {
66 int dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
68 if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)
70 if (version >= maxVersion)
71 throw "Data too long";
73 if (dataUsedBits == -1)
74 throw "Assertion error";
77 const Ecc *newEcl = &ecl;
79 if (dataUsedBits <= getNumDataCodewords(version, Ecc::MEDIUM ) * 8) newEcl = &Ecc::MEDIUM ;
80 if (dataUsedBits <= getNumDataCodewords(version, Ecc::QUARTILE) * 8) newEcl = &Ecc::QUARTILE;
81 if (dataUsedBits <= getNumDataCodewords(version, Ecc::HIGH ) * 8) newEcl = &Ecc::HIGH ;
85 int dataCapacityBits = getNumDataCodewords(version, *newEcl) * 8;
87 for (
size_t i = 0; i < segs.size(); i++) {
99 for (uint8_t padByte = 0xEC; bb.
getBitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
102 throw "Assertion error";
112 size(1 <= ver && ver <= 40 ? ver * 4 + 17 : -1),
113 errorCorrectionLevel(ecl) {
116 if (ver < 1 || ver > 40 || mask < -1 || mask > 7)
117 throw "Value out of range";
119 std::vector<bool> row(
size);
120 for (
int i = 0; i <
size; i++) {
121 modules.push_back(row);
122 isFunction.push_back(row);
126 drawFunctionPatterns();
127 const std::vector<uint8_t> allCodewords(appendErrorCorrection(dataCodewords));
128 drawCodewords(allCodewords);
129 this->mask = handleConstructorMasking(mask);
137 errorCorrectionLevel(qr.errorCorrectionLevel) {
140 if (mask < -1 || mask > 7)
141 throw "Mask value out of range";
144 modules = qr.modules;
145 isFunction = qr.isFunction;
149 this->mask = handleConstructorMasking(mask);
159 if (0 <= x && x < size && 0 <= y && y < size)
160 return modules.at(y).at(x) ? 1 : 0;
168 throw "Border must be non-negative";
169 std::ostringstream sb;
170 sb <<
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
171 sb <<
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
172 sb <<
"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 ";
173 sb << (size + border * 2) <<
" " << (size + border * 2) <<
"\">\n";
174 sb <<
"\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\" stroke-width=\"0\"/>\n";
175 sb <<
"\t<path d=\"";
177 for (
int y = -border; y < size + border; y++) {
178 for (
int x = -border; x < size + border; x++) {
179 if (getModule(x, y) == 1) {
184 sb <<
"M" << (x + border) <<
"," << (y + border) <<
"h1v1h-1z";
188 sb <<
"\" fill=\"#000000\" stroke-width=\"0\"/>\n";
194void qrcodegen::QrCode::drawFunctionPatterns() {
196 for (
int i = 0; i < size; i++) {
197 setFunctionModule(6, i, i % 2 == 0);
198 setFunctionModule(i, 6, i % 2 == 0);
202 drawFinderPattern(3, 3);
203 drawFinderPattern(size - 4, 3);
204 drawFinderPattern(3, size - 4);
207 const std::vector<int> alignPatPos(getAlignmentPatternPositions(version));
208 int numAlign = alignPatPos.size();
209 for (
int i = 0; i < numAlign; i++) {
210 for (
int j = 0; j < numAlign; j++) {
211 if ((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0))
214 drawAlignmentPattern(alignPatPos.at(i), alignPatPos.at(j));
224void qrcodegen::QrCode::drawFormatBits(
int mask) {
226 int data = errorCorrectionLevel.formatBits << 3 | mask;
228 for (
int i = 0; i < 10; i++)
229 rem = (rem << 1) ^ ((rem >> 9) * 0x537);
233 throw "Assertion error";
236 for (
int i = 0; i <= 5; i++)
237 setFunctionModule(8, i, ((data >> i) & 1) != 0);
238 setFunctionModule(8, 7, ((data >> 6) & 1) != 0);
239 setFunctionModule(8, 8, ((data >> 7) & 1) != 0);
240 setFunctionModule(7, 8, ((data >> 8) & 1) != 0);
241 for (
int i = 9; i < 15; i++)
242 setFunctionModule(14 - i, 8, ((data >> i) & 1) != 0);
245 for (
int i = 0; i <= 7; i++)
246 setFunctionModule(size - 1 - i, 8, ((data >> i) & 1) != 0);
247 for (
int i = 8; i < 15; i++)
248 setFunctionModule(8, size - 15 + i, ((data >> i) & 1) != 0);
249 setFunctionModule(8, size - 8,
true);
253void qrcodegen::QrCode::drawVersion() {
259 for (
int i = 0; i < 12; i++)
260 rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
261 int data = version << 12 | rem;
263 throw "Assertion error";
266 for (
int i = 0; i < 18; i++) {
267 bool bit = ((
data >> i) & 1) != 0;
268 int a =
size - 11 + i % 3, b = i / 3;
269 setFunctionModule(a, b, bit);
270 setFunctionModule(b, a, bit);
275void qrcodegen::QrCode::drawFinderPattern(
int x,
int y) {
276 for (
int i = -4; i <= 4; i++) {
277 for (
int j = -4; j <= 4; j++) {
278 int dist = std::max(std::abs(i), std::abs(j));
279 int xx = x + j, yy = y + i;
280 if (0 <= xx && xx < size && 0 <= yy && yy < size)
281 setFunctionModule(xx, yy, dist != 2 && dist != 4);
287void qrcodegen::QrCode::drawAlignmentPattern(
int x,
int y) {
288 for (
int i = -2; i <= 2; i++) {
289 for (
int j = -2; j <= 2; j++)
290 setFunctionModule(x + j, y + i, std::max(std::abs(i), std::abs(j)) != 1);
295void qrcodegen::QrCode::setFunctionModule(
int x,
int y,
bool isBlack) {
296 modules.at(y).at(x) = isBlack;
297 isFunction.at(y).at(x) =
true;
301std::vector<uint8_t> qrcodegen::QrCode::appendErrorCorrection(
const std::vector<uint8_t> &data)
const {
302 if (
data.size() !=
static_cast<unsigned int>(getNumDataCodewords(version, errorCorrectionLevel)))
303 throw "Invalid argument";
306 int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.ordinal][version];
307 int totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[errorCorrectionLevel.ordinal][version];
308 if (totalEcc % numBlocks != 0)
309 throw "Assertion error";
310 int blockEccLen = totalEcc / numBlocks;
311 int numShortBlocks = numBlocks - getNumRawDataModules(version) / 8 % numBlocks;
312 int shortBlockLen = getNumRawDataModules(version) / 8 / numBlocks;
315 std::vector<std::vector<uint8_t> > blocks;
316 const ReedSolomonGenerator rs(blockEccLen);
317 for (
int i = 0, k = 0; i < numBlocks; i++) {
318 std::vector<uint8_t> dat;
319 dat.insert(dat.begin(),
data.begin() + k,
data.begin() + (k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1)));
321 const std::vector<uint8_t> ecc(rs.getRemainder(dat));
322 if (i < numShortBlocks)
324 dat.insert(dat.end(), ecc.begin(), ecc.end());
325 blocks.push_back(dat);
329 std::vector<uint8_t>
result;
330 for (
int i = 0;
static_cast<unsigned int>(i) < blocks.at(0).
size(); i++) {
331 for (
int j = 0;
static_cast<unsigned int>(j) < blocks.size(); j++) {
333 if (i != shortBlockLen - blockEccLen || j >= numShortBlocks)
334 result.push_back(blocks.at(j).at(i));
337 if (
result.size() !=
static_cast<unsigned int>(getNumRawDataModules(version) / 8))
338 throw "Assertion error";
343void qrcodegen::QrCode::drawCodewords(
const std::vector<uint8_t> &data) {
344 if (
data.size() !=
static_cast<unsigned int>(getNumRawDataModules(version) / 8))
345 throw "Invalid argument";
349 for (
int right = size - 1; right >= 1; right -= 2) {
352 for (
int vert = 0; vert <
size; vert++) {
353 for (
int j = 0; j < 2; j++) {
355 bool upwards = ((right & 2) == 0) ^ (x < 6);
356 int y = upwards ?
size - 1 - vert : vert;
357 if (!isFunction.at(y).at(x) && i <
data.size() * 8) {
358 modules.at(y).at(x) = ((
data.at(i >> 3) >> (7 - (i & 7))) & 1) != 0;
366 if (
static_cast<unsigned int>(i) !=
data.size() * 8)
367 throw "Assertion error";
371void qrcodegen::QrCode::applyMask(
int mask) {
372 if (mask < 0 || mask > 7)
373 throw "Mask value out of range";
374 for (
int y = 0; y <
size; y++) {
375 for (
int x = 0; x <
size; x++) {
378 case 0: invert = (x + y) % 2 == 0;
break;
379 case 1: invert = y % 2 == 0;
break;
380 case 2: invert = x % 3 == 0;
break;
381 case 3: invert = (x + y) % 3 == 0;
break;
382 case 4: invert = (x / 3 + y / 2) % 2 == 0;
break;
383 case 5: invert = x * y % 2 + x * y % 3 == 0;
break;
384 case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0;
break;
385 case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0;
break;
386 default:
throw "Assertion error";
388 modules.at(y).at(x) = modules.at(y).at(x) ^ (invert & !isFunction.at(y).at(x));
394int qrcodegen::QrCode::handleConstructorMasking(
int mask) {
396 int32_t minPenalty = INT32_MAX;
397 for (
int i = 0; i < 8; i++) {
400 int penalty = getPenaltyScore();
401 if (penalty < minPenalty) {
403 minPenalty = penalty;
408 if (mask < 0 || mask > 7)
409 throw "Assertion error";
410 drawFormatBits(mask);
416int qrcodegen::QrCode::getPenaltyScore()
const {
420 for (
int y = 0; y <
size; y++) {
421 bool colorX = modules.at(y).at(0);
422 for (
int x = 1, runX = 1; x <
size; x++) {
423 if (modules.at(y).at(x) != colorX) {
424 colorX = modules.at(y).at(x);
436 for (
int x = 0; x <
size; x++) {
437 bool colorY = modules.at(0).at(x);
438 for (
int y = 1, runY = 1; y <
size; y++) {
439 if (modules.at(y).at(x) != colorY) {
440 colorY = modules.at(y).at(x);
453 for (
int y = 0; y <
size - 1; y++) {
454 for (
int x = 0; x <
size - 1; x++) {
455 bool color = modules.at(y).at(x);
456 if ( color == modules.at(y).at(x + 1) &&
457 color == modules.at(y + 1).at(x) &&
458 color == modules.at(y + 1).at(x + 1))
464 for (
int y = 0; y <
size; y++) {
465 for (
int x = 0, bits = 0; x <
size; x++) {
466 bits = ((bits << 1) & 0x7FF) | (modules.at(y).at(x) ? 1 : 0);
467 if (x >= 10 && (bits == 0x05D || bits == 0x5D0))
472 for (
int x = 0; x <
size; x++) {
473 for (
int y = 0, bits = 0; y <
size; y++) {
474 bits = ((bits << 1) & 0x7FF) | (modules.at(y).at(x) ? 1 : 0);
475 if (y >= 10 && (bits == 0x05D || bits == 0x5D0))
482 for (
int y = 0; y <
size; y++) {
483 for (
int x = 0; x <
size; x++) {
484 if (modules.at(y).at(x))
490 for (
int k = 0; black*20 < (9-k)*total || black*20 > (11+k)*total; k++)
496std::vector<int> qrcodegen::QrCode::getAlignmentPatternPositions(
int ver) {
497 if (ver < 1 || ver > 40)
498 throw "Version number out of range";
500 return std::vector<int>();
502 int numAlign = ver / 7 + 2;
505 step = (ver * 4 + numAlign * 2 + 1) / (2 * numAlign - 2) * 2;
510 int size = ver * 4 + 17;
511 for (
int i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step)
519int qrcodegen::QrCode::getNumRawDataModules(
int ver) {
520 if (ver < 1 || ver > 40)
521 throw "Version number out of range";
522 int result = (16 * ver + 128) * ver + 64;
524 int numAlign = ver / 7 + 2;
525 result -= (25 * numAlign - 10) * numAlign - 55;
533int qrcodegen::QrCode::getNumDataCodewords(
int ver,
const Ecc &ecl) {
534 if (ver < 1 || ver > 40)
535 throw "Version number out of range";
536 return getNumRawDataModules(ver) / 8 - NUM_ERROR_CORRECTION_CODEWORDS[ecl.ordinal][ver];
542const int qrcodegen::QrCode::PENALTY_N1 = 3;
543const int qrcodegen::QrCode::PENALTY_N2 = 3;
544const int qrcodegen::QrCode::PENALTY_N3 = 40;
545const int qrcodegen::QrCode::PENALTY_N4 = 10;
548const int16_t qrcodegen::QrCode::NUM_ERROR_CORRECTION_CODEWORDS[4][41] = {
551 {-1, 7, 10, 15, 20, 26, 36, 40, 48, 60, 72, 80, 96, 104, 120, 132, 144, 168, 180, 196, 224, 224, 252, 270, 300, 312, 336, 360, 390, 420, 450, 480, 510, 540, 570, 570, 600, 630, 660, 720, 750},
552 {-1, 10, 16, 26, 36, 48, 64, 72, 88, 110, 130, 150, 176, 198, 216, 240, 280, 308, 338, 364, 416, 442, 476, 504, 560, 588, 644, 700, 728, 784, 812, 868, 924, 980, 1036, 1064, 1120, 1204, 1260, 1316, 1372},
553 {-1, 13, 22, 36, 52, 72, 96, 108, 132, 160, 192, 224, 260, 288, 320, 360, 408, 448, 504, 546, 600, 644, 690, 750, 810, 870, 952, 1020, 1050, 1140, 1200, 1290, 1350, 1440, 1530, 1590, 1680, 1770, 1860, 1950, 2040},
554 {-1, 17, 28, 44, 64, 88, 112, 130, 156, 192, 224, 264, 308, 352, 384, 432, 480, 532, 588, 650, 700, 750, 816, 900, 960, 1050, 1110, 1200, 1260, 1350, 1440, 1530, 1620, 1710, 1800, 1890, 1980, 2100, 2220, 2310, 2430},
557const int8_t qrcodegen::QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
560 {-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},
561 {-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},
562 {-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},
563 {-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},
567qrcodegen::QrCode::ReedSolomonGenerator::ReedSolomonGenerator(
int degree) :
569 if (degree < 1 || degree > 255)
570 throw "Degree out of range";
573 coefficients.resize(degree);
574 coefficients.at(degree - 1) = 1;
580 for (
int i = 0; i < degree; i++) {
582 for (
size_t j = 0; j < coefficients.size(); j++) {
583 coefficients.at(j) = multiply(coefficients.at(j),
static_cast<uint8_t
>(root));
584 if (j + 1 < coefficients.size())
585 coefficients.at(j) ^= coefficients.at(j + 1);
587 root = (root << 1) ^ ((root >> 7) * 0x11D);
592std::vector<uint8_t> qrcodegen::QrCode::ReedSolomonGenerator::getRemainder(
const std::vector<uint8_t> &data)
const {
594 std::vector<uint8_t>
result(coefficients.size());
595 for (
size_t i = 0; i <
data.size(); i++) {
599 for (
size_t j = 0; j <
result.size(); j++)
600 result.at(j) ^= multiply(coefficients.at(j), factor);
606uint8_t qrcodegen::QrCode::ReedSolomonGenerator::multiply(uint8_t x, uint8_t y) {
609 for (
int i = 7; i >= 0; i--) {
610 z = (z << 1) ^ ((z >> 7) * 0x11D);
611 z ^= ((y >> i) & 1) * x;
614 throw "Assertion error";
615 return static_cast<uint8_t
>(z);
std::vector< uint8_t > getBytes() const
void appendData(const QrSegment &seg)
void appendBits(uint32_t val, int len)
static const Ecc QUARTILE
QrCode(int ver, const Ecc &ecl, const std::vector< uint8_t > &dataCodewords, int mask)
static QrCode encodeText(const char *text, int version, const Ecc &ecl)
static QrCode encodeBinary(const std::vector< uint8_t > &data, const Ecc &ecl)
int getModule(int x, int y) const
std::string toSvgString(int border) const
static QrCode encodeSegments(const std::vector< QrSegment > &segs, const Ecc &ecl, int minVersion=1, int maxVersion=40, int mask=-1, bool boostEcl=true)
int numCharCountBits(int ver) const
static std::vector< QrSegment > makeSegments(const char *text)
static int getTotalBits(const std::vector< QrSegment > &segs, int version)
static QrSegment makeBytes(const std::vector< uint8_t > &data)