summaryrefslogtreecommitdiffstats
path: root/vcl/qa/cppunit/jpeg/JpegReaderTest.cxx
blob: 0900c02d4bb5df0d48556a3b1f07fc289f621755 (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
/* -*- 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 <sal/config.h>

#include <string_view>

#include <unotest/bootstrapfixturebase.hxx>
#include <vcl/graphicfilter.hxx>
#include <bitmap/BitmapWriteAccess.hxx>
#include <tools/stream.hxx>

constexpr OUStringLiteral gaDataUrl(u"/vcl/qa/cppunit/jpeg/data/");

class JpegReaderTest : public test::BootstrapFixtureBase
{
    OUString getFullUrl(std::u16string_view sFileName)
    {
        return m_directories.getURLFromSrc(gaDataUrl) + sFileName;
    }

    Graphic loadJPG(const OUString& aURL);

public:
    void testReadRGB();
    void testReadGray();
    void testReadCMYK();
    void testTdf138950();

    CPPUNIT_TEST_SUITE(JpegReaderTest);
    CPPUNIT_TEST(testReadRGB);
    CPPUNIT_TEST(testReadGray);
    CPPUNIT_TEST(testReadCMYK);
    CPPUNIT_TEST(testTdf138950);
    CPPUNIT_TEST_SUITE_END();
};

static int deltaColor(BitmapColor aColor1, BitmapColor aColor2)
{
    int deltaR = std::abs(aColor1.GetRed() - aColor2.GetRed());
    int deltaG = std::abs(aColor1.GetGreen() - aColor2.GetGreen());
    int deltaB = std::abs(aColor1.GetBlue() - aColor2.GetBlue());

    return std::max(std::max(deltaR, deltaG), deltaB);
}

static bool checkRect(Bitmap& rBitmap, int aLayerNumber, tools::Long nAreaHeight,
                      tools::Long nAreaWidth, Color aExpectedColor, int nMaxDelta)
{
    BitmapScopedWriteAccess pAccess(rBitmap);

    tools::Long nWidth = std::min(nAreaWidth, pAccess->Width());
    tools::Long nHeight = std::min(nAreaHeight, pAccess->Height());

    tools::Long firstX = 0 + aLayerNumber;
    tools::Long firstY = 0 + aLayerNumber;

    tools::Long lastX = nWidth - 1 - aLayerNumber;
    tools::Long lastY = nHeight - 1 - aLayerNumber;

    int delta;

    for (tools::Long y = firstY; y <= lastY; y++)
    {
        Color aColorFirst = pAccess->GetPixel(y, firstX);
        delta = deltaColor(aColorFirst, aExpectedColor);
        if (delta > nMaxDelta)
            return false;

        Color aColorLast = pAccess->GetPixel(y, lastX);
        delta = deltaColor(aColorLast, aExpectedColor);
        if (delta > nMaxDelta)
            return false;
    }
    for (tools::Long x = firstX; x <= lastX; x++)
    {
        Color aColorFirst = pAccess->GetPixel(firstY, x);
        delta = deltaColor(aColorFirst, aExpectedColor);
        if (delta > nMaxDelta)
            return false;

        Color aColorLast = pAccess->GetPixel(lastY, x);
        delta = deltaColor(aColorLast, aExpectedColor);
        if (delta > nMaxDelta)
            return false;
    }
    return true;
}

static int getNumberOfImageComponents(const Graphic& rGraphic)
{
    GfxLink aLink = rGraphic.GetGfxLink();
    SvMemoryStream aMemoryStream(const_cast<sal_uInt8*>(aLink.GetData()), aLink.GetDataSize(),
                                 StreamMode::READ | StreamMode::WRITE);
    GraphicDescriptor aDescriptor(aMemoryStream, nullptr);
    CPPUNIT_ASSERT(aDescriptor.Detect(true));
    return aDescriptor.GetNumberOfImageComponents();
}

Graphic JpegReaderTest::loadJPG(const OUString& aURL)
{
    GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
    Graphic aGraphic;
    SvFileStream aFileStream(aURL, StreamMode::READ);
    ErrCode bResult = rFilter.ImportGraphic(aGraphic, aURL, aFileStream);
    CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, bResult);
    return aGraphic;
}

void JpegReaderTest::testReadRGB()
{
    Graphic aGraphic = loadJPG(getFullUrl(u"JPEGTestRGB.jpeg"));
    Bitmap aBitmap = aGraphic.GetBitmapEx().GetBitmap();
    Size aSize = aBitmap.GetSizePixel();
    CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Width());
    CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Height());

    int nMaxDelta = 1; // still acceptable color error
    CPPUNIT_ASSERT(checkRect(aBitmap, 0, 8, 8, Color(0xff, 0xff, 0xff), nMaxDelta));
    CPPUNIT_ASSERT(checkRect(aBitmap, 1, 8, 8, Color(0xff, 0x00, 0x00), nMaxDelta));
    CPPUNIT_ASSERT(checkRect(aBitmap, 2, 8, 8, Color(0x00, 0xff, 0x00), nMaxDelta));
    CPPUNIT_ASSERT(checkRect(aBitmap, 3, 8, 8, Color(0x00, 0x00, 0xff), nMaxDelta));

    CPPUNIT_ASSERT_EQUAL(3, getNumberOfImageComponents(aGraphic));
}

void JpegReaderTest::testReadGray()
{
    Graphic aGraphic = loadJPG(getFullUrl(u"JPEGTestGray.jpeg"));
    Bitmap aBitmap = aGraphic.GetBitmapEx().GetBitmap();
    Size aSize = aBitmap.GetSizePixel();
    CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Width());
    CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Height());

    aBitmap.Convert(
        BmpConversion::N24Bit); // convert to 24bit so we don't need to deal with palette

    int nMaxDelta = 1;
    CPPUNIT_ASSERT(checkRect(aBitmap, 0, 8, 8, Color(0xff, 0xff, 0xff), nMaxDelta));
    CPPUNIT_ASSERT(checkRect(aBitmap, 1, 8, 8, Color(0x36, 0x36, 0x36), nMaxDelta));
    CPPUNIT_ASSERT(checkRect(aBitmap, 2, 8, 8, Color(0xb6, 0xb6, 0xb6), nMaxDelta));
    CPPUNIT_ASSERT(checkRect(aBitmap, 3, 8, 8, Color(0x12, 0x12, 0x12), nMaxDelta));

    CPPUNIT_ASSERT_EQUAL(1, getNumberOfImageComponents(aGraphic));
}

void JpegReaderTest::testReadCMYK()
{
    Graphic aGraphic = loadJPG(getFullUrl(u"JPEGTestCMYK.jpeg"));
    Bitmap aBitmap = aGraphic.GetBitmapEx().GetBitmap();
    Size aSize = aBitmap.GetSizePixel();
    CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Width());
    CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Height());

    int maxDelta = 1;
    CPPUNIT_ASSERT(checkRect(aBitmap, 0, 8, 8, Color(0xff, 0xff, 0xff), maxDelta));
    CPPUNIT_ASSERT(checkRect(aBitmap, 1, 8, 8, Color(0xff, 0x00, 0x00), maxDelta));
    CPPUNIT_ASSERT(checkRect(aBitmap, 2, 8, 8, Color(0x00, 0xff, 0x00), maxDelta));
    CPPUNIT_ASSERT(checkRect(aBitmap, 3, 8, 8, Color(0x00, 0x00, 0xff), maxDelta));

    CPPUNIT_ASSERT_EQUAL(4, getNumberOfImageComponents(aGraphic));
}

void JpegReaderTest::testTdf138950()
{
    Graphic aGraphic = loadJPG(getFullUrl(u"tdf138950.jpeg"));
    Bitmap aBitmap = aGraphic.GetBitmapEx().GetBitmap();
    Size aSize = aBitmap.GetSizePixel();
    CPPUNIT_ASSERT_EQUAL(tools::Long(720), aSize.Width());
    CPPUNIT_ASSERT_EQUAL(tools::Long(1280), aSize.Height());

    Bitmap::ScopedReadAccess pReadAccess(aBitmap);
    int nBlackCount = 0;
    for (tools::Long nY = 0; nY < aSize.Height(); ++nY)
    {
        for (tools::Long nX = 0; nX < aSize.Width(); ++nX)
        {
            const Color aColor = pReadAccess->GetColor(nY, nX);
            if ((aColor.GetRed() == 0x00) && (aColor.GetGreen() == 0x00)
                && (aColor.GetBlue() == 0x00))
                ++nBlackCount;
        }
    }

    // Without the fix in place, this test would have failed with
    // - Expected: 0
    // - Actual  : 921600
    CPPUNIT_ASSERT_EQUAL(0, nBlackCount);
}

CPPUNIT_TEST_SUITE_REGISTRATION(JpegReaderTest);

CPPUNIT_PLUGIN_IMPLEMENT();

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