When auto cropping if pixel alpha is 0 then ignore rest of color channels

This commit is contained in:
Jaex 2018-06-14 13:11:19 +03:00
parent f6852a1592
commit 7e7f456ee3

View file

@ -1721,13 +1721,15 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
bool leave = false;
ColorBgra checkColor = unsafeBitmap.GetPixel(0, 0);
uint mask = checkColor.Alpha == 0 ? 0xFF000000 : 0xFFFFFFFF;
uint check = checkColor.Bgra & mask;
// Find X (Left to right)
for (int x = 0; x < bmp.Width && !leave; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
if (unsafeBitmap.GetPixel(x, y) != checkColor)
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
{
crop.X = x;
leave = true;
@ -1747,6 +1749,8 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
if (!sameColorCrop)
{
checkColor = unsafeBitmap.GetPixel(0, 0);
mask = checkColor.Alpha == 0 ? 0xFF000000 : 0xFFFFFFFF;
check = checkColor.Bgra & mask;
}
// Find Y (Top to bottom)
@ -1754,7 +1758,7 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
{
for (int x = 0; x < bmp.Width; x++)
{
if (unsafeBitmap.GetPixel(x, y) != checkColor)
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
{
crop.Y = y;
leave = true;
@ -1768,6 +1772,8 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
if (!sameColorCrop)
{
checkColor = unsafeBitmap.GetPixel(bmp.Width - 1, 0);
mask = checkColor.Alpha == 0 ? 0xFF000000 : 0xFFFFFFFF;
check = checkColor.Bgra & mask;
}
// Find Width (Right to left)
@ -1775,7 +1781,7 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
{
for (int y = 0; y < bmp.Height; y++)
{
if (unsafeBitmap.GetPixel(x, y) != checkColor)
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
{
crop.Width = x - crop.X + 1;
leave = true;
@ -1789,6 +1795,8 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
if (!sameColorCrop)
{
checkColor = unsafeBitmap.GetPixel(0, bmp.Height - 1);
mask = checkColor.Alpha == 0 ? 0xFF000000 : 0xFFFFFFFF;
check = checkColor.Bgra & mask;
}
// Find Height (Bottom to top)
@ -1796,7 +1804,7 @@ public static Rectangle FindAutoCropRectangle(Bitmap bmp, bool sameColorCrop = f
{
for (int x = 0; x < bmp.Width; x++)
{
if (unsafeBitmap.GetPixel(x, y) != checkColor)
if ((unsafeBitmap.GetPixel(x, y).Bgra & mask) != check)
{
crop.Height = y - crop.Y + 1;
leave = true;