コード例 #1
0
ファイル: Filter.cs プロジェクト: DarkDeyus/MatrixFilters
        static public Bitmap UseFilter(Bitmap bitmap, int[,] filter, double divisor, int displacement, IBrush brush)
        {
            int      width  = bitmap.Width;
            int      height = bitmap.Height;
            PixelMap copy   = new PixelMap(bitmap);
            PixelMap map    = new PixelMap(width, height);

            for (int i = 0; i < width; i++)
            {
                map[i, 0]          = copy[i, 0];
                map[i, height - 1] = copy[i, height - 1];
            }

            for (int j = 0; j < height; j++)
            {
                map[0, j]         = copy[0, j];
                map[width - 1, j] = copy[width - 1, j];
            }


            for (int x = 1; x < width - 1; x++)
            {
                for (int y = 1; y < height - 1; y++)
                {
                    //use filter
                    if (brush.PointInBrush(x, y))
                    {
                        int R = 0;
                        int G = 0;
                        int B = 0;

                        for (int i = -1; i <= 1; i++)
                        {
                            for (int j = -1; j <= 1; j++)
                            {
                                R += copy[x + i, y + j].R * filter[i + 1, j + 1];
                                G += copy[x + i, y + j].G * filter[i + 1, j + 1];
                                B += copy[x + i, y + j].B * filter[i + 1, j + 1];
                            }
                        }
                        R         = (int)Math.Round(R / divisor) + displacement;
                        G         = (int)Math.Round(G / divisor) + displacement;
                        B         = (int)Math.Round(B / divisor) + displacement;
                        R         = Math.Max(Math.Min(255, R), 0);
                        G         = Math.Max(Math.Min(255, G), 0);
                        B         = Math.Max(Math.Min(255, B), 0);
                        map[x, y] = new Pixel(R, G, B);
                    }
                    else
                    {
                        map[x, y] = copy[x, y];
                    }
                }
            }

            return(map.GetBitmap());
        }