1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <cppunit/TestAssert.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
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>
33 #include <basebmp/color.hxx>
34 #include <basebmp/scanlineformats.hxx>
35 #include <basebmp/bitmapdevice.hxx>
38 using namespace ::basebmp
;
42 class PolyTest
: public CppUnit::TestFixture
45 BitmapDeviceSharedPtr mpDevice1bpp
;
46 BitmapDeviceSharedPtr mpDevice32bpp
;
48 void implTestEmpty(const BitmapDeviceSharedPtr
& rDevice
)
50 const Color
aCol(0xFFFFFFFF);
51 const Color
aBgCol(0);
52 rDevice
->clear(aBgCol
);
53 basegfx::B2DPolyPolygon aPoly
;
55 basegfx::tools::importFromSvgD(
57 OUString( "M2 2 l7 7 z" ), false, NULL
);
58 rDevice
->fillPolyPolygon(
62 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 0",
63 countPixel( rDevice
, aCol
) == 0);
67 rDevice
->clear(aBgCol
);
69 basegfx::tools::importFromSvgD(
71 OUString( "M7 2 l-6 6 z" ), false, NULL
);
72 rDevice
->fillPolyPolygon(
76 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 0(b)",
77 countPixel( rDevice
, aCol
) == 0);
80 void implTestHairline(const BitmapDeviceSharedPtr
& rDevice
)
82 const Color
aCol(0xFFFFFFFF);
83 const Color
aBgCol(0);
84 rDevice
->clear(aBgCol
);
85 basegfx::B2DPolyPolygon aPoly
;
87 basegfx::tools::importFromSvgD(
89 OUString( "M2 2 h1 l7 7 h-1 z" ), false, NULL
);
90 rDevice
->fillPolyPolygon(
94 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 7",
95 countPixel( rDevice
, aCol
) == 7);
99 rDevice
->clear(aBgCol
);
101 basegfx::tools::importFromSvgD(
103 OUString( "M7 2 h-1 l-6 6 h1 z" ), false, NULL
);
104 rDevice
->fillPolyPolygon(
108 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 6",
109 countPixel( rDevice
, aCol
) == 6);
113 rDevice
->clear(aBgCol
);
115 basegfx::tools::importFromSvgD(
117 OUString( "M0 0 l7 7 h-1 l-5-7 z" ), false, NULL
);
118 rDevice
->fillPolyPolygon(
122 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 3",
123 countPixel( rDevice
, aCol
) == 3);
126 void implTestPolyPoly(const BitmapDeviceSharedPtr
& rDevice
)
128 const Color
aCol(0xFFFFFFFF);
129 const Color
aBgCol(0);
130 rDevice
->clear(aBgCol
);
131 basegfx::B2DPolyPolygon aPoly
;
133 basegfx::tools::importFromSvgD( aPoly
,
134 OUString( "M0 0 h7 v7 h-7 z M2 2 v3 h3 v-3 z" ),
137 rDevice
->fillPolyPolygon(
141 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 40",
142 countPixel( rDevice
, aCol
) == 40);
145 void implTestPolyPolyClip(const BitmapDeviceSharedPtr
& rDevice
)
147 const Color
aCol(0xFFFFFFFF);
148 const Color
aBgCol(0);
149 rDevice
->clear(aBgCol
);
150 basegfx::B2DPolyPolygon aPoly
;
152 basegfx::tools::importFromSvgD( aPoly
,
153 OUString( "M0 0 h7 v7 h-7 z M2 2 v3 h3 v-3 z" ),
155 basegfx::B2DHomMatrix aMat
;
156 aMat
.translate(-3,-3);
159 aPoly
.transform(aMat
);
161 rDevice
->fillPolyPolygon(
166 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 39",
167 countPixel( rDevice
, aCol
) == 39);
169 BitmapDeviceSharedPtr
pClippedDevice(
170 subsetBitmapDevice( rDevice
,
171 basegfx::B2IBox(3,3,5,8) ));
172 CPPUNIT_ASSERT_MESSAGE("size of subsetted device is not (2,5)",
173 pClippedDevice
->getSize() == basegfx::B2IVector(2,5));
175 rDevice
->clear(aBgCol
);
176 pClippedDevice
->fillPolyPolygon(
180 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 7",
181 countPixel( rDevice
, aCol
) == 7);
184 void implTestLineDrawClip(const BitmapDeviceSharedPtr
& rDevice
)
186 const Color
aCol(0xFFFFFFFF);
187 const Color
aBgCol(0);
188 rDevice
->clear(aBgCol
);
190 // create rectangular subset, such that we can 'see' extra
192 BitmapDeviceSharedPtr pClippedDevice
=(
193 subsetBitmapDevice( rDevice
,
194 basegfx::B2IBox(3,3,5,9) ));
196 // trigger "alternate bresenham" case in
197 // clippedlinerenderer.hxx, first point not clipped
198 const basegfx::B2IPoint
aPt1(3,3);
199 const basegfx::B2IPoint
aPt2(4,2);
200 pClippedDevice
->drawLine( aPt1
, aPt2
, aCol
, DrawMode_PAINT
);
202 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 1",
203 countPixel( rDevice
, aCol
) == 1);
205 // trigger "alternate bresenham" case in
206 // clippedlinerenderer.hxx, both start and endpoint clipped
207 const basegfx::B2IPoint
aPt3(0,4);
208 pClippedDevice
->drawLine( aPt3
, aPt2
, aCol
, DrawMode_XOR
);
210 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 0",
211 countPixel( rDevice
, aCol
) == 0);
213 // trigger "standard bresenham" case in
214 // clippedlinerenderer.hxx, first point not clipped
215 const basegfx::B2IPoint
aPt4(6,2);
216 pClippedDevice
->drawLine( aPt1
, aPt4
, aCol
, DrawMode_PAINT
);
218 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 2",
219 countPixel( rDevice
, aCol
) == 2);
221 // trigger "clipCode1 & aMinFlag/bMinFlag" cases in
222 // clippedlinerenderer.hxx (note1: needs forcing end point to
223 // be clipped as well, otherwise optimization kicks in. note2:
224 // needs forcing end point to clip on two edges, not only on
225 // one, otherwise swap kicks in)
226 const basegfx::B2IPoint
aPt5(1,1);
227 const basegfx::B2IPoint
aPt6(6,10);
228 pClippedDevice
->drawLine( aPt5
, aPt6
, aCol
, DrawMode_XOR
);
230 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 6",
231 countPixel( rDevice
, aCol
) == 6);
233 // trigger "clipCode1 & (aMinFlag|aMaxFlag)" case in
234 // clippedlinerenderer.hxx that was not taken for the test
236 pClippedDevice
->drawLine( aPt3
, aPt6
, aCol
, DrawMode_XOR
);
238 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 8",
239 countPixel( rDevice
, aCol
) == 8);
243 void implTestPolyDrawClip(const BitmapDeviceSharedPtr
& rDevice
)
245 const Color
aCol(0xFFFFFFFF);
246 const Color
aBgCol(0);
247 rDevice
->clear(aBgCol
);
248 basegfx::B2DPolyPolygon aPoly
;
250 // test all corner-touching lines of our clip rect. note that
251 // *all* of the four two-pixel lines in that polygon do *not*
252 // generate a single pixel, due to the rasterization effect.
253 basegfx::tools::importFromSvgD( aPoly
,
254 OUString( "M2 3 l1 -1 M4 2 l1 1 M2 8 l1 1 M5 8 l-1 1 M2 5 h4 M3 0 v10" ),
256 BitmapDeviceSharedPtr pClippedDevice
=(
257 subsetBitmapDevice( rDevice
,
258 basegfx::B2IBox(3,3,5,9) ));
260 for( unsigned int i
=0; i
<aPoly
.count(); ++i
)
261 pClippedDevice
->drawPolygon(
262 aPoly
.getB2DPolygon(i
),
266 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 7",
267 countPixel( rDevice
, aCol
) == 7);
270 void implTestPolyPolyCrissCross(const BitmapDeviceSharedPtr
& rDevice
)
272 const Color
aCol(0xFFFFFFFF);
273 const Color
aBgCol(0);
274 rDevice
->clear(aBgCol
);
275 basegfx::B2DPolyPolygon aPoly
;
277 basegfx::tools::importFromSvgD( aPoly
,
278 OUString( "M0 0 v2 l10 2 v-2 z"
279 "M10 6 v-2 l-10 2 v2 z"
282 "M8 0 h1 v10 h-1 z" ),
284 rDevice
->fillPolyPolygon(
288 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 46",
289 countPixel( rDevice
, aCol
) == 46);
294 void setUp() SAL_OVERRIDE
296 const basegfx::B2ISize
aSize(10,10);
297 mpDevice1bpp
= createBitmapDevice( aSize
,
299 FORMAT_ONE_BIT_MSB_PAL
,
300 basebmp::getBitmapDeviceStrideForWidth(FORMAT_ONE_BIT_MSB_PAL
, aSize
.getX()));
301 mpDevice32bpp
= createBitmapDevice( aSize
,
303 FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA
,
304 basebmp::getBitmapDeviceStrideForWidth(FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA
, aSize
.getX()));
309 implTestEmpty( mpDevice1bpp
);
310 implTestEmpty( mpDevice32bpp
);
315 implTestHairline( mpDevice1bpp
);
316 implTestHairline( mpDevice32bpp
);
321 implTestPolyPoly( mpDevice1bpp
);
322 implTestPolyPoly( mpDevice32bpp
);
325 void testPolyPolyClip()
327 implTestPolyPolyClip(mpDevice1bpp
);
328 implTestPolyPolyClip(mpDevice32bpp
);
331 void testLineDrawClip()
333 implTestLineDrawClip(mpDevice1bpp
);
334 implTestLineDrawClip(mpDevice32bpp
);
337 void testPolyDrawClip()
339 implTestPolyDrawClip(mpDevice1bpp
);
340 implTestPolyDrawClip(mpDevice32bpp
);
343 void testPolyPolyCrissCross()
345 implTestPolyPolyCrissCross(mpDevice1bpp
);
346 implTestPolyPolyCrissCross(mpDevice32bpp
);
349 // Change the following lines only, if you add, remove or rename
350 // member functions of the current class,
351 // because these macros are need by auto register mechanism.
353 CPPUNIT_TEST_SUITE(PolyTest
);
354 CPPUNIT_TEST(testEmpty
);
355 CPPUNIT_TEST(testHairline
);
356 CPPUNIT_TEST(testPolyPoly
);
357 CPPUNIT_TEST(testPolyPolyClip
);
358 CPPUNIT_TEST(testLineDrawClip
);
359 CPPUNIT_TEST(testPolyDrawClip
);
360 CPPUNIT_TEST(testPolyPolyCrissCross
);
361 CPPUNIT_TEST_SUITE_END();
364 CPPUNIT_TEST_SUITE_REGISTRATION(PolyTest
);
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */