Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
More code + resizing and properties.
  • Loading branch information
karamanolev committed Sep 13, 2012
1 parent ae527c5 commit c8e6b44
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 18 deletions.
40 changes: 40 additions & 0 deletions NImageMagick/FilterType.cs
@@ -0,0 +1,40 @@
using System;
using System.Linq;

namespace NImageMagick
{
public enum FilterType
{
UndefinedFilter,
PointFilter,
BoxFilter,
TriangleFilter,
HermiteFilter,
HanningFilter,
HammingFilter,
BlackmanFilter,
GaussianFilter,
QuadraticFilter,
CubicFilter,
CatromFilter,
MitchellFilter,
JincFilter,
SincFilter,
SincFastFilter,
KaiserFilter,
WelshFilter,
ParzenFilter,
BohmanFilter,
BartlettFilter,
LagrangeFilter,
LanczosFilter,
LanczosSharpFilter,
Lanczos2Filter,
Lanczos2SharpFilter,
RobidouxFilter,
RobidouxSharpFilter,
CosineFilter,
SplineFilter,
SentinelFilter /* a count of all the filters, not a real filter */
}
}
77 changes: 65 additions & 12 deletions NImageMagick/Image.cs
@@ -1,34 +1,87 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;

namespace NImageMagick
{
public class Image
public class Image : IDisposable
{
private IntPtr handle;
public IntPtr Handle { get; private set; }

public IntPtr Handle
public int ImageCompressionQuality
{
get { return this.handle; }
get
{
return (int)ImageMagick.MagickGetImageCompressionQuality(this.Handle);
}
set
{
this.ExecuteChecked(ImageMagick.MagickSetImageCompressionQuality, (uint)value);
}
}

public int Width
{
get
{
return (int)ImageMagick.MagickGetImageWidth(this.Handle);
}
}

public int Height
{
get
{
return (int)ImageMagick.MagickGetImageHeight(this.Handle);
}
}

public Image(string path)
{
ImageMagick.EnsureInitialized();
handle = ImageMagick.NewMagickWand();
if (handle == IntPtr.Zero)
Handle = ImageMagick.NewMagickWand();
if (Handle == IntPtr.Zero)
{
throw new Exception("Error acquiring wand.");
}
IntPtr str = ImageMagick.AllocateUTF8String(path);
int result = ImageMagick.MagickReadImage(this.handle, str);
ImageMagick.FreeString(str);

if (result == 0)
using (NativeString pathString = new NativeString(path))
{
throw new ImageMagickException(this.handle);
this.ExecuteChecked(ImageMagick.MagickReadImage, pathString.Pointer);
}
}

public void ExecuteChecked<T>(Func<IntPtr, T, int> action, T param1)
{
if (action(this.Handle, param1) != 1)
{
throw new ImageMagickException(this.Handle);
}
}

public void ExecuteChecked<T1, T2, T3, T4>(Func<IntPtr, T1, T2, T3, T4, int> action, T1 param1, T2 param2, T3 param3, T4 param4)
{
if (action(this.Handle, param1, param2, param3, param4) != 1)
{
throw new ImageMagickException(this.Handle);
}
}

public void Save(string path)
{
using (NativeString pathString = new NativeString(path))
{
this.ExecuteChecked(ImageMagick.MagickWriteImage, pathString.Pointer);
}
}

public void Resize(int width, int height, FilterType filterType, double blur)
{
this.ExecuteChecked(ImageMagick.MagickResizeImage, (uint)width, (uint)height, (int)filterType, blur);
}

public void Dispose()
{
this.Handle = ImageMagick.DestroyMagickWand(this.Handle);
}
}
}
20 changes: 19 additions & 1 deletion NImageMagick/ImageMagick.cs
Expand Up @@ -32,7 +32,25 @@ static class ImageMagick
//public static extern int MagickGetExceptionType(IntPtr ptr);

[DllImport(WandDll, CallingConvention = WandConvention)]
public static extern IntPtr MagickGetException(IntPtr ptr, ref int exceptionType);
public static extern IntPtr MagickGetException(IntPtr ptr, out int exceptionType);

[DllImport(WandDll, CallingConvention = WandConvention)]
public static extern int MagickClearException(IntPtr ptr);

[DllImport(WandDll, CallingConvention = WandConvention)]
public static extern uint MagickGetImageCompressionQuality(IntPtr ptr);

[DllImport(WandDll, CallingConvention = WandConvention)]
public static extern int MagickSetImageCompressionQuality(IntPtr ptr, uint quality);

[DllImport(WandDll, CallingConvention = WandConvention)]
public static extern uint MagickGetImageWidth(IntPtr ptr);

[DllImport(WandDll, CallingConvention = WandConvention)]
public static extern uint MagickGetImageHeight(IntPtr ptr);

[DllImport(WandDll, CallingConvention = WandConvention)]
public static extern int MagickResizeImage(IntPtr ptr, uint columns, uint rows, int filterType, double blur);

private static object isInitializedSyncRoot = new object();
private static bool isInitialized = false;
Expand Down
8 changes: 4 additions & 4 deletions NImageMagick/ImageMagickException.cs
@@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;

namespace NImageMagick
{
Expand All @@ -13,9 +12,10 @@ public ImageMagickException(IntPtr wandHandle)

private static string DecodeException(IntPtr wandHandle)
{
int exceptionSeverity = 0;
IntPtr exceptionPtr = ImageMagick.MagickGetException(wandHandle, ref exceptionSeverity);
return Marshal.PtrToStringAnsi(exceptionPtr);
int exceptionSeverity;
IntPtr exceptionPtr = ImageMagick.MagickGetException(wandHandle, out exceptionSeverity);
ImageMagick.MagickClearException(wandHandle);
return NativeString.Load(exceptionPtr);
}
}
}
3 changes: 3 additions & 0 deletions NImageMagick/NImageMagick.csproj
Expand Up @@ -39,8 +39,11 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FilterType.cs" />
<Compile Include="Image.cs" />
<Compile Include="ImageMagick.cs" />
<Compile Include="ImageMagickException.cs" />
<Compile Include="NativeString.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
6 changes: 6 additions & 0 deletions NImageMagick/NImageMagick.csproj.user
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>
19 changes: 19 additions & 0 deletions NImageMagick/NativeString.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand All @@ -23,5 +24,23 @@ public void Dispose()
{
Marshal.FreeHGlobal(this.Pointer);
}

public static string Load(IntPtr pointer)
{
List<byte> bytes = new List<byte>();
byte[] buf = new byte[1];
int index = 0;
while (true)
{
Marshal.Copy(pointer + index, buf, 0, 1);
if (buf[0] == 0)
{
break;
}
bytes.Add(buf[0]);
++index;
}
return Encoding.UTF8.GetString(bytes.ToArray());
}
}
}
4 changes: 3 additions & 1 deletion Test/Program.cs
Expand Up @@ -8,7 +8,9 @@ class Program
{
static void Main(string[] args)
{
var image = new Image(@"C:\test.png");
var image = new Image(@"Z:\Temp\dcd\folder.jpg");
image.Resize(800, 600, FilterType.CubicFilter, 1);
image.Save(@"C:\Temp\test.jpg");
}
}
}

0 comments on commit c8e6b44

Please sign in to comment.