bump product version to 5.0.4.1
[LibreOffice.git] / vcl / qa / cppunit / BitmapFilterTest.cxx
blobe0cf3ef1ca8aaf32dfc25c2f89bc3d4961203b46
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <cppunit/TestAssert.h>
11 #include <cppunit/TestFixture.h>
12 #include <cppunit/extensions/HelperMacros.h>
13 #include <cppunit/plugin/TestPlugIn.h>
15 #include <vcl/bitmap.hxx>
16 #include <vcl/bmpacc.hxx>
18 #include <tools/stream.hxx>
19 #include <vcl/graphicfilter.hxx>
21 #include <vcl/BitmapFilterStackBlur.hxx>
22 #include "BitmapSymmetryCheck.hxx"
24 #include <chrono>
26 namespace
29 const bool constWriteResultBitmap(false);
30 const bool constEnablePerformanceTest(false);
32 class BitmapFilterTest : public CppUnit::TestFixture
35 void testBlurCorrectness();
36 void testPerformance();
38 CPPUNIT_TEST_SUITE(BitmapFilterTest);
39 CPPUNIT_TEST(testBlurCorrectness);
40 CPPUNIT_TEST(testPerformance);
41 CPPUNIT_TEST_SUITE_END();
44 void BitmapFilterTest::testBlurCorrectness()
46 // Setup test bitmap
47 Size aSize(41, 31);
48 Bitmap aBitmap24Bit(aSize, 24);
50 sal_uLong nScanlineFormat = 0;
51 sal_uInt16 nBPP = aBitmap24Bit.GetBitCount();
54 long aMargin1 = 1;
55 long aMargin2 = 3;
56 Bitmap::ScopedWriteAccess aWriteAccess(aBitmap24Bit);
57 nScanlineFormat = aWriteAccess->GetScanlineFormat();
58 aWriteAccess->Erase(COL_WHITE);
59 aWriteAccess->SetLineColor(COL_BLACK);
61 Rectangle aRectangle1(
62 aMargin1,
63 aMargin1,
64 aSize.Width() - 1 - aMargin1,
65 aSize.Height() - 1 - aMargin1);
67 Rectangle aRectangle2(
68 aMargin2,
69 aMargin2,
70 aSize.Width() - 1 - aMargin2,
71 aSize.Height() - 1 - aMargin2);
73 Rectangle aRectangle3(
74 aSize.Width() / 2,
75 aSize.Height() / 2,
76 aSize.Width() / 2,
77 aSize.Height() / 2);
79 aWriteAccess->DrawRect(aRectangle1);
80 aWriteAccess->DrawRect(aRectangle2);
81 aWriteAccess->DrawRect(aRectangle3);
84 if (constWriteResultBitmap)
86 SvFileStream aStream(OUString("~/blurBefore.png"), StreamMode::WRITE | StreamMode::TRUNC);
87 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
88 rFilter.compressAsPNG(aBitmap24Bit, aStream, 1);
91 // Perform blur
92 BitmapFilterStackBlur aBlurFilter(2);
93 aBlurFilter.filter(aBitmap24Bit);
95 // Check the result
97 if (constWriteResultBitmap)
99 SvFileStream aStream(OUString("~/blurAfter.png"), StreamMode::WRITE | StreamMode::TRUNC);
100 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
101 rFilter.compressAsPNG(aBitmap24Bit, aStream, 1);
104 // Check blurred bitmap parameters
105 CPPUNIT_ASSERT_EQUAL(static_cast<long>(45), aBitmap24Bit.GetSizePixel().Width());
106 CPPUNIT_ASSERT_EQUAL(static_cast<long>(35), aBitmap24Bit.GetSizePixel().Height());
108 CPPUNIT_ASSERT_EQUAL(nBPP, aBitmap24Bit.GetBitCount());
110 // Check that the bitmap is horizontally and vertically symmetrical
111 BitmapSymmetryCheck symmetryCheck;
112 CPPUNIT_ASSERT(symmetryCheck.check(aBitmap24Bit));
115 Bitmap::ScopedReadAccess aReadAccess(aBitmap24Bit);
116 CPPUNIT_ASSERT_EQUAL(nScanlineFormat, aReadAccess->GetScanlineFormat());
120 void BitmapFilterTest::testPerformance()
122 if (!constEnablePerformanceTest)
123 return;
125 Size aSize(4000, 3000); // A rather common picture size
127 // Prepare bitmap
128 Bitmap aBigBitmap(aSize, 24);
130 long aMargin = 500;
131 Bitmap::ScopedWriteAccess aWriteAccess(aBigBitmap);
132 aWriteAccess->Erase(COL_WHITE);
133 aWriteAccess->SetLineColor(COL_BLACK);
134 aWriteAccess->SetFillColor(COL_BLACK);
135 Rectangle aRectangle(
136 aMargin,
137 aMargin,
138 aSize.Width() - 1 - aMargin,
139 aSize.Height() - 1 - aMargin);
141 aWriteAccess->DrawRect(aRectangle);
144 int nIterations = 10;
145 auto start = std::chrono::high_resolution_clock::now();
146 for (int i = 0; i < nIterations; i++)
149 BitmapFilterStackBlur aBlurFilter(250, false); // don't extend the image
150 aBlurFilter.filter(aBigBitmap);
153 auto end = std::chrono::high_resolution_clock::now();
154 auto elapsed = (end - start) / nIterations;
156 if (constWriteResultBitmap)
158 std::unique_ptr<SvFileStream> pStream(
159 new SvFileStream(OUString("~/BlurBigPerformance.png"), StreamMode::WRITE | StreamMode::TRUNC));
160 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
161 rFilter.compressAsPNG(aBigBitmap, *pStream, 1);
163 pStream.reset(
164 new SvFileStream(OUString("~/BlurBigPerformance.txt"), StreamMode::WRITE));
165 pStream->WriteOString(OString("Blur average time: "));
166 pStream->WriteOString(OString::number(std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count()));
167 pStream->WriteOString(OString("\n"));
171 } // namespace
173 CPPUNIT_TEST_SUITE_REGISTRATION(BitmapFilterTest);
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */