コード例 #1
0
ファイル: Program.cs プロジェクト: Nk185/GaussianBlur
        static void Main(string[] Args)
        {
            Again:
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Filter = "JPEG (*.jpg)|*.jpg";
            openDlg.Title = "Select your image to blur: ";

            if (openDlg.ShowDialog() == DialogResult.OK)
            {
                Bitmap bmp;
                BitmapData bmpData;
                ColorStructure rgbValues;
                GaussianDistribution gaussianDistribution;
                GaussianBlurProcessing gBlurProcessing;
                Rectangle rect;
                Rectangle blurRect;

                int[] userBlurRect = new int[4];
                int userBlurRadius;
                sbyte highlightingCoef;
                string userRectParams;
                string inputFileAddr;

                inputFileAddr = openDlg.FileName;
                outputFileAddr = Path.GetDirectoryName(inputFileAddr) + @"\[Result] " + (new FileInfo(inputFileAddr).Name);
                bmp = new Bitmap(inputFileAddr);
                userBlurRadius = Convert.ToInt32(Interaction.InputBox("Enter your blur radius.", "Blur radius selection", "1"));
                highlightingCoef = Convert.ToSByte(Interaction.InputBox("Enter your highlighting coefficient. It's should be in [-127 ; 127] \n\nTo save the same light level, leave default value: 0.",
                    "Highlighting coefficient selection", "0"));
                userRectParams = Interaction.InputBox("Enter your blur rectangle using spaces between values (x  y  width  height).\nTo blur all image, enter \"$gb_blurAll\". \nOr \"$gb_blurCentre\" to blur whole centre line" +
                "\n\nPlease, notice that sum of (x + width) must be lower or equal to " + Convert.ToString(bmp.Width) + ", and sum of (y + height) must be lower or equal to "
                + Convert.ToString(bmp.Height) + ".", "Blur rectangle selection", "$gb_blurAll");

                #region BlurRectangleSettingUp
                if (userRectParams != "$gb_blurAll" && userRectParams != "$gb_blurCentre" && userRectParams != "$gd_calcSingle" && userRectParams != "$gd_calc2D")
                {
                    int i = 0;
                    foreach (string value in userRectParams.Split(new string[1] { " " }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        userBlurRect[i] = Convert.ToInt32(value);
                        i++;
                    }

                    if ((userBlurRect[0] + userBlurRect[2] <= bmp.Width) && (userBlurRect[1] + userBlurRect[3] <= bmp.Width) && (userBlurRect[0] >= 0) & (userBlurRect[1] >= 0))
                        blurRect = new Rectangle(userBlurRect[0], userBlurRect[1], userBlurRect[2] - 1, userBlurRect[3] - 1);
                    else
                        throw new Exception("Incorrect rectangle value(s). Please, restart application.");
                }
                else if (userRectParams == "$gb_blurAll")
                    blurRect = new Rectangle(0, 0, bmp.Width - 1, bmp.Height - 1);
                else if (userRectParams == "$gb_blurCentre")
                    blurRect = new Rectangle(0, (bmp.Height / 2) - (bmp.Height / 4), bmp.Width - 1, (bmp.Height / 2) - 1);
                else if (userRectParams == "$gd_calcSingle")
                {
                    GaussianDistribution gd = new GaussianDistribution(userBlurRadius, 0, false);
                    double[] gdk = gd.GetKernel;

                    foreach (double koef in gdk)
                        Console.WriteLine(koef);

                    using (Stream Str = new FileStream(Path.GetDirectoryName(inputFileAddr) + @"\GaussKernel.txt", FileMode.Create))
                    {
                        using (StreamWriter StrWriter = new StreamWriter(Str))
                        {
                            foreach (double koef in gdk)
                                StrWriter.WriteLine(koef);
                        }
                    }

                    gd = null;
                    gdk = null;

                    Console.Clear();

                    goto Again;
                }
                else if (userRectParams == "$gd_calc2D")
                {
                    GaussianDistribution gd = new GaussianDistribution(userBlurRadius, 0, true);
                    double[,] gdk = gd.GetKernel2D;

                    foreach (double koef in gdk)
                        Console.WriteLine(koef);

                    for (int i = 0; i < gdk.GetLength(0); i++)
                        using (Stream Str = new FileStream(Path.GetDirectoryName(inputFileAddr) + @"\GaussKernel2D[" + i + "].txt", FileMode.Create))
                        {
                            using (StreamWriter StrWriter = new StreamWriter(Str))
                            {
                                for (int j = 0; j < gdk.GetLength(1); j++)
                                    StrWriter.WriteLine(gdk[i, j]);
                            }
                        }

                    goto Again;
                }
                else
                    throw new Exception("Incorrect rectangle value(s). Please, restart application.");
                #endregion

                Console.WriteLine("--------------------------------- [Summary] ---------------------------------");
                Console.WriteLine("> Chosen image: {0} ({1} x {2})", inputFileAddr, bmp.Width, bmp.Height);
                Console.WriteLine("> Chosen blur radius: {0}", userBlurRadius);
                Console.WriteLine("> Chosen highlighting coefficient: {0}", highlightingCoef);
                Console.WriteLine("> Chosen blur rectangle: x = {0} | y = {1} | width = {2} | height = {3}", blurRect.X, blurRect.Y, blurRect.Width + 1, blurRect.Height + 1);
                Console.WriteLine("\n> Output file name: {0}", outputFileAddr);
                Console.WriteLine("-----------------------------------------------------------------------------");
                Console.Write("> If it's correct press '1', otherwise press another key: ");

                if (Console.ReadKey().KeyChar != '1')
                {
                    Console.Clear();
                    goto Again;
                }

                gaussianDistribution = new GaussianDistribution(userBlurRadius, highlightingCoef, false);
                rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
                rgbValues = ConvertTo2D(ref bmpData, ref bmp);
                gBlurProcessing = new GaussianBlurProcessing(gaussianDistribution, ref rgbValues, blurRect, bmp, bmpData);
                gBlurProcessing.Done += gbp_Done;

                Console.WriteLine("\n\n> Processing image. It could take awhile...");
                timer1 = new Stopwatch();
                timer1.Start();
                gBlurProcessing.Start();

                Console.ReadLine();
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Nk185/GaussianBlur
        private void __ProcessRedH(ColorStructure rgbValues, GaussianDistribution gd, Rectangle blurRect)
        {
            #region Left -> Right
            for (int i = blurRect.Y; i <= blurRect.Y + blurRect.Height; i++)
            {
                for (int j = blurRect.X; j <= blurRect.X + blurRect.Width; j++)
                {
                    double newValue = 0.0;
                    double divisor = 0.0;
                    int kernelAddr;

                    for (int rad = -gd.GetRadius; rad <= gd.GetRadius; rad++)
                    {
                        kernelAddr = rad + gd.GetRadius;
                        if (j + rad >= blurRect.X && j + rad <= blurRect.X + blurRect.Width)
                        {
                            newValue += rgbValues.Red[i, j + rad] * gd.GetKernel[kernelAddr];
                            divisor += gd.GetKernel[kernelAddr];
                        }
                    }

                    rgbValues.Red[i, j] = Convert.ToByte(newValue / divisor);
                    newValue = 0.0;

                    if (rgbValues.Red[i, j] + gd.GetOffset <= 255 && rgbValues.Red[i, j] + gd.GetOffset >= 0)
                        rgbValues.Red[i, j] = (byte)(rgbValues.Red[i, j] + gd.GetOffset);
                    else if (rgbValues.Red[i, j] + gd.GetOffset <= 0)
                        rgbValues.Red[i, j] = 0;
                    else if (rgbValues.Red[i, j] + gd.GetOffset >= 255)
                        rgbValues.Red[i, j] = 255;
                }
            }
            #endregion
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: Nk185/GaussianBlur
        private void __ProcessRedV(ColorStructure rgbValues, GaussianDistribution gd, Rectangle blurRect)
        {
            #region Up -> Down
            for (int j = blurRect.X; j <= blurRect.X + blurRect.Width; j++)
            {
                for (int i = blurRect.Y; i <= blurRect.Y + blurRect.Height; i++)
                {
                    double newValue = 0.0;
                    double divisor = 0.0;
                    int kernelAddr = 0;

                    for (int rad = -gd.GetRadius; rad <= gd.GetRadius; rad++)
                    {
                        kernelAddr = rad + gd.GetRadius;

                        if ((i + rad >= blurRect.Y) && (i + rad <= blurRect.Y + blurRect.Height))
                        {
                            newValue += rgbValues.Red[i + rad, j] * gd.GetKernel[kernelAddr];
                            divisor += gd.GetKernel[kernelAddr];
                        }
                    }

                    rgbValues.Red[i, j] = Convert.ToByte(newValue / divisor);
                    newValue = 0.0;
                }
            }
            #endregion
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: Nk185/GaussianBlur
 public GaussianBlurProcessing(GaussianDistribution gaussianKernel, ref ColorStructure rgbValues, Rectangle blurRectangle, Bitmap bmp, BitmapData bmpData)
 {
     this._GaussianKernel = gaussianKernel;
     this._RGBValues = rgbValues;
     this._BlurRect = blurRectangle;
     this._Bitmap = bmp;
     this._BitmapData = bmpData;
 }