diff --git a/src/common/otsu_level_binarizer.rs b/src/common/otsu_level_binarizer.rs index cd2de5f..a51c690 100644 --- a/src/common/otsu_level_binarizer.rs +++ b/src/common/otsu_level_binarizer.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, rc::Rc}; +use std::borrow::Cow; use image::{DynamicImage, ImageBuffer, Luma}; use once_cell::sync::OnceCell; @@ -8,16 +8,16 @@ use crate::{Binarizer, Exceptions, LuminanceSource}; use super::{BitArray, BitMatrix}; -pub struct OtsuLevelBinarizer { +pub struct OtsuLevelBinarizer { width: usize, height: usize, - source: Box, + source: LS, black_matrix: OnceCell, black_row_cache: Vec>, } -impl OtsuLevelBinarizer { - fn generate_threshold_matrix(source: &dyn LuminanceSource) -> Result { +impl OtsuLevelBinarizer { + fn generate_threshold_matrix(source: &LS2) -> Result { let image_buffer = { let Some(buff) : Option,Vec>> = ImageBuffer::from_vec(source.get_width() as u32, source.get_height() as u32, source.get_matrix()) else { return Err(Exceptions::ILLEGAL_ARGUMENT) @@ -34,7 +34,7 @@ impl OtsuLevelBinarizer { dynamic_filtered.try_into() } - pub fn new(source: Box) -> Self { + pub fn new(source: LS) -> Self { Self { width: source.get_width(), height: source.get_height(), @@ -45,12 +45,14 @@ impl OtsuLevelBinarizer { } } -impl Binarizer for OtsuLevelBinarizer { - fn get_luminance_source(&self) -> &Box { +impl Binarizer for OtsuLevelBinarizer { + type Source = LS; + + fn get_luminance_source(&self) -> &LS { &self.source } - fn get_black_row(&self, y: usize) -> Result> { + fn get_black_row(&self, y: usize) -> Result> { let row = self.black_row_cache[y].get_or_try_init(|| { let matrix = self.get_black_matrix()?; Ok(matrix.getRow(y as u32)) @@ -59,18 +61,15 @@ impl Binarizer for OtsuLevelBinarizer { Ok(Cow::Borrowed(row)) } - fn get_black_matrix(&self) -> Result<&super::BitMatrix> { + fn get_black_matrix(&self) -> Result<&BitMatrix> { let matrix = self .black_matrix - .get_or_try_init(|| Self::generate_threshold_matrix(self.source.as_ref()))?; + .get_or_try_init(|| Self::generate_threshold_matrix(&self.source))?; Ok(matrix) } - fn create_binarizer( - &self, - source: Box, - ) -> std::rc::Rc { - Rc::new(Self::new(source)) + fn create_binarizer(&self, source: LS) -> Self { + Self::new(source) } fn get_width(&self) -> usize { diff --git a/src/helpers.rs b/src/helpers.rs index 5559bbf..71f3e85 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -57,9 +57,7 @@ pub fn detect_in_svg_with_hints( .or_insert(DecodeHintValue::TryHarder(true)); multi_format_reader.decode_with_hints( - &mut BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new( - SVGLuminanceSource::new(&svg_data)?, - )))), + &mut BinaryBitmap::new(HybridBinarizer::new(SVGLuminanceSource::new(&svg_data)?)), hints, ) } @@ -100,9 +98,7 @@ pub fn detect_multiple_in_svg_with_hints( .or_insert(DecodeHintValue::TryHarder(true)); scanner.decode_multiple_with_hints( - &mut BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new( - SVGLuminanceSource::new(&svg_data)?, - )))), + &mut BinaryBitmap::new(HybridBinarizer::new(SVGLuminanceSource::new(&svg_data)?)), hints, ) } diff --git a/src/qrcode/decoder/decoded_bit_stream_parser.rs b/src/qrcode/decoder/decoded_bit_stream_parser.rs index 4d6451e..bf97309 100644 --- a/src/qrcode/decoder/decoded_bit_stream_parser.rs +++ b/src/qrcode/decoder/decoded_bit_stream_parser.rs @@ -310,7 +310,7 @@ fn decodeByteSegment( { encoding::all::ISO_8859_1 } else { - StringUtils::guessCharset(&readBytes, hints) + StringUtils::guessCharset(&readBytes, hints).ok_or(Exceptions::ILLEGAL_STATE)? } } else { CharacterSetECI::getCharset( diff --git a/src/svg_luminance_source.rs b/src/svg_luminance_source.rs index 2a367a1..fda0196 100644 --- a/src/svg_luminance_source.rs +++ b/src/svg_luminance_source.rs @@ -22,34 +22,28 @@ impl LuminanceSource for SVGLuminanceSource { self.0.get_height() } - fn invert(&mut self) { - self.0.invert() - } - fn is_crop_supported(&self) -> bool { self.0.is_crop_supported() } - fn crop( - &self, - left: usize, - top: usize, - width: usize, - height: usize, - ) -> Result> { - self.0.crop(left, top, width, height) + fn crop(&self, left: usize, top: usize, width: usize, height: usize) -> Result { + self.0.crop(left, top, width, height).map(|src| Self(src)) } fn is_rotate_supported(&self) -> bool { self.0.is_rotate_supported() } - fn rotate_counter_clockwise(&self) -> Result> { - self.0.rotate_counter_clockwise() + fn invert(&mut self) { + self.0.invert() } - fn rotate_counter_clockwise_45(&self) -> Result> { - self.0.rotate_counter_clockwise_45() + fn rotate_counter_clockwise(&self) -> Result { + self.0.rotate_counter_clockwise().map(|src| Self(src)) + } + + fn rotate_counter_clockwise_45(&self) -> Result { + self.0.rotate_counter_clockwise_45().map(|src| Self(src)) } }