zedbar/image.rs
1//! Image handling and format support
2//!
3//! This module provides the [`Image`] type for holding barcode image data.
4//! Images must be in grayscale format (8-bit luminance).
5//!
6//! # Example
7//!
8//! ```
9//! use zedbar::{Image, Scanner};
10//!
11//! // Create image from grayscale data
12//! let width = 640;
13//! let height = 480;
14//! let data = vec![0u8; (width * height) as usize];
15//! let mut image = Image::from_gray(&data, width, height).unwrap();
16//!
17//! // Scan the image
18//! let mut scanner = Scanner::new();
19//! let symbols = scanner.scan(&mut image);
20//! ```
21
22use crate::image_ffi::zbar_image_t;
23use crate::{Error, Result};
24
25/// An image containing barcode data
26#[derive(Default)]
27pub struct Image {
28 image: zbar_image_t,
29}
30
31impl Image {
32 pub(crate) fn as_mut_image(&mut self) -> &mut zbar_image_t {
33 &mut self.image
34 }
35
36 /// Create an image from grayscale data
37 pub fn from_gray(data: &[u8], width: u32, height: u32) -> Result<Self> {
38 if (data.len() as u64) != (width as u64) * (height as u64) {
39 return Err(Error::Invalid);
40 }
41
42 let mut image = Self::default();
43 image.image.width = width;
44 image.image.height = height;
45 image.image.data.extend_from_slice(data);
46 Ok(image)
47 }
48
49 /// Get the image width
50 pub fn width(&self) -> u32 {
51 self.image.width
52 }
53
54 /// Get the image height
55 pub fn height(&self) -> u32 {
56 self.image.height
57 }
58
59 /// Get access to the raw image data
60 pub fn data(&self) -> &[u8] {
61 &self.image.data
62 }
63}