diff options
author | Chris Sherlock <chris.sherlock79@gmail.com> | 2021-09-03 18:55:08 +1000 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2021-09-08 10:11:42 +0200 |
commit | 6d6c0e2c35186323bb5af35f5a58a2c9d9baa1ed (patch) | |
tree | 3d92d4a0adacc9b16444f4051ebc79f26cc738eb /vcl | |
parent | f83d1b824c1abd07d24318adda6c876b9471ccec (diff) |
vcl: migrate GetDownsampledBitmap() from OutputDevice to vcl::bitmap
Change-Id: Iee6caa8292472a3acca66d670a113b701f4b637e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/121581
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'vcl')
-rw-r--r-- | vcl/qa/cppunit/BitmapTest.cxx | 25 | ||||
-rw-r--r-- | vcl/qa/cppunit/outdev.cxx | 71 | ||||
-rw-r--r-- | vcl/source/bitmap/BitmapTools.cxx | 68 | ||||
-rw-r--r-- | vcl/source/outdev/bitmap.cxx | 68 | ||||
-rw-r--r-- | vcl/source/outdev/transparent.cxx | 5 |
5 files changed, 89 insertions, 148 deletions
diff --git a/vcl/qa/cppunit/BitmapTest.cxx b/vcl/qa/cppunit/BitmapTest.cxx index 25630ea9cabb..9d4cbb592f68 100644 --- a/vcl/qa/cppunit/BitmapTest.cxx +++ b/vcl/qa/cppunit/BitmapTest.cxx @@ -11,21 +11,22 @@ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/plugin/TestPlugIn.h> -#include <unordered_map> +#include <config_features.h> +#include <rtl/strbuf.hxx> + +#include <vcl/BitmapTools.hxx> #include <vcl/bitmap.hxx> #include <vcl/virdev.hxx> - -#include <rtl/strbuf.hxx> -#include <config_features.h> #include <vcl/skia/SkiaHelper.hxx> #include <vcl/BitmapMonochromeFilter.hxx> #include <bitmap/BitmapWriteAccess.hxx> - -#include <svdata.hxx> -#include <salinst.hxx> #include <bitmap/Octree.hxx> +#include <salinst.hxx> +#include <svdata.hxx> + +#include <unordered_map> namespace { @@ -46,6 +47,7 @@ class BitmapTest : public CppUnit::TestFixture void testDitherSize(); void testMirror(); void testCrop(); + void testCroppedDownsampledBitmap(); CPPUNIT_TEST_SUITE(BitmapTest); CPPUNIT_TEST(testCreation); @@ -63,6 +65,7 @@ class BitmapTest : public CppUnit::TestFixture CPPUNIT_TEST(testDitherSize); CPPUNIT_TEST(testMirror); CPPUNIT_TEST(testCrop); + CPPUNIT_TEST(testCroppedDownsampledBitmap); CPPUNIT_TEST_SUITE_END(); }; @@ -687,6 +690,14 @@ void BitmapTest::testMirror() } } +void BitmapTest::testCroppedDownsampledBitmap() +{ + Bitmap aBitmap(Size(16, 16), vcl::PixelFormat::N24_BPP); + Bitmap aDownsampledBmp(vcl::bitmap::GetDownsampledBitmap(Size(10, 10), Point(20, 20), + Size(5, 5), aBitmap, 72, 72)); + CPPUNIT_ASSERT(aDownsampledBmp.IsEmpty()); +} + void BitmapTest::testCrop() { Bitmap aBitmap(Bitmap(Size(16, 16), vcl::PixelFormat::N24_BPP)); diff --git a/vcl/qa/cppunit/outdev.cxx b/vcl/qa/cppunit/outdev.cxx index 0a635a1a139d..5f78c5127807 100644 --- a/vcl/qa/cppunit/outdev.cxx +++ b/vcl/qa/cppunit/outdev.cxx @@ -44,7 +44,6 @@ public: void testDrawScalePartBitmap(); void testDrawTransformedBitmapEx(); void testDrawTransformedBitmapExFlip(); - void testCroppedDownsampledBitmap(); void testRTL(); void testRTLGuard(); void testDefaultFillColor(); @@ -78,7 +77,6 @@ public: CPPUNIT_TEST(testGetReadableFontColorWindow); CPPUNIT_TEST(testDrawTransformedBitmapEx); CPPUNIT_TEST(testDrawTransformedBitmapExFlip); - CPPUNIT_TEST(testCroppedDownsampledBitmap); CPPUNIT_TEST(testRTL); CPPUNIT_TEST(testRTLGuard); CPPUNIT_TEST(testDefaultFillColor); @@ -514,75 +512,6 @@ void VclOutdevTest::testDrawTransformedBitmapExFlip() CPPUNIT_ASSERT_EQUAL_MESSAGE(ss.str(), COL_BLACK, Color(aColor)); } -namespace -{ -class DownsampleBitmapTester : public OutputDevice -{ -public: - DownsampleBitmapTester() - : OutputDevice(OUTDEV_VIRDEV) - , maBitmap(Bitmap(Size(16, 16), vcl::PixelFormat::N24_BPP)) - { - SetDPIX(96); - SetDPIY(96); - } - - bool AcquireGraphics() const { return true; } - void ReleaseGraphics(bool) {} - bool UsePolyPolygonForComplexGradient() { return false; } - - Bitmap testCropFullyOutsideBounds() - { - return GetDownsampledBitmap(Size(10, 10), Point(20, 20), Size(5, 5), maBitmap, 72, 72); - } - - Bitmap testCropSameSize() - { - return GetDownsampledBitmap(Size(10, 10), Point(0, 0), maBitmap.GetSizePixel(), maBitmap, - 72, 72); - } - - Bitmap testFullyOvercrop() - { - return GetDownsampledBitmap(Size(10, 10), Point(0, 0), Size(100, 100), maBitmap, 72, 72); - } - - Bitmap testPartiallyOvercrop() - { - return GetDownsampledBitmap(Size(10, 10), Point(10, 10), Size(100, 100), maBitmap, 72, 72); - } - -private: - Bitmap maBitmap; -}; -} - -void VclOutdevTest::testCroppedDownsampledBitmap() -{ - ScopedVclPtrInstance<DownsampleBitmapTester> pTester; - - { - Bitmap aDownsampledBmp(pTester->testCropFullyOutsideBounds()); - CPPUNIT_ASSERT_MESSAGE("Crop was fully outside of bitmap bounds", - aDownsampledBmp.IsEmpty()); - } - - { - Bitmap aDownsampledBmp(pTester->testCropSameSize()); - CPPUNIT_ASSERT_MESSAGE("Crop same size as bitmap", !aDownsampledBmp.IsEmpty()); - } - - { - Bitmap aDownsampledBmp(pTester->testFullyOvercrop()); - CPPUNIT_ASSERT_MESSAGE("Crop larger than bitmap", !aDownsampledBmp.IsEmpty()); - } - - { - Bitmap aDownsampledBmp(pTester->testPartiallyOvercrop()); - CPPUNIT_ASSERT_MESSAGE("Crop partially overcrops bitmap", !aDownsampledBmp.IsEmpty()); - } -} - void VclOutdevTest::testRTL() { ScopedVclPtrInstance<vcl::Window> pWindow(nullptr, WB_APP | WB_STDWORK); diff --git a/vcl/source/bitmap/BitmapTools.cxx b/vcl/source/bitmap/BitmapTools.cxx index eddef22cfb53..2a9dc870ddc1 100644 --- a/vcl/source/bitmap/BitmapTools.cxx +++ b/vcl/source/bitmap/BitmapTools.cxx @@ -13,6 +13,7 @@ #include <array> #include <utility> +#include <tools/helpers.hxx> #include <vcl/BitmapTools.hxx> #include <sal/log.hxx> @@ -1140,6 +1141,73 @@ bool convertBitmap32To24Plus8(BitmapEx const & rInput, BitmapEx & rResult) return true; } +Bitmap GetDownsampledBitmap(Size const& rDstSizeTwip, Point const& rSrcPt, Size const& rSrcSz, + Bitmap const& rBmp, tools::Long nMaxBmpDPIX, tools::Long nMaxBmpDPIY) +{ + Bitmap aBmp(rBmp); + + if (!aBmp.IsEmpty()) + { + const tools::Rectangle aBmpRect( Point(), aBmp.GetSizePixel() ); + tools::Rectangle aSrcRect( rSrcPt, rSrcSz ); + + // do cropping if necessary + if( aSrcRect.Intersection( aBmpRect ) != aBmpRect ) + { + if( !aSrcRect.IsEmpty() ) + aBmp.Crop( aSrcRect ); + else + aBmp.SetEmpty(); + } + + if( !aBmp.IsEmpty() ) + { + // do downsampling if necessary + // #103209# Normalize size (mirroring has to happen outside of this method) + Size aDstSizeTwip(std::abs(rDstSizeTwip.Width()), std::abs(rDstSizeTwip.Height())); + + const Size aBmpSize( aBmp.GetSizePixel() ); + const double fBmpPixelX = aBmpSize.Width(); + const double fBmpPixelY = aBmpSize.Height(); + const double fMaxPixelX + = o3tl::convert<double>(aDstSizeTwip.Width(), o3tl::Length::twip, o3tl::Length::in) + * nMaxBmpDPIX; + const double fMaxPixelY + = o3tl::convert<double>(aDstSizeTwip.Height(), o3tl::Length::twip, o3tl::Length::in) + * nMaxBmpDPIY; + + // check, if the bitmap DPI exceeds the maximum DPI (allow 4 pixel rounding tolerance) + if (((fBmpPixelX > (fMaxPixelX + 4)) || + (fBmpPixelY > (fMaxPixelY + 4))) && + (fBmpPixelY > 0.0) && (fMaxPixelY > 0.0)) + { + // do scaling + Size aNewBmpSize; + const double fBmpWH = fBmpPixelX / fBmpPixelY; + const double fMaxWH = fMaxPixelX / fMaxPixelY; + + if (fBmpWH < fMaxWH) + { + aNewBmpSize.setWidth(FRound(fMaxPixelY * fBmpWH)); + aNewBmpSize.setHeight(FRound(fMaxPixelY)); + } + else if (fBmpWH > 0.0) + { + aNewBmpSize.setWidth(FRound(fMaxPixelX)); + aNewBmpSize.setHeight(FRound(fMaxPixelX / fBmpWH)); + } + + if( aNewBmpSize.Width() && aNewBmpSize.Height() ) + aBmp.Scale(aNewBmpSize); + else + aBmp.SetEmpty(); + } + } + } + + return aBmp; +} + } // end vcl::bitmap /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/outdev/bitmap.cxx b/vcl/source/outdev/bitmap.cxx index 5466340c2bfe..6f548cbfc7a3 100644 --- a/vcl/source/outdev/bitmap.cxx +++ b/vcl/source/outdev/bitmap.cxx @@ -170,74 +170,6 @@ void OutputDevice::DrawBitmap( const Point& rDestPt, const Size& rDestSize, } } -Bitmap OutputDevice::GetDownsampledBitmap( const Size& rDstSizeTwip, - const Point& rSrcPt, const Size& rSrcSz, - const Bitmap& rBmp, tools::Long nMaxBmpDPIX, tools::Long nMaxBmpDPIY ) -{ - Bitmap aBmp( rBmp ); - - if( !aBmp.IsEmpty() ) - { - const tools::Rectangle aBmpRect( Point(), aBmp.GetSizePixel() ); - tools::Rectangle aSrcRect( rSrcPt, rSrcSz ); - - // do cropping if necessary - if( aSrcRect.Intersection( aBmpRect ) != aBmpRect ) - { - if( !aSrcRect.IsEmpty() ) - aBmp.Crop( aSrcRect ); - else - aBmp.SetEmpty(); - } - - if( !aBmp.IsEmpty() ) - { - // do downsampling if necessary - // #103209# Normalize size (mirroring has to happen outside of this method) - Size aDstSizeTwip(std::abs(rDstSizeTwip.Width()), std::abs(rDstSizeTwip.Height())); - - const Size aBmpSize( aBmp.GetSizePixel() ); - const double fBmpPixelX = aBmpSize.Width(); - const double fBmpPixelY = aBmpSize.Height(); - const double fMaxPixelX - = o3tl::convert<double>(aDstSizeTwip.Width(), o3tl::Length::twip, o3tl::Length::in) - * nMaxBmpDPIX; - const double fMaxPixelY - = o3tl::convert<double>(aDstSizeTwip.Height(), o3tl::Length::twip, o3tl::Length::in) - * nMaxBmpDPIY; - - // check, if the bitmap DPI exceeds the maximum DPI (allow 4 pixel rounding tolerance) - if( ( ( fBmpPixelX > ( fMaxPixelX + 4 ) ) || - ( fBmpPixelY > ( fMaxPixelY + 4 ) ) ) && - ( fBmpPixelY > 0.0 ) && ( fMaxPixelY > 0.0 ) ) - { - // do scaling - Size aNewBmpSize; - const double fBmpWH = fBmpPixelX / fBmpPixelY; - const double fMaxWH = fMaxPixelX / fMaxPixelY; - - if( fBmpWH < fMaxWH ) - { - aNewBmpSize.setWidth( FRound( fMaxPixelY * fBmpWH ) ); - aNewBmpSize.setHeight( FRound( fMaxPixelY ) ); - } - else if( fBmpWH > 0.0 ) - { - aNewBmpSize.setWidth( FRound( fMaxPixelX ) ); - aNewBmpSize.setHeight( FRound( fMaxPixelX / fBmpWH) ); - } - - if( aNewBmpSize.Width() && aNewBmpSize.Height() ) - aBmp.Scale( aNewBmpSize ); - else - aBmp.SetEmpty(); - } - } - } - - return aBmp; -} - Bitmap OutputDevice::GetBitmap( const Point& rSrcPt, const Size& rSize ) const { Bitmap aBmp; diff --git a/vcl/source/outdev/transparent.cxx b/vcl/source/outdev/transparent.cxx index 8cb2cf51632f..fa17063fa0dd 100644 --- a/vcl/source/outdev/transparent.cxx +++ b/vcl/source/outdev/transparent.cxx @@ -24,6 +24,7 @@ #include <tools/helpers.hxx> #include <officecfg/Office/Common.hxx> +#include <vcl/BitmapTools.hxx> #include <vcl/metaact.hxx> #include <vcl/print.hxx> #include <vcl/settings.hxx> @@ -1836,9 +1837,9 @@ bool OutputDevice::RemoveTransparenciesFromMetaFile( const GDIMetaFile& rInMtf, // scale down bitmap, if requested if( bDownsampleBitmaps ) - aBandBmp = GetDownsampledBitmap( PixelToLogic(LogicToPixel(aDstSzPix), MapMode(MapUnit::MapTwip)), + aBandBmp = vcl::bitmap::GetDownsampledBitmap(PixelToLogic(LogicToPixel(aDstSzPix), MapMode(MapUnit::MapTwip)), Point(), aBandBmp.GetSizePixel(), - aBandBmp, nMaxBmpDPIX, nMaxBmpDPIY ); + aBandBmp, nMaxBmpDPIX, nMaxBmpDPIY); rOutMtf.AddAction( new MetaCommentAction( "PRNSPOOL_TRANSPARENTBITMAP_BEGIN" ) ); rOutMtf.AddAction( new MetaBmpScaleAction( aDstPtPix, aDstSzPix, aBandBmp ) ); |