Browse Source

Adding Tint

Former-commit-id: dee84652a23ad506edd06c701fe1119bf6e89cae
af/merge-core
James South 12 years ago
parent
commit
e7dc6e659d
  1. 2
      build/NuSpecs/ImageProcessor.Web.Config.nuspec
  2. 2
      build/NuSpecs/ImageProcessor.Web.nuspec
  3. 2
      build/NuSpecs/ImageProcessor.nuspec
  4. 26
      src/ImageProcessor.Tests/RegularExpressionUnitTests.cs
  5. 1
      src/ImageProcessor.Web/NET45/Config/Resources/processing.config
  6. 20
      src/ImageProcessor/ImageFactory.cs
  7. 1
      src/ImageProcessor/ImageProcessor.csproj
  8. 183
      src/ImageProcessor/Processors/Tint.cs
  9. 2
      src/ImageProcessorConsole/Program.cs
  10. 2
      src/ImageProcessorConsole/images/output/120430.gif.REMOVED.git-id
  11. 1
      src/ImageProcessorConsole/images/output/nLpfllv .gif.REMOVED.git-id
  12. 1
      src/ImageProcessorConsole/images/output/nLpfllv.gif.REMOVED.git-id
  13. 12
      src/TestWebsites/NET45/Test_Website_NET45/Views/Home/Index.cshtml
  14. 1
      src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config

2
build/NuSpecs/ImageProcessor.Web.Config.nuspec

@ -18,7 +18,7 @@ Feedback is always welcome</description>
<releaseNotes />
<copyright>James South</copyright>
<language>en-GB</language>
<tags>Image Imaging ASP Performance Processing HttpModule Cache Resize Rotate RoundedCorners Flip Crop Filter Effects Quality Watermark Alpha Vignette Saturation Brightness Contrast Gif Jpg Jpeg Bitmap Png Fluent GDI Gaussian Blur Sharpen</tags>
<tags>Image Imaging ASP Performance Processing HttpModule Cache Resize Rotate RoundedCorners Flip Crop Filter Effects Quality Watermark Alpha Vignette Saturation Brightness Contrast Gif Jpg Jpeg Bitmap Png Fluent GDI Gaussian Blur Sharpen Tint Quantizer Animated</tags>
</metadata>
<files>
<file src="..\..\src\ImageProcessor.Web\NET45\Config\Resources\cache.config" target="content\config\imageprocessor\cache.config" />

2
build/NuSpecs/ImageProcessor.Web.nuspec

@ -22,7 +22,7 @@ Feedback is always welcome</description>
<releaseNotes />
<copyright>James South</copyright>
<language>en-GB</language>
<tags>Image Imaging ASP Performance Processing HttpModule Cache Resize Rotate RoundedCorners Flip Crop Filter Effects Quality Watermark Alpha Vignette Saturation Brightness Contrast Gif Jpg Jpeg Bitmap Png Fluent GDI Gaussian Blur Sharpen</tags>
<tags>Image Imaging ASP Performance Processing HttpModule Cache Resize Rotate RoundedCorners Flip Crop Filter Effects Quality Watermark Alpha Vignette Saturation Brightness Contrast Gif Jpg Jpeg Bitmap Png Fluent GDI Gaussian Blur Sharpen Tint Quantizer Animated</tags>
<dependencies>
<group targetFramework=".NETFramework4.0">
<dependency id="Microsoft.Bcl.Async" version="1.0.168" />

2
build/NuSpecs/ImageProcessor.nuspec

@ -21,7 +21,7 @@ Feedback is always welcome.</description>
<releaseNotes />
<copyright>James South</copyright>
<language>en-GB</language>
<tags>Image Imaging ASP Performance Processing Resize Rotate RoundedCorners Flip Crop Filter Effects Quality Watermark Alpha Vignette Saturation Brightness Contrast Gif Jpg Jpeg Bitmap Png Fluent GDI Gaussian Blur Sharpen</tags>
<tags>Image Imaging ASP Performance Processing Resize Rotate RoundedCorners Flip Crop Filter Effects Quality Watermark Alpha Vignette Saturation Brightness Contrast Gif Jpg Jpeg Bitmap Png Fluent GDI Gaussian Blur Sharpen Tint Quantizer Animated</tags>
</metadata>
<files>
<file src="..\_BuildOutput\ImageProcessor\lib\ImageProcessor.dll" target="lib\ImageProcessor.dll" />

26
src/ImageProcessor.Tests/RegularExpressionUnitTests.cs

@ -149,12 +149,12 @@ namespace ImageProcessor.Tests
public void TestResizeRegex()
{
const string Querystring = "width=300";
Size expected = new Size(300, 0);
ResizeLayer expected = new ResizeLayer(new Size(300, 0));
Resize resize = new Resize();
resize.MatchRegexIndex(Querystring);
Size actual = resize.DynamicParameter;
ResizeLayer actual = resize.DynamicParameter;
Assert.AreEqual(expected, actual);
}
@ -192,6 +192,28 @@ namespace ImageProcessor.Tests
Assert.AreEqual(expected, actual);
}
/// <summary>
/// The rounded corners regex unit test.
/// </summary>
[TestMethod]
public void TestTintRegex()
{
const string HexQuerystring = "tint=6aa6cc";
const string RgbaQuerystring = "tint=106,166,204,255";
Color expectedHex = ColorTranslator.FromHtml("#" + "6aa6cc");
Color expectedRgba = Color.FromArgb(255, 106, 166, 204);
Tint tint = new Tint();
tint.MatchRegexIndex(HexQuerystring);
Color actualHex = tint.DynamicParameter;
Assert.AreEqual(expectedHex, actualHex);
tint = new Tint();
tint.MatchRegexIndex(RgbaQuerystring);
Color actualRgba = tint.DynamicParameter;
Assert.AreEqual(expectedRgba, actualRgba);
}
#endregion
}
}

1
src/ImageProcessor.Web/NET45/Config/Resources/processing.config

@ -33,6 +33,7 @@
<plugin name="Rotate" type="ImageProcessor.Processors.Rotate, ImageProcessor"/>
<plugin name="RoundedCorners" type="ImageProcessor.Processors.RoundedCorners, ImageProcessor"/>
<plugin name="Saturation" type="ImageProcessor.Processors.Saturation, ImageProcessor"/>
<plugin name="Tint" type="ImageProcessor.Processors.Tint, ImageProcessor"/>
<plugin name="Vignette" type="ImageProcessor.Processors.Vignette, ImageProcessor"/>
<plugin name="Watermark" type="ImageProcessor.Processors.Watermark, ImageProcessor"/>
</plugins>

20
src/ImageProcessor/ImageFactory.cs

@ -743,6 +743,26 @@ namespace ImageProcessor
return this;
}
/// <summary>
/// Tints the current image with the given color.
/// </summary>
/// <param name="color">
/// The <see cref="T:System.Drawing.Color"/> to tint the image with.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Tint(Color color)
{
if (this.ShouldProcess)
{
Tint tint = new Tint { DynamicParameter = color };
this.ApplyProcessor(tint.ProcessImage);
}
return this;
}
/// <summary>
/// Adds a vignette image effect to the current image.
/// </summary>

1
src/ImageProcessor/ImageProcessor.csproj

@ -111,6 +111,7 @@
<Compile Include="Processors\Quality.cs" />
<Compile Include="Processors\Resize.cs" />
<Compile Include="Processors\Format.cs" />
<Compile Include="Processors\Tint.cs" />
<Compile Include="Processors\Vignette.cs" />
<Compile Include="Processors\Watermark.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

183
src/ImageProcessor/Processors/Tint.cs

@ -0,0 +1,183 @@
// --------------------------------------------------------------------------------------------------------------------
// <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 colour.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Processors
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
using ImageProcessor.Extensions;
/// <summary>
/// Tints an image with the given colour.
/// </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>
/// <param name="factory">
/// The 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)
{
Bitmap newImage = null;
Image image = factory.Image;
try
{
Color tintColour = (Color)this.DynamicParameter;
float[][] colorMatrixElements =
{
new[] { tintColour.R / 255f, 0, 0, 0, 0 }, // Red
new[] { 0, tintColour.G / 255f, 0, 0, 0 }, // Green
new[] { 0, 0, tintColour.B / 255f, 0, 0 }, // Blue
new[] { 0, 0, 0, tintColour.A / 255f, 0 }, // Alpha
new float[] { 0, 0, 0, 0, 1 }
};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
newImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb);
using (Graphics graphics = Graphics.FromImage(newImage))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
using (ImageAttributes attributes = new ImageAttributes())
{
attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
image.Dispose();
image = newImage;
}
}
return image;
}
catch
{
if (newImage != null)
{
newImage.Dispose();
}
}
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;
}
}
}

2
src/ImageProcessorConsole/Program.cs

@ -43,7 +43,7 @@ namespace ImageProcessorConsole
// Load, resize, set the format and quality and save an image.
imageFactory.Load(inStream)
.Constrain(size)
.Filter(MatrixFilters.Comic)
.Tint(Color.FromArgb(255, 106, 166, 204))
.Format(format)
.Save(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\output", fileInfo.Name)));
}

2
src/ImageProcessorConsole/images/output/120430.gif.REMOVED.git-id

@ -1 +1 @@
7880376ac9108d4e74412efb31f544484079ce16
17e154964bfb4da80c1e0aec623cd2486d493b47

1
src/ImageProcessorConsole/images/output/nLpfllv .gif.REMOVED.git-id

@ -1 +0,0 @@
673e6c6d9223e0906fe342a17496168b8d9017fb

1
src/ImageProcessorConsole/images/output/nLpfllv.gif.REMOVED.git-id

@ -0,0 +1 @@
069f8472ed7b83ea57f4cf511f83396e1a0b8877

12
src/TestWebsites/NET45/Test_Website_NET45/Views/Home/Index.cshtml

@ -186,6 +186,18 @@
</div>
</div>
</section>
<section>
<div class="row">
<div class="col-s-6">
<h2>Tint rgba</h2>
<img src="/images/Penguins.jpg?width=300&tint=175,118,166,255" />
</div>
<div class="col-s-6">
<h2>Tint Hex</h2>
<img src="/images/Penguins.jpg?width=300&tint=af76a6" />
</div>
</div>
</section>
</article>
<article>
<h1>Color Profiles</h1>

1
src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config

@ -36,6 +36,7 @@
<plugin name="Rotate" type="ImageProcessor.Processors.Rotate, ImageProcessor"/>
<plugin name="RoundedCorners" type="ImageProcessor.Processors.RoundedCorners, ImageProcessor"/>
<plugin name="Saturation" type="ImageProcessor.Processors.Saturation, ImageProcessor"/>
<plugin name="Tint" type="ImageProcessor.Processors.Tint, ImageProcessor"/>
<plugin name="Vignette" type="ImageProcessor.Processors.Vignette, ImageProcessor"/>
<plugin name="Watermark" type="ImageProcessor.Processors.Watermark, ImageProcessor"/>
</plugins>

Loading…
Cancel
Save