many small rustifications

This commit is contained in:
Henry Schimke
2023-01-10 14:33:47 -06:00
parent 752d8c87b9
commit 9f7a41f81c
23 changed files with 218 additions and 187 deletions

View File

@@ -30,6 +30,7 @@ use std::{f32, i32};
* @param d real value to round
* @return nearest {@code int}
*/
#[inline(always)]
pub fn round(d: f32) -> i32 {
(d + (if d < 0.0f32 { -0.5f32 } else { 0.5f32 })) as i32
}
@@ -41,6 +42,7 @@ pub fn round(d: f32) -> i32 {
* @param bY point B y coordinate
* @return Euclidean distance between points A and B
*/
#[inline(always)]
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();
@@ -54,6 +56,7 @@ pub fn distance_float(aX: f32, aY: f32, bX: f32, bY: f32) -> f32 {
* @param bY point B y coordinate
* @return Euclidean distance between points A and B
*/
#[inline(always)]
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();
@@ -64,12 +67,14 @@ pub fn distance_int(aX: i32, aY: i32, bX: i32, bY: i32) -> f32 {
* @param array values to sum
* @return sum of values in array
*/
#[inline(always)]
pub fn sum(array: &[i32]) -> i32 {
let mut count = 0;
for a in array {
count += a;
}
count
array.iter().sum()
// let mut count = 0;
// for a in array {
// count += a;
// }
// count
}
#[cfg(test)]

View File

@@ -29,15 +29,13 @@ use crate::{common::BitMatrix, Exceptions, RXingResultPoint, ResultPoint};
*/
const MAX_MODULES: i32 = 32;
#[deprecated]
pub struct MonochromeRectangleDetector {
image: BitMatrix,
pub struct MonochromeRectangleDetector<'a> {
image: &'a BitMatrix,
}
impl MonochromeRectangleDetector {
pub fn new(image: &BitMatrix) -> Self {
Self {
image: image.clone(),
}
impl<'a> MonochromeRectangleDetector<'_> {
pub fn new(image: &'a BitMatrix) -> MonochromeRectangleDetector<'a> {
MonochromeRectangleDetector { image: image }
}
/**
@@ -50,7 +48,7 @@ impl MonochromeRectangleDetector {
* third, the rightmost
* @throws NotFoundException if no Data Matrix Code can be found
*/
pub fn detect(&self) -> Result<Vec<RXingResultPoint>, Exceptions> {
pub fn detect(&self) -> Result<[RXingResultPoint; 4], Exceptions> {
let height = self.image.getHeight() as i32;
let width = self.image.getWidth() as i32;
let halfHeight = height / 2;
@@ -124,7 +122,7 @@ impl MonochromeRectangleDetector {
halfWidth / 4,
)?;
Ok(vec![pointA, pointB, pointC, pointD])
Ok([pointA, pointB, pointC, pointD])
}
/**
@@ -157,11 +155,11 @@ impl MonochromeRectangleDetector {
bottom: i32,
maxWhiteRun: i32,
) -> Result<RXingResultPoint, Exceptions> {
let mut lastRange_z: Option<Vec<i32>> = None;
let mut lastRange_z: Option<[i32; 2]> = None;
let mut y: i32 = centerY;
let mut x: i32 = centerX;
while y < bottom && y >= top && x < right && x >= left {
let range: Option<Vec<i32>> = if deltaX == 0 {
let range: Option<[i32; 2]> = if deltaX == 0 {
// horizontal slices, up and down
self.blackWhiteRange(y, maxWhiteRun, left, right, true)
} else {
@@ -231,7 +229,7 @@ impl MonochromeRectangleDetector {
minDim: i32,
maxDim: i32,
horizontal: bool,
) -> Option<Vec<i32>> {
) -> Option<[i32; 2]> {
let center = (minDim + maxDim) / 2;
// Scan left/up first
@@ -295,7 +293,7 @@ impl MonochromeRectangleDetector {
end -= 1;
if end > start {
Some(vec![start, end])
Some([start, end])
} else {
None
}

View File

@@ -32,8 +32,8 @@ use super::MathUtils;
*/
const INIT_SIZE: i32 = 10;
const CORR: i32 = 1;
pub struct WhiteRectangleDetector {
image: BitMatrix,
pub struct WhiteRectangleDetector<'a> {
image: &'a BitMatrix,
height: i32,
width: i32,
leftInit: i32,
@@ -42,9 +42,9 @@ pub struct WhiteRectangleDetector {
upInit: i32,
}
impl WhiteRectangleDetector {
pub fn new_from_image(image: &BitMatrix) -> Result<Self, Exceptions> {
Self::new(
impl<'a> WhiteRectangleDetector<'_> {
pub fn new_from_image(image: &'a BitMatrix) -> Result<WhiteRectangleDetector<'a>, Exceptions> {
WhiteRectangleDetector::new(
image,
INIT_SIZE,
image.getWidth() as i32 / 2,
@@ -59,7 +59,12 @@ impl WhiteRectangleDetector {
* @param y y position of search center
* @throws NotFoundException if image is too small to accommodate {@code initSize}
*/
pub fn new(image: &BitMatrix, initSize: i32, x: i32, y: i32) -> Result<Self, Exceptions> {
pub fn new(
image: &'a BitMatrix,
initSize: i32,
x: i32,
y: i32,
) -> Result<WhiteRectangleDetector<'a>, Exceptions> {
let halfsize = initSize / 2;
let leftInit = x - halfsize;
@@ -75,8 +80,8 @@ impl WhiteRectangleDetector {
return Err(Exceptions::NotFoundException(None));
}
Ok(Self {
image: image.clone(),
Ok(WhiteRectangleDetector {
image: image,
height: image.getHeight() as i32,
width: image.getWidth() as i32,
leftInit,
@@ -100,7 +105,7 @@ impl WhiteRectangleDetector {
* leftmost and the third, the rightmost
* @throws NotFoundException if no Data Matrix Code can be found
*/
pub fn detect(&self) -> Result<Vec<RXingResultPoint>, Exceptions> {
pub fn detect(&self) -> Result<[RXingResultPoint; 4], Exceptions> {
let mut left: i32 = self.leftInit;
let mut right: i32 = self.rightInit;
let mut up: i32 = self.upInit;
@@ -321,7 +326,7 @@ impl WhiteRectangleDetector {
z: &RXingResultPoint,
x: &RXingResultPoint,
t: &RXingResultPoint,
) -> Vec<RXingResultPoint> {
) -> [RXingResultPoint; 4] {
//
// t t
// z x
@@ -339,14 +344,14 @@ impl WhiteRectangleDetector {
let tj = t.getY();
if yi < self.width as f32 / 2.0f32 {
vec![
[
RXingResultPoint::new(ti - CORR as f32, tj + CORR as f32),
RXingResultPoint::new(zi + CORR as f32, zj + CORR as f32),
RXingResultPoint::new(xi - CORR as f32, xj - CORR as f32),
RXingResultPoint::new(yi + CORR as f32, yj - CORR as f32),
]
} else {
vec![
[
RXingResultPoint::new(ti + CORR as f32, tj + CORR as f32),
RXingResultPoint::new(zi + CORR as f32, zj - CORR as f32),
RXingResultPoint::new(xi - CORR as f32, xj + CORR as f32),