diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2020-10-14 21:02:45 +0200 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2020-10-15 09:21:06 +0200 |
commit | 807669774a87b9d5a69eededbaf26962205f9ff8 (patch) | |
tree | 473426e5e04687543f54debe83990e078fc33ae2 | |
parent | ce98648b85169fa0fb8b5d2de179b280a682bde0 (diff) |
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@collabora.com>
-rw-r--r-- | include/vcl/filter/PDFiumLibrary.hxx | 15 | ||||
-rw-r--r-- | svx/source/svdraw/svdpdf.cxx | 18 | ||||
-rw-r--r-- | vcl/qa/cppunit/pdfexport/pdfexport.cxx | 21 | ||||
-rw-r--r-- | vcl/source/pdf/PDFiumLibrary.cxx | 24 |
4 files changed, 54 insertions, 24 deletions
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) { |