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 2 : sal_Int32 nStride = basebmp::getBitmapDeviceStrideForWidth(Format::EightBitGrey, rDevice->getSize().getX());
158 : BitmapDeviceSharedPtr pBmp( createBitmapDevice( rDevice->getSize(),
159 : true,
160 2 : Format::EightBitGrey, nStride ));
161 :
162 4 : OUString aSvg( "m 0 0h5v10h5v-5h-10z" );
163 :
164 4 : basegfx::B2DPolyPolygon aPoly;
165 2 : basegfx::tools::importFromSvgD( aPoly, aSvg, false, NULL );
166 2 : const basebmp::Color aCol(0xFF);
167 2 : pBmp->clear( basebmp::Color(0) );
168 : pBmp->fillPolyPolygon(
169 : aPoly,
170 : aCol,
171 2 : basebmp::DrawMode::Paint );
172 :
173 2 : const basegfx::B2IBox aSourceRect(0,0,10,10);
174 4 : const basegfx::B2IPoint aDestLeftTop(0,0);
175 2 : const Color aCol2(0xF0F0F0F0);
176 : rDevice->drawMaskedColor(
177 : aCol2,
178 : pBmp,
179 : aSourceRect,
180 : aDestLeftTop,
181 2 : mpClipMask );
182 4 : const basegfx::B2IPoint aPt(1,1);
183 4 : CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 41",
184 4 : countPixel( rDevice, rDevice->getPixel(aPt) ) == 41);
185 :
186 2 : }
187 :
188 : public:
189 5 : void setUp() SAL_OVERRIDE
190 : {
191 5 : const basegfx::B2ISize aSize(11,11);
192 5 : sal_Int32 nStride = basebmp::getBitmapDeviceStrideForWidth(Format::OneBitMsbGrey, aSize.getX());
193 10 : mpClipMask = createBitmapDevice( aSize,
194 : true,
195 5 : Format::OneBitMsbGrey, nStride );
196 5 : nStride = basebmp::getBitmapDeviceStrideForWidth(Format::OneBitMsbPal, aSize.getX());
197 10 : mpDevice1bpp = createBitmapDevice( aSize,
198 : true,
199 5 : Format::OneBitMsbPal, nStride );
200 5 : nStride = basebmp::getBitmapDeviceStrideForWidth(Format::ThirtyTwoBitTcMaskBGRA, aSize.getX());
201 10 : mpDevice32bpp = createBitmapDevice( aSize,
202 : true,
203 5 : Format::ThirtyTwoBitTcMaskBGRA, nStride );
204 :
205 10 : OUString aSvg( "m 0 0 h5 l5 5 v5 h-5 l-5-5 z" );
206 10 : basegfx::B2DPolyPolygon aPoly;
207 5 : basegfx::tools::importFromSvgD( aPoly, aSvg, false, NULL );
208 5 : mpClipMask->clear(Color(0));
209 : mpClipMask->drawPolygon(
210 : aPoly.getB2DPolygon(0),
211 : Color(0xFFFFFFFF),
212 10 : DrawMode::Paint );
213 5 : }
214 :
215 1 : void testPixelClip()
216 : {
217 1 : implTestPixelClip( mpDevice1bpp );
218 1 : implTestPixelClip( mpDevice32bpp );
219 1 : }
220 :
221 1 : void testLineClip()
222 : {
223 1 : implTestLineClip( mpDevice1bpp );
224 1 : implTestLineClip( mpDevice32bpp );
225 1 : }
226 :
227 1 : void testFillClip()
228 : {
229 1 : implTestFillClip( mpDevice1bpp );
230 1 : implTestFillClip( mpDevice32bpp );
231 1 : }
232 :
233 1 : void testBmpClip()
234 : {
235 1 : implTestBmpClip( mpDevice1bpp );
236 1 : implTestBmpClip( mpDevice32bpp );
237 1 : }
238 :
239 1 : void testMaskColorClip()
240 : {
241 1 : implTestMaskColorClip( mpDevice1bpp );
242 1 : implTestMaskColorClip( mpDevice32bpp );
243 1 : }
244 :
245 : // Change the following lines only, if you add, remove or rename
246 : // member functions of the current class,
247 : // because these macros are need by auto register mechanism.
248 :
249 2 : CPPUNIT_TEST_SUITE(ClipTest);
250 1 : CPPUNIT_TEST(testPixelClip);
251 1 : CPPUNIT_TEST(testLineClip);
252 1 : CPPUNIT_TEST(testFillClip);
253 1 : CPPUNIT_TEST(testBmpClip);
254 1 : CPPUNIT_TEST(testMaskColorClip);
255 5 : CPPUNIT_TEST_SUITE_END();
256 : };
257 :
258 1 : CPPUNIT_TEST_SUITE_REGISTRATION(ClipTest);
259 3 : }
260 :
261 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|