From 60cd033adf6c469ff47bdaf85a66b5817fdd188b Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 27 Apr 2017 23:58:03 -0700 Subject: Remove dead dimensions code in fxbarcode. Remove more exceptions. Change-Id: I3b8b8b9837b0010b1e0060ddd56e93c78f9f0fb5 Reviewed-on: https://pdfium-review.googlesource.com/4410 Reviewed-by: Tom Sepez Commit-Queue: Lei Zhang --- fxbarcode/pdf417/BC_PDF417.cpp | 91 +++++++++++++-------------- fxbarcode/pdf417/BC_PDF417.h | 10 ++- fxbarcode/pdf417/BC_PDF417ErrorCorrection.cpp | 64 ++++++------------- fxbarcode/pdf417/BC_PDF417ErrorCorrection.h | 11 ++-- fxbarcode/pdf417/BC_PDF417Writer.cpp | 4 +- 5 files changed, 71 insertions(+), 109 deletions(-) (limited to 'fxbarcode/pdf417') diff --git a/fxbarcode/pdf417/BC_PDF417.cpp b/fxbarcode/pdf417/BC_PDF417.cpp index 7907150831..9844798b24 100644 --- a/fxbarcode/pdf417/BC_PDF417.cpp +++ b/fxbarcode/pdf417/BC_PDF417.cpp @@ -399,48 +399,49 @@ CBC_BarcodeMatrix* CBC_PDF417::getBarcodeMatrix() { return m_barcodeMatrix.get(); } -void CBC_PDF417::generateBarcodeLogic(CFX_WideString msg, - int32_t errorCorrectionLevel, - int32_t& e) { +bool CBC_PDF417::generateBarcodeLogic(CFX_WideString msg, + int32_t errorCorrectionLevel) { int32_t errorCorrectionCodeWords = CBC_PDF417ErrorCorrection::getErrorCorrectionCodewordCount( - errorCorrectionLevel, e); - if (e != BCExceptionNO) - return; + errorCorrectionLevel); + if (errorCorrectionCodeWords < 0) + return false; + + int32_t e = BCExceptionNO; CFX_WideString highLevel = CBC_PDF417HighLevelEncoder::encodeHighLevel(msg, m_compaction, e); if (e != BCExceptionNO) - return; + return false; int32_t sourceCodeWords = highLevel.GetLength(); - std::vector* dimension = - determineDimensions(sourceCodeWords, errorCorrectionCodeWords, e); - if (e != BCExceptionNO) - return; - int32_t cols = (*dimension)[0]; - int32_t rows = (*dimension)[1]; - delete dimension; + std::vector dimensions = + determineDimensions(sourceCodeWords, errorCorrectionCodeWords); + if (dimensions.size() != 2) + return false; + int32_t cols = dimensions[0]; + int32_t rows = dimensions[1]; int32_t pad = getNumberOfPadCodewords(sourceCodeWords, errorCorrectionCodeWords, cols, rows); - if (sourceCodeWords + errorCorrectionCodeWords + 1 > 929) { - e = BCExceptionEncodedMessageContainsTooManyCodeWords; - return; - } + if (sourceCodeWords + errorCorrectionCodeWords + 1 > 929) + return false; + int32_t n = sourceCodeWords + pad + 1; CFX_WideString sb; sb += (wchar_t)n; sb += highLevel; - for (int32_t i = 0; i < pad; i++) { + for (int32_t i = 0; i < pad; i++) sb += (wchar_t)900; - } + CFX_WideString dataCodewords(sb); - CFX_WideString ec = CBC_PDF417ErrorCorrection::generateErrorCorrection( - dataCodewords, errorCorrectionLevel, e); - if (e != BCExceptionNO) - return; + CFX_WideString ec; + if (!CBC_PDF417ErrorCorrection::generateErrorCorrection( + dataCodewords, errorCorrectionLevel, &ec)) { + return false; + } CFX_WideString fullCodewords = dataCodewords + ec; m_barcodeMatrix = pdfium::MakeUnique(rows, cols); encodeLowLevel(fullCodewords, cols, rows, errorCorrectionLevel, m_barcodeMatrix.get()); + return true; } void CBC_PDF417::setDimensions(int32_t maxCols, @@ -536,49 +537,41 @@ void CBC_PDF417::encodeLowLevel(CFX_WideString fullCodewords, } } -std::vector* CBC_PDF417::determineDimensions( +std::vector CBC_PDF417::determineDimensions( int32_t sourceCodeWords, - int32_t errorCorrectionCodeWords, - int32_t& e) { + int32_t errorCorrectionCodeWords) const { + std::vector dimensions; float ratio = 0.0f; - std::vector* dimension = nullptr; for (int32_t cols = m_minCols; cols <= m_maxCols; cols++) { int32_t rows = calculateNumberOfRows(sourceCodeWords, errorCorrectionCodeWords, cols); - if (rows < m_minRows) { + if (rows < m_minRows) break; - } - if (rows > m_maxRows) { + if (rows > m_maxRows) continue; - } float newRatio = ((17 * cols + 69) * DEFAULT_MODULE_WIDTH) / (rows * HEIGHT); - if (dimension && + if (!dimensions.empty() && fabsf(newRatio - PREFERRED_RATIO) > fabsf(ratio - PREFERRED_RATIO)) { continue; } ratio = newRatio; - delete dimension; - dimension = new std::vector; - dimension->push_back(cols); - dimension->push_back(rows); + dimensions.resize(2); + dimensions[0] = cols; + dimensions[1] = rows; } - if (!dimension) { + if (dimensions.empty()) { int32_t rows = calculateNumberOfRows(sourceCodeWords, errorCorrectionCodeWords, m_minCols); if (rows < m_minRows) { - dimension = new std::vector; - dimension->push_back(m_minCols); - dimension->push_back(m_minRows); + dimensions.resize(2); + dimensions[0] = m_minCols; + dimensions[1] = m_minRows; } else if (rows >= 3 && rows <= 90) { - dimension = new std::vector; - dimension->push_back(m_minCols); - dimension->push_back(rows); + dimensions.resize(2); + dimensions[0] = m_minCols; + dimensions[1] = rows; } } - if (!dimension) { - e = BCExceptionUnableToFitMessageInColumns; - return nullptr; - } - return dimension; + return dimensions; } diff --git a/fxbarcode/pdf417/BC_PDF417.h b/fxbarcode/pdf417/BC_PDF417.h index bc36ea5037..f5ca2de60f 100644 --- a/fxbarcode/pdf417/BC_PDF417.h +++ b/fxbarcode/pdf417/BC_PDF417.h @@ -23,9 +23,7 @@ class CBC_PDF417 { virtual ~CBC_PDF417(); CBC_BarcodeMatrix* getBarcodeMatrix(); - void generateBarcodeLogic(CFX_WideString msg, - int32_t errorCorrectionLevel, - int32_t& e); + bool generateBarcodeLogic(CFX_WideString msg, int32_t errorCorrectionLevel); void setDimensions(int32_t maxCols, int32_t minCols, int32_t maxRows, @@ -52,9 +50,9 @@ class CBC_PDF417 { int32_t r, int32_t errorCorrectionLevel, CBC_BarcodeMatrix* logic); - std::vector* determineDimensions(int32_t sourceCodeWords, - int32_t errorCorrectionCodeWords, - int32_t& e); + std::vector determineDimensions( + int32_t sourceCodeWords, + int32_t errorCorrectionCodeWords) const; std::unique_ptr m_barcodeMatrix; bool m_compact; diff --git a/fxbarcode/pdf417/BC_PDF417ErrorCorrection.cpp b/fxbarcode/pdf417/BC_PDF417ErrorCorrection.cpp index abdd113911..6f1d7946eb 100644 --- a/fxbarcode/pdf417/BC_PDF417ErrorCorrection.cpp +++ b/fxbarcode/pdf417/BC_PDF417ErrorCorrection.cpp @@ -21,7 +21,8 @@ */ #include "fxbarcode/pdf417/BC_PDF417ErrorCorrection.h" -#include "fxbarcode/utils.h" + +#include namespace { @@ -124,47 +125,24 @@ const uint16_t* const EC_COEFFICIENTS[9] = { CBC_PDF417ErrorCorrection::CBC_PDF417ErrorCorrection() {} CBC_PDF417ErrorCorrection::~CBC_PDF417ErrorCorrection() {} int32_t CBC_PDF417ErrorCorrection::getErrorCorrectionCodewordCount( - int32_t errorCorrectionLevel, - int32_t& e) { - if (errorCorrectionLevel < 0 || errorCorrectionLevel > 8) { - e = BCExceptionErrorCorrectionLevelMustBeBetween0And8; + int32_t errorCorrectionLevel) { + if (errorCorrectionLevel < 0 || errorCorrectionLevel > 8) return -1; - } return 1 << (errorCorrectionLevel + 1); } -int32_t CBC_PDF417ErrorCorrection::getRecommendedMinimumErrorCorrectionLevel( - int32_t n, - int32_t& e) { - if (n <= 0) { - e = BCExceptionIllegalArgumentnMustBeAbove0; - return -1; - } - if (n <= 40) { - return 2; - } - if (n <= 160) { - return 3; - } - if (n <= 320) { - return 4; - } - if (n <= 863) { - return 5; - } - e = BCExceptionNoRecommendationPossible; - return -1; -} - -CFX_WideString CBC_PDF417ErrorCorrection::generateErrorCorrection( - CFX_WideString dataCodewords, +bool CBC_PDF417ErrorCorrection::generateErrorCorrection( + const CFX_WideString& dataCodewords, int32_t errorCorrectionLevel, - int32_t& e) { - int32_t k = getErrorCorrectionCodewordCount(errorCorrectionLevel, e); - if (e != BCExceptionNO) - return L" "; - wchar_t* ech = FX_Alloc(wchar_t, k); - memset(ech, 0, k * sizeof(wchar_t)); + CFX_WideString* result) { + assert(result); + assert(result->IsEmpty()); + + int32_t k = getErrorCorrectionCodewordCount(errorCorrectionLevel); + if (k < 0) + return false; + + std::vector ech(k); int32_t sld = dataCodewords.GetLength(); for (int32_t i = 0; i < sld; i++) { int32_t t1 = (dataCodewords.GetAt(i) + ech[k - 1]) % 929; @@ -179,13 +157,11 @@ CFX_WideString CBC_PDF417ErrorCorrection::generateErrorCorrection( t3 = 929 - t2; ech[0] = (wchar_t)(t3 % 929); } - CFX_WideString sb; + result->Reserve(k); for (int32_t j = k - 1; j >= 0; j--) { - if (ech[j] != 0) { - ech[j] = (wchar_t)(929 - ech[j]); - } - sb += (wchar_t)ech[j]; + if (ech[j] != 0) + ech[j] = static_cast(929) - ech[j]; + *result += ech[j]; } - FX_Free(ech); - return sb; + return true; } diff --git a/fxbarcode/pdf417/BC_PDF417ErrorCorrection.h b/fxbarcode/pdf417/BC_PDF417ErrorCorrection.h index d1c6eed1b3..3a84ef5845 100644 --- a/fxbarcode/pdf417/BC_PDF417ErrorCorrection.h +++ b/fxbarcode/pdf417/BC_PDF417ErrorCorrection.h @@ -16,13 +16,10 @@ class CBC_PDF417ErrorCorrection { CBC_PDF417ErrorCorrection(); virtual ~CBC_PDF417ErrorCorrection(); - static int32_t getErrorCorrectionCodewordCount(int32_t errorCorrectionLevel, - int32_t& e); - static int32_t getRecommendedMinimumErrorCorrectionLevel(int32_t n, - int32_t& e); - static CFX_WideString generateErrorCorrection(CFX_WideString dataCodewords, - int32_t errorCorrectionLevel, - int32_t& e); + static int32_t getErrorCorrectionCodewordCount(int32_t errorCorrectionLevel); + static bool generateErrorCorrection(const CFX_WideString& dataCodewords, + int32_t errorCorrectionLevel, + CFX_WideString* result); }; #endif // FXBARCODE_PDF417_BC_PDF417ERRORCORRECTION_H_ diff --git a/fxbarcode/pdf417/BC_PDF417Writer.cpp b/fxbarcode/pdf417/BC_PDF417Writer.cpp index 6f06321e82..2f6fc9e1b0 100644 --- a/fxbarcode/pdf417/BC_PDF417Writer.cpp +++ b/fxbarcode/pdf417/BC_PDF417Writer.cpp @@ -60,9 +60,7 @@ uint8_t* CBC_PDF417Writer::Encode(const CFX_WideString& contents, encoder.setDimensions(col, col, 90, 3); else if (row >= 3 && row <= 90) encoder.setDimensions(30, 1, row, row); - int32_t e = BCExceptionNO; - encoder.generateBarcodeLogic(contents, m_iCorrectLevel, e); - if (e != BCExceptionNO) + if (!encoder.generateBarcodeLogic(contents, m_iCorrectLevel)) return nullptr; int32_t lineThickness = 2; -- cgit v1.2.3