1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  */
9 
10 #include <cppunit/TestAssert.h>
11 #include <cppunit/TestFixture.h>
12 #include <cppunit/extensions/HelperMacros.h>
13 
14 #include <vcl/bitmapex.hxx>
15 #include <basegfx/matrix/b2dhommatrix.hxx>
16 #include <bitmapwriteaccess.hxx>
17 #include <svdata.hxx>
18 #include <salinst.hxx>
19 
20 namespace
21 {
22 class BitmapExTest : public CppUnit::TestFixture
23 {
24     void testGetPixelColor24_8();
25     void testGetPixelColor32();
26     void testTransformBitmapEx();
27 
28     CPPUNIT_TEST_SUITE(BitmapExTest);
29     CPPUNIT_TEST(testGetPixelColor24_8);
30     CPPUNIT_TEST(testGetPixelColor32);
31     CPPUNIT_TEST(testTransformBitmapEx);
32     CPPUNIT_TEST_SUITE_END();
33 };
34 
testGetPixelColor24_8()35 void BitmapExTest::testGetPixelColor24_8()
36 {
37     Bitmap aBitmap(Size(3, 3), 24);
38     {
39         BitmapScopedWriteAccess pWriteAccess(aBitmap);
40         pWriteAccess->Erase(Color(0x00, 0x00, 0xFF, 0x00));
41     }
42     AlphaMask aMask(Size(3, 3));
43     {
44         AlphaScopedWriteAccess pWriteAccess(aMask);
45         pWriteAccess->Erase(Color(0x00, 0xAA, 0xAA, 0xAA));
46     }
47 
48     BitmapEx aBitmapEx(aBitmap, aMask);
49 
50     CPPUNIT_ASSERT_EQUAL(Color(0xAA, 0x00, 0xFF, 0x00), aBitmapEx.GetPixelColor(0, 0));
51 }
52 
testGetPixelColor32()53 void BitmapExTest::testGetPixelColor32()
54 {
55     // Check backend capabilities and return from the test successfully
56     // if the backend doesn't support 32-bit bitmap
57     auto pBackendCapabilities = ImplGetSVData()->mpDefInst->GetBackendCapabilities();
58     if (!pBackendCapabilities->mbSupportsBitmap32)
59         return;
60 
61     Bitmap aBitmap(Size(3, 3), 32);
62     {
63         BitmapScopedWriteAccess pWriteAccess(aBitmap);
64         pWriteAccess->Erase(Color(0xAA, 0x00, 0xFF, 0x00));
65     }
66 
67     BitmapEx aBitmapEx(aBitmap);
68 
69     CPPUNIT_ASSERT_EQUAL(Color(0xAA, 0x00, 0xFF, 0x00), aBitmapEx.GetPixelColor(0, 0));
70 }
71 
testTransformBitmapEx()72 void BitmapExTest::testTransformBitmapEx()
73 {
74     Bitmap aBitmap(Size(16, 16), 24);
75     {
76         BitmapScopedWriteAccess pWriteAccess(aBitmap);
77         pWriteAccess->Erase(COL_WHITE);
78         for (int i = 0; i < 8; ++i)
79         {
80             for (int j = 0; j < 8; ++j)
81             {
82                 pWriteAccess->SetPixel(i, j, COL_BLACK);
83             }
84         }
85     }
86     BitmapEx aBitmapEx(aBitmap);
87 
88     basegfx::B2DHomMatrix aMatrix;
89     aMatrix.rotate(M_PI / 2);
90     BitmapEx aTransformed = aBitmapEx.TransformBitmapEx(16, 16, aMatrix);
91     aBitmap = aTransformed.GetBitmap();
92     Bitmap::ScopedReadAccess pAccess(aBitmap);
93     for (int i = 0; i < 16; ++i)
94     {
95         for (int j = 0; j < 16; ++j)
96         {
97             BitmapColor aColor = pAccess->GetPixel(i, j);
98             std::stringstream ss;
99             ss << "Color is expected to be white or black, is '" << aColor.AsRGBHexString() << "'";
100             // Without the accompanying fix in place, this test would have failed with:
101             // - Expression: aColor == COL_WHITE || aColor == COL_BLACK
102             // - Color is expected to be white or black, is 'bfbfbf'
103             // i.e. smoothing introduced noise for a simple 90 deg rotation.
104             CPPUNIT_ASSERT_MESSAGE(ss.str(), aColor == COL_WHITE || aColor == COL_BLACK);
105         }
106     }
107 }
108 
109 } // namespace
110 
111 CPPUNIT_TEST_SUITE_REGISTRATION(BitmapExTest);
112 
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
114