cargo fmt

This commit is contained in:
Henry Schimke
2023-04-23 14:36:51 -05:00
parent cb7050a5dd
commit a811b56fbb
12 changed files with 78 additions and 63 deletions

View File

@@ -83,11 +83,11 @@ impl BitArray {
(self.bits[i / 32] & (1 << (i & 0x1F))) != 0 (self.bits[i / 32] & (1 << (i & 0x1F))) != 0
} }
pub fn try_get(&self, i:usize) -> Option<bool> { pub fn try_get(&self, i: usize) -> Option<bool> {
if (i / 32) >= self.bits.len() { if (i / 32) >= self.bits.len() {
None None
}else { } else {
Some(self.get(i)) Some(self.get(i))
} }
} }

View File

@@ -21,7 +21,7 @@
use std::fmt; use std::fmt;
use crate::common::Result; use crate::common::Result;
use crate::{point, Exceptions, Point, point_i}; use crate::{point, point_i, Exceptions, Point};
use super::BitArray; use super::BitArray;
@@ -761,10 +761,10 @@ impl fmt::Display for BitMatrix {
impl From<&BitMatrix> for Vec<bool> { impl From<&BitMatrix> for Vec<bool> {
fn from(value: &BitMatrix) -> Self { fn from(value: &BitMatrix) -> Self {
let mut arr = vec![false;(value.width * value.height) as usize]; let mut arr = vec![false; (value.width * value.height) as usize];
for x in 0..value.width { for x in 0..value.width {
for y in 0..value.height { for y in 0..value.height {
let insert_pos = ((y * value.width) + x )as usize; let insert_pos = ((y * value.width) + x) as usize;
arr[insert_pos] = value.get(x, y); arr[insert_pos] = value.get(x, y);
} }
} }

View File

@@ -221,9 +221,12 @@ pub fn CenterOfRing(
n += 1; n += 1;
// find out if we come full circle around the center. 8 bits have to be set in the end. // find out if we come full circle around the center. 8 bits have to be set in the end.
neighbourMask |= (1 neighbourMask |= 1
<< (4.0 + Point::dot(Point::round(Point::bresenhamDirection(cur.p() - center)), point(1.0, 3.0))) << (4.0
as u32); + Point::dot(
Point::round(Point::bresenhamDirection(cur.p() - center)),
point(1.0, 3.0),
)) as u32;
if !cur.stepAlongEdge(edgeDir, None) { if !cur.stepAlongEdge(edgeDir, None) {
return None; return None;
@@ -325,8 +328,11 @@ pub fn CollectRingPoints(
// find out if we come full circle around the center. 8 bits have to be set in the end. // find out if we come full circle around the center. 8 bits have to be set in the end.
neighbourMask |= 1 neighbourMask |= 1
<< (4.0 + Point::dot(Point::bresenhamDirection(cur.p - centerI), point(1.0, 3.0))) << (4.0
as u32; + Point::dot(
Point::round(Point::bresenhamDirection(cur.p - centerI)),
point(1.0, 3.0),
)) as u32;
if !cur.stepAlongEdge(edgeDir, None) { if !cur.stepAlongEdge(edgeDir, None) {
return Vec::default(); return Vec::default();
@@ -423,8 +429,7 @@ pub fn FitQadrilateralToPoints(center: Point, points: &mut [Point]) -> Option<Qu
} }
pub fn QuadrilateralIsPlausibleSquare(q: &Quadrilateral, lineIndex: usize) -> bool { pub fn QuadrilateralIsPlausibleSquare(q: &Quadrilateral, lineIndex: usize) -> bool {
let mut m = f64::default(); let mut m;
// let mut M = f64::default();
m = Point::distance(q[0], q[3]) as f64; //M = distance(q[0], q[3]); m = Point::distance(q[0], q[3]) as f64; //M = distance(q[0], q[3]);
let mut M = m; let mut M = m;

View File

@@ -11,13 +11,14 @@ pub struct FastEdgeToEdgeCounter<'a> {
stepsToBorder: i32, // = 0; stepsToBorder: i32, // = 0;
//arr: BitArray, //arr: BitArray,
_arr: isize, _arr: isize,
under_arry: &'a BitMatrix//,Vec<bool> under_arry: &'a BitMatrix, //,Vec<bool>
} }
impl<'a> FastEdgeToEdgeCounter<'a> { impl<'a> FastEdgeToEdgeCounter<'a> {
pub fn new<T: BitMatrixCursorTrait>(cur: &T) -> FastEdgeToEdgeCounter { pub fn new<T: BitMatrixCursorTrait>(cur: &T) -> FastEdgeToEdgeCounter {
let stride = cur.d().y as isize * cur.img().width() as isize + cur.d().x as isize; let stride = cur.d().y as isize * cur.img().width() as isize + cur.d().x as isize;
let p = ((cur.p().y as isize * cur.img().width() as isize).abs() as i32 + cur.p().x as i32) as u32; // P IS SET WRONG IN REVERSE let p = ((cur.p().y as isize * cur.img().width() as isize).abs() as i32 + cur.p().x as i32)
as u32; // P IS SET WRONG IN REVERSE
let maxStepsX = if cur.d().x != 0.0 { let maxStepsX = if cur.d().x != 0.0 {
if cur.d().x > 0.0 { if cur.d().x > 0.0 {
@@ -44,7 +45,7 @@ impl<'a> FastEdgeToEdgeCounter<'a> {
stride, stride,
stepsToBorder, stepsToBorder,
_arr: cur.p().y as isize * stride as isize, //cur.img().getRow(cur.p().y as u32), _arr: cur.p().y as isize * stride as isize, //cur.img().getRow(cur.p().y as u32),
under_arry: cur.img()//.into(), under_arry: cur.img(), //.into(),
} }
} }
@@ -65,11 +66,10 @@ impl<'a> FastEdgeToEdgeCounter<'a> {
// if !(self.under_arry[idx_pt] // if !(self.under_arry[idx_pt]
// == self.under_arry[self.p as usize]) // == self.under_arry[self.p as usize])
if !(self.under_arry.get_index(idx_pt) == self.under_arry.get_index(self.p as usize)) if !(self.under_arry.get_index(idx_pt) == self.under_arry.get_index(self.p as usize)) {
{
break true; break true;
} }
}; // while (p[steps * stride] == p[0]); } // while (p[steps * stride] == p[0]);
self.p = (self.p as isize + (steps as isize * self.stride)).abs() as u32; self.p = (self.p as isize + (steps as isize * self.stride)).abs() as u32;
self.stepsToBorder -= steps; self.stepsToBorder -= steps;

View File

@@ -23,7 +23,7 @@ impl<T: Default + Clone + Copy> Matrix<T> {
} }
pub fn new(width: usize, height: usize) -> Result<Matrix<T>> { pub fn new(width: usize, height: usize) -> Result<Matrix<T>> {
if (width != 0 && height != 0) { if (width != 0 && (width * height) / width as usize != height as usize) {
return Err(Exceptions::illegal_argument_with( return Err(Exceptions::illegal_argument_with(
"invalid size: width * height is too big", "invalid size: width * height is too big",
)); ));
@@ -64,7 +64,7 @@ impl<T: Default + Clone + Copy> Matrix<T> {
} }
pub fn get(&self, x: usize, y: usize) -> Option<T> { pub fn get(&self, x: usize, y: usize) -> Option<T> {
if x >= 0 && x < self.width && y >= 0 && y < self.height { if x >= self.width || y >= self.height {
None None
} else { } else {
Some(self.data[Self::get_offset(x, y, self.width)]) Some(self.data[Self::get_offset(x, y, self.width)])

View File

@@ -75,7 +75,11 @@ impl<'a> Iterator for PatternViewIterator<'_> {
self.current_position += 1; self.current_position += 1;
Some(*self.pattern_view.data.0.get(self.current_position - 1 + self.pattern_view.start + self.pattern_view.current)?) Some(
*self.pattern_view.data.0.get(
self.current_position - 1 + self.pattern_view.start + self.pattern_view.current,
)?,
)
} }
} }
@@ -147,7 +151,10 @@ impl<'a> PatternView<'a> {
} }
pub fn iter(&'a self) -> PatternViewIterator<'a> { pub fn iter(&'a self) -> PatternViewIterator<'a> {
PatternViewIterator { pattern_view: self, current_position: 0 } PatternViewIterator {
pattern_view: self,
current_position: 0,
}
} }
pub fn size(&self) -> usize { pub fn size(&self) -> usize {
@@ -273,7 +280,7 @@ impl<'a> PatternView<'a> {
} }
if index >= 0 { if index >= 0 {
let fetch_spot = ((self.start + self.current) as isize + index) as usize; let fetch_spot = ((self.start + self.current) as isize + index) as usize;
return Some(self.data.0[fetch_spot]) return Some(self.data.0[fetch_spot]);
} }
if index.abs() > (self.start + self.current) as isize { if index.abs() > (self.start + self.current) as isize {
return None; return None;
@@ -288,7 +295,7 @@ impl<'a> std::ops::Index<isize> for PatternView<'_> {
fn index(&self, index: isize) -> &Self::Output { fn index(&self, index: isize) -> &Self::Output {
if self.count == self.data.len() { if self.count == self.data.len() {
return &self.data[index.abs() as usize] return &self.data[index.abs() as usize];
} }
if index > self.data.0.len() as isize { if index > self.data.0.len() as isize {
@@ -296,7 +303,7 @@ impl<'a> std::ops::Index<isize> for PatternView<'_> {
} }
if index >= 0 { if index >= 0 {
let fetch_spot = ((self.start + self.current) as isize + index) as usize; let fetch_spot = ((self.start + self.current) as isize + index) as usize;
return &self.data.0[fetch_spot] return &self.data.0[fetch_spot];
} }
if index.abs() > self.start as isize { if index.abs() > self.start as isize {
panic!("array index out of bounds") panic!("array index out of bounds")
@@ -311,7 +318,7 @@ impl<'a> std::ops::Index<usize> for PatternView<'_> {
fn index(&self, index: usize) -> &Self::Output { fn index(&self, index: usize) -> &Self::Output {
if self.count == self.data.len() { if self.count == self.data.len() {
return &self.data[index] return &self.data[index];
} }
if index > self.data.0.len() { if index > self.data.0.len() {

View File

@@ -36,7 +36,7 @@ impl GridSampler for DefaultGridSampler {
dimensionX: u32, dimensionX: u32,
dimensionY: u32, dimensionY: u32,
controls: &[SamplerControl], controls: &[SamplerControl],
) -> Result<(BitMatrix,[Point;4])> { ) -> Result<(BitMatrix, [Point; 4])> {
if dimensionX <= 0 || dimensionY <= 0 { if dimensionX <= 0 || dimensionY <= 0 {
return Err(Exceptions::NOT_FOUND); return Err(Exceptions::NOT_FOUND);
} }
@@ -94,6 +94,6 @@ impl GridSampler for DefaultGridSampler {
let bl = projectCorner(Point::from((dimensionX, dimensionY))); let bl = projectCorner(Point::from((dimensionX, dimensionY)));
let br = projectCorner(Point::from((0, dimensionX))); let br = projectCorner(Point::from((0, dimensionX)));
Ok((bits, [tl,tr,bl,br])) Ok((bits, [tl, tr, bl, br]))
} }
} }

View File

@@ -21,7 +21,10 @@
// import java.nio.charset.Charset; // import java.nio.charset.Charset;
// import java.nio.charset.StandardCharsets; // import java.nio.charset.StandardCharsets;
use std::{collections::HashMap, fmt::{self, Display}}; use std::{
collections::HashMap,
fmt::{self, Display},
};
use crate::BarcodeFormat; use crate::BarcodeFormat;

View File

@@ -95,7 +95,7 @@ pub trait GridSampler {
dimensionY: u32, dimensionY: u32,
dst: Quadrilateral, dst: Quadrilateral,
src: Quadrilateral, src: Quadrilateral,
) -> Result<(BitMatrix,[Point;4])> { ) -> Result<(BitMatrix, [Point; 4])> {
let transform = PerspectiveTransform::quadrilateralToQuadrilateral(dst, src)?; let transform = PerspectiveTransform::quadrilateralToQuadrilateral(dst, src)?;
self.sample_grid( self.sample_grid(
@@ -112,7 +112,7 @@ pub trait GridSampler {
dimensionX: u32, dimensionX: u32,
dimensionY: u32, dimensionY: u32,
controls: &[SamplerControl], controls: &[SamplerControl],
) -> Result<(BitMatrix,[Point;4])> { ) -> Result<(BitMatrix, [Point; 4])> {
if dimensionX == 0 || dimensionY == 0 { if dimensionX == 0 || dimensionY == 0 {
return Err(Exceptions::NOT_FOUND); return Err(Exceptions::NOT_FOUND);
} }
@@ -172,7 +172,15 @@ pub trait GridSampler {
} }
// dbg!(bits.to_string()); // dbg!(bits.to_string());
Ok((bits, [Point::default(), Point::default(), Point::default(), Point::default()])) Ok((
bits,
[
Point::default(),
Point::default(),
Point::default(),
Point::default(),
],
))
} }
/** /**

View File

@@ -343,7 +343,7 @@ impl<'a> Detector<'_> {
); );
let src = Quadrilateral::new(topRight, topLeft, bottomRight, bottomLeft); let src = Quadrilateral::new(topRight, topLeft, bottomRight, bottomLeft);
let(res,_)=sampler.sample_grid_detailed(image, dimensionX, dimensionY, dst, src)?; let (res, _) = sampler.sample_grid_detailed(image, dimensionX, dimensionY, dst, src)?;
Ok(res) Ok(res)
} }

View File

@@ -428,7 +428,7 @@ pub fn Decode(bits: &BitMatrix) -> Result<DecoderResult<bool>> {
} }
// resultIterator = std::copy_n(codewordBytes.begin(), numDataCodewords, resultIterator); // resultIterator = std::copy_n(codewordBytes.begin(), numDataCodewords, resultIterator);
resultBytes[resultIterator..(resultIterator+numDataCodewords)] resultBytes[resultIterator..(resultIterator + numDataCodewords)]
.copy_from_slice(&codewordBytes[..numDataCodewords]); .copy_from_slice(&codewordBytes[..numDataCodewords]);
resultIterator += numDataCodewords; resultIterator += numDataCodewords;
} }

View File

@@ -340,7 +340,7 @@ pub fn TraceLine(image: &BitMatrix, p: Point, d: Point, edge: i32) -> impl Regre
line.add(Point::centered(c.p)) line.add(Point::centered(c.p))
.expect("could not add point on line"); .expect("could not add point on line");
stepCount -= 1; stepCount -= 1;
if !(stepCount > 0 && c.stepAlongEdge(dir, Some(true))) { if !(stepCount > 0 && c.stepAlongEdge(dir, Some(true))) {
break; break;
} }
@@ -414,13 +414,13 @@ pub fn LocateAlignmentPattern(
); );
// if we did not land on a black pixel the concentric pattern finder will fail // if we did not land on a black pixel the concentric pattern finder will fail
if (cor.is_none() || !image.get_point(cor.unwrap())) { if cor.is_none() || !image.get_point(cor.unwrap()) {
continue; continue;
} }
if let Some(cor1) = CenterOfRing(image, cor.unwrap(), moduleSize, 1, true) { if let Some(cor1) = CenterOfRing(image, cor.unwrap(), moduleSize, 1, true) {
if let Some(cor2) = CenterOfRing(image, cor.unwrap(), moduleSize * 3, -2, true) { if let Some(cor2) = CenterOfRing(image, cor.unwrap(), moduleSize * 3, -2, true) {
if (Point::distance(cor1, cor2) < moduleSize as f32 / 2.0) { if Point::distance(cor1, cor2) < moduleSize as f32 / 2.0 {
let res = (cor1 + cor2) / 2.0; let res = (cor1 + cor2) / 2.0;
// log(res, 3); // log(res, 3);
return Some(res); return Some(res);
@@ -442,13 +442,13 @@ pub fn ReadVersion(
for mirror in [false, true] { for mirror in [false, true] {
// Read top-right/bottom-left version info: 3 wide by 6 tall (depending on mirrored) // Read top-right/bottom-left version info: 3 wide by 6 tall (depending on mirrored)
let mut versionBits = 0; let mut versionBits = 0;
for y in (0..5).rev() { for y in (0..=5).rev() {
// for (int y = 5; y >= 0; --y) // for (int y = 5; y >= 0; --y)
for x in ((dimension - 11)..(dimension - 9)).rev() { for x in ((dimension - 11)..=(dimension - 9)).rev() {
// for (int x = dimension - 9; x >= dimension - 11; --x) { // for (int x = dimension - 9; x >= dimension - 11; --x) {
let mod_ = if mirror { point_i(y, x) } else { point_i(x, y) }; let mod_ = if mirror { point_i(y, x) } else { point_i(x, y) };
let pix = mod2Pix.transform_point((mod_).centered()); let pix = mod2Pix.transform_point((mod_).centered());
if (!image.is_in(pix)) { if !image.is_in(pix) {
versionBits = -1; versionBits = -1;
} else { } else {
AppendBit(&mut versionBits, image.get_point(pix)); AppendBit(&mut versionBits, image.get_point(pix));
@@ -563,14 +563,14 @@ pub fn SampleQR(image: &BitMatrix, fp: &FinderPatternSet) -> Result<QRCodeDetect
let N = (apM.len()) - 1; let N = (apM.len()) - 1;
// project the alignment pattern at module coordinates x/y to pixel coordinate based on current mod2Pix // project the alignment pattern at module coordinates x/y to pixel coordinate based on current mod2Pix
let projectM2P = /*[&mod2Pix, &apM]*/| x, y, mod2Pix: &PerspectiveTransform| { return mod2Pix.transform_point(Point::centered(point_i(apM[x], apM[y]))); }; let projectM2P = /*[&mod2Pix, &apM]*/| 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 mut findInnerCornerOfConcentricPattern = /*[&image, &apP, &projectM2P]*/| x, y, fp:ConcentricPattern| {
let pc = apP.set(x, y, projectM2P(x, y, &mod2Pix)); let pc = apP.set(x, y, projectM2P(x, y, &mod2Pix));
if let Some(fpQuad) = FindConcentricPatternCorners(image, fp.p, fp.size, 2) if let Some(fpQuad) = FindConcentricPatternCorners(image, fp.p, fp.size, 2)
// if (auto fpQuad = FindConcentricPatternCorners(image, fp, fp.size, 2)) // if (auto fpQuad = FindConcentricPatternCorners(image, fp, fp.size, 2))
{for c in fpQuad .0 {for c in fpQuad .0
{if (Point::distance(c, pc) < (fp.size as f32) / 2.0) {if Point::distance(c, pc) < (fp.size as f32) / 2.0
{apP.set(x, y, c);}}} {apP.set(x, y, c);}}}
}; };
@@ -731,31 +731,26 @@ pub fn SampleQR(image: &BitMatrix, fp: &FinderPatternSet) -> Result<QRCodeDetect
} }
} }
let grid_sampler = DefaultGridSampler::default(); let grid_sampler = DefaultGridSampler::default();
let (sampled,rp) = grid_sampler.sample_grid(image, dimension as u32, dimension as u32, &rois)?; let (sampled, rp) =
let result = QRCodeDetectorResult::new( grid_sampler.sample_grid(image, dimension as u32, dimension as u32, &rois)?;
sampled, let result = QRCodeDetectorResult::new(sampled, rp.to_vec());
rp.to_vec(),
);
return Ok(result); return Ok(result);
// grid_sampler.sample_grid(image, dimension, dimension, &rois); // grid_sampler.sample_grid(image, dimension, dimension, &rois);
// #endif // #endif
} }
let grid_sampler = DefaultGridSampler::default(); let grid_sampler = DefaultGridSampler::default();
let (sampled,rps) = grid_sampler.sample_grid( let (sampled, rps) = grid_sampler.sample_grid(
image, image,
dimension as u32, dimension as u32,
dimension as u32, dimension as u32,
&[SamplerControl { &[SamplerControl {
p1: point_i(dimension as u32, dimension as u32), p1: point_i(dimension as u32, dimension as u32),
p0: point_i(0,0 ), p0: point_i(0, 0),
transform: mod2Pix, transform: mod2Pix,
}], }],
)?; )?;
let result = QRCodeDetectorResult::new( let result = QRCodeDetectorResult::new(sampled, rps.to_vec());
sampled,
rps.to_vec(),
);
Ok(result) Ok(result)
// return SampleGrid(image, dimension, dimension, mod2Pix); // return SampleGrid(image, dimension, dimension, mod2Pix);
} }
@@ -1020,7 +1015,7 @@ pub fn SampleMQR(image: &BitMatrix, fp: ConcentricPattern) -> Result<QRCodeDetec
} }
let grid_sampler = DefaultGridSampler::default(); let grid_sampler = DefaultGridSampler::default();
let (sample, rps) =grid_sampler.sample_grid( let (sample, rps) = grid_sampler.sample_grid(
image, image,
dim, dim,
dim, dim,
@@ -1030,10 +1025,7 @@ pub fn SampleMQR(image: &BitMatrix, fp: ConcentricPattern) -> Result<QRCodeDetec
transform: bestPT, transform: bestPT,
}], }],
)?; )?;
Ok(QRCodeDetectorResult::new( Ok(QRCodeDetectorResult::new(sample, rps.to_vec()))
sample,
rps.to_vec(),
))
// SampleGrid(image, dim, dim, bestPT) // SampleGrid(image, dim, dim, bestPT)
} }