Change CFX_DIBBase::GetPaletteSize() to return size_t.

Also un-inline the function and make it easier to see what all the cases
are, instead of writing it as a 1-liner with 3 ternary operators.

Change-Id: Icc5d06f11bd6c1306c1aa4c104a218427809479f
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/65611
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp
index ec1e040..55090b3 100644
--- a/core/fpdfapi/page/cpdf_image.cpp
+++ b/core/fpdfapi/page/cpdf_image.cpp
@@ -227,16 +227,17 @@
     pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 1);
     dest_pitch = (BitmapWidth + 7) / 8;
   } else if (bpp == 8) {
-    int32_t iPalette = pBitmap->GetPaletteSize();
-    if (iPalette > 0) {
+    size_t palette_size = pBitmap->GetPaletteSize();
+    if (palette_size > 0) {
+      ASSERT(palette_size <= 256);
       CPDF_Array* pCS = m_pDocument->NewIndirect<CPDF_Array>();
       pCS->AddNew<CPDF_Name>("Indexed");
       pCS->AddNew<CPDF_Name>("DeviceRGB");
-      pCS->AddNew<CPDF_Number>(iPalette - 1);
+      pCS->AddNew<CPDF_Number>(static_cast<int>(palette_size - 1));
       std::unique_ptr<uint8_t, FxFreeDeleter> pColorTable(
-          FX_Alloc2D(uint8_t, iPalette, 3));
+          FX_Alloc2D(uint8_t, palette_size, 3));
       uint8_t* ptr = pColorTable.get();
-      for (int32_t i = 0; i < iPalette; i++) {
+      for (size_t i = 0; i < palette_size; i++) {
         uint32_t argb = pBitmap->GetPaletteArgb(i);
         ptr[0] = (uint8_t)(argb >> 16);
         ptr[1] = (uint8_t)(argb >> 8);
@@ -245,7 +246,7 @@
       }
       auto pNewDict = m_pDocument->New<CPDF_Dictionary>();
       CPDF_Stream* pCTS = m_pDocument->NewIndirect<CPDF_Stream>(
-          std::move(pColorTable), iPalette * 3, std::move(pNewDict));
+          std::move(pColorTable), palette_size * 3, std::move(pNewDict));
       pCS->AddNew<CPDF_Reference>(m_pDocument.Get(), pCTS->GetObjNum());
       pDict->SetNewFor<CPDF_Reference>("ColorSpace", m_pDocument.Get(),
                                        pCS->GetObjNum());
diff --git a/core/fpdfapi/render/cpdf_imagecacheentry.cpp b/core/fpdfapi/render/cpdf_imagecacheentry.cpp
index 79053a8..98896db 100644
--- a/core/fpdfapi/render/cpdf_imagecacheentry.cpp
+++ b/core/fpdfapi/render/cpdf_imagecacheentry.cpp
@@ -35,7 +35,7 @@
     const RetainPtr<CFX_DIBBase>& pDIB) {
   return pDIB && pDIB->GetBuffer()
              ? (uint32_t)pDIB->GetHeight() * pDIB->GetPitch() +
-                   (uint32_t)pDIB->GetPaletteSize() * 4
+                   pDIB->GetPaletteSize() * 4
              : 0;
 }
 
diff --git a/core/fxge/dib/cfx_dibbase.cpp b/core/fxge/dib/cfx_dibbase.cpp
index e3bbaa8..4a4223a 100644
--- a/core/fxge/dib/cfx_dibbase.cpp
+++ b/core/fxge/dib/cfx_dibbase.cpp
@@ -292,9 +292,9 @@
   ConvertBuffer_IndexCopy(dest_buf, dest_pitch, width, height, pSrcBitmap,
                           src_left, src_top);
   uint32_t* src_plt = pSrcBitmap->GetPalette();
-  int plt_size = pSrcBitmap->GetPaletteSize();
+  size_t plt_size = pSrcBitmap->GetPaletteSize();
   if (pSrcBitmap->IsCmykImage()) {
-    for (int i = 0; i < plt_size; ++i) {
+    for (size_t i = 0; i < plt_size; ++i) {
       uint8_t r;
       uint8_t g;
       uint8_t b;
@@ -807,6 +807,20 @@
   return true;
 }
 
+size_t CFX_DIBBase::GetPaletteSize() const {
+  if (IsAlphaMask())
+    return 0;
+
+  switch (m_bpp) {
+    case 1:
+      return 2;
+    case 8:
+      return 256;
+    default:
+      return 0;
+  }
+}
+
 uint32_t CFX_DIBBase::GetPaletteArgb(int index) const {
   ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask());
   if (m_pPalette)
diff --git a/core/fxge/dib/cfx_dibbase.h b/core/fxge/dib/cfx_dibbase.h
index cbde6f9..15d37b7 100644
--- a/core/fxge/dib/cfx_dibbase.h
+++ b/core/fxge/dib/cfx_dibbase.h
@@ -63,9 +63,7 @@
   bool IsCmykImage() const { return !!(m_AlphaFlag & 4); }
   bool IsOpaqueImage() const { return !IsAlphaMask() && !HasAlpha(); }
 
-  int GetPaletteSize() const {
-    return IsAlphaMask() ? 0 : (m_bpp == 1 ? 2 : (m_bpp == 8 ? 256 : 0));
-  }
+  size_t GetPaletteSize() const;
 
   uint32_t GetPaletteArgb(int index) const;
   void SetPaletteArgb(int index, uint32_t color);