Skip to content

Commit

Permalink
fix custom icon trimming if byte order is reversed
Browse files Browse the repository at this point in the history
fix #1161
  • Loading branch information
jamie-mh committed Jul 26, 2024
1 parent fae4800 commit 5cf5221
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions AuthenticatorPro.Droid/src/Interface/CustomIconDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ private static Bitmap Trim(Bitmap bitmap)
var width = bitmap.Width;
var height = bitmap.Height;

using var alphaMap = bitmap.ExtractAlpha();
var pixels = new int[width * height];
bitmap.GetPixels(pixels, 0, width, 0, 0, width, height);
alphaMap.GetPixels(pixels, 0, width, 0, 0, width, height);

bool IsTransparent(int x, int y)
{
return (pixels[y * width + x] & 0xF000) == 0;
return pixels[y * width + x] == 0;
}

var left = width;
Expand All @@ -100,12 +101,16 @@ bool IsTransparent(int x, int y)
{
for (var x = 0; x < width; ++x)
{
if (IsTransparent(x, y) || x > left)
if (IsTransparent(x, y))
{
continue;
}

left = x;
if (x < left)
{
left = x;
break;
}
}
}

Expand All @@ -115,12 +120,16 @@ bool IsTransparent(int x, int y)
{
for (var y = 0; y < height; ++y)
{
if (IsTransparent(x, y) || y > top)
if (IsTransparent(x, y))
{
continue;
}

top = y;
if (y < top)
{
top = y;
break;
}
}
}

Expand All @@ -130,12 +139,16 @@ bool IsTransparent(int x, int y)
{
for (var x = width - 1; x >= 0; --x)
{
if (IsTransparent(x, y) || x < right)
if (IsTransparent(x, y))
{
continue;
}

right = x;
if (x > right)
{
right = x;
break;
}
}
}

Expand All @@ -145,15 +158,19 @@ bool IsTransparent(int x, int y)
{
for (var y = height - 1; y >= 0; --y)
{
if (IsTransparent(x, y) || y < bottom)
if (IsTransparent(x, y))
{
continue;
}

bottom = y;
if (y > bottom)
{
bottom = y;
break;
}
}
}

return Bitmap.CreateBitmap(bitmap, left, top, right - left, bottom - top);
}
}
Expand Down

0 comments on commit 5cf5221

Please sign in to comment.