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/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>
33 #include <basebmp/color.hxx>
34 #include <basebmp/scanlineformats.hxx>
35 #include <basebmp/bitmapdevice.hxx>
38 using namespace ::basebmp
;
42 class ClipTest
: public CppUnit::TestFixture
45 BitmapDeviceSharedPtr mpClipMask
;
46 BitmapDeviceSharedPtr mpDevice1bpp
;
47 BitmapDeviceSharedPtr mpDevice32bpp
;
49 void implTestPixelClip(const BitmapDeviceSharedPtr
& rDevice
)
51 const Color
aBgCol(0);
52 rDevice
->clear(aBgCol
);
54 const basegfx::B2IPoint
aPt(0,0);
55 const Color
aCol(0xFFFFFFFF);
56 rDevice
->setPixel( aPt
, aCol
, DrawMode_PAINT
, mpClipMask
);
57 CPPUNIT_ASSERT_MESSAGE("get/setPixel clip #1",
58 rDevice
->getPixel(aPt
) == aBgCol
);
60 const basegfx::B2IPoint
aPt2(10,10);
61 rDevice
->setPixel( aPt2
, aCol
, DrawMode_PAINT
, mpClipMask
);
62 CPPUNIT_ASSERT_MESSAGE("get/setPixel clip #2",
63 rDevice
->getPixel(aPt2
) == aBgCol
);
65 const basegfx::B2IPoint
aPt1(10,0);
66 rDevice
->setPixel( aPt1
, aCol
, DrawMode_PAINT
, mpClipMask
);
67 CPPUNIT_ASSERT_MESSAGE("get/setPixel clip #3",
68 rDevice
->getPixel(aPt1
) != aBgCol
);
70 const basegfx::B2IPoint
aPt3(0,10);
71 rDevice
->setPixel( aPt3
, aCol
, DrawMode_PAINT
, mpClipMask
);
72 CPPUNIT_ASSERT_MESSAGE("get/setPixel clip #4",
73 rDevice
->getPixel(aPt3
) != aBgCol
);
76 void implTestLineClip(const BitmapDeviceSharedPtr
& rDevice
)
78 const Color
aBgCol(0);
79 rDevice
->clear(aBgCol
);
81 const basegfx::B2IPoint
aPt1(0,0);
82 const basegfx::B2IPoint
aPt2(1,9);
83 const Color
aCol(0xFFFFFFFF);
84 rDevice
->drawLine( aPt1
, aPt2
, aCol
, DrawMode_PAINT
, mpClipMask
);
86 const basegfx::B2IPoint
aPt3(1,5);
87 CPPUNIT_ASSERT_MESSAGE("get line pixel",
88 rDevice
->getPixel(aPt3
) != aBgCol
);
89 CPPUNIT_ASSERT_MESSAGE("number of rendered line pixel is not 4",
91 rDevice
->getPixel(aPt3
) ) == 4);
93 rDevice
->drawLine( aPt1
, aPt2
, aCol
, DrawMode_XOR
, mpClipMask
);
94 CPPUNIT_ASSERT_MESSAGE("number of xor-rendered line pixel is not 0",
96 rDevice
->getPixel(aPt3
) ) == 121);
99 void implTestFillClip(const BitmapDeviceSharedPtr
& rDevice
)
101 rDevice
->clear(Color(0));
103 const basegfx::B2DRange
aAllOver(-10,-10,20,20);
104 const Color
aCol(0xFFFFFFFF);
105 rDevice
->fillPolyPolygon( basegfx::B2DPolyPolygon(
106 basegfx::tools::createPolygonFromRect(aAllOver
)),
110 const basegfx::B2IPoint
aPt(0,10);
111 CPPUNIT_ASSERT_MESSAGE("number of clipped pixel is not 30",
112 countPixel( rDevice
, rDevice
->getPixel(aPt
) ) == 121-30);
114 rDevice
->fillPolyPolygon( basegfx::B2DPolyPolygon(
115 basegfx::tools::createPolygonFromRect(aAllOver
)),
118 CPPUNIT_ASSERT_MESSAGE("number of filled pixel is not 121",
119 countPixel( rDevice
, rDevice
->getPixel(aPt
) ) == 121);
121 rDevice
->fillPolyPolygon( basegfx::B2DPolyPolygon(
122 basegfx::tools::createPolygonFromRect(aAllOver
)),
126 CPPUNIT_ASSERT_MESSAGE("number of xor-cleared pixel is not 91",
127 countPixel( rDevice
, rDevice
->getPixel(aPt
) ) == 121-30);
130 void implTestBmpClip(const BitmapDeviceSharedPtr
& rDevice
)
132 BitmapDeviceSharedPtr
pBmp( cloneBitmapDevice(
133 basegfx::B2IVector(3,3),
136 Color
aCol2(0xFFFFFFFF);
138 pBmp
->setPixel(basegfx::B2IPoint(0,0),aCol2
,DrawMode_PAINT
);
139 pBmp
->setPixel(basegfx::B2IPoint(1,1),aCol2
,DrawMode_PAINT
);
140 pBmp
->setPixel(basegfx::B2IPoint(2,2),aCol2
,basebmp::DrawMode_PAINT
);
142 rDevice
->clear(aCol1
);
143 rDevice
->drawBitmap(pBmp
,
144 basegfx::B2IBox(0,0,3,3),
145 basegfx::B2IBox(-1,-1,4,4),
149 const basegfx::B2IPoint
aPt(1,1);
150 CPPUNIT_ASSERT_MESSAGE("number of clipped pixel is not 5",
152 rDevice
->getPixel(aPt
) ) == 5);
155 void implTestMaskColorClip(const BitmapDeviceSharedPtr
& rDevice
)
157 sal_Int32 nStride
= basebmp::getBitmapDeviceStrideForWidth(FORMAT_EIGHT_BIT_GREY
, rDevice
->getSize().getX());
158 BitmapDeviceSharedPtr
pBmp( createBitmapDevice( rDevice
->getSize(),
160 FORMAT_EIGHT_BIT_GREY
, nStride
));
162 OUString
aSvg( "m 0 0h5v10h5v-5h-10z" );
164 basegfx::B2DPolyPolygon aPoly
;
165 basegfx::tools::importFromSvgD( aPoly
, aSvg
, false, NULL
);
166 const basebmp::Color
aCol(0xFF);
167 pBmp
->clear( basebmp::Color(0) );
168 pBmp
->fillPolyPolygon(
171 basebmp::DrawMode_PAINT
);
173 const basegfx::B2IBox
aSourceRect(0,0,10,10);
174 const basegfx::B2IPoint
aDestLeftTop(0,0);
175 const Color
aCol2(0xF0F0F0F0);
176 rDevice
->drawMaskedColor(
182 const basegfx::B2IPoint
aPt(1,1);
183 CPPUNIT_ASSERT_MESSAGE("number of rendered pixel is not 41",
184 countPixel( rDevice
, rDevice
->getPixel(aPt
) ) == 41);
189 void setUp() SAL_OVERRIDE
191 const basegfx::B2ISize
aSize(11,11);
192 sal_Int32 nStride
= basebmp::getBitmapDeviceStrideForWidth(FORMAT_ONE_BIT_MSB_GREY
, aSize
.getX());
193 mpClipMask
= createBitmapDevice( aSize
,
195 FORMAT_ONE_BIT_MSB_GREY
, nStride
);
196 nStride
= basebmp::getBitmapDeviceStrideForWidth(FORMAT_ONE_BIT_MSB_PAL
, aSize
.getX());
197 mpDevice1bpp
= createBitmapDevice( aSize
,
199 FORMAT_ONE_BIT_MSB_PAL
, nStride
);
200 nStride
= basebmp::getBitmapDeviceStrideForWidth(FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA
, aSize
.getX());
201 mpDevice32bpp
= createBitmapDevice( aSize
,
203 FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA
, nStride
);
205 OUString
aSvg( "m 0 0 h5 l5 5 v5 h-5 l-5-5 z" );
206 basegfx::B2DPolyPolygon aPoly
;
207 basegfx::tools::importFromSvgD( aPoly
, aSvg
, false, NULL
);
208 mpClipMask
->clear(Color(0));
209 mpClipMask
->drawPolygon(
210 aPoly
.getB2DPolygon(0),
217 implTestPixelClip( mpDevice1bpp
);
218 implTestPixelClip( mpDevice32bpp
);
223 implTestLineClip( mpDevice1bpp
);
224 implTestLineClip( mpDevice32bpp
);
229 implTestFillClip( mpDevice1bpp
);
230 implTestFillClip( mpDevice32bpp
);
235 implTestBmpClip( mpDevice1bpp
);
236 implTestBmpClip( mpDevice32bpp
);
239 void testMaskColorClip()
241 implTestMaskColorClip( mpDevice1bpp
);
242 implTestMaskColorClip( mpDevice32bpp
);
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.
249 CPPUNIT_TEST_SUITE(ClipTest
);
250 CPPUNIT_TEST(testPixelClip
);
251 CPPUNIT_TEST(testLineClip
);
252 CPPUNIT_TEST(testFillClip
);
253 CPPUNIT_TEST(testBmpClip
);
254 CPPUNIT_TEST(testMaskColorClip
);
255 CPPUNIT_TEST_SUITE_END();
258 CPPUNIT_TEST_SUITE_REGISTRATION(ClipTest
);
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */