/// <summary>
 /// Generates a cellular texture image
 /// </summary>
 /// <param name="Width">Width</param>
 /// <param name="Height">Height</param>
 /// <param name="NumberOfPoints">Number of points</param>
 /// <param name="Seed">Random seed</param>
 /// <returns>Returns an image of a cellular texture</returns>
 public static Bitmap Generate(int Width, int Height, int NumberOfPoints, int Seed)
 {
     float[,] DistanceBuffer = new float[Width, Height];
     float MinimumDistance = float.MaxValue;
     float MaxDistance = float.MinValue;
     CellularMap Map = new CellularMap(Seed, Width, Height, NumberOfPoints);
     MaxDistance = Map.MaxDistance;
     MinimumDistance = Map.MinDistance;
     DistanceBuffer = Map.Distances;
     Bitmap ReturnValue = new Bitmap(Width, Height);
     BitmapData ImageData = ReturnValue.LockImage();
     int ImagePixelSize = ImageData.GetPixelSize();
     for (int x = 0; x < Width; ++x)
     {
         for (int y = 0; y < Height; ++y)
         {
             float Value = GetHeight(x, y, DistanceBuffer, MinimumDistance, MaxDistance);
             Value *= 255;
             int RGBValue = ((int)Value).Clamp(255, 0);
             ImageData.SetPixel(x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize);
         }
     }
     ReturnValue.UnlockImage(ImageData);
     return ReturnValue;
 }
 /// <summary>
 /// adds noise to the image
 /// </summary>
 /// <param name="OriginalImage">Image to add noise to</param>
 /// <param name="FileName">Location to save the image to (optional)</param>
 /// <param name="Amount">Amount of noise to add (defaults to 10)</param>
 /// <returns>New image object with the noise added</returns>
 public static Bitmap AddNoise(this Bitmap OriginalImage, int Amount = 10, string FileName = "")
 {
     OriginalImage.ThrowIfNull("OriginalImage");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = OriginalImage.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     int Height = NewBitmap.Height;
     int Width = NewBitmap.Width;
     Parallel.For(0, Width, x =>
     {
         for (int y = 0; y < Height; ++y)
         {
             Color CurrentPixel = OldData.GetPixel(x, y, OldPixelSize);
             int R = CurrentPixel.R + Random.Random.ThreadSafeNext(-Amount, Amount + 1);
             int G = CurrentPixel.G + Random.Random.ThreadSafeNext(-Amount, Amount + 1);
             int B = CurrentPixel.B + Random.Random.ThreadSafeNext(-Amount, Amount + 1);
             R = R.Clamp(255, 0);
             G = G.Clamp(255, 0);
             B = B.Clamp(255, 0);
             Color TempValue = Color.FromArgb(R, G, B);
             NewData.SetPixel(x, y, TempValue, NewPixelSize);
         }
     });
     NewBitmap.UnlockImage(NewData);
     OriginalImage.UnlockImage(OldData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 /// <summary>
 /// Generates a number of faults, returning an image
 /// </summary>
 /// <param name="Width">Width of the resulting image</param>
 /// <param name="Height">Height of the resulting image</param>
 /// <param name="NumberFaults">Number of faults</param>
 /// <param name="Seed">Random seed</param>
 /// <returns>An image from the resulting faults</returns>
 public static Bitmap Generate(int Width,int Height,int NumberFaults,int Seed)
 {
     float[,] Heights = new float[Width, Height];
     float IncreaseVal = 0.1f;
     System.Random Generator = new System.Random(Seed);
     for (int x = 0; x < NumberFaults; ++x)
     {
         IncreaseVal = GenerateFault(Width, Height, NumberFaults, Heights, IncreaseVal, Generator);
     }
     Bitmap ReturnValue = new Bitmap(Width, Height);
     BitmapData ImageData = ReturnValue.LockImage();
     int ImagePixelSize = ImageData.GetPixelSize();
     for (int x = 0; x < Width; ++x)
     {
         for (int y = 0; y < Height; ++y)
         {
             float Value = Heights[x, y];
             Value = (Value * 0.5f) + 0.5f;
             Value *= 255;
             int RGBValue = ((int)Value).Clamp(255, 0);
             ImageData.SetPixel(x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize);
         }
     }
     ReturnValue.UnlockImage(ImageData);
     return ReturnValue;
 }
 /// <summary>
 /// adds noise to the image
 /// </summary>
 /// <param name="OriginalImage">Image to add noise to</param>
 /// <param name="FileName">Location to save the image to (optional)</param>
 /// <param name="Amount">Amount of noise to add (defaults to 10)</param>
 /// <returns>New image object with the noise added</returns>
 public static Bitmap AddNoise(this Bitmap OriginalImage, int Amount = 10, string FileName = "")
 {
     if (OriginalImage == null)
         throw new ArgumentNullException("OriginalImage");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = OriginalImage.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     Random.Random TempRandom = new Random.Random();
     for (int x = 0; x < NewBitmap.Width; ++x)
     {
         for (int y = 0; y < NewBitmap.Height; ++y)
         {
             Color CurrentPixel = OldData.GetPixel(x, y, OldPixelSize);
             int R = CurrentPixel.R + TempRandom.Next(-Amount, Amount + 1);
             int G = CurrentPixel.G + TempRandom.Next(-Amount, Amount + 1);
             int B = CurrentPixel.B + TempRandom.Next(-Amount, Amount + 1);
             R = R > 255 ? 255 : R;
             R = R < 0 ? 0 : R;
             G = G > 255 ? 255 : G;
             G = G < 0 ? 0 : G;
             B = B > 255 ? 255 : B;
             B = B < 0 ? 0 : B;
             Color TempValue = Color.FromArgb(R, G, B);
             NewData.SetPixel(x, y, TempValue, NewPixelSize);
         }
     }
     NewBitmap.UnlockImage(NewData);
     OriginalImage.UnlockImage(OldData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 /// <summary>
 /// Generates perlin noise
 /// </summary>
 /// <param name="Width">Width of the resulting image</param>
 /// <param name="Height">Height of the resulting image</param>
 /// <param name="MaxRGBValue">MaxRGBValue</param>
 /// <param name="MinRGBValue">MinRGBValue</param>
 /// <param name="Frequency">Frequency</param>
 /// <param name="Amplitude">Amplitude</param>
 /// <param name="Persistance">Persistance</param>
 /// <param name="Octaves">Octaves</param>
 /// <param name="Seed">Random seed</param>
 /// <returns>An image containing perlin noise</returns>
 public static Bitmap Generate(int Width, int Height, int MaxRGBValue, int MinRGBValue,
     float Frequency, float Amplitude, float Persistance, int Octaves, int Seed)
 {
     Bitmap ReturnValue = new Bitmap(Width, Height);
     BitmapData ImageData = ReturnValue.LockImage();
     int ImagePixelSize = ImageData.GetPixelSize();
     float[,] Noise = GenerateNoise(Seed, Width, Height);
     for (int x = 0; x < Width; ++x)
     {
         for (int y = 0; y < Height; ++y)
         {
             float Value = GetValue(x, y, Width, Height, Frequency, Amplitude, Persistance, Octaves, Noise);
             Value = (Value * 0.5f) + 0.5f;
             Value *= 255;
             int RGBValue = ((int)Value).Clamp(MaxRGBValue, MinRGBValue);
             ImageData.SetPixel(x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize);
         }
     }
     ReturnValue.UnlockImage(ImageData);
     return ReturnValue;
 }
 public static Bitmap Equalize(this Bitmap OriginalImage, string FileName = "")
 {
     OriginalImage.ThrowIfNull("OriginalImage");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     RGBHistogram TempHistogram = new RGBHistogram(OriginalImage);
     TempHistogram.Equalize();
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = OriginalImage.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     int Width = NewBitmap.Width;
     int Height = NewBitmap.Height;
     Parallel.For(0, Width, x =>
     {
         for (int y = 0; y < Height; ++y)
         {
             Color Current = OldData.GetPixel(x, y, OldPixelSize);
             int NewR = (int)TempHistogram.R[Current.R];
             int NewG = (int)TempHistogram.G[Current.G];
             int NewB = (int)TempHistogram.B[Current.B];
             NewR = NewR.Clamp(255, 0);
             NewG = NewG.Clamp(255, 0);
             NewB = NewB.Clamp(255, 0);
             NewData.SetPixel(x, y, Color.FromArgb(NewR, NewG, NewB), NewPixelSize);
         }
     });
     NewBitmap.UnlockImage(NewData);
     OriginalImage.UnlockImage(OldData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
        public static Bitmap Jitter(this Bitmap OriginalImage, int MaxJitter = 5, string FileName = "")
        {
            OriginalImage.ThrowIfNull("OriginalImage");
            ImageFormat FormatUsing = FileName.GetImageFormat();
            Bitmap NewBitmap = new Bitmap(OriginalImage, OriginalImage.Width, OriginalImage.Height);
            BitmapData NewData = NewBitmap.LockImage();
            BitmapData OldData = OriginalImage.LockImage();
            int NewPixelSize = NewData.GetPixelSize();
            int OldPixelSize = OldData.GetPixelSize();
            int Width = NewBitmap.Width;
            int Height = NewBitmap.Height;
            Parallel.For(0, Width, x =>
            {
                for (int y = 0; y < Height; ++y)
                {
                    int NewX = Random.Random.ThreadSafeNext(-MaxJitter, MaxJitter);
                    int NewY = Random.Random.ThreadSafeNext(-MaxJitter, MaxJitter);
                    NewX += x;
                    NewY += y;
                    NewX = NewX.Clamp(Width - 1, 0);
                    NewY = NewY.Clamp(Height - 1, 0);

                    NewData.SetPixel(x, y, OldData.GetPixel(NewX, NewY, OldPixelSize), NewPixelSize);
                }
            });
            NewBitmap.UnlockImage(NewData);
            OriginalImage.UnlockImage(OldData);
            if (!string.IsNullOrEmpty(FileName))
                NewBitmap.Save(FileName, FormatUsing);
            return NewBitmap;
        }
Example #8
0
        public static Bitmap AdjustGamma(Bitmap OriginalImage, float Value)
        {
            Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
            BitmapData NewData = NewBitmap.LockImage();//Image.LockImage(NewBitmap);
            BitmapData OldData = OriginalImage.LockImage();//Image.LockImage(OriginalImage);
            int NewPixelSize = NewData.GetPixelSize();//Image.GetPixelSize(NewData);
            int OldPixelSize = OldData.GetPixelSize();//Image.GetPixelSize(OldData);

            int[] RedRamp = new int[256];
            int[] GreenRamp = new int[256];
            int[] BlueRamp = new int[256];
            for (int x = 0; x < 256; ++x)
            {
                RedRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
                GreenRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
                BlueRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
            }

            for (int x = 0; x < NewBitmap.Width; ++x)
            {
                for (int y = 0; y < NewBitmap.Height; ++y)
                {
                    Color Pixel = OldData.GetPixel(x, y, OldPixelSize);//Image.GetPixel(OldData, x, y, OldPixelSize);
                    int Red = RedRamp[Pixel.R];
                    int Green = GreenRamp[Pixel.G];
                    int Blue = BlueRamp[Pixel.B];
                    NewData.SetPixel(x, y, Color.FromArgb(Red, Green, Blue), NewPixelSize);//Image.SetPixel(NewData, x, y, Color.FromArgb(Red, Green, Blue), NewPixelSize);
                }
            }

            NewBitmap.UnlockImage(NewData);//Image.UnlockImage(NewBitmap, NewData);
            OriginalImage.UnlockImage(OldData);//Image.UnlockImage(OriginalImage, OldData);
            return NewBitmap;
        }
 public static Bitmap SinWave(this Bitmap OriginalImage, float Amplitude, float Frequency, bool XDirection, bool YDirection, string FileName = "")
 {
     OriginalImage.ThrowIfNull("OriginalImage");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = OriginalImage.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     int Width = NewBitmap.Width;
     int Height = NewBitmap.Height;
     Parallel.For(0, Width, x =>
     {
         for (int y = 0; y < Height; ++y)
         {
             double Value1 = 0;
             double Value2 = 0;
             if (YDirection)
                 Value1 = System.Math.Sin(((x * Frequency) * System.Math.PI) / 180.0d) * Amplitude;
             if (XDirection)
                 Value2 = System.Math.Sin(((y * Frequency) * System.Math.PI) / 180.0d) * Amplitude;
             Value1 = y - (int)Value1;
             Value2 = x - (int)Value2;
             while (Value1 < 0)
                 Value1 += Height;
             while (Value2 < 0)
                 Value2 += Width;
             while (Value1 >= Height)
                 Value1 -= Height;
             while (Value2 >= Width)
                 Value2 -= Width;
             NewData.SetPixel(x, y,
                 OldData.GetPixel((int)Value2, (int)Value1, OldPixelSize),
                 NewPixelSize);
         }
     });
     NewBitmap.UnlockImage(NewData);
     OriginalImage.UnlockImage(OldData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 /// <summary>
 /// Runs the motion detection algorithm
 /// </summary>
 /// <param name="NewImage">The "new" frame</param>
 /// <param name="OldImage">The "old" frame</param>
 /// <param name="Threshold">The threshold used to detect changes in the image</param>
 /// <param name="DetectionColor">Color to display changes in the images as</param>
 /// <returns>A bitmap indicating where changes between frames have occurred overlayed on top of the new image.</returns>
 public static Bitmap Process(Bitmap NewImage, Bitmap OldImage, int Threshold, Color DetectionColor)
 {
     if (NewImage == null)
         throw new ArgumentNullException("NewImage");
     if (OldImage == null)
         throw new ArgumentNullException("OldImage");
     if (DetectionColor == null)
         throw new ArgumentNullException("DetectionColor");
     using (Bitmap NewImage1 = NewImage.BlackAndWhite())
     {
         using (Bitmap OldImage1 = OldImage.BlackAndWhite())
         {
             using (Bitmap NewImage2 = NewImage1.SNNBlur(5))
             {
                 using (Bitmap OldImage2 = OldImage1.SNNBlur(5))
                 {
                     using (Bitmap OutputImage = new Bitmap(NewImage2, NewImage2.Width, NewImage2.Height))
                     {
                         using (Bitmap Overlay = new Bitmap(NewImage, NewImage.Width, NewImage.Height))
                         {
                             BitmapData NewImage2Data = NewImage2.LockImage();
                             int NewImage2PixelSize = NewImage2Data.GetPixelSize();
                             BitmapData OldImage2Data = OldImage2.LockImage();
                             int OldImage2PixelSize = OldImage2Data.GetPixelSize();
                             BitmapData OverlayData = Overlay.LockImage();
                             int OverlayPixelSize = OverlayData.GetPixelSize();
                             for (int x = 0; x < OutputImage.Width; ++x)
                             {
                                 for (int y = 0; y < OutputImage.Height; ++y)
                                 {
                                     Color NewPixel = NewImage2Data.GetPixel(x, y, NewImage2PixelSize);
                                     Color OldPixel = OldImage2Data.GetPixel(x, y, OldImage2PixelSize);
                                     if (System.Math.Pow((double)(NewPixel.R - OldPixel.R), 2.0) > Threshold)
                                     {
                                         OverlayData.SetPixel(x, y, Color.FromArgb(100, 0, 100), OverlayPixelSize);
                                     }
                                     else
                                     {
                                         OverlayData.SetPixel(x, y, Color.FromArgb(200, 0, 200), OverlayPixelSize);
                                     }
                                 }
                             }
                             Overlay.UnlockImage(OverlayData);
                             NewImage2.UnlockImage(NewImage2Data);
                             OldImage2.UnlockImage(OldImage2Data);
                             using (Bitmap Overlay2 = Overlay.EdgeDetection(25, DetectionColor))
                             {
                                 BitmapData Overlay2Data = Overlay2.LockImage();
                                 int Overlay2PixelSize = Overlay2Data.GetPixelSize();
                                 for (int x = 0; x < OutputImage.Width; ++x)
                                 {
                                     for (int y = 0; y < OutputImage.Height; ++y)
                                     {
                                         Color Pixel1 = Overlay2Data.GetPixel(x, y, Overlay2PixelSize);
                                         if (Pixel1.R != DetectionColor.R || Pixel1.G != DetectionColor.G || Pixel1.B != DetectionColor.B)
                                         {
                                             Overlay2Data.SetPixel(x, y, Color.FromArgb(200, 0, 200), Overlay2PixelSize);
                                         }
                                     }
                                 }
                                 Overlay2.UnlockImage(Overlay2Data);
                                 return OutputImage.Watermark(Overlay2, 1.0f, 0, 0, Color.FromArgb(200, 0, 200));
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 public static Bitmap NormalMap(this Bitmap ImageUsing,bool InvertX=false,bool InvertY=false)
 {
     ImageUsing.ThrowIfNull("ImageUsing");
     using (Bitmap TempImageX = ImageUsing.BumpMap(Direction.LeftRight,InvertX))
     {
         using (Bitmap TempImageY = ImageUsing.BumpMap(Direction.TopBottom, InvertY))
         {
             Bitmap ReturnImage = new Bitmap(TempImageX.Width, TempImageX.Height);
             BitmapData TempImageXData = TempImageX.LockImage();
             BitmapData TempImageYData = TempImageY.LockImage();
             BitmapData ReturnImageData = ReturnImage.LockImage();
             int TempImageXPixelSize = TempImageXData.GetPixelSize();
             int TempImageYPixelSize = TempImageYData.GetPixelSize();
             int ReturnImagePixelSize = ReturnImageData.GetPixelSize();
             int Width = TempImageX.Width;
             int Height = TempImageX.Height;
             Parallel.For(0, Height, y =>
             {
                 Math.Vector3 TempVector = new Utilities.Math.Vector3(0.0, 0.0, 0.0);
                 for (int x = 0; x < Width; ++x)
                 {
                     Color TempPixelX = TempImageXData.GetPixel(x, y, TempImageXPixelSize);
                     Color TempPixelY = TempImageYData.GetPixel(x, y, TempImageYPixelSize);
                     TempVector.X = (double)(TempPixelX.R) / 255.0;
                     TempVector.Y = (double)(TempPixelY.R) / 255.0;
                     TempVector.Z = 1.0;
                     TempVector.Normalize();
                     TempVector.X = ((TempVector.X + 1.0) / 2.0) * 255.0;
                     TempVector.Y = ((TempVector.Y + 1.0) / 2.0) * 255.0;
                     TempVector.Z = ((TempVector.Z + 1.0) / 2.0) * 255.0;
                     ReturnImageData.SetPixel(x, y,
                         Color.FromArgb((int)TempVector.X,
                             (int)TempVector.Y,
                             (int)TempVector.Z),
                         ReturnImagePixelSize);
                 }
             });
             TempImageX.UnlockImage(TempImageXData);
             TempImageY.UnlockImage(TempImageYData);
             ReturnImage.UnlockImage(ReturnImageData);
             return ReturnImage;
         }
     }
 }
 public static Bitmap Turbulence(this Bitmap OriginalImage, int Roughness = 8, float Power = 5.0f, int Seed = 25123864, string FileName = "")
 {
     OriginalImage.ThrowIfNull("OriginalImage");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     int Width = OriginalImage.Width;
     int Height = OriginalImage.Height;
     BitmapData OriginalData = OriginalImage.LockImage();
     int OriginalPixelSize = OriginalData.GetPixelSize();
     Bitmap NewBitmap = new Bitmap(Width, Height);
     BitmapData ReturnData = NewBitmap.LockImage();
     int ReturnPixelSize = ReturnData.GetPixelSize();
     using (Bitmap XNoise = PerlinNoise.Generate(Width, Height, 255, 0, 0.0625f, 1.0f, 0.5f, Roughness, Seed))
     {
         BitmapData XNoiseData = XNoise.LockImage();
         int XNoisePixelSize = XNoiseData.GetPixelSize();
         using (Bitmap YNoise = PerlinNoise.Generate(Width, Height, 255, 0, 0.0625f, 1.0f, 0.5f, Roughness, Seed * 2))
         {
             BitmapData YNoiseData = YNoise.LockImage();
             int YNoisePixelSize = YNoiseData.GetPixelSize();
             Parallel.For(0, Height, y =>
             {
                 for (int x = 0; x < Width; ++x)
                 {
                     float XDistortion = x + (GetHeight(x, y, XNoiseData, XNoisePixelSize) * Power);
                     float YDistortion = y + (GetHeight(x, y, YNoiseData, YNoisePixelSize) * Power);
                     int X1 = ((int)XDistortion).Clamp(Width - 1, 0);
                     int Y1 = ((int)YDistortion).Clamp(Height - 1, 0);
                     ReturnData.SetPixel(x, y, OriginalData.GetPixel(X1, Y1, OriginalPixelSize), ReturnPixelSize);
                 }
             });
             YNoise.UnlockImage(YNoiseData);
         }
         XNoise.UnlockImage(XNoiseData);
     }
     NewBitmap.UnlockImage(ReturnData);
     UnlockImage(OriginalImage, OriginalData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 /// <summary>
 /// Loads an image
 /// </summary>
 /// <param name="ImageUsing">Image to load</param>
 public virtual void LoadImage(Bitmap ImageUsing)
 {
     ImageUsing.ThrowIfNull("ImageUsing");
     BitmapData OldData = ImageUsing.LockImage();
     int PixelSize = OldData.GetPixelSize();
     Width = ImageUsing.Width;
     Height = ImageUsing.Height;
     R.Clear();
     G.Clear();
     B.Clear();
     for (int x = 0; x < Width; ++x)
     {
         for (int y = 0; y < Height; ++y)
         {
             Color TempColor = OldData.GetPixel(x, y, PixelSize);
             ++R[(int)TempColor.R];
             ++G[(int)TempColor.G];
             ++B[(int)TempColor.B];
         }
     }
     ImageUsing.UnlockImage(OldData);
 }
 public static Bitmap StretchContrast(this Bitmap OriginalImage, string FileName = "")
 {
     OriginalImage.ThrowIfNull("OriginalImage");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = OriginalImage.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     Color MinValue;
     Color MaxValue;
     GetMinMaxPixel(out MinValue, out MaxValue, OldData, OldPixelSize);
     int Width = NewBitmap.Width;
     int Height = NewBitmap.Height;
     Parallel.For(0, Width, x =>
     {
         for (int y = 0; y < Height; ++y)
         {
             Color CurrentPixel = OldData.GetPixel(x, y, OldPixelSize);
             Color TempValue = Color.FromArgb(Map(CurrentPixel.R, MinValue.R, MaxValue.R),
                 Map(CurrentPixel.G, MinValue.G, MaxValue.G),
                 Map(CurrentPixel.B, MinValue.B, MaxValue.B));
             NewData.SetPixel(x, y, TempValue, NewPixelSize);
         }
     });
     NewBitmap.UnlockImage(NewData);
     OriginalImage.UnlockImage(OldData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 public static Bitmap Threshold(this Bitmap OriginalImage, float Threshold = 0.5f, string FileName = "")
 {
     OriginalImage.ThrowIfNull("OriginalImage");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = OriginalImage.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     int Width = NewBitmap.Width;
     int Height = NewBitmap.Height;
     Parallel.For(0, Width, x =>
     {
         for (int y = 0; y < Height; ++y)
         {
             Color TempColor = OldData.GetPixel(x, y, OldPixelSize);
             if ((TempColor.R + TempColor.G + TempColor.B) / 755.0f > Threshold)
                 NewData.SetPixel(x, y, Color.White, NewPixelSize);
             else
                 NewData.SetPixel(x, y, Color.Black, NewPixelSize);
         }
     });
     NewBitmap.UnlockImage(NewData);
     OriginalImage.UnlockImage(OldData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 /// <summary>
 /// Sobel edge detection function
 /// </summary>
 /// <param name="Input">Image to manipulate</param>
 /// <param name="FileName">File to save to</param>
 /// <returns>A bitmap image</returns>
 public static Bitmap SobelEdgeDetection(this Bitmap Input, string FileName = "")
 {
     Input.ThrowIfNull("Input");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     using (Bitmap TempImage = Input.BlackAndWhite())
     {
         Filter TempFilter = new Filter(3, 3);
         TempFilter.MyFilter[0, 0] = -1;
         TempFilter.MyFilter[0, 1] = 0;
         TempFilter.MyFilter[0, 2] = 1;
         TempFilter.MyFilter[1, 0] = -2;
         TempFilter.MyFilter[1, 1] = 0;
         TempFilter.MyFilter[1, 2] = 2;
         TempFilter.MyFilter[2, 0] = -1;
         TempFilter.MyFilter[2, 1] = 0;
         TempFilter.MyFilter[2, 2] = 1;
         TempFilter.Absolute = true;
         using (Bitmap TempImageX = TempFilter.ApplyFilter(TempImage))
         {
             TempFilter = new Filter(3, 3);
             TempFilter.MyFilter[0, 0] = 1;
             TempFilter.MyFilter[0, 1] = 2;
             TempFilter.MyFilter[0, 2] = 1;
             TempFilter.MyFilter[1, 0] = 0;
             TempFilter.MyFilter[1, 1] = 0;
             TempFilter.MyFilter[1, 2] = 0;
             TempFilter.MyFilter[2, 0] = -1;
             TempFilter.MyFilter[2, 1] = -2;
             TempFilter.MyFilter[2, 2] = -1;
             TempFilter.Absolute = true;
             using (Bitmap TempImageY = TempFilter.ApplyFilter(TempImage))
             {
                 using (Bitmap NewBitmap = new Bitmap(TempImage.Width, TempImage.Height))
                 {
                     BitmapData NewData = NewBitmap.LockImage();
                     BitmapData OldData1 = TempImageX.LockImage();
                     BitmapData OldData2 = TempImageY.LockImage();
                     int NewPixelSize = NewData.GetPixelSize();
                     int OldPixelSize1 = OldData1.GetPixelSize();
                     int OldPixelSize2 = OldData2.GetPixelSize();
                     int Width = NewBitmap.Width;
                     int Height = NewBitmap.Height;
                     Parallel.For(0, Width, x =>
                     {
                         for (int y = 0; y < Height; ++y)
                         {
                             Color Pixel1 = OldData1.GetPixel(x, y, OldPixelSize1);
                             Color Pixel2 = OldData2.GetPixel(x, y, OldPixelSize2);
                             NewData.SetPixel(x, y,
                                 Color.FromArgb((Pixel1.R + Pixel2.R).Clamp(255, 0),
                                     (Pixel1.G + Pixel2.G).Clamp(255, 0),
                                     (Pixel1.B + Pixel2.B).Clamp(255, 0)),
                                 NewPixelSize);
                         }
                     });
                     NewBitmap.UnlockImage(NewData);
                     TempImageX.UnlockImage(OldData1);
                     TempImageY.UnlockImage(OldData2);
                     Bitmap NewBitmap2 = NewBitmap.Negative();
                     if (!string.IsNullOrEmpty(FileName))
                         NewBitmap2.Save(FileName, FormatUsing);
                     return NewBitmap2;
                 }
             }
         }
     }
 }
        public static Bitmap AdjustContrast(this Bitmap OriginalImage, float Value = 0, string FileName = "")
        {
            if (OriginalImage == null)
                throw new ArgumentNullException("OriginalImage");
            ImageFormat FormatUsing = FileName.GetImageFormat();
            Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
            BitmapData NewData = NewBitmap.LockImage();
            BitmapData OldData = OriginalImage.LockImage();
            int NewPixelSize = NewData.GetPixelSize();
            int OldPixelSize = OldData.GetPixelSize();
            Value = (100.0f + Value) / 100.0f;
            Value *= Value;
            int Width = NewBitmap.Width;
            int Height = NewBitmap.Height;

            Parallel.For(0, Width, x =>
            {
                for (int y = 0; y < Height; ++y)
                {
                    Color Pixel = OldData.GetPixel(x, y, OldPixelSize);
                    float Red = Pixel.R / 255.0f;
                    float Green = Pixel.G / 255.0f;
                    float Blue = Pixel.B / 255.0f;
                    Red = (((Red - 0.5f) * Value) + 0.5f) * 255.0f;
                    Green = (((Green - 0.5f) * Value) + 0.5f) * 255.0f;
                    Blue = (((Blue - 0.5f) * Value) + 0.5f) * 255.0f;
                    NewData.SetPixel(x, y,
                        Color.FromArgb(((int)Red).Clamp(255, 0),
                        ((int)Green).Clamp(255, 0),
                        ((int)Blue).Clamp(255, 0)),
                        NewPixelSize);
                }
            });
            NewBitmap.UnlockImage(NewData);
            OriginalImage.UnlockImage(OldData);
            if (!string.IsNullOrEmpty(FileName))
                NewBitmap.Save(FileName, FormatUsing);
            return NewBitmap;
        }
 public static Bitmap SNNBlur(this Bitmap OriginalImage, int Size = 3, string FileName = "")
 {
     OriginalImage.ThrowIfNull("OriginalImage");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = OriginalImage.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     int ApetureMinX = -(Size / 2);
     int ApetureMaxX = (Size / 2);
     int ApetureMinY = -(Size / 2);
     int ApetureMaxY = (Size / 2);
     int Width = NewBitmap.Width;
     int Height = NewBitmap.Height;
     Parallel.For(0, Width, x =>
     {
         for (int y = 0; y < Height; ++y)
         {
             int RValue = 0;
             int GValue = 0;
             int BValue = 0;
             int NumPixels = 0;
             for (int x2 = ApetureMinX; x2 < ApetureMaxX; ++x2)
             {
                 int TempX1 = x + x2;
                 int TempX2 = x - x2;
                 if (TempX1 >= 0 && TempX1 < Width && TempX2 >= 0 && TempX2 < Width)
                 {
                     for (int y2 = ApetureMinY; y2 < ApetureMaxY; ++y2)
                     {
                         int TempY1 = y + y2;
                         int TempY2 = y - y2;
                         if (TempY1 >= 0 && TempY1 < Height && TempY2 >= 0 && TempY2 < Height)
                         {
                             Color TempColor = OldData.GetPixel(x, y, OldPixelSize);
                             Color TempColor2 = OldData.GetPixel(TempX1, TempY1, OldPixelSize);
                             Color TempColor3 = OldData.GetPixel(TempX2, TempY2, OldPixelSize);
                             if (Distance(TempColor.R, TempColor2.R, TempColor.G, TempColor2.G, TempColor.B, TempColor2.B) <
                                 Distance(TempColor.R, TempColor3.R, TempColor.G, TempColor3.G, TempColor.B, TempColor3.B))
                             {
                                 RValue += TempColor2.R;
                                 GValue += TempColor2.G;
                                 BValue += TempColor2.B;
                             }
                             else
                             {
                                 RValue += TempColor3.R;
                                 GValue += TempColor3.G;
                                 BValue += TempColor3.B;
                             }
                             ++NumPixels;
                         }
                     }
                 }
             }
             Color MeanPixel = Color.FromArgb(RValue / NumPixels,
                 GValue / NumPixels,
                 BValue / NumPixels);
             NewData.SetPixel(x, y, MeanPixel, NewPixelSize);
         }
     });
     NewBitmap.UnlockImage(NewData);
     OriginalImage.UnlockImage(OldData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
        public static Bitmap KuwaharaBlur(this Bitmap OriginalImage, int Size = 3, string FileName = "")
        {
            OriginalImage.ThrowIfNull("OriginalImage");
            ImageFormat FormatUsing = FileName.GetImageFormat();
            Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
            BitmapData NewData = NewBitmap.LockImage();
            BitmapData OldData = OriginalImage.LockImage();
            int NewPixelSize = NewData.GetPixelSize();
            int OldPixelSize = OldData.GetPixelSize();
            int[] ApetureMinX = { -(Size / 2), 0, -(Size / 2), 0 };
            int[] ApetureMaxX = { 0, (Size / 2), 0, (Size / 2) };
            int[] ApetureMinY = { -(Size / 2), -(Size / 2), 0, 0 };
            int[] ApetureMaxY = { 0, 0, (Size / 2), (Size / 2) };
            int Width = NewBitmap.Width;
            int Height = NewBitmap.Height;
            Parallel.For(0, Width, x =>
            {
                for (int y = 0; y < Height; ++y)
                {
                    int[] RValues = { 0, 0, 0, 0 };
                    int[] GValues = { 0, 0, 0, 0 };
                    int[] BValues = { 0, 0, 0, 0 };
                    int[] NumPixels = { 0, 0, 0, 0 };
                    int[] MaxRValue = { 0, 0, 0, 0 };
                    int[] MaxGValue = { 0, 0, 0, 0 };
                    int[] MaxBValue = { 0, 0, 0, 0 };
                    int[] MinRValue = { 255, 255, 255, 255 };
                    int[] MinGValue = { 255, 255, 255, 255 };
                    int[] MinBValue = { 255, 255, 255, 255 };
                    for (int i = 0; i < 4; ++i)
                    {
                        for (int x2 = ApetureMinX[i]; x2 < ApetureMaxX[i]; ++x2)
                        {
                            int TempX = x + x2;
                            if (TempX >= 0 && TempX < Width)
                            {
                                for (int y2 = ApetureMinY[i]; y2 < ApetureMaxY[i]; ++y2)
                                {
                                    int TempY = y + y2;
                                    if (TempY >= 0 && TempY < Height)
                                    {
                                        Color TempColor = OldData.GetPixel(TempX, TempY, OldPixelSize);
                                        RValues[i] += TempColor.R;
                                        GValues[i] += TempColor.G;
                                        BValues[i] += TempColor.B;
                                        if (TempColor.R > MaxRValue[i])
                                            MaxRValue[i] = TempColor.R;
                                        else if (TempColor.R < MinRValue[i])
                                            MinRValue[i] = TempColor.R;

                                        if (TempColor.G > MaxGValue[i])
                                            MaxGValue[i] = TempColor.G;
                                        else if (TempColor.G < MinGValue[i])
                                            MinGValue[i] = TempColor.G;

                                        if (TempColor.B > MaxBValue[i])
                                            MaxBValue[i] = TempColor.B;
                                        else if (TempColor.B < MinBValue[i])
                                            MinBValue[i] = TempColor.B;

                                        ++NumPixels[i];
                                    }
                                }
                            }
                        }
                    }
                    int j = 0;
                    int MinDifference = 10000;
                    for (int i = 0; i < 4; ++i)
                    {
                        int CurrentDifference = (MaxRValue[i] - MinRValue[i]) + (MaxGValue[i] - MinGValue[i]) + (MaxBValue[i] - MinBValue[i]);
                        if (CurrentDifference < MinDifference && NumPixels[i] > 0)
                        {
                            j = i;
                            MinDifference = CurrentDifference;
                        }
                    }

                    Color MeanPixel = Color.FromArgb(RValues[j] / NumPixels[j],
                        GValues[j] / NumPixels[j],
                        BValues[j] / NumPixels[j]);
                    NewData.SetPixel(x, y, MeanPixel, NewPixelSize);
                }
            });
            NewBitmap.UnlockImage(NewData);
            OriginalImage.UnlockImage(OldData);
            if (!string.IsNullOrEmpty(FileName))
                NewBitmap.Save(FileName, FormatUsing);
            return NewBitmap;
        }
        public static Bitmap AdjustGamma(this Bitmap OriginalImage, float Value = 1.0f, string FileName = "")
        {
            OriginalImage.ThrowIfNull("OriginalImage");
            ImageFormat FormatUsing = FileName.GetImageFormat();
            Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
            BitmapData NewData = NewBitmap.LockImage();
            BitmapData OldData = OriginalImage.LockImage();
            int NewPixelSize = NewData.GetPixelSize();
            int OldPixelSize = OldData.GetPixelSize();

            int[] Ramp = new int[256];
            Parallel.For(0, 256, x =>
            {
                Ramp[x] = ((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5)).Clamp(255, 0);
            });
            int Width = NewBitmap.Width;
            int Height = NewBitmap.Height;

            Parallel.For(0, Width, x =>
            {
                for (int y = 0; y < Height; ++y)
                {
                    Color Pixel = OldData.GetPixel(x, y, OldPixelSize);
                    int Red = Ramp[Pixel.R];
                    int Green = Ramp[Pixel.G];
                    int Blue = Ramp[Pixel.B];
                    NewData.SetPixel(x, y, Color.FromArgb(Red, Green, Blue), NewPixelSize);
                }
            });

            NewBitmap.UnlockImage(NewData);
            OriginalImage.UnlockImage(OldData);
            if (!string.IsNullOrEmpty(FileName))
                NewBitmap.Save(FileName, FormatUsing);
            return NewBitmap;
        }
 /// <summary>
 /// Creates the bump map
 /// </summary>
 public virtual Bitmap Create(Bitmap ImageUsing)
 {
     ImageUsing.ThrowIfNull("ImageUsing");
     CreateFilter();
     using (Bitmap TempImageX = FilterX.Create(ImageUsing))
     {
         using (Bitmap TempImageY = FilterY.Create(ImageUsing))
         {
             Bitmap ReturnImage = new Bitmap(TempImageX.Width, TempImageX.Height);
             BitmapData TempImageXData = TempImageX.LockImage();
             BitmapData TempImageYData = TempImageY.LockImage();
             BitmapData ReturnImageData = ReturnImage.LockImage();
             int TempImageXPixelSize = TempImageXData.GetPixelSize();
             int TempImageYPixelSize = TempImageYData.GetPixelSize();
             int ReturnImagePixelSize = ReturnImageData.GetPixelSize();
             int Width = TempImageX.Width;
             int Height = TempImageX.Height;
             Parallel.For(0, Height, y =>
             {
                 Math.Vector3 TempVector = new Utilities.Math.Vector3(0.0, 0.0, 0.0);
                 for (int x = 0; x < Width; ++x)
                 {
                     Color TempPixelX = TempImageXData.GetPixel(x, y, TempImageXPixelSize);
                     Color TempPixelY = TempImageYData.GetPixel(x, y, TempImageYPixelSize);
                     TempVector.X = (double)(TempPixelX.R) / 255.0;
                     TempVector.Y = (double)(TempPixelY.R) / 255.0;
                     TempVector.Z = 1.0;
                     TempVector.Normalize();
                     TempVector.X = ((TempVector.X + 1.0) / 2.0) * 255.0;
                     TempVector.Y = ((TempVector.Y + 1.0) / 2.0) * 255.0;
                     TempVector.Z = ((TempVector.Z + 1.0) / 2.0) * 255.0;
                     ReturnImageData.SetPixel(x, y,
                         Color.FromArgb((int)TempVector.X,
                             (int)TempVector.Y,
                             (int)TempVector.Z),
                         ReturnImagePixelSize);
                 }
             });
             TempImageX.UnlockImage(TempImageXData);
             TempImageY.UnlockImage(TempImageYData);
             ReturnImage.UnlockImage(ReturnImageData);
             return ReturnImage;
         }
     }
 }
 public static Bitmap And(this Bitmap Image1, Bitmap Image2, string FileName = "")
 {
     Image1.ThrowIfNull("Image1");
     Image2.ThrowIfNull("Image2");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(Image1.Width, Image1.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData1 = Image1.LockImage();
     BitmapData OldData2 = Image2.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize1 = OldData1.GetPixelSize();
     int OldPixelSize2 = OldData2.GetPixelSize();
     int Width = NewBitmap.Width;
     int Height = NewBitmap.Height;
     Parallel.For(0, Width, x =>
     {
         for (int y = 0; y < Height; ++y)
         {
             Color Pixel1 = OldData1.GetPixel(x, y, OldPixelSize1);
             Color Pixel2 = OldData2.GetPixel(x, y, OldPixelSize2);
             NewData.SetPixel(x, y,
                 Color.FromArgb(Pixel1.R & Pixel2.R,
                     Pixel1.G & Pixel2.G,
                     Pixel1.B & Pixel2.B),
                 NewPixelSize);
         }
     });
     NewBitmap.UnlockImage(NewData);
     Image1.UnlockImage(OldData1);
     Image2.UnlockImage(OldData2);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 /// <summary>
 /// Applies the filter to the input image
 /// </summary>
 /// <param name="Input">input image</param>
 /// <returns>Returns a separate image with the filter applied</returns>
 public virtual Bitmap ApplyFilter(Bitmap Input)
 {
     Input.ThrowIfNull("Input");
     Bitmap NewBitmap = new Bitmap(Input.Width, Input.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = Input.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     int Width2 = Input.Width;
     int Height2 = Input.Height;
     Parallel.For(0, Width2, x =>
     {
         for (int y = 0; y < Height2; ++y)
         {
             int RValue = 0;
             int GValue = 0;
             int BValue = 0;
             int Weight = 0;
             int XCurrent = -Width / 2;
             for (int x2 = 0; x2 < Width; ++x2)
             {
                 if (XCurrent + x < Width2 && XCurrent + x >= 0)
                 {
                     int YCurrent = -Height / 2;
                     for (int y2 = 0; y2 < Height; ++y2)
                     {
                         if (YCurrent + y < Height2 && YCurrent + y >= 0)
                         {
                             Color Pixel = OldData.GetPixel(XCurrent + x, YCurrent + y, OldPixelSize);
                             RValue += MyFilter[x2, y2] * Pixel.R;
                             GValue += MyFilter[x2, y2] * Pixel.G;
                             BValue += MyFilter[x2, y2] * Pixel.B;
                             Weight += MyFilter[x2, y2];
                         }
                         ++YCurrent;
                     }
                 }
                 ++XCurrent;
             }
             Color MeanPixel = OldData.GetPixel(x, y, OldPixelSize);
             if (Weight == 0)
                 Weight = 1;
             if (Weight > 0)
             {
                 if (Absolute)
                 {
                     RValue = System.Math.Abs(RValue);
                     GValue = System.Math.Abs(GValue);
                     BValue = System.Math.Abs(BValue);
                 }
                 RValue = (RValue / Weight) + Offset;
                 RValue = RValue.Clamp(255, 0);
                 GValue = (GValue / Weight) + Offset;
                 GValue = GValue.Clamp(255, 0);
                 BValue = (BValue / Weight) + Offset;
                 BValue = BValue.Clamp(255, 0);
                 MeanPixel = Color.FromArgb(RValue, GValue, BValue);
             }
             NewData.SetPixel(x, y, MeanPixel, NewPixelSize);
         }
     });
     NewBitmap.UnlockImage(NewData);
     Input.UnlockImage(OldData);
     return NewBitmap;
 }
 public static Bitmap OilPainting(this Bitmap Image, int Seed, int NumberOfPoints=100)
 {
     Image.ThrowIfNull("Image");
     Bitmap _Image = new Bitmap(Image);
     CellularMap Map = new CellularMap(Seed, Image.Width, Image.Height, NumberOfPoints);
     BitmapData ImageData = _Image.LockImage();
     int ImagePixelSize = ImageData.GetPixelSize();
     int Width = _Image.Width;
     int Height = _Image.Height;
     Parallel.For(0, NumberOfPoints, i =>
     {
         int Red = 0;
         int Green = 0;
         int Blue = 0;
         int Counter = 0;
         for (int x = 0; x < Width; ++x)
         {
             for (int y = 0; y < Height; ++y)
             {
                 if (Map.ClosestPoint[x,y] == i)
                 {
                     Color Pixel = ImageData.GetPixel(x, y, ImagePixelSize);
                     Red += Pixel.R;
                     Green += Pixel.G;
                     Blue += Pixel.B;
                     ++Counter;
                 }
             }
         }
         int Counter2 = 0;
         for (int x = 0; x < Width; ++x)
         {
             for (int y = 0; y < Height; ++y)
             {
                 if (Map.ClosestPoint[x,y] == i)
                 {
                     ImageData.SetPixel(x, y, Color.FromArgb(Red / Counter, Green / Counter, Blue / Counter), ImagePixelSize);
                     ++Counter2;
                     if (Counter2 == Counter)
                         break;
                 }
             }
             if (Counter2 == Counter)
                 break;
         }
     });
     _Image.UnlockImage(ImageData);
     return _Image;
 }
 /// <summary>
 /// Creates the bump map
 /// </summary>
 public virtual Bitmap Create(Bitmap ImageUsing)
 {
     if (ImageUsing == null)
         throw new ArgumentNullException("ImageUsing");
     CreateFilter();
     using (Bitmap TempImageX = FilterX.Create(ImageUsing))
     {
         using (Bitmap TempImageY = FilterY.Create(ImageUsing))
         {
             Bitmap ReturnImage = new Bitmap(TempImageX.Width, TempImageX.Height);
             Math.Vector3 TempVector = new Utilities.Math.Vector3(0.0, 0.0, 0.0);
             BitmapData TempImageXData = TempImageX.LockImage();
             BitmapData TempImageYData = TempImageY.LockImage();
             BitmapData ReturnImageData = ReturnImage.LockImage();
             int TempImageXPixelSize = TempImageXData.GetPixelSize();
             int TempImageYPixelSize = TempImageYData.GetPixelSize();
             int ReturnImagePixelSize = ReturnImageData.GetPixelSize();
             for (int y = 0; y < TempImageX.Height; ++y)
             {
                 for (int x = 0; x < TempImageX.Width; ++x)
                 {
                     Color TempPixelX = TempImageXData.GetPixel(x, y, TempImageXPixelSize);
                     Color TempPixelY = TempImageYData.GetPixel(x, y, TempImageYPixelSize);
                     TempVector.X = (double)(TempPixelX.R) / 255.0;
                     TempVector.Y = (double)(TempPixelY.R) / 255.0;
                     TempVector.Z = 1.0;
                     TempVector.Normalize();
                     TempVector.X = ((TempVector.X + 1.0) / 2.0) * 255.0;
                     TempVector.Y = ((TempVector.Y + 1.0) / 2.0) * 255.0;
                     TempVector.Z = ((TempVector.Z + 1.0) / 2.0) * 255.0;
                     ReturnImageData.SetPixel(x, y,
                         Color.FromArgb((int)TempVector.X,
                             (int)TempVector.Y,
                             (int)TempVector.Z),
                         ReturnImagePixelSize);
                 }
             }
             TempImageX.UnlockImage(TempImageXData);
             TempImageY.UnlockImage(TempImageYData);
             ReturnImage.UnlockImage(ReturnImageData);
             return ReturnImage;
         }
     }
 }
 public static Bitmap Dilate(this Bitmap OriginalImage, int Size, string FileName = "")
 {
     OriginalImage.ThrowIfNull("OriginalImage");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = OriginalImage.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     int ApetureMin = -(Size / 2);
     int ApetureMax = (Size / 2);
     int Width = NewBitmap.Width;
     int Height = NewBitmap.Height;
     Parallel.For(0, Width, x =>
     {
         for (int y = 0; y < Height; ++y)
         {
             int RValue = 0;
             int GValue = 0;
             int BValue = 0;
             for (int x2 = ApetureMin; x2 < ApetureMax; ++x2)
             {
                 int TempX = x + x2;
                 if (TempX >= 0 && TempX < Width)
                 {
                     for (int y2 = ApetureMin; y2 < ApetureMax; ++y2)
                     {
                         int TempY = y + y2;
                         if (TempY >= 0 && TempY < Height)
                         {
                             Color TempColor = OldData.GetPixel(TempX, TempY, OldPixelSize);
                             RValue = RValue.Max(TempColor.R);
                             GValue = GValue.Max(TempColor.G);
                             BValue = BValue.Max(TempColor.B);
                         }
                     }
                 }
             }
             Color TempPixel = Color.FromArgb(RValue, GValue, BValue);
             NewData.SetPixel(x, y, TempPixel, NewPixelSize);
         }
     });
     NewBitmap.UnlockImage(NewData);
     OriginalImage.UnlockImage(OldData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
Example #27
0
 public static Bitmap AddNoise2(Bitmap OriginalImage, int Amount)
 {
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     BitmapData NewData = NewBitmap.LockImage();//Image.LockImage(NewBitmap);
     BitmapData OldData = OriginalImage.LockImage();//Image.LockImage(OriginalImage);
     int NewPixelSize = NewData.GetPixelSize();//Image.GetPixelSize(NewData);
     int OldPixelSize = OldData.GetPixelSize();//Image.GetPixelSize(OldData);
     Utilities.Random.Random TempRandom = new Utilities.Random.Random();
     for (int x = 0; x < NewBitmap.Width; ++x)
     {
         for (int y = 0; y < NewBitmap.Height; ++y)
         {
             Color CurrentPixel = OldData.GetPixel(x, y, OldPixelSize);//Image.GetPixel(OldData, x, y, OldPixelSize);
             int noise = (int)AWGN() * 255;
             int R = CurrentPixel.R + noise;//(int)Statistic.GaussianRandom(0, 1);//(int)Statistic.NextGaussian(0f, 1f, -Amount, Amount + 1);//TempRandom.Next(-Amount, Amount + 1);
             int G = CurrentPixel.G + noise;//(int)Statistic.GaussianRandom(0, 1);//(int)Statistic.NextGaussian(0f, 1f, -Amount, Amount + 1);//TempRandom.Next(-Amount, Amount + 1);
             int B = CurrentPixel.B + noise;//(int)Statistic.GaussianRandom(0, 1);//(int)Statistic.NextGaussian(0f, 1f, -Amount, Amount + 1);//TempRandom.Next(-Amount, Amount + 1);
             R = R > 255 ? 255 : R;
             R = R < 0 ? 0 : R;
             G = G > 255 ? 255 : G;
             G = G < 0 ? 0 : G;
             B = B > 255 ? 255 : B;
             B = B < 0 ? 0 : B;
             Color TempValue = Color.FromArgb(R, G, B);
             NewData.SetPixel(x, y, TempValue, NewPixelSize);//Image.SetPixel(NewData, x, y, TempValue, NewPixelSize);
         }
     }
     NewBitmap.UnlockImage(NewData);//Image.UnlockImage(NewBitmap, NewData);
     OriginalImage.UnlockImage(OldData);//Image.UnlockImage(OriginalImage, OldData);
     return NewBitmap;
 }
 public static Bitmap EdgeDetection(this Bitmap OriginalImage, float Threshold, Color EdgeColor, string FileName = "")
 {
     OriginalImage.ThrowIfNull("OriginalImage");
     EdgeColor.ThrowIfNull("EdgeColor");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(OriginalImage, OriginalImage.Width, OriginalImage.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = OriginalImage.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     int Width = NewBitmap.Width;
     int Height = NewBitmap.Height;
     Parallel.For(0, Width, x =>
     {
         for (int y = 0; y < Height; ++y)
         {
             Color CurrentColor = OldData.GetPixel(x, y, OldPixelSize);
             if (y < Height - 1 && x < Width - 1)
             {
                 Color TempColor = OldData.GetPixel(x + 1, y + 1, OldPixelSize);
                 if (Distance(CurrentColor.R, TempColor.R, CurrentColor.G, TempColor.G, CurrentColor.B, TempColor.B) > Threshold)
                     NewData.SetPixel(x, y, EdgeColor, NewPixelSize);
             }
             else if (y < Height - 1)
             {
                 Color TempColor = OldData.GetPixel(x, y + 1, OldPixelSize);
                 if (Distance(CurrentColor.R, TempColor.R, CurrentColor.G, TempColor.G, CurrentColor.B, TempColor.B) > Threshold)
                     NewData.SetPixel(x, y, EdgeColor, NewPixelSize);
             }
             else if (x < Width - 1)
             {
                 Color TempColor = OldData.GetPixel(x + 1, y, OldPixelSize);
                 if (Distance(CurrentColor.R, TempColor.R, CurrentColor.G, TempColor.G, CurrentColor.B, TempColor.B) > Threshold)
                     NewData.SetPixel(x, y, EdgeColor, NewPixelSize);
             }
         }
     });
     NewBitmap.UnlockImage(NewData);
     OriginalImage.UnlockImage(OldData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 public static Bitmap Colorize(this Bitmap OriginalImage, Color[] Colors, string FileName = "")
 {
     OriginalImage.ThrowIfNull("OriginalImage");
     if (Colors.Length < 256)
         return new Bitmap(1, 1);
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = OriginalImage.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     int Width = OriginalImage.Width;
     int Height = OriginalImage.Height;
     Parallel.For(0, Width, x =>
     {
         for (int y = 0; y < Height; ++y)
         {
             int ColorUsing = OldData.GetPixel(x, y, OldPixelSize).R;
             NewData.SetPixel(x, y, Colors[ColorUsing], NewPixelSize);
         }
     });
     NewBitmap.UnlockImage(NewData);
     OriginalImage.UnlockImage(OldData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 public static Bitmap Pixelate(this Bitmap OriginalImage, int PixelSize = 5, string FileName = "")
 {
     OriginalImage.ThrowIfNull("OriginalImage");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = OriginalImage.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     for (int x = 0; x < NewBitmap.Width; x += (PixelSize / 2))
     {
         int MinX = (x - (PixelSize / 2)).Clamp(NewBitmap.Width, 0);
         int MaxX = (x + (PixelSize / 2)).Clamp(NewBitmap.Width, 0);
         for (int y = 0; y < NewBitmap.Height; y += (PixelSize / 2))
         {
             int RValue = 0;
             int GValue = 0;
             int BValue = 0;
             int MinY = (y - (PixelSize / 2)).Clamp(NewBitmap.Height, 0);
             int MaxY = (y + (PixelSize / 2)).Clamp(NewBitmap.Height, 0);
             for (int x2 = MinX; x2 < MaxX; ++x2)
             {
                 for (int y2 = MinY; y2 < MaxY; ++y2)
                 {
                     Color Pixel = OldData.GetPixel(x2, y2, OldPixelSize);
                     RValue += Pixel.R;
                     GValue += Pixel.G;
                     BValue += Pixel.B;
                 }
             }
             RValue = RValue / (PixelSize * PixelSize);
             GValue = GValue / (PixelSize * PixelSize);
             BValue = BValue / (PixelSize * PixelSize);
             Color TempPixel = Color.FromArgb(RValue, GValue, BValue);
             Parallel.For(MinX, MaxX, x2 =>
             {
                 for (int y2 = MinY; y2 < MaxY; ++y2)
                 {
                     NewData.SetPixel(x2, y2, TempPixel, NewPixelSize);
                 }
             });
         }
     }
     NewBitmap.UnlockImage(NewData);
     OriginalImage.UnlockImage(OldData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }