Browse Source

Updated blur and sharpen

Former-commit-id: 37bb4c7c0a160966e8a002659048821e29a902c3
af/merge-core
James South 12 years ago
parent
commit
91e9284cd7
  1. 117
      src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs
  2. 10
      src/ImageProcessor.Web/NET45/Helpers/ImageHelpers.cs
  3. 2
      src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj
  4. 101
      src/ImageProcessor.Web/NET45/Processors/GaussianBlur.cs
  5. 101
      src/ImageProcessor.Web/NET45/Processors/GaussianSharpen.cs
  6. 162
      src/ImageProcessor/Processors/GaussianBlur.cs
  7. 160
      src/ImageProcessor/Processors/GaussianSharpen.cs

117
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;
/// <summary>
/// Encapsulates methods to correctly parse querystring parameters.
@ -35,6 +37,27 @@ namespace ImageProcessor.Web.Helpers
/// </summary>
private static readonly Regex In100RangeRegex = new Regex(@"(-?(?:100)|-?([1-9]?[0-9]))", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex GuassianRegex = new Regex(@"(blur|sharpen|sigma|threshold)(=|-)[^&]*", RegexOptions.Compiled);
/// <summary>
/// The sharpen regex.
/// </summary>
private static readonly Regex BlurSharpenRegex = new Regex(@"(blur|sharpen)=\d+", RegexOptions.Compiled);
/// <summary>
/// The sigma regex.
/// </summary>
private static readonly Regex SigmaRegex = new Regex(@"sigma(=|-)\d+(.?\d+)?", RegexOptions.Compiled);
/// <summary>
/// The threshold regex.
/// </summary>
private static readonly Regex ThresholdRegex = new Regex(@"threshold(=|-)\d+", RegexOptions.Compiled);
/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> containing the angle for the given string.
/// </summary>
@ -111,5 +134,99 @@ namespace ImageProcessor.Web.Helpers
return value;
}
/// <summary>
/// Returns the correct <see cref="GaussianLayer"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <param name="maxSize">
/// The maximum size to set the Gaussian kernel to.
/// </param>
/// <param name="maxSigma">
/// The maximum Sigma value (standard deviation) for Gaussian function used to calculate the kernel.
/// </param>
/// <param name="maxThreshold">
/// The maximum threshold value, which is added to each weighted sum of pixels.
/// </param>
/// <returns>
/// The correct <see cref="GaussianLayer"/> .
/// </returns>
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);
}
/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> containing the blur value
/// for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Int32"/> for the given string.
/// </returns>
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;
}
/// <summary>
/// Returns the correct <see cref="T:System.Double"/> containing the sigma value
/// for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Double"/> for the given string.
/// </returns>
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;
}
/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> containing the threshold value
/// for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Int32"/> for the given string.
/// </returns>
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;
}
}
}

10
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
/// <summary>
/// The image helpers.
/// </summary>
@ -47,8 +42,7 @@ namespace ImageProcessor.Web.Helpers
/// <returns>True the value contains a valid image extension, otherwise false.</returns>
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
/// </returns>
public static string GetExtension(string input)
{
Match match = FormatRegex.Matches(input)[0];
Match match = FormatRegex.Match(input);
if (match.Success)
{

2
src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj

@ -69,6 +69,8 @@
<Compile Include="Processors\Filter.cs" />
<Compile Include="Processors\Flip.cs" />
<Compile Include="Processors\Format.cs" />
<Compile Include="Processors\GaussianSharpen.cs" />
<Compile Include="Processors\GaussianBlur.cs" />
<Compile Include="Processors\IWebGraphicsProcessor.cs" />
<Compile Include="Processors\Quality.cs" />
<Compile Include="Processors\Resize.cs" />

101
src/ImageProcessor.Web/NET45/Processors/GaussianBlur.cs

@ -0,0 +1,101 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GaussianBlur.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Applies a Gaussian blur to the image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System.Globalization;
using System.Text.RegularExpressions;
using ImageProcessor.Processors;
using ImageProcessor.Web.Helpers;
/// <summary>
/// Applies a Gaussian blur to the image.
/// </summary>
public class GaussianBlur : IWebGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"blur=[^&]*", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="GaussianBlur"/> class.
/// </summary>
public GaussianBlur()
{
this.Processor = new ImageProcessor.Processors.GaussianBlur();
}
/// <summary>
/// Gets the regular expression to search strings for.
/// </summary>
public Regex RegexPattern
{
get
{
return QueryRegex;
}
}
/// <summary>
/// Gets the order in which this processor is to be used in a chain.
/// </summary>
public int SortOrder { get; private set; }
/// <summary>
/// Gets the associated graphics processor.
/// </summary>
public IGraphicsProcessor Processor { get; private set; }
/// <summary>
/// The position in the original string where the first character of the captured substring was found.
/// </summary>
/// <param name="queryString">
/// The query string to search.
/// </param>
/// <returns>
/// The zero-based starting position in the original string where the captured substring was found.
/// </returns>
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;
}
}
}

101
src/ImageProcessor.Web/NET45/Processors/GaussianSharpen.cs

@ -0,0 +1,101 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GaussianSharpen.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Applies a Gaussian sharpen to the image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System.Globalization;
using System.Text.RegularExpressions;
using ImageProcessor.Processors;
using ImageProcessor.Web.Helpers;
/// <summary>
/// Applies a Gaussian sharpen to the image.
/// </summary>
public class GaussianSharpen : IWebGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"sharpen=[^&]*", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="GaussianSharpen"/> class.
/// </summary>
public GaussianSharpen()
{
this.Processor = new ImageProcessor.Processors.GaussianSharpen();
}
/// <summary>
/// Gets the regular expression to search strings for.
/// </summary>
public Regex RegexPattern
{
get
{
return QueryRegex;
}
}
/// <summary>
/// Gets the order in which this processor is to be used in a chain.
/// </summary>
public int SortOrder { get; private set; }
/// <summary>
/// Gets the associated graphics processor.
/// </summary>
public IGraphicsProcessor Processor { get; private set; }
/// <summary>
/// The position in the original string where the first character of the captured substring was found.
/// </summary>
/// <param name="queryString">
/// The query string to search.
/// </param>
/// <returns>
/// The zero-based starting position in the original string where the captured substring was found.
/// </returns>
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;
}
}
}

162
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;
/// <summary>
@ -22,105 +19,16 @@ namespace ImageProcessor.Processors
/// </summary>
public class GaussianBlur : IGraphicsProcessor
{
#region Fields
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"blur=[^&]*", RegexOptions.Compiled);
/// <summary>
/// The blur regex.
/// </summary>
private static readonly Regex BlurRegex = new Regex(@"blur=\d+", RegexOptions.Compiled);
/// <summary>
/// The sigma regex.
/// </summary>
private static readonly Regex SigmaRegex = new Regex(@"sigma-\d+(.?\d+)?", RegexOptions.Compiled);
/// <summary>
/// The threshold regex.
/// </summary>
private static readonly Regex ThresholdRegex = new Regex(@"threshold-\d+", RegexOptions.Compiled);
#endregion
#region IGraphicsProcessor Members
/// <summary>
/// Gets the regular expression to search strings for.
/// </summary>
public Regex RegexPattern
{
get
{
return QueryRegex;
}
}
/// <summary>
/// Gets or sets the DynamicParameter.
/// </summary>
public dynamic DynamicParameter { get; set; }
/// <summary>
/// Gets the order in which this processor is to be used in a chain.
/// </summary>
public int SortOrder { get; private set; }
/// <summary>
/// Gets or sets any additional settings required by the processor.
/// </summary>
public Dictionary<string, string> Settings { get; set; }
/// <summary>
/// The position in the original string where the first character of the captured substring was found.
/// </summary>
/// <param name="queryString">The query string to search.</param>
/// <returns>
/// The zero-based starting position in the original string where the captured substring was found.
/// </returns>
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;
}
/// <summary>
/// Processes the image.
/// </summary>
@ -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
/// <summary>
/// Returns the correct <see cref="T:System.Double"/> containing the sigma value
/// for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Double"/> for the given string.
/// </returns>
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;
}
/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> containing the threshold value
/// for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Int32"/> for the given string.
/// </returns>
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;
}
/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> containing the blur value
/// for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Int32"/> for the given string.
/// </returns>
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
}
}
}

160
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;
/// <summary>
@ -22,105 +19,16 @@ namespace ImageProcessor.Processors
/// </summary>
public class GaussianSharpen : IGraphicsProcessor
{
#region Fields
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"sharpen=[^&]*", RegexOptions.Compiled);
/// <summary>
/// The sharpen regex.
/// </summary>
private static readonly Regex SharpenRegex = new Regex(@"sharpen=\d+", RegexOptions.Compiled);
/// <summary>
/// The sigma regex.
/// </summary>
private static readonly Regex SigmaRegex = new Regex(@"sigma-\d+(.?\d+)?", RegexOptions.Compiled);
/// <summary>
/// The threshold regex.
/// </summary>
private static readonly Regex ThresholdRegex = new Regex(@"threshold-\d+", RegexOptions.Compiled);
#endregion
#region IGraphicsProcessor Members
/// <summary>
/// Gets the regular expression to search strings for.
/// </summary>
public Regex RegexPattern
{
get
{
return QueryRegex;
}
}
/// <summary>
/// Gets or sets the DynamicParameter.
/// </summary>
public dynamic DynamicParameter { get; set; }
/// <summary>
/// Gets the order in which this processor is to be used in a chain.
/// </summary>
public int SortOrder { get; private set; }
/// <summary>
/// Gets or sets any additional settings required by the processor.
/// </summary>
public Dictionary<string, string> Settings { get; set; }
/// <summary>
/// The position in the original string where the first character of the captured substring was found.
/// </summary>
/// <param name="queryString">The query string to search.</param>
/// <returns>
/// The zero-based starting position in the original string where the captured substring was found.
/// </returns>
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;
}
/// <summary>
/// Processes the image.
/// </summary>
@ -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
/// <summary>
/// Returns the correct <see cref="T:System.Double"/> containing the sigma value
/// for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Double"/> for the given string.
/// </returns>
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;
}
/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> containing the threshold value
/// for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Int32"/> for the given string.
/// </returns>
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;
}
/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> containing the sharpen value
/// for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Int32"/> for the given string.
/// </returns>
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
}
}

Loading…
Cancel
Save