Skip to main content

rqrr/
lib.rs

1//! Find and read QR-Codes
2//!
3//! This crates exports functions and types that can be used to search for
4//! QR-Codes in images and decode them.
5//!
6//!
7#![cfg_attr(
8    feature = "img",
9    doc = r##"
10# Usage
11The most basic usage is shown below:
12
13```rust
14use image;
15
16# fn main() -> Result<(), Box<dyn ::std::error::Error>> {
17// Load on image to search, convert it to grayscale
18let img = image::open("tests/data/github.gif")?.to_luma8();
19// Prepare for detection
20let mut img = rqrr::PreparedImage::prepare(img);
21// Search for grids, without decoding
22let grids = img.detect_grids();
23assert_eq!(grids.len(), 1);
24// Decode the grid
25let (meta, content) = grids[0].decode()?;
26assert_eq!(meta.ecc_level, 0);
27assert_eq!(content, "https://github.com/WanzenBug/rqrr");
28# Ok(())
29# }
30```
31
32If you have some other form of picture storage, you can use
33[`PreparedImage::prepare_from_*`](struct.PreparedImage.html). This allows
34you to define your own source for images.
35"##
36)]
37pub use self::decode::{MetaData, RawData, Version, MAX_PAYLOAD_SIZE};
38pub(crate) use self::detect::{capstones_from_image, CapStone};
39pub use self::identify::Point;
40pub(crate) use self::identify::SkewedGridLocation;
41pub use self::prepare::PreparedImage;
42use std::error::Error;
43use std::io::Write;
44
45mod decode;
46mod detect;
47pub(crate) mod geometry;
48mod identify;
49mod prepare;
50mod version_db;
51
52/// Wrapper around any grid that can be interpreted as a QR code
53#[derive(Debug, Clone)]
54pub struct Grid<G> {
55    /// The backing binary square
56    pub grid: G,
57    /// The bounds of the square, in underlying coordinates.
58    ///
59    /// The points are listed in the following order:
60    /// [top-left, top-right, bottom-right, bottom-left]
61    ///
62    /// If this grid references for example an underlying image, these values
63    /// will be set to coordinates in that image.
64    pub bounds: [Point; 4],
65}
66
67impl<G> Grid<G>
68where
69    G: BitGrid,
70{
71    /// Create a new grid from a BitGrid.
72    ///
73    /// This just initialises the bounds to 0.
74    pub fn new(grid: G) -> Self {
75        Grid {
76            grid,
77            bounds: [
78                Point { x: 0, y: 0 },
79                Point { x: 0, y: 0 },
80                Point { x: 0, y: 0 },
81                Point { x: 0, y: 0 },
82            ],
83        }
84    }
85
86    /// Try to decode the grid.
87    ///
88    /// If successful returns the decoded string as well as metadata about the
89    /// code.
90    pub fn decode(&self) -> DeQRResult<(MetaData, String)> {
91        let mut out = Vec::new();
92        let meta = self.decode_to(&mut out)?;
93        let out = String::from_utf8(out)?;
94        Ok((meta, out))
95    }
96
97    /// Try to read metadata, and return the raw, uncorrected bit stream.
98    ///
99    /// If successful, returns the metadata along with the raw bit pattern.
100    /// The raw data is still masked, so bits appear as in the source image.
101    pub fn get_raw_data(&self) -> DeQRResult<(MetaData, RawData)> {
102        decode::get_raw(&self.grid, false)
103    }
104
105    /// Try to decode the grid.
106    ///
107    /// Instead of returning a String, this method writes the decoded result to
108    /// the given writer
109    ///
110    /// **Warning**: This may lead to half decoded content to be written to the
111    /// writer.
112    pub fn decode_to<W>(&self, writer: W) -> DeQRResult<MetaData>
113    where
114        W: Write,
115    {
116        decode::decode(&self.grid, writer)
117    }
118}
119
120/// A grid that contains exactly one QR code square.
121///
122/// The common trait for everything that can be decoded as a QR code. Given a
123/// normal image, we first need to find the QR grids in it.
124///
125/// This trait can be implemented when some object is known to be exactly the
126/// bit-pattern of a QR code.
127pub trait BitGrid {
128    /// Return the size of the grid.
129    ///
130    /// Since QR codes are always squares, the grid is assumed to be size *
131    /// size.
132    fn size(&self) -> usize;
133
134    /// Return the value of the bit at the given location.
135    ///
136    /// `true` means 'black', `false` means 'white'
137    fn bit(&self, y: usize, x: usize) -> bool;
138
139    #[cfg(feature = "img")]
140    fn write_grid_to(&self, p: &str) {
141        let mut dyn_img = image::GrayImage::new(self.size() as u32, self.size() as u32);
142        for y in 0..self.size() {
143            for x in 0..self.size() {
144                let color = match self.bit(y, x) {
145                    true => 0,
146                    false => 255,
147                };
148                dyn_img.get_pixel_mut(x as u32, y as u32).0[0] = color;
149            }
150        }
151        dyn_img.save(p).unwrap();
152    }
153}
154
155/// Mirrored grid, switching x and y coordinates
156///
157/// Some QR codes are read mirrored, even though the spec does not officially support it.
158/// Since there is no marker in the QR code spec, we simply have to try to read the grid both ways
159/// if the first does not succeed.
160pub struct MirroredGrid<'a>(&'a dyn BitGrid);
161
162impl BitGrid for MirroredGrid<'_> {
163    fn size(&self) -> usize {
164        self.0.size()
165    }
166
167    fn bit(&self, y: usize, x: usize) -> bool {
168        self.0.bit(x, y)
169    }
170}
171
172/// A basic GridImage that can be generated from a given function.
173///
174/// # Example
175///
176/// ```rust
177/// # fn main() -> Result<(), rqrr::DeQRError> {
178/// let grid = [
179///     [
180///         1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
181///     ],
182///     [
183///         1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
184///     ],
185///     [
186///         1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1,
187///     ],
188///     [
189///         1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1,
190///     ],
191///     [
192///         1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1,
193///     ],
194///     [
195///         1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1,
196///     ],
197///     [
198///         1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,
199///     ],
200///     [
201///         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202///     ],
203///     [
204///         1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0,
205///     ],
206///     [
207///         1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1,
208///     ],
209///     [
210///         0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1,
211///     ],
212///     [
213///         1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1,
214///     ],
215///     [
216///         0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1,
217///     ],
218///     [
219///         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,
220///     ],
221///     [
222///         1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1,
223///     ],
224///     [
225///         1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0,
226///     ],
227///     [
228///         1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1,
229///     ],
230///     [
231///         1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
232///     ],
233///     [
234///         1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1,
235///     ],
236///     [
237///         1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0,
238///     ],
239///     [
240///         1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1,
241///     ],
242/// ];
243///
244/// let simple = rqrr::SimpleGrid::from_func(21, |x, y| grid[y][x] == 1);
245/// let grid = rqrr::Grid::new(simple);
246/// let (_meta, content) = grid.decode()?;
247/// assert_eq!(content, "rqrr");
248/// # Ok(())
249/// # }
250/// ```
251#[derive(Debug, Clone)]
252pub struct SimpleGrid {
253    cell_bitmap: Vec<u8>,
254    size: usize,
255}
256
257impl SimpleGrid {
258    pub fn from_func<F>(size: usize, fill_func: F) -> Self
259    where
260        F: Fn(usize, usize) -> bool,
261    {
262        let mut cell_bitmap = vec![0; (size * size).div_ceil(8)];
263        let mut c = 0;
264        for y in 0..size {
265            for x in 0..size {
266                if fill_func(x, y) {
267                    cell_bitmap[c >> 3] |= 1 << (c & 7) as u8;
268                }
269                c += 1;
270            }
271        }
272
273        SimpleGrid { cell_bitmap, size }
274    }
275}
276
277impl BitGrid for SimpleGrid {
278    fn size(&self) -> usize {
279        self.size
280    }
281
282    fn bit(&self, y: usize, x: usize) -> bool {
283        let c = y * self.size + x;
284        self.cell_bitmap[c >> 3] & (1 << (c & 7) as u8) != 0
285    }
286}
287
288/// Possible errors that can happen during decoding
289#[derive(Debug, Copy, Clone, Eq, PartialEq)]
290pub enum DeQRError {
291    /// Could not write the output to the output stream/string
292    IoError,
293    /// Expected more bits to decode
294    DataUnderflow,
295    /// Expected less bits to decode
296    DataOverflow,
297    /// Unknown data type in encoding
298    UnknownDataType,
299    /// Could not correct errors / code corrupt
300    DataEcc,
301    /// Could not read format information from both locations
302    FormatEcc,
303    /// Unsupported / non-existent version read
304    InvalidVersion,
305    /// Unsupported / non-existent grid size read
306    InvalidGridSize,
307    /// Output was not encoded in expected UTF8
308    EncodingError,
309}
310
311type DeQRResult<T> = Result<T, DeQRError>;
312
313impl Error for DeQRError {}
314
315impl From<::std::string::FromUtf8Error> for DeQRError {
316    fn from(_: ::std::string::FromUtf8Error) -> Self {
317        DeQRError::EncodingError
318    }
319}
320
321impl ::std::fmt::Display for DeQRError {
322    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
323        let msg = match self {
324            DeQRError::IoError => "IoError(Could not write to output)",
325            DeQRError::DataUnderflow => "DataUnderflow(Expected more bits to decode)",
326            DeQRError::DataOverflow => "DataOverflow(Expected less bits to decode)",
327            DeQRError::UnknownDataType => "UnknownDataType(DataType not known or not implemented)",
328            DeQRError::DataEcc => "Ecc(Too many errors to correct)",
329            DeQRError::FormatEcc => "Ecc(Version information corrupt)",
330            DeQRError::InvalidVersion => "InvalidVersion(Invalid version or corrupt)",
331            DeQRError::InvalidGridSize => "InvalidGridSize(Invalid version or corrupt)",
332            DeQRError::EncodingError => "Encoding(Not UTF8)",
333        };
334        write!(f, "{msg}")
335    }
336}
337
338#[cfg(test)]
339mod tests {
340    use super::*;
341
342    #[test]
343    fn test_rqrr() {
344        let grid = [
345            [
346                1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
347            ],
348            [
349                1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
350            ],
351            [
352                1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1,
353            ],
354            [
355                1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1,
356            ],
357            [
358                1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1,
359            ],
360            [
361                1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1,
362            ],
363            [
364                1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,
365            ],
366            [
367                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
368            ],
369            [
370                1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0,
371            ],
372            [
373                1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1,
374            ],
375            [
376                0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1,
377            ],
378            [
379                1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1,
380            ],
381            [
382                0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1,
383            ],
384            [
385                0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,
386            ],
387            [
388                1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1,
389            ],
390            [
391                1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0,
392            ],
393            [
394                1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1,
395            ],
396            [
397                1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
398            ],
399            [
400                1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1,
401            ],
402            [
403                1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0,
404            ],
405            [
406                1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1,
407            ],
408        ];
409
410        let img = crate::SimpleGrid::from_func(21, |x, y| grid[y][x] == 1);
411
412        let mut buf = vec![0; img.size() * img.size() / 8 + 1];
413        for y in 0..img.size() {
414            for x in 0..img.size() {
415                let i = y * img.size() + x;
416                if img.bit(y, x) {
417                    buf[i >> 3] |= 1 << ((i & 7) as u8);
418                }
419            }
420        }
421
422        let mut vec = Vec::new();
423        crate::decode::decode(&img, &mut vec).unwrap();
424
425        assert_eq!(b"rqrr".as_ref(), &vec[..])
426    }
427
428    #[test]
429    fn test_github() {
430        let grid = [
431            [
432                1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1,
433                1,
434            ],
435            [
436                1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0,
437                1,
438            ],
439            [
440                1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0,
441                1,
442            ],
443            [
444                1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0,
445                1,
446            ],
447            [
448                1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0,
449                1,
450            ],
451            [
452                1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0,
453                1,
454            ],
455            [
456                1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1,
457                1,
458            ],
459            [
460                0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
461                0,
462            ],
463            [
464                1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1,
465                0,
466            ],
467            [
468                0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0,
469                1,
470            ],
471            [
472                1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1,
473                1,
474            ],
475            [
476                1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1,
477                0,
478            ],
479            [
480                0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1,
481                1,
482            ],
483            [
484                0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0,
485                1,
486            ],
487            [
488                1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1,
489                1,
490            ],
491            [
492                1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1,
493                0,
494            ],
495            [
496                1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1,
497                1,
498            ],
499            [
500                0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
501                1,
502            ],
503            [
504                1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1,
505                1,
506            ],
507            [
508                0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
509                0,
510            ],
511            [
512                1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0,
513                0,
514            ],
515            [
516                0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1,
517                1,
518            ],
519            [
520                1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1,
521                1,
522            ],
523            [
524                1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0,
525                0,
526            ],
527            [
528                1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0,
529                1,
530            ],
531            [
532                1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1,
533                1,
534            ],
535            [
536                1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0,
537                1,
538            ],
539            [
540                1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1,
541                0,
542            ],
543            [
544                1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1,
545                1,
546            ],
547        ];
548
549        let img = crate::SimpleGrid::from_func(29, |x, y| grid[y][x] == 1);
550
551        let mut buf = vec![0; img.size() * img.size() / 8 + 1];
552        for y in 0..img.size() {
553            for x in 0..img.size() {
554                let i = y * img.size() + x;
555                if img.bit(y, x) {
556                    buf[i >> 3] |= 1 << ((i & 7) as u8);
557                }
558            }
559        }
560
561        let mut vec = Vec::new();
562        crate::decode::decode(&img, &mut vec).unwrap();
563
564        assert_eq!(b"https://github.com/WanzenBug/rqrr".as_ref(), &vec[..])
565    }
566
567    #[test]
568    fn test_number() {
569        let grid = [
570            [
571                1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1,
572                1,
573            ],
574            [
575                1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
576                1,
577            ],
578            [
579                1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0,
580                1,
581            ],
582            [
583                1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0,
584                1,
585            ],
586            [
587                1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0,
588                1,
589            ],
590            [
591                1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0,
592                1,
593            ],
594            [
595                1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1,
596                1,
597            ],
598            [
599                0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
600                0,
601            ],
602            [
603                1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0,
604                0,
605            ],
606            [
607                1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0,
608                1,
609            ],
610            [
611                0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1,
612                0,
613            ],
614            [
615                0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
616                1,
617            ],
618            [
619                0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
620                0,
621            ],
622            [
623                0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0,
624                1,
625            ],
626            [
627                0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1,
628                0,
629            ],
630            [
631                1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1,
632                1,
633            ],
634            [
635                0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
636                0,
637            ],
638            [
639                1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0,
640                1,
641            ],
642            [
643                1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1,
644                0,
645            ],
646            [
647                1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0,
648                0,
649            ],
650            [
651                1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
652                0,
653            ],
654            [
655                0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0,
656                1,
657            ],
658            [
659                1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1,
660                0,
661            ],
662            [
663                1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
664                1,
665            ],
666            [
667                1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0,
668                1,
669            ],
670            [
671                1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0,
672                0,
673            ],
674            [
675                1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1,
676                0,
677            ],
678            [
679                1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1,
680                0,
681            ],
682            [
683                1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0,
684                0,
685            ],
686        ];
687
688        let img = crate::SimpleGrid::from_func(29, |x, y| grid[y][x] == 1);
689
690        let mut buf = vec![0; img.size() * img.size() / 8 + 1];
691        for y in 0..img.size() {
692            for x in 0..img.size() {
693                let i = y * img.size() + x;
694                if img.bit(y, x) {
695                    buf[i >> 3] |= 1 << ((i & 7) as u8);
696                }
697            }
698        }
699
700        let mut vec = Vec::new();
701        crate::decode::decode(&img, &mut vec).unwrap();
702
703        assert_eq!(b"1234567891011121314151617181920".as_ref(), &vec[..])
704    }
705}