| 1 | //! `QRCode` need a way to define if they are readable, using a |
| 2 | //! scoring system. The lesser, the better. |
| 3 | |
| 4 | #![warn(missing_docs)] |
| 5 | |
| 6 | #[cfg(test)] |
| 7 | use crate::default::transpose; |
| 8 | use crate::module::{Module, ModuleType}; |
| 9 | use crate::QRCode; |
| 10 | |
| 11 | use super::hardcode; |
| 12 | |
| 13 | #[cfg(test)] |
| 14 | pub fn test_score_line(l: &[Module]) -> u32 { |
| 15 | line(l).1 |
| 16 | } |
| 17 | |
| 18 | #[cfg(test)] |
| 19 | pub fn test_score_pattern(l: &[Module]) -> u32 { |
| 20 | line(l).0 |
| 21 | } |
| 22 | |
| 23 | #[cfg(test)] |
| 24 | pub fn test_matrix_dark_modules(qr: &QRCode) -> u32 { |
| 25 | dark_module_score(qr) |
| 26 | } |
| 27 | |
| 28 | #[cfg(test)] |
| 29 | pub fn test_matrix_pattern_and_line(qr: &QRCode) -> (u32, u32, u32) { |
| 30 | let transpose = transpose(qr); |
| 31 | matrix_pattern_and_line(qr, &transpose) |
| 32 | } |
| 33 | |
| 34 | #[cfg(test)] |
| 35 | pub fn test_matrix_score_squares(qr: &QRCode) -> u32 { |
| 36 | matrix_score_squares(qr) |
| 37 | } |
| 38 | |
| 39 | /// Computes scores for squares, any 2x2 square (black or white) |
| 40 | /// add 3 to the score |
| 41 | /// |
| 42 | /// ### Opti: |
| 43 | /// We don't want to access the 4 squares each time, so we score the left most |
| 44 | /// ones and only fetch the next right ones |
| 45 | fn matrix_score_squares(qr: &QRCode) -> u32 { |
| 46 | let mut square_score = 0; |
| 47 | |
| 48 | for i in 0..qr.size - 1 { |
| 49 | let mut count_data = 2; |
| 50 | |
| 51 | let line1 = &qr[i]; |
| 52 | let line2 = &qr[i + 1]; |
| 53 | |
| 54 | let mut buffer = 0u8; |
| 55 | buffer |= u8::from(line1[0].value()) << 2; |
| 56 | buffer |= u8::from(line2[0].value()) << 3; |
| 57 | |
| 58 | for j in 0..qr.size - 1 { |
| 59 | buffer >>= 2; |
| 60 | buffer |= u8::from(line1[j + 1].value()) << 2; |
| 61 | buffer |= u8::from(line2[j + 1].value()) << 3; |
| 62 | |
| 63 | if line1[j + 1].module_type() != ModuleType::Data |
| 64 | || line2[j + 1].module_type() != ModuleType::Data |
| 65 | { |
| 66 | count_data = 0; |
| 67 | } |
| 68 | |
| 69 | if count_data >= 2 && (buffer == 0b1111 || buffer == 0b0000) { |
| 70 | square_score += 3; |
| 71 | } |
| 72 | |
| 73 | count_data += 1; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | square_score |
| 78 | } |
| 79 | |
| 80 | /// Computes scores for both patterns (`0b10111010000` or `0b00001011101`) |
| 81 | /// |
| 82 | /// ### Opti: |
| 83 | /// We convert the line to a u11 (supposedly) so comparing it to a pattern is |
| 84 | /// a simple comparison. |
| 85 | fn line(line: &[Module]) -> (u32, u32) { |
| 86 | const PATTERN_LEN: usize = 7; |
| 87 | |
| 88 | let mut line_score = 0; |
| 89 | let mut patt_score = 0; |
| 90 | |
| 91 | let mut count = 1; |
| 92 | let mut current = !line[0].value(); |
| 93 | |
| 94 | let mut buffer = 0; |
| 95 | let mut count_data = 0; |
| 96 | |
| 97 | for &item in line { |
| 98 | buffer = ((buffer << 1) | u16::from(item.value())) & 0b111_1111; |
| 99 | count_data += 1; |
| 100 | |
| 101 | if item.value() != current { |
| 102 | if count >= 5 { |
| 103 | line_score += count - 2; |
| 104 | } |
| 105 | count = 0; |
| 106 | current = item.value(); |
| 107 | } |
| 108 | |
| 109 | if item.module_type() != ModuleType::Data { |
| 110 | if count >= 5 { |
| 111 | line_score += count - 2; |
| 112 | } |
| 113 | |
| 114 | count_data = 0; |
| 115 | count = 0; |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | if count_data >= PATTERN_LEN && buffer == 0b101_1101 { |
| 120 | patt_score += 40; |
| 121 | } |
| 122 | |
| 123 | count += 1; |
| 124 | } |
| 125 | |
| 126 | if count >= 5 { |
| 127 | line_score += count - 2; |
| 128 | } |
| 129 | |
| 130 | (patt_score, line_score) |
| 131 | } |
| 132 | |
| 133 | /// Converts the matrix to lines & columns and feed it to `score_line` |
| 134 | fn matrix_pattern_and_line(qr: &QRCode, qr_transpose: &QRCode) -> (u32, u32, u32) { |
| 135 | let mut line_score = 0; |
| 136 | let mut col_score = 0; |
| 137 | let mut patt_score = 0; |
| 138 | |
| 139 | let n = qr.size; |
| 140 | |
| 141 | for i in 0..n { |
| 142 | let l = line(&qr[i]); |
| 143 | line_score += l.1; |
| 144 | |
| 145 | let c = line(&qr_transpose[i]); |
| 146 | col_score += c.1; |
| 147 | |
| 148 | patt_score += l.0 + c.0; |
| 149 | } |
| 150 | |
| 151 | (line_score, col_score, patt_score) |
| 152 | } |
| 153 | |
| 154 | /// Computes the number of `ModuleType::Dark` modules |
| 155 | fn dark_module_score(qr: &QRCode) -> u32 { |
| 156 | let n = qr.size; |
| 157 | let dark_modules = qr.data[..n * n] |
| 158 | .iter() |
| 159 | .filter(|m| m.value() == Module::DARK) |
| 160 | .count(); |
| 161 | |
| 162 | let percent = (dark_modules * 100) / (n * n); |
| 163 | u32::from(hardcode::PERCENT_SCORE[percent]) |
| 164 | } |
| 165 | |
| 166 | /// Computes the score for the matrix |
| 167 | /// - `matrix_pattern_and_line`: |
| 168 | /// - 40 points for each [TFTTTFT] pattern (T: true / F: false) |
| 169 | /// - N - 2 points for each line with N consecutive modules of the same color (N >= 5) |
| 170 | /// - `matrix_score_squares`: 3 points for each 2x2 square (black or white) |
| 171 | /// - `dark_module_score`: 10 points for each 5% of dark modules away from 50% |
| 172 | pub fn score(qr: &QRCode, qr_transpose: &QRCode) -> u32 { |
| 173 | let dark_score = dark_module_score(qr); |
| 174 | let square_score = matrix_score_squares(qr); |
| 175 | let (line_score, col_score, patt_score) = matrix_pattern_and_line(qr, qr_transpose); |
| 176 | |
| 177 | line_score + patt_score + col_score + dark_score + square_score |
| 178 | } |