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