Make CPDF_Function::Load() return an unique_ptr.

Review-Url: https://codereview.chromium.org/2000973002
diff --git a/core/fpdfapi/fpdf_page/cpdf_colorspace.cpp b/core/fpdfapi/fpdf_page/cpdf_colorspace.cpp
index 77e8c00..d66b920 100644
--- a/core/fpdfapi/fpdf_page/cpdf_colorspace.cpp
+++ b/core/fpdfapi/fpdf_page/cpdf_colorspace.cpp
@@ -6,6 +6,8 @@
 
 #include "core/fpdfapi/fpdf_page/include/cpdf_colorspace.h"
 
+#include <memory>
+
 #include "core/fpdfapi/fpdf_page/cpdf_pagemodule.h"
 #include "core/fpdfapi/fpdf_page/pageint.h"
 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
@@ -220,7 +222,7 @@
   void EnableStdConversion(FX_BOOL bEnabled) override;
 
   CPDF_ColorSpace* m_pAltCS;
-  CPDF_Function* m_pFunc;
+  std::unique_ptr<CPDF_Function> m_pFunc;
   enum { None, All, Colorant } m_Type;
 };
 
@@ -242,7 +244,7 @@
   void EnableStdConversion(FX_BOOL bEnabled) override;
 
   CPDF_ColorSpace* m_pAltCS;
-  CPDF_Function* m_pFunc;
+  std::unique_ptr<CPDF_Function> m_pFunc;
 };
 
 FX_FLOAT RGB_Conversion(FX_FLOAT colorComponent) {
@@ -1098,15 +1100,11 @@
 }
 
 CPDF_SeparationCS::CPDF_SeparationCS(CPDF_Document* pDoc)
-    : CPDF_ColorSpace(pDoc, PDFCS_SEPARATION, 1),
-      m_pAltCS(nullptr),
-      m_pFunc(nullptr) {}
+    : CPDF_ColorSpace(pDoc, PDFCS_SEPARATION, 1), m_pAltCS(nullptr) {}
 
 CPDF_SeparationCS::~CPDF_SeparationCS() {
-  if (m_pAltCS) {
+  if (m_pAltCS)
     m_pAltCS->ReleaseCS();
-  }
-  delete m_pFunc;
 }
 
 void CPDF_SeparationCS::GetDefaultValue(int iComponent,
@@ -1122,25 +1120,24 @@
   CFX_ByteString name = pArray->GetStringAt(1);
   if (name == "None") {
     m_Type = None;
-  } else {
-    m_Type = Colorant;
-    CPDF_Object* pAltCS = pArray->GetDirectObjectAt(2);
-    if (pAltCS == m_pArray) {
-      return FALSE;
-    }
-    m_pAltCS = Load(pDoc, pAltCS);
-    if (!m_pAltCS) {
-      return FALSE;
-    }
-    CPDF_Object* pFuncObj = pArray->GetDirectObjectAt(3);
-    if (pFuncObj && !pFuncObj->IsName())
-      m_pFunc = CPDF_Function::Load(pFuncObj);
-
-    if (m_pFunc && m_pFunc->CountOutputs() < m_pAltCS->CountComponents()) {
-      delete m_pFunc;
-      m_pFunc = NULL;
-    }
+    return TRUE;
   }
+
+  m_Type = Colorant;
+  CPDF_Object* pAltCS = pArray->GetDirectObjectAt(2);
+  if (pAltCS == m_pArray)
+    return FALSE;
+
+  m_pAltCS = Load(pDoc, pAltCS);
+  if (!m_pAltCS)
+    return FALSE;
+
+  CPDF_Object* pFuncObj = pArray->GetDirectObjectAt(3);
+  if (pFuncObj && !pFuncObj->IsName())
+    m_pFunc = CPDF_Function::Load(pFuncObj);
+
+  if (m_pFunc && m_pFunc->CountOutputs() < m_pAltCS->CountComponents())
+    m_pFunc.reset();
   return TRUE;
 }
 
@@ -1148,50 +1145,47 @@
                                   FX_FLOAT& R,
                                   FX_FLOAT& G,
                                   FX_FLOAT& B) const {
-  if (m_Type == None) {
+  if (m_Type == None)
     return FALSE;
-  }
+
   if (!m_pFunc) {
-    if (!m_pAltCS) {
+    if (!m_pAltCS)
       return FALSE;
-    }
+
     int nComps = m_pAltCS->CountComponents();
     CFX_FixedBufGrow<FX_FLOAT, 16> results(nComps);
-    for (int i = 0; i < nComps; i++) {
+    for (int i = 0; i < nComps; i++)
       results[i] = *pBuf;
-    }
     return m_pAltCS->GetRGB(results, R, G, B);
   }
+
   CFX_FixedBufGrow<FX_FLOAT, 16> results(m_pFunc->CountOutputs());
   int nresults = 0;
   m_pFunc->Call(pBuf, 1, results, nresults);
-  if (nresults == 0) {
+  if (nresults == 0)
     return FALSE;
-  }
-  if (m_pAltCS) {
+
+  if (m_pAltCS)
     return m_pAltCS->GetRGB(results, R, G, B);
-  }
-  R = G = B = 0;
+
+  R = 0;
+  G = 0;
+  B = 0;
   return FALSE;
 }
 
 void CPDF_SeparationCS::EnableStdConversion(FX_BOOL bEnabled) {
   CPDF_ColorSpace::EnableStdConversion(bEnabled);
-  if (m_pAltCS) {
+  if (m_pAltCS)
     m_pAltCS->EnableStdConversion(bEnabled);
-  }
 }
 
 CPDF_DeviceNCS::CPDF_DeviceNCS(CPDF_Document* pDoc)
-    : CPDF_ColorSpace(pDoc, PDFCS_DEVICEN, 0),
-      m_pAltCS(nullptr),
-      m_pFunc(nullptr) {}
+    : CPDF_ColorSpace(pDoc, PDFCS_DEVICEN, 0), m_pAltCS(nullptr) {}
 
 CPDF_DeviceNCS::~CPDF_DeviceNCS() {
-  delete m_pFunc;
-  if (m_pAltCS) {
+  if (m_pAltCS)
     m_pAltCS->ReleaseCS();
-  }
 }
 
 void CPDF_DeviceNCS::GetDefaultValue(int iComponent,
@@ -1210,33 +1204,30 @@
 
   m_nComponents = pObj->GetCount();
   CPDF_Object* pAltCS = pArray->GetDirectObjectAt(2);
-  if (!pAltCS || pAltCS == m_pArray) {
+  if (!pAltCS || pAltCS == m_pArray)
     return FALSE;
-  }
+
   m_pAltCS = Load(pDoc, pAltCS);
   m_pFunc = CPDF_Function::Load(pArray->GetDirectObjectAt(3));
-  if (!m_pAltCS || !m_pFunc) {
+  if (!m_pAltCS || !m_pFunc)
     return FALSE;
-  }
-  if (m_pFunc->CountOutputs() < m_pAltCS->CountComponents()) {
-    return FALSE;
-  }
-  return TRUE;
+
+  return m_pFunc->CountOutputs() >= m_pAltCS->CountComponents();
 }
 
 FX_BOOL CPDF_DeviceNCS::GetRGB(FX_FLOAT* pBuf,
                                FX_FLOAT& R,
                                FX_FLOAT& G,
                                FX_FLOAT& B) const {
-  if (!m_pFunc) {
+  if (!m_pFunc)
     return FALSE;
-  }
+
   CFX_FixedBufGrow<FX_FLOAT, 16> results(m_pFunc->CountOutputs());
   int nresults = 0;
   m_pFunc->Call(pBuf, m_nComponents, results, nresults);
-  if (nresults == 0) {
+  if (nresults == 0)
     return FALSE;
-  }
+
   return m_pAltCS->GetRGB(results, R, G, B);
 }
 
diff --git a/core/fpdfapi/fpdf_page/cpdf_meshstream.cpp b/core/fpdfapi/fpdf_page/cpdf_meshstream.cpp
index f4a1c16..491a480 100644
--- a/core/fpdfapi/fpdf_page/cpdf_meshstream.cpp
+++ b/core/fpdfapi/fpdf_page/cpdf_meshstream.cpp
@@ -10,15 +10,14 @@
 #include "core/fpdfapi/fpdf_page/pageint.h"
 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
 
-FX_BOOL CPDF_MeshStream::Load(CPDF_Stream* pShadingStream,
-                              CPDF_Function** pFuncs,
-                              int nFuncs,
-                              CPDF_ColorSpace* pCS) {
+CPDF_MeshStream::CPDF_MeshStream(
+    const std::vector<std::unique_ptr<CPDF_Function>>& funcs,
+    CPDF_ColorSpace* pCS)
+    : m_funcs(funcs), m_pCS(pCS) {}
+
+bool CPDF_MeshStream::Load(CPDF_Stream* pShadingStream) {
   m_Stream.LoadAllData(pShadingStream);
   m_BitStream.Init(m_Stream.GetData(), m_Stream.GetSize());
-  m_pFuncs = pFuncs;
-  m_nFuncs = nFuncs;
-  m_pCS = pCS;
   CPDF_Dictionary* pDict = pShadingStream->GetDict();
   m_nCoordBits = pDict->GetIntegerBy("BitsPerCoordinate");
   m_nCompBits = pDict->GetIntegerBy("BitsPerComponent");
@@ -26,14 +25,11 @@
   if (!m_nCoordBits || !m_nCompBits)
     return FALSE;
 
-  uint32_t nComps = pCS->CountComponents();
+  uint32_t nComps = m_pCS->CountComponents();
   if (nComps > 8)
     return FALSE;
 
-  m_nComps = nFuncs ? 1 : nComps;
-  if (((int)m_nComps < 0) || m_nComps > 8)
-    return FALSE;
-
+  m_nComps = m_funcs.empty() ? nComps : 1;
   m_CoordMax = m_nCoordBits == 32 ? -1 : (1 << m_nCoordBits) - 1;
   m_CompMax = (1 << m_nCompBits) - 1;
   CPDF_Array* pDecode = pDict->GetArrayBy("Decode");
@@ -44,7 +40,7 @@
   m_xmax = pDecode->GetNumberAt(1);
   m_ymin = pDecode->GetNumberAt(2);
   m_ymax = pDecode->GetNumberAt(3);
-  for (uint32_t i = 0; i < m_nComps; i++) {
+  for (uint32_t i = 0; i < m_nComps; ++i) {
     m_ColorMin[i] = pDecode->GetNumberAt(i * 2 + 4);
     m_ColorMax[i] = pDecode->GetNumberAt(i * 2 + 5);
   }
@@ -70,27 +66,26 @@
 }
 
 void CPDF_MeshStream::GetColor(FX_FLOAT& r, FX_FLOAT& g, FX_FLOAT& b) {
-  uint32_t i;
-  FX_FLOAT color_value[8];
-  for (i = 0; i < m_nComps; i++) {
+  static const int kMaxResults = 8;
+  FX_FLOAT color_value[kMaxResults];
+  for (uint32_t i = 0; i < m_nComps; ++i) {
     color_value[i] = m_ColorMin[i] +
                      m_BitStream.GetBits(m_nCompBits) *
                          (m_ColorMax[i] - m_ColorMin[i]) / m_CompMax;
   }
-  if (m_nFuncs) {
-    static const int kMaxResults = 8;
-    FX_FLOAT result[kMaxResults];
-    int nResults;
-    FXSYS_memset(result, 0, sizeof(result));
-    for (uint32_t i = 0; i < m_nFuncs; i++) {
-      if (m_pFuncs[i] && m_pFuncs[i]->CountOutputs() <= kMaxResults) {
-        m_pFuncs[i]->Call(color_value, 1, result, nResults);
-      }
-    }
-    m_pCS->GetRGB(result, r, g, b);
-  } else {
+  if (m_funcs.empty()) {
     m_pCS->GetRGB(color_value, r, g, b);
+    return;
   }
+
+  FX_FLOAT result[kMaxResults];
+  FXSYS_memset(result, 0, sizeof(result));
+  int nResults;
+  for (const auto& func : m_funcs) {
+    if (func && func->CountOutputs() <= kMaxResults)
+      func->Call(color_value, 1, result, nResults);
+  }
+  m_pCS->GetRGB(result, r, g, b);
 }
 
 uint32_t CPDF_MeshStream::GetVertex(CPDF_MeshVertex& vertex,
diff --git a/core/fpdfapi/fpdf_page/cpdf_meshstream.h b/core/fpdfapi/fpdf_page/cpdf_meshstream.h
index 462dcf0..e5d37fe 100644
--- a/core/fpdfapi/fpdf_page/cpdf_meshstream.h
+++ b/core/fpdfapi/fpdf_page/cpdf_meshstream.h
@@ -7,6 +7,9 @@
 #ifndef CORE_FPDFAPI_FPDF_PAGE_CPDF_MESHSTREAM_H_
 #define CORE_FPDFAPI_FPDF_PAGE_CPDF_MESHSTREAM_H_
 
+#include <memory>
+#include <vector>
+
 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h"
 #include "core/fxcrt/include/fx_basic.h"
 #include "core/fxcrt/include/fx_system.h"
@@ -26,11 +29,10 @@
 
 class CPDF_MeshStream {
  public:
-  FX_BOOL Load(CPDF_Stream* pShadingStream,
-               CPDF_Function** pFuncs,
-               int nFuncs,
-               CPDF_ColorSpace* pCS);
+  CPDF_MeshStream(const std::vector<std::unique_ptr<CPDF_Function>>& funcs,
+                  CPDF_ColorSpace* pCS);
 
+  bool Load(CPDF_Stream* pShadingStream);
   uint32_t GetFlag();
 
   void GetCoords(FX_FLOAT& x, FX_FLOAT& y);
@@ -41,9 +43,15 @@
                        int count,
                        CFX_Matrix* pObject2Bitmap);
 
-  CPDF_Function** m_pFuncs;
-  CPDF_ColorSpace* m_pCS;
-  uint32_t m_nFuncs;
+  CFX_BitStream* BitStream() { return &m_BitStream; }
+  uint32_t CoordBits() const { return m_nCoordBits; }
+  uint32_t CompBits() const { return m_nCompBits; }
+  uint32_t FlagBits() const { return m_nFlagBits; }
+  uint32_t comps() const { return m_nComps; }
+
+ private:
+  const std::vector<std::unique_ptr<CPDF_Function>>& m_funcs;
+  CPDF_ColorSpace* const m_pCS;
   uint32_t m_nCoordBits;
   uint32_t m_nCompBits;
   uint32_t m_nFlagBits;
diff --git a/core/fpdfapi/fpdf_page/cpdf_shadingobject.cpp b/core/fpdfapi/fpdf_page/cpdf_shadingobject.cpp
index 0858132..de84f29 100644
--- a/core/fpdfapi/fpdf_page/cpdf_shadingobject.cpp
+++ b/core/fpdfapi/fpdf_page/cpdf_shadingobject.cpp
@@ -22,7 +22,7 @@
   if (obj->m_pShading && obj->m_pShading->document()) {
     CPDF_DocPageData* pDocPageData = obj->m_pShading->document()->GetPageData();
     CPDF_Pattern* pattern = pDocPageData->GetPattern(
-        obj->m_pShading->m_pShadingObj, m_pShading->m_bShadingObj,
+        obj->m_pShading->GetShadingObject(), m_pShading->IsShadingObject(),
         obj->m_pShading->parent_matrix());
     obj->m_pShading = pattern ? pattern->AsShadingPattern() : nullptr;
   }
diff --git a/core/fpdfapi/fpdf_page/cpdf_shadingpattern.cpp b/core/fpdfapi/fpdf_page/cpdf_shadingpattern.cpp
index b7174b4..1636e17 100644
--- a/core/fpdfapi/fpdf_page/cpdf_shadingpattern.cpp
+++ b/core/fpdfapi/fpdf_page/cpdf_shadingpattern.cpp
@@ -6,6 +6,7 @@
 
 #include "core/fpdfapi/fpdf_page/cpdf_shadingpattern.h"
 
+#include "core/fpdfapi/fpdf_page/pageint.h"
 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h"
 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
@@ -34,28 +35,22 @@
       m_bShadingObj(bShading),
       m_pShadingObj(pPatternObj),
       m_pCS(nullptr),
-      m_pCountedCS(nullptr),
-      m_nFuncs(0) {
+      m_pCountedCS(nullptr) {
   if (!bShading) {
     CPDF_Dictionary* pDict = m_pPatternObj->GetDict();
     m_Pattern2Form = pDict->GetMatrixBy("Matrix");
     m_pShadingObj = pDict->GetDirectObjectBy("Shading");
     m_Pattern2Form.Concat(parentMatrix);
   }
-  for (size_t i = 0; i < FX_ArraySize(m_pFunctions); ++i)
-    m_pFunctions[i] = nullptr;
 }
 
 CPDF_ShadingPattern::~CPDF_ShadingPattern() {
-  for (size_t i = 0; i < m_nFuncs; ++i)
-    delete m_pFunctions[i];
-
   CPDF_ColorSpace* pCS = m_pCountedCS ? m_pCountedCS->get() : nullptr;
   if (pCS && m_pDocument)
     m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray());
 }
 
-FX_BOOL CPDF_ShadingPattern::Load() {
+bool CPDF_ShadingPattern::Load() {
   if (m_ShadingType != kInvalidShading)
     return TRUE;
 
@@ -64,21 +59,15 @@
   if (!pShadingDict)
     return FALSE;
 
-  if (m_nFuncs) {
-    for (size_t i = 0; i < m_nFuncs; i++)
-      delete m_pFunctions[i];
-    m_nFuncs = 0;
-  }
+  m_pFunctions.clear();
   CPDF_Object* pFunc = pShadingDict->GetDirectObjectBy("Function");
   if (pFunc) {
     if (CPDF_Array* pArray = pFunc->AsArray()) {
-      m_nFuncs = std::min<size_t>(pArray->GetCount(), 4);
-
-      for (size_t i = 0; i < m_nFuncs; i++)
+      m_pFunctions.resize(std::min<size_t>(pArray->GetCount(), 4));
+      for (size_t i = 0; i < m_pFunctions.size(); ++i)
         m_pFunctions[i] = CPDF_Function::Load(pArray->GetDirectObjectAt(i));
     } else {
-      m_pFunctions[0] = CPDF_Function::Load(pFunc);
-      m_nFuncs = 1;
+      m_pFunctions.push_back(CPDF_Function::Load(pFunc));
     }
   }
   CPDF_Object* pCSObj = pShadingDict->GetDirectObjectBy("ColorSpace");
diff --git a/core/fpdfapi/fpdf_page/cpdf_shadingpattern.h b/core/fpdfapi/fpdf_page/cpdf_shadingpattern.h
index 1872764..0ff258b 100644
--- a/core/fpdfapi/fpdf_page/cpdf_shadingpattern.h
+++ b/core/fpdfapi/fpdf_page/cpdf_shadingpattern.h
@@ -7,9 +7,11 @@
 #ifndef CORE_FPDFAPI_FPDF_PAGE_CPDF_SHADINGPATTERN_H_
 #define CORE_FPDFAPI_FPDF_PAGE_CPDF_SHADINGPATTERN_H_
 
+#include <memory>
+#include <vector>
+
 #include "core/fpdfapi/fpdf_page/cpdf_countedobject.h"
 #include "core/fpdfapi/fpdf_page/cpdf_pattern.h"
-#include "core/fpdfapi/fpdf_page/pageint.h"
 #include "core/fxcrt/include/fx_system.h"
 
 enum ShadingType {
@@ -27,6 +29,7 @@
 class CFX_Matrix;
 class CPDF_ColorSpace;
 class CPDF_Document;
+class CPDF_Function;
 class CPDF_Object;
 
 class CPDF_ShadingPattern : public CPDF_Pattern {
@@ -35,7 +38,6 @@
                       CPDF_Object* pPatternObj,
                       FX_BOOL bShading,
                       const CFX_Matrix& parentMatrix);
-
   ~CPDF_ShadingPattern() override;
 
   CPDF_TilingPattern* AsTilingPattern() override { return nullptr; }
@@ -47,8 +49,17 @@
            m_ShadingType == kCoonsPatchMeshShading ||
            m_ShadingType == kTensorProductPatchMeshShading;
   }
-  FX_BOOL Load();
+  bool Load();
 
+  ShadingType GetShadingType() const { return m_ShadingType; }
+  FX_BOOL IsShadingObject() const { return m_bShadingObj; }
+  CPDF_Object* GetShadingObject() const { return m_pShadingObj; }
+  CPDF_ColorSpace* GetCS() const { return m_pCS; }
+  const std::vector<std::unique_ptr<CPDF_Function>>& GetFuncs() {
+    return m_pFunctions;
+  }
+
+ private:
   ShadingType m_ShadingType;
   FX_BOOL m_bShadingObj;
   CPDF_Object* m_pShadingObj;
@@ -58,8 +69,7 @@
   CPDF_ColorSpace* m_pCS;
 
   CPDF_CountedColorSpace* m_pCountedCS;
-  CPDF_Function* m_pFunctions[4];
-  size_t m_nFuncs;
+  std::vector<std::unique_ptr<CPDF_Function>> m_pFunctions;
 };
 
 #endif  // CORE_FPDFAPI_FPDF_PAGE_CPDF_SHADINGPATTERN_H_
diff --git a/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp b/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp
index f0c5302..ae02cfb 100644
--- a/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp
+++ b/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp
@@ -351,8 +351,7 @@
   }
   CPDF_Pattern* pPattern = nullptr;
   if (bShading) {
-    pPattern =
-        new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, bShading, matrix);
+    pPattern = new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, TRUE, matrix);
   } else {
     CPDF_Dictionary* pDict = pPatternObj ? pPatternObj->GetDict() : nullptr;
     if (pDict) {
diff --git a/core/fpdfapi/fpdf_page/fpdf_page_func.cpp b/core/fpdfapi/fpdf_page/fpdf_page_func.cpp
index bb1d631..ed8e609 100644
--- a/core/fpdfapi/fpdf_page/fpdf_page_func.cpp
+++ b/core/fpdfapi/fpdf_page/fpdf_page_func.cpp
@@ -792,9 +792,10 @@
 }
 
 // static
-CPDF_Function* CPDF_Function::Load(CPDF_Object* pFuncObj) {
+std::unique_ptr<CPDF_Function> CPDF_Function::Load(CPDF_Object* pFuncObj) {
+  std::unique_ptr<CPDF_Function> pFunc;
   if (!pFuncObj)
-    return nullptr;
+    return pFunc;
 
   int iType = -1;
   if (CPDF_Stream* pStream = pFuncObj->AsStream())
@@ -803,7 +804,6 @@
     iType = pDict->GetIntegerBy("FunctionType");
 
   Type type = IntegerToFunctionType(iType);
-  std::unique_ptr<CPDF_Function> pFunc;
   if (type == Type::kType0Sampled)
     pFunc.reset(new CPDF_SampledFunc());
   else if (type == Type::kType2ExpotentialInterpolation)
@@ -814,8 +814,8 @@
     pFunc.reset(new CPDF_PSFunc());
 
   if (!pFunc || !pFunc->Init(pFuncObj))
-    return nullptr;
-  return pFunc.release();
+    return std::unique_ptr<CPDF_Function>();
+  return pFunc;
 }
 
 // static
diff --git a/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp
index b30006b..ddea2e6 100644
--- a/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp
+++ b/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp
@@ -31,6 +31,7 @@
 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h"
 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h"
 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h"
+#include "core/fxcrt/include/fx_safe_types.h"
 
 namespace {
 
@@ -97,17 +98,16 @@
                              : CFX_ByteStringC();
 }
 
-CFX_FloatRect GetShadingBBox(CPDF_Stream* pStream,
-                             ShadingType type,
-                             const CFX_Matrix& matrix,
-                             CPDF_Function** pFuncs,
-                             int nFuncs,
-                             CPDF_ColorSpace* pCS) {
-  if (!pStream || !pFuncs || !pCS)
+CFX_FloatRect GetShadingBBox(CPDF_ShadingPattern* pShading,
+                             const CFX_Matrix& matrix) {
+  ShadingType type = pShading->GetShadingType();
+  CPDF_Stream* pStream = ToStream(pShading->GetShadingObject());
+  CPDF_ColorSpace* pCS = pShading->GetCS();
+  if (!pStream || !pCS)
     return CFX_FloatRect(0, 0, 0, 0);
 
-  CPDF_MeshStream stream;
-  if (!stream.Load(pStream, pFuncs, nFuncs, pCS))
+  CPDF_MeshStream stream(pShading->GetFuncs(), pCS);
+  if (!stream.Load(pStream))
     return CFX_FloatRect(0, 0, 0, 0);
 
   CFX_FloatRect rect;
@@ -125,7 +125,7 @@
   if (type == kCoonsPatchMeshShading || type == kTensorProductPatchMeshShading)
     color_count = kQuadColorsPerPatch;
 
-  while (!stream.m_BitStream.IsEOF()) {
+  while (!stream.BitStream()->IsEOF()) {
     uint32_t flag = 0;
     if (type != kLatticeFormGouraudTriangleMeshShading)
       flag = stream.GetFlag();
@@ -146,10 +146,15 @@
         bStarted = true;
       }
     }
-    stream.m_BitStream.SkipBits(stream.m_nComps * stream.m_nCompBits *
-                                color_count);
+    FX_SAFE_UINT32 nBits = stream.comps();
+    nBits *= stream.CompBits();
+    nBits *= color_count;
+    if (!nBits.IsValid())
+      break;
+
+    stream.BitStream()->SkipBits(nBits.ValueOrDie());
     if (bGouraud)
-      stream.m_BitStream.ByteAlign();
+      stream.BitStream()->ByteAlign();
   }
   rect.Transform(&matrix);
   return rect;
@@ -171,16 +176,13 @@
 bool IsPathOperator(const uint8_t* buf, size_t len) {
   if (len == 1) {
     uint8_t op = buf[0];
-    if (op == kPathOperatorSubpath || op == kPathOperatorLine ||
-        op == kPathOperatorCubicBezier1 || op == kPathOperatorCubicBezier2 ||
-        op == kPathOperatorCubicBezier3) {
-      return true;
-    }
-  } else if (len == 2) {
-    if (buf[0] == kPathOperatorRectangle[0] &&
-        buf[1] == kPathOperatorRectangle[1]) {
-      return true;
-    }
+    return op == kPathOperatorSubpath || op == kPathOperatorLine ||
+           op == kPathOperatorCubicBezier1 || op == kPathOperatorCubicBezier2 ||
+           op == kPathOperatorCubicBezier3;
+  }
+  if (len == 2) {
+    return buf[0] == kPathOperatorRectangle[0] &&
+           buf[1] == kPathOperatorRectangle[1];
   }
   return false;
 }
@@ -1087,7 +1089,7 @@
   if (!pShading)
     return;
 
-  if (!pShading->m_bShadingObj || !pShading->Load())
+  if (!pShading->IsShadingObject() || !pShading->Load())
     return;
 
   std::unique_ptr<CPDF_ShadingObject> pObj(new CPDF_ShadingObject);
@@ -1097,12 +1099,8 @@
   pObj->m_Matrix.Concat(m_mtContentToUser);
   CFX_FloatRect bbox =
       pObj->m_ClipPath.IsNull() ? m_BBox : pObj->m_ClipPath.GetClipBox();
-  if (pShading->IsMeshShading()) {
-    bbox.Intersect(GetShadingBBox(ToStream(pShading->m_pShadingObj),
-                                  pShading->m_ShadingType, pObj->m_Matrix,
-                                  pShading->m_pFunctions, pShading->m_nFuncs,
-                                  pShading->m_pCS));
-  }
+  if (pShading->IsMeshShading())
+    bbox.Intersect(GetShadingBBox(pShading, pObj->m_Matrix));
   pObj->m_Left = bbox.left;
   pObj->m_Right = bbox.right;
   pObj->m_Top = bbox.top;
diff --git a/core/fpdfapi/fpdf_page/pageint.h b/core/fpdfapi/fpdf_page/pageint.h
index b884338..b2204d8 100644
--- a/core/fpdfapi/fpdf_page/pageint.h
+++ b/core/fpdfapi/fpdf_page/pageint.h
@@ -382,7 +382,7 @@
     kType4PostScript = 4,
   };
 
-  static CPDF_Function* Load(CPDF_Object* pFuncObj);
+  static std::unique_ptr<CPDF_Function> Load(CPDF_Object* pFuncObj);
   static Type IntegerToFunctionType(int iType);
 
   virtual ~CPDF_Function();
diff --git a/core/fpdfapi/fpdf_render/fpdf_render.cpp b/core/fpdfapi/fpdf_render/fpdf_render.cpp
index 4ca8a53..5ffedca 100644
--- a/core/fpdfapi/fpdf_render/fpdf_render.cpp
+++ b/core/fpdfapi/fpdf_render/fpdf_render.cpp
@@ -342,6 +342,7 @@
     pBitmap->DitherFS(pal, 16, &rect);
   }
 }
+
 void CPDF_RenderStatus::ProcessObjectNoClip(const CPDF_PageObject* pObj,
                                             const CFX_Matrix* pObj2Device) {
   FX_BOOL bRet = FALSE;
@@ -356,16 +357,16 @@
       bRet = ProcessImage(pObj->AsImage(), pObj2Device);
       break;
     case CPDF_PageObject::SHADING:
-      bRet = ProcessShading(pObj->AsShading(), pObj2Device);
-      break;
+      ProcessShading(pObj->AsShading(), pObj2Device);
+      return;
     case CPDF_PageObject::FORM:
       bRet = ProcessForm(pObj->AsForm(), pObj2Device);
       break;
   }
-  if (!bRet) {
+  if (!bRet)
     DrawObjWithBackground(pObj, pObj2Device);
-  }
 }
+
 FX_BOOL CPDF_RenderStatus::DrawObjWithBlend(const CPDF_PageObject* pObj,
                                             const CFX_Matrix* pObj2Device) {
   FX_BOOL bRet = FALSE;
@@ -1112,12 +1113,12 @@
       return nullptr;
 
     for (uint32_t i = 0; i < 3; ++i) {
-      pFuncs[2 - i].reset(CPDF_Function::Load(pArray->GetDirectObjectAt(i)));
+      pFuncs[2 - i] = CPDF_Function::Load(pArray->GetDirectObjectAt(i));
       if (!pFuncs[2 - i])
         return nullptr;
     }
   } else {
-    pFuncs[0].reset(CPDF_Function::Load(pObj));
+    pFuncs[0] = CPDF_Function::Load(pObj);
     if (!pFuncs[0])
       return nullptr;
   }
diff --git a/core/fpdfapi/fpdf_render/fpdf_render_image.cpp b/core/fpdfapi/fpdf_render/fpdf_render_image.cpp
index 53c40b6..d84c003 100644
--- a/core/fpdfapi/fpdf_render/fpdf_render_image.cpp
+++ b/core/fpdfapi/fpdf_render/fpdf_render_image.cpp
@@ -872,20 +872,21 @@
     int nComps,
     int bpc,
     const CPDF_Dictionary* pParams);
+
 CFX_DIBitmap* CPDF_RenderStatus::LoadSMask(CPDF_Dictionary* pSMaskDict,
                                            FX_RECT* pClipRect,
                                            const CFX_Matrix* pMatrix) {
-  if (!pSMaskDict) {
-    return NULL;
-  }
+  if (!pSMaskDict)
+    return nullptr;
+
   CPDF_Stream* pGroup = pSMaskDict->GetStreamBy("G");
-  if (!pGroup) {
-    return NULL;
-  }
+  if (!pGroup)
+    return nullptr;
+
   std::unique_ptr<CPDF_Function> pFunc;
   CPDF_Object* pFuncObj = pSMaskDict->GetDirectObjectBy("TR");
   if (pFuncObj && (pFuncObj->IsDictionary() || pFuncObj->IsStream()))
-    pFunc.reset(CPDF_Function::Load(pFuncObj));
+    pFunc = CPDF_Function::Load(pFuncObj);
 
   CFX_Matrix matrix = *pMatrix;
   matrix.TranslateI(-pClipRect->left, -pClipRect->top);
diff --git a/core/fpdfapi/fpdf_render/fpdf_render_pattern.cpp b/core/fpdfapi/fpdf_render/fpdf_render_pattern.cpp
index ef604ec..b8fa400 100644
--- a/core/fpdfapi/fpdf_render/fpdf_render_pattern.cpp
+++ b/core/fpdfapi/fpdf_render/fpdf_render_pattern.cpp
@@ -6,6 +6,8 @@
 
 #include "core/fpdfapi/fpdf_render/render_int.h"
 
+#include <algorithm>
+
 #include "core/fpdfapi/fpdf_page/cpdf_graphicstates.h"
 #include "core/fpdfapi/fpdf_page/cpdf_meshstream.h"
 #include "core/fpdfapi/fpdf_page/cpdf_shadingpattern.h"
@@ -21,14 +23,25 @@
 #include "core/fpdfapi/fpdf_render/include/cpdf_renderoptions.h"
 #include "core/fxge/include/fx_ge.h"
 
+namespace {
+
+uint32_t CountOutputs(
+    const std::vector<std::unique_ptr<CPDF_Function>>& funcs) {
+  uint32_t total = 0;
+  for (const auto& func : funcs) {
+    if (func)
+      total += func->CountOutputs();
+  }
+  return total;
+}
+
 #define SHADING_STEPS 256
-static void DrawAxialShading(CFX_DIBitmap* pBitmap,
-                             CFX_Matrix* pObject2Bitmap,
-                             CPDF_Dictionary* pDict,
-                             CPDF_Function** pFuncs,
-                             int nFuncs,
-                             CPDF_ColorSpace* pCS,
-                             int alpha) {
+void DrawAxialShading(CFX_DIBitmap* pBitmap,
+                      CFX_Matrix* pObject2Bitmap,
+                      CPDF_Dictionary* pDict,
+                      const std::vector<std::unique_ptr<CPDF_Function>>& funcs,
+                      CPDF_ColorSpace* pCS,
+                      int alpha) {
   ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
   CPDF_Array* pCoords = pDict->GetArrayBy("Coords");
   if (!pCoords) {
@@ -57,13 +70,8 @@
   FX_FLOAT axis_len_square = (x_span * x_span) + (y_span * y_span);
   CFX_Matrix matrix;
   matrix.SetReverse(*pObject2Bitmap);
-  uint32_t total_results = 0;
-  for (int j = 0; j < nFuncs; j++) {
-    if (pFuncs[j])
-      total_results += pFuncs[j]->CountOutputs();
-  }
-  if (pCS->CountComponents() > total_results)
-    total_results = pCS->CountComponents();
+  uint32_t total_results =
+      std::max(CountOutputs(funcs), pCS->CountComponents());
   CFX_FixedBufGrow<FX_FLOAT, 16> result_array(total_results);
   FX_FLOAT* pResults = result_array;
   FXSYS_memset(pResults, 0, total_results * sizeof(FX_FLOAT));
@@ -71,12 +79,11 @@
   for (int i = 0; i < SHADING_STEPS; i++) {
     FX_FLOAT input = (t_max - t_min) * i / SHADING_STEPS + t_min;
     int offset = 0;
-    for (int j = 0; j < nFuncs; j++) {
-      if (pFuncs[j]) {
+    for (const auto& func : funcs) {
+      if (func) {
         int nresults = 0;
-        if (pFuncs[j]->Call(&input, 1, pResults + offset, nresults)) {
+        if (func->Call(&input, 1, pResults + offset, nresults))
           offset += nresults;
-        }
       }
     }
     FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
@@ -109,13 +116,13 @@
     }
   }
 }
-static void DrawRadialShading(CFX_DIBitmap* pBitmap,
-                              CFX_Matrix* pObject2Bitmap,
-                              CPDF_Dictionary* pDict,
-                              CPDF_Function** pFuncs,
-                              int nFuncs,
-                              CPDF_ColorSpace* pCS,
-                              int alpha) {
+
+void DrawRadialShading(CFX_DIBitmap* pBitmap,
+                       CFX_Matrix* pObject2Bitmap,
+                       CPDF_Dictionary* pDict,
+                       const std::vector<std::unique_ptr<CPDF_Function>>& funcs,
+                       CPDF_ColorSpace* pCS,
+                       int alpha) {
   ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
   CPDF_Array* pCoords = pDict->GetArrayBy("Coords");
   if (!pCoords) {
@@ -141,15 +148,8 @@
     bStartExtend = pArray->GetIntegerAt(0);
     bEndExtend = pArray->GetIntegerAt(1);
   }
-  uint32_t total_results = 0;
-  for (int j = 0; j < nFuncs; j++) {
-    if (pFuncs[j]) {
-      total_results += pFuncs[j]->CountOutputs();
-    }
-  }
-  if (pCS->CountComponents() > total_results) {
-    total_results = pCS->CountComponents();
-  }
+  uint32_t total_results =
+      std::max(CountOutputs(funcs), pCS->CountComponents());
   CFX_FixedBufGrow<FX_FLOAT, 16> result_array(total_results);
   FX_FLOAT* pResults = result_array;
   FXSYS_memset(pResults, 0, total_results * sizeof(FX_FLOAT));
@@ -157,12 +157,11 @@
   for (int i = 0; i < SHADING_STEPS; i++) {
     FX_FLOAT input = (t_max - t_min) * i / SHADING_STEPS + t_min;
     int offset = 0;
-    for (int j = 0; j < nFuncs; j++) {
-      if (pFuncs[j]) {
+    for (const auto& func : funcs) {
+      if (func) {
         int nresults;
-        if (pFuncs[j]->Call(&input, 1, pResults + offset, nresults)) {
+        if (func->Call(&input, 1, pResults + offset, nresults))
           offset += nresults;
-        }
       }
     }
     FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
@@ -246,13 +245,13 @@
     }
   }
 }
-static void DrawFuncShading(CFX_DIBitmap* pBitmap,
-                            CFX_Matrix* pObject2Bitmap,
-                            CPDF_Dictionary* pDict,
-                            CPDF_Function** pFuncs,
-                            int nFuncs,
-                            CPDF_ColorSpace* pCS,
-                            int alpha) {
+
+void DrawFuncShading(CFX_DIBitmap* pBitmap,
+                     CFX_Matrix* pObject2Bitmap,
+                     CPDF_Dictionary* pDict,
+                     const std::vector<std::unique_ptr<CPDF_Function>>& funcs,
+                     CPDF_ColorSpace* pCS,
+                     int alpha) {
   ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
   CPDF_Array* pDomain = pDict->GetArrayBy("Domain");
   FX_FLOAT xmin = 0, ymin = 0, xmax = 1.0f, ymax = 1.0f;
@@ -270,13 +269,8 @@
   int width = pBitmap->GetWidth();
   int height = pBitmap->GetHeight();
   int pitch = pBitmap->GetPitch();
-  uint32_t total_results = 0;
-  for (int j = 0; j < nFuncs; j++) {
-    if (pFuncs[j])
-      total_results += pFuncs[j]->CountOutputs();
-  }
-  if (pCS->CountComponents() > total_results)
-    total_results = pCS->CountComponents();
+  uint32_t total_results =
+      std::max(CountOutputs(funcs), pCS->CountComponents());
   CFX_FixedBufGrow<FX_FLOAT, 16> result_array(total_results);
   FX_FLOAT* pResults = result_array;
   FXSYS_memset(pResults, 0, total_results * sizeof(FX_FLOAT));
@@ -292,12 +286,11 @@
       int offset = 0;
       input[0] = x;
       input[1] = y;
-      for (int j = 0; j < nFuncs; j++) {
-        if (pFuncs[j]) {
+      for (const auto& func : funcs) {
+        if (func) {
           int nresults;
-          if (pFuncs[j]->Call(input, 2, pResults + offset, nresults)) {
+          if (func->Call(input, 2, pResults + offset, nresults))
             offset += nresults;
-          }
         }
       }
       FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
@@ -307,30 +300,30 @@
     }
   }
 }
-FX_BOOL _GetScanlineIntersect(int y,
-                              FX_FLOAT x1,
-                              FX_FLOAT y1,
-                              FX_FLOAT x2,
-                              FX_FLOAT y2,
-                              FX_FLOAT& x) {
-  if (y1 == y2) {
+
+bool GetScanlineIntersect(int y,
+                          FX_FLOAT x1,
+                          FX_FLOAT y1,
+                          FX_FLOAT x2,
+                          FX_FLOAT y2,
+                          FX_FLOAT* x) {
+  if (y1 == y2)
     return FALSE;
-  }
+
   if (y1 < y2) {
-    if (y < y1 || y > y2) {
+    if (y < y1 || y > y2)
       return FALSE;
-    }
   } else {
-    if (y < y2 || y > y1) {
+    if (y < y2 || y > y1)
       return FALSE;
-    }
   }
-  x = x1 + ((x2 - x1) * (y - y1) / (y2 - y1));
+  *x = x1 + ((x2 - x1) * (y - y1) / (y2 - y1));
   return TRUE;
 }
-static void DrawGouraud(CFX_DIBitmap* pBitmap,
-                        int alpha,
-                        CPDF_MeshVertex triangle[3]) {
+
+void DrawGouraud(CFX_DIBitmap* pBitmap,
+                 int alpha,
+                 CPDF_MeshVertex triangle[3]) {
   FX_FLOAT min_y = triangle[0].y, max_y = triangle[0].y;
   for (int i = 1; i < 3; i++) {
     if (min_y > triangle[i].y) {
@@ -356,11 +349,10 @@
     for (int i = 0; i < 3; i++) {
       CPDF_MeshVertex& vertex1 = triangle[i];
       CPDF_MeshVertex& vertex2 = triangle[(i + 1) % 3];
-      FX_BOOL bIntersect = _GetScanlineIntersect(
-          y, vertex1.x, vertex1.y, vertex2.x, vertex2.y, inter_x[nIntersects]);
-      if (!bIntersect) {
+      bool bIntersect = GetScanlineIntersect(y, vertex1.x, vertex1.y, vertex2.x,
+                                             vertex2.y, &inter_x[nIntersects]);
+      if (!bIntersect)
         continue;
-      }
 
       FX_FLOAT y_dist = (y - vertex1.y) / (vertex2.y - vertex1.y);
       r[nIntersects] = vertex1.r + ((vertex2.r - vertex1.r) * y_dist);
@@ -409,23 +401,24 @@
     }
   }
 }
-static void DrawFreeGouraudShading(CFX_DIBitmap* pBitmap,
-                                   CFX_Matrix* pObject2Bitmap,
-                                   CPDF_Stream* pShadingStream,
-                                   CPDF_Function** pFuncs,
-                                   int nFuncs,
-                                   CPDF_ColorSpace* pCS,
-                                   int alpha) {
+
+void DrawFreeGouraudShading(
+    CFX_DIBitmap* pBitmap,
+    CFX_Matrix* pObject2Bitmap,
+    CPDF_Stream* pShadingStream,
+    const std::vector<std::unique_ptr<CPDF_Function>>& funcs,
+    CPDF_ColorSpace* pCS,
+    int alpha) {
   ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
 
-  CPDF_MeshStream stream;
-  if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS))
+  CPDF_MeshStream stream(funcs, pCS);
+  if (!stream.Load(pShadingStream))
     return;
 
   CPDF_MeshVertex triangle[3];
   FXSYS_memset(triangle, 0, sizeof(triangle));
 
-  while (!stream.m_BitStream.IsEOF()) {
+  while (!stream.BitStream()->IsEOF()) {
     CPDF_MeshVertex vertex;
     uint32_t flag = stream.GetVertex(vertex, pObject2Bitmap);
     if (flag == 0) {
@@ -443,36 +436,36 @@
     DrawGouraud(pBitmap, alpha, triangle);
   }
 }
-static void DrawLatticeGouraudShading(CFX_DIBitmap* pBitmap,
-                                      CFX_Matrix* pObject2Bitmap,
-                                      CPDF_Stream* pShadingStream,
-                                      CPDF_Function** pFuncs,
-                                      int nFuncs,
-                                      CPDF_ColorSpace* pCS,
-                                      int alpha) {
+
+void DrawLatticeGouraudShading(
+    CFX_DIBitmap* pBitmap,
+    CFX_Matrix* pObject2Bitmap,
+    CPDF_Stream* pShadingStream,
+    const std::vector<std::unique_ptr<CPDF_Function>>& funcs,
+    CPDF_ColorSpace* pCS,
+    int alpha) {
   ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
 
   int row_verts = pShadingStream->GetDict()->GetIntegerBy("VerticesPerRow");
   if (row_verts < 2)
     return;
 
-  CPDF_MeshStream stream;
-  if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS))
+  CPDF_MeshStream stream(funcs, pCS);
+  if (!stream.Load(pShadingStream))
     return;
 
-  CPDF_MeshVertex* vertex = FX_Alloc2D(CPDF_MeshVertex, row_verts, 2);
-  if (!stream.GetVertexRow(vertex, row_verts, pObject2Bitmap)) {
-    FX_Free(vertex);
+  std::unique_ptr<CPDF_MeshVertex, FxFreeDeleter> vertex(
+      FX_Alloc2D(CPDF_MeshVertex, row_verts, 2));
+  if (!stream.GetVertexRow(vertex.get(), row_verts, pObject2Bitmap))
     return;
-  }
+
   int last_index = 0;
   while (1) {
-    CPDF_MeshVertex* last_row = vertex + last_index * row_verts;
-    CPDF_MeshVertex* this_row = vertex + (1 - last_index) * row_verts;
-    if (!stream.GetVertexRow(this_row, row_verts, pObject2Bitmap)) {
-      FX_Free(vertex);
+    CPDF_MeshVertex* last_row = vertex.get() + last_index * row_verts;
+    CPDF_MeshVertex* this_row = vertex.get() + (1 - last_index) * row_verts;
+    if (!stream.GetVertexRow(this_row, row_verts, pObject2Bitmap))
       return;
-    }
+
     CPDF_MeshVertex triangle[3];
     for (int i = 1; i < row_verts; i++) {
       triangle[0] = last_row[i];
@@ -484,8 +477,8 @@
     }
     last_index = 1 - last_index;
   }
-  FX_Free(vertex);
 }
+
 struct Coon_BezierCoeff {
   float a, b, c, d;
   void FromPoints(float p0, float p1, float p2, float p3) {
@@ -537,6 +530,7 @@
     return dis < 0 ? -dis : dis;
   }
 };
+
 struct Coon_Bezier {
   Coon_BezierCoeff x, y;
   void FromPoints(float x0,
@@ -595,45 +589,43 @@
   }
   float Distance() { return x.Distance() + y.Distance(); }
 };
-static int _BiInterpol(int c0,
-                       int c1,
-                       int c2,
-                       int c3,
-                       int x,
-                       int y,
-                       int x_scale,
-                       int y_scale) {
+
+int BiInterpolImpl(int c0,
+                   int c1,
+                   int c2,
+                   int c3,
+                   int x,
+                   int y,
+                   int x_scale,
+                   int y_scale) {
   int x1 = c0 + (c3 - c0) * x / x_scale;
   int x2 = c1 + (c2 - c1) * x / x_scale;
   return x1 + (x2 - x1) * y / y_scale;
 }
+
 struct Coon_Color {
   Coon_Color() { FXSYS_memset(comp, 0, sizeof(int) * 3); }
   int comp[3];
+
   void BiInterpol(Coon_Color colors[4],
                   int x,
                   int y,
                   int x_scale,
                   int y_scale) {
-    for (int i = 0; i < 3; i++)
-      comp[i] =
-          _BiInterpol(colors[0].comp[i], colors[1].comp[i], colors[2].comp[i],
-                      colors[3].comp[i], x, y, x_scale, y_scale);
+    for (int i = 0; i < 3; i++) {
+      comp[i] = BiInterpolImpl(colors[0].comp[i], colors[1].comp[i],
+                               colors[2].comp[i], colors[3].comp[i], x, y,
+                               x_scale, y_scale);
+    }
   }
+
   int Distance(Coon_Color& o) {
-    int max, diff;
-    max = FXSYS_abs(comp[0] - o.comp[0]);
-    diff = FXSYS_abs(comp[1] - o.comp[1]);
-    if (max < diff) {
-      max = diff;
-    }
-    diff = FXSYS_abs(comp[2] - o.comp[2]);
-    if (max < diff) {
-      max = diff;
-    }
-    return max;
+    return std::max({FXSYS_abs(comp[0] - o.comp[0]),
+                     FXSYS_abs(comp[1] - o.comp[1]),
+                     FXSYS_abs(comp[2] - o.comp[2])});
   }
 };
+
 struct CPDF_PatchDrawer {
   Coon_Color patch_colors[4];
   int max_delta;
@@ -732,39 +724,39 @@
   }
 };
 
-bool _CheckCoonTensorPara(const CPDF_MeshStream& stream) {
-  bool bCoorBits = (stream.m_nCoordBits == 1 || stream.m_nCoordBits == 2 ||
-                    stream.m_nCoordBits == 4 || stream.m_nCoordBits == 8 ||
-                    stream.m_nCoordBits == 12 || stream.m_nCoordBits == 16 ||
-                    stream.m_nCoordBits == 24 || stream.m_nCoordBits == 32);
+bool CheckCoonTensorPara(const CPDF_MeshStream& stream) {
+  uint32_t coord = stream.CoordBits();
+  bool bCoordBitsValid =
+      (coord == 1 || coord == 2 || coord == 4 || coord == 8 || coord == 12 ||
+       coord == 16 || coord == 24 || coord == 32);
 
-  bool bCompBits = (stream.m_nCompBits == 1 || stream.m_nCompBits == 2 ||
-                    stream.m_nCompBits == 4 || stream.m_nCompBits == 8 ||
-                    stream.m_nCompBits == 12 || stream.m_nCompBits == 16);
+  uint32_t comp = stream.CompBits();
+  bool bCompBitsValid = (comp == 1 || comp == 2 || comp == 4 || comp == 8 ||
+                         comp == 12 || comp == 16);
 
-  bool bFlagBits = (stream.m_nFlagBits == 2 || stream.m_nFlagBits == 4 ||
-                    stream.m_nFlagBits == 8);
+  uint32_t flag = stream.FlagBits();
+  bool bFlagBitsValid = (flag == 2 || flag == 4 || flag == 8);
 
-  return bCoorBits && bCompBits && bFlagBits;
+  return bCoordBitsValid && bCompBitsValid && bFlagBitsValid;
 }
 
-static void DrawCoonPatchMeshes(FX_BOOL bTensor,
-                                CFX_DIBitmap* pBitmap,
-                                CFX_Matrix* pObject2Bitmap,
-                                CPDF_Stream* pShadingStream,
-                                CPDF_Function** pFuncs,
-                                int nFuncs,
-                                CPDF_ColorSpace* pCS,
-                                int fill_mode,
-                                int alpha) {
+void DrawCoonPatchMeshes(
+    FX_BOOL bTensor,
+    CFX_DIBitmap* pBitmap,
+    CFX_Matrix* pObject2Bitmap,
+    CPDF_Stream* pShadingStream,
+    const std::vector<std::unique_ptr<CPDF_Function>>& funcs,
+    CPDF_ColorSpace* pCS,
+    int fill_mode,
+    int alpha) {
   ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
 
   CFX_FxgeDevice device;
   device.Attach(pBitmap);
-  CPDF_MeshStream stream;
-  if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS))
+  CPDF_MeshStream stream(funcs, pCS);
+  if (!stream.Load(pShadingStream))
     return;
-  if (!_CheckCoonTensorPara(stream))
+  if (!CheckCoonTensorPara(stream))
     return;
 
   CPDF_PatchDrawer patch;
@@ -779,7 +771,7 @@
   }
   CFX_PointF coords[16];
   int point_count = bTensor ? 16 : 12;
-  while (!stream.m_BitStream.IsEOF()) {
+  while (!stream.BitStream()->IsEOF()) {
     uint32_t flag = stream.GetFlag();
     int iStartPoint = 0, iStartColor = 0, i = 0;
     if (flag) {
@@ -823,23 +815,59 @@
     patch.Draw(1, 1, 0, 0, C1, C2, D1, D2);
   }
 }
+
+std::unique_ptr<CFX_DIBitmap> DrawPatternBitmap(
+    CPDF_Document* pDoc,
+    CPDF_PageRenderCache* pCache,
+    CPDF_TilingPattern* pPattern,
+    const CFX_Matrix* pObject2Device,
+    int width,
+    int height,
+    int flags) {
+  std::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
+  if (!pBitmap->Create(width, height,
+                       pPattern->colored() ? FXDIB_Argb : FXDIB_8bppMask)) {
+    return std::unique_ptr<CFX_DIBitmap>();
+  }
+  CFX_FxgeDevice bitmap_device;
+  bitmap_device.Attach(pBitmap.get());
+  pBitmap->Clear(0);
+  CFX_FloatRect cell_bbox = pPattern->bbox();
+  pPattern->pattern_to_form()->TransformRect(cell_bbox);
+  pObject2Device->TransformRect(cell_bbox);
+  CFX_FloatRect bitmap_rect(0.0f, 0.0f, (FX_FLOAT)width, (FX_FLOAT)height);
+  CFX_Matrix mtAdjust;
+  mtAdjust.MatchRect(bitmap_rect, cell_bbox);
+  CFX_Matrix mtPattern2Bitmap = *pObject2Device;
+  mtPattern2Bitmap.Concat(mtAdjust);
+  CPDF_RenderOptions options;
+  if (!pPattern->colored())
+    options.m_ColorMode = RENDER_COLOR_ALPHA;
+
+  flags |= RENDER_FORCE_HALFTONE;
+  options.m_Flags = flags;
+  CPDF_RenderContext context(pDoc, pCache);
+  context.AppendLayer(pPattern->form(), &mtPattern2Bitmap);
+  context.Render(&bitmap_device, &options, nullptr);
+  return pBitmap;
+}
+
+}  // namespace
+
 void CPDF_RenderStatus::DrawShading(CPDF_ShadingPattern* pPattern,
                                     CFX_Matrix* pMatrix,
                                     FX_RECT& clip_rect,
                                     int alpha,
                                     FX_BOOL bAlphaMode) {
-  CPDF_Function** pFuncs = pPattern->m_pFunctions;
-  int nFuncs = pPattern->m_nFuncs;
-  CPDF_Dictionary* pDict = pPattern->m_pShadingObj->GetDict();
-  CPDF_ColorSpace* pColorSpace = pPattern->m_pCS;
-  if (!pColorSpace) {
+  const auto& funcs = pPattern->GetFuncs();
+  CPDF_Dictionary* pDict = pPattern->GetShadingObject()->GetDict();
+  CPDF_ColorSpace* pColorSpace = pPattern->GetCS();
+  if (!pColorSpace)
     return;
-  }
+
   FX_ARGB background = 0;
-  if (!pPattern->m_bShadingObj &&
-      pPattern->m_pShadingObj->GetDict()->KeyExist("Background")) {
-    CPDF_Array* pBackColor =
-        pPattern->m_pShadingObj->GetDict()->GetArrayBy("Background");
+  if (!pPattern->IsShadingObject() && pDict->KeyExist("Background")) {
+    CPDF_Array* pBackColor = pDict->GetArrayBy("Background");
     if (pBackColor &&
         pBackColor->GetCount() >= pColorSpace->CountComponents()) {
       CFX_FixedBufGrow<FX_FLOAT, 16> comps(pColorSpace->CountComponents());
@@ -866,70 +894,68 @@
   CFX_Matrix FinalMatrix = *pMatrix;
   FinalMatrix.Concat(*buffer.GetMatrix());
   CFX_DIBitmap* pBitmap = buffer.GetBitmap();
-  if (!pBitmap->GetBuffer()) {
+  if (!pBitmap->GetBuffer())
     return;
-  }
+
   pBitmap->Clear(background);
   int fill_mode = m_Options.m_Flags;
-  switch (pPattern->m_ShadingType) {
+  switch (pPattern->GetShadingType()) {
     case kInvalidShading:
     case kMaxShading:
       return;
     case kFunctionBasedShading:
-      DrawFuncShading(pBitmap, &FinalMatrix, pDict, pFuncs, nFuncs, pColorSpace,
-                      alpha);
+      DrawFuncShading(pBitmap, &FinalMatrix, pDict, funcs, pColorSpace, alpha);
       break;
     case kAxialShading:
-      DrawAxialShading(pBitmap, &FinalMatrix, pDict, pFuncs, nFuncs,
-                       pColorSpace, alpha);
+      DrawAxialShading(pBitmap, &FinalMatrix, pDict, funcs, pColorSpace, alpha);
       break;
     case kRadialShading:
-      DrawRadialShading(pBitmap, &FinalMatrix, pDict, pFuncs, nFuncs,
-                        pColorSpace, alpha);
+      DrawRadialShading(pBitmap, &FinalMatrix, pDict, funcs, pColorSpace,
+                        alpha);
       break;
     case kFreeFormGouraudTriangleMeshShading: {
       // The shading object can be a stream or a dictionary. We do not handle
       // the case of dictionary at the moment.
-      if (CPDF_Stream* pStream = ToStream(pPattern->m_pShadingObj)) {
-        DrawFreeGouraudShading(pBitmap, &FinalMatrix, pStream, pFuncs, nFuncs,
+      if (CPDF_Stream* pStream = ToStream(pPattern->GetShadingObject())) {
+        DrawFreeGouraudShading(pBitmap, &FinalMatrix, pStream, funcs,
                                pColorSpace, alpha);
       }
     } break;
     case kLatticeFormGouraudTriangleMeshShading: {
       // The shading object can be a stream or a dictionary. We do not handle
       // the case of dictionary at the moment.
-      if (CPDF_Stream* pStream = ToStream(pPattern->m_pShadingObj)) {
-        DrawLatticeGouraudShading(pBitmap, &FinalMatrix, pStream, pFuncs,
-                                  nFuncs, pColorSpace, alpha);
+      if (CPDF_Stream* pStream = ToStream(pPattern->GetShadingObject())) {
+        DrawLatticeGouraudShading(pBitmap, &FinalMatrix, pStream, funcs,
+                                  pColorSpace, alpha);
       }
     } break;
     case kCoonsPatchMeshShading:
     case kTensorProductPatchMeshShading: {
       // The shading object can be a stream or a dictionary. We do not handle
       // the case of dictionary at the moment.
-      if (CPDF_Stream* pStream = ToStream(pPattern->m_pShadingObj)) {
+      if (CPDF_Stream* pStream = ToStream(pPattern->GetShadingObject())) {
         DrawCoonPatchMeshes(
-            pPattern->m_ShadingType == kTensorProductPatchMeshShading, pBitmap,
-            &FinalMatrix, pStream, pFuncs, nFuncs, pColorSpace, fill_mode,
+            pPattern->GetShadingType() == kTensorProductPatchMeshShading,
+            pBitmap, &FinalMatrix, pStream, funcs, pColorSpace, fill_mode,
             alpha);
       }
     } break;
   }
-  if (bAlphaMode) {
+  if (bAlphaMode)
     pBitmap->LoadChannel(FXDIB_Red, pBitmap, FXDIB_Alpha);
-  }
-  if (m_Options.m_ColorMode == RENDER_COLOR_GRAY) {
+
+  if (m_Options.m_ColorMode == RENDER_COLOR_GRAY)
     pBitmap->ConvertColorScale(m_Options.m_ForeColor, m_Options.m_BackColor);
-  }
   buffer.OutputToDevice();
 }
+
 void CPDF_RenderStatus::DrawShadingPattern(CPDF_ShadingPattern* pattern,
                                            const CPDF_PageObject* pPageObj,
                                            const CFX_Matrix* pObj2Device,
                                            FX_BOOL bStroke) {
-  if (!pattern->Load()) {
+  if (!pattern->Load())
     return;
-  }
+
   m_pDevice->SaveState();
   if (pPageObj->IsPath()) {
     if (!SelectClipPath(pPageObj->AsPath(), pObj2Device, bStroke)) {
@@ -954,56 +980,22 @@
               m_Options.m_ColorMode == RENDER_COLOR_ALPHA);
   m_pDevice->RestoreState();
 }
-FX_BOOL CPDF_RenderStatus::ProcessShading(const CPDF_ShadingObject* pShadingObj,
-                                          const CFX_Matrix* pObj2Device) {
+
+void CPDF_RenderStatus::ProcessShading(const CPDF_ShadingObject* pShadingObj,
+                                       const CFX_Matrix* pObj2Device) {
   FX_RECT rect = pShadingObj->GetBBox(pObj2Device);
   FX_RECT clip_box = m_pDevice->GetClipBox();
   rect.Intersect(clip_box);
-  if (rect.IsEmpty()) {
-    return TRUE;
-  }
+  if (rect.IsEmpty())
+    return;
+
   CFX_Matrix matrix = pShadingObj->m_Matrix;
   matrix.Concat(*pObj2Device);
   DrawShading(pShadingObj->m_pShading, &matrix, rect,
               pShadingObj->m_GeneralState.GetAlpha(FALSE),
               m_Options.m_ColorMode == RENDER_COLOR_ALPHA);
-  return TRUE;
 }
-static CFX_DIBitmap* DrawPatternBitmap(CPDF_Document* pDoc,
-                                       CPDF_PageRenderCache* pCache,
-                                       CPDF_TilingPattern* pPattern,
-                                       const CFX_Matrix* pObject2Device,
-                                       int width,
-                                       int height,
-                                       int flags) {
-  CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
-  if (!pBitmap->Create(width, height,
-                       pPattern->colored() ? FXDIB_Argb : FXDIB_8bppMask)) {
-    delete pBitmap;
-    return NULL;
-  }
-  CFX_FxgeDevice bitmap_device;
-  bitmap_device.Attach(pBitmap);
-  pBitmap->Clear(0);
-  CFX_FloatRect cell_bbox = pPattern->bbox();
-  pPattern->pattern_to_form()->TransformRect(cell_bbox);
-  pObject2Device->TransformRect(cell_bbox);
-  CFX_FloatRect bitmap_rect(0.0f, 0.0f, (FX_FLOAT)width, (FX_FLOAT)height);
-  CFX_Matrix mtAdjust;
-  mtAdjust.MatchRect(bitmap_rect, cell_bbox);
-  CFX_Matrix mtPattern2Bitmap = *pObject2Device;
-  mtPattern2Bitmap.Concat(mtAdjust);
-  CPDF_RenderOptions options;
-  if (!pPattern->colored())
-    options.m_ColorMode = RENDER_COLOR_ALPHA;
 
-  flags |= RENDER_FORCE_HALFTONE;
-  options.m_Flags = flags;
-  CPDF_RenderContext context(pDoc, pCache);
-  context.AppendLayer(pPattern->form(), &mtPattern2Bitmap);
-  context.Render(&bitmap_device, &options, nullptr);
-  return pBitmap;
-}
 void CPDF_RenderStatus::DrawTilingPattern(CPDF_TilingPattern* pPattern,
                                           const CPDF_PageObject* pPageObj,
                                           const CFX_Matrix* pObj2Device,
@@ -1120,13 +1112,12 @@
   }
   FX_FLOAT left_offset = cell_bbox.left - mtPattern2Device.e;
   FX_FLOAT top_offset = cell_bbox.bottom - mtPattern2Device.f;
-  CFX_DIBitmap* pPatternBitmap = NULL;
+  std::unique_ptr<CFX_DIBitmap> pPatternBitmap;
   if (width * height < 16) {
-    CFX_DIBitmap* pEnlargedBitmap =
+    std::unique_ptr<CFX_DIBitmap> pEnlargedBitmap =
         DrawPatternBitmap(m_pContext->GetDocument(), m_pContext->GetPageCache(),
                           pPattern, pObj2Device, 8, 8, m_Options.m_Flags);
-    pPatternBitmap = pEnlargedBitmap->StretchTo(width, height);
-    delete pEnlargedBitmap;
+    pPatternBitmap.reset(pEnlargedBitmap->StretchTo(width, height));
   } else {
     pPatternBitmap = DrawPatternBitmap(
         m_pContext->GetDocument(), m_pContext->GetPageCache(), pPattern,
@@ -1177,10 +1168,10 @@
       } else {
         if (pPattern->colored()) {
           screen.CompositeBitmap(start_x, start_y, width, height,
-                                 pPatternBitmap, 0, 0);
+                                 pPatternBitmap.get(), 0, 0);
         } else {
-          screen.CompositeMask(start_x, start_y, width, height, pPatternBitmap,
-                               fill_argb, 0, 0);
+          screen.CompositeMask(start_x, start_y, width, height,
+                               pPatternBitmap.get(), fill_argb, 0, 0);
         }
       }
     }
@@ -1188,7 +1179,6 @@
   CompositeDIBitmap(&screen, clip_box.left, clip_box.top, 0, 255,
                     FXDIB_BLEND_NORMAL, FALSE);
   m_pDevice->RestoreState();
-  delete pPatternBitmap;
 }
 
 void CPDF_RenderStatus::DrawPathWithPattern(const CPDF_PathObject* pPathObj,
diff --git a/core/fpdfapi/fpdf_render/render_int.h b/core/fpdfapi/fpdf_render/render_int.h
index 464ca38..46689d3 100644
--- a/core/fpdfapi/fpdf_render/render_int.h
+++ b/core/fpdfapi/fpdf_render/render_int.h
@@ -205,8 +205,8 @@
                          int bitmap_alpha,
                          int blend_mode,
                          int bIsolated);
-  FX_BOOL ProcessShading(const CPDF_ShadingObject* pShadingObj,
-                         const CFX_Matrix* pObj2Device);
+  void ProcessShading(const CPDF_ShadingObject* pShadingObj,
+                      const CFX_Matrix* pObj2Device);
   void DrawShading(CPDF_ShadingPattern* pPattern,
                    CFX_Matrix* pMatrix,
                    FX_RECT& clip_rect,