diff --git a/src/binary_bitmap.rs b/src/binary_bitmap.rs index 3f4189e..04a8b61 100644 --- a/src/binary_bitmap.rs +++ b/src/binary_bitmap.rs @@ -197,6 +197,14 @@ impl BinaryBitmap { .create_binarizer(newSource.expect("new lum source expected")), ) } + + pub fn get_source(&self) -> &B::Source { + &self.binarizer.get_luminance_source() + } + + pub fn get_binarizer(&self) -> &B { + &self.binarizer + } } impl fmt::Display for BinaryBitmap { diff --git a/src/filtered_image_reader.rs b/src/filtered_image_reader.rs index 421f042..c4532bf 100644 --- a/src/filtered_image_reader.rs +++ b/src/filtered_image_reader.rs @@ -2,62 +2,52 @@ use std::collections::HashMap; use crate::{Binarizer, BinaryBitmap, Exceptions, Luma8LuminanceSource, LuminanceSource, Reader}; -use crate::common::{BitMatrix, Result}; +use crate::common::{BitMatrix, HybridBinarizer, Result}; pub const DEFAULT_DOWNSCALE_THRESHHOLD: usize = 500; pub const DEFAULT_DOWNSCALE_FACTOR: usize = 3; /// Passed image data is ignored, only the image data -pub struct FilteredImageReader> { - reader: R, - source: Luma8LuminanceSource, - binarizer: B, -} +pub struct FilteredImageReader(R); -impl> FilteredImageReader { - pub fn new>( - reader: R, - source: I, - binarizer: B, - ) -> Self { - Self { - reader, - source: source.into(), - binarizer, - } +impl FilteredImageReader { + pub fn new(reader: R) -> Self { + Self(reader) } } -impl> Reader - for FilteredImageReader -{ +impl Reader for FilteredImageReader { fn decode( &mut self, - _image: &mut crate::BinaryBitmap, + image: &mut crate::BinaryBitmap, ) -> crate::common::Result { - self.decode_with_hints(_image, &HashMap::default()) + self.decode_with_hints(image, &HashMap::default()) } fn decode_with_hints( &mut self, - _image: &mut crate::BinaryBitmap, + image: &mut crate::BinaryBitmap, hints: &crate::DecodingHintDictionary, ) -> crate::common::Result { let pyramids = LumImagePyramid::new( - self.source.clone(), + Luma8LuminanceSource::new( + image.get_source().get_matrix(), + image.get_source().get_width() as u32, + image.get_source().get_height() as u32, + ), DEFAULT_DOWNSCALE_THRESHHOLD, DEFAULT_DOWNSCALE_FACTOR, ) .ok_or(Exceptions::ILLEGAL_ARGUMENT)?; for layer in pyramids.layers { - let mut b = BinaryBitmap::new(self.binarizer.create_binarizer(layer)); + let mut b = BinaryBitmap::new(HybridBinarizer::new(layer)); for close in [false, true] { if close { let Ok(_) = b.close() else { continue; }; } - if let Ok(res) = self.reader.decode_with_hints(&mut b, hints) { + if let Ok(res) = self.0.decode_with_hints(&mut b, hints) { return Ok(res); } else { continue; @@ -135,15 +125,15 @@ impl LumImagePyramid { // for (int dy = 0; dy < div.height(); ++dy){ for dx in 0..div_width { // for (int dx = 0; dx < div.width(); ++dx) { - let mut sum = (N * N) as u8 / 2; + let mut sum = (N * N) / 2; for ty in 0..N { // for (int ty = 0; ty < N; ++ty){ for tx in 0..N { // for (int tx = 0; tx < N; ++tx) { - sum += siv.get_luma8_point(dx * N + tx, dy * N + ty); + sum += siv.get_luma8_point(dx * N + tx, dy * N + ty) as usize; } } - *d = sum / (N * N) as u8; + *d = (sum / (N * N)) as u8; } } } diff --git a/src/luma_luma_source.rs b/src/luma_luma_source.rs index f81a59b..5bff37a 100644 --- a/src/luma_luma_source.rs +++ b/src/luma_luma_source.rs @@ -17,15 +17,29 @@ pub struct Luma8LuminanceSource { } impl LuminanceSource for Luma8LuminanceSource { fn get_row(&self, y: usize) -> Vec { - self.data - .chunks_exact(self.original_dimension.0 as usize) - .skip(y + self.origin.1 as usize) - .take(1) - .flatten() - .skip(self.origin.0 as usize) - .take(self.dimensions.0 as usize) - .map(|byte| Self::invert_if_should(*byte, self.inverted)) - .collect() + let chunk_size = self.original_dimension.0 as usize; + let row_skip = y + self.origin.1 as usize; + let column_skip = self.origin.0 as usize; + let column_take = self.dimensions.0 as usize; + + let data_start = (chunk_size * row_skip) + column_skip; + let data_end = (chunk_size * row_skip) + column_skip + column_take; + + if self.inverted { + self.invert_block_of_bytes(Vec::from(&self.data[data_start..data_end])) + } else { + Vec::from(&self.data[data_start..data_end]) + } + + // self.data + // .chunks_exact(chunk_size) + // .skip(row_skip) + // .take(1) + // .flatten() + // .skip(column_skip) + // .take(column_take) + // .map(|byte| Self::invert_if_should(*byte, self.inverted)) + // .collect() } fn get_column(&self, x: usize) -> Vec { @@ -108,8 +122,8 @@ impl LuminanceSource for Luma8LuminanceSource { )) } - fn get_luma8_point(&self, x: usize, y: usize) -> u8 { - todo!() + fn get_luma8_point(&self, column: usize, row: usize) -> u8 { + self.get_row(row)[column] } } @@ -127,7 +141,6 @@ impl Luma8LuminanceSource { b -= 1; } } - // print_matrix(&self.data, self.get_width(), self.get_height()); } fn transpose_square(&mut self) { diff --git a/tests/dx_film_edge.rs b/tests/dx_film_edge.rs index e0d6037..5524ed2 100644 --- a/tests/dx_film_edge.rs +++ b/tests/dx_film_edge.rs @@ -2,19 +2,19 @@ #![cfg(feature = "image")] -use rxing::{BarcodeFormat, MultiFormatReader}; +use rxing::{BarcodeFormat, FilteredImageReader, MultiFormatReader}; mod common; /** * @author Sean Owen */ -#[cfg(feature = "image-formats")] +#[cfg(feature = "image_formats")] #[test] fn dx_film_edge() { let mut tester = common::AbstractBlackBoxTestCase::new( "test_resources/blackbox/dxfilmedge-1", - MultiFormatReader::default(), + FilteredImageReader::new(MultiFormatReader::default()), BarcodeFormat::DXFilmEdge, );