mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-28 05:12:34 +00:00
bitmatrix ported untested
This commit is contained in:
@@ -14,16 +14,14 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::{Exceptions, common::BitMatrix};
|
use crate::{common::BitMatrix, Exceptions};
|
||||||
|
|
||||||
use super::{Version, FormatInformation, VersionRef, DataMask};
|
|
||||||
|
|
||||||
|
use super::{DataMask, FormatInformation, Version, VersionRef};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Sean Owen
|
* @author Sean Owen
|
||||||
*/
|
*/
|
||||||
pub struct BitMatrixParser {
|
pub struct BitMatrixParser {
|
||||||
|
|
||||||
bitMatrix: BitMatrix,
|
bitMatrix: BitMatrix,
|
||||||
parsedVersion: Option<VersionRef>,
|
parsedVersion: Option<VersionRef>,
|
||||||
parsedFormatInfo: Option<FormatInformation>,
|
parsedFormatInfo: Option<FormatInformation>,
|
||||||
@@ -31,7 +29,6 @@ pub struct BitMatrixParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BitMatrixParser {
|
impl BitMatrixParser {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bitMatrix {@link BitMatrix} to parse
|
* @param bitMatrix {@link BitMatrix} to parse
|
||||||
* @throws FormatException if dimension is not >= 21 and 1 mod 4
|
* @throws FormatException if dimension is not >= 21 and 1 mod 4
|
||||||
@@ -39,13 +36,16 @@ impl BitMatrixParser {
|
|||||||
pub fn new(bit_matrix: BitMatrix) -> Result<Self, Exceptions> {
|
pub fn new(bit_matrix: BitMatrix) -> Result<Self, Exceptions> {
|
||||||
let dimension = bit_matrix.getHeight();
|
let dimension = bit_matrix.getHeight();
|
||||||
if dimension < 21 || (dimension & 0x03) != 1 {
|
if dimension < 21 || (dimension & 0x03) != 1 {
|
||||||
Err(Exceptions::FormatException(format!("{} < 21 || ({} % 0x03) != 1", dimension,dimension)))
|
Err(Exceptions::FormatException(format!(
|
||||||
|
"{} < 21 || ({} % 0x03) != 1",
|
||||||
|
dimension, dimension
|
||||||
|
)))
|
||||||
} else {
|
} else {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
bitMatrix: bit_matrix,
|
bitMatrix: bit_matrix,
|
||||||
parsedVersion: None,
|
parsedVersion: None,
|
||||||
parsedFormatInfo: None,
|
parsedFormatInfo: None,
|
||||||
mirror: false
|
mirror: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,14 +57,13 @@ impl BitMatrixParser {
|
|||||||
* @throws FormatException if both format information locations cannot be parsed as
|
* @throws FormatException if both format information locations cannot be parsed as
|
||||||
* the valid encoding of format information
|
* the valid encoding of format information
|
||||||
*/
|
*/
|
||||||
pub fn readFormatInformation(&self) -> Result<&FormatInformation,Exceptions>{
|
pub fn readFormatInformation(&mut self) -> Result<&FormatInformation, Exceptions> {
|
||||||
|
if self.parsedFormatInfo.is_some() {
|
||||||
if let Some(pfi) = self.parsedFormatInfo {
|
return Ok(&self.parsedFormatInfo.as_ref().unwrap());
|
||||||
return Ok(&pfi)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read top-left format info bits
|
// Read top-left format info bits
|
||||||
let formatInfoBits1 = 0;
|
let mut formatInfoBits1 = 0;
|
||||||
for i in 0..6 {
|
for i in 0..6 {
|
||||||
// for (int i = 0; i < 6; i++) {
|
// for (int i = 0; i < 6; i++) {
|
||||||
formatInfoBits1 = self.copyBit(i, 8, formatInfoBits1);
|
formatInfoBits1 = self.copyBit(i, 8, formatInfoBits1);
|
||||||
@@ -81,7 +80,7 @@ impl BitMatrixParser {
|
|||||||
|
|
||||||
// Read the top-right/bottom-left pattern too
|
// Read the top-right/bottom-left pattern too
|
||||||
let dimension = self.bitMatrix.getHeight();
|
let dimension = self.bitMatrix.getHeight();
|
||||||
let formatInfoBits2 = 0;
|
let mut formatInfoBits2 = 0;
|
||||||
let jMin = dimension - 7;
|
let jMin = dimension - 7;
|
||||||
for j in (jMin..=dimension).rev() {
|
for j in (jMin..=dimension).rev() {
|
||||||
// for (int j = dimension - 1; j >= jMin; j--) {
|
// for (int j = dimension - 1; j >= jMin; j--) {
|
||||||
@@ -92,9 +91,10 @@ impl BitMatrixParser {
|
|||||||
formatInfoBits2 = self.copyBit(i, 8, formatInfoBits2);
|
formatInfoBits2 = self.copyBit(i, 8, formatInfoBits2);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.parsedFormatInfo = FormatInformation::decodeFormatInformation(formatInfoBits1, formatInfoBits2);
|
self.parsedFormatInfo =
|
||||||
if let Some(pfi) = self.parsedFormatInfo {
|
FormatInformation::decodeFormatInformation(formatInfoBits1, formatInfoBits2);
|
||||||
return Ok(&pfi)
|
if let Some(pfi) = &self.parsedFormatInfo {
|
||||||
|
return Ok(pfi);
|
||||||
}
|
}
|
||||||
Err(Exceptions::FormatException("".to_owned()))
|
Err(Exceptions::FormatException("".to_owned()))
|
||||||
}
|
}
|
||||||
@@ -106,21 +106,20 @@ impl BitMatrixParser {
|
|||||||
* @throws FormatException if both version information locations cannot be parsed as
|
* @throws FormatException if both version information locations cannot be parsed as
|
||||||
* the valid encoding of version information
|
* the valid encoding of version information
|
||||||
*/
|
*/
|
||||||
pub fn readVersion(&self) -> Result<VersionRef,Exceptions> {
|
pub fn readVersion(&mut self) -> Result<VersionRef, Exceptions> {
|
||||||
|
|
||||||
if let Some(pv) = self.parsedVersion {
|
if let Some(pv) = self.parsedVersion {
|
||||||
return Ok(&pv)
|
return Ok(&pv);
|
||||||
}
|
}
|
||||||
|
|
||||||
let dimension = self.bitMatrix.getHeight();
|
let dimension = self.bitMatrix.getHeight();
|
||||||
|
|
||||||
let provisionalVersion = (dimension - 17) / 4;
|
let provisionalVersion = (dimension - 17) / 4;
|
||||||
if provisionalVersion <= 6 {
|
if provisionalVersion <= 6 {
|
||||||
return Version::getVersionForNumber(provisionalVersion)
|
return Version::getVersionForNumber(provisionalVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read top-right version info: 3 wide by 6 tall
|
// Read top-right version info: 3 wide by 6 tall
|
||||||
let versionBits = 0;
|
let mut versionBits = 0;
|
||||||
let ijMin = dimension - 11;
|
let ijMin = dimension - 11;
|
||||||
for j in (0..=5).rev() {
|
for j in (0..=5).rev() {
|
||||||
// for (int j = 5; j >= 0; j--) {
|
// for (int j = 5; j >= 0; j--) {
|
||||||
@@ -133,8 +132,9 @@ impl BitMatrixParser {
|
|||||||
if let Ok(theParsedVersion) = Version::decodeVersionInformation(versionBits) {
|
if let Ok(theParsedVersion) = Version::decodeVersionInformation(versionBits) {
|
||||||
if theParsedVersion.getDimensionForVersion() == dimension {
|
if theParsedVersion.getDimensionForVersion() == dimension {
|
||||||
self.parsedVersion = Some(theParsedVersion);
|
self.parsedVersion = Some(theParsedVersion);
|
||||||
return Ok(theParsedVersion)
|
return Ok(theParsedVersion);
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Hmm, failed. Try bottom left: 6 wide by 3 tall
|
// Hmm, failed. Try bottom left: 6 wide by 3 tall
|
||||||
versionBits = 0;
|
versionBits = 0;
|
||||||
@@ -149,14 +149,23 @@ impl BitMatrixParser {
|
|||||||
if let Ok(theParsedVersion) = Version::decodeVersionInformation(versionBits) {
|
if let Ok(theParsedVersion) = Version::decodeVersionInformation(versionBits) {
|
||||||
if theParsedVersion.getDimensionForVersion() == dimension {
|
if theParsedVersion.getDimensionForVersion() == dimension {
|
||||||
self.parsedVersion = Some(theParsedVersion);
|
self.parsedVersion = Some(theParsedVersion);
|
||||||
return Ok(theParsedVersion)
|
return Ok(theParsedVersion);
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
Err(Exceptions::FormatException("".to_owned()))
|
Err(Exceptions::FormatException("".to_owned()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copyBit(&self, i: u32, j: u32, versionBits: u32) -> u32 {
|
fn copyBit(&self, i: u32, j: u32, versionBits: u32) -> u32 {
|
||||||
let bit = if self.mirror {self.bitMatrix.get(j, i)} else {self.bitMatrix.get(i, j)};
|
let bit = if self.mirror {
|
||||||
if bit {(versionBits << 1) | 0x1} else {versionBits << 1}
|
self.bitMatrix.get(j, i)
|
||||||
|
} else {
|
||||||
|
self.bitMatrix.get(i, j)
|
||||||
|
};
|
||||||
|
if bit {
|
||||||
|
(versionBits << 1) | 0x1
|
||||||
|
} else {
|
||||||
|
versionBits << 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -167,24 +176,23 @@ impl BitMatrixParser {
|
|||||||
* @return bytes encoded within the QR Code
|
* @return bytes encoded within the QR 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 formatInfo = self.readFormatInformation()?;
|
||||||
let formatInfo = self.readFormatInformation()?;
|
|
||||||
let version = self.readVersion()?;
|
let version = self.readVersion()?;
|
||||||
|
|
||||||
// Get the data mask for the format used in this QR Code. This will exclude
|
// Get the data mask for the format used in this QR Code. This will exclude
|
||||||
// some bits from reading as we wind through the bit matrix.
|
// some bits from reading as we wind through the bit matrix.
|
||||||
let dataMask :DataMask= formatInfo.getDataMask().try_into()?;//DataMask.values()[formatInfo.getDataMask()];
|
let dataMask: DataMask = self.readFormatInformation()?.getDataMask().try_into()?; //DataMask.values()[formatInfo.getDataMask()];
|
||||||
let dimension = self.bitMatrix.getHeight();
|
let dimension = self.bitMatrix.getHeight();
|
||||||
dataMask.unmaskBitMatrix(self.bitMatrix, dimension);
|
dataMask.unmaskBitMatrix(&mut self.bitMatrix, dimension);
|
||||||
|
|
||||||
let functionPattern = version.buildFunctionPattern();
|
let functionPattern = version.buildFunctionPattern()?;
|
||||||
|
|
||||||
let readingUp = true;
|
let mut readingUp = true;
|
||||||
let result = vec![0u8;version.getTotalCodewords() as usize];
|
let mut result = vec![0u8; version.getTotalCodewords() as usize];
|
||||||
let resultOffset = 0;
|
let mut resultOffset = 0;
|
||||||
let currentByte = 0;
|
let mut currentByte = 0;
|
||||||
let bitsRead = 0;
|
let mut bitsRead = 0;
|
||||||
// Read columns in pairs, from right to left
|
// Read columns in pairs, from right to left
|
||||||
let mut j = dimension - 1;
|
let mut j = dimension - 1;
|
||||||
while j > 0 {
|
while j > 0 {
|
||||||
@@ -197,11 +205,15 @@ impl BitMatrixParser {
|
|||||||
// Read alternatingly from bottom to top then top to bottom
|
// Read alternatingly from bottom to top then top to bottom
|
||||||
for count in 0..dimension {
|
for count in 0..dimension {
|
||||||
// for (int count = 0; count < dimension; count++) {
|
// for (int count = 0; count < dimension; count++) {
|
||||||
let i = if readingUp {dimension - 1 - count} else {count};
|
let i = if readingUp {
|
||||||
|
dimension - 1 - count
|
||||||
|
} else {
|
||||||
|
count
|
||||||
|
};
|
||||||
for col in 0..2 {
|
for col in 0..2 {
|
||||||
// for (int col = 0; col < 2; col++) {
|
// for (int col = 0; col < 2; col++) {
|
||||||
// Ignore bits covered by the function pattern
|
// Ignore bits covered by the function pattern
|
||||||
if (!functionPattern.get(j - col, i)) {
|
if !functionPattern.get(j - col, i) {
|
||||||
// Read a bit
|
// Read a bit
|
||||||
bitsRead += 1;
|
bitsRead += 1;
|
||||||
currentByte <<= 1;
|
currentByte <<= 1;
|
||||||
@@ -209,7 +221,7 @@ impl BitMatrixParser {
|
|||||||
currentByte |= 1;
|
currentByte |= 1;
|
||||||
}
|
}
|
||||||
// If we've made a whole byte, save it off
|
// If we've made a whole byte, save it off
|
||||||
if (bitsRead == 8) {
|
if bitsRead == 8 {
|
||||||
result[resultOffset] = currentByte;
|
result[resultOffset] = currentByte;
|
||||||
resultOffset += 1;
|
resultOffset += 1;
|
||||||
bitsRead = 0;
|
bitsRead = 0;
|
||||||
@@ -223,22 +235,29 @@ impl BitMatrixParser {
|
|||||||
j -= 2;
|
j -= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resultOffset != version.getTotalCodewords()) {
|
if resultOffset != version.getTotalCodewords() as usize {
|
||||||
throw FormatException.getFormatInstance();
|
return Err(Exceptions::FormatException("".to_owned()));
|
||||||
}
|
}
|
||||||
return result;
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Revert the mask removal done while reading the code words. The bit matrix should revert to its original state.
|
* Revert the mask removal done while reading the code words. The bit matrix should revert to its original state.
|
||||||
*/
|
*/
|
||||||
pub fn remask(&self) {
|
pub fn remask(&mut self) {
|
||||||
if self.parsedFormatInfo.is_none() {
|
if let Some(pfi) = &self.parsedFormatInfo {
|
||||||
|
let dataMask: DataMask = pfi.getDataMask().try_into().unwrap(); // DataMask.values()[self.parsedFormatInfo.getDataMask()];
|
||||||
|
let dimension = self.bitMatrix.getHeight();
|
||||||
|
dataMask.unmaskBitMatrix(&mut self.bitMatrix, dimension);
|
||||||
|
} else {
|
||||||
return; // We have no format information, and have no data mask
|
return; // We have no format information, and have no data mask
|
||||||
}
|
}
|
||||||
let dataMask = DataMask.values()[self.parsedFormatInfo.getDataMask()];
|
// if self.parsedFormatInfo.is_none() {
|
||||||
let dimension = self.bitMatrix.getHeight();
|
// return; // We have no format information, and have no data mask
|
||||||
dataMask.unmaskBitMatrix(self.bitMatrix, dimension);
|
// }
|
||||||
|
// let dataMask = self.parsedFormatInfo.getDataMask()// DataMask.values()[self.parsedFormatInfo.getDataMask()];
|
||||||
|
// let dimension = self.bitMatrix.getHeight();
|
||||||
|
// dataMask.unmaskBitMatrix(self.bitMatrix, dimension);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -249,24 +268,23 @@ impl BitMatrixParser {
|
|||||||
*
|
*
|
||||||
* @param mirror Whether to read version and format information mirrored.
|
* @param mirror Whether to read version and format information mirrored.
|
||||||
*/
|
*/
|
||||||
pub fn setMirror(&self, mirror:bool) {
|
pub fn setMirror(&mut self, mirror: bool) {
|
||||||
self.parsedVersion = None;
|
self.parsedVersion = None;
|
||||||
self.parsedFormatInfo = None;
|
self.parsedFormatInfo = None;
|
||||||
self.mirror = mirror;
|
self.mirror = mirror;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Mirror the bit matrix in order to attempt a second reading. */
|
/** Mirror the bit matrix in order to attempt a second reading. */
|
||||||
pub fn mirror(&self) {
|
pub fn mirror(&mut self) {
|
||||||
for x in 0..self.bitMatrix.getWidth() {
|
for x in 0..self.bitMatrix.getWidth() {
|
||||||
// for (int x = 0; x < bitMatrix.getWidth(); x++) {
|
// for (int x = 0; x < bitMatrix.getWidth(); x++) {
|
||||||
for y in (x + 1)..self.bitMatrix.getHeight() {
|
for y in (x + 1)..self.bitMatrix.getHeight() {
|
||||||
// for (int y = x + 1; y < bitMatrix.getHeight(); y++) {
|
// for (int y = x + 1; y < bitMatrix.getHeight(); y++) {
|
||||||
if (self.bitMatrix.get(x, y) != self.bitMatrix.get(y, x)) {
|
if self.bitMatrix.get(x, y) != self.bitMatrix.get(y, x) {
|
||||||
self.bitMatrix.flip_coords(y, x);
|
self.bitMatrix.flip_coords(y, x);
|
||||||
self.bitMatrix.flip_coords(x, y);
|
self.bitMatrix.flip_coords(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user