Branch data Line data Source code
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 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "cppunit/TestAssert.h"
21 : : #include "cppunit/TestFixture.h"
22 : : #include "cppunit/extensions/HelperMacros.h"
23 : :
24 : : #include <basegfx/vector/b2isize.hxx>
25 : : #include <basegfx/range/b2ibox.hxx>
26 : : #include <basegfx/point/b2ipoint.hxx>
27 : : #include <basegfx/matrix/b2dhommatrix.hxx>
28 : : #include <basegfx/polygon/b2dpolygon.hxx>
29 : : #include <basegfx/polygon/b2dpolygontools.hxx>
30 : : #include <basegfx/polygon/b2dpolypolygon.hxx>
31 : : #include <basegfx/polygon/b2dpolypolygontools.hxx>
32 : :
33 : : #include <basebmp/color.hxx>
34 : : #include <basebmp/scanlineformats.hxx>
35 : : #include <basebmp/bitmapdevice.hxx>
36 : : #include "tools.hxx"
37 : :
38 : : using namespace ::basebmp;
39 : :
40 : : namespace
41 : : {
42 [ + - ][ + - ]: 105 : class PolyTest : public CppUnit::TestFixture
[ - + ][ + - ]
[ + - ]
43 : : {
44 : : private:
45 : : BitmapDeviceSharedPtr mpDevice1bpp;
46 : : BitmapDeviceSharedPtr mpDevice32bpp;
47 : :
48 : 10 : void implTestEmpty(const BitmapDeviceSharedPtr& rDevice)
49 : : {
50 : 10 : const Color aCol(0xFFFFFFFF);
51 : 10 : const Color aBgCol(0);
52 [ + - ]: 10 : rDevice->clear(aBgCol);
53 [ + - ]: 10 : basegfx::B2DPolyPolygon aPoly;
54 : :
55 : : basegfx::tools::importFromSvgD(
56 : : aPoly,
57 [ + - ]: 10 : rtl::OUString( "M2 2 l7 7 z" ) );
58 : : rDevice->fillPolyPolygon(
59 : : aPoly,
60 : : aCol,
61 [ + - ]: 10 : DrawMode_PAINT );
62 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 0",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
63 [ + - ]: 10 : countPixel( rDevice, aCol ) == 0);
64 : :
65 : : // --------------------------------------------------
66 : :
67 [ + - ]: 10 : rDevice->clear(aBgCol);
68 [ + - ]: 10 : aPoly.clear();
69 : : basegfx::tools::importFromSvgD(
70 : : aPoly,
71 [ + - ]: 10 : rtl::OUString( "M7 2 l-6 6 z" ) );
72 : : rDevice->fillPolyPolygon(
73 : : aPoly,
74 : : aCol,
75 [ + - ]: 10 : DrawMode_PAINT );
76 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 0(b)",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
77 [ + - + - ]: 20 : countPixel( rDevice, aCol ) == 0);
78 : 10 : }
79 : :
80 : 10 : void implTestHairline(const BitmapDeviceSharedPtr& rDevice)
81 : : {
82 : 10 : const Color aCol(0xFFFFFFFF);
83 : 10 : const Color aBgCol(0);
84 [ + - ]: 10 : rDevice->clear(aBgCol);
85 [ + - ]: 10 : basegfx::B2DPolyPolygon aPoly;
86 : :
87 : : basegfx::tools::importFromSvgD(
88 : : aPoly,
89 [ + - ]: 10 : rtl::OUString( "M2 2 h1 l7 7 h-1 z" ) );
90 : : rDevice->fillPolyPolygon(
91 : : aPoly,
92 : : aCol,
93 [ + - ]: 10 : DrawMode_PAINT );
94 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 7",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
95 [ + - ]: 10 : countPixel( rDevice, aCol ) == 7);
96 : :
97 : : // --------------------------------------------------
98 : :
99 [ + - ]: 10 : rDevice->clear(aBgCol);
100 [ + - ]: 10 : aPoly.clear();
101 : : basegfx::tools::importFromSvgD(
102 : : aPoly,
103 [ + - ]: 10 : rtl::OUString( "M7 2 h-1 l-6 6 h1 z" ) );
104 : : rDevice->fillPolyPolygon(
105 : : aPoly,
106 : : aCol,
107 [ + - ]: 10 : DrawMode_PAINT );
108 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 6",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
109 [ + - ]: 10 : countPixel( rDevice, aCol ) == 6);
110 : :
111 : : // --------------------------------------------------
112 : :
113 [ + - ]: 10 : rDevice->clear(aBgCol);
114 [ + - ]: 10 : aPoly.clear();
115 : : basegfx::tools::importFromSvgD(
116 : : aPoly,
117 [ + - ]: 10 : rtl::OUString( "M0 0 l7 7 h-1 l-5-7 z" ) );
118 : : rDevice->fillPolyPolygon(
119 : : aPoly,
120 : : aCol,
121 [ + - ]: 10 : DrawMode_PAINT );
122 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 3",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
123 [ + - + - ]: 20 : countPixel( rDevice, aCol ) == 3);
124 : 10 : }
125 : :
126 : 10 : void implTestPolyPoly(const BitmapDeviceSharedPtr& rDevice)
127 : : {
128 : 10 : const Color aCol(0xFFFFFFFF);
129 : 10 : const Color aBgCol(0);
130 [ + - ]: 10 : rDevice->clear(aBgCol);
131 [ + - ]: 10 : basegfx::B2DPolyPolygon aPoly;
132 : :
133 : : basegfx::tools::importFromSvgD( aPoly,
134 [ + - ]: 10 : ::rtl::OUString( "M0 0 h7 v7 h-7 z M2 2 v3 h3 v-3 z" ) );
135 : :
136 : : rDevice->fillPolyPolygon(
137 : : aPoly,
138 : : aCol,
139 [ + - ]: 10 : DrawMode_PAINT );
140 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 40",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
141 [ + - + - ]: 20 : countPixel( rDevice, aCol ) == 40);
142 : 10 : }
143 : :
144 : 10 : void implTestPolyPolyClip(const BitmapDeviceSharedPtr& rDevice)
145 : : {
146 : 10 : const Color aCol(0xFFFFFFFF);
147 : 10 : const Color aBgCol(0);
148 [ + - ]: 10 : rDevice->clear(aBgCol);
149 [ + - ]: 10 : basegfx::B2DPolyPolygon aPoly;
150 : :
151 : : basegfx::tools::importFromSvgD( aPoly,
152 [ + - ]: 10 : ::rtl::OUString( "M0 0 h7 v7 h-7 z M2 2 v3 h3 v-3 z" ) );
153 [ + - ]: 10 : basegfx::B2DHomMatrix aMat;
154 [ + - ]: 10 : aMat.translate(-3,-3);
155 [ + - ]: 10 : aMat.rotate( 1.7 );
156 [ + - ]: 10 : aMat.translate(6,5);
157 [ + - ]: 10 : aPoly.transform(aMat);
158 : :
159 : : rDevice->fillPolyPolygon(
160 : : aPoly,
161 : : aCol,
162 [ + - ]: 10 : DrawMode_PAINT );
163 : :
164 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 39",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
165 [ + - ]: 10 : countPixel( rDevice, aCol ) == 39);
166 : :
167 : : BitmapDeviceSharedPtr pClippedDevice(
168 : : subsetBitmapDevice( rDevice,
169 [ + - ][ + - ]: 10 : basegfx::B2IBox(3,3,5,8) ));
170 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("size of subsetted device is not (2,5)",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
171 [ + - ]: 10 : pClippedDevice->getSize() == basegfx::B2IVector(2,5));
172 : :
173 [ + - ]: 10 : rDevice->clear(aBgCol);
174 : : pClippedDevice->fillPolyPolygon(
175 : : aPoly,
176 : : aCol,
177 [ + - ]: 10 : DrawMode_PAINT );
178 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 7",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
179 [ + - + - ]: 20 : countPixel( rDevice, aCol ) == 7);
[ + - ][ + - ]
180 : 10 : }
181 : :
182 : 10 : void implTestLineDrawClip(const BitmapDeviceSharedPtr& rDevice)
183 : : {
184 : 10 : const Color aCol(0xFFFFFFFF);
185 : 10 : const Color aBgCol(0);
186 [ + - ]: 10 : rDevice->clear(aBgCol);
187 : :
188 : : // create rectangular subset, such that we can 'see' extra
189 : : // pixel outside
190 : : BitmapDeviceSharedPtr pClippedDevice=(
191 : : subsetBitmapDevice( rDevice,
192 [ + - ][ + - ]: 10 : basegfx::B2IBox(3,3,5,9) ));
193 : :
194 : : // trigger "alternate bresenham" case in
195 : : // clippedlinerenderer.hxx, first point not clipped
196 : 10 : const basegfx::B2IPoint aPt1(3,3);
197 : 10 : const basegfx::B2IPoint aPt2(4,2);
198 [ + - ]: 10 : pClippedDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT );
199 : :
200 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 1",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
201 [ + - ]: 10 : countPixel( rDevice, aCol ) == 1);
202 : :
203 : : // trigger "alternate bresenham" case in
204 : : // clippedlinerenderer.hxx, both start and endpoint clipped
205 : 10 : const basegfx::B2IPoint aPt3(0,4);
206 [ + - ]: 10 : pClippedDevice->drawLine( aPt3, aPt2, aCol, DrawMode_XOR );
207 : :
208 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 0",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
209 [ + - ]: 10 : countPixel( rDevice, aCol ) == 0);
210 : :
211 : : // trigger "standard bresenham" case in
212 : : // clippedlinerenderer.hxx, first point not clipped
213 : 10 : const basegfx::B2IPoint aPt4(6,2);
214 [ + - ]: 10 : pClippedDevice->drawLine( aPt1, aPt4, aCol, DrawMode_PAINT );
215 : :
216 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 2",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
217 [ + - ]: 10 : countPixel( rDevice, aCol ) == 2);
218 : :
219 : : // trigger "clipCode1 & aMinFlag/bMinFlag" cases in
220 : : // clippedlinerenderer.hxx (note1: needs forcing end point to
221 : : // be clipped as well, otherwise optimisation kicks in. note2:
222 : : // needs forcing end point to clip on two edges, not only on
223 : : // one, otherwise swap kicks in)
224 : 10 : const basegfx::B2IPoint aPt5(1,1);
225 : 10 : const basegfx::B2IPoint aPt6(6,10);
226 [ + - ]: 10 : pClippedDevice->drawLine( aPt5, aPt6, aCol, DrawMode_XOR );
227 : :
228 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 6",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
229 [ + - ]: 10 : countPixel( rDevice, aCol ) == 6);
230 : :
231 : : // trigger "clipCode1 & (aMinFlag|aMaxFlag)" case in
232 : : // clippedlinerenderer.hxx that was not taken for the test
233 : : // above
234 [ + - ]: 10 : pClippedDevice->drawLine( aPt3, aPt6, aCol, DrawMode_XOR );
235 : :
236 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 8",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
237 [ + - + - ]: 20 : countPixel( rDevice, aCol ) == 8);
238 : :
239 : 10 : }
240 : :
241 : 10 : void implTestPolyDrawClip(const BitmapDeviceSharedPtr& rDevice)
242 : : {
243 : 10 : const Color aCol(0xFFFFFFFF);
244 : 10 : const Color aBgCol(0);
245 [ + - ]: 10 : rDevice->clear(aBgCol);
246 [ + - ]: 10 : basegfx::B2DPolyPolygon aPoly;
247 : :
248 : : // test all corner-touching lines of our clip rect. note that
249 : : // *all* of the four two-pixel lines in that polygon do *not*
250 : : // generate a single pixel, due to the rasterization effect.
251 : : basegfx::tools::importFromSvgD( aPoly,
252 [ + - ]: 10 : ::rtl::OUString( "M2 3 l1 -1 M4 2 l1 1 M2 8 l1 1 M5 8 l-1 1 M2 5 h4 M3 0 v10" ) );
253 : : BitmapDeviceSharedPtr pClippedDevice=(
254 : : subsetBitmapDevice( rDevice,
255 [ + - ][ + - ]: 10 : basegfx::B2IBox(3,3,5,9) ));
256 : :
257 [ + - ][ + + ]: 70 : for( unsigned int i=0; i<aPoly.count(); ++i )
258 : : pClippedDevice->drawPolygon(
259 : : aPoly.getB2DPolygon(i),
260 : : aCol,
261 [ + - ][ + - ]: 60 : DrawMode_PAINT );
[ + - ]
262 : :
263 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 7",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
264 [ + - + - ]: 20 : countPixel( rDevice, aCol ) == 7);
[ + - ]
265 : 10 : }
266 : :
267 : 10 : void implTestPolyPolyCrissCross(const BitmapDeviceSharedPtr& rDevice)
268 : : {
269 : 10 : const Color aCol(0xFFFFFFFF);
270 : 10 : const Color aBgCol(0);
271 [ + - ]: 10 : rDevice->clear(aBgCol);
272 [ + - ]: 10 : basegfx::B2DPolyPolygon aPoly;
273 : :
274 : : basegfx::tools::importFromSvgD( aPoly,
275 : : ::rtl::OUString( "M0 0 v2 l10 2 v-2 z"
276 : : "M10 6 v-2 l-10 2 v2 z"
277 : : "M1 0 h1 v10 h-1 z"
278 : : "M4 0 h1 v10 h-1 z"
279 [ + - ]: 10 : "M8 0 h1 v10 h-1 z" ) );
280 : : rDevice->fillPolyPolygon(
281 : : aPoly,
282 : : aCol,
283 [ + - ]: 10 : DrawMode_PAINT );
284 [ + - ][ + - ]: 20 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 46",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
285 [ + - + - ]: 20 : countPixel( rDevice, aCol ) == 46);
286 : 10 : }
287 : :
288 : :
289 : : public:
290 : 35 : void setUp()
291 : : {
292 : 35 : const basegfx::B2ISize aSize(10,10);
293 : : mpDevice1bpp = createBitmapDevice( aSize,
294 : : true,
295 [ + - ][ + - ]: 35 : Format::ONE_BIT_MSB_PAL );
[ + - ]
296 : : mpDevice32bpp = createBitmapDevice( aSize,
297 : : true,
298 [ + - ][ + - ]: 35 : Format::THIRTYTWO_BIT_TC_MASK );
[ + - ]
299 : 35 : }
300 : :
301 : 5 : void testEmpty()
302 : : {
303 : 5 : implTestEmpty( mpDevice1bpp );
304 : 5 : implTestEmpty( mpDevice32bpp );
305 : 5 : }
306 : :
307 : 5 : void testHairline()
308 : : {
309 : 5 : implTestHairline( mpDevice1bpp );
310 : 5 : implTestHairline( mpDevice32bpp );
311 : 5 : }
312 : :
313 : 5 : void testPolyPoly()
314 : : {
315 : 5 : implTestPolyPoly( mpDevice1bpp );
316 : 5 : implTestPolyPoly( mpDevice32bpp );
317 : 5 : }
318 : :
319 : 5 : void testPolyPolyClip()
320 : : {
321 : 5 : implTestPolyPolyClip(mpDevice1bpp);
322 : 5 : implTestPolyPolyClip(mpDevice32bpp);
323 : 5 : }
324 : :
325 : 5 : void testLineDrawClip()
326 : : {
327 : 5 : implTestLineDrawClip(mpDevice1bpp);
328 : 5 : implTestLineDrawClip(mpDevice32bpp);
329 : 5 : }
330 : :
331 : 5 : void testPolyDrawClip()
332 : : {
333 : 5 : implTestPolyDrawClip(mpDevice1bpp);
334 : 5 : implTestPolyDrawClip(mpDevice32bpp);
335 : 5 : }
336 : :
337 : 5 : void testPolyPolyCrissCross()
338 : : {
339 : 5 : implTestPolyPolyCrissCross(mpDevice1bpp);
340 : 5 : implTestPolyPolyCrissCross(mpDevice32bpp);
341 : 5 : }
342 : :
343 : : // Change the following lines only, if you add, remove or rename
344 : : // member functions of the current class,
345 : : // because these macros are need by auto register mechanism.
346 : :
347 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE(PolyTest);
[ + - ][ + - ]
[ # # ]
348 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testEmpty);
[ + - ][ + - ]
[ + - ][ + - ]
349 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testHairline);
[ + - ][ + - ]
[ + - ][ + - ]
350 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testPolyPoly);
[ + - ][ + - ]
[ + - ][ + - ]
351 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testPolyPolyClip);
[ + - ][ + - ]
[ + - ][ + - ]
352 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testLineDrawClip);
[ + - ][ + - ]
[ + - ][ + - ]
353 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testPolyDrawClip);
[ + - ][ + - ]
[ + - ][ + - ]
354 [ + - ][ + - ]: 5 : CPPUNIT_TEST(testPolyPolyCrissCross);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
355 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
356 : : };
357 : :
358 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(PolyTest);
359 [ + - ][ + - ]: 15 : }
360 : :
361 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|