Browse Source

Updating cache checker.

Original method was too processor intensive


Former-commit-id: c4e96a8657da8e48267667a9c790b7730d3434ef
pull/17/head
James South 12 years ago
parent
commit
bb5ae2ebb9
  1. 7
      src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj
  2. 161
      src/ImageProcessor.Web/NET45/Processors/Filter.cs
  3. 37
      src/ImageProcessor/Imaging/Filters/MatrixFilterRegexAttribute.cs
  4. 32
      src/ImageProcessor/Imaging/Filters/MatrixFilters.cs
  5. 130
      src/ImageProcessor/Processors/Filter.cs

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

@ -61,10 +61,12 @@
<Compile Include="HttpModules\ImageProcessingModule.cs" />
<Compile Include="ImageFactoryExtensions.cs" />
<Compile Include="Processors\Alpha.cs" />
<Compile Include="Processors\AutoRotate.cs" />
<Compile Include="Processors\BackgroundColor.cs" />
<Compile Include="Processors\Brightness.cs" />
<Compile Include="Processors\Contrast.cs" />
<Compile Include="Processors\Crop.cs" />
<Compile Include="Processors\Filter.cs" />
<Compile Include="Processors\IWebGraphicsProcessor.cs" />
<Compile Include="Processors\Quality.cs" />
<Compile Include="Processors\Resize.cs" />
@ -87,11 +89,6 @@
</EmbeddedResource>
<EmbeddedResource Include="Configuration\Resources\security.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Configuration\Resources\processing - Copy.config">
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

161
src/ImageProcessor.Web/NET45/Processors/Filter.cs

@ -0,0 +1,161 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Filter.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods with which to add filters to an image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using ImageProcessor.Imaging.Filters;
using ImageProcessor.Processors;
/// <summary>
/// Encapsulates methods with which to add filters to an image.
/// </summary>
public class Filter : IWebGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = BuildRegex();
/// <summary>
/// Initializes a new instance of the <see cref="Filter"/> class.
/// </summary>
public Filter()
{
this.Processor = new ImageProcessor.Processors.Filter();
}
/// <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 processor.
/// </summary>
/// <value>
/// The processor.
/// </value>
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 = this.ParseFilter(match.Value.Split('=')[1]);
}
index += 1;
}
}
return this.SortOrder;
}
/// <summary>
/// Builds a regular expression from the <see cref="MatrixFilters"/> type, this allows extensibility.
/// </summary>
/// <returns>
/// The <see cref="Regex"/> to match matrix filters.
/// </returns>
private static Regex BuildRegex()
{
const BindingFlags Flags = BindingFlags.Public | BindingFlags.Static;
Type type = typeof(MatrixFilters);
IEnumerable<PropertyInfo> filters = type.GetProperties(Flags)
.Where(p => p.PropertyType.IsAssignableFrom(typeof(IMatrixFilter)))
.ToList();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("filter=(");
int counter = 0;
foreach (PropertyInfo filter in filters)
{
if (counter == 0)
{
stringBuilder.Append(filter.Name.ToLowerInvariant());
}
else
{
stringBuilder.AppendFormat("|{0}", filter.Name.ToLowerInvariant());
}
counter++;
}
stringBuilder.Append(")");
return new Regex(stringBuilder.ToString(), RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
/// <summary>
/// Parses the filter.
/// </summary>
/// <param name="identifier">
/// The identifier.
/// </param>
/// <returns>
/// The <see cref="IMatrixFilter"/>.
/// </returns>
private IMatrixFilter ParseFilter(string identifier)
{
const BindingFlags Flags = BindingFlags.Public | BindingFlags.Static;
Type type = typeof(MatrixFilters);
PropertyInfo filter =
type.GetProperties(Flags)
.Where(p => p.PropertyType.IsAssignableFrom(typeof(IMatrixFilter)))
.First(p => p.Name.Equals(identifier, StringComparison.InvariantCultureIgnoreCase));
return filter.GetValue(null, null) as IMatrixFilter;
}
}
}

37
src/ImageProcessor/Imaging/Filters/MatrixFilterRegexAttribute.cs

@ -1,37 +0,0 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MatrixFilterRegexAttribute.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// The filter attribute for identifying matrix filter properties.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Imaging.Filters
{
using System;
/// <summary>
/// The filter attribute for identifying matrix filter properties.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class MatrixFilterRegexAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="MatrixFilterRegexAttribute"/> class.
/// </summary>
/// <param name="regexIdentifier">
/// The regex identifier.
/// </param>
public MatrixFilterRegexAttribute(string regexIdentifier)
{
this.RegexIdentifier = regexIdentifier;
}
/// <summary>
/// Gets the regex identifier.
/// </summary>
public string RegexIdentifier { get; private set; }
}
}

32
src/ImageProcessor/Imaging/Filters/MatrixFilters.cs

@ -18,9 +18,8 @@ namespace ImageProcessor.Imaging.Filters
public static class MatrixFilters
{
/// <summary>
/// Gets the black white filter.
/// Gets the <see cref="IMatrixFilter"/> for generating the black and white filter.
/// </summary>
[MatrixFilterRegex("blackwhite")]
public static IMatrixFilter BlackWhite
{
get
@ -30,9 +29,8 @@ namespace ImageProcessor.Imaging.Filters
}
/// <summary>
/// Gets the comic filter.
/// Gets the <see cref="IMatrixFilter"/> for generating the comic filter.
/// </summary>
[MatrixFilterRegex("comic")]
public static IMatrixFilter Comic
{
get
@ -42,9 +40,8 @@ namespace ImageProcessor.Imaging.Filters
}
/// <summary>
/// Gets the gotham filter.
/// Gets the <see cref="IMatrixFilter"/> for generating the gotham filter.
/// </summary>
[MatrixFilterRegex("gotham")]
public static IMatrixFilter Gotham
{
get
@ -54,9 +51,8 @@ namespace ImageProcessor.Imaging.Filters
}
/// <summary>
/// Gets the greyscale filter.
/// Gets the <see cref="IMatrixFilter"/> for generating the greyscale filter.
/// </summary>
[MatrixFilterRegex("greyscale")]
public static IMatrixFilter GreyScale
{
get
@ -66,9 +62,8 @@ namespace ImageProcessor.Imaging.Filters
}
/// <summary>
/// Gets the high saturation filter.
/// Gets the <see cref="IMatrixFilter"/> for generating the high saturation filter.
/// </summary>
[MatrixFilterRegex("hisatch")]
public static IMatrixFilter HiSatch
{
get
@ -78,9 +73,8 @@ namespace ImageProcessor.Imaging.Filters
}
/// <summary>
/// Gets the invert filter.
/// Gets the <see cref="IMatrixFilter"/> for generating the invert filter.
/// </summary>
[MatrixFilterRegex("invert")]
public static IMatrixFilter Invert
{
get
@ -90,9 +84,8 @@ namespace ImageProcessor.Imaging.Filters
}
/// <summary>
/// Gets the lomograph filter.
/// Gets the <see cref="IMatrixFilter"/> for generating the lomograph filter.
/// </summary>
[MatrixFilterRegex("lomograph")]
public static IMatrixFilter Lomograph
{
get
@ -102,9 +95,8 @@ namespace ImageProcessor.Imaging.Filters
}
/// <summary>
/// Gets the low saturation filter.
/// Gets the <see cref="IMatrixFilter"/> for generating the low saturation filter.
/// </summary>
[MatrixFilterRegex("losatch")]
public static IMatrixFilter LoSatch
{
get
@ -114,9 +106,8 @@ namespace ImageProcessor.Imaging.Filters
}
/// <summary>
/// Gets the polaroid filter.
/// Gets the <see cref="IMatrixFilter"/> for generating the polaroid filter.
/// </summary>
[MatrixFilterRegex("polaroid")]
public static IMatrixFilter Polaroid
{
get
@ -126,9 +117,8 @@ namespace ImageProcessor.Imaging.Filters
}
/// <summary>
/// Gets the sepia filter.
/// Gets the <see cref="IMatrixFilter"/> for generating the sepia filter.
/// </summary>
[MatrixFilterRegex("sepia")]
public static IMatrixFilter Sepia
{
get
@ -137,4 +127,4 @@ namespace ImageProcessor.Imaging.Filters
}
}
}
}
}

130
src/ImageProcessor/Processors/Filter.cs

@ -11,14 +11,9 @@
namespace ImageProcessor.Processors
{
#region Using
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using ImageProcessor.Imaging.Filters;
#endregion
@ -27,23 +22,6 @@ namespace ImageProcessor.Processors
/// </summary>
public class Filter : IGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = BuildRegex();
#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>
@ -53,15 +31,6 @@ namespace ImageProcessor.Processors
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>
@ -71,40 +40,6 @@ namespace ImageProcessor.Processors
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 = match.Value.Split('=')[1];
}
index += 1;
}
}
return this.SortOrder;
}
/// <summary>
/// Processes the image.
/// </summary>
@ -123,8 +58,7 @@ namespace ImageProcessor.Processors
try
{
newImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb);
IMatrixFilter matrix = this.DynamicParameter as IMatrixFilter ?? this.ParseFilter((string)this.DynamicParameter);
IMatrixFilter matrix = this.DynamicParameter as IMatrixFilter;
if (matrix != null)
{
@ -141,67 +75,5 @@ namespace ImageProcessor.Processors
return image;
}
#endregion
/// <summary>
/// Builds a regular expression from the <see cref="MatrixFilters"/> type, this allows extensibility.
/// </summary>
/// <returns>
/// The <see cref="Regex"/> to match matrix filters.
/// </returns>
private static Regex BuildRegex()
{
const BindingFlags Flags = BindingFlags.Public | BindingFlags.Static;
Type type = typeof(MatrixFilters);
IEnumerable<PropertyInfo> filters = type.GetProperties(Flags)
.Where(p => p.IsDefined(typeof(MatrixFilterRegexAttribute), false))
.ToList();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("filter=(");
int counter = 0;
foreach (PropertyInfo filter in filters)
{
MatrixFilterRegexAttribute attribute = (MatrixFilterRegexAttribute)filter.GetCustomAttributes(typeof(MatrixFilterRegexAttribute), false).First();
if (counter == 0)
{
stringBuilder.Append(attribute.RegexIdentifier);
}
else
{
stringBuilder.AppendFormat("|{0}", attribute.RegexIdentifier);
}
counter++;
}
stringBuilder.Append(")");
return new Regex(stringBuilder.ToString(), RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
/// <summary>
/// Parses the filter.
/// </summary>
/// <param name="identifier">
/// The identifier.
/// </param>
/// <returns>
/// The <see cref="IMatrixFilter"/>.
/// </returns>
private IMatrixFilter ParseFilter(string identifier)
{
const BindingFlags Flags = BindingFlags.Public | BindingFlags.Static;
Type type = typeof(MatrixFilters);
PropertyInfo filter =
type.GetProperties(Flags)
.Where(p => p.IsDefined(typeof(MatrixFilterRegexAttribute), false))
.First(p => ((MatrixFilterRegexAttribute)p.GetCustomAttributes(typeof(MatrixFilterRegexAttribute), false).First()).RegexIdentifier == identifier);
return filter.GetValue(null, null) as IMatrixFilter;
}
}
}

Loading…
Cancel
Save