From 67516a90849f8a98c57e8a3bf215f67dbb596e89 Mon Sep 17 00:00:00 2001 From: Oskari Alaranta Date: Mon, 20 Oct 2025 22:21:28 +0300 Subject: [PATCH] Cleanup format string placement code --- main.cpp | 59 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/main.cpp b/main.cpp index 92bf29c..58dc1df 100644 --- a/main.cpp +++ b/main.cpp @@ -773,37 +773,36 @@ QRCode generate_qr_code(std::string_view data, ErrorCorrection ec) const uint16_t format_data = (ec_to_val[static_cast(qr_info.error_correction)] << 13) | (pattern << 10); const uint16_t format_string = (format_data | mod2_remainder(format_data, 0b10100110111, 10)) ^ 0b101010000010010; - qr_code.set(0, 8, (format_string >> 14) & 1); - qr_code.set(1, 8, (format_string >> 13) & 1); - qr_code.set(2, 8, (format_string >> 12) & 1); - qr_code.set(3, 8, (format_string >> 11) & 1); - qr_code.set(4, 8, (format_string >> 10) & 1); - qr_code.set(5, 8, (format_string >> 9) & 1); - qr_code.set(7, 8, (format_string >> 8) & 1); - qr_code.set(8, 8, (format_string >> 7) & 1); - qr_code.set(8, 7, (format_string >> 6) & 1); - qr_code.set(8, 5, (format_string >> 5) & 1); - qr_code.set(8, 4, (format_string >> 4) & 1); - qr_code.set(8, 3, (format_string >> 3) & 1); - qr_code.set(8, 2, (format_string >> 2) & 1); - qr_code.set(8, 1, (format_string >> 1) & 1); - qr_code.set(8, 0, (format_string >> 0) & 1); + constexpr int8_t format_location[][2][2] { + { { 8, 0 }, { -1, 8 } }, + { { 8, 1 }, { -2, 8 } }, + { { 8, 2 }, { -3, 8 } }, + { { 8, 3 }, { -4, 8 } }, + { { 8, 4 }, { -5, 8 } }, + { { 8, 5 }, { -6, 8 } }, + { { 8, 7 }, { -7, 8 } }, + { { 8, 8 }, { -8, 8 } }, + { { 7, 8 }, { 8, -7 } }, + { { 5, 8 }, { 8, -6 } }, + { { 4, 8 }, { 8, -5 } }, + { { 3, 8 }, { 8, -4 } }, + { { 2, 8 }, { 8, -3 } }, + { { 1, 8 }, { 8, -2 } }, + { { 0, 8 }, { 8, -1 } }, + }; - qr_code.set(8, size - 1, (format_string >> 14) & 1); - qr_code.set(8, size - 2, (format_string >> 13) & 1); - qr_code.set(8, size - 3, (format_string >> 12) & 1); - qr_code.set(8, size - 4, (format_string >> 11) & 1); - qr_code.set(8, size - 5, (format_string >> 10) & 1); - qr_code.set(8, size - 6, (format_string >> 9) & 1); - qr_code.set(8, size - 7, (format_string >> 8) & 1); - qr_code.set(size - 8, 8, (format_string >> 7) & 1); - qr_code.set(size - 7, 8, (format_string >> 6) & 1); - qr_code.set(size - 6, 8, (format_string >> 5) & 1); - qr_code.set(size - 5, 8, (format_string >> 4) & 1); - qr_code.set(size - 4, 8, (format_string >> 3) & 1); - qr_code.set(size - 3, 8, (format_string >> 2) & 1); - qr_code.set(size - 2, 8, (format_string >> 1) & 1); - qr_code.set(size - 1, 8, (format_string >> 0) & 1); + for (size_t bit = 0; bit < 15; bit++) + { + for (size_t i = 0; i < 2; i++) + { + const auto [x, y] = format_location[bit][i]; + qr_code.set( + x < 0 ? size + x : x, + y < 0 ? size + y : y, + (format_string >> bit) & 1 + ); + } + } } // version string