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
44 private ResizeListener myRL;
45 private ImageManager imgmanager;
48 private int selected = 0;
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;
58 public ImageViewer( String j2kfilename, ImageManager manager, boolean session)
62 this.setSize( 200, 200);
63 Dimension asz = this.getSize();
68 setBackground(Color.black);
69 myMML = new MML(this);
70 myRL = new ResizeListener(this);
73 img = imgmanager.getImage( j2kfilename, vw, vh, session);
75 addMouseListener(myMML);
76 addMouseMotionListener(myMML);
77 addComponentListener(myRL);
80 public Image getImage()
90 double scalex = vw/(double)rect.width;
91 double scaley = vh/(double)rect.height;
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);
98 img = imgmanager.getImage( fw, fh, rx, ry, vw, vh);
100 rect.x = rect.y = rect.width = rect.height = 0;
107 public void enlarge()
112 Dimension asz = this.getSize();
117 double scalex = vw/(double)imgmanager.getRw();
118 double scaley = vh/(double)imgmanager.getRh();
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);
125 img = imgmanager.getImage( fw, fh, rx, ry, vw, vh);
131 public void setSelected(int state)
136 if (state != selected) {
143 public boolean isInsideRect(int x, int y)
145 return rect.contains(x - offset.x, y - offset.y);
148 public void setRGeom(int x1, int y1, int x2, int y2)
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);
156 // public void annotate( JP2XMLparser.ROIparams roi[])
158 // int numofroi = roi.length;
160 // roirect = new Rectangle [numofroi];
161 // roiname = new String [numofroi];
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();
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);
180 // roirect[i] = null;
181 // roiname[i] = null;
187 public boolean hasAnnotation()
195 public boolean isInsideROIRect(int x, int y)
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)){
206 public void paint(Graphics g)
210 Graphics2D g2 = (Graphics2D) g;
213 g2.clearRect(0, 0, vw, vh);
216 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
217 RenderingHints.VALUE_ANTIALIAS_ON);
218 g2.setRenderingHint(RenderingHints.KEY_RENDERING,
219 RenderingHints.VALUE_RENDER_QUALITY);
224 iw = img.getWidth(this);
225 ih = img.getHeight(this);
227 bi = new BufferedImage( iw, ih, BufferedImage.TYPE_INT_RGB);
228 big = bi.createGraphics();
230 big.drawImage(img, 0, 0, this);
231 big.setPaint(Color.red);
232 if ((rect.width > 0) && (rect.height > 0))
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);
243 shadeExt(big, 0, 0, 0, 64);
244 else if (selected == 2) {
245 shadeExt(big, 0, 0, 0, 255);
248 g2.drawImage(bi, offset.x, offset.y, this);
251 private void shadeRect(Graphics2D g2, int r, int g, int b, int a)
253 g2.setPaint(new Color(r, g, b, a));
254 g2.fillRect(rect.x + 1, rect.y + 1, rect.width - 1, rect.height - 1);
257 private void shadeExt(Graphics2D g2, int r, int g, int b, int a)
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_ */