[Libreoffice-commits] core.git: vcl/source

Luboš Luňák (via logerrit) logerrit at kemper.freedesktop.org
Thu Apr 15 19:14:47 UTC 2021


 vcl/source/filter/png/PngImageReader.cxx |  255 ++++++++++++++-----------------
 1 file changed, 121 insertions(+), 134 deletions(-)

New commits:
commit 4168956575e71fec5f5137da600aacffd8d7b255
Author:     Luboš Luňák <l.lunak at collabora.com>
AuthorDate: Thu Apr 15 16:05:55 2021 +0200
Commit:     Luboš Luňák <l.lunak at collabora.com>
CommitDate: Thu Apr 15 21:14:01 2021 +0200

    remove unnecessary nesting levels
    
    Change-Id: I32ec17ebb267351c486477eef1309cad89603d36
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114163
    Tested-by: Jenkins
    Reviewed-by: Luboš Luňák <l.lunak at collabora.com>

diff --git a/vcl/source/filter/png/PngImageReader.cxx b/vcl/source/filter/png/PngImageReader.cxx
index 4136d9d9aa97..7c32188c22ba 100644
--- a/vcl/source/filter/png/PngImageReader.cxx
+++ b/vcl/source/filter/png/PngImageReader.cxx
@@ -187,169 +187,156 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool bUseBitmap32)
                         static_cast<sal_Int32>((100000.0 * height) / res_y));
     }
 
+    if (colorType == PNG_COLOR_TYPE_RGB)
     {
-        if (colorType == PNG_COLOR_TYPE_RGB)
+        aBitmap = Bitmap(Size(width, height), vcl::PixelFormat::N24_BPP);
+        pWriteAccess = BitmapScopedWriteAccess(aBitmap);
+        if (!pWriteAccess)
         {
-            aBitmap = Bitmap(Size(width, height), vcl::PixelFormat::N24_BPP);
-            {
-                pWriteAccess = BitmapScopedWriteAccess(aBitmap);
-                if (!pWriteAccess)
-                {
-                    png_destroy_read_struct(&pPng, &pInfo, nullptr);
-                    return false;
-                }
-                ScanlineFormat eFormat = pWriteAccess->GetScanlineFormat();
-                if (eFormat == ScanlineFormat::N24BitTcBgr)
-                    png_set_bgr(pPng);
+            png_destroy_read_struct(&pPng, &pInfo, nullptr);
+            return false;
+        }
+        ScanlineFormat eFormat = pWriteAccess->GetScanlineFormat();
+        if (eFormat == ScanlineFormat::N24BitTcBgr)
+            png_set_bgr(pPng);
 
-                for (int pass = 0; pass < nNumberOfPasses; pass++)
-                {
-                    for (png_uint_32 y = 0; y < height; y++)
-                    {
-                        Scanline pScanline = pWriteAccess->GetScanline(y);
-                        png_read_row(pPng, pScanline, nullptr);
-                    }
-                }
-                pWriteAccess.reset();
+        for (int pass = 0; pass < nNumberOfPasses; pass++)
+        {
+            for (png_uint_32 y = 0; y < height; y++)
+            {
+                Scanline pScanline = pWriteAccess->GetScanline(y);
+                png_read_row(pPng, pScanline, nullptr);
             }
-            rBitmapEx = BitmapEx(aBitmap);
         }
-        else if (colorType == PNG_COLOR_TYPE_RGB_ALPHA)
+        pWriteAccess.reset();
+        rBitmapEx = BitmapEx(aBitmap);
+    }
+    else if (colorType == PNG_COLOR_TYPE_RGB_ALPHA)
+    {
+        size_t aRowSizeBytes = png_get_rowbytes(pPng, pInfo);
+
+        if (bUseBitmap32)
         {
-            size_t aRowSizeBytes = png_get_rowbytes(pPng, pInfo);
+            aBitmap = Bitmap(Size(width, height), vcl::PixelFormat::N32_BPP);
+            pWriteAccess = BitmapScopedWriteAccess(aBitmap);
+            if (!pWriteAccess)
+            {
+                png_destroy_read_struct(&pPng, &pInfo, nullptr);
+                return false;
+            }
+            ScanlineFormat eFormat = pWriteAccess->GetScanlineFormat();
+            if (eFormat == ScanlineFormat::N32BitTcAbgr || eFormat == ScanlineFormat::N32BitTcBgra)
+            {
+                png_set_bgr(pPng);
+            }
 
-            if (bUseBitmap32)
+            for (int pass = 0; pass < nNumberOfPasses; pass++)
             {
-                aBitmap = Bitmap(Size(width, height), vcl::PixelFormat::N32_BPP);
+                for (png_uint_32 y = 0; y < height; y++)
                 {
-                    pWriteAccess = BitmapScopedWriteAccess(aBitmap);
-                    if (!pWriteAccess)
-                    {
-                        png_destroy_read_struct(&pPng, &pInfo, nullptr);
-                        return false;
-                    }
-                    ScanlineFormat eFormat = pWriteAccess->GetScanlineFormat();
-                    if (eFormat == ScanlineFormat::N32BitTcAbgr
-                        || eFormat == ScanlineFormat::N32BitTcBgra)
-                    {
-                        png_set_bgr(pPng);
-                    }
-
-                    for (int pass = 0; pass < nNumberOfPasses; pass++)
+                    Scanline pScanline = pWriteAccess->GetScanline(y);
+                    png_read_row(pPng, pScanline, nullptr);
+                }
+            }
+            const vcl::bitmap::lookup_table& premultiply = vcl::bitmap::get_premultiply_table();
+            if (eFormat == ScanlineFormat::N32BitTcAbgr || eFormat == ScanlineFormat::N32BitTcArgb)
+            { // alpha first and premultiply
+                for (png_uint_32 y = 0; y < height; y++)
+                {
+                    Scanline pScanline = pWriteAccess->GetScanline(y);
+                    for (size_t i = 0; i < aRowSizeBytes; i += 4)
                     {
-                        for (png_uint_32 y = 0; y < height; y++)
-                        {
-                            Scanline pScanline = pWriteAccess->GetScanline(y);
-                            png_read_row(pPng, pScanline, nullptr);
-                        }
-                    }
-                    const vcl::bitmap::lookup_table& premultiply
-                        = vcl::bitmap::get_premultiply_table();
-                    if (eFormat == ScanlineFormat::N32BitTcAbgr
-                        || eFormat == ScanlineFormat::N32BitTcArgb)
-                    { // alpha first and premultiply
-                        for (png_uint_32 y = 0; y < height; y++)
-                        {
-                            Scanline pScanline = pWriteAccess->GetScanline(y);
-                            for (size_t i = 0; i < aRowSizeBytes; i += 4)
-                            {
-                                const sal_uInt8 alpha = pScanline[i + 3];
-                                pScanline[i + 3] = premultiply[alpha][pScanline[i + 2]];
-                                pScanline[i + 2] = premultiply[alpha][pScanline[i + 1]];
-                                pScanline[i + 1] = premultiply[alpha][pScanline[i]];
-                                pScanline[i] = alpha;
-                            }
-                        }
+                        const sal_uInt8 alpha = pScanline[i + 3];
+                        pScanline[i + 3] = premultiply[alpha][pScanline[i + 2]];
+                        pScanline[i + 2] = premultiply[alpha][pScanline[i + 1]];
+                        pScanline[i + 1] = premultiply[alpha][pScanline[i]];
+                        pScanline[i] = alpha;
                     }
-                    else
-                    { // keep alpha last, only premultiply
-                        for (png_uint_32 y = 0; y < height; y++)
-                        {
-                            Scanline pScanline = pWriteAccess->GetScanline(y);
-                            for (size_t i = 0; i < aRowSizeBytes; i += 4)
-                            {
-                                const sal_uInt8 alpha = pScanline[i + 3];
-                                pScanline[i] = premultiply[alpha][pScanline[i]];
-                                pScanline[i + 1] = premultiply[alpha][pScanline[i + 1]];
-                                pScanline[i + 2] = premultiply[alpha][pScanline[i + 2]];
-                            }
-                        }
-                    }
-                    pWriteAccess.reset();
                 }
-                rBitmapEx = BitmapEx(aBitmap);
             }
             else
-            {
-                aBitmap = Bitmap(Size(width, height), vcl::PixelFormat::N24_BPP);
-                aBitmapAlpha = AlphaMask(Size(width, height), nullptr);
+            { // keep alpha last, only premultiply
+                for (png_uint_32 y = 0; y < height; y++)
                 {
-                    pWriteAccess = BitmapScopedWriteAccess(aBitmap);
-                    if (!pWriteAccess)
-                    {
-                        png_destroy_read_struct(&pPng, &pInfo, nullptr);
-                        return false;
-                    }
-                    ScanlineFormat eFormat = pWriteAccess->GetScanlineFormat();
-                    if (eFormat == ScanlineFormat::N24BitTcBgr)
-                        png_set_bgr(pPng);
-
-                    pWriteAccessAlpha = AlphaScopedWriteAccess(aBitmapAlpha);
-
-                    aRows = std::vector<std::vector<png_byte>>(height);
-                    for (auto& rRow : aRows)
-                        rRow.resize(aRowSizeBytes, 0);
-
-                    for (int pass = 0; pass < nNumberOfPasses; pass++)
+                    Scanline pScanline = pWriteAccess->GetScanline(y);
+                    for (size_t i = 0; i < aRowSizeBytes; i += 4)
                     {
-                        for (png_uint_32 y = 0; y < height; y++)
-                        {
-                            Scanline pScanline = pWriteAccess->GetScanline(y);
-                            Scanline pScanAlpha = pWriteAccessAlpha->GetScanline(y);
-                            png_bytep pRow = aRows[y].data();
-                            png_read_row(pPng, pRow, nullptr);
-                            size_t iAlpha = 0;
-                            size_t iColor = 0;
-                            for (size_t i = 0; i < aRowSizeBytes; i += 4)
-                            {
-                                pScanline[iColor++] = pRow[i + 0];
-                                pScanline[iColor++] = pRow[i + 1];
-                                pScanline[iColor++] = pRow[i + 2];
-                                pScanAlpha[iAlpha++] = 0xFF - pRow[i + 3];
-                            }
-                        }
+                        const sal_uInt8 alpha = pScanline[i + 3];
+                        pScanline[i] = premultiply[alpha][pScanline[i]];
+                        pScanline[i + 1] = premultiply[alpha][pScanline[i + 1]];
+                        pScanline[i + 2] = premultiply[alpha][pScanline[i + 2]];
                     }
-                    pWriteAccess.reset();
-                    pWriteAccessAlpha.reset();
                 }
-                rBitmapEx = BitmapEx(aBitmap, aBitmapAlpha);
             }
+            pWriteAccess.reset();
+            rBitmapEx = BitmapEx(aBitmap);
         }
-        else if (colorType == PNG_COLOR_TYPE_GRAY)
+        else
         {
-            aBitmap = Bitmap(Size(width, height), vcl::PixelFormat::N8_BPP,
-                             &Bitmap::GetGreyPalette(256));
-            aBitmap.Erase(COL_WHITE);
+            aBitmap = Bitmap(Size(width, height), vcl::PixelFormat::N24_BPP);
+            aBitmapAlpha = AlphaMask(Size(width, height), nullptr);
+            pWriteAccess = BitmapScopedWriteAccess(aBitmap);
+            if (!pWriteAccess)
             {
-                pWriteAccess = BitmapScopedWriteAccess(aBitmap);
-                if (!pWriteAccess)
-                {
-                    png_destroy_read_struct(&pPng, &pInfo, nullptr);
-                    return false;
-                }
+                png_destroy_read_struct(&pPng, &pInfo, nullptr);
+                return false;
+            }
+            ScanlineFormat eFormat = pWriteAccess->GetScanlineFormat();
+            if (eFormat == ScanlineFormat::N24BitTcBgr)
+                png_set_bgr(pPng);
+
+            pWriteAccessAlpha = AlphaScopedWriteAccess(aBitmapAlpha);
 
-                for (int pass = 0; pass < nNumberOfPasses; pass++)
+            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++)
                 {
-                    for (png_uint_32 y = 0; y < height; y++)
+                    Scanline pScanline = pWriteAccess->GetScanline(y);
+                    Scanline pScanAlpha = pWriteAccessAlpha->GetScanline(y);
+                    png_bytep pRow = aRows[y].data();
+                    png_read_row(pPng, pRow, nullptr);
+                    size_t iAlpha = 0;
+                    size_t iColor = 0;
+                    for (size_t i = 0; i < aRowSizeBytes; i += 4)
                     {
-                        Scanline pScanline = pWriteAccess->GetScanline(y);
-                        png_read_row(pPng, pScanline, nullptr);
+                        pScanline[iColor++] = pRow[i + 0];
+                        pScanline[iColor++] = pRow[i + 1];
+                        pScanline[iColor++] = pRow[i + 2];
+                        pScanAlpha[iAlpha++] = 0xFF - pRow[i + 3];
                     }
                 }
-                pWriteAccess.reset();
             }
-            rBitmapEx = BitmapEx(aBitmap);
+            pWriteAccess.reset();
+            pWriteAccessAlpha.reset();
+            rBitmapEx = BitmapEx(aBitmap, aBitmapAlpha);
+        }
+    }
+    else if (colorType == PNG_COLOR_TYPE_GRAY)
+    {
+        aBitmap
+            = Bitmap(Size(width, height), vcl::PixelFormat::N8_BPP, &Bitmap::GetGreyPalette(256));
+        aBitmap.Erase(COL_WHITE);
+        pWriteAccess = BitmapScopedWriteAccess(aBitmap);
+        if (!pWriteAccess)
+        {
+            png_destroy_read_struct(&pPng, &pInfo, nullptr);
+            return false;
         }
+
+        for (int pass = 0; pass < nNumberOfPasses; pass++)
+        {
+            for (png_uint_32 y = 0; y < height; y++)
+            {
+                Scanline pScanline = pWriteAccess->GetScanline(y);
+                png_read_row(pPng, pScanline, nullptr);
+            }
+        }
+        pWriteAccess.reset();
+        rBitmapEx = BitmapEx(aBitmap);
     }
 
     png_read_end(pPng, pInfo);


More information about the Libreoffice-commits mailing list