Introduce CPDF_Image::CreateNewDIB() helper method

Consolidate some code inside CPDF_Image class.

Change-Id: I7e4a456da0a9813bda8a415290bb74546eaa2242
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/96310
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/fpdf_editimg.cpp b/fpdfsdk/fpdf_editimg.cpp
index 0ead01d..5a40c7b 100644
--- a/fpdfsdk/fpdf_editimg.cpp
+++ b/fpdfsdk/fpdf_editimg.cpp
@@ -375,8 +375,11 @@
   if (!pPage || !pPage->GetDocument() || !pImg->GetStream())
     return true;
 
-  auto pSource =
-      pdfium::MakeRetain<CPDF_DIB>(pPage->GetDocument(), pImg->GetStream());
+  // A cross-document image may have come from the embedder.
+  if (pPage->GetDocument() != pImg->GetDocument())
+    return false;
+
+  RetainPtr<CPDF_DIB> pSource = pImg->CreateNewDIB();
   CPDF_DIB::LoadState ret = pSource->StartLoadDIBBase(
       false, nullptr, pPage->GetPageResources(), false,
       CPDF_ColorSpace::Family::kUnknown, false);