Call PNG_PredictLine() from PNG_Predictor()

It turns out PNG_Predictor(), which processes data line-by-line, can use
PNG_PredictLine() with enough refactoring to show its inner-loop is the
same as PNG_PredictLine().

Change-Id: I8501f6ce3fbd7d030f1d7bdfdff38d74ea90780f
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/119316
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcodec/flate/flatemodule.cpp b/core/fxcodec/flate/flatemodule.cpp
index c5372a2..e92b038 100644
--- a/core/fxcodec/flate/flatemodule.cpp
+++ b/core/fxcodec/flate/flatemodule.cpp
@@ -336,7 +336,7 @@
 void PNG_PredictLine(pdfium::span<uint8_t> dest_span,
                      pdfium::span<const uint8_t> src_span,
                      pdfium::span<const uint8_t> last_span,
-                     uint32_t row_size,
+                     size_t row_size,
                      uint32_t bytes_per_pixel) {
   const uint8_t tag = src_span.front();
   pdfium::span<const uint8_t> remaining_src_span =
@@ -416,51 +416,11 @@
   pdfium::span<uint8_t> prev_dest_span;
   const uint32_t bytes_per_pixel = (Colors * BitsPerComponent + 7) / 8;
   for (size_t row = 0; row < row_count; row++) {
-    const uint8_t tag = remaining_src_span.front();
-    remaining_src_span = remaining_src_span.subspan(1);
     const size_t remaining_row_size =
-        std::min<size_t>(row_size, remaining_src_span.size());
-    switch (tag) {
-      case 1: {
-        for (uint32_t i = 0; i < remaining_row_size; ++i) {
-          uint8_t left = GetLeftValue(remaining_dest_span, i, bytes_per_pixel);
-          remaining_dest_span[i] = remaining_src_span[i] + left;
-        }
-        break;
-      }
-      case 2: {
-        for (uint32_t i = 0; i < remaining_row_size; ++i) {
-          uint8_t up = GetUpValue(prev_dest_span, i);
-          remaining_dest_span[i] = remaining_src_span[i] + up;
-        }
-        break;
-      }
-      case 3: {
-        for (uint32_t i = 0; i < remaining_row_size; ++i) {
-          uint8_t left = GetLeftValue(remaining_dest_span, i, bytes_per_pixel);
-          uint8_t up = GetUpValue(prev_dest_span, i);
-          remaining_dest_span[i] = remaining_src_span[i] + (up + left) / 2;
-        }
-        break;
-      }
-      case 4: {
-        for (uint32_t i = 0; i < remaining_row_size; ++i) {
-          uint8_t left = GetLeftValue(remaining_dest_span, i, bytes_per_pixel);
-          uint8_t up = GetUpValue(prev_dest_span, i);
-          uint8_t upper_left =
-              GetUpperLeftValue(prev_dest_span, i, bytes_per_pixel);
-          remaining_dest_span[i] =
-              remaining_src_span[i] + PathPredictor(left, up, upper_left);
-        }
-        break;
-      }
-      default: {
-        fxcrt::spancpy(remaining_dest_span,
-                       remaining_src_span.first(remaining_row_size));
-        break;
-      }
-    }
-    remaining_src_span = remaining_src_span.subspan(remaining_row_size);
+        std::min<size_t>(row_size, remaining_src_span.size() - 1);
+    PNG_PredictLine(remaining_dest_span, remaining_src_span, prev_dest_span,
+                    remaining_row_size, bytes_per_pixel);
+    remaining_src_span = remaining_src_span.subspan(remaining_row_size + 1);
     prev_dest_span = remaining_dest_span;
     remaining_dest_span = remaining_dest_span.subspan(remaining_row_size);
   }