Create helper function and move a constant to a header file

Move the process of creating a palette buffer from a 1-bit-per-pixel
bitmap into helper CFX_ImageStretcher::BuildPaletteFrom1BppSource()
and move constant `kPaletteSize` into cfx_dibbase.h so that they can
be reused for Skia rendering process in the future.

Also in this helper function, adds DCHECKs for the two palette colors' alpha values (They are set to 255 during the palette construction).
This helps avoid the calculation of `a` within the for-loop.

Bug: pdfium:1810
Change-Id: Id5a4b94097002c5811a5cbb23ff450da25b50869
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98330
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Nigi <nigi@chromium.org>
diff --git a/core/fxge/dib/cfx_dibbase.cpp b/core/fxge/dib/cfx_dibbase.cpp
index e882d07..0d5ccc5 100644
--- a/core/fxge/dib/cfx_dibbase.cpp
+++ b/core/fxge/dib/cfx_dibbase.cpp
@@ -854,7 +854,6 @@
 }
 
 void CFX_DIBBase::SetPalette(pdfium::span<const uint32_t> src_palette) {
-  static const uint32_t kPaletteSize = 256;
   if (src_palette.empty() || GetBPP() > 8) {
     m_palette.clear();
     return;
diff --git a/core/fxge/dib/cfx_dibbase.h b/core/fxge/dib/cfx_dibbase.h
index 5ad1774..c23a985 100644
--- a/core/fxge/dib/cfx_dibbase.h
+++ b/core/fxge/dib/cfx_dibbase.h
@@ -30,6 +30,8 @@
   static constexpr FXDIB_Format kPlatformRGBFormat = FXDIB_Format::kRgb;
 #endif  // BUILDFLAG(IS_APPLE)
 
+  static constexpr uint32_t kPaletteSize = 256;
+
   ~CFX_DIBBase() override;
 
   virtual uint8_t* GetBuffer() const;
diff --git a/core/fxge/dib/cfx_imagestretcher.cpp b/core/fxge/dib/cfx_imagestretcher.cpp
index 4636d8f..94f7dcc 100644
--- a/core/fxge/dib/cfx_imagestretcher.cpp
+++ b/core/fxge/dib/cfx_imagestretcher.cpp
@@ -12,6 +12,8 @@
 #include "core/fxge/dib/cstretchengine.h"
 #include "core/fxge/dib/fx_dib.h"
 #include "third_party/base/check.h"
+#include "third_party/base/check_op.h"
+#include "third_party/base/span.h"
 
 namespace {
 
@@ -53,30 +55,43 @@
 
 CFX_ImageStretcher::~CFX_ImageStretcher() = default;
 
+// static
+void CFX_ImageStretcher::BuildPaletteFrom1BppSource(
+    const RetainPtr<const CFX_DIBBase>& source,
+    pdfium::span<FX_ARGB> palette_span) {
+  DCHECK_EQ(FXDIB_Format::k1bppRgb, source->GetFormat());
+  DCHECK(source->HasPalette());
+  DCHECK_EQ(CFX_DIBBase::kPaletteSize, palette_span.size());
+
+  int a0;
+  int r0;
+  int g0;
+  int b0;
+  std::tie(a0, r0, g0, b0) = ArgbDecode(source->GetPaletteArgb(0));
+  int a1;
+  int r1;
+  int g1;
+  int b1;
+  std::tie(a1, r1, g1, b1) = ArgbDecode(source->GetPaletteArgb(1));
+  DCHECK_EQ(255, a0);
+  DCHECK_EQ(255, a1);
+
+  for (int i = 0; i < static_cast<int>(CFX_DIBBase::kPaletteSize); ++i) {
+    int r = r0 + (r1 - r0) * i / 255;
+    int g = g0 + (g1 - g0) * i / 255;
+    int b = b0 + (b1 - b0) * i / 255;
+    palette_span[i] = ArgbEncode(255, r, g, b);
+  }
+}
+
 bool CFX_ImageStretcher::Start() {
   if (m_DestWidth == 0 || m_DestHeight == 0)
     return false;
 
   if (m_pSource->GetFormat() == FXDIB_Format::k1bppRgb &&
       m_pSource->HasPalette()) {
-    FX_ARGB pal[256];
-    int a0;
-    int r0;
-    int g0;
-    int b0;
-    std::tie(a0, r0, g0, b0) = ArgbDecode(m_pSource->GetPaletteArgb(0));
-    int a1;
-    int r1;
-    int g1;
-    int b1;
-    std::tie(a1, r1, g1, b1) = ArgbDecode(m_pSource->GetPaletteArgb(1));
-    for (int i = 0; i < 256; ++i) {
-      int a = a0 + (a1 - a0) * i / 255;
-      int r = r0 + (r1 - r0) * i / 255;
-      int g = g0 + (g1 - g0) * i / 255;
-      int b = b0 + (b1 - b0) * i / 255;
-      pal[i] = ArgbEncode(a, r, g, b);
-    }
+    FX_ARGB pal[CFX_DIBBase::kPaletteSize];
+    BuildPaletteFrom1BppSource(m_pSource, pal);
     if (!m_pDest->SetInfo(m_ClipRect.Width(), m_ClipRect.Height(), m_DestFormat,
                           pal)) {
       return false;
diff --git a/core/fxge/dib/cfx_imagestretcher.h b/core/fxge/dib/cfx_imagestretcher.h
index 8a43f7a..a6b2b7e 100644
--- a/core/fxge/dib/cfx_imagestretcher.h
+++ b/core/fxge/dib/cfx_imagestretcher.h
@@ -14,6 +14,7 @@
 #include "core/fxcrt/unowned_ptr.h"
 #include "core/fxge/dib/fx_dib.h"
 #include "core/fxge/dib/scanlinecomposer_iface.h"
+#include "third_party/base/span.h"
 
 class CFX_DIBBase;
 class CStretchEngine;
@@ -29,6 +30,17 @@
                      const FXDIB_ResampleOptions& options);
   ~CFX_ImageStretcher();
 
+  // Builds a new palette with a size of `CFX_DIBBase::kPaletteSize` from the
+  // existing palette in `source`. Note: The caller must make sure that the
+  // parameters meet the following conditions:
+  //   source       - The format must be `FXDIB_Format::k1bppRgb` and it must
+  //                  have a palette.
+  //   palette_span - The size must be `CFX_DIBBase::kPaletteSize` to be able
+  //                  to hold the new palette.
+  static void BuildPaletteFrom1BppSource(
+      const RetainPtr<const CFX_DIBBase>& source,
+      pdfium::span<FX_ARGB> palette_span);
+
   bool Start();
   bool Continue(PauseIndicatorIface* pPause);