// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Encapsulates methods to pixelate an image. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Web.Processors { using System; using System.Collections.Specialized; using System.Drawing; using System.Globalization; using System.Text; using System.Text.RegularExpressions; using System.Web; using ImageProcessor.Processors; using ImageProcessor.Web.Extensions; using ImageProcessor.Web.Helpers; /// /// Encapsulates methods to pixelate an image. /// public class Pixelate : IWebGraphicsProcessor { /// /// The regular expression to search strings for. /// private static readonly Regex QueryRegex = new Regex(@"pixelate=[^&]", 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 Pixelate() { this.Processor = new ImageProcessor.Processors.Pixelate(); } /// /// 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) { this.SortOrder = int.MaxValue; Match match = this.RegexPattern.Match(queryString); if (match.Success) { this.SortOrder = match.Index; NameValueCollection queryCollection = HttpUtility.ParseQueryString(queryString); int size = QueryParamParser.Instance.ParseValue(queryCollection["pixelate"]); Rectangle? rectangle = queryCollection["pixelrect"] != null ? QueryParamParser.Instance.ParseValue(queryCollection["pixelrect"]) : (Rectangle?)null; 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; } } }