Make CFX_DIBBase::IsPremultiplied() available in non-Skia builds
Make it available so //fpdfsdk code can more easily check that
FPDF_BITMAPs that are passed in and being returned are not
pre-multiplied. This is important to ensure right now, as PDFium does
not have a public bitmap type that corresponds to
`FXDIB_Format::kArgbPremul` yet, and upcoming CLs will try to put that
format to use.
Bug: 42271020
Change-Id: Ife31dea4d31d3fbd64f743e8a82b95ef611839de
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/122933
Reviewed-by: Tom Sepez <tsepez@google.com>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxge/dib/cfx_dibbase.cpp b/core/fxge/dib/cfx_dibbase.cpp
index e5a677c..ddf4df8 100644
--- a/core/fxge/dib/cfx_dibbase.cpp
+++ b/core/fxge/dib/cfx_dibbase.cpp
@@ -1044,3 +1044,7 @@
#endif
}
}
+
+bool CFX_DIBBase::IsPremultiplied() const {
+ return false;
+}
diff --git a/core/fxge/dib/cfx_dibbase.h b/core/fxge/dib/cfx_dibbase.h
index aecc9db..6e70dd5 100644
--- a/core/fxge/dib/cfx_dibbase.h
+++ b/core/fxge/dib/cfx_dibbase.h
@@ -107,15 +107,15 @@
int& src_top,
const CFX_ClipRgn* pClipRgn) const;
+ // Whether alpha is premultiplied (if `IsAlphaFormat()`).
+ virtual bool IsPremultiplied() const;
+
#if defined(PDF_USE_SKIA)
// Realizes an `SkImage` from this DIB.
//
// This may share the underlying pixels, in which case, this DIB should not be
// modified during the lifetime of the `SkImage`.
virtual sk_sp<SkImage> RealizeSkImage() const;
-
- // Whether alpha is premultiplied (if `IsAlphaFormat()`).
- virtual bool IsPremultiplied() const;
#endif // defined(PDF_USE_SKIA)
protected:
diff --git a/core/fxge/skia/cfx_dibbase_skia.cpp b/core/fxge/skia/cfx_dibbase_skia.cpp
index eda97a1..7d122c2 100644
--- a/core/fxge/skia/cfx_dibbase_skia.cpp
+++ b/core/fxge/skia/cfx_dibbase_skia.cpp
@@ -266,7 +266,3 @@
NOTREACHED_NORETURN();
}
}
-
-bool CFX_DIBBase::IsPremultiplied() const {
- return false;
-}
diff --git a/fpdfsdk/fpdf_editimg.cpp b/fpdfsdk/fpdf_editimg.cpp
index f0d93eb..f29bc4a 100644
--- a/fpdfsdk/fpdf_editimg.cpp
+++ b/fpdfsdk/fpdf_editimg.cpp
@@ -176,6 +176,7 @@
if (!holder) {
return false;
}
+ CHECK(!holder->IsPremultiplied());
if (pages) {
for (int index = 0; index < count; index++) {
@@ -272,10 +273,14 @@
pBitmap = pSource->ConvertTo(FXDIB_Format::kRgb);
break;
}
- if (pBitmap) {
- CHECK(!pBitmap->HasPalette());
+ if (!pBitmap) {
+ return nullptr;
}
+ CHECK(!pBitmap->HasPalette());
+ CHECK(!pBitmap->IsPremultiplied());
+
+ // Caller takes ownership.
return FPDFBitmapFromCFXDIBitmap(pBitmap.Leak());
}
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index 4c6ce82..355d3aa 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -758,6 +758,8 @@
if (rect.IsEmpty())
return nullptr;
+ // TODO(crbug.com/42271020): Consider adding support for
+ // `FXDIB_Format::kArgbPremul`
auto result_bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
if (!result_bitmap->Create(rect.Width(), rect.Height(), FXDIB_Format::kArgb))
return nullptr;
@@ -789,6 +791,8 @@
render_matrix *= scale_matrix;
status.RenderSingleObject(text, render_matrix);
+ CHECK(!result_bitmap->IsPremultiplied());
+
// Caller takes ownership.
return FPDFBitmapFromCFXDIBitmap(result_bitmap.Leak());
}
diff --git a/fpdfsdk/fpdf_formfill.cpp b/fpdfsdk/fpdf_formfill.cpp
index 9b8ee18..19a79ba 100644
--- a/fpdfsdk/fpdf_formfill.cpp
+++ b/fpdfsdk/fpdf_formfill.cpp
@@ -211,6 +211,7 @@
if (dest_is_bitmap) {
holder.Reset(absl::get<CFX_DIBitmap*>(dest));
CHECK(holder);
+ CHECK(!holder->IsPremultiplied());
} else {
#if defined(PDF_USE_SKIA)
if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
diff --git a/fpdfsdk/fpdf_progressive.cpp b/fpdfsdk/fpdf_progressive.cpp
index 9c446c5..455d42d 100644
--- a/fpdfsdk/fpdf_progressive.cpp
+++ b/fpdfsdk/fpdf_progressive.cpp
@@ -62,6 +62,7 @@
if (!pBitmap) {
return FPDF_RENDER_FAILED;
}
+ CHECK(!pBitmap->IsPremultiplied());
auto owned_context = std::make_unique<CPDF_PageRenderContext>();
CPDF_PageRenderContext* context = owned_context.get();
diff --git a/fpdfsdk/fpdf_thumbnail.cpp b/fpdfsdk/fpdf_thumbnail.cpp
index 2bd886d..5e8f211 100644
--- a/fpdfsdk/fpdf_thumbnail.cpp
+++ b/fpdfsdk/fpdf_thumbnail.cpp
@@ -86,5 +86,6 @@
return nullptr;
}
+ CHECK(!thumb_bitmap->IsPremultiplied());
return FPDFBitmapFromCFXDIBitmap(thumb_bitmap.Leak());
}
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp
index 4943746..2c9c79c 100644
--- a/fpdfsdk/fpdf_view.cpp
+++ b/fpdfsdk/fpdf_view.cpp
@@ -712,6 +712,7 @@
if (!pBitmap) {
return;
}
+ CHECK(!pBitmap->IsPremultiplied());
auto owned_context = std::make_unique<CPDF_PageRenderContext>();
CPDF_PageRenderContext* context = owned_context.get();
@@ -749,6 +750,7 @@
if (!pBitmap) {
return;
}
+ CHECK(!pBitmap->IsPremultiplied());
auto owned_context = std::make_unique<CPDF_PageRenderContext>();
CPDF_PageRenderContext* context = owned_context.get();
@@ -889,6 +891,10 @@
alpha ? FXDIB_Format::kArgb : FXDIB_Format::kRgb32)) {
return nullptr;
}
+
+ CHECK(!pBitmap->IsPremultiplied());
+
+ // Caller takes ownership.
return FPDFBitmapFromCFXDIBitmap(pBitmap.Leak());
}
@@ -918,9 +924,13 @@
// Ensure external memory is good at least for the duration of this call.
UnownedPtr<uint8_t> pChecker(static_cast<uint8_t*>(first_scan));
auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
- if (!pBitmap->Create(width, height, fx_format, pChecker, stride))
+ if (!pBitmap->Create(width, height, fx_format, pChecker, stride)) {
return nullptr;
+ }
+ CHECK(!pBitmap->IsPremultiplied());
+
+ // Caller takes ownership.
return FPDFBitmapFromCFXDIBitmap(pBitmap.Leak());
}
@@ -955,6 +965,7 @@
if (!pBitmap) {
return;
}
+ CHECK(!pBitmap->IsPremultiplied());
CFX_DefaultRenderDevice device;
device.Attach(pBitmap);