vcl/qa/cppunit/png/PngFilterTest.cxx | 49 +++++++++++++++++++++++++++++++ vcl/source/filter/png/PngImageWriter.cxx | 5 +-- 2 files changed, 52 insertions(+), 2 deletions(-)
New commits: commit 3d1032cf6b67f3f6fa539d1d42d681080517d38c Author: offtkp <parisop...@gmail.com> AuthorDate: Tue Jul 5 00:49:14 2022 +0300 Commit: Tomaž Vajngerl <qui...@gmail.com> CommitDate: Tue Jul 19 09:59:27 2022 +0200 Add PngImageWriter 8 bit palette export support Also add test that fills a 256 color palette, exports a 16x16 image and reimports it and checks each pixel color Change-Id: Id53f3116e42d669cd528edfd791a1a981551ae0f Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136817 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <qui...@gmail.com> diff --git a/vcl/qa/cppunit/png/PngFilterTest.cxx b/vcl/qa/cppunit/png/PngFilterTest.cxx index 2eae9544ecbb..64a99756aa89 100644 --- a/vcl/qa/cppunit/png/PngFilterTest.cxx +++ b/vcl/qa/cppunit/png/PngFilterTest.cxx @@ -176,6 +176,7 @@ public: void testPngRoundtrip24_8(); void testPngRoundtrip32(); void testPngWrite1BitRGBPalette(); + void testPngWrite8BitRGBPalette(); CPPUNIT_TEST_SUITE(PngFilterTest); CPPUNIT_TEST(testPng); @@ -186,6 +187,7 @@ public: CPPUNIT_TEST(testPngRoundtrip24_8); CPPUNIT_TEST(testPngRoundtrip32); CPPUNIT_TEST(testPngWrite1BitRGBPalette); + CPPUNIT_TEST(testPngWrite8BitRGBPalette); CPPUNIT_TEST_SUITE_END(); }; @@ -1929,6 +1931,53 @@ void PngFilterTest::testPngWrite1BitRGBPalette() } } +void PngFilterTest::testPngWrite8BitRGBPalette() +{ + SvMemoryStream aExportStream; + BitmapPalette aRedPalette; + aRedPalette.SetEntryCount(256); + for (sal_uInt16 i = 0; i < 256; i++) + { + aRedPalette[i].SetRed(i); + aRedPalette[i].SetGreen(0); + aRedPalette[i].SetBlue(0); + } + { + Bitmap aBitmap(Size(16, 16), vcl::PixelFormat::N8_BPP, &aRedPalette); + { + BitmapScopedWriteAccess pWriteAccessBitmap(aBitmap); + for (int i = 0; i < 16; i++) + { + for (int j = 0; j < 16; j++) + { + pWriteAccessBitmap->SetPixelIndex(i, j, i * 16 + j); + } + } + } + BitmapEx aBitmapEx(aBitmap); + vcl::PngImageWriter aPngWriter(aExportStream); + CPPUNIT_ASSERT_EQUAL(true, aPngWriter.write(aBitmapEx)); + } + aExportStream.Seek(0); + { + vcl::PngImageReader aPngReader(aExportStream); + BitmapEx aBitmapEx; + CPPUNIT_ASSERT_EQUAL(true, aPngReader.read(aBitmapEx)); + + CPPUNIT_ASSERT_EQUAL(16L, aBitmapEx.GetSizePixel().Width()); + CPPUNIT_ASSERT_EQUAL(16L, aBitmapEx.GetSizePixel().Height()); + + for (int i = 0; i < 16; i++) + { + for (int j = 0; j < 16; j++) + { + CPPUNIT_ASSERT_EQUAL(aRedPalette[i * 16 + j].GetRGBColor(), + aBitmapEx.GetPixelColor(j, i)); + } + } + } +} + CPPUNIT_TEST_SUITE_REGISTRATION(PngFilterTest); CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/vcl/source/filter/png/PngImageWriter.cxx b/vcl/source/filter/png/PngImageWriter.cxx index 48399ba05018..6a123e4eb547 100644 --- a/vcl/source/filter/png/PngImageWriter.cxx +++ b/vcl/source/filter/png/PngImageWriter.cxx @@ -110,8 +110,9 @@ static bool pngWrite(SvStream& rStream, BitmapEx& rBitmapEx, int nCompressionLev case ScanlineFormat::N8BitPal: { if (!aBitmap.HasGreyPalette8Bit()) - return false; - colorType = PNG_COLOR_TYPE_GRAY; + colorType = PNG_COLOR_TYPE_PALETTE; + else + colorType = PNG_COLOR_TYPE_GRAY; bitDepth = 8; break; }