xref: /aoo4110/main/basebmp/test/cliptest.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // autogenerated file with codegen.pl
25 
26 #include "preextstl.h"
27 #include "cppunit/TestAssert.h"
28 #include "cppunit/TestFixture.h"
29 #include "cppunit/extensions/HelperMacros.h"
30 #include "postextstl.h"
31 
32 #include <basegfx/vector/b2isize.hxx>
33 #include <basegfx/point/b2ipoint.hxx>
34 #include <basegfx/range/b2drange.hxx>
35 #include <basegfx/range/b2irange.hxx>
36 #include <basegfx/polygon/b2dpolygon.hxx>
37 #include <basegfx/polygon/b2dpolygontools.hxx>
38 #include <basegfx/polygon/b2dpolypolygon.hxx>
39 #include <basegfx/polygon/b2dpolypolygontools.hxx>
40 
41 #include <basebmp/color.hxx>
42 #include <basebmp/scanlineformats.hxx>
43 #include <basebmp/bitmapdevice.hxx>
44 #include <basebmp/debug.hxx>
45 #include "tools.hxx"
46 
47 #include <iostream>
48 #include <fstream>
49 
50 using namespace ::basebmp;
51 
52 namespace
53 {
54 /*
55   std::ofstream output("32bpp_test.dump");
56   debugDump( mpDevice32bpp, output );
57 */
58 
59 class ClipTest : public CppUnit::TestFixture
60 {
61 private:
62     BitmapDeviceSharedPtr mpClipMask;
63     BitmapDeviceSharedPtr mpDevice1bpp;
64     BitmapDeviceSharedPtr mpDevice32bpp;
65 
implTestPixelClip(const BitmapDeviceSharedPtr & rDevice)66     void implTestPixelClip(const BitmapDeviceSharedPtr& rDevice)
67     {
68         const Color aBgCol(0);
69         rDevice->clear(aBgCol);
70 
71         const basegfx::B2IPoint aPt(0,0);
72         const Color aCol(0xFFFFFFFF);
73         rDevice->setPixel( aPt, aCol, DrawMode_PAINT, mpClipMask );
74         CPPUNIT_ASSERT_MESSAGE("get/setPixel clip #1",
75                                rDevice->getPixel(aPt) == aBgCol);
76 
77         const basegfx::B2IPoint aPt2(10,10);
78         rDevice->setPixel( aPt2, aCol, DrawMode_PAINT, mpClipMask );
79         CPPUNIT_ASSERT_MESSAGE("get/setPixel clip #2",
80                                rDevice->getPixel(aPt2) == aBgCol);
81 
82         const basegfx::B2IPoint aPt1(10,0);
83         rDevice->setPixel( aPt1, aCol, DrawMode_PAINT, mpClipMask );
84         CPPUNIT_ASSERT_MESSAGE("get/setPixel clip #3",
85                                rDevice->getPixel(aPt1) != aBgCol);
86 
87         const basegfx::B2IPoint aPt3(0,10);
88         rDevice->setPixel( aPt3, aCol, DrawMode_PAINT, mpClipMask );
89         CPPUNIT_ASSERT_MESSAGE("get/setPixel clip #4",
90                                rDevice->getPixel(aPt3) != aBgCol);
91     }
92 
implTestLineClip(const BitmapDeviceSharedPtr & rDevice)93     void implTestLineClip(const BitmapDeviceSharedPtr& rDevice)
94     {
95         const Color aBgCol(0);
96         rDevice->clear(aBgCol);
97 
98         const basegfx::B2IPoint aPt1(0,0);
99         const basegfx::B2IPoint aPt2(1,9);
100         const Color aCol(0xFFFFFFFF);
101         rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT, mpClipMask );
102 
103         const basegfx::B2IPoint aPt3(1,5);
104         CPPUNIT_ASSERT_MESSAGE("get line pixel",
105                                rDevice->getPixel(aPt3) != aBgCol);
106         CPPUNIT_ASSERT_MESSAGE("number of rendered line pixel is not 4",
107                                countPixel( rDevice,
108                                            rDevice->getPixel(aPt3) ) == 4);
109 
110         rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_XOR, mpClipMask );
111         CPPUNIT_ASSERT_MESSAGE("number of xor-rendered line pixel is not 0",
112                                countPixel( rDevice,
113                                            rDevice->getPixel(aPt3) ) == 121);
114     }
115 
implTestFillClip(const BitmapDeviceSharedPtr & rDevice)116     void implTestFillClip(const BitmapDeviceSharedPtr& rDevice)
117     {
118         rDevice->clear(Color(0));
119 
120         const basegfx::B2DRange aAllOver(-10,-10,20,20);
121         const Color aCol(0xFFFFFFFF);
122         rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
123                                       basegfx::tools::createPolygonFromRect(aAllOver)),
124                                   aCol,
125                                   DrawMode_PAINT,
126                                   mpClipMask );
127         const basegfx::B2IPoint aPt(0,10);
128         CPPUNIT_ASSERT_MESSAGE("number of clipped pixel is not 30",
129                                countPixel( rDevice, rDevice->getPixel(aPt) ) == 121-30);
130 
131         rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
132                                       basegfx::tools::createPolygonFromRect(aAllOver)),
133                                   aCol,
134                                   DrawMode_PAINT );
135         CPPUNIT_ASSERT_MESSAGE("number of filled pixel is not 121",
136                                countPixel( rDevice, rDevice->getPixel(aPt) ) == 121);
137 
138         rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
139                                       basegfx::tools::createPolygonFromRect(aAllOver)),
140                                   aCol,
141                                   DrawMode_XOR,
142                                   mpClipMask );
143         CPPUNIT_ASSERT_MESSAGE("number of xor-cleared pixel is not 91",
144                                countPixel( rDevice, rDevice->getPixel(aPt) ) == 121-30);
145     }
146 
implTestBmpClip(const BitmapDeviceSharedPtr & rDevice)147     void implTestBmpClip(const BitmapDeviceSharedPtr& rDevice)
148     {
149         BitmapDeviceSharedPtr pBmp( cloneBitmapDevice(
150                                         basegfx::B2IVector(3,3),
151                                         rDevice ));
152         Color aCol1(0);
153         Color aCol2(0xFFFFFFFF);
154         pBmp->clear(aCol1);
155         pBmp->setPixel(basegfx::B2IPoint(0,0),aCol2,DrawMode_PAINT);
156         pBmp->setPixel(basegfx::B2IPoint(1,1),aCol2,DrawMode_PAINT);
157         pBmp->setPixel(basegfx::B2IPoint(2,2),aCol2,basebmp::DrawMode_PAINT);
158 
159         rDevice->clear(aCol1);
160         rDevice->drawBitmap(pBmp,
161                             basegfx::B2IRange(0,0,3,3),
162                             basegfx::B2IRange(-1,-1,4,4),
163                             DrawMode_PAINT,
164                             mpClipMask);
165 
166         const basegfx::B2IPoint aPt(1,1);
167         CPPUNIT_ASSERT_MESSAGE("number of clipped pixel is not 5",
168                                countPixel( rDevice,
169                                            rDevice->getPixel(aPt) ) == 5);
170     }
171 
implTestMaskColorClip(const BitmapDeviceSharedPtr & rDevice)172     void implTestMaskColorClip(const BitmapDeviceSharedPtr& rDevice)
173     {
174         BitmapDeviceSharedPtr pBmp( createBitmapDevice( rDevice->getSize(),
175                                                         true,
176                                                         Format::EIGHT_BIT_GREY ));
177 
178         ::rtl::OUString aSvg = ::rtl::OUString::createFromAscii(
179             "m 0 0h5v10h5v-5h-10z" );
180 
181         basegfx::B2DPolyPolygon aPoly;
182         basegfx::tools::importFromSvgD( aPoly, aSvg );
183         const basebmp::Color aCol(0xFF);
184         pBmp->clear( basebmp::Color(0) );
185         pBmp->fillPolyPolygon(
186             aPoly,
187             aCol,
188             basebmp::DrawMode_PAINT );
189 
190         const basegfx::B2IRange aSourceRect(0,0,10,10);
191         const basegfx::B2IPoint aDestLeftTop(0,0);
192         const Color aCol2(0xF0F0F0F0);
193         rDevice->drawMaskedColor(
194             aCol2,
195             pBmp,
196             aSourceRect,
197             aDestLeftTop,
198             mpClipMask );
199         const basegfx::B2IPoint aPt(1,1);
200         CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 41",
201                                countPixel( rDevice, rDevice->getPixel(aPt) ) == 41);
202 
203     }
204 
205 public:
setUp()206     void setUp()
207     {
208         const basegfx::B2ISize aSize(11,11);
209         mpClipMask = createBitmapDevice( aSize,
210                                          true,
211                                          Format::ONE_BIT_MSB_GREY );
212         mpDevice1bpp = createBitmapDevice( aSize,
213                                            true,
214                                            Format::ONE_BIT_MSB_PAL );
215         mpDevice32bpp = createBitmapDevice( aSize,
216                                            true,
217                                            Format::THIRTYTWO_BIT_TC_MASK );
218 
219         ::rtl::OUString aSvg = ::rtl::OUString::createFromAscii(
220             "m 0 0 h5 l5 5 v5 h-5 l-5-5 z" );
221         basegfx::B2DPolyPolygon aPoly;
222         basegfx::tools::importFromSvgD( aPoly, aSvg );
223         mpClipMask->clear(Color(0));
224         mpClipMask->drawPolygon(
225             aPoly.getB2DPolygon(0),
226             Color(0xFFFFFFFF),
227             DrawMode_PAINT );
228     }
229 
testPixelClip()230     void testPixelClip()
231     {
232         implTestPixelClip( mpDevice1bpp );
233         implTestPixelClip( mpDevice32bpp );
234     }
235 
testLineClip()236     void testLineClip()
237     {
238         implTestLineClip( mpDevice1bpp );
239         implTestLineClip( mpDevice32bpp );
240     }
241 
testFillClip()242     void testFillClip()
243     {
244         implTestFillClip( mpDevice1bpp );
245         implTestFillClip( mpDevice32bpp );
246     }
247 
testBmpClip()248     void testBmpClip()
249     {
250         implTestBmpClip( mpDevice1bpp );
251         implTestBmpClip( mpDevice32bpp );
252     }
253 
testMaskColorClip()254     void testMaskColorClip()
255     {
256         implTestMaskColorClip( mpDevice1bpp );
257         implTestMaskColorClip( mpDevice32bpp );
258     }
259 
260     // Change the following lines only, if you add, remove or rename
261     // member functions of the current class,
262     // because these macros are need by auto register mechanism.
263 
264     CPPUNIT_TEST_SUITE(ClipTest);
265     CPPUNIT_TEST(testPixelClip);
266     CPPUNIT_TEST(testLineClip);
267     CPPUNIT_TEST(testFillClip);
268     CPPUNIT_TEST(testBmpClip);
269     CPPUNIT_TEST(testMaskColorClip);
270     CPPUNIT_TEST_SUITE_END();
271 };
272 
273 // -----------------------------------------------------------------------------
274 CPPUNIT_TEST_SUITE_REGISTRATION(ClipTest);
275 }
276 
277 
278 // -----------------------------------------------------------------------------
279 
280 // this macro creates an empty function, which will called by the RegisterAllFunctions()
281 // to let the user the possibility to also register some functions by hand.
282 //NOADDITIONAL;
283 
284