From: Skullheadx Date: Wed, 20 Nov 2024 01:51:03 +0000 (-0500) Subject: Decode the format information adjacent to the upper left finder pattern X-Git-Url: http://git.skullheadx.com/nixos/static/gitweb.css?a=commitdiff_plain;h=22beb4bb3ee0aa43fa94042f15cf96a96e5c91f3;p=qrcodescanner.git Decode the format information adjacent to the upper left finder pattern --- diff --git a/camera.cpp b/camera.cpp index 0d20f12..7ce54fd 100644 --- a/camera.cpp +++ b/camera.cpp @@ -3,15 +3,23 @@ #include #include -void read_words_from_file(char const *filename, std::vector> &symbol); +void print(std::vector &vec); void print(std::vector> &symbol); +void vec_xor(std::vector &v1, const std::vector v2); +unsigned int distance(std::vector &v1, const std::vector &v2); +void read_words_from_file(char const *filename, std::vector> &symbol); + +std::vector get_format_info(std::vector> &symbol); int main(){ std::vector> symbol(21, std::vector(21,0)); read_words_from_file("output.pbm", symbol); print(symbol); + + std::vector format_info = get_format_info(symbol); + print(format_info); return 0; } @@ -24,6 +32,106 @@ void print(std::vector> &symbol){ } std::cout << std::endl; } + std::cout << std::endl; +} + +void print(std::vector &vec){ + for (auto i : vec){ + std::cout << i; + } + std::cout << std::endl; +} + +void vec_xor(std::vector &v1, const std::vector v2){ + for (std::size_t i{0}; i < v1.size(); ++i){ + v1[i] = v1[i] ^ v2[i]; + } +} + +unsigned int distance(std::vector &v1, const std::vector &v2){ + unsigned int dist{0}; + for (std::size_t i{0}; i < v1.size(); ++i){ + if (v1[i] != v2[i]){ + ++dist; + } + + if (dist > 3){ + return dist; + } + } + return dist; +} + + +std::vector get_format_info(std::vector> &symbol){ + std::vector format_info(15,0); + for (std::size_t row{0}, counter{14}; row < 21; ++row){ + if (row == 6 || (row >= 9 && row <= 13)){ + continue; + } + format_info[counter] = (symbol[row][8]); + --counter; + } + + std::vector> valid_bit_format_info = { + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,1,0,1,0,0,1,1,0,1,1,1}, + {0,0,0,1,0,1,0,0,1,1,0,1,1,1,0}, + {0,0,0,1,1,1,1,0,1,0,1,1,0,0,1}, + {0,0,1,0,0,0,1,1,1,1,0,1,0,1,1}, + {0,0,1,0,1,0,0,1,1,0,1,1,1,0,0}, + {0,0,1,1,0,1,1,1,0,0,0,0,1,0,1}, + {0,0,1,1,1,1,0,1,0,1,1,0,0,1,0}, + {0,1,0,0,0,1,1,1,1,0,1,0,1,1,0}, + {0,1,0,0,1,1,0,1,1,1,0,0,0,0,1}, + {0,1,0,1,0,0,1,1,0,1,1,1,0,0,0}, + {0,1,0,1,1,0,0,1,0,0,0,1,1,1,1}, + {0,1,1,0,0,1,0,0,0,1,1,1,1,0,1}, + {0,1,1,0,1,1,1,0,0,0,0,1,0,1,0}, + {0,1,1,1,0,0,0,0,1,0,1,0,0,1,1}, + {0,1,1,1,1,0,1,0,1,1,0,0,1,0,0}, + {1,0,0,0,0,1,0,1,0,0,1,1,0,1,1}, + {1,0,0,0,1,1,1,1,0,1,0,1,1,0,0}, + {1,0,0,1,0,0,0,1,1,1,1,0,1,0,1}, + {1,0,0,1,1,0,1,1,1,0,0,0,0,1,0}, + {1,0,1,0,0,1,1,0,1,1,1,0,0,0,0}, + {1,0,1,0,1,1,0,0,1,0,0,0,1,1,1}, + {1,0,1,1,0,0,1,0,0,0,1,1,1,1,0}, + {1,0,1,1,1,0,0,0,0,1,0,1,0,0,1}, + {1,1,0,0,0,0,1,0,1,0,0,1,1,0,1}, + {1,1,0,0,1,0,0,0,1,1,1,1,0,1,0}, + {1,1,0,1,0,1,1,0,0,1,0,0,0,1,1}, + {1,1,0,1,1,1,0,0,0,0,1,0,1,0,0}, + {1,1,1,0,0,0,0,1,0,1,0,0,1,1,0}, + {1,1,1,0,1,0,1,1,0,0,1,0,0,0,1}, + {1,1,1,1,0,1,0,1,1,0,0,1,0,0,0}, + {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, + }; + + std::vector mask = {1,0,1,0,1,0,0,0,0,0,1,0,0,1,0}; + vec_xor(format_info, mask); + + unsigned int min_dist = distance(format_info, valid_bit_format_info[0]); + std::size_t min_dist_index{0}; + for (std::size_t i{0}; i < valid_bit_format_info.size(); ++i){ + unsigned int curr_dist = distance(format_info, valid_bit_format_info[i]); + if (curr_dist <= min_dist){ + min_dist = curr_dist; + min_dist_index = i; + if (curr_dist == 0){ + break; + } + } + } + //std::cout << min_dist << " " << std::endl; + if (min_dist <= 3){ + format_info = valid_bit_format_info[min_dist_index]; + } + else{ + throw std::runtime_error("format information incorrectly scanned!"); + } + + return format_info; } void read_words_from_file(char const *filename, std::vector> &symbol) { diff --git a/tablec1.txt b/tablec1.txt new file mode 100644 index 0000000..0c33e37 --- /dev/null +++ b/tablec1.txt @@ -0,0 +1,32 @@ +000000000000000 +000010100110111 +000101001101110 +000111101011001 +001000111101011 +001010011011100 +001101110000101 +001111010110010 +010001111010110 +010011011100001 +010100110111000 +010110010001111 +011001000111101 +011011100001010 +011100001010011 +011110101100100 +100001010011011 +100011110101100 +100100011110101 +100110111000010 +101001101110000 +101011001000111 +101100100011110 +101110000101001 +110000101001101 +110010001111010 +110101100100011 +110111000010100 +111000010100110 +111010110010001 +111101011001000 +111111111111111