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/b2drange.hxx>
26 : #include <basegfx/point/b2ipoint.hxx>
27 : #include <basegfx/polygon/b2dpolygon.hxx>
28 : #include <basegfx/polygon/b2dpolygontools.hxx>
29 : #include <basegfx/polygon/b2dpolypolygon.hxx>
30 : #include <basegfx/polygon/b2dpolypolygontools.hxx>
31 :
32 : #include <basebmp/color.hxx>
33 : #include <basebmp/scanlineformats.hxx>
34 : #include <basebmp/bitmapdevice.hxx>
35 : #include "tools.hxx"
36 :
37 : using namespace ::basebmp;
38 :
39 : namespace
40 : {
41 9 : class FillTest : public CppUnit::TestFixture
42 : {
43 : private:
44 : BitmapDeviceSharedPtr mpDevice1bpp;
45 : BitmapDeviceSharedPtr mpDevice32bpp;
46 :
47 2 : void implTestRectFill(const BitmapDeviceSharedPtr& rDevice)
48 : {
49 2 : rDevice->clear(Color(0));
50 :
51 2 : const basegfx::B2DRange aRect(1,1,10,10);
52 :
53 2 : const Color aCol(0xFFFFFFFF);
54 : rDevice->fillPolyPolygon(
55 : basegfx::B2DPolyPolygon(
56 : basegfx::tools::createPolygonFromRect( aRect )),
57 : aCol,
58 2 : DrawMode_PAINT );
59 :
60 2 : const basegfx::B2IPoint aPt1(1,1);
61 4 : CPPUNIT_ASSERT_MESSAGE("first pixel set",
62 2 : rDevice->getPixel(aPt1) == aCol);
63 4 : const basegfx::B2IPoint aPt2(9,9);
64 4 : CPPUNIT_ASSERT_MESSAGE("last pixel set",
65 2 : rDevice->getPixel(aPt2) == aCol);
66 4 : const basegfx::B2IPoint aPt3(0,0);
67 4 : CPPUNIT_ASSERT_MESSAGE("topmost pixel not set",
68 2 : rDevice->getPixel(aPt3) != aCol);
69 4 : const basegfx::B2IPoint aPt4(10,10);
70 4 : CPPUNIT_ASSERT_MESSAGE("bottommost pixel not set",
71 2 : rDevice->getPixel(aPt4) != aCol);
72 :
73 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 81",
74 4 : countPixel( rDevice, aCol ) == 81);
75 2 : }
76 :
77 2 : void implTestCornerCases(const BitmapDeviceSharedPtr& rDevice)
78 : {
79 2 : rDevice->clear(Color(0));
80 :
81 2 : const basegfx::B2DRange aEmpty1(0,0,0,11);
82 2 : const basegfx::B2DRange aEmpty2(0,0,11,0);
83 2 : const basegfx::B2DRange aVertLineLeft(0,0,1,11);
84 2 : const basegfx::B2DRange aVertLineRight(10,0,11,11);
85 2 : const basegfx::B2DRange aHorzLineTop(0,0,11,1);
86 2 : const basegfx::B2DRange aHorzLineBottom(0,10,11,11);
87 :
88 2 : const Color aCol(0xFFFFFFFF);
89 : rDevice->fillPolyPolygon(
90 : basegfx::B2DPolyPolygon(
91 : basegfx::tools::createPolygonFromRect( aEmpty1 )),
92 : aCol,
93 2 : DrawMode_PAINT );
94 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 0",
95 2 : countPixel( rDevice, aCol ) == 0);
96 :
97 : rDevice->fillPolyPolygon(
98 : basegfx::B2DPolyPolygon(
99 : basegfx::tools::createPolygonFromRect( aEmpty2 )),
100 : aCol,
101 2 : DrawMode_PAINT );
102 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 0",
103 2 : countPixel( rDevice, aCol ) == 0);
104 :
105 : rDevice->fillPolyPolygon(
106 : basegfx::B2DPolyPolygon(
107 : basegfx::tools::createPolygonFromRect( aVertLineLeft )),
108 : aCol,
109 2 : DrawMode_PAINT );
110 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 11",
111 2 : countPixel( rDevice, aCol ) == 11);
112 2 : const basegfx::B2IPoint aPt1(0,0);
113 4 : CPPUNIT_ASSERT_MESSAGE("first pixel set",
114 2 : rDevice->getPixel(aPt1) == aCol);
115 :
116 : rDevice->fillPolyPolygon(
117 : basegfx::B2DPolyPolygon(
118 : basegfx::tools::createPolygonFromRect( aVertLineRight )),
119 : aCol,
120 2 : DrawMode_PAINT );
121 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 22",
122 2 : countPixel( rDevice, aCol ) == 22);
123 4 : const basegfx::B2IPoint aPt2(10,10);
124 4 : CPPUNIT_ASSERT_MESSAGE("last pixel set",
125 2 : rDevice->getPixel(aPt2) == aCol);
126 :
127 : rDevice->fillPolyPolygon(
128 : basegfx::B2DPolyPolygon(
129 : basegfx::tools::createPolygonFromRect( aHorzLineTop )),
130 : aCol,
131 2 : DrawMode_PAINT );
132 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 31",
133 2 : countPixel( rDevice, aCol ) == 31);
134 4 : const basegfx::B2IPoint aPt3(5,0);
135 4 : CPPUNIT_ASSERT_MESSAGE("top-middle pixel set",
136 2 : rDevice->getPixel(aPt3) == aCol);
137 :
138 : rDevice->fillPolyPolygon(
139 : basegfx::B2DPolyPolygon(
140 : basegfx::tools::createPolygonFromRect( aHorzLineBottom )),
141 : aCol,
142 2 : DrawMode_PAINT );
143 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 40",
144 2 : countPixel( rDevice, aCol ) == 40);
145 4 : const basegfx::B2IPoint aPt4(5,10);
146 4 : CPPUNIT_ASSERT_MESSAGE("bottom-middle pixel set",
147 2 : rDevice->getPixel(aPt4) == aCol);
148 :
149 4 : OUString aSvg( "m 0 0l7 7h-1z" );
150 :
151 4 : basegfx::B2DPolyPolygon aPoly;
152 2 : basegfx::tools::importFromSvgD( aPoly, aSvg );
153 : rDevice->fillPolyPolygon(
154 : aPoly,
155 : aCol,
156 2 : DrawMode_PAINT );
157 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 43",
158 4 : countPixel( rDevice, aCol ) == 43);
159 2 : }
160 :
161 2 : void implTestClipping(const BitmapDeviceSharedPtr& rDevice)
162 : {
163 2 : rDevice->clear(Color(0));
164 :
165 2 : const basegfx::B2DRange aLeftTop(-10,-10,1,1);
166 2 : const basegfx::B2DRange aRightTop(10,-10,20,1);
167 2 : const basegfx::B2DRange aLeftBottom(-10,10,1,20);
168 2 : const basegfx::B2DRange aRightBottom(10,10,20,20);
169 2 : const basegfx::B2DRange aAllOver(-10,-10,20,20);
170 :
171 2 : const Color aCol(0xFFFFFFFF);
172 : rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
173 : basegfx::tools::createPolygonFromRect(aLeftTop)),
174 : aCol,
175 2 : DrawMode_PAINT );
176 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 1",
177 2 : countPixel( rDevice, aCol ) == 1);
178 :
179 : rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
180 : basegfx::tools::createPolygonFromRect(aRightTop)),
181 : aCol,
182 2 : DrawMode_PAINT );
183 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 2",
184 2 : countPixel( rDevice, aCol ) == 2);
185 :
186 : rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
187 : basegfx::tools::createPolygonFromRect(aLeftBottom)),
188 : aCol,
189 2 : DrawMode_PAINT );
190 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 3",
191 2 : countPixel( rDevice, aCol ) == 3);
192 :
193 : rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
194 : basegfx::tools::createPolygonFromRect(aRightBottom)),
195 : aCol,
196 2 : DrawMode_PAINT );
197 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 4",
198 2 : countPixel( rDevice, aCol ) == 4);
199 :
200 : rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
201 : basegfx::tools::createPolygonFromRect(aAllOver)),
202 : aCol,
203 2 : DrawMode_PAINT );
204 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 121",
205 2 : countPixel( rDevice, aCol ) == 121);
206 2 : }
207 :
208 : public:
209 3 : void setUp()
210 : {
211 3 : const basegfx::B2ISize aSize(11,11);
212 6 : mpDevice1bpp = createBitmapDevice( aSize,
213 : true,
214 3 : Format::ONE_BIT_MSB_PAL );
215 6 : mpDevice32bpp = createBitmapDevice( aSize,
216 : true,
217 6 : Format::THIRTYTWO_BIT_TC_MASK );
218 3 : }
219 :
220 1 : void testRectFill()
221 : {
222 1 : implTestRectFill( mpDevice1bpp );
223 1 : implTestRectFill( mpDevice32bpp );
224 1 : }
225 :
226 1 : void testClipping()
227 : {
228 1 : implTestClipping( mpDevice1bpp );
229 1 : implTestClipping( mpDevice32bpp );
230 1 : }
231 :
232 1 : void testCornerCases()
233 : {
234 1 : implTestCornerCases( mpDevice1bpp );
235 1 : implTestCornerCases( mpDevice32bpp );
236 1 : }
237 :
238 : // Change the following lines only, if you add, remove or rename
239 : // member functions of the current class,
240 : // because these macros are need by auto register mechanism.
241 :
242 2 : CPPUNIT_TEST_SUITE(FillTest);
243 1 : CPPUNIT_TEST(testRectFill);
244 1 : CPPUNIT_TEST(testClipping);
245 1 : CPPUNIT_TEST(testCornerCases);
246 2 : CPPUNIT_TEST_SUITE_END();
247 : };
248 :
249 1 : CPPUNIT_TEST_SUITE_REGISTRATION(FillTest);
250 3 : }
251 :
252 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|