summaryrefslogtreecommitdiff
path: root/basebmp/test/basictest.cxx
blob: 4290ed7a865a80c2bbeee444574a6a9f8348b909 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

// autogenerated file with codegen.pl

#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>

#include <basegfx/vector/b2isize.hxx>
#include <basegfx/point/b2ipoint.hxx>

#include <basebmp/color.hxx>
#include <basebmp/scanlineformats.hxx>
#include <basebmp/bitmapdevice.hxx>
#include "tools.hxx"

using namespace ::basebmp;

namespace
{
class BasicTest : public CppUnit::TestFixture
{
public:
    void colorTest()
    {
        Color aTestColor;

        aTestColor = Color(0xDEADBEEF);
        CPPUNIT_ASSERT_MESSAGE("unary constructor",
                               aTestColor.toInt32() == 0xDEADBEEF );

        aTestColor = Color( 0x10, 0x20, 0xFF );
        CPPUNIT_ASSERT_MESSAGE("ternary constructor",
                               aTestColor.toInt32() == 0x001020FF );

        aTestColor.setRed( 0x0F );
        CPPUNIT_ASSERT_MESSAGE("setRed()",
                               aTestColor.toInt32() == 0x00F20FF );

        aTestColor.setGreen( 0x0F );
        CPPUNIT_ASSERT_MESSAGE("setGreen()",
                               aTestColor.toInt32() == 0x00F0FFF );

        aTestColor.setBlue( 0x10 );
        CPPUNIT_ASSERT_MESSAGE("setBlue()",
                               aTestColor.toInt32() == 0x00F0F10 );

        aTestColor.setGrey( 0x13 );
        CPPUNIT_ASSERT_MESSAGE("setGrey()",
                               aTestColor.toInt32() == 0x00131313 );

        aTestColor = Color( 0x10, 0x20, 0xFF );
        CPPUNIT_ASSERT_MESSAGE("getRed()",
                               aTestColor.getRed() == 0x10 );
        CPPUNIT_ASSERT_MESSAGE("getGreen()",
                               aTestColor.getGreen() == 0x20 );
        CPPUNIT_ASSERT_MESSAGE("getBlue()",
                               aTestColor.getBlue() == 0xFF );

    }

    void testConstruction()
    {
        const basegfx::B2ISize aSize(101,101);
        basegfx::B2ISize       aSize2(aSize);
        BitmapDeviceSharedPtr pDevice( createBitmapDevice( aSize,
                                                           true,
                                                           FORMAT_ONE_BIT_MSB_PAL ));
        CPPUNIT_ASSERT_MESSAGE("right size",
                               pDevice->getSize() == aSize2 );
        CPPUNIT_ASSERT_MESSAGE("Top down format",
                               pDevice->isTopDown() == true );
        CPPUNIT_ASSERT_MESSAGE("Scanline format",
                               pDevice->getScanlineFormat() == FORMAT_ONE_BIT_MSB_PAL );
        CPPUNIT_ASSERT_MESSAGE("Scanline len",
                               pDevice->getScanlineStride() == (aSize2.getY() + 7)/8 );
        CPPUNIT_ASSERT_MESSAGE("Palette existence",
                               pDevice->getPalette() );
        CPPUNIT_ASSERT_MESSAGE("Palette entry 0 is black",
                               (*pDevice->getPalette())[0] == Color(0) );
        CPPUNIT_ASSERT_MESSAGE("Palette entry 1 is white",
                               (*pDevice->getPalette())[1] == Color(0xFFFFFFFF) );
    }

    void testClone()
    {
        const basegfx::B2ISize aSize(101,101);
        basegfx::B2ISize       aSize2(3,3);
        BitmapDeviceSharedPtr  pDevice( createBitmapDevice( aSize,
                                                            true,
                                                            FORMAT_ONE_BIT_MSB_PAL ));

        BitmapDeviceSharedPtr  pClone( cloneBitmapDevice(
                                           aSize2,
                                           pDevice ));
        CPPUNIT_ASSERT_MESSAGE("right size",
                               pClone->getSize() == aSize2 );
    }

    void testPixelFuncs()
    {
        // 1bpp
        const basegfx::B2ISize aSize(64,64);
        BitmapDeviceSharedPtr pDevice( createBitmapDevice( aSize,
                                                           true,
                                                           FORMAT_ONE_BIT_MSB_PAL ));

        const basegfx::B2IPoint aPt(3,3);
        CPPUNIT_ASSERT_MESSAGE("getPixelData for virgin device",
                               pDevice->getPixelData(aPt) == 0);

        const Color aCol(0xFFFFFFFF);
        pDevice->setPixel( aPt, aCol, DrawMode_PAINT );
        CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #1",
                               pDevice->getPixel(aPt) == aCol);
        CPPUNIT_ASSERT_MESSAGE("getPixelData for white pixel",
                               pDevice->getPixelData(aPt) == 1);

        const basegfx::B2IPoint aPt2(0,0);
        const Color aCol2(0xFFFFFFFF);
        pDevice->setPixel( aPt2, aCol2, DrawMode_PAINT );
        CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #2",
                               pDevice->getPixel(aPt2) == aCol2);

        const basegfx::B2IPoint aPt3(aSize.getX()-1,aSize.getY()-1);
        const Color aCol3(0x00000000);
        pDevice->setPixel( aPt3, aCol3, DrawMode_PAINT );
        CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #3",
                               pDevice->getPixel(aPt3) == aCol3);

        pDevice->setPixel( aPt3, aCol2, DrawMode_PAINT );
        CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #3.5",
                               pDevice->getPixel(aPt3) == aCol2);

        const basegfx::B2IPoint aPt4(-100000,-100000);
        pDevice->setPixel( aPt4, aCol3, DrawMode_PAINT );
        const basegfx::B2IPoint aPt5(100000,100000);
        pDevice->setPixel( aPt5, aCol3, DrawMode_PAINT );

        sal_Int32 nPixel(countPixel(pDevice, aCol2));
        const basegfx::B2IPoint aPt6(aSize.getX(),aSize.getY());
        pDevice->setPixel( aPt6, aCol2, DrawMode_PAINT );
        CPPUNIT_ASSERT_MESSAGE("setPixel clipping",
                               countPixel(pDevice, aCol2) == nPixel);

        CPPUNIT_ASSERT_MESSAGE("raw pixel value #1",
                               pDevice->getBuffer()[0] == 0x80);

        // 1bit LSB
        {
            pDevice = createBitmapDevice( aSize,
                                          true,
                                          FORMAT_ONE_BIT_LSB_PAL );

            pDevice->setPixel( aPt2, aCol, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #4",
                                   pDevice->getPixel(aPt2) == aCol);

            const basegfx::B2IPoint aPt222(1,1);
            pDevice->setPixel( aPt222, aCol, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #5",
                                   pDevice->getPixel(aPt222) == aCol);

            pDevice->setPixel( aPt3, aCol, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #6",
                                   pDevice->getPixel(aPt3) == aCol);

            CPPUNIT_ASSERT_MESSAGE("raw pixel value #2",
                                   pDevice->getBuffer()[0] == 0x01);
            CPPUNIT_ASSERT_MESSAGE("raw pixel value #3",
                                   pDevice->getBuffer()[8] == 0x02);
        }

        // 8bit alpha
        {
            pDevice = createBitmapDevice( aSize,
                                          true,
                                          FORMAT_EIGHT_BIT_GREY );

            const Color aCol4(0x010101);
            pDevice->setPixel( aPt, aCol4, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #4",
                                   pDevice->getPixel(aPt) == aCol4);

            const Color aCol5(0x0F0F0F);
            pDevice->setPixel( aPt2, aCol5, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #5",
                                   pDevice->getPixel(aPt2) == aCol5);

            const Color aCol6(0xFFFFFF);
            pDevice->setPixel( aPt3, aCol6, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #6",
                                   pDevice->getPixel(aPt3) == aCol6);
        }

        // 16bpp
        {
            pDevice = createBitmapDevice( aSize,
                                          true,
                                          FORMAT_SIXTEEN_BIT_LSB_TC_MASK );
            const Color aCol7(0);
            pDevice->clear( aCol7 );

            const Color aCol4(0x00101010);
            pDevice->setPixel( aPt, aCol4, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #7",
                                   pDevice->getPixel(aPt) == aCol4);

            const Color aCol5(0x00F0F0F0);
            pDevice->setPixel( aPt2, aCol5, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #8",
                                   pDevice->getPixel(aPt2) != aCol7);

            const Color aCol6(0x00FFFFFF);
            pDevice->setPixel( aPt3, aCol6, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #9",
                                   pDevice->getPixel(aPt3) == aCol6);
        }

        // 24bpp
        {
            pDevice = createBitmapDevice( aSize,
                                          true,
                                          FORMAT_TWENTYFOUR_BIT_TC_MASK );

            const Color aCol4(0x01010101);
            pDevice->setPixel( aPt, aCol4, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #10",
                                   pDevice->getPixel(aPt) == aCol4);

            const Color aCol5(0x0F3F2F1F);
            pDevice->setPixel( aPt2, aCol5, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #11",
                                   pDevice->getPixel(aPt2) == aCol5);

            const Color aCol6(0xFFFFFFFF);
            pDevice->setPixel( aPt3, aCol6, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #12",
                                   pDevice->getPixel(aPt3) == aCol6);

            CPPUNIT_ASSERT_MESSAGE("raw pixel value #4",
                                   pDevice->getBuffer()[2] == 0x3F
                                   && pDevice->getBuffer()[1] == 0x2F
                                   && pDevice->getBuffer()[0] == 0x1F);
        }

        // 32bpp
        {
            pDevice = createBitmapDevice( aSize,
                                          true,
                                          FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA );

            const Color aCol4(0x01010101);
            pDevice->setPixel( aPt, aCol4, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #13",
                                   pDevice->getPixel(aPt) == aCol4);

            const Color aCol5(0x0F0F0F0F);
            pDevice->setPixel( aPt2, aCol5, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #14",
                                   pDevice->getPixel(aPt2) == aCol5);

            const Color aCol6(0xFFFFFFFF);
            pDevice->setPixel( aPt3, aCol6, DrawMode_PAINT );
            CPPUNIT_ASSERT_MESSAGE("get/setPixel roundtrip #15",
                                   pDevice->getPixel(aPt3) == aCol6);
        }
    }

    // Change the following lines only, if you add, remove or rename
    // member functions of the current class,
    // because these macros are need by auto register mechanism.

    CPPUNIT_TEST_SUITE(BasicTest);
    CPPUNIT_TEST(colorTest);
    CPPUNIT_TEST(testConstruction);
    CPPUNIT_TEST(testClone);
    CPPUNIT_TEST(testPixelFuncs);
    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(BasicTest);
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */