vcl/source/bitmap/BitmapEmbossGreyFilter.cxx |   62 ++++++++++++---------------
 1 file changed, 28 insertions(+), 34 deletions(-)

New commits:
commit 6dd514f633211b3cd6a6096b687c4e51a331ee4b
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Tue Oct 3 10:45:39 2023 +0300
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Tue Oct 3 10:59:15 2023 +0200

    Try to use doubles in a saner way
    
    This avoids convertion to integers too early, so makes the calculations
    more consistent (no more "use a truncated double later in a calculation
    involving doubles").
    Also it moves variables into proper scopes.
    
    Change-Id: Idc11c30c2c6892db092a04a127eb31266b4b70e7
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/157510
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/vcl/source/bitmap/BitmapEmbossGreyFilter.cxx 
b/vcl/source/bitmap/BitmapEmbossGreyFilter.cxx
index c1e96c11709a..06406152d6d6 100644
--- a/vcl/source/bitmap/BitmapEmbossGreyFilter.cxx
+++ b/vcl/source/bitmap/BitmapEmbossGreyFilter.cxx
@@ -39,25 +39,21 @@ BitmapEx BitmapEmbossGreyFilter::execute(BitmapEx const& 
rBitmapEx) const
     BitmapColor aGrey(sal_uInt8(0));
     const sal_Int32 nWidth = pWriteAcc->Width();
     const sal_Int32 nHeight = pWriteAcc->Height();
-    sal_Int32 nGrey11, nGrey12, nGrey13;
-    sal_Int32 nGrey21, nGrey22, nGrey23;
-    sal_Int32 nGrey31, nGrey32, nGrey33;
-    double fAzim = basegfx::deg2rad<100>(mnAzimuthAngle100);
-    double fElev = basegfx::deg2rad<100>(mnElevationAngle100);
-    std::unique_ptr<sal_Int32[]> pHMap(new sal_Int32[nWidth + 2]);
-    std::unique_ptr<sal_Int32[]> pVMap(new sal_Int32[nHeight + 2]);
-    sal_Int32 nX, nY, nNx, nNy, nDotL;
-    const sal_Int32 nLx = FRound(cos(fAzim) * cos(fElev) * 255.0);
-    const sal_Int32 nLy = FRound(sin(fAzim) * cos(fElev) * 255.0);
-    const sal_Int32 nLz = FRound(sin(fElev) * 255.0);
-    const auto nZ2 = (6 * 255) / 4;
-    const sal_Int32 nNzLz = ((6 * 255) / 4) * nLz;
-    const sal_uInt8 cLz = static_cast<sal_uInt8>(std::clamp(nLz, sal_Int32(0), 
sal_Int32(255)));
+    const double fAzim = basegfx::deg2rad<100>(mnAzimuthAngle100);
+    const double fElev = basegfx::deg2rad<100>(mnElevationAngle100);
+    std::vector<sal_Int32> pHMap(nWidth + 2);
+    std::vector<sal_Int32> pVMap(nHeight + 2);
+    const double nLx = cos(fAzim) * cos(fElev) * 255.0;
+    const double nLy = sin(fAzim) * cos(fElev) * 255.0;
+    const double nLz = sin(fElev) * 255.0;
+    const double nNz = 6 * 255.0 / 4;
+    const double nNzLz = nNz * nLz;
+    const sal_uInt8 cLz = FRound(std::clamp(nLz, 0.0, 255.0));
 
     // fill mapping tables
     pHMap[0] = 0;
 
-    for (nX = 1; nX <= nWidth; nX++)
+    for (sal_Int32 nX = 1; nX <= nWidth; nX++)
     {
         pHMap[nX] = nX - 1;
     }
@@ -66,43 +62,43 @@ BitmapEx BitmapEmbossGreyFilter::execute(BitmapEx const& 
rBitmapEx) const
 
     pVMap[0] = 0;
 
-    for (nY = 1; nY <= nHeight; nY++)
+    for (sal_Int32 nY = 1; nY <= nHeight; nY++)
     {
         pVMap[nY] = nY - 1;
     }
 
     pVMap[nHeight + 1] = nHeight - 1;
 
-    for (nY = 0; nY < nHeight; nY++)
+    for (sal_Int32 nY = 0; nY < nHeight; nY++)
     {
-        nGrey11 = pReadAcc->GetPixel(pVMap[nY], pHMap[0]).GetIndex();
-        nGrey12 = pReadAcc->GetPixel(pVMap[nY], pHMap[1]).GetIndex();
-        nGrey13 = pReadAcc->GetPixel(pVMap[nY], pHMap[2]).GetIndex();
-        nGrey21 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[0]).GetIndex();
-        nGrey22 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[1]).GetIndex();
-        nGrey23 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[2]).GetIndex();
-        nGrey31 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[0]).GetIndex();
-        nGrey32 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[1]).GetIndex();
-        nGrey33 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[2]).GetIndex();
+        sal_Int32 nGrey11 = pReadAcc->GetPixel(pVMap[nY], pHMap[0]).GetIndex();
+        sal_Int32 nGrey12 = pReadAcc->GetPixel(pVMap[nY], pHMap[1]).GetIndex();
+        sal_Int32 nGrey13 = pReadAcc->GetPixel(pVMap[nY], pHMap[2]).GetIndex();
+        sal_Int32 nGrey21 = pReadAcc->GetPixel(pVMap[nY + 1], 
pHMap[0]).GetIndex();
+        sal_Int32 nGrey22 = pReadAcc->GetPixel(pVMap[nY + 1], 
pHMap[1]).GetIndex();
+        sal_Int32 nGrey23 = pReadAcc->GetPixel(pVMap[nY + 1], 
pHMap[2]).GetIndex();
+        sal_Int32 nGrey31 = pReadAcc->GetPixel(pVMap[nY + 2], 
pHMap[0]).GetIndex();
+        sal_Int32 nGrey32 = pReadAcc->GetPixel(pVMap[nY + 2], 
pHMap[1]).GetIndex();
+        sal_Int32 nGrey33 = pReadAcc->GetPixel(pVMap[nY + 2], 
pHMap[2]).GetIndex();
 
         Scanline pScanline = pWriteAcc->GetScanline(nY);
-        for (nX = 0; nX < nWidth; nX++)
+        for (sal_Int32 nX = 0; nX < nWidth; nX++)
         {
-            nNx = nGrey11 + nGrey21 + nGrey31 - nGrey13 - nGrey23 - nGrey33;
-            nNy = nGrey31 + nGrey32 + nGrey33 - nGrey11 - nGrey12 - nGrey13;
+            const sal_Int32 nNx = nGrey11 + nGrey21 + nGrey31 - nGrey13 - 
nGrey23 - nGrey33;
+            const sal_Int32 nNy = nGrey31 + nGrey32 + nGrey33 - nGrey11 - 
nGrey12 - nGrey13;
 
             if (!nNx && !nNy)
             {
                 aGrey.SetIndex(cLz);
             }
-            else if ((nDotL = nNx * nLx + nNy * nLy + nNzLz) < 0)
+            else if (double nDotL = nNx * nLx + nNy * nLy + nNzLz; nDotL < 0)
             {
                 aGrey.SetIndex(0);
             }
             else
             {
-                const double fGrey = nDotL / std::hypot(nNx, nNy, nZ2);
-                aGrey.SetIndex(static_cast<sal_uInt8>(std::clamp(fGrey, 0.0, 
255.0)));
+                const double fGrey = nDotL / std::hypot(nNx, nNy, nNz);
+                aGrey.SetIndex(FRound(std::clamp(fGrey, 0.0, 255.0)));
             }
 
             pWriteAcc->SetPixelOnData(pScanline, nX, aGrey);
@@ -124,8 +120,6 @@ BitmapEx BitmapEmbossGreyFilter::execute(BitmapEx const& 
rBitmapEx) const
         }
     }
 
-    pHMap.reset();
-    pVMap.reset();
     pWriteAcc.reset();
     pReadAcc.reset();
 

Reply via email to