Consolidate code into CPDF_Image::CreateXObjectImageDict().

Add a helper method for creating basic image dictionaries.

Change-Id: I16bffd7c100deadef4356ef70d65cd8a1160f652
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/65831
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 5fbb154..ce46538 100644
--- a/core/fpdfapi/page/cpdf_image.cpp
+++ b/core/fpdfapi/page/cpdf_image.cpp
@@ -94,11 +94,8 @@
     return nullptr;
   }
 
-  auto pDict = m_pDocument->New<CPDF_Dictionary>();
-  pDict->SetNewFor<CPDF_Name>("Type", "XObject");
-  pDict->SetNewFor<CPDF_Name>("Subtype", "Image");
-  pDict->SetNewFor<CPDF_Number>("Width", info.width);
-  pDict->SetNewFor<CPDF_Number>("Height", info.height);
+  RetainPtr<CPDF_Dictionary> pDict =
+      CreateXObjectImageDict(info.width, info.height);
   const char* csname = nullptr;
   if (info.num_components == 1) {
     csname = "DeviceGray";
@@ -173,12 +170,8 @@
   if (BitmapWidth < 1 || BitmapHeight < 1)
     return;
 
-  auto pDict = m_pDocument->New<CPDF_Dictionary>();
-  pDict->SetNewFor<CPDF_Name>("Type", "XObject");
-  pDict->SetNewFor<CPDF_Name>("Subtype", "Image");
-  pDict->SetNewFor<CPDF_Number>("Width", BitmapWidth);
-  pDict->SetNewFor<CPDF_Number>("Height", BitmapHeight);
-
+  RetainPtr<CPDF_Dictionary> pDict =
+      CreateXObjectImageDict(BitmapWidth, BitmapHeight);
   const int32_t bpp = pBitmap->GetBPP();
   size_t dest_pitch = 0;
   bool bCopyWithoutAlpha = true;
@@ -270,11 +263,8 @@
     int32_t maskHeight = pMaskBitmap->GetHeight();
     std::unique_ptr<uint8_t, FxFreeDeleter> mask_buf;
     int32_t mask_size = 0;
-    auto pMaskDict = m_pDocument->New<CPDF_Dictionary>();
-    pMaskDict->SetNewFor<CPDF_Name>("Type", "XObject");
-    pMaskDict->SetNewFor<CPDF_Name>("Subtype", "Image");
-    pMaskDict->SetNewFor<CPDF_Number>("Width", maskWidth);
-    pMaskDict->SetNewFor<CPDF_Number>("Height", maskHeight);
+    RetainPtr<CPDF_Dictionary> pMaskDict =
+        CreateXObjectImageDict(maskWidth, maskHeight);
     pMaskDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray");
     pMaskDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8);
     if (pMaskBitmap->GetFormat() != FXDIB_1bppMask) {
@@ -398,3 +388,13 @@
   }
   return false;
 }
+
+RetainPtr<CPDF_Dictionary> CPDF_Image::CreateXObjectImageDict(int width,
+                                                              int height) {
+  auto dict = m_pDocument->New<CPDF_Dictionary>();
+  dict->SetNewFor<CPDF_Name>("Type", "XObject");
+  dict->SetNewFor<CPDF_Name>("Subtype", "Image");
+  dict->SetNewFor<CPDF_Number>("Width", width);
+  dict->SetNewFor<CPDF_Number>("Height", height);
+  return dict;
+}
diff --git a/core/fpdfapi/page/cpdf_image.h b/core/fpdfapi/page/cpdf_image.h
index f8d0d00..4194d49 100644
--- a/core/fpdfapi/page/cpdf_image.h
+++ b/core/fpdfapi/page/cpdf_image.h
@@ -79,6 +79,8 @@
   void FinishInitialization(CPDF_Dictionary* pStreamDict);
   RetainPtr<CPDF_Dictionary> InitJPEG(pdfium::span<uint8_t> src_span);
 
+  RetainPtr<CPDF_Dictionary> CreateXObjectImageDict(int width, int height);
+
   int32_t m_Height = 0;
   int32_t m_Width = 0;
   bool m_bIsInline = false;