From 508f5f14c540a334dd45fd5ae388b8f2c6b1c775 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sat, 29 Apr 2023 09:58:04 -0500 Subject: [PATCH] code cleanup --- src/client/result/EmailAddressResultParser.rs | 20 ++------ src/common/bit_array.rs | 1 - .../base_extentions/qr_ec_level.rs | 1 - .../base_extentions/qr_formatinformation.rs | 2 - .../cpp_essentials/concentric_finder.rs | 33 ------------ src/common/cpp_essentials/decoder_result.rs | 2 +- .../fast_edge_to_edge_counter.rs | 2 +- src/common/cpp_essentials/pattern.rs | 51 +++++-------------- src/common/eci_string_builder.rs | 2 - src/common/hybrid_binarizer.rs | 2 +- src/common/quad.rs | 2 - .../detector/zxing_cpp_detector/mod.rs | 1 - src/qrcode/cpp_port/decoder.rs | 3 +- src/qrcode/cpp_port/detector.rs | 22 +++++--- src/qrcode/cpp_port/qr_cpp_reader.rs | 6 +-- src/qrcode/cpp_port/test/QRDataMaskTest.rs | 48 +++++------------ .../test/QRDecodedBitStreamParserTest.rs | 4 +- src/qrcode/cpp_port/test/QRVersionTest.rs | 4 +- src/qrcode/decoder/mode.rs | 4 +- 19 files changed, 56 insertions(+), 154 deletions(-) diff --git a/src/client/result/EmailAddressResultParser.rs b/src/client/result/EmailAddressResultParser.rs index c255d60..30da964 100644 --- a/src/client/result/EmailAddressResultParser.rs +++ b/src/client/result/EmailAddressResultParser.rs @@ -61,10 +61,7 @@ pub fn parse(result: &RXingResult) -> Option { let mut tos = if hostEmail.is_empty() { Vec::new() } else { - COMMA - .split(hostEmail) - .map(|s| s.to_owned()) - .collect() + COMMA.split(hostEmail).map(|s| s.to_owned()).collect() }; // if (!hostEmail.isEmpty()) { // tos = COMMA.split(hostEmail); @@ -78,29 +75,20 @@ pub fn parse(result: &RXingResult) -> Option { // if (nameValues != null) { if tos.is_empty() { if let Some(tosString) = nv.get("to") { - tos = COMMA - .split(tosString) - .map(|s| s.to_owned()) - .collect(); + tos = COMMA.split(tosString).map(|s| s.to_owned()).collect(); } // if tosString != null { // tos = COMMA.split(tosString); // } } if let Some(ccString) = nv.get("cc") { - ccs = COMMA - .split(ccString) - .map(|s| s.to_owned()) - .collect(); + ccs = COMMA.split(ccString).map(|s| s.to_owned()).collect(); } // if ccString != null { // ccs = COMMA.split(ccString); // } if let Some(bccString) = nv.get("bcc") { - bccs = COMMA - .split(bccString) - .map(|s| s.to_owned()) - .collect(); + bccs = COMMA.split(bccString).map(|s| s.to_owned()).collect(); } // if bccString != null { // bccs = COMMA.split(bccString); diff --git a/src/common/bit_array.rs b/src/common/bit_array.rs index e353974..402c012 100644 --- a/src/common/bit_array.rs +++ b/src/common/bit_array.rs @@ -18,7 +18,6 @@ // import java.util.Arrays; - use std::{cmp, fmt}; use crate::common::Result; diff --git a/src/common/cpp_essentials/base_extentions/qr_ec_level.rs b/src/common/cpp_essentials/base_extentions/qr_ec_level.rs index 00a5cf0..eeda7ec 100644 --- a/src/common/cpp_essentials/base_extentions/qr_ec_level.rs +++ b/src/common/cpp_essentials/base_extentions/qr_ec_level.rs @@ -1,4 +1,3 @@ - use crate::qrcode::decoder::ErrorCorrectionLevel; impl ErrorCorrectionLevel { diff --git a/src/common/cpp_essentials/base_extentions/qr_formatinformation.rs b/src/common/cpp_essentials/base_extentions/qr_formatinformation.rs index 4a865a4..5e785b3 100644 --- a/src/common/cpp_essentials/base_extentions/qr_formatinformation.rs +++ b/src/common/cpp_essentials/base_extentions/qr_formatinformation.rs @@ -1,5 +1,3 @@ - - use crate::qrcode::decoder::{ ErrorCorrectionLevel, FormatInformation, FORMAT_INFO_DECODE_LOOKUP, FORMAT_INFO_MASK_QR, }; diff --git a/src/common/cpp_essentials/concentric_finder.rs b/src/common/cpp_essentials/concentric_finder.rs index a70d3d6..38c987b 100644 --- a/src/common/cpp_essentials/concentric_finder.rs +++ b/src/common/cpp_essentials/concentric_finder.rs @@ -8,8 +8,6 @@ use crate::{ point, Point, }; - - use super::{ BitMatrixCursorTrait, EdgeTracer, FastEdgeToEdgeCounter, Pattern, RegressionLine, RegressionLineTrait, UpdateMinMax, UpdateMinMaxFloat, @@ -342,18 +340,11 @@ pub fn CollectRingPoints( } pub fn FitQadrilateralToPoints(center: Point, points: &mut [Point]) -> Option { - let _dist2Center = |a, b| Point::distance(a, center) < Point::distance(b, center); // rotate points such that the first one is the furthest away from the center (hence, a corner) - let max_by_pred = |a: &&Point, b: &&Point| { let da = Point::distance(**a, center); let db = Point::distance(**b, center); da.partial_cmp(&db).unwrap() - // if dist2Center(**a, **b) { - // std::cmp::Ordering::Greater - // } else { - // std::cmp::Ordering::Less - // } }; let max = points.iter().max_by(max_by_pred)?; @@ -361,7 +352,6 @@ pub fn FitQadrilateralToPoints(center: Point, points: &mut [Point]) -> Option Option Option FastEdgeToEdgeCounter<'a> { stride, stepsToBorder, _arr: cur.p().y as isize * stride, //cur.img().getRow(cur.p().y as u32), - under_arry: cur.img(), //.into(), + under_arry: cur.img(), //.into(), } } diff --git a/src/common/cpp_essentials/pattern.rs b/src/common/cpp_essentials/pattern.rs index f800bc7..be25007 100644 --- a/src/common/cpp_essentials/pattern.rs +++ b/src/common/cpp_essentials/pattern.rs @@ -217,11 +217,6 @@ impl<'a> PatternView<'a> { || Into::::into(self.data.0[self.count]) >= Into::::into(self.sum(None)) * scale } - // template - // bool hasQuietZoneBefore(float scale) const - // { - // return (acceptIfAtFirstBar && isAtFirstBar()) || _data[-1] >= sum() * scale; - // } pub fn hasQuietZoneAfter(&self, scale: f32, acceptIfAtLastBar: Option) -> bool { (acceptIfAtLastBar.unwrap_or(true) && self.isAtLastBar()) @@ -229,12 +224,6 @@ impl<'a> PatternView<'a> { >= Into::::into(self.sum(None)) * scale } - // template - // bool hasQuietZoneAfter(float scale) const - // { - // return (acceptIfAtLastBar && isAtLastBar()) || _data[_size] >= sum() * scale; - // } - pub fn subView(&self, offset: usize, size: Option) -> PatternView<'a> { let mut size = size.unwrap_or(0); if size == 0 { @@ -251,28 +240,11 @@ impl<'a> PatternView<'a> { } } - // PatternView subView(int offset, int size = 0) const - // { - // // if(std::abs(size) > count()) - // // printf("%d > %d\n", std::abs(size), _count); - // // assert(std::abs(size) <= count()); - // if (size == 0) - // size = _size - offset; - // else if (size < 0) - // size = _size - offset + size; - // return {begin() + offset, std::max(size, 0), _base, _end}; - // } - pub fn shift(&mut self, n: usize) -> bool { self.current += n; !self.data.0.is_empty() && self.start + self.count <= (self.start + self.count) } - // bool shift(int n) - // { - // return _data && ((_data += n) + _size <= _end); - // } - pub fn skipPair(&mut self) -> bool { self.shift(2) } @@ -361,6 +333,16 @@ impl<'a> Into> for &PatternView<'a> { } } +impl<'a, const LEN: usize> Into<[PatternType; LEN]> for &PatternView<'a> { + fn into(self) -> [PatternType; LEN] { + let mut result = [PatternType::default(); LEN]; + for i in 0..self.count { + result[i] = self[i]; + } + result + } +} + /** * @brief The BarAndSpace struct is a simple 2 element data structure to hold information about bar(s) and space(s). * @@ -407,7 +389,7 @@ impl std::ops::IndexMut for BarAndSpace // bool isValid() const { return bar != T{} && space != T{}; } // }; -type BarAndSpaceI = BarAndSpace; +// type BarAndSpaceI = BarAndSpace; /** * @brief FixedPattern describes a compile-time constant (start/stop) pattern. @@ -488,7 +470,7 @@ pub fn IsPattern = view.into(); + let v_src: [PatternType; LEN] = view.into(); let widths = BarAndSpaceSum::(&v_src); let sums = pattern.sums(); let modSize: BarAndSpace = BarAndSpace { @@ -627,13 +609,8 @@ pub fn FindLeftGuard<'a, const LEN: usize, const SUM: usize, const IS_SPARCE: bo { return false; } - IsPattern::( - window, - pattern, - spaceInPixel, - minQuietZone, - 0.0, - ) != 0.0 + IsPattern::(window, pattern, spaceInPixel, minQuietZone, 0.0) + != 0.0 }) } diff --git a/src/common/eci_string_builder.rs b/src/common/eci_string_builder.rs index eddf6fb..657906a 100644 --- a/src/common/eci_string_builder.rs +++ b/src/common/eci_string_builder.rs @@ -26,8 +26,6 @@ use std::{ fmt::{self}, }; - - use super::{CharacterSet, Eci, StringUtils}; /** diff --git a/src/common/hybrid_binarizer.rs b/src/common/hybrid_binarizer.rs index c2df49f..3fd4dbf 100644 --- a/src/common/hybrid_binarizer.rs +++ b/src/common/hybrid_binarizer.rs @@ -113,7 +113,7 @@ impl HybridBinarizer { let source = ghb.get_luminance_source(); let width = source.get_width(); let height = source.get_height(); - + // dbg!(matrix.to_string()); if width >= MINIMUM_DIMENSION && height >= MINIMUM_DIMENSION { let luminances = source.get_matrix(); diff --git a/src/common/quad.rs b/src/common/quad.rs index 54518cd..e2eee14 100644 --- a/src/common/quad.rs +++ b/src/common/quad.rs @@ -1,7 +1,5 @@ use crate::{point, Point}; - - #[derive(Clone, Copy, Debug)] pub struct Quadrilateral(pub [Point; 4]); diff --git a/src/datamatrix/detector/zxing_cpp_detector/mod.rs b/src/datamatrix/detector/zxing_cpp_detector/mod.rs index 75fc05c..5282907 100644 --- a/src/datamatrix/detector/zxing_cpp_detector/mod.rs +++ b/src/datamatrix/detector/zxing_cpp_detector/mod.rs @@ -5,7 +5,6 @@ pub(self) use crate::common::cpp_essentials::bitmatrix_cursor_trait::*; pub(self) use crate::common::cpp_essentials::dm_regression_line::*; pub(self) use crate::common::cpp_essentials::edge_tracer::*; - pub(self) use crate::common::cpp_essentials::util; pub use cpp_new_detector::detect; diff --git a/src/qrcode/cpp_port/decoder.rs b/src/qrcode/cpp_port/decoder.rs index d13bd64..f8298a3 100644 --- a/src/qrcode/cpp_port/decoder.rs +++ b/src/qrcode/cpp_port/decoder.rs @@ -9,8 +9,7 @@ use crate::common::reedsolomon::{ get_predefined_genericgf, PredefinedGenericGF, ReedSolomonDecoder, }; use crate::common::{ - AIFlag, BitMatrix, BitSource, CharacterSet, ECIStringBuilder, Eci, Result, - SymbologyIdentifier, + AIFlag, BitMatrix, BitSource, CharacterSet, ECIStringBuilder, Eci, Result, SymbologyIdentifier, }; use crate::qrcode::cpp_port::bitmatrix_parser::{ ReadCodewords, ReadFormatInformation, ReadVersion, diff --git a/src/qrcode/cpp_port/detector.rs b/src/qrcode/cpp_port/detector.rs index ed948ca..3686170 100644 --- a/src/qrcode/cpp_port/detector.rs +++ b/src/qrcode/cpp_port/detector.rs @@ -4,7 +4,8 @@ use crate::{ CenterOfRing, DMRegressionLine, FindConcentricPatternCorners, FindLeftGuardBy, Matrix, }, DefaultGridSampler, GridSampler, Result, SamplerControl, - }, point_i, + }, + point_i, qrcode::{ decoder::{FormatInformation, Version, VersionRef}, detector::QRCodeDetectorResult, @@ -16,10 +17,9 @@ use multimap::MultiMap; use crate::{ common::{ cpp_essentials::{ - BitMatrixCursorTrait, ConcentricPattern, Direction, EdgeTracer, - FixedPattern, GetPatternRowTP, IsPattern, LocateConcentricPattern, - PatternRow, PatternType, PatternView, ReadSymmetricPattern, RegressionLine, - RegressionLineTrait, + BitMatrixCursorTrait, ConcentricPattern, Direction, EdgeTracer, FixedPattern, + GetPatternRowTP, IsPattern, LocateConcentricPattern, PatternRow, PatternType, + PatternView, ReadSymmetricPattern, RegressionLine, RegressionLineTrait, }, BitMatrix, PerspectiveTransform, Quadrilateral, }, @@ -502,8 +502,16 @@ pub fn SampleQR(image: &BitMatrix, fp: &FinderPatternSet) -> Result left.dim { top } else { left } - } else if top.err < left.err { top } else { left }; + if top.dim > left.dim { + top + } else { + left + } + } else if top.err < left.err { + top + } else { + left + }; let mut dimension = best.dim; let moduleSize = (best.ms + 1.0) as i32; diff --git a/src/qrcode/cpp_port/qr_cpp_reader.rs b/src/qrcode/cpp_port/qr_cpp_reader.rs index 1510df5..6d711a2 100644 --- a/src/qrcode/cpp_port/qr_cpp_reader.rs +++ b/src/qrcode/cpp_port/qr_cpp_reader.rs @@ -120,8 +120,8 @@ use crate::{ common::{cpp_essentials::ConcentricPattern, DetectorRXingResult}, multi::MultipleBarcodeReader, - BarcodeFormat, DecodeHintType, DecodeHintValue, DecodingHintDictionary, - Exceptions, RXingResult, Reader, + BarcodeFormat, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions, + RXingResult, Reader, }; use super::{ @@ -132,8 +132,6 @@ use super::{ }, }; - - #[derive(Default)] pub struct QrReader; diff --git a/src/qrcode/cpp_port/test/QRDataMaskTest.rs b/src/qrcode/cpp_port/test/QRDataMaskTest.rs index 47ab795..c332ba8 100644 --- a/src/qrcode/cpp_port/test/QRDataMaskTest.rs +++ b/src/qrcode/cpp_port/test/QRDataMaskTest.rs @@ -8,86 +8,62 @@ use crate::{common::BitMatrix, qrcode::cpp_port::data_mask::GetMaskedBit}; #[test] fn Mask0() { - TestMaskAcrossDimensions(0, |i, j| { - (i + j) % 2 == 0 - }); + TestMaskAcrossDimensions(0, |i, j| (i + j) % 2 == 0); } #[test] fn Mask1() { - TestMaskAcrossDimensions(1, |i, _| { - i % 2 == 0 - }); + TestMaskAcrossDimensions(1, |i, _| i % 2 == 0); } #[test] fn Mask2() { - TestMaskAcrossDimensions(2, |_, j| { - j % 3 == 0 - }); + TestMaskAcrossDimensions(2, |_, j| j % 3 == 0); } #[test] fn Mask3() { - TestMaskAcrossDimensions(3, |i, j| { - (i + j) % 3 == 0 - }); + TestMaskAcrossDimensions(3, |i, j| (i + j) % 3 == 0); } #[test] fn Mask4() { - TestMaskAcrossDimensions(4, |i, j| { - (i / 2 + j / 3) % 2 == 0 - }); + TestMaskAcrossDimensions(4, |i, j| (i / 2 + j / 3) % 2 == 0); } #[test] fn Mask5() { - TestMaskAcrossDimensions(5, |i, j| { - (i * j) % 2 + (i * j) % 3 == 0 - }); + TestMaskAcrossDimensions(5, |i, j| (i * j) % 2 + (i * j) % 3 == 0); } #[test] fn Mask6() { - TestMaskAcrossDimensions(6, |i, j| { - ((i * j) % 2 + (i * j) % 3) % 2 == 0 - }); + TestMaskAcrossDimensions(6, |i, j| ((i * j) % 2 + (i * j) % 3) % 2 == 0); } #[test] fn Mask7() { - TestMaskAcrossDimensions(7, |i, j| { - ((i + j) % 2 + (i * j) % 3) % 2 == 0 - }); + TestMaskAcrossDimensions(7, |i, j| ((i + j) % 2 + (i * j) % 3) % 2 == 0); } #[test] fn MicroMask0() { - TestMicroMaskAcrossDimensions(0, |i, _| { - i % 2 == 0 - }); + TestMicroMaskAcrossDimensions(0, |i, _| i % 2 == 0); } #[test] fn MicroMask1() { - TestMicroMaskAcrossDimensions(1, |i, j| { - (i / 2 + j / 3) % 2 == 0 - }); + TestMicroMaskAcrossDimensions(1, |i, j| (i / 2 + j / 3) % 2 == 0); } #[test] fn MicroMask2() { - TestMicroMaskAcrossDimensions(2, |i, j| { - ((i * j) % 2 + (i * j) % 3) % 2 == 0 - }); + TestMicroMaskAcrossDimensions(2, |i, j| ((i * j) % 2 + (i * j) % 3) % 2 == 0); } #[test] fn MicroMask3() { - TestMicroMaskAcrossDimensions(3, |i, j| { - ((i + j) % 2 + (i * j) % 3) % 2 == 0 - }); + TestMicroMaskAcrossDimensions(3, |i, j| ((i + j) % 2 + (i * j) % 3) % 2 == 0); } fn TestMaskAcrossDimensionsImpl( diff --git a/src/qrcode/cpp_port/test/QRDecodedBitStreamParserTest.rs b/src/qrcode/cpp_port/test/QRDecodedBitStreamParserTest.rs index b19ff37..68c19b1 100644 --- a/src/qrcode/cpp_port/test/QRDecodedBitStreamParserTest.rs +++ b/src/qrcode/cpp_port/test/QRDecodedBitStreamParserTest.rs @@ -23,10 +23,8 @@ // using namespace ZXing; // using namespace ZXing::QRCode; - - use crate::{ - common::{BitArray}, + common::BitArray, qrcode::{ cpp_port::decoder::DecodeBitStream, decoder::{ErrorCorrectionLevel, Version}, diff --git a/src/qrcode/cpp_port/test/QRVersionTest.rs b/src/qrcode/cpp_port/test/QRVersionTest.rs index 6228b62..6decf1b 100644 --- a/src/qrcode/cpp_port/test/QRVersionTest.rs +++ b/src/qrcode/cpp_port/test/QRVersionTest.rs @@ -43,8 +43,8 @@ fn VersionForNumber() { fn GetProvisionalVersionForDimension() { for i in 1..=40 { // for (int i = 1; i <= 40; i++) { - let prov = - Version::FromDimension(4 * i + 17).unwrap_or_else(|_| panic!("version should exist for {i}")); + let prov = Version::FromDimension(4 * i + 17) + .unwrap_or_else(|_| panic!("version should exist for {i}")); // assert_ne!(prov, nullptr); assert_eq!(i, prov.getVersionNumber()); } diff --git a/src/qrcode/decoder/mode.rs b/src/qrcode/decoder/mode.rs index 5a212f6..858f4ba 100644 --- a/src/qrcode/decoder/mode.rs +++ b/src/qrcode/decoder/mode.rs @@ -151,10 +151,10 @@ impl Mode { return Mode::try_from(bits); } } else { - const Bits2Mode: [Mode; BITS_2_MODE_LEN] = + const BITS_2_MODE: [Mode; BITS_2_MODE_LEN] = [Mode::NUMERIC, Mode::ALPHANUMERIC, Mode::BYTE, Mode::KANJI]; if (bits as usize) < BITS_2_MODE_LEN { - return Ok(Bits2Mode[bits as usize]); + return Ok(BITS_2_MODE[bits as usize]); } }