// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Applies a Gaussian sharpen to the image. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Processors { using System; using System.Collections.Generic; using System.Drawing; using System.Text.RegularExpressions; using ImageProcessor.Imaging; /// /// Applies a Gaussian sharpen to the image. /// public class GaussianSharpen : IGraphicsProcessor { #region Fields /// /// The regular expression to search strings for. /// private static readonly Regex QueryRegex = new Regex(@"sharpen=[^&]*", RegexOptions.Compiled); /// /// The sharpen regex. /// private static readonly Regex SharpenRegex = new Regex(@"sharpen=\d+", RegexOptions.Compiled); /// /// The sigma regex. /// private static readonly Regex SigmaRegex = new Regex(@"sigma-\d+(.?\d+)?", RegexOptions.Compiled); /// /// The threshold regex. /// private static readonly Regex ThresholdRegex = new Regex(@"threshold-\d+", RegexOptions.Compiled); #endregion #region IGraphicsProcessor Members /// /// Gets the regular expression to search strings for. /// public Regex RegexPattern { get { return QueryRegex; } } /// /// Gets or sets the DynamicParameter. /// public dynamic DynamicParameter { get; set; } /// /// Gets the order in which this processor is to be used in a chain. /// public int SortOrder { get; private set; } /// /// Gets or sets any additional settings required by the processor. /// public Dictionary Settings { get; set; } /// /// The position in the original string where the first character of the captured substring was found. /// /// The query string to search. /// /// The zero-based starting position in the original string where the captured substring was found. /// public int MatchRegexIndex(string queryString) { int index = 0; // Set the sort order to max to allow filtering. this.SortOrder = int.MaxValue; foreach (Match match in this.RegexPattern.Matches(queryString)) { if (match.Success) { if (index == 0) { // Set the index on the first instance only. this.SortOrder = match.Index; // Normalise and set the variables. int maxSize; double maxSigma; int maxThreshold; int.TryParse(this.Settings["MaxSize"], out maxSize); double.TryParse(this.Settings["MaxSigma"], out maxSigma); int.TryParse(this.Settings["MaxThreshold"], out maxThreshold); int size = this.ParseSharpen(match.Value); double sigma = this.ParseSigma(match.Value); int threshold = this.ParseThreshold(match.Value); size = maxSize < size ? maxSize : size; sigma = maxSigma < sigma ? maxSigma : sigma; threshold = maxThreshold < threshold ? maxThreshold : threshold; this.DynamicParameter = new GaussianLayer(size, sigma, threshold); } index += 1; } } return this.SortOrder; } /// /// Processes the image. /// /// The the current instance of the class containing /// the image to process. /// /// The processed image from the current instance of the class. /// public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Bitmap image = (Bitmap)factory.Image; try { newImage = new Bitmap(image); GaussianLayer gaussianLayer = (GaussianLayer)this.DynamicParameter; Convolution convolution = new Convolution(gaussianLayer.Sigma) { Threshold = gaussianLayer.Threshold }; double[,] kernel = convolution.CreateGuassianSharpenFilter(gaussianLayer.Size); newImage = convolution.ProcessKernel(newImage, kernel); newImage.Tag = image.Tag; image.Dispose(); image = newImage; } catch { if (newImage != null) { newImage.Dispose(); } } return image; } #endregion #region Private /// /// Returns the correct containing the sigma value /// for the given string. /// /// /// The input string containing the value to parse. /// /// /// The correct for the given string. /// private double ParseSigma(string input) { foreach (Match match in SigmaRegex.Matches(input)) { // split on text- return Convert.ToDouble(match.Value.Split('-')[1]); } return 1.4d; } /// /// Returns the correct containing the threshold value /// for the given string. /// /// /// The input string containing the value to parse. /// /// /// The correct for the given string. /// private int ParseThreshold(string input) { // ReSharper disable once LoopCanBeConvertedToQuery foreach (Match match in ThresholdRegex.Matches(input)) { return Convert.ToInt32(match.Value.Split('-')[1]); } return 0; } /// /// Returns the correct containing the sharpen value /// for the given string. /// /// /// The input string containing the value to parse. /// /// /// The correct for the given string. /// private int ParseSharpen(string input) { // ReSharper disable once LoopCanBeConvertedToQuery foreach (Match match in SharpenRegex.Matches(input)) { return Convert.ToInt32(match.Value.Split('=')[1]); } return 0; } #endregion } }