Example #1
0
 protected static int Normalize(double value, ConvolutionFilter filter)
 {
     if (filter.Divisor == 0)
     {
         filter.Divisor = 1;
     }
     return(Math.Min(Math.Max((int)(value / filter.Divisor + filter.Offset), 0), 255));
 }
Example #2
0
        public Bitmap ComputeWith(ConvolutionFilter filter)
        {
            var result = new Bitmap(original.Width, original.Height);
            var offset = filter.Size / 2;

            filter.Offset = 0;
            for (int i = 0; i < filter.Size; i++)
            {
                for (int j = 0; j < filter.Size; j++)
                {
                    filter.Divisor += (int)filter._matrix[i, j];
                }
            }

            if (filter.Divisor == 0)
            {
                filter.Divisor = 1;
            }


            for (int x = 0; x < original.Width; x++)
            {
                for (int y = 0; y < original.Height; y++)
                {
                    var colorMap = new Color[filter.Size, filter.Size];

                    for (int filterY = 0; filterY < filter.Size; filterY++)
                    {
                        int pk = (filterY + x - offset <= 0) ? 0 :
                                 (filterY + x - offset >= original.Width - 1) ? original.Width - 1 : filterY + x - offset;
                        for (int filterX = 0; filterX < filter.Size; filterX++)
                        {
                            int pl = (filterX + y - offset <= 0) ? 0 :
                                     (filterX + y - offset >= original.Height - 1) ? original.Height - 1 : filterX + y - offset;

                            colorMap[filterY, filterX] = original.GetPixel(pk, pl);
                        }
                    }

                    result.SetPixel(x, y, colorMap * filter);
                }
            }

            original = result;
            return(result);
        }