diff --git a/src/qrcode/cpp_port/bitmatrix_parser.rs b/src/qrcode/cpp_port/bitmatrix_parser.rs index b70c978..d629043 100644 --- a/src/qrcode/cpp_port/bitmatrix_parser.rs +++ b/src/qrcode/cpp_port/bitmatrix_parser.rs @@ -149,7 +149,7 @@ pub fn ReadQRCodewords( // Read a bit AppendBit( &mut currentByte, - GetDataMaskBit(formatInfo.data_mask as u32, xx, y, None) + GetDataMaskBit(formatInfo.data_mask as u32, xx, y, None)? != getBit(bitMatrix, xx, y, Some(formatInfo.isMirrored)), ); // If we've made a whole byte, save it off @@ -214,7 +214,7 @@ pub fn ReadMQRCodewords( // Read a bit AppendBit( &mut currentByte, - GetDataMaskBit(formatInfo.data_mask as u32, xx, y, Some(true)) + GetDataMaskBit(formatInfo.data_mask as u32, xx, y, Some(true))? != getBit(bitMatrix, xx, y, Some(formatInfo.isMirrored)), ); bitsRead += 1; diff --git a/src/qrcode/cpp_port/data_mask.rs b/src/qrcode/cpp_port/data_mask.rs index 1db71c3..6effb85 100644 --- a/src/qrcode/cpp_port/data_mask.rs +++ b/src/qrcode/cpp_port/data_mask.rs @@ -5,6 +5,8 @@ // SPDX-License-Identifier: Apache-2.0 use crate::common::BitMatrix; +use crate::common::Result; +use crate::Exceptions; /** *

Encapsulates data masks for the data bits in a QR and micro QR code, per ISO 18004:2006 6.8.

@@ -13,27 +15,33 @@ use crate::common::BitMatrix; * and j is row position. In fact, as the text says, i is row position and j is column position.

*/ -pub fn GetDataMaskBit(maskIndex: u32, x: u32, y: u32, isMicro: Option) -> bool { +pub fn GetDataMaskBit(maskIndex: u32, x: u32, y: u32, isMicro: Option) -> Result { let isMicro = isMicro.unwrap_or(false); - todo!() - // if (isMicro) { - // if (maskIndex < 0 || maskIndex >= 4) - // throw std::invalid_argument("QRCode maskIndex out of range"); - // maskIndex = std::array{1, 4, 6, 7}[maskIndex]; // map from MQR to QR indices - // } + let mut maskIndex = maskIndex; + if (isMicro) { + if (maskIndex < 0 || maskIndex >= 4) { + return Err(Exceptions::illegal_argument_with( + "QRCode maskIndex out of range", + )); + } + maskIndex = [1, 4, 6, 7][maskIndex as usize]; // map from MQR to QR indices + } - // switch (maskIndex) { - // case 0: return (y + x) % 2 == 0; - // case 1: return y % 2 == 0; - // case 2: return x % 3 == 0; - // case 3: return (y + x) % 3 == 0; - // case 4: return ((y / 2) + (x / 3)) % 2 == 0; - // case 5: return (y * x) % 6 == 0; - // case 6: return ((y * x) % 6) < 3; - // case 7: return (y + x + ((y * x) % 3)) % 2 == 0; - // } + match (maskIndex) { + 0 => return Ok((y + x) % 2 == 0), + 1 => return Ok(y % 2 == 0), + 2 => return Ok(x % 3 == 0), + 3 => return Ok((y + x) % 3 == 0), + 4 => return Ok(((y / 2) + (x / 3)) % 2 == 0), + 5 => return Ok((y * x) % 6 == 0), + 6 => return Ok(((y * x) % 6) < 3), + 7 => return Ok((y + x + ((y * x) % 3)) % 2 == 0), + _ => {} + } - // throw std::invalid_argument("QRCode maskIndex out of range"); + Err(Exceptions::illegal_argument_with( + "QRCode maskIndex out of range", + )) } pub fn GetMaskedBit( @@ -42,6 +50,6 @@ pub fn GetMaskedBit( y: u32, maskIndex: u32, isMicro: Option, -) -> bool { - return GetDataMaskBit(maskIndex, x, y, isMicro) != bits.get(x, y); +) -> Result { + Ok(GetDataMaskBit(maskIndex, x, y, isMicro)? != bits.get(x, y)) }