Browse Source

Adding entropy crop to web

TODO: Unit tests

Former-commit-id: 91dffbbaeb62ae184dd3ecc5500db7a1d6b3321b
pull/17/head
James South 12 years ago
parent
commit
c287d7ce3d
  1. 3
      src/ImageProcessor.Web/Configuration/Resources/processing.config
  2. 2
      src/ImageProcessor.Web/Helpers/CommonParameterParserUtility.cs
  3. 1
      src/ImageProcessor.Web/ImageProcessor.Web.csproj
  4. 3
      src/ImageProcessor.Web/Processors/Crop.cs
  5. 119
      src/ImageProcessor.Web/Processors/EntropyCrop.cs
  6. 1
      src/TestWebsites/MVC/config/imageprocessor/processing.config

3
src/ImageProcessor.Web/Configuration/Resources/processing.config

@ -7,8 +7,9 @@
<plugin name="BackgroundColor" type="ImageProcessor.Web.Processors.BackgroundColor, ImageProcessor.Web"/>
<plugin name="Brightness" type="ImageProcessor.Web.Processors.Brightness, ImageProcessor.Web"/>
<plugin name="Contrast" type="ImageProcessor.Web.Processors.Contrast, ImageProcessor.Web"/>
<plugin name="DetectEdges" type="ImageProcessor.Web.Processors.DetectEdges, ImageProcessor.Web"/>
<plugin name="Crop" type="ImageProcessor.Web.Processors.Crop, ImageProcessor.Web"/>
<plugin name="DetectEdges" type="ImageProcessor.Web.Processors.DetectEdges, ImageProcessor.Web"/>
<plugin name="EntropyCrop" type="ImageProcessor.Web.Processors.EntropyCrop, ImageProcessor.Web"/>
<plugin name="Filter" type="ImageProcessor.Web.Processors.Filter, ImageProcessor.Web"/>
<plugin name="Flip" type="ImageProcessor.Web.Processors.Flip, ImageProcessor.Web"/>
<plugin name="Format" type="ImageProcessor.Web.Processors.Format, ImageProcessor.Web"/>

2
src/ImageProcessor.Web/Helpers/CommonParameterParserUtility.cs

@ -35,7 +35,7 @@ namespace ImageProcessor.Web.Helpers
/// The regular expression to search strings for colors.
/// </summary>
private static readonly Regex ColorRegex = BuildColorRegex();
/// <summary>
/// The regular expression to search strings for angles.
/// </summary>

1
src/ImageProcessor.Web/ImageProcessor.Web.csproj

@ -47,6 +47,7 @@
<ItemGroup>
<Compile Include="Caching\CachedImage.cs" />
<Compile Include="Processors\DetectEdges.cs" />
<Compile Include="Processors\EntropyCrop.cs" />
<Compile Include="Services\IImageService.cs" />
<Compile Include="Caching\MemCache.cs" />
<Compile Include="Caching\DiskCache.cs" />

3
src/ImageProcessor.Web/Processors/Crop.cs

@ -86,7 +86,8 @@ namespace ImageProcessor.Web.Processors
foreach (Match match in this.RegexPattern.Matches(queryString))
{
if (match.Success)
// Match but ignore entropy
if (match.Success && !queryString.ToUpperInvariant().Contains("ENTROPYCROP="))
{
if (index == 0)
{

119
src/ImageProcessor.Web/Processors/EntropyCrop.cs

@ -0,0 +1,119 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EntropyCrop.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Performs a crop on an image to the area of greatest entropy.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System.Globalization;
using System.Text.RegularExpressions;
using ImageProcessor.Common.Extensions;
using ImageProcessor.Processors;
/// <summary>
/// Performs a crop on an image to the area of greatest entropy.
/// </summary>
public class EntropyCrop : IWebGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"entropycrop=(\d+)[^&]+|entropycrop", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="EntropyCrop"/> class.
/// </summary>
public EntropyCrop()
{
this.Processor = new ImageProcessor.Processors.EntropyCrop();
}
/// <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;
byte threshold = this.ParseThreshold(match.Value);
this.Processor.DynamicParameter = threshold;
}
index += 1;
}
}
return this.SortOrder;
}
/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> containing the radius for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Int32"/> containing the radius for the given string.
/// </returns>
private byte ParseThreshold(string input)
{
foreach (Match match in QueryRegex.Matches(input))
{
if (!match.Value.Contains("="))
{
continue;
}
// Split on threshold
int threshold;
int.TryParse(match.Value.Split('=')[1], NumberStyles.Any, CultureInfo.InvariantCulture, out threshold);
return threshold.ToByte();
}
// No threshold - matches the EntropyCrop default.
return 128;
}
}
}

1
src/TestWebsites/MVC/config/imageprocessor/processing.config

@ -9,6 +9,7 @@
<plugin name="Contrast" type="ImageProcessor.Web.Processors.Contrast, ImageProcessor.Web"/>
<plugin name="Crop" type="ImageProcessor.Web.Processors.Crop, ImageProcessor.Web"/>
<plugin name="DetectEdges" type="ImageProcessor.Web.Processors.DetectEdges, ImageProcessor.Web"/>
<plugin name="EntropyCrop" type="ImageProcessor.Web.Processors.EntropyCrop, ImageProcessor.Web"/>
<plugin name="Filter" type="ImageProcessor.Web.Processors.Filter, ImageProcessor.Web"/>
<plugin name="Flip" type="ImageProcessor.Web.Processors.Flip, ImageProcessor.Web"/>
<plugin name="Format" type="ImageProcessor.Web.Processors.Format, ImageProcessor.Web"/>

Loading…
Cancel
Save