convert to using point...

instead of vectors for finding corners in bitmatrix
This commit is contained in:
Henry Schimke
2023-03-21 11:56:53 -05:00
parent 890fb4901f
commit 8a77e7b7ab
4 changed files with 43 additions and 41 deletions

View File

@@ -21,7 +21,7 @@
use std::fmt;
use crate::common::Result;
use crate::{Exceptions, Point};
use crate::{point, Exceptions, Point};
use super::BitArray;
@@ -561,7 +561,7 @@ impl BitMatrix {
*
* @return {@code x,y} coordinate of top-left-most 1 bit, or null if it is all white
*/
pub fn getTopLeftOnBit(&self) -> Option<Vec<u32>> {
pub fn getTopLeftOnBit(&self) -> Option<Point> {
let mut bitsOffset = 0;
while bitsOffset < self.bits.len() && self.bits[bitsOffset] == 0 {
bitsOffset += 1;
@@ -578,10 +578,10 @@ impl BitMatrix {
bit += 1;
}
x += bit;
Some(vec![x as u32, y as u32])
Some(point(x as f32, y as f32))
}
pub fn getBottomRightOnBit(&self) -> Option<[u32; 2]> {
pub fn getBottomRightOnBit(&self) -> Option<Point> {
let mut bitsOffset = self.bits.len() as i64 - 1;
while bitsOffset >= 0 && self.bits[bitsOffset as usize] == 0 {
bitsOffset -= 1;
@@ -600,7 +600,7 @@ impl BitMatrix {
}
x += bit;
Some([x as u32, y as u32])
Some(point(x as f32, y as f32))
}
/**

View File

@@ -27,6 +27,8 @@
// */
// public final class BitMatrixTestCase extends Assert {
use crate::point;
use super::BitMatrix;
static BIT_MATRIX_POINTS: [u32; 6] = [1, 2, 2, 0, 3, 1];
@@ -87,14 +89,14 @@ fn test_on_bit() {
assert!(matrix.getTopLeftOnBit().is_none());
assert!(matrix.getBottomRightOnBit().is_none());
matrix.setRegion(1, 1, 1, 1).expect("must set");
assert_eq!(vec![1, 1], matrix.getTopLeftOnBit().unwrap());
assert_eq!(vec![1, 1], matrix.getBottomRightOnBit().unwrap());
assert_eq!(point(1.0, 1.0), matrix.getTopLeftOnBit().unwrap());
assert_eq!(point(1.0, 1.0), matrix.getBottomRightOnBit().unwrap());
matrix.setRegion(1, 1, 3, 2).expect("must set");
assert_eq!(vec![1, 1], matrix.getTopLeftOnBit().unwrap());
assert_eq!(vec![3, 2], matrix.getBottomRightOnBit().unwrap());
assert_eq!(point(1.0, 1.0), matrix.getTopLeftOnBit().unwrap());
assert_eq!(point(3.0, 2.0), matrix.getBottomRightOnBit().unwrap());
matrix.setRegion(0, 0, 5, 5).expect("must set");
assert_eq!(vec![0, 0], matrix.getTopLeftOnBit().unwrap());
assert_eq!(vec![4, 4], matrix.getBottomRightOnBit().unwrap());
assert_eq!(point(0.0, 0.0), matrix.getTopLeftOnBit().unwrap());
assert_eq!(point(4.0, 4.0), matrix.getBottomRightOnBit().unwrap());
}
#[test]

View File

@@ -18,8 +18,8 @@ use std::collections::HashMap;
use crate::{
common::{BitMatrix, DecoderRXingResult, DetectorRXingResult, Result},
BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, Exceptions, RXingResult,
RXingResultMetadataType, RXingResultMetadataValue, Reader,
point, BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, Exceptions, Point,
RXingResult, RXingResultMetadataType, RXingResultMetadataValue, Reader,
};
use super::{
@@ -187,12 +187,12 @@ impl DataMatrixReader {
return Err(Exceptions::NOT_FOUND)
};
let moduleSize = Self::moduleSize(&leftTopBlack, image)?;
let moduleSize = Self::moduleSize(leftTopBlack, image)?;
let mut top = leftTopBlack[1];
let bottom = rightBottomBlack[1];
let mut left = leftTopBlack[0];
let right = rightBottomBlack[0];
let mut top = leftTopBlack.y;
let bottom = rightBottomBlack.y;
let mut left = leftTopBlack.x;
let right = rightBottomBlack.x;
let matrixWidth = (right as i32 - left as i32 + 1) / moduleSize as i32;
let matrixHeight = (bottom as i32 - top as i32 + 1) / moduleSize as i32;
@@ -207,7 +207,7 @@ impl DataMatrixReader {
// Push in the "border" by half the module width so that we start
// sampling in the middle of the module. Just in case the image is a
// little off, this will help recover.
let nudge = moduleSize / 2;
let nudge = moduleSize as f32 / 2.0;
top += nudge;
left += nudge;
@@ -215,10 +215,10 @@ impl DataMatrixReader {
let mut bits = BitMatrix::new(matrixWidth, matrixHeight)?;
for y in 0..matrixHeight {
// for (int y = 0; y < matrixHeight; y++) {
let iOffset = top + y * moduleSize;
let iOffset = top + y as f32 * moduleSize as f32;
for x in 0..matrixWidth {
// for (int x = 0; x < matrixWidth; x++) {
if image.get(left + x * moduleSize, iOffset) {
if image.get_point(point(left as f32 + x as f32 * moduleSize as f32, iOffset)) {
bits.set(x, y);
}
}
@@ -226,10 +226,10 @@ impl DataMatrixReader {
Ok(bits)
}
fn moduleSize(leftTopBlack: &[u32], image: &BitMatrix) -> Result<u32> {
fn moduleSize(leftTopBlack: Point, image: &BitMatrix) -> Result<u32> {
let width = image.getWidth();
let mut x = leftTopBlack[0];
let y = leftTopBlack[1];
let mut x = leftTopBlack.x as u32;
let y = leftTopBlack.y as u32;
while x < width && image.get(x, y) {
x += 1;
}
@@ -237,7 +237,7 @@ impl DataMatrixReader {
return Err(Exceptions::NOT_FOUND);
}
let moduleSize = x - leftTopBlack[0];
let moduleSize = x - leftTopBlack.x as u32;
if moduleSize == 0 {
return Err(Exceptions::NOT_FOUND);
}

View File

@@ -18,8 +18,8 @@ use std::collections::HashMap;
use crate::{
common::{BitMatrix, DecoderRXingResult, DetectorRXingResult, Result},
BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, Exceptions, Point, RXingResult,
RXingResultMetadataType, RXingResultMetadataValue, Reader,
point, BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, Exceptions, Point,
RXingResult, RXingResultMetadataType, RXingResultMetadataValue, Reader,
};
use super::{
@@ -156,12 +156,12 @@ impl QRCodeReader {
let leftTopBlack = leftTopBlack.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
let rightBottomBlack = rightBottomBlack.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
let moduleSize = Self::moduleSize(&leftTopBlack, image)?;
let moduleSize = Self::moduleSize(leftTopBlack, image)?;
let mut top = leftTopBlack[1] as i32;
let bottom = rightBottomBlack[1] as i32;
let mut left = leftTopBlack[0] as i32;
let mut right = rightBottomBlack[0] as i32;
let mut top = leftTopBlack.y as i32;
let bottom = rightBottomBlack.y as i32;
let mut left = leftTopBlack.x as i32;
let mut right = rightBottomBlack.x as i32;
// Sanity check!
if left >= right || top >= bottom {
@@ -229,27 +229,27 @@ impl QRCodeReader {
Ok(bits)
}
fn moduleSize(leftTopBlack: &[u32], image: &BitMatrix) -> Result<f32> {
let height = image.getHeight();
let width = image.getWidth();
let mut x = leftTopBlack[0];
let mut y = leftTopBlack[1];
fn moduleSize(leftTopBlack: Point, image: &BitMatrix) -> Result<f32> {
let height = image.getHeight() as f32;
let width = image.getWidth() as f32;
let mut x = leftTopBlack.x;
let mut y = leftTopBlack.y;
let mut inBlack = true;
let mut transitions = 0;
while x < width && y < height {
if inBlack != image.get(x, y) {
if inBlack != image.get_point(point(x, y)) {
transitions += 1;
if transitions == 5 {
break;
}
inBlack = !inBlack;
}
x += 1;
y += 1;
x += 1.0;
y += 1.0;
}
if x == width || y == height {
return Err(Exceptions::NOT_FOUND);
}
Ok((x - leftTopBlack[0]) as f32 / 7.0)
Ok((x - leftTopBlack.x) as f32 / 7.0)
}
}