1use crate::{
18 Exceptions,
19 common::{BitArray, BitFieldBaseType, Result},
20 qrcode::decoder::{ErrorCorrectionLevel, Version},
21};
22
23use super::{ByteMatrix, QRCode, mask_util};
24
25const POSITION_DETECTION_PATTERN: [[u8; 7]; 7] = [
31 [1, 1, 1, 1, 1, 1, 1],
32 [1, 0, 0, 0, 0, 0, 1],
33 [1, 0, 1, 1, 1, 0, 1],
34 [1, 0, 1, 1, 1, 0, 1],
35 [1, 0, 1, 1, 1, 0, 1],
36 [1, 0, 0, 0, 0, 0, 1],
37 [1, 1, 1, 1, 1, 1, 1],
38];
39
40const POSITION_ADJUSTMENT_PATTERN: [[u8; 5]; 5] = [
41 [1, 1, 1, 1, 1],
42 [1, 0, 0, 0, 1],
43 [1, 0, 1, 0, 1],
44 [1, 0, 0, 0, 1],
45 [1, 1, 1, 1, 1],
46];
47
48const POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE: [[i16; 7]; 40] = [
50 [-1, -1, -1, -1, -1, -1, -1], [6, 18, -1, -1, -1, -1, -1], [6, 22, -1, -1, -1, -1, -1], [6, 26, -1, -1, -1, -1, -1], [6, 30, -1, -1, -1, -1, -1], [6, 34, -1, -1, -1, -1, -1], [6, 22, 38, -1, -1, -1, -1], [6, 24, 42, -1, -1, -1, -1], [6, 26, 46, -1, -1, -1, -1], [6, 28, 50, -1, -1, -1, -1], [6, 30, 54, -1, -1, -1, -1], [6, 32, 58, -1, -1, -1, -1], [6, 34, 62, -1, -1, -1, -1], [6, 26, 46, 66, -1, -1, -1], [6, 26, 48, 70, -1, -1, -1], [6, 26, 50, 74, -1, -1, -1], [6, 30, 54, 78, -1, -1, -1], [6, 30, 56, 82, -1, -1, -1], [6, 30, 58, 86, -1, -1, -1], [6, 34, 62, 90, -1, -1, -1], [6, 28, 50, 72, 94, -1, -1], [6, 26, 50, 74, 98, -1, -1], [6, 30, 54, 78, 102, -1, -1], [6, 28, 54, 80, 106, -1, -1], [6, 32, 58, 84, 110, -1, -1], [6, 30, 58, 86, 114, -1, -1], [6, 34, 62, 90, 118, -1, -1], [6, 26, 50, 74, 98, 122, -1], [6, 30, 54, 78, 102, 126, -1], [6, 26, 52, 78, 104, 130, -1], [6, 30, 56, 82, 108, 134, -1], [6, 34, 60, 86, 112, 138, -1], [6, 30, 58, 86, 114, 142, -1], [6, 34, 62, 90, 118, 146, -1], [6, 30, 54, 78, 102, 126, 150], [6, 24, 50, 76, 102, 128, 154], [6, 28, 54, 80, 106, 132, 158], [6, 32, 58, 84, 110, 136, 162], [6, 26, 54, 82, 110, 138, 166], [6, 30, 58, 86, 114, 142, 170], ];
91
92const TYPE_INFO_COORDINATES: [[u32; 2]; 15] = [
94 [8, 0],
95 [8, 1],
96 [8, 2],
97 [8, 3],
98 [8, 4],
99 [8, 5],
100 [8, 7],
101 [8, 8],
102 [7, 8],
103 [5, 8],
104 [4, 8],
105 [3, 8],
106 [2, 8],
107 [1, 8],
108 [0, 8],
109];
110
111const VERSION_INFO_POLY: u32 = 0x1f25; const TYPE_INFO_POLY: u32 = 0x537;
116const TYPE_INFO_MASK_PATTERN: BitFieldBaseType = 0x5412;
117
118pub fn clearMatrix(matrix: &mut ByteMatrix) {
123 matrix.clear(-1i8 as u8);
124}
125
126pub fn buildMatrix(
129 dataBits: &BitArray,
130 ecLevel: &ErrorCorrectionLevel,
131 version: &Version,
132 maskPattern: i32,
133 matrix: &mut ByteMatrix,
134) -> Result<()> {
135 clearMatrix(matrix);
136 embedBasicPatterns(version, matrix)?;
137 embedTypeInfo(ecLevel, maskPattern, matrix)?;
139 maybeEmbedVersionInfo(version, matrix)?;
141 embedDataBits(dataBits, maskPattern, matrix)?;
143 Ok(())
144}
145
146pub fn embedBasicPatterns(version: &Version, matrix: &mut ByteMatrix) -> Result<()> {
153 embedPositionDetectionPatternsAndSeparators(matrix)?;
155 embedDarkDotAtLeftBottomCorner(matrix)?;
157
158 maybeEmbedPositionAdjustmentPatterns(version, matrix);
160 embedTimingPatterns(matrix);
162 Ok(())
163}
164
165pub fn embedTypeInfo(
167 ecLevel: &ErrorCorrectionLevel,
168 maskPattern: i32,
169 matrix: &mut ByteMatrix,
170) -> Result<()> {
171 let mut typeInfoBits = BitArray::new();
172 makeTypeInfoBits(ecLevel, maskPattern as u32, &mut typeInfoBits)?;
173
174 for (i, coordinates) in TYPE_INFO_COORDINATES
175 .iter()
176 .enumerate()
177 .take(typeInfoBits.get_size())
178 {
179 let bit = typeInfoBits.get(typeInfoBits.get_size() - 1 - i);
182
183 let x1 = coordinates[0];
186 let y1 = coordinates[1];
187 matrix.set_bool(x1, y1, bit);
188
189 let x2;
190 let y2;
191 if i < 8 {
192 x2 = matrix.getWidth() - i as u32 - 1;
194 y2 = 8;
195 } else {
196 x2 = 8;
198 y2 = matrix.getHeight() - 7 + (i as u32 - 8);
199 }
200 matrix.set_bool(x2, y2, bit);
201 }
202 Ok(())
203}
204
205pub fn maybeEmbedVersionInfo(version: &Version, matrix: &mut ByteMatrix) -> Result<()> {
208 if version.getVersionNumber() < 7 {
209 return Ok(()); }
212 let mut versionInfoBits = BitArray::new();
213 makeVersionInfoBits(version, &mut versionInfoBits)?;
214
215 let mut bitIndex = 6 * 3 - 1; for i in 0..6 {
217 for j in 0..3 {
218 let bit = versionInfoBits.get(bitIndex);
220 bitIndex = bitIndex.saturating_sub(1);
221 matrix.set_bool(i, matrix.getHeight() - 11 + j, bit);
223 matrix.set_bool(matrix.getHeight() - 11 + j, i, bit);
225 }
226 }
227 Ok(())
228}
229
230pub fn embedDataBits(dataBits: &BitArray, maskPattern: i32, matrix: &mut ByteMatrix) -> Result<()> {
234 let mut bitIndex = 0;
235 let mut direction: i32 = -1;
236 let mut x = matrix.getWidth() as i32 - 1;
238 let mut y = matrix.getHeight() as i32 - 1;
239 while x > 0 {
240 if x == 6 {
242 x -= 1;
243 }
244 while y >= 0 && y < matrix.getHeight() as i32 {
245 for i in 0..2 {
246 let xx = x - i;
247 if !isEmpty(matrix.get(xx as u32, y as u32)) {
249 continue;
250 }
251 let mut bit;
252 if bitIndex < dataBits.get_size() {
253 bit = dataBits.get(bitIndex);
254 bitIndex += 1;
255 } else {
256 bit = false;
259 }
260
261 if maskPattern != -1
263 && mask_util::getDataMaskBit(maskPattern as u32, xx as u32, y as u32)?
264 {
265 bit = !bit;
266 }
267 matrix.set_bool(xx as u32, y as u32, bit);
268 }
269 y += direction;
270 }
271 direction = -direction; y += direction;
273 x -= 2; }
275 if bitIndex != dataBits.get_size() {
277 return Err(Exceptions::writer_with(format!(
278 "Not all bits consumed: {}/{}",
279 bitIndex,
280 dataBits.get_size()
281 )));
282 }
283 Ok(())
284}
285
286pub fn findMSBSet(value: u32) -> u32 {
292 32 - value.leading_zeros()
293}
294
295pub fn calculateBCHCode(value: u32, poly: u32) -> Result<u32> {
321 if poly == 0 {
322 return Err(Exceptions::illegal_argument_with("0 polynomial"));
323 }
324 let mut value = value;
325 let msbSetInPoly = findMSBSet(poly);
328 value <<= msbSetInPoly - 1;
329 while findMSBSet(value) >= msbSetInPoly {
331 value ^= poly << (findMSBSet(value) - msbSetInPoly);
332 }
333 Ok(value)
335}
336
337pub fn makeTypeInfoBits(
341 ecLevel: &ErrorCorrectionLevel,
342 maskPattern: u32,
343 bits: &mut BitArray,
344) -> Result<()> {
345 if !QRCode::isValidMaskPattern(maskPattern as i32) {
346 return Err(Exceptions::writer_with("Invalid mask pattern"));
347 }
348 let typeInfo = (ecLevel.get_value() << 3) as u32 | maskPattern;
349 bits.appendBits(typeInfo as BitFieldBaseType, 5)?;
350
351 let bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY)?;
352 bits.appendBits(bchCode as BitFieldBaseType, 10)?;
353
354 let mut maskBits = BitArray::new();
355 maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15)?;
356 bits.xor(&maskBits)?;
357
358 if bits.get_size() != 15 {
359 return Err(Exceptions::writer_with(format!(
361 "should not happen but we got: {}",
362 bits.get_size()
363 )));
364 }
365 Ok(())
366}
367
368pub fn makeVersionInfoBits(version: &Version, bits: &mut BitArray) -> Result<()> {
371 bits.appendBits(version.getVersionNumber() as BitFieldBaseType, 6)?;
372 let bchCode = calculateBCHCode(version.getVersionNumber(), VERSION_INFO_POLY)?;
373 bits.appendBits(bchCode as BitFieldBaseType, 12)?;
374
375 if bits.get_size() != 18 {
376 return Err(Exceptions::writer_with(format!(
378 "should not happen but we got: {}",
379 bits.get_size()
380 )));
381 }
382 Ok(())
383}
384
385pub fn isEmpty(value: u8) -> bool {
387 value == -1i8 as u8
388}
389
390pub fn embedTimingPatterns(matrix: &mut ByteMatrix) {
391 for i in 8..matrix.getWidth() - 8 {
394 let bit = (i as u8 + 1) % 2;
396 if isEmpty(matrix.get(i, 6)) {
398 matrix.set(i, 6, bit);
399 }
400 if isEmpty(matrix.get(6, i)) {
402 matrix.set(6, i, bit);
403 }
404 }
405}
406
407pub fn embedDarkDotAtLeftBottomCorner(matrix: &mut ByteMatrix) -> Result<()> {
409 if matrix.get(8, matrix.getHeight() - 8) == 0 {
410 return Err(Exceptions::WRITER);
411 }
412 matrix.set(8, matrix.getHeight() - 8, 1);
413 Ok(())
414}
415
416pub fn embedHorizontalSeparationPattern(
417 xStart: u32,
418 yStart: u32,
419 matrix: &mut ByteMatrix,
420) -> Result<()> {
421 for x in 0..8 {
422 if !isEmpty(matrix.get(xStart + x, yStart)) {
423 return Err(Exceptions::WRITER);
424 }
425 matrix.set(xStart + x, yStart, 0);
426 }
427 Ok(())
428}
429
430pub fn embedVerticalSeparationPattern(
431 xStart: u32,
432 yStart: u32,
433 matrix: &mut ByteMatrix,
434) -> Result<()> {
435 for y in 0..7 {
436 if !isEmpty(matrix.get(xStart, yStart + y)) {
437 return Err(Exceptions::WRITER);
438 }
439 matrix.set(xStart, yStart + y, 0);
440 }
441 Ok(())
442}
443
444pub fn embedPositionAdjustmentPattern(xStart: u32, yStart: u32, matrix: &mut ByteMatrix) {
445 for (y, patternY) in POSITION_ADJUSTMENT_PATTERN.iter().enumerate() {
446 for x in 0..5 {
447 matrix.set(xStart + x, yStart + y as u32, patternY[x as usize]);
448 }
449 }
450}
451
452pub fn embedPositionDetectionPattern(xStart: u32, yStart: u32, matrix: &mut ByteMatrix) {
453 for (y, patternY) in POSITION_DETECTION_PATTERN.iter().enumerate() {
454 for x in 0..7 {
455 matrix.set(xStart + x, yStart + y as u32, patternY[x as usize]);
456 }
457 }
458}
459
460pub fn embedPositionDetectionPatternsAndSeparators(matrix: &mut ByteMatrix) -> Result<()> {
462 let pdpWidth = POSITION_DETECTION_PATTERN[0].len() as u32;
464 embedPositionDetectionPattern(0, 0, matrix);
466 embedPositionDetectionPattern(matrix.getWidth() - pdpWidth, 0, matrix);
468 embedPositionDetectionPattern(0, matrix.getWidth() - pdpWidth, matrix);
470
471 let hspWidth = 8;
473 embedHorizontalSeparationPattern(0, hspWidth - 1, matrix)?;
475 embedHorizontalSeparationPattern(matrix.getWidth() - hspWidth, hspWidth - 1, matrix)?;
477 embedHorizontalSeparationPattern(0, matrix.getWidth() - hspWidth, matrix)?;
479
480 let vspSize = 7;
482 embedVerticalSeparationPattern(vspSize, 0, matrix)?;
484 embedVerticalSeparationPattern(matrix.getHeight() - vspSize - 1, 0, matrix)?;
486 embedVerticalSeparationPattern(vspSize, matrix.getHeight() - vspSize, matrix)?;
488
489 Ok(())
490}
491
492pub fn maybeEmbedPositionAdjustmentPatterns(version: &Version, matrix: &mut ByteMatrix) {
494 if version.getVersionNumber() < 2 {
495 return;
497 }
498 let index = version.getVersionNumber() - 1;
499 let coordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index as usize];
500 for y in coordinates {
501 if y >= 0 {
502 for x in coordinates {
503 if x >= 0 && isEmpty(matrix.get(x as u32, y as u32)) {
504 embedPositionAdjustmentPattern((x - 2) as u32, (y - 2) as u32, matrix);
508 }
509 }
510 }
511 }
512}