wip: inop continued dev

This commit is contained in:
Henry Schimke
2024-02-01 14:14:41 -06:00
parent fbd48b5cfc
commit c193c96e7f
4 changed files with 55 additions and 44 deletions

View File

@@ -197,6 +197,14 @@ impl<B: Binarizer> BinaryBitmap<B> {
.create_binarizer(newSource.expect("new lum source expected")), .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<B: Binarizer> fmt::Display for BinaryBitmap<B> { impl<B: Binarizer> fmt::Display for BinaryBitmap<B> {

View File

@@ -2,62 +2,52 @@ use std::collections::HashMap;
use crate::{Binarizer, BinaryBitmap, Exceptions, Luma8LuminanceSource, LuminanceSource, Reader}; 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_THRESHHOLD: usize = 500;
pub const DEFAULT_DOWNSCALE_FACTOR: usize = 3; pub const DEFAULT_DOWNSCALE_FACTOR: usize = 3;
/// Passed image data is ignored, only the image data /// Passed image data is ignored, only the image data
pub struct FilteredImageReader<R: Reader, B: Binarizer<Source = Luma8LuminanceSource>> { pub struct FilteredImageReader<R: Reader>(R);
reader: R,
source: Luma8LuminanceSource,
binarizer: B,
}
impl<R: Reader, B: Binarizer<Source = Luma8LuminanceSource>> FilteredImageReader<R, B> { impl<R: Reader> FilteredImageReader<R> {
pub fn new<I: LuminanceSource + Clone + Into<Luma8LuminanceSource>>( pub fn new(reader: R) -> Self {
reader: R, Self(reader)
source: I,
binarizer: B,
) -> Self {
Self {
reader,
source: source.into(),
binarizer,
}
} }
} }
impl<R: Reader, B1: Binarizer<Source = Luma8LuminanceSource>> Reader impl<R: Reader> Reader for FilteredImageReader<R> {
for FilteredImageReader<R, B1>
{
fn decode<B: crate::Binarizer>( fn decode<B: crate::Binarizer>(
&mut self, &mut self,
_image: &mut crate::BinaryBitmap<B>, image: &mut crate::BinaryBitmap<B>,
) -> crate::common::Result<crate::RXingResult> { ) -> crate::common::Result<crate::RXingResult> {
self.decode_with_hints(_image, &HashMap::default()) self.decode_with_hints(image, &HashMap::default())
} }
fn decode_with_hints<B: crate::Binarizer>( fn decode_with_hints<B: crate::Binarizer>(
&mut self, &mut self,
_image: &mut crate::BinaryBitmap<B>, image: &mut crate::BinaryBitmap<B>,
hints: &crate::DecodingHintDictionary, hints: &crate::DecodingHintDictionary,
) -> crate::common::Result<crate::RXingResult> { ) -> crate::common::Result<crate::RXingResult> {
let pyramids = LumImagePyramid::new( 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_THRESHHOLD,
DEFAULT_DOWNSCALE_FACTOR, DEFAULT_DOWNSCALE_FACTOR,
) )
.ok_or(Exceptions::ILLEGAL_ARGUMENT)?; .ok_or(Exceptions::ILLEGAL_ARGUMENT)?;
for layer in pyramids.layers { 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] { for close in [false, true] {
if close { if close {
let Ok(_) = b.close() else { let Ok(_) = b.close() else {
continue; 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); return Ok(res);
} else { } else {
continue; continue;
@@ -135,15 +125,15 @@ impl LumImagePyramid {
// for (int dy = 0; dy < div.height(); ++dy){ // for (int dy = 0; dy < div.height(); ++dy){
for dx in 0..div_width { for dx in 0..div_width {
// for (int dx = 0; dx < div.width(); ++dx) { // 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 ty in 0..N {
// for (int ty = 0; ty < N; ++ty){ // for (int ty = 0; ty < N; ++ty){
for tx in 0..N { for tx in 0..N {
// for (int tx = 0; tx < N; ++tx) { // 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;
} }
} }
} }

View File

@@ -17,15 +17,29 @@ pub struct Luma8LuminanceSource {
} }
impl LuminanceSource for Luma8LuminanceSource { impl LuminanceSource for Luma8LuminanceSource {
fn get_row(&self, y: usize) -> Vec<u8> { fn get_row(&self, y: usize) -> Vec<u8> {
self.data let chunk_size = self.original_dimension.0 as usize;
.chunks_exact(self.original_dimension.0 as usize) let row_skip = y + self.origin.1 as usize;
.skip(y + self.origin.1 as usize) let column_skip = self.origin.0 as usize;
.take(1) let column_take = self.dimensions.0 as usize;
.flatten()
.skip(self.origin.0 as usize) let data_start = (chunk_size * row_skip) + column_skip;
.take(self.dimensions.0 as usize) let data_end = (chunk_size * row_skip) + column_skip + column_take;
.map(|byte| Self::invert_if_should(*byte, self.inverted))
.collect() 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<u8> { fn get_column(&self, x: usize) -> Vec<u8> {
@@ -108,8 +122,8 @@ impl LuminanceSource for Luma8LuminanceSource {
)) ))
} }
fn get_luma8_point(&self, x: usize, y: usize) -> u8 { fn get_luma8_point(&self, column: usize, row: usize) -> u8 {
todo!() self.get_row(row)[column]
} }
} }
@@ -127,7 +141,6 @@ impl Luma8LuminanceSource {
b -= 1; b -= 1;
} }
} }
// print_matrix(&self.data, self.get_width(), self.get_height());
} }
fn transpose_square(&mut self) { fn transpose_square(&mut self) {

View File

@@ -2,19 +2,19 @@
#![cfg(feature = "image")] #![cfg(feature = "image")]
use rxing::{BarcodeFormat, MultiFormatReader}; use rxing::{BarcodeFormat, FilteredImageReader, MultiFormatReader};
mod common; mod common;
/** /**
* @author Sean Owen * @author Sean Owen
*/ */
#[cfg(feature = "image-formats")] #[cfg(feature = "image_formats")]
#[test] #[test]
fn dx_film_edge() { fn dx_film_edge() {
let mut tester = common::AbstractBlackBoxTestCase::new( let mut tester = common::AbstractBlackBoxTestCase::new(
"test_resources/blackbox/dxfilmedge-1", "test_resources/blackbox/dxfilmedge-1",
MultiFormatReader::default(), FilteredImageReader::new(MultiFormatReader::default()),
BarcodeFormat::DXFilmEdge, BarcodeFormat::DXFilmEdge,
); );