Browse Source

More refactoring

Former-commit-id: cbd2713de7d6378edc260d6f35f50dd106e39e5a
af/merge-core
James South 12 years ago
parent
commit
b725f4ea49
  1. 39
      src/ImageProcessor.Web/NET45/Helpers/ImageHelpers.cs
  2. 36
      src/ImageProcessor/Extensions/DoubleExtensions.cs
  3. 36
      src/ImageProcessor/Extensions/IntegerExtensions.cs
  4. 3
      src/ImageProcessor/ImageProcessor.csproj
  5. 2
      src/ImageProcessor/Imaging/ColorQuantizer.cs
  6. 9
      src/ImageProcessor/Imaging/Convolution.cs
  7. 25
      src/ImageProcessor/Imaging/Filters/ComicMatrixFilter.cs
  8. 24
      src/ImageProcessor/Processors/Resize.cs
  9. 3
      src/TestWebsites/NET45/Test_Website_NET45/Images/circle.png
  10. 2
      src/TestWebsites/NET45/Test_Website_NET45/Web.config
  11. 2
      src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config

39
src/ImageProcessor.Web/NET45/Helpers/ImageHelpers.cs

@ -11,8 +11,6 @@
namespace ImageProcessor.Web.Helpers
{
#region Using
using System.Drawing.Imaging;
using System.IO;
using System.Text.RegularExpressions;
#endregion
@ -53,44 +51,7 @@ namespace ImageProcessor.Web.Helpers
public static string GetExtension(string input)
{
Match match = FormatRegex.Matches(input)[0];
return match.Success ? match.Value : string.Empty;
}
/// <summary>
/// Returns the correct image format based on the given file extension.
/// </summary>
/// <param name="fileName">The string containing the filename to check against.</param>
/// <returns>The correct image format based on the given filename.</returns>
//public static ImageFormat GetImageFormat(string fileName)
//{
// string extension = Path.GetExtension(fileName);
// if (extension != null)
// {
// string ext = extension.ToUpperInvariant();
// switch (ext)
// {
// case ".ICO":
// return ImageFormat.Icon;
// case ".PNG":
// return ImageFormat.Png;
// case ".BMP":
// return ImageFormat.Bmp;
// case ".GIF":
// return ImageFormat.Gif;
// case ".TIF":
// case ".TIFF":
// return ImageFormat.Tiff;
// default:
// // Should be a jpeg.
// return ImageFormat.Jpeg;
// }
// }
// // TODO: Show custom exception?
// return null;
//}
}
}

36
src/ImageProcessor/Extensions/DoubleExtensions.cs

@ -0,0 +1,36 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DoubleExtensions.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates a series of time saving extension methods to the <see cref="T:System.Double" /> class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Extensions
{
/// <summary>
/// Encapsulates a series of time saving extension methods to the <see cref="T:System.Double"/> class.
/// </summary>
public static class DoubleExtensions
{
/// <summary>
/// Converts an <see cref="T:System.Double"/> value into a valid <see cref="T:System.Byte"/>.
/// <remarks>
/// If the value given is less than 0 or greater than 255, the value will be constrained into
/// those restricted ranges.
/// </remarks>
/// </summary>
/// <param name="d">
/// The <see cref="T:System.Double"/> to convert.
/// </param>
/// <returns>
/// The <see cref="T:System.Byte"/>.
/// </returns>
public static byte ToByte(this double d)
{
return (byte)((d > byte.MaxValue) ? byte.MaxValue : ((d < byte.MinValue) ? byte.MinValue : d));
}
}
}

36
src/ImageProcessor/Extensions/IntegerExtensions.cs

@ -0,0 +1,36 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IntegerExtensions.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates a series of time saving extension methods to the <see cref="T:System.Int32" /> class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Extensions
{
/// <summary>
/// Encapsulates a series of time saving extension methods to the <see cref="T:System.Int32"/> class.
/// </summary>
public static class IntegerExtensions
{
/// <summary>
/// Converts an <see cref="T:System.Int32"/> value into a valid <see cref="T:System.Byte"/>.
/// <remarks>
/// If the value given is less than 0 or greater than 255, the value will be constrained into
/// those restricted ranges.
/// </remarks>
/// </summary>
/// <param name="integer">
/// The <see cref="T:System.Int32"/> to convert.
/// </param>
/// <returns>
/// The <see cref="T:System.Byte"/>.
/// </returns>
public static byte ToByte(this int integer)
{
return ((double)integer).ToByte();
}
}
}

3
src/ImageProcessor/ImageProcessor.csproj

@ -59,8 +59,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Extensions\EnumExtensions.cs" />
<Compile Include="Extensions\DoubleExtensions.cs" />
<Compile Include="Extensions\ImageFormatExtensions.cs" />
<Compile Include="Extensions\IntegerExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="ImageFactory.cs" />
<Compile Include="Imaging\AnchorPosition.cs" />

2
src/ImageProcessor/Imaging/ColorQuantizer.cs

@ -56,7 +56,7 @@ namespace ImageProcessor.Imaging
int height = image.Height;
Rectangle sourceRect = Rectangle.FromLTRB(0, 0, width, height);
// create a 24-bit rgb version of the source image
// Create a 24-bit rgb version of the source image
using (Bitmap bitmapSource = new Bitmap(width, height, PixelFormat.Format32bppArgb))
{
using (Graphics grfx = Graphics.FromImage(bitmapSource))

9
src/ImageProcessor/Imaging/Convolution.cs

@ -14,6 +14,7 @@ namespace ImageProcessor.Imaging
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using ImageProcessor.Extensions;
/// <summary>
/// Provides methods for applying blurring and sharpening effects to an image..
@ -383,10 +384,10 @@ namespace ImageProcessor.Imaging
blue += this.Threshold;
alpha += this.Threshold;
resultBuffer[byteOffset] = (byte)((blue > 255) ? 255 : ((blue < 0) ? 0 : blue));
resultBuffer[byteOffset + 1] = (byte)((green > 255) ? 255 : ((green < 0) ? 0 : green));
resultBuffer[byteOffset + 2] = (byte)((red > 255) ? 255 : ((red < 0) ? 0 : red));
resultBuffer[byteOffset + 3] = (byte)((alpha > 255) ? 255 : ((alpha < 0) ? 0 : alpha));
resultBuffer[byteOffset] = blue.ToByte(); // (byte)((blue > 255) ? 255 : ((blue < 0) ? 0 : blue));
resultBuffer[byteOffset + 1] = green.ToByte(); // (byte)((green > 255) ? 255 : ((green < 0) ? 0 : green));
resultBuffer[byteOffset + 2] = red.ToByte(); // (byte)((red > 255) ? 255 : ((red < 0) ? 0 : red));
resultBuffer[byteOffset + 3] = alpha.ToByte(); // (byte)((alpha > 255) ? 255 : ((alpha < 0) ? 0 : alpha));
}
}

25
src/ImageProcessor/Imaging/Filters/ComicMatrixFilter.cs

@ -17,6 +17,9 @@ namespace ImageProcessor.Imaging.Filters
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using ImageProcessor.Extensions;
#endregion
/// <summary>
@ -307,14 +310,10 @@ namespace ImageProcessor.Imaging.Filters
double green = Math.Abs(greenBin[maxIndex] / maxIntensity);
double red = Math.Abs(redBin[maxIndex] / maxIntensity);
blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);
green = green > 255 ? 255 : (green < 0 ? 0 : green);
red = red > 255 ? 255 : (red < 0 ? 0 : red);
resultBuffer[byteOffset] = (byte)blue;
resultBuffer[byteOffset + 1] = (byte)green;
resultBuffer[byteOffset + 2] = (byte)red;
resultBuffer[byteOffset + 3] = 255;
resultBuffer[byteOffset] = blue.ToByte();
resultBuffer[byteOffset + 1] = green.ToByte();
resultBuffer[byteOffset + 2] = red.ToByte();
resultBuffer[byteOffset + 3] = pixelBuffer[byteOffset + 3];
}
}
@ -333,7 +332,7 @@ namespace ImageProcessor.Imaging.Filters
/// <summary>
/// Detects and draws edges.
/// TODO: Move this to another class and do move edge detection.
/// TODO: Move this to another class and do edge detection.
/// </summary>
/// <param name="sourceBitmap">
/// The source bitmap.
@ -494,7 +493,7 @@ namespace ImageProcessor.Imaging.Filters
}
else
{
// These would normally be used to transfer the correct value accross.
// These would normally be used to transfer the correct value across.
// blue = pixelBuffer[byteOffset];
// green = pixelBuffer[byteOffset + 1];
// red = pixelBuffer[byteOffset + 2];
@ -581,7 +580,11 @@ namespace ImageProcessor.Imaging.Filters
for (int i = rectangle.Height * rectangle.Width; i > 0; i--)
{
// Copy the alpha values across.
destinationRgbValues[d] = sourceRgbValues[s];
if (destinationRgbValues[d] != 0)
{
destinationRgbValues[d] = sourceRgbValues[s];
}
d += 4;
s += 4;
}

24
src/ImageProcessor/Processors/Resize.cs

@ -33,7 +33,7 @@ namespace ImageProcessor.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"((width|height)=\d+)|(mode=(pad|stretch|crop|max))|(anchor=(top|bottom|left|right|center))|(bgcolor=([0-9a-fA-F]{3}){1,2})|(upscale=false)", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"((width|height)=\d+)|(mode=(pad|stretch|crop|max))|(anchor=(top|bottom|left|right|center))|(bgcolor=(transparent|\d+,\d+,\d+,\d+|([0-9a-fA-F]{3}){1,2}))|(upscale=false)", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the size attribute.
@ -53,7 +53,7 @@ namespace ImageProcessor.Processors
/// <summary>
/// The regular expression to search strings for the color attribute.
/// </summary>
private static readonly Regex ColorRegex = new Regex(@"bgcolor=([0-9a-fA-F]{3}){1,2}", RegexOptions.Compiled);
private static readonly Regex ColorRegex = new Regex(@"bgcolor=(transparent|\d+,\d+,\d+,\d+|([0-9a-fA-F]{3}){1,2})", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the upscale attribute.
@ -530,8 +530,26 @@ namespace ImageProcessor.Processors
{
foreach (Match match in ColorRegex.Matches(input))
{
string value = match.Value.Split('=')[1];
if (value == "transparent")
{
return Color.Transparent;
}
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("#" + match.Value.Split('=')[1]);
return ColorTranslator.FromHtml("#" + value);
}
return Color.Transparent;

3
src/TestWebsites/NET45/Test_Website_NET45/Images/circle.png

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:3df296a3fd58930899d308558b128db760879cb776afba8f2d6511aba3934dd0
size 6957

2
src/TestWebsites/NET45/Test_Website_NET45/Web.config

@ -16,7 +16,7 @@
<security configSource="config\imageprocessor\security.config"/>
<cache configSource="config\imageprocessor\cache.config"/>
<processing configSource="config\imageprocessor\processing.config"/>
</imageProcessor>
</imageProcessor>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />

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

@ -23,7 +23,7 @@
</plugin>
<plugin name="Preset">
<settings>
<setting key="demo" value="width=300&#038;height=150&#038;format=png"/>
<setting key="demo" value="width=300&#038;height=150&#038;&#038;bgcolor=transparent"/>
</settings>
</plugin>
</plugins>

Loading…
Cancel
Save