Remove unreachable code in cgdi_plus_ext.cpp

The only caller to CGdiplusExt::StretchDIBits() always passes in a
bitmap with an alpha channel. Add a CHECK() for that, and then remove
all the code that tried to handle other bitmap formats. Also:

- Remove a redundant GdipDeleteGraphics call.
- Remove support code for calling GdipSetImagePalette(), now that there
  are no callers.

Change-Id: I3d863eb06f0b9f055865b4a2ecd8355fcf46b065
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/122197
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@google.com>
diff --git a/core/fxge/win32/cgdi_plus_ext.cpp b/core/fxge/win32/cgdi_plus_ext.cpp
index ad93217..edfda9f 100644
--- a/core/fxge/win32/cgdi_plus_ext.cpp
+++ b/core/fxge/win32/cgdi_plus_ext.cpp
@@ -16,12 +16,12 @@
 #include <utility>
 #include <vector>
 
+#include "core/fxcrt/check_op.h"
 #include "core/fxcrt/fx_memcpy_wrappers.h"
 #include "core/fxcrt/fx_memory.h"
 #include "core/fxcrt/fx_string.h"
 #include "core/fxcrt/fx_string_wrappers.h"
 #include "core/fxcrt/fx_system.h"
-#include "core/fxcrt/notreached.h"
 #include "core/fxcrt/numerics/safe_conversions.h"
 #include "core/fxcrt/span.h"
 #include "core/fxge/cfx_fillrenderoptions.h"
@@ -60,7 +60,6 @@
   FuncId_GdipDeleteGraphics,
   FuncId_GdipDisposeImage,
   FuncId_GdipCreateBitmapFromScan0,
-  FuncId_GdipSetImagePalette,
   FuncId_GdipSetInterpolationMode,
   FuncId_GdipDrawImagePointsI,
   FuncId_GdiplusStartup,
@@ -98,7 +97,6 @@
     "GdipDeleteGraphics",
     "GdipDisposeImage",
     "GdipCreateBitmapFromScan0",
-    "GdipSetImagePalette",
     "GdipSetInterpolationMode",
     "GdipDrawImagePointsI",
     "GdiplusStartup",
@@ -150,8 +148,6 @@
     decltype(&Gdiplus::DllExports::GdipDisposeImage);
 using FuncType_GdipCreateBitmapFromScan0 =
     decltype(&Gdiplus::DllExports::GdipCreateBitmapFromScan0);
-using FuncType_GdipSetImagePalette =
-    decltype(&Gdiplus::DllExports::GdipSetImagePalette);
 using FuncType_GdipSetInterpolationMode =
     decltype(&Gdiplus::DllExports::GdipSetInterpolationMode);
 using FuncType_GdipDrawImagePointsI =
@@ -212,19 +208,10 @@
                  int dest_top,
                  int dest_width,
                  int dest_height) {
+  CHECK_EQ(FXDIB_Format::kArgb, source->GetFormat());
   int src_width = src_rect.Width();
   int src_height = src_rect.Height();
   const CGdiplusExt& GdiplusExt = GetGdiplusExt();
-  if (source->GetBPP() == 1 && (src_rect.left % 8)) {
-    FX_RECT new_rect(0, 0, src_width, src_height);
-    source = source->ClipTo(src_rect);
-    if (!source) {
-      return;
-    }
-    OutputImage(pGraphics, std::move(source), new_rect, dest_left, dest_top,
-                dest_width, dest_height);
-    return;
-  }
 
   RetainPtr<const CFX_DIBitmap> realized_source = source->RealizeIfNeeded();
   if (!realized_source) {
@@ -242,43 +229,8 @@
                    realized_source->GetBPP() * src_rect.left / 8)
           .data());
   Gdiplus::GpBitmap* bitmap = nullptr;
-  switch (source->GetFormat()) {
-    case FXDIB_Format::kArgb:
-      CallFunc(GdipCreateBitmapFromScan0)(src_width, src_height, src_pitch,
-                                          PixelFormat32bppARGB, scan0, &bitmap);
-      break;
-    case FXDIB_Format::kRgb32:
-      CallFunc(GdipCreateBitmapFromScan0)(src_width, src_height, src_pitch,
-                                          PixelFormat32bppRGB, scan0, &bitmap);
-      break;
-    case FXDIB_Format::kRgb:
-      CallFunc(GdipCreateBitmapFromScan0)(src_width, src_height, src_pitch,
-                                          PixelFormat24bppRGB, scan0, &bitmap);
-      break;
-    case FXDIB_Format::k8bppRgb: {
-      CallFunc(GdipCreateBitmapFromScan0)(src_width, src_height, src_pitch,
-                                          PixelFormat8bppIndexed, scan0,
-                                          &bitmap);
-      std::array<UINT, 258> pal;
-      pal[0] = 0;
-      pal[1] = 256;
-      for (int i = 0; i < 256; i++) {
-        pal[i + 2] = realized_source->GetPaletteArgb(i);
-      }
-      CallFunc(GdipSetImagePalette)(bitmap, (Gdiplus::ColorPalette*)pal.data());
-      break;
-    }
-    case FXDIB_Format::k1bppRgb: {
-      CallFunc(GdipCreateBitmapFromScan0)(src_width, src_height, src_pitch,
-                                          PixelFormat1bppIndexed, scan0,
-                                          &bitmap);
-      break;
-    }
-    case FXDIB_Format::kInvalid:
-    case FXDIB_Format::k1bppMask:
-    case FXDIB_Format::k8bppMask:
-      NOTREACHED_NORETURN();
-  }
+  CallFunc(GdipCreateBitmapFromScan0)(src_width, src_height, src_pitch,
+                                      PixelFormat32bppARGB, scan0, &bitmap);
   if (dest_height < 0) {
     dest_height--;
   }
@@ -604,6 +556,7 @@
                                 int dest_height,
                                 const FX_RECT* pClipRect,
                                 const FXDIB_ResampleOptions& options) {
+  CHECK(source->IsAlphaFormat());
   Gdiplus::GpGraphics* pGraphics;
   const CGdiplusExt& GdiplusExt = GetGdiplusExt();
   CallFunc(GdipCreateFromHDC)(hDC, &pGraphics);
@@ -623,7 +576,6 @@
   OutputImage(pGraphics, std::move(source), src_rect, dest_left, dest_top,
               dest_width, dest_height);
   CallFunc(GdipDeleteGraphics)(pGraphics);
-  CallFunc(GdipDeleteGraphics)(pGraphics);
   return true;
 }