Skip to content

Commit

Permalink
Initial commit. Exported from Bitbucket repo
Browse files Browse the repository at this point in the history
  • Loading branch information
daelsepara committed Sep 27, 2018
1 parent de8e1d2 commit f88adc9
Show file tree
Hide file tree
Showing 35 changed files with 5,358 additions and 1 deletion.
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Autosave files
*~

# build
[Oo]bj/
[Bb]in/
packages/
TestResults/

# globs
Makefile.in
*.DS_Store
*.sln.cache
*.suo
*.cache
*.pidb
*.userprefs
*.usertasks
config.log
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.user
*.tar.gz
tarballs/
test-results/
Thumbs.db
.vs/

# Mac bundle stuff
*.dmg
*.app

# resharper
*_Resharper.*
*.Resharper

# dotCover
*.dotCover
17 changes: 17 additions & 0 deletions PhaseExplorer.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhaseExplorer", "PhaseExplorer\PhaseExplorer.csproj", "{6E96C13B-CAF2-444C-9C11-6BCF2E33C2EB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6E96C13B-CAF2-444C-9C11-6BCF2E33C2EB}.Debug|x86.ActiveCfg = Debug|x86
{6E96C13B-CAF2-444C-9C11-6BCF2E33C2EB}.Debug|x86.Build.0 = Debug|x86
{6E96C13B-CAF2-444C-9C11-6BCF2E33C2EB}.Release|x86.ActiveCfg = Release|x86
{6E96C13B-CAF2-444C-9C11-6BCF2E33C2EB}.Release|x86.Build.0 = Release|x86
EndGlobalSection
EndGlobal
75 changes: 75 additions & 0 deletions PhaseExplorer/BlazedPhaseDLL.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Gdk;
using System;
using System.IO;
using System.Runtime.InteropServices;

public static class BlazedPhaseDLL
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
unsafe delegate double* FPhase();

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate void FRelease();

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
unsafe delegate void FCompute(int argc, void** argv);

static int Width;
static int Height;
static double X;
static double Y;

public static void SetParameters(int width, int height, double x, double y)
{
Width = width;
Height = height;
X = x;
Y = y;
}

unsafe public static PhaseOutput ComputePhase(string dll)
{
var file = Common.OSTest.IsWindows() ? String.Format("./libphase++{0}.dll", dll) : (Common.OSTest.IsRunningOnMac() ? String.Format("./libphase++{0}.dylib", dll) : String.Format("./libphase++{0}.so", dll));

if (File.Exists(file))
{
IntPtr pLibrary = DLLLoader.LoadLibrary(file);
IntPtr pCompute = DLLLoader.GetProcAddress(pLibrary, "Compute");
IntPtr pPhase = DLLLoader.GetProcAddress(pLibrary, "Phase");
IntPtr pRelease = DLLLoader.GetProcAddress(pLibrary, "Release");

var Compute = DLLLoader.LoadFunction<FCompute>(pCompute);
var Phase = DLLLoader.LoadFunction<FPhase>(pPhase);
var Release = DLLLoader.LoadFunction<FRelease>(pRelease);

void** Parameters = stackalloc void*[4];

var width = Width;
var height = Height;
var x = X;
var y = Y;

Parameters[0] = &width;
Parameters[1] = &height;
Parameters[2] = &x;
Parameters[3] = &y;

Compute(4, Parameters);

var output = Phase();

var phase = new PhaseOutput(output, width * height);

// Free resources
Release();

DLLLoader.FreeLibrary(pLibrary);

return phase;
}

var length = Width * Height > 0 ? Width * Height : 256 * 256;

return new PhaseOutput(length);
}
}
208 changes: 208 additions & 0 deletions PhaseExplorer/Common.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
using Gdk;
using System;
using System.Runtime.InteropServices;

public static class Common
{
// see: https://github.com/jpobst/Pinta/blob/1.6/Pinta.Core/Managers/SystemManager.cs#L125
public static class OSTest
{
[DllImport("libc", EntryPoint = "uname")]
static extern int Uname(IntPtr buf);

public static bool IsWindows()
{
var isWindows = false;

switch (Environment.OSVersion.Platform)
{
case PlatformID.Win32NT:
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.WinCE:
isWindows = true;

break;
}

return isWindows;
}

public static bool IsRunningOnMac()
{
IntPtr buf = IntPtr.Zero;
try
{
buf = Marshal.AllocHGlobal(8192);
// This is a hacktastic way of getting sysname from uname ()
if (Uname(buf) == 0)
{
string os = Marshal.PtrToStringAnsi(buf);

if (os == "Darwin")
return true;
}
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
finally
{
if (buf != IntPtr.Zero)
Marshal.FreeHGlobal(buf);
}

return false;
}
}

public static Pixbuf InitializePixbuf(int width, int height)
{
var pixbuf = new Pixbuf(Colorspace.Rgb, false, 8, width, height);

pixbuf.Fill(0);

return pixbuf;
}

unsafe public static byte* PreparePixbuf(Pixbuf input)
{
var src = InitializePixbuf(input.Width, input.Height);

input.Composite(src, 0, 0, input.Width, input.Height, 0, 0, 1, 1, InterpType.Nearest, 255);

var Channels = 3;

var temp = (byte*)Marshal.AllocHGlobal(src.Width * src.Height * Channels);

for (var y = 0; y < src.Height; y++)
{
for (var x = 0; x < src.Width; x++)
{
var ptr = src.Pixels + y * src.Rowstride + x * src.NChannels;

for (var offset = 0; offset < src.NChannels; offset++)
{
temp[(y * src.Width + x) * Channels + offset] = Marshal.ReadByte(ptr, offset);
}
}
}

Free(src);

return temp;
}

unsafe public static Pixbuf PreparePixbuf(double* src, int srcx, int srcy)
{
var dst = InitializePixbuf(srcx, srcy);

for (var y = 0; y < dst.Height; y++)
{
for (var x = 0; x < dst.Width; x++)
{
var ptr = dst.Pixels + y * dst.Rowstride + x * dst.NChannels;
var c = (byte)(255 * src[y * srcx + x] / (2.0 * Math.PI));

for (var offset = 0; offset < dst.NChannels; offset++)
{
Marshal.WriteByte(ptr, offset, c);
}
}
}

return dst;
}

unsafe public static Pixbuf Intensity(double* src, int srcx, int srcy)
{
var dst = InitializePixbuf(srcx, srcy);

for (var y = 0; y < dst.Height; y++)
{
for (var x = 0; x < dst.Width; x++)
{
var ptr = dst.Pixels + y * dst.Rowstride + x * dst.NChannels;
var c = (byte)(255 * src[y * srcx + x]);

for (var offset = 0; offset < dst.NChannels; offset++)
{
Marshal.WriteByte(ptr, offset, c);
}
}
}

return dst;
}

unsafe public static void Copy(Pixbuf dst, byte* src)
{
if (dst != null)
{
for (var y = 0; y < dst.Height; y++)
{
for (var x = 0; x < dst.Width; x++)
{
var ptr = dst.Pixels + y * dst.Rowstride + x * dst.NChannels;

for (var offset = 0; offset < dst.NChannels; offset++)
{
Marshal.WriteByte(ptr, offset, src[(y * dst.Width + x) * dst.NChannels + offset]);
}
}
}
}
}

// see: https://www.johndcook.com/blog/csharp_erf/
public static double Erf(double x)
{
// constants
double a1 = 0.254829592;
double a2 = -0.284496736;
double a3 = 1.421413741;
double a4 = -1.453152027;
double a5 = 1.061405429;
double p = 0.3275911;

// Save the sign of x
int sign = 1;
if (x < 0)
sign = -1;
x = Math.Abs(x);

// A&S formula 7.1.26
double t = 1.0 / (1.0 + p * x);
double y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.Exp(-x * x);

return sign * y;
}

public static double Mod(double a, double m)
{
return a - m * Math.Floor(a / m);
}

public static void Free(params IDisposable[] trash)
{
foreach (var item in trash)
{
if (item != null)
{
item.Dispose();
}
}
}

unsafe public static void Free(params byte*[] trash)
{
foreach (var item in trash)
{
if (item != null)
{
Marshal.FreeHGlobal((IntPtr)item);
}
}
}
}
Loading

0 comments on commit f88adc9

Please sign in to comment.