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