2 * QR Code generator test worker (C++)
4 * This program reads data and encoding parameters from standard input and writes
5 * QR Code bitmaps to standard output. The I/O format is one integer per line.
6 * Run with no command line arguments. The program is intended for automated
7 * batch testing of end-to-end functionality of this QR Code generator library.
9 * Copyright (c) Project Nayuki. (MIT License)
10 * https://www.nayuki.io/page/qr-code-generator-library
12 * Permission is hereby granted, free of charge, to any person obtaining a copy of
13 * this software and associated documentation files (the "Software"), to deal in
14 * the Software without restriction, including without limitation the rights to
15 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
16 * the Software, and to permit persons to whom the Software is furnished to do so,
17 * subject to the following conditions:
18 * - The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 * - The Software is provided "as is", without warranty of any kind, express or
21 * implied, including but not limited to the warranties of merchantability,
22 * fitness for a particular purpose and noninfringement. In no event shall the
23 * authors or copyright holders be liable for any claim, damages or other
24 * liability, whether in an action of contract, tort or otherwise, arising from,
25 * out of or in connection with the Software or the use or other dealings in the
37 using qrcodegen::QrCode;
38 using qrcodegen::QrSegment;
41 static const std::vector<QrCode::Ecc> ECC_LEVELS{
44 QrCode::Ecc::QUARTILE,
52 // Read data length or exit
60 std::vector<uint8_t> data;
61 for (int i = 0; i < length; i++) {
64 data.push_back(static_cast<uint8_t>(b));
65 isAscii &= 0 < b && b < 128;
68 // Read encoding parameters
69 int errCorLvl, minVersion, maxVersion, mask, boostEcl;
70 std::cin >> errCorLvl;
71 std::cin >> minVersion;
72 std::cin >> maxVersion;
76 // Make list of segments
77 std::vector<QrSegment> segs;
79 std::vector<char> text(data.cbegin(), data.cend());
81 segs = QrSegment::makeSegments(text.data());
83 segs.push_back(QrSegment::makeBytes(data));
85 try { // Try to make QR Code symbol
86 const QrCode qr = QrCode::encodeSegments(segs,
87 ECC_LEVELS.at(static_cast<std::size_t>(errCorLvl)), minVersion, maxVersion, mask, boostEcl == 1);
88 // Print grid of modules
89 std::cout << qr.getVersion() << std::endl;
90 for (int y = 0; y < qr.getSize(); y++) {
91 for (int x = 0; x < qr.getSize(); x++)
92 std::cout << (qr.getModule(x, y) ? 1 : 0) << std::endl;
95 } catch (const qrcodegen::data_too_long &ex) {
96 std::cout << -1 << std::endl;
98 std::cout << std::flush;