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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
32 import java.awt.event.*;
34 import java.awt.image.*;
35 import java.awt.geom.*;
37 import javax.swing.border.*;
41 public class ImageViewer extends JPanel
43 private ImageManager imgmanager;
46 private int selected = 0;
49 private String cmdline = new String();
50 private boolean fullRefresh = false;
51 private Point offset = new Point(0,0);
52 private Rectangle rect = new Rectangle();
53 private Rectangle roirect[] = null;
54 private String roiname[] = null;
56 public ImageViewer( String j2kfilename, ImageManager manager, boolean session, boolean jppstream, int aux)
61 this.setSize( 170, 170);
62 Dimension asz = this.getSize();
67 setBackground(Color.black);
68 myMML = new MML(this);
72 img = imgmanager.getImage( j2kfilename, vw, vh, session, aux, jppstream, !jppstream);
74 addMouseListener(myMML);
75 addMouseMotionListener(myMML);
76 addComponentListener( new ResizeListener(this));
79 public Image getImage()
89 double scalex = (double)vw/(double)rect.width;
90 double scaley = (double)vh/(double)rect.height;
92 int fw = (int)(imgmanager.getFw()*scalex);
93 int fh = (int)(imgmanager.getFh()*scaley);
94 int rx = (int)((imgmanager.getRx()+rect.x)*scalex);
95 int ry = (int)((imgmanager.getRy()+rect.y)*scaley);
97 img = imgmanager.getImage( fw, fh, rx, ry, vw, vh);
99 rect.x = rect.y = rect.width = rect.height = 0;
106 public void enlarge()
111 Dimension asz = this.getSize();
116 double scalex = vw/(double)imgmanager.getRw();
117 double scaley = vh/(double)imgmanager.getRh();
119 int fw = (int)(imgmanager.getFw()*scalex);
120 int fh = (int)(imgmanager.getFh()*scaley);
121 int rx = (int)(imgmanager.getRx()*scalex);
122 int ry = (int)(imgmanager.getRy()*scaley);
124 img = imgmanager.getImage( fw, fh, rx, ry, vw, vh);
130 public void setSelected(int state)
135 if (state != selected) {
142 public boolean isInsideRect(int x, int y)
144 return rect.contains(x - offset.x, y - offset.y);
147 public void setRGeom(int x1, int y1, int x2, int y2)
149 rect.x = Math.min(x1,x2) - offset.x;
150 rect.y = Math.min(y1,y2) - offset.y;
151 rect.width = Math.abs(x2-x1);
152 rect.height = Math.abs(y2-y1);
155 // public void annotate( JP2XMLparser.ROIparams roi[])
157 // int numofroi = roi.length;
159 // roirect = new Rectangle [numofroi];
160 // roiname = new String [numofroi];
162 // double scale_x = imgmanager.getFw()/(double)imgmanager.getOrigWidth();
163 // double scale_y = imgmanager.getFh()/(double)imgmanager.getOrigHeight();
164 // int rx = imgmanager.getRx();
165 // int ry = imgmanager.getRy();
166 // int rw = imgmanager.getRw();
167 // int rh = imgmanager.getRh();
169 // for( int i=0; i<numofroi ; i++){
170 // int x = (int)(roi[i].x*scale_x) - rx;
171 // int y = (int)(roi[i].y*scale_y) - ry;
172 // int w = (int)(roi[i].w*scale_x);
173 // int h = (int)(roi[i].h*scale_y);
174 // if( 0<=x && 0<=y && x+w<=rw && y+h<=rh){ // can be optimized
175 // roirect[i] = new Rectangle( x, y, w, h);
176 // roiname[i] = new String( roi[i].name);
179 // roirect[i] = null;
180 // roiname[i] = null;
186 public boolean hasAnnotation()
194 public boolean isInsideROIRect(int x, int y)
196 for( int i=0; i<roirect.length; i++)
197 if( roirect[i] != null)
198 if( roirect[i].contains(x - offset.x, y - offset.y)){
205 public void paint(Graphics g)
209 Graphics2D g2 = (Graphics2D) g;
212 g2.clearRect(0, 0, vw, vh);
215 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
216 RenderingHints.VALUE_ANTIALIAS_ON);
217 g2.setRenderingHint(RenderingHints.KEY_RENDERING,
218 RenderingHints.VALUE_RENDER_QUALITY);
223 iw = img.getWidth(this);
224 ih = img.getHeight(this);
226 bi = new BufferedImage( iw, ih, BufferedImage.TYPE_INT_RGB);
227 big = bi.createGraphics();
229 big.drawImage(img, 0, 0, this);
230 big.setPaint(Color.red);
231 if ((rect.width > 0) && (rect.height > 0))
234 if( roirect != null){
235 for( int i=0; i<roirect.length; i++)
236 if( roirect[i] != null){
237 big.draw( roirect[i]);
238 big.drawString( roiname[i], roirect[i].x+3, roirect[i].y+roirect[i].height*2/3);
242 shadeExt(big, 0, 0, 0, 64);
243 else if (selected == 2) {
244 shadeExt(big, 0, 0, 0, 255);
247 g2.drawImage(bi, offset.x, offset.y, this);
250 private void shadeRect(Graphics2D g2, int r, int g, int b, int a)
252 g2.setPaint(new Color(r, g, b, a));
253 g2.fillRect(rect.x + 1, rect.y + 1, rect.width - 1, rect.height - 1);
256 private void shadeExt(Graphics2D g2, int r, int g, int b, int a)
258 g2.setPaint(new Color(r, g, b, a));
259 g2.fillRect(0, 0, iw, rect.y); /* _N_ */
260 g2.fillRect(rect.x + rect.width + 1, rect.y,
261 iw - rect.x - rect.width - 1, rect.height + 1); /* E */
262 g2.fillRect(0, rect.y, rect.x, rect.height + 1); /* W */
263 g2.fillRect(0, rect.y + rect.height + 1,
264 iw, ih - rect.y - rect.height - 1); /* _S_ */