Consolidate pre-multiply checks into a single function

Take a bunch of IsPremultiplied() checks and consolidate them into
ValidateBitmapPremultiplyState(). With a single function, it will be
easier to change the logic later.

Bug: 42271033
Change-Id: I8d853a96f3f9ce122e786b4012e29878f4deaef6
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/129010
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/fpdfsdk/cpdfsdk_helpers.cpp b/fpdfsdk/cpdfsdk_helpers.cpp
index eb74519..aff8aed 100644
--- a/fpdfsdk/cpdfsdk_helpers.cpp
+++ b/fpdfsdk/cpdfsdk_helpers.cpp
@@ -28,6 +28,7 @@
 #include "core/fxcrt/span_util.h"
 #include "core/fxcrt/stl_util.h"
 #include "core/fxcrt/unowned_ptr.h"
+#include "core/fxge/dib/cfx_dibitmap.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 
 namespace {
@@ -229,6 +230,10 @@
   }
 }
 
+void ValidateBitmapPremultiplyState(CFX_DIBitmap* bitmap) {
+  CHECK(!bitmap->IsPremultiplied());
+}
+
 CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle) {
   CPDFSDK_FormFillEnvironment* pFormFillEnv =
       CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
diff --git a/fpdfsdk/cpdfsdk_helpers.h b/fpdfsdk/cpdfsdk_helpers.h
index 5a9eab2..9621092 100644
--- a/fpdfsdk/cpdfsdk_helpers.h
+++ b/fpdfsdk/cpdfsdk_helpers.h
@@ -264,6 +264,10 @@
 
 FXDIB_Format FXDIBFormatFromFPDFFormat(int format);
 
+// CHECK() `bitmap` is not pre-multiplied, as PDFium does not take or hand out
+// pre-multiplied bitmaps to the embedder.
+void ValidateBitmapPremultiplyState(CFX_DIBitmap* bitmap);
+
 CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle);
 
 UNSAFE_BUFFER_USAGE ByteString
diff --git a/fpdfsdk/fpdf_editimg.cpp b/fpdfsdk/fpdf_editimg.cpp
index 656d0ed..a149e5e 100644
--- a/fpdfsdk/fpdf_editimg.cpp
+++ b/fpdfsdk/fpdf_editimg.cpp
@@ -178,7 +178,7 @@
   if (!holder) {
     return false;
   }
-  CHECK(!holder->IsPremultiplied());
+  ValidateBitmapPremultiplyState(holder);
 
   if (pages) {
     for (int index = 0; index < count; index++) {
@@ -280,7 +280,7 @@
   }
 
   CHECK(!pBitmap->HasPalette());
-  CHECK(!pBitmap->IsPremultiplied());
+  ValidateBitmapPremultiplyState(pBitmap);
 
   // Caller takes ownership.
   return FPDFBitmapFromCFXDIBitmap(pBitmap.Leak());
@@ -340,7 +340,7 @@
   if (!renderer.GetResult())
     return nullptr;
 
-  CHECK(!result_bitmap->IsPremultiplied());
+  ValidateBitmapPremultiplyState(result_bitmap);
 
   // Caller takes ownership.
   return FPDFBitmapFromCFXDIBitmap(result_bitmap.Leak());
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index 17c81dd..06b8520 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -828,7 +828,7 @@
   render_matrix *= scale_matrix;
   status.RenderSingleObject(text, render_matrix);
 
-  CHECK(!result_bitmap->IsPremultiplied());
+  ValidateBitmapPremultiplyState(result_bitmap);
 
   // Caller takes ownership.
   return FPDFBitmapFromCFXDIBitmap(result_bitmap.Leak());
diff --git a/fpdfsdk/fpdf_progressive.cpp b/fpdfsdk/fpdf_progressive.cpp
index 150b46a..2af73ce 100644
--- a/fpdfsdk/fpdf_progressive.cpp
+++ b/fpdfsdk/fpdf_progressive.cpp
@@ -62,7 +62,7 @@
   if (!pBitmap) {
     return FPDF_RENDER_FAILED;
   }
-  CHECK(!pBitmap->IsPremultiplied());
+  ValidateBitmapPremultiplyState(pBitmap);
 
   auto owned_context = std::make_unique<CPDF_PageRenderContext>();
   CPDF_PageRenderContext* context = owned_context.get();
diff --git a/fpdfsdk/fpdf_thumbnail.cpp b/fpdfsdk/fpdf_thumbnail.cpp
index 5e8f211..4fb7965 100644
--- a/fpdfsdk/fpdf_thumbnail.cpp
+++ b/fpdfsdk/fpdf_thumbnail.cpp
@@ -86,6 +86,6 @@
     return nullptr;
   }
 
-  CHECK(!thumb_bitmap->IsPremultiplied());
+  ValidateBitmapPremultiplyState(thumb_bitmap);
   return FPDFBitmapFromCFXDIBitmap(thumb_bitmap.Leak());
 }
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp
index ac960e8..316f205 100644
--- a/fpdfsdk/fpdf_view.cpp
+++ b/fpdfsdk/fpdf_view.cpp
@@ -710,7 +710,7 @@
   if (!pBitmap) {
     return;
   }
-  CHECK(!pBitmap->IsPremultiplied());
+  ValidateBitmapPremultiplyState(pBitmap);
 
   auto owned_context = std::make_unique<CPDF_PageRenderContext>();
   CPDF_PageRenderContext* context = owned_context.get();
@@ -747,7 +747,7 @@
   if (!pBitmap) {
     return;
   }
-  CHECK(!pBitmap->IsPremultiplied());
+  ValidateBitmapPremultiplyState(pBitmap);
 
   auto owned_context = std::make_unique<CPDF_PageRenderContext>();
   CPDF_PageRenderContext* context = owned_context.get();
@@ -893,7 +893,7 @@
     return nullptr;
   }
 
-  CHECK(!pBitmap->IsPremultiplied());
+  ValidateBitmapPremultiplyState(pBitmap);
 
   // Caller takes ownership.
   return FPDFBitmapFromCFXDIBitmap(pBitmap.Leak());
@@ -916,7 +916,7 @@
     return nullptr;
   }
 
-  CHECK(!pBitmap->IsPremultiplied());
+  ValidateBitmapPremultiplyState(pBitmap);
 
   // Caller takes ownership.
   return FPDFBitmapFromCFXDIBitmap(pBitmap.Leak());
@@ -953,7 +953,7 @@
   if (!pBitmap) {
     return false;
   }
-  CHECK(!pBitmap->IsPremultiplied());
+  ValidateBitmapPremultiplyState(pBitmap);
 
   FX_SAFE_INT32 right = left;
   right += width;