/Users/lyon/j4p/src/ip/raul/GridImage.java

1    package ip.raul; 
2     
3    import ip.gui.frames.OpenFrame; 
4     
5    import java.awt.*; 
6    import java.awt.event.*; 
7     
8    public class GridImage extends OpenFrame 
9            implements ActionListener, MouseListener, MouseMotionListener { 
10    
11       MenuBar mb = new MenuBar(); 
12       Menu SettingsMenu = new Menu("Settings"); 
13    
14       MenuItem openGif_mi = addMenuItem(SettingsMenu, "[c]hange image..."); 
15       MenuItem openFTPGif_mi = addMenuItem(SettingsMenu, "[C]hange image(FTP)..."); 
16       MenuItem default_mi = addMenuItem(SettingsMenu, "[d]efault image"); 
17       MenuItem revert_mi = addMenuItem(SettingsMenu, "[r]evert"); 
18       MenuItem groff_mi = addMenuItem(SettingsMenu, "[g]rid on/off"); 
19       MenuItem applyBilinear4Points_mi = addMenuItem(SettingsMenu, "[a]pplyBilinear4Points"); 
20    
21       public Polygon p = new Polygon(); 
22       Image img = null; 
23       Color c = Color.black; 
24       boolean doRevert = false; 
25       short rn[][] = new short[0][0]; 
26       short gn[][] = new short[0][0]; 
27       short bn[][] = new short[0][0]; 
28       int xPoints = 5; //grid X 
29       int yPoints = 5; //grid Y 
30       boolean busy = false; 
31       int groff = 1; 
32       int pointToMove = -1; 
33       MyFileDialog mf = null; 
34    
35    
36       public void actionPerformed(ActionEvent e) { 
37           try { 
38               Button b = (Button) e.getSource(); 
39               if (b == mf.openButton) { 
40                   getShortImage(mf.theFile); 
41                   short2Image(); 
42                   setImageWidth(shortImageBean.getR().length); 
43                   setImageHeight(shortImageBean.getR()[0].length); 
44                   img = getImage(); 
45                   doRevert = true; 
46                   p = new Polygon(); 
47                   init(); 
48                   setSize(getImageWidth(), getImageHeight()); 
49                   repaint(); 
50               } 
51           } catch (Exception ex) { 
52           } 
53    
54           if (match(e, applyBilinear4Points_mi)) { 
55               revert(); 
56               applyBilinear(); 
57               return; 
58           } 
59           if (match(e, revert_mi)) { 
60               revert1(); 
61               return; 
62           } 
63           if (match(e, groff_mi)) { 
64               groff = 1 - groff; 
65               repaint(); 
66               return; 
67           } 
68    
69           if (match(e, openGif_mi)) { 
70               openGif1(); 
71               return; 
72           } 
73    
74           if (match(e, openFTPGif_mi)) { 
75               openFTPGif(); 
76               return; 
77           } 
78    
79           if (match(e, default_mi)) { 
80               default1(); 
81               return; 
82           } 
83    
84           super.actionPerformed(e); 
85       } 
86    
87       GridImage(String title, Color Clr) { 
88           super(title); 
89           default1(); 
90           c = Clr; 
91           addMouseListener(this); 
92           addMouseMotionListener(this); 
93           mb.add(SettingsMenu); 
94           setMenuBar(mb); 
95           repaint(); 
96       } 
97    
98       private void init() { 
99           groff = 1; 
100          for (int i = 0; i <= yPoints; i++) 
101              for (int j = 0; j <= xPoints; j++) p.addPoint((int) (j * getImageWidth() / xPoints), (int) (i * getImageHeight() / yPoints)); 
102          groff = 0; 
103      } 
104   
105   
106      private void openGif1() { 
107          openGif(); 
108          img = getImage(); 
109          doRevert = true; 
110          p = new Polygon(); 
111          init(); 
112          setSize(getImageWidth(), getImageHeight()); 
113          repaint(); 
114      } 
115   
116      private void openFTPGif() { 
117          mf = new MyFileDialog(this); 
118          mf.show(); 
119          mf.openButton.addActionListener(this); 
120      } 
121   
122      public void revert1() { 
123          revert(); 
124          img = getImage(); 
125          doRevert = true; 
126          p = new Polygon(); 
127          init(); 
128          setSize(getImageWidth(), getImageHeight()); 
129          repaint(); 
130      } 
131   
132      public void default1() { 
133          grabNumImage(); 
134          show(); 
135          setSize(getImageWidth(), getImageHeight()); 
136          img = getImage(); 
137          doRevert = true; 
138          p = new Polygon(); 
139          init(); 
140      } 
141   
142      public static void main(String args[]) { 
143          GridImage af = new GridImage( 
144                  "GridImage", Color.green); 
145          af.setSize(150, 150); 
146          af.setVisible(true); 
147          af.default1(); 
148      } 
149   
150   
151      public void setGrid(int x, int y) { 
152          xPoints = x; 
153          yPoints = y; 
154          revert1(); 
155      } 
156   
157      public void update(Graphics g) { 
158          paint(g); 
159      } 
160   
161   
162      public void paint(Graphics g) { 
163          if (img == null) return; 
164          if (doRevert) { 
165              if ((img != null)) g.drawImage(img, 0, 0, getImageWidth(), getImageHeight(), this); 
166          } 
167          g.setColor(c); 
168          if (groff == 0) { 
169              for (int i = 0; i <= yPoints; i++) 
170                  for (int j = 0; j <= xPoints; j++) { 
171                      if (j != (xPoints)) 
172                          g.drawLine(p.xpoints[j + i * (xPoints + 1)], 
173                                  p.ypoints[j + i * (xPoints + 1)], 
174                                  p.xpoints[j + 1 + i * (xPoints + 1)], 
175                                  p.ypoints[j + 1 + i * (xPoints + 1)]); 
176                      if (i != (yPoints)) 
177                          g.drawLine(p.xpoints[j + i * (xPoints + 1)], 
178                                  p.ypoints[j + i * (xPoints + 1)], 
179                                  p.xpoints[j + (i + 1) * (xPoints + 1)], 
180                                  p.ypoints[j + (i + 1) * (xPoints + 1)]); 
181                  } 
182              for (int i = 0; i < p.npoints; i++) 
183                  g.drawRect(p.xpoints[i] - 2, p.ypoints[i] - 2, 4, 4); 
184          } 
185      } 
186   
187      public void applyBilinear() { 
188          rn = new short[getImageWidth()][getImageHeight()]; 
189          gn = new short[getImageWidth()][getImageHeight()]; 
190          bn = new short[getImageWidth()][getImageHeight()]; 
191          Point s0,s1,s2,s3,d0,d1,d2,d3; 
192   
193          for (int i = 0; i < yPoints; i++) 
194              for (int j = 0; j < xPoints; j++) { 
195                  s0 = new Point((int) (j * getImageWidth() / xPoints), (int) (i * getImageHeight() / yPoints)); 
196                  s1 = new Point((int) ((j + 1) * getImageWidth() / xPoints), (int) (i * getImageHeight() / yPoints)); 
197                  s2 = new Point((int) ((j + 1) * getImageWidth() / xPoints), (int) ((i + 1) * getImageHeight() / yPoints)); 
198                  s3 = new Point((int) (j * getImageWidth() / xPoints), (int) ((i + 1) * getImageHeight() / yPoints)); 
199                  d0 = new Point(p.xpoints[j + i * (xPoints + 1)], p.ypoints[j + i * (xPoints + 1)]); 
200                  d1 = new Point(p.xpoints[j + 1 + i * (xPoints + 1)], p.ypoints[j + 1 + i * (xPoints + 1)]); 
201                  d2 = new Point(p.xpoints[j + 1 + (i + 1) * (xPoints + 1)], p.ypoints[j + 1 + (i + 1) * (xPoints + 1)]); 
202                  d3 = new Point(p.xpoints[j + (i + 1) * (xPoints + 1)], p.ypoints[j + (i + 1) * (xPoints + 1)]); 
203                  solve(s0, s1, s2, s3, d0, d1, d2, d3); 
204              } 
205          short[][] r = rn; 
206          shortImageBean.setR(r); 
207          setG(gn); 
208          setB(bn); 
209          short2Image(); 
210          img = getImage(); 
211      } 
212   
213      int Dist(Point a, Point b) { 
214          return (int) (Math.sqrt(((b.x - a.x) * (b.x - a.x)) + ((b.y - a.y) * (b.y - a.y)))); 
215      } 
216   
217      public void solve(Point s0, Point s1, Point s2, Point s3, Point d0, Point d1, Point d2, Point d3) { 
218          double xStart1, xEnd1,yStart1, yEnd1; 
219          double xStart2, xEnd2,yStart2, yEnd2; 
220          double k = 0; 
221          int MAX = 0; 
222          int MAX1 = 0; 
223          int xc, yc, xp, yp; 
224   
225          MAX = Dist(d0, d1); 
226          MAX1 = Dist(d1, d2); 
227          if (MAX1 > MAX) MAX = MAX1; 
228          MAX1 = Dist(d2, d3); 
229          if (MAX1 > MAX) MAX = MAX1; 
230          MAX1 = Dist(d3, d0); 
231          if (MAX1 > MAX) MAX = MAX1; 
232   
233          MAX1 = Dist(s0, s1); 
234          if (MAX1 > MAX) MAX = MAX1; 
235          MAX1 = Dist(s1, s2); 
236          if (MAX1 > MAX) MAX = MAX1; 
237          MAX1 = Dist(s2, s3); 
238          if (MAX1 > MAX) MAX = MAX1; 
239          MAX1 = Dist(s3, s0); 
240          if (MAX1 > MAX) MAX = MAX1; 
241          MAX = MAX + 1; 
242   
243          for (int i = 0; i <= MAX; i++) { 
244              k = (double) ((double) i / (double) MAX); 
245   
246              xStart1 = linearY(d0.x, d3.x, k); 
247              yStart1 = linearY(d0.y, d3.y, k); 
248              xEnd1 = linearY(d1.x, d2.x, k); 
249              yEnd1 = linearY(d1.y, d2.y, k); 
250   
251              xStart2 = linearY(s0.x, s3.x, k); 
252              yStart2 = linearY(s0.y, s3.y, k); 
253              xEnd2 = linearY(s1.x, s2.x, k); 
254              yEnd2 = linearY(s1.y, s2.y, k); 
255   
256              for (int j = 0; j <= MAX; j++) { 
257                  k = (double) ((double) j / (double) MAX); 
258                  xc = (int) (linearY(xStart1, xEnd1, k)); 
259                  yc = (int) (linearY(yStart1, yEnd1, k)); 
260   
261                  xp = (int) (linearY(xStart2, xEnd2, k)); 
262                  yp = (int) (linearY(yStart2, yEnd2, k)); 
263   
264                  if ((xp < getImageWidth()) && (yp < getImageHeight()) && (xp >= 0) && (yp >= 0) && 
265                          (xc < getImageWidth()) && (yc < getImageHeight()) && (xc >= 0) && (yc >= 0)) { 
266                      rn[xc][yc] = shortImageBean.getR()[xp][yp]; 
267                      gn[xc][yc] = shortImageBean.getG()[xp][yp]; 
268                      bn[xc][yc] = shortImageBean.getB()[xp][yp]; 
269                  } 
270              } 
271          } 
272      } 
273   
274   
275      double linearY(double x1, double x2, double t) { 
276          double dx = 0; 
277          dx = (double) (x2 - x1); 
278          return (double) (x1 + (double) (dx * t)); 
279      } 
280   
281      public void movePoints(MouseEvent e) { 
282          int i = pointToMove; 
283          p.xpoints[i] = getX(e); 
284          p.ypoints[i] = getY(e); 
285          repaint(); 
286      } 
287   
288      private int getX(MouseEvent e) { 
289          return (int) (e.getX()); 
290      } 
291   
292      private int getY(MouseEvent e) { 
293          return (int) (e.getY()); 
294      } 
295   
296      public void mousePressed(MouseEvent e) { 
297      } 
298   
299      public void mouseExited(MouseEvent e) { 
300      } 
301   
302      public void mouseEntered(MouseEvent e) { 
303      } 
304   
305      public void mouseClicked(MouseEvent e) { 
306      } 
307   
308      public void mouseReleased(MouseEvent e) { 
309          busy = false; 
310          repaint(); 
311      } 
312   
313      public void mouseDragged(MouseEvent e) { 
314          e.consume(); 
315          double dx = getX(e); 
316          double dy = getY(e); 
317          double sx, sy; 
318          double ray = 100000; 
319          if (!busy) { 
320              busy = true; 
321              pointToMove = -1; 
322              for (int i = 0; i < p.npoints; i++) { 
323                  sx = (p.xpoints[i] - dx) * (p.xpoints[i] - dx); 
324                  sy = (p.ypoints[i] - dy) * (p.ypoints[i] - dy); 
325                  if ((sx + sy) < ray) { 
326                      ray = sx + sy; 
327                      pointToMove = i; 
328                  } 
329              } 
330          } 
331          if (pointToMove != -1) { 
332              movePoints(e); 
333              return; 
334          } 
335          repaint(); 
336      } 
337   
338      public void mouseMoved(MouseEvent e) { 
339      } 
340  }