From e60bc73b2f586fc2bc6accc5e721eeffec568da9 Mon Sep 17 00:00:00 2001 From: James South Date: Thu, 30 Oct 2014 22:49:01 +0000 Subject: [PATCH] Begin web Hue DO NOT USE!! Former-commit-id: 9466e13485c2523873b7beac1f71e3bf240158b6 Former-commit-id: 61d543966d54b52225fb08a4b4add733b6ad2012 --- src/ImageProcessor.Web/Processors/Hue.cs | 150 +++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 src/ImageProcessor.Web/Processors/Hue.cs diff --git a/src/ImageProcessor.Web/Processors/Hue.cs b/src/ImageProcessor.Web/Processors/Hue.cs new file mode 100644 index 000000000..0662e5f94 --- /dev/null +++ b/src/ImageProcessor.Web/Processors/Hue.cs @@ -0,0 +1,150 @@ +namespace ImageProcessor.Web.Processors +{ + using System; + using System.Drawing; + using System.Globalization; + using System.Text; + using System.Text.RegularExpressions; + + using ImageProcessor.Processors; + using ImageProcessor.Web.Extensions; + + public class Hue : IWebGraphicsProcessor + { + /// + /// The regular expression to search strings for. + /// + private static readonly Regex QueryRegex = new Regex(@"(hue=|pixelrect=)[^&]+", RegexOptions.Compiled); + + /// + /// The pixel regex. + /// + private static readonly Regex PixelRegex = new Regex(@"pixelate=\d+", RegexOptions.Compiled); + + /// + /// The rectangle regex. + /// + private static readonly Regex RectangleRegex = new Regex(@"pixelrect=\d+,\d+,\d+,\d+", RegexOptions.Compiled); + + /// + /// Initializes a new instance of the class. + /// + public Hue() + { + this.Processor = new ImageProcessor.Processors.Hue(); + } + + /// + /// 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; + + // First merge the matches so we can parse . + StringBuilder stringBuilder = new StringBuilder(); + + 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; + } + + stringBuilder.Append(match.Value); + + index += 1; + } + } + + if (this.SortOrder < int.MaxValue) + { + // Match syntax + string toParse = stringBuilder.ToString(); + int size = this.ParseSize(toParse); + Rectangle? rectangle = this.ParseRectangle(toParse); + this.Processor.DynamicParameter = new Tuple(size, rectangle); + } + + return this.SortOrder; + } + + /// + /// Returns the correct size of pixels. + /// + /// + /// The input containing the value to parse. + /// + /// + /// The representing the pixel size. + /// + public int ParseSize(string input) + { + int size = 0; + + foreach (Match match in PixelRegex.Matches(input)) + { + size = int.Parse(match.Value.Split('=')[1], CultureInfo.InvariantCulture); + } + + return size; + } + + /// + /// Returns the correct for the given string. + /// + /// + /// The input string containing the value to parse. + /// + /// + /// The correct + /// + private Rectangle? ParseRectangle(string input) + { + int[] dimensions = { }; + + foreach (Match match in RectangleRegex.Matches(input)) + { + dimensions = match.Value.ToPositiveIntegerArray(); + } + + if (dimensions.Length == 4) + { + return new Rectangle(dimensions[0], dimensions[1], dimensions[2], dimensions[3]); + } + + return null; + } + } +}