From 44f7eed9d4f9f9634e85e757172d5d3b5a5d534a Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Mon, 20 Jan 2020 17:53:25 +0100 Subject: PDF export: skip pointless downsampling for very small images Regression from commit b6588bd7c831ce88a29131ca7ea8d3f3e082564e (Reduce image resolution by default in PDF Export, 2014-03-02) the problem is that in case you have small enough bitmaps, then these used to look OK at reasonable zoom levels, but now we intentionally scale down bitmaps by default. That makes little sense for tiny images, do this only for large ones. Change-Id: Iff15325b842b47d9285a7c0f83f402897498392d Reviewed-on: https://gerrit.libreoffice.org/c/core/+/87086 Reviewed-by: Miklos Vajna Tested-by: Jenkins --- .../cppunit/pdfexport/data/reduce-small-image.fodt | 21 +++++++++++ vcl/qa/cppunit/pdfexport/pdfexport.cxx | 41 ++++++++++++++++++++++ vcl/source/gdi/pdfwriter_impl2.cxx | 6 ++-- 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 vcl/qa/cppunit/pdfexport/data/reduce-small-image.fodt diff --git a/vcl/qa/cppunit/pdfexport/data/reduce-small-image.fodt b/vcl/qa/cppunit/pdfexport/data/reduce-small-image.fodt new file mode 100644 index 000000000000..99ff22746e48 --- /dev/null +++ b/vcl/qa/cppunit/pdfexport/data/reduce-small-image.fodt @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAMklEQVR42mP4//8/AyWYYXAZ + wHSK+z8pbOoaAJIgBWM1gFh/jxqAxwCKYmHgE9KAZSYAhK3Dgc2FxfUAAAAASUVORK5CYII= + + + + diff --git a/vcl/qa/cppunit/pdfexport/pdfexport.cxx b/vcl/qa/cppunit/pdfexport/pdfexport.cxx index 32288d569728..602dd6a4a0b7 100644 --- a/vcl/qa/cppunit/pdfexport/pdfexport.cxx +++ b/vcl/qa/cppunit/pdfexport/pdfexport.cxx @@ -149,6 +149,7 @@ public: //BAD CPPUNIT_TEST(testTdf106972Pdf17); //BAD CPPUNIT_TEST(testTdf107018); //BAD CPPUNIT_TEST(testTdf107089); + void testReduceSmallImage(); CPPUNIT_TEST_SUITE(PdfExportTest); // CPPUNIT_TEST(testTdf106059); @@ -186,6 +187,7 @@ public: CPPUNIT_TEST(testTdf115967); CPPUNIT_TEST(testTdf121615); CPPUNIT_TEST(testTocLink); + CPPUNIT_TEST(testReduceSmallImage); CPPUNIT_TEST_SUITE_END(); }; @@ -1830,6 +1832,45 @@ void PdfExportTest::testTocLink() CPPUNIT_ASSERT(FPDFLink_Enumerate(pPdfPage.get(), &nStartPos, &pLinkAnnot)); } +void PdfExportTest::testReduceSmallImage() +{ + // Load the Writer document. + OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "reduce-small-image.fodt"; + mxComponent = loadFromDesktop(aURL); + + // Save as PDF. + uno::Reference xStorable(mxComponent, uno::UNO_QUERY); + utl::MediaDescriptor aMediaDescriptor; + aMediaDescriptor["FilterName"] <<= OUString("writer_pdf_Export"); + xStorable->storeToURL(maTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList()); + + // Parse the PDF: get the image. + SvFileStream aFile(maTempFile.GetURL(), StreamMode::READ); + maMemory.WriteStream(aFile); + DocumentHolder pPdfDocument( + FPDF_LoadMemDocument(maMemory.GetData(), maMemory.GetSize(), /*password=*/nullptr)); + CPPUNIT_ASSERT(pPdfDocument.get()); + CPPUNIT_ASSERT_EQUAL(1, FPDF_GetPageCount(pPdfDocument.get())); + PageHolder pPdfPage(FPDF_LoadPage(pPdfDocument.get(), /*page_index=*/0)); + CPPUNIT_ASSERT(pPdfPage.get()); + CPPUNIT_ASSERT_EQUAL(1, FPDFPage_CountObjects(pPdfPage.get())); + FPDF_PAGEOBJECT pPageObject = FPDFPage_GetObject(pPdfPage.get(), 0); + CPPUNIT_ASSERT_EQUAL(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(pPageObject)); + + // Make sure we don't scale down a tiny bitmap. + FPDF_BITMAP pBitmap = FPDFImageObj_GetBitmap(pPageObject); + CPPUNIT_ASSERT(pBitmap); + int nWidth = FPDFBitmap_GetWidth(pBitmap); + int nHeight = FPDFBitmap_GetHeight(pBitmap); + FPDFBitmap_Destroy(pBitmap); + // Without the accompanying fix in place, this test would have failed with: + // - Expected: 16 + // - Actual : 6 + // i.e. the image was scaled down to 300 DPI, even if it had tiny size. + CPPUNIT_ASSERT_EQUAL(16, nWidth); + CPPUNIT_ASSERT_EQUAL(16, nHeight); +} + CPPUNIT_TEST_SUITE_REGISTRATION(PdfExportTest); } diff --git a/vcl/source/gdi/pdfwriter_impl2.cxx b/vcl/source/gdi/pdfwriter_impl2.cxx index 5e5c0e5fbf6a..5edf11eba18d 100644 --- a/vcl/source/gdi/pdfwriter_impl2.cxx +++ b/vcl/source/gdi/pdfwriter_impl2.cxx @@ -110,11 +110,13 @@ void PDFWriterImpl::implWriteBitmapEx( const Point& i_rPoint, const Size& i_rSiz bIsPng = (eType == GfxLinkType::NativePng); } - if( i_rContext.m_nMaxImageResolution > 50 ) + // Do not downsample images smaller than 50x50px. + const Size aBmpSize(aBitmapEx.GetSizePixel()); + if (i_rContext.m_nMaxImageResolution > 50 && aBmpSize.getWidth() > 50 + && aBmpSize.getHeight() > 50) { // do downsampling if necessary const Size aDstSizeTwip( i_pDummyVDev->PixelToLogic(i_pDummyVDev->LogicToPixel(aSize), MapMode(MapUnit::MapTwip)) ); - const Size aBmpSize( aBitmapEx.GetSizePixel() ); const double fBmpPixelX = aBmpSize.Width(); const double fBmpPixelY = aBmpSize.Height(); const double fMaxPixelX = aDstSizeTwip.Width() * i_rContext.m_nMaxImageResolution / 1440.0; -- cgit