xref: /trunk/main/basebmp/test/cliptest.cxx (revision 97c63c1b)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // autogenerated file with codegen.pl
25 
26 #include "preextstl.h"
27 #include "gtest/gtest.h"
28 #include "postextstl.h"
29 
30 #include <basegfx/vector/b2isize.hxx>
31 #include <basegfx/point/b2ipoint.hxx>
32 #include <basegfx/range/b2drange.hxx>
33 #include <basegfx/range/b2irange.hxx>
34 #include <basegfx/polygon/b2dpolygon.hxx>
35 #include <basegfx/polygon/b2dpolygontools.hxx>
36 #include <basegfx/polygon/b2dpolypolygon.hxx>
37 #include <basegfx/polygon/b2dpolypolygontools.hxx>
38 
39 #include <basebmp/color.hxx>
40 #include <basebmp/scanlineformats.hxx>
41 #include <basebmp/bitmapdevice.hxx>
42 #include <basebmp/debug.hxx>
43 #include "tools.hxx"
44 
45 #include <iostream>
46 #include <fstream>
47 
48 using namespace ::basebmp;
49 
50 namespace
51 {
52 /*
53   std::ofstream output("32bpp_test.dump");
54   debugDump( mpDevice32bpp, output );
55 */
56 
57 class ClipTest : public ::testing::Test
58 {
59 protected:
60     BitmapDeviceSharedPtr mpClipMask;
61     BitmapDeviceSharedPtr mpDevice1bpp;
62     BitmapDeviceSharedPtr mpDevice32bpp;
63 
implTestPixelClip(const BitmapDeviceSharedPtr & rDevice)64     void implTestPixelClip(const BitmapDeviceSharedPtr& rDevice)
65     {
66         const Color aBgCol(0);
67         rDevice->clear(aBgCol);
68 
69         const basegfx::B2IPoint aPt(0,0);
70         const Color aCol(0xFFFFFFFF);
71         rDevice->setPixel( aPt, aCol, DrawMode_PAINT, mpClipMask );
72         ASSERT_TRUE(rDevice->getPixel(aPt) == aBgCol) << "get/setPixel clip #1";
73 
74         const basegfx::B2IPoint aPt2(10,10);
75         rDevice->setPixel( aPt2, aCol, DrawMode_PAINT, mpClipMask );
76         ASSERT_TRUE(rDevice->getPixel(aPt2) == aBgCol) << "get/setPixel clip #2";
77 
78         const basegfx::B2IPoint aPt1(10,0);
79         rDevice->setPixel( aPt1, aCol, DrawMode_PAINT, mpClipMask );
80         ASSERT_TRUE(rDevice->getPixel(aPt1) != aBgCol) << "get/setPixel clip #3";
81 
82         const basegfx::B2IPoint aPt3(0,10);
83         rDevice->setPixel( aPt3, aCol, DrawMode_PAINT, mpClipMask );
84         ASSERT_TRUE(rDevice->getPixel(aPt3) != aBgCol) << "get/setPixel clip #4";
85     }
86 
implTestLineClip(const BitmapDeviceSharedPtr & rDevice)87     void implTestLineClip(const BitmapDeviceSharedPtr& rDevice)
88     {
89         const Color aBgCol(0);
90         rDevice->clear(aBgCol);
91 
92         const basegfx::B2IPoint aPt1(0,0);
93         const basegfx::B2IPoint aPt2(1,9);
94         const Color aCol(0xFFFFFFFF);
95         rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_PAINT, mpClipMask );
96 
97         const basegfx::B2IPoint aPt3(1,5);
98         ASSERT_TRUE(rDevice->getPixel(aPt3) != aBgCol) << "get line pixel";
99         ASSERT_TRUE(countPixel( rDevice, rDevice->getPixel(aPt3) ) == 4)
100             << "number of rendered line pixel is not 4";
101 
102         rDevice->drawLine( aPt1, aPt2, aCol, DrawMode_XOR, mpClipMask );
103         ASSERT_TRUE(countPixel( rDevice, rDevice->getPixel(aPt3) ) == 121)
104             << "number of xor-rendered line pixel is not 0";
105     }
106 
implTestFillClip(const BitmapDeviceSharedPtr & rDevice)107     void implTestFillClip(const BitmapDeviceSharedPtr& rDevice)
108     {
109         rDevice->clear(Color(0));
110 
111         const basegfx::B2DRange aAllOver(-10,-10,20,20);
112         const Color aCol(0xFFFFFFFF);
113         rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
114                                       basegfx::tools::createPolygonFromRect(aAllOver)),
115                                   aCol,
116                                   DrawMode_PAINT,
117                                   mpClipMask );
118         const basegfx::B2IPoint aPt(0,10);
119         ASSERT_TRUE(countPixel( rDevice, rDevice->getPixel(aPt) ) == 121-30)
120             << "number of clipped pixel is not 30";
121 
122         rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
123                                       basegfx::tools::createPolygonFromRect(aAllOver)),
124                                   aCol,
125                                   DrawMode_PAINT );
126         ASSERT_TRUE(countPixel( rDevice, rDevice->getPixel(aPt) ) == 121)
127             << "number of filled pixel is not 121";
128 
129         rDevice->fillPolyPolygon( basegfx::B2DPolyPolygon(
130                                       basegfx::tools::createPolygonFromRect(aAllOver)),
131                                   aCol,
132                                   DrawMode_XOR,
133                                   mpClipMask );
134         ASSERT_TRUE(countPixel( rDevice, rDevice->getPixel(aPt) ) == 121-30)
135             << "number of xor-cleared pixel is not 91";
136     }
137 
implTestBmpClip(const BitmapDeviceSharedPtr & rDevice)138     void implTestBmpClip(const BitmapDeviceSharedPtr& rDevice)
139     {
140         BitmapDeviceSharedPtr pBmp( cloneBitmapDevice(
141                                         basegfx::B2IVector(3,3),
142                                         rDevice ));
143         Color aCol1(0);
144         Color aCol2(0xFFFFFFFF);
145         pBmp->clear(aCol1);
146         pBmp->setPixel(basegfx::B2IPoint(0,0),aCol2,DrawMode_PAINT);
147         pBmp->setPixel(basegfx::B2IPoint(1,1),aCol2,DrawMode_PAINT);
148         pBmp->setPixel(basegfx::B2IPoint(2,2),aCol2,basebmp::DrawMode_PAINT);
149 
150         rDevice->clear(aCol1);
151         rDevice->drawBitmap(pBmp,
152                             basegfx::B2IRange(0,0,3,3),
153                             basegfx::B2IRange(-1,-1,4,4),
154                             DrawMode_PAINT,
155                             mpClipMask);
156 
157         const basegfx::B2IPoint aPt(1,1);
158         ASSERT_TRUE(countPixel( rDevice, rDevice->getPixel(aPt) ) == 5)
159             << "number of clipped pixel is not 5";
160     }
161 
implTestMaskColorClip(const BitmapDeviceSharedPtr & rDevice)162     void implTestMaskColorClip(const BitmapDeviceSharedPtr& rDevice)
163     {
164         BitmapDeviceSharedPtr pBmp( createBitmapDevice( rDevice->getSize(),
165                                                         true,
166                                                         Format::EIGHT_BIT_GREY ));
167 
168         ::rtl::OUString aSvg = ::rtl::OUString::createFromAscii(
169             "m 0 0h5v10h5v-5h-10z" );
170 
171         basegfx::B2DPolyPolygon aPoly;
172         basegfx::tools::importFromSvgD( aPoly, aSvg, false, NULL );
173         const basebmp::Color aCol(0xFF);
174         pBmp->clear( basebmp::Color(0) );
175         pBmp->fillPolyPolygon(
176             aPoly,
177             aCol,
178             basebmp::DrawMode_PAINT );
179 
180         const basegfx::B2IRange aSourceRect(0,0,10,10);
181         const basegfx::B2IPoint aDestLeftTop(0,0);
182         const Color aCol2(0xF0F0F0F0);
183         rDevice->drawMaskedColor(
184             aCol2,
185             pBmp,
186             aSourceRect,
187             aDestLeftTop,
188             mpClipMask );
189         const basegfx::B2IPoint aPt(1,1);
190         ASSERT_TRUE(countPixel( rDevice, rDevice->getPixel(aPt) ) == 41)
191             << "number of rendered pixel is not 41";
192 
193     }
194 
195 public:
SetUp()196     virtual void SetUp()
197     {
198         const basegfx::B2ISize aSize(11,11);
199         mpClipMask = createBitmapDevice( aSize,
200                                          true,
201                                          Format::ONE_BIT_MSB_GREY );
202         mpDevice1bpp = createBitmapDevice( aSize,
203                                            true,
204                                            Format::ONE_BIT_MSB_PAL );
205         mpDevice32bpp = createBitmapDevice( aSize,
206                                            true,
207                                            Format::THIRTYTWO_BIT_TC_MASK );
208 
209         ::rtl::OUString aSvg = ::rtl::OUString::createFromAscii(
210             "m 0 0 h5 l5 5 v5 h-5 l-5-5 z" );
211         basegfx::B2DPolyPolygon aPoly;
212         basegfx::tools::importFromSvgD( aPoly, aSvg, false, NULL );
213         mpClipMask->clear(Color(0));
214         mpClipMask->drawPolygon(
215             aPoly.getB2DPolygon(0),
216             Color(0xFFFFFFFF),
217             DrawMode_PAINT );
218     }
219 };
220 
TEST_F(ClipTest,testPixelClip)221 TEST_F(ClipTest, testPixelClip)
222 {
223     implTestPixelClip( mpDevice1bpp );
224     implTestPixelClip( mpDevice32bpp );
225 }
226 
TEST_F(ClipTest,testLineClip)227 TEST_F(ClipTest, testLineClip)
228 {
229     implTestLineClip( mpDevice1bpp );
230     implTestLineClip( mpDevice32bpp );
231 }
232 
TEST_F(ClipTest,testFillClip)233 TEST_F(ClipTest, testFillClip)
234 {
235     implTestFillClip( mpDevice1bpp );
236     implTestFillClip( mpDevice32bpp );
237 }
238 
TEST_F(ClipTest,testBmpClip)239 TEST_F(ClipTest, testBmpClip)
240 {
241     implTestBmpClip( mpDevice1bpp );
242     implTestBmpClip( mpDevice32bpp );
243 }
244 
TEST_F(ClipTest,testMaskColorClip)245 TEST_F(ClipTest, testMaskColorClip)
246 {
247     implTestMaskColorClip( mpDevice1bpp );
248     implTestMaskColorClip( mpDevice32bpp );
249 }
250 
251 
252 }
253