Rebased ref, commits from common ancestor: commit 0724adb06d7313d20c0fbcf12f2cecb4ef4d4191 Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Wed Mar 31 19:13:17 2021 +0900 Commit: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> CommitDate: Wed Mar 31 22:31:47 2021 +0900
vcl: move MapMode reading adn writing to TypeSerializer remove usage of ReadMapMode and WriteMapMode and add tests Change-Id: I40e1da8aea5c2171d9dbb6343fbf61912e5b3367 diff --git a/include/vcl/TypeSerializer.hxx b/include/vcl/TypeSerializer.hxx index 7be2f54198e7..e85b25b1cfe3 100644 --- a/include/vcl/TypeSerializer.hxx +++ b/include/vcl/TypeSerializer.hxx @@ -50,6 +50,9 @@ public: void readGraphic(Graphic& rGraphic); void writeGraphic(const Graphic& rGraphic); + + void readMapMode(MapMode& rMapMode); + void writeMapMode(MapMode const& rMapMode); }; #endif diff --git a/include/vcl/mapmod.hxx b/include/vcl/mapmod.hxx index e6c96048c90c..9af61f8b7e69 100644 --- a/include/vcl/mapmod.hxx +++ b/include/vcl/mapmod.hxx @@ -59,20 +59,20 @@ public: MapMode& operator=( MapMode&& rMapMode ); bool operator==( const MapMode& rMapMode ) const; bool operator!=( const MapMode& rMapMode ) const - { return !(MapMode::operator==( rMapMode )); } + { + return !(MapMode::operator==( rMapMode )); + } bool IsDefault() const; - friend SvStream& ReadMapMode( SvStream& rIStm, MapMode& rMapMode ); - friend SvStream& WriteMapMode( SvStream& rOStm, const MapMode& rMapMode ); - // tdf#117984 needs to be thread-safe due to being used e.g. in Bitmaps // vcl::ScopedBitmapAccess in parallelized 3D renderer typedef o3tl::cow_wrapper< ImplMapMode, o3tl::ThreadSafeRefCountingPolicy > ImplType; + // If only the map unit is set. + bool IsSimple() const; + private: ImplType mpImplMapMode; - - SAL_DLLPRIVATE bool IsSimple() const; }; template<typename charT, typename traits> diff --git a/vcl/qa/cppunit/TypeSerializerTest.cxx b/vcl/qa/cppunit/TypeSerializerTest.cxx index f8de4e9ce3a2..f402373e6ca3 100644 --- a/vcl/qa/cppunit/TypeSerializerTest.cxx +++ b/vcl/qa/cppunit/TypeSerializerTest.cxx @@ -24,6 +24,7 @@ #include <comphelper/hash.hxx> #include <tools/vcompat.hxx> #include <comphelper/fileformat.h> +#include <tools/fract.hxx> #include <vcl/TypeSerializer.hxx> @@ -67,6 +68,7 @@ private: void testGraphic_Bitmap_NoGfxLink(); void testGraphic_Animation(); void testGraphic_GDIMetaFile(); + void testMapMode(); CPPUNIT_TEST_SUITE(TypeSerializerTest); CPPUNIT_TEST(testGradient); @@ -74,6 +76,7 @@ private: CPPUNIT_TEST(testGraphic_Bitmap_NoGfxLink); CPPUNIT_TEST(testGraphic_Animation); CPPUNIT_TEST(testGraphic_GDIMetaFile); + CPPUNIT_TEST(testMapMode); CPPUNIT_TEST_SUITE_END(); }; @@ -345,6 +348,42 @@ void TypeSerializerTest::testGraphic_GDIMetaFile() } } +void TypeSerializerTest::testMapMode() +{ + { + MapMode aMapMode(MapUnit::Map100thMM); + + SvMemoryStream aStream; + TypeSerializer aSerializer(aStream); + aSerializer.writeMapMode(aMapMode); + aStream.Seek(STREAM_SEEK_TO_BEGIN); + + MapMode aReadMapMode; + aSerializer.readMapMode(aReadMapMode); + CPPUNIT_ASSERT_EQUAL(MapUnit::Map100thMM, aReadMapMode.GetMapUnit()); + CPPUNIT_ASSERT_EQUAL(true, aReadMapMode.IsSimple()); + } + { + MapMode aMapMode(MapUnit::MapTwip, Point(5, 10), Fraction(1, 2), Fraction(2, 3)); + + SvMemoryStream aStream; + TypeSerializer aSerializer(aStream); + aSerializer.writeMapMode(aMapMode); + aStream.Seek(STREAM_SEEK_TO_BEGIN); + + MapMode aReadMapMode; + aSerializer.readMapMode(aReadMapMode); + CPPUNIT_ASSERT_EQUAL(MapUnit::MapTwip, aReadMapMode.GetMapUnit()); + CPPUNIT_ASSERT_EQUAL(tools::Long(5), aReadMapMode.GetOrigin().X()); + CPPUNIT_ASSERT_EQUAL(tools::Long(10), aReadMapMode.GetOrigin().Y()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aReadMapMode.GetScaleX().GetNumerator()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aReadMapMode.GetScaleX().GetDenominator()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aReadMapMode.GetScaleY().GetNumerator()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(3), aReadMapMode.GetScaleY().GetDenominator()); + CPPUNIT_ASSERT_EQUAL(false, aReadMapMode.IsSimple()); + } +} + } // namespace CPPUNIT_TEST_SUITE_REGISTRATION(TypeSerializerTest); diff --git a/vcl/source/filter/graphicfilter2.cxx b/vcl/source/filter/graphicfilter2.cxx index 2b66d2d29395..5666497ce08f 100644 --- a/vcl/source/filter/graphicfilter2.cxx +++ b/vcl/source/filter/graphicfilter2.cxx @@ -1062,10 +1062,9 @@ bool GraphicDescriptor::ImpDetectSVM( SvStream& rStm, bool bExtendedInfo ) if( bExtendedInfo ) { MapMode aMapMode; - rStm.SeekRel( 0x06 ); - ReadMapMode( rStm, aMapMode ); TypeSerializer aSerializer(rStm); + aSerializer.readMapMode(aMapMode); aSerializer.readSize(aLogSize); aLogSize = OutputDevice::LogicToLogic( aLogSize, aMapMode, MapMode( MapUnit::Map100thMM ) ); } diff --git a/vcl/source/gdi/TypeSerializer.cxx b/vcl/source/gdi/TypeSerializer.cxx index fb354d7f3dd0..7f0609e01826 100644 --- a/vcl/source/gdi/TypeSerializer.cxx +++ b/vcl/source/gdi/TypeSerializer.cxx @@ -19,6 +19,7 @@ #include <vcl/TypeSerializer.hxx> #include <tools/vcompat.hxx> +#include <tools/fract.hxx> #include <sal/log.hxx> #include <comphelper/fileformat.h> #include <vcl/gdimtf.hxx> @@ -109,7 +110,7 @@ void TypeSerializer::readGfxLink(GfxLink& rGfxLink) if (aCompat.GetVersion() >= 2) { readSize(aSize); - ReadMapMode(mrStream, aMapMode); + readMapMode(aMapMode); bMapAndSizeValid = true; } } @@ -146,7 +147,7 @@ void TypeSerializer::writeGfxLink(const GfxLink& rGfxLink) // Version 2 writeSize(rGfxLink.GetPrefSize()); - WriteMapMode(mrStream, rGfxLink.GetPrefMapMode()); + writeMapMode(rGfxLink.GetPrefMapMode()); } if (rGfxLink.GetDataSize()) @@ -415,4 +416,37 @@ void TypeSerializer::writeGraphic(const Graphic& rGraphic) } } +void TypeSerializer::readMapMode(MapMode& rMapMode) +{ + VersionCompatRead aCompat(mrStream); + sal_uInt16 nTmp16; + Point aOrigin; + Fraction aScaleX; + Fraction aScaleY; + bool bSimple; + + mrStream.ReadUInt16(nTmp16); + MapUnit eUnit = static_cast<MapUnit>(nTmp16); + readPoint(aOrigin); + ReadFraction(mrStream, aScaleX); + ReadFraction(mrStream, aScaleY); + mrStream.ReadCharAsBool(bSimple); + + if (bSimple) + rMapMode = MapMode(eUnit); + else + rMapMode = MapMode(eUnit, aOrigin, aScaleX, aScaleY); +} + +void TypeSerializer::writeMapMode(MapMode const& rMapMode) +{ + VersionCompatWrite aCompat(mrStream, 1); + + mrStream.WriteUInt16(sal_uInt16(rMapMode.GetMapUnit())); + writePoint(rMapMode.GetOrigin()); + WriteFraction(mrStream, rMapMode.GetScaleX()); + WriteFraction(mrStream, rMapMode.GetScaleY()); + mrStream.WriteBool(rMapMode.IsSimple()); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/gdi/gdimtf.cxx b/vcl/source/gdi/gdimtf.cxx index 9d329439de80..7ef6c73ce47b 100644 --- a/vcl/source/gdi/gdimtf.cxx +++ b/vcl/source/gdi/gdimtf.cxx @@ -2667,8 +2667,8 @@ SvStream& ReadGDIMetaFile(SvStream& rIStm, GDIMetaFile& rGDIMetaFile, ImplMetaRe std::unique_ptr<VersionCompatRead> pCompat(new VersionCompatRead(rIStm)); rIStm.ReadUInt32( nStmCompressMode ); - ReadMapMode( rIStm, rGDIMetaFile.m_aPrefMapMode ); TypeSerializer aSerializer(rIStm); + aSerializer.readMapMode(rGDIMetaFile.m_aPrefMapMode); aSerializer.readSize(rGDIMetaFile.m_aPrefSize); rIStm.ReadUInt32( nCount ); @@ -2752,8 +2752,8 @@ SvStream& GDIMetaFile::Write( SvStream& rOStm ) VersionCompatWrite aCompat(rOStm, 1); rOStm.WriteUInt32(static_cast<sal_uInt32>(nStmCompressMode)); - WriteMapMode(rOStm, m_aPrefMapMode); TypeSerializer aSerializer(rOStm); + aSerializer.writeMapMode(m_aPrefMapMode); aSerializer.writeSize(m_aPrefSize); rOStm.WriteUInt32(GetActionSize()); } // VersionCompatWrite dtor writes stuff into the header diff --git a/vcl/source/gdi/mapmod.cxx b/vcl/source/gdi/mapmod.cxx index 2ce154ae08e3..925732a66e39 100644 --- a/vcl/source/gdi/mapmod.cxx +++ b/vcl/source/gdi/mapmod.cxx @@ -133,38 +133,6 @@ bool MapMode::IsDefault() const return mpImplMapMode.same_object(theGlobalDefault::get()); } -SvStream& ReadMapMode( SvStream& rIStm, MapMode& rMapMode ) -{ - VersionCompatRead aCompat(rIStm); - sal_uInt16 nTmp16; - - TypeSerializer aSerializer(rIStm); - - rIStm.ReadUInt16( nTmp16 ); rMapMode.mpImplMapMode->meUnit = static_cast<MapUnit>(nTmp16); - aSerializer.readPoint(rMapMode.mpImplMapMode->maOrigin); - ReadFraction( rIStm, rMapMode.mpImplMapMode->maScaleX ); - ReadFraction( rIStm, rMapMode.mpImplMapMode->maScaleY ); - rIStm.ReadCharAsBool( rMapMode.mpImplMapMode->mbSimple ); - - return rIStm; -} - -SvStream& WriteMapMode( SvStream& rOStm, const MapMode& rMapMode ) -{ - VersionCompatWrite aCompat(rOStm, 1); - - TypeSerializer aSerializer(rOStm); - - rOStm.WriteUInt16( static_cast<sal_uInt16>(rMapMode.mpImplMapMode->meUnit) ); - aSerializer.writePoint(rMapMode.mpImplMapMode->maOrigin); - WriteFraction( rOStm, rMapMode.mpImplMapMode->maScaleX ); - WriteFraction( rOStm, rMapMode.mpImplMapMode->maScaleY ); - rOStm.WriteBool( rMapMode.mpImplMapMode->mbSimple ); - - return rOStm; -} - - MapUnit MapMode::GetMapUnit() const { return mpImplMapMode->meUnit; } const Point& MapMode::GetOrigin() const { return mpImplMapMode->maOrigin; } diff --git a/vcl/source/gdi/metaact.cxx b/vcl/source/gdi/metaact.cxx index 02fce9554f14..cc95892240d6 100644 --- a/vcl/source/gdi/metaact.cxx +++ b/vcl/source/gdi/metaact.cxx @@ -2735,13 +2735,15 @@ void MetaMapModeAction::Write( SvStream& rOStm, ImplMetaWriteData* pData ) { MetaAction::Write(rOStm, pData); VersionCompatWrite aCompat(rOStm, 1); - WriteMapMode( rOStm, maMapMode ); + TypeSerializer aSerializer(rOStm); + aSerializer.writeMapMode(maMapMode); } void MetaMapModeAction::Read( SvStream& rIStm, ImplMetaReadData* ) { VersionCompatRead aCompat(rIStm); - ReadMapMode( rIStm, maMapMode ); + TypeSerializer aSerializer(rIStm); + aSerializer.readMapMode(maMapMode); } MetaFontAction::MetaFontAction() : commit e1c3e81722a2876362645da5b3e30b0a9680a266 Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Thu Mar 18 15:59:20 2021 +0900 Commit: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> CommitDate: Wed Mar 31 19:33:57 2021 +0900 vcl: bring back RGB565 scanline transformer While we don't support this as a Bitmap format anymore, we still need to transform a buffer that is in RGB565 format in some cases. For example backwards compatibility or if a certain bitmap format supports such pixel format. This change also simplifies some scanline transformers by removing code duplication. Change-Id: I64aa258b8b1fbebf0ed174c0d5fdd2f75f382b28 diff --git a/vcl/inc/bitmap/ScanlineTools.hxx b/vcl/inc/bitmap/ScanlineTools.hxx index 98e702549f2b..fcb243e72014 100644 --- a/vcl/inc/bitmap/ScanlineTools.hxx +++ b/vcl/inc/bitmap/ScanlineTools.hxx @@ -16,7 +16,7 @@ namespace vcl::bitmap { -class ScanlineTransformer +class IScanlineTransformer { public: virtual void startLine(sal_uInt8* pLine) = 0; @@ -24,127 +24,163 @@ public: virtual Color readPixel() = 0; virtual void writePixel(Color nColor) = 0; - virtual ~ScanlineTransformer() = default; + virtual ~IScanlineTransformer() = default; }; -class ScanlineTransformer_ARGB final : public ScanlineTransformer +class ScanlineTransformer_RGB565 : public IScanlineTransformer { -private: - sal_uInt8* pData; +protected: + sal_uInt16* mpData; public: - virtual void startLine(sal_uInt8* pLine) override { pData = pLine; } + void startLine(sal_uInt8* pLine) override { mpData = reinterpret_cast<sal_uInt16*>(pLine); } - virtual void skipPixel(sal_uInt32 nPixel) override { pData += nPixel << 2; } + void skipPixel(sal_uInt32 nPixel) override { mpData += nPixel; } - virtual Color readPixel() override + Color readPixel() override { - const Color aColor(ColorTransparency, pData[4], pData[1], pData[2], pData[3]); - pData += 4; - return aColor; + sal_uInt8 R = sal_uInt8((*mpData & 0xf800) >> 8); + sal_uInt8 G = sal_uInt8((*mpData & 0x07e0) >> 3); + sal_uInt8 B = sal_uInt8((*mpData & 0x001f) << 3); + mpData++; + return Color(R, G, B); } - virtual void writePixel(Color nColor) override + void writePixel(Color nColor) override { - *pData++ = nColor.GetAlpha(); - *pData++ = nColor.GetRed(); - *pData++ = nColor.GetGreen(); - *pData++ = nColor.GetBlue(); + sal_uInt16 R = (nColor.GetRed() & 0xf8) << 8; + sal_uInt16 G = (nColor.GetGreen() & 0xfc) << 3; + sal_uInt16 B = (nColor.GetBlue() & 0xf8) >> 3; + + *mpData++ = R | G | B; } }; -class ScanlineTransformer_BGR final : public ScanlineTransformer +class ScanlineTransformerBase : public IScanlineTransformer { -private: - sal_uInt8* pData; +protected: + sal_uInt8* mpData; public: - virtual void startLine(sal_uInt8* pLine) override { pData = pLine; } + ScanlineTransformerBase() + : mpData(nullptr) + { + } - virtual void skipPixel(sal_uInt32 nPixel) override { pData += (nPixel << 1) + nPixel; } + void startLine(sal_uInt8* pLine) override { mpData = pLine; } +}; - virtual Color readPixel() override +class ScanlineTransformer_ARGB final : public ScanlineTransformerBase +{ +public: + void skipPixel(sal_uInt32 nPixel) override { mpData += nPixel << 2; } + + Color readPixel() override { - const Color aColor(pData[2], pData[1], pData[0]); - pData += 3; + const Color aColor(ColorTransparency, mpData[4], mpData[1], mpData[2], mpData[3]); + mpData += 4; return aColor; } - virtual void writePixel(Color nColor) override + void writePixel(Color nColor) override { - *pData++ = nColor.GetBlue(); - *pData++ = nColor.GetGreen(); - *pData++ = nColor.GetRed(); + *mpData++ = nColor.GetAlpha(); + *mpData++ = nColor.GetRed(); + *mpData++ = nColor.GetGreen(); + *mpData++ = nColor.GetBlue(); } }; -class ScanlineTransformer_8BitPalette final : public ScanlineTransformer +class ScanlineTransformer_BGR final : public ScanlineTransformerBase { -private: - sal_uInt8* pData; +public: + void skipPixel(sal_uInt32 nPixel) override { mpData += (nPixel << 1) + nPixel; } + + Color readPixel() override + { + const Color aColor(mpData[2], mpData[1], mpData[0]); + mpData += 3; + return aColor; + } + + void writePixel(Color nColor) override + { + *mpData++ = nColor.GetBlue(); + *mpData++ = nColor.GetGreen(); + *mpData++ = nColor.GetRed(); + } +}; + +class ScanlineTransformerPaletteBase : public ScanlineTransformerBase +{ +protected: const BitmapPalette& mrPalette; public: - explicit ScanlineTransformer_8BitPalette(const BitmapPalette& rPalette) - : pData(nullptr) + ScanlineTransformerPaletteBase(const BitmapPalette& rPalette) + : ScanlineTransformerBase() , mrPalette(rPalette) { } +}; - virtual void startLine(sal_uInt8* pLine) override { pData = pLine; } +class ScanlineTransformer_8BitPalette final : public ScanlineTransformerPaletteBase +{ +public: + explicit ScanlineTransformer_8BitPalette(const BitmapPalette& rPalette) + : ScanlineTransformerPaletteBase(rPalette) + { + } - virtual void skipPixel(sal_uInt32 nPixel) override { pData += nPixel; } + void skipPixel(sal_uInt32 nPixel) override { mpData += nPixel; } - virtual Color readPixel() override + Color readPixel() override { - const sal_uInt8 nIndex(*pData++); + const sal_uInt8 nIndex(*mpData++); if (nIndex < mrPalette.GetEntryCount()) return mrPalette[nIndex]; else return COL_BLACK; } - virtual void writePixel(Color nColor) override + void writePixel(Color nColor) override { - *pData++ = static_cast<sal_uInt8>(mrPalette.GetBestIndex(nColor)); + *mpData++ = static_cast<sal_uInt8>(mrPalette.GetBestIndex(nColor)); } }; -class ScanlineTransformer_4BitPalette final : public ScanlineTransformer +class ScanlineTransformer_4BitPalette final : public ScanlineTransformerPaletteBase { private: - sal_uInt8* pData; - const BitmapPalette& mrPalette; sal_uInt32 mnX; sal_uInt32 mnShift; public: explicit ScanlineTransformer_4BitPalette(const BitmapPalette& rPalette) - : pData(nullptr) - , mrPalette(rPalette) + : ScanlineTransformerPaletteBase(rPalette) , mnX(0) , mnShift(0) { } - virtual void skipPixel(sal_uInt32 nPixel) override + void skipPixel(sal_uInt32 nPixel) override { mnX += nPixel; if (nPixel & 1) // is nPixel an odd number mnShift ^= 4; } - virtual void startLine(sal_uInt8* pLine) override + void startLine(sal_uInt8* pLine) override { - pData = pLine; + ScanlineTransformerBase::startLine(pLine); mnX = 0; mnShift = 4; } - virtual Color readPixel() override + Color readPixel() override { const sal_uInt32 nDataIndex = mnX / 2; - const sal_uInt8 nIndex((pData[nDataIndex] >> mnShift) & 0x0f); + const sal_uInt8 nIndex((mpData[nDataIndex] >> mnShift) & 0x0f); mnX++; mnShift ^= 4; @@ -154,42 +190,39 @@ public: return COL_BLACK; } - virtual void writePixel(Color nColor) override + void writePixel(Color nColor) override { const sal_uInt32 nDataIndex = mnX / 2; const sal_uInt8 nColorIndex = mrPalette.GetBestIndex(nColor); - pData[nDataIndex] |= (nColorIndex & 0x0f) << mnShift; + mpData[nDataIndex] |= (nColorIndex & 0x0f) << mnShift; mnX++; mnShift ^= 4; } }; -class ScanlineTransformer_1BitPalette final : public ScanlineTransformer +class ScanlineTransformer_1BitPalette final : public ScanlineTransformerPaletteBase { private: - sal_uInt8* pData; - const BitmapPalette& mrPalette; sal_uInt32 mnX; public: explicit ScanlineTransformer_1BitPalette(const BitmapPalette& rPalette) - : pData(nullptr) - , mrPalette(rPalette) + : ScanlineTransformerPaletteBase(rPalette) , mnX(0) { } - virtual void skipPixel(sal_uInt32 nPixel) override { mnX += nPixel; } + void skipPixel(sal_uInt32 nPixel) override { mnX += nPixel; } - virtual void startLine(sal_uInt8* pLine) override + void startLine(sal_uInt8* pLine) override { - pData = pLine; + ScanlineTransformerBase::startLine(pLine); mnX = 0; } - virtual Color readPixel() override + Color readPixel() override { - const sal_uInt8 nIndex((pData[mnX >> 3] >> (7 - (mnX & 7))) & 1); + const sal_uInt8 nIndex((mpData[mnX >> 3] >> (7 - (mnX & 7))) & 1); mnX++; if (nIndex < mrPalette.GetEntryCount()) @@ -198,18 +231,18 @@ public: return COL_BLACK; } - virtual void writePixel(Color nColor) override + void writePixel(Color nColor) override { if (mrPalette.GetBestIndex(nColor) & 1) - pData[mnX >> 3] |= 1 << (7 - (mnX & 7)); + mpData[mnX >> 3] |= 1 << (7 - (mnX & 7)); else - pData[mnX >> 3] &= ~(1 << (7 - (mnX & 7))); + mpData[mnX >> 3] &= ~(1 << (7 - (mnX & 7))); mnX++; } }; -std::unique_ptr<ScanlineTransformer> getScanlineTransformer(sal_uInt16 nBits, - const BitmapPalette& rPalette) +std::unique_ptr<IScanlineTransformer> getScanlineTransformer(sal_uInt16 nBits, + const BitmapPalette& rPalette) { switch (nBits) { @@ -219,6 +252,8 @@ std::unique_ptr<ScanlineTransformer> getScanlineTransformer(sal_uInt16 nBits, return std::make_unique<ScanlineTransformer_4BitPalette>(rPalette); case 8: return std::make_unique<ScanlineTransformer_8BitPalette>(rPalette); + case 16: + return std::make_unique<ScanlineTransformer_RGB565>(); case 24: return std::make_unique<ScanlineTransformer_BGR>(); case 32: diff --git a/vcl/qa/cppunit/ScanlineToolsTest.cxx b/vcl/qa/cppunit/ScanlineToolsTest.cxx index 3a4fc7da9348..8f5f0c0a2f5c 100644 --- a/vcl/qa/cppunit/ScanlineToolsTest.cxx +++ b/vcl/qa/cppunit/ScanlineToolsTest.cxx @@ -19,6 +19,7 @@ class ScanlineToolsTest : public CppUnit::TestFixture { void ScanlineTransformer_32_ARGB(); void ScanlineTransformer_24_BGR(); + void ScanlineTransformer_16_RGB565(); void ScanlineTransformer_8bit_Palette(); void ScanlineTransformer_4bit_Palette(); void ScanlineTransformer_1bit_Palette(); @@ -26,6 +27,7 @@ class ScanlineToolsTest : public CppUnit::TestFixture CPPUNIT_TEST_SUITE(ScanlineToolsTest); CPPUNIT_TEST(ScanlineTransformer_32_ARGB); CPPUNIT_TEST(ScanlineTransformer_24_BGR); + CPPUNIT_TEST(ScanlineTransformer_16_RGB565); CPPUNIT_TEST(ScanlineTransformer_8bit_Palette); CPPUNIT_TEST(ScanlineTransformer_4bit_Palette); CPPUNIT_TEST(ScanlineTransformer_1bit_Palette); @@ -35,8 +37,7 @@ class ScanlineToolsTest : public CppUnit::TestFixture void ScanlineToolsTest::ScanlineTransformer_32_ARGB() { BitmapPalette aPalette; - std::unique_ptr<vcl::bitmap::ScanlineTransformer> pScanlineTransformer - = vcl::bitmap::getScanlineTransformer(32, aPalette); + auto pScanlineTransformer = vcl::bitmap::getScanlineTransformer(32, aPalette); std::vector<sal_uInt8> aScanLine(5 * 4, 0); // 5 * 4 BytesPerPixel pScanlineTransformer->startLine(aScanLine.data()); @@ -64,8 +65,7 @@ void ScanlineToolsTest::ScanlineTransformer_32_ARGB() void ScanlineToolsTest::ScanlineTransformer_24_BGR() { BitmapPalette aPalette; - std::unique_ptr<vcl::bitmap::ScanlineTransformer> pScanlineTransformer - = vcl::bitmap::getScanlineTransformer(24, aPalette); + auto pScanlineTransformer = vcl::bitmap::getScanlineTransformer(24, aPalette); std::vector<sal_uInt8> aScanLine(5 * 3, 0); // 5 * 3 BytesPerPixel pScanlineTransformer->startLine(aScanLine.data()); @@ -90,6 +90,51 @@ void ScanlineToolsTest::ScanlineTransformer_24_BGR() } } +void ScanlineToolsTest::ScanlineTransformer_16_RGB565() +{ + BitmapPalette aPalette; + auto pScanlineTransformer = vcl::bitmap::getScanlineTransformer(16, aPalette); + + // Test writing - we apply colors which will be written into the scanline + // in the R5G6B5 format + std::vector<sal_uInt8> aScanLine(5 * 2, 0); // 5 * 2 BytesPerPixel + pScanlineTransformer->startLine(aScanLine.data()); + + std::vector<Color> aColors{ + Color(ColorTransparency, 0, 10, 250, 120), Color(ColorTransparency, 50, 30, 230, 110), + Color(ColorTransparency, 100, 50, 210, 100), Color(ColorTransparency, 150, 70, 190, 90), + Color(ColorTransparency, 200, 90, 170, 80), + }; + + for (Color const& aColor : aColors) + { + pScanlineTransformer->writePixel(aColor); + } + + std::vector<sal_uInt8> aExpectedBytes{ 207, 15, 45, 31, 140, 54, 235, 69, 74, 93 }; + + for (size_t i = 0; i < aScanLine.size(); ++i) + { + CPPUNIT_ASSERT_EQUAL(int(aExpectedBytes[i]), int(aScanLine[i])); + } + + // Test reading - we insert a scanline in R5G6B5 format and read + // the colors from it + + pScanlineTransformer->startLine(aScanLine.data()); + + std::vector<Color> aExpectedColors{ + Color(8, 248, 120), Color(24, 228, 104), Color(48, 208, 96), + Color(64, 188, 88), Color(88, 168, 80), + }; + + for (size_t i = 0; i < aExpectedColors.size(); ++i) + { + Color aColor = pScanlineTransformer->readPixel(); + CPPUNIT_ASSERT_EQUAL(aExpectedColors[i], aColor); + } +} + void ScanlineToolsTest::ScanlineTransformer_8bit_Palette() { std::vector<Color> aColors{ @@ -102,8 +147,7 @@ void ScanlineToolsTest::ScanlineTransformer_8bit_Palette() for (size_t i = 0; i < aColors.size(); ++i) aPalette[i] = aColors[i]; - std::unique_ptr<vcl::bitmap::ScanlineTransformer> pScanlineTransformer - = vcl::bitmap::getScanlineTransformer(8, aPalette); + auto pScanlineTransformer = vcl::bitmap::getScanlineTransformer(8, aPalette); std::vector<sal_uInt8> aScanLine(5, 0); // 5 * 1 BytesPerPixel pScanlineTransformer->startLine(aScanLine.data()); @@ -142,8 +186,7 @@ void ScanlineToolsTest::ScanlineTransformer_4bit_Palette() aPalette[i] = aColors[i]; } - std::unique_ptr<vcl::bitmap::ScanlineTransformer> pScanlineTransformer - = vcl::bitmap::getScanlineTransformer(4, aPalette); + auto pScanlineTransformer = vcl::bitmap::getScanlineTransformer(4, aPalette); std::vector<sal_uInt8> aScanLine(3, 0); // 6 * 0.5 BytesPerPixel pScanlineTransformer->startLine(aScanLine.data()); @@ -182,8 +225,7 @@ void ScanlineToolsTest::ScanlineTransformer_1bit_Palette() aPalette[0] = Color(10, 250, 120); aPalette[1] = Color(110, 150, 70); - std::unique_ptr<vcl::bitmap::ScanlineTransformer> pScanlineTransformer - = vcl::bitmap::getScanlineTransformer(1, aPalette); + auto pScanlineTransformer = vcl::bitmap::getScanlineTransformer(1, aPalette); std::vector<sal_uInt8> aScanLine(2, 0); // 13 * 1/8 BytesPerPixel pScanlineTransformer->startLine(aScanLine.data()); commit 8fd02b5f2ae9ae3ab5e0d80232fc494f8e2fbe1d Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Sun Mar 7 13:48:39 2021 +0900 Commit: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> CommitDate: Wed Mar 31 19:15:21 2021 +0900 vcl: add PNG writer based on libpng Change-Id: I52ffd1b286162ee0dd9f694c4f3210385f71daf8 diff --git a/include/vcl/filter/PngImageWriter.hxx b/include/vcl/filter/PngImageWriter.hxx new file mode 100644 index 000000000000..4f64a028af53 --- /dev/null +++ b/include/vcl/filter/PngImageWriter.hxx @@ -0,0 +1,50 @@ +/* -*- 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 <vcl/dllapi.h> +#include <com/sun/star/task/XStatusIndicator.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/uno/Sequence.hxx> +#include <tools/stream.hxx> +#include <vcl/bitmapex.hxx> + +#pragma once + +namespace vcl +{ +class VCL_DLLPUBLIC PngImageWriter +{ + SvStream& mrStream; + css::uno::Reference<css::task::XStatusIndicator> mxStatusIndicator; + + int mnCompressionLevel; + bool mbInterlaced; + +public: + PngImageWriter(SvStream& rStream); + + virtual ~PngImageWriter() {} + + void setParameters(css::uno::Sequence<css::beans::PropertyValue> const& rParameters) + { + for (auto const& rValue : rParameters) + { + if (rValue.Name == "Compression") + rValue.Value >>= mnCompressionLevel; + else if (rValue.Name == "Interlaced") + rValue.Value >>= mbInterlaced; + } + } + bool write(BitmapEx& rBitmap); +}; + +} // namespace vcl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk index df1fc1136fb1..fac00b731409 100644 --- a/vcl/Library_vcl.mk +++ b/vcl/Library_vcl.mk @@ -480,6 +480,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/filter/wmf/wmfexternal \ vcl/source/filter/wmf/wmfwr \ vcl/source/filter/png/PngImageReader \ + vcl/source/filter/png/PngImageWriter \ vcl/source/filter/png/pngwrite \ vcl/source/font/Feature \ vcl/source/font/FeatureCollector \ diff --git a/vcl/qa/cppunit/png/PngFilterTest.cxx b/vcl/qa/cppunit/png/PngFilterTest.cxx index c167c4c9c636..1c5f29b40abe 100644 --- a/vcl/qa/cppunit/png/PngFilterTest.cxx +++ b/vcl/qa/cppunit/png/PngFilterTest.cxx @@ -24,14 +24,20 @@ #include <test/bootstrapfixture.hxx> #include <tools/stream.hxx> #include <vcl/filter/PngImageReader.hxx> +#include <vcl/filter/PngImageWriter.hxx> #include <vcl/BitmapReadAccess.hxx> +#include <bitmap/BitmapWriteAccess.hxx> #include <vcl/alpha.hxx> #include <vcl/graphicfilter.hxx> +#include <unotools/tempfile.hxx> using namespace css; class PngFilterTest : public test::BootstrapFixture { + // Should keep the temp files (should be false) + static constexpr bool bKeepTemp = true; + OUString maDataUrl; OUString getFullUrl(std::u16string_view sFileName) @@ -48,10 +54,16 @@ public: void testPng(); void testMsGifInPng(); + void testPngRoundtrip8BitGrey(); + void testPngRoundtrip24(); + void testPngRoundtrip32(); CPPUNIT_TEST_SUITE(PngFilterTest); CPPUNIT_TEST(testPng); CPPUNIT_TEST(testMsGifInPng); + CPPUNIT_TEST(testPngRoundtrip8BitGrey); + CPPUNIT_TEST(testPngRoundtrip24); + CPPUNIT_TEST(testPngRoundtrip32); CPPUNIT_TEST_SUITE_END(); }; @@ -245,6 +257,106 @@ void PngFilterTest::testMsGifInPng() CPPUNIT_ASSERT(aGraphic.IsAnimated()); } +void PngFilterTest::testPngRoundtrip8BitGrey() +{ + utl::TempFile aTempFile("testPngRoundtrip8BitGrey"); + if (!bKeepTemp) + aTempFile.EnableKillingFile(); + { + SvStream& rStream = *aTempFile.GetStream(StreamMode::WRITE); + Bitmap aBitmap(Size(16, 16), vcl::PixelFormat::N8_BPP, &Bitmap::GetGreyPalette(256)); + { + BitmapScopedWriteAccess pWriteAccess(aBitmap); + pWriteAccess->Erase(COL_BLACK); + for (int i = 0; i < 8; ++i) + { + for (int j = 0; j < 8; ++j) + { + pWriteAccess->SetPixel(i, j, COL_GRAY); + } + } + for (int i = 8; i < 16; ++i) + { + for (int j = 8; j < 16; ++j) + { + pWriteAccess->SetPixel(i, j, COL_LIGHTGRAY); + } + } + } + BitmapEx aBitmapEx(aBitmap); + + vcl::PngImageWriter aPngWriter(rStream); + CPPUNIT_ASSERT_EQUAL(true, aPngWriter.write(aBitmapEx)); + aTempFile.CloseStream(); + } + { + SvStream& rStream = *aTempFile.GetStream(StreamMode::READ); + + vcl::PngImageReader aPngReader(rStream); + BitmapEx aBitmapEx; + CPPUNIT_ASSERT_EQUAL(true, aPngReader.read(aBitmapEx)); + + CPPUNIT_ASSERT_EQUAL(16L, aBitmapEx.GetSizePixel().Width()); + CPPUNIT_ASSERT_EQUAL(16L, aBitmapEx.GetSizePixel().Height()); + + CPPUNIT_ASSERT_EQUAL(COL_GRAY, aBitmapEx.GetPixelColor(0, 0)); + CPPUNIT_ASSERT_EQUAL(COL_LIGHTGRAY, aBitmapEx.GetPixelColor(15, 15)); + CPPUNIT_ASSERT_EQUAL(COL_BLACK, aBitmapEx.GetPixelColor(15, 0)); + CPPUNIT_ASSERT_EQUAL(COL_BLACK, aBitmapEx.GetPixelColor(0, 15)); + } +} + +void PngFilterTest::testPngRoundtrip24() +{ + utl::TempFile aTempFile("testPngRoundtrip24"); + if (!bKeepTemp) + aTempFile.EnableKillingFile(); + { + SvStream& rStream = *aTempFile.GetStream(StreamMode::WRITE); + Bitmap aBitmap(Size(16, 16), vcl::PixelFormat::N24_BPP); + { + BitmapScopedWriteAccess pWriteAccess(aBitmap); + pWriteAccess->Erase(COL_BLACK); + for (int i = 0; i < 8; ++i) + { + for (int j = 0; j < 8; ++j) + { + pWriteAccess->SetPixel(i, j, COL_LIGHTRED); + } + } + for (int i = 8; i < 16; ++i) + { + for (int j = 8; j < 16; ++j) + { + pWriteAccess->SetPixel(i, j, COL_LIGHTBLUE); + } + } + } + BitmapEx aBitmapEx(aBitmap); + + vcl::PngImageWriter aPngWriter(rStream); + CPPUNIT_ASSERT_EQUAL(true, aPngWriter.write(aBitmapEx)); + } + { + SvStream& rStream = *aTempFile.GetStream(StreamMode::READ); + rStream.Seek(0); + + vcl::PngImageReader aPngReader(rStream); + BitmapEx aBitmapEx; + CPPUNIT_ASSERT_EQUAL(true, aPngReader.read(aBitmapEx)); + + CPPUNIT_ASSERT_EQUAL(16L, aBitmapEx.GetSizePixel().Width()); + CPPUNIT_ASSERT_EQUAL(16L, aBitmapEx.GetSizePixel().Height()); + + CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED, aBitmapEx.GetPixelColor(0, 0)); + CPPUNIT_ASSERT_EQUAL(COL_LIGHTBLUE, aBitmapEx.GetPixelColor(15, 15)); + CPPUNIT_ASSERT_EQUAL(COL_BLACK, aBitmapEx.GetPixelColor(15, 0)); + CPPUNIT_ASSERT_EQUAL(COL_BLACK, aBitmapEx.GetPixelColor(0, 15)); + } +} + +void PngFilterTest::testPngRoundtrip32() {} + CPPUNIT_TEST_SUITE_REGISTRATION(PngFilterTest); CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/vcl/source/filter/png/PngImageWriter.cxx b/vcl/source/filter/png/PngImageWriter.cxx new file mode 100644 index 000000000000..c1e638e0aad0 --- /dev/null +++ b/vcl/source/filter/png/PngImageWriter.cxx @@ -0,0 +1,143 @@ +/* -*- 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 <vcl/filter/PngImageWriter.hxx> +#include <png.h> +#include <bitmap/BitmapWriteAccess.hxx> +#include <vcl/bitmap.hxx> + +namespace vcl +{ +static void lclWriteStream(png_structp pPng, png_bytep pData, png_size_t pDataSize) +{ + png_voidp pIO = png_get_io_ptr(pPng); + + if (pIO == nullptr) + return; + + SvStream* pStream = static_cast<SvStream*>(pIO); + + sal_Size nBytesWritten = pStream->WriteBytes(pData, pDataSize); + + if (nBytesWritten != pDataSize) + png_error(pPng, "Write Error"); +} + +bool pngWrite(SvStream& rStream, BitmapEx& rBitmapEx, int nCompressionLevel) +{ + png_structp pPng = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); + + if (!pPng) + return false; + + png_infop pInfo = png_create_info_struct(pPng); + if (!pInfo) + { + png_destroy_write_struct(&pPng, nullptr); + return false; + } + + Bitmap aBitmap; + + if (setjmp(png_jmpbuf(pPng))) + { + png_destroy_read_struct(&pPng, &pInfo, nullptr); + return false; + } + + // Set our custom stream writer + png_set_write_fn(pPng, &rStream, lclWriteStream, nullptr); + + aBitmap = rBitmapEx.GetBitmap(); + + { + Bitmap::ScopedReadAccess pAccess(aBitmap); + Size aSize = rBitmapEx.GetSizePixel(); + + int bitDepth = -1; + int colorType = -1; + + /* PNG_COLOR_TYPE_GRAY (1, 2, 4, 8, 16) + PNG_COLOR_TYPE_GRAY_ALPHA (8, 16) + PNG_COLOR_TYPE_PALETTE (bit depths 1, 2, 4, 8) + PNG_COLOR_TYPE_RGB (bit_depths 8, 16) + PNG_COLOR_TYPE_RGB_ALPHA (bit_depths 8, 16) + PNG_COLOR_MASK_PALETTE + PNG_COLOR_MASK_COLOR + PNG_COLOR_MASK_ALPHA + */ + auto eScanlineFormat = pAccess->GetScanlineFormat(); + if (eScanlineFormat == ScanlineFormat::N8BitPal && aBitmap.HasGreyPalette8Bit()) + { + colorType = PNG_COLOR_TYPE_GRAY; + bitDepth = 8; + } + else if (eScanlineFormat == ScanlineFormat::N24BitTcBgr + || eScanlineFormat == ScanlineFormat::N24BitTcRgb) + { + colorType = PNG_COLOR_TYPE_RGB; + bitDepth = 8; + if (eScanlineFormat == ScanlineFormat::N24BitTcBgr) + png_set_bgr(pPng); + } + else + { + return false; + } + + png_set_compression_level(pPng, nCompressionLevel); + + int interlaceType = PNG_INTERLACE_NONE; + int compressionType = PNG_COMPRESSION_TYPE_DEFAULT; + int filterMethod = PNG_FILTER_TYPE_DEFAULT; + + png_set_IHDR(pPng, pInfo, aSize.Width(), aSize.Height(), bitDepth, colorType, interlaceType, + compressionType, filterMethod); + + png_write_info(pPng, pInfo); + + int nNumberOfPasses = 1; + + Scanline pSourcePointer; + + tools::Long nHeight = pAccess->Height(); + + for (int nPass = 0; nPass < nNumberOfPasses; nPass++) + { + for (tools::Long y = 0; y <= nHeight; y++) + { + pSourcePointer = pAccess->GetScanline(y); + png_write_rows(pPng, &pSourcePointer, 1); + } + } + } + + png_write_end(pPng, pInfo); + + png_destroy_write_struct(&pPng, &pInfo); + + return true; +} + +PngImageWriter::PngImageWriter(SvStream& rStream) + : mrStream(rStream) + , mnCompressionLevel(6) + , mbInterlaced(false) +{ +} + +bool PngImageWriter::write(BitmapEx& rBitmapEx) +{ + return pngWrite(mrStream, rBitmapEx, mnCompressionLevel); +} + +} // namespace vcl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits