opj_viewer enabled stateless requests
[openjpeg.git] / applications / jpip / opj_client / opj_viewer / src / ImageViewer.java
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2011, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 import java.awt.*;
32 import java.awt.event.*;
33 import javax.swing.*;
34 import java.awt.image.*;
35 import java.awt.geom.*;
36 import java.net.URL;
37 import javax.swing.border.*;
38 import java.util.*;
39 import java.io.*;
40
41 public class ImageViewer extends JPanel
42 {  
43     private MML myMML;
44     private ResizeListener myRL;
45     private ImageManager imgmanager;
46     private int vw, vh;
47     private int iw, ih;
48     private int selected = 0;
49     private Image img;
50   
51     private String cmdline = new String();
52     private boolean fullRefresh = false;
53     private Point offset = new Point(0,0);
54     private Rectangle rect = new Rectangle();
55     private Rectangle roirect[] = null;
56     private String roiname[] = null;
57       
58     public ImageViewer( String j2kfilename, ImageManager manager, boolean session)
59     {
60         String str;
61
62         this.setSize( 200, 200);
63         Dimension asz = this.getSize();
64     
65         vw = asz.width;
66         vh = asz.height;
67     
68         setBackground(Color.black);
69         myMML = new MML(this);
70         myRL = new ResizeListener(this);
71
72         imgmanager = manager;
73         img = imgmanager.getImage( j2kfilename, vw, vh, session);
74
75         addMouseListener(myMML);
76         addMouseMotionListener(myMML);
77         addComponentListener(myRL);
78     }
79
80     public Image getImage()
81     {
82         return img;
83     }
84     
85     public void zoomIn()
86     {
87         roirect = null;
88         roiname = null;
89
90         double scalex = vw/(double)rect.width;
91         double scaley = vh/(double)rect.height;
92     
93         int fw = (int)(imgmanager.getFw()*scalex);
94         int fh = (int)(imgmanager.getFh()*scaley);
95         int rx = (int)((imgmanager.getRx()+rect.x)*scalex);
96         int ry = (int)((imgmanager.getRy()+rect.y)*scaley);
97
98         img = imgmanager.getImage( fw, fh, rx, ry, vw, vh);
99   
100         rect.x = rect.y = rect.width = rect.height = 0;
101         
102         selected = 0;
103         fullRefresh = true;
104         repaint();
105     }
106
107     public void enlarge()
108     {
109         roirect = null;
110         roiname = null;
111
112         Dimension asz = this.getSize();
113     
114         vw = asz.width;
115         vh = asz.height;
116         
117         double scalex = vw/(double)imgmanager.getRw();
118         double scaley = vh/(double)imgmanager.getRh();
119
120         int fw = (int)(imgmanager.getFw()*scalex);
121         int fh = (int)(imgmanager.getFh()*scaley);
122         int rx = (int)(imgmanager.getRx()*scalex);
123         int ry = (int)(imgmanager.getRy()*scaley);
124
125         img = imgmanager.getImage( fw, fh, rx, ry, vw, vh);
126     
127         fullRefresh = true;
128         repaint();
129     }
130
131     public void setSelected(int state)
132     {
133         roirect = null;
134         roiname = null;
135
136         if (state != selected) {
137             
138             selected = state;
139             repaint();
140         }
141     }
142   
143     public boolean isInsideRect(int x, int y)
144     {
145         return rect.contains(x - offset.x, y - offset.y);
146     }
147
148     public void setRGeom(int x1, int y1, int x2, int y2)
149     {
150         rect.x = Math.min(x1,x2) - offset.x;
151         rect.y = Math.min(y1,y2) - offset.y;
152         rect.width = Math.abs(x2-x1);
153         rect.height = Math.abs(y2-y1);
154     }
155     
156     // public void annotate( JP2XMLparser.ROIparams roi[])
157     // {
158     //  int numofroi = roi.length;
159
160     //  roirect = new Rectangle [numofroi];
161     //  roiname = new String [numofroi];
162         
163     //  double scale_x = imgmanager.getFw()/(double)imgmanager.getOrigWidth();
164     //  double scale_y = imgmanager.getFh()/(double)imgmanager.getOrigHeight();
165     //  int rx = imgmanager.getRx();
166     //  int ry = imgmanager.getRy();
167     //  int rw = imgmanager.getRw();
168     //  int rh = imgmanager.getRh();
169
170     //  for( int i=0; i<numofroi ; i++){
171     //      int x = (int)(roi[i].x*scale_x) - rx;
172     //      int y = (int)(roi[i].y*scale_y) - ry;
173     //      int w = (int)(roi[i].w*scale_x);
174     //      int h = (int)(roi[i].h*scale_y);
175     //      if( 0<=x && 0<=y && x+w<=rw && y+h<=rh){ // can be optimized
176     //          roirect[i] = new Rectangle( x, y, w, h);
177     //          roiname[i] = new String( roi[i].name);
178     //      }
179     //      else{
180     //          roirect[i] = null;
181     //          roiname[i] = null;
182     //      }
183     //  }
184     //  repaint();
185     // }
186     
187     public boolean hasAnnotation()
188     {
189         if( roirect == null)
190             return false;
191         else
192             return true;
193     }
194     
195     public boolean isInsideROIRect(int x, int y)
196     {
197         for( int i=0; i<roirect.length; i++)
198             if( roirect[i] != null)
199                 if( roirect[i].contains(x - offset.x, y - offset.y)){
200                     rect = roirect[i];
201                     return true;
202                 }
203         return false;
204     }
205
206     public void paint(Graphics g)
207     {
208         BufferedImage bi;
209         Graphics2D big;
210         Graphics2D g2 = (Graphics2D) g;
211
212         if (fullRefresh) {
213             g2.clearRect(0, 0, vw, vh);
214             fullRefresh = false;
215         }
216         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
217                             RenderingHints.VALUE_ANTIALIAS_ON);
218         g2.setRenderingHint(RenderingHints.KEY_RENDERING,
219                             RenderingHints.VALUE_RENDER_QUALITY);
220
221         offset.x = 0;
222         offset.y = 0;
223
224         iw = img.getWidth(this);
225         ih = img.getHeight(this);
226     
227         bi = new BufferedImage( iw, ih, BufferedImage.TYPE_INT_RGB);
228         big = bi.createGraphics();
229     
230         big.drawImage(img, 0, 0, this);
231         big.setPaint(Color.red);
232         if ((rect.width > 0) && (rect.height > 0))
233             big.draw(rect);
234
235         if( roirect != null){
236             for( int i=0; i<roirect.length; i++)
237                 if( roirect[i] != null){
238                     big.draw( roirect[i]);
239                     big.drawString( roiname[i], roirect[i].x+3, roirect[i].y+roirect[i].height*2/3);
240                 }
241         }
242         if (selected == 1)
243             shadeExt(big, 0, 0, 0, 64);
244         else if (selected == 2) {
245             shadeExt(big, 0, 0, 0, 255);
246             selected = 1;
247         }
248         g2.drawImage(bi, offset.x, offset.y, this);
249     }
250
251     private void shadeRect(Graphics2D g2, int r, int g, int b, int a)
252     {
253         g2.setPaint(new Color(r, g, b, a));
254         g2.fillRect(rect.x + 1, rect.y + 1, rect.width - 1, rect.height - 1);
255     }
256   
257     private void shadeExt(Graphics2D g2, int r, int g, int b, int a)
258     {
259         g2.setPaint(new Color(r, g, b, a));
260         g2.fillRect(0, 0, iw, rect.y); /* _N_ */
261         g2.fillRect(rect.x + rect.width + 1, rect.y,
262                     iw - rect.x - rect.width - 1, rect.height + 1); /* E */
263         g2.fillRect(0, rect.y, rect.x, rect.height + 1); /* W */
264         g2.fillRect(0, rect.y + rect.height + 1,
265                     iw, ih - rect.y - rect.height - 1); /* _S_ */
266     }
267 }