diff --git a/src/common/bit_matrix.rs b/src/common/bit_matrix.rs index 9a30749..d6c0d08 100644 --- a/src/common/bit_matrix.rs +++ b/src/common/bit_matrix.rs @@ -358,11 +358,6 @@ impl BitMatrix { * Clears all bits (sets to false). */ 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); } @@ -375,11 +370,6 @@ impl BitMatrix { * @param height The height of the region */ 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 { return Err(Exceptions::illegal_argument_with( "height and width must be at least 1", @@ -412,15 +402,6 @@ impl BitMatrix { * your own row */ 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 offset = y as usize * self.row_size; @@ -547,12 +528,9 @@ impl BitMatrix { //for (int x32 = 0; x32 < rowSize; x32++) { let theBits = self.bits[y as usize * self.row_size + x32]; if theBits != 0 { - if y < top { - top = y; - } - if y > bottom { - bottom = y; - } + top = top.min(y); + bottom = bottom.max(y); + if x32 * 32 < left as usize { let mut 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::parse_strings(value, "X", " ") + } +} + impl From<&BitMatrix> for Vec { fn from(value: &BitMatrix) -> Self { let mut arr = vec![false; (value.width * value.height) as usize]; diff --git a/src/common/global_histogram_binarizer.rs b/src/common/global_histogram_binarizer.rs index fee8ad1..bbb694c 100644 --- a/src/common/global_histogram_binarizer.rs +++ b/src/common/global_histogram_binarizer.rs @@ -156,12 +156,9 @@ impl GlobalHistogramBinarizer { let row = height * y / 5; let localLuminances = source.get_row(row); let right = (width * 4) / 5; - let mut x = width / 5; - while x < right { - // for (int x = width / 5; x < right; x++) { + for x in (width/5)..right { let pixel = localLuminances[x]; localBuckets[(pixel >> LUMINANCE_SHIFT) as usize] += 1; - x += 1; } } let blackPoint = Self::estimateBlackPoint(&localBuckets)?; @@ -171,10 +168,8 @@ impl GlobalHistogramBinarizer { // "fail quickly" which is necessary for continuous scanning. let localLuminances = source.get_matrix(); for y in 0..height { - // for (int y = 0; y < height; y++) { let offset = y * width; for x in 0..width { - // for (int x = 0; x < width; x++) { let pixel = localLuminances[offset + x]; if (pixel as u32) < blackPoint { matrix.set(x as u32, y as u32); @@ -185,16 +180,6 @@ impl GlobalHistogramBinarizer { 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 { // Find the tallest peak in the histogram. let numBuckets = buckets.len(); diff --git a/src/common/grid_sampler.rs b/src/common/grid_sampler.rs index 87137b9..0a0d519 100644 --- a/src/common/grid_sampler.rs +++ b/src/common/grid_sampler.rs @@ -175,10 +175,7 @@ pub trait GridSampler { Ok(( bits, [ - Point::default(), - Point::default(), - Point::default(), - Point::default(), + Point::default();4 ], )) } diff --git a/src/common/hybrid_binarizer.rs b/src/common/hybrid_binarizer.rs index 3fd4dbf..2fa37ac 100644 --- a/src/common/hybrid_binarizer.rs +++ b/src/common/hybrid_binarizer.rs @@ -169,17 +169,13 @@ impl HybridBinarizer { let maxXOffset = width - BLOCK_SIZE as u32; for y in 0..sub_height { // for (int y = 0; y < subHeight; y++) { - let mut yoffset = y << BLOCK_SIZE_POWER; - if yoffset > maxYOffset { - yoffset = maxYOffset; - } + let yoffset = u32::min(y << BLOCK_SIZE_POWER, maxYOffset); + 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 << BLOCK_SIZE_POWER; - if xoffset > maxXOffset { - xoffset = maxXOffset; - } + let xoffset = u32::min(x << BLOCK_SIZE_POWER,maxXOffset); + let left = Self::cap(x, sub_width - 3); let mut sum = 0; for z in -2..=2 { @@ -199,11 +195,7 @@ impl HybridBinarizer { #[inline(always)] fn cap(value: u32, max: u32) -> u32 { - if value < 2 { - 2 - } else { - value.min(max) - } + u32::clamp(value, 2, max) } /** @@ -248,16 +240,12 @@ impl HybridBinarizer { let mut blackPoints = vec![vec![0; subWidth as usize]; subHeight as usize]; for y in 0..subHeight { // for (int y = 0; y < subHeight; y++) { - let mut yoffset = y << BLOCK_SIZE_POWER; - if yoffset > maxYOffset as u32 { - yoffset = maxYOffset as u32; - } + let yoffset = u32::min(y << BLOCK_SIZE_POWER,maxYOffset as u32); + for x in 0..subWidth { // for (int x = 0; x < subWidth; x++) { - let mut xoffset = x << BLOCK_SIZE_POWER; - if xoffset > maxXOffset as u32 { - xoffset = maxXOffset as u32; - } + let xoffset = u32::min(x << BLOCK_SIZE_POWER,maxXOffset as u32); + let mut sum: u32 = 0; let mut min = 0xff; let mut max = 0; @@ -271,12 +259,8 @@ impl HybridBinarizer { let pixel = luminances[offset as usize + xx]; sum += pixel as u32; // still looking for good contrast - if pixel < min { - min = pixel; - } - if pixel > max { - max = pixel; - } + min = min.min(pixel); + max = max.max(pixel); } // short-circuit min/max tests once dynamic range is met if (max - min) as usize > MIN_DYNAMIC_RANGE { diff --git a/src/common/minimal_eci_input.rs b/src/common/minimal_eci_input.rs index 388425b..3d13f50 100644 --- a/src/common/minimal_eci_input.rs +++ b/src/common/minimal_eci_input.rs @@ -361,7 +361,7 @@ impl MinimalECIInput { } else { let bytes: Vec = encoderSet .encode_char(&c.c, c.encoderIndex) - .unwrap() + .unwrap_or_default() .iter() .map(|x| *x as u16) .collect(); diff --git a/src/common/perspective_transform.rs b/src/common/perspective_transform.rs index 03695fe..97c1e70 100644 --- a/src/common/perspective_transform.rs +++ b/src/common/perspective_transform.rs @@ -96,14 +96,6 @@ impl PerspectiveTransform { for point in points.iter_mut() { *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]) { diff --git a/src/common/string_utils.rs b/src/common/string_utils.rs index f16c762..5b12ac3 100644 --- a/src/common/string_utils.rs +++ b/src/common/string_utils.rs @@ -88,19 +88,15 @@ impl StringUtils { if let Some(DecodeHintValue::CharacterSet(cs_name)) = hints.get(&DecodeHintType::CHARACTER_SET) { - // if let DecodeHintValue::CharacterSet(cs_name) = hint { 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 + 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); } else { return Some(CharacterSet::UTF16LE); @@ -125,7 +121,7 @@ impl StringUtils { let mut sjis_max_double_bytes_word_length = 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 value in bytes.iter().take(length).copied() {