datamatrix decoder (few tests)

This commit is contained in:
Henry Schimke
2022-11-22 07:22:46 -06:00
parent 8573212ef6
commit 325fdcfda0
7 changed files with 752 additions and 629 deletions

View File

@@ -166,6 +166,12 @@ impl ECIStringBuilder {
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
return self.current_bytes.is_empty() && self.result.is_empty(); return self.current_bytes.is_empty() && self.result.is_empty();
} }
pub fn build_result(mut self) -> Self {
self.encodeCurrentBytesIfAny();
self
}
} }
impl fmt::Display for ECIStringBuilder { impl fmt::Display for ECIStringBuilder {

View File

@@ -1,47 +0,0 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.datamatrix.decoder;
import org.junit.Assert;
import org.junit.Test;
/**
* @author bbrown@google.com (Brian Brown)
*/
public final class DecodedBitStreamParserTestCase extends Assert {
@Test
public void testAsciiStandardDecode() throws Exception {
// ASCII characters 0-127 are encoded as the value + 1
byte[] bytes = {(byte) ('a' + 1), (byte) ('b' + 1), (byte) ('c' + 1),
(byte) ('A' + 1), (byte) ('B' + 1), (byte) ('C' + 1)};
String decodedString = DecodedBitStreamParser.decode(bytes).getText();
assertEquals("abcABC", decodedString);
}
@Test
public void testAsciiDoubleDigitDecode() throws Exception {
// ASCII double digit (00 - 99) Numeric Value + 130
byte[] bytes = {(byte) 130 , (byte) (1 + 130),
(byte) (98 + 130), (byte) (99 + 130)};
String decodedString = DecodedBitStreamParser.decode(bytes).getText();
assertEquals("00019899", decodedString);
}
// TODO(bbrown): Add test cases for each encoding type
// TODO(bbrown): Add test cases for switching encoding types
}

View File

@@ -78,20 +78,20 @@ impl BitMatrixParser {
* @return bytes encoded within the Data Matrix Code * @return bytes encoded within the Data Matrix Code
* @throws FormatException if the exact number of bytes expected is not read * @throws FormatException if the exact number of bytes expected is not read
*/ */
pub fn readCodewords(&self) -> Result<Vec<u8>, Exceptions> { pub fn readCodewords(&mut self) -> Result<Vec<u8>, Exceptions> {
let result = vec![0u8; self.version.getTotalCodewords() as usize]; let mut result = vec![0u8; self.version.getTotalCodewords() as usize];
let resultOffset = 0; let mut resultOffset = 0;
let row = 4; let mut row = 4;
let column = 0_usize; let mut column = 0_usize;
let numRows = self.mappingBitMatrix.getHeight() as usize; let numRows = self.mappingBitMatrix.getHeight() as usize;
let numColumns = self.mappingBitMatrix.getWidth() as usize; let numColumns = self.mappingBitMatrix.getWidth() as usize;
let corner1Read = false; let mut corner1Read = false;
let corner2Read = false; let mut corner2Read = false;
let corner3Read = false; let mut corner3Read = false;
let corner4Read = false; let mut corner4Read = false;
// Read all of the codewords // Read all of the codewords
loop { loop {
@@ -193,7 +193,9 @@ impl BitMatrixParser {
* @param numColumns Number of columns in the mapping matrix * @param numColumns Number of columns in the mapping matrix
* @return value of the given bit in the mapping matrix * @return value of the given bit in the mapping matrix
*/ */
fn readModule(&self, row: usize, column: usize, numRows: usize, numColumns: usize) -> bool { fn readModule(&mut self, row: usize, column: usize, numRows: usize, numColumns: usize) -> bool {
let mut row = row;
let mut column = column;
// Adjust the row and column indices based on boundary wrapping // Adjust the row and column indices based on boundary wrapping
if row < 0 { if row < 0 {
row += numRows; row += numRows;
@@ -222,7 +224,7 @@ impl BitMatrixParser {
* @param numColumns Number of columns in the mapping matrix * @param numColumns Number of columns in the mapping matrix
* @return byte from the utah shape * @return byte from the utah shape
*/ */
fn readUtah(&self, row: usize, column: usize, numRows: usize, numColumns: usize) -> u32 { fn readUtah(&mut self, row: usize, column: usize, numRows: usize, numColumns: usize) -> u32 {
let mut currentByte = 0; let mut currentByte = 0;
if self.readModule(row - 2, column - 2, numRows, numColumns) { if self.readModule(row - 2, column - 2, numRows, numColumns) {
currentByte |= 1; currentByte |= 1;
@@ -267,7 +269,7 @@ impl BitMatrixParser {
* @param numColumns Number of columns in the mapping matrix * @param numColumns Number of columns in the mapping matrix
* @return byte from the Corner condition 1 * @return byte from the Corner condition 1
*/ */
fn readCorner1(&self, numRows: usize, numColumns: usize) -> u32 { fn readCorner1(&mut self, numRows: usize, numColumns: usize) -> u32 {
let mut currentByte = 0; let mut currentByte = 0;
if self.readModule(numRows - 1, 0, numRows, numColumns) { if self.readModule(numRows - 1, 0, numRows, numColumns) {
currentByte |= 1; currentByte |= 1;
@@ -312,7 +314,7 @@ impl BitMatrixParser {
* @param numColumns Number of columns in the mapping matrix * @param numColumns Number of columns in the mapping matrix
* @return byte from the Corner condition 2 * @return byte from the Corner condition 2
*/ */
fn readCorner2(&self, numRows: usize, numColumns: usize) -> u32 { fn readCorner2(&mut self, numRows: usize, numColumns: usize) -> u32 {
let mut currentByte = 0; let mut currentByte = 0;
if self.readModule(numRows - 3, 0, numRows, numColumns) { if self.readModule(numRows - 3, 0, numRows, numColumns) {
currentByte |= 1; currentByte |= 1;
@@ -357,7 +359,7 @@ impl BitMatrixParser {
* @param numColumns Number of columns in the mapping matrix * @param numColumns Number of columns in the mapping matrix
* @return byte from the Corner condition 3 * @return byte from the Corner condition 3
*/ */
fn readCorner3(&self, numRows: usize, numColumns: usize) -> u32 { fn readCorner3(&mut self, numRows: usize, numColumns: usize) -> u32 {
let mut currentByte = 0; let mut currentByte = 0;
if self.readModule(numRows - 1, 0, numRows, numColumns) { if self.readModule(numRows - 1, 0, numRows, numColumns) {
currentByte |= 1; currentByte |= 1;
@@ -402,7 +404,7 @@ impl BitMatrixParser {
* @param numColumns Number of columns in the mapping matrix * @param numColumns Number of columns in the mapping matrix
* @return byte from the Corner condition 4 * @return byte from the Corner condition 4
*/ */
fn readCorner4(&self, numRows: usize, numColumns: usize) -> u32 { fn readCorner4(&mut self, numRows: usize, numColumns: usize) -> u32 {
let mut currentByte = 0; let mut currentByte = 0;
if self.readModule(numRows - 3, 0, numRows, numColumns) { if self.readModule(numRows - 3, 0, numRows, numColumns) {
currentByte |= 1; currentByte |= 1;
@@ -467,7 +469,8 @@ impl BitMatrixParser {
let sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows; let sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows;
let sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns; let sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns;
let bitMatrixWithoutAlignment = BitMatrix::new(sizeDataRegionColumn, sizeDataRegionRow)?; let mut bitMatrixWithoutAlignment =
BitMatrix::new(sizeDataRegionColumn, sizeDataRegionRow)?;
for dataRegionRow in 0..numDataRegionsRow { for dataRegionRow in 0..numDataRegionsRow {
// for (int dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) { // for (int dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) {
let dataRegionRowOffset = dataRegionRow * dataRegionSizeRows; let dataRegionRowOffset = dataRegionRow * dataRegionSizeRows;

File diff suppressed because it is too large Load Diff

View File

@@ -14,10 +14,15 @@
* limitations under the License. * limitations under the License.
*/ */
use crate::{common::{reedsolomon::{ReedSolomonDecoder, get_predefined_genericgf, PredefinedGenericGF}, DecoderRXingResult, BitMatrix}, Exceptions}; use crate::{
common::{
use super::{DataBlock, BitMatrixParser, decoded_bit_stream_parser}; reedsolomon::{get_predefined_genericgf, PredefinedGenericGF, ReedSolomonDecoder},
BitMatrix, DecoderRXingResult,
},
Exceptions,
};
use super::{decoded_bit_stream_parser, BitMatrixParser, DataBlock};
/** /**
* <p>The main class which implements Data Matrix Code decoding -- as opposed to locating and extracting * <p>The main class which implements Data Matrix Code decoding -- as opposed to locating and extracting
@@ -28,10 +33,10 @@ use super::{DataBlock, BitMatrixParser, decoded_bit_stream_parser};
pub struct Decoder(ReedSolomonDecoder); pub struct Decoder(ReedSolomonDecoder);
impl Decoder { impl Decoder {
pub fn new() -> Self { pub fn new() -> Self {
Self(ReedSolomonDecoder::new(get_predefined_genericgf(
Self(ReedSolomonDecoder::new(get_predefined_genericgf(PredefinedGenericGF::DataMatrixField256))) PredefinedGenericGF::DataMatrixField256,
)))
// rsDecoder = new ReedSolomonDecoder(GenericGF.DATA_MATRIX_FIELD_256); // rsDecoder = new ReedSolomonDecoder(GenericGF.DATA_MATRIX_FIELD_256);
} }
@@ -44,7 +49,7 @@ impl Decoder {
* @throws FormatException if the Data Matrix Code cannot be decoded * @throws FormatException if the Data Matrix Code cannot be decoded
* @throws ChecksumException if error correction fails * @throws ChecksumException if error correction fails
*/ */
pub fn decode_bools(&self, image:&Vec<Vec<bool>>) -> Result<DecoderRXingResult,Exceptions> { pub fn decode_bools(&self, image: &Vec<Vec<bool>>) -> Result<DecoderRXingResult, Exceptions> {
self.decode(&BitMatrix::parse_bools(&image)) self.decode(&BitMatrix::parse_bools(&image))
} }
@@ -57,33 +62,33 @@ impl Decoder {
* @throws FormatException if the Data Matrix Code cannot be decoded * @throws FormatException if the Data Matrix Code cannot be decoded
* @throws ChecksumException if error correction fails * @throws ChecksumException if error correction fails
*/ */
pub fn decode(&self, bits:&BitMatrix) -> Result<DecoderRXingResult,Exceptions> { pub fn decode(&self, bits: &BitMatrix) -> Result<DecoderRXingResult, Exceptions> {
// Construct a parser and read version, error-correction level // Construct a parser and read version, error-correction level
let parser = BitMatrixParser::new(bits)?; let mut parser = BitMatrixParser::new(bits)?;
let version = parser.getVersion();
// Read codewords // Read codewords
let codewords = parser.readCodewords()?; let codewords = parser.readCodewords()?;
let version = parser.getVersion();
// Separate into data blocks // Separate into data blocks
let dataBlocks = DataBlock::getDataBlocks(&codewords, version)?; let dataBlocks = DataBlock::getDataBlocks(&codewords, &version)?;
// Count total number of data bytes // Count total number of data bytes
let totalBytes = 0; let mut totalBytes = 0;
for db in dataBlocks { for db in &dataBlocks {
totalBytes += db.getNumDataCodewords(); totalBytes += db.getNumDataCodewords();
} }
let resultBytes = vec![0u8;totalBytes as usize]; let mut resultBytes = vec![0u8; totalBytes as usize];
let dataBlocksCount = dataBlocks.len(); let dataBlocksCount = dataBlocks.len();
// Error-correct and copy data blocks together into a stream of bytes // Error-correct and copy data blocks together into a stream of bytes
for j in 0..dataBlocksCount { for j in 0..dataBlocksCount {
// for (int j = 0; j < dataBlocksCount; j++) { // for (int j = 0; j < dataBlocksCount; j++) {
let dataBlock = dataBlocks[j]; let dataBlock = &dataBlocks[j];
let codewordBytes = dataBlock.getCodewords(); let mut codewordBytes = dataBlock.getCodewords().to_vec();
let numDataCodewords = dataBlock.getNumDataCodewords() as usize; let numDataCodewords = dataBlock.getNumDataCodewords() as usize;
self.correctErrors(codewordBytes, numDataCodewords as u32); self.correctErrors(&mut codewordBytes, numDataCodewords as u32)?;
for i in 0..numDataCodewords { for i in 0..numDataCodewords {
// for (int i = 0; i < numDataCodewords; i++) { // for (int i = 0; i < numDataCodewords; i++) {
// De-interlace data blocks. // De-interlace data blocks.
@@ -103,18 +108,25 @@ impl Decoder {
* @param numDataCodewords number of codewords that are data bytes * @param numDataCodewords number of codewords that are data bytes
* @throws ChecksumException if error correction fails * @throws ChecksumException if error correction fails
*/ */
fn correctErrors(&self, codewordBytes:&[u8], numDataCodewords:u32) -> Result<(),Exceptions> { fn correctErrors(
let numCodewords = codewordBytes.len(); &self,
codewordBytes: &mut [u8],
numDataCodewords: u32,
) -> Result<(), Exceptions> {
let _numCodewords = codewordBytes.len();
// First read into an array of ints // First read into an array of ints
// let codewordsInts = vec![0i32;numCodewords]; // let codewordsInts = vec![0i32;numCodewords];
// for i in 0..numCodewords { // for i in 0..numCodewords {
// // for (int i = 0; i < numCodewords; i++) { // // for (int i = 0; i < numCodewords; i++) {
// codewordsInts[i] = codewordBytes[i]; // codewordsInts[i] = codewordBytes[i];
// } // }
let codewordsInts : Vec<i32> = codewordBytes.iter().map(|x| *x as i32).collect(); let mut codewordsInts: Vec<i32> = codewordBytes.iter().map(|x| *x as i32).collect();
//try { //try {
self.0.decode(&mut codewordsInts, codewordBytes.len() as i32- numDataCodewords as i32)?; self.0.decode(
&mut codewordsInts,
codewordBytes.len() as i32 - numDataCodewords as i32,
)?;
//} catch (ReedSolomonException ignored) { //} catch (ReedSolomonException ignored) {
//throw ChecksumException.getChecksumInstance(); //throw ChecksumException.getChecksumInstance();
//} //}
@@ -127,5 +139,4 @@ impl Decoder {
// codewordsInts.into_iter().take(numDataCodewords as usize).map(|x| x as u8).collect::<Vec<u8>>() // codewordsInts.into_iter().take(numDataCodewords as usize).map(|x| x as u8).collect::<Vec<u8>>()
Ok(()) Ok(())
} }
} }

View File

@@ -1,12 +1,11 @@
mod bit_matrix_parser;
mod version;
mod data_block; mod data_block;
mod decoder; mod decoder;
mod bit_matrix_parser; mod version;
pub use version::*; pub use bit_matrix_parser::*;
pub use data_block::*; pub use data_block::*;
pub use decoder::*; pub use decoder::*;
pub use bit_matrix_parser::*; pub use version::*;
pub mod decoded_bit_stream_parser; pub mod decoded_bit_stream_parser;

View File

@@ -132,7 +132,7 @@ impl EdifactEncoder {
} else if c >= '@' && c <= '^' { } else if c >= '@' && c <= '^' {
sb.push((c as u8 - 64) as char); sb.push((c as u8 - 64) as char);
} else { } else {
high_level_encoder::illegalCharacter(c); high_level_encoder::illegalCharacter(c).expect("");
} }
} }