Ejemplo n.º 1
0
        // Obraz wyostrzony maską 3x3
        internal void sharpenBitmap(MyBitmap myBitmap)
        {
            myBitmap.PreviousBitmap = (Bitmap)myBitmap.CurrentBitmap.Clone();
            Bitmap sharpenImage = new Bitmap(myBitmap.BitmapInfo.SizeX, myBitmap.BitmapInfo.SizeY);
            int    filterWidth  = 3;
            int    filterHeight = 3;
            int    width        = myBitmap.BitmapInfo.SizeX;
            int    height       = myBitmap.BitmapInfo.SizeY;

            // Tablica maski.
            int[,] filter   = { { -1, -1, -1 }, { -1, 9, -1 }, { -1, -1, -1 } };
            Color[,] result = new Color[width, height];

            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    int   red = 0, green = 0, blue = 0;
                    Color imageColor = myBitmap.CurrentBitmap.GetPixel(x, y);

                    for (int filterX = 0; filterX < filterWidth; filterX++)
                    {
                        for (int filterY = 0; filterY < filterHeight; filterY++)
                        {
                            int imageX = (x - filterWidth / 2 + filterX + width) % width;
                            int imageY = (y - filterHeight / 2 + filterY + height) % height;

                            Color bmpColor = myBitmap.CurrentBitmap.GetPixel(imageX, imageY);
                            red   += bmpColor.R * filter[filterX, filterY];
                            green += bmpColor.G * filter[filterX, filterY];
                            blue  += bmpColor.B * filter[filterX, filterY];
                        }
                        // Ustawiamy wartość na zakres <0,255> jesli nie miesci sie w tym przedziale;
                        int r = Math.Min(Math.Max(red, 0), 255);
                        int g = Math.Min(Math.Max(green, 0), 255);
                        int b = Math.Min(Math.Max(blue, 0), 255);

                        result[x, y] = Color.FromArgb(r, g, b);
                    }
                }
            }
            for (int i = 0; i < width; ++i)
            {
                for (int j = 0; j < height; ++j)
                {
                    sharpenImage.SetPixel(i, j, result[i, j]);
                }
            }
            myBitmap.CurrentBitmap = MyBitmap.convertTo24bpp(sharpenImage);
            myBitmap.updateArrays();
        }