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