2018-04-28 13:37:17 +09:00
|
|
|
/* -*- 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 <cppunit/plugin/TestPlugIn.h>
|
|
|
|
|
2018-05-17 15:15:01 +09:00
|
|
|
#include <com/sun/star/beans/PropertyValue.hpp>
|
|
|
|
|
|
|
|
#include <vcl/bitmapaccess.hxx>
|
2018-04-28 13:37:17 +09:00
|
|
|
#include <vcl/graph.hxx>
|
|
|
|
#include <vcl/graphicfilter.hxx>
|
|
|
|
#include <tools/stream.hxx>
|
|
|
|
|
2018-05-17 15:15:01 +09:00
|
|
|
using namespace css;
|
|
|
|
|
2018-04-28 13:37:17 +09:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class GraphicTest : public CppUnit::TestFixture
|
|
|
|
{
|
|
|
|
void testUnloadedGraphic();
|
2018-05-17 15:15:01 +09:00
|
|
|
void testUnloadedGraphicLoading();
|
2019-09-30 21:36:56 +02:00
|
|
|
void testUnloadedGraphicWmf();
|
2018-04-28 13:37:17 +09:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(GraphicTest);
|
|
|
|
CPPUNIT_TEST(testUnloadedGraphic);
|
2018-05-17 15:15:01 +09:00
|
|
|
CPPUNIT_TEST(testUnloadedGraphicLoading);
|
2019-09-30 21:36:56 +02:00
|
|
|
CPPUNIT_TEST(testUnloadedGraphicWmf);
|
2018-04-28 13:37:17 +09:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
};
|
|
|
|
|
|
|
|
BitmapEx createBitmap()
|
|
|
|
{
|
|
|
|
Bitmap aBitmap(Size(100, 100), 24);
|
|
|
|
aBitmap.Erase(COL_LIGHTRED);
|
|
|
|
|
|
|
|
return BitmapEx(aBitmap);
|
|
|
|
}
|
|
|
|
|
2018-05-17 15:15:01 +09:00
|
|
|
void createBitmapAndExportForType(SvStream& rStream, OUString const& sType)
|
2018-04-28 13:37:17 +09:00
|
|
|
{
|
|
|
|
BitmapEx aBitmapEx = createBitmap();
|
|
|
|
|
2018-05-17 15:15:01 +09:00
|
|
|
uno::Sequence<beans::PropertyValue> aFilterData;
|
|
|
|
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
|
|
|
|
sal_uInt16 nFilterFormat = rGraphicFilter.GetExportFormatNumberForShortName(sType);
|
|
|
|
rGraphicFilter.ExportGraphic(aBitmapEx, "none", rStream, nFilterFormat, &aFilterData);
|
2018-04-28 13:37:17 +09:00
|
|
|
|
|
|
|
rStream.Seek(STREAM_SEEK_TO_BEGIN);
|
|
|
|
}
|
|
|
|
|
2018-05-17 15:15:01 +09:00
|
|
|
Graphic makeUnloadedGraphic(OUString const& sType)
|
2018-04-28 13:37:17 +09:00
|
|
|
{
|
|
|
|
SvMemoryStream aStream;
|
|
|
|
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
|
2018-05-17 15:15:01 +09:00
|
|
|
createBitmapAndExportForType(aStream, sType);
|
2018-04-28 13:37:17 +09:00
|
|
|
return rGraphicFilter.ImportUnloadedGraphic(aStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicTest::testUnloadedGraphic()
|
|
|
|
{
|
|
|
|
// make unloaded test graphic
|
2018-05-17 15:15:01 +09:00
|
|
|
Graphic aGraphic = makeUnloadedGraphic("png");
|
2018-04-28 13:37:17 +09:00
|
|
|
Graphic aGraphic2 = aGraphic;
|
|
|
|
|
|
|
|
// check available
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic2.isAvailable());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic2.makeAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic2.isAvailable());
|
|
|
|
|
|
|
|
// check GetSizePixel doesn't load graphic
|
2018-05-17 15:15:01 +09:00
|
|
|
aGraphic = makeUnloadedGraphic("png");
|
2018-04-28 13:37:17 +09:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(100L, aGraphic.GetSizePixel().Width());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(100L, aGraphic.GetSizePixel().Height());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
|
|
|
|
// check GetSizeBytes loads graphic
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT(aGraphic.GetSizeBytes() > 0);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
}
|
|
|
|
|
2018-05-17 15:15:01 +09:00
|
|
|
void GraphicTest::testUnloadedGraphicLoading()
|
|
|
|
{
|
|
|
|
const OUString aFormats[] = { "png", "gif", "jpg" };
|
|
|
|
|
|
|
|
for (OUString const& sFormat : aFormats)
|
|
|
|
{
|
|
|
|
Graphic aGraphic = makeUnloadedGraphic(sFormat);
|
|
|
|
|
|
|
|
// check available
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(100L, aGraphic.GetSizePixel().Width());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(100L, aGraphic.GetSizePixel().Height());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
|
|
|
|
CPPUNIT_ASSERT(aGraphic.GetSizeBytes() > 0);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
|
|
|
|
Bitmap aBitmap(aGraphic.GetBitmapEx().GetBitmap());
|
|
|
|
|
|
|
|
{
|
|
|
|
Bitmap::ScopedReadAccess pReadAccess(aBitmap);
|
|
|
|
for (long y = 0; y < aGraphic.GetSizePixel().Height(); y++)
|
|
|
|
{
|
|
|
|
for (long x = 0; x < aGraphic.GetSizePixel().Width(); x++)
|
|
|
|
{
|
|
|
|
if (pReadAccess->HasPalette())
|
|
|
|
{
|
|
|
|
Color aColor
|
2019-05-11 11:49:21 +00:00
|
|
|
= pReadAccess->GetPaletteColor(pReadAccess->GetPixelIndex(y, x));
|
2018-05-17 15:15:01 +09:00
|
|
|
CPPUNIT_ASSERT_EQUAL(OUString("ff0000"), aColor.AsRGBHexString());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-11 11:49:21 +00:00
|
|
|
Color aColor = pReadAccess->GetPixel(y, x);
|
2018-05-17 15:15:01 +09:00
|
|
|
if (sFormat != "jpg")
|
|
|
|
CPPUNIT_ASSERT_EQUAL(OUString("ff0000"), aColor.AsRGBHexString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 21:36:56 +02:00
|
|
|
void GraphicTest::testUnloadedGraphicWmf()
|
|
|
|
{
|
|
|
|
// Create some in-memory WMF data, set its own preferred size to 99x99.
|
|
|
|
BitmapEx aBitmapEx = createBitmap();
|
|
|
|
SvMemoryStream aStream;
|
|
|
|
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
|
|
|
|
sal_uInt16 nFilterFormat = rGraphicFilter.GetExportFormatNumberForShortName("wmf");
|
|
|
|
Graphic aGraphic(aBitmapEx);
|
|
|
|
aGraphic.SetPrefSize(Size(99, 99));
|
|
|
|
aGraphic.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
|
|
|
|
rGraphicFilter.ExportGraphic(aGraphic, "none", aStream, nFilterFormat);
|
|
|
|
aStream.Seek(STREAM_SEEK_TO_BEGIN);
|
|
|
|
|
|
|
|
// Now lazy-load this WMF data, with a custom preferred size of 42x42.
|
|
|
|
Size aMtfSize100(42, 42);
|
|
|
|
aGraphic = rGraphicFilter.ImportUnloadedGraphic(aStream, 0, &aMtfSize100);
|
|
|
|
aGraphic.makeAvailable();
|
|
|
|
|
|
|
|
// Without the accompanying fix in place, this test would have failed with:
|
|
|
|
// - Expected: 42x42
|
|
|
|
// - Actual : 99x99
|
2019-10-01 12:26:03 +02:00
|
|
|
// i.e. the custom preferred size was lost after lazy-load.
|
2019-09-30 21:36:56 +02:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Size(42, 42), aGraphic.GetPrefSize());
|
|
|
|
}
|
|
|
|
|
2018-04-28 13:37:17 +09:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(GraphicTest);
|
|
|
|
|
|
|
|
CPPUNIT_PLUGIN_IMPLEMENT();
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|