From 9293f970219244efcd57d6662e7f7a0111f0c18e Mon Sep 17 00:00:00 2001 From: dm1sh Date: Wed, 15 Dec 2021 17:33:11 +0300 Subject: [PATCH] Added QR code drawing on trit matrix. Increaced max version to 40 --- QRCodeLibrary/BitArray.hpp | 3 +- QRCodeLibrary/DataBlocks.hpp | 4 +- QRCodeLibrary/Encoder.cpp | 13 +- QRCodeLibrary/Encoder.hpp | 6 +- QRCodeLibrary/QRCode.cpp | 16 +- QRCodeLibrary/QRCode.hpp | 13 +- QRCodeLibrary/QRCodeLibrary.vcxproj | 2 + QRCodeLibrary/QRCodeLibrary.vcxproj.filters | 6 + QRCodeLibrary/QRMatrix.cpp | 179 ++++++++++++++++++++ QRCodeLibrary/QRMatrix.hpp | 37 ++++ QRCodeLibrary/Tables.hpp | 124 +++++++++++--- QRCodeLibrary/TritMatrix.cpp | 17 +- QRCodeLibrary/TritMatrix.hpp | 10 +- tests/Encoder_test.cpp | 3 + tests/QRCode_test.cpp | 26 ++- tests/QRMatrix_test.cpp | 46 +++++ tests/tests.vcxproj | 1 + 17 files changed, 453 insertions(+), 53 deletions(-) create mode 100644 QRCodeLibrary/QRMatrix.cpp create mode 100644 QRCodeLibrary/QRMatrix.hpp create mode 100644 tests/QRMatrix_test.cpp diff --git a/QRCodeLibrary/BitArray.hpp b/QRCodeLibrary/BitArray.hpp index 53d0dd6..cca56ac 100644 --- a/QRCodeLibrary/BitArray.hpp +++ b/QRCodeLibrary/BitArray.hpp @@ -12,7 +12,8 @@ constexpr unsigned ceil_div(unsigned a, unsigned b) { class BitArray { public: - BitArray(unsigned size_ = 0) : size{ size_ }, v(ceil_div(size_, 8)) {}; + BitArray(unsigned size_ = 0) : size{ size_ }, v(ceil_div(size_, 8), 0) {}; + BitArray(const vector& input) : size(input.size() * 8), v{ input } {}; operator std::string() const; diff --git a/QRCodeLibrary/DataBlocks.hpp b/QRCodeLibrary/DataBlocks.hpp index 3bfcc7f..10e5206 100644 --- a/QRCodeLibrary/DataBlocks.hpp +++ b/QRCodeLibrary/DataBlocks.hpp @@ -10,7 +10,7 @@ using namespace std; class DataBlocks { public: - DataBlocks(const vector& e_data_, CorrectionLevel corr_lvl_, unsigned char version_) : e_data{ e_data_ }, corr_lvl{ corr_lvl_ }, version{ version_ } {}; + DataBlocks(const vector& e_data_, CorrectionLevel corr_lvl_, char version_) : e_data{ e_data_ }, corr_lvl{ corr_lvl_ }, version{ version_ } {}; vector& compose_joined_data_and_EC_blocks(); @@ -21,7 +21,7 @@ public: private: const vector& e_data; CorrectionLevel corr_lvl; - unsigned char version; + char version; vector data; }; diff --git a/QRCodeLibrary/Encoder.cpp b/QRCodeLibrary/Encoder.cpp index e686e4e..e908325 100644 --- a/QRCodeLibrary/Encoder.cpp +++ b/QRCodeLibrary/Encoder.cpp @@ -30,11 +30,14 @@ BitArray& Encoder::encode() return e; } -unsigned char Encoder::determite_version(unsigned size, CorrectionLevel corr_lvl) +char Encoder::determite_version(unsigned size, CorrectionLevel corr_lvl) { const auto& sizes = Tables::max_capability.at(corr_lvl); - return upper_index(sizes, size); + char version = upper_index(sizes, size); + if (version > 39) throw std::runtime_error("Too much data. There is no such QR code version that can store so mush characters"); + + return version; } unsigned Encoder::calculate_encoded_input_size(unsigned input_size, QRCodeMethod method) @@ -64,7 +67,7 @@ unsigned Encoder::calculate_encoded_input_size(unsigned input_size, QRCodeMethod return bit_num; } -unsigned Encoder::calculate_metadata_size(QRCodeMethod method, unsigned char version) +unsigned Encoder::calculate_metadata_size(QRCodeMethod method, char version) { if (method == QRCodeMethod::Dynamic) throw std::runtime_error("Specify correct method"); @@ -72,7 +75,7 @@ unsigned Encoder::calculate_metadata_size(QRCodeMethod method, unsigned char ver auto lengths = Tables::data_amount_lengths.at(method); - for (int i = 0; i < 2 && lengths[i].first <= version; i++) + for (int i = 0; i < 3 && lengths[i].first <= version; i++) size = lengths[i].second; return size + 4; @@ -138,7 +141,7 @@ void Encoder::pad_data(BitArray& arr, unsigned bits_written) arr.v[i] = ((i - encoded_bytes) % 2 == 0) ? 0b11101100 : 0b00010001; } -unsigned char Encoder::get_version() const +char Encoder::get_version() const { if (version < 0) throw std::runtime_error("Determite version before getting it"); diff --git a/QRCodeLibrary/Encoder.hpp b/QRCodeLibrary/Encoder.hpp index 79f3926..cf359dd 100644 --- a/QRCodeLibrary/Encoder.hpp +++ b/QRCodeLibrary/Encoder.hpp @@ -15,10 +15,10 @@ public: BitArray& encode(); - static unsigned char determite_version(unsigned size, CorrectionLevel corr_lvl); + static char determite_version(unsigned size, CorrectionLevel corr_lvl); static unsigned calculate_encoded_input_size(unsigned input_size, QRCodeMethod method); - static unsigned calculate_metadata_size(QRCodeMethod method, unsigned char version); + static unsigned calculate_metadata_size(QRCodeMethod method, char version); static void write_metadata(unsigned input_size, unsigned input_bits_amount_size, QRCodeMethod method, BitArray& out); @@ -44,7 +44,7 @@ public: static void pad_data(BitArray& arr, unsigned bits_written); - unsigned char get_version() const; + char get_version() const; BitArray get_data() const; private: diff --git a/QRCodeLibrary/QRCode.cpp b/QRCodeLibrary/QRCode.cpp index a1de701..8289eff 100644 --- a/QRCodeLibrary/QRCode.cpp +++ b/QRCodeLibrary/QRCode.cpp @@ -4,16 +4,24 @@ #include "Encoder.hpp" #include "DataBlocks.hpp" -QRCode::QRCode(string& input_, CorrectionLevel corr_lvl_, QRCodeMethod method_, unsigned char version_) : - input{ input_ }, corr_lvl{ corr_lvl_ }, method{ method_ }, version{ version_ } +QRCode::QRCode(string& input_, CorrectionLevel corr_lvl_, QRCodeMethod method_, char version_, unsigned char mask_n) : + input{ input_ }, corr_lvl{ corr_lvl_ }, method{ method_ }, version{ version_ }, matrix(0) { if (method == QRCodeMethod::Dynamic) method = Method::determite_method(input); Encoder encoder(input, corr_lvl, method, version); - version = encoder.get_version(); const BitArray& encoded_data = encoder.encode(); + version = encoder.get_version(); DataBlocks data_blocks(encoded_data.v, corr_lvl, version); - vector& final_message = data_blocks.compose_joined_data_and_EC_blocks(); + const BitArray final_message(data_blocks.compose_joined_data_and_EC_blocks()); + + matrix.set_version(version); + + matrix.draw_patterns(); + + matrix.place_metadata(corr_lvl, mask_n); + + matrix.place_data(final_message, mask_n); } diff --git a/QRCodeLibrary/QRCode.hpp b/QRCodeLibrary/QRCode.hpp index 0cb9562..d46240c 100644 --- a/QRCodeLibrary/QRCode.hpp +++ b/QRCodeLibrary/QRCode.hpp @@ -2,19 +2,26 @@ #include -#include "method.hpp" +#include "Method.hpp" +#include "QRMatrix.hpp" using namespace std; class QRCode { public: - QRCode(string& input_, CorrectionLevel corr_lvl_ = CorrectionLevel::Q, QRCodeMethod method_ = QRCodeMethod::Dynamic, unsigned char version_ = 0); + QRCode(string& input_, CorrectionLevel corr_lvl_ = CorrectionLevel::M, QRCodeMethod method_ = QRCodeMethod::Dynamic, char version_ = -1, unsigned char mask_n = 0); + + string to_string() { return matrix.to_string(); }; + string to_ascii(char black = '#', char white = ' ', char empty = 'E') { return matrix.to_ascii(black, white, empty); } + vector> to_vector() const { return matrix.to_vector(); }; protected: string input; CorrectionLevel corr_lvl; QRCodeMethod method; - unsigned char version; + char version; + + QRMatrix matrix; }; diff --git a/QRCodeLibrary/QRCodeLibrary.vcxproj b/QRCodeLibrary/QRCodeLibrary.vcxproj index 5606540..595d4ac 100644 --- a/QRCodeLibrary/QRCodeLibrary.vcxproj +++ b/QRCodeLibrary/QRCodeLibrary.vcxproj @@ -158,6 +158,7 @@ + @@ -173,6 +174,7 @@ Create + diff --git a/QRCodeLibrary/QRCodeLibrary.vcxproj.filters b/QRCodeLibrary/QRCodeLibrary.vcxproj.filters index f8d1dab..c346194 100644 --- a/QRCodeLibrary/QRCodeLibrary.vcxproj.filters +++ b/QRCodeLibrary/QRCodeLibrary.vcxproj.filters @@ -42,6 +42,9 @@ Header Files + + Header Files + @@ -65,5 +68,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/QRCodeLibrary/QRMatrix.cpp b/QRCodeLibrary/QRMatrix.cpp new file mode 100644 index 0000000..2acd103 --- /dev/null +++ b/QRCodeLibrary/QRMatrix.cpp @@ -0,0 +1,179 @@ +#include "pch.h" + +#include "QRMatrix.hpp" +#include "Tables.hpp" + +void QRMatrix::draw_patterns() +{ + draw_finder_patterns(); + draw_alignment_patters(); + draw_timing_patterns(); + draw_dark_module(); +} + +void QRMatrix::draw_finder_patterns() +{ + draw_finder_square(0, 0); + draw_finder_square(c.size() - 7, 0); + draw_finder_square(0, c.size() - 7); + draw_finder_square_separators(); +} + +void QRMatrix::draw_finder_square(unsigned y, unsigned x) +{ + set(y, x, 0b1111111, 7); + set(y + 1, x, 0b1000001, 7); + + for (int i = 2; i < 5; i++) + set(y + i, x, 0b1011101, 7); + + set(y + 5, x, 0b1000001, 7); + set(y + 6, x, 0b1111111, 7); +} + +void QRMatrix::draw_finder_square_separators() +{ + set(7, 0, 0b0000000, 7); + set(7, c.size() - 7, 0b0000000, 7); + set(c.size() - 8, 0, 0b0000000, 7); + + for (unsigned i = 0; i < 8; i++) { + set(i, 7, 0); + set(i, c.size() - 8, 0); + set(c.size() - 8 + i, 7, 0); + } +} + +void QRMatrix::draw_alignment_patters() +{ + auto& coordinates = Tables::alignment_patterns_coordinates.at(version); + for (unsigned i = 0; i < coordinates.size(); i++) { + unsigned s = i, e = coordinates.size(); + if (coordinates[i] == 6) + s++, e--; + + for (unsigned j = s; j < e; j++) { + draw_alignment_square(coordinates[i], coordinates[j]); + if (i != j) + draw_alignment_square(coordinates[j], coordinates[i]); + } + } +} + +void QRMatrix::draw_alignment_square(unsigned y, unsigned x) +{ + set(y - 2, x - 2, 0b11111, 5); + set(y - 1, x - 2, 0b10001, 5); + set(y, x - 2, 0b10101, 5); + set(y + 1, x - 2, 0b10001, 5); + set(y + 2, x - 2, 0b11111, 5); +} + +void QRMatrix::draw_timing_patterns() +{ + for (unsigned i = 8; i < c.size() - 8; i += 2) { + set(6, i, 1); + set(6, i + 1, 0); + set(i, 6, 1); + set(i + 1, 6, 0); + } +} + +void QRMatrix::draw_dark_module() +{ + set(c.size() - 8, 8, 1); +} + +void QRMatrix::place_metadata(CorrectionLevel corr_lvl, unsigned char mask_n) +{ + if (version >= 6) { + const auto& v_codes = Tables::version_codes.at(version-6); + for (unsigned i = 0; i < 3; i++) + for (unsigned j = 0; j < 6; j++) { + set(c.size() - 11 + i, j, (v_codes.at(i) >> (6 - 1 - j)) & 1); + set(j, c.size() - 11 + i, (v_codes.at(i) >> (6 - 1 - j)) & 1); + } + } + + unsigned code = Tables::corr_lvl_and_mask_codes.at(corr_lvl)[mask_n]; + + unsigned y1 = 8, y2 = c.size() - 1, x1 = 0, x2 = 8; + for (unsigned i = 0; i < 15; i++) { + set(y1, x1, (code >> (15 - 1 - i)) & 1); + set(y2, x2, (code >> (15 - 1 - i)) & 1); + + if (x1 < 8) { + x1++; + if (x1 == 6) x1++; + } + else { + if (x1 == 8) y1--; + if (y1 == 6) y1--; + } + + if (y2 > c.size() - 8) y2--; + if (y2 == c.size() - 8) { y2 = 8; x2 = c.size() - 8; } + else if (y2 == 8) + x2++; + + } + + return; +} + +void QRMatrix::place_data(const BitArray& data, unsigned char mask_n) +{ + unsigned y = c.size() - 1; + unsigned x = y; + unsigned step = 0; + bool horiz_dir = true; + bool up = true; + + const auto& mask = Tables::mask_functions.at(mask_n); + + while (true) { + if (x == 6) + x--; + + if (get(y, x) == Trit::EMPTY) { + if (step < data.size) { + set(y, x, mask(y, x) ? data.get(step) : !data.get(step)); + step++; + } + else set(y, x, !mask(y, x)); + } + + if (horiz_dir) + x--; + else { + x++; + if (up) { + if (y == 0) { + x -= 2; + up = !up; + } + else + y--; + } + else { + if (y == c.size() - 1) { + if (x == 1) // т.к. сделали шаг "вправо" на строчке 130 + break; + + x -= 2; + up = !up; + } + else + y++; + } + } + + horiz_dir = !horiz_dir; + } + +} + +void QRMatrix::set_version(char version_) { + TritMatrix::resize(v_to_size(version_), v_to_size(version_)); + version = version_; +} diff --git a/QRCodeLibrary/QRMatrix.hpp b/QRCodeLibrary/QRMatrix.hpp new file mode 100644 index 0000000..6bdb4d6 --- /dev/null +++ b/QRCodeLibrary/QRMatrix.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include + +#include "Method.hpp" +#include "BitArray.hpp" +#include "TritMatrix.hpp" + +constexpr unsigned v_to_size(char version) { return version * 4 + 21; }; + +class QRMatrix : public TritMatrix +{ +public: + QRMatrix(char version_) : TritMatrix(v_to_size(version_)), version{ version_ } {}; + + void draw_patterns(); + + void draw_finder_patterns(); + void draw_finder_square(unsigned y, unsigned x); + void draw_finder_square_separators(); + + void draw_alignment_patters(); + void draw_alignment_square(unsigned y, unsigned x); + + void draw_timing_patterns(); + void draw_dark_module(); + + void place_metadata(CorrectionLevel corr_lvl, unsigned char mask_n); + + void place_data(const BitArray& data, unsigned char mask_n); + + void set_version(char version); + +private: + char version; +}; + diff --git a/QRCodeLibrary/Tables.hpp b/QRCodeLibrary/Tables.hpp index 12eee82..a768746 100644 --- a/QRCodeLibrary/Tables.hpp +++ b/QRCodeLibrary/Tables.hpp @@ -1,8 +1,9 @@ -#pragma once +#pragma once #include #include #include +#include #include "Method.hpp" @@ -15,39 +16,39 @@ namespace Tables { '+', '-', '.', '/', ':' }; - static const std::map> max_capability{ - { CorrectionLevel::L, {{152, 272, 440, 640, 864, 1088, 1248, 1552, 1856, 2192, 2592, 2960, 3424, 3688, 4184, 4712, 5176, 5768, 6360, 6888}} }, - { CorrectionLevel::M, {{128, 224, 352, 512, 688, 864, 992, 1232, 1456, 1728, 2032, 2320, 2672, 2920, 3320, 3624, 4056, 4504, 5016, 5352}} }, - { CorrectionLevel::Q, {{104, 176, 272, 384, 496, 608, 704, 880, 1056, 1232, 1440, 1648, 1952, 2088, 2360, 2600, 2936, 3176, 3560, 3880}} }, - { CorrectionLevel::H, {{72, 128, 208, 288, 368, 480, 528, 688, 800, 976, 1120, 1264, 1440, 1576, 1784, 2024, 2264, 2504, 2728, 3080}} } + static const std::map>max_capability{ + { CorrectionLevel::L, {{ 152, 272, 440, 640, 864, 1088, 1248, 1552, 1856, 2192, 2592, 2960, 3424, 3688, 4184, 4712, 5176, 5768, 6360, 6888, 7456, 8048, 8752, 9392, 10208, 10960, 11744, 12248, 13048, 13880, 14744, 15640, 16568, 17528, 18448, 19472, 20528, 21616, 22496, 23648 }} }, + { CorrectionLevel::M, {{ 128, 224, 352, 512, 688, 864, 992, 1232, 1456, 1728, 2032, 2320, 2672, 2920, 3320, 3624, 4056, 4504, 5016, 5352, 5712, 6256, 6880, 7312, 8000, 8496, 9024, 9544, 10136, 10984, 11640, 12328, 13048, 13800, 14496, 15312, 15936, 16816, 17728, 18672 }} }, + { CorrectionLevel::Q, {{ 104, 176, 272, 384, 496, 608, 704, 880, 1056, 1232, 1440, 1648, 1952, 2088, 2360, 2600, 2936, 3176, 3560, 3880, 4096, 4544, 4912, 5312, 5744, 6032, 6464, 6968, 7288, 7880, 8264, 8920, 9368, 9848, 10288, 10832, 11408, 12016, 12656, 13328 }} }, + { CorrectionLevel::H, {{ 72, 128, 208, 288, 368, 480, 528, 688, 800, 976, 1120, 1264, 1440, 1576, 1784, 2024, 2264, 2504, 2728, 3080, 3248, 3536, 3712, 4112, 4304, 4768, 5024, 5288, 5608, 5960, 6344, 6760, 7208, 7688, 7888, 8432, 8768, 9136, 9776, 10208 }} } }; - static const std::map mode_indicator{ + static const std::mapmode_indicator{ { QRCodeMethod::Numeric, 0b0001 }, { QRCodeMethod::Alphabetic, 0b0010 }, { QRCodeMethod::Byte, 0b0100 } }; - static const std::map, 3>> data_amount_lengths{ - { QRCodeMethod::Numeric, {{ {0, 10}, {8, 12}, {25, 14} }} }, - { QRCodeMethod::Alphabetic, {{ {0, 9}, {8, 11}, {25, 13} }} } , - { QRCodeMethod::Byte, {{ {0, 8}, {8, 16}, {25, 16} }} } + static const std::map, 3>>data_amount_lengths{ + { QRCodeMethod::Numeric, {{ {0, 10}, {9, 12}, {26, 14} }} }, + { QRCodeMethod::Alphabetic, {{ {0, 9}, {9, 11}, {26, 13} }} }, + { QRCodeMethod::Byte, {{ {0, 8}, {9, 16}, {26, 16} }} } }; - static const std::map> data_blocks_number{ - { CorrectionLevel::L, {{ 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8 }} }, - { CorrectionLevel::M, {{ 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16 }} }, - { CorrectionLevel::Q, {{ 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20 }} }, - { CorrectionLevel::H, {{ 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25 }} } + static const std::map>data_blocks_number{ + { CorrectionLevel::L, {{ 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 }} }, + { CorrectionLevel::M, {{ 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 }} }, + { CorrectionLevel::Q, {{ 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 }} }, + { CorrectionLevel::H, {{ 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 }} } }; - static const std::map> correction_bytes_num{ - { CorrectionLevel::L, {{ 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28 }} }, - { CorrectionLevel::M, {{ 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26 }} }, - { CorrectionLevel::Q, {{ 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30 }} }, - { CorrectionLevel::H, {{ 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28 }} } + static const std::map>correction_bytes_num{ + { CorrectionLevel::L, {{ 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 }} }, + { CorrectionLevel::M, {{ 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 }} }, + { CorrectionLevel::Q, {{ 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 }} }, + { CorrectionLevel::H, {{ 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 }} } }; - static const std::map> reed_solomon_generative_polynomial{ + static const std::map>reed_solomon_generative_polynomial{ { 7, {{ 87, 229, 146, 149, 238, 102, 21 }}}, { 10, {{ 251, 67, 46, 61, 118, 70, 64, 94, 32, 45 }}}, { 13, {{ 74, 152, 176, 100, 86, 100, 106, 104, 130, 218, 206, 140, 78 }}}, @@ -63,7 +64,7 @@ namespace Tables { { 30, {{ 41, 173, 145, 152, 216, 31, 179, 182, 50, 48, 110, 86, 239, 96, 222, 125, 42, 173, 226, 193, 224, 130, 156, 37, 251, 216, 238, 40, 192, 180 }}} }; - static const std::array galois_field{ + static const std::arraygalois_field{ 1, 2, 4, 8, 16, 32, 64, 128, 29, 58, 116, 232, 205, 135, 19, 38, 76, 152, 45, 90, 180, 117, 234, 201, 143, 3, 6, 12, 24, 48, 96, 192, 157, 39, 78, 156, 37, 74, 148, 53, 106, 212, 181, 119, 238, 193, 159, 35, @@ -82,7 +83,7 @@ namespace Tables { 44, 88, 176, 125, 250, 233, 207, 131, 27, 54, 108, 216, 173, 71, 142, 1, }; - static const std::array reverse_galois_field{ + static const std::arrayreverse_galois_field{ 0, 0, 1, 25, 2, 50, 26, 198, 3, 223, 51, 238, 27, 104, 199, 75, 4, 100, 224, 14, 52, 141, 239, 129, 28, 193, 105, 248, 200, 8, 76, 113, 5, 138, 101, 47, 225, 36, 15, 33, 53, 147, 142, 218, 240, 18, 130, 69, @@ -100,4 +101,79 @@ namespace Tables { 203, 89, 95, 176, 156, 169, 160, 81, 11, 245, 22, 235, 122, 117, 44, 215, 79, 174, 213, 233, 230, 231, 173, 232, 116, 214, 244, 234, 168, 80, 88, 175 }; + + static const array, 40>alignment_patterns_coordinates{ + vector(), {18,}, {22,}, {26,}, {30,}, + {34,}, {6, 22, 38}, {6, 24, 42}, {6, 26, 46}, {6, 28, 50}, + {6, 30, 54}, {6, 32, 58}, {6, 34, 62}, {6, 26, 46, 66}, {6, 26, 48, 70}, + {6, 26, 50, 74}, {6, 30, 54, 78}, {6, 30, 56, 82}, {6, 30, 58, 86}, {6, 34, 62, 90}, + {6, 28, 50, 72, 94}, {6, 26, 50, 74, 98}, {6, 30, 54, 78, 102}, {6, 28, 54, 80, 106}, + {6, 32, 58, 84, 110}, {6, 30, 58, 86, 114}, {6, 34, 62, 90, 118}, {6, 26, 50, 74, 98, 122}, + {6, 30, 54, 78, 102, 126}, {6, 26, 52, 78, 104, 130}, {6, 30, 56, 82, 108, 134}, {6, 34, 60, 86, 112, 138}, + {6, 30, 58, 86, 114, 142}, {6, 34, 62, 90, 118, 146}, {6, 30, 54, 78, 102, 126, 150}, {6, 24, 50, 76, 102, 128, 154}, + {6, 28, 54, 80, 106, 132, 158}, {6, 32, 58, 84, 110, 136, 162}, {6, 26, 54, 82, 110, 138, 166}, {6, 30, 58, 86, 114, 142, 170} + }; + + static const array, 34>version_codes{ { + { 0b000010, 0b011110, 0b100110 }, + { 0b010001, 0b011100, 0b111000 }, + { 0b110111, 0b011000, 0b000100 }, + { 0b101001, 0b111110, 0b000000 }, + { 0b001111, 0b111010, 0b111100 }, + { 0b001101, 0b100100, 0b011010 }, + { 0b101011, 0b100000, 0b100110 }, + { 0b110101, 0b000110, 0b100010 }, + { 0b010011, 0b000010, 0b011110 }, + { 0b011100, 0b010001, 0b011100 }, + { 0b111010, 0b010101, 0b100000 }, + { 0b100100, 0b110011, 0b100100 }, + { 0b000010, 0b110111, 0b011000 }, + { 0b000000, 0b101001, 0b111110 }, + { 0b100110, 0b101101, 0b000010 }, + { 0b111000, 0b001011, 0b000110 }, + { 0b011110, 0b001111, 0b111010 }, + { 0b001101, 0b001101, 0b100100 }, + { 0b101011, 0b001001, 0b011000 }, + { 0b110101, 0b101111, 0b011100 }, + { 0b010011, 0b101011, 0b100000 }, + { 0b010001, 0b110101, 0b000110 }, + { 0b110111, 0b110001, 0b111010 }, + { 0b101001, 0b010111, 0b111110 }, + { 0b001111, 0b010011, 0b000010 }, + { 0b101000, 0b011000, 0b101101 }, + { 0b001110, 0b011100, 0b010001 }, + { 0b010000, 0b111010, 0b010101 }, + { 0b110110, 0b111110, 0b101001 }, + { 0b110100, 0b100000, 0b001111 }, + { 0b010010, 0b100100, 0b110011 }, + { 0b001100, 0b000010, 0b110111 }, + { 0b101010, 0b000110, 0b001011 }, + { 0b111001, 0b000100, 0b010101 } + } }; + + static const map>corr_lvl_and_mask_codes{ + { CorrectionLevel::L, + { 0b111011111000100, 0b111001011110011, 0b111110110101010, 0b111100010011101, 0b110011000101111, 0b110001100011000, 0b110110001000001, 0b110100101110110 } + }, + { CorrectionLevel::M, + { 0b101010000010010, 0b101000100100101, 0b101111001111100, 0b101101101001011, 0b100010111111001, 0b100000011001110, 0b100111110010111, 0b100101010100000 } + }, + { CorrectionLevel::Q, + { 0b011010101011111, 0b011000001101000, 0b011111100110001, 0b011101000000110, 0b010010010110100, 0b010000110000011, 0b010111011011010, 0b010101111101101 } + }, + { CorrectionLevel::H, + { 0b001011010001001, 0b001001110111110, 0b001110011100111, 0b001100111010000, 0b000011101100010, 0b000001001010101, 0b000110100001100, 0b000100000111011 } + } + }; + + static const array, 8>mask_functions{ { + [](unsigned y, unsigned x) { return (x + y) % 2; }, + [](unsigned y, unsigned x) { return y % 2; }, + [](unsigned y, unsigned x) { return x % 3; }, + [](unsigned y, unsigned x) { return (x + y) % 3; }, + [](unsigned y, unsigned x) { return (x / 3 + y / 2) % 2; }, + [](unsigned y, unsigned x) { return (x * y) % 2 + (x * y) % 3; }, + [](unsigned y, unsigned x) { return ((x * y) % 2 + (x * y) % 3) % 2; }, + [](unsigned y, unsigned x) { return ((x * y) % 3 + (x + y) % 2) % 2; }, + } }; } diff --git a/QRCodeLibrary/TritMatrix.cpp b/QRCodeLibrary/TritMatrix.cpp index 84b231e..0141112 100644 --- a/QRCodeLibrary/TritMatrix.cpp +++ b/QRCodeLibrary/TritMatrix.cpp @@ -1,4 +1,4 @@ -#include "pch.h" +#include "pch.h" #include "TritMatrix.hpp" #include @@ -25,7 +25,7 @@ void TritMatrix::set(unsigned y, unsigned x, bool val) set(y, x, val ? Trit::T : Trit::F); } -string TritMatrix::to_ascii() const +string TritMatrix::to_ascii(char black, char white, char empty) const { string res; @@ -34,13 +34,13 @@ string TritMatrix::to_ascii() const switch (c[i][j]) { case Trit::T: - res.push_back('#'); + res.push_back(black); break; case Trit::F: - res.push_back(' '); + res.push_back(white); break; case Trit::EMPTY: - res.push_back('E'); + res.push_back(empty); break; } if (i != c.size() - 1) @@ -74,3 +74,10 @@ string TritMatrix::to_string() const return res; } + +void TritMatrix::resize(unsigned width, unsigned height) +{ + c.resize(height); + for (unsigned i = 0; i < height; i++) + c[i].resize(width, Trit::EMPTY); +} diff --git a/QRCodeLibrary/TritMatrix.hpp b/QRCodeLibrary/TritMatrix.hpp index 4dac810..26c9c07 100644 --- a/QRCodeLibrary/TritMatrix.hpp +++ b/QRCodeLibrary/TritMatrix.hpp @@ -15,6 +15,7 @@ class TritMatrix { public: TritMatrix(unsigned width, unsigned height) : c{ height, vector(width, Trit::EMPTY) } {}; + TritMatrix(unsigned size) : c{ size, vector(size, Trit::EMPTY) } {}; Trit get(unsigned y, unsigned x) const; @@ -22,10 +23,13 @@ public: void set(unsigned y, unsigned x, bool val); void set(unsigned y, unsigned x, int32_t val, unsigned char size); - string to_ascii() const; - string to_string() const; + void resize(unsigned width, unsigned height); -private: + string to_ascii(char black = '#', char white = ' ', char empty = 'E') const; + string to_string() const; + vector> to_vector() const { return c; }; + +protected: vector> c; }; diff --git a/tests/Encoder_test.cpp b/tests/Encoder_test.cpp index 24ab014..a366c5e 100644 --- a/tests/Encoder_test.cpp +++ b/tests/Encoder_test.cpp @@ -53,6 +53,9 @@ TEST(EncoderTests, CalculatesMetadataSize) { EXPECT_EQ(Encoder::calculate_metadata_size(QRCodeMethod::Numeric, 0), 14); EXPECT_EQ(Encoder::calculate_metadata_size(QRCodeMethod::Alphabetic, 5), 13); EXPECT_EQ(Encoder::calculate_metadata_size(QRCodeMethod::Numeric, 10), 16); + EXPECT_EQ(Encoder::calculate_metadata_size(QRCodeMethod::Byte, 9), 20); + EXPECT_EQ(Encoder::calculate_metadata_size(QRCodeMethod::Byte, 8), 12); + EXPECT_EQ(Encoder::calculate_metadata_size(QRCodeMethod::Alphabetic, 26), 17); } TEST(EncoderTests, DetermitesVersion) { diff --git a/tests/QRCode_test.cpp b/tests/QRCode_test.cpp index 839524c..e760afd 100644 --- a/tests/QRCode_test.cpp +++ b/tests/QRCode_test.cpp @@ -1,4 +1,4 @@ -#include "pch.h" +#include "pch.h" #define protected public #define private public @@ -14,6 +14,26 @@ TEST(QRCodeTests, ConstructsClass) { QRCode qr(std::string("TEST")); EXPECT_EQ(qr.input, "TEST"); - EXPECT_EQ(qr.corr_lvl, CorrectionLevel::Q); + EXPECT_EQ(qr.corr_lvl, CorrectionLevel::M); EXPECT_EQ(qr.method, QRCodeMethod::Alphabetic); -} \ No newline at end of file +} + +TEST(QRCodeTests, CreatesQRCode) { + //QRCode qr(std::string("Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversations?”\n" + // "So she was considering in her own mind(as well as she could, for the hot day made her feel very sleepyand stupid), whether the pleasure of making a daisy - chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.\n" + // "There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itself, “Oh dear! Oh dear! I shall be late!” (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural);" + // "but when the Rabbit actually took a watch out of its waistcoat - pocket, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat - pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit - hole under the hedge." + // "In another moment down went Alice after it, never once considering how in the world she was to get out again." + // "The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself before she found herself falling down a very deep well." + // "Either the well was very deep, or she fell very slowly, for she had plenty of time as she went down to look about her and to wonder what was going to happen next. First, she tried to look down and make out what she was coming to, but it was too dark to see anything; then she looked at the sides of the well, and noticed that they were filled with cupboards and book-shelves; here and there she saw maps and pictures hung upon pegs. She took down a jar from one of the shelves as she passed; it was labelled “ORANGE MARMALADE”, but to her great disappointment it was empty: she did not like to drop the jar for fear of killing somebody underneath, so managed to put it into one of the cupboards as she fell past it." + // "“Well!” thought Alice to herself, “after such a fall as this, I shall think nothing of tumbling down stairs! How brave they’ll all think me at home! Why, I wouldn’t say anything about it, even if I fell off the top of the house!” (Which was very likely true.)" + // "Down, down, down. Would the fall never come to an end? “I wonder how many miles I’ve fallen by this time?” she said aloud. “I must be getting somewhere near the centre of the earth. Let me see: that would be four thousand miles down, I think—” (for, you see, Alice had learnt several things of this sort in her lessons )" + // ), CorrectionLevel::L); + + //QRCode qr(std::string("ALICE WAS BEGINNING TO GET VERY TIRED OF SITTING BY HER SISTER ON THE BANK AND OF HAVING NOTHING TO DO: ONCE OR TWICE SHE HAD PEEPED INTO THE BOOK HER SISTER WAS READING BUT IT HAD NO PICTURES OR CONVERSATIONS IN IT AND WHAT IS THE USE OF A BOOK THOUGHT ALICE WITHOUT PICTURES OR CONVERSATIONS. SO SHE WAS CONSIDERING IN HER OWN MIND AS WELL AS SHE COULD FOR THE HOT DAY MADE HER FEEL VERY SLEEPY AND STUPID. WHETHER THE PLEASURE OF MAKING A DAISY-CHAIN WOULD BE WORTH THE TROUBLE OF GETTING UP AND PICKING THE DAISIES WHEN SUDDENLY A WHITE RABBIT WITH PINK EYES RAN CLOSE BY HER. THERE WAS NOTHING SO VERY REMARKABLE IN THAT NOR DID ALICE THINK IT SO VERY MUCH OUT OF THE WAY TO HEAR THE RABBIT SAY TO ITSELF OH DEAR. OH DEAR. I SHALL BE LATE. WHEN SHE THOUGHT IT OVER AFTERWARDS IT OCCURRED TO HER THAT SHE OUGHT TO HAVE WONDERED AT THIS BUT AT THE TIME IT ALL SEEMED QUITE NATURAL. BUT WHEN THE RABBIT ACTUALLY TOOK A WATCH OUT OF ITS WAISTCOAT-POCKET AND LOOKED AT IT AND THEN HURRIED ON ALICE STARTED TO HER FEET FOR IT FLASHED ACROSS HER MIND THAT SHE HAD NEVER BEFORE SEEN A RABBIT WITH EITHER A WAISTCOAT-POCKET OR A WATCH TO TAKE OUT OF IT AND BURNING WITH CURIOSITY SHE RAN ACROSS THE FIELD AFTER IT AND FORTUNATELY WAS JUST IN TIME TO SEE IT POP DOWN A LARGE RABBIT-HOLE UNDER THE HEDGE. IN ANOTHER MOMENT DOWN WENT ALICE AFTER IT NEVER ONCE CONSIDERING HOW IN THE WORLD SHE WAS TO GET OUT AGAIN. THE RABBIT-HOLE WENT STRAIGHT ON LIKE A TUNNEL FOR SOME WAY AND THEN DIPPED SUDDENLY DOWN SO SUDDENLY THAT ALICE HAD NOT A MOMENT TO THINK ABOUT STOPPING HERSELF BEFORE SHE FOUND HERSELF FALLING DOWN A VERY DEEP WELL. EITHER THE WELL WAS VERY DEEP OR SHE FELL VERY SLOWLY FOR SHE HAD PLENTY OF TIME AS SHE WENT DOWN TO LOOK ABOUT HER AND TO WONDER WHAT WAS GOING TO HAPPEN NEXT. FIRST SHE TRIED TO LOOK DOWN AND MAKE OUT WHAT SHE WAS COMING TO BUT IT WAS TOO DARK TO SEE ANYTHING THEN SHE LOOKED AT THE SIDES OF THE WELL AND NOTICED THAT THEY WERE FILLED WITH CUPBOARDS AND BOOK-SHELVES HERE AND THERE SHE SAW MAPS AND PICTURES HUNG UPON PEGS. SHE TOOK DOWN A JAR FROM ONE OF THE SHELVES AS SHE PASSED IT WAS LABELLED ORANGE MARMALADE BUT TO HER GREAT DISAPPOINTMENT IT WAS EMPTY: SHE DID NOT LIKE TO DROP THE JAR FOR FEAR OF KILLING SOMEBODY UNDERNEATH SO MANAGED TO PUT IT INTO ONE OF THE CUPBOARDS AS SHE FELL PAST IT. WELL. THOUGHT ALICE TO HERSELF AFTER SUCH A FALL AS THIS I SHALL THINK NOTHING OF TUMBLING DOWN STAIRS. HOW BRAVE THEYLL ALL THINK ME AT HOME. WHY I WOULDNT SAY ANYTHING ABOUT IT EVEN IF I FELL OFF THE TOP OF THE HOUSE. WHICH WAS VERY LIKELY TRUE.. DOWN DOWN DOWN. WOULD THE FALL NEVER COME TO AN END. I WONDER HOW MANY MILES IVE FALLEN BY THIS TIME. SHE SAID ALOUD. I MUST BE GETTING SOMEWHERE NEAR THE CENTRE OF THE EARTH. LET ME SEE: THAT WOULD BE FOUR THOUSAND MILES DOWN I THINK- FOR YOU SEE ALICE HAD LEARNT SEVERAL THINGS OF THIS SORT IN HER LESSONS IN THE SCHOOLROOM AND THOUGH THIS WAS NOT A VERY GOOD OPPORTUNITY FOR SHOWING OFF HER KNOWLEDGE AS THERE WAS NO ONE TO LISTEN TO HER STILL IT WAS GOOD PRACTICE TO SAY IT OVER. -YES THATS ABOUT THE RIGHT DISTANCE-BUT THEN I WONDER WHAT LATITUDE OR LONGITUDE IVE GOT TO. ALICE HAD NO IDEA WHAT LATITUDE WAS OR LONGITUDE EITHER BUT THOUGHT THEY WERE NICE GRAND WORDS TO SAY.. PRESENTLY SHE BEGAN AGAIN. I WONDER IF I SHALL FALL RIGHT THROUGH THE EARTH. HOW FUNNY ITLL SEEM TO COME OUT AMONG THE PEOPLE THAT WALK WITH THEIR HEADS DOWNWARD. THE ANTIPATHIES I THINK- SHE WAS RATHER GLAD THERE WAS NO ONE LISTENING THIS TIME AS IT DIDNT SOUND AT ALL THE RIGHT WORD. -BUT I SHALL HAVE TO ASK THEM WHAT THE NAME OF THE COUNTRY IS YOU KNOW. PLEASE MAAM IS THIS NEW ZEALAND OR AUSTRALIA. AND SHE TRIED TO CURTSEY AS SHE SPOKE-FANCY CURTSEYING AS YOURE FALLING THROUGH THE AIR. DO YOU THINK YOU COULD MANAGE IT.. AND WHAT AN IGNORANT LITTLE GIRL SHELL THINK ME FOR ASKING. NO ITLL NEVER DO TO ASK: PERHAPS I SHALL SEE IT WRITTEN UP SOMEWHERE. DOWN DOWN DOWN. THERE WAS NOTHING ELSE TO DO SO ALICE SOON BEGAN TALKING AGAIN. DINAHLL MISS ME VERY MUCH TO-NIGHT I SHOULD THINK. DINAH WAS THE CAT.. I HOPE THEYLL REMEMBER HER SAUCER OF MILK AT TEA-TIME. DINAH MY DEAR. I WISH YOU WERE DOWN HERE WITH ME. THERE ARE NO MICE IN THE AIR IM AFRAID BUT YOU MIGHT CATCH A BAT AND THATS VERY LIKE A MOUSE YOU KNOW. BUT DO CATS EAT BATS I WONDER. AND HERE ALICE BEGAN TO GET RATHER SLEEPY AND WENT ON SAYING TO HERSELF IN A DREAMY SORT OF WAY DO CATS EAT BATS. DO CATS EAT BATS. AND SOMETIMES DO BATS EAT CATS. FOR YOU SEE AS SHE COULDNT ANSWER EITHER QUESTION IT DIDNT MUCH MATTER WHICH WAY SHE PUT IT. SHE FELT THAT SHE WAS DOZING OFF AND HAD JUST BEGUN TO DREAM THAT SHE WAS WALKING HAND IN HAND WITH DINAH AND SAYING TO HER VERY EARNESTLY NOW DINAH TELL ME THE TRUTH: DID YOU EVER EAT A BAT. WHEN SUDDENLY THUMP. THUMP. DOWN SHE CAME UPON A HEAP OF STICKS AND DRY LEAVES AND THE FALL WAS OVER." + //).substr(0, 4296), CorrectionLevel::L); + + QRCode qr(std::string(u8"Здравствуйте, я - QR-код, сгенерированный при помощи библиотеки QRCodeLibrary!")); + + EXPECT_EQ(qr.to_string(), "1111111000000000111011111011101110111100101111111\n1000001010110100000100010001000100010111101000001\n1011101000110110000110011101101110110101101011101\n1011101000000001001000110010111011111001001011101\n1011101011001100001011111110110011001000001011101\n1000001000011100110000100011100110010010001000001\n1111111010101010101010101010101010101010101111111\n0000000001001100101101100010101011010001000000000\n1010101000011111111001111110000000100110000010010\n0100100111010011010101000101001100100010000001001\n0110011111011011101010101000111011101110001110100\n1111110111010011101111100011010101101011010010110\n1101101110011100001011010110001000100011001001101\n0000000010001100100010111010011001010011101101000\n0000001100000100000000001110110010001110111010000\n0001010111110010001010110101001100110101010100010\n0000101101000100100100100011000100010111011000101\n1000100110101101101111000111101110111001101000000\n0110101010010101110010001101001100110100111011011\n1000010011011011101101000010110011000101001100100\n1000011000000001011100011000001001110111010001101\n1000010110101001100111010011011101100111101000010\n0101111110101001111010111110110010100010111110100\n0010100010001100100101100010010001000101100011000\n0010101010011001000111101011001100110010101010001\n0001100011101010100011100011011101100111100011000\n0111111110001110000001111110110011011101111110110\n1100000011010010110111110100010001000100100101111\n0000011101001011101110001011011100110010110001010\n1101010111010010001100101101001000111110010001000\n1111101010111111000110000010111001101100011011010\n1101100100011110010101101110010001010101111000010\n0000011010101010111110101010111011101001101110001\n0100010011110100100011111100010001000000110011000\n1011111100001000011000111000111011101101000010111\n0010010011010010000110110010000001010101101101110\n0000011101011110010110011101011101000111001111001\n0111110110101010101010101110110000100010010010100\n0100011111100001111010011100000011101110000010110\n0111000000000001111100101000110101101011101101001\n1110001010101100010100111110000000100111111110010\n0000000011110111100111100011001000110011100011101\n1111111001001101101011101010111011101110101010100\n1000001001000100110101100011110011010011100011010\n1011101011010000110010111110101000100000111110001\n1011101001100101111011111101011101100011110000110\n1011101011110010011000101111010011001111101101000\n1000001000100101011011000101001100111110011100010\n1111111011110000101100010111000100001110010110011"); +} diff --git a/tests/QRMatrix_test.cpp b/tests/QRMatrix_test.cpp new file mode 100644 index 0000000..0d76554 --- /dev/null +++ b/tests/QRMatrix_test.cpp @@ -0,0 +1,46 @@ +#include "pch.h" + +#define protected public +#define private public + +#include "../QRCodeLibrary/QRMatrix.hpp" + +TEST(QRMatrixTests, CreatesMatrixOfAppropriateSize) { + QRMatrix m(4); + + EXPECT_EQ(m.c.size(), 37); +} + +TEST(QRMatrixTests, DrawsFinderSquare) { + QRMatrix m(0); + + m.draw_finder_square(0, 0); + EXPECT_EQ(m.to_string(), "1111111EEEEEEEEEEEEEE\n1000001EEEEEEEEEEEEEE\n1011101EEEEEEEEEEEEEE\n1011101EEEEEEEEEEEEEE\n1011101EEEEEEEEEEEEEE\n1000001EEEEEEEEEEEEEE\n1111111EEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE"); + + m.draw_finder_square(7, 7); + EXPECT_EQ(m.to_string(), "1111111EEEEEEEEEEEEEE\n1000001EEEEEEEEEEEEEE\n1011101EEEEEEEEEEEEEE\n1011101EEEEEEEEEEEEEE\n1011101EEEEEEEEEEEEEE\n1000001EEEEEEEEEEEEEE\n1111111EEEEEEEEEEEEEE\nEEEEEEE1111111EEEEEEE\nEEEEEEE1000001EEEEEEE\nEEEEEEE1011101EEEEEEE\nEEEEEEE1011101EEEEEEE\nEEEEEEE1011101EEEEEEE\nEEEEEEE1000001EEEEEEE\nEEEEEEE1111111EEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE"); + + m.draw_finder_square(1, 1); + EXPECT_EQ(m.to_string(), "1111111EEEEEEEEEEEEEE\n11111111EEEEEEEEEEEEE\n11000001EEEEEEEEEEEEE\n11011101EEEEEEEEEEEEE\n11011101EEEEEEEEEEEEE\n11011101EEEEEEEEEEEEE\n11000001EEEEEEEEEEEEE\nE1111111111111EEEEEEE\nEEEEEEE1000001EEEEEEE\nEEEEEEE1011101EEEEEEE\nEEEEEEE1011101EEEEEEE\nEEEEEEE1011101EEEEEEE\nEEEEEEE1000001EEEEEEE\nEEEEEEE1111111EEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE"); +} + +TEST(QRMatrixTests, DrawsFinderPatterns) { + QRMatrix m(0); + + m.draw_finder_patterns(); + + EXPECT_EQ(m.to_string(), "11111110EEEEE01111111\n10000010EEEEE01000001\n10111010EEEEE01011101\n10111010EEEEE01011101\n10111010EEEEE01011101\n10000010EEEEE01000001\n11111110EEEEE01111111\n00000000EEEEE00000000\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\nEEEEEEEEEEEEEEEEEEEEE\n00000000EEEEEEEEEEEEE\n11111110EEEEEEEEEEEEE\n10000010EEEEEEEEEEEEE\n10111010EEEEEEEEEEEEE\n10111010EEEEEEEEEEEEE\n10111010EEEEEEEEEEEEE\n10000010EEEEEEEEEEEEE\n11111110EEEEEEEEEEEEE"); +} + +TEST(QRMatrixTests, DrawsPatterns) { + BitArray tmp(441); + QRMatrix m(1); + + m.draw_patterns(); + + EXPECT_EQ(m.to_ascii(), "####### EEEEEEEEE #######\n# # EEEEEEEEE # #\n# ### # EEEEEEEEE # ### #\n# ### # EEEEEEEEE # ### #\n# ### # EEEEEEEEE # ### #\n# # EEEEEEEEE # #\n####### # # # # # #######\n EEEEEEEEE \nEEEEEE#EEEEEEEEEEEEEEEEEE\nEEEEEE EEEEEEEEEEEEEEEEEE\nEEEEEE#EEEEEEEEEEEEEEEEEE\nEEEEEE EEEEEEEEEEEEEEEEEE\nEEEEEE#EEEEEEEEEEEEEEEEEE\nEEEEEE EEEEEEEEEEEEEEEEEE\nEEEEEE#EEEEEEEEEEEEEEEEEE\nEEEEEE EEEEEEEEEEEEEEEEEE\nEEEEEE#EEEEEEEEE#####EEEE\n #EEEEEEE# #EEEE\n####### EEEEEEEE# # #EEEE\n# # EEEEEEEE# #EEEE\n# ### # EEEEEEEE#####EEEE\n# ### # EEEEEEEEEEEEEEEEE\n# ### # EEEEEEEEEEEEEEEEE\n# # EEEEEEEEEEEEEEEEE\n####### EEEEEEEEEEEEEEEEE"); + + m.place_data(tmp, 0); + + EXPECT_EQ(m.to_ascii(), "####### # # # # # #######\n# # # # # # # #\n# ### # # # # # # # ### #\n# ### # # # # # # ### #\n# ### # # # # # # # ### #\n# # # # # # # #\n####### # # # # # #######\n # # # # \n# # # # # # # # # # # # #\n # # # # # # # # # # # # \n# # # # # # # # # # # # #\n # # # # # # # # # # # # \n# # # # # # # # # # # # #\n # # # # # # # # # # # # \n# # # # # # # # # # # # #\n # # # # # # # # # # # # \n# # # # # # # # ##### # #\n ## # # ## ## # \n####### # # # # # # # # #\n# # # # # ## ## # \n# ### # # # # # ##### # #\n# ### # # # # # # # # # \n# ### # # # # # # # # # #\n# # # # # # # # # # \n####### # # # # # # # # #"); +} \ No newline at end of file diff --git a/tests/tests.vcxproj b/tests/tests.vcxproj index 5617c86..9203482 100644 --- a/tests/tests.vcxproj +++ b/tests/tests.vcxproj @@ -47,6 +47,7 @@ Create Create +