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

@@ -51,6 +51,7 @@ impl BitArray {
}
// For testing only
#[cfg(test)]
pub fn with_initial_values(bits: Vec<u32>, size: usize) -> Self {
Self { bits, size }
}
@@ -253,7 +254,7 @@ impl BitArray {
pub fn appendBits(&mut self, value: u32, num_bits: usize) -> Result<(), Exceptions> {
if num_bits > 32 {
return Err(Exceptions::IllegalArgumentException(Some(
"Num bits must be between 0 and 32".to_owned(),
"num bits must be between 0 and 32".to_owned(),
)));
}
@@ -310,15 +311,15 @@ impl BitArray {
let mut bitOffset = bitOffset;
for i in 0..numBytes {
//for (int i = 0; i < numBytes; i++) {
let mut theByte = 0;
let mut the_byte = 0;
for j in 0..8 {
//for (int j = 0; j < 8; j++) {
if self.get(bitOffset) {
theByte |= 1 << (7 - j);
the_byte |= 1 << (7 - j);
}
bitOffset += 1;
}
array[offset + i] = theByte;
array[offset + i] = the_byte;
}
}

View File

@@ -203,12 +203,18 @@ impl BitMatrix {
* @return value of given bit in matrix
*/
pub fn get(&self, x: u32, y: u32) -> bool {
let offset = y as usize * self.row_size + (x as usize / 32);
let offset = self.get_offset(y, x);
((self.bits[offset] >> (x & 0x1f)) & 1) != 0
}
pub fn try_get(&self, x: u32, y: u32) -> Result<bool, Exceptions> {
#[inline(always)]
fn get_offset(&self, y: u32, x: u32) -> usize {
let offset = y as usize * self.row_size + (x as usize / 32);
offset
}
pub fn try_get(&self, x: u32, y: u32) -> Result<bool, Exceptions> {
let offset = self.get_offset(y, x);
if offset > self.bits.len() {
return Err(Exceptions::IndexOutOfBoundsException(None));
}
@@ -217,7 +223,7 @@ impl BitMatrix {
/// Confusingly returns true if the requested element is out of bounds
pub fn check_in_bounds(&self, x: u32, y: u32) -> bool {
(y as usize * self.row_size + (x as usize / 32)) > self.bits.len()
(self.get_offset(y, x)) > self.bits.len()
}
/**
@@ -227,12 +233,12 @@ impl BitMatrix {
* @param y The vertical component (i.e. which row)
*/
pub fn set(&mut self, x: u32, y: u32) {
let offset = y as usize * self.row_size + (x as usize / 32);
let offset = self.get_offset(y, x);
self.bits[offset] |= 1 << (x & 0x1f);
}
pub fn unset(&mut self, x: u32, y: u32) {
let offset = y as usize * self.row_size + (x as usize / 32);
let offset = self.get_offset(y, x);
self.bits[offset] &= !(1 << (x & 0x1f));
}
@@ -243,7 +249,7 @@ impl BitMatrix {
* @param y The vertical component (i.e. which row)
*/
pub fn flip_coords(&mut self, x: u32, y: u32) {
let offset = y as usize * self.row_size + (x as usize / 32);
let offset = self.get_offset(y, x);
self.bits[offset] ^= 1 << (x & 0x1f);
}
@@ -252,9 +258,8 @@ impl BitMatrix {
*/
pub fn flip_self(&mut self) {
let max = self.bits.len();
for i in 0..max {
//for (int i = 0; i < max; i++) {
self.bits[i] = !self.bits[i];
for bit_set in self.bits.iter_mut().take(max) {
*bit_set = !*bit_set;
}
}
@@ -320,14 +325,14 @@ impl BitMatrix {
// }
if height < 1 || width < 1 {
return Err(Exceptions::IllegalArgumentException(Some(
"Height and width must be at least 1".to_owned(),
"height and width must be at least 1".to_owned(),
)));
}
let right = left + width;
let bottom = top + height;
if bottom > self.height || right > self.width {
return Err(Exceptions::IllegalArgumentException(Some(
"The region must fit inside the matrix".to_owned(),
"the region must fit inside the matrix".to_owned(),
)));
}
for y in top..bottom {
@@ -374,8 +379,8 @@ impl BitMatrix {
* @param row {@link BitArray} to copy from
*/
pub fn setRow(&mut self, y: u32, row: &BitArray) {
return self.bits[y as usize * self.row_size..y as usize * self.row_size + self.row_size]
.clone_from_slice(&row.getBitArray()[0..self.row_size]);
self.bits[y as usize * self.row_size..y as usize * self.row_size + self.row_size]
.clone_from_slice(&row.getBitArray()[0..self.row_size])
//System.arraycopy(row.getBitArray(), 0, self.bits, y * self.rowSize, self.rowSize);
}
@@ -438,7 +443,7 @@ impl BitMatrix {
//for (int y = 0; y < height; y++) {
for x in 0..self.width {
//for (int x = 0; x < width; x++) {
let offset = y as usize * self.row_size + (x as usize / 32);
let offset = self.get_offset(y, x);
if ((self.bits[offset] >> (x & 0x1f)) & 1) != 0 {
let newOffset: usize = ((newHeight - 1 - x) * newRowSize + (y / 32)) as usize;
newBits[newOffset] |= 1 << (y & 0x1f);
@@ -456,7 +461,7 @@ impl BitMatrix {
*
* @return {@code left,top,width,height} enclosing rectangle of all 1 bits, or null if it is all white
*/
pub fn getEnclosingRectangle(&self) -> Option<Vec<u32>> {
pub fn getEnclosingRectangle(&self) -> Option<[u32; 4]> {
let mut left = self.width;
let mut top = self.height;
// let right = -1;
@@ -476,22 +481,22 @@ impl BitMatrix {
if y > bottom {
bottom = y;
}
if x32 * 32 < left.try_into().unwrap() {
if x32 * 32 < left as usize {
let mut bit = 0;
while (theBits << (31 - bit)) == 0 {
bit += 1;
}
if (x32 * 32 + bit) < left.try_into().unwrap() {
left = (x32 * 32 + bit).try_into().unwrap();
if (x32 * 32 + bit) < left as usize {
left = (x32 * 32 + bit) as u32;
}
}
if x32 * 32 + 31 > right.try_into().unwrap() {
if x32 * 32 + 31 > right as usize {
let mut bit = 31;
while (theBits >> bit) == 0 {
bit -= 1;
}
if (x32 * 32 + bit) > right.try_into().unwrap() {
right = (x32 * 32 + bit).try_into().unwrap();
if (x32 * 32 + bit) > right as usize {
right = (x32 * 32 + bit) as u32;
}
}
}
@@ -502,7 +507,7 @@ impl BitMatrix {
return None;
}
Some(vec![left, top, right - left + 1, bottom - top + 1])
Some([left, top, right - left + 1, bottom - top + 1])
}
/**
@@ -530,7 +535,7 @@ impl BitMatrix {
Some(vec![x as u32, y as u32])
}
pub fn getBottomRightOnBit(&self) -> Option<Vec<u32>> {
pub fn getBottomRightOnBit(&self) -> Option<[u32; 2]> {
let mut bitsOffset = self.bits.len() as i64 - 1;
while bitsOffset >= 0 && self.bits[bitsOffset as usize] == 0 {
bitsOffset -= 1;
@@ -549,7 +554,7 @@ impl BitMatrix {
}
x += bit;
Some(vec![x as u32, y as u32])
Some([x as u32, y as u32])
}
/**

View File

@@ -25,7 +25,8 @@ use super::{BitMatrix, GridSampler, PerspectiveTransform};
/**
* @author Sean Owen
*/
pub struct DefaultGridSampler {}
#[derive(Default)]
pub struct DefaultGridSampler;
impl GridSampler for DefaultGridSampler {
fn sample_grid_detailed(

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),

View File

@@ -112,9 +112,7 @@ impl ECIInput for MinimalECIInput {
*/
fn subSequence(&self, start: usize, end: usize) -> Result<Vec<char>, Exceptions> {
if start > end || end > self.length() {
return Err(Exceptions::IndexOutOfBoundsException(Some(
start.to_string(),
)));
return Err(Exceptions::IndexOutOfBoundsException(None));
}
let mut result = String::new();
for i in start..end {
@@ -143,9 +141,7 @@ impl ECIInput for MinimalECIInput {
*/
fn isECI(&self, index: u32) -> Result<bool, Exceptions> {
if index >= self.length() as u32 {
return Err(Exceptions::IndexOutOfBoundsException(Some(
index.to_string(),
)));
return Err(Exceptions::IndexOutOfBoundsException(None));
}
Ok(self.bytes[index as usize] > 255) // && self.bytes[index as usize] <= u16::MAX)
}
@@ -170,9 +166,7 @@ impl ECIInput for MinimalECIInput {
*/
fn getECIValue(&self, index: usize) -> Result<i32, Exceptions> {
if index >= self.length() {
return Err(Exceptions::IndexOutOfBoundsException(Some(
index.to_string(),
)));
return Err(Exceptions::IndexOutOfBoundsException(None));
}
if !self.isECI(index as u32)? {
return Err(Exceptions::IllegalArgumentException(Some(format!(
@@ -261,9 +255,7 @@ impl MinimalECIInput {
*/
pub fn isFNC1(&self, index: usize) -> Result<bool, Exceptions> {
if index >= self.length() {
return Err(Exceptions::IndexOutOfBoundsException(Some(
index.to_string(),
)));
return Err(Exceptions::IndexOutOfBoundsException(None));
}
Ok(self.bytes[index] == 1000)
}
@@ -271,7 +263,8 @@ impl MinimalECIInput {
fn addEdge(edges: &mut [Vec<Option<Rc<InputEdge>>>], to: usize, edge: Rc<InputEdge>) {
if edges[to][edge.encoderIndex].is_none()
|| edges[to][edge.encoderIndex]
.clone()
// .clone()
.as_ref()
.unwrap()
.cachedTotalSize
> edge.cachedTotalSize
@@ -359,7 +352,7 @@ impl MinimalECIInput {
}
}
if minimalJ < 0 {
panic!("Internal error: failed to encode \"{}\"", stringToEncode);
panic!("internal error: failed to encode \"{}\"", stringToEncode);
}
let mut intsAL: Vec<u16> = Vec::new();
let mut current = edges[inputLength][minimalJ as usize].clone();
@@ -395,13 +388,13 @@ impl MinimalECIInput {
}
current = c.previous.clone();
}
let mut ints = vec![0; intsAL.len()];
//let mut ints = vec![0; intsAL.len()];
// for i in 0..ints.len() {
// // for (int i = 0; i < ints.length; i++) {
// ints[i] = *intsAL.get(i).unwrap();
// }
ints[..].copy_from_slice(&intsAL[..]);
ints
//ints[..].copy_from_slice(&intsAL[..]);
intsAL
}
}
@@ -412,6 +405,8 @@ struct InputEdge {
cachedTotalSize: usize,
}
impl InputEdge {
const FNC1_UNICODE: &str = "\u{1000}";
pub fn new(
c: &str,
encoderSet: &ECIEncoderSet,
@@ -419,7 +414,7 @@ impl InputEdge {
previous: Option<Rc<InputEdge>>,
fnc1: Option<&str>,
) -> Self {
let mut size = if c == "\u{1000}" {
let mut size = if c == Self::FNC1_UNICODE {
1
} else {
encoderSet.encode_char(c, encoderIndex).len()
@@ -436,7 +431,7 @@ impl InputEdge {
Self {
c: if fnc1.is_some() && &c == fnc1.as_ref().unwrap() {
String::from("\u{1000}")
String::from(Self::FNC1_UNICODE)
} else {
String::from(c)
},
@@ -452,7 +447,7 @@ impl InputEdge {
Self {
c: if fnc1.is_some() && &c == fnc1.as_ref().unwrap() {
String::from("\u{1000}")
String::from(Self::FNC1_UNICODE)
} else {
String::from(c)
},
@@ -489,7 +484,7 @@ impl InputEdge {
}
pub fn isFNC1(&self) -> bool {
self.c == "\u{1000}"
self.c == Self::FNC1_UNICODE
}
}

View File

@@ -109,13 +109,12 @@ impl PerspectiveTransform {
pub fn transform_points_double(&self, x_values: &mut [f32], y_valuess: &mut [f32]) {
let n = x_values.len();
for i in 0..n {
// for i in 0..n {
for (x, y) in x_values.iter_mut().zip(y_valuess.iter_mut()).take(n) {
// for (int i = 0; i < n; i++) {
let x = x_values[i];
let y = y_valuess[i];
let denominator = self.a13 * x + self.a23 * y + self.a33;
x_values[i] = (self.a11 * x + self.a21 * y + self.a31) / denominator;
y_valuess[i] = (self.a12 * x + self.a22 * y + self.a32) / denominator;
let denominator = self.a13 * *x + self.a23 * *y + self.a33;
*x = (self.a11 * *x + self.a21 * *y + self.a31) / denominator;
*y = (self.a12 * *x + self.a22 * *y + self.a32) / denominator;
}
}