/Users/lyon/j4p/src/j2d/edge/gabor/FilterCanvas.java

1    package j2d.edge.gabor; 
2     
3     
4    import futils.Out; 
5    import j2d.ImageUtils; 
6    import graphics.NumImage; 
7    import ip.gui.frames.ClosableFrame; 
8    import ip.transforms.ConvolutionUtils; 
9    import ip.transforms.Kernels; 
10    
11   import java.awt.*; 
12   import java.awt.image.ImageObserver; 
13   import java.io.File; 
14   import java.io.FileWriter; 
15   import java.io.PrintWriter; 
16    
17   public class FilterCanvas extends Canvas { 
18       private Image image; 
19       private short g[][]; 
20    
21    
22       public FilterCanvas(Image img) { 
23           image = img; 
24           this.setSize(image.getWidth(this), image.getHeight(this)); 
25           g = j2d.ImageUtils.getGreenFromImage(image, this); 
26       } 
27    
28       public FilterCanvas() { 
29           image = NumImage.getImage(); 
30           setSize(image.getWidth(this), image.getHeight(this)); 
31           g = j2d.ImageUtils.getGreenFromImage(image, this); 
32       } 
33    
34       public int getGreenValueAt(Point p) { 
35           int x = p.x; 
36           int y = p.y; 
37           if (x >= g.length) x = g.length - 1; 
38           if (y >= g[0].length) y = g[0].length - 1; 
39           if (x < 0) x = 0; 
40           if (y < 0) y = 0; 
41           return g[x][y]; 
42       } 
43    
44       public void drawImage() { 
45           this.repaint(); 
46       } 
47    
48       public Image getImage() { 
49           return image; 
50       } 
51    
52       public void setImage(Image img) { 
53           image = img; 
54           setSize(image.getWidth(this), image.getHeight(this)); 
55           g = j2d.ImageUtils.getGreenFromImage(image, this); 
56       } 
57    
58       public void paint(Graphics graphics) { 
59           if (image != null) 
60               graphics.drawImage(image, 0, 0, getWidth(), getHeight(), this); 
61    
62       } 
63    
64       public void convolutionFast(Image img, float k[][]) { 
65           image = ImageUtils.convolution(image, k); 
66           setSize(image.getWidth(this), image.getHeight(this)); 
67           repaint(); 
68       } 
69    
70       public void convolution(Image img, float k[][]) { 
71           short orig[][] = j2d.ImageUtils.getGreenFromImage(img, this); 
72    
73           // The old slow way.... 
74           orig = ConvolutionUtils.convolve(orig, k); 
75           // 
76           image = j2d.ImageUtils.getImage(orig); 
77           setSize(image.getWidth(this), image.getHeight(this)); 
78           repaint(); 
79       } 
80    
81    
82       public void saveAsJava(File f) { 
83           try { 
84               FileWriter fw = 
85                       new FileWriter(f); 
86               PrintWriter pw = 
87                       new PrintWriter(fw); 
88               Out.saveAsJava(pw, image, this); 
89               pw.flush(); 
90               fw.close(); 
91           } catch (Exception e) { 
92           } 
93       } 
94    
95       public static FilterCanvas[] getFilterCanvas(Image img, ImageObserver io) { 
96           FilterCanvas fc[] = { 
97               new FilterCanvas(img), 
98               new FilterCanvas( 
99                       ConvolutionUtils.convolution(img, 
100                              Kernels.getRobinson1(), io)), 
101              new FilterCanvas( 
102                      ConvolutionUtils.convolution(img, 
103                              Kernels.getRobinson2(), io)), 
104              new FilterCanvas( 
105                      ConvolutionUtils.convolution(img, 
106                              Kernels.getRobinson3(), io)), 
107              new FilterCanvas( 
108                      ConvolutionUtils.convolution(img, 
109                              Kernels.getRobinson4(), io)), 
110              new FilterCanvas( 
111                      ConvolutionUtils.convolution(img, 
112                              Kernels.getRobinson5(), io)), 
113              new FilterCanvas( 
114                      ConvolutionUtils.convolution(img, 
115                              Kernels.getRobinson6(), io)), 
116              new FilterCanvas( 
117                      ConvolutionUtils.convolution(img, 
118                              Kernels.getRobinson7(), io)), 
119              new FilterCanvas( 
120                      ConvolutionUtils.convolution(img, 
121                              Kernels.getRobinson8(), io)) 
122          }; 
123          return fc; 
124      } 
125   
126      public static FilterCanvas[] getSubBands(Image img, ImageObserver io) { 
127          ClosableFrame f = new ClosableFrame(); 
128          f.setLayout(new GridLayout(4, 4)); 
129          FilterCanvas[] fc = getFilterCanvas(img, io); 
130          for (int i = 0; i < fc.length; i++) 
131              f.add(fc[i]); 
132          f.show(); 
133          return fc; 
134      } 
135   
136   
137  } 
138