mirror of https://github.com/SixLabors/ImageSharp
13 changed files with 212 additions and 68 deletions
@ -1,54 +0,0 @@ |
|||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|
||||
<PropertyGroup> |
|
||||
<MSBuildCommunityTasksPath>.\</MSBuildCommunityTasksPath> |
|
||||
</PropertyGroup> |
|
||||
|
|
||||
<Import Project=".\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" /> |
|
||||
|
|
||||
<!-- |
|
||||
**************************************************** |
|
||||
VARIABLES |
|
||||
***************************************************** |
|
||||
--> |
|
||||
|
|
||||
<PropertyGroup> |
|
||||
<BuildConfiguration>Release</BuildConfiguration> |
|
||||
<BuildFolder>_BuildOutput\</BuildFolder> |
|
||||
<IncludeSymbols>False</IncludeSymbols> |
|
||||
<BuildFolderAbsolutePath>$(MSBuildProjectDirectory)\$(BuildFolder)</BuildFolderAbsolutePath> |
|
||||
<SolutionBinFolderAbsolutePath>$(BuildFolderAbsolutePath)ImageProcessor.Plugins.Cair\lib\net45</SolutionBinFolderAbsolutePath> |
|
||||
<BuildInputDir>..\src\Plugins\ImageProcessor\ImageProcessor.Plugins.Cair\</BuildInputDir> |
|
||||
</PropertyGroup> |
|
||||
|
|
||||
<!-- |
|
||||
**************************************************** |
|
||||
TARGETS |
|
||||
***************************************************** |
|
||||
--> |
|
||||
|
|
||||
<Target Name="Build" DependsOnTargets="BuildImageProcessorPluginsCair"> |
|
||||
<Message Text="Build finished" /> |
|
||||
</Target> |
|
||||
|
|
||||
<Target Name="BuildImageProcessorPluginsCair" DependsOnTargets="SetVersionNumber"> |
|
||||
<Message Text="Compiling ImageProcessor.Plugins.Cair project to build\$(BuildFolder)" Importance="High" /> |
|
||||
|
|
||||
<MSBuild Projects="$(BuildInputDir)\ImageProcessor.Plugins.Cair.csproj" Properties="WarningLevel=0;Configuration=$(BuildConfiguration);PipelineDependsOnBuild=False;OutDir=$(SolutionBinFolderAbsolutePath);" Targets="Clean;Rebuild;" BuildInParallel="False" ToolsVersion="4.0" UnloadProjectsOnCompletion="False" /> |
|
||||
|
|
||||
<Message Text="Finished compiling project" Importance="High" /> |
|
||||
</Target> |
|
||||
|
|
||||
<Target Name="SetVersionNumber" Condition="'$(BUILD_RELEASE)'!=''"> |
|
||||
<Message Text="Creating Version File: $(BUILD_RELEASE)"/> |
|
||||
<ItemGroup> |
|
||||
<AssemblyFiles Include="$(BuildInputDir)Properties\AssemblyInfo.cs;" /> |
|
||||
</ItemGroup> |
|
||||
|
|
||||
<FileUpdate Files="@(AssemblyFiles)" |
|
||||
Multiline="true" |
|
||||
Singleline="false" |
|
||||
Regex="(AssemblyVersion|AssemblyFileVersionAttribute|AssemblyFileVersion)\("([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)?"\)" |
|
||||
ReplacementText="$1("$(BUILD_RELEASE)")" /> |
|
||||
|
|
||||
</Target> |
|
||||
</Project> |
|
||||
@ -0,0 +1,66 @@ |
|||||
|
|
||||
|
namespace ImageProcessor.Imaging.EdgeDetection |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Drawing; |
||||
|
using System.Drawing.Imaging; |
||||
|
|
||||
|
using ImageProcessor.Common.Extensions; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// http://pastebin.com/xHHD3pXi
|
||||
|
/// </summary>
|
||||
|
public class ConvolutionFilter |
||||
|
{ |
||||
|
private readonly IEdgeFilter edgeFilter; |
||||
|
|
||||
|
public ConvolutionFilter(IEdgeFilter edgeFilter) |
||||
|
{ |
||||
|
this.edgeFilter = edgeFilter; |
||||
|
} |
||||
|
|
||||
|
public Bitmap ProcessFilter(Bitmap source) |
||||
|
{ |
||||
|
double[,] horizontalFilter = this.edgeFilter.HorizontalMatrix; |
||||
|
double[,] verticallFilter = this.edgeFilter.VerticalMatrix; |
||||
|
|
||||
|
int width = source.Width; |
||||
|
int height = source.Height; |
||||
|
int maxWidth = width - 1; |
||||
|
int maxHeight = height - 1; |
||||
|
|
||||
|
Bitmap destination = new Bitmap(width, height, PixelFormat.Format32bppArgb); |
||||
|
using (FastBitmap sourceBitmap = new FastBitmap(source)) |
||||
|
{ |
||||
|
using (FastBitmap destinationBitmap = new FastBitmap(destination)) |
||||
|
{ |
||||
|
for (int y = 1; y < maxHeight; y++) |
||||
|
{ |
||||
|
for (int x = 1; x < maxWidth; x++) |
||||
|
{ |
||||
|
double newX = 0; |
||||
|
double newY = 0; |
||||
|
|
||||
|
for (int hw = -1; hw < 2; hw++) |
||||
|
{ |
||||
|
for (int wi = -1; wi < 2; wi++) |
||||
|
{ |
||||
|
double component = sourceBitmap.GetPixel(x + wi, y + hw).B; |
||||
|
newX += horizontalFilter[hw + 1, wi + 1] * component; |
||||
|
newY += verticallFilter[hw + 1, wi + 1] * component; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
byte value = Math.Sqrt((newX * newX) + (newY * newY)).ToByte(); |
||||
|
Color tempcolor = Color.FromArgb(value, value, value); |
||||
|
destinationBitmap.SetPixel(x, y, tempcolor); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return destination; |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
namespace ImageProcessor.Imaging.EdgeDetection |
||||
|
{ |
||||
|
public interface IEdgeFilter |
||||
|
{ |
||||
|
double[,] HorizontalMatrix { get; } |
||||
|
|
||||
|
double[,] VerticalMatrix { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
namespace ImageProcessor.Imaging.EdgeDetection |
||||
|
{ |
||||
|
public class SobelEdgeFilter : IEdgeFilter |
||||
|
{ |
||||
|
public double[,] HorizontalMatrix |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
return new double[,] |
||||
|
{ { -1, 0, 1, }, |
||||
|
{ -2, 0, 2, }, |
||||
|
{ -1, 0, 1, }, }; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public double[,] VerticalMatrix |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
return new double[,] |
||||
|
{ { 1, 2, 1, }, |
||||
|
{ 0, 0, 0, }, |
||||
|
{ -1, -2, -1, }, }; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,78 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace ImageProcessor.Processors |
||||
|
{ |
||||
|
using System.Drawing; |
||||
|
|
||||
|
using ImageProcessor.Common.Exceptions; |
||||
|
using ImageProcessor.Imaging.EdgeDetection; |
||||
|
|
||||
|
public class DetectEdges : IGraphicsProcessor |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="DetectEdges"/> class.
|
||||
|
/// </summary>
|
||||
|
public DetectEdges() |
||||
|
{ |
||||
|
this.Settings = new Dictionary<string, string>(); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the dynamic parameter.
|
||||
|
/// </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) |
||||
|
{ |
||||
|
Bitmap newImage = null; |
||||
|
Image image = factory.Image; |
||||
|
IEdgeFilter filter = this.DynamicParameter; |
||||
|
try |
||||
|
{ |
||||
|
ConvolutionFilter convolutionFilter = new ConvolutionFilter(filter); |
||||
|
newImage = convolutionFilter.ProcessFilter((Bitmap)image); |
||||
|
|
||||
|
image.Dispose(); |
||||
|
image = newImage; |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
if (newImage != null) |
||||
|
{ |
||||
|
newImage.Dispose(); |
||||
|
} |
||||
|
|
||||
|
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); |
||||
|
} |
||||
|
|
||||
|
return image; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue