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.10"
version = "0.0.11"

[profile.release]
codegen-units = 1
Expand Down
2 changes: 1 addition & 1 deletion rxing-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn decode_image_bytes(bytes: &[u8]) -> Result<(Vec<u8>, usize, usize)> {
}

fn decode_symbols(rgba: &[u8], w: usize, h: usize, max: usize) -> Result<Vec<QrSymbol>> {
let luma = rgba_to_luma(rgba, w, h).map_err(anyhow::Error::msg)?;
let luma = rgba_to_luma(rgba, w, h).context("converting RGBA pixels to luma")?;
let primary = decode_qr_codes_luma(&luma, w, h, true, true, true, max)?;
if !primary.is_empty() {
return Ok(primary);
Expand Down
49 changes: 21 additions & 28 deletions rxing-reader/src/common/bit_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use std::fmt;

use bitvec::prelude::*;

use anyhow::{Result, ensure};

use crate::Error;
use anyhow::Result;

type BaseType = super::BitFieldBaseType;
const BASE_BITS: usize = super::BIT_FIELD_BASE_BITS;
Expand Down Expand Up @@ -126,14 +127,12 @@ impl BitArray {

pub fn set_range(&mut self, start: usize, end: usize) -> Result<()> {
let len = self.bits.len();
if end < start || end > len {
return Err(Error::InvalidArgument {
message: format!(
ensure!(
end >= start && end <= len,
Error::invalid_argument(format!(
"set_range: start={start}, end={end} out of range for bit array of length {len}"
).into(),
}
.into());
}
))
);
if end == start {
return Ok(());
}
Expand All @@ -147,14 +146,12 @@ impl BitArray {

pub fn is_range(&self, start: usize, end: usize, value: bool) -> Result<bool> {
let len = self.bits.len();
if end < start || end > len {
return Err(Error::InvalidArgument {
message: format!(
ensure!(
end >= start && end <= len,
Error::invalid_argument(format!(
"is_range: start={start}, end={end} out of range for bit array of length {len}"
).into(),
}
.into());
}
))
);
if end == start {
return Ok(true);
}
Expand All @@ -169,12 +166,10 @@ impl BitArray {
/// Appends the least-significant `num_bits` of `value`, from most-significant to
/// least-significant. For example, appending 6 bits from 0x1E appends 0,1,1,1,1,0.
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).into(),
}
.into());
}
ensure!(
num_bits <= BASE_BITS,
Error::invalid_argument(format!("num bits must be between 0 and {}", BaseType::BITS))
);
for i in (0..num_bits).rev() {
self.bits.push((value >> i) & 1 != 0);
}
Expand All @@ -190,16 +185,14 @@ impl BitArray {
}

pub fn xor(&mut self, other: &BitArray) -> Result<()> {
if self.bits.len() != other.bits.len() {
return Err(Error::InvalidArgument {
message: format!(
ensure!(
self.bits.len() == other.bits.len(),
Error::invalid_argument(format!(
"xor: bit array sizes differ (self={}, other={})",
self.bits.len(),
other.bits.len(),
).into(),
}
.into());
}
))
);
self.bits ^= &other.bits;
Ok(())
}
Expand Down
122 changes: 48 additions & 74 deletions rxing-reader/src/common/bit_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

use std::fmt;

use anyhow::{Context, Result, bail, ensure};

use crate::{Error, Point, point};
use anyhow::Result;

use super::BitArray;

Expand Down Expand Up @@ -56,14 +57,12 @@ impl BitMatrix {
/// - `width`: bit matrix width
/// - `height`: bit matrix height
pub fn new(width: usize, height: usize) -> Result<Self> {
if width < 1 || height < 1 {
return Err(Error::InvalidArgument {
message: format!(
ensure!(
width >= 1 && height >= 1,
Error::invalid_argument(format!(
"BitMatrix::new: both dimensions must be greater than 0 (got width={width}, height={height})"
).into(),
}
.into());
}
))
);
Ok(Self {
width,
height,
Expand Down Expand Up @@ -146,12 +145,9 @@ impl BitMatrix {
row_length = bits_pos - row_start_pos;
} else if bits_pos - row_start_pos != row_length {
let this_row_length = bits_pos - row_start_pos;
return Err(Error::InvalidArgument {
message: format!(
bail!(Error::invalid_argument(format!(
"parse_strings: row lengths do not match (row {n_rows} has {this_row_length} cells, expected {row_length})"
).into(),
}
.into());
)));
}
row_start_pos = bits_pos;
n_rows += 1;
Expand All @@ -166,13 +162,10 @@ impl BitMatrix {
bits[bits_pos] = false;
bits_pos += 1;
} else {
return Err(Error::InvalidArgument {
message: format!(
bail!(Error::invalid_argument(format!(
"illegal character encountered: {}",
&string_representation[pos..]
).into(),
}
.into());
)));
}
}

Expand All @@ -182,17 +175,14 @@ impl BitMatrix {
row_length = bits_pos - row_start_pos;
} else if bits_pos - row_start_pos != row_length {
let this_row_length = bits_pos - row_start_pos;
return Err(Error::InvalidArgument {
message: format!(
bail!(Error::invalid_argument(format!(
"parse_strings: final row lengths do not match (row {n_rows} has {this_row_length} cells, expected {row_length})"
).into(),
}
.into());
)));
}
n_rows += 1;
}

let mut matrix = BitMatrix::new(row_length, n_rows)?;
let mut matrix = BitMatrix::new(row_length, n_rows).context("building parsed bit matrix")?;
for (i, bit) in bits.iter().enumerate().take(bits_pos) {
if *bit {
matrix.set(i % row_length, i / row_length);
Expand Down Expand Up @@ -330,17 +320,14 @@ impl BitMatrix {
///
/// - `mask`: XOR mask
pub fn xor(&mut self, mask: &BitMatrix) -> Result<()> {
if self.width != mask.width || self.height != mask.height || self.row_size != mask.row_size
{
return Err(Error::InvalidArgument {
message: format!(
ensure!(
self.width == mask.width && self.height == mask.height && self.row_size == mask.row_size,
Error::invalid_argument(format!(
"BitMatrix::xor: input matrix dimensions do not match (self={}x{} row_size={}, mask={}x{} row_size={})",
self.width, self.height, self.row_size,
mask.width, mask.height, mask.row_size,
).into(),
}
.into());
}
))
);
for y in 0..self.height {
let offset = y * self.row_size;
let row_array = mask.row(y);
Expand All @@ -365,33 +352,29 @@ impl BitMatrix {
/// - `width`: The width of the region
/// - `height`: The height of the region
pub fn set_region(&mut self, left: usize, top: usize, width: usize, height: usize) -> Result<()> {
if height < 1 || width < 1 {
return Err(Error::InvalidArgument {
message: format!(
ensure!(
height >= 1 && width >= 1,
Error::invalid_argument(format!(
"set_region: width and height must be at least 1 (got width={width}, height={height})"
).into(),
}
.into());
}
let right = left.checked_add(width).ok_or_else(|| Error::InvalidArgument {
message: format!(
))
);
let right = left.checked_add(width).with_context(|| {
Error::invalid_argument(format!(
"set_region: left + width overflowed (left={left}, width={width})"
).into(),
))
})?;
let bottom = top.checked_add(height).ok_or_else(|| Error::InvalidArgument {
message: format!(
let bottom = top.checked_add(height).with_context(|| {
Error::invalid_argument(format!(
"set_region: top + height overflowed (top={top}, height={height})"
).into(),
))
})?;
if bottom > self.height || right > self.width {
return Err(Error::InvalidArgument {
message: format!(
ensure!(
bottom <= self.height && right <= self.width,
Error::invalid_argument(format!(
"set_region: region (left={left}, top={top}, width={width}, height={height} -> right={right}, bottom={bottom}) does not fit inside matrix {}x{}",
self.width, self.height,
).into(),
}
.into());
}
))
);
for y in top..bottom {
let offset = y * self.row_size;
for x in left..right {
Expand Down Expand Up @@ -457,12 +440,9 @@ impl BitMatrix {
self.rotate180();
Ok(())
}
other => Err(Error::InvalidArgument {
message: format!(
other => bail!(Error::invalid_argument(format!(
"rotate: degrees must be 0, 90, 180, or 270 (got {degrees}, normalized to {other})"
).into(),
}
.into()),
))),
}
}

Expand Down Expand Up @@ -621,26 +601,20 @@ impl BitMatrix {
}

pub fn crop(&self, top: usize, left: usize, height: usize, width: usize) -> Result<BitMatrix> {
if width == 0 || height == 0 {
return Err(Error::InvalidArgument {
message: format!(
ensure!(
width != 0 && height != 0,
Error::invalid_argument(format!(
"crop: width and height must be greater than 0 (got width={width}, height={height})"
).into(),
}
.into());
}
if left.saturating_add(width) > self.width
|| top.saturating_add(height) > self.height
{
return Err(Error::InvalidArgument {
message: format!(
))
);
ensure!(
left.saturating_add(width) <= self.width && top.saturating_add(height) <= self.height,
Error::invalid_argument(format!(
"crop: region (left={left}, top={top}, width={width}, height={height}) does not fit inside matrix {}x{}",
self.width, self.height,
).into(),
}
.into());
}
let mut new_bm = BitMatrix::new(width, height)?;
))
);
let mut new_bm = BitMatrix::new(width, height).context("building cropped bit matrix")?;
for y in top..top + height {
for x in left..left + width {
if self.get(x, y) {
Expand Down
13 changes: 4 additions & 9 deletions rxing-reader/src/common/bit_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
use std::cmp;
use std::io::{ErrorKind, Read};

use anyhow::{Result, bail};

use crate::Error;
use anyhow::Result;

/// This provides an easy abstraction to read bits at a time from a sequence of bytes, where the
/// number of bits read is not often a multiple of 8.
Expand Down Expand Up @@ -55,10 +56,7 @@ impl<'a> BitSource<'a> {
if num_bits > available_bits {
message.push_str(&format!("; available bits: {available_bits}"));
}
return Err(Error::InvalidArgument {
message: message.into(),
}
.into());
bail!(Error::invalid_argument(message));
}

let mut result: u32 = 0;
Expand Down Expand Up @@ -109,10 +107,7 @@ impl<'a> BitSource<'a> {
if num_bits > available_bits {
message.push_str(&format!("; available bits: {available_bits}"));
}
return Err(Error::InvalidArgument {
message: message.into(),
}
.into());
bail!(Error::invalid_argument(message));
}

let mut bit_offset = self.bit_offset;
Expand Down
Loading
Loading