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

1    package ip.gui.frames; 
2     
3    import ip.gui.dialog.MatLog; 
4    import ip.transforms.ConvolutionUtils; 
5    import math.MathUtils; 
6     
7    import java.awt.*; 
8     
9    public class ConvolutionFrame extends OpenFrame { 
10    
11       ConvolutionFrame(String title) { 
12           super(title); 
13       } 
14    
15       private boolean showKernal = false; 
16    
17       /** 
18        Toggles the convolutions kernel boolean 
19        on and off in the 
20        @see SpatialFilterFrame 
21        */ 
22       public void showConvolutionKernal() { 
23           showKernal = !showKernal; 
24       } 
25    
26       public int cx(int x) { 
27           if (x > getImageWidth() - 1) return x - getImageWidth() + 1; 
28           if (x < 0) return getImageWidth() + x; 
29           return x; 
30       } 
31    
32       public int cy(int y) { 
33           if (y > getImageHeight() - 1) return y - getImageHeight() + 1; 
34           if (y < 0) return getImageHeight() + y; 
35           return y; 
36       } 
37    
38    
39       public void randImage() { 
40           randImage(0, 255); 
41       } 
42    
43       public void randImage(int min, int max) { 
44           for (int x = 0; x < getImageWidth(); x++) 
45               for (int y = 0; y < getImageHeight(); y++) { 
46                   shortImageBean.getR()[x][y] = (short) MathUtils.rand(min, max); 
47                   shortImageBean.getG()[x][y] = (short) MathUtils.rand(min, max); 
48                   shortImageBean.getB()[x][y] = (short) MathUtils.rand(min, max); 
49               } 
50           short2Image(); 
51       } 
52    
53       private void showMatLog() { 
54           Rectangle r = getBounds(); 
55           Dimension d = r.getSize(); 
56           ml.setLocation(d.width, d.height / 2); 
57           ml.setVisible(true); 
58       } 
59    
60       private MatLog ml; 
61    
62       public void convolve(float k[][]) { 
63           //super.convolveFast(k); 
64           convolveSlow(k); 
65       } 
66    
67       public void convolveSlow(float k[] []) { 
68           // a 1kx1k image allocates not more than 
69           // 1 MB at a time. 
70           //Mat.print(k); 
71           //Timer t = new Timer(); 
72           //t.start(); 
73           if (showKernal) { 
74               ml = new MatLog(k); 
75               showMatLog(); 
76           } 
77           short[][] r = ConvolutionUtils.convolve2(shortImageBean.getR(), k); 
78           shortImageBean.setR(r); 
79           setG(ConvolutionUtils.convolve2(shortImageBean.getG(), k)); 
80           setB(ConvolutionUtils.convolve2(shortImageBean.getB(), k)); 
81           getClipper().clip(shortImageBean.getR(), shortImageBean.getG(), shortImageBean.getB()); 
82           short2Image(); 
83           //t.print("convolution done"); 
84       } 
85   } 
86    
87