public Bitmap Execute(Bitmap initial, Filter filter)
        {
            var threadCount = System.Environment.ProcessorCount;

            Color[,] result = new Color[initial.Height, initial.Width];


            BitmapToColorArrayConverter bitmapToColorArray = new BitmapToColorArrayConverter();

            Color[,] colMatrix = bitmapToColorArray.Convert(initial);
            int size = initial.Height / threadCount;

            List <Thread> threadList = new List <Thread>();

            for (int i = 0, start = 0; i < threadCount; i++)
            {
                int finish = start + size;

                ImageProcessInfo info = new ImageProcessInfo(start, finish, colMatrix, filter);

                var thread = new Thread(delegate()
                {
                    Convolution.Process(info, result);
                });


                threadList.Add(thread);

                thread.Start();

                start += size;
            }

            threadList.ForEach(thread => thread.Join());
            ColorArrayToBitmapConverter colorArrayToBitmap = new ColorArrayToBitmapConverter();

            return(colorArrayToBitmap.Convert(result));
        }
Exemplo n.º 2
0
        public static void Process(ImageProcessInfo info, Color[,] result)
        {
            int height = info.Colors.GetLength(0);
            int width  = info.Colors.GetLength(1);

            //Color[,] result = new Color[height, width];
            for (int x = info.StartPosition; x <= info.FinishPosition; x++)
            {
                for (int y = 0; y < width; y++)
                {
                    double red   = 0,
                           green = 0,
                           blue  = 0;
                    for (int i = 0; i < info.Filter.Size; i++)
                    {
                        for (int j = 0; j < info.Filter.Size; j++)
                        {
                            if ((x + i < height && y + j < width))
                            {
                                var color = info.Colors[x + i, y + j];
                                red   += color.R * info.Filter[i, j];
                                green += color.G * info.Filter[i, j];
                                blue  += color.B * info.Filter[i, j];
                            }
                        }
                    }
                    if (x < height && y < width)
                    {
                        result[x, y] = Color.FromArgb(255, Filter.Normalize(red, info.Filter),
                                                      Filter.Normalize(green, info.Filter),
                                                      Filter.Normalize(blue, info.Filter));
                    }
                }
            }


            //return result;
        }