From 96cadb8e665512b4f8913ba9a35186881a287b30 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Wed, 26 Apr 2023 13:17:57 -0500 Subject: [PATCH] some progress in passing images --- .../cpp_essentials/concentric_finder.rs | 4 +- .../fast_edge_to_edge_counter.rs | 16 ++++---- src/common/cpp_essentials/pattern.rs | 22 ++++++++--- src/qrcode/cpp_port/detector.rs | 39 ++++++++++++------- tests/cpp_qr_code_blackbox_tests.rs | 1 + 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/common/cpp_essentials/concentric_finder.rs b/src/common/cpp_essentials/concentric_finder.rs index 2e2a887..afb4377 100644 --- a/src/common/cpp_essentials/concentric_finder.rs +++ b/src/common/cpp_essentials/concentric_finder.rs @@ -263,7 +263,7 @@ pub fn CenterOfRings( // for (int i = 1; i < numOfRings; ++i) { let c = CenterOfRing(image, center.floor(), range, i as i32, false)?; - if !(c == Point::default()) { + if (c == Point::default()) { if n == 1 { return None; } else { @@ -566,7 +566,7 @@ pub fn LocateConcentricPattern Option { - let mut cur = EdgeTracer::new(image, center, Point::default()); + let mut cur = EdgeTracer::new(image, center.floor(), Point::default()); let mut minSpread = image.getWidth() as i32; let mut maxSpread = 0_i32; diff --git a/src/common/cpp_essentials/fast_edge_to_edge_counter.rs b/src/common/cpp_essentials/fast_edge_to_edge_counter.rs index dba41a6..d28729c 100644 --- a/src/common/cpp_essentials/fast_edge_to_edge_counter.rs +++ b/src/common/cpp_essentials/fast_edge_to_edge_counter.rs @@ -20,23 +20,23 @@ impl<'a> FastEdgeToEdgeCounter<'a> { let p = ((cur.p().y as isize * cur.img().width() as isize).abs() as i32 + cur.p().x as i32) as u32; // P IS SET WRONG IN REVERSE - let maxStepsX = if cur.d().x != 0.0 { + let maxStepsX: i32 = if cur.d().x != 0.0 { if cur.d().x > 0.0 { - cur.img().width() - 1 - cur.p().x as u32 + cur.img().width() as i32 - 1 - cur.p().x as i32 } else { - cur.p().x as u32 + cur.p().x as i32 } } else { - u32::MAX + i32::MAX }; - let maxStepsY = if cur.d().y != 0.0 { + let maxStepsY: i32 = if cur.d().y != 0.0 { if cur.d().y > 0.0 { - cur.img().height() - 1 - cur.p().y as u32 + cur.img().height() as i32 - 1 - cur.p().y as i32 } else { - cur.p().y as u32 + cur.p().y as i32 } } else { - u32::MAX + i32::MAX }; let stepsToBorder = std::cmp::min(maxStepsX, maxStepsY) as i32; diff --git a/src/common/cpp_essentials/pattern.rs b/src/common/cpp_essentials/pattern.rs index 704c07a..f04b873 100644 --- a/src/common/cpp_essentials/pattern.rs +++ b/src/common/cpp_essentials/pattern.rs @@ -351,6 +351,16 @@ impl<'a> std::ops::Index for PatternView<'_> { } } +impl<'a> Into> for &PatternView<'a> { + fn into(self) -> Vec { + let mut v = vec![PatternType::default(); self.count as usize]; + for i in 0..self.count { + v[i] = self[i]; + } + v + } +} + /** * @brief The BarAndSpace struct is a simple 2 element data structure to hold information about bar(s) and space(s). * @@ -473,13 +483,13 @@ pub fn IsPattern, ) -> f32 { - let e2e = E2E; //e2e.unwrap_or(false); + //let e2e = E2E; //e2e.unwrap_or(false); let mut module_size_ref = module_size_ref; - if (e2e) { + if E2E { //using float_t = double; - - let widths = BarAndSpaceSum::(&view.data().0); + let v_src: Vec = view.into(); + let widths = BarAndSpaceSum::(&v_src); let sums = pattern.sums(); let modSize: BarAndSpace = BarAndSpace { bar: widths[0] / sums[0] as f64, @@ -534,7 +544,7 @@ pub fn IsPattern) } let mut window = view.subView(0, Some(LEN)); - if window.isAtFirstBar() && isGuard(&window, None) { + if window.isAtFirstBar() && isGuard(&window, Some(f32::MAX)) { return Ok(window); } let end = Into::::into(view.end().ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?) - minSize; diff --git a/src/qrcode/cpp_port/detector.rs b/src/qrcode/cpp_port/detector.rs index 618b541..850aaad 100644 --- a/src/qrcode/cpp_port/detector.rs +++ b/src/qrcode/cpp_port/detector.rs @@ -1,6 +1,8 @@ use crate::{ common::{ - cpp_essentials::{CenterOfRing, DMRegressionLine, FindConcentricPatternCorners, Matrix}, + cpp_essentials::{ + CenterOfRing, DMRegressionLine, FindConcentricPatternCorners, FindLeftGuardBy, Matrix, + }, DefaultGridSampler, GridSampler, Result, SamplerControl, }, dimension, point_g, point_i, @@ -35,18 +37,26 @@ pub struct FinderPatternSet { pub type FinderPatterns = Vec; pub type FinderPatternSets = Vec; -const PATTERN: FixedPattern<5, 7, false> = FixedPattern::new([1, 1, 3, 1, 1]); +const LEN: usize = 5; +const SUM: usize = 7; +const PATTERN: FixedPattern = FixedPattern::new([1, 1, 3, 1, 1]); const E2E: bool = true; -// pub fn FindPattern( view: &PatternView) -> PatternView -// { -// return FindLeftGuard(view, PATTERN.size(), [](const PatternView& view, int spaceInPixel) { -// // perform a fast plausability test for 1:1:3:1:1 pattern -// if (view[2] < 2 * std::max(view[0], view[4]) || view[2] < std::max(view[1], view[3])) -// return 0.f; -// return IsPattern(view, PATTERN, spaceInPixel, 0.5); -// }); -// } +fn FindPattern<'a>(view: PatternView<'a>) -> Result> { + FindLeftGuardBy::( + view, + LEN, + |view: &PatternView, spaceInPixel: Option| { + // perform a fast plausability test for 1:1:3:1:1 pattern + if (view[2] < 2 as PatternType * std::cmp::max(view[0], view[4]) + || view[2] < std::cmp::max(view[1], view[3])) + { + return false; + } + IsPattern::(view, &PATTERN, spaceInPixel, 0.5, 0.0) != 0.0 + }, + ) +} /// Locate the finder patterns for the symbol. /// This function can panic @@ -74,7 +84,7 @@ pub fn FindFinderPatterns(image: &BitMatrix, tryHarder: bool) -> FinderPatterns let mut next: PatternView = PatternView::new(&row); while { - if let Ok(up_next) = FindLeftGuard(next, 0, &PATTERN, 0.5) { + if let Ok(up_next) = FindPattern(next) { next = up_next; next.isValid() } else { @@ -105,7 +115,7 @@ pub fn FindFinderPatterns(image: &BitMatrix, tryHarder: bool) -> FinderPatterns // Reduce(next) * 3); // 3 for very skewed samples if (pattern.is_some()) { // log(*pattern, 3); - assert!(image.get_point(pattern.as_ref().unwrap().p)); + // assert!(image.get_point(pattern.as_ref().unwrap().p)); res.push(pattern.unwrap()); } } @@ -432,7 +442,8 @@ pub fn LocateAlignmentPattern( } if let Some(cor1) = CenterOfRing(image, cor.unwrap(), moduleSize, 1, true) { - if let Some(cor2) = CenterOfRing(image, cor.unwrap(), moduleSize * 3, -2, true) { + if let Some(cor2) = CenterOfRing(image, cor.unwrap().floor(), moduleSize * 3, -2, true) + { if Point::distance(cor1, cor2) < moduleSize as f32 / 2.0 { let res = (cor1 + cor2) / 2.0; // log(res, 3); diff --git a/tests/cpp_qr_code_blackbox_tests.rs b/tests/cpp_qr_code_blackbox_tests.rs index 0360ce3..d4517f8 100644 --- a/tests/cpp_qr_code_blackbox_tests.rs +++ b/tests/cpp_qr_code_blackbox_tests.rs @@ -209,6 +209,7 @@ fn cpp_qrcode_black_box3_test_case() { QrReader::default(), BarcodeFormat::QR_CODE, ); + tester.add_test(28, 28, 0.0); tester.add_test(28, 28, 90.0); tester.add_test(28, 28, 180.0);