Beispiel #1
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);
        }
Beispiel #2
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);
        }
        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;
        }
Beispiel #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;
        }
Beispiel #5
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);
             }
         }
     }
 }
Beispiel #6
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);
        }
        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);
        }
        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;
        }
Beispiel #9
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);
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #10
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;
                    }
                }
            }
        }
Beispiel #11
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]);
                }
            }
        }