From 1c14ae2fbe1ae95dac3a7f5e60d049d9630aef02 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 3 Jul 2018 15:57:03 +0000 Subject: Avoid explicit allocs in fxbarcode matrix classes. Other cleanups: Remove unused method. Fold Init() into constructor. Return span<> where possible. Change-Id: Ie38d32efb6e63d86ae24e93684903a6dd900810f Reviewed-on: https://pdfium-review.googlesource.com/36810 Commit-Queue: Tom Sepez Reviewed-by: Lei Zhang --- fxbarcode/common/BC_CommonBitMatrix.cpp | 16 +++++----- fxbarcode/common/BC_CommonBitMatrix.h | 5 ++-- fxbarcode/common/BC_CommonByteMatrix.cpp | 45 ++++++++++------------------ fxbarcode/common/BC_CommonByteMatrix.h | 18 ++++++----- fxbarcode/datamatrix/BC_DataMatrixWriter.cpp | 3 +- fxbarcode/qrcode/BC_QRCodeWriter.cpp | 2 +- fxbarcode/qrcode/BC_QRCoderEncoder.cpp | 1 - fxbarcode/qrcode/BC_QRCoderMaskUtil.cpp | 11 ++++--- 8 files changed, 43 insertions(+), 58 deletions(-) diff --git a/fxbarcode/common/BC_CommonBitMatrix.cpp b/fxbarcode/common/BC_CommonBitMatrix.cpp index 6fe447db6f..a8a6e53bc1 100644 --- a/fxbarcode/common/BC_CommonBitMatrix.cpp +++ b/fxbarcode/common/BC_CommonBitMatrix.cpp @@ -22,9 +22,13 @@ #include "fxbarcode/common/BC_CommonBitMatrix.h" +#include +#include + #include "fxbarcode/common/BC_CommonBitArray.h" #include "fxbarcode/utils.h" #include "third_party/base/ptr_util.h" +#include "third_party/base/stl_util.h" CBC_CommonBitMatrix::CBC_CommonBitMatrix() {} @@ -33,8 +37,7 @@ void CBC_CommonBitMatrix::Init(int32_t dimension) { m_height = dimension; int32_t rowSize = (m_height + 31) >> 5; m_rowSize = rowSize; - m_bits = FX_Alloc2D(int32_t, m_rowSize, m_height); - memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); + m_bits = pdfium::Vector2D(m_rowSize, m_height); } void CBC_CommonBitMatrix::Init(int32_t width, int32_t height) { @@ -42,13 +45,10 @@ void CBC_CommonBitMatrix::Init(int32_t width, int32_t height) { m_height = height; int32_t rowSize = (width + 31) >> 5; m_rowSize = rowSize; - m_bits = FX_Alloc2D(int32_t, m_rowSize, m_height); - memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); + m_bits = pdfium::Vector2D(m_rowSize, m_height); } -CBC_CommonBitMatrix::~CBC_CommonBitMatrix() { - FX_Free(m_bits); -} +CBC_CommonBitMatrix::~CBC_CommonBitMatrix() = default; bool CBC_CommonBitMatrix::Get(int32_t x, int32_t y) const { int32_t offset = y * m_rowSize + (x >> 5); @@ -70,7 +70,7 @@ void CBC_CommonBitMatrix::Flip(int32_t x, int32_t y) { } void CBC_CommonBitMatrix::Clear() { - memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); + std::fill(std::begin(m_bits), std::end(m_bits), 0); } bool CBC_CommonBitMatrix::SetRegion(int32_t left, diff --git a/fxbarcode/common/BC_CommonBitMatrix.h b/fxbarcode/common/BC_CommonBitMatrix.h index 67c0f888cc..51c3728a7a 100644 --- a/fxbarcode/common/BC_CommonBitMatrix.h +++ b/fxbarcode/common/BC_CommonBitMatrix.h @@ -7,7 +7,7 @@ #ifndef FXBARCODE_COMMON_BC_COMMONBITMATRIX_H_ #define FXBARCODE_COMMON_BC_COMMONBITMATRIX_H_ -#include +#include #include "core/fxcrt/fx_system.h" @@ -31,13 +31,12 @@ class CBC_CommonBitMatrix { void SetCol(int32_t y, CBC_CommonBitArray* col); int32_t GetWidth() const { return m_width; } int32_t GetHeight() const { return m_height; } - int32_t* GetBits() const { return m_bits; } private: int32_t m_width = 0; int32_t m_height = 0; int32_t m_rowSize = 0; - int32_t* m_bits = nullptr; + std::vector m_bits; }; #endif // FXBARCODE_COMMON_BC_COMMONBITMATRIX_H_ diff --git a/fxbarcode/common/BC_CommonByteMatrix.cpp b/fxbarcode/common/BC_CommonByteMatrix.cpp index 2ab1e9ac50..db0a4a9647 100644 --- a/fxbarcode/common/BC_CommonByteMatrix.cpp +++ b/fxbarcode/common/BC_CommonByteMatrix.cpp @@ -19,46 +19,33 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "fxbarcode/common/BC_CommonByteMatrix.h" +#include +#include #include "core/fxcrt/fx_memory.h" -#include "fxbarcode/common/BC_CommonByteMatrix.h" +#include "third_party/base/stl_util.h" -CBC_CommonByteMatrix::CBC_CommonByteMatrix(int32_t width, int32_t height) { - m_height = height; - m_width = width; - m_bytes = nullptr; -} -void CBC_CommonByteMatrix::Init() { - m_bytes = FX_Alloc2D(uint8_t, m_height, m_width); - memset(m_bytes, 0xff, m_height * m_width); -} -CBC_CommonByteMatrix::~CBC_CommonByteMatrix() { - FX_Free(m_bytes); -} -int32_t CBC_CommonByteMatrix::GetHeight() { - return m_height; -} -int32_t CBC_CommonByteMatrix::GetWidth() { - return m_width; +CBC_CommonByteMatrix::CBC_CommonByteMatrix(int32_t width, int32_t height) + : m_width(width), m_height(height) { + m_bytes = pdfium::Vector2D(m_height, m_width); + clear(0xff); } -uint8_t CBC_CommonByteMatrix::Get(int32_t x, int32_t y) { + +CBC_CommonByteMatrix::~CBC_CommonByteMatrix() = default; + +uint8_t CBC_CommonByteMatrix::Get(int32_t x, int32_t y) const { return m_bytes[y * m_width + x]; } + void CBC_CommonByteMatrix::Set(int32_t x, int32_t y, int32_t value) { m_bytes[y * m_width + x] = (uint8_t)value; } + void CBC_CommonByteMatrix::Set(int32_t x, int32_t y, uint8_t value) { m_bytes[y * m_width + x] = value; } + void CBC_CommonByteMatrix::clear(uint8_t value) { - int32_t y; - for (y = 0; y < m_height; y++) { - int32_t x; - for (x = 0; x < m_width; x++) { - m_bytes[y * m_width + x] = value; - } - } -} -uint8_t* CBC_CommonByteMatrix::GetArray() { - return m_bytes; + std::fill(std::begin(m_bytes), std::end(m_bytes), value); } diff --git a/fxbarcode/common/BC_CommonByteMatrix.h b/fxbarcode/common/BC_CommonByteMatrix.h index 9f13a376d1..c5a66a3e82 100644 --- a/fxbarcode/common/BC_CommonByteMatrix.h +++ b/fxbarcode/common/BC_CommonByteMatrix.h @@ -9,27 +9,29 @@ #include +#include + #include "core/fxcrt/fx_system.h" +#include "third_party/base/span.h" -class CBC_CommonByteMatrix { +class CBC_CommonByteMatrix final { public: CBC_CommonByteMatrix(int32_t width, int32_t height); - virtual ~CBC_CommonByteMatrix(); + ~CBC_CommonByteMatrix(); - int32_t GetHeight(); - int32_t GetWidth(); - uint8_t Get(int32_t x, int32_t y); - uint8_t* GetArray(); + int32_t GetWidth() const { return m_width; } + int32_t GetHeight() const { return m_height; } + pdfium::span GetArray() const { return m_bytes; } + uint8_t Get(int32_t x, int32_t y) const; void Set(int32_t x, int32_t y, int32_t value); void Set(int32_t x, int32_t y, uint8_t value); void clear(uint8_t value); - virtual void Init(); private: - uint8_t* m_bytes; int32_t m_width; int32_t m_height; + std::vector m_bytes; }; #endif // FXBARCODE_COMMON_BC_COMMONBYTEMATRIX_H_ diff --git a/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp b/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp index d4f9ad3479..799b7749ca 100644 --- a/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp +++ b/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp @@ -59,7 +59,6 @@ std::unique_ptr encodeLowLevel( ASSERT(height); auto matrix = pdfium::MakeUnique(width, height); - matrix->Init(); int32_t matrixY = 0; for (int32_t y = 0; y < symbolHeight; y++) { int32_t matrixX; @@ -144,6 +143,6 @@ uint8_t* CBC_DataMatrixWriter::Encode(const WideString& contents, outWidth = bytematrix->GetWidth(); outHeight = bytematrix->GetHeight(); uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight); - memcpy(result, bytematrix->GetArray(), outWidth * outHeight); + memcpy(result, bytematrix->GetArray().data(), outWidth * outHeight); return result; } diff --git a/fxbarcode/qrcode/BC_QRCodeWriter.cpp b/fxbarcode/qrcode/BC_QRCodeWriter.cpp index eb35af3867..8e4a57311a 100644 --- a/fxbarcode/qrcode/BC_QRCodeWriter.cpp +++ b/fxbarcode/qrcode/BC_QRCodeWriter.cpp @@ -83,6 +83,6 @@ uint8_t* CBC_QRCodeWriter::Encode(const WideString& contents, outWidth = qr.GetMatrixWidth(); outHeight = qr.GetMatrixWidth(); uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight); - memcpy(result, qr.GetMatrix()->GetArray(), outWidth * outHeight); + memcpy(result, qr.GetMatrix()->GetArray().data(), outWidth * outHeight); return result; } diff --git a/fxbarcode/qrcode/BC_QRCoderEncoder.cpp b/fxbarcode/qrcode/BC_QRCoderEncoder.cpp index 8da2d48e34..9c600b01b0 100644 --- a/fxbarcode/qrcode/BC_QRCoderEncoder.cpp +++ b/fxbarcode/qrcode/BC_QRCoderEncoder.cpp @@ -599,7 +599,6 @@ bool CBC_QRCoderEncoder::Encode(const WideString& content, auto matrix = pdfium::MakeUnique( qrCode->GetMatrixWidth(), qrCode->GetMatrixWidth()); - matrix->Init(); int32_t maskPattern = ChooseMaskPattern( &finalBits, qrCode->GetECLevel(), qrCode->GetVersion(), matrix.get(), e); if (e != BCExceptionNO) diff --git a/fxbarcode/qrcode/BC_QRCoderMaskUtil.cpp b/fxbarcode/qrcode/BC_QRCoderMaskUtil.cpp index cdf1e4caf9..582da85078 100644 --- a/fxbarcode/qrcode/BC_QRCoderMaskUtil.cpp +++ b/fxbarcode/qrcode/BC_QRCoderMaskUtil.cpp @@ -37,7 +37,7 @@ int32_t CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule1( int32_t CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule2( CBC_CommonByteMatrix* matrix) { int32_t penalty = 0; - uint8_t* array = matrix->GetArray(); + pdfium::span array = matrix->GetArray(); int32_t width = matrix->GetWidth(); int32_t height = matrix->GetHeight(); for (int32_t y = 0; y < height - 1; y++) { @@ -56,7 +56,7 @@ int32_t CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule2( int32_t CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule3( CBC_CommonByteMatrix* matrix) { int32_t penalty = 0; - uint8_t* array = matrix->GetArray(); + pdfium::span array = matrix->GetArray(); int32_t width = matrix->GetWidth(); int32_t height = matrix->GetHeight(); for (int32_t y = 0; y < height; ++y) { @@ -108,14 +108,13 @@ int32_t CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule3( int32_t CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule4( CBC_CommonByteMatrix* matrix) { int32_t numDarkCells = 0; - uint8_t* array = matrix->GetArray(); + pdfium::span array = matrix->GetArray(); int32_t width = matrix->GetWidth(); int32_t height = matrix->GetHeight(); for (int32_t y = 0; y < height; ++y) { for (int32_t x = 0; x < width; ++x) { - if (array[y * width + x] == 1) { + if (array[y * width + x] == 1) numDarkCells += 1; - } } } int32_t numTotalCells = matrix->GetHeight() * matrix->GetWidth(); @@ -176,7 +175,7 @@ int32_t CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule1Internal( int32_t height = matrix->GetHeight(); int32_t iLimit = isHorizontal ? height : width; int32_t jLimit = isHorizontal ? width : height; - uint8_t* array = matrix->GetArray(); + pdfium::span 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]; -- cgit v1.2.3