From d56e0a26749fd41c1038877497eddd6ec0009862 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sat, 20 Aug 2022 17:04:16 -0500 Subject: [PATCH] accidentally deleted, repair --- src/common/detector/MathUtils.rs | 68 ++++++++++++++++--- .../detector/MonochromeRectangleDetector.rs | 67 +++++++++--------- src/common/detector/WhiteRectangleDetector.rs | 66 +++++++++--------- src/common/detector/mod.rs | 7 ++ src/common/mod.rs | 2 + src/common/reedsolomon/mod.rs | 0 src/lib.rs | 1 + 7 files changed, 137 insertions(+), 74 deletions(-) create mode 100644 src/common/detector/mod.rs create mode 100644 src/common/mod.rs create mode 100644 src/common/reedsolomon/mod.rs diff --git a/src/common/detector/MathUtils.rs b/src/common/detector/MathUtils.rs index 8118837..50160f5 100644 --- a/src/common/detector/MathUtils.rs +++ b/src/common/detector/MathUtils.rs @@ -15,6 +15,7 @@ */ //package com.google.zxing.common.detector; +use std::{i32,f32}; /** * General math-related and numeric utility functions. @@ -30,7 +31,7 @@ * @return nearest {@code int} */ pub fn round(d:f32) -> i32 { - return (d + (if d < 0.0f { -0.5f } else { 0.5f})); + return (d + (if d < 0.0f32 { -0.5f32 } else { 0.5f32})) as i32; } /** @@ -40,10 +41,10 @@ * @param bY point B y coordinate * @return Euclidean distance between points A and B */ - pub fn distance( aX:f32, aY:f32, bX: f32, bY:f32) -> f32 { - let xDiff:f64 = aX - bX; - let yDiff:f64 = aY - bY; - return Math.sqrt(xDiff * xDiff + yDiff * yDiff); + pub fn distance_float( aX:f32, aY:f32, bX: f32, bY:f32) -> f32 { + let xDiff:f64 = (aX - bX).into(); + let yDiff:f64 = (aY - bY).into(); + return (xDiff * xDiff + yDiff * yDiff).sqrt() as f32; } /** @@ -53,10 +54,10 @@ * @param bY point B y coordinate * @return Euclidean distance between points A and B */ - pub fn distance( aX : i32, aY: i32, bX: i32, bY: i32) -> f32 { - let xDiff : f64 = aX - bX; - let yDiff :f64 = aY - bY; - return Math.sqrt(xDiff * xDiff + yDiff * yDiff); + pub fn distance_int( aX : i32, aY: i32, bX: i32, bY: i32) -> f32 { + let xDiff : f64 = (aX - bX).into(); + let yDiff :f64 = (aY - bY).into(); + return (xDiff * xDiff + yDiff * yDiff).sqrt() as f32; } /** @@ -69,4 +70,53 @@ count += a; } return count; + } + + #[cfg(test)] + mod tests { + use crate::common::detector::MathUtils; + + static EPSILON : f32 = 1.0E-8f32; + + #[test] + fn testRound() { + assert_eq!(-1, MathUtils::round(-1.0f32)); + assert_eq!(0, MathUtils::round(0.0f32)); + assert_eq!(1, MathUtils::round(1.0f32)); + + assert_eq!(2, MathUtils::round(1.9f32)); + assert_eq!(2, MathUtils::round(2.1f32)); + + assert_eq!(3, MathUtils::round(2.5f32)); + + assert_eq!(-2, MathUtils::round(-1.9f32)); + assert_eq!(-2, MathUtils::round(-2.1f32)); + + assert_eq!(-3, MathUtils::round(-2.5f32)); // This differs from Math.round() + + assert_eq!(i32::MAX, MathUtils::round(i32::MAX as f32)); + assert_eq!(i32::MIN, MathUtils::round(i32::MIN as f32)); + + assert_eq!(i32::MAX, MathUtils::round(f32::MAX)); + assert_eq!(i32::MIN, MathUtils::round(f32::NEG_INFINITY)); + + assert_eq!(0, MathUtils::round(f32::NAN)); + } + + #[test] + fn testDistance() { + assert_eq!((8.0f32).sqrt(), MathUtils::distance_float(1.0f32, 2.0f32, 3.0f32, 4.0f32)); + assert_eq!(0.0f32, MathUtils::distance_float(1.0f32, 2.0f32, 1.0f32, 2.0f32)); + + assert_eq!((8.0f32).sqrt(), MathUtils::distance_int(1, 2, 3, 4)); + assert_eq!(0.0f32, MathUtils::distance_int(1, 2, 1, 2)); + } + + #[test] + fn testSum() { + assert_eq!(0, MathUtils::sum(&vec![])); + assert_eq!(1, MathUtils::sum(&[1])); + assert_eq!(4, MathUtils::sum(&[1, 3])); + assert_eq!(0, MathUtils::sum(&[-1, 1])); + } } \ No newline at end of file diff --git a/src/common/detector/MonochromeRectangleDetector.rs b/src/common/detector/MonochromeRectangleDetector.rs index 98134ee..9a59d91 100644 --- a/src/common/detector/MonochromeRectangleDetector.rs +++ b/src/common/detector/MonochromeRectangleDetector.rs @@ -26,9 +26,9 @@ use crate::{NotFoundException, RXingResultPoint}; * @author Sean Owen * @deprecated without replacement since 3.3.0 */ +const MAX_MODULES : i32 = 32; #[deprecated] pub struct MonochromeRectangleDetector { - MAX_MODULES: i32, image: BitMatrix, } @@ -36,7 +36,6 @@ pub struct MonochromeRectangleDetector { impl MonochromeRectangleDetector { pub fn new(image: &BitMatrix) -> Self { Self { - MAX_MODULES: 32, image: image, } } @@ -51,19 +50,19 @@ impl MonochromeRectangleDetector { * third, the rightmost * @throws NotFoundException if no Data Matrix Code can be found */ - pub fn detect() -> Result, NotFoundException> { - let height = image.getHeight(); - let width = image.getWidth(); + pub fn detect(&self) -> Result, NotFoundException> { + let height = self.image.getHeight(); + let width = self.image.getWidth(); let halfHeight = height / 2; let halfWidth = width / 2; - let deltaY = Math.max(1, height / (MAX_MODULES * 8)); - let deltaX = Math.max(1, width / (MAX_MODULES * 8)); + let deltaY = 1.max( height / (MAX_MODULES * 8)); + let deltaX = 1.max( width / (MAX_MODULES * 8)); let top = 0; let bottom = height; let left = 0; let right = width; - let pointA = findCornerFromCenter( + let pointA = self.findCornerFromCenter( halfWidth, 0, left, @@ -73,9 +72,9 @@ impl MonochromeRectangleDetector { top, bottom, halfWidth / 2, - ); + )?; top = pointA.getY() - 1; - let pointB = findCornerFromCenter( + let pointB = self.findCornerFromCenter( halfWidth, -deltaX, left, @@ -85,9 +84,9 @@ impl MonochromeRectangleDetector { top, bottom, halfHeight / 2, - ); + )?; left = pointB.getX() - 1; - let pointC = findCornerFromCenter( + let pointC = self.findCornerFromCenter( halfWidth, deltaX, left, @@ -97,9 +96,9 @@ impl MonochromeRectangleDetector { top, bottom, halfHeight / 2, - ); + )?; right = pointC.getX() + 1; - let pointD = findCornerFromCenter( + let pointD = self.findCornerFromCenter( halfWidth, 0, left, @@ -109,11 +108,11 @@ impl MonochromeRectangleDetector { top, bottom, halfWidth / 2, - ); + )?; bottom = pointD.getY() + 1; // Go try to find point A again with better information -- might have been off at first. - pointA = findCornerFromCenter( + pointA = self.findCornerFromCenter( halfWidth, 0, left, @@ -123,9 +122,9 @@ impl MonochromeRectangleDetector { top, bottom, halfWidth / 4, - ); + )?; - return Vec!([pointA, pointB, pointC, pointD]); + return vec!([pointA, pointB, pointC, pointD]); } /** @@ -147,6 +146,7 @@ impl MonochromeRectangleDetector { * @throws NotFoundException if such a point cannot be found */ fn findCornerFromCenter( + &self, centerX: i32, deltaX: i32, left: i32, @@ -157,20 +157,20 @@ impl MonochromeRectangleDetector { bottom: i32, maxWhiteRun: i32, ) -> Result { - lastRange = null; + let lastRange : Option> = None; let y: i32 = centerY; let x: i32 = centerX; while (y < bottom && y >= top && x < right && x >= left) { - let range: Vec::new(); + let range: Option>; if (deltaX == 0) { // horizontal slices, up and down - range = blackWhiteRange(y, maxWhiteRun, left, right, true); + range = self.blackWhiteRange(y, maxWhiteRun, left, right, true); } else { // vertical slices, left and right - range = blackWhiteRange(x, maxWhiteRun, top, bottom, false); + range = self.blackWhiteRange(x, maxWhiteRun, top, bottom, false); } - if (range == null) { - if (lastRange == null) { + if (range .is_none()) { + if (lastRange .is_none()) { return Err(NotFoundException.getNotFoundInstance()); } // lastRange was found @@ -225,6 +225,7 @@ impl MonochromeRectangleDetector { * (e.g. only white was found) */ fn blackWhiteRange( + &self, fixedDimension: i32, maxWhiteRun: i32, minDim: i32, @@ -237,9 +238,9 @@ impl MonochromeRectangleDetector { let start = center; while (start >= minDim) { if (if horizontal { - image.get(start, fixedDimension) + self.image.get(start, fixedDimension) } else { - image.get(fixedDimension, start) + self.image.get(fixedDimension, start) }) { start = start - 1; } else { @@ -247,9 +248,9 @@ impl MonochromeRectangleDetector { start = start - 1; while start >= minDim && !(if horizontal { - image.get(start, fixedDimension); + self.image.get(start, fixedDimension) } else { - image.get(fixedDimension, start); + self.image.get(fixedDimension, start) }) { start = start - 1; @@ -267,9 +268,9 @@ impl MonochromeRectangleDetector { let end = center; while (end < maxDim) { if (if horizontal { - image.get(end, fixedDimension) + self.image.get(end, fixedDimension) } else { - image.get(fixedDimension, end) + self.image.get(fixedDimension, end) }) { end = end + 1; } else { @@ -277,9 +278,9 @@ impl MonochromeRectangleDetector { end = end + 1; while end < maxDim && !(if horizontal { - image.get(end, fixedDimension) + self.image.get(end, fixedDimension) } else { - image.get(fixedDimension, end) + self.image.get(fixedDimension, end) }) { end = end + 1; @@ -294,7 +295,7 @@ impl MonochromeRectangleDetector { end = end - 1; return if end > start { - Some(Vec! {start, end}) + Some(vec! {start, end}) } else { None }; diff --git a/src/common/detector/WhiteRectangleDetector.rs b/src/common/detector/WhiteRectangleDetector.rs index 3d7a51c..770d45c 100644 --- a/src/common/detector/WhiteRectangleDetector.rs +++ b/src/common/detector/WhiteRectangleDetector.rs @@ -19,6 +19,8 @@ use crate::{NotFoundException,RXingResultPoint}; use crate::common::BitMatrix; +use super::MathUtils; + /** *

* Detects a candidate barcode-like rectangular region within an image. It @@ -45,7 +47,7 @@ pub struct WhiteRectangleDetector { impl WhiteRectangleDetector { pub fn new_from_image(image:&BitMatrix) -> Result { - Self::new(image, INIT_SIZE, image.getWidth() / 2, image.getHeight() / 2); + Self::new(image, INIT_SIZE, image.getWidth() / 2, image.getHeight() / 2) } /** @@ -65,7 +67,7 @@ impl WhiteRectangleDetector { new_wrd.rightInit = x + halfsize; new_wrd.upInit = y - halfsize; new_wrd.downInit = y + halfsize; - if (upInit < 0 || leftInit < 0 || downInit >= height || rightInit >= width) { + if (new_wrd.upInit < 0 || new_wrd.leftInit < 0 || new_wrd.downInit >= new_wrd.height || new_wrd.rightInit >= new_wrd.width) { return Err( NotFoundException.getNotFoundInstance()); } @@ -86,12 +88,12 @@ impl WhiteRectangleDetector { * leftmost and the third, the rightmost * @throws NotFoundException if no Data Matrix Code can be found */ - pub fn detect() -> Result, NotFoundException> { + pub fn detect(&self) -> Result, NotFoundException> { - let left :i32= leftInit; - let right:i32 = rightInit; - let up:i32 = upInit; - let down:i32 = downInit; + let left :i32= self.leftInit; + let right:i32 = self.rightInit; + let up:i32 = self.upInit; + let down:i32 = self.downInit; let sizeExceeded = false; let aBlackPointFoundOnBorder = true; @@ -108,8 +110,8 @@ impl WhiteRectangleDetector { // . | // ..... let rightBorderNotWhite = true; - while ((rightBorderNotWhite || !atLeastOneBlackPointFoundOnRight) && right < width) { - rightBorderNotWhite = containsBlackPoint(up, down, right, false); + while ((rightBorderNotWhite || !atLeastOneBlackPointFoundOnRight) && right < self.width) { + rightBorderNotWhite = self.containsBlackPoint(up, down, right, false); if (rightBorderNotWhite) { right += 1; aBlackPointFoundOnBorder = true; @@ -119,7 +121,7 @@ impl WhiteRectangleDetector { } } - if (right >= width) { + if (right >= self.width) { sizeExceeded = true; break; } @@ -128,8 +130,8 @@ impl WhiteRectangleDetector { // . . // .___. let bottomBorderNotWhite = true; - while ((bottomBorderNotWhite || !atLeastOneBlackPointFoundOnBottom) && down < height) { - bottomBorderNotWhite = containsBlackPoint(left, right, down, true); + while ((bottomBorderNotWhite || !atLeastOneBlackPointFoundOnBottom) && down < self.height) { + bottomBorderNotWhite = self.containsBlackPoint(left, right, down, true); if (bottomBorderNotWhite) { down+=1; aBlackPointFoundOnBorder = true; @@ -139,7 +141,7 @@ impl WhiteRectangleDetector { } } - if (down >= height) { + if (down >= self.height) { sizeExceeded = true; break; } @@ -149,7 +151,7 @@ impl WhiteRectangleDetector { // ..... let leftBorderNotWhite = true; while ((leftBorderNotWhite || !atLeastOneBlackPointFoundOnLeft) && left >= 0) { - leftBorderNotWhite = containsBlackPoint(up, down, left, false); + leftBorderNotWhite = self.containsBlackPoint(up, down, left, false); if (leftBorderNotWhite) { left-=1; aBlackPointFoundOnBorder = true; @@ -169,7 +171,7 @@ impl WhiteRectangleDetector { // ..... let topBorderNotWhite = true; while ((topBorderNotWhite || !atLeastOneBlackPointFoundOnTop) && up >= 0) { - topBorderNotWhite = containsBlackPoint(left, right, up, true); + topBorderNotWhite = self.containsBlackPoint(left, right, up, true); if (topBorderNotWhite) { up-=1; aBlackPointFoundOnBorder = true; @@ -194,7 +196,7 @@ impl WhiteRectangleDetector { let mut i = 1; while z.is_none() && i < maxSize { //for (int i = 1; z == null && i < maxSize; i++) { - z = getBlackPointOnSegment(left, down - i, left + i, down); + z = self.getBlackPointOnSegment(left, down - i, left + i, down); i+=1; } @@ -207,7 +209,7 @@ impl WhiteRectangleDetector { let mut i = 1; while t.is_none() && i < maxSize { //for (int i = 1; t == null && i < maxSize; i++) { - t = getBlackPointOnSegment(left, up + i, left + i, up); + t = self.getBlackPointOnSegment(left, up + i, left + i, up); i+=1; } @@ -220,7 +222,7 @@ impl WhiteRectangleDetector { let mut i = 1; while x.is_none() && i < maxSize { //for (int i = 1; x == null && i < maxSize; i++) { - x = getBlackPointOnSegment(right, up + i, right - i, up); + x = self.getBlackPointOnSegment(right, up + i, right - i, up); i += 1; } @@ -233,7 +235,7 @@ impl WhiteRectangleDetector { let mut i = 1; while y.is_none() && i < maxSize { //for (int i = 1; y == null && i < maxSize; i++) { - y = getBlackPointOnSegment(right, down - i, right - i, down); + y = self.getBlackPointOnSegment(right, down - i, right - i, down); i+=1; } @@ -241,22 +243,22 @@ impl WhiteRectangleDetector { return Err( NotFoundException.getNotFoundInstance()); } - return centerEdges(y, z, x, t); + return self.centerEdges(y, z, x, t); } else { return Err( NotFoundException.getNotFoundInstance()); } } - fn getBlackPointOnSegment( aX:f32, aY:f32, bX:f32, bY:f32) -> Option { - let dist = MathUtils.round(MathUtils.distance(aX, aY, bX, bY)); + fn getBlackPointOnSegment(&self, aX:f32, aY:f32, bX:f32, bY:f32) -> Option { + let dist = MathUtils::round(MathUtils::distance_float(aX, aY, bX, bY)); let xStep :f32= (bX - aX) / dist; let yStep:f32 = (bY - aY) / dist; for i in 0..dist { - let x = MathUtils.round(aX + i * xStep); - let y = MathUtils.round(aY + i * yStep); - if (image.get(x, y)) { + let x = MathUtils::round(aX + i * xStep); + let y = MathUtils::round(aY + i * yStep); + if (self.image.get(x, y)) { return RXingResultPoint::new(x, y); } } @@ -276,7 +278,7 @@ impl WhiteRectangleDetector { * point and the last, the bottommost. The second point will be * leftmost and the third, the rightmost */ - fn centerEdges( y:&RXingResultPoint, z:&RXingResultPoint, + fn centerEdges( &self,y:&RXingResultPoint, z:&RXingResultPoint, x:&RXingResultPoint, t:&RXingResultPoint) -> Vec { // @@ -295,14 +297,14 @@ impl WhiteRectangleDetector { let ti = t.getX(); let tj = t.getY(); - if (yi < width / 2.0f) { - return Vec! + if (yi < self.width.into() / 2.0f32) { + return vec! [ RXingResultPoint::new(ti - CORR, tj + CORR), RXingResultPoint::new(zi + CORR, zj + CORR), RXingResultPoint::new(xi - CORR, xj - CORR), RXingResultPoint::new(yi + CORR, yj - CORR)]; } else { - return Vec![ + return vec![ RXingResultPoint::new(ti + CORR, tj + CORR), RXingResultPoint::new(zi + CORR, zj - CORR), RXingResultPoint::new(xi - CORR, xj + CORR), @@ -319,19 +321,19 @@ impl WhiteRectangleDetector { * @param horizontal set to true if scan must be horizontal, false if vertical * @return true if a black point has been found, else false. */ - fn containsBlackPoint( a:i32, b:i32, fixed:i32, horizontal:bool) -> bool { + fn containsBlackPoint(&self, a:i32, b:i32, fixed:i32, horizontal:bool) -> bool { if (horizontal) { for x in a..=b { - if (image.get(x, fixed)) { + if (self.image.get(x, fixed)) { return true; } } } else { for y in a..=b { - if (image.get(fixed, y)) { + if (self.image.get(fixed, y)) { return true; } } diff --git a/src/common/detector/mod.rs b/src/common/detector/mod.rs new file mode 100644 index 0000000..6f1e495 --- /dev/null +++ b/src/common/detector/mod.rs @@ -0,0 +1,7 @@ +mod MonochromeRectangleDetector; +mod WhiteRectangleDetector; + +pub mod MathUtils; + +pub use MonochromeRectangleDetector::*; +pub use WhiteRectangleDetector::*; \ No newline at end of file diff --git a/src/common/mod.rs b/src/common/mod.rs new file mode 100644 index 0000000..de21653 --- /dev/null +++ b/src/common/mod.rs @@ -0,0 +1,2 @@ +pub mod detector; +pub mod reedsolomon; \ No newline at end of file diff --git a/src/common/reedsolomon/mod.rs b/src/common/reedsolomon/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/lib.rs b/src/lib.rs index e69de29..cee91dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -0,0 +1 @@ +mod common; \ No newline at end of file