#![cfg_attr(not(feature = "std"), no_std)]
#![deny(missing_docs)]
extern crate alloc;
#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
use alloc::vec::Vec;
use core::fmt;
use qrcode_core::{EcLevel, Version};
#[cfg(feature = "rxing")]
mod engine;
#[cfg(feature = "rqrr")]
pub mod rqrr;
#[cfg(feature = "rxing")]
pub mod rxing;
#[cfg(feature = "rxing")]
pub use rxing::{ScanSymbol, StructuredAppendHeader};
pub mod sa_parse;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GrayPixelsError {
DimensionsOverflow,
BufferLengthMismatch {
expected: usize,
actual: usize,
},
}
impl fmt::Display for GrayPixelsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DimensionsOverflow => f.write_str("grayscale image dimensions exceed the supported buffer size"),
Self::BufferLengthMismatch { expected, actual } => {
write!(f, "grayscale buffer length mismatch: expected {expected} bytes, got {actual}")
}
}
}
}
impl ::core::error::Error for GrayPixelsError {}
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub struct GrayPixels<'a> {
width: u32,
height: u32,
data: &'a [u8],
}
impl<'a> GrayPixels<'a> {
#[must_use]
pub fn new(width: u32, height: u32, data: &'a [u8]) -> Self {
Self { width, height, data }
}
pub fn try_new(width: u32, height: u32, data: &'a [u8]) -> Result<Self, GrayPixelsError> {
let expected = u64::from(width)
.checked_mul(u64::from(height))
.and_then(|length| usize::try_from(length).ok())
.filter(|&length| length <= isize::MAX as usize)
.ok_or(GrayPixelsError::DimensionsOverflow)?;
if data.len() != expected {
return Err(GrayPixelsError::BufferLengthMismatch { expected, actual: data.len() });
}
Ok(Self::new(width, height, data))
}
#[must_use]
pub const fn width(&self) -> u32 {
self.width
}
#[must_use]
pub const fn height(&self) -> u32 {
self.height
}
#[must_use]
pub fn get(&self, x: u32, y: u32) -> u8 {
assert!(x < self.width && y < self.height, "grayscale pixel coordinates are out of bounds");
self.data[(y as usize) * (self.width as usize) + (x as usize)]
}
#[must_use]
pub fn try_get(&self, x: u32, y: u32) -> Option<u8> {
if x >= self.width || y >= self.height {
return None;
}
let width = usize::try_from(self.width).ok()?;
let x = usize::try_from(x).ok()?;
let y = usize::try_from(y).ok()?;
let index = y.checked_mul(width)?.checked_add(x)?;
self.data.get(index).copied()
}
}
#[cfg(feature = "image")]
impl<'a> From<&'a image::GrayImage> for GrayPixels<'a> {
fn from(img: &'a image::GrayImage) -> Self {
let (width, height) = (img.width(), img.height());
let logical_len = width as usize * height as usize;
Self::new(width, height, &img.as_raw()[..logical_len])
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecodedQrCode {
data: Vec<u8>,
version: Version,
ec_level: EcLevel,
}
impl DecodedQrCode {
#[must_use]
pub fn new(data: Vec<u8>, version: Version, ec_level: EcLevel) -> Self {
Self { data, version, ec_level }
}
#[must_use]
pub fn data(&self) -> &[u8] {
&self.data
}
#[must_use]
pub fn into_data(self) -> Vec<u8> {
self.data
}
#[must_use]
pub fn version(&self) -> Version {
self.version
}
#[must_use]
pub fn ec_level(&self) -> EcLevel {
self.ec_level
}
}
pub trait QrDecoder {
type Error;
fn decode(&self, image: GrayPixels<'_>) -> Result<Vec<DecodedQrCode>, Self::Error>;
}
#[cfg(test)]
mod tests {
use super::{DecodedQrCode, GrayPixels, GrayPixelsError};
use alloc::vec::Vec;
use qrcode_core::{EcLevel, Version};
#[test]
fn consuming_decoded_payload_transfers_the_binary_buffer_and_capacity() {
let mut payload = Vec::with_capacity(32);
payload.extend_from_slice(&[0x00, 0xff, 0x80, 0x11, 0x00]);
let pointer = payload.as_ptr();
let capacity = payload.capacity();
let decoded = DecodedQrCode::new(payload, Version::Normal(7), EcLevel::Q);
let bytes = decoded.into_data();
assert_eq!(bytes, [0x00, 0xff, 0x80, 0x11, 0x00]);
assert_eq!(bytes.as_ptr(), pointer);
assert_eq!(bytes.capacity(), capacity);
}
#[cfg(feature = "image")]
#[test]
fn gray_image_conversion_borrows_only_logical_pixels_from_padded_storage() {
let image = image::GrayImage::from_raw(2, 2, Vec::from([1, 2, 3, 4, 99, 100, 101])).unwrap();
let pixels = GrayPixels::from(&image);
assert_eq!((pixels.width(), pixels.height()), (2, 2));
assert_eq!(pixels.data, [1, 2, 3, 4]);
assert_eq!(pixels.data.as_ptr(), image.as_raw().as_ptr());
assert!(GrayPixels::try_new(pixels.width(), pixels.height(), pixels.data).is_ok());
assert_eq!(pixels.try_get(1, 1), Some(4));
}
#[cfg(feature = "image")]
#[test]
fn gray_image_conversion_borrows_an_empty_prefix_for_zero_area_padded_storage() {
for (width, height) in [(0, 0), (0, 7), (7, 0), (0, u32::MAX), (u32::MAX, 0)] {
let image = image::GrayImage::from_raw(width, height, Vec::from([99, 100, 101])).unwrap();
let pixels = GrayPixels::from(&image);
assert_eq!((pixels.width(), pixels.height()), (width, height));
assert!(pixels.data.is_empty());
assert_eq!(pixels.data.as_ptr(), image.as_raw().as_ptr());
assert!(GrayPixels::try_new(width, height, pixels.data).is_ok());
}
}
#[test]
fn grayscale_pixels_use_row_major_coordinates() {
let pixels = GrayPixels::new(2, 2, &[1, 2, 3, 4]);
assert_eq!(pixels.get(0, 0), 1);
assert_eq!(pixels.get(1, 0), 2);
assert_eq!(pixels.get(0, 1), 3);
assert_eq!(pixels.get(1, 1), 4);
}
#[test]
#[should_panic(expected = "grayscale pixel coordinates are out of bounds")]
fn grayscale_x_coordinate_cannot_alias_the_next_row() {
let _ = GrayPixels::new(2, 2, &[1, 2, 3, 4]).get(2, 0);
}
#[test]
#[should_panic(expected = "grayscale pixel coordinates are out of bounds")]
fn grayscale_y_coordinate_cannot_read_outside_the_image() {
let _ = GrayPixels::new(2, 2, &[1, 2, 3, 4]).get(0, 2);
}
#[test]
#[should_panic(expected = "grayscale pixel coordinates are out of bounds")]
fn empty_grayscale_image_has_no_valid_coordinates() {
let _ = GrayPixels::new(0, 0, &[]).get(0, 0);
}
#[test]
fn checked_grayscale_view_and_pixel_access_use_the_same_coordinates() {
let pixels = GrayPixels::try_new(2, 2, &[1, 2, 3, 4]).unwrap();
for y in 0..2 {
for x in 0..2 {
assert_eq!(pixels.try_get(x, y), Some(pixels.get(x, y)));
}
}
assert_eq!(pixels.try_get(2, 0), None);
assert_eq!(pixels.try_get(0, 2), None);
assert_eq!(pixels.try_get(u32::MAX, u32::MAX), None);
}
#[test]
fn checked_grayscale_view_rejects_short_and_long_buffers() {
for (data, actual) in [(&[1, 2, 3][..], 3), (&[1, 2, 3, 4, 5][..], 5)] {
assert!(matches!(
GrayPixels::try_new(2, 2, data),
Err(GrayPixelsError::BufferLengthMismatch { expected: 4, actual: length }) if length == actual
));
}
}
#[test]
fn checked_grayscale_view_accepts_empty_buffers_for_zero_area() {
for (width, height) in [(0, 0), (0, u32::MAX), (u32::MAX, 0)] {
let pixels = GrayPixels::try_new(width, height, &[]).unwrap();
assert_eq!(pixels.try_get(0, 0), None);
}
}
#[test]
fn checked_grayscale_view_rejects_unrepresentable_dimensions() {
assert!(matches!(GrayPixels::try_new(u32::MAX, u32::MAX, &[]), Err(GrayPixelsError::DimensionsOverflow)));
}
#[test]
fn checked_access_is_safe_for_an_unchecked_short_buffer() {
let pixels = GrayPixels::new(2, 2, &[7]);
assert_eq!(pixels.try_get(0, 0), Some(7));
assert_eq!(pixels.try_get(1, 0), None);
assert_eq!(pixels.try_get(0, 1), None);
assert_eq!(GrayPixels::new(u32::MAX, u32::MAX, &[]).try_get(u32::MAX - 1, u32::MAX - 1), None);
}
}