Skip to content

Commit f88adc9

Browse files
committed
Initial commit. Exported from Bitbucket repo
1 parent de8e1d2 commit f88adc9

35 files changed

+5358
-1
lines changed

.gitignore

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Autosave files
2+
*~
3+
4+
# build
5+
[Oo]bj/
6+
[Bb]in/
7+
packages/
8+
TestResults/
9+
10+
# globs
11+
Makefile.in
12+
*.DS_Store
13+
*.sln.cache
14+
*.suo
15+
*.cache
16+
*.pidb
17+
*.userprefs
18+
*.usertasks
19+
config.log
20+
config.make
21+
config.status
22+
aclocal.m4
23+
install-sh
24+
autom4te.cache/
25+
*.user
26+
*.tar.gz
27+
tarballs/
28+
test-results/
29+
Thumbs.db
30+
.vs/
31+
32+
# Mac bundle stuff
33+
*.dmg
34+
*.app
35+
36+
# resharper
37+
*_Resharper.*
38+
*.Resharper
39+
40+
# dotCover
41+
*.dotCover

PhaseExplorer.sln

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2012
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhaseExplorer", "PhaseExplorer\PhaseExplorer.csproj", "{6E96C13B-CAF2-444C-9C11-6BCF2E33C2EB}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|x86 = Debug|x86
9+
Release|x86 = Release|x86
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{6E96C13B-CAF2-444C-9C11-6BCF2E33C2EB}.Debug|x86.ActiveCfg = Debug|x86
13+
{6E96C13B-CAF2-444C-9C11-6BCF2E33C2EB}.Debug|x86.Build.0 = Debug|x86
14+
{6E96C13B-CAF2-444C-9C11-6BCF2E33C2EB}.Release|x86.ActiveCfg = Release|x86
15+
{6E96C13B-CAF2-444C-9C11-6BCF2E33C2EB}.Release|x86.Build.0 = Release|x86
16+
EndGlobalSection
17+
EndGlobal

PhaseExplorer/BlazedPhaseDLL.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Gdk;
2+
using System;
3+
using System.IO;
4+
using System.Runtime.InteropServices;
5+
6+
public static class BlazedPhaseDLL
7+
{
8+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
9+
unsafe delegate double* FPhase();
10+
11+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
12+
delegate void FRelease();
13+
14+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
15+
unsafe delegate void FCompute(int argc, void** argv);
16+
17+
static int Width;
18+
static int Height;
19+
static double X;
20+
static double Y;
21+
22+
public static void SetParameters(int width, int height, double x, double y)
23+
{
24+
Width = width;
25+
Height = height;
26+
X = x;
27+
Y = y;
28+
}
29+
30+
unsafe public static PhaseOutput ComputePhase(string dll)
31+
{
32+
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));
33+
34+
if (File.Exists(file))
35+
{
36+
IntPtr pLibrary = DLLLoader.LoadLibrary(file);
37+
IntPtr pCompute = DLLLoader.GetProcAddress(pLibrary, "Compute");
38+
IntPtr pPhase = DLLLoader.GetProcAddress(pLibrary, "Phase");
39+
IntPtr pRelease = DLLLoader.GetProcAddress(pLibrary, "Release");
40+
41+
var Compute = DLLLoader.LoadFunction<FCompute>(pCompute);
42+
var Phase = DLLLoader.LoadFunction<FPhase>(pPhase);
43+
var Release = DLLLoader.LoadFunction<FRelease>(pRelease);
44+
45+
void** Parameters = stackalloc void*[4];
46+
47+
var width = Width;
48+
var height = Height;
49+
var x = X;
50+
var y = Y;
51+
52+
Parameters[0] = &width;
53+
Parameters[1] = &height;
54+
Parameters[2] = &x;
55+
Parameters[3] = &y;
56+
57+
Compute(4, Parameters);
58+
59+
var output = Phase();
60+
61+
var phase = new PhaseOutput(output, width * height);
62+
63+
// Free resources
64+
Release();
65+
66+
DLLLoader.FreeLibrary(pLibrary);
67+
68+
return phase;
69+
}
70+
71+
var length = Width * Height > 0 ? Width * Height : 256 * 256;
72+
73+
return new PhaseOutput(length);
74+
}
75+
}

PhaseExplorer/Common.cs

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
using Gdk;
2+
using System;
3+
using System.Runtime.InteropServices;
4+
5+
public static class Common
6+
{
7+
// see: https://github.com/jpobst/Pinta/blob/1.6/Pinta.Core/Managers/SystemManager.cs#L125
8+
public static class OSTest
9+
{
10+
[DllImport("libc", EntryPoint = "uname")]
11+
static extern int Uname(IntPtr buf);
12+
13+
public static bool IsWindows()
14+
{
15+
var isWindows = false;
16+
17+
switch (Environment.OSVersion.Platform)
18+
{
19+
case PlatformID.Win32NT:
20+
case PlatformID.Win32S:
21+
case PlatformID.Win32Windows:
22+
case PlatformID.WinCE:
23+
isWindows = true;
24+
25+
break;
26+
}
27+
28+
return isWindows;
29+
}
30+
31+
public static bool IsRunningOnMac()
32+
{
33+
IntPtr buf = IntPtr.Zero;
34+
try
35+
{
36+
buf = Marshal.AllocHGlobal(8192);
37+
// This is a hacktastic way of getting sysname from uname ()
38+
if (Uname(buf) == 0)
39+
{
40+
string os = Marshal.PtrToStringAnsi(buf);
41+
42+
if (os == "Darwin")
43+
return true;
44+
}
45+
}
46+
catch (Exception ex)
47+
{
48+
Console.WriteLine("Error: {0}", ex.Message);
49+
}
50+
finally
51+
{
52+
if (buf != IntPtr.Zero)
53+
Marshal.FreeHGlobal(buf);
54+
}
55+
56+
return false;
57+
}
58+
}
59+
60+
public static Pixbuf InitializePixbuf(int width, int height)
61+
{
62+
var pixbuf = new Pixbuf(Colorspace.Rgb, false, 8, width, height);
63+
64+
pixbuf.Fill(0);
65+
66+
return pixbuf;
67+
}
68+
69+
unsafe public static byte* PreparePixbuf(Pixbuf input)
70+
{
71+
var src = InitializePixbuf(input.Width, input.Height);
72+
73+
input.Composite(src, 0, 0, input.Width, input.Height, 0, 0, 1, 1, InterpType.Nearest, 255);
74+
75+
var Channels = 3;
76+
77+
var temp = (byte*)Marshal.AllocHGlobal(src.Width * src.Height * Channels);
78+
79+
for (var y = 0; y < src.Height; y++)
80+
{
81+
for (var x = 0; x < src.Width; x++)
82+
{
83+
var ptr = src.Pixels + y * src.Rowstride + x * src.NChannels;
84+
85+
for (var offset = 0; offset < src.NChannels; offset++)
86+
{
87+
temp[(y * src.Width + x) * Channels + offset] = Marshal.ReadByte(ptr, offset);
88+
}
89+
}
90+
}
91+
92+
Free(src);
93+
94+
return temp;
95+
}
96+
97+
unsafe public static Pixbuf PreparePixbuf(double* src, int srcx, int srcy)
98+
{
99+
var dst = InitializePixbuf(srcx, srcy);
100+
101+
for (var y = 0; y < dst.Height; y++)
102+
{
103+
for (var x = 0; x < dst.Width; x++)
104+
{
105+
var ptr = dst.Pixels + y * dst.Rowstride + x * dst.NChannels;
106+
var c = (byte)(255 * src[y * srcx + x] / (2.0 * Math.PI));
107+
108+
for (var offset = 0; offset < dst.NChannels; offset++)
109+
{
110+
Marshal.WriteByte(ptr, offset, c);
111+
}
112+
}
113+
}
114+
115+
return dst;
116+
}
117+
118+
unsafe public static Pixbuf Intensity(double* src, int srcx, int srcy)
119+
{
120+
var dst = InitializePixbuf(srcx, srcy);
121+
122+
for (var y = 0; y < dst.Height; y++)
123+
{
124+
for (var x = 0; x < dst.Width; x++)
125+
{
126+
var ptr = dst.Pixels + y * dst.Rowstride + x * dst.NChannels;
127+
var c = (byte)(255 * src[y * srcx + x]);
128+
129+
for (var offset = 0; offset < dst.NChannels; offset++)
130+
{
131+
Marshal.WriteByte(ptr, offset, c);
132+
}
133+
}
134+
}
135+
136+
return dst;
137+
}
138+
139+
unsafe public static void Copy(Pixbuf dst, byte* src)
140+
{
141+
if (dst != null)
142+
{
143+
for (var y = 0; y < dst.Height; y++)
144+
{
145+
for (var x = 0; x < dst.Width; x++)
146+
{
147+
var ptr = dst.Pixels + y * dst.Rowstride + x * dst.NChannels;
148+
149+
for (var offset = 0; offset < dst.NChannels; offset++)
150+
{
151+
Marshal.WriteByte(ptr, offset, src[(y * dst.Width + x) * dst.NChannels + offset]);
152+
}
153+
}
154+
}
155+
}
156+
}
157+
158+
// see: https://www.johndcook.com/blog/csharp_erf/
159+
public static double Erf(double x)
160+
{
161+
// constants
162+
double a1 = 0.254829592;
163+
double a2 = -0.284496736;
164+
double a3 = 1.421413741;
165+
double a4 = -1.453152027;
166+
double a5 = 1.061405429;
167+
double p = 0.3275911;
168+
169+
// Save the sign of x
170+
int sign = 1;
171+
if (x < 0)
172+
sign = -1;
173+
x = Math.Abs(x);
174+
175+
// A&S formula 7.1.26
176+
double t = 1.0 / (1.0 + p * x);
177+
double y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.Exp(-x * x);
178+
179+
return sign * y;
180+
}
181+
182+
public static double Mod(double a, double m)
183+
{
184+
return a - m * Math.Floor(a / m);
185+
}
186+
187+
public static void Free(params IDisposable[] trash)
188+
{
189+
foreach (var item in trash)
190+
{
191+
if (item != null)
192+
{
193+
item.Dispose();
194+
}
195+
}
196+
}
197+
198+
unsafe public static void Free(params byte*[] trash)
199+
{
200+
foreach (var item in trash)
201+
{
202+
if (item != null)
203+
{
204+
Marshal.FreeHGlobal((IntPtr)item);
205+
}
206+
}
207+
}
208+
}

0 commit comments

Comments
 (0)