[Libreoffice-commits] core.git: include/vcl svx/source vcl/qa vcl/source

Miklos Vajna (via logerrit) logerrit at kemper.freedesktop.org
Thu Oct 15 07:21:41 UTC 2020


 include/vcl/filter/PDFiumLibrary.hxx   |   15 +++++++++++++++
 svx/source/svdraw/svdpdf.cxx           |   18 ++++++------------
 vcl/qa/cppunit/pdfexport/pdfexport.cxx |   21 +++++++++------------
 vcl/source/pdf/PDFiumLibrary.cxx       |   24 ++++++++++++++++++++++++
 4 files changed, 54 insertions(+), 24 deletions(-)

New commits:
commit 807669774a87b9d5a69eededbaf26962205f9ff8
Author:     Miklos Vajna <vmiklos at collabora.com>
AuthorDate: Wed Oct 14 21:02:45 2020 +0200
Commit:     Miklos Vajna <vmiklos at collabora.com>
CommitDate: Thu Oct 15 09:21:06 2020 +0200

    pdfium: add an FPDFImageObj_GetBitmap() wrapper
    
    Replaces both FPDFBitmapDeleter and most manual calls to
    FPDFBitmap_Destroy().
    
    Change-Id: I4676dd2f51b6b348ebbb6edc0379d7afd8fa79d0
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/104324
    Tested-by: Jenkins
    Reviewed-by: Miklos Vajna <vmiklos at collabora.com>

diff --git a/include/vcl/filter/PDFiumLibrary.hxx b/include/vcl/filter/PDFiumLibrary.hxx
index 877308d7d40b..7c01f2f505b0 100644
--- a/include/vcl/filter/PDFiumLibrary.hxx
+++ b/include/vcl/filter/PDFiumLibrary.hxx
@@ -57,6 +57,20 @@ public:
     std::unique_ptr<PDFiumDocument> openDocument(const void* pData, int nSize);
 };
 
+class VCL_DLLPUBLIC PDFiumBitmap final
+{
+private:
+    FPDF_BITMAP mpBitmap;
+
+    PDFiumBitmap(const PDFiumBitmap&) = delete;
+    PDFiumBitmap& operator=(const PDFiumBitmap&) = delete;
+
+public:
+    PDFiumBitmap(FPDF_BITMAP pBitmap);
+    ~PDFiumBitmap();
+    FPDF_BITMAP getPointer() { return mpBitmap; }
+};
+
 class VCL_DLLPUBLIC PDFiumAnnotation final
 {
 private:
@@ -134,6 +148,7 @@ public:
     int getPathSegmentCount();
     std::unique_ptr<PDFiumPathSegment> getPathSegment(int index);
     Size getImageSize(PDFiumPage& rPage);
+    std::unique_ptr<PDFiumBitmap> getImageBitmap();
 };
 
 class VCL_DLLPUBLIC PDFiumTextPage final
diff --git a/svx/source/svdraw/svdpdf.cxx b/svx/source/svdraw/svdpdf.cxx
index 5c34b510301f..97c59fd94ae8 100644
--- a/svx/source/svdraw/svdpdf.cxx
+++ b/svx/source/svdraw/svdpdf.cxx
@@ -81,11 +81,6 @@
 namespace
 {
 double sqrt2(double a, double b) { return sqrt(a * a + b * b); }
-
-struct FPDFBitmapDeleter
-{
-    void operator()(FPDF_BITMAP bitmap) { FPDFBitmap_Destroy(bitmap); }
-};
 }
 
 using namespace com::sun::star;
@@ -882,15 +877,14 @@ void ImpSdrPdfImport::MapScaling()
 void ImpSdrPdfImport::ImportImage(std::unique_ptr<vcl::pdf::PDFiumPageObject> const& pPageObject,
                                   int /*nPageObjectIndex*/)
 {
-    std::unique_ptr<std::remove_pointer<FPDF_BITMAP>::type, FPDFBitmapDeleter> bitmap(
-        FPDFImageObj_GetBitmap(pPageObject->getPointer()));
+    std::unique_ptr<vcl::pdf::PDFiumBitmap> bitmap = pPageObject->getImageBitmap();
     if (!bitmap)
     {
         SAL_WARN("sd.filter", "Failed to get IMAGE");
         return;
     }
 
-    const int format = FPDFBitmap_GetFormat(bitmap.get());
+    const int format = FPDFBitmap_GetFormat(bitmap->getPointer());
     if (format == FPDFBitmap_Unknown)
     {
         SAL_WARN("sd.filter", "Failed to get IMAGE format");
@@ -898,10 +892,10 @@ void ImpSdrPdfImport::ImportImage(std::unique_ptr<vcl::pdf::PDFiumPageObject> co
     }
 
     const unsigned char* pBuf
-        = static_cast<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap.get()));
-    const int nWidth = FPDFBitmap_GetWidth(bitmap.get());
-    const int nHeight = FPDFBitmap_GetHeight(bitmap.get());
-    const int nStride = FPDFBitmap_GetStride(bitmap.get());
+        = static_cast<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap->getPointer()));
+    const int nWidth = FPDFBitmap_GetWidth(bitmap->getPointer());
+    const int nHeight = FPDFBitmap_GetHeight(bitmap->getPointer());
+    const int nStride = FPDFBitmap_GetStride(bitmap->getPointer());
     BitmapEx aBitmap(Size(nWidth, nHeight), 24);
 
     switch (format)
diff --git a/vcl/qa/cppunit/pdfexport/pdfexport.cxx b/vcl/qa/cppunit/pdfexport/pdfexport.cxx
index 8dab51e76805..47799ec81b3d 100644
--- a/vcl/qa/cppunit/pdfexport/pdfexport.cxx
+++ b/vcl/qa/cppunit/pdfexport/pdfexport.cxx
@@ -1448,11 +1448,10 @@ CPPUNIT_TEST_FIXTURE(PdfExportTest, testTdf128630)
         if (pPageObject->getType() != FPDF_PAGEOBJ_IMAGE)
             continue;
 
-        FPDF_BITMAP pBitmap = FPDFImageObj_GetBitmap(pPageObject->getPointer());
+        std::unique_ptr<vcl::pdf::PDFiumBitmap> pBitmap = pPageObject->getImageBitmap();
         CPPUNIT_ASSERT(pBitmap);
-        int nWidth = FPDFBitmap_GetWidth(pBitmap);
-        int nHeight = FPDFBitmap_GetHeight(pBitmap);
-        FPDFBitmap_Destroy(pBitmap);
+        int nWidth = FPDFBitmap_GetWidth(pBitmap->getPointer());
+        int nHeight = FPDFBitmap_GetHeight(pBitmap->getPointer());
         // Without the accompanying fix in place, this test would have failed with:
         // assertion failed
         // - Expression: nWidth != nHeight
@@ -1789,11 +1788,10 @@ CPPUNIT_TEST_FIXTURE(PdfExportTest, testReduceSmallImage)
     CPPUNIT_ASSERT_EQUAL(FPDF_PAGEOBJ_IMAGE, pPageObject->getType());
 
     // Make sure we don't scale down a tiny bitmap.
-    FPDF_BITMAP pBitmap = FPDFImageObj_GetBitmap(pPageObject->getPointer());
+    std::unique_ptr<vcl::pdf::PDFiumBitmap> pBitmap = pPageObject->getImageBitmap();
     CPPUNIT_ASSERT(pBitmap);
-    int nWidth = FPDFBitmap_GetWidth(pBitmap);
-    int nHeight = FPDFBitmap_GetHeight(pBitmap);
-    FPDFBitmap_Destroy(pBitmap);
+    int nWidth = FPDFBitmap_GetWidth(pBitmap->getPointer());
+    int nHeight = FPDFBitmap_GetHeight(pBitmap->getPointer());
     // Without the accompanying fix in place, this test would have failed with:
     // - Expected: 16
     // - Actual  : 6
@@ -1845,11 +1843,10 @@ CPPUNIT_TEST_FIXTURE(PdfExportTest, testReduceImage)
     CPPUNIT_ASSERT_EQUAL(FPDF_PAGEOBJ_IMAGE, pPageObject->getType());
 
     // Make sure we don't scale down a bitmap.
-    FPDF_BITMAP pBitmap = FPDFImageObj_GetBitmap(pPageObject->getPointer());
+    std::unique_ptr<vcl::pdf::PDFiumBitmap> pBitmap = pPageObject->getImageBitmap();
     CPPUNIT_ASSERT(pBitmap);
-    int nWidth = FPDFBitmap_GetWidth(pBitmap);
-    int nHeight = FPDFBitmap_GetHeight(pBitmap);
-    FPDFBitmap_Destroy(pBitmap);
+    int nWidth = FPDFBitmap_GetWidth(pBitmap->getPointer());
+    int nHeight = FPDFBitmap_GetHeight(pBitmap->getPointer());
     // Without the accompanying fix in place, this test would have failed with:
     // - Expected: 160
     // - Actual  : 6
diff --git a/vcl/source/pdf/PDFiumLibrary.cxx b/vcl/source/pdf/PDFiumLibrary.cxx
index b8d0cb536be7..074863156f97 100644
--- a/vcl/source/pdf/PDFiumLibrary.cxx
+++ b/vcl/source/pdf/PDFiumLibrary.cxx
@@ -362,6 +362,17 @@ Size PDFiumPageObject::getImageSize(PDFiumPage& rPage)
     return Size(aMeta.width, aMeta.height);
 }
 
+std::unique_ptr<PDFiumBitmap> PDFiumPageObject::getImageBitmap()
+{
+    std::unique_ptr<PDFiumBitmap> pPDFiumBitmap;
+    FPDF_BITMAP pBitmap = FPDFImageObj_GetBitmap(mpPageObject);
+    if (pBitmap)
+    {
+        pPDFiumBitmap = std::make_unique<PDFiumBitmap>(pBitmap);
+    }
+    return pPDFiumBitmap;
+}
+
 BitmapChecksum PDFiumPage::getChecksum()
 {
     size_t nPageWidth = getWidth();
@@ -414,6 +425,19 @@ bool PDFiumPathSegment::isClosed() const { return FPDFPathSegment_GetClose(mpPat
 
 int PDFiumPathSegment::getType() const { return FPDFPathSegment_GetType(mpPathSegment); }
 
+PDFiumBitmap::PDFiumBitmap(FPDF_BITMAP pBitmap)
+    : mpBitmap(pBitmap)
+{
+}
+
+PDFiumBitmap::~PDFiumBitmap()
+{
+    if (mpBitmap)
+    {
+        FPDFBitmap_Destroy(mpBitmap);
+    }
+}
+
 PDFiumAnnotation::PDFiumAnnotation(FPDF_ANNOTATION pAnnotation)
     : mpAnnotation(pAnnotation)
 {


More information about the Libreoffice-commits mailing list