cid#1607171 Data race condition
[LibreOffice.git] / vcl / qa / cppunit / jpeg / JpegReaderTest.cxx
blobf83d58d51d733aeba7ae9428bb5512ff874944c0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
11 #include <sal/config.h>
13 #include <string_view>
15 #include <unotest/bootstrapfixturebase.hxx>
16 #include <vcl/graphicfilter.hxx>
17 #include <vcl/BitmapWriteAccess.hxx>
18 #include <tools/stream.hxx>
20 constexpr OUStringLiteral gaDataUrl(u"/vcl/qa/cppunit/jpeg/data/");
22 class JpegReaderTest : public test::BootstrapFixtureBase
24 OUString getFullUrl(std::u16string_view sFileName)
26 return m_directories.getURLFromSrc(gaDataUrl) + sFileName;
29 Graphic loadJPG(const OUString& aURL);
31 public:
32 void testReadRGB();
33 void testReadGray();
34 void testReadCMYK();
35 void testTdf138950();
37 CPPUNIT_TEST_SUITE(JpegReaderTest);
38 CPPUNIT_TEST(testReadRGB);
39 CPPUNIT_TEST(testReadGray);
40 CPPUNIT_TEST(testReadCMYK);
41 CPPUNIT_TEST(testTdf138950);
42 CPPUNIT_TEST_SUITE_END();
45 static int deltaColor(BitmapColor aColor1, BitmapColor aColor2)
47 int deltaR = std::abs(aColor1.GetRed() - aColor2.GetRed());
48 int deltaG = std::abs(aColor1.GetGreen() - aColor2.GetGreen());
49 int deltaB = std::abs(aColor1.GetBlue() - aColor2.GetBlue());
51 return std::max(std::max(deltaR, deltaG), deltaB);
54 static bool checkRect(Bitmap& rBitmap, int aLayerNumber, tools::Long nAreaHeight,
55 tools::Long nAreaWidth, Color aExpectedColor, int nMaxDelta)
57 BitmapScopedWriteAccess pAccess(rBitmap);
59 tools::Long nWidth = std::min(nAreaWidth, pAccess->Width());
60 tools::Long nHeight = std::min(nAreaHeight, pAccess->Height());
62 tools::Long firstX = 0 + aLayerNumber;
63 tools::Long firstY = 0 + aLayerNumber;
65 tools::Long lastX = nWidth - 1 - aLayerNumber;
66 tools::Long lastY = nHeight - 1 - aLayerNumber;
68 int delta;
70 for (tools::Long y = firstY; y <= lastY; y++)
72 Color aColorFirst = pAccess->GetPixel(y, firstX);
73 delta = deltaColor(aColorFirst, aExpectedColor);
74 if (delta > nMaxDelta)
75 return false;
77 Color aColorLast = pAccess->GetPixel(y, lastX);
78 delta = deltaColor(aColorLast, aExpectedColor);
79 if (delta > nMaxDelta)
80 return false;
82 for (tools::Long x = firstX; x <= lastX; x++)
84 Color aColorFirst = pAccess->GetPixel(firstY, x);
85 delta = deltaColor(aColorFirst, aExpectedColor);
86 if (delta > nMaxDelta)
87 return false;
89 Color aColorLast = pAccess->GetPixel(lastY, x);
90 delta = deltaColor(aColorLast, aExpectedColor);
91 if (delta > nMaxDelta)
92 return false;
94 return true;
97 static int getNumberOfImageComponents(const Graphic& rGraphic)
99 GfxLink aLink = rGraphic.GetGfxLink();
100 SvMemoryStream aMemoryStream(const_cast<sal_uInt8*>(aLink.GetData()), aLink.GetDataSize(),
101 StreamMode::READ | StreamMode::WRITE);
102 GraphicDescriptor aDescriptor(aMemoryStream, nullptr);
103 CPPUNIT_ASSERT(aDescriptor.Detect(true));
104 return aDescriptor.GetNumberOfImageComponents();
107 Graphic JpegReaderTest::loadJPG(const OUString& aURL)
109 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
110 Graphic aGraphic;
111 SvFileStream aFileStream(aURL, StreamMode::READ);
112 ErrCode bResult = rFilter.ImportGraphic(aGraphic, aURL, aFileStream);
113 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, bResult);
114 return aGraphic;
117 void JpegReaderTest::testReadRGB()
119 Graphic aGraphic = loadJPG(getFullUrl(u"JPEGTestRGB.jpeg"));
120 Bitmap aBitmap = aGraphic.GetBitmapEx().GetBitmap();
121 Size aSize = aBitmap.GetSizePixel();
122 CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Width());
123 CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Height());
125 int nMaxDelta = 1; // still acceptable color error
126 CPPUNIT_ASSERT(checkRect(aBitmap, 0, 8, 8, Color(0xff, 0xff, 0xff), nMaxDelta));
127 CPPUNIT_ASSERT(checkRect(aBitmap, 1, 8, 8, Color(0xff, 0x00, 0x00), nMaxDelta));
128 CPPUNIT_ASSERT(checkRect(aBitmap, 2, 8, 8, Color(0x00, 0xff, 0x00), nMaxDelta));
129 CPPUNIT_ASSERT(checkRect(aBitmap, 3, 8, 8, Color(0x00, 0x00, 0xff), nMaxDelta));
131 CPPUNIT_ASSERT_EQUAL(3, getNumberOfImageComponents(aGraphic));
134 void JpegReaderTest::testReadGray()
136 Graphic aGraphic = loadJPG(getFullUrl(u"JPEGTestGray.jpeg"));
137 Bitmap aBitmap = aGraphic.GetBitmapEx().GetBitmap();
138 Size aSize = aBitmap.GetSizePixel();
139 CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Width());
140 CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Height());
142 aBitmap.Convert(
143 BmpConversion::N24Bit); // convert to 24bit so we don't need to deal with palette
145 int nMaxDelta = 1;
146 CPPUNIT_ASSERT(checkRect(aBitmap, 0, 8, 8, Color(0xff, 0xff, 0xff), nMaxDelta));
147 CPPUNIT_ASSERT(checkRect(aBitmap, 1, 8, 8, Color(0x36, 0x36, 0x36), nMaxDelta));
148 CPPUNIT_ASSERT(checkRect(aBitmap, 2, 8, 8, Color(0xb6, 0xb6, 0xb6), nMaxDelta));
149 CPPUNIT_ASSERT(checkRect(aBitmap, 3, 8, 8, Color(0x12, 0x12, 0x12), nMaxDelta));
151 CPPUNIT_ASSERT_EQUAL(1, getNumberOfImageComponents(aGraphic));
154 void JpegReaderTest::testReadCMYK()
156 Graphic aGraphic = loadJPG(getFullUrl(u"JPEGTestCMYK.jpeg"));
157 Bitmap aBitmap = aGraphic.GetBitmapEx().GetBitmap();
158 Size aSize = aBitmap.GetSizePixel();
159 CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Width());
160 CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Height());
162 int maxDelta = 1;
163 CPPUNIT_ASSERT(checkRect(aBitmap, 0, 8, 8, Color(0xff, 0xff, 0xff), maxDelta));
164 CPPUNIT_ASSERT(checkRect(aBitmap, 1, 8, 8, Color(0xff, 0x00, 0x00), maxDelta));
165 CPPUNIT_ASSERT(checkRect(aBitmap, 2, 8, 8, Color(0x00, 0xff, 0x00), maxDelta));
166 CPPUNIT_ASSERT(checkRect(aBitmap, 3, 8, 8, Color(0x00, 0x00, 0xff), maxDelta));
168 CPPUNIT_ASSERT_EQUAL(4, getNumberOfImageComponents(aGraphic));
171 void JpegReaderTest::testTdf138950()
173 Graphic aGraphic = loadJPG(getFullUrl(u"tdf138950.jpeg"));
174 Bitmap aBitmap = aGraphic.GetBitmapEx().GetBitmap();
175 Size aSize = aBitmap.GetSizePixel();
176 CPPUNIT_ASSERT_EQUAL(tools::Long(720), aSize.Width());
177 CPPUNIT_ASSERT_EQUAL(tools::Long(1280), aSize.Height());
179 BitmapScopedReadAccess pReadAccess(aBitmap);
180 int nBlackCount = 0;
181 for (tools::Long nY = 0; nY < aSize.Height(); ++nY)
183 for (tools::Long nX = 0; nX < aSize.Width(); ++nX)
185 const Color aColor = pReadAccess->GetColor(nY, nX);
186 if ((aColor.GetRed() == 0x00) && (aColor.GetGreen() == 0x00)
187 && (aColor.GetBlue() == 0x00))
188 ++nBlackCount;
192 // Without the fix in place, this test would have failed with
193 // - Expected: 0
194 // - Actual : 921600
195 CPPUNIT_ASSERT_EQUAL(0, nBlackCount);
198 CPPUNIT_TEST_SUITE_REGISTRATION(JpegReaderTest);
200 CPPUNIT_PLUGIN_IMPLEMENT();
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */