fix: code cleanup and updates

This commit is contained in:
Henry Schimke
2024-01-31 18:03:42 -06:00
parent 20c61c9c13
commit 175511180f
7 changed files with 29 additions and 89 deletions

View File

@@ -358,11 +358,6 @@ impl BitMatrix {
* Clears all bits (sets to false). * Clears all bits (sets to false).
*/ */
pub fn clear(&mut self) { pub fn clear(&mut self) {
// let max = self.bits.len();
// for i in 0..max {
// //for (int i = 0; i < max; i++) {
// self.bits[i] = 0;
// }
self.bits.fill(0); self.bits.fill(0);
} }
@@ -375,11 +370,6 @@ impl BitMatrix {
* @param height The height of the region * @param height The height of the region
*/ */
pub fn setRegion(&mut self, left: u32, top: u32, width: u32, height: u32) -> Result<()> { pub fn setRegion(&mut self, left: u32, top: u32, width: u32, height: u32) -> Result<()> {
// if top < 0 || left < 0 {
// return Err(Exceptions::IllegalArgumentException(
// "Left and top must be nonnegative".to_owned(),
// ));
// }
if height < 1 || width < 1 { if height < 1 || width < 1 {
return Err(Exceptions::illegal_argument_with( return Err(Exceptions::illegal_argument_with(
"height and width must be at least 1", "height and width must be at least 1",
@@ -412,15 +402,6 @@ impl BitMatrix {
* your own row * your own row
*/ */
pub fn getRow(&self, y: u32) -> BitArray { pub fn getRow(&self, y: u32) -> BitArray {
// let mut rw: BitArray = if row.getSize() < self.width as usize {
// BitArray::with_size(self.width as usize)
// } else {
// let mut z = row; //row.clone();
// z.clear();
// z
// // row.clear();
// // row.clone()
// };
let mut rw = BitArray::with_size(self.width as usize); let mut rw = BitArray::with_size(self.width as usize);
let offset = y as usize * self.row_size; let offset = y as usize * self.row_size;
@@ -547,12 +528,9 @@ impl BitMatrix {
//for (int x32 = 0; x32 < rowSize; x32++) { //for (int x32 = 0; x32 < rowSize; x32++) {
let theBits = self.bits[y as usize * self.row_size + x32]; let theBits = self.bits[y as usize * self.row_size + x32];
if theBits != 0 { if theBits != 0 {
if y < top { top = top.min(y);
top = y; bottom = bottom.max(y);
}
if y > bottom {
bottom = y;
}
if x32 * 32 < left as usize { if x32 * 32 < left as usize {
let mut bit = 0; let mut bit = 0;
while (theBits << (31 - bit)) == 0 { while (theBits << (31 - bit)) == 0 {
@@ -759,6 +737,14 @@ impl fmt::Display for BitMatrix {
} }
} }
impl TryFrom<&str> for BitMatrix {
type Error = Exceptions;
fn try_from(value: &str) -> std::prelude::v1::Result<Self, Self::Error> {
Self::parse_strings(value, "X", " ")
}
}
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];

View File

@@ -156,12 +156,9 @@ impl<LS: LuminanceSource> GlobalHistogramBinarizer<LS> {
let row = height * y / 5; let row = height * y / 5;
let localLuminances = source.get_row(row); let localLuminances = source.get_row(row);
let right = (width * 4) / 5; let right = (width * 4) / 5;
let mut x = width / 5; for x in (width/5)..right {
while x < right {
// for (int x = width / 5; x < right; x++) {
let pixel = localLuminances[x]; let pixel = localLuminances[x];
localBuckets[(pixel >> LUMINANCE_SHIFT) as usize] += 1; localBuckets[(pixel >> LUMINANCE_SHIFT) as usize] += 1;
x += 1;
} }
} }
let blackPoint = Self::estimateBlackPoint(&localBuckets)?; let blackPoint = Self::estimateBlackPoint(&localBuckets)?;
@@ -171,10 +168,8 @@ impl<LS: LuminanceSource> GlobalHistogramBinarizer<LS> {
// "fail quickly" which is necessary for continuous scanning. // "fail quickly" which is necessary for continuous scanning.
let localLuminances = source.get_matrix(); let localLuminances = source.get_matrix();
for y in 0..height { for y in 0..height {
// for (int y = 0; y < height; y++) {
let offset = y * width; let offset = y * width;
for x in 0..width { for x in 0..width {
// for (int x = 0; x < width; x++) {
let pixel = localLuminances[offset + x]; let pixel = localLuminances[offset + x];
if (pixel as u32) < blackPoint { if (pixel as u32) < blackPoint {
matrix.set(x as u32, y as u32); matrix.set(x as u32, y as u32);
@@ -185,16 +180,6 @@ impl<LS: LuminanceSource> GlobalHistogramBinarizer<LS> {
Ok(matrix) Ok(matrix)
} }
// fn initArrays(&mut self, luminanceSize: usize) {
// // if self.luminances.len() < luminanceSize {
// // self.luminances = ;
// // }
// // // for x in 0..GlobalHistogramBinarizer::LUMINANCE_BUCKETS {
// // // for (int x = 0; x < LUMINANCE_BUCKETS; x++) {
// // self.buckets[x] = 0;
// // }
// }
fn estimateBlackPoint(buckets: &[u32]) -> Result<u32> { fn estimateBlackPoint(buckets: &[u32]) -> Result<u32> {
// Find the tallest peak in the histogram. // Find the tallest peak in the histogram.
let numBuckets = buckets.len(); let numBuckets = buckets.len();

View File

@@ -175,10 +175,7 @@ pub trait GridSampler {
Ok(( Ok((
bits, bits,
[ [
Point::default(), Point::default();4
Point::default(),
Point::default(),
Point::default(),
], ],
)) ))
} }

View File

@@ -169,17 +169,13 @@ impl<LS: LuminanceSource> HybridBinarizer<LS> {
let maxXOffset = width - BLOCK_SIZE as u32; let maxXOffset = width - BLOCK_SIZE as u32;
for y in 0..sub_height { for y in 0..sub_height {
// for (int y = 0; y < subHeight; y++) { // for (int y = 0; y < subHeight; y++) {
let mut yoffset = y << BLOCK_SIZE_POWER; let yoffset = u32::min(y << BLOCK_SIZE_POWER, maxYOffset);
if yoffset > maxYOffset {
yoffset = maxYOffset;
}
let top = Self::cap(y, sub_height - 3); let top = Self::cap(y, sub_height - 3);
for x in 0..sub_width { for x in 0..sub_width {
// for (int x = 0; x < subWidth; x++) { // for (int x = 0; x < subWidth; x++) {
let mut xoffset = x << BLOCK_SIZE_POWER; let xoffset = u32::min(x << BLOCK_SIZE_POWER,maxXOffset);
if xoffset > maxXOffset {
xoffset = maxXOffset;
}
let left = Self::cap(x, sub_width - 3); let left = Self::cap(x, sub_width - 3);
let mut sum = 0; let mut sum = 0;
for z in -2..=2 { for z in -2..=2 {
@@ -199,11 +195,7 @@ impl<LS: LuminanceSource> HybridBinarizer<LS> {
#[inline(always)] #[inline(always)]
fn cap(value: u32, max: u32) -> u32 { fn cap(value: u32, max: u32) -> u32 {
if value < 2 { u32::clamp(value, 2, max)
2
} else {
value.min(max)
}
} }
/** /**
@@ -248,16 +240,12 @@ impl<LS: LuminanceSource> HybridBinarizer<LS> {
let mut blackPoints = vec![vec![0; subWidth as usize]; subHeight as usize]; let mut blackPoints = vec![vec![0; subWidth as usize]; subHeight as usize];
for y in 0..subHeight { for y in 0..subHeight {
// for (int y = 0; y < subHeight; y++) { // for (int y = 0; y < subHeight; y++) {
let mut yoffset = y << BLOCK_SIZE_POWER; let yoffset = u32::min(y << BLOCK_SIZE_POWER,maxYOffset as u32);
if yoffset > maxYOffset as u32 {
yoffset = maxYOffset as u32;
}
for x in 0..subWidth { for x in 0..subWidth {
// for (int x = 0; x < subWidth; x++) { // for (int x = 0; x < subWidth; x++) {
let mut xoffset = x << BLOCK_SIZE_POWER; let xoffset = u32::min(x << BLOCK_SIZE_POWER,maxXOffset as u32);
if xoffset > maxXOffset as u32 {
xoffset = maxXOffset as u32;
}
let mut sum: u32 = 0; let mut sum: u32 = 0;
let mut min = 0xff; let mut min = 0xff;
let mut max = 0; let mut max = 0;
@@ -271,12 +259,8 @@ impl<LS: LuminanceSource> HybridBinarizer<LS> {
let pixel = luminances[offset as usize + xx]; let pixel = luminances[offset as usize + xx];
sum += pixel as u32; sum += pixel as u32;
// still looking for good contrast // still looking for good contrast
if pixel < min { min = min.min(pixel);
min = pixel; max = max.max(pixel);
}
if pixel > max {
max = pixel;
}
} }
// short-circuit min/max tests once dynamic range is met // short-circuit min/max tests once dynamic range is met
if (max - min) as usize > MIN_DYNAMIC_RANGE { if (max - min) as usize > MIN_DYNAMIC_RANGE {

View File

@@ -361,7 +361,7 @@ impl MinimalECIInput {
} else { } else {
let bytes: Vec<u16> = encoderSet let bytes: Vec<u16> = encoderSet
.encode_char(&c.c, c.encoderIndex) .encode_char(&c.c, c.encoderIndex)
.unwrap() .unwrap_or_default()
.iter() .iter()
.map(|x| *x as u16) .map(|x| *x as u16)
.collect(); .collect();

View File

@@ -96,14 +96,6 @@ impl PerspectiveTransform {
for point in points.iter_mut() { for point in points.iter_mut() {
*point = self.transform_point(Point::new(point.x, point.y)); *point = self.transform_point(Point::new(point.x, point.y));
} }
// for point in points.iter_mut() {
// // for (int i = 0; i < maxI; i += 2) {
// let x = point.x;
// let y = point.y;
// let denominator = self.a13 * x + self.a23 * y + self.a33;
// point.x = (self.a11 * x + self.a21 * y + self.a31) / denominator;
// point.y = (self.a12 * x + self.a22 * y + self.a32) / denominator;
// }
} }
pub fn transform_points_double(&self, x_values: &mut [f32], y_valuess: &mut [f32]) { pub fn transform_points_double(&self, x_values: &mut [f32], y_valuess: &mut [f32]) {

View File

@@ -88,19 +88,15 @@ impl StringUtils {
if let Some(DecodeHintValue::CharacterSet(cs_name)) = if let Some(DecodeHintValue::CharacterSet(cs_name)) =
hints.get(&DecodeHintType::CHARACTER_SET) hints.get(&DecodeHintType::CHARACTER_SET)
{ {
// if let DecodeHintValue::CharacterSet(cs_name) = hint {
return CharacterSet::get_character_set_by_name(cs_name); return CharacterSet::get_character_set_by_name(cs_name);
// }
} }
// if hints.contains_key(&DecodeHintType::CHARACTER_SET) {
// return Charset.forName(hints.get(DecodeHintType.CHARACTER_SET).toString());
// }
// First try UTF-16, assuming anything with its BOM is UTF-16 // First try UTF-16, assuming anything with its BOM is UTF-16
if bytes.len() > 2 if bytes.len() > 2
&& ((bytes[0] == 0xFE && bytes[1] == 0xFF) || (bytes[0] == 0xFF && bytes[1] == 0xFE)) && ((bytes[0..=1] == [0xFE,0xFF]) || (bytes[0..=1] == [0xFF , 0xFE]))
{ {
if bytes[0] == 0xFE && bytes[1] == 0xFF { if bytes[0..=1] == [0xFE , 0xFF] {
return Some(CharacterSet::UTF16BE); return Some(CharacterSet::UTF16BE);
} else { } else {
return Some(CharacterSet::UTF16LE); return Some(CharacterSet::UTF16LE);
@@ -125,7 +121,7 @@ impl StringUtils {
let mut sjis_max_double_bytes_word_length = 0; let mut sjis_max_double_bytes_word_length = 0;
let mut iso_high_other = 0; let mut iso_high_other = 0;
let utf8bom = bytes.len() > 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF; let utf8bom = bytes.len() > 3 && bytes[0..=2] == [ 0xEF , 0xBB , 0xBF];
// for i in 0..length { // for i in 0..length {
for value in bytes.iter().take(length).copied() { for value in bytes.iter().take(length).copied() {