Browse Source

Adding meta and more tests

Former-commit-id: 25a5a78cd5908ff459e4f5a49de5ffa88a871523
af/merge-core
James South 12 years ago
parent
commit
07ebfd9a70
  1. 135
      src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs
  2. 1
      src/ImageProcessor.Web/Configuration/Resources/processing.config
  3. 1
      src/ImageProcessor.Web/ImageProcessor.Web.csproj
  4. 92
      src/ImageProcessor.Web/Processors/Meta.cs
  5. 1
      src/ImageProcessor/ImageProcessor.csproj
  6. 74
      src/ImageProcessor/Processors/Meta.cs

135
src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs

@ -102,32 +102,6 @@ namespace ImageProcessor.Web.UnitTests
Assert.AreEqual(expected, result);
}
/// <summary>
/// The saturation regex unit test.
/// </summary>
/// <param name="input">
/// The input string.
/// </param>
/// <param name="expected">
/// The expected result.
/// </param>
[Test]
[TestCase("saturation=56", 56)]
[TestCase("saturation=84", 84)]
[TestCase("saturation=66", 66)]
[TestCase("saturation=101", 1)]
[TestCase("saturation=00001", 1)]
[TestCase("saturation=-50", -50)]
[TestCase("saturation=0", 0)]
public void TestSaturationRegex(string input, int expected)
{
Processors.Saturation saturation = new Processors.Saturation();
saturation.MatchRegexIndex(input);
int result = saturation.Processor.DynamicParameter;
Assert.AreEqual(expected, result);
}
/// <summary>
/// The rotate regex unit test.
/// </summary>
@ -246,21 +220,53 @@ namespace ImageProcessor.Web.UnitTests
Assert.AreEqual(expected, result);
}
/// <summary>
/// The meta regex unit test.
/// </summary>
/// <param name="input">
/// The input.
/// </param>
/// <param name="expected">
/// The expected result.
/// </param>
[Test]
[TestCase("meta=true", true)]
[TestCase("meta=false", false)]
public void TestMetaRegex(string input, bool expected)
{
Processors.Meta meta = new Processors.Meta();
meta.MatchRegexIndex(input);
bool result = meta.Processor.DynamicParameter;
Assert.AreEqual(expected, result);
}
/// <summary>
/// The resize regex unit test.
/// </summary>
[Test]
public void TestResizeRegex()
{
const string Querystring = "width=300";
ResizeLayer expected = new ResizeLayer(new Size(300, 0));
Dictionary<string, ResizeLayer> data = new Dictionary<string, ResizeLayer>
{
{
"width=300", new ResizeLayer(new Size(300, 0))
},
{
"height=300", new ResizeLayer(new Size(0, 300))
},
{
"height=300&mode=crop", new ResizeLayer(new Size(0, 300), ResizeMode.Crop)
}
};
Processors.Resize resize = new Processors.Resize();
resize.MatchRegexIndex(Querystring);
ResizeLayer actual = resize.Processor.DynamicParameter;
Assert.AreEqual(expected, actual);
foreach (KeyValuePair<string, ResizeLayer> item in data)
{
resize.MatchRegexIndex(item.Key);
ResizeLayer result = resize.Processor.DynamicParameter;
Assert.AreEqual(item.Value, result);
}
}
/// <summary>
@ -315,6 +321,32 @@ namespace ImageProcessor.Web.UnitTests
}
}
/// <summary>
/// The saturation regex unit test.
/// </summary>
/// <param name="input">
/// The input string.
/// </param>
/// <param name="expected">
/// The expected result.
/// </param>
[Test]
[TestCase("saturation=56", 56)]
[TestCase("saturation=84", 84)]
[TestCase("saturation=66", 66)]
[TestCase("saturation=101", 1)]
[TestCase("saturation=00001", 1)]
[TestCase("saturation=-50", -50)]
[TestCase("saturation=0", 0)]
public void TestSaturationRegex(string input, int expected)
{
Processors.Saturation saturation = new Processors.Saturation();
saturation.MatchRegexIndex(input);
int result = saturation.Processor.DynamicParameter;
Assert.AreEqual(expected, result);
}
/// <summary>
/// The tint regex unit test.
/// </summary>
@ -345,5 +377,42 @@ namespace ImageProcessor.Web.UnitTests
Assert.AreEqual(item.Value, result);
}
}
/// <summary>
/// The vignette regex unit test.
/// </summary>
[Test]
public void TestVignetteRegex()
{
Dictionary<string, Color> data = new Dictionary<string, Color>
{
{
"vignette", Color.Black
},
{
"vignette=true", Color.Black
},
{
"vignette=6aa6cc", ColorTranslator.FromHtml("#" + "6aa6cc")
},
{
"vignette=106,166,204,255", Color.FromArgb(255, 106, 166, 204)
},
{
"vignette=fff", Color.FromArgb(255, 255, 255, 255)
},
{
"vignette=white", Color.White
}
};
Processors.Vignette vignette = new Processors.Vignette();
foreach (KeyValuePair<string, Color> item in data)
{
vignette.MatchRegexIndex(item.Key);
Color result = vignette.Processor.DynamicParameter;
Assert.AreEqual(item.Value, result);
}
}
}
}

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

@ -25,6 +25,7 @@
<setting key="MaxThreshold" value="100"/>
</settings>
</plugin>
<plugin name="Meta" type="ImageProcessor.Web.Processors.Meta, ImageProcessor.Web"/>
<plugin name="Quality" type="ImageProcessor.Web.Processors.Quality, ImageProcessor.Web"/>
<plugin name="Resize" type="ImageProcessor.Web.Processors.Resize, ImageProcessor.Web">
<settings>

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

@ -82,6 +82,7 @@
<Compile Include="Processors\RoundedCorners.cs" />
<Compile Include="Processors\Saturation.cs" />
<Compile Include="Processors\Tint.cs" />
<Compile Include="Processors\Meta.cs" />
<Compile Include="Processors\Vignette.cs" />
<Compile Include="Processors\Watermark.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

92
src/ImageProcessor.Web/Processors/Meta.cs

@ -0,0 +1,92 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Meta.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods to control preservation of meta information.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System.Text.RegularExpressions;
using ImageProcessor.Processors;
/// <summary>
/// Encapsulates methods to control preservation of meta information.
/// </summary>
public class Meta : IWebGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"meta=(true|false)", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Meta"/> class.
/// </summary>
public Meta()
{
this.Processor = new ImageProcessor.Processors.Meta();
}
/// <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;
bool preserve = bool.Parse(match.Value.Split('=')[1]);
this.Processor.DynamicParameter = preserve;
}
index += 1;
}
}
return this.SortOrder;
}
}
}

1
src/ImageProcessor/ImageProcessor.csproj

@ -118,6 +118,7 @@
<Compile Include="Processors\Brightness.cs" />
<Compile Include="Processors\Contrast.cs" />
<Compile Include="Processors\GaussianSharpen.cs" />
<Compile Include="Processors\Meta.cs" />
<Compile Include="Processors\RoundedCorners.cs" />
<Compile Include="Processors\Saturation.cs" />
<Compile Include="Processors\Flip.cs" />

74
src/ImageProcessor/Processors/Meta.cs

@ -0,0 +1,74 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Meta.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods to control preservation of meta information.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Processors
{
using System;
using System.Collections.Generic;
using System.Drawing;
using ImageProcessor.Common.Exceptions;
/// <summary>
/// Encapsulates methods to control preservation of meta information.
/// </summary>
public class Meta : IGraphicsProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="Meta"/> class.
/// </summary>
public Meta()
{
this.Settings = new Dictionary<string, string>();
}
/// <summary>
/// Gets or sets DynamicParameter.
/// </summary>
public dynamic DynamicParameter
{
get;
set;
}
/// <summary>
/// Gets or sets any additional settings required by the processor.
/// </summary>
public Dictionary<string, string> Settings
{
get;
set;
}
/// <summary>
/// Processes the image.
/// </summary>
/// <param name="factory">
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
/// the image to process.
/// </param>
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory)
{
try
{
factory.PreserveExifData = this.DynamicParameter;
}
catch (Exception ex)
{
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
return factory.Image;
}
}
}
Loading…
Cancel
Save