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/point/b2ipoint.hxx>
26 : #include <basegfx/range/b2drange.hxx>
27 : #include <basegfx/range/b2ibox.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 30 : class ClipTest : public CppUnit::TestFixture
43 : {
44 : private:
45 : BitmapDeviceSharedPtr mpClipMask;
46 : BitmapDeviceSharedPtr mpDevice1bpp;
47 : BitmapDeviceSharedPtr mpDevice32bpp;
48 :
49 4 : void implTestPixelClip(const BitmapDeviceSharedPtr& rDevice)
50 : {
51 4 : const Color aBgCol(0);
52 4 : rDevice->clear(aBgCol);
53 :
54 4 : const basegfx::B2IPoint aPt(0,0);
55 4 : const Color aCol(0xFFFFFFFF);
56 4 : rDevice->setPixel( aPt, aCol, DrawMode_PAINT, mpClipMask );
57 8 : CPPUNIT_ASSERT_MESSAGE("get/setPixel clip #1",
58 4 : rDevice->getPixel(aPt) == aBgCol);
59 :
60 8 : const basegfx::B2IPoint aPt2(10,10);
61 4 : rDevice->setPixel( aPt2, aCol, DrawMode_PAINT, mpClipMask );
62 8 : CPPUNIT_ASSERT_MESSAGE("get/setPixel clip #2",
63 4 : rDevice->getPixel(aPt2) == aBgCol);
64 :
65 8 : const basegfx::B2IPoint aPt1(10,0);
66 4 : rDevice->setPixel( aPt1, aCol, DrawMode_PAINT, mpClipMask );
67 8 : CPPUNIT_ASSERT_MESSAGE("get/setPixel clip #3",
68 4 : rDevice->getPixel(aPt1) != aBgCol);
69 :
70 8 : const basegfx::B2IPoint aPt3(0,10);
71 4 : rDevice->setPixel( aPt3, aCol, DrawMode_PAINT, mpClipMask );
72 8 : CPPUNIT_ASSERT_MESSAGE("get/setPixel clip #4",
73 8 : rDevice->getPixel(aPt3) != aBgCol);
74 4 : }
75 :
76 4 : void implTestLineClip(const BitmapDeviceSharedPtr& rDevice)
77 : {
78 4 : const Color aBgCol(0);
79 4 : rDevice->clear(aBgCol);
80 :
81 4 : const basegfx::B2IPoint aPt1(0,0);
82 8 : const basegfx::B2IPoint aPt2(1,9);
83 4 : const Color aCol(0xFFFFFFFF);
84 4 : rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT, mpClipMask );
85 :
86 8 : const basegfx::B2IPoint aPt3(1,5);
87 8 : CPPUNIT_ASSERT_MESSAGE("get line pixel",
88 4 : rDevice->getPixel(aPt3) != aBgCol);
89 8 : CPPUNIT_ASSERT_MESSAGE("number of rendered line pixel is not 4",
90 : countPixel( rDevice,
91 4 : rDevice->getPixel(aPt3) ) == 4);
92 :
93 4 : rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_XOR, mpClipMask );
94 8 : CPPUNIT_ASSERT_MESSAGE("number of xor-rendered line pixel is not 0",
95 : countPixel( rDevice,
96 8 : rDevice->getPixel(aPt3) ) == 121);
97 4 : }
98 :
99 4 : void implTestFillClip(const BitmapDeviceSharedPtr& rDevice)
100 : {
101 4 : rDevice->clear(Color(0));
102 :
103 4 : const basegfx::B2DRange aAllOver(-10,-10,20,20);
104 4 : const Color aCol(0xFFFFFFFF);
105 : rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
106 : basegfx::tools::createPolygonFromRect(aAllOver)),
107 : aCol,
108 : DrawMode_PAINT,
109 4 : mpClipMask );
110 4 : const basegfx::B2IPoint aPt(0,10);
111 8 : CPPUNIT_ASSERT_MESSAGE("number of clipped pixel is not 30",
112 4 : countPixel( rDevice, rDevice->getPixel(aPt) ) == 121-30);
113 :
114 : rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
115 : basegfx::tools::createPolygonFromRect(aAllOver)),
116 : aCol,
117 4 : DrawMode_PAINT );
118 8 : CPPUNIT_ASSERT_MESSAGE("number of filled pixel is not 121",
119 4 : countPixel( rDevice, rDevice->getPixel(aPt) ) == 121);
120 :
121 : rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
122 : basegfx::tools::createPolygonFromRect(aAllOver)),
123 : aCol,
124 : DrawMode_XOR,
125 4 : mpClipMask );
126 8 : CPPUNIT_ASSERT_MESSAGE("number of xor-cleared pixel is not 91",
127 8 : countPixel( rDevice, rDevice->getPixel(aPt) ) == 121-30);
128 4 : }
129 :
130 4 : void implTestBmpClip(const BitmapDeviceSharedPtr& rDevice)
131 : {
132 : BitmapDeviceSharedPtr pBmp( cloneBitmapDevice(
133 : basegfx::B2IVector(3,3),
134 4 : rDevice ));
135 4 : Color aCol1(0);
136 4 : Color aCol2(0xFFFFFFFF);
137 4 : pBmp->clear(aCol1);
138 4 : pBmp->setPixel(basegfx::B2IPoint(0,0),aCol2,DrawMode_PAINT);
139 4 : pBmp->setPixel(basegfx::B2IPoint(1,1),aCol2,DrawMode_PAINT);
140 4 : pBmp->setPixel(basegfx::B2IPoint(2,2),aCol2,basebmp::DrawMode_PAINT);
141 :
142 4 : rDevice->clear(aCol1);
143 : rDevice->drawBitmap(pBmp,
144 : basegfx::B2IBox(0,0,3,3),
145 : basegfx::B2IBox(-1,-1,4,4),
146 : DrawMode_PAINT,
147 4 : mpClipMask);
148 :
149 8 : const basegfx::B2IPoint aPt(1,1);
150 8 : CPPUNIT_ASSERT_MESSAGE("number of clipped pixel is not 5",
151 : countPixel( rDevice,
152 8 : rDevice->getPixel(aPt) ) == 5);
153 4 : }
154 :
155 4 : void implTestMaskColorClip(const BitmapDeviceSharedPtr& rDevice)
156 : {
157 : BitmapDeviceSharedPtr pBmp( createBitmapDevice( rDevice->getSize(),
158 : true,
159 4 : FORMAT_EIGHT_BIT_GREY ));
160 :
161 8 : OUString aSvg( "m 0 0h5v10h5v-5h-10z" );
162 :
163 8 : basegfx::B2DPolyPolygon aPoly;
164 4 : basegfx::tools::importFromSvgD( aPoly, aSvg, false, NULL );
165 4 : const basebmp::Color aCol(0xFF);
166 4 : pBmp->clear( basebmp::Color(0) );
167 : pBmp->fillPolyPolygon(
168 : aPoly,
169 : aCol,
170 4 : basebmp::DrawMode_PAINT );
171 :
172 4 : const basegfx::B2IBox aSourceRect(0,0,10,10);
173 8 : const basegfx::B2IPoint aDestLeftTop(0,0);
174 4 : const Color aCol2(0xF0F0F0F0);
175 : rDevice->drawMaskedColor(
176 : aCol2,
177 : pBmp,
178 : aSourceRect,
179 : aDestLeftTop,
180 4 : mpClipMask );
181 8 : const basegfx::B2IPoint aPt(1,1);
182 8 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 41",
183 8 : countPixel( rDevice, rDevice->getPixel(aPt) ) == 41);
184 :
185 4 : }
186 :
187 : public:
188 10 : void setUp() SAL_OVERRIDE
189 : {
190 10 : const basegfx::B2ISize aSize(11,11);
191 20 : mpClipMask = createBitmapDevice( aSize,
192 : true,
193 10 : FORMAT_ONE_BIT_MSB_GREY );
194 20 : mpDevice1bpp = createBitmapDevice( aSize,
195 : true,
196 10 : FORMAT_ONE_BIT_MSB_PAL );
197 20 : mpDevice32bpp = createBitmapDevice( aSize,
198 : true,
199 10 : FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA );
200 :
201 20 : OUString aSvg( "m 0 0 h5 l5 5 v5 h-5 l-5-5 z" );
202 20 : basegfx::B2DPolyPolygon aPoly;
203 10 : basegfx::tools::importFromSvgD( aPoly, aSvg, false, NULL );
204 10 : mpClipMask->clear(Color(0));
205 : mpClipMask->drawPolygon(
206 : aPoly.getB2DPolygon(0),
207 : Color(0xFFFFFFFF),
208 20 : DrawMode_PAINT );
209 10 : }
210 :
211 2 : void testPixelClip()
212 : {
213 2 : implTestPixelClip( mpDevice1bpp );
214 2 : implTestPixelClip( mpDevice32bpp );
215 2 : }
216 :
217 2 : void testLineClip()
218 : {
219 2 : implTestLineClip( mpDevice1bpp );
220 2 : implTestLineClip( mpDevice32bpp );
221 2 : }
222 :
223 2 : void testFillClip()
224 : {
225 2 : implTestFillClip( mpDevice1bpp );
226 2 : implTestFillClip( mpDevice32bpp );
227 2 : }
228 :
229 2 : void testBmpClip()
230 : {
231 2 : implTestBmpClip( mpDevice1bpp );
232 2 : implTestBmpClip( mpDevice32bpp );
233 2 : }
234 :
235 2 : void testMaskColorClip()
236 : {
237 2 : implTestMaskColorClip( mpDevice1bpp );
238 2 : implTestMaskColorClip( mpDevice32bpp );
239 2 : }
240 :
241 : // Change the following lines only, if you add, remove or rename
242 : // member functions of the current class,
243 : // because these macros are need by auto register mechanism.
244 :
245 4 : CPPUNIT_TEST_SUITE(ClipTest);
246 2 : CPPUNIT_TEST(testPixelClip);
247 2 : CPPUNIT_TEST(testLineClip);
248 2 : CPPUNIT_TEST(testFillClip);
249 2 : CPPUNIT_TEST(testBmpClip);
250 2 : CPPUNIT_TEST(testMaskColorClip);
251 4 : CPPUNIT_TEST_SUITE_END();
252 : };
253 :
254 2 : CPPUNIT_TEST_SUITE_REGISTRATION(ClipTest);
255 6 : }
256 :
257 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|