Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ members = [
resolver = "3"

[workspace.package]
version = "0.0.7"
version = "0.0.8"

[profile.release]
codegen-units = 1
Expand Down
6 changes: 3 additions & 3 deletions rxing-reader/src/binarizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ use crate::{LuminanceSource, common::BitMatrix};
pub trait Binarizer {
type Source: LuminanceSource;

fn get_luminance_source(&self) -> &Self::Source;
fn luminance_source(&self) -> &Self::Source;

/// Converts the image to a black/white matrix for QR detection.
/// Returns the image bits, where `true` means black.
/// Returns an error if the image cannot be binarized into a matrix.
fn get_black_matrix(&self) -> Result<&BitMatrix>;
fn black_matrix(&self) -> Result<&BitMatrix>;

fn get_black_matrix_mut(&mut self) -> Result<&mut BitMatrix>;
fn black_matrix_mut(&mut self) -> Result<&mut BitMatrix>;
}
12 changes: 6 additions & 6 deletions rxing-reader/src/binary_bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ impl<B: Binarizer> BinaryBitmap<B> {

/// Returns the mutable image matrix, where `true` means black.
/// Returns an error if the image cannot be binarized into a matrix.
pub fn get_black_matrix_mut(&mut self) -> Result<&mut BitMatrix> {
self.binarizer.get_black_matrix_mut()
pub fn black_matrix_mut(&mut self) -> Result<&mut BitMatrix> {
self.binarizer.black_matrix_mut()
}

/// Converts the image to a black/white matrix for QR detection.
/// Returns the image matrix, where `true` means black.
/// Returns an error if the image cannot be binarized into a matrix.
pub fn get_black_matrix(&self) -> Result<&BitMatrix> {
self.binarizer.get_black_matrix()
pub fn black_matrix(&self) -> Result<&BitMatrix> {
self.binarizer.black_matrix()
}

/// Apply a 3×3 morphological close to the cached BitMatrix: dilate (set
Expand All @@ -52,7 +52,7 @@ impl<B: Binarizer> BinaryBitmap<B> {
/// photos (cf. zxing-cpp's `tryDenoise`). The 1-pixel border is left
/// unchanged. No-op if the matrix is smaller than 3×3.
pub fn close(&mut self) -> Result<()> {
let matrix = self.get_black_matrix_mut()?;
let matrix = self.black_matrix_mut()?;
let (w, h) = (matrix.width(), matrix.height());
if w < 3 || h < 3 {
return Ok(());
Expand Down Expand Up @@ -84,7 +84,7 @@ fn sum_filter_3x3<F: Fn(u8) -> bool>(input: &BitMatrix, output: &mut BitMatrix,

impl<B: Binarizer> fmt::Display for BinaryBitmap<B> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.binarizer.get_black_matrix() {
match self.binarizer.black_matrix() {
Ok(matrix) => write!(f, "{matrix:?}"),
Err(_) => write!(f, "<unavailable>"),
}
Expand Down
32 changes: 19 additions & 13 deletions rxing-reader/src/common/bit_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ impl BitArray {
}
}

pub fn get_size(&self) -> usize {
#[must_use]
pub fn len(&self) -> usize {
self.bits.len()
}

pub fn get_size_in_bytes(&self) -> usize {
#[must_use]
pub fn is_empty(&self) -> bool {
self.bits.is_empty()
}

pub fn byte_len(&self) -> usize {
self.bits.len().div_ceil(8)
}

Expand All @@ -90,7 +96,7 @@ impl BitArray {
self.bits.set(i, !v);
}

pub fn get_next_set(&self, from: usize) -> usize {
pub fn next_set(&self, from: usize) -> usize {
let size = self.bits.len();
if from >= size {
return size;
Expand All @@ -101,7 +107,7 @@ impl BitArray {
.unwrap_or(size)
}

pub fn get_next_unset(&self, from: usize) -> usize {
pub fn next_unset(&self, from: usize) -> usize {
let size = self.bits.len();
if from >= size {
return size;
Expand All @@ -124,7 +130,7 @@ impl BitArray {
return Err(Error::InvalidArgument {
message: format!(
"set_range: start={start}, end={end} out of range for bit array of length {len}"
),
).into(),
}
.into());
}
Expand All @@ -145,7 +151,7 @@ impl BitArray {
return Err(Error::InvalidArgument {
message: format!(
"is_range: start={start}, end={end} out of range for bit array of length {len}"
),
).into(),
}
.into());
}
Expand All @@ -165,7 +171,7 @@ impl BitArray {
pub fn append_bits(&mut self, value: BaseType, num_bits: usize) -> Result<()> {
if num_bits > BASE_BITS {
return Err(Error::InvalidArgument {
message: format!("num bits must be between 0 and {}", BaseType::BITS),
message: format!("num bits must be between 0 and {}", BaseType::BITS).into(),
}
.into());
}
Expand All @@ -190,7 +196,7 @@ impl BitArray {
"xor: bit array sizes differ (self={}, other={})",
self.bits.len(),
other.bits.len(),
),
).into(),
}
.into());
}
Expand All @@ -214,7 +220,7 @@ impl BitArray {
}
}

pub fn get_bit_array(&self) -> &[BaseType] {
pub fn words(&self) -> &[BaseType] {
self.bits.as_raw_slice()
}

Expand Down Expand Up @@ -251,8 +257,8 @@ impl From<BitArray> for Vec<u8> {

impl From<&BitArray> for Vec<u8> {
fn from(value: &BitArray) -> Self {
let mut array = vec![0; value.get_size_in_bytes()];
value.to_bytes(0, &mut array, 0, value.get_size_in_bytes());
let mut array = vec![0; value.byte_len()];
value.to_bytes(0, &mut array, 0, value.byte_len());
array
}
}
Expand Down Expand Up @@ -358,7 +364,7 @@ mod tests {
let bytes = [0b1000_0001, 0b0101_1010, 0];
let bits = BitArray::from(bytes.to_vec());

assert_eq!(bits.get_size(), bytes.len() * 8);
assert_eq!(bits.len(), bytes.len() * 8);
assert_eq!(Vec::<u8>::from(bits), bytes);
}

Expand All @@ -368,6 +374,6 @@ mod tests {

bits.set_bulk(5, 0b101);

assert_eq!(bits.get_bit_array()[0], 0b101);
assert_eq!(bits.words()[0], 0b101);
}
}
Loading
Loading