| 1 | //! Places data on a matrix |
| 2 | #![deny(unsafe_code)] |
| 3 | #![warn(missing_docs)] |
| 4 | |
| 5 | use crate::compact::CompactQR; |
| 6 | use crate::datamasking::Mask; |
| 7 | use crate::encode::Mode; |
| 8 | |
| 9 | use crate::module::ModuleType; |
| 10 | use crate::{datamasking, default, encode, polynomials, score, QRCode}; |
| 11 | use crate::{Version, ECL}; |
| 12 | use core::iter::Rev; |
| 13 | use core::ops::Range; |
| 14 | |
| 15 | pub enum BiRange { |
| 16 | Forward(Range<usize>), |
| 17 | Backwards(Rev<Range<usize>>), |
| 18 | } |
| 19 | |
| 20 | impl Iterator for BiRange { |
| 21 | type Item = usize; |
| 22 | fn next(&mut self) -> Option<usize> { |
| 23 | match self { |
| 24 | BiRange::Forward(range) => range.next(), |
| 25 | BiRange::Backwards(range) => range.next(), |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | #[cfg(test)] |
| 31 | pub fn test_place_on_matrix_data(qr: &mut QRCode, structure_as_binarystring: &CompactQR) { |
| 32 | place_on_matrix_data(qr, structure_as_binarystring); |
| 33 | } |
| 34 | |
| 35 | /// Places the data on the matrix |
| 36 | pub fn place_on_matrix_data(qr: &mut QRCode, structure_as_binarystring: &CompactQR) { |
| 37 | let structure_bytes_tmp = structure_as_binarystring.get_data(); |
| 38 | |
| 39 | let mut rev = true; |
| 40 | let mut idx = 0; |
| 41 | |
| 42 | // 0, 2, 4, 7, 9, .., N (skipping 6) |
| 43 | for x in (0..6).chain(7..qr.size).rev().step_by(2) { |
| 44 | let y_range = if rev { |
| 45 | BiRange::Backwards((0..qr.size).rev()) |
| 46 | } else { |
| 47 | BiRange::Forward(0..qr.size) |
| 48 | }; |
| 49 | |
| 50 | for y in y_range { |
| 51 | if qr[y][x].module_type() == ModuleType::Data { |
| 52 | let c = structure_bytes_tmp[idx / 8] & (1 << (7 - idx % 8)); |
| 53 | idx += 1; |
| 54 | qr[y][x].set(c != 0); |
| 55 | } |
| 56 | if qr[y][x - 1].module_type() == ModuleType::Data { |
| 57 | let c = structure_bytes_tmp[idx / 8] & (1 << (7 - idx % 8)); |
| 58 | idx += 1; |
| 59 | qr[y][x - 1].set(c != 0); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | rev = !rev; |
| 64 | } |
| 65 | |
| 66 | #[cfg(debug_assertions)] |
| 67 | { |
| 68 | let version = Version::from_n(qr.size); |
| 69 | assert_eq!(idx - version.missing_bits(), version.max_bytes() * 8); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const MASKS: [Mask; 8] = [ |
| 74 | Mask::Checkerboard, |
| 75 | Mask::HorizontalLines, |
| 76 | Mask::VerticalLines, |
| 77 | Mask::DiagonalLines, |
| 78 | Mask::LargeCheckerboard, |
| 79 | Mask::Fields, |
| 80 | Mask::Diamonds, |
| 81 | Mask::Meadow, |
| 82 | ]; |
| 83 | |
| 84 | /// Main function to place everything in the `QRCode`, returns a valid matrix |
| 85 | pub fn place_on_matrix( |
| 86 | structure_as_binarystring: &CompactQR, |
| 87 | quality: ECL, |
| 88 | version: Version, |
| 89 | mask: &mut Option<Mask>, |
| 90 | ) -> QRCode { |
| 91 | let mut best_score = u32::MAX; |
| 92 | let mut best_mask = MASKS[0]; |
| 93 | |
| 94 | let mut qr = default::create_matrix(version); |
| 95 | place_on_matrix_data(&mut qr, structure_as_binarystring); |
| 96 | |
| 97 | let transpose = default::transpose(&qr); |
| 98 | |
| 99 | for mask in MASKS { |
| 100 | let mut copy = qr.clone(); |
| 101 | let copy_transpose = transpose.clone(); |
| 102 | |
| 103 | datamasking::mask(&mut copy, mask); |
| 104 | let matrix_score = score::score(©, ©_transpose); |
| 105 | if matrix_score < best_score { |
| 106 | best_score = matrix_score; |
| 107 | best_mask = mask; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | best_mask = mask.unwrap_or(best_mask); |
| 112 | *mask = Some(best_mask); |
| 113 | |
| 114 | default::create_matrix_format_info(&mut qr, quality, best_mask); |
| 115 | datamasking::mask(&mut qr, best_mask); |
| 116 | |
| 117 | qr.mask = *mask; |
| 118 | qr |
| 119 | } |
| 120 | |
| 121 | /// Generate the whole matrix |
| 122 | pub fn create_matrix( |
| 123 | input: &[u8], |
| 124 | ecl: ECL, |
| 125 | mode: Mode, |
| 126 | version: Version, |
| 127 | mask: &mut Option<Mask>, |
| 128 | ) -> QRCode { |
| 129 | let data_codewords = encode::encode(input, ecl, mode, version); |
| 130 | let structure = polynomials::structure(data_codewords.get_data(), ecl, version); |
| 131 | |
| 132 | let max = version.max_bytes() * 8; |
| 133 | let structure_binstring = CompactQR::from_array(&structure, max + version.missing_bits()); |
| 134 | |
| 135 | QRCode { |
| 136 | mode: Some(mode), |
| 137 | ecl: Some(ecl), |
| 138 | version: Some(version), |
| 139 | ..place_on_matrix(&structure_binstring, ecl, version, mask) |
| 140 | } |
| 141 | } |