-
Notifications
You must be signed in to change notification settings - Fork 7
/
NativeCodeJNA.java
97 lines (57 loc) · 3.13 KB
/
NativeCodeJNA.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import com.sun.jna.Library;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;
import ij.IJ;
public class NativeCodeJNA {
// Interface that declares native methods of the ONLM C program
public interface ONLMInterface extends Library {
public void ONLM(Pointer p1, int x1, int x2, Pointer p2, float x3, Pointer p3, int x4, int x5, int x6, PointerByReference valsRef);
public void cleanup(Pointer s);
}
// Interface that declares native methods of the NoiseEstimation C program
public interface NoiseEstimationInterface extends Library {
public void estimate(Pointer p1, int x1, int x2, int x3, int x4, PointerByReference valsRef);
}
// Method that calls the two (NoiseEstimation and ONLM) native programs.
static public float[] NativeCall(float[] img , float[] medfiltimg, int searchrad , int patchrad, float beta, int x, int y, int z ){
// Load Native Library: libNoiseEstimation.dylib
NoiseEstimationInterface noisest = (NoiseEstimationInterface) Native.loadLibrary("NoiseEstimation", NoiseEstimationInterface.class);
int size = x*y*z;
// Allocate memory for native pointers
Pointer ima = new Memory(size * Native.getNativeSize(Float.TYPE));
Pointer ref = new Memory(size * Native.getNativeSize(Float.TYPE));
for (int dloop=0; dloop<(size); dloop++) {
ima.setFloat(dloop * Native.getNativeSize(Float.TYPE), img[dloop] );
ref.setFloat(dloop * Native.getNativeSize(Float.TYPE), medfiltimg[dloop] );
}
// Pointer to a pointer : This lets us read the output array from the first native method
PointerByReference PMAP = new PointerByReference();
IJ.log("Wavelet-based local noise estimation");
long startTime = System.currentTimeMillis();
// Call to the first native method
noisest.estimate(ima, x, y, z, 2*searchrad, PMAP);
long endTime = System.currentTimeMillis();
IJ.log("Elapsed time: " + ((endTime - startTime)/1000) );
Pointer MAP = PMAP.getValue();
// Load Native Library: libONLM_Mod.dylib
ONLMInterface onlm = (ONLMInterface) Native.loadLibrary("ONLM_Mod", ONLMInterface.class);
// Pointer to a pointer : This lets us read the output array from the second native method
PointerByReference Pfima = new PointerByReference();
IJ.log("Denoising: 3D Optimized Non-local Means Filter");
startTime = System.currentTimeMillis();
// Call to the second native method
onlm.ONLM(ima, searchrad, patchrad, MAP, beta, ref, x, y, z, Pfima);
endTime = System.currentTimeMillis();
IJ.log("Elapsed time: " + ((endTime - startTime)/1000) );
Pointer fima = Pfima.getValue();
// Get final result
for (int dloop=0; dloop<size; dloop++) {
medfiltimg[dloop] = fima.getFloat(dloop * Native.getNativeSize(Float.TYPE));
}
// free memory
onlm.cleanup(fima);
return medfiltimg;
}
}