Esempio n. 1
0
        public static Bitmap Soften(this Bitmap bmp, Kernel kernel)
        {
            if (kernel == Kernel.Conv3x3)
            {
                var filterSettings = new ConvFilterSettings();
                filterSettings.Factor = 1.0 / 8.0;
                filterSettings.Bias   = 0.0;
                filterSettings.Matrix =
                    new double[, ] {
                    { 1, 1, 1, },
                    { 1, 1, 1, },
                    { 1, 1, 1, },
                };

                return(ApplyConvolutionFilter(bmp, filterSettings));
            }
            else if (kernel == Kernel.Conv5x5)
            {
                throw new NotImplementedException();
            }
            else if (kernel == Kernel.Conv7x7)
            {
                throw new NotImplementedException();
            }
            return(null);
        }
Esempio n. 2
0
        public static Bitmap HighPass(this Bitmap bmp)
        {
            var filterSettings = new ConvFilterSettings();

            filterSettings.Factor = 1.0 / 16.0;
            filterSettings.Bias   = 128.0;
            filterSettings.Matrix =
                new double[, ] {
                { -1, -2, -1, },
                { -2, 12, -2, },
                { -1, -2, -1, }
            };

            return(ApplyConvolutionFilter(bmp, filterSettings));
        }
Esempio n. 3
0
        public static Bitmap Emboss(this Bitmap bmp)
        {
            var filterSettings = new ConvFilterSettings();

            filterSettings.Factor = 1.0;
            filterSettings.Bias   = 128.0;
            filterSettings.Matrix =
                new double[, ] {
                { 2, 0, 0, },
                { 0, -1, 0, },
                { 0, 0, -1, },
            };

            return(ApplyConvolutionFilter(bmp, filterSettings));
        }
Esempio n. 4
0
        public static Bitmap EdgeDetection(this Bitmap bmp)
        {
            var filterSettings = new ConvFilterSettings();

            filterSettings.Factor = 1.0;
            filterSettings.Bias   = 0.0;
            filterSettings.Matrix =
                new double[, ] {
                { -1, -1, -1, },
                { -1, 8, -1, },
                { -1, -1, -1, },
            };

            return(ApplyConvolutionFilter(bmp, filterSettings));
        }
Esempio n. 5
0
        public static Bitmap MotionBlur(this Bitmap bmp)
        {
            var filterSettings = new ConvFilterSettings();

            filterSettings.Factor = 1.0 / 18.0;
            filterSettings.Bias   = 0.0;
            filterSettings.Matrix =
                new double[, ] {
                { 1, 0, 0, 0, 0, 0, 0, 0, 1, },
                { 0, 1, 0, 0, 0, 0, 0, 1, 0, },
                { 0, 0, 1, 0, 0, 0, 1, 0, 0, },
                { 0, 0, 0, 1, 0, 1, 0, 0, 0, },
                { 0, 0, 0, 0, 1, 0, 0, 0, 0, },
                { 0, 0, 0, 1, 0, 1, 0, 0, 0, },
                { 0, 0, 1, 0, 0, 0, 1, 0, 0, },
                { 0, 1, 0, 0, 0, 0, 0, 1, 0, },
                { 1, 0, 0, 0, 0, 0, 0, 0, 1, },
            };

            return(ApplyConvolutionFilter(bmp, filterSettings));
        }
Esempio n. 6
0
        public static Bitmap GaussianBlur(this Bitmap bmp, Kernel kernel)
        {
            if (kernel == Kernel.Conv3x3)
            {
                var filterSettings = new ConvFilterSettings();
                filterSettings.Factor = 1.0 / 16.0;
                filterSettings.Bias   = 0.0;
                filterSettings.Matrix =
                    new double[, ] {
                    { 1, 2, 1, },
                    { 2, 4, 2, },
                    { 1, 2, 1, },
                };

                return(ApplyConvolutionFilter(bmp, filterSettings));
            }
            else if (kernel == Kernel.Conv5x5)
            {
                var filterSettings = new ConvFilterSettings();
                filterSettings.Factor = 1.0 / 159.0;
                filterSettings.Bias   = 0.0;
                filterSettings.Matrix =
                    new double[, ] {
                    { 2, 04, 05, 04, 2, },
                    { 4, 09, 12, 09, 4, },
                    { 5, 12, 15, 12, 5, },
                    { 4, 09, 12, 09, 4, },
                    { 2, 04, 05, 04, 2, },
                };

                return(ApplyConvolutionFilter(bmp, filterSettings));
            }
            else if (kernel == Kernel.Conv7x7)
            {
                throw new NotImplementedException();
            }
            return(null);
        }
Esempio n. 7
0
        private static Bitmap ApplyConvolutionFilter(Bitmap sourceBitmap, ConvFilterSettings filter)
        {
            BitmapData sourceData = sourceBitmap.LockBits(
                new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            int bufferSize = sourceData.Stride * sourceData.Height;

            byte[] pixelBuffer  = new byte[bufferSize];
            byte[] resultBuffer = new byte[bufferSize];

            Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);
            sourceBitmap.UnlockBits(sourceData);

            double blue;
            double green;
            double red;

            int filterWidth  = filter.Matrix.GetLength(1);
            int filterHeight = filter.Matrix.GetLength(0);

            int filterOffset = (filterWidth - 1) / 2;
            int calcOffset;

            int byteOffset;

            for (int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++)
            {
                for (int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++)
                {
                    blue       = green = red = 0;
                    byteOffset = offsetY * sourceData.Stride + offsetX * 4;

                    for (int filterY = -filterOffset; filterY <= filterOffset; filterY++)
                    {
                        for (int filterX = -filterOffset; filterX <= filterOffset; filterX++)
                        {
                            calcOffset = byteOffset + (filterX * 4) + (filterY * sourceData.Stride);

                            blue += (double)(pixelBuffer[calcOffset]) * filter.Matrix[filterY + filterOffset, filterX + filterOffset];

                            green += (double)(pixelBuffer[calcOffset + 1]) * filter.Matrix[filterY + filterOffset, filterX + filterOffset];

                            red += (double)(pixelBuffer[calcOffset + 2]) * filter.Matrix[filterY + filterOffset, filterX + filterOffset];
                        }
                    }

                    blue = filter.Factor * blue + filter.Bias;
                    if (blue > 255)
                    {
                        blue = 255;
                    }
                    if (blue < 0)
                    {
                        blue = 0;
                    }

                    green = filter.Factor * green + filter.Bias;
                    if (green > 255)
                    {
                        green = 255;
                    }
                    if (green < 0)
                    {
                        green = 0;
                    }

                    red = filter.Factor * red + filter.Bias;
                    if (red > 255)
                    {
                        red = 255;
                    }
                    if (red < 0)
                    {
                        red = 0;
                    }


                    resultBuffer[byteOffset]     = (byte)(blue);
                    resultBuffer[byteOffset + 1] = (byte)(green);
                    resultBuffer[byteOffset + 2] = (byte)(red);
                    resultBuffer[byteOffset + 3] = 255;
                }
            }

            Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);

            BitmapData resultData = resultBitmap.LockBits(
                new Rectangle(0, 0, resultBitmap.Width, resultBitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

            Marshal.Copy(resultBuffer, 0, resultData.Scan0, resultBuffer.Length);
            resultBitmap.UnlockBits(resultData);

            return(resultBitmap);
        }