Exemple #1
0
        /// <summary>
        /// Inserts Gaussian noise into a bitmap.
        /// </summary>
        /// <param name="origem">Bitmap to be processed</param>
        /// <param name="montante">Standard deviation of perturbation for each color channel.</param>
        /// <returns>New, speckled bitmap</returns>
        /// <remarks>
        /// This code uses Bitmap.GetPixel and SetPixel methods for clarity. An implementation using Bitmap.LockBits
        /// and then directly modifying the image data may be faster, espectially for large images.
        /// </remarks>
        public static Bitmap AdicionarDesfoque(this Bitmap origem, double montante)
        {
            if (origem == null)
            {
                throw new ArgumentNullException("source");
            }
            Bitmap bitmap     = null;
            Bitmap tempBitmap = null;

            try
            {
                var gerar = new DesfocagemGaussianaAleatoria(0.0, montante, Utilitarios.SementesAleatorias());
                tempBitmap = new Bitmap(origem.Width, origem.Height);
                for (int y = 0; y < tempBitmap.Height; y++)
                {
                    for (int x = 0; x < tempBitmap.Width; x++)
                    {
                        var   pixel    = origem.GetPixel(x, y);
                        Color newPixel = AdicionarPixelsDesfoque(pixel, gerar);
                        tempBitmap.SetPixel(x, y, newPixel);
                    }
                }
                bitmap     = tempBitmap;
                tempBitmap = null;
            }
            finally
            {
                if (tempBitmap != null)
                {
                    tempBitmap.Dispose();
                }
            }
            return(bitmap);
        }
Exemple #2
0
        static Color AdicionarPixelsDesfoque(Color pixel, DesfocagemGaussianaAleatoria generator)
        {
            int newR = (int)pixel.R + generator.ProximoInteiro();
            int newG = (int)pixel.G + generator.ProximoInteiro();
            int newB = (int)pixel.B + generator.ProximoInteiro();
            int r    = Math.Max(0, Math.Min(newR, 255));
            int g    = Math.Max(0, Math.Min(newG, 255));
            int b    = Math.Max(0, Math.Min(newB, 255));

            return(Color.FromArgb(r, g, b));
        }