]> Skullheadx's Git Forge - qrcodescanner.git/commitdiff
Decode the format information adjacent to the upper left finder pattern
authorSkullheadx <admonty1@gmail.com>
Wed, 20 Nov 2024 01:51:03 +0000 (20:51 -0500)
committerSkullheadx <admonty1@gmail.com>
Wed, 20 Nov 2024 01:51:03 +0000 (20:51 -0500)
camera.cpp
tablec1.txt [new file with mode: 0644]

index 0d20f12035e21534adf9e098b7ec1e10fa503a83..7ce54fd0aebe040971772ac20de5043077b8ca84 100644 (file)
@@ -3,15 +3,23 @@
 #include <cassert>
 #include <vector>
 
-void read_words_from_file(char const *filename, std::vector<std::vector<bool>> &symbol);
+void print(std::vector<bool> &vec);
 void print(std::vector<std::vector<bool>> &symbol);
+void vec_xor(std::vector<bool> &v1, const std::vector<bool> v2);
+unsigned int distance(std::vector<bool> &v1, const std::vector<bool> &v2);
+void read_words_from_file(char const *filename, std::vector<std::vector<bool>> &symbol);
+
+std::vector<bool> get_format_info(std::vector<std::vector<bool>> &symbol);
 
 int main(){
        std::vector<std::vector<bool>> symbol(21, std::vector<bool>(21,0));
 
        read_words_from_file("output.pbm", symbol);
        print(symbol);
+       
+       std::vector<bool> format_info = get_format_info(symbol);
 
+       print(format_info);
        
        return 0;
 }
@@ -24,6 +32,106 @@ void print(std::vector<std::vector<bool>> &symbol){
              }
                std::cout << std::endl;
        }
+       std::cout << std::endl;
+}
+
+void print(std::vector<bool> &vec){
+       for (auto i : vec){
+               std::cout << i;
+       }
+       std::cout << std::endl;
+}
+
+void vec_xor(std::vector<bool> &v1, const std::vector<bool> v2){
+       for (std::size_t i{0}; i < v1.size(); ++i){
+               v1[i] = v1[i] ^ v2[i];
+       }
+}
+
+unsigned int distance(std::vector<bool> &v1, const std::vector<bool> &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<bool> get_format_info(std::vector<std::vector<bool>> &symbol){
+       std::vector<bool> 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<std::vector<bool>> 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<bool> 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<std::vector<bool>> &symbol) {
diff --git a/tablec1.txt b/tablec1.txt
new file mode 100644 (file)
index 0000000..0c33e37
--- /dev/null
@@ -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