Browse Source

Adding pixelate to Web

Former-commit-id: 5282fe802e662491408ae81b4690a82e78a250e7
Former-commit-id: 9073787f97a927614ab35ba9a402e421e5f0b8b2
af/merge-core
James South 11 years ago
parent
commit
fbb7cfe8e9
  1. 8
      src/ImageProcessor.Playground/Program.cs
  2. 1
      src/ImageProcessor.Playground/images/input/img_52061.jfif.REMOVED.git-id
  3. 1
      src/ImageProcessor.Web/Configuration/Resources/processing.config
  4. 5
      src/ImageProcessor.Web/Helpers/CommonParameterParserUtility.cs
  5. 1
      src/ImageProcessor.Web/ImageProcessor.Web.csproj
  6. 4
      src/ImageProcessor.Web/Processors/Crop.cs
  7. 163
      src/ImageProcessor.Web/Processors/Pixelate.cs
  8. 6
      src/TestWebsites/MVC/Test_Website_MVC.csproj
  9. 31
      src/TestWebsites/MVC/Web.config
  10. 2
      src/TestWebsites/MVC/packages.config

8
src/ImageProcessor.Playground/Program.cs

@ -50,7 +50,7 @@ namespace ImageProcessor.PlayGround
}
Image mask = Image.FromFile(Path.Combine(resolvedPath, "mask2.png"));
IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".gif");
IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".jfif");
//IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".gif", ".webp", ".bmp", ".jpg", ".png", ".tif");
foreach (FileInfo fileInfo in files)
@ -66,7 +66,7 @@ namespace ImageProcessor.PlayGround
{
using (ImageFactory imageFactory = new ImageFactory(true))
{
Size size = new Size(270, 260);
Size size = new Size(400, 400);
ResizeLayer layer = new ResizeLayer(size, ResizeMode.Max, AnchorPosition.Center, false);
//ContentAwareResizeLayer layer = new ContentAwareResizeLayer(size)
@ -85,7 +85,9 @@ namespace ImageProcessor.PlayGround
//.BackgroundColor(Color.Cyan)
//.ReplaceColor(Color.FromArgb(255, 1, 107, 165), Color.FromArgb(255, 1, 165, 13), 80)
//.Resize(size)
.DetectEdges(new SobelEdgeFilter(), true)
.Resize(new ResizeLayer(size, ResizeMode.Max))
.Resize(new ResizeLayer(size, ResizeMode.Stretch))
//.DetectEdges(new SobelEdgeFilter(), true)
//.DetectEdges(new LaplacianOfGaussianEdgeFilter())
//.EntropyCrop()
//.Filter(MatrixFilters.Invert)

1
src/ImageProcessor.Playground/images/input/img_52061.jfif.REMOVED.git-id

@ -0,0 +1 @@
8725839c23cba63894d1c8646e8cfd87c615c1fc

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

@ -28,6 +28,7 @@
</settings>
</plugin>
<plugin name="Meta" type="ImageProcessor.Web.Processors.Meta, ImageProcessor.Web"/>
<plugin name="Pixelate" type="ImageProcessor.Web.Processors.Pixelate, ImageProcessor.Web"/>
<plugin name="Quality" type="ImageProcessor.Web.Processors.Quality, ImageProcessor.Web"/>
<plugin name="Resize" type="ImageProcessor.Web.Processors.Resize, ImageProcessor.Web">
<settings>

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

@ -39,7 +39,6 @@ namespace ImageProcessor.Web.Helpers
/// <summary>
/// The regular expression to search strings for angles.
/// </summary>
//private static readonly Regex AngleRegex = new Regex(@"(rotate|angle)(=|-)(?:3[0-5][0-9]|[12][0-9]{2}|[1-9][0-9]?)", RegexOptions.Compiled);
private static readonly Regex AngleRegex = new Regex(@"(rotate|angle)(=|-)(-)?\d+(.?\d+)?", RegexOptions.Compiled);
/// <summary>
@ -78,8 +77,8 @@ namespace ImageProcessor.Web.Helpers
// Split on angle
float angle;
string value = match.Value;
value = match.Value.ToUpperInvariant().Contains("ANGLE")
? value.Substring(value.IndexOf("-", System.StringComparison.Ordinal) + 1)
value = match.Value.ToUpperInvariant().Contains("ANGLE")
? value.Substring(value.IndexOf("-", System.StringComparison.Ordinal) + 1)
: match.Value.Split('=')[1];
float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out angle);

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

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

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

@ -145,13 +145,13 @@ namespace ImageProcessor.Web.Processors
}
/// <summary>
/// Returns the correct <see cref="CropMode"/> for the given string.
/// Returns the crop coordinates for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="CropMode"/>.
/// The <see cref="float"/> array containing the crop coordinates.
/// </returns>
private float[] ParseCoordinates(string input)
{

163
src/ImageProcessor.Web/Processors/Pixelate.cs

@ -0,0 +1,163 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Pixelate.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods to pixelate an image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using ImageProcessor.Processors;
using ImageProcessor.Web.Extensions;
/// <summary>
/// Encapsulates methods to pixelate an image.
/// </summary>
public class Pixelate : IWebGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"(pixelate=|pixelrect=)[^&]+", RegexOptions.Compiled);
/// <summary>
/// The pixel regex.
/// </summary>
private static readonly Regex PixelRegex = new Regex(@"pixelate=\d+", RegexOptions.Compiled);
/// <summary>
/// The rectangle regex.
/// </summary>
private static readonly Regex RectangleRegex = new Regex(@"pixelrect=\d+,\d+,\d+,\d+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Pixelate"/> class.
/// </summary>
public Pixelate()
{
this.Processor = new ImageProcessor.Processors.Pixelate();
}
/// <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;
// First merge the matches so we can parse .
StringBuilder stringBuilder = new StringBuilder();
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;
}
stringBuilder.Append(match.Value);
index += 1;
}
}
if (this.SortOrder < int.MaxValue)
{
// Match syntax
string toParse = stringBuilder.ToString();
int size = this.ParseSize(toParse);
Rectangle? rectangle = this.ParseRectangle(toParse);
this.Processor.DynamicParameter = new Tuple<int, Rectangle?>(size, rectangle);
}
return this.SortOrder;
}
/// <summary>
/// Returns the correct size of pixels.
/// </summary>
/// <param name="input">
/// The input containing the value to parse.
/// </param>
/// <returns>
/// The <see cref="int"/> representing the pixel size.
/// </returns>
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;
}
/// <summary>
/// Returns the correct <see cref="Nullable{Rectange}"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="Nullable{Rectange}"/>
/// </returns>
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;
}
}
}

6
src/TestWebsites/MVC/Test_Website_MVC.csproj

@ -47,7 +47,7 @@
<HintPath>..\..\..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.Mvc.FixedDisplayModes">
<HintPath>..\..\..\packages\Microsoft.AspNet.Mvc.FixedDisplayModes.1.0.1\lib\net40\Microsoft.Web.Mvc.FixedDisplayModes.dll</HintPath>
<HintPath>..\..\packages\Microsoft.AspNet.Mvc.FixedDisplayModes.1.0.1\lib\net40\Microsoft.Web.Mvc.FixedDisplayModes.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\..\packages\Newtonsoft.Json.5.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
@ -65,9 +65,9 @@
<Private>True</Private>
<HintPath>..\..\..\packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.Helpers.dll</HintPath>
</Reference>
<Reference Include="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Reference Include="System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\..\..\packages\Microsoft.AspNet.Mvc.4.0.30506.0\lib\net40\System.Web.Mvc.dll</HintPath>
<HintPath>..\..\packages\Microsoft.AspNet.Mvc.4.0.40804.0\lib\net40\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>

31
src/TestWebsites/MVC/Web.config

@ -7,15 +7,15 @@
<configuration>
<configSections>
<sectionGroup name="imageProcessor">
<section name="security" requirePermission="false" type="ImageProcessor.Web.Configuration.ImageSecuritySection, ImageProcessor.Web"/>
<section name="processing" requirePermission="false" type="ImageProcessor.Web.Configuration.ImageProcessingSection, ImageProcessor.Web"/>
<section name="cache" requirePermission="false" type="ImageProcessor.Web.Configuration.ImageCacheSection, ImageProcessor.Web"/>
<section name="security" requirePermission="false" type="ImageProcessor.Web.Configuration.ImageSecuritySection, ImageProcessor.Web" />
<section name="processing" requirePermission="false" type="ImageProcessor.Web.Configuration.ImageProcessingSection, ImageProcessor.Web" />
<section name="cache" requirePermission="false" type="ImageProcessor.Web.Configuration.ImageCacheSection, ImageProcessor.Web" />
</sectionGroup>
</configSections>
<imageProcessor >
<security configSource="config\imageprocessor\security.config"/>
<cache configSource="config\imageprocessor\cache.config"/>
<processing configSource="config\imageprocessor\processing.config"/>
<imageProcessor>
<security configSource="config\imageprocessor\security.config" />
<cache configSource="config\imageprocessor\cache.config" />
<processing configSource="config\imageprocessor\processing.config" />
</imageProcessor>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
@ -42,15 +42,15 @@
</namespaces>
</pages>
<httpModules>
<add name="ImageProcessorModule" type="ImageProcessor.Web.HttpModules.ImageProcessingModule, ImageProcessor.Web"/>
<add name="ImageProcessorModule" type="ImageProcessor.Web.HttpModules.ImageProcessingModule, ImageProcessor.Web" />
</httpModules>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<validation validateIntegratedModeConfiguration="false" />
<staticContent>
<remove fileExtension=".webp"/>
<remove fileExtension=".webp" />
<mimeMap fileExtension=".webp" mimeType="image/webp" />
</staticContent>
<handlers>
@ -62,7 +62,16 @@
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<modules>
<add name="ImageProcessorModule" type="ImageProcessor.Web.HttpModules.ImageProcessingModule, ImageProcessor.Web"/>
<add name="ImageProcessorModule" type="ImageProcessor.Web.HttpModules.ImageProcessingModule, ImageProcessor.Web" />
</modules>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.1" newVersion="4.0.0.1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

2
src/TestWebsites/MVC/packages.config

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.Mvc" version="4.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="4.0.40804.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="1.0.1" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi" version="4.0.20710.0" targetFramework="net45" />

Loading…
Cancel
Save