Skip to content

Instantly share code, notes, and snippets.

@aensidhe
Created December 21, 2017 22:09
Show Gist options
  • Save aensidhe/bd447d4d9d2f511ddb5b2092ab4efc94 to your computer and use it in GitHub Desktop.
Save aensidhe/bd447d4d9d2f511ddb5b2092ab4efc94 to your computer and use it in GitHub Desktop.
Trying to write a wrapper to ImageMagick
var histogram = GetImageHistogram(this.wand, out var lenPtr);
var len = lenPtr.ToUInt32();
var result = new Dictionary<MagickColor, int>();
try
{
for (var i = 0; i < len; i++)
{
var ptr = histogram + i * IntPtr.Size;
Console.WriteLine($"{i} iteration, ptr: {histogram}, cur: {ptr}");
// PixelGetRed fails with assert in native code:
// MagickWand/pixel-wand.c:1367: PixelGetRed: Assertion `wand->signature == MagickWandSignature' failed.
var color = new MagickColor(
PixelGetRed(ptr),
PixelGetGreen(ptr),
PixelGetBlue(ptr),
PixelGetAlpha(ptr));
result[color] = (int) PixelGetColorCount(ptr).ToUInt32();
}
return result;
}
finally
{
DestroyPixelWands(histogram, len);
}
// PixelWand **MagickGetImageHistogram(MagickWand *wand, size_t* number_colors)
[DllImport(libPath, EntryPoint = "MagickGetImageHistogram", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetImageHistogram(IntPtr wand, [Out] out UIntPtr numberColors);
// PixelWand **DestroyPixelWands(PixelWand** wand, const size_t number_wands)
[DllImport(libPath, EntryPoint = "DestroyPixelWands", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr DestroyPixelWands(IntPtr pixelWands, [Out] uint numberWands);
// double PixelGetRed(const PixelWand* wand)
[DllImport(libPath, EntryPoint = "PixelGetRed", CallingConvention = CallingConvention.Cdecl)]
public static extern float PixelGetRed(IntPtr pixelWand);
// double PixelGetGreen(const PixelWand* wand)
[DllImport(libPath, EntryPoint = "PixelGetGreen", CallingConvention = CallingConvention.Cdecl)]
public static extern float PixelGetGreen(IntPtr pixelWand);
// double PixelGetBlue(const PixelWand* wand)
[DllImport(libPath, EntryPoint = "PixelGetBlue", CallingConvention = CallingConvention.Cdecl)]
public static extern float PixelGetBlue(IntPtr pixelWand);
// double PixelGetAlpha(const PixelWand* wand)
[DllImport(libPath, EntryPoint = "PixelGetAlpha", CallingConvention = CallingConvention.Cdecl)]
public static extern float PixelGetAlpha(IntPtr pixelWand);
// size_t PixelGetColorCount(const PixelWand *wand)
[DllImport(libPath, EntryPoint = "PixelGetColorCount", CallingConvention = CallingConvention.Cdecl)]
public static extern UIntPtr PixelGetColorCount(IntPtr pixelWand);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment