nss: upgrade to release 3.73
[LibreOffice.git] / vcl / qa / cppunit / jpeg / JpegReaderTest.cxx
blob78124beef5f3342f2d68433c5d8625ce5040954b
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 <unotest/bootstrapfixturebase.hxx>
12 #include <vcl/graphicfilter.hxx>
13 #include <bitmapwriteaccess.hxx>
14 #include <tools/stream.hxx>
16 constexpr OUStringLiteral gaDataUrl(u"/vcl/qa/cppunit/jpeg/data/");
18 class JpegReaderTest : public test::BootstrapFixtureBase
20 OUString getFullUrl(const OUString& sFileName)
22 return m_directories.getURLFromSrc(gaDataUrl) + sFileName;
25 Graphic loadJPG(const OUString& aURL);
27 public:
28 void testReadRGB();
29 void testReadGray();
30 void testReadCMYK();
32 CPPUNIT_TEST_SUITE(JpegReaderTest);
33 CPPUNIT_TEST(testReadRGB);
34 CPPUNIT_TEST(testReadGray);
35 CPPUNIT_TEST(testReadCMYK);
36 CPPUNIT_TEST_SUITE_END();
39 static int deltaColor(BitmapColor aColor1, BitmapColor aColor2)
41 int deltaR = std::abs(aColor1.GetRed() - aColor2.GetRed());
42 int deltaG = std::abs(aColor1.GetGreen() - aColor2.GetGreen());
43 int deltaB = std::abs(aColor1.GetBlue() - aColor2.GetBlue());
45 return std::max(std::max(deltaR, deltaG), deltaB);
48 static bool checkRect(Bitmap& rBitmap, int aLayerNumber, tools::Long nAreaHeight,
49 tools::Long nAreaWidth, Color aExpectedColor, int nMaxDelta)
51 BitmapScopedWriteAccess pAccess(rBitmap);
53 tools::Long nWidth = std::min(nAreaWidth, pAccess->Width());
54 tools::Long nHeight = std::min(nAreaHeight, pAccess->Height());
56 tools::Long firstX = 0 + aLayerNumber;
57 tools::Long firstY = 0 + aLayerNumber;
59 tools::Long lastX = nWidth - 1 - aLayerNumber;
60 tools::Long lastY = nHeight - 1 - aLayerNumber;
62 int delta;
64 for (tools::Long y = firstY; y <= lastY; y++)
66 Color aColorFirst = pAccess->GetPixel(y, firstX);
67 delta = deltaColor(aColorFirst, aExpectedColor);
68 if (delta > nMaxDelta)
69 return false;
71 Color aColorLast = pAccess->GetPixel(y, lastX);
72 delta = deltaColor(aColorLast, aExpectedColor);
73 if (delta > nMaxDelta)
74 return false;
76 for (tools::Long x = firstX; x <= lastX; x++)
78 Color aColorFirst = pAccess->GetPixel(firstY, x);
79 delta = deltaColor(aColorFirst, aExpectedColor);
80 if (delta > nMaxDelta)
81 return false;
83 Color aColorLast = pAccess->GetPixel(lastY, x);
84 delta = deltaColor(aColorLast, aExpectedColor);
85 if (delta > nMaxDelta)
86 return false;
88 return true;
91 static int getNumberOfImageComponents(const Graphic& rGraphic)
93 GfxLink aLink = rGraphic.GetGfxLink();
94 SvMemoryStream aMemoryStream(const_cast<sal_uInt8*>(aLink.GetData()), aLink.GetDataSize(),
95 StreamMode::READ | StreamMode::WRITE);
96 GraphicDescriptor aDescriptor(aMemoryStream, nullptr);
97 CPPUNIT_ASSERT(aDescriptor.Detect(true));
98 return aDescriptor.GetNumberOfImageComponents();
101 Graphic JpegReaderTest::loadJPG(const OUString& aURL)
103 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
104 Graphic aGraphic;
105 SvFileStream aFileStream(aURL, StreamMode::READ);
106 ErrCode bResult = rFilter.ImportGraphic(aGraphic, aURL, aFileStream);
107 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, bResult);
108 return aGraphic;
111 void JpegReaderTest::testReadRGB()
113 Graphic aGraphic = loadJPG(getFullUrl("JPEGTestRGB.jpeg"));
114 Bitmap aBitmap = aGraphic.GetBitmapEx().GetBitmap();
115 Size aSize = aBitmap.GetSizePixel();
116 CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Width());
117 CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Height());
119 int nMaxDelta = 1; // still acceptable color error
120 CPPUNIT_ASSERT(checkRect(aBitmap, 0, 8, 8, Color(0xff, 0xff, 0xff), nMaxDelta));
121 CPPUNIT_ASSERT(checkRect(aBitmap, 1, 8, 8, Color(0xff, 0x00, 0x00), nMaxDelta));
122 CPPUNIT_ASSERT(checkRect(aBitmap, 2, 8, 8, Color(0x00, 0xff, 0x00), nMaxDelta));
123 CPPUNIT_ASSERT(checkRect(aBitmap, 3, 8, 8, Color(0x00, 0x00, 0xff), nMaxDelta));
125 CPPUNIT_ASSERT_EQUAL(3, getNumberOfImageComponents(aGraphic));
128 void JpegReaderTest::testReadGray()
130 Graphic aGraphic = loadJPG(getFullUrl("JPEGTestGray.jpeg"));
131 Bitmap aBitmap = aGraphic.GetBitmapEx().GetBitmap();
132 Size aSize = aBitmap.GetSizePixel();
133 CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Width());
134 CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Height());
136 aBitmap.Convert(
137 BmpConversion::N24Bit); // convert to 24bit so we don't need to deal with palette
139 int nMaxDelta = 1;
140 CPPUNIT_ASSERT(checkRect(aBitmap, 0, 8, 8, Color(0xff, 0xff, 0xff), nMaxDelta));
141 CPPUNIT_ASSERT(checkRect(aBitmap, 1, 8, 8, Color(0x36, 0x36, 0x36), nMaxDelta));
142 CPPUNIT_ASSERT(checkRect(aBitmap, 2, 8, 8, Color(0xb6, 0xb6, 0xb6), nMaxDelta));
143 CPPUNIT_ASSERT(checkRect(aBitmap, 3, 8, 8, Color(0x12, 0x12, 0x12), nMaxDelta));
145 CPPUNIT_ASSERT_EQUAL(1, getNumberOfImageComponents(aGraphic));
148 void JpegReaderTest::testReadCMYK()
150 Graphic aGraphic = loadJPG(getFullUrl("JPEGTestCMYK.jpeg"));
151 Bitmap aBitmap = aGraphic.GetBitmapEx().GetBitmap();
152 Size aSize = aBitmap.GetSizePixel();
153 CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Width());
154 CPPUNIT_ASSERT_EQUAL(tools::Long(12), aSize.Height());
156 int maxDelta = 1;
157 CPPUNIT_ASSERT(checkRect(aBitmap, 0, 8, 8, Color(0xff, 0xff, 0xff), maxDelta));
158 CPPUNIT_ASSERT(checkRect(aBitmap, 1, 8, 8, Color(0xff, 0x00, 0x00), maxDelta));
159 CPPUNIT_ASSERT(checkRect(aBitmap, 2, 8, 8, Color(0x00, 0xff, 0x00), maxDelta));
160 CPPUNIT_ASSERT(checkRect(aBitmap, 3, 8, 8, Color(0x00, 0x00, 0xff), maxDelta));
162 CPPUNIT_ASSERT_EQUAL(4, getNumberOfImageComponents(aGraphic));
165 CPPUNIT_TEST_SUITE_REGISTRATION(JpegReaderTest);
167 CPPUNIT_PLUGIN_IMPLEMENT();
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */