Initial generics

This commit is contained in:
Steve Cook
2023-03-01 22:08:12 -05:00
parent 26325928e7
commit a9bc58108c
65 changed files with 1013 additions and 883 deletions

View File

@@ -39,43 +39,43 @@ fn testAppendBit() {
assert_eq!(0, v.getSizeInBytes());
// 1
v.appendBit(true);
assert_eq!(1, v.getSize());
assert_eq!(1, v.get_size());
assert_eq!(0x80000000, getUnsignedInt(&v));
// 10
v.appendBit(false);
assert_eq!(2, v.getSize());
assert_eq!(2, v.get_size());
assert_eq!(0x80000000, getUnsignedInt(&v));
// 101
v.appendBit(true);
assert_eq!(3, v.getSize());
assert_eq!(3, v.get_size());
assert_eq!(0xa0000000, getUnsignedInt(&v));
// 1010
v.appendBit(false);
assert_eq!(4, v.getSize());
assert_eq!(4, v.get_size());
assert_eq!(0xa0000000, getUnsignedInt(&v));
// 10101
v.appendBit(true);
assert_eq!(5, v.getSize());
assert_eq!(5, v.get_size());
assert_eq!(0xa8000000, getUnsignedInt(&v));
// 101010
v.appendBit(false);
assert_eq!(6, v.getSize());
assert_eq!(6, v.get_size());
assert_eq!(0xa8000000, getUnsignedInt(&v));
// 1010101
v.appendBit(true);
assert_eq!(7, v.getSize());
assert_eq!(7, v.get_size());
assert_eq!(0xaa000000, getUnsignedInt(&v));
// 10101010
v.appendBit(false);
assert_eq!(8, v.getSize());
assert_eq!(8, v.get_size());
assert_eq!(0xaa000000, getUnsignedInt(&v));
// 10101010 1
v.appendBit(true);
assert_eq!(9, v.getSize());
assert_eq!(9, v.get_size());
assert_eq!(0xaa800000, getUnsignedInt(&v));
// 10101010 10
v.appendBit(false);
assert_eq!(10, v.getSize());
assert_eq!(10, v.get_size());
assert_eq!(0xaa800000, getUnsignedInt(&v));
}
@@ -83,15 +83,15 @@ fn testAppendBit() {
fn testAppendBits() {
let mut v = BitArray::new();
v.appendBits(0x1, 1).expect("append");
assert_eq!(1, v.getSize());
assert_eq!(1, v.get_size());
assert_eq!(0x80000000, getUnsignedInt(&v));
let mut v = BitArray::new();
v.appendBits(0xff, 8).expect("append");
assert_eq!(8, v.getSize());
assert_eq!(8, v.get_size());
assert_eq!(0xff000000, getUnsignedInt(&v));
let mut v = BitArray::new();
v.appendBits(0xff7, 12).expect("append");
assert_eq!(12, v.getSize());
assert_eq!(12, v.get_size());
assert_eq!(0xff700000, getUnsignedInt(&v));
}

View File

@@ -174,11 +174,11 @@ pub fn embedTypeInfo(
for (i, coordinates) in TYPE_INFO_COORDINATES
.iter()
.enumerate()
.take(typeInfoBits.getSize())
.take(typeInfoBits.get_size())
{
// Place bits in LSB to MSB order. LSB (least significant bit) is the last value in
// "typeInfoBits".
let bit = typeInfoBits.get(typeInfoBits.getSize() - 1 - i);
let bit = typeInfoBits.get(typeInfoBits.get_size() - 1 - i);
// Type info bits at the left top corner. See 8.9 of JISX0510:2004 (p.46).
// let coordinates = TYPE_INFO_COORDINATES[i];
@@ -249,7 +249,7 @@ pub fn embedDataBits(dataBits: &BitArray, maskPattern: i32, matrix: &mut ByteMat
continue;
}
let mut bit;
if bitIndex < dataBits.getSize() {
if bitIndex < dataBits.get_size() {
bit = dataBits.get(bitIndex);
bitIndex += 1;
} else {
@@ -273,11 +273,11 @@ pub fn embedDataBits(dataBits: &BitArray, maskPattern: i32, matrix: &mut ByteMat
x -= 2; // Move to the left.
}
// All bits should be consumed.
if bitIndex != dataBits.getSize() {
if bitIndex != dataBits.get_size() {
return Err(Exceptions::writer_with(format!(
"Not all bits consumed: {}/{}",
bitIndex,
dataBits.getSize()
dataBits.get_size()
)));
}
Ok(())
@@ -355,11 +355,11 @@ pub fn makeTypeInfoBits(
maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15)?;
bits.xor(&maskBits)?;
if bits.getSize() != 15 {
if bits.get_size() != 15 {
// Just in case.
return Err(Exceptions::writer_with(format!(
"should not happen but we got: {}",
bits.getSize()
bits.get_size()
)));
}
Ok(())
@@ -372,11 +372,11 @@ pub fn makeVersionInfoBits(version: &Version, bits: &mut BitArray) -> Result<()>
let bchCode = calculateBCHCode(version.getVersionNumber(), VERSION_INFO_POLY)?;
bits.appendBits(bchCode, 12)?;
if bits.getSize() != 18 {
if bits.get_size() != 18 {
// Just in case.
return Err(Exceptions::writer_with(format!(
"should not happen but we got: {}",
bits.getSize()
bits.get_size()
)));
}
Ok(())

View File

@@ -286,7 +286,7 @@ fn calculateBitsNeeded(
data_bits: &BitArray,
version: VersionRef,
) -> u32 {
(header_bits.getSize() + mode.getCharacterCountBits(version) as usize + data_bits.getSize())
(header_bits.get_size() + mode.getCharacterCountBits(version) as usize + data_bits.get_size())
as u32
}
@@ -413,21 +413,21 @@ pub fn willFit(numInputBits: u32, version: VersionRef, ecLevel: &ErrorCorrection
*/
pub fn terminateBits(num_data_bytes: u32, bits: &mut BitArray) -> Result<()> {
let capacity = num_data_bytes * 8;
if bits.getSize() > capacity as usize {
if bits.get_size() > capacity as usize {
return Err(Exceptions::writer_with(format!(
"data bits cannot fit in the QR Code{capacity} > "
)));
}
// Append Mode.TERMINATE if there is enough space (value is 0000)
for _i in 0..4 {
if bits.getSize() >= capacity as usize {
if bits.get_size() >= capacity as usize {
break;
}
bits.appendBit(false);
}
// Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details.
// If the last byte isn't 8-bit aligned, we'll add padding bits.
let num_bits_in_last_byte = bits.getSize() & 0x07;
let num_bits_in_last_byte = bits.get_size() & 0x07;
if num_bits_in_last_byte > 0 {
for _i in num_bits_in_last_byte..8 {
bits.appendBit(false);
@@ -441,7 +441,7 @@ pub fn terminateBits(num_data_bytes: u32, bits: &mut BitArray) -> Result<()> {
}
bits.appendBits(if (i & 0x01) == 0 { 0xEC } else { 0x11 }, 8)?;
}
if bits.getSize() != capacity as usize {
if bits.get_size() != capacity as usize {
return Err(Exceptions::writer_with("Bits size does not equal capacity"));
}
Ok(())

View File

@@ -18,7 +18,7 @@ use std::collections::HashMap;
use crate::{
common::{BitMatrix, DecoderRXingResult, DetectorRXingResult, Result},
BarcodeFormat, DecodeHintType, DecodeHintValue, Exceptions, Point, RXingResult,
BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, Exceptions, Point, RXingResult,
RXingResultMetadataType, RXingResultMetadataValue, Reader,
};
@@ -48,27 +48,27 @@ impl Reader for QRCodeReader {
* @throws FormatException if a QR code cannot be decoded
* @throws ChecksumException if error correction fails
*/
fn decode(&mut self, image: &mut crate::BinaryBitmap) -> Result<crate::RXingResult> {
fn decode<B: Binarizer>(&mut self, image: &mut crate::BinaryBitmap<B>) -> Result<RXingResult> {
self.decode_with_hints(image, &HashMap::new())
}
fn decode_with_hints(
fn decode_with_hints<B: Binarizer>(
&mut self,
image: &mut crate::BinaryBitmap,
image: &mut crate::BinaryBitmap<B>,
hints: &crate::DecodingHintDictionary,
) -> Result<crate::RXingResult> {
) -> Result<RXingResult> {
let decoderRXingResult: DecoderRXingResult;
let mut points: Vec<Point>;
if matches!(
hints.get(&DecodeHintType::PURE_BARCODE),
Some(DecodeHintValue::PureBarcode(true))
) {
let bits = Self::extractPureBits(image.getBlackMatrix())?;
let bits = Self::extractPureBits(image.get_black_matrix())?;
decoderRXingResult = qrcode_decoder::decode_bitmatrix_with_hints(&bits, hints)?;
points = Vec::new();
} else {
let detectorRXingResult =
Detector::new(image.getBlackMatrix()).detect_with_hints(hints)?;
Detector::new(image.get_black_matrix()).detect_with_hints(hints)?;
decoderRXingResult =
qrcode_decoder::decode_bitmatrix_with_hints(detectorRXingResult.getBits(), hints)?;
points = detectorRXingResult.getPoints().to_vec();