/// <summary> /// Uses an RGB histogram to equalize the image /// </summary> /// <param name="OriginalImage">Image to manipulate</param> /// <returns>The resulting SwiftBitmap image</returns> public static SwiftBitmap Equalize(this SwiftBitmap OriginalImage) { Contract.Requires<ArgumentNullException>(OriginalImage != null, "OriginalImage"); using (SwiftBitmap NewSwiftBitmap = new SwiftBitmap(OriginalImage.Width, OriginalImage.Height)) { var TempHistogram = new RGBHistogram(OriginalImage); TempHistogram.Equalize(); NewSwiftBitmap.Lock(); OriginalImage.Lock(); Parallel.For(0, OriginalImage.Width, x => { for (int y = 0; y < OriginalImage.Height; ++y) { Color Current = OriginalImage.GetPixel(x, y); var NewR = (int)TempHistogram.R[Current.R]; var NewG = (int)TempHistogram.G[Current.G]; var NewB = (int)TempHistogram.B[Current.B]; NewR = NewR.Clamp(255, 0); NewG = NewG.Clamp(255, 0); NewB = NewB.Clamp(255, 0); NewSwiftBitmap.SetPixel(x, y, Color.FromArgb(NewR, NewG, NewB)); } }); NewSwiftBitmap.Unlock(); return OriginalImage.Unlock().Copy(NewSwiftBitmap); } }
/// <summary> /// Uses an RGB histogram to equalize the image /// </summary> /// <param name="Image">Image to manipulate</param> public static Bitmap Equalize(Bitmap Image) { System.Drawing.Bitmap TempBitmap = Image; System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height); System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap); NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel); NewGraphics.Dispose(); RGBHistogram TempHistogram = new RGBHistogram(NewBitmap); TempHistogram.Equalize(); for (int x = 0; x < NewBitmap.Width; ++x) { for (int y = 0; y < NewBitmap.Height; ++y) { Color Current=NewBitmap.GetPixel(x,y); int NewR = (int)TempHistogram.R[Current.R]; int NewG = (int)TempHistogram.G[Current.G]; int NewB = (int)TempHistogram.B[Current.B]; if (NewR >= 256) NewR = 255; if (NewG >= 256) NewG = 255; if (NewB >= 256) NewB = 255; NewBitmap.SetPixel(x, y, Color.FromArgb(NewR, NewG, NewB)); } } return NewBitmap; }