/* * Based on Craig's Utility Library (CUL) by James Craig. * http://www.gutgames.com/post/Edge-detection-in-C.aspx * MIT License (http://www.opensource.org/licenses/mit-license.php) */ /// <summary> /// Gets a new bitmap containing edges detected in source bitmap. /// The source bitmap is unchanged. /// </summary> /// <param name="bitmap">DFBitmap source.</param> /// <param name="threshold">Edge detection threshold.</param> /// <param name="edgeColor">Edge colour to write.</param> /// <returns>DFBitmap containing edges.</returns> private static DFBitmap FindEdges(DFBitmap bitmap, float threshold, DFBitmap.DFColor edgeColor) { // Must be a colour format if (bitmap.Format == DFBitmap.Formats.Indexed) { return(null); } // Clone bitmap settings DFBitmap newBitmap = DFBitmap.CloneDFBitmap(bitmap); for (int x = 0; x < bitmap.Width; x++) { for (int y = 0; y < bitmap.Height; y++) { DFBitmap.DFColor currentColor = DFBitmap.GetPixel(bitmap, x, y); if (y < newBitmap.Height - 1 && x < newBitmap.Width - 1) { DFBitmap.DFColor tempColor = DFBitmap.GetPixel(bitmap, x + 1, y + 1); if (ColorDistance(currentColor, tempColor) > threshold) { DFBitmap.SetPixel(newBitmap, x, y, edgeColor); } } else if (y < newBitmap.Height - 1) { DFBitmap.DFColor tempColor = DFBitmap.GetPixel(bitmap, x, y + 1); if (ColorDistance(currentColor, tempColor) > threshold) { DFBitmap.SetPixel(newBitmap, x, y, edgeColor); } } else if (x < newBitmap.Width - 1) { DFBitmap.DFColor tempColor = DFBitmap.GetPixel(bitmap, x + 1, y); if (ColorDistance(currentColor, tempColor) > threshold) { DFBitmap.SetPixel(newBitmap, x, y, edgeColor); } } } } 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 DFBitmap ApplyFilter(DFBitmap Input) { // Must be a colour format if (Input.Format == DFBitmap.Formats.Indexed) { return(null); } DFBitmap NewBitmap = DFBitmap.CloneDFBitmap(Input); for (int x = 0; x < Input.Width; ++x) { for (int y = 0; y < Input.Height; ++y) { int RValue = 0; int GValue = 0; int BValue = 0; int AValue = 0; int Weight = 0; int XCurrent = -Width / 2; for (int x2 = 0; x2 < Width; ++x2) { if (XCurrent + x < Input.Width && XCurrent + x >= 0) { int YCurrent = -Height / 2; for (int y2 = 0; y2 < Height; ++y2) { if (YCurrent + y < Input.Height && YCurrent + y >= 0) { DFBitmap.DFColor Pixel = DFBitmap.GetPixel(Input, XCurrent + x, YCurrent + y); RValue += MyFilter[x2, y2] * Pixel.r; GValue += MyFilter[x2, y2] * Pixel.g; BValue += MyFilter[x2, y2] * Pixel.b; AValue = Pixel.a; Weight += MyFilter[x2, y2]; } ++YCurrent; } } ++XCurrent; } DFBitmap.DFColor MeanPixel = DFBitmap.GetPixel(Input, x, y); 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 = Clamp(RValue, 0, 255); GValue = (GValue / Weight) + Offset; GValue = Clamp(GValue, 0, 255); BValue = (BValue / Weight) + Offset; BValue = Clamp(BValue, 0, 255); MeanPixel = DFBitmap.DFColor.FromRGBA((byte)RValue, (byte)GValue, (byte)BValue, (byte)AValue); } DFBitmap.SetPixel(NewBitmap, x, y, MeanPixel); } } return(NewBitmap); }