partially passing encoder

This commit is contained in:
Henry Schimke
2022-09-25 16:52:12 -05:00
parent fb08ee0e34
commit b2a460a226
10 changed files with 769 additions and 512 deletions

View File

@@ -34,6 +34,8 @@
// import java.util.Random;
// import java.util.TreeSet;
use rand::Rng;
use crate::{aztec::decoder, common::BitMatrix, exceptions::Exceptions};
use super::{
@@ -69,7 +71,7 @@ fn testErrorInParameterLocatorNotCompact() {
fn testErrorInParameterLocator(data: &str) {
let aztec = encoder::encoder::encode(data, 25, encoder::encoder::DEFAULT_AZTEC_LAYERS)
.expect("encode should create");
let random = rand::thread_rng(); //Random(aztec.getMatrix().hashCode()); // pseudo-random, but deterministic
let mut random = rand::thread_rng(); //Random(aztec.getMatrix().hashCode()); // pseudo-random, but deterministic
let layers = aztec.getLayers();
let compact = aztec.isCompact();
let orientationPoints = getOrientationPoints(&aztec);
@@ -78,24 +80,24 @@ fn testErrorInParameterLocator(data: &str) {
for matrix in getRotations(aztec.getMatrix()) {
// for (BitMatrix matrix : getRotations(aztec.getMatrix())) {
// Systematically try every possible 1- and 2-bit error.
for error1 in 0..orientationPoints.size() {
for error1 in 0..orientationPoints.len() {
// for (int error1 = 0; error1 < orientationPoints.size(); error1++) {
for error2 in error1..orientationPoints.size() {
for error2 in error1..orientationPoints.len() {
// for (int error2 = error1; error2 < orientationPoints.size(); error2++) {
let copy = if isMirror {
let mut copy = if isMirror {
transpose(&matrix)
} else {
clone(&matrix)
};
copy.flip(
orientationPoints.get(error1).getX(),
orientationPoints.get(error1).getY(),
copy.flip_coords(
orientationPoints.get(error1).unwrap().getX() as u32,
orientationPoints.get(error1).unwrap().getY() as u32,
);
if error2 > error1 {
// if error2 == error1, we only test a single error
copy.flip(
orientationPoints.get(error2).getX(),
orientationPoints.get(error2).getY(),
copy.flip_coords(
orientationPoints.get(error2).unwrap().getX() as u32,
orientationPoints.get(error2).unwrap().getY() as u32,
);
}
// The detector doesn't seem to work when matrix bits are only 1x1. So magnify.
@@ -111,17 +113,17 @@ fn testErrorInParameterLocator(data: &str) {
// Try a few random three-bit errors;
for i in 0..5 {
// for (int i = 0; i < 5; i++) {
let copy = clone(&matrix);
let errors = Vec::new();
while errors.size() < 3 {
let mut copy = clone(&matrix);
let mut errors = Vec::new();
while errors.len() < 3 {
// Quick and dirty way of getting three distinct integers between 1 and n.
errors.push(random.nextInt(orientationPoints.size()));
errors.push(random.gen_range(0..=orientationPoints.len()));
}
for error in errors {
// for (int error : errors) {
copy.flip(
orientationPoints.get(error).getX(),
orientationPoints.get(error).getY(),
copy.flip_coords(
orientationPoints.get(error).unwrap().getX() as u32,
orientationPoints.get(error).unwrap().getY() as u32,
);
}
// try {
@@ -147,7 +149,7 @@ fn testErrorInParameterLocator(data: &str) {
// Zooms a bit matrix so that each bit is factor x factor
fn makeLarger(input: &BitMatrix, factor: u32) -> BitMatrix {
let width = input.getWidth();
let output = BitMatrix::with_single_dimension(width * factor);
let mut output = BitMatrix::with_single_dimension(width * factor);
for inputY in 0..width {
// for (int inputY = 0; inputY < width; inputY++) {
for inputX in 0..width {
@@ -165,13 +167,13 @@ fn getRotations(matrix0: &BitMatrix) -> Vec<BitMatrix> {
let matrix90 = rotateRight(matrix0);
let matrix180 = rotateRight(&matrix90);
let matrix270 = rotateRight(&matrix180);
vec![*matrix0, matrix90, matrix180, matrix270]
vec![matrix0.clone(), matrix90, matrix180, matrix270]
}
// Rotates a square BitMatrix to the right by 90 degrees
fn rotateRight(input: &BitMatrix) -> BitMatrix {
let width = input.getWidth();
let result = BitMatrix::with_single_dimension(width);
let mut result = BitMatrix::with_single_dimension(width);
for x in 0..width {
// for (int x = 0; x < width; x++) {
for y in 0..width {
@@ -188,7 +190,7 @@ fn rotateRight(input: &BitMatrix) -> BitMatrix {
// matrix to the right, and then flipping it left-to-right
fn transpose(input: &BitMatrix) -> BitMatrix {
let width = input.getWidth();
let result = BitMatrix::with_single_dimension(width);
let mut result = BitMatrix::with_single_dimension(width);
for x in 0..width {
// for (int x = 0; x < width; x++) {
for y in 0..width {
@@ -203,7 +205,7 @@ fn transpose(input: &BitMatrix) -> BitMatrix {
fn clone(input: &BitMatrix) -> BitMatrix {
let width = input.getWidth();
let result = BitMatrix::with_single_dimension(width);
let mut result = BitMatrix::with_single_dimension(width);
for x in 0..width {
// for (int x = 0; x < width; x++) {
for y in 0..width {
@@ -217,21 +219,21 @@ fn clone(input: &BitMatrix) -> BitMatrix {
}
fn getOrientationPoints(code: &AztecCode) -> Vec<Point> {
let center = code.getMatrix().getWidth() / 2;
let center = code.getMatrix().getWidth() as i32 / 2;
let offset = if code.isCompact() { 5 } else { 7 };
let result = Vec::new();
let mut xSign = -1;
let mut result = Vec::new();
let mut xSign: i32 = -1;
while xSign <= 1 {
// for (int xSign = -1; xSign <= 1; xSign += 2) {
let mut ySign = -1;
let mut ySign: i32 = -1;
while ySign <= 1 {
// for (int ySign = -1; ySign <= 1; ySign += 2) {
result.add(Point::new(center + xSign * offset, center + ySign * offset));
result.add(Point::new(
result.push(Point::new(center + xSign * offset, center + ySign * offset));
result.push(Point::new(
center + xSign * (offset - 1),
center + ySign * offset,
));
result.add(Point::new(
result.push(Point::new(
center + xSign * offset,
center + ySign * (offset - 1),
));