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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#![cfg_attr(feature="bench", feature(test))]
#[cfg(feature="bench")]
extern crate test;
extern crate num;
use std::ops::Index;
pub use types::{QrResult, EcLevel, Version};
pub mod types;
pub mod bits;
pub mod optimize;
pub mod ec;
pub mod canvas;
#[derive(Clone)]
pub struct QrCode {
content: Vec<bool>,
version: Version,
ec_level: EcLevel,
width: usize,
}
impl QrCode {
pub fn new(data: &[u8]) -> QrResult<QrCode> {
QrCode::with_error_correction_level(data, EcLevel::M)
}
pub fn with_error_correction_level(data: &[u8], ec_level: EcLevel) -> QrResult<QrCode> {
let bits = try!(bits::encode_auto(data, ec_level));
QrCode::with_bits(bits, ec_level)
}
pub fn with_version(data: &[u8], version: Version, ec_level: EcLevel) -> QrResult<QrCode> {
let mut bits = bits::Bits::new(version);
try!(bits.push_optimal_data(data));
try!(bits.push_terminator(ec_level));
QrCode::with_bits(bits, ec_level)
}
pub fn with_bits(bits: bits::Bits, ec_level: EcLevel) -> QrResult<QrCode> {
let version = bits.version();
let data = bits.into_bytes();
let (encoded_data, ec_data) = try!(ec::construct_codewords(&*data, version, ec_level));
let mut canvas = canvas::Canvas::new(version, ec_level);
canvas.draw_all_functional_patterns();
canvas.draw_data(&*encoded_data, &*ec_data);
let canvas = canvas.apply_best_mask();
Ok(QrCode {
content: canvas.to_bools(),
version: version,
ec_level: ec_level,
width: version.width() as usize,
})
}
pub fn version(&self) -> Version {
self.version
}
pub fn error_correction_level(&self) -> EcLevel {
self.ec_level
}
pub fn width(&self) -> usize {
self.width
}
pub fn max_allowed_errors(&self) -> usize {
ec::max_allowed_errors(self.version, self.ec_level).unwrap()
}
pub fn is_functional(&self, x: usize, y: usize) -> bool {
canvas::is_functional(self.version, self.version.width(), x as i16, y as i16)
}
pub fn to_debug_str(&self, on_char: char, off_char: char) -> String {
let width = self.width;
let mut k = 0;
let mut res = String::with_capacity(width * (width + 1));
for _ in 0 .. width {
res.push('\n');
for _ in 0 .. width {
res.push(if self.content[k] { on_char } else { off_char });
k += 1;
}
}
res
}
pub fn to_vec(&self) -> Vec<bool> {
self.content.clone()
}
pub fn into_vec(self) -> Vec<bool> {
self.content.clone()
}
}
impl Index<(usize, usize)> for QrCode {
type Output = bool;
fn index(&self, (x, y): (usize, usize)) -> &bool {
let index = y * self.width + x;
&self.content[index]
}
}
#[cfg(test)]
mod tests {
use {QrCode, Version, EcLevel};
#[test]
fn test_annex_i_qr() {
let code = QrCode::with_version(b"01234567", Version::Normal(1), EcLevel::M).unwrap();
assert_eq!(&*code.to_debug_str('#', '.'), "\n\
#######..#.##.#######\n\
#.....#..####.#.....#\n\
#.###.#.#.....#.###.#\n\
#.###.#.##....#.###.#\n\
#.###.#.#.###.#.###.#\n\
#.....#.#...#.#.....#\n\
#######.#.#.#.#######\n\
........#..##........\n\
#.#####..#..#.#####..\n\
...#.#.##.#.#..#.##..\n\
..#...##.#.#.#..#####\n\
....#....#.....####..\n\
...######..#.#..#....\n\
........#.#####..##..\n\
#######..##.#.##.....\n\
#.....#.#.#####...#.#\n\
#.###.#.#...#..#.##..\n\
#.###.#.##..#..#.....\n\
#.###.#.#.##.#..#.#..\n\
#.....#........##.##.\n\
#######.####.#..#.#..");
}
#[test]
fn test_annex_i_micro_qr() {
let code = QrCode::with_version(b"01234567", Version::Micro(2), EcLevel::L).unwrap();
assert_eq!(&*code.to_debug_str('#', '.'), "\n\
#######.#.#.#\n\
#.....#.###.#\n\
#.###.#..##.#\n\
#.###.#..####\n\
#.###.#.###..\n\
#.....#.#...#\n\
#######..####\n\
.........##..\n\
##.#....#...#\n\
.##.#.#.#.#.#\n\
###..#######.\n\
...#.#....##.\n\
###.#..##.###");
}
}