// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Sets the output of the image to a specific format. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Web.Processors { using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using ImageProcessor.Configuration; using ImageProcessor.Imaging.Formats; using ImageProcessor.Processors; /// /// Sets the output of the image to a specific format. /// public class Format : IWebGraphicsProcessor { /// /// The regular expression to search strings for. /// private static readonly Regex QueryRegex = BuildRegex(); /// /// Initializes a new instance of the class. /// public Format() { this.Processor = new ImageProcessor.Processors.Format(); } /// /// 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; ISupportedImageFormat format = this.ParseFormat(match.Value.Split('=')[1]); if (format != null) { this.Processor.DynamicParameter = format; } } index += 1; } } return this.SortOrder; } /// /// Builds a regular expression from the type, this allows extensibility. /// /// /// The to match matrix filters. /// private static Regex BuildRegex() { StringBuilder stringBuilder = new StringBuilder(); // png8 is a special case for determining indexed pngs. stringBuilder.Append("format=(png8"); foreach (ISupportedImageFormat imageFormat in ImageProcessorBootstrapper.Instance.SupportedImageFormats) { foreach (string fileExtension in imageFormat.FileExtensions) { stringBuilder.AppendFormat("|{0}", fileExtension.ToLowerInvariant()); } } stringBuilder.Append(")"); return new Regex(stringBuilder.ToString(), RegexOptions.IgnoreCase); } /// /// Parses the input string to return the correct . /// /// /// The identifier. /// /// /// The . /// private ISupportedImageFormat ParseFormat(string identifier) { identifier = identifier.ToLowerInvariant(); string finalIdentifier = identifier.Equals("png8") ? "png" : identifier; ISupportedImageFormat newFormat = null; List formats = ImageProcessorBootstrapper.Instance.SupportedImageFormats.ToList(); ISupportedImageFormat format = formats.FirstOrDefault(f => f.FileExtensions.Any(e => e.Equals(finalIdentifier, StringComparison.InvariantCultureIgnoreCase))); if (format != null) { // Return a new instance as we want to use instance properties. newFormat = Activator.CreateInstance(format.GetType()) as ISupportedImageFormat; if (newFormat != null) { // I wish this wasn't hard-coded but there's no way I can // find to preserve the palette. if (identifier.Equals("png8")) { newFormat.IsIndexed = true; } else if (identifier.Equals("png")) { newFormat.IsIndexed = false; } } } return newFormat; } } }