use crate::{
common::{BitArray, Result},
qrcode::decoder::{ErrorCorrectionLevel, Version},
Exceptions,
};
use super::{mask_util, ByteMatrix, QRCode};
const POSITION_DETECTION_PATTERN: [[u8; 7]; 7] = [
[1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1],
];
const POSITION_ADJUSTMENT_PATTERN: [[u8; 5]; 5] = [
[1, 1, 1, 1, 1],
[1, 0, 0, 0, 1],
[1, 0, 1, 0, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1],
];
const POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE: [[i16; 7]; 40] = [
[-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], ];
const TYPE_INFO_COORDINATES: [[u32; 2]; 15] = [
[8, 0],
[8, 1],
[8, 2],
[8, 3],
[8, 4],
[8, 5],
[8, 7],
[8, 8],
[7, 8],
[5, 8],
[4, 8],
[3, 8],
[2, 8],
[1, 8],
[0, 8],
];
const VERSION_INFO_POLY: u32 = 0x1f25;
const TYPE_INFO_POLY: u32 = 0x537;
const TYPE_INFO_MASK_PATTERN: u32 = 0x5412;
pub fn clearMatrix(matrix: &mut ByteMatrix) {
matrix.clear(-1i8 as u8);
}
pub fn buildMatrix(
dataBits: &BitArray,
ecLevel: &ErrorCorrectionLevel,
version: &Version,
maskPattern: i32,
matrix: &mut ByteMatrix,
) -> Result<()> {
clearMatrix(matrix);
embedBasicPatterns(version, matrix)?;
embedTypeInfo(ecLevel, maskPattern, matrix)?;
maybeEmbedVersionInfo(version, matrix)?;
embedDataBits(dataBits, maskPattern, matrix)?;
Ok(())
}
pub fn embedBasicPatterns(version: &Version, matrix: &mut ByteMatrix) -> Result<()> {
embedPositionDetectionPatternsAndSeparators(matrix)?;
embedDarkDotAtLeftBottomCorner(matrix)?;
maybeEmbedPositionAdjustmentPatterns(version, matrix);
embedTimingPatterns(matrix);
Ok(())
}
pub fn embedTypeInfo(
ecLevel: &ErrorCorrectionLevel,
maskPattern: i32,
matrix: &mut ByteMatrix,
) -> Result<()> {
let mut typeInfoBits = BitArray::new();
makeTypeInfoBits(ecLevel, maskPattern as u32, &mut typeInfoBits)?;
for (i, coordinates) in TYPE_INFO_COORDINATES
.iter()
.enumerate()
.take(typeInfoBits.get_size())
{
let bit = typeInfoBits.get(typeInfoBits.get_size() - 1 - i);
let x1 = coordinates[0];
let y1 = coordinates[1];
matrix.set_bool(x1, y1, bit);
let x2;
let y2;
if i < 8 {
x2 = matrix.getWidth() - i as u32 - 1;
y2 = 8;
} else {
x2 = 8;
y2 = matrix.getHeight() - 7 + (i as u32 - 8);
}
matrix.set_bool(x2, y2, bit);
}
Ok(())
}
pub fn maybeEmbedVersionInfo(version: &Version, matrix: &mut ByteMatrix) -> Result<()> {
if version.getVersionNumber() < 7 {
return Ok(()); }
let mut versionInfoBits = BitArray::new();
makeVersionInfoBits(version, &mut versionInfoBits)?;
let mut bitIndex = 6 * 3 - 1; for i in 0..6 {
for j in 0..3 {
let bit = versionInfoBits.get(bitIndex);
bitIndex = bitIndex.saturating_sub(1);
matrix.set_bool(i, matrix.getHeight() - 11 + j, bit);
matrix.set_bool(matrix.getHeight() - 11 + j, i, bit);
}
}
Ok(())
}
pub fn embedDataBits(dataBits: &BitArray, maskPattern: i32, matrix: &mut ByteMatrix) -> Result<()> {
let mut bitIndex = 0;
let mut direction: i32 = -1;
let mut x = matrix.getWidth() as i32 - 1;
let mut y = matrix.getHeight() as i32 - 1;
while x > 0 {
if x == 6 {
x -= 1;
}
while y >= 0 && y < matrix.getHeight() as i32 {
for i in 0..2 {
let xx = x - i;
if !isEmpty(matrix.get(xx as u32, y as u32)) {
continue;
}
let mut bit;
if bitIndex < dataBits.get_size() {
bit = dataBits.get(bitIndex);
bitIndex += 1;
} else {
bit = false;
}
if maskPattern != -1
&& mask_util::getDataMaskBit(maskPattern as u32, xx as u32, y as u32)?
{
bit = !bit;
}
matrix.set_bool(xx as u32, y as u32, bit);
}
y += direction;
}
direction = -direction; y += direction;
x -= 2; }
if bitIndex != dataBits.get_size() {
return Err(Exceptions::writer_with(format!(
"Not all bits consumed: {}/{}",
bitIndex,
dataBits.get_size()
)));
}
Ok(())
}
pub fn findMSBSet(value: u32) -> u32 {
32 - value.leading_zeros()
}
pub fn calculateBCHCode(value: u32, poly: u32) -> Result<u32> {
if poly == 0 {
return Err(Exceptions::illegal_argument_with("0 polynomial"));
}
let mut value = value;
let msbSetInPoly = findMSBSet(poly);
value <<= msbSetInPoly - 1;
while findMSBSet(value) >= msbSetInPoly {
value ^= poly << (findMSBSet(value) - msbSetInPoly);
}
Ok(value)
}
pub fn makeTypeInfoBits(
ecLevel: &ErrorCorrectionLevel,
maskPattern: u32,
bits: &mut BitArray,
) -> Result<()> {
if !QRCode::isValidMaskPattern(maskPattern as i32) {
return Err(Exceptions::writer_with("Invalid mask pattern"));
}
let typeInfo = (ecLevel.get_value() << 3) as u32 | maskPattern;
bits.appendBits(typeInfo, 5)?;
let bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY)?;
bits.appendBits(bchCode, 10)?;
let mut maskBits = BitArray::new();
maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15)?;
bits.xor(&maskBits)?;
if bits.get_size() != 15 {
return Err(Exceptions::writer_with(format!(
"should not happen but we got: {}",
bits.get_size()
)));
}
Ok(())
}
pub fn makeVersionInfoBits(version: &Version, bits: &mut BitArray) -> Result<()> {
bits.appendBits(version.getVersionNumber(), 6)?;
let bchCode = calculateBCHCode(version.getVersionNumber(), VERSION_INFO_POLY)?;
bits.appendBits(bchCode, 12)?;
if bits.get_size() != 18 {
return Err(Exceptions::writer_with(format!(
"should not happen but we got: {}",
bits.get_size()
)));
}
Ok(())
}
pub fn isEmpty(value: u8) -> bool {
value == -1i8 as u8
}
pub fn embedTimingPatterns(matrix: &mut ByteMatrix) {
for i in 8..matrix.getWidth() - 8 {
let bit = (i as u8 + 1) % 2;
if isEmpty(matrix.get(i, 6)) {
matrix.set(i, 6, bit);
}
if isEmpty(matrix.get(6, i)) {
matrix.set(6, i, bit);
}
}
}
pub fn embedDarkDotAtLeftBottomCorner(matrix: &mut ByteMatrix) -> Result<()> {
if matrix.get(8, matrix.getHeight() - 8) == 0 {
return Err(Exceptions::WRITER);
}
matrix.set(8, matrix.getHeight() - 8, 1);
Ok(())
}
pub fn embedHorizontalSeparationPattern(
xStart: u32,
yStart: u32,
matrix: &mut ByteMatrix,
) -> Result<()> {
for x in 0..8 {
if !isEmpty(matrix.get(xStart + x, yStart)) {
return Err(Exceptions::WRITER);
}
matrix.set(xStart + x, yStart, 0);
}
Ok(())
}
pub fn embedVerticalSeparationPattern(
xStart: u32,
yStart: u32,
matrix: &mut ByteMatrix,
) -> Result<()> {
for y in 0..7 {
if !isEmpty(matrix.get(xStart, yStart + y)) {
return Err(Exceptions::WRITER);
}
matrix.set(xStart, yStart + y, 0);
}
Ok(())
}
pub fn embedPositionAdjustmentPattern(xStart: u32, yStart: u32, matrix: &mut ByteMatrix) {
for (y, patternY) in POSITION_ADJUSTMENT_PATTERN.iter().enumerate() {
for x in 0..5 {
matrix.set(xStart + x, yStart + y as u32, patternY[x as usize]);
}
}
}
pub fn embedPositionDetectionPattern(xStart: u32, yStart: u32, matrix: &mut ByteMatrix) {
for (y, patternY) in POSITION_DETECTION_PATTERN.iter().enumerate() {
for x in 0..7 {
matrix.set(xStart + x, yStart + y as u32, patternY[x as usize]);
}
}
}
pub fn embedPositionDetectionPatternsAndSeparators(matrix: &mut ByteMatrix) -> Result<()> {
let pdpWidth = POSITION_DETECTION_PATTERN[0].len() as u32;
embedPositionDetectionPattern(0, 0, matrix);
embedPositionDetectionPattern(matrix.getWidth() - pdpWidth, 0, matrix);
embedPositionDetectionPattern(0, matrix.getWidth() - pdpWidth, matrix);
let hspWidth = 8;
embedHorizontalSeparationPattern(0, hspWidth - 1, matrix)?;
embedHorizontalSeparationPattern(matrix.getWidth() - hspWidth, hspWidth - 1, matrix)?;
embedHorizontalSeparationPattern(0, matrix.getWidth() - hspWidth, matrix)?;
let vspSize = 7;
embedVerticalSeparationPattern(vspSize, 0, matrix)?;
embedVerticalSeparationPattern(matrix.getHeight() - vspSize - 1, 0, matrix)?;
embedVerticalSeparationPattern(vspSize, matrix.getHeight() - vspSize, matrix)?;
Ok(())
}
pub fn maybeEmbedPositionAdjustmentPatterns(version: &Version, matrix: &mut ByteMatrix) {
if version.getVersionNumber() < 2 {
return;
}
let index = version.getVersionNumber() - 1;
let coordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index as usize];
for y in coordinates {
if y >= 0 {
for x in coordinates {
if x >= 0 && isEmpty(matrix.get(x as u32, y as u32)) {
embedPositionAdjustmentPattern((x - 2) as u32, (y - 2) as u32, matrix);
}
}
}
}
}