/Users/lyon/j4p/src/ip/gui/frames/FFTFrame.java

1    package ip.gui.frames; 
2     
3     
4    import ip.transforms.FFTImage; 
5    import ip.transforms.FFTRadix2; 
6    import utils.Timer; 
7     
8    import java.awt.*; 
9    import java.awt.event.ActionEvent; 
10    
11   public class FFTFrame extends XformFrame { 
12       private Menu fftMenu = getMenu("FFT"); 
13       private MenuItem fftpfa_mi = addMenuItem(fftMenu, "pfa transforms.fft"); 
14       private MenuItem fftipfa_mi = addMenuItem(fftMenu, "pfa ifft"); 
15    
16       private MenuItem fftR2_mi = addMenuItem(fftMenu, "[E-T-f]ft radix 2"); 
17       private MenuItem ifftR2_mi = addMenuItem(fftMenu, "[E-T-i]transforms.fft radix 2"); 
18       private MenuItem complexMultR2_mi = addMenuItem(fftMenu, "complex multR2"); 
19       private MenuItem rgb2Complex_mi = addMenuItem(fftMenu, "rgb2Complex"); 
20       private MenuItem phaseFFTR2_mi = addMenuItem(fftMenu, "[p]haseFFTR2"); 
21       private MenuItem filterGabor_mi = addMenuItem(fftMenu, "[T-f]ilterGabor"); 
22       private MenuItem radix2fftStats_mi = addMenuItem(fftMenu, "Radix2 fft stats"); 
23    
24    
25       FFTFrame(String title) { 
26           super(title); 
27           getXformMenu().add(fftMenu); 
28       } 
29    
30    
31       public void actionPerformed(ActionEvent e) { 
32    
33           if (match(e, radix2fftStats_mi)) { 
34               radix2fftStats(); 
35               return; 
36           } 
37    
38           if (match(e, filterGabor_mi)) { 
39               filterGabor(); 
40               return; 
41           } 
42           if (match(e, phaseFFTR2_mi)) { 
43               phaseFFTR2(); 
44               return; 
45           } 
46           if (match(e, fftpfa_mi)) { 
47               fftpfa(); 
48               return; 
49           } 
50           if (match(e, fftipfa_mi)) { 
51               fftipfa(); 
52               return; 
53           } 
54    
55           if (match(e, complexMultR2_mi)) { 
56               complexMultR2(); 
57               return; 
58           } 
59           if (match(e, rgb2Complex_mi)) { 
60               rgb2Complex(); 
61               return; 
62           } 
63           if (match(e, fftR2_mi)) { 
64               fftR2(); 
65               return; 
66           } 
67           if (match(e, ifftR2_mi)) { 
68               ifftR2(); 
69               return; 
70           } 
71           super.actionPerformed(e); 
72       } 
73    
74    
75       public void test1DFFTvs() { 
76           String args[] = {}; 
77           math.transforms.FFT1d.main(args); 
78       } 
79    
80       private FFTImage fftimage = null; 
81    
82       public void fftpfa() { 
83           Timer t = new Timer(); 
84           t.start(); 
85           fftimage = new FFTImage(shortImageBean.getPels(), 
86                   getImageWidth(), 100.0f, true); 
87           pels2Image(fftimage.getFftIntArray()); 
88           image2Short(); 
89           t.print("PSA is done"); 
90       } 
91    
92       public void fftipfa() { 
93           pels2Image(fftimage.getIfftIntArray()); 
94           image2Short(); 
95       } 
96    
97       public static int gcd(int a, int b) { 
98           if (b == 0) return a; 
99           return gcd(b, a % b); 
100      } 
101   
102      public void radix2fftStats() { 
103          fftradix2.fft.printStats(); 
104      } 
105   
106      public void fftR2() { 
107          //Timer t = new Timer(); 
108          //t.start(); 
109          isSquareAndPowerOfTwoCheck(); 
110          fftradix2 = new FFTRadix2(this); 
111          int psd[] = fftradix2.fft(); 
112          viewPsd(psd); 
113          //t.print("transforms.fft done"); 
114      } 
115   
116      private void viewPsd(int psd[]) { 
117   
118          pels2Image(psd); 
119          image2Short(); 
120      } 
121   
122      public void phaseFFTR2() { 
123          //Timer t = new Timer(); 
124          //t.start(); 
125          isSquareAndPowerOfTwoCheck(); 
126          fftradix2 = new FFTRadix2(this); 
127          int psd[] = fftradix2.getPhaseImage(); 
128          viewPsd(psd); 
129          //t.print("transforms.fft done"); 
130      } 
131   
132      public void filterGabor() { 
133          if (getChild() == null) 
134              copyToChildFrame(); 
135          revert(); 
136          getChild().grabGabor(); 
137          getChild().fftR2(); 
138          fftR2(); 
139          complexMultR2(); 
140          ifftR2(); 
141      } 
142   
143      public void complexMultR2() { 
144          fftradix2.complexMult(getChild().fftradix2); 
145          viewPsd(fftradix2.getPsd()); 
146      } 
147   
148      private static double LOG2 = Math.log(2); 
149   
150      private int log2(int l) { 
151          return (int) (Math.log(l) / LOG2); 
152      } 
153   
154      private double log2(double l) { 
155          return Math.log(l) / LOG2; 
156      } 
157   
158      private boolean isPowerOfTwo(double l) { 
159          return log2(l) == log2((int) l); 
160      } 
161   
162      private boolean bothDimensionsPowerOfTwo() { 
163          return 
164                  isPowerOfTwo(getImageWidth()) && isPowerOfTwo(getImageHeight()); 
165      } 
166   
167      private boolean isSquare() { 
168          return getImageWidth() == getImageHeight(); 
169      } 
170   
171      private boolean isSquareAndPowerOfTwo() { 
172          return isSquare() && bothDimensionsPowerOfTwo(); 
173      } 
174   
175      private void isSquareAndPowerOfTwoCheck() { 
176          if (isSquareAndPowerOfTwo()) return; 
177          System.out.println("Image is not square and power of two"); 
178      } 
179   
180      public void ifftR2() { 
181          //Timer t = new Timer(); 
182          //System.out.println("Computing ifft..."); 
183          //t.start(); 
184          int psd[] = fftradix2.ifft(); 
185          //t.print("ifft is done"); 
186          pels2Image(psd); 
187          image2Short(); 
188      } 
189   
190      public FFTRadix2 fftradix2 = null; 
191   
192      public void rgb2Complex() { 
193          fftradix2 = new FFTRadix2(this); 
194      } 
195   
196      public Menu getFftMenu() { 
197          return fftMenu; 
198      } 
199   
200      public MenuItem getFftpfa_mi() { 
201          return fftpfa_mi; 
202      } 
203   
204      public MenuItem getFftipfa_mi() { 
205          return fftipfa_mi; 
206      } 
207   
208      public MenuItem getFftR2_mi() { 
209          return fftR2_mi; 
210      } 
211   
212      public MenuItem getIfftR2_mi() { 
213          return ifftR2_mi; 
214      } 
215   
216      public MenuItem getComplexMultR2_mi() { 
217          return complexMultR2_mi; 
218      } 
219   
220      public MenuItem getRgb2Complex_mi() { 
221          return rgb2Complex_mi; 
222      } 
223   
224      public MenuItem getPhaseFFTR2_mi() { 
225          return phaseFFTR2_mi; 
226      } 
227   
228      public MenuItem getFilterGabor_mi() { 
229          return filterGabor_mi; 
230      } 
231   
232      public MenuItem getRadix2fftStats_mi() { 
233          return radix2fftStats_mi; 
234      } 
235   
236      public FFTImage getFftimage() { 
237          return fftimage; 
238      } 
239   
240      public static double getLOG2() { 
241          return LOG2; 
242      } 
243   
244      public FFTRadix2 getFftradix2() { 
245          return fftradix2; 
246      } 
247   
248  }