xref: /aoo4110/main/basebmp/test/basictest.cxx (revision b1cdbd2c)
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 "cppunit/TestAssert.h"
28 #include "cppunit/TestFixture.h"
29 #include "cppunit/extensions/HelperMacros.h"
30 #include "cppunit/plugin/TestPlugIn.h"
31 #include "postextstl.h"
32 
33 #include <basegfx/vector/b2isize.hxx>
34 #include <basegfx/point/b2ipoint.hxx>
35 
36 #include <basebmp/color.hxx>
37 #include <basebmp/scanlineformats.hxx>
38 #include <basebmp/bitmapdevice.hxx>
39 #include <basebmp/debug.hxx>
40 #include "tools.hxx"
41 
42 #include <iostream>
43 #include <fstream>
44 
45 using namespace ::basebmp;
46 
47 namespace
48 {
49 /*
50   std::ofstream output("32bpp_test.dump");
51   debugDump( mpDevice32bpp, output );
52 */
53 
54 class BasicTest : public CppUnit::TestFixture
55 {
56 public:
colorTest()57     void colorTest()
58     {
59         Color aTestColor;
60 
61         aTestColor = Color(0xDEADBEEF);
62         CPPUNIT_ASSERT_MESSAGE("unary constructor",
63                                aTestColor.toInt32() == 0xDEADBEEF );
64 
65         aTestColor = Color( 0x10, 0x20, 0xFF );
66         CPPUNIT_ASSERT_MESSAGE("ternary constructor",
67                                aTestColor.toInt32() == 0x001020FF );
68 
69         aTestColor.setRed( 0x0F );
70         CPPUNIT_ASSERT_MESSAGE("setRed()",
71                                aTestColor.toInt32() == 0x00F20FF );
72 
73         aTestColor.setGreen( 0x0F );
74         CPPUNIT_ASSERT_MESSAGE("setGreen()",
75                                aTestColor.toInt32() == 0x00F0FFF );
76 
77         aTestColor.setBlue( 0x10 );
78         CPPUNIT_ASSERT_MESSAGE("setBlue()",
79                                aTestColor.toInt32() == 0x00F0F10 );
80 
81         aTestColor.setGrey( 0x13 );
82         CPPUNIT_ASSERT_MESSAGE("setGrey()",
83                                aTestColor.toInt32() == 0x00131313 );
84 
85         aTestColor = Color( 0x10, 0x20, 0xFF );
86         CPPUNIT_ASSERT_MESSAGE("getRed()",
87                                aTestColor.getRed() == 0x10 );
88         CPPUNIT_ASSERT_MESSAGE("getGreen()",
89                                aTestColor.getGreen() == 0x20 );
90         CPPUNIT_ASSERT_MESSAGE("getBlue()",
91                                aTestColor.getBlue() == 0xFF );
92 
93     }
94 
testConstruction()95     void testConstruction()
96     {
97         const basegfx::B2ISize aSize(101,101);
98         basegfx::B2ISize       aSize2(aSize);
99         BitmapDeviceSharedPtr pDevice( createBitmapDevice( aSize,
100                                                            true,
101                                                            Format::ONE_BIT_MSB_PAL ));
102         CPPUNIT_ASSERT_MESSAGE("right size",
103                                pDevice->getSize() == aSize2 );
104         CPPUNIT_ASSERT_MESSAGE("Top down format",
105                                pDevice->isTopDown() == true );
106         CPPUNIT_ASSERT_MESSAGE("Scanline format",
107                                pDevice->getScanlineFormat() == Format::ONE_BIT_MSB_PAL );
108         CPPUNIT_ASSERT_MESSAGE("Scanline len",
109                                pDevice->getScanlineStride() == (aSize2.getY() + 7)/8 );
110         CPPUNIT_ASSERT_MESSAGE("Palette existence",
111                                pDevice->getPalette() );
112         CPPUNIT_ASSERT_MESSAGE("Palette entry 0 is black",
113                                (*pDevice->getPalette())[0] == Color(0) );
114         CPPUNIT_ASSERT_MESSAGE("Palette entry 1 is white",
115                                (*pDevice->getPalette())[1] == Color(0xFFFFFFFF) );
116     }
117 
testPixelFuncs()118     void testPixelFuncs()
119     {
120         // 1bpp
121         const basegfx::B2ISize aSize(64,64);
122         BitmapDeviceSharedPtr pDevice( createBitmapDevice( aSize,
123                                                            true,
124                                                            Format::ONE_BIT_MSB_PAL ));
125 
126         const basegfx::B2IPoint aPt(3,3);
127         const Color aCol(0xFFFFFFFF);
128         pDevice->setPixel( aPt, aCol, DrawMode_PAINT );
129         CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #1",
130                                pDevice->getPixel(aPt) == aCol);
131 
132         const basegfx::B2IPoint aPt2(0,0);
133         const Color aCol2(0xFFFFFFFF);
134         pDevice->setPixel( aPt2, aCol2, DrawMode_PAINT );
135         CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #2",
136                                pDevice->getPixel(aPt2) == aCol2);
137 
138         const basegfx::B2IPoint aPt3(aSize.getX()-1,aSize.getY()-1);
139         const Color aCol3(0x00000000);
140         pDevice->setPixel( aPt3, aCol3, DrawMode_PAINT );
141         CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #3",
142                                pDevice->getPixel(aPt3) == aCol3);
143 
144         pDevice->setPixel( aPt3, aCol2, DrawMode_PAINT );
145         CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #3.5",
146                                pDevice->getPixel(aPt3) == aCol2);
147 
148         const basegfx::B2IPoint aPt4(-100000,-100000);
149         pDevice->setPixel( aPt4, aCol3, DrawMode_PAINT );
150         const basegfx::B2IPoint aPt5(100000,100000);
151         pDevice->setPixel( aPt5, aCol3, DrawMode_PAINT );
152 
153         sal_Int32 nPixel(countPixel(pDevice, aCol2));
154         const basegfx::B2IPoint aPt6(aSize.getX(),aSize.getY());
155         pDevice->setPixel( aPt6, aCol2, DrawMode_PAINT );
156         CPPUNIT_ASSERT_MESSAGE("setPixel clipping",
157                                countPixel(pDevice, aCol2) == nPixel);
158 
159         CPPUNIT_ASSERT_MESSAGE("raw pixel value #1",
160                                pDevice->getBuffer()[0] == 0x80);
161 
162         // 1bit LSB
163         {
164             pDevice = createBitmapDevice( aSize,
165                                           true,
166                                           Format::ONE_BIT_LSB_PAL );
167 
168             pDevice->setPixel( aPt2, aCol, DrawMode_PAINT );
169             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #4",
170                                    pDevice->getPixel(aPt2) == aCol);
171 
172             const basegfx::B2IPoint aPt222(1,1);
173             pDevice->setPixel( aPt222, aCol, DrawMode_PAINT );
174             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #5",
175                                    pDevice->getPixel(aPt222) == aCol);
176 
177             pDevice->setPixel( aPt3, aCol, DrawMode_PAINT );
178             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #6",
179                                    pDevice->getPixel(aPt3) == aCol);
180 
181             CPPUNIT_ASSERT_MESSAGE("raw pixel value #2",
182                                    pDevice->getBuffer()[0] == 0x01);
183             CPPUNIT_ASSERT_MESSAGE("raw pixel value #3",
184                                    pDevice->getBuffer()[8] == 0x02);
185         }
186 
187         // 8bit alpha
188         {
189             pDevice = createBitmapDevice( aSize,
190                                           true,
191                                           Format::EIGHT_BIT_GREY );
192 
193             const Color aCol4(0x010101);
194             pDevice->setPixel( aPt, aCol4, DrawMode_PAINT );
195             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #4",
196                                    pDevice->getPixel(aPt) == aCol4);
197 
198             const Color aCol5(0x0F0F0F);
199             pDevice->setPixel( aPt2, aCol5, DrawMode_PAINT );
200             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #5",
201                                    pDevice->getPixel(aPt2) == aCol5);
202 
203             const Color aCol6(0xFFFFFF);
204             pDevice->setPixel( aPt3, aCol6, DrawMode_PAINT );
205             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #6",
206                                    pDevice->getPixel(aPt3) == aCol6);
207         }
208 
209         // 16bpp
210         {
211             pDevice = createBitmapDevice( aSize,
212                                           true,
213                                           Format::SIXTEEN_BIT_LSB_TC_MASK );
214             const Color aCol7(0);
215             pDevice->clear( aCol7 );
216 
217             const Color aCol4(0x00101010);
218             pDevice->setPixel( aPt, aCol4, DrawMode_PAINT );
219             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #7",
220                                    pDevice->getPixel(aPt) == aCol4);
221 
222             const Color aCol5(0x00F0F0F0);
223             pDevice->setPixel( aPt2, aCol5, DrawMode_PAINT );
224             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #8",
225                                    pDevice->getPixel(aPt2) != aCol7);
226 
227             const Color aCol6(0x00FFFFFF);
228             pDevice->setPixel( aPt3, aCol6, DrawMode_PAINT );
229             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #9",
230                                    pDevice->getPixel(aPt3) == aCol6);
231         }
232 
233         // 24bpp
234         {
235             pDevice = createBitmapDevice( aSize,
236                                           true,
237                                           Format::TWENTYFOUR_BIT_TC_MASK );
238 
239             const Color aCol4(0x01010101);
240             pDevice->setPixel( aPt, aCol4, DrawMode_PAINT );
241             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #10",
242                                    pDevice->getPixel(aPt) == aCol4);
243 
244             const Color aCol5(0x0F3F2F1F);
245             pDevice->setPixel( aPt2, aCol5, DrawMode_PAINT );
246             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #11",
247                                    pDevice->getPixel(aPt2) == aCol5);
248 
249             const Color aCol6(0xFFFFFFFF);
250             pDevice->setPixel( aPt3, aCol6, DrawMode_PAINT );
251             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #12",
252                                    pDevice->getPixel(aPt3) == aCol6);
253 
254             CPPUNIT_ASSERT_MESSAGE("raw pixel value #4",
255                                    pDevice->getBuffer()[2] == 0x3F
256                                    && pDevice->getBuffer()[1] == 0x2F
257                                    && pDevice->getBuffer()[0] == 0x1F);
258         }
259 
260         // 32bpp
261         {
262             pDevice = createBitmapDevice( aSize,
263                                           true,
264                                           Format::THIRTYTWO_BIT_TC_MASK );
265 
266             const Color aCol4(0x01010101);
267             pDevice->setPixel( aPt, aCol4, DrawMode_PAINT );
268             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #13",
269                                    pDevice->getPixel(aPt) == aCol4);
270 
271             const Color aCol5(0x0F0F0F0F);
272             pDevice->setPixel( aPt2, aCol5, DrawMode_PAINT );
273             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #14",
274                                    pDevice->getPixel(aPt2) == aCol5);
275 
276             const Color aCol6(0xFFFFFFFF);
277             pDevice->setPixel( aPt3, aCol6, DrawMode_PAINT );
278             CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #15",
279                                    pDevice->getPixel(aPt3) == aCol6);
280         }
281     }
282 
283     // Change the following lines only, if you add, remove or rename
284     // member functions of the current class,
285     // because these macros are need by auto register mechanism.
286 
287     CPPUNIT_TEST_SUITE(BasicTest);
288     CPPUNIT_TEST(colorTest);
289     CPPUNIT_TEST(testConstruction);
290     CPPUNIT_TEST(testPixelFuncs);
291     CPPUNIT_TEST_SUITE_END();
292 };
293 
294 // -----------------------------------------------------------------------------
295 CPPUNIT_TEST_SUITE_REGISTRATION(BasicTest);
296 }
297 
298 CPPUNIT_PLUGIN_IMPLEMENT();
299