summaryrefslogtreecommitdiffstats
path: root/vcl/qa/cppunit/BitmapScaleTest.cxx
blob: 9bb6a48e487243ae296a317984793c8e152f7a98 (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
306
307
308
309
310
311
312
/* -*- 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/.
 */

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

#include <vcl/bitmap.hxx>

#include <tools/stream.hxx>
#include <vcl/graphicfilter.hxx>

#include <BitmapSymmetryCheck.hxx>
#include <vcl/BitmapWriteAccess.hxx>

namespace
{
class BitmapScaleTest : public CppUnit::TestFixture
{
    void testScale();
    void testScale2();
    void testScaleSymmetry();

    CPPUNIT_TEST_SUITE(BitmapScaleTest);
    CPPUNIT_TEST(testScale);
    CPPUNIT_TEST(testScale2);
    CPPUNIT_TEST(testScaleSymmetry);
    CPPUNIT_TEST_SUITE_END();
};

bool checkBitmapColor(Bitmap const& rBitmap, Color const& rExpectedColor)
{
    bool bResult = true;
    Bitmap aBitmap(rBitmap);
    BitmapScopedReadAccess pReadAccess(aBitmap);
    tools::Long nHeight = pReadAccess->Height();
    tools::Long nWidth = pReadAccess->Width();
    for (tools::Long y = 0; y < nHeight; ++y)
    {
        Scanline pScanlineRead = pReadAccess->GetScanline(y);
        for (tools::Long x = 0; x < nWidth; ++x)
        {
            Color aColor = pReadAccess->GetPixelFromData(pScanlineRead, x);
            if (aColor != rExpectedColor)
                bResult = false;
        }
    }

    return bResult;
}

void assertColorsAreSimilar(int maxDifference, int line, const BitmapColor& expected,
                            const BitmapColor& actual)
{
    // Check that the two colors match or are reasonably similar.
    if (expected.GetColorError(actual) <= maxDifference)
        return;

    std::stringstream stream;
    stream << "Line: " << line;

    CPPUNIT_ASSERT_EQUAL_MESSAGE(stream.str(), expected, actual);
}

void BitmapScaleTest::testScale()
{
    const bool bExportBitmap(false);
    using tools::Rectangle;

    static const BmpScaleFlag scaleMethods[]
        = { BmpScaleFlag::Default,     BmpScaleFlag::Fast,    BmpScaleFlag::BestQuality,
            BmpScaleFlag::Interpolate, BmpScaleFlag::Lanczos, BmpScaleFlag::BiCubic,
            BmpScaleFlag::BiLinear };
    for (BmpScaleFlag scaleMethod : scaleMethods)
    {
        struct ScaleSize
        {
            Size srcSize;
            Size destSize;
        };
        static const ScaleSize scaleSizes[]
            = { // test no-op
                { Size(16, 16), Size(16, 16) },
                // powers of 2 (OpenGL may use texture atlas)
                { Size(16, 16), Size(14, 14) },
                { Size(14, 14), Size(16, 16) }, // both upscaling and downscaling
                // "random" sizes
                { Size(18, 18), Size(14, 14) },
                { Size(14, 14), Size(18, 18) },
                // different x/y ratios
                { Size(16, 30), Size(14, 18) },
                { Size(14, 18), Size(16, 30) },
                // ratio larger than 16 (triggers different paths in some OpenGL algorithms)
                { Size(18 * 20, 18 * 20), Size(14, 14) },
                { Size(14, 14), Size(18 * 20, 18 * 20) },
                // Boundary cases.
                { Size(1, 1), Size(1, 1) },
                { Size(16, 1), Size(12, 1) },
                { Size(1, 16), Size(1, 12) }
              };
        for (const ScaleSize& scaleSize : scaleSizes)
        {
            OString testStr = "Testing scale (" + scaleSize.srcSize.toString() + ")->("
                              + scaleSize.destSize.toString() + "), method "
                              + OString::number(static_cast<int>(scaleMethod));
            fprintf(stderr, "%s\n", testStr.getStr());
            Bitmap bitmap(scaleSize.srcSize, vcl::PixelFormat::N24_BPP);
            {
                // Fill each quarter of the source bitmap with a different color,
                // and center with yet another color.
                BitmapScopedWriteAccess writeAccess(bitmap);
                const int halfW = scaleSize.srcSize.getWidth() / 2;
                const int halfH = scaleSize.srcSize.getHeight() / 2;
                const Size aSize(std::max(halfW, 1), std::max(halfH, 1));

                writeAccess->SetFillColor(COL_GREEN);
                writeAccess->FillRect(Rectangle(Point(0, 0), aSize));
                writeAccess->SetFillColor(COL_RED);
                writeAccess->FillRect(Rectangle(Point(0, halfH), aSize));
                writeAccess->SetFillColor(COL_YELLOW);
                writeAccess->FillRect(Rectangle(Point(halfW, 0), aSize));
                writeAccess->SetFillColor(COL_BLACK);
                writeAccess->FillRect(Rectangle(Point(halfW, halfH), aSize));
                writeAccess->SetFillColor(COL_BLUE);
                writeAccess->FillRect(Rectangle(Point(halfW / 2, halfH / 2), aSize));
            }
            if (bExportBitmap)
            {
                SvFileStream aStream("~/scale_before.png", StreamMode::WRITE | StreamMode::TRUNC);
                GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
                rFilter.compressAsPNG(BitmapEx(bitmap), aStream);
            }
            CPPUNIT_ASSERT(bitmap.Scale(scaleSize.destSize, scaleMethod));
            if (bExportBitmap)
            {
                SvFileStream aStream("~/scale_after.png", StreamMode::WRITE | StreamMode::TRUNC);
                GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
                rFilter.compressAsPNG(BitmapEx(bitmap), aStream);
            }
            CPPUNIT_ASSERT_EQUAL(scaleSize.destSize, bitmap.GetSizePixel());
            const int lastW = scaleSize.destSize.getWidth() - 1;
            const int lastH = scaleSize.destSize.getHeight() - 1;
            if (scaleSize.srcSize.getWidth() == 1 && scaleSize.srcSize.getHeight() == 1)
            {
                BitmapReadAccess readAccess(bitmap);
                assertColorsAreSimilar(2, __LINE__, COL_BLUE, readAccess.GetColor(0, 0));
                assertColorsAreSimilar(2, __LINE__, COL_BLUE, readAccess.GetColor(lastH, 0));
                assertColorsAreSimilar(2, __LINE__, COL_BLUE, readAccess.GetColor(0, lastW));
                assertColorsAreSimilar(2, __LINE__, COL_BLUE, readAccess.GetColor(lastH, lastW));
                assertColorsAreSimilar(2, __LINE__, COL_BLUE,
                                       readAccess.GetColor(lastH / 2, lastW / 2));
            }
            else if (lastW && lastH)
            {
                // Scaling should keep each quarter of the resulting bitmap have the same color,
                // so check that color in each corner of the result bitmap is the same color,
                // or reasonably close (some algorithms may alter the color very slightly).
                BitmapReadAccess readAccess(bitmap);
                assertColorsAreSimilar(2, __LINE__, COL_GREEN, readAccess.GetColor(0, 0));
                assertColorsAreSimilar(2, __LINE__, COL_RED, readAccess.GetColor(lastH, 0));
                assertColorsAreSimilar(2, __LINE__, COL_YELLOW, readAccess.GetColor(0, lastW));
                assertColorsAreSimilar(2, __LINE__, COL_BLACK, readAccess.GetColor(lastH, lastW));
                assertColorsAreSimilar(2, __LINE__, COL_BLUE,
                                       readAccess.GetColor(lastH / 2, lastW / 2));
            }
            else if (lastW)
            {
                BitmapReadAccess readAccess(bitmap);
                assertColorsAreSimilar(2, __LINE__, COL_RED, readAccess.GetColor(0, 0));
                assertColorsAreSimilar(2, __LINE__, COL_BLACK, readAccess.GetColor(0, lastW));
                assertColorsAreSimilar(2, __LINE__, COL_BLUE, readAccess.GetColor(0, lastW / 2));
            }
            else if (lastH)
            {
                BitmapReadAccess readAccess(bitmap);
                assertColorsAreSimilar(2, __LINE__, COL_YELLOW, readAccess.GetColor(0, 0));
                assertColorsAreSimilar(2, __LINE__, COL_BLACK, readAccess.GetColor(lastH, 0));
                assertColorsAreSimilar(2, __LINE__, COL_BLUE, readAccess.GetColor(lastH / 2, 0));
            }
        }
    }
}

void BitmapScaleTest::testScale2()
{
    const bool bExportBitmap(false);

    Bitmap aBitmap24Bit(Size(4096, 4096), vcl::PixelFormat::N24_BPP);
    CPPUNIT_ASSERT_EQUAL(vcl::PixelFormat::N24_BPP, aBitmap24Bit.getPixelFormat());
    Color aBitmapColor = COL_YELLOW;
    {
        BitmapScopedWriteAccess aWriteAccess(aBitmap24Bit);
        aWriteAccess->Erase(aBitmapColor);
    }

    if (bExportBitmap)
    {
        SvFileStream aStream("scale_before.png", StreamMode::WRITE | StreamMode::TRUNC);
        GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
        rFilter.compressAsPNG(BitmapEx(aBitmap24Bit), aStream);
    }

    // Scale - 65x65
    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(4096), aBitmap24Bit.GetSizePixel().Width());
    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(4096), aBitmap24Bit.GetSizePixel().Height());
    Bitmap aScaledBitmap = aBitmap24Bit;
    aScaledBitmap.Scale(Size(65, 65));

    if (bExportBitmap)
    {
        SvFileStream aStream("scale_after_65x65.png", StreamMode::WRITE | StreamMode::TRUNC);
        GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
        rFilter.compressAsPNG(BitmapEx(aScaledBitmap), aStream);
    }

    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(65), aScaledBitmap.GetSizePixel().Width());
    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(65), aScaledBitmap.GetSizePixel().Height());
    CPPUNIT_ASSERT(checkBitmapColor(aScaledBitmap, aBitmapColor));

    // Scale - 64x64
    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(4096), aBitmap24Bit.GetSizePixel().Width());
    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(4096), aBitmap24Bit.GetSizePixel().Height());
    aScaledBitmap = aBitmap24Bit;
    aScaledBitmap.Scale(Size(64, 64));

    if (bExportBitmap)
    {
        SvFileStream aStream("scale_after_64x64.png", StreamMode::WRITE | StreamMode::TRUNC);
        GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
        rFilter.compressAsPNG(BitmapEx(aScaledBitmap), aStream);
    }

    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(64), aScaledBitmap.GetSizePixel().Width());
    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(64), aScaledBitmap.GetSizePixel().Height());
    CPPUNIT_ASSERT(checkBitmapColor(aScaledBitmap, aBitmapColor));

    // Scale - 63x63
    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(4096), aBitmap24Bit.GetSizePixel().Width());
    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(4096), aBitmap24Bit.GetSizePixel().Height());
    aScaledBitmap = aBitmap24Bit;
    aScaledBitmap.Scale(Size(63, 63));

    if (bExportBitmap)
    {
        SvFileStream aStream("scale_after_63x63.png", StreamMode::WRITE | StreamMode::TRUNC);
        GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
        rFilter.compressAsPNG(BitmapEx(aScaledBitmap), aStream);
    }

    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(63), aScaledBitmap.GetSizePixel().Width());
    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(63), aScaledBitmap.GetSizePixel().Height());
    CPPUNIT_ASSERT(checkBitmapColor(aScaledBitmap, aBitmapColor));
}

void BitmapScaleTest::testScaleSymmetry()
{
    const bool bExportBitmap(false);

    Bitmap aBitmap24Bit(Size(10, 10), vcl::PixelFormat::N24_BPP);
    CPPUNIT_ASSERT_EQUAL(vcl::PixelFormat::N24_BPP, aBitmap24Bit.getPixelFormat());

    {
        BitmapScopedWriteAccess aWriteAccess(aBitmap24Bit);
        aWriteAccess->Erase(COL_WHITE);
        aWriteAccess->SetLineColor(COL_BLACK);
        aWriteAccess->DrawRect(tools::Rectangle(1, 1, 8, 8));
        aWriteAccess->DrawRect(tools::Rectangle(3, 3, 6, 6));
    }

    BitmapSymmetryCheck aBitmapSymmetryCheck;

    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(10), aBitmap24Bit.GetSizePixel().Width());
    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(10), aBitmap24Bit.GetSizePixel().Height());

    // Check symmetry of the bitmap
    CPPUNIT_ASSERT(BitmapSymmetryCheck::check(aBitmap24Bit));

    if (bExportBitmap)
    {
        SvFileStream aStream("~/scale_before.png", StreamMode::WRITE | StreamMode::TRUNC);
        GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
        rFilter.compressAsPNG(BitmapEx(aBitmap24Bit), aStream);
    }

    aBitmap24Bit.Scale(2, 2, BmpScaleFlag::Fast);

    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(20), aBitmap24Bit.GetSizePixel().Width());
    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(20), aBitmap24Bit.GetSizePixel().Height());

    // After scaling the bitmap should still be symmetrical. This check guarantees that
    // scaling doesn't misalign the bitmap.
    CPPUNIT_ASSERT(BitmapSymmetryCheck::check(aBitmap24Bit));

    if (bExportBitmap)
    {
        SvFileStream aStream("~/scale_after.png", StreamMode::WRITE | StreamMode::TRUNC);
        GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
        rFilter.compressAsPNG(BitmapEx(aBitmap24Bit), aStream);
    }
}

} // namespace

CPPUNIT_TEST_SUITE_REGISTRATION(BitmapScaleTest);

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