vcl/source/filter/png/PngImageReader.cxx |   37 +++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

New commits:
commit 12eac05c8088cefadace2629efce5473212662eb
Author:     Luboš Luňák <l.lu...@collabora.com>
AuthorDate: Fri Mar 5 21:45:11 2021 +0100
Commit:     Luboš Luňák <l.lu...@collabora.com>
CommitDate: Fri Mar 12 15:36:05 2021 +0100

    make PngImageReader read grayscale images as 8bpp, not 24bpp
    
    Grayscale is still a direct-color format, so it can save memory
    while not being slow (well, at least with Skia I made sure it's
    fast). PNGReader also reads grayscale images this way.
    
    Change-Id: I896f9901aca4defc8263fdcea6d2bebd574d1e8a
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112040
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>
    Reviewed-by: Luboš Luňák <l.lu...@collabora.com>

diff --git a/vcl/source/filter/png/PngImageReader.cxx 
b/vcl/source/filter/png/PngImageReader.cxx
index a510c6704686..3351e314b3fe 100644
--- a/vcl/source/filter/png/PngImageReader.cxx
+++ b/vcl/source/filter/png/PngImageReader.cxx
@@ -134,7 +134,9 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool 
bUseBitmap32)
     if (bitDepth < 8)
         png_set_packing(pPng);
 
-    if (colorType == PNG_COLOR_TYPE_GRAY || colorType == 
PNG_COLOR_TYPE_GRAY_ALPHA)
+    // Convert gray+alpha to RGBA, keep gray as gray.
+    if (colorType == PNG_COLOR_TYPE_GRAY_ALPHA
+        || (colorType == PNG_COLOR_TYPE_GRAY && png_get_valid(pPng, pInfo, 
PNG_INFO_tRNS)))
     {
         png_set_gray_to_rgb(pPng);
     }
@@ -154,7 +156,9 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool 
bUseBitmap32)
         return false;
     }
 
-    if (bitDepth != 8 || (colorType != PNG_COLOR_TYPE_RGB && colorType != 
PNG_COLOR_TYPE_RGB_ALPHA))
+    if (bitDepth != 8
+        || (colorType != PNG_COLOR_TYPE_RGB && colorType != 
PNG_COLOR_TYPE_RGB_ALPHA
+            && colorType != PNG_COLOR_TYPE_GRAY))
     {
         png_destroy_read_struct(&pPng, &pInfo, nullptr);
         return false;
@@ -299,6 +303,35 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool 
bUseBitmap32)
                 rBitmapEx = BitmapEx(aBitmap, aBitmapAlpha);
             }
         }
+        else if (colorType == PNG_COLOR_TYPE_GRAY)
+        {
+            size_t aRowSizeBytes = png_get_rowbytes(pPng, pInfo);
+
+            aBitmap = Bitmap(Size(width, height), 8, 
&Bitmap::GetGreyPalette(256));
+            aBitmap.Erase(COL_WHITE);
+            {
+                pWriteAccess = BitmapScopedWriteAccess(aBitmap);
+
+                aRows = std::vector<std::vector<png_byte>>(height);
+                for (auto& rRow : aRows)
+                    rRow.resize(aRowSizeBytes, 0);
+
+                for (int pass = 0; pass < nNumberOfPasses; pass++)
+                {
+                    for (png_uint_32 y = 0; y < height; y++)
+                    {
+                        Scanline pScanline = pWriteAccess->GetScanline(y);
+                        png_bytep pRow = aRows[y].data();
+                        png_read_row(pPng, pRow, nullptr);
+                        size_t iColor = 0;
+                        for (size_t i = 0; i < aRowSizeBytes; ++i)
+                            pScanline[iColor++] = pRow[i];
+                    }
+                }
+                pWriteAccess.reset();
+            }
+            rBitmapEx = BitmapEx(aBitmap);
+        }
     }
 
     png_read_end(pPng, pInfo);
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to