Skip to content

Commit

Permalink
fix: remove mut requirement for BinaryBitmat::get_black_matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimke committed Mar 23, 2024
1 parent 01f2803 commit dc101e0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
49 changes: 35 additions & 14 deletions src/binary_bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

//package com.google.zxing;

use std::{borrow::Cow, fmt};
use std::{
borrow::{Borrow, Cow},
fmt,
};

use once_cell::sync::OnceCell;

use crate::{
common::{BitArray, BitMatrix, LineOrientation, Result},
Expand All @@ -32,13 +37,13 @@ use crate::{

pub struct BinaryBitmap<B: Binarizer> {
binarizer: B,
pub(crate) matrix: Option<BitMatrix>,
pub(crate) matrix: OnceCell<BitMatrix>,
}

impl<B: Binarizer> BinaryBitmap<B> {
pub fn new(binarizer: B) -> Self {
Self {
matrix: None,
matrix: OnceCell::new(),
binarizer,
}
}
Expand Down Expand Up @@ -94,10 +99,19 @@ impl<B: Binarizer> BinaryBitmap<B> {
// 1. This work will never be done if the caller only installs 1D Reader objects, or if a
// 1D Reader finds a barcode before the 2D Readers run.
// 2. This work will only be done once even if the caller installs multiple 2D Readers.
if self.matrix.is_none() {
self.matrix = Some(self.binarizer.get_black_matrix().unwrap().clone());
}
self.matrix.as_mut().unwrap()
// if self.matrix.borrow().is_none() {
// _=self.matrix.replace(Some(self.binarizer.get_black_matrix().unwrap().clone()));
// // self.matrix.get_mut() = ;
// }
// &mut self.matrix.get_mut().unwrap()
self.matrix
.get_or_init(|| match self.binarizer.get_black_matrix() {
Ok(a) => a.clone(),
Err(_) => {
BitMatrix::new(self.get_width() as u32, self.get_height() as u32).unwrap()
}
});
self.matrix.get_mut().unwrap()
}

/**
Expand All @@ -111,22 +125,29 @@ impl<B: Binarizer> BinaryBitmap<B> {
* @return The 2D array of bits for the image (true means black).
* @throws NotFoundException if image can't be binarized to make a matrix
*/
pub fn get_black_matrix(&mut self) -> &BitMatrix {
pub fn get_black_matrix(&self) -> &BitMatrix {
// The matrix is created on demand the first time it is requested, then cached. There are two
// reasons for this:
// 1. This work will never be done if the caller only installs 1D Reader objects, or if a
// 1D Reader finds a barcode before the 2D Readers run.
// 2. This work will only be done once even if the caller installs multiple 2D Readers.
if self.matrix.is_none() {
self.matrix = Some(match self.binarizer.get_black_matrix() {
// if self.matrix.borrow().is_none() {
// _= self.matrix.replace(Some(match self.binarizer.get_black_matrix() {
// Ok(a) => a.clone(),
// Err(_) => {
// BitMatrix::new(self.get_width() as u32, self.get_height() as u32).unwrap()
// }
// }));
// // self.binarizer.get_black_matrix().unwrap_or_else( |_| BitMatrix::new(self.get_width() as u32, self.get_height() as u32).unwrap()).clone())
// }
// &self.matrix.borrow().as_ref().unwrap()
self.matrix
.get_or_init(|| match self.binarizer.get_black_matrix() {
Ok(a) => a.clone(),
Err(_) => {
BitMatrix::new(self.get_width() as u32, self.get_height() as u32).unwrap()
}
})
// self.binarizer.get_black_matrix().unwrap_or_else( |_| BitMatrix::new(self.get_width() as u32, self.get_height() as u32).unwrap()).clone())
}
self.matrix.as_ref().unwrap()
}

/**
Expand Down Expand Up @@ -215,6 +236,6 @@ impl<B: Binarizer> BinaryBitmap<B> {

impl<B: Binarizer> fmt::Display for BinaryBitmap<B> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.matrix)
write!(f, "{:?}", self.matrix.borrow())
}
}
2 changes: 1 addition & 1 deletion src/filtered_image_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl LumImagePyramid {

impl<B: Binarizer> BinaryBitmap<B> {
pub fn close(&mut self) -> Result<()> {
if let Some(matrix) = self.matrix.as_mut() {
if let Some(matrix) = self.matrix.get_mut() {
let mut tmp = BitMatrix::new(matrix.width(), matrix.height())?;
// dilate
SumFilter(matrix, &mut tmp, |sum| sum > 0);
Expand Down

0 comments on commit dc101e0

Please sign in to comment.