From 74a830f462c157d6361ae8cca35d44d628899d8a Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sat, 29 Apr 2023 17:51:26 -0500 Subject: [PATCH] switch to using Point in one-d-derive macros Also start updates to make point more generic, still not complete or fully implemented. Some code cleanup --- Cargo.toml | 2 +- crates/one-d-proc-derive/Cargo.toml | 2 +- crates/one-d-proc-derive/src/lib.rs | 4 +-- src/common/bit_source.rs | 2 +- src/common/cpp_essentials/pattern.rs | 7 +++--- src/qrcode/cpp_port/decoder.rs | 2 +- src/qrcode/cpp_port/detector.rs | 26 ++++++++++--------- src/rxing_result_point.rs | 37 +++++++++++++++++++++++----- 8 files changed, 55 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2f30049..196b61b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ image = {version = "0.24", optional = true} imageproc = {version = "0.23", optional = true} unicode-segmentation = "1.10" codepage-437 = "0.1.0" -rxing-one-d-proc-derive = {version = "0.4", path ="./crates/one-d-proc-derive"} +rxing-one-d-proc-derive = {version = "0.5", path ="./crates/one-d-proc-derive"} num = "0.4.0" svg = {version = "0.13", optional = true} resvg = {version = "0.29.0", optional = true, default-features=false} diff --git a/crates/one-d-proc-derive/Cargo.toml b/crates/one-d-proc-derive/Cargo.toml index f55fa5a..6dbf0e6 100644 --- a/crates/one-d-proc-derive/Cargo.toml +++ b/crates/one-d-proc-derive/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rxing-one-d-proc-derive" repository = "https://github.com/rxing-core/rxing/tree/main/crates/one-d-proc-derive" -version = "0.4.0" +version = "0.5.0" edition = "2021" description = "proc macros to simplify the development of one-d barcode symbologies in rxing (https://github.com/rxing-core/rxing)" license = "Apache-2.0" diff --git a/crates/one-d-proc-derive/src/lib.rs b/crates/one-d-proc-derive/src/lib.rs index 59ac4f1..45d7de7 100644 --- a/crates/one-d-proc-derive/src/lib.rs +++ b/crates/one-d-proc-derive/src/lib.rs @@ -20,7 +20,7 @@ fn impl_one_d_reader_macro(ast: &syn::DeriveInput) -> TokenStream { use crate::DecodingHintDictionary; use crate::RXingResultMetadataType; use crate::RXingResultMetadataValue; - use crate::RXingResultPoint; + use crate::Point; use crate::Reader; use crate::Binarizer; @@ -64,7 +64,7 @@ fn impl_one_d_reader_macro(ast: &syn::DeriveInput) -> TokenStream { let points = result.getRXingResultPointsMut(); for i in 0..total_points{ // for (int i = 0; i < points.length; i++) { - points[i] = RXingResultPoint::new(height as f32- points[i].getY() - 1.0, points[i].getX()); + points[i] = Point::new(height as f32- points[i].getY() - 1.0, points[i].getX()); } // } diff --git a/src/common/bit_source.rs b/src/common/bit_source.rs index cfb4174..2d9bae2 100644 --- a/src/common/bit_source.rs +++ b/src/common/bit_source.rs @@ -159,7 +159,7 @@ impl BitSource { let mask = (0xFF >> bits_to_not_read) << bits_to_not_read; result = (result << num_bits) | ((self.bytes[byte_offset] & mask) as u32 >> bits_to_not_read); - bit_offset += num_bits; + //bit_offset += num_bits; // Removed, this is a holdover from setting the actual values in c++ } } diff --git a/src/common/cpp_essentials/pattern.rs b/src/common/cpp_essentials/pattern.rs index be25007..bd8ebd4 100644 --- a/src/common/cpp_essentials/pattern.rs +++ b/src/common/cpp_essentials/pattern.rs @@ -228,9 +228,10 @@ impl<'a> PatternView<'a> { let mut size = size.unwrap_or(0); if size == 0 { size = self.count - offset; - } else if size < 0 { - size += self.count - offset; - } + } + // else if size < 0 { + // size += self.count - offset; + // } PatternView { data: self.data, diff --git a/src/qrcode/cpp_port/decoder.rs b/src/qrcode/cpp_port/decoder.rs index f8298a3..491b639 100644 --- a/src/qrcode/cpp_port/decoder.rs +++ b/src/qrcode/cpp_port/decoder.rs @@ -145,7 +145,7 @@ pub fn ToAlphaNumericChar(value: u32) -> Result { ' ', '$', '%', '*', '+', '-', '.', '/', ':', ]; - if value < 0 || value >= (ALPHANUMERIC_CHARS.len()) { + if value >= (ALPHANUMERIC_CHARS.len()) { return Err(Exceptions::index_out_of_bounds_with( "oAlphaNumericChar: out of range", )); diff --git a/src/qrcode/cpp_port/detector.rs b/src/qrcode/cpp_port/detector.rs index 3686170..c091895 100644 --- a/src/qrcode/cpp_port/detector.rs +++ b/src/qrcode/cpp_port/detector.rs @@ -590,20 +590,23 @@ pub fn SampleQR(image: &BitMatrix, fp: &FinderPatternSet) -> Result>(Size(apM), Size(apM)); // found/guessed alignment pattern positions in pixels let N = (apM.len()) - 1; // project the alignment pattern at module coordinates x/y to pixel coordinate based on current mod2Pix - let projectM2P = /*[&mod2Pix, &apM]*/| x, y, mod2Pix: &PerspectiveTransform| { mod2Pix.transform_point(Point::centered(point_i(apM[x], apM[y]))) }; + let projectM2P = |x, y, mod2Pix: &PerspectiveTransform| { + mod2Pix.transform_point(Point::centered(point_i(apM[x], apM[y]))) + }; - let mut findInnerCornerOfConcentricPattern = /*[&image, &apP, &projectM2P]*/| x, y, fp:ConcentricPattern| { - let pc = apP.set(x, y, projectM2P(x, y, &mod2Pix)); - if let Some(fpQuad) = FindConcentricPatternCorners(image, fp.p, fp.size, 2) - // if (auto fpQuad = FindConcentricPatternCorners(image, fp, fp.size, 2)) - {for c in fpQuad .0 - {if Point::distance(c, pc) < (fp.size as f32) / 2.0 - {apP.set(x, y, c);}}} - }; + let mut findInnerCornerOfConcentricPattern = |x, y, fp: ConcentricPattern| { + let pc = apP.set(x, y, projectM2P(x, y, &mod2Pix)); + if let Some(fpQuad) = FindConcentricPatternCorners(image, fp.p, fp.size, 2) { + for c in fpQuad.0 { + if Point::distance(c, pc) < (fp.size as f32) / 2.0 { + apP.set(x, y, c); + } + } + } + }; findInnerCornerOfConcentricPattern(0, 0, fp.tl); findInnerCornerOfConcentricPattern(0, N, fp.bl); @@ -823,11 +826,10 @@ pub fn DetectPureQR(image: &BitMatrix) -> Result { [tr, point(-1.0, 1.0)], [bl, point(1.0, -1.0)], ] { - // for (auto [p, d] : {std::pair(tl, PointI{1, 1}), {tr, {-1, 1}}, {bl, {1, -1}}}) { diagonal = EdgeTracer::new(image, p, d) .readPatternFromBlack(1, Some((width / 3 + 1) as i32)) .ok_or(Exceptions::NOT_FOUND)?; - // diagonal = BitMatrixCursorI(image, p, d).readPatternFromBlack(1, width / 3 + 1); + let diag_hld = diagonal.to_vec().into(); let view = PatternView::new(&diag_hld); if !(IsPattern::(&view, &PATTERN, None, 0.0, 0.0) != 0.0) { diff --git a/src/rxing_result_point.rs b/src/rxing_result_point.rs index 069d8ad..08b5abb 100644 --- a/src/rxing_result_point.rs +++ b/src/rxing_result_point.rs @@ -15,9 +15,37 @@ use crate::ResultPoint; */ #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, Default)] -pub struct Point { - pub(crate) x: f32, - pub(crate) y: f32, +pub struct PointT { + pub x: T, + pub y: T, +} +// #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +// #[derive(Debug, Clone, Copy, Default)] +// pub struct Point { +// pub(crate) x: f32, +// pub(crate) y: f32, +// } + +pub type PointF = PointT; +pub type PointI = PointT; +pub type Point = PointF; + +impl Into for Point { + fn into(self) -> PointI { + PointI{ + x: self.x.floor() as u32, + y: self.y.floor() as u32, + } + } +} + +impl Into for PointI { + fn into(self) -> Point { + Point { + x: self.x as f32, + y: self.y as f32, + } + } } /** An alias for `Point::new`. */ @@ -33,9 +61,6 @@ pub fn point_i>(x: T, y: T) -> Point { Point::new(x.into() as f32, y.into() as f32) } -/** Currently necessary because the external OneDReader proc macro uses it. */ -pub type RXingResultPoint = Point; - impl Hash for Point { fn hash(&self, state: &mut H) { self.x.to_string().hash(state);