Nits in CFX_RenderDevice and CPDF_ContentParser

Change-Id: I29f1c4f68356e335cd55e38014699780bf658249
Reviewed-on: https://pdfium-review.googlesource.com/7610
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Nicolás Peña <npm@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_contentparser.h b/core/fpdfapi/page/cpdf_contentparser.h
index b18e070..982a624 100644
--- a/core/fpdfapi/page/cpdf_contentparser.h
+++ b/core/fpdfapi/page/cpdf_contentparser.h
@@ -29,7 +29,9 @@
   ~CPDF_ContentParser();
 
   ParseStatus GetStatus() const { return m_Status; }
-  CPDF_StreamContentParser* GetParser() const { return m_pParser.get(); }
+  const CPDF_AllStates* GetCurStates() const {
+    return m_pParser ? m_pParser->GetCurStates() : nullptr;
+  }
   void Start(CPDF_Page* pPage);
   void Start(CPDF_Form* pForm,
              CPDF_AllStates* pGraphicStates,
diff --git a/core/fpdfapi/page/cpdf_pageobjectholder.cpp b/core/fpdfapi/page/cpdf_pageobjectholder.cpp
index 2b7e62b..138cb0d 100644
--- a/core/fpdfapi/page/cpdf_pageobjectholder.cpp
+++ b/core/fpdfapi/page/cpdf_pageobjectholder.cpp
@@ -39,8 +39,8 @@
     return;
 
   m_ParseState = CONTENT_PARSED;
-  if (m_pParser->GetParser() && m_pParser->GetParser()->GetCurStates())
-    m_LastCTM = m_pParser->GetParser()->GetCurStates()->m_CTM;
+  if (m_pParser->GetCurStates())
+    m_LastCTM = m_pParser->GetCurStates()->m_CTM;
   m_pParser.reset();
 }
 
diff --git a/core/fxge/cfx_renderdevice.cpp b/core/fxge/cfx_renderdevice.cpp
index b14829e..5d2ae86 100644
--- a/core/fxge/cfx_renderdevice.cpp
+++ b/core/fxge/cfx_renderdevice.cpp
@@ -138,18 +138,6 @@
   dest[3] = dest_alpha;
 }
 
-void NormalizeRgbDst(int src_value, int r, int g, int b, int a, uint8_t* dest) {
-  ApplyAlpha(dest, b, g, r, CalcAlpha(TextGammaAdjust(src_value), a));
-}
-
-void NormalizeRgbSrc(int src_value, int r, int g, int b, int a, uint8_t* dest) {
-  int src_alpha = CalcAlpha(TextGammaAdjust(src_value), a);
-  if (src_alpha == 0)
-    return;
-
-  ApplyAlpha(dest, b, g, r, src_alpha);
-}
-
 void NormalizeArgb(int src_value,
                    int r,
                    int g,
@@ -164,22 +152,36 @@
     ApplyDestAlpha(back_alpha, src_alpha, r, g, b, dest);
 }
 
-void NormalizeArgbDest(int src_value,
-                       int r,
-                       int g,
-                       int b,
-                       int a,
-                       uint8_t* dest) {
-  NormalizeArgb(src_value, r, g, b, a, dest,
-                CalcAlpha(TextGammaAdjust(src_value), a));
+void NormalizeDest(bool has_alpha,
+                   int src_value,
+                   int r,
+                   int g,
+                   int b,
+                   int a,
+                   uint8_t* dest) {
+  if (has_alpha) {
+    NormalizeArgb(src_value, r, g, b, a, dest,
+                  CalcAlpha(TextGammaAdjust(src_value), a));
+    return;
+  }
+  int src_alpha = CalcAlpha(TextGammaAdjust(src_value), a);
+  if (src_alpha == 0)
+    return;
+
+  ApplyAlpha(dest, b, g, r, src_alpha);
 }
 
-void NormalizeArgbSrc(int src_value,
-                      int r,
-                      int g,
-                      int b,
-                      int a,
-                      uint8_t* dest) {
+void NormalizeSrc(bool has_alpha,
+                  int src_value,
+                  int r,
+                  int g,
+                  int b,
+                  int a,
+                  uint8_t* dest) {
+  if (!has_alpha) {
+    ApplyAlpha(dest, b, g, r, CalcAlpha(TextGammaAdjust(src_value), a));
+    return;
+  }
   int src_alpha = CalcAlpha(TextGammaAdjust(src_value), a);
   if (src_alpha != 0)
     NormalizeArgb(src_value, r, g, b, a, dest, src_alpha);
@@ -190,12 +192,11 @@
   *dst_scan += bpp;
 }
 
-void SetAlpha(uint8_t* alpha) {
-  alpha[3] = 255;
+void SetAlpha(bool has_alpha, uint8_t* alpha) {
+  if (has_alpha)
+    alpha[3] = 255;
 }
 
-void SetAlphaDoNothing(uint8_t* alpha) {}
-
 void DrawNormalTextHelper(const CFX_RetainPtr<CFX_DIBitmap>& bitmap,
                           const CFX_RetainPtr<CFX_DIBitmap>& pGlyph,
                           int nrows,
@@ -216,10 +217,6 @@
   uint8_t* dest_buf = bitmap->GetBuffer();
   int dest_pitch = bitmap->GetPitch();
   const int Bpp = has_alpha ? 4 : bitmap->GetBPP() / 8;
-  auto* pNormalizeSrcFunc = has_alpha ? &NormalizeArgbSrc : &NormalizeRgbDst;
-  auto* pNormalizeDstFunc = has_alpha ? &NormalizeArgbDest : &NormalizeRgbSrc;
-  auto* pSetAlpha = has_alpha ? &SetAlpha : &SetAlphaDoNothing;
-
   for (int row = 0; row < nrows; ++row) {
     int dest_row = row + top;
     if (dest_row < 0 || dest_row >= bitmap->GetHeight())
@@ -229,7 +226,7 @@
     uint8_t* dest_scan = dest_buf + dest_row * dest_pitch + start_col * Bpp;
     if (bBGRStripe) {
       if (x_subpixel == 0) {
-        for (int col = start_col; col < end_col; col++) {
+        for (int col = start_col; col < end_col; ++col) {
           if (has_alpha) {
             Merge(src_scan[2], r, a, &dest_scan[2]);
             Merge(src_scan[1], g, a, &dest_scan[1]);
@@ -237,7 +234,7 @@
           } else {
             MergeGammaAdjustBgr(&src_scan[0], r, g, b, a, &dest_scan[0]);
           }
-          pSetAlpha(dest_scan);
+          SetAlpha(has_alpha, dest_scan);
           NextPixel(&src_scan, &dest_scan, Bpp);
         }
         continue;
@@ -247,11 +244,11 @@
         MergeGammaAdjust(src_scan[0], g, a, &dest_scan[1]);
         if (start_col > left)
           MergeGammaAdjust(src_scan[-1], b, a, &dest_scan[0]);
-        pSetAlpha(dest_scan);
+        SetAlpha(has_alpha, dest_scan);
         NextPixel(&src_scan, &dest_scan, Bpp);
         for (int col = start_col + 1; col < end_col - 1; ++col) {
           MergeGammaAdjustBgr(&src_scan[-1], r, g, b, a, &dest_scan[0]);
-          pSetAlpha(dest_scan);
+          SetAlpha(has_alpha, dest_scan);
           NextPixel(&src_scan, &dest_scan, Bpp);
         }
         continue;
@@ -261,11 +258,11 @@
         MergeGammaAdjust(src_scan[-1], g, a, &dest_scan[1]);
         MergeGammaAdjust(src_scan[-2], b, a, &dest_scan[0]);
       }
-      pSetAlpha(dest_scan);
+      SetAlpha(has_alpha, dest_scan);
       NextPixel(&src_scan, &dest_scan, Bpp);
       for (int col = start_col + 1; col < end_col - 1; ++col) {
         MergeGammaAdjustBgr(&src_scan[-2], r, g, b, a, &dest_scan[0]);
-        pSetAlpha(dest_scan);
+        SetAlpha(has_alpha, dest_scan);
         NextPixel(&src_scan, &dest_scan, Bpp);
       }
       continue;
@@ -274,10 +271,10 @@
       for (int col = start_col; col < end_col; ++col) {
         if (bNormal) {
           int src_value = AverageRgb(&src_scan[0]);
-          pNormalizeDstFunc(src_value, r, g, b, a, dest_scan);
+          NormalizeDest(has_alpha, src_value, r, g, b, a, dest_scan);
         } else {
           MergeGammaAdjustRgb(&src_scan[0], r, g, b, a, &dest_scan[0]);
-          pSetAlpha(dest_scan);
+          SetAlpha(has_alpha, dest_scan);
         }
         NextPixel(&src_scan, &dest_scan, Bpp);
       }
@@ -287,22 +284,22 @@
       if (bNormal) {
         int src_value = start_col > left ? AverageRgb(&src_scan[-1])
                                          : (src_scan[0] + src_scan[1]) / 3;
-        pNormalizeSrcFunc(src_value, r, g, b, a, dest_scan);
+        NormalizeSrc(has_alpha, src_value, r, g, b, a, dest_scan);
       } else {
         if (start_col > left)
           MergeGammaAdjust(src_scan[-1], r, a, &dest_scan[2]);
         MergeGammaAdjust(src_scan[0], g, a, &dest_scan[1]);
         MergeGammaAdjust(src_scan[1], b, a, &dest_scan[0]);
-        pSetAlpha(dest_scan);
+        SetAlpha(has_alpha, dest_scan);
       }
       NextPixel(&src_scan, &dest_scan, Bpp);
       for (int col = start_col + 1; col < end_col; ++col) {
         if (bNormal) {
           int src_value = AverageRgb(&src_scan[-1]);
-          pNormalizeDstFunc(src_value, r, g, b, a, dest_scan);
+          NormalizeDest(has_alpha, src_value, r, g, b, a, dest_scan);
         } else {
           MergeGammaAdjustRgb(&src_scan[-1], r, g, b, a, &dest_scan[0]);
-          pSetAlpha(dest_scan);
+          SetAlpha(has_alpha, dest_scan);
         }
         NextPixel(&src_scan, &dest_scan, Bpp);
       }
@@ -311,23 +308,23 @@
     if (bNormal) {
       int src_value =
           start_col > left ? AverageRgb(&src_scan[-2]) : src_scan[0] / 3;
-      pNormalizeSrcFunc(src_value, r, g, b, a, dest_scan);
+      NormalizeSrc(has_alpha, src_value, r, g, b, a, dest_scan);
     } else {
       if (start_col > left) {
         MergeGammaAdjust(src_scan[-2], r, a, &dest_scan[2]);
         MergeGammaAdjust(src_scan[-1], g, a, &dest_scan[1]);
       }
       MergeGammaAdjust(src_scan[0], b, a, &dest_scan[0]);
-      pSetAlpha(dest_scan);
+      SetAlpha(has_alpha, dest_scan);
     }
     NextPixel(&src_scan, &dest_scan, Bpp);
     for (int col = start_col + 1; col < end_col; ++col) {
       if (bNormal) {
         int src_value = AverageRgb(&src_scan[-2]);
-        pNormalizeDstFunc(src_value, r, g, b, a, dest_scan);
+        NormalizeDest(has_alpha, src_value, r, g, b, a, dest_scan);
       } else {
         MergeGammaAdjustRgb(&src_scan[-2], r, g, b, a, &dest_scan[0]);
-        pSetAlpha(dest_scan);
+        SetAlpha(has_alpha, dest_scan);
       }
       NextPixel(&src_scan, &dest_scan, Bpp);
     }