163193b9284211015f1c549de6b552b8df5a2845
[openjpeg.git] / src / bin / jpip / 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 ImageManager imgmanager;
44     private int vw, vh;
45     private int iw, ih;
46     private int selected = 0;
47     private Image img;
48   
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;
55       
56     public ImageViewer( String j2kfilename, ImageManager manager, boolean session, boolean jppstream, int aux)
57     {
58         String str;
59         MML myMML;
60         
61         this.setSize( 170, 170);
62         Dimension asz = this.getSize();
63     
64         vw = asz.width;
65         vh = asz.height;
66     
67         setBackground(Color.black);
68         myMML = new MML(this);
69
70         imgmanager = manager;
71
72         img = imgmanager.getImage( j2kfilename, vw, vh, session, aux, jppstream, !jppstream);
73         
74         addMouseListener(myMML);
75         addMouseMotionListener(myMML);
76         addComponentListener( new ResizeListener(this));
77     }
78
79     public Image getImage()
80     {
81         return img;
82     }
83     
84     public void zoomIn()
85     {
86         roirect = null;
87         roiname = null;
88
89         double scalex = (double)vw/(double)rect.width;
90         double scaley = (double)vh/(double)rect.height;
91     
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);
96
97         img = imgmanager.getImage( fw, fh, rx, ry, vw, vh);
98   
99         rect.x = rect.y = rect.width = rect.height = 0;
100         
101         selected = 0;
102         fullRefresh = true;
103         repaint();
104     }
105
106     public void enlarge()
107     {
108         roirect = null;
109         roiname = null;
110         
111         Dimension asz = this.getSize();
112     
113         vw = asz.width;
114         vh = asz.height;
115
116         double scalex = vw/(double)imgmanager.getRw();
117         double scaley = vh/(double)imgmanager.getRh();
118
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);
123         
124         img = imgmanager.getImage( fw, fh, rx, ry, vw, vh);
125
126         fullRefresh = true;
127         repaint();
128     }
129
130     public void setSelected(int state)
131     {
132         roirect = null;
133         roiname = null;
134
135         if (state != selected) {
136             
137             selected = state;
138             repaint();
139         }
140     }
141   
142     public boolean isInsideRect(int x, int y)
143     {
144         return rect.contains(x - offset.x, y - offset.y);
145     }
146
147     public void setRGeom(int x1, int y1, int x2, int y2)
148     {
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);
153     }
154     
155     // public void annotate( JP2XMLparser.ROIparams roi[])
156     // {
157     //  int numofroi = roi.length;
158
159     //  roirect = new Rectangle [numofroi];
160     //  roiname = new String [numofroi];
161         
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();
168
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);
177     //      }
178     //      else{
179     //          roirect[i] = null;
180     //          roiname[i] = null;
181     //      }
182     //  }
183     //  repaint();
184     // }
185     
186     public boolean hasAnnotation()
187     {
188         if( roirect == null)
189             return false;
190         else
191             return true;
192     }
193     
194     public boolean isInsideROIRect(int x, int y)
195     {
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)){
199                     rect = roirect[i];
200                     return true;
201                 }
202         return false;
203     }
204
205     public void paint(Graphics g)
206     {
207         BufferedImage bi;
208         Graphics2D big;
209         Graphics2D g2 = (Graphics2D) g;
210
211         if (fullRefresh) {
212             g2.clearRect(0, 0, vw, vh);
213             fullRefresh = false;
214         }
215         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
216                             RenderingHints.VALUE_ANTIALIAS_ON);
217         g2.setRenderingHint(RenderingHints.KEY_RENDERING,
218                             RenderingHints.VALUE_RENDER_QUALITY);
219
220         offset.x = 0;
221         offset.y = 0;
222
223         iw = img.getWidth(this);
224         ih = img.getHeight(this);
225     
226         bi = new BufferedImage( iw, ih, BufferedImage.TYPE_INT_RGB);
227         big = bi.createGraphics();
228     
229         big.drawImage(img, 0, 0, this);
230         big.setPaint(Color.red);
231         if ((rect.width > 0) && (rect.height > 0))
232             big.draw(rect);
233
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);
239                 }
240         }
241         if (selected == 1)
242             shadeExt(big, 0, 0, 0, 64);
243         else if (selected == 2) {
244             shadeExt(big, 0, 0, 0, 255);
245             selected = 1;
246         }
247         g2.drawImage(bi, offset.x, offset.y, this);
248     }
249
250     private void shadeRect(Graphics2D g2, int r, int g, int b, int a)
251     {
252         g2.setPaint(new Color(r, g, b, a));
253         g2.fillRect(rect.x + 1, rect.y + 1, rect.width - 1, rect.height - 1);
254     }
255   
256     private void shadeExt(Graphics2D g2, int r, int g, int b, int a)
257     {
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_ */
265     }
266 }