use rxing_one_d_proc_derive::OneDReader;
use crate::{
common::{BitArray, Result},
point_f, BarcodeFormat, Exceptions, RXingResult,
};
use super::{one_d_reader, OneDReader};
#[derive(OneDReader)]
pub struct Code93Reader {
decodeRowRXingResult: String,
counters: [u32; 6],
}
impl Default for Code93Reader {
fn default() -> Self {
Self {
decodeRowRXingResult: String::with_capacity(20),
counters: [0; 6],
}
}
}
impl OneDReader for Code93Reader {
fn decode_row(
&mut self,
rowNumber: u32,
row: &crate::common::BitArray,
_hints: &crate::DecodingHintDictionary,
) -> Result<crate::RXingResult> {
let start = self.findAsteriskPattern(row)?;
let mut nextStart = row.getNextSet(start[1]);
let end = row.get_size();
let mut theCounters = self.counters;
theCounters.fill(0);
self.decodeRowRXingResult.truncate(0);
let mut decodedChar;
let mut lastStart;
loop {
one_d_reader::record_pattern(row, nextStart, &mut theCounters)?;
let pattern = Self::toPattern(&theCounters);
if pattern < 0 {
return Err(Exceptions::NOT_FOUND);
}
decodedChar = Self::patternToChar(pattern as u32)?;
self.decodeRowRXingResult.push(decodedChar);
lastStart = nextStart;
for counter in theCounters {
nextStart += counter as usize;
}
nextStart = row.getNextSet(nextStart);
if decodedChar == '*' {
break;
}
} self.decodeRowRXingResult
.truncate(self.decodeRowRXingResult.chars().count() - 1); let lastPatternSize: u32 = theCounters.iter().sum();
if nextStart == end || !row.get(nextStart) {
return Err(Exceptions::NOT_FOUND);
}
if self.decodeRowRXingResult.chars().count() < 2 {
return Err(Exceptions::NOT_FOUND);
}
Self::checkChecksums(&self.decodeRowRXingResult)?;
self.decodeRowRXingResult
.truncate(self.decodeRowRXingResult.chars().count() - 2);
let resultString = Self::decodeExtended(&self.decodeRowRXingResult)?;
let left: f32 = (start[1] + start[0]) as f32 / 2.0;
let right: f32 = lastStart as f32 + lastPatternSize as f32 / 2.0;
let mut resultObject = RXingResult::new(
&resultString,
Vec::new(),
vec![
point_f(left, rowNumber as f32),
point_f(right, rowNumber as f32),
],
BarcodeFormat::CODE_93,
);
resultObject.putMetadata(
RXingResultMetadataType::SYMBOLOGY_IDENTIFIER,
RXingResultMetadataValue::SymbologyIdentifier("]G0".to_owned()),
);
Ok(resultObject)
}
}
impl Code93Reader {
pub const ALPHABET_STRING: &'static str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%abcd*";
pub const ALPHABET: [char; 48] = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'-', '.', ' ', '$', '/', '+', '%', 'a', 'b', 'c', 'd', '*',
];
pub const CHARACTER_ENCODINGS: [u32; 48] = [
0x114, 0x148, 0x144, 0x142, 0x128, 0x124, 0x122, 0x150, 0x112, 0x10A, 0x1A8, 0x1A4, 0x1A2, 0x194, 0x192, 0x18A, 0x168, 0x164, 0x162, 0x134, 0x11A, 0x158, 0x14C, 0x146, 0x12C, 0x116, 0x1B4, 0x1B2, 0x1AC, 0x1A6, 0x196, 0x19A, 0x16C, 0x166, 0x136, 0x13A, 0x12E, 0x1D4, 0x1D2, 0x1CA, 0x16E, 0x176, 0x1AE, 0x126, 0x1DA, 0x1D6, 0x132, 0x15E, ];
pub const ASTERISK_ENCODING: i32 = Self::CHARACTER_ENCODINGS[47] as i32;
pub fn new() -> Self {
Self {
decodeRowRXingResult: String::with_capacity(20),
counters: [0; 6],
}
}
fn findAsteriskPattern(&mut self, row: &BitArray) -> Result<[usize; 2]> {
let width = row.get_size();
let rowOffset = row.getNextSet(0);
self.counters.fill(0);
let mut theCounters = self.counters;
let mut patternStart = rowOffset;
let mut isWhite = false;
let patternLength = theCounters.len();
let mut counterPosition = 0;
for i in rowOffset..width {
if row.get(i) != isWhite {
theCounters[counterPosition] += 1;
} else {
if counterPosition == patternLength - 1 {
if Self::toPattern(&theCounters) == Self::ASTERISK_ENCODING {
return Ok([patternStart, i]);
}
patternStart += (theCounters[0] + theCounters[1]) as usize;
theCounters.copy_within(2..(counterPosition - 1 + 2), 0);
theCounters[counterPosition - 1] = 0;
theCounters[counterPosition] = 0;
counterPosition -= 1;
} else {
counterPosition += 1;
}
theCounters[counterPosition] = 1;
isWhite = !isWhite;
}
}
Err(Exceptions::NOT_FOUND)
}
fn toPattern(counters: &[u32; 6]) -> i32 {
let sum = counters.iter().sum::<u32>();
let mut pattern = 0;
let max = counters.len();
for (i, counter) in counters.iter().enumerate().take(max) {
let scaled = (*counter as f32 * 9.0 / sum as f32).round() as u32;
if !(1..=4).contains(&scaled) {
return -1;
}
if (i & 0x01) == 0 {
for _j in 0..scaled {
pattern = (pattern << 1) | 0x01;
}
} else {
pattern <<= scaled;
}
}
pattern
}
fn patternToChar(pattern: u32) -> Result<char> {
for i in 0..Self::CHARACTER_ENCODINGS.len() {
if Self::CHARACTER_ENCODINGS[i] == pattern {
return Ok(Self::ALPHABET[i]);
}
}
Err(Exceptions::NOT_FOUND)
}
fn decodeExtended(encoded: &str) -> Result<String> {
let length = encoded.chars().count();
let mut decoded = String::with_capacity(length);
let mut i = 0;
while i < length {
let c = encoded
.chars()
.nth(i)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
if ('a'..='d').contains(&c) {
if i >= length - 1 {
return Err(Exceptions::FORMAT);
}
let next = encoded
.chars()
.nth(i + 1)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
let mut decodedChar = '\0';
match c {
'd' => {
if next.is_ascii_uppercase() {
decodedChar =
char::from_u32(next as u32 + 32).ok_or(Exceptions::PARSE)?;
} else {
return Err(Exceptions::FORMAT);
}
}
'a' => {
if next.is_ascii_uppercase() {
decodedChar =
char::from_u32(next as u32 - 64).ok_or(Exceptions::PARSE)?;
} else {
return Err(Exceptions::FORMAT);
}
}
'b' => {
if ('A'..='E').contains(&next) {
decodedChar =
char::from_u32(next as u32 - 38).ok_or(Exceptions::PARSE)?;
} else if ('F'..='J').contains(&next) {
decodedChar =
char::from_u32(next as u32 - 11).ok_or(Exceptions::PARSE)?;
} else if ('K'..='O').contains(&next) {
decodedChar =
char::from_u32(next as u32 + 16).ok_or(Exceptions::PARSE)?;
} else if ('P'..='T').contains(&next) {
decodedChar =
char::from_u32(next as u32 + 43).ok_or(Exceptions::PARSE)?;
} else if next == 'U' {
decodedChar = '\0';
} else if next == 'V' {
decodedChar = '@';
} else if next == 'W' {
decodedChar = '`';
} else if ('X'..='Z').contains(&next) {
decodedChar = 127 as char;
} else {
return Err(Exceptions::FORMAT);
}
}
'c' => {
if ('A'..='O').contains(&next) {
decodedChar =
char::from_u32(next as u32 - 32).ok_or(Exceptions::PARSE)?;
} else if next == 'Z' {
decodedChar = ':';
} else {
return Err(Exceptions::FORMAT);
}
}
_ => {}
}
decoded.push(decodedChar);
i += 1;
} else {
decoded.push(c);
}
i += 1;
}
Ok(decoded)
}
fn checkChecksums(result: &str) -> Result<()> {
let length = result.chars().count();
Self::checkOneChecksum(result, length - 2, 20)?;
Self::checkOneChecksum(result, length - 1, 15)?;
Ok(())
}
fn checkOneChecksum(result: &str, checkPosition: usize, weightMax: u32) -> Result<()> {
let mut weight = 1;
let mut total = 0;
for i in (0..checkPosition).rev() {
total += weight
* Self::ALPHABET_STRING
.find(
result
.chars()
.nth(i)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?,
)
.map_or_else(|| -1_i32, |v| v as i32);
weight += 1;
if weight > weightMax as i32 {
weight = 1;
}
}
if result
.chars()
.nth(checkPosition)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
!= Self::ALPHABET[(total as usize) % 47]
{
Err(Exceptions::CHECKSUM)
} else {
Ok(())
}
}
}
#[cfg(test)]
mod Code93ReaderTestCase {
use std::collections::HashMap;
use crate::{common::BitMatrix, oned::OneDReader};
use super::Code93Reader;
#[test]
fn testDecode() {
doTest("Code93!\n$%/+ :\u{001b};[{\u{007f}\u{0000}@`\u{007f}\u{007f}\u{007f}",
"0000001010111101101000101001100101001011001001100101100101001001100101100100101000010101010000101110101101101010001001001101001101001110010101101011101011011101011101101110100101110101101001110101110110101101010001110110101100010101110110101000110101110110101000101101110110101101001101110110101100101101110110101100110101110110101011011001110110101011001101110110101001101101110110101001110101001100101101010001010111101111");
}
fn doTest(expectedRXingResult: &str, encodedRXingResult: &str) {
let mut sut = Code93Reader::new();
let matrix = BitMatrix::parse_strings(encodedRXingResult, "1", "0").expect("must parse");
let row = matrix.getRow(0);
let result = sut
.decode_row(0, &row, &HashMap::new())
.expect("must decode");
assert_eq!(expectedRXingResult, result.getText());
}
}