| 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 craterust-analyzer cargo fast_qr 0.13.1 crate/::defaultrust-analyzer cargo fast_qr 0.13.1 default/::transposerust-analyzer cargo fast_qr 0.13.1 default/transpose().; |
| 8 | use craterust-analyzer cargo fast_qr 0.13.1 crate/::modulerust-analyzer cargo fast_qr 0.13.1 module/::{Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#, ModuleTyperust-analyzer cargo fast_qr 0.13.1 module/ModuleType#}; |
| 9 | use craterust-analyzer cargo fast_qr 0.13.1 crate/::QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#; |
| 10 | |
| 11 | use superrust-analyzer cargo fast_qr 0.13.1 crate/::hardcoderust-analyzer cargo fast_qr 0.13.1 hardcode/; |
| 12 | |
| 13 | #[cfg(test)] |
| 14 | pub fn test_score_linerust-analyzer cargo fast_qr 0.13.1 score/test_score_line().(l: &[Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#]) -> u32 { |
| 15 | linerust-analyzer cargo fast_qr 0.13.1 score/line().(l).1 |
| 16 | } |
| 17 | |
| 18 | #[cfg(test)] |
| 19 | pub fn test_score_patternrust-analyzer cargo fast_qr 0.13.1 score/test_score_pattern().(l: &[Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#]) -> u32 { |
| 20 | linerust-analyzer cargo fast_qr 0.13.1 score/line().(l).0 |
| 21 | } |
| 22 | |
| 23 | #[cfg(test)] |
| 24 | pub fn test_matrix_dark_modulesrust-analyzer cargo fast_qr 0.13.1 score/test_matrix_dark_modules().(qr: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#) -> u32 { |
| 25 | dark_module_scorerust-analyzer cargo fast_qr 0.13.1 score/dark_module_score().(qr) |
| 26 | } |
| 27 | |
| 28 | #[cfg(test)] |
| 29 | pub fn test_matrix_pattern_and_linerust-analyzer cargo fast_qr 0.13.1 score/test_matrix_pattern_and_line().(qr: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#) -> (u32, u32, u32) { |
| 30 | let transpose = transposerust-analyzer cargo fast_qr 0.13.1 default/transpose().(qr); |
| 31 | matrix_pattern_and_linerust-analyzer cargo fast_qr 0.13.1 score/matrix_pattern_and_line().(qr, &transpose) |
| 32 | } |
| 33 | |
| 34 | #[cfg(test)] |
| 35 | pub fn test_matrix_score_squaresrust-analyzer cargo fast_qr 0.13.1 score/test_matrix_score_squares().(qr: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#) -> u32 { |
| 36 | matrix_score_squaresrust-analyzer cargo fast_qr 0.13.1 score/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_squaresrust-analyzer cargo fast_qr 0.13.1 score/matrix_score_squares().(qr: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#) -> u32 { |
| 46 | let mut square_score = 0; |
| 47 | |
| 48 | for i in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#qr.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1 { |
| 49 | let mut count_data = 2; |
| 50 | |
| 51 | let line1 = &qr[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 52 | let line2 = &qr[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 53 | |
| 54 | let mut buffer = 0u8; |
| 55 | buffer rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign().u8::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u8][`From<bool>`]from().(line1[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..valuerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]value().()) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shl<i32>`]shl().2; |
| 56 | buffer rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign().u8::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u8][`From<bool>`]from().(line2[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..valuerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]value().()) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shl<i32>`]shl().3; |
| 57 | |
| 58 | for j in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#qr.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1 { |
| 59 | buffer rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`ShrAssign<i32>`]shr_assign().>>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`ShrAssign<i32>`]shr_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`ShrAssign<i32>`]shr_assign().2; |
| 60 | buffer rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign().u8::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u8][`From<bool>`]from().(line1[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().j rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..valuerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]value().()) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shl<i32>`]shl().2; |
| 61 | buffer rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign().u8::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u8][`From<bool>`]from().(line2[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().j rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..valuerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]value().()) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shl<i32>`]shl().3; |
| 62 | |
| 63 | if line1[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().j rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..module_typerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]module_type().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().ModuleTyperust-analyzer cargo fast_qr 0.13.1 module/ModuleType#::Datarust-analyzer cargo fast_qr 0.13.1 module/ModuleType#Data# |
| 64 | || line2[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().j rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..module_typerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]module_type().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().ModuleTyperust-analyzer cargo fast_qr 0.13.1 module/ModuleType#::Datarust-analyzer cargo fast_qr 0.13.1 module/ModuleType#Data# |
| 65 | { |
| 66 | count_data = 0; |
| 67 | } |
| 68 | |
| 69 | if count_data rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]ge().2 && (buffer rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0b1111 || buffer rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0b0000) { |
| 70 | square_score rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().3; |
| 71 | } |
| 72 | |
| 73 | count_data rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().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 linerust-analyzer cargo fast_qr 0.13.1 score/line().(line: &[Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#]) -> (u32, u32) { |
| 86 | const PATTERN_LENrust-analyzer cargo fast_qr 0.13.1 score/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 = !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().line[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..valuerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]value().(); |
| 93 | |
| 94 | let mut buffer = 0; |
| 95 | let mut count_data = 0; |
| 96 | |
| 97 | for &item in line { |
| 98 | buffer = ((buffer rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`Shl<i32>`]shl().1) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitOr<Self>`]bitor().u16::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u16][`From<bool>`]from().(item.valuerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]value().())) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u16][`BitAnd<Self>`]bitand().0b111_1111; |
| 99 | count_data rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 100 | |
| 101 | if item.valuerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]value().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[bool][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[bool][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[bool][`PartialEq<Self>`]ne().current { |
| 102 | if count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge().5 { |
| 103 | line_score rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().2; |
| 104 | } |
| 105 | count = 0; |
| 106 | current = item.valuerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]value().(); |
| 107 | } |
| 108 | |
| 109 | if item.module_typerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]module_type().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().ModuleTyperust-analyzer cargo fast_qr 0.13.1 module/ModuleType#::Datarust-analyzer cargo fast_qr 0.13.1 module/ModuleType#Data# { |
| 110 | if count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge().5 { |
| 111 | line_score rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().2; |
| 112 | } |
| 113 | |
| 114 | count_data = 0; |
| 115 | count = 0; |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | if count_data rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().PATTERN_LENrust-analyzer cargo fast_qr 0.13.1 score/PATTERN_LEN. && buffer rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]eq().0b101_1101 { |
| 120 | patt_score rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().40; |
| 121 | } |
| 122 | |
| 123 | count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().1; |
| 124 | } |
| 125 | |
| 126 | if count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge().5 { |
| 127 | line_score rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().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_linerust-analyzer cargo fast_qr 0.13.1 score/matrix_pattern_and_line().(qr: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#, qr_transpose: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/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.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size.; |
| 140 | |
| 141 | for i in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#n { |
| 142 | let l = linerust-analyzer cargo fast_qr 0.13.1 score/line().(&qr[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 143 | line_score rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().l.1; |
| 144 | |
| 145 | let c = linerust-analyzer cargo fast_qr 0.13.1 score/line().(&qr_transpose[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 146 | col_score rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().c.1; |
| 147 | |
| 148 | patt_score rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().l.0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().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_scorerust-analyzer cargo fast_qr 0.13.1 score/dark_module_score().(qr: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#) -> u32 { |
| 156 | let n = qr.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size.; |
| 157 | let dark_modules = qr.datarust-analyzer cargo fast_qr 0.13.1 qr/QRCode#data.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().n]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). |
| 158 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 159 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|m| m.valuerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]value().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[bool][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[bool][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[bool][`PartialEq<Self>`]eq().Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#::DARKrust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]DARK.) |
| 160 | .countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter/impl#[`Filter<I, P>`][Iterator]count().(); |
| 161 | |
| 162 | let percent = (dark_modules rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().100) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().(n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().n); |
| 163 | u32::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u32][`From<u8>`]from().(hardcoderust-analyzer cargo fast_qr 0.13.1 hardcode/::PERCENT_SCORErust-analyzer cargo fast_qr 0.13.1 hardcode/PERCENT_SCORE.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().percent]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().) |
| 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 scorerust-analyzer cargo fast_qr 0.13.1 score/score().(qr: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#, qr_transpose: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#) -> u32 { |
| 173 | let dark_score = dark_module_scorerust-analyzer cargo fast_qr 0.13.1 score/dark_module_score().(qr); |
| 174 | let square_score = matrix_score_squaresrust-analyzer cargo fast_qr 0.13.1 score/matrix_score_squares().(qr); |
| 175 | let (line_score, col_score, patt_score) = matrix_pattern_and_linerust-analyzer cargo fast_qr 0.13.1 score/matrix_pattern_and_line().(qr, qr_transpose); |
| 176 | |
| 177 | line_score rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().patt_score rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().col_score rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().dark_score rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().square_score |
| 178 | } |