Use better names for CFX_DIBBase methods that produce CFX_DIBitmaps.

Otherwise, it is not clear why we'd need cloned copies of the
CFX_DIBBase in several places (hint: they're not copies but a
generated CFX_DIBitmap alternate representation).

Fixed: pdfium:1732
Change-Id: I92b64271fbc29cf5fb89e85a0515abc1c54ad123
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/86550
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_imageobject.cpp b/core/fpdfapi/page/cpdf_imageobject.cpp
index 79dc912..44ca600 100644
--- a/core/fpdfapi/page/cpdf_imageobject.cpp
+++ b/core/fpdfapi/page/cpdf_imageobject.cpp
@@ -60,12 +60,13 @@
 RetainPtr<CFX_DIBitmap> CPDF_ImageObject::GetIndependentBitmap() const {
   RetainPtr<CFX_DIBBase> pSource = GetImage()->LoadDIBBase();
 
-  // Clone() is non-virtual, and can't be overloaded by CPDF_DIB to
-  // return a clone of the subclass as one would typically expect from a
-  // such a method. Instead, it only clones the CFX_DIBBase, none of whose
-  // members point to objects owned by |this| or the form containing |this|.
-  // As a result, the clone may outlive them.
-  return pSource ? pSource->Clone(nullptr) : nullptr;
+  // Realize() is non-virtual, and can't be overloaded by CPDF_DIB to
+  // return a full-up CPDF_DIB subclass. Instead, it only works upon the
+  // CFX_DIBBase, which is convenient since none of its members point to
+  // objects owned by |this| or the form containing |this|. As a result,
+  // the new bitmap may outlive them, giving the "independent" property
+  // this method is named after.
+  return pSource ? pSource->Realize() : nullptr;
 }
 
 void CPDF_ImageObject::SetImageMatrix(const CFX_Matrix& matrix) {
diff --git a/core/fpdfapi/render/cpdf_imagecacheentry.cpp b/core/fpdfapi/render/cpdf_imagecacheentry.cpp
index 840505c..6d95348 100644
--- a/core/fpdfapi/render/cpdf_imagecacheentry.cpp
+++ b/core/fpdfapi/render/cpdf_imagecacheentry.cpp
@@ -85,13 +85,13 @@
   CPDF_PageRenderCache* pPageRenderCache = pContext->GetPageCache();
   m_dwTimeCount = pPageRenderCache->GetTimeCount();
   if (m_pCurBitmap->GetPitch() * m_pCurBitmap->GetHeight() < kHugeImageSize) {
-    m_pCachedBitmap = m_pCurBitmap->Clone(nullptr);
+    m_pCachedBitmap = m_pCurBitmap->Realize();
     m_pCurBitmap.Reset();
   } else {
     m_pCachedBitmap = m_pCurBitmap;
   }
   if (m_pCurMask) {
-    m_pCachedMask = m_pCurMask->Clone(nullptr);
+    m_pCachedMask = m_pCurMask->Realize();
     m_pCurMask.Reset();
   }
   m_pCurBitmap = m_pCachedBitmap;
diff --git a/core/fpdfapi/render/cpdf_imageloader.cpp b/core/fpdfapi/render/cpdf_imageloader.cpp
index f409c93..d55d4e3 100644
--- a/core/fpdfapi/render/cpdf_imageloader.cpp
+++ b/core/fpdfapi/render/cpdf_imageloader.cpp
@@ -56,7 +56,7 @@
 
   m_pBitmap = pTransferFunc->TranslateImage(m_pBitmap);
   if (m_bCached && m_pMask)
-    m_pMask = m_pMask->Clone(nullptr);
+    m_pMask = m_pMask->Realize();
   m_bCached = false;
   return m_pBitmap;
 }
diff --git a/core/fpdfapi/render/cpdf_imagerenderer.cpp b/core/fpdfapi/render/cpdf_imagerenderer.cpp
index d2aa2c7..b0a4b6e 100644
--- a/core/fpdfapi/render/cpdf_imagerenderer.cpp
+++ b/core/fpdfapi/render/cpdf_imagerenderer.cpp
@@ -103,7 +103,7 @@
     }
     m_FillArgb = m_pRenderStatus->GetFillArgb(m_pImageObject.Get());
   } else if (GetRenderOptions().ColorModeIs(CPDF_RenderOptions::kGray)) {
-    RetainPtr<CFX_DIBitmap> pClone = m_pDIBBase->Clone(nullptr);
+    RetainPtr<CFX_DIBitmap> pClone = m_pDIBBase->Realize();
     if (!pClone)
       return false;
 
@@ -392,7 +392,7 @@
     }
   }
 #if defined(_SKIA_SUPPORT_)
-  RetainPtr<CFX_DIBitmap> premultiplied = m_pDIBBase->Clone(nullptr);
+  RetainPtr<CFX_DIBitmap> premultiplied = m_pDIBBase->Realize();
   if (m_pDIBBase->IsAlphaFormat())
     CFX_SkiaDeviceDriver::PreMultiply(premultiplied);
   if (m_pRenderStatus->GetRenderDevice()->StartDIBitsWithBlend(
diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp
index d3d286d..5ec9419 100644
--- a/core/fpdfapi/render/cpdf_renderstatus.cpp
+++ b/core/fpdfapi/render/cpdf_renderstatus.cpp
@@ -1310,7 +1310,7 @@
     rect.Intersect(m_pDevice->GetClipBox());
     RetainPtr<CFX_DIBitmap> pClone;
     if (m_pDevice->GetBackDrop() && m_pDevice->GetBitmap()) {
-      pClone = m_pDevice->GetBackDrop()->Clone(&rect);
+      pClone = m_pDevice->GetBackDrop()->ClipTo(rect);
       if (!pClone)
         return;
 
diff --git a/core/fxcodec/progressive_decoder.cpp b/core/fxcodec/progressive_decoder.cpp
index d1db0f3..416372f 100644
--- a/core/fxcodec/progressive_decoder.cpp
+++ b/core/fxcodec/progressive_decoder.cpp
@@ -1300,7 +1300,7 @@
       (m_clipBox.left == 0 && m_clipBox.top == 0 &&
        m_clipBox.right == m_SrcWidth && m_clipBox.bottom == m_SrcHeight)
           ? pDIBitmap
-          : pDIBitmap->Clone(&m_clipBox);
+          : pDIBitmap->ClipTo(m_clipBox);
   if (!pClipBitmap) {
     m_pDeviceBitmap = nullptr;
     m_pFile = nullptr;
diff --git a/core/fxge/agg/fx_agg_driver.cpp b/core/fxge/agg/fx_agg_driver.cpp
index 11c49bb..b850aaa 100644
--- a/core/fxge/agg/fx_agg_driver.cpp
+++ b/core/fxge/agg/fx_agg_driver.cpp
@@ -1321,14 +1321,14 @@
                top + pBitmap->GetHeight());
   RetainPtr<CFX_DIBitmap> pBack;
   if (m_pBackdropBitmap) {
-    pBack = m_pBackdropBitmap->Clone(&rect);
+    pBack = m_pBackdropBitmap->ClipTo(rect);
     if (!pBack)
       return true;
 
     pBack->CompositeBitmap(0, 0, pBack->GetWidth(), pBack->GetHeight(),
                            m_pBitmap, 0, 0, BlendMode::kNormal, nullptr, false);
   } else {
-    pBack = m_pBitmap->Clone(&rect);
+    pBack = m_pBitmap->ClipTo(rect);
     if (!pBack)
       return true;
   }
diff --git a/core/fxge/dib/cfx_dibbase.cpp b/core/fxge/dib/cfx_dibbase.cpp
index bbffc47..6842362 100644
--- a/core/fxge/dib/cfx_dibbase.cpp
+++ b/core/fxge/dib/cfx_dibbase.cpp
@@ -618,7 +618,16 @@
   return GetRequiredPaletteSize() * sizeof(uint32_t);
 }
 
-RetainPtr<CFX_DIBitmap> CFX_DIBBase::Clone(const FX_RECT* pClip) const {
+RetainPtr<CFX_DIBitmap> CFX_DIBBase::Realize() const {
+  return ClipToInternal(nullptr);
+}
+
+RetainPtr<CFX_DIBitmap> CFX_DIBBase::ClipTo(const FX_RECT& rect) const {
+  return ClipToInternal(&rect);
+}
+
+RetainPtr<CFX_DIBitmap> CFX_DIBBase::ClipToInternal(
+    const FX_RECT* pClip) const {
   FX_RECT rect(0, 0, m_Width, m_Height);
   if (pClip) {
     rect.Intersect(*pClip);
@@ -999,10 +1008,9 @@
   return pFlipped;
 }
 
-RetainPtr<CFX_DIBitmap> CFX_DIBBase::CloneConvert(
-    FXDIB_Format dest_format) const {
+RetainPtr<CFX_DIBitmap> CFX_DIBBase::ConvertTo(FXDIB_Format dest_format) const {
   if (dest_format == GetFormat())
-    return Clone(nullptr);
+    return Realize();
 
   auto pClone = pdfium::MakeRetain<CFX_DIBitmap>();
   if (!pClone->Create(m_Width, m_Height, dest_format))
@@ -1158,7 +1166,7 @@
     return nullptr;
 
   if (dest_width == m_Width && dest_height == m_Height)
-    return Clone(&clip_rect);
+    return ClipTo(clip_rect);
 
   CFX_BitmapStorer storer;
   CFX_ImageStretcher stretcher(&storer, holder, dest_width, dest_height,
diff --git a/core/fxge/dib/cfx_dibbase.h b/core/fxge/dib/cfx_dibbase.h
index bfe2762..0473b52 100644
--- a/core/fxge/dib/cfx_dibbase.h
+++ b/core/fxge/dib/cfx_dibbase.h
@@ -60,8 +60,9 @@
   // Copies into internally-owned palette.
   void SetPalette(pdfium::span<const uint32_t> src_palette);
 
-  RetainPtr<CFX_DIBitmap> Clone(const FX_RECT* pClip) const;
-  RetainPtr<CFX_DIBitmap> CloneConvert(FXDIB_Format format) const;
+  RetainPtr<CFX_DIBitmap> Realize() const;
+  RetainPtr<CFX_DIBitmap> ClipTo(const FX_RECT& clip) const;
+  RetainPtr<CFX_DIBitmap> ConvertTo(FXDIB_Format format) const;
   RetainPtr<CFX_DIBitmap> StretchTo(int dest_width,
                                     int dest_height,
                                     const FXDIB_ResampleOptions& options,
@@ -112,6 +113,7 @@
       int src_top,
       std::vector<uint32_t, FxAllocAllocator<uint32_t>>* pal);
 
+  RetainPtr<CFX_DIBitmap> ClipToInternal(const FX_RECT* pClip) const;
   void BuildPalette();
   bool BuildAlphaMask();
   int FindPalette(uint32_t color) const;
diff --git a/core/fxge/dib/cfx_dibextractor.cpp b/core/fxge/dib/cfx_dibextractor.cpp
index ce8ca58..68488ba 100644
--- a/core/fxge/dib/cfx_dibextractor.cpp
+++ b/core/fxge/dib/cfx_dibextractor.cpp
@@ -11,7 +11,7 @@
 
 CFX_DIBExtractor::CFX_DIBExtractor(const RetainPtr<CFX_DIBBase>& pSrc) {
   if (!pSrc->GetBuffer()) {
-    m_pBitmap = pSrc->Clone(nullptr);
+    m_pBitmap = pSrc->Realize();
     return;
   }
   RetainPtr<CFX_DIBBase> pOldSrc(pSrc);
diff --git a/core/fxge/dib/cfx_dibitmap.cpp b/core/fxge/dib/cfx_dibitmap.cpp
index 29419e5..bb89d79 100644
--- a/core/fxge/dib/cfx_dibitmap.cpp
+++ b/core/fxge/dib/cfx_dibitmap.cpp
@@ -301,7 +301,7 @@
     return false;
 
   if (pSrcBitmap->GetBPP() == 1) {
-    pSrcClone = pSrcBitmap->CloneConvert(FXDIB_Format::k8bppMask);
+    pSrcClone = pSrcBitmap->ConvertTo(FXDIB_Format::k8bppMask);
     if (!pSrcClone)
       return false;
   }
diff --git a/core/fxge/skia/fx_skia_device.cpp b/core/fxge/skia/fx_skia_device.cpp
index d270a56..ce8f61d 100644
--- a/core/fxge/skia/fx_skia_device.cpp
+++ b/core/fxge/skia/fx_skia_device.cpp
@@ -2407,14 +2407,14 @@
                top + pBitmap->GetHeight());
   RetainPtr<CFX_DIBitmap> pBack;
   if (m_pBackdropBitmap) {
-    pBack = m_pBackdropBitmap->Clone(&rect);
+    pBack = m_pBackdropBitmap->ClipTo(rect);
     if (!pBack)
       return true;
 
     pBack->CompositeBitmap(0, 0, pBack->GetWidth(), pBack->GetHeight(),
                            m_pBitmap, 0, 0, BlendMode::kNormal, nullptr, false);
   } else {
-    pBack = m_pBitmap->Clone(&rect);
+    pBack = m_pBitmap->ClipTo(rect);
     if (!pBack)
       return true;
   }
diff --git a/core/fxge/win32/cfx_psrenderer.cpp b/core/fxge/win32/cfx_psrenderer.cpp
index b358cab..69d2add 100644
--- a/core/fxge/win32/cfx_psrenderer.cpp
+++ b/core/fxge/win32/cfx_psrenderer.cpp
@@ -545,11 +545,11 @@
     switch (pSource->GetFormat()) {
       case FXDIB_Format::k1bppRgb:
       case FXDIB_Format::kRgb32:
-        pConverted = pConverted->CloneConvert(FXDIB_Format::kRgb);
+        pConverted = pConverted->ConvertTo(FXDIB_Format::kRgb);
         break;
       case FXDIB_Format::k8bppRgb:
         if (pSource->HasPalette())
-          pConverted = pConverted->CloneConvert(FXDIB_Format::kRgb);
+          pConverted = pConverted->ConvertTo(FXDIB_Format::kRgb);
         break;
       default:
         break;
diff --git a/core/fxge/win32/cgdi_plus_ext.cpp b/core/fxge/win32/cgdi_plus_ext.cpp
index 693f1f2..1618dfb 100644
--- a/core/fxge/win32/cgdi_plus_ext.cpp
+++ b/core/fxge/win32/cgdi_plus_ext.cpp
@@ -203,25 +203,26 @@
 
 void OutputImage(Gdiplus::GpGraphics* pGraphics,
                  const RetainPtr<CFX_DIBitmap>& pBitmap,
-                 const FX_RECT* pSrcRect,
+                 const FX_RECT& src_rect,
                  int dest_left,
                  int dest_top,
                  int dest_width,
                  int dest_height) {
-  int src_width = pSrcRect->Width(), src_height = pSrcRect->Height();
+  int src_width = src_rect.Width();
+  int src_height = src_rect.Height();
   const CGdiplusExt& GdiplusExt = GetGdiplusExt();
-  if (pBitmap->GetBPP() == 1 && (pSrcRect->left % 8)) {
+  if (pBitmap->GetBPP() == 1 && (src_rect.left % 8)) {
     FX_RECT new_rect(0, 0, src_width, src_height);
-    RetainPtr<CFX_DIBitmap> pCloned = pBitmap->Clone(pSrcRect);
+    RetainPtr<CFX_DIBitmap> pCloned = pBitmap->ClipTo(src_rect);
     if (!pCloned)
       return;
-    OutputImage(pGraphics, pCloned, &new_rect, dest_left, dest_top, dest_width,
+    OutputImage(pGraphics, pCloned, new_rect, dest_left, dest_top, dest_width,
                 dest_height);
     return;
   }
   int src_pitch = pBitmap->GetPitch();
-  uint8_t* scan0 = pBitmap->GetBuffer() + pSrcRect->top * src_pitch +
-                   pBitmap->GetBPP() * pSrcRect->left / 8;
+  uint8_t* scan0 = pBitmap->GetBuffer() + src_rect.top * src_pitch +
+                   pBitmap->GetBPP() * src_rect.left / 8;
   Gdiplus::GpBitmap* bitmap = nullptr;
   switch (pBitmap->GetFormat()) {
     case FXDIB_Format::kArgb:
@@ -589,7 +590,7 @@
                                        Gdiplus::InterpolationModeBilinear);
   }
   FX_RECT src_rect(0, 0, pBitmap->GetWidth(), pBitmap->GetHeight());
-  OutputImage(pGraphics, pBitmap, &src_rect, dest_left, dest_top, dest_width,
+  OutputImage(pGraphics, pBitmap, src_rect, dest_left, dest_top, dest_width,
               dest_height);
   CallFunc(GdipDeleteGraphics)(pGraphics);
   CallFunc(GdipDeleteGraphics)(pGraphics);
diff --git a/fpdfsdk/fpdf_editimg.cpp b/fpdfsdk/fpdf_editimg.cpp
index 90c48cc..fae6b4c 100644
--- a/fpdfsdk/fpdf_editimg.cpp
+++ b/fpdfsdk/fpdf_editimg.cpp
@@ -194,15 +194,13 @@
   if (!pSource)
     return nullptr;
 
-  RetainPtr<CFX_DIBitmap> pBitmap;
   // If the source image has a representation of 1 bit per pixel, then convert
   // it to a grayscale bitmap having 1 byte per pixel, since bitmaps have no
   // concept of bits. Otherwise, convert the source image to a bitmap directly,
   // retaining its color representation.
-  if (pSource->GetBPP() == 1)
-    pBitmap = pSource->CloneConvert(FXDIB_Format::k8bppRgb);
-  else
-    pBitmap = pSource->Clone(nullptr);
+  RetainPtr<CFX_DIBitmap> pBitmap =
+      pSource->GetBPP() == 1 ? pSource->ConvertTo(FXDIB_Format::k8bppRgb)
+                             : pSource->Realize();
 
   return FPDFBitmapFromCFXDIBitmap(pBitmap.Leak());
 }
diff --git a/xfa/fxfa/cxfa_imagerenderer.cpp b/xfa/fxfa/cxfa_imagerenderer.cpp
index f280f8d..1ec506c 100644
--- a/xfa/fxfa/cxfa_imagerenderer.cpp
+++ b/xfa/fxfa/cxfa_imagerenderer.cpp
@@ -42,7 +42,7 @@
     if (m_pDIBBase->IsAlphaFormat() &&
         !(m_pDevice->GetRenderCaps() & FXRC_ALPHA_IMAGE) &&
         !(m_pDevice->GetRenderCaps() & FXRC_GET_BITS)) {
-      m_pCloneConvert = m_pDIBBase->CloneConvert(FXDIB_Format::kRgb);
+      m_pCloneConvert = m_pDIBBase->ConvertTo(FXDIB_Format::kRgb);
       if (!m_pCloneConvert)
         return false;
 
@@ -144,12 +144,11 @@
     return;
   }
 
-  RetainPtr<CFX_DIBitmap> pCloneConvert =
-      pDIBitmap->CloneConvert(FXDIB_Format::kRgb);
-  if (!pCloneConvert)
+  RetainPtr<CFX_DIBitmap> pConverted = pDIBitmap->ConvertTo(FXDIB_Format::kRgb);
+  if (!pConverted)
     return;
 
-  CXFA_ImageRenderer imageRender(m_pDevice.Get(), pCloneConvert, m_ImageMatrix);
+  CXFA_ImageRenderer imageRender(m_pDevice.Get(), pConverted, m_ImageMatrix);
   if (!imageRender.Start())
     return;