From 438ae9f5886f99b2ab27e1a10e9de9015c425700 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Fri, 30 Sep 2022 15:53:15 -0500 Subject: [PATCH] Passing blackbox tests aztec --- src/aztec/detector.rs | 1 + src/buffered_image_luminance_source.rs | 3 +- src/common/mod.rs | 42 ++++++++++---------- tests/common/abstract_black_box_test_case.rs | 15 +++++-- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/aztec/detector.rs b/src/aztec/detector.rs index d3e7052..61b38d2 100644 --- a/src/aztec/detector.rs +++ b/src/aztec/detector.rs @@ -76,6 +76,7 @@ impl Detector { * @throws NotFoundException if no Aztec Code can be found */ pub fn detect(&mut self, is_mirror: bool) -> Result { + // dbg!(self.image.to_string()); // 1. Get the center of the aztec matrix let p_center = self.get_matrix_center(); diff --git a/src/buffered_image_luminance_source.rs b/src/buffered_image_luminance_source.rs index 7548582..ed52030 100644 --- a/src/buffered_image_luminance_source.rs +++ b/src/buffered_image_luminance_source.rs @@ -97,7 +97,6 @@ impl BufferedImageLuminanceSource { // } // } - Self { image: DynamicImage::from(image.to_luma8()), @@ -191,7 +190,7 @@ impl LuminanceSource for BufferedImageLuminanceSource { &self.image.to_luma8(), MINUS_45_IN_RADIANS, imageproc::geometric_transformations::Interpolation::Nearest, - Luma([255; 1]), + Luma([u8::MAX/2; 1]), ); let new_img = DynamicImage::from(img); diff --git a/src/common/mod.rs b/src/common/mod.rs index 6c506ab..259a385 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -3899,33 +3899,33 @@ impl HybridBinarizer { */ fn calculateThresholdForBlock( luminances: &[u8], - subWidth: u32, - subHeight: u32, + sub_width: u32, + sub_height: u32, width: u32, height: u32, - blackPoints: &Vec>, + black_points: &Vec>, matrix: &mut BitMatrix, ) { let maxYOffset = height - HybridBinarizer::BLOCK_SIZE as u32; let maxXOffset = width - HybridBinarizer::BLOCK_SIZE as u32; - for y in 0..subHeight { + for y in 0..sub_height { // for (int y = 0; y < subHeight; y++) { let mut yoffset = y << HybridBinarizer::BLOCK_SIZE_POWER; if yoffset > maxYOffset { yoffset = maxYOffset; } - let top = Self::cap(y, subHeight - 3); - for x in 0..subWidth { + let top = Self::cap(y, sub_height - 3); + for x in 0..sub_width { // for (int x = 0; x < subWidth; x++) { let mut xoffset = x << HybridBinarizer::BLOCK_SIZE_POWER; if xoffset > maxXOffset { xoffset = maxXOffset; } - let left = Self::cap(x, subWidth - 3); + let left = Self::cap(x, sub_width - 3); let mut sum = 0; for z in -2i32..=2 { // for (int z = -2; z <= 2; z++) { - let blackRow = &blackPoints[(top as i32 + z) as usize]; + let blackRow = &black_points[(top as i32 + z) as usize]; sum += blackRow[(left - 2) as usize] + blackRow[(left - 1) as usize] + blackRow[left as usize] @@ -3999,10 +3999,12 @@ impl HybridBinarizer { xoffset = maxXOffset as u32; } let mut sum = 0u32; - let mut min = 0; - let mut max = 0xFF; + let mut min = 0xff; + let mut max = 0; + let mut offset = yoffset * width + xoffset; - for yy in 0..HybridBinarizer::BLOCK_SIZE { + let mut yy = 0; + while yy < HybridBinarizer::BLOCK_SIZE { // for (int yy = 0, offset = yoffset * width + xoffset; yy < HybridBinarizer::BLOCK_SIZE; yy++, offset += width) { for xx in 0..HybridBinarizer::BLOCK_SIZE { // for (int xx = 0; xx < HybridBinarizer::BLOCK_SIZE; xx++) { @@ -4020,16 +4022,19 @@ impl HybridBinarizer { if (max - min) as usize > HybridBinarizer::MIN_DYNAMIC_RANGE { // finish the rest of the rows quickly offset += width; - for _yy_s in yy + 1..HybridBinarizer::BLOCK_SIZE { + yy+=1; + while yy < HybridBinarizer::BLOCK_SIZE { // for (yy++, offset += width; yy < HybridBinarizer::BLOCK_SIZE; yy++, offset += width) { for xx in 0..HybridBinarizer::BLOCK_SIZE { // for (int xx = 0; xx < BLOCK_SIZE; xx++) { sum += luminances[offset as usize + xx] as u32; } + yy+=1; offset += width; } break; } + yy+=1; offset += width; } @@ -4052,21 +4057,18 @@ impl HybridBinarizer { // the boundaries is used for the interior. // The (min < bp) is arbitrary but works better than other heuristics that were tried. - let average_neighbor_black_point = (blackPoints[y as usize - 1][x as usize] + let average_neighbor_black_point:u32 = (blackPoints[y as usize - 1][x as usize] + (2 * blackPoints[y as usize][x as usize - 1]) + blackPoints[y as usize - 1][x as usize - 1]) / 4; - if (min < average_neighbor_black_point) { - average = average_neighbor_black_point as u32; + if (min as u32) < average_neighbor_black_point { + average = average_neighbor_black_point; } } } - blackPoints[y as usize][x as usize] = average as u8; + blackPoints[y as usize][x as usize] = average; } } - return blackPoints - .into_iter() - .map(|x| x.iter().map(|y| *y as u32).collect()) - .collect(); + blackPoints } } diff --git a/tests/common/abstract_black_box_test_case.rs b/tests/common/abstract_black_box_test_case.rs index 1a39ee8..157fbb3 100644 --- a/tests/common/abstract_black_box_test_case.rs +++ b/tests/common/abstract_black_box_test_case.rs @@ -16,8 +16,8 @@ use std::{ collections::HashMap, - fs::{read_dir, read_to_string}, - path::{Path, PathBuf}, + fs::{read_dir, read_to_string, File}, + path::{Path, PathBuf}, io::Write, }; use rxing::{ @@ -231,6 +231,15 @@ impl AbstractBlackBoxTestCase { let rotated_image = Self::rotate_image(&image, rotation); let source = BufferedImageLuminanceSource::new(rotated_image); let bitmap = BinaryBitmap::new(Box::new(HybridBinarizer::new(Box::new(source)))); + + // if file_base_name == "09" { + // let mut f = File::create("test_file_output.txt").unwrap(); + // dbg!("dumb"); + // write!(f,"{}", bitmap.getBlackMatrix().unwrap()); + // drop(f); + // Self::rotate_image(&image, rotation).save("test_image.png").unwrap(); + // } + if let Ok(decoded) = self.decode(&bitmap, rotation, &expected_text, &expected_metadata, false) { @@ -499,7 +508,7 @@ impl AbstractBlackBoxTestCase { &original.to_luma8(), radians, Interpolation::Nearest, - Luma([255; 1]), + Luma([u8::MAX/2; 1]), ); DynamicImage::from(i)