/* -*- 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/.
*/
CPPUNIT_TEST_FIXTURE(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(u"wmf");
Graphic aGraphic(aBitmapEx);
aGraphic.SetPrefSize(Size(99, 99));
aGraphic.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
rGraphicFilter.ExportGraphic(aGraphic, u"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 // i.e. the custom preferred size was lost after lazy-load.
CPPUNIT_ASSERT_EQUAL(Size(42, 42), aGraphic.GetPrefSize());
}
CPPUNIT_TEST_FIXTURE(GraphicTest, testUnloadedGraphicAlpha)
{ // make unloaded test graphic with alpha
Graphic aGraphic = makeUnloadedGraphic(u"png", true);
// Without the accompanying fix in place, this test would have failed with: // - Expected: 400x363 // - Actual : 42x42 // i.e. a mm100 size was used as a hint and the inch size was set for a non-matching unit.
CPPUNIT_ASSERT_EQUAL(Size(400, 363), aGraphic.GetPrefSize());
}
// Save as WMF.
utl::TempFileNamed aTempFile;
aTempFile.EnableKillingFile();
sal_uInt16 nFormat = rGraphicFilter.GetExportFormatNumberForShortName(u"WMF");
SvStream& rOutStream = *aTempFile.GetStream(StreamMode::READWRITE);
rGraphicFilter.ExportGraphic(aGraphic, u"", rOutStream, nFormat);
// Check if we preserved the WMF data perfectly.
sal_uInt64 nActualSize = rOutStream.TellEnd();
// Without the accompanying fix in place, this test would have failed with: // - Expected: 6475 // - Actual : 2826 // i.e. we lost some of the WMF data on roundtrip.
CPPUNIT_ASSERT_EQUAL(nExpectedSize, nActualSize);
}
for (bool useConvertMetafile : { false, true })
{ // Save as WMF.
utl::TempFileNamed aTempFile;
SvStream& rOutStream = *aTempFile.GetStream(StreamMode::READWRITE); if (useConvertMetafile)
ConvertGraphicToWMF(aGraphic, rOutStream, nullptr); else
{
sal_uInt16 nFormat = rGraphicFilter.GetExportFormatNumberForShortName(u"WMF");
rGraphicFilter.ExportGraphic(aGraphic, u"", rOutStream, nFormat);
}
CPPUNIT_ASSERT_EQUAL(nExpectedSize, rOutStream.TellEnd());
rOutStream.Seek(0);
Graphic aNewGraphic = rGraphicFilter.ImportUnloadedGraphic(rOutStream); // Check that reading the WMF back preserves the EMF+ actions in it.
CPPUNIT_ASSERT_GREATER(0, getEmfPlusActionsCount(aNewGraphic)); // EmfReader::ReadEnhWMF() drops non-EMF+ drawing actions if EMF+ is found.
CPPUNIT_ASSERT_EQUAL(0, getPolygonActionsCount(aNewGraphic));
// With EMF+ disabled there should be no EMF+ actions. auto aVectorGraphicData = std::make_shared<VectorGraphicData>(
aNewGraphic.GetGfxLink().getDataContainer(), VectorGraphicDataType::Wmf);
aVectorGraphicData->setEnableEMFPlus(false);
Graphic aNoEmfPlusGraphic(aVectorGraphicData);
CPPUNIT_ASSERT_EQUAL(0, getEmfPlusActionsCount(aNoEmfPlusGraphic));
CPPUNIT_ASSERT_GREATER(0, getPolygonActionsCount(aNoEmfPlusGraphic));
}
}
CPPUNIT_TEST_FIXTURE(GraphicTest, testEmfToWmfConversion)
{ // Load EMF data.
GraphicFilter aGraphicFilter;
test::Directories aDirectories;
OUString aURL = aDirectories.getURLFromSrc(DATA_DIRECTORY) + "to-wmf.emf";
SvFileStream aStream(aURL, StreamMode::READ);
Graphic aGraphic; // This similar to an application/x-openoffice-wmf mime type in manifest.xml in the ODF case.
sal_uInt16 nFormatEMF = aGraphicFilter.GetImportFormatNumberForShortName(u"EMF");
CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE,
aGraphicFilter.ImportGraphic(aGraphic, u"", aStream, nFormatEMF));
CPPUNIT_ASSERT_EQUAL(VectorGraphicDataType::Emf, aGraphic.getVectorGraphicData()->getType());
// Without the accompanying fix in place, this test would have failed with: // - Expected: WMF // - Actual : EMF // i.e. EMF data was requested to be converted to WMF, but the output was still EMF.
CPPUNIT_ASSERT_EQUAL(u"WMF"_ustr,
vcl::getImportFormatShortName(aDetector.getMetadata().mnFormat));
// Import the WMF result and check for traces of EMF+ in it.
Graphic aWmfGraphic;
aGraphicStream.Seek(0);
sal_uInt16 nFormatWMF = aGraphicFilter.GetImportFormatNumberForShortName(u"WMF");
CPPUNIT_ASSERT_EQUAL(
ERRCODE_NONE, aGraphicFilter.ImportGraphic(aWmfGraphic, u"", aGraphicStream, nFormatWMF)); int nCommentCount = 0; for (size_t i = 0; i < aWmfGraphic.GetGDIMetaFile().GetActionSize(); ++i)
{
MetaAction* pAction = aWmfGraphic.GetGDIMetaFile().GetAction(i); if (pAction->GetType() == MetaActionType::COMMENT)
{ auto pComment = static_cast<MetaCommentAction*>(pAction); if (pComment->GetComment().startsWith("EMF_PLUS"))
{
++nCommentCount;
}
}
} // Without the accompanying fix in place, this test would have failed with: // - Expected less or equal than: 4 // - Actual : 8 // i.e. even more EMF+ comments were left in the WMF output. The ideal would be to get this down // to 0, though.
CPPUNIT_ASSERT_LESSEQUAL(4, nCommentCount);
}
CPPUNIT_TEST_FIXTURE(GraphicTest, testSwappingGraphic_PNG_WithGfxLink)
{ // Prepare Graphic from a PNG image first
Graphic aGraphic = makeUnloadedGraphic(u"png");
CPPUNIT_ASSERT_EQUAL(true, aGraphic.IsGfxLink()); // Expect the compressed png is be at least W * H / 2
CPPUNIT_ASSERT_LESS(sal_uInt32(120 * 100 / 2), aGraphic.GetGfxLink().GetDataSize());
// We loaded the Graphic and made it available
CPPUNIT_ASSERT_EQUAL(false, aGraphic.ImplGetImpGraphic()->isSwappedOut()); // Get the declared byte size of the graphic
sal_uLong rByteSize = aGraphic.GetSizeBytes();
CPPUNIT_TEST_FIXTURE(GraphicTest, testSwappingGraphic_PNG_WithoutGfxLink)
{ // Prepare Graphic from a PNG image first
// Make sure to construct the Graphic from BitmapEx, so that we // don't have the GfxLink present.
Graphic aGraphic(makeUnloadedGraphic(u"png").GetBitmapEx());
// We loaded the Graphic and made it available
CPPUNIT_ASSERT_EQUAL(false, aGraphic.ImplGetImpGraphic()->isSwappedOut());
// Get the declared byte size of the graphic
sal_uLong rByteSize = aGraphic.GetSizeBytes();
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->getSwapFileStream() == nullptr);
// Swapping out
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->swapOut());
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->isSwappedOut());
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
// Byte size doesn't change when we swapped out
CPPUNIT_ASSERT_EQUAL(rByteSize, aGraphic.GetSizeBytes());
// reset the checksum to make sure we don't get the cached value
aGraphic.ImplGetImpGraphic()->resetChecksum();
CPPUNIT_ASSERT_EQUAL(aChecksumBeforeSwapping, aGraphic.GetChecksum());
// File shouldn't be available anymore
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->getSwapFileStream() == nullptr);
// Check the bitmap
CPPUNIT_ASSERT_EQUAL(tools::Long(120), aGraphic.GetSizePixel().Width());
CPPUNIT_ASSERT_EQUAL(tools::Long(100), aGraphic.GetSizePixel().Height());
// Check that the state is as expected
CPPUNIT_ASSERT_EQUAL(GraphicType::Bitmap, aGraphic.GetType());
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
// Load the vector graphic auto pVectorData = aGraphic.getVectorGraphicData();
CPPUNIT_ASSERT_EQUAL(true, bool(pVectorData));
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
// Remember checksum so we can compare after swapping back in again
BitmapChecksum aBitmapChecksumBeforeSwapping = aGraphic.GetBitmapEx().GetChecksum();
// Check we are not swapped out yet
CPPUNIT_ASSERT_EQUAL(false, aGraphic.ImplGetImpGraphic()->isSwappedOut());
// Get the declared byte size of the graphic
sal_uLong rByteSize = aGraphic.GetSizeBytes();
CPPUNIT_ASSERT_EQUAL(sal_uLong(223), rByteSize);
// Make sure we don't have a file
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->getSwapFileStream() == nullptr);
// SWAP OUT the Graphic and make sure it's not available currently
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->swapOut());
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->isSwappedOut());
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
// We use GfxLink so no swap file in this case
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->getSwapFileStream() == nullptr);
// Byte size doesn't change when we swapped out
CPPUNIT_ASSERT_EQUAL(rByteSize, aGraphic.GetSizeBytes());
// Compare that the checksum of the bitmap is still the same
CPPUNIT_ASSERT_EQUAL(aBitmapChecksumBeforeSwapping, aGraphic.GetBitmapEx().GetChecksum());
// Swapping out
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->swapOut());
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->isSwappedOut());
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
// Byte size doesn't change when we swapped out // TODO: In case we don't trigger GetBitmapEx (above) the size is 0
CPPUNIT_ASSERT_EQUAL(rByteSize, aGraphic.GetSizeBytes());
// Let's check the swap file
{ // Check the swap file content
SvStream* pStream = aGraphic.ImplGetImpGraphic()->getSwapFileStream();
pStream->Seek(0);
// Check size of the stream
CPPUNIT_ASSERT_EQUAL(sal_uInt64(247), pStream->remainingSize());
// File shouldn't be available anymore
CPPUNIT_ASSERT_EQUAL(static_cast<SvStream*>(nullptr),
aGraphic.ImplGetImpGraphic()->getSwapFileStream());
}
CPPUNIT_TEST_FIXTURE(GraphicTest, testSwappingGraphicProperties_SVG_WithGfxLink)
{ // FIXME: the DPI check should be removed when either (1) the test is fixed to work with // non-default DPI; or (2) unit tests on Windows are made to use svp VCL plugin. if (!IsDefaultDPI()) return;
// We check that Graphic properties like MapMode, PrefSize are properly // restored through a swap cycle
// Check size in pixels
CPPUNIT_ASSERT_EQUAL(tools::Long(51), aGraphic.GetSizePixel().Width());
CPPUNIT_ASSERT_EQUAL(tools::Long(51), aGraphic.GetSizePixel().Height());
// Set and check the PrefSize
CPPUNIT_ASSERT_EQUAL(tools::Long(1349), aGraphic.GetPrefSize().Width());
CPPUNIT_ASSERT_EQUAL(tools::Long(1349), aGraphic.GetPrefSize().Height());
aGraphic.SetPrefSize(Size(200, 100));
CPPUNIT_ASSERT_EQUAL(tools::Long(200), aGraphic.GetPrefSize().Width());
CPPUNIT_ASSERT_EQUAL(tools::Long(100), aGraphic.GetPrefSize().Height());
// SWAP OUT the Graphic and make sure it's not available currently
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->swapOut());
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->isSwappedOut());
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
CPPUNIT_TEST_FIXTURE(GraphicTest, testSwappingGraphicProperties_SVG_WithoutGfxLink)
{ // FIXME: the DPI check should be removed when either (1) the test is fixed to work with // non-default DPI; or (2) unit tests on Windows are made to use svp VCL plugin. if (!IsDefaultDPI()) return;
// Check size in pixels
CPPUNIT_ASSERT_EQUAL(tools::Long(51), aGraphic.GetSizePixel().Width());
CPPUNIT_ASSERT_EQUAL(tools::Long(51), aGraphic.GetSizePixel().Height());
// Set and check the PrefSize
CPPUNIT_ASSERT_EQUAL(tools::Long(1349), aGraphic.GetPrefSize().Width());
CPPUNIT_ASSERT_EQUAL(tools::Long(1349), aGraphic.GetPrefSize().Height());
aGraphic.SetPrefSize(Size(200, 100));
CPPUNIT_ASSERT_EQUAL(tools::Long(200), aGraphic.GetPrefSize().Width());
CPPUNIT_ASSERT_EQUAL(tools::Long(100), aGraphic.GetPrefSize().Height());
// SWAP OUT the Graphic and make sure it's not available currently
CPPUNIT_ASSERT_EQUAL(true, aGraphic.isAvailable());
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->swapOut());
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->isSwappedOut());
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
// Check that the state is as expected
CPPUNIT_ASSERT_EQUAL(GraphicType::Bitmap, aGraphic.GetType());
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
// Remember checksum so we can compare after swapping back in again
BitmapChecksum aBitmapChecksumBeforeSwapping = aGraphic.GetBitmapEx().GetChecksum();
// Check we are not swapped out yet
CPPUNIT_ASSERT_EQUAL(false, aGraphic.ImplGetImpGraphic()->isSwappedOut());
// Get the declared byte size of the graphic
sal_uLong rByteSize = aGraphic.GetSizeBytes();
CPPUNIT_ASSERT_EQUAL(sal_uLong(89552), rByteSize);
// Make sure we don't have a file
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->getSwapFileStream() == nullptr);
// SWAP OUT the Graphic and make sure it's not available currently
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->swapOut());
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->isSwappedOut());
CPPUNIT_ASSERT_EQUAL(false, aGraphic.isAvailable());
// We use GfxLink so no swap file in this case
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->getSwapFileStream() == nullptr);
// Byte size doesn't change when we swapped out
CPPUNIT_ASSERT_EQUAL(rByteSize, aGraphic.GetSizeBytes());
// Compare that the checksum of the bitmap is still the same
CPPUNIT_ASSERT_EQUAL(aBitmapChecksumBeforeSwapping, aGraphic.GetBitmapEx().GetChecksum());
// We loaded the Graphic and made it available
CPPUNIT_ASSERT_EQUAL(false, aGraphic.ImplGetImpGraphic()->isSwappedOut());
// Get the declared byte size of the graphic
sal_uLong rByteSize = aGraphic.GetSizeBytes();
CPPUNIT_ASSERT_EQUAL(true, aGraphic.ImplGetImpGraphic()->getSwapFileStream() == nullptr);
auto xGraphicAfter = xGraphicTransformer->colorChange(
xGraphic, static_cast<sal_Int32>(nColorFrom), nTolerance, static_cast<sal_Int32>(nColorTo), static_cast<sal_Int8>(nColorTo.GetAlpha()));
Graphic aGraphicAfter{ xGraphicAfter }; const BitmapEx& rBitmapAfter = aGraphicAfter.GetBitmapExRef(); const BitmapEx& rBitmapBefore = aGraphic.GetBitmapExRef(); // Without the accompanying fix in place, this test would have failed with: // - Expected: rgba[ff000000] // - Actual : rgba[f00000ff] // i.e. the color change to transparent didn't apply correctly
CPPUNIT_ASSERT_EQUAL(nColorTo, rBitmapAfter.GetPixelColor(386, 140));
// Test if color stayed same on 410,140 // colorChange with nTolerance 15 shouldn't change this pixel.
CPPUNIT_ASSERT_EQUAL(rBitmapBefore.GetPixelColor(410, 140),
rBitmapAfter.GetPixelColor(410, 140));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.