// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp { using System; using Processing; using Processing.Processors; /// /// Extension methods for the type. /// public static partial class ImageExtensions { /// /// Detects any edges within the image. Uses the filter /// operating in Grayscale mode. /// /// The pixel format. /// The image this method extends. /// The . public static Image DetectEdges(this Image source) where TColor : struct, IPixel { return DetectEdges(source, source.Bounds, new SobelProcessor { Grayscale = true }); } /// /// Detects any edges within the image. Uses the filter /// operating in Grayscale mode. /// /// The pixel format. /// The image this method extends. /// /// The structure that specifies the portion of the image object to alter. /// /// The . public static Image DetectEdges(this Image source, Rectangle rectangle) where TColor : struct, IPixel { return DetectEdges(source, rectangle, new SobelProcessor { Grayscale = true }); } /// /// Detects any edges within the image. /// /// The pixel format. /// The image this method extends. /// The filter for detecting edges. /// Whether to convert the image to Grayscale first. Defaults to true. /// The . public static Image DetectEdges(this Image source, EdgeDetection filter, bool grayscale = true) where TColor : struct, IPixel { return DetectEdges(source, filter, source.Bounds, grayscale); } /// /// Detects any edges within the image. /// /// The pixel format. /// The image this method extends. /// The filter for detecting edges. /// /// The structure that specifies the portion of the image object to alter. /// /// Whether to convert the image to Grayscale first. Defaults to true. /// The . public static Image DetectEdges(this Image source, EdgeDetection filter, Rectangle rectangle, bool grayscale = true) where TColor : struct, IPixel { IEdgeDetectorProcessor processor; switch (filter) { case EdgeDetection.Kayyali: processor = new KayyaliProcessor { Grayscale = grayscale }; break; case EdgeDetection.Kirsch: processor = new KirschProcessor { Grayscale = grayscale }; break; case EdgeDetection.Lapacian3X3: processor = new Laplacian3X3Processor { Grayscale = grayscale }; break; case EdgeDetection.Lapacian5X5: processor = new Laplacian5X5Processor { Grayscale = grayscale }; break; case EdgeDetection.LaplacianOfGaussian: processor = new LaplacianOfGaussianProcessor { Grayscale = grayscale }; break; case EdgeDetection.Prewitt: processor = new PrewittProcessor { Grayscale = grayscale }; break; case EdgeDetection.RobertsCross: processor = new RobertsCrossProcessor { Grayscale = grayscale }; break; case EdgeDetection.Robinson: processor = new RobinsonProcessor { Grayscale = grayscale }; break; case EdgeDetection.Scharr: processor = new ScharrProcessor { Grayscale = grayscale }; break; default: processor = new SobelProcessor { Grayscale = grayscale }; break; } return DetectEdges(source, rectangle, processor); } /// /// Detects any edges within the image. /// /// The pixel format. /// The image this method extends. /// The filter for detecting edges. /// The . public static Image DetectEdges(this Image source, IEdgeDetectorProcessor filter) where TColor : struct, IPixel { return DetectEdges(source, source.Bounds, filter); } /// /// Detects any edges within the image. /// /// The pixel format. /// The image this method extends. /// /// The structure that specifies the portion of the image object to alter. /// /// The filter for detecting edges. /// The . public static Image DetectEdges(this Image source, Rectangle rectangle, IEdgeDetectorProcessor filter) where TColor : struct, IPixel { source.ApplyProcessor(filter, rectangle); return source; } } }