1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#![cfg_attr(feature = "unstable", feature(test))]
#![deny(unsafe_code)]

#[cfg(all(feature = "unstable", test))]
extern crate test;

use bit_vec::BigEndianBitVec;
use version_db::{RsParams, VersionInfo, VERSION_DB};

pub use identify::{ScanResults, Scanner};
pub use linear_algebra::Point;

mod bit_vec;
mod decode;
mod gf256;
mod identify;
mod linear_algebra;
mod pixels;
mod version_db;

#[cfg(test)]
mod test_data;
#[cfg(test)]
mod tests;

#[derive(Debug, Copy, Clone)]
pub enum EccLevel {
    M = 0,
    L,
    H,
    Q,
}

impl EccLevel {
    fn into_int(self) -> usize {
        use EccLevel::*;

        match self {
            M => 0,
            L => 1,
            H => 2,
            Q => 3,
        }
    }

    fn from_int(val: isize) -> Option<EccLevel> {
        match val {
            0 => Some(EccLevel::M),
            1 => Some(EccLevel::L),
            2 => Some(EccLevel::H),
            3 => Some(EccLevel::Q),

            _ => None,
        }
    }
}

#[derive(Debug)]
pub enum Payload {
    Numeric(String),
    Alpha(String),
    Byte(Vec<u8>),
}

/// QR payload
#[derive(Debug)]
pub struct Data {
    // Various parameters of the QR-code. These can mostly be
    // ignored if you only care about the data.
    pub version: u8,
    pub ecc_level: EccLevel,
    pub mask: usize,
    pub eci: Option<usize>,

    /// Array of payload datas extracted from QR
    pub payloads: Vec<Payload>,
}

impl Data {
    /// Try to convert all payloads to `String` and concatenate then
    pub fn try_string(&self) -> Result<String, Error> {
        let mut result = String::new();

        for i in self.payloads.iter() {
            match i {
                Payload::Alpha(ref alpha) => result += alpha,
                Payload::Numeric(ref numeric) => result += numeric,
                Payload::Byte(ref bytes) => {
                    result += String::from_utf8_lossy(bytes).as_ref();
                }
            }
        }

        Ok(result)
    }

    fn new(version: u8, ecc_level: EccLevel, mask: usize) -> Data {
        Data {
            version,
            ecc_level,
            mask,
            payloads: Vec::new(),
            eci: None,
        }
    }

    fn version_info(&self) -> Option<&'static VersionInfo> {
        VERSION_DB.get(self.version as usize)
    }

    fn ecc_rs_params(&self) -> Option<&'static RsParams> {
        self.version_info()
            .and_then(|i| i.ecc.get(self.ecc_level.into_int()))
    }

    fn mask_bit(&self, x: usize, y: usize) -> bool {
        match self.mask {
            0 => (y + x) % 2 == 0,
            1 => (y % 2) == 0,
            2 => (x % 3) == 0,
            3 => ((y + x) % 3) == 0,
            4 => (((y / 2) + (x / 3)) % 2) == 0,
            5 => ((y * x) % 2 + (y * x) % 3) == 0,
            6 => (((y * x) % 2 + (y * x) % 3) % 2) == 0,
            7 => (((y * x) % 3 + (y + x) % 2) % 2) == 0,

            _ => unreachable!(),
        }
    }
}

pub struct Code {
    pub corners: [Point; 4],
    pub size: usize,
    cell_bitmap: BigEndianBitVec,
}

pub enum Error {
    Unknown,
}

impl Code {
    /// Try to decode QR payload
    pub fn decode(&self) -> Result<Data, Error> {
        decode::decode(self).map_err(|_| Error::Unknown)
    }

    fn grid_bit(&self, x: usize, y: usize) -> Option<bool> {
        self.cell_bitmap.get(y * self.size + x)
    }
}