2015-03-25 11:35:55 +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>
|
|
|
|
|
2015-07-13 15:38:09 +01:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2015-03-25 11:35:55 +09:00
|
|
|
#include <vcl/bitmap.hxx>
|
|
|
|
#include <vcl/bmpacc.hxx>
|
2015-07-13 15:38:09 +01:00
|
|
|
#include <vcl/virdev.hxx>
|
2015-03-25 11:35:55 +09:00
|
|
|
|
2015-07-13 15:38:09 +01:00
|
|
|
#include <rtl/strbuf.hxx>
|
2015-03-25 11:35:55 +09:00
|
|
|
#include <tools/stream.hxx>
|
|
|
|
#include <vcl/graphicfilter.hxx>
|
2015-09-01 17:28:21 +01:00
|
|
|
#include <vcl/opengl/OpenGLHelper.hxx>
|
2015-03-25 11:35:55 +09:00
|
|
|
|
|
|
|
#include "BitmapSymmetryCheck.hxx"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
class BitmapTest : public CppUnit::TestFixture
|
|
|
|
{
|
2015-03-29 14:35:11 +09:00
|
|
|
void testConvert();
|
2015-03-25 11:35:55 +09:00
|
|
|
void testScale();
|
2015-07-13 15:38:09 +01:00
|
|
|
void testCRC();
|
2015-03-25 11:35:55 +09:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BitmapTest);
|
2015-03-29 14:35:11 +09:00
|
|
|
CPPUNIT_TEST(testConvert);
|
2015-03-25 11:35:55 +09:00
|
|
|
CPPUNIT_TEST(testScale);
|
2015-07-13 15:38:09 +01:00
|
|
|
CPPUNIT_TEST(testCRC);
|
2015-03-25 11:35:55 +09:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
};
|
|
|
|
|
2015-03-29 14:35:11 +09:00
|
|
|
void BitmapTest::testConvert()
|
|
|
|
{
|
|
|
|
Bitmap aBitmap(Size(10, 10), 8);
|
|
|
|
|
|
|
|
aBitmap.Erase(COL_LIGHTGRAYBLUE);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(8), aBitmap.GetBitCount());
|
|
|
|
{
|
|
|
|
Bitmap::ScopedReadAccess pReadAccess(aBitmap);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(8), pReadAccess->GetBitCount());
|
2015-03-30 23:28:39 +03:00
|
|
|
#if defined WNT
|
2015-09-01 17:28:21 +01:00
|
|
|
if (!OpenGLHelper::isVCLOpenGLEnabled())
|
|
|
|
{
|
|
|
|
// GDI Scanlines padded to DWORD multiples, it seems
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<sal_uLong>(12), pReadAccess->GetScanlineSize());
|
|
|
|
}
|
|
|
|
else
|
2015-03-30 23:28:39 +03:00
|
|
|
#endif
|
2015-09-01 17:28:21 +01:00
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<sal_uLong>(10), pReadAccess->GetScanlineSize());
|
|
|
|
}
|
2015-03-29 14:35:11 +09:00
|
|
|
CPPUNIT_ASSERT(pReadAccess->HasPalette());
|
|
|
|
const BitmapColor& rColor = pReadAccess->GetPaletteColor(pReadAccess->GetPixelIndex(1, 1));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_Int32(204), sal_Int32(rColor.GetRed()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_Int32(204), sal_Int32(rColor.GetGreen()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_Int32(255), sal_Int32(rColor.GetBlue()));
|
|
|
|
}
|
|
|
|
|
|
|
|
aBitmap.Convert(BMP_CONVERSION_24BIT);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_uInt16(24), aBitmap.GetBitCount());
|
|
|
|
{
|
|
|
|
Bitmap::ScopedReadAccess pReadAccess(aBitmap);
|
2015-03-29 20:30:53 +02:00
|
|
|
#if defined LINUX
|
2015-03-29 14:35:11 +09:00
|
|
|
// 24 bit Bitmap on SVP backend uses 32bit BGRX format
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(32), pReadAccess->GetBitCount());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_uLong(40), pReadAccess->GetScanlineSize());
|
2015-03-29 20:30:53 +02:00
|
|
|
#else
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(24), pReadAccess->GetBitCount());
|
2015-03-30 23:28:39 +03:00
|
|
|
#if defined WNT
|
2015-09-01 17:28:21 +01:00
|
|
|
if (!OpenGLHelper::isVCLOpenGLEnabled())
|
|
|
|
{
|
|
|
|
// GDI Scanlines padded to DWORD multiples, it seems
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_uLong(32), pReadAccess->GetScanlineSize());
|
|
|
|
}
|
|
|
|
else
|
2015-03-30 23:28:39 +03:00
|
|
|
#endif
|
2015-09-01 17:28:21 +01:00
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_uLong(30), pReadAccess->GetScanlineSize());
|
|
|
|
}
|
2015-03-29 20:30:53 +02:00
|
|
|
#endif
|
2015-03-29 14:35:11 +09:00
|
|
|
CPPUNIT_ASSERT(!pReadAccess->HasPalette());
|
|
|
|
Color aColor = pReadAccess->GetPixel(0, 0);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_Int32(204), sal_Int32(aColor.GetRed()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_Int32(204), sal_Int32(aColor.GetGreen()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(sal_Int32(255), sal_Int32(aColor.GetBlue()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 11:35:55 +09:00
|
|
|
void BitmapTest::testScale()
|
|
|
|
{
|
2015-03-29 13:33:06 +01:00
|
|
|
const bool bExportBitmap(false);
|
2015-03-25 11:35:55 +09:00
|
|
|
|
|
|
|
Bitmap aBitmap24Bit(Size(10, 10), 24);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(24), aBitmap24Bit.GetBitCount());
|
|
|
|
|
|
|
|
{
|
|
|
|
Bitmap::ScopedWriteAccess aWriteAccess(aBitmap24Bit);
|
|
|
|
aWriteAccess->Erase(COL_WHITE);
|
|
|
|
aWriteAccess->SetLineColor(COL_BLACK);
|
|
|
|
aWriteAccess->DrawRect(Rectangle(1, 1, 8, 8));
|
|
|
|
aWriteAccess->DrawRect(Rectangle(3, 3, 6, 6));
|
|
|
|
}
|
|
|
|
|
|
|
|
BitmapSymmetryCheck aBitmapSymmetryCheck;
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<long>(10), aBitmap24Bit.GetSizePixel().Width());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<long>(10), aBitmap24Bit.GetSizePixel().Height());
|
|
|
|
|
|
|
|
// Check symmetry of the bitmap
|
|
|
|
CPPUNIT_ASSERT(aBitmapSymmetryCheck.check(aBitmap24Bit));
|
|
|
|
|
|
|
|
if (bExportBitmap)
|
|
|
|
{
|
|
|
|
SvFileStream aStream(OUString("~/scale_before.png"), StreamMode::WRITE | StreamMode::TRUNC);
|
|
|
|
GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
|
|
|
|
rFilter.compressAsPNG(aBitmap24Bit, aStream, 9);
|
|
|
|
}
|
|
|
|
|
2015-05-05 14:09:24 +02:00
|
|
|
aBitmap24Bit.Scale(2, 2, BmpScaleFlag::Fast);
|
2015-03-25 11:35:55 +09:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<long>(20), aBitmap24Bit.GetSizePixel().Width());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<long>(20), aBitmap24Bit.GetSizePixel().Height());
|
|
|
|
|
|
|
|
// After scaling the bitmap should still be symmetrical. This check guarantees that
|
|
|
|
// scaling doesn't misalign the bitmap.
|
|
|
|
CPPUNIT_ASSERT(aBitmapSymmetryCheck.check(aBitmap24Bit));
|
|
|
|
|
|
|
|
if (bExportBitmap)
|
|
|
|
{
|
|
|
|
SvFileStream aStream(OUString("~/scale_after.png"), StreamMode::WRITE | StreamMode::TRUNC);
|
|
|
|
GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
|
|
|
|
rFilter.compressAsPNG(aBitmap24Bit, aStream, 9);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 15:38:09 +01:00
|
|
|
typedef std::unordered_map<sal_uInt64, const char *> CRCHash;
|
|
|
|
|
|
|
|
void checkAndInsert(CRCHash &rHash, sal_uInt64 nCRC, const char *pLocation)
|
|
|
|
{
|
|
|
|
auto it = rHash.find(nCRC);
|
|
|
|
if (it != rHash.end()) {
|
|
|
|
OStringBuffer aBuf("CRC collision between ");
|
|
|
|
aBuf.append(pLocation);
|
|
|
|
aBuf.append(" and ");
|
|
|
|
aBuf.append(it->second);
|
|
|
|
aBuf.append(" hash is 0x");
|
|
|
|
aBuf.append((sal_Int64)nCRC, 16);
|
|
|
|
CPPUNIT_FAIL(aBuf.toString().getStr());
|
|
|
|
}
|
|
|
|
rHash[nCRC] = pLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkAndInsert(CRCHash &rHash, Bitmap rBmp, const char *pLocation)
|
|
|
|
{
|
|
|
|
checkAndInsert(rHash, rBmp.GetChecksum(), pLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap getAsBitmap(VclPtr<OutputDevice> pOut)
|
|
|
|
{
|
|
|
|
return pOut->GetBitmap(Point(), pOut->GetOutputSizePixel());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapTest::testCRC()
|
|
|
|
{
|
|
|
|
CRCHash aCRCs;
|
|
|
|
|
|
|
|
Bitmap aBitmap(Size(1023,759), 24, 0);
|
|
|
|
aBitmap.Erase(COL_BLACK);
|
|
|
|
checkAndInsert(aCRCs, aBitmap, "black bitmap");
|
|
|
|
aBitmap.Invert();
|
|
|
|
checkAndInsert(aCRCs, aBitmap, "white bitmap");
|
|
|
|
|
|
|
|
ScopedVclPtrInstance<VirtualDevice> aVDev;
|
2015-09-01 14:11:34 +02:00
|
|
|
aVDev->SetBackground(Wallpaper(COL_WHITE));
|
2015-07-13 15:38:09 +01:00
|
|
|
aVDev->SetOutputSizePixel(Size(1023, 759));
|
2015-09-01 19:33:43 +01:00
|
|
|
|
|
|
|
#if 0 // disabled for now - oddly breaks on OS/X - but why ?
|
2015-07-13 15:38:09 +01:00
|
|
|
Bitmap aWhiteCheck = getAsBitmap(aVDev);
|
|
|
|
CPPUNIT_ASSERT(aCRCs.find(aWhiteCheck.GetChecksum()) != aCRCs.end());
|
2015-09-01 19:33:43 +01:00
|
|
|
#endif
|
2015-07-13 15:38:09 +01:00
|
|
|
|
|
|
|
// a 1x1 black & white checkerboard
|
|
|
|
aVDev->DrawCheckered(Point(), aVDev->GetOutputSizePixel(), 1, 1);
|
|
|
|
Bitmap aChecker = getAsBitmap(aVDev);
|
|
|
|
checkAndInsert(aCRCs, aChecker, "checkerboard");
|
|
|
|
aChecker.Invert();
|
|
|
|
checkAndInsert(aCRCs, aChecker, "inverted checkerboard");
|
|
|
|
}
|
|
|
|
|
2015-03-25 11:35:55 +09:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BitmapTest);
|
|
|
|
|
|
|
|
CPPUNIT_PLUGIN_IMPLEMENT();
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|