changed variables names alpha, beta, gamma, delta in dwt.c to avoid re-declarations...
[openjpeg.git] / j2kviewer / src / ImageViewer.java
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 import java.awt.image.*;
5 import java.awt.geom.*;
6 import java.net.URL;
7 import javax.swing.border.*;
8 import java.util.*;
9 import java.io.*;
10
11 public class ImageViewer extends JApplet
12 {
13   private class zoomLevel {
14     int x1, y1, x2, y2, zf;
15     
16     zoomLevel() {}
17     zoomLevel(zoomLevel zl)
18     {
19       x1 = zl.x1;
20       y1 = zl.y1;
21       x2 = zl.x2;
22       y2 = zl.y2;
23       zf = zl.zf;
24     }
25   }
26   
27   private BufferedImage bi;
28   private Graphics2D big;
29   private MML myMML;
30   private int iw, ih;
31   private int selected = 0, imgId;
32   private Image img;
33   private PgmImage pgm = new PgmImage();
34   private String cmdline = new String();
35   private static String hostname;
36   private static boolean isApplet = true;
37   private boolean fullRefresh = false;
38   private Point offset = new Point(0,0);
39   private zoomLevel zl = new zoomLevel();
40   private Rectangle rect = new Rectangle();
41   private Stack zoomStack = new Stack();
42   private static String j2kfilename;
43
44   public int getX()      { return offset.x; }
45   public int getY()      { return offset.y; }
46   public int getWidth()  { return iw; }
47   public int getHeight() { return ih; }
48   
49   public void destroy()
50   {
51   }
52   
53   public void zoomIn()
54   {
55     Dimension asz = this.getSize();
56     int maxzf = 3;
57     int coef = 1;
58     int r;
59     
60     cmdline = 
61       "/bin/sh get.sh " + j2kfilename + " " + iw
62       + " " + ih + " " + rect.x + " " + rect.y + " "
63       + rect.width + " " + rect.height;
64     Exec.execPrint(cmdline);
65
66     rect.x = rect.y = rect.width = rect.height = 0;
67
68     img = pgm.open("out.pgm");
69     
70     iw = img.getWidth(this);
71     ih = img.getHeight(this);
72     bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
73     big = bi.createGraphics();
74     selected = 0;
75     fullRefresh = true;
76     repaint();
77   }
78
79   public void zoomOut()
80   {
81   }
82
83   public void init()
84   {
85     String str;
86     int port;
87
88     imgId = 4;
89     if (isApplet && (((hostname = this.getParameter("hostname")) == null)
90                     || hostname.equals("")))
91       hostname = "localhost";
92     if (!isApplet || ((str = this.getParameter("cmdPort")) == null)) {
93       port = 3000;
94     } else {
95       port = new Integer(str).intValue();
96     }
97     
98     this.setSize(512, 512);
99     Dimension asz = this.getSize();
100     zl.x2 = asz.width;
101     zl.y2 = asz.height;
102     
103     cmdline = 
104       "/bin/sh get.sh " + j2kfilename + " " + asz.width
105       + " " + asz.height + " " + zl.x1 + " " + zl.y1 + " "
106       + zl.x2 + " " + zl.y2;
107     Exec.execPrint(cmdline);
108     img = pgm.open("out.pgm");
109     
110     iw = img.getWidth(this);
111     ih = img.getHeight(this);
112     
113     setBackground(Color.black);
114     bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
115     big = bi.createGraphics();
116     myMML = new MML(this);
117     addMouseListener(myMML);
118     addMouseMotionListener(myMML);
119   }
120   
121   public void setSelected(int state)
122   {
123     if (state != selected) {
124       selected = state;
125       repaint();
126     }
127   }
128   
129   public boolean isInsideRect(int x, int y)
130   {
131     return rect.contains(x - offset.x, y - offset.y);
132   }
133
134   public void setRGeom(int x1, int y1, int x2, int y2)
135   {
136     rect.x = Math.min(x1,x2) - offset.x;
137     rect.y = Math.min(y1,y2) - offset.y;
138     rect.width = Math.abs(x2-x1);
139     rect.height = Math.abs(y2-y1);
140   }
141
142   public void paint(Graphics g)
143   {
144     Graphics2D g2 = (Graphics2D) g;
145     Dimension asz = this.getSize();
146
147     if (fullRefresh) {
148       g2.clearRect(0, 0, asz.width, asz.height);
149       fullRefresh = false;
150     }
151     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
152                         RenderingHints.VALUE_ANTIALIAS_ON);
153     g2.setRenderingHint(RenderingHints.KEY_RENDERING,
154                         RenderingHints.VALUE_RENDER_QUALITY);
155     big.setColor(Color.black);
156     offset.x = (int) (asz.width  - iw) / 2;
157     offset.y = (int) (asz.height - ih) / 2;
158     big.drawImage(img, 0, 0, this);
159     big.setPaint(Color.red);
160     if ((rect.width > 0) && (rect.height > 0))
161       big.draw(rect);
162     if (selected == 1)
163       shadeExt(big, 0, 0, 0, 64);
164     else if (selected == 2) {
165       shadeExt(big, 0, 0, 0, 255);
166       selected = 1;
167     }
168     g2.drawImage(bi, offset.x, offset.y, this);
169   }
170
171   private void shadeRect(Graphics2D g2, int r, int g, int b, int a)
172   {
173     g2.setPaint(new Color(r, g, b, a));
174     g2.fillRect(rect.x + 1, rect.y + 1, rect.width - 1, rect.height - 1);
175   }
176   
177   private void shadeExt(Graphics2D g2, int r, int g, int b, int a)
178   {
179     g2.setPaint(new Color(r, g, b, a));
180     g2.fillRect(0, 0, iw, rect.y); /* _N_ */
181     g2.fillRect(rect.x + rect.width + 1, rect.y,
182                 iw - rect.x - rect.width - 1, rect.height + 1); /* E */
183     g2.fillRect(0, rect.y, rect.x, rect.height + 1); /* W */
184     g2.fillRect(0, rect.y + rect.height + 1,
185                 iw, ih - rect.y - rect.height - 1); /* _S_ */
186   }
187
188   protected URL getURL(String filename)
189   {
190     URL codeBase = this.getCodeBase();
191     URL url = null;
192
193     try {
194       url = new URL(codeBase, filename);
195     } catch (java.net.MalformedURLException e) {
196       System.out.println("Couldn't create image: badly specified URL");
197       return null;
198     }
199
200     return url;
201   }
202
203   public static void main(String s[])
204   {
205     if (s.length > 0)
206       j2kfilename = s[0];
207     else
208       j2kfilename = "girl";
209       System.out.println(j2kfilename);
210     isApplet = false;
211     JFrame f = new JFrame("ImageViewer");
212     f.addWindowListener(new WindowAdapter() {
213         public void windowClosing(WindowEvent e) {System.exit(0);}
214     });
215     JApplet applet = new ImageViewer();
216     f.getContentPane().add("Center", applet);
217     applet.init();
218     f.pack();
219     f.setSize(new Dimension(550,550));
220     f.show();
221   }
222 }