Reduce copying in fxbarcode.

In several cases, instead of creating a new DataVector and copying into
it, just take an existing DataVector.

Change-Id: I9ea0fc8e0fa596fc040af11266cbb5865c4e9ac6
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/97891
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fxbarcode/common/BC_CommonByteMatrix.cpp b/fxbarcode/common/BC_CommonByteMatrix.cpp
index e7849e2..2902e8c 100644
--- a/fxbarcode/common/BC_CommonByteMatrix.cpp
+++ b/fxbarcode/common/BC_CommonByteMatrix.cpp
@@ -23,6 +23,7 @@
 
 #include <algorithm>
 #include <iterator>
+#include <utility>
 
 #include "core/fxcrt/data_vector.h"
 #include "third_party/base/check_op.h"
@@ -37,6 +38,10 @@
 
 CBC_CommonByteMatrix::~CBC_CommonByteMatrix() = default;
 
+DataVector<uint8_t> CBC_CommonByteMatrix::TakeArray() {
+  return std::move(m_bytes);
+}
+
 uint8_t CBC_CommonByteMatrix::Get(size_t x, size_t y) const {
   const size_t offset = y * m_width + x;
   CHECK_LT(offset, m_bytes.size());
diff --git a/fxbarcode/common/BC_CommonByteMatrix.h b/fxbarcode/common/BC_CommonByteMatrix.h
index ea12db2..f7e2f88 100644
--- a/fxbarcode/common/BC_CommonByteMatrix.h
+++ b/fxbarcode/common/BC_CommonByteMatrix.h
@@ -20,6 +20,7 @@
   size_t GetWidth() const { return m_width; }
   size_t GetHeight() const { return m_height; }
   pdfium::span<const uint8_t> GetArray() const { return m_bytes; }
+  DataVector<uint8_t> TakeArray();
 
   uint8_t Get(size_t x, size_t y) const;
   void Set(size_t x, size_t y, uint8_t value);
diff --git a/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp b/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp
index 88ae579..2a60eb6 100644
--- a/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp
+++ b/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp
@@ -27,7 +27,6 @@
 #include <memory>
 
 #include "core/fxcrt/data_vector.h"
-#include "core/fxcrt/stl_util.h"
 #include "fxbarcode/BC_TwoDimWriter.h"
 #include "fxbarcode/BC_Writer.h"
 #include "fxbarcode/common/BC_CommonBitMatrix.h"
@@ -49,7 +48,7 @@
 
 namespace {
 
-std::unique_ptr<CBC_CommonByteMatrix> encodeLowLevel(
+std::unique_ptr<CBC_CommonByteMatrix> EncodeLowLevel(
     CBC_DefaultPlacement* placement,
     const CBC_SymbolInfo* symbolInfo) {
   int32_t symbolWidth = symbolInfo->GetSymbolDataWidth();
@@ -113,20 +112,19 @@
 DataVector<uint8_t> CBC_DataMatrixWriter::Encode(const WideString& contents,
                                                  int32_t* pOutWidth,
                                                  int32_t* pOutHeight) {
-  DataVector<uint8_t> results;
   WideString encoded = CBC_HighLevelEncoder::EncodeHighLevel(contents);
   if (encoded.IsEmpty())
-    return results;
+    return DataVector<uint8_t>();
 
   const CBC_SymbolInfo* pSymbolInfo =
       CBC_SymbolInfo::Lookup(encoded.GetLength(), false);
   if (!pSymbolInfo)
-    return results;
+    return DataVector<uint8_t>();
 
   WideString codewords =
       CBC_ErrorCorrection::EncodeECC200(encoded, pSymbolInfo);
   if (codewords.IsEmpty())
-    return results;
+    return DataVector<uint8_t>();
 
   int32_t width = pSymbolInfo->GetSymbolDataWidth();
   DCHECK(width);
@@ -136,15 +134,10 @@
   auto placement =
       std::make_unique<CBC_DefaultPlacement>(codewords, width, height);
   placement->place();
-  auto bytematrix = encodeLowLevel(placement.get(), pSymbolInfo);
-  if (!bytematrix)
-    return results;
+  auto bytematrix = EncodeLowLevel(placement.get(), pSymbolInfo);
+  DCHECK(bytematrix);
 
   *pOutWidth = bytematrix->GetWidth();
   *pOutHeight = bytematrix->GetHeight();
-  results = fxcrt::Vector2D<uint8_t, FxAllocAllocator<uint8_t>>(*pOutWidth,
-                                                                *pOutHeight);
-  memcpy(results.data(), bytematrix->GetArray().data(),
-         *pOutWidth * *pOutHeight);
-  return results;
+  return bytematrix->TakeArray();
 }
diff --git a/fxbarcode/pdf417/BC_PDF417Writer.cpp b/fxbarcode/pdf417/BC_PDF417Writer.cpp
index cbfe789..f80a2cd 100644
--- a/fxbarcode/pdf417/BC_PDF417Writer.cpp
+++ b/fxbarcode/pdf417/BC_PDF417Writer.cpp
@@ -49,7 +49,6 @@
 DataVector<uint8_t> CBC_PDF417Writer::Encode(WideStringView contents,
                                              int32_t* pOutWidth,
                                              int32_t* pOutHeight) {
-  DataVector<uint8_t> results;
   CBC_PDF417 encoder;
   int32_t col = (m_Width / m_ModuleWidth - 69) / 17;
   int32_t row = m_Height / (m_ModuleWidth * 20);
@@ -60,7 +59,7 @@
   else if (row >= 3 && row <= 90)
     encoder.setDimensions(30, 1, row, row);
   if (!encoder.GenerateBarcodeLogic(contents, error_correction_level()))
-    return results;
+    return DataVector<uint8_t>();
 
   CBC_BarcodeMatrix* barcodeMatrix = encoder.getBarcodeMatrix();
   DataVector<uint8_t> matrixData = barcodeMatrix->toBitArray();
@@ -73,10 +72,7 @@
   }
   *pOutWidth = matrixWidth;
   *pOutHeight = matrixHeight;
-  results = fxcrt::Vector2D<uint8_t, FxAllocAllocator<uint8_t>>(*pOutWidth,
-                                                                *pOutHeight);
-  memcpy(results.data(), matrixData.data(), *pOutWidth * *pOutHeight);
-  return results;
+  return matrixData;
 }
 
 void CBC_PDF417Writer::RotateArray(DataVector<uint8_t>* bitarray,
diff --git a/fxbarcode/qrcode/BC_QRCodeWriter.cpp b/fxbarcode/qrcode/BC_QRCodeWriter.cpp
index 77fe042..490692b 100644
--- a/fxbarcode/qrcode/BC_QRCodeWriter.cpp
+++ b/fxbarcode/qrcode/BC_QRCodeWriter.cpp
@@ -24,8 +24,9 @@
 
 #include <stdint.h>
 
+#include <memory>
+
 #include "core/fxcrt/data_vector.h"
-#include "core/fxcrt/stl_util.h"
 #include "fxbarcode/common/BC_CommonByteMatrix.h"
 #include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
 #include "fxbarcode/qrcode/BC_QRCoder.h"
@@ -50,7 +51,6 @@
                                              int32_t ecLevel,
                                              int32_t* pOutWidth,
                                              int32_t* pOutHeight) {
-  DataVector<uint8_t> results;
   CBC_QRCoderErrorCorrectionLevel* ec = nullptr;
   switch (ecLevel) {
     case 0:
@@ -66,17 +66,14 @@
       ec = CBC_QRCoderErrorCorrectionLevel::H;
       break;
     default:
-      return results;
+      return DataVector<uint8_t>();
   }
   CBC_QRCoder qr;
   if (!CBC_QRCoderEncoder::Encode(contents, ec, &qr))
-    return results;
+    return DataVector<uint8_t>();
 
   *pOutWidth = qr.GetMatrixWidth();
   *pOutHeight = qr.GetMatrixWidth();
-  results = fxcrt::Vector2D<uint8_t, FxAllocAllocator<uint8_t>>(*pOutWidth,
-                                                                *pOutHeight);
-  memcpy(results.data(), qr.GetMatrix()->GetArray().data(),
-         *pOutWidth * *pOutHeight);
-  return results;
+  std::unique_ptr<CBC_CommonByteMatrix> matrix = qr.TakeMatrix();
+  return matrix->TakeArray();
 }
diff --git a/fxbarcode/qrcode/BC_QRCoder.cpp b/fxbarcode/qrcode/BC_QRCoder.cpp
index 02ab3e4..4d30830 100644
--- a/fxbarcode/qrcode/BC_QRCoder.cpp
+++ b/fxbarcode/qrcode/BC_QRCoder.cpp
@@ -60,8 +60,8 @@
   return m_numRSBlocks;
 }
 
-const CBC_CommonByteMatrix* CBC_QRCoder::GetMatrix() const {
-  return m_matrix.get();
+std::unique_ptr<CBC_CommonByteMatrix> CBC_QRCoder::TakeMatrix() {
+  return std::move(m_matrix);
 }
 
 bool CBC_QRCoder::IsValid() const {
diff --git a/fxbarcode/qrcode/BC_QRCoder.h b/fxbarcode/qrcode/BC_QRCoder.h
index d7bb840..390ef6b 100644
--- a/fxbarcode/qrcode/BC_QRCoder.h
+++ b/fxbarcode/qrcode/BC_QRCoder.h
@@ -30,7 +30,7 @@
   int32_t GetNumTotalBytes() const;
   int32_t GetNumDataBytes() const;
   int32_t GetNumRSBlocks() const;
-  const CBC_CommonByteMatrix* GetMatrix() const;
+  std::unique_ptr<CBC_CommonByteMatrix> TakeMatrix();
 
   bool IsValid() const;