diff --git a/cpp/QrCodeGeneratorWorker.cpp b/cpp/QrCodeGeneratorWorker.cpp new file mode 100644 index 0000000..2b58f0a --- /dev/null +++ b/cpp/QrCodeGeneratorWorker.cpp @@ -0,0 +1,107 @@ +/* + * QR Code generator test worker (C++) + * + * This program reads data and encoding parameters from standard input and writes + * QR Code bitmaps to standard output. The I/O format is one integer per line. + * Run with no command line arguments. The program is intended for automated + * batch testing of end-to-end functionality of this QR Code generator library. + * + * Copyright (c) Project Nayuki + * https://www.nayuki.io/page/qr-code-generator-library + * + * (MIT License) + * 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 +#include +#include +#include +#include +#include "QrCode.hpp" + + +static const qrcodegen::QrCode::Ecc *(ECC_LEVELS[]) = { + &qrcodegen::QrCode::Ecc::LOW, + &qrcodegen::QrCode::Ecc::MEDIUM, + &qrcodegen::QrCode::Ecc::QUARTILE, + &qrcodegen::QrCode::Ecc::HIGH, +}; + + +int main() { + while (true) { + + // Read data length or exit + int length; + std::cin >> length; + if (length == -1) + break; + + // Read data bytes + bool isAscii = true; + std::vector data; + for (int i = 0; i < length; i++) { + int b; + std::cin >> b; + data.push_back((uint8_t)b); + isAscii &= 0 < b && b < 128; + } + + // Read encoding parameters + int errCorLvl, minVersion, maxVersion, mask, boostEcl; + std::cin >> errCorLvl; + std::cin >> minVersion; + std::cin >> maxVersion; + std::cin >> mask; + std::cin >> boostEcl; + + // Make list of segments + std::vector segs; + if (isAscii) { + std::vector text; + for (std::vector::iterator it = data.begin(); it != data.end(); ++it) + text.push_back((char)*it); + text.push_back('\0'); + segs = qrcodegen::QrSegment::makeSegments(text.data()); + } else + segs.push_back(qrcodegen::QrSegment::makeBytes(data)); + + // Try to make QR Code symbol + try { + const qrcodegen::QrCode qr = qrcodegen::QrCode::encodeSegments(segs, + *ECC_LEVELS[errCorLvl], minVersion, maxVersion, mask, boostEcl == 1); + + // Print grid of modules + std::cout << qr.version << std::endl; + for (int y = 0; y < qr.size; y++) { + for (int x = 0; x < qr.size; x++) + std::cout << qr.getModule(x, y) << std::endl; + } + + } catch (const char *msg) { + if (strcmp(msg, "Data too long") == 0) + std::cout << -1 << std::endl; + else { + std::cerr << msg << std::endl; + return EXIT_FAILURE; + } + } + std::cout << std::flush; + } + return EXIT_SUCCESS; +} diff --git a/python/qrcodegen-batch-test.py b/python/qrcodegen-batch-test.py index c64976e..2b10f11 100644 --- a/python/qrcodegen-batch-test.py +++ b/python/qrcodegen-batch-test.py @@ -35,6 +35,7 @@ CHILD_PROGRAMS = [ ["python", "qrcodegen-worker.py"], ["java", "io/nayuki/qrcodegen/QrCodeGeneratorWorker"], ["./qrcodegen-worker"], + ["./QrCodeGeneratorWorker"], ]