use crate::engine::{
Exceptions,
common::{BitMatrix, Result},
qrcode::common::{ErrorCorrectionLevel, FormatInformation, Version, VersionRef},
};
use super::{Type, data_mask::GetDataMaskBit};
use crate::engine::common::cpp_essentials::AppendBit;
pub fn getBit(bitMatrix: &BitMatrix, x: u32, y: u32, mirrored: Option<bool>) -> bool {
let mirrored = mirrored.unwrap_or(false);
if mirrored { bitMatrix.get(y, x) } else { bitMatrix.get(x, y) }
}
pub fn ReadVersion(bitMatrix: &BitMatrix, qr_type: Type) -> Result<VersionRef> {
if !Version::HasValidSize(bitMatrix) {
return Err(Exceptions::FORMAT);
}
let number = Version::Number(bitMatrix);
match qr_type {
Type::Model1 => Version::Model1(number),
Type::Micro => Version::Micro(number),
Type::Model2 => Version::Model2(number),
Type::RectMicro => Version::rMQR(number),
}
}
pub fn ReadFormatInformation(bitMatrix: &BitMatrix) -> Result<FormatInformation> {
if Version::HasValidSizeType(bitMatrix, Type::Micro) {
let mut formatInfoBits = 0;
for x in 1..9 {
AppendBit(&mut formatInfoBits, getBit(bitMatrix, x, 8, None));
}
for y in (1..=7).rev() {
AppendBit(&mut formatInfoBits, getBit(bitMatrix, 8, y, None));
}
return Ok(FormatInformation::DecodeMQR(formatInfoBits as u32));
}
if Version::HasValidSizeType(bitMatrix, Type::RectMicro) {
let mut formatInfoBits1 = 0;
for y in (1..=3).rev() {
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, 11, y, None));
}
for x in (8..=10).rev() {
for y in (1..=5).rev() {
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, x, y, None));
}
}
let mut formatInfoBits2 = 0;
let width = bitMatrix.width();
let height = bitMatrix.height();
for x in 3..=5 {
AppendBit(&mut formatInfoBits2, getBit(bitMatrix, width - x, height - 6, None));
}
for x in 6..=8 {
for y in 2..=6 {
AppendBit(&mut formatInfoBits2, getBit(bitMatrix, width - x, height - y, None));
}
}
return Ok(FormatInformation::DecodeRMQR(formatInfoBits1 as u32, formatInfoBits2 as u32));
}
let mut formatInfoBits1 = 0;
for x in 0..6 {
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, x, 8, None));
}
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, 7, 8, None));
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, 8, 8, None));
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, 8, 7, None));
for y in (0..=5).rev() {
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, 8, y, None));
}
let dimension = bitMatrix.height();
let mut formatInfoBits2 = 0;
for y in ((dimension - 8)..=(dimension - 1)).rev() {
AppendBit(&mut formatInfoBits2, getBit(bitMatrix, 8, y, None));
}
for x in (dimension - 8)..dimension {
AppendBit(&mut formatInfoBits2, getBit(bitMatrix, x, 8, None));
}
Ok(FormatInformation::DecodeQR(formatInfoBits1 as u32, formatInfoBits2 as u32))
}
pub fn ReadQRCodewords(bitMatrix: &BitMatrix, version: VersionRef, formatInfo: &FormatInformation) -> Result<Vec<u8>> {
let functionPattern: BitMatrix = version.buildFunctionPattern()?;
let mut result = Vec::with_capacity(version.getTotalCodewords() as usize);
let mut currentByte = 0;
let mut readingUp = true;
let mut bitsRead = 0;
let dimension = bitMatrix.height();
let mut x = (dimension as i32) - 1;
while x > 0 {
if x == 6 {
x -= 1;
}
for row in 0..dimension {
let y = if readingUp { dimension - 1 - row } else { row };
for col in 0..2 {
let xx = (x - col) as u32;
if !functionPattern.get(xx, y) {
AppendBit(
&mut currentByte,
GetDataMaskBit(formatInfo.data_mask as u32, xx, y, None)?
!= getBit(bitMatrix, xx, y, Some(formatInfo.isMirrored)),
);
bitsRead += 1;
if bitsRead % 8 == 0 {
result.push(std::mem::take(&mut currentByte) as u8);
}
}
}
}
readingUp = !readingUp;
x -= 2;
}
if (result.len()) != version.getTotalCodewords() as usize {
return Err(Exceptions::FORMAT);
}
Ok(result)
}
pub fn ReadMQRCodewords(bitMatrix: &BitMatrix, version: VersionRef, formatInfo: &FormatInformation) -> Result<Vec<u8>> {
let functionPattern = version.buildFunctionPattern()?;
let hasD4mBlock = version.getVersionNumber() % 2 == 1;
let d4mBlockIndex = if version.getVersionNumber() == 1 {
3
} else if formatInfo.error_correction_level == ErrorCorrectionLevel::L {
11
} else {
9
};
let mut result = Vec::with_capacity(version.getTotalCodewords() as usize);
let mut currentByte = 0;
let mut readingUp = true;
let mut bitsRead = 0;
let dimension = bitMatrix.height();
let mut x = dimension - 1;
while x > 0 {
for row in 0..dimension {
let y = if readingUp { dimension - 1 - row } else { row };
for col in 0..2 {
let xx = x - col;
if !functionPattern.get(xx, y) {
AppendBit(
&mut currentByte,
GetDataMaskBit(formatInfo.data_mask as u32, xx, y, Some(true))?
!= getBit(bitMatrix, xx, y, Some(formatInfo.isMirrored)),
);
bitsRead += 1;
if bitsRead == 8 || (bitsRead == 4 && hasD4mBlock && (result.len()) == d4mBlockIndex - 1) {
result.push(std::mem::take(&mut currentByte) as u8);
bitsRead = 0;
}
}
}
}
readingUp = !readingUp;
x -= 2;
}
if (result.len()) != version.getTotalCodewords() as usize {
return Err(Exceptions::FORMAT);
}
Ok(result)
}
pub fn ReadQRCodewordsModel1(
bitMatrix: &BitMatrix,
version: VersionRef,
formatInfo: &FormatInformation,
) -> Result<Vec<u8>> {
let mut result = Vec::with_capacity(version.getTotalCodewords() as usize);
let dimension = bitMatrix.height();
let columns = dimension / 4 + 1 + 2;
for j in 0..columns {
if j <= 1 {
let rows = (dimension - 8) / 4;
for i in 0..rows {
if j == 0 && i % 2 == 0 && i > 0 && i < rows - 1
{
continue;
}
let x = (dimension - 1) - (j * 2);
let y = (dimension - 1) - (i * 4);
let mut currentByte = 0;
for b in 0..8 {
AppendBit(
&mut currentByte,
GetDataMaskBit(formatInfo.data_mask as u32, x - b % 2, y - (b / 2), None)?
!= getBit(bitMatrix, x - b % 2, y - (b / 2), Some(formatInfo.isMirrored)),
);
}
result.push(currentByte as u8);
}
} else if columns - j <= 4 {
let rows = (dimension - 16) / 4;
for i in 0..rows {
let x = (columns - j - 1) * 2 + 1 + (if columns - j == 4 { 1 } else { 0 }); let y = (dimension - 1) - 8 - (i * 4);
let mut currentByte = 0;
for b in 0..8 {
AppendBit(
&mut currentByte,
GetDataMaskBit(formatInfo.data_mask as u32, x - b % 2, y - (b / 2), None)?
!= getBit(bitMatrix, x - b % 2, y - (b / 2), Some(formatInfo.isMirrored)),
);
}
result.push(currentByte as u8);
}
} else {
let rows = dimension / 2;
for i in 0..rows {
if j == 2 && i >= rows - 4
{
continue;
}
if i == 0 && j % 2 == 1 && j + 1 != columns - 4
{
continue;
}
let x = (dimension - 1) - (2 * 2) - (j - 2) * 4;
let y = (dimension - 1) - (i * 2) - (if i >= rows - 3 { 1 } else { 0 }); let mut currentByte = 0;
for b in 0..8 {
AppendBit(
&mut currentByte,
GetDataMaskBit(formatInfo.data_mask as u32, x - b % 4, y - (b / 4), None)?
!= getBit(bitMatrix, x - b % 4, y - (b / 4), Some(formatInfo.isMirrored)),
);
}
result.push(currentByte as u8);
}
}
}
result[0] &= 0xf; if (result.len()) != version.getTotalCodewords() as usize {
return Err(Exceptions::FORMAT);
}
Ok(result)
}
pub fn ReadRMQRCodewords(
bitMatrix: &BitMatrix,
version: VersionRef,
formatInfo: &FormatInformation,
) -> Result<Vec<u8>> {
let functionPattern = version.buildFunctionPattern()?;
let mut result = Vec::with_capacity(version.getTotalCodewords() as usize);
let mut currentByte = 0;
let mut readingUp = true;
let mut bitsRead = 0;
let width = bitMatrix.width();
let height = bitMatrix.height();
let mut x = width as i32 - 1 - 1;
while x > 0 {
for row in 0..height {
let y = if readingUp { height - 1 - row } else { row };
for col in 0..2 {
let xx = x - col;
if !functionPattern.get(xx as u32, y) {
AppendBit(
&mut currentByte,
GetDataMaskBit(formatInfo.data_mask as u32, xx as u32, y, None)?
!= getBit(bitMatrix, xx as u32, y, Some(formatInfo.isMirrored)),
);
bitsRead += 1;
if bitsRead % 8 == 0 {
result.push(currentByte as u8);
currentByte = 0;
}
}
}
}
readingUp = !readingUp;
x -= 2
}
if (result.len()) != version.getTotalCodewords() as usize {
return Err(Exceptions::FORMAT);
}
Ok(result)
}
pub fn ReadCodewords(bitMatrix: &BitMatrix, version: VersionRef, formatInfo: &FormatInformation) -> Result<Vec<u8>> {
match version.qr_type {
Type::Model1 => ReadQRCodewordsModel1(bitMatrix, version, formatInfo),
Type::Model2 => ReadQRCodewords(bitMatrix, version, formatInfo),
Type::Micro => ReadMQRCodewords(bitMatrix, version, formatInfo),
Type::RectMicro => ReadRMQRCodewords(bitMatrix, version, formatInfo),
}
}