diff options
author | Sarper Akdemir <sarper.akdemir@collabora.com> | 2023-01-25 15:36:59 +0300 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2023-03-05 10:55:44 +0000 |
commit | c010b8cc712d1336ed79fb220764f1a850dc22e5 (patch) | |
tree | d1de8171b3c5db1fd57dcb795bbb101fbbd1de28 /vcl/qa | |
parent | ef6bf01f1b1d9b93bb306295f4723b52e4e1e5b4 (diff) |
tdf#153180: PngImageWriter add support for gray alpha colortype
8 bit grayscale images can have alpha accompanying it, and
PNG supports 2 channel gray-alpha images.
libpng supports this with the use of colortype
PNG_COLOR_TYPE_GRAY_ALPHA.
This patch adds support for writing GRAY_ALPHA colorType PNG
images.
This is done by expanding what was in place for
ScanlineFormat::N24BitTcRgb with alpha PNG_COLOR_TYPE_RGBA.
Change-Id: I80d462d784f91529eb9371c6bdb029c78f32f81e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/146138
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'vcl/qa')
-rw-r--r-- | vcl/qa/cppunit/png/PngFilterTest.cxx | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/vcl/qa/cppunit/png/PngFilterTest.cxx b/vcl/qa/cppunit/png/PngFilterTest.cxx index dae007eb7898..fc3963c356e8 100644 --- a/vcl/qa/cppunit/png/PngFilterTest.cxx +++ b/vcl/qa/cppunit/png/PngFilterTest.cxx @@ -26,6 +26,7 @@ #include <vcl/filter/PngImageReader.hxx> #include <vcl/filter/PngImageWriter.hxx> #include <vcl/BitmapReadAccess.hxx> +#include <vcl/BitmapMonochromeFilter.hxx> #include <bitmap/BitmapWriteAccess.hxx> #include <vcl/alpha.hxx> #include <vcl/graphicfilter.hxx> @@ -176,6 +177,7 @@ public: void testPngRoundtrip24_8(); void testPngRoundtrip32(); void testPngWrite8BitRGBPalette(); + void testTdf153180MonochromeFilterPngExport(); void testDump(); CPPUNIT_TEST_SUITE(PngFilterTest); @@ -188,6 +190,7 @@ public: CPPUNIT_TEST(testPngRoundtrip32); CPPUNIT_TEST(testPngWrite8BitRGBPalette); CPPUNIT_TEST(testDump); + CPPUNIT_TEST(testTdf153180MonochromeFilterPngExport); CPPUNIT_TEST_SUITE_END(); }; @@ -1958,6 +1961,56 @@ void PngFilterTest::testPngWrite8BitRGBPalette() } } +void PngFilterTest::testTdf153180MonochromeFilterPngExport() +{ + GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter(); + + Graphic aGraphicOriginal; + { + // 3 * 16 bits rgb color alpha, no background chunk + const OUString aURL(getFullUrl(u"bgan6a16.png")); + SvFileStream aFileStream(aURL, StreamMode::READ); + ErrCode aResult = rFilter.ImportGraphic(aGraphicOriginal, aURL, aFileStream); + CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, aResult); + CPPUNIT_ASSERT(aGraphicOriginal.IsAlpha()); + } + + // Apply the monochrome filter to the graphic but keep the alpha. + BitmapEx aBitmapEx(aGraphicOriginal.GetBitmapEx()); + AlphaMask aAlphaMask(aBitmapEx.GetAlphaMask()); + + BitmapEx aTmpBmpEx(aBitmapEx.GetBitmap()); + BitmapFilter::Filter(aTmpBmpEx, BitmapMonochromeFilter{ sal_uInt8{ 127 } }); + + Graphic aGraphicAfterFilter{ BitmapEx(aTmpBmpEx.GetBitmap(), aAlphaMask) }; + CPPUNIT_ASSERT(aGraphicAfterFilter.IsAlpha()); + + // export the resulting graphic + utl::TempFileNamed aTempFile(u"testPngExportTdf153180", true, u".png"); + if (!bKeepTemp) + aTempFile.EnableKillingFile(); + { + SvStream& rStream = *aTempFile.GetStream(StreamMode::WRITE); + vcl::PngImageWriter aPngWriter(rStream); + bool bWriteSuccess = aPngWriter.write(aGraphicAfterFilter.GetBitmapEx()); + CPPUNIT_ASSERT_EQUAL(true, bWriteSuccess); + aTempFile.CloseStream(); + } + { + SvStream& rStream = *aTempFile.GetStream(StreamMode::READ); + rStream.Seek(0); + // Import the png and check that it still has alpha + Graphic aGraphic; + ErrCode aResult = rFilter.ImportGraphic(aGraphic, aTempFile.GetURL(), rStream); + CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, aResult); + + // Without the accompanying patch would fail with: + // assertion failed + // -Expression : aGraphic.IsAlpha() + CPPUNIT_ASSERT(aGraphic.IsAlpha()); + } +} + void PngFilterTest::testDump() { utl::TempFileNamed aTempFile; |