QrCode struct and associated types ported NOPASS

This commit is contained in:
Henry Schimke
2022-10-01 16:24:53 -05:00
parent afc7a20747
commit e5abca3884
15 changed files with 476 additions and 428 deletions

View File

@@ -1,52 +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.qrcode.decoder;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Sean Owen
*/
public final class ModeTestCase extends Assert {
@Test
public void testForBits() {
assertSame(Mode.TERMINATOR, Mode.forBits(0x00));
assertSame(Mode.NUMERIC, Mode.forBits(0x01));
assertSame(Mode.ALPHANUMERIC, Mode.forBits(0x02));
assertSame(Mode.BYTE, Mode.forBits(0x04));
assertSame(Mode.KANJI, Mode.forBits(0x08));
}
@Test(expected = IllegalArgumentException.class)
public void testBadMode() {
Mode.forBits(0x10);
}
@Test
public void testCharacterCount() {
// Spot check a few values
assertEquals(10, Mode.NUMERIC.getCharacterCountBits(Version.getVersionForNumber(5)));
assertEquals(12, Mode.NUMERIC.getCharacterCountBits(Version.getVersionForNumber(26)));
assertEquals(14, Mode.NUMERIC.getCharacterCountBits(Version.getVersionForNumber(40)));
assertEquals(9, Mode.ALPHANUMERIC.getCharacterCountBits(Version.getVersionForNumber(6)));
assertEquals(8, Mode.BYTE.getCharacterCountBits(Version.getVersionForNumber(7)));
assertEquals(8, Mode.KANJI.getCharacterCountBits(Version.getVersionForNumber(8)));
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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.
*/
use crate::qrcode::decoder::Version;
use super::Mode;
/**
* @author Sean Owen
*/
#[test]
fn testForBits() {
assert_eq!(Mode::TERMINATOR, Mode::forBits(0x00).unwrap());
assert_eq!(Mode::NUMERIC, Mode::forBits(0x01).unwrap());
assert_eq!(Mode::ALPHANUMERIC, Mode::forBits(0x02).unwrap());
assert_eq!(Mode::BYTE, Mode::forBits(0x04).unwrap());
assert_eq!(Mode::KANJI, Mode::forBits(0x08).unwrap());
}
#[test]
#[should_panic]
fn testBadMode() {
assert!(Mode::forBits(0x10).is_ok());
}
#[test]
fn testCharacterCount() {
// Spot check a few values
assert_eq!(10, Mode::NUMERIC.getCharacterCountBits(Version::getVersionForNumber(5).unwrap()));
assert_eq!(12, Mode::NUMERIC.getCharacterCountBits(Version::getVersionForNumber(26).unwrap()));
assert_eq!(14, Mode::NUMERIC.getCharacterCountBits(Version::getVersionForNumber(40).unwrap()));
assert_eq!(9, Mode::ALPHANUMERIC.getCharacterCountBits(Version::getVersionForNumber(6).unwrap()));
assert_eq!(8, Mode::BYTE.getCharacterCountBits(Version::getVersionForNumber(7).unwrap()));
assert_eq!(8, Mode::KANJI.getCharacterCountBits(Version::getVersionForNumber(8).unwrap()));
}

View File

@@ -1,78 +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.qrcode.decoder;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Sean Owen
*/
public final class VersionTestCase extends Assert {
@Test(expected = IllegalArgumentException.class)
public void testBadVersion() {
Version.getVersionForNumber(0);
}
@Test
public void testVersionForNumber() {
for (int i = 1; i <= 40; i++) {
checkVersion(Version.getVersionForNumber(i), i, 4 * i + 17);
}
}
private static void checkVersion(Version version, int number, int dimension) {
assertNotNull(version);
assertEquals(number, version.getVersionNumber());
assertNotNull(version.getAlignmentPatternCenters());
if (number > 1) {
assertTrue(version.getAlignmentPatternCenters().length > 0);
}
assertEquals(dimension, version.getDimensionForVersion());
assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel.H));
assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel.L));
assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel.M));
assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel.Q));
assertNotNull(version.buildFunctionPattern());
}
@Test
public void testGetProvisionalVersionForDimension() throws Exception {
for (int i = 1; i <= 40; i++) {
assertEquals(i, Version.getProvisionalVersionForDimension(4 * i + 17).getVersionNumber());
}
}
@Test
public void testDecodeVersionInformation() {
// Spot check
doTestVersion(7, 0x07C94);
doTestVersion(12, 0x0C762);
doTestVersion(17, 0x1145D);
doTestVersion(22, 0x168C9);
doTestVersion(27, 0x1B08E);
doTestVersion(32, 0x209D5);
}
private static void doTestVersion(int expectedVersion, int mask) {
Version version = Version.decodeVersionInformation(mask);
assertNotNull(version);
assertEquals(expectedVersion, version.getVersionNumber());
}
}

View File

@@ -0,0 +1,85 @@
/*
* 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.
*/
use crate::{qrcode::decoder::{Version, ErrorCorrectionLevel}, Exceptions};
/**
* @author Sean Owen
*/
#[test]
#[should_panic]
fn testBadVersion() {
assert!(Version::getVersionForNumber(0).is_ok());
}
#[test]
fn testVersionForNumber() {
for i in 1..=40 {
// for (int i = 1; i <= 40; i++) {
checkVersion(Version::getVersionForNumber(i), i, 4 * i + 17);
}
}
fn checkVersion( version:Result<&Version,Exceptions>, number:u32, dimension:u32) {
assert!(version.is_ok());
let version = version.unwrap();
assert_eq!(number, version.getVersionNumber());
// assertNotNull(version.getAlignmentPatternCenters());
if (number > 1) {
assert!(version.getAlignmentPatternCenters().len() > 0);
}
assert_eq!(dimension, version.getDimensionForVersion());
let _tmp = version.getECBlocksForLevel(ErrorCorrectionLevel::H);
let _tmp = version.getECBlocksForLevel(ErrorCorrectionLevel::L);
let _tmp = version.getECBlocksForLevel(ErrorCorrectionLevel::M);
let _tmp = version.getECBlocksForLevel(ErrorCorrectionLevel::Q);
let _tmp = version.buildFunctionPattern();
// assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel::H));
// assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel::L));
// assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel::M));
// assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel::Q));
// assertNotNull(version.buildFunctionPattern());
}
#[test]
fn testGetProvisionalVersionForDimension() {
for i in 1..=40 {
// for (int i = 1; i <= 40; i++) {
assert_eq!(i, Version::getProvisionalVersionForDimension(4 * i + 17).expect("must exist for supplied values").getVersionNumber());
}
}
#[test]
fn testDecodeVersionInformation() {
// Spot check
doTestVersion(7, 0x07C94);
doTestVersion(12, 0x0C762);
doTestVersion(17, 0x1145D);
doTestVersion(22, 0x168C9);
doTestVersion(27, 0x1B08E);
doTestVersion(32, 0x209D5);
}
fn doTestVersion( expectedVersion:u32, mask:u32) {
let version = Version::decodeVersionInformation(mask);
assert!(version.is_ok());
assert_eq!(expectedVersion, version.unwrap().getVersionNumber());
}

View File

@@ -118,8 +118,8 @@ impl FormatInformation {
masked_format_info2: u32,
) -> Option<FormatInformation> {
// Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing
let best_difference = u32::MAX;
let best_format_info = 0;
let mut best_difference = u32::MAX;
let mut best_format_info = 0;
for decodeInfo in FORMAT_INFO_DECODE_LOOKUP {
// for (int[] decodeInfo : FORMAT_INFO_DECODE_LOOKUP) {
let targetInfo = decodeInfo[0];
@@ -127,7 +127,7 @@ impl FormatInformation {
// Found an exact match
return Some(FormatInformation::new(decodeInfo[1] as u8));
}
let bits_difference = Self::numBitsDiffering(masked_format_info1, targetInfo);
let mut bits_difference = Self::numBitsDiffering(masked_format_info1, targetInfo);
if bits_difference < best_difference {
best_format_info = decodeInfo[1] as u8;
best_difference = bits_difference;

View File

@@ -5,6 +5,10 @@ mod format_information;
#[cfg(test)]
mod ErrorCorrectionLevelTestCase;
#[cfg(test)]
mod ModeTestCase;
#[cfg(test)]
mod VersionTestCase;
pub use version::*;
pub use mode::*;

View File

@@ -24,6 +24,7 @@ use super::Version;
*
* @author Sean Owen
*/
#[derive(Debug, PartialEq, Eq)]
pub enum Mode {
TERMINATOR, //(new int[]{0, 0, 0}, 0x00), // Not really a mode...
NUMERIC, //(new int[]{10, 12, 14}, 0x01),

View File

@@ -20,6 +20,12 @@ use crate::{common::BitMatrix, Exceptions};
use super::{ErrorCorrectionLevel, FormatInformation};
use lazy_static::lazy_static;
lazy_static! {
static ref VERSIONS: Vec<Version> = Version::buildVersions();
}
/**
* See ISO 18004:2006 Annex D.
* Element i represents the raw version bits that specify version i + 7
@@ -31,7 +37,7 @@ const VERSION_DECODE_INFO: [u32; 34] = [
0x2542E, 0x26A64, 0x27541, 0x28C69,
];
const VERSIONS: Vec<Version> = Version::buildVersions();
// const VERSIONS: &'static[Version] = &Version::buildVersions();
/**
* See ISO 18004:2006 Annex D
*
@@ -45,12 +51,8 @@ pub struct Version {
totalCodewords: u32,
}
impl Version {
const fn new(
versionNumber: u32,
alignmentPatternCenters: Vec<u32>,
ecBlocks: Vec<ECBlocks>,
) -> Self {
let total = 0;
fn new(versionNumber: u32, alignmentPatternCenters: Vec<u32>, ecBlocks: Vec<ECBlocks>) -> Self {
let mut total = 0;
let ecCodewords = ecBlocks[0].getECCodewordsPerBlock();
let ecbArray = ecBlocks[0].getECBlocks();
let mut i = 0;
@@ -96,7 +98,9 @@ impl Version {
* @return Version for a QR Code of that dimension
* @throws FormatException if dimension is not 1 mod 4
*/
pub fn getProvisionalVersionForDimension(dimension: u32) -> Result<Version, Exceptions> {
pub fn getProvisionalVersionForDimension(
dimension: u32,
) -> Result<&'static Version, Exceptions> {
if dimension % 4 != 1 {
return Err(Exceptions::FormatException(
"dimension incorrect".to_owned(),
@@ -110,18 +114,18 @@ impl Version {
// }
}
pub fn getVersionForNumber(versionNumber: u32) -> Result<Version, Exceptions> {
pub fn getVersionForNumber(versionNumber: u32) -> Result<&'static Version, Exceptions> {
if versionNumber < 1 || versionNumber > 40 {
return Err(Exceptions::IllegalArgumentException(
"version out of spec".to_owned(),
));
}
Ok(VERSIONS[versionNumber as usize - 1])
Ok(&VERSIONS[versionNumber as usize - 1])
}
pub fn decodeVersionInformation(versionBits: u32) -> Result<Version, Exceptions> {
let bestDifference = u32::MAX;
let bestVersion = 0;
pub fn decodeVersionInformation(versionBits: u32) -> Result<&'static Version, Exceptions> {
let mut bestDifference = u32::MAX;
let mut bestVersion = 0;
for i in 0..VERSION_DECODE_INFO.len() as u32 {
// for (int i = 0; i < VERSION_DECODE_INFO.length; i++) {
let targetVersion = VERSION_DECODE_INFO[i as usize];
@@ -151,7 +155,7 @@ impl Version {
*/
pub fn buildFunctionPattern(&self) -> BitMatrix {
let dimension = self.getDimensionForVersion();
let bitMatrix = BitMatrix::with_single_dimension(dimension);
let mut bitMatrix = BitMatrix::with_single_dimension(dimension);
// Top left finder pattern + separator + format
bitMatrix.setRegion(0, 0, 9, 9);
@@ -192,7 +196,7 @@ impl Version {
/**
* See ISO 18004:2006 6.5.1 Table 9
*/
pub const fn buildVersions() -> Vec<Version> {
pub fn buildVersions() -> Vec<Version> {
Vec::from([
Version::new(
1,
@@ -942,8 +946,8 @@ impl ECBlocks {
}
pub fn getNumBlocks(&self) -> u32 {
let total = 0;
for ecBlock in self.ecBlocks {
let mut total = 0;
for ecBlock in &self.ecBlocks {
// for (ECB ecBlock : ecBlocks) {
total += ecBlock.getCount();
}