accidentally deleted, repair

This commit is contained in:
Henry Schimke
2022-08-20 17:04:16 -05:00
parent a6f0b0fd42
commit d56e0a2674
7 changed files with 137 additions and 74 deletions

View File

@@ -15,6 +15,7 @@
*/ */
//package com.google.zxing.common.detector; //package com.google.zxing.common.detector;
use std::{i32,f32};
/** /**
* General math-related and numeric utility functions. * General math-related and numeric utility functions.
@@ -30,7 +31,7 @@
* @return nearest {@code int} * @return nearest {@code int}
*/ */
pub fn round(d:f32) -> i32 { 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 * @param bY point B y coordinate
* @return Euclidean distance between points A and B * @return Euclidean distance between points A and B
*/ */
pub fn distance( aX:f32, aY:f32, bX: f32, bY:f32) -> f32 { pub fn distance_float( aX:f32, aY:f32, bX: f32, bY:f32) -> f32 {
let xDiff:f64 = aX - bX; let xDiff:f64 = (aX - bX).into();
let yDiff:f64 = aY - bY; let yDiff:f64 = (aY - bY).into();
return Math.sqrt(xDiff * xDiff + yDiff * yDiff); return (xDiff * xDiff + yDiff * yDiff).sqrt() as f32;
} }
/** /**
@@ -53,10 +54,10 @@
* @param bY point B y coordinate * @param bY point B y coordinate
* @return Euclidean distance between points A and B * @return Euclidean distance between points A and B
*/ */
pub fn distance( aX : i32, aY: i32, bX: i32, bY: i32) -> f32 { pub fn distance_int( aX : i32, aY: i32, bX: i32, bY: i32) -> f32 {
let xDiff : f64 = aX - bX; let xDiff : f64 = (aX - bX).into();
let yDiff :f64 = aY - bY; let yDiff :f64 = (aY - bY).into();
return Math.sqrt(xDiff * xDiff + yDiff * yDiff); return (xDiff * xDiff + yDiff * yDiff).sqrt() as f32;
} }
/** /**
@@ -69,4 +70,53 @@
count += a; count += a;
} }
return count; 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]));
}
} }

View File

@@ -26,9 +26,9 @@ use crate::{NotFoundException, RXingResultPoint};
* @author Sean Owen * @author Sean Owen
* @deprecated without replacement since 3.3.0 * @deprecated without replacement since 3.3.0
*/ */
const MAX_MODULES : i32 = 32;
#[deprecated] #[deprecated]
pub struct MonochromeRectangleDetector { pub struct MonochromeRectangleDetector {
MAX_MODULES: i32,
image: BitMatrix, image: BitMatrix,
} }
@@ -36,7 +36,6 @@ pub struct MonochromeRectangleDetector {
impl MonochromeRectangleDetector { impl MonochromeRectangleDetector {
pub fn new(image: &BitMatrix) -> Self { pub fn new(image: &BitMatrix) -> Self {
Self { Self {
MAX_MODULES: 32,
image: image, image: image,
} }
} }
@@ -51,19 +50,19 @@ impl MonochromeRectangleDetector {
* third, the rightmost * third, the rightmost
* @throws NotFoundException if no Data Matrix Code can be found * @throws NotFoundException if no Data Matrix Code can be found
*/ */
pub fn detect() -> Result<Vec<RXingResultPoint>, NotFoundException> { pub fn detect(&self) -> Result<Vec<RXingResultPoint>, NotFoundException> {
let height = image.getHeight(); let height = self.image.getHeight();
let width = image.getWidth(); let width = self.image.getWidth();
let halfHeight = height / 2; let halfHeight = height / 2;
let halfWidth = width / 2; let halfWidth = width / 2;
let deltaY = Math.max(1, height / (MAX_MODULES * 8)); let deltaY = 1.max( height / (MAX_MODULES * 8));
let deltaX = Math.max(1, width / (MAX_MODULES * 8)); let deltaX = 1.max( width / (MAX_MODULES * 8));
let top = 0; let top = 0;
let bottom = height; let bottom = height;
let left = 0; let left = 0;
let right = width; let right = width;
let pointA = findCornerFromCenter( let pointA = self.findCornerFromCenter(
halfWidth, halfWidth,
0, 0,
left, left,
@@ -73,9 +72,9 @@ impl MonochromeRectangleDetector {
top, top,
bottom, bottom,
halfWidth / 2, halfWidth / 2,
); )?;
top = pointA.getY() - 1; top = pointA.getY() - 1;
let pointB = findCornerFromCenter( let pointB = self.findCornerFromCenter(
halfWidth, halfWidth,
-deltaX, -deltaX,
left, left,
@@ -85,9 +84,9 @@ impl MonochromeRectangleDetector {
top, top,
bottom, bottom,
halfHeight / 2, halfHeight / 2,
); )?;
left = pointB.getX() - 1; left = pointB.getX() - 1;
let pointC = findCornerFromCenter( let pointC = self.findCornerFromCenter(
halfWidth, halfWidth,
deltaX, deltaX,
left, left,
@@ -97,9 +96,9 @@ impl MonochromeRectangleDetector {
top, top,
bottom, bottom,
halfHeight / 2, halfHeight / 2,
); )?;
right = pointC.getX() + 1; right = pointC.getX() + 1;
let pointD = findCornerFromCenter( let pointD = self.findCornerFromCenter(
halfWidth, halfWidth,
0, 0,
left, left,
@@ -109,11 +108,11 @@ impl MonochromeRectangleDetector {
top, top,
bottom, bottom,
halfWidth / 2, halfWidth / 2,
); )?;
bottom = pointD.getY() + 1; bottom = pointD.getY() + 1;
// Go try to find point A again with better information -- might have been off at first. // Go try to find point A again with better information -- might have been off at first.
pointA = findCornerFromCenter( pointA = self.findCornerFromCenter(
halfWidth, halfWidth,
0, 0,
left, left,
@@ -123,9 +122,9 @@ impl MonochromeRectangleDetector {
top, top,
bottom, bottom,
halfWidth / 4, 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 * @throws NotFoundException if such a point cannot be found
*/ */
fn findCornerFromCenter( fn findCornerFromCenter(
&self,
centerX: i32, centerX: i32,
deltaX: i32, deltaX: i32,
left: i32, left: i32,
@@ -157,20 +157,20 @@ impl MonochromeRectangleDetector {
bottom: i32, bottom: i32,
maxWhiteRun: i32, maxWhiteRun: i32,
) -> Result<RXingResultPoint, NotFoundException> { ) -> Result<RXingResultPoint, NotFoundException> {
lastRange = null; let lastRange : Option<Vec<i32>> = None;
let y: i32 = centerY; let y: i32 = centerY;
let x: i32 = centerX; let x: i32 = centerX;
while (y < bottom && y >= top && x < right && x >= left) { while (y < bottom && y >= top && x < right && x >= left) {
let range: Vec::new(); let range: Option<Vec<i32>>;
if (deltaX == 0) { if (deltaX == 0) {
// horizontal slices, up and down // horizontal slices, up and down
range = blackWhiteRange(y, maxWhiteRun, left, right, true); range = self.blackWhiteRange(y, maxWhiteRun, left, right, true);
} else { } else {
// vertical slices, left and right // vertical slices, left and right
range = blackWhiteRange(x, maxWhiteRun, top, bottom, false); range = self.blackWhiteRange(x, maxWhiteRun, top, bottom, false);
} }
if (range == null) { if (range .is_none()) {
if (lastRange == null) { if (lastRange .is_none()) {
return Err(NotFoundException.getNotFoundInstance()); return Err(NotFoundException.getNotFoundInstance());
} }
// lastRange was found // lastRange was found
@@ -225,6 +225,7 @@ impl MonochromeRectangleDetector {
* (e.g. only white was found) * (e.g. only white was found)
*/ */
fn blackWhiteRange( fn blackWhiteRange(
&self,
fixedDimension: i32, fixedDimension: i32,
maxWhiteRun: i32, maxWhiteRun: i32,
minDim: i32, minDim: i32,
@@ -237,9 +238,9 @@ impl MonochromeRectangleDetector {
let start = center; let start = center;
while (start >= minDim) { while (start >= minDim) {
if (if horizontal { if (if horizontal {
image.get(start, fixedDimension) self.image.get(start, fixedDimension)
} else { } else {
image.get(fixedDimension, start) self.image.get(fixedDimension, start)
}) { }) {
start = start - 1; start = start - 1;
} else { } else {
@@ -247,9 +248,9 @@ impl MonochromeRectangleDetector {
start = start - 1; start = start - 1;
while start >= minDim while start >= minDim
&& !(if horizontal { && !(if horizontal {
image.get(start, fixedDimension); self.image.get(start, fixedDimension)
} else { } else {
image.get(fixedDimension, start); self.image.get(fixedDimension, start)
}) })
{ {
start = start - 1; start = start - 1;
@@ -267,9 +268,9 @@ impl MonochromeRectangleDetector {
let end = center; let end = center;
while (end < maxDim) { while (end < maxDim) {
if (if horizontal { if (if horizontal {
image.get(end, fixedDimension) self.image.get(end, fixedDimension)
} else { } else {
image.get(fixedDimension, end) self.image.get(fixedDimension, end)
}) { }) {
end = end + 1; end = end + 1;
} else { } else {
@@ -277,9 +278,9 @@ impl MonochromeRectangleDetector {
end = end + 1; end = end + 1;
while end < maxDim while end < maxDim
&& !(if horizontal { && !(if horizontal {
image.get(end, fixedDimension) self.image.get(end, fixedDimension)
} else { } else {
image.get(fixedDimension, end) self.image.get(fixedDimension, end)
}) })
{ {
end = end + 1; end = end + 1;
@@ -294,7 +295,7 @@ impl MonochromeRectangleDetector {
end = end - 1; end = end - 1;
return if end > start { return if end > start {
Some(Vec! {start, end}) Some(vec! {start, end})
} else { } else {
None None
}; };

View File

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

View File

@@ -0,0 +1,7 @@
mod MonochromeRectangleDetector;
mod WhiteRectangleDetector;
pub mod MathUtils;
pub use MonochromeRectangleDetector::*;
pub use WhiteRectangleDetector::*;

2
src/common/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod detector;
pub mod reedsolomon;

View File

View File

@@ -0,0 +1 @@
mod common;