/Users/lyon/j4p/src/ip/apurva/MorphLog.java

1    package ip.apurva; 
2     
3    import ip.gui.frames.ImageFrame; 
4    import ip.gui.frames.XformFrame; 
5    import math.Mat3; 
6     
7    import java.awt.*; 
8    import java.awt.event.ActionEvent; 
9    import java.awt.event.ActionListener; 
10    
11   public class MorphLog implements ActionListener { 
12       String titles[] = {"Width of Frame", 
13                          "Height of Frame", 
14                          "Number of Frames", 
15                          "File Name"}; 
16       String defaults[] = {"128", "128", "10", 
17                            "AnimateFrame"}; 
18       RotationDialog Elog = new RotationDialog( 
19               new Frame(), 
20               "Mesh and Cross fade", 
21               titles, 
22               defaults, 
23               50); 
24       ip.gui.frames.AnimateFrame af = new ip.gui.frames.AnimateFrame(); 
25       XformFrame loadimage1 = new XformFrame( 
26               "Image 1"); 
27       XformFrame loadimage2 = new XformFrame( 
28               "Image 2"); 
29       AnimateFrame mg; 
30    
31       String ImagePic1, ImagePic2; 
32       int w = 0; 
33       int h = 0; 
34       int no_of_frames = 0; 
35       String filename = new String(); 
36    
37       MorphLog() { 
38    
39           Elog.Image1.addActionListener(this); 
40           Elog.Image2.addActionListener(this); 
41           Elog.setButton.addActionListener(this); 
42           Elog.Play.addActionListener(this); 
43           af.setVisible(true); 
44           mg = new AnimateFrame(af); 
45           mg.setSize(100, 100); 
46           mg.setVisible(true); 
47           //mg.init(); 
48    
49       } 
50    
51       public static void main(String args[]) { 
52           new MorphLog(); 
53       } 
54    
55       public void display() { 
56           Elog.show(); 
57       } 
58    
59       public void actionPerformed(ActionEvent e) { 
60    
61           Button b = (Button) e.getSource(); 
62           if (b == Elog.setButton) { 
63    
64               String s[] = Elog.getUserInput(); 
65               w = Integer.parseInt(s[0]); 
66               h = Integer.parseInt(s[1]); 
67               no_of_frames = 
68               Integer.parseInt(s[2]); 
69               filename = s[3]; 
70               Mat3 at = new Mat3(); 
71               Dimension d = new Dimension(); 
72               d = loadimage1.getSize(); 
73               double xscale, yscale; 
74               xscale = (w / d.width) * 100; 
75               yscale = (h / d.height) * 100; 
76               System.out.println( 
77                       "" + w + " " + h + " " + 
78                       d.width + 
79                       " " + 
80                       d.height + 
81                       "" + 
82                       " " + 
83                       xscale + 
84                       " " + 
85                       yscale + 
86                       ""); 
87               System.out.println( 
88                       xscale + " " + yscale + ""); 
89               at.setScale(xscale, yscale); 
90               // loadimage1.xform(at); 
91               // loadimage2.xform(at); 
92               // loadimage1.setSize(w,h); 
93               // loadimage2.setSize(w,h); 
94               loadimage1.image2Short(); 
95               loadimage2.image2Short(); 
96               short diffarr[][] = new short[loadimage1.getImageWidth()][loadimage1.getImageHeight()]; 
97               short diffarb[][] = new short[loadimage1.getImageWidth()][loadimage1.getImageHeight()]; 
98               short diffarg[][] = new short[loadimage1.getImageWidth()][loadimage1.getImageHeight()]; 
99               ImageFrame frame = loadimage2; 
100              for (int i = 0; i < 
101                              frame.shortImageBean.getR() 
102                      .length; i++) { 
103                  ImageFrame frame1 = loadimage2; 
104                  for (int j = 0; j < 
105                                  frame1.shortImageBean.getR()[0].length; j++) { 
106                      ImageFrame frame2 = loadimage2; 
107                      diffarr[i][j] = 
108                      (short) ((frame2.shortImageBean.getR()[i][j] - 
109                                loadimage1.shortImageBean.getR()[i][j]) / 
110                               no_of_frames); 
111                      ImageFrame frame4 = loadimage2; 
112                      diffarb[i][j] = 
113                      (short) ((frame4.shortImageBean.getB()[i][j] - 
114                                loadimage1.shortImageBean.getB()[i][j]) / 
115                               no_of_frames); 
116                      ImageFrame frame3 = loadimage2; 
117                      diffarg[i][j] = 
118                      (short) ((frame3.shortImageBean.getG()[i][j] - 
119                                loadimage1.shortImageBean.getG()[i][j]) / 
120                               no_of_frames); 
121                  } 
122              } 
123              af.addImage(loadimage1.getImage()); 
124              af.addImage(loadimage2.getImage()); 
125   
126  //          loadimage1.saveAsGif(""+filename+"0.ip.gif"); 
127  //          loadimage2.saveAsGif(""+filename+""+(no_of_frames-1)+".ip.gif"); 
128              System.out.println( 
129                      "" + filename + "0.ip.gif"); 
130              loadimage2 = loadimage1; 
131              for (int i = 1; i < no_of_frames - 1; i++) { 
132                  ImageFrame frame1 = loadimage2; 
133                  for (int j = 0; j < 
134                                  frame1.shortImageBean.getR() 
135                          .length; j++) { 
136                      ImageFrame frame2 = loadimage2; 
137                      for (int k = 0; k < 
138                                      frame2.shortImageBean.getR()[0].length; k++) { 
139                          ImageFrame frame3 = loadimage2; 
140                          ImageFrame frame4 = loadimage2; 
141                          frame3.shortImageBean.getR()[j][k] = 
142                          (short) (frame4.shortImageBean.getR()[j][k] + 
143                                   diffarr[j][k]); 
144                          ImageFrame frame7 = loadimage2; 
145                          ImageFrame frame8 = loadimage2; 
146                          frame7.shortImageBean.getB()[j][k] = 
147                          (short) (frame8.shortImageBean.getB()[j][k] + 
148                                   diffarb[j][k]); 
149                          ImageFrame frame5 = loadimage2; 
150                          ImageFrame frame6 = loadimage2; 
151                          frame5.shortImageBean.getG()[j][k] = 
152                          (short) (frame6.shortImageBean.getG()[j][k] + 
153                                   diffarg[j][k]); 
154                      } 
155                  } 
156                  loadimage2.short2Image(); 
157                  af.addImage( 
158                          loadimage2.getImage()); 
159  //          loadimage2.saveAsGif(""+filename+i+".ip.gif"); 
160                  System.out.println( 
161                          "" + filename + i + 
162                          ".ip.gif"); 
163              } 
164              System.out.println( 
165                      "" + filename + "" + 
166                      (no_of_frames - 1) + 
167                      ".ip.gif"); 
168   
169          } 
170   
171          if (b == Elog.Image1) { 
172              FileDialog fd = new FileDialog( 
173                      new Frame(), 
174                      "Open Image # 1"); 
175              fd.show(); 
176              ImagePic1 = fd.getDirectory() + 
177                          fd.getFile(); 
178              loadimage1.openGif(ImagePic1); 
179              fd.dispose(); 
180          } 
181          if (b == Elog.Image2) { 
182              FileDialog fd = new FileDialog( 
183                      new Frame(), 
184                      "Open Image # 2"); 
185              fd.show(); 
186              ImagePic2 = fd.getDirectory() + 
187                          fd.getFile(); 
188              loadimage2.openGif(ImagePic2); 
189              fd.dispose(); 
190          } 
191          if (b == Elog.Play) { 
192              af.setRunning(true); 
193  /*AnimateFrame af = new AnimateFrame(); 
194        String files[]=new String [no_of_frames]; 
195        System.out.println(filename); 
196        for(int j=0;j<no_of_frames;j++){ 
197            files[j]=""+filename+j+".ip.gif"; 
198            System.out.println(files[j]); 
199            } 
200   
201        af.setSize(loadimage1.r.length,loadimage1.r[0].length); 
202        Toolkit tk = Toolkit.getDefaultToolkit(); 
203        for (int i=0; i < files.length; i++) { 
204        Image img = tk.getImage(files[i]); 
205        waitForImage(new Frame(), img); 
206        af.addImage(img); 
207        if ((i % 100) == 0) { 
208        System.out.println("read file "+i); 
209                } 
210                } 
211            af.setVisible(true); 
212        */ 
213          } 
214      } 
215   
216      static void waitForImage(Component component, 
217                               Image image) { 
218          MediaTracker tracker = new MediaTracker( 
219                  component); 
220          try { 
221              tracker.addImage(image, 0); 
222              tracker.waitForID(0); 
223              if (!tracker.checkID(0)) 
224                  System.out.println( 
225                          "Load failure!"); 
226          } catch (InterruptedException e) { 
227          } 
228      } 
229  } 
230