Exemplo n.º 1
0
        public static bool Compare(UnsafeBitmap bmp1, UnsafeBitmap bmp2)
        {
            int pixelCount = bmp1.PixelCount;

            if (pixelCount != bmp2.PixelCount)
            {
                return(false);
            }

            bmp1.Lock(ImageLockMode.ReadOnly);
            bmp2.Lock(ImageLockMode.ReadOnly);

            ColorBgra *pointer1 = bmp1.Pointer;
            ColorBgra *pointer2 = bmp2.Pointer;

            for (int i = 0; i < pixelCount; i++)
            {
                if (pointer1->Bgra != pointer2->Bgra)
                {
                    return(false);
                }

                pointer1++;
                pointer2++;
            }

            return(true);
        }
Exemplo n.º 2
0
 public static bool IsImagesEqual(Bitmap bmp1, Bitmap bmp2)
 {
     using (UnsafeBitmap unsafeBitmap1 = new UnsafeBitmap(bmp1))
         using (UnsafeBitmap unsafeBitmap2 = new UnsafeBitmap(bmp2))
         {
             return(unsafeBitmap1 == unsafeBitmap2);
         }
 }
Exemplo n.º 3
0
        public static Image Apply(this ConvolutionMatrix kernel, Image img)
        {
            Bitmap result = (Bitmap)img.Clone();

            using (UnsafeBitmap source = new UnsafeBitmap((Bitmap)img, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap dest = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
                {
                    int originX = (kernel.Width - 1) / 2;
                    int originY = (kernel.Height - 1) / 2;

                    for (int y = 0; y < source.Height; y++)
                    {
                        for (int x = 0; x < source.Width; x++)
                        {
                            double r = 0.0;
                            double g = 0.0;
                            double b = 0.0;

                            // Apply each matrix multiplier to the color components for each pixel.
                            for (int fy = 0; fy < kernel.Height; fy++)
                            {
                                int fyr     = fy - originY;
                                int offsetY = y + fyr;

                                offsetY.Clamp(0, source.Height - 1);

                                for (int fx = 0; fx < kernel.Width; fx++)
                                {
                                    int fxr     = fx - originX;
                                    int offsetX = x + fxr;

                                    offsetX.Clamp(0, source.Width - 1);

                                    ColorBgra currentColor = source.GetPixel(offsetX, offsetY);

                                    r += kernel[fy, fx] * currentColor.Red;
                                    g += kernel[fy, fx] * currentColor.Green;
                                    b += kernel[fy, fx] * currentColor.Blue;
                                }
                            }

                            r += kernel.Offset;
                            r.Clamp(0, 255);

                            g += kernel.Offset;
                            g.Clamp(0, 255);

                            b += kernel.Offset;
                            b.Clamp(0, 255);

                            dest.SetPixel(x, y, new ColorBgra((byte)b, (byte)g, (byte)r, source.GetPixel(x, y).Alpha));
                        }
                    }
                }

            return(result);
        }
Exemplo n.º 4
0
        public static Bitmap AddReflection(Image img, int percentage, int maxAlpha, int minAlpha)
        {
            percentage = percentage.Between(1, 100);
            maxAlpha   = maxAlpha.Between(0, 255);
            minAlpha   = minAlpha.Between(0, 255);

            Bitmap reflection;

            using (Bitmap bitmapRotate = (Bitmap)img.Clone())
            {
                bitmapRotate.RotateFlip(RotateFlipType.RotateNoneFlipY);
                reflection = bitmapRotate.Clone(new Rectangle(0, 0, bitmapRotate.Width, (int)(bitmapRotate.Height * ((float)percentage / 100))), PixelFormat.Format32bppArgb);
            }

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(reflection, true))
            {
                int   alphaAdd         = maxAlpha - minAlpha;
                float reflectionHeight = reflection.Height - 1;

                for (int y = 0; y < reflection.Height; ++y)
                {
                    for (int x = 0; x < reflection.Width; ++x)
                    {
                        ColorBgra color = unsafeBitmap.GetPixel(x, y);
                        byte      alpha = (byte)(maxAlpha - (alphaAdd * (y / reflectionHeight)));

                        if (color.Alpha > alpha)
                        {
                            color.Alpha = alpha;
                            unsafeBitmap.SetPixel(x, y, color);
                        }
                    }
                }
            }

            return(reflection);
        }
Exemplo n.º 5
0
        public static Bitmap AddReflection(Image img, int percentage, int maxAlpha, int minAlpha)
        {
            percentage = percentage.Between(1, 100);
            maxAlpha = maxAlpha.Between(0, 255);
            minAlpha = minAlpha.Between(0, 255);

            Bitmap reflection;

            using (Bitmap bitmapRotate = (Bitmap)img.Clone())
            {
                bitmapRotate.RotateFlip(RotateFlipType.RotateNoneFlipY);
                reflection = bitmapRotate.Clone(new Rectangle(0, 0, bitmapRotate.Width, (int)(bitmapRotate.Height * ((float)percentage / 100))), PixelFormat.Format32bppArgb);
            }

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(reflection, true))
            {
                int alphaAdd = maxAlpha - minAlpha;
                float reflectionHeight = reflection.Height - 1;

                for (int y = 0; y < reflection.Height; ++y)
                {
                    for (int x = 0; x < reflection.Width; ++x)
                    {
                        ColorBgra color = unsafeBitmap.GetPixel(x, y);
                        byte alpha = (byte)(maxAlpha - (alphaAdd * (y / reflectionHeight)));

                        if (color.Alpha > alpha)
                        {
                            color.Alpha = alpha;
                            unsafeBitmap.SetPixel(x, y, color);
                        }
                    }
                }
            }

            return reflection;
        }
Exemplo n.º 6
0
 public static void HighlightImage(Bitmap bmp, Rectangle rect, Color highlightColor)
 {
     using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true))
     {
         for (int y = rect.Y; y < rect.Height; y++)
         {
             for (int x = rect.X; x < rect.Width; x++)
             {
                 ColorBgra color = unsafeBitmap.GetPixel(x, y);
                 color.Red = Math.Min(color.Red, highlightColor.R);
                 color.Green = Math.Min(color.Green, highlightColor.G);
                 color.Blue = Math.Min(color.Blue, highlightColor.B);
                 unsafeBitmap.SetPixel(x, y, color);
             }
         }
     }
 }
Exemplo n.º 7
0
        public static Bitmap Apply(this ConvolutionMatrix kernel, Bitmap bmp)
        {
            Bitmap bmpResult = (Bitmap)bmp.Clone();

            using (UnsafeBitmap source = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap dest = new UnsafeBitmap(bmpResult, true, ImageLockMode.WriteOnly))
                {
                    int originX = (kernel.Width - 1) / 2;
                    int originY = (kernel.Height - 1) / 2;

                    Parallel.For(0, source.Height, y =>
                    {
                        Parallel.For(0, source.Width, x =>
                        {
                            double r = 0.0;
                            double g = 0.0;
                            double b = 0.0;
                            double a = 0.0;

                            // Apply each matrix multiplier to the color components for each pixel.
                            for (int fy = 0; fy < kernel.Height; fy++)
                            {
                                int fyr     = fy - originY;
                                int offsetY = y + fyr;

                                offsetY = offsetY.Clamp(0, source.Height - 1);

                                for (int fx = 0; fx < kernel.Width; fx++)
                                {
                                    int fxr     = fx - originX;
                                    int offsetX = x + fxr;

                                    offsetX = offsetX.Clamp(0, source.Width - 1);

                                    ColorBgra currentColor = source.GetPixel(offsetX, offsetY);

                                    r += kernel[fy, fx] * currentColor.Red;
                                    g += kernel[fy, fx] * currentColor.Green;
                                    b += kernel[fy, fx] * currentColor.Blue;
                                    if (kernel.ConsiderAlpha)
                                    {
                                        a += kernel[fy, fx] * currentColor.Alpha;
                                    }
                                }
                            }

                            r += kernel.Offset;
                            r  = r.Clamp(0, 255);

                            g += kernel.Offset;
                            g  = g.Clamp(0, 255);

                            b += kernel.Offset;
                            b  = b.Clamp(0, 255);

                            if (kernel.ConsiderAlpha)
                            {
                                a += kernel.Offset;
                                a  = a.Clamp(0, 255);
                            }

                            dest.SetPixel(x, y, new ColorBgra((byte)b, (byte)g, (byte)r, kernel.ConsiderAlpha ? (byte)a : source.GetPixel(x, y).Alpha));
                        });
                    });
                }

            return(bmpResult);
        }
Exemplo n.º 8
0
        private static void BoxBlurVertical(UnsafeBitmap unsafeBitmap, int range)
        {
            int w = unsafeBitmap.Width;
            int h = unsafeBitmap.Height;
            int halfRange = range / 2;
            ColorBgra[] newColors = new ColorBgra[w];

            for (int x = 0; x < w; x++)
            {
                int hits = 0;
                int r = 0;
                int g = 0;
                int b = 0;
                int a = 0;

                for (int y = -halfRange; y < h; y++)
                {
                    int oldPixel = y - halfRange - 1;
                    if (oldPixel >= 0)
                    {
                        ColorBgra color = unsafeBitmap.GetPixel(x, oldPixel);

                        if (color.Bgra != 0)
                        {
                            r -= color.Red;
                            g -= color.Green;
                            b -= color.Blue;
                            a -= color.Alpha;
                        }

                        hits--;
                    }

                    int newPixel = y + halfRange;
                    if (newPixel < h)
                    {
                        ColorBgra color = unsafeBitmap.GetPixel(x, newPixel);

                        if (color.Bgra != 0)
                        {
                            r += color.Red;
                            g += color.Green;
                            b += color.Blue;
                            a += color.Alpha;
                        }

                        hits++;
                    }

                    if (y >= 0)
                    {
                        newColors[y] = new ColorBgra((byte)(b / hits), (byte)(g / hits), (byte)(r / hits), (byte)(a / hits));
                    }
                }

                for (int y = 0; y < h; y++)
                {
                    unsafeBitmap.SetPixel(x, y, newColors[y]);
                }
            }
        }
Exemplo n.º 9
0
        private static Rectangle TrimTransparentFindWidth(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int x = rect.Width - 1; x >= rect.X; x--)
            {
                for (int y = rect.Y; y < rect.Height; y++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Width = x - rect.X + 1;
                        return rect;
                    }
                }
            }

            return rect;
        }
Exemplo n.º 10
0
        /// <summary>Automatically crop image to remove transparent outside area.</summary>
        public static Bitmap AutoCropImage(Bitmap bmp)
        {
            Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height);
            Rectangle rect = source;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly))
            {
                bool leave = false;

                // Find X
                for (int x = rect.X; x < rect.Width && !leave; x++)
                {
                    for (int y = rect.Y; y < rect.Height; y++)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                        {
                            rect.X = x;
                            leave = true;
                            break;
                        }
                    }
                }

                leave = false;

                // Find Y
                for (int y = rect.Y; y < rect.Height && !leave; y++)
                {
                    for (int x = rect.X; x < rect.Width; x++)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                        {
                            rect.Y = y;
                            leave = true;
                            break;
                        }
                    }
                }

                leave = false;

                // Find Width
                for (int x = rect.Width - 1; x >= rect.X && !leave; x--)
                {
                    for (int y = rect.Y; y < rect.Height; y++)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                        {
                            rect.Width = x - rect.X + 1;
                            leave = true;
                            break;
                        }
                    }
                }

                leave = false;

                // Find Height
                for (int y = rect.Height - 1; y >= rect.Y && !leave; y--)
                {
                    for (int x = rect.X; x < rect.Width; x++)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                        {
                            rect.Height = y - rect.Y + 1;
                            leave = true;
                            break;
                        }
                    }
                }
            }

            if (source != rect)
            {
                Bitmap croppedBitmap = CropBitmap(bmp, rect);

                if (croppedBitmap != null)
                {
                    bmp.Dispose();
                    return croppedBitmap;
                }
            }

            return bmp;
        }
Exemplo n.º 11
0
        private static Bitmap QuickTrimTransparent(Bitmap bitmap)
        {
            Rectangle source = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            Rectangle rect = source;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true, ImageLockMode.ReadOnly))
            {
                int middleX = rect.Width / 2;
                int middleY = rect.Height / 2;

                // Find X
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0)
                    {
                        rect.X = x;
                        break;
                    }
                }

                // Find Y
                for (int y = rect.Y; y < rect.Height; y++)
                {
                    if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0)
                    {
                        rect.Y = y;
                        break;
                    }
                }

                // Find Width
                for (int x = rect.Width - 1; x >= rect.X; x--)
                {
                    if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0)
                    {
                        rect.Width = x - rect.X + 1;
                        break;
                    }
                }

                // Find Height
                for (int y = rect.Height - 1; y >= rect.Y; y--)
                {
                    if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0)
                    {
                        rect.Height = y - rect.Y + 1;
                        break;
                    }
                }
            }

            if (source != rect)
            {
                return ImageHelpers.CropBitmap(bitmap, rect);
            }

            return bitmap;
        }
Exemplo n.º 12
0
        private int CalculateVerticalOffset(Image img1, Image img2, int ignoreRightOffset = 50)
        {
            int lastMatchCount = 0;
            int lastMatchOffset = 0;

            Rectangle rect = new Rectangle(Options.TrimLeftEdge, Options.TrimTopEdge,
                img1.Width - Options.TrimLeftEdge - Options.TrimRightEdge - (img1.Width > ignoreRightOffset ? ignoreRightOffset : 0),
                img1.Height - Options.TrimTopEdge - Options.TrimBottomEdge);

            using (UnsafeBitmap bmp1 = new UnsafeBitmap((Bitmap)img1, true, ImageLockMode.ReadOnly))
            using (UnsafeBitmap bmp2 = new UnsafeBitmap((Bitmap)img2, true, ImageLockMode.ReadOnly))
            {
                for (int y = rect.Y; y < rect.Bottom; y++)
                {
                    bool isLineMatches = true;

                    for (int x = rect.X; x < rect.Right; x++)
                    {
                        if (bmp2.GetPixel(x, y) != bmp1.GetPixel(x, rect.Bottom - 1))
                        {
                            isLineMatches = false;
                            break;
                        }
                    }

                    if (isLineMatches)
                    {
                        int lineMatchesCount = 1;
                        int y3 = 2;

                        for (int y2 = y - 1; y2 >= rect.Y; y2--)
                        {
                            bool isLineMatches2 = true;

                            for (int x2 = rect.X; x2 < rect.Right; x2++)
                            {
                                if (bmp2.GetPixel(x2, y2) != bmp1.GetPixel(x2, rect.Bottom - y3))
                                {
                                    isLineMatches2 = false;
                                    break;
                                }
                            }

                            if (isLineMatches2)
                            {
                                lineMatchesCount++;
                                y3++;
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (lineMatchesCount > lastMatchCount)
                        {
                            lastMatchCount = lineMatchesCount;
                            lastMatchOffset = y - rect.Y + 1;
                        }
                    }
                }
            }

            return lastMatchOffset;
        }
Exemplo n.º 13
0
        private Padding GuessEdges(Image img1, Image img2)
        {
            Padding result = new Padding();
            Rectangle rect = new Rectangle(0, 0, img1.Width, img1.Height);

            using (UnsafeBitmap bmp1 = new UnsafeBitmap((Bitmap)img1, true, ImageLockMode.ReadOnly))
            using (UnsafeBitmap bmp2 = new UnsafeBitmap((Bitmap)img2, true, ImageLockMode.ReadOnly))
            {
                bool valueFound = false;

                // Left edge
                for (int x = rect.X; !valueFound && x < rect.Width; x++)
                {
                    for (int y = rect.Y; y < rect.Height; y++)
                    {
                        if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
                        {
                            valueFound = true;
                            result.Left = x;
                            rect.X = x;
                            break;
                        }
                    }
                }

                valueFound = false;

                // Top edge
                for (int y = rect.Y; !valueFound && y < rect.Height; y++)
                {
                    for (int x = rect.X; x < rect.Width; x++)
                    {
                        if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
                        {
                            valueFound = true;
                            result.Top = y;
                            rect.Y = y;
                            break;
                        }
                    }
                }

                valueFound = false;

                // Right edge
                for (int x = rect.Width - 1; !valueFound && x >= rect.X; x--)
                {
                    for (int y = rect.Y; y < rect.Height; y++)
                    {
                        if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
                        {
                            valueFound = true;
                            result.Right = rect.Width - x - 1;
                            rect.Width = x + 1;
                            break;
                        }
                    }
                }

                valueFound = false;

                // Bottom edge
                for (int y = rect.Height - 1; !valueFound && y >= rect.X; y--)
                {
                    for (int x = rect.X; x < rect.Width; x++)
                    {
                        if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
                        {
                            valueFound = true;
                            result.Bottom = rect.Height - y - 1;
                            rect.Height = y + 1;
                            break;
                        }
                    }
                }
            }

            return result;
        }
Exemplo n.º 14
0
        public static bool Compare(UnsafeBitmap bmp1, UnsafeBitmap bmp2)
        {
            int pixelCount = bmp1.PixelCount;

            if (pixelCount != bmp2.PixelCount)
            {
                return false;
            }

            bmp1.Lock(ImageLockMode.ReadOnly);
            bmp2.Lock(ImageLockMode.ReadOnly);

            ColorBgra* pointer1 = bmp1.Pointer;
            ColorBgra* pointer2 = bmp2.Pointer;

            for (int i = 0; i < pixelCount; i++)
            {
                if (pointer1->Bgra != pointer2->Bgra)
                {
                    return false;
                }

                pointer1++;
                pointer2++;
            }

            return true;
        }
Exemplo n.º 15
0
        public static void Pixelate(Bitmap bmp, int pixelSize)
        {
            if (pixelSize > 1)
            {
                using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true))
                {
                    for (int y = 0; y < unsafeBitmap.Height; y += pixelSize)
                    {
                        for (int x = 0; x < unsafeBitmap.Width; x += pixelSize)
                        {
                            int xLimit = Math.Min(x + pixelSize, unsafeBitmap.Width);
                            int yLimit = Math.Min(y + pixelSize, unsafeBitmap.Height);
                            int pixelCount = (xLimit - x) * (yLimit - y);
                            int r = 0, g = 0, b = 0, a = 0;

                            for (int y2 = y; y2 < yLimit; y2++)
                            {
                                for (int x2 = x; x2 < xLimit; x2++)
                                {
                                    ColorBgra color = unsafeBitmap.GetPixel(x2, y2);

                                    r += color.Red;
                                    g += color.Green;
                                    b += color.Blue;
                                    a += color.Alpha;
                                }
                            }

                            ColorBgra averageColor = new ColorBgra((byte)(b / pixelCount), (byte)(g / pixelCount), (byte)(r / pixelCount), (byte)(a / pixelCount));

                            for (int y2 = y; y2 < yLimit; y2++)
                            {
                                for (int x2 = x; x2 < xLimit; x2++)
                                {
                                    unsafeBitmap.SetPixel(x2, y2, averageColor);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 16
0
        /// <summary>Automatically crop image to remove transparent outside area. Only checks center pixels.</summary>
        public static Bitmap QuickAutoCropImage(Bitmap bmp)
        {
            Rectangle source = new Rectangle(0, 0, bmp.Width, bmp.Height);
            Rectangle rect = source;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly))
            {
                int middleX = rect.Width / 2;
                int middleY = rect.Height / 2;

                // Find X
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0)
                    {
                        rect.X = x;
                        break;
                    }
                }

                // Find Y
                for (int y = rect.Y; y < rect.Height; y++)
                {
                    if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0)
                    {
                        rect.Y = y;
                        break;
                    }
                }

                // Find Width
                for (int x = rect.Width - 1; x >= rect.X; x--)
                {
                    if (unsafeBitmap.GetPixel(x, middleY).Alpha > 0)
                    {
                        rect.Width = x - rect.X + 1;
                        break;
                    }
                }

                // Find Height
                for (int y = rect.Height - 1; y >= rect.Y; y--)
                {
                    if (unsafeBitmap.GetPixel(middleX, y).Alpha > 0)
                    {
                        rect.Height = y - rect.Y + 1;
                        break;
                    }
                }
            }

            if (source != rect)
            {
                Bitmap croppedBitmap = CropBitmap(bmp, rect);

                if (croppedBitmap != null)
                {
                    bmp.Dispose();
                    return croppedBitmap;
                }
            }

            return bmp;
        }
Exemplo n.º 17
0
 public static bool IsImagesEqual(Bitmap bmp1, Bitmap bmp2)
 {
     using (UnsafeBitmap unsafeBitmap1 = new UnsafeBitmap(bmp1))
     using (UnsafeBitmap unsafeBitmap2 = new UnsafeBitmap(bmp2))
     {
         return unsafeBitmap1 == unsafeBitmap2;
     }
 }
Exemplo n.º 18
0
        private static Bitmap TrimTransparent(Bitmap bitmap)
        {
            Rectangle source = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            Rectangle rect = source;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true, ImageLockMode.ReadOnly))
            {
                rect = TrimTransparentFindX(unsafeBitmap, rect);
                rect = TrimTransparentFindY(unsafeBitmap, rect);
                rect = TrimTransparentFindWidth(unsafeBitmap, rect);
                rect = TrimTransparentFindHeight(unsafeBitmap, rect);
            }

            if (source != rect)
            {
                Bitmap croppedBitmap = ImageHelpers.CropBitmap(bitmap, rect);

                if (croppedBitmap != null)
                {
                    bitmap.Dispose();
                    return croppedBitmap;
                }
            }

            return bitmap;
        }
Exemplo n.º 19
0
        private static Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackground)
        {
            if (whiteBackground != null && blackBackground != null && whiteBackground.Size == blackBackground.Size)
            {
                Bitmap result = new Bitmap(whiteBackground.Width, whiteBackground.Height, PixelFormat.Format32bppArgb);

                using (UnsafeBitmap whiteBitmap = new UnsafeBitmap(whiteBackground, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap blackBitmap = new UnsafeBitmap(blackBackground, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap resultBitmap = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
                {
                    int pixelCount = blackBitmap.PixelCount;

                    for (int i = 0; i < pixelCount; i++)
                    {
                        ColorBgra white = whiteBitmap.GetPixel(i);
                        ColorBgra black = blackBitmap.GetPixel(i);

                        double alpha = (black.Red - white.Red + 255) / 255.0;

                        if (alpha == 1)
                        {
                            resultBitmap.SetPixel(i, white);
                        }
                        else if (alpha > 0)
                        {
                            white.Blue = (byte)(black.Blue / alpha);
                            white.Green = (byte)(black.Green / alpha);
                            white.Red = (byte)(black.Red / alpha);
                            white.Alpha = (byte)(255 * alpha);

                            resultBitmap.SetPixel(i, white);
                        }
                    }
                }

                return result;
            }

            return whiteBackground;
        }
Exemplo n.º 20
0
        // http://incubator.quasimondo.com/processing/superfast_blur.php
        public static void FastBoxBlur(Bitmap bmp, int radius)
        {
            if (radius < 1) return;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true))
            {
                int w = unsafeBitmap.Width;
                int h = unsafeBitmap.Height;
                int wm = w - 1;
                int hm = h - 1;
                int wh = w * h;
                int div = radius + radius + 1;
                byte[] r = new byte[wh];
                byte[] g = new byte[wh];
                byte[] b = new byte[wh];
                byte[] a = new byte[wh];
                int rsum, gsum, bsum, asum, x, y, i, p, p1, p2, yp, yi, yw;
                int[] vmin = new int[Math.Max(w, h)];
                int[] vmax = new int[Math.Max(w, h)];

                byte[] dv = new byte[256 * div];

                for (i = 0; i < 256 * div; i++)
                {
                    dv[i] = (byte)(i / div);
                }

                yw = yi = 0;

                for (y = 0; y < h; y++)
                {
                    rsum = gsum = bsum = asum = 0;

                    for (i = -radius; i <= radius; i++)
                    {
                        p = (yi + Math.Min(wm, Math.Max(i, 0)));

                        ColorBgra color = unsafeBitmap.GetPixel(p);
                        rsum += color.Red;
                        gsum += color.Green;
                        bsum += color.Blue;
                        asum += color.Alpha;
                    }

                    for (x = 0; x < w; x++)
                    {
                        r[yi] = dv[rsum];
                        g[yi] = dv[gsum];
                        b[yi] = dv[bsum];
                        a[yi] = dv[asum];

                        if (y == 0)
                        {
                            vmin[x] = Math.Min(x + radius + 1, wm);
                            vmax[x] = Math.Max(x - radius, 0);
                        }

                        p1 = (yw + vmin[x]);
                        p2 = (yw + vmax[x]);

                        ColorBgra color1 = unsafeBitmap.GetPixel(p1);
                        ColorBgra color2 = unsafeBitmap.GetPixel(p2);

                        rsum += color1.Red - color2.Red;
                        gsum += color1.Green - color2.Green;
                        bsum += color1.Blue - color2.Blue;
                        asum += color1.Alpha - color2.Alpha;

                        yi++;
                    }

                    yw += w;
                }

                for (x = 0; x < w; x++)
                {
                    rsum = gsum = bsum = asum = 0;
                    yp = -radius * w;

                    for (i = -radius; i <= radius; i++)
                    {
                        yi = Math.Max(0, yp) + x;
                        rsum += r[yi];
                        gsum += g[yi];
                        bsum += b[yi];
                        asum += a[yi];
                        yp += w;
                    }

                    yi = x;

                    for (y = 0; y < h; y++)
                    {
                        ColorBgra color = new ColorBgra(dv[bsum], dv[gsum], dv[rsum], dv[asum]);
                        unsafeBitmap.SetPixel(yi, color);

                        if (x == 0)
                        {
                            vmin[y] = Math.Min(y + radius + 1, hm) * w;
                            vmax[y] = Math.Max(y - radius, 0) * w;
                        }

                        p1 = x + vmin[y];
                        p2 = x + vmax[y];

                        rsum += r[p1] - r[p2];
                        gsum += g[p1] - g[p2];
                        bsum += b[p1] - b[p2];
                        asum += a[p1] - a[p2];

                        yi += w;
                    }
                }
            }
        }
Exemplo n.º 21
0
        private static void TrimShadow(Bitmap bitmap)
        {
            int sizeLimit = 10;
            int alphaLimit = 200;

            using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true))
            {
                for (int i = 0; i < sizeLimit; i++)
                {
                    int y = i;
                    int width = bitmap.Width;

                    // Left top
                    for (int x = 0; x < sizeLimit; x++)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Right top
                    for (int x = width - 1; x > width - sizeLimit - 1; x--)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }

                    y = bitmap.Height - i - 1;

                    // Left bottom
                    for (int x = 0; x < sizeLimit; x++)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Right bottom
                    for (int x = width - 1; x > width - sizeLimit - 1; x--)
                    {
                        if (unsafeBitmap.GetPixel(x, y).Alpha < alphaLimit)
                        {
                            unsafeBitmap.ClearPixel(x, y);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }
Exemplo n.º 22
0
        public static Image Apply(this ConvolutionMatrix matrix, Image img)
        {
            int factor = Math.Max(matrix.Factor, 1);

            Bitmap result = (Bitmap)img.Clone();

            using (UnsafeBitmap source = new UnsafeBitmap((Bitmap)img, true, ImageLockMode.ReadOnly))
            using (UnsafeBitmap dest = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
            {
                int height = source.Height - 2;
                int width = source.Width - 2;
                ColorBgra[,] pixelColor = new ColorBgra[3, 3];
                int pixel;
                ColorBgra color = new ColorBgra();

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        pixelColor[0, 0] = source.GetPixel(x, y);
                        pixelColor[0, 1] = source.GetPixel(x, y + 1);
                        pixelColor[0, 2] = source.GetPixel(x, y + 2);
                        pixelColor[1, 0] = source.GetPixel(x + 1, y);
                        pixelColor[1, 1] = source.GetPixel(x + 1, y + 1);
                        pixelColor[1, 2] = source.GetPixel(x + 1, y + 2);
                        pixelColor[2, 0] = source.GetPixel(x + 2, y);
                        pixelColor[2, 1] = source.GetPixel(x + 2, y + 1);
                        pixelColor[2, 2] = source.GetPixel(x + 2, y + 2);

                        pixel = (((pixelColor[0, 0].Blue * matrix.Matrix[0, 0]) +
                            (pixelColor[1, 0].Blue * matrix.Matrix[1, 0]) +
                            (pixelColor[2, 0].Blue * matrix.Matrix[2, 0]) +
                            (pixelColor[0, 1].Blue * matrix.Matrix[0, 1]) +
                            (pixelColor[1, 1].Blue * matrix.Matrix[1, 1]) +
                            (pixelColor[2, 1].Blue * matrix.Matrix[2, 1]) +
                            (pixelColor[0, 2].Blue * matrix.Matrix[0, 2]) +
                            (pixelColor[1, 2].Blue * matrix.Matrix[1, 2]) +
                            (pixelColor[2, 2].Blue * matrix.Matrix[2, 2])) / factor) + matrix.Offset;

                        if (pixel < 0) pixel = 0;
                        else if (pixel > 255) pixel = 255;

                        color.Blue = (byte)pixel;

                        pixel = (((pixelColor[0, 0].Green * matrix.Matrix[0, 0]) +
                            (pixelColor[1, 0].Green * matrix.Matrix[1, 0]) +
                            (pixelColor[2, 0].Green * matrix.Matrix[2, 0]) +
                            (pixelColor[0, 1].Green * matrix.Matrix[0, 1]) +
                            (pixelColor[1, 1].Green * matrix.Matrix[1, 1]) +
                            (pixelColor[2, 1].Green * matrix.Matrix[2, 1]) +
                            (pixelColor[0, 2].Green * matrix.Matrix[0, 2]) +
                            (pixelColor[1, 2].Green * matrix.Matrix[1, 2]) +
                            (pixelColor[2, 2].Green * matrix.Matrix[2, 2])) / factor) + matrix.Offset;

                        if (pixel < 0) pixel = 0;
                        else if (pixel > 255) pixel = 255;

                        color.Green = (byte)pixel;

                        pixel = (((pixelColor[0, 0].Red * matrix.Matrix[0, 0]) +
                            (pixelColor[1, 0].Red * matrix.Matrix[1, 0]) +
                            (pixelColor[2, 0].Red * matrix.Matrix[2, 0]) +
                            (pixelColor[0, 1].Red * matrix.Matrix[0, 1]) +
                            (pixelColor[1, 1].Red * matrix.Matrix[1, 1]) +
                            (pixelColor[2, 1].Red * matrix.Matrix[2, 1]) +
                            (pixelColor[0, 2].Red * matrix.Matrix[0, 2]) +
                            (pixelColor[1, 2].Red * matrix.Matrix[1, 2]) +
                            (pixelColor[2, 2].Red * matrix.Matrix[2, 2])) / factor) + matrix.Offset;

                        if (pixel < 0) pixel = 0;
                        else if (pixel > 255) pixel = 255;

                        color.Red = (byte)pixel;

                        color.Alpha = pixelColor[1, 1].Alpha;

                        dest.SetPixel(x + 1, y + 1, color);
                    }
                }
            }

            return result;
        }
Exemplo n.º 23
0
        private static Rectangle TrimTransparentFindHeight(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int y = rect.Height - 1; y >= rect.Y; y--)
            {
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Height = y - rect.Y + 1;
                        return rect;
                    }
                }
            }

            return rect;
        }
Exemplo n.º 24
0
        public static Image Apply(this ConvolutionMatrix matrix, Image img)
        {
            int factor = Math.Max(matrix.Factor, 1);

            Bitmap result = (Bitmap)img.Clone();

            using (UnsafeBitmap source = new UnsafeBitmap((Bitmap)img, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap dest = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
                {
                    int height = source.Height - 2;
                    int width  = source.Width - 2;
                    ColorBgra[,] pixelColor = new ColorBgra[3, 3];
                    int       pixel;
                    ColorBgra color = new ColorBgra();

                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            pixelColor[0, 0] = source.GetPixel(x, y);
                            pixelColor[0, 1] = source.GetPixel(x, y + 1);
                            pixelColor[0, 2] = source.GetPixel(x, y + 2);
                            pixelColor[1, 0] = source.GetPixel(x + 1, y);
                            pixelColor[1, 1] = source.GetPixel(x + 1, y + 1);
                            pixelColor[1, 2] = source.GetPixel(x + 1, y + 2);
                            pixelColor[2, 0] = source.GetPixel(x + 2, y);
                            pixelColor[2, 1] = source.GetPixel(x + 2, y + 1);
                            pixelColor[2, 2] = source.GetPixel(x + 2, y + 2);

                            pixel = (((pixelColor[0, 0].Blue * matrix.Matrix[0, 0]) +
                                      (pixelColor[1, 0].Blue * matrix.Matrix[1, 0]) +
                                      (pixelColor[2, 0].Blue * matrix.Matrix[2, 0]) +
                                      (pixelColor[0, 1].Blue * matrix.Matrix[0, 1]) +
                                      (pixelColor[1, 1].Blue * matrix.Matrix[1, 1]) +
                                      (pixelColor[2, 1].Blue * matrix.Matrix[2, 1]) +
                                      (pixelColor[0, 2].Blue * matrix.Matrix[0, 2]) +
                                      (pixelColor[1, 2].Blue * matrix.Matrix[1, 2]) +
                                      (pixelColor[2, 2].Blue * matrix.Matrix[2, 2])) / factor) + matrix.Offset;

                            if (pixel < 0)
                            {
                                pixel = 0;
                            }
                            else if (pixel > 255)
                            {
                                pixel = 255;
                            }

                            color.Blue = (byte)pixel;

                            pixel = (((pixelColor[0, 0].Green * matrix.Matrix[0, 0]) +
                                      (pixelColor[1, 0].Green * matrix.Matrix[1, 0]) +
                                      (pixelColor[2, 0].Green * matrix.Matrix[2, 0]) +
                                      (pixelColor[0, 1].Green * matrix.Matrix[0, 1]) +
                                      (pixelColor[1, 1].Green * matrix.Matrix[1, 1]) +
                                      (pixelColor[2, 1].Green * matrix.Matrix[2, 1]) +
                                      (pixelColor[0, 2].Green * matrix.Matrix[0, 2]) +
                                      (pixelColor[1, 2].Green * matrix.Matrix[1, 2]) +
                                      (pixelColor[2, 2].Green * matrix.Matrix[2, 2])) / factor) + matrix.Offset;

                            if (pixel < 0)
                            {
                                pixel = 0;
                            }
                            else if (pixel > 255)
                            {
                                pixel = 255;
                            }

                            color.Green = (byte)pixel;

                            pixel = (((pixelColor[0, 0].Red * matrix.Matrix[0, 0]) +
                                      (pixelColor[1, 0].Red * matrix.Matrix[1, 0]) +
                                      (pixelColor[2, 0].Red * matrix.Matrix[2, 0]) +
                                      (pixelColor[0, 1].Red * matrix.Matrix[0, 1]) +
                                      (pixelColor[1, 1].Red * matrix.Matrix[1, 1]) +
                                      (pixelColor[2, 1].Red * matrix.Matrix[2, 1]) +
                                      (pixelColor[0, 2].Red * matrix.Matrix[0, 2]) +
                                      (pixelColor[1, 2].Red * matrix.Matrix[1, 2]) +
                                      (pixelColor[2, 2].Red * matrix.Matrix[2, 2])) / factor) + matrix.Offset;

                            if (pixel < 0)
                            {
                                pixel = 0;
                            }
                            else if (pixel > 255)
                            {
                                pixel = 255;
                            }

                            color.Red = (byte)pixel;

                            color.Alpha = pixelColor[1, 1].Alpha;

                            dest.SetPixel(x + 1, y + 1, color);
                        }
                    }
                }

            return(result);
        }
Exemplo n.º 25
0
        private static Rectangle TrimTransparentFindY(UnsafeBitmap unsafeBitmap, Rectangle rect)
        {
            for (int y = rect.Y; y < rect.Height; y++)
            {
                for (int x = rect.X; x < rect.Width; x++)
                {
                    if (unsafeBitmap.GetPixel(x, y).Alpha > 0)
                    {
                        rect.Y = y;
                        return rect;
                    }
                }
            }

            return rect;
        }
Exemplo n.º 26
0
        // https://lotsacode.wordpress.com/2010/12/08/fast-blur-box-blur-with-accumulator/
        public static void BoxBlur(Bitmap bmp, int range)
        {
            if (range > 1)
            {
                if (range.IsEvenNumber())
                {
                    range++;
                }

                using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true))
                {
                    BoxBlurHorizontal(unsafeBitmap, range);
                    BoxBlurVertical(unsafeBitmap, range);
                    BoxBlurHorizontal(unsafeBitmap, range);
                    BoxBlurVertical(unsafeBitmap, range);
                }
            }
        }