Get rid of "exceptions" in CBC_QRCoderMaskUtil::GetDataMaskBit(). It can never occur with the existing caller. Clean up CBC_QRCoderMaskUtil along the way. Change-Id: Iad7fcef8de6b0a866030d03a52510653930b7945 Reviewed-on: https://pdfium-review.googlesource.com/c/45769 Commit-Queue: Lei Zhang <thestig@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fxbarcode/qrcode/BC_QRCoderMaskUtil.cpp b/fxbarcode/qrcode/BC_QRCoderMaskUtil.cpp index 582da85..cab6b9e 100644 --- a/fxbarcode/qrcode/BC_QRCoderMaskUtil.cpp +++ b/fxbarcode/qrcode/BC_QRCoderMaskUtil.cpp
@@ -26,14 +26,48 @@ #include "fxbarcode/qrcode/BC_QRCoderMaskUtil.h" #include "fxbarcode/utils.h" -CBC_QRCoderMaskUtil::CBC_QRCoderMaskUtil() {} -CBC_QRCoderMaskUtil::~CBC_QRCoderMaskUtil() {} +namespace { + +int32_t ApplyMaskPenaltyRule1Internal(CBC_CommonByteMatrix* matrix, + bool isHorizontal) { + int32_t penalty = 0; + int32_t numSameBitCells = 0; + int32_t prevBit = -1; + int32_t width = matrix->GetWidth(); + int32_t height = matrix->GetHeight(); + int32_t iLimit = isHorizontal ? height : width; + int32_t jLimit = isHorizontal ? width : height; + pdfium::span<const uint8_t> array = matrix->GetArray(); + for (int32_t i = 0; i < iLimit; ++i) { + for (int32_t j = 0; j < jLimit; ++j) { + int32_t bit = isHorizontal ? array[i * width + j] : array[j * width + i]; + if (bit == prevBit) { + numSameBitCells += 1; + if (numSameBitCells == 5) { + penalty += 3; + } else if (numSameBitCells > 5) { + penalty += 1; + } + } else { + numSameBitCells = 1; + prevBit = bit; + } + } + numSameBitCells = 0; + } + return penalty; +} + +} // namespace + +// static int32_t CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule1( CBC_CommonByteMatrix* matrix) { return ApplyMaskPenaltyRule1Internal(matrix, true) + ApplyMaskPenaltyRule1Internal(matrix, false); } +// static int32_t CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule2( CBC_CommonByteMatrix* matrix) { int32_t penalty = 0; @@ -53,6 +87,7 @@ return 3 * penalty; } +// static int32_t CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule3( CBC_CommonByteMatrix* matrix) { int32_t penalty = 0; @@ -105,6 +140,8 @@ } return penalty; } + +// static int32_t CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule4( CBC_CommonByteMatrix* matrix) { int32_t numDarkCells = 0; @@ -118,17 +155,16 @@ } } int32_t numTotalCells = matrix->GetHeight() * matrix->GetWidth(); - double darkRatio = (double)numDarkCells / numTotalCells; - return abs((int32_t)(darkRatio * 100 - 50) / 5) * 5 * 10; + double darkRatio = static_cast<double>(numDarkCells) / numTotalCells; + return abs(static_cast<int32_t>(darkRatio * 100 - 50) / 5) * 5 * 10; } + +// static bool CBC_QRCoderMaskUtil::GetDataMaskBit(int32_t maskPattern, int32_t x, - int32_t y, - int32_t& e) { - if (!CBC_QRCoder::IsValidMaskPattern(maskPattern)) { - e = (BCExceptionInvalidateMaskPattern); - return false; - } + int32_t y) { + ASSERT(CBC_QRCoder::IsValidMaskPattern(maskPattern)); + int32_t intermediate = 0, temp = 0; switch (maskPattern) { case 0: @@ -158,40 +194,9 @@ temp = y * x; intermediate = (((temp % 3) + ((y + x) & 0x1)) & 0x1); break; - default: { - e = BCExceptionInvalidateMaskPattern; + default: + NOTREACHED(); return false; - } } return intermediate == 0; } -int32_t CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule1Internal( - CBC_CommonByteMatrix* matrix, - bool isHorizontal) { - int32_t penalty = 0; - int32_t numSameBitCells = 0; - int32_t prevBit = -1; - int32_t width = matrix->GetWidth(); - int32_t height = matrix->GetHeight(); - int32_t iLimit = isHorizontal ? height : width; - int32_t jLimit = isHorizontal ? width : height; - pdfium::span<const uint8_t> array = matrix->GetArray(); - for (int32_t i = 0; i < iLimit; ++i) { - for (int32_t j = 0; j < jLimit; ++j) { - int32_t bit = isHorizontal ? array[i * width + j] : array[j * width + i]; - if (bit == prevBit) { - numSameBitCells += 1; - if (numSameBitCells == 5) { - penalty += 3; - } else if (numSameBitCells > 5) { - penalty += 1; - } - } else { - numSameBitCells = 1; - prevBit = bit; - } - } - numSameBitCells = 0; - } - return penalty; -}
diff --git a/fxbarcode/qrcode/BC_QRCoderMaskUtil.h b/fxbarcode/qrcode/BC_QRCoderMaskUtil.h index 63fccbb..6cbbe2f 100644 --- a/fxbarcode/qrcode/BC_QRCoderMaskUtil.h +++ b/fxbarcode/qrcode/BC_QRCoderMaskUtil.h
@@ -6,22 +6,20 @@ #ifndef FXBARCODE_QRCODE_BC_QRCODERMASKUTIL_H_ #define FXBARCODE_QRCODE_BC_QRCODERMASKUTIL_H_ + class CBC_CommonByteMatrix; + class CBC_QRCoderMaskUtil { public: - CBC_QRCoderMaskUtil(); - virtual ~CBC_QRCoderMaskUtil(); - static bool GetDataMaskBit(int32_t maskPattern, - int32_t x, - int32_t y, - int32_t& e); + CBC_QRCoderMaskUtil() = delete; + ~CBC_QRCoderMaskUtil() = delete; + + static bool GetDataMaskBit(int32_t maskPattern, int32_t x, int32_t y); static int32_t ApplyMaskPenaltyRule1(CBC_CommonByteMatrix* matrix); static int32_t ApplyMaskPenaltyRule2(CBC_CommonByteMatrix* matrix); static int32_t ApplyMaskPenaltyRule3(CBC_CommonByteMatrix* matrix); static int32_t ApplyMaskPenaltyRule4(CBC_CommonByteMatrix* matrix); - static int32_t ApplyMaskPenaltyRule1Internal(CBC_CommonByteMatrix* matrix, - bool isHorizontal); }; #endif // FXBARCODE_QRCODE_BC_QRCODERMASKUTIL_H_
diff --git a/fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp b/fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp index 59d275f..a94a346 100644 --- a/fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp +++ b/fxbarcode/qrcode/BC_QRCoderMatrixUtil.cpp
@@ -95,7 +95,6 @@ bool EmbedDataBits(CBC_QRCoderBitVector* dataBits, int32_t maskPattern, CBC_CommonByteMatrix* matrix) { - int32_t e = BCExceptionNO; size_t szBitIndex = 0; int32_t direction = -1; int32_t x = matrix->GetWidth() - 1; @@ -121,14 +120,9 @@ } else { bit = 0; } - if (maskPattern != -1) { - bool bol = CBC_QRCoderMaskUtil::GetDataMaskBit(maskPattern, xx, y, e); - if (e != BCExceptionNO) - return false; - if (bol) { - bit ^= 0x01; - } - } + ASSERT(CBC_QRCoder::IsValidMaskPattern(maskPattern)); + if (CBC_QRCoderMaskUtil::GetDataMaskBit(maskPattern, xx, y)) + bit ^= 0x01; matrix->Set(xx, y, bit); } y += direction;
diff --git a/fxbarcode/utils.h b/fxbarcode/utils.h index be45e3e..bb3bc1c 100644 --- a/fxbarcode/utils.h +++ b/fxbarcode/utils.h
@@ -25,7 +25,6 @@ #define BCExceptionNO 0 #define BCExceptionIllegalArgument 16 #define BCExceptionValueMustBeEither0or1 50 -#define BCExceptionInvalidateMaskPattern 68 #define BCExceptionGeneric 107 #endif // FXBARCODE_UTILS_H_