Browse Source

Tint

Also tweaking format


Former-commit-id: 49ac885f761811ba96635401e191b4136e456df6
af/merge-core
James South 12 years ago
parent
commit
79fa29758c
  1. 12
      src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs
  2. 10
      src/ImageProcessor.Web/NET45/Helpers/ImageHelpers.cs
  3. 1
      src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj
  4. 2
      src/ImageProcessor.Web/NET45/Processors/BackgroundColor.cs
  5. 7
      src/ImageProcessor.Web/NET45/Processors/Format.cs
  6. 85
      src/ImageProcessor.Web/NET45/Processors/Tint.cs
  7. 2
      src/ImageProcessor/Imaging/Formats/PngFormat.cs
  8. 98
      src/ImageProcessor/Processors/Tint.cs
  9. 1
      src/TestWebsites/NET45/Test_Website_NET45/Web.config

12
src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs

@ -25,7 +25,7 @@ namespace ImageProcessor.Web.Helpers
/// <summary>
/// The regular expression to search strings for colors.
/// </summary>
private static readonly Regex ColorRegex = new Regex(@"(bgcolor|color)(=|-)(\d+,\d+,\d+,\d+|([0-9a-fA-F]{3}){1,2})", RegexOptions.Compiled);
private static readonly Regex ColorRegex = new Regex(@"(bgcolor|color|tint)(=|-)(\d+,\d+,\d+,\d+|([0-9a-fA-F]{3}){1,2})", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for angles.
@ -37,11 +37,6 @@ 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>
@ -57,7 +52,6 @@ namespace ImageProcessor.Web.Helpers
/// </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>
@ -202,7 +196,7 @@ namespace ImageProcessor.Web.Helpers
foreach (Match match in SigmaRegex.Matches(input))
{
// split on text-
return Convert.ToDouble(match.Value.Split('-')[1]);
return Convert.ToDouble(match.Value.Split(new[] { '=', '-' })[1]);
}
return 1.4d;
@ -223,7 +217,7 @@ namespace ImageProcessor.Web.Helpers
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (Match match in ThresholdRegex.Matches(input))
{
return Convert.ToInt32(match.Value.Split('-')[1]);
return Convert.ToInt32(match.Value.Split(new[] { '=', '-' })[1]);
}
return 0;

10
src/ImageProcessor.Web/NET45/Helpers/ImageHelpers.cs

@ -28,7 +28,7 @@ namespace ImageProcessor.Web.Helpers
/// <summary>
/// The image format regex.
/// </summary>
private static readonly Regex FormatRegex = new Regex(@"(\.?)" + RegexPattern, RegexOptions.IgnoreCase | RegexOptions.RightToLeft);
private static readonly Regex FormatRegex = new Regex(@"(\.?)(png8|" + RegexPattern + ")", RegexOptions.IgnoreCase | RegexOptions.RightToLeft);
/// <summary>
/// The image format regex for matching the file format at the end of a string.
@ -42,13 +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.Match(fileName);
if (match.Success && !match.Value.ToLowerInvariant().EndsWith("png8"))
{
return true;
}
return false;
return EndFormatRegex.IsMatch(fileName);
}
/// <summary>

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

@ -76,6 +76,7 @@
<Compile Include="Processors\Resize.cs" />
<Compile Include="Processors\Rotate.cs" />
<Compile Include="Processors\Saturation.cs" />
<Compile Include="Processors\Tint.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

2
src/ImageProcessor.Web/NET45/Processors/BackgroundColor.cs

@ -22,7 +22,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"bgcolor(=|-)[^&|,]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"bgcolor(=|-)[^&]*", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="BackgroundColor"/> class.

7
src/ImageProcessor.Web/NET45/Processors/Format.cs

@ -133,13 +133,14 @@ namespace ImageProcessor.Web.Processors
private ISupportedImageFormat ParseFormat(string identifier)
{
identifier = identifier.ToLowerInvariant();
string finalIdentifier = identifier.Equals("png8") ? "png" : identifier;
ISupportedImageFormat format = ImageProcessorBootstrapper.Instance.SupportedImageFormats
.FirstOrDefault(f => f.FileExtensions.Any(e => e.Equals(identifier, StringComparison.InvariantCultureIgnoreCase)));
.FirstOrDefault(f => f.FileExtensions.Any(e => e.Equals(finalIdentifier, StringComparison.InvariantCultureIgnoreCase)));
if (format != null)
{
// I wish this wasn't hardcoded but there's no way I can
// find to preserve the pallete.
// I wish this wasn't hard-coded but there's no way I can
// find to preserve the palette.
if (identifier.Equals("png8"))
{
format.IsIndexed = true;

85
src/ImageProcessor.Web/NET45/Processors/Tint.cs

@ -0,0 +1,85 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Tint.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Tints an image with the given color.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System.Text.RegularExpressions;
using ImageProcessor.Processors;
using ImageProcessor.Web.Helpers;
/// <summary>
/// Tints an image with the given color.
/// </summary>
public class Tint : IWebGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"tint=[^&]*", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Tint"/> class.
/// </summary>
public Tint()
{
this.Processor = new ImageProcessor.Processors.Tint();
}
/// <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;
this.Processor.DynamicParameter = CommonParameterParserUtility.ParseColor(match.Value);
}
index += 1;
}
}
return this.SortOrder;
}
}
}

2
src/ImageProcessor/Imaging/Formats/PngFormat.cs

@ -39,7 +39,7 @@ namespace ImageProcessor.Imaging.Formats
{
get
{
return new[] { "png8", "png" };
return new[] { "png" };
}
}

98
src/ImageProcessor/Processors/Tint.cs

@ -4,7 +4,7 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Tints an image with the given colour.
// Tints an image with the given color.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
@ -14,78 +14,22 @@ namespace ImageProcessor.Processors
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
using ImageProcessor.Core.Common.Extensions;
/// <summary>
/// Tints an image with the given colour.
/// Tints an image with the given color.
/// </summary>
public class Tint : IGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"tint=(\d+,\d+,\d+,\d+|([0-9a-fA-F]{3}){1,2})", RegexOptions.Compiled);
#region IGraphicsProcessor Members
/// <summary>
/// Gets the regular expression to search strings for.
/// </summary>
public Regex RegexPattern
{
get { return QueryRegex; }
}
/// <summary>
/// Gets or sets 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;
this.DynamicParameter = this.ParseColor(match.Value);
}
index += 1;
}
}
return this.SortOrder;
}
/// <summary>
/// Processes the image.
/// </summary>
@ -103,7 +47,7 @@ namespace ImageProcessor.Processors
try
{
Color tintColour = (Color)this.DynamicParameter;
Color tintColour = this.DynamicParameter;
float[][] colorMatrixElements =
{
new[] { tintColour.R / 255f, 0, 0, 0, 0 }, // Red
@ -144,39 +88,5 @@ namespace ImageProcessor.Processors
return image;
}
#endregion
/// <summary>
/// Returns the correct <see cref="T:System.Drawing.Color"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Drawing.Color"/>
/// </returns>
private Color ParseColor(string input)
{
foreach (Match match in QueryRegex.Matches(input))
{
string value = match.Value.Split('=')[1];
if (value.Contains(","))
{
int[] split = value.ToPositiveIntegerArray();
byte red = split[0].ToByte();
byte green = split[1].ToByte();
byte blue = split[2].ToByte();
byte alpha = split[3].ToByte();
return Color.FromArgb(alpha, red, green, blue);
}
// Split on color-hex
return ColorTranslator.FromHtml("#" + value);
}
return Color.Transparent;
}
}
}
}

1
src/TestWebsites/NET45/Test_Website_NET45/Web.config

@ -59,6 +59,5 @@
<modules>
<add name="ImageProcessorModule" type="ImageProcessor.Web.HttpModules.ImageProcessingModule, ImageProcessor.Web"/>
</modules>
</system.webServer>
</configuration>

Loading…
Cancel
Save