diff --git a/src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs b/src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs index d4039acdd..115fa0451 100644 --- a/src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs +++ b/src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs @@ -10,10 +10,12 @@ namespace ImageProcessor.Web.Helpers { + using System; using System.Drawing; using System.Globalization; using System.Text.RegularExpressions; using ImageProcessor.Core.Common.Extensions; + using ImageProcessor.Imaging; /// /// Encapsulates methods to correctly parse querystring parameters. @@ -35,6 +37,27 @@ namespace ImageProcessor.Web.Helpers /// private static readonly Regex In100RangeRegex = new Regex(@"(-?(?:100)|-?([1-9]?[0-9]))", RegexOptions.Compiled); + /// + /// The regular expression to search strings for. + /// + private static readonly Regex GuassianRegex = new Regex(@"(blur|sharpen|sigma|threshold)(=|-)[^&]*", RegexOptions.Compiled); + + /// + /// The sharpen regex. + /// + private static readonly Regex BlurSharpenRegex = new Regex(@"(blur|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); + + /// /// Returns the correct containing the angle for the given string. /// @@ -111,5 +134,99 @@ namespace ImageProcessor.Web.Helpers return value; } + + /// + /// Returns the correct for the given string. + /// + /// + /// The input string containing the value to parse. + /// + /// + /// The maximum size to set the Gaussian kernel to. + /// + /// + /// The maximum Sigma value (standard deviation) for Gaussian function used to calculate the kernel. + /// + /// + /// The maximum threshold value, which is added to each weighted sum of pixels. + /// + /// + /// The correct . + /// + public static GaussianLayer ParseGaussianLayer(string input, int maxSize, double maxSigma, int maxThreshold) + { + int size = ParseBlurSharpen(input); + double sigma = ParseSigma(input); + int threshold = ParseThreshold(input); + + size = maxSize < size ? maxSize : size; + sigma = maxSigma < sigma ? maxSigma : sigma; + threshold = maxThreshold < threshold ? maxThreshold : threshold; + + return new GaussianLayer(size, sigma, threshold); + } + + /// + /// Returns the correct containing the blur value + /// for the given string. + /// + /// + /// The input string containing the value to parse. + /// + /// + /// The correct for the given string. + /// + private static int ParseBlurSharpen(string input) + { + // ReSharper disable once LoopCanBeConvertedToQuery + foreach (Match match in BlurSharpenRegex.Matches(input)) + { + return Convert.ToInt32(match.Value.Split('=')[1]); + } + + return 0; + } + + /// + /// 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 static 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 static int ParseThreshold(string input) + { + // ReSharper disable once LoopCanBeConvertedToQuery + foreach (Match match in ThresholdRegex.Matches(input)) + { + return Convert.ToInt32(match.Value.Split('-')[1]); + } + + return 0; + } } } diff --git a/src/ImageProcessor.Web/NET45/Helpers/ImageHelpers.cs b/src/ImageProcessor.Web/NET45/Helpers/ImageHelpers.cs index 1b6e45a3a..88ed8e4b5 100644 --- a/src/ImageProcessor.Web/NET45/Helpers/ImageHelpers.cs +++ b/src/ImageProcessor.Web/NET45/Helpers/ImageHelpers.cs @@ -10,16 +10,11 @@ namespace ImageProcessor.Web.Helpers { - #region Using - using System.Text; using System.Text.RegularExpressions; - using ImageProcessor.Configuration; using ImageProcessor.Imaging.Formats; - #endregion - /// /// The image helpers. /// @@ -47,8 +42,7 @@ namespace ImageProcessor.Web.Helpers /// True the value contains a valid image extension, otherwise false. public static bool IsValidImageExtension(string fileName) { - Match match = EndFormatRegex.Matches(fileName)[0]; - + Match match = EndFormatRegex.Match(fileName); if (match.Success && !match.Value.ToLowerInvariant().EndsWith("png8")) { return true; @@ -68,7 +62,7 @@ namespace ImageProcessor.Web.Helpers /// public static string GetExtension(string input) { - Match match = FormatRegex.Matches(input)[0]; + Match match = FormatRegex.Match(input); if (match.Success) { diff --git a/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj b/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj index 7f63f275a..1f1601bf2 100644 --- a/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj +++ b/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj @@ -69,6 +69,8 @@ + + diff --git a/src/ImageProcessor.Web/NET45/Processors/GaussianBlur.cs b/src/ImageProcessor.Web/NET45/Processors/GaussianBlur.cs new file mode 100644 index 000000000..88f107ee7 --- /dev/null +++ b/src/ImageProcessor.Web/NET45/Processors/GaussianBlur.cs @@ -0,0 +1,101 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Applies a Gaussian blur to the image. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Web.Processors +{ + using System.Globalization; + using System.Text.RegularExpressions; + using ImageProcessor.Processors; + using ImageProcessor.Web.Helpers; + + /// + /// Applies a Gaussian blur to the image. + /// + public class GaussianBlur : IWebGraphicsProcessor + { + /// + /// The regular expression to search strings for. + /// + private static readonly Regex QueryRegex = new Regex(@"blur=[^&]*", RegexOptions.Compiled); + + /// + /// Initializes a new instance of the class. + /// + public GaussianBlur() + { + this.Processor = new ImageProcessor.Processors.GaussianBlur(); + } + + /// + /// Gets the regular expression to search strings for. + /// + public Regex RegexPattern + { + get + { + return QueryRegex; + } + } + + /// + /// Gets the order in which this processor is to be used in a chain. + /// + public int SortOrder { get; private set; } + + /// + /// Gets the associated graphics processor. + /// + public IGraphicsProcessor Processor { get; private 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.Processor.Settings["MaxSize"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxSize); + double.TryParse(this.Processor.Settings["MaxSigma"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxSigma); + int.TryParse(this.Processor.Settings["MaxThreshold"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxThreshold); + + this.Processor.DynamicParameter = CommonParameterParserUtility.ParseGaussianLayer(queryString, maxSize, maxSigma, maxThreshold); + } + + index += 1; + } + } + + return this.SortOrder; + } + } +} \ No newline at end of file diff --git a/src/ImageProcessor.Web/NET45/Processors/GaussianSharpen.cs b/src/ImageProcessor.Web/NET45/Processors/GaussianSharpen.cs new file mode 100644 index 000000000..60d0235cf --- /dev/null +++ b/src/ImageProcessor.Web/NET45/Processors/GaussianSharpen.cs @@ -0,0 +1,101 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Applies a Gaussian sharpen to the image. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Web.Processors +{ + using System.Globalization; + using System.Text.RegularExpressions; + using ImageProcessor.Processors; + using ImageProcessor.Web.Helpers; + + /// + /// Applies a Gaussian sharpen to the image. + /// + public class GaussianSharpen : IWebGraphicsProcessor + { + /// + /// The regular expression to search strings for. + /// + private static readonly Regex QueryRegex = new Regex(@"sharpen=[^&]*", RegexOptions.Compiled); + + /// + /// Initializes a new instance of the class. + /// + public GaussianSharpen() + { + this.Processor = new ImageProcessor.Processors.GaussianSharpen(); + } + + /// + /// Gets the regular expression to search strings for. + /// + public Regex RegexPattern + { + get + { + return QueryRegex; + } + } + + /// + /// Gets the order in which this processor is to be used in a chain. + /// + public int SortOrder { get; private set; } + + /// + /// Gets the associated graphics processor. + /// + public IGraphicsProcessor Processor { get; private 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.Processor.Settings["MaxSize"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxSize); + double.TryParse(this.Processor.Settings["MaxSigma"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxSigma); + int.TryParse(this.Processor.Settings["MaxThreshold"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxThreshold); + + this.Processor.DynamicParameter = CommonParameterParserUtility.ParseGaussianLayer(queryString, maxSize, maxSigma, maxThreshold); + } + + index += 1; + } + } + + return this.SortOrder; + } + } +} \ No newline at end of file diff --git a/src/ImageProcessor/Processors/GaussianBlur.cs b/src/ImageProcessor/Processors/GaussianBlur.cs index d96ee7f54..3df89045b 100644 --- a/src/ImageProcessor/Processors/GaussianBlur.cs +++ b/src/ImageProcessor/Processors/GaussianBlur.cs @@ -10,11 +10,8 @@ namespace ImageProcessor.Processors { - using System; using System.Collections.Generic; using System.Drawing; - using System.Globalization; - using System.Text.RegularExpressions; using ImageProcessor.Imaging; /// @@ -22,105 +19,16 @@ namespace ImageProcessor.Processors /// public class GaussianBlur : IGraphicsProcessor { - #region Fields - /// - /// The regular expression to search strings for. - /// - private static readonly Regex QueryRegex = new Regex(@"blur=[^&]*", RegexOptions.Compiled); - - /// - /// The blur regex. - /// - private static readonly Regex BlurRegex = new Regex(@"blur=\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"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxSize); - double.TryParse(this.Settings["MaxSigma"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxSigma); - int.TryParse(this.Settings["MaxThreshold"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxThreshold); - - int size = this.ParseBlur(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. /// @@ -137,7 +45,7 @@ namespace ImageProcessor.Processors try { newImage = new Bitmap(image); - GaussianLayer gaussianLayer = (GaussianLayer)this.DynamicParameter; + GaussianLayer gaussianLayer = this.DynamicParameter; Convolution convolution = new Convolution(gaussianLayer.Sigma) { Threshold = gaussianLayer.Threshold }; double[,] kernel = convolution.CreateGuassianBlurFilter(gaussianLayer.Size); @@ -156,71 +64,5 @@ namespace ImageProcessor.Processors 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 blur value - /// for the given string. - /// - /// - /// The input string containing the value to parse. - /// - /// - /// The correct for the given string. - /// - private int ParseBlur(string input) - { - // ReSharper disable once LoopCanBeConvertedToQuery - foreach (Match match in BlurRegex.Matches(input)) - { - return Convert.ToInt32(match.Value.Split('=')[1]); - } - - return 0; - } - #endregion } -} +} \ No newline at end of file diff --git a/src/ImageProcessor/Processors/GaussianSharpen.cs b/src/ImageProcessor/Processors/GaussianSharpen.cs index 8c4672bf6..f27e6f299 100644 --- a/src/ImageProcessor/Processors/GaussianSharpen.cs +++ b/src/ImageProcessor/Processors/GaussianSharpen.cs @@ -10,11 +10,8 @@ namespace ImageProcessor.Processors { - using System; using System.Collections.Generic; using System.Drawing; - using System.Globalization; - using System.Text.RegularExpressions; using ImageProcessor.Imaging; /// @@ -22,105 +19,16 @@ namespace ImageProcessor.Processors /// 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"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxSize); - double.TryParse(this.Settings["MaxSigma"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxSigma); - int.TryParse(this.Settings["MaxThreshold"], NumberStyles.Any, CultureInfo.InvariantCulture, 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. /// @@ -137,7 +45,7 @@ namespace ImageProcessor.Processors try { newImage = new Bitmap(image); - GaussianLayer gaussianLayer = (GaussianLayer)this.DynamicParameter; + GaussianLayer gaussianLayer = this.DynamicParameter; Convolution convolution = new Convolution(gaussianLayer.Sigma) { Threshold = gaussianLayer.Threshold }; double[,] kernel = convolution.CreateGuassianSharpenFilter(gaussianLayer.Size); @@ -156,71 +64,5 @@ namespace ImageProcessor.Processors 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 } }