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
|
/* -*- 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 <unotest/filters-test.hxx>
#include <test/bootstrapfixture.hxx>
#include <vcl/FilterConfigItem.hxx>
#include <bitmap/BitmapWriteAccess.hxx>
#include <tools/stream.hxx>
#include <vcl/graph.hxx>
#include <vcl/graphicfilter.hxx>
#include <graphic/GraphicFormatDetector.hxx>
#include <filter/WebpReader.hxx>
#include <comphelper/propertyvalue.hxx>
using namespace css;
/* Implementation of Filters test */
class WebpFilterTest : public test::FiltersTest, public test::BootstrapFixture
{
public:
WebpFilterTest()
: BootstrapFixture(true, false)
{
}
virtual bool load(const OUString&, const OUString& rURL, const OUString&, SfxFilterFlags,
SotClipboardFormatId, unsigned int) override;
/**
* Ensure CVEs remain unbroken
*/
void testCVEs();
void testRoundtripLossless();
void testRoundtripLossy();
void testReadAlphaLossless();
void testReadAlphaLossy();
void testReadNoAlphaLossless();
void testReadNoAlphaLossy();
CPPUNIT_TEST_SUITE(WebpFilterTest);
CPPUNIT_TEST(testCVEs);
CPPUNIT_TEST(testRoundtripLossless);
CPPUNIT_TEST(testRoundtripLossy);
CPPUNIT_TEST(testReadAlphaLossless);
CPPUNIT_TEST(testReadAlphaLossy);
CPPUNIT_TEST(testReadNoAlphaLossless);
CPPUNIT_TEST(testReadNoAlphaLossy);
CPPUNIT_TEST_SUITE_END();
private:
void testRoundtrip(bool lossy);
void testRead(bool lossy, bool alpha);
};
bool WebpFilterTest::load(const OUString&, const OUString& rURL, const OUString&, SfxFilterFlags,
SotClipboardFormatId, unsigned int)
{
SvFileStream aFileStream(rURL, StreamMode::READ);
Graphic aGraphic;
return ImportWebpGraphic(aFileStream, aGraphic);
}
void WebpFilterTest::testCVEs()
{
#ifndef DISABLE_CVE_TESTS
testDir(OUString(), m_directories.getURLFromSrc(u"/vcl/qa/cppunit/graphicfilter/data/webp/"));
#endif
}
void WebpFilterTest::testRoundtripLossless() { testRoundtrip(false); }
void WebpFilterTest::testRoundtripLossy() { testRoundtrip(true); }
void WebpFilterTest::testRoundtrip(bool lossy)
{
// Do not use just 2x2, lossy saving would change colors.
Bitmap aBitmap(Size(20, 20), vcl::PixelFormat::N24_BPP);
AlphaMask aAlpha(Size(20, 20));
{
BitmapScopedWriteAccess pAccess(aBitmap);
pAccess->SetFillColor(COL_WHITE);
pAccess->FillRect(tools::Rectangle(Point(0, 0), Size(10, 10)));
pAccess->SetFillColor(COL_BLACK);
pAccess->FillRect(tools::Rectangle(Point(10, 0), Size(10, 10)));
pAccess->SetFillColor(COL_LIGHTRED);
pAccess->FillRect(tools::Rectangle(Point(0, 10), Size(10, 10)));
pAccess->SetFillColor(COL_BLUE);
pAccess->FillRect(tools::Rectangle(Point(10, 10), Size(10, 10)));
AlphaScopedWriteAccess pAccessAlpha(aAlpha);
pAccessAlpha->SetFillColor(BitmapColor(0)); // opaque
pAccessAlpha->FillRect(tools::Rectangle(Point(0, 0), Size(10, 10)));
pAccessAlpha->FillRect(tools::Rectangle(Point(10, 0), Size(10, 10)));
pAccessAlpha->FillRect(tools::Rectangle(Point(0, 10), Size(10, 10)));
pAccessAlpha->SetFillColor(BitmapColor(64, 64, 64));
pAccessAlpha->FillRect(tools::Rectangle(Point(10, 10), Size(10, 10)));
}
BitmapEx aBitmapEx(aBitmap, aAlpha);
SvMemoryStream aStream;
GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName(u"webp");
css::uno::Sequence<css::beans::PropertyValue> aFilterData{
comphelper::makePropertyValue("Lossless", !lossy),
comphelper::makePropertyValue("Quality", sal_Int32(100))
};
rFilter.ExportGraphic(Graphic(aBitmapEx), u"none", aStream, nFilterFormat, &aFilterData);
aStream.Seek(STREAM_SEEK_TO_BEGIN);
Graphic aGraphic;
ErrCode bResult = rFilter.ImportGraphic(aGraphic, u"none", aStream);
CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, bResult);
CPPUNIT_ASSERT_EQUAL(GfxLinkType::NativeWebp, aGraphic.GetGfxLink().GetType());
BitmapEx aResultBitmap = aGraphic.GetBitmapEx();
CPPUNIT_ASSERT_EQUAL(Size(20, 20), aResultBitmap.GetSizePixel());
CPPUNIT_ASSERT(aResultBitmap.IsAlpha());
{
Bitmap tmpBitmap = aResultBitmap.GetBitmap();
Bitmap::ScopedReadAccess pAccess(tmpBitmap);
// Note that x,y are swapped.
CPPUNIT_ASSERT_EQUAL(COL_WHITE, Color(pAccess->GetPixel(0, 0)));
CPPUNIT_ASSERT_EQUAL(COL_BLACK, Color(pAccess->GetPixel(0, 19)));
if (lossy)
{
CPPUNIT_ASSERT_LESS(sal_uInt16(3),
pAccess->GetPixel(19, 0).GetColorError(COL_LIGHTRED));
CPPUNIT_ASSERT_LESS(sal_uInt16(3), pAccess->GetPixel(19, 19).GetColorError(COL_BLUE));
}
else
{
CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED, Color(pAccess->GetPixel(19, 0)));
CPPUNIT_ASSERT_EQUAL(COL_BLUE, Color(pAccess->GetPixel(19, 19)));
}
AlphaMask tmpAlpha = aResultBitmap.GetAlpha();
AlphaMask::ScopedReadAccess pAccessAlpha(tmpAlpha);
CPPUNIT_ASSERT_EQUAL(sal_uInt8(0), pAccessAlpha->GetPixelIndex(0, 0));
CPPUNIT_ASSERT_EQUAL(sal_uInt8(0), pAccessAlpha->GetPixelIndex(0, 19));
CPPUNIT_ASSERT_EQUAL(sal_uInt8(0), pAccessAlpha->GetPixelIndex(19, 0));
CPPUNIT_ASSERT_EQUAL(sal_uInt8(64), pAccessAlpha->GetPixelIndex(19, 19));
}
aStream.Seek(STREAM_SEEK_TO_BEGIN);
vcl::GraphicFormatDetector aDetector(aStream, "");
CPPUNIT_ASSERT_EQUAL(true, aDetector.detect());
CPPUNIT_ASSERT_EQUAL(true, aDetector.checkWEBP());
CPPUNIT_ASSERT_EQUAL(OUString(u"WEBP"), aDetector.msDetectedFormat);
}
void WebpFilterTest::testReadAlphaLossless() { testRead(false, true); }
void WebpFilterTest::testReadAlphaLossy() { testRead(true, true); }
void WebpFilterTest::testReadNoAlphaLossless() { testRead(false, false); }
void WebpFilterTest::testReadNoAlphaLossy() { testRead(true, false); }
void WebpFilterTest::testRead(bool lossy, bool alpha)
{
// Read a file created in GIMP and check it's read correctly.
OUString file = m_directories.getURLFromSrc(u"/vcl/qa/cppunit/graphicfilter/data/webp/")
+ (alpha ? u"alpha" : u"noalpha") + "_" + (lossy ? u"lossy" : u"lossless")
+ ".webp";
SvFileStream aFileStream(file, StreamMode::READ);
Graphic aGraphic;
GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
ErrCode bResult = rFilter.ImportGraphic(aGraphic, u"none", aFileStream);
CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, bResult);
CPPUNIT_ASSERT_EQUAL(GfxLinkType::NativeWebp, aGraphic.GetGfxLink().GetType());
BitmapEx aResultBitmap = aGraphic.GetBitmapEx();
CPPUNIT_ASSERT_EQUAL(Size(10, 10), aResultBitmap.GetSizePixel());
CPPUNIT_ASSERT_EQUAL(alpha, aResultBitmap.IsAlpha());
{
Bitmap tmpBitmap = aResultBitmap.GetBitmap();
Bitmap::ScopedReadAccess pAccess(tmpBitmap);
// Note that x,y are swapped.
if (lossy)
CPPUNIT_ASSERT_LESS(sal_uInt16(2), pAccess->GetPixel(0, 0).GetColorError(COL_LIGHTRED));
else
CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED, Color(pAccess->GetPixel(0, 0)));
CPPUNIT_ASSERT_EQUAL(COL_LIGHTBLUE, Color(pAccess->GetPixel(9, 9)));
if (alpha)
{
AlphaMask tmpAlpha = aResultBitmap.GetAlpha();
AlphaMask::ScopedReadAccess pAccessAlpha(tmpAlpha);
CPPUNIT_ASSERT_EQUAL(sal_uInt8(0), pAccessAlpha->GetPixelIndex(0, 0));
CPPUNIT_ASSERT_EQUAL(sal_uInt8(255), pAccessAlpha->GetPixelIndex(0, 9));
}
}
}
CPPUNIT_TEST_SUITE_REGISTRATION(WebpFilterTest);
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|