Adjust PNG_PredictLine() parameters

Move some calculations outside of the function to the callers. Then
GetNextLineWithoutPredictedPitch() can do the calculation once and reuse
the results in multiple PNG_PredictLine() calls.

Change-Id: I20619b8de495c12852910ebc0bd4c157196c6f56
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/119314
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/core/fxcodec/flate/flatemodule.cpp b/core/fxcodec/flate/flatemodule.cpp
index 95b5d8e..23e4660 100644
--- a/core/fxcodec/flate/flatemodule.cpp
+++ b/core/fxcodec/flate/flatemodule.cpp
@@ -335,14 +335,11 @@
 void PNG_PredictLine(pdfium::span<uint8_t> dest_span,
                      pdfium::span<const uint8_t> src_span,
                      pdfium::span<const uint8_t> last_span,
-                     int bpc,
-                     int nColors,
-                     int nPixels) {
-  const uint32_t row_size = fxge::CalculatePitch8OrDie(bpc, nColors, nPixels);
+                     uint32_t row_size,
+                     uint32_t bytes_per_pixel) {
   const uint8_t tag = src_span.front();
   pdfium::span<const uint8_t> remaining_src_span =
       src_span.subspan(1, row_size);
-  const uint32_t bytes_per_pixel = (bpc * nColors + 7) / 8;
   switch (tag) {
     case 1: {
       for (size_t i = 0; i < remaining_src_span.size(); ++i) {
@@ -764,19 +761,25 @@
 
 void FlatePredictorScanlineDecoder::GetNextLineWithPredictedPitch() {
   switch (m_Predictor) {
-    case PredictorType::kPng:
+    case PredictorType::kPng: {
+      const uint32_t row_size =
+          fxge::CalculatePitch8OrDie(m_BitsPerComponent, m_Colors, m_Columns);
+      const uint32_t bytes_per_pixel = (m_BitsPerComponent * m_Colors + 7) / 8;
       FlateOutput(m_pFlate.get(), m_PredictRaw.data(), m_PredictPitch + 1);
-      PNG_PredictLine(m_Scanline, m_PredictRaw, m_LastLine, m_BitsPerComponent,
-                      m_Colors, m_Columns);
+      PNG_PredictLine(m_Scanline, m_PredictRaw, m_LastLine, row_size,
+                      bytes_per_pixel);
       FXSYS_memcpy(m_LastLine.data(), m_Scanline.data(), m_PredictPitch);
       break;
-    case PredictorType::kFlate:
+    }
+    case PredictorType::kFlate: {
       FlateOutput(m_pFlate.get(), m_Scanline.data(), m_Pitch);
       TIFF_PredictLine(pdfium::make_span(m_Scanline).first(m_PredictPitch),
                        m_bpc, m_nComps, m_OutputWidth);
       break;
-    case PredictorType::kNone:
+    }
+    case PredictorType::kNone: {
       NOTREACHED_NORETURN();
+    }
   }
 }
 
@@ -789,21 +792,27 @@
     m_LeftOver -= read_leftover;
     bytes_to_go -= read_leftover;
   }
+  const uint32_t row_size =
+      fxge::CalculatePitch8OrDie(m_BitsPerComponent, m_Colors, m_Columns);
+  const uint32_t bytes_per_pixel = (m_BitsPerComponent * m_Colors + 7) / 8;
   while (bytes_to_go) {
     switch (m_Predictor) {
-      case PredictorType::kPng:
+      case PredictorType::kPng: {
         FlateOutput(m_pFlate.get(), m_PredictRaw.data(), m_PredictPitch + 1);
-        PNG_PredictLine(m_PredictBuffer, m_PredictRaw, m_LastLine,
-                        m_BitsPerComponent, m_Colors, m_Columns);
+        PNG_PredictLine(m_PredictBuffer, m_PredictRaw, m_LastLine, row_size,
+                        bytes_per_pixel);
         FXSYS_memcpy(m_LastLine.data(), m_PredictBuffer.data(), m_PredictPitch);
         break;
-      case PredictorType::kFlate:
+      }
+      case PredictorType::kFlate: {
         FlateOutput(m_pFlate.get(), m_PredictBuffer.data(), m_PredictPitch);
         TIFF_PredictLine(m_PredictBuffer, m_BitsPerComponent, m_Colors,
                          m_Columns);
         break;
-      case PredictorType::kNone:
+      }
+      case PredictorType::kNone: {
         NOTREACHED_NORETURN();
+      }
     }
     size_t read_bytes =
         m_PredictPitch > bytes_to_go ? bytes_to_go : m_PredictPitch;