Clean up Gouraud / Mesh shading code.

- Properly initialize data structures, instead of using memset().
- Fix various style nits and lint errors.

Change-Id: I9d1b491ae30afe7a376986497a9768274aaac22b
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/70910
Reviewed-by: Daniel Hosseinian <dhoss@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_meshstream.cpp b/core/fpdfapi/page/cpdf_meshstream.cpp
index 1376c3b..eb1e011 100644
--- a/core/fpdfapi/page/cpdf_meshstream.cpp
+++ b/core/fpdfapi/page/cpdf_meshstream.cpp
@@ -102,19 +102,7 @@
       m_funcs(funcs),
       m_pShadingStream(pShadingStream),
       m_pCS(pCS),
-      m_nCoordBits(0),
-      m_nComponentBits(0),
-      m_nFlagBits(0),
-      m_nComponents(0),
-      m_CoordMax(0),
-      m_ComponentMax(0),
-      m_xmin(0),
-      m_xmax(0),
-      m_ymin(0),
-      m_ymax(0),
       m_pStream(pdfium::MakeRetain<CPDF_StreamAcc>(pShadingStream)) {
-  memset(&m_ColorMin, 0, sizeof(m_ColorMin));
-  memset(&m_ColorMax, 0, sizeof(m_ColorMax));
 }
 
 CPDF_MeshStream::~CPDF_MeshStream() = default;
@@ -214,8 +202,7 @@
     return std::tuple<float, float, float>(r, g, b);
   }
 
-  float result[kMaxComponents];
-  memset(result, 0, sizeof(result));
+  float result[kMaxComponents] = {};
   int nResults;
   for (const auto& func : m_funcs) {
     if (func && func->CountOutputs() <= kMaxComponents)
diff --git a/core/fpdfapi/page/cpdf_meshstream.h b/core/fpdfapi/page/cpdf_meshstream.h
index 5f93e0d..a152ecb 100644
--- a/core/fpdfapi/page/cpdf_meshstream.h
+++ b/core/fpdfapi/page/cpdf_meshstream.h
@@ -25,9 +25,9 @@
   ~CPDF_MeshVertex();
 
   CFX_PointF position;
-  float r;
-  float g;
-  float b;
+  float r = 0.0f;
+  float g = 0.0f;
+  float b = 0.0f;
 };
 
 class CFX_Matrix;
@@ -70,20 +70,20 @@
   const std::vector<std::unique_ptr<CPDF_Function>>& m_funcs;
   RetainPtr<const CPDF_Stream> const m_pShadingStream;
   RetainPtr<CPDF_ColorSpace> const m_pCS;
-  uint32_t m_nCoordBits;
-  uint32_t m_nComponentBits;
-  uint32_t m_nFlagBits;
-  uint32_t m_nComponents;
-  uint32_t m_CoordMax;
-  uint32_t m_ComponentMax;
-  float m_xmin;
-  float m_xmax;
-  float m_ymin;
-  float m_ymax;
+  uint32_t m_nCoordBits = 0;
+  uint32_t m_nComponentBits = 0;
+  uint32_t m_nFlagBits = 0;
+  uint32_t m_nComponents = 0;
+  uint32_t m_CoordMax = 0;
+  uint32_t m_ComponentMax = 0;
+  float m_xmin = 0.0f;
+  float m_xmax = 0.0f;
+  float m_ymin = 0.0f;
+  float m_ymax = 0.0f;
   RetainPtr<CPDF_StreamAcc> m_pStream;
   std::unique_ptr<CFX_BitStream> m_BitStream;
-  float m_ColorMin[kMaxComponents];
-  float m_ColorMax[kMaxComponents];
+  float m_ColorMin[kMaxComponents] = {};
+  float m_ColorMax[kMaxComponents] = {};
 };
 
 #endif  // CORE_FPDFAPI_PAGE_CPDF_MESHSTREAM_H_
diff --git a/core/fpdfapi/render/cpdf_rendershading.cpp b/core/fpdfapi/render/cpdf_rendershading.cpp
index 5c20028..02e5490 100644
--- a/core/fpdfapi/render/cpdf_rendershading.cpp
+++ b/core/fpdfapi/render/cpdf_rendershading.cpp
@@ -342,9 +342,8 @@
   if (min_y == max_y)
     return;
 
-  int min_yi = std::max(static_cast<int>(floor(min_y)), 0);
-  int max_yi = static_cast<int>(ceil(max_y));
-
+  int min_yi = std::max(static_cast<int>(floorf(min_y)), 0);
+  int max_yi = static_cast<int>(ceilf(max_y));
   if (max_yi >= pBitmap->GetHeight())
     max_yi = pBitmap->GetHeight() - 1;
 
@@ -373,39 +372,40 @@
     if (nIntersects != 2)
       continue;
 
-    int min_x, max_x, start_index, end_index;
+    int min_x;
+    int max_x;
+    int start_index;
+    int end_index;
     if (inter_x[0] < inter_x[1]) {
-      min_x = (int)floor(inter_x[0]);
-      max_x = (int)ceil(inter_x[1]);
+      min_x = static_cast<int>(floorf(inter_x[0]));
+      max_x = static_cast<int>(ceilf(inter_x[1]));
       start_index = 0;
       end_index = 1;
     } else {
-      min_x = (int)floor(inter_x[1]);
-      max_x = (int)ceil(inter_x[0]);
+      min_x = static_cast<int>(floorf(inter_x[1]));
+      max_x = static_cast<int>(ceilf(inter_x[0]));
       start_index = 1;
       end_index = 0;
     }
 
     int start_x = std::max(min_x, 0);
-    int end_x = max_x;
-    if (end_x > pBitmap->GetWidth())
-      end_x = pBitmap->GetWidth();
+    int end_x = std::min(max_x, pBitmap->GetWidth());
 
     uint8_t* dib_buf =
         pBitmap->GetBuffer() + y * pBitmap->GetPitch() + start_x * 4;
     float r_unit = (r[end_index] - r[start_index]) / (max_x - min_x);
     float g_unit = (g[end_index] - g[start_index]) / (max_x - min_x);
     float b_unit = (b[end_index] - b[start_index]) / (max_x - min_x);
-    float R = r[start_index] + (start_x - min_x) * r_unit;
-    float G = g[start_index] + (start_x - min_x) * g_unit;
-    float B = b[start_index] + (start_x - min_x) * b_unit;
+    float r_result = r[start_index] + (start_x - min_x) * r_unit;
+    float g_result = g[start_index] + (start_x - min_x) * g_unit;
+    float b_result = b[start_index] + (start_x - min_x) * b_unit;
     for (int x = start_x; x < end_x; x++) {
-      R += r_unit;
-      G += g_unit;
-      B += b_unit;
-      FXARGB_SETDIB(dib_buf,
-                    ArgbEncode(alpha, (int32_t)(R * 255), (int32_t)(G * 255),
-                               (int32_t)(B * 255)));
+      r_result += r_unit;
+      g_result += g_unit;
+      b_result += b_unit;
+      FXARGB_SETDIB(dib_buf, ArgbEncode(alpha, static_cast<int>(r_result * 255),
+                                        static_cast<int>(g_result * 255),
+                                        static_cast<int>(b_result * 255)));
       dib_buf += 4;
     }
   }
@@ -426,8 +426,6 @@
     return;
 
   CPDF_MeshVertex triangle[3];
-  memset(triangle, 0, sizeof(triangle));
-
   while (!stream.BitStream()->IsEOF()) {
     CPDF_MeshVertex vertex;
     uint32_t flag;
@@ -436,9 +434,9 @@
 
     if (flag == 0) {
       triangle[0] = vertex;
-      for (int j = 1; j < 3; j++) {
-        uint32_t tflag;
-        if (!stream.ReadVertex(mtObject2Bitmap, &triangle[j], &tflag))
+      for (int i = 1; i < 3; ++i) {
+        uint32_t dummy_flag;
+        if (!stream.ReadVertex(mtObject2Bitmap, &triangle[i], &dummy_flag))
           return;
       }
     } else {
@@ -916,29 +914,35 @@
     case kFreeFormGouraudTriangleMeshShading: {
       // The shading object can be a stream or a dictionary. We do not handle
       // the case of dictionary at the moment.
-      if (const CPDF_Stream* pStream = ToStream(pPattern->GetShadingObject())) {
+      const CPDF_Stream* pStream = ToStream(pPattern->GetShadingObject());
+      if (pStream) {
         DrawFreeGouraudShading(pBitmap, FinalMatrix, pStream, funcs,
                                pColorSpace, alpha);
       }
-    } break;
+      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 (const CPDF_Stream* pStream = ToStream(pPattern->GetShadingObject())) {
+      const CPDF_Stream* pStream = ToStream(pPattern->GetShadingObject());
+      if (pStream) {
         DrawLatticeGouraudShading(pBitmap, FinalMatrix, pStream, funcs,
                                   pColorSpace, alpha);
       }
-    } break;
+      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 (const CPDF_Stream* pStream = ToStream(pPattern->GetShadingObject())) {
+      const CPDF_Stream* pStream = ToStream(pPattern->GetShadingObject());
+      if (pStream) {
         DrawCoonPatchMeshes(pPattern->GetShadingType(), pBitmap, FinalMatrix,
                             pStream, funcs, pColorSpace,
                             options.GetOptions().bNoPathSmooth, alpha);
       }
-    } break;
+      break;
+    }
   }
   if (bAlphaMode)
     pBitmap->LoadChannelFromAlpha(FXDIB_Red, pBitmap);