[1.5][JPIP] added execution argument to set port number for opj_dec_server, opj_viewer*
[openjpeg.git] / applications / jpip / util / opj_viewer / src / ImageManager.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.Image;
32
33 public class ImageManager extends JPIPHttpClient
34 {
35     private PnmImage pnmimage;
36     private int origwidth;
37     private int origheight;
38     private ImgdecClient imgdecoder;
39
40     public ImageManager( String uri, String host, int port)
41     {
42         super( uri);
43         imgdecoder = new ImgdecClient( host, port);
44         pnmimage = null;
45         origwidth = 0;
46         origheight = 0;
47     }
48     
49     public int getOrigWidth(){
50         if( origwidth == 0){
51             if( cid != null || tid != null){
52                 java.awt.Dimension dim = imgdecoder.query_imagesize( cid, tid);
53                 if( dim != null){
54                     origwidth = dim.width;
55                     origheight = dim.height;
56                 }
57             }
58             else
59                 System.err.println("Neither cid or tid obtained before to get Original Image Dimension");
60         }
61         return origwidth;
62     }
63     public int getOrigHeight(){ return origheight;}
64     
65     public Image getImage( String j2kfilename, int reqfw, int reqfh, boolean reqcnew, int reqaux, boolean reqJPP, boolean reqJPT)
66     {
67         System.err.println();
68         
69         String refcid = null;
70         byte[] jpipstream;
71         
72         // Todo: check if the cid is for the same stream type
73         if( reqcnew)
74             refcid = imgdecoder.query_cid( j2kfilename);
75         
76         if( refcid == null){
77             String reftid = imgdecoder.query_tid( j2kfilename);
78             if( reftid == null)
79                 jpipstream = super.requestViewWindow( j2kfilename, reqfw, reqfh, reqcnew, reqaux, reqJPP, reqJPT);
80             else
81                 jpipstream = super.requestViewWindow( j2kfilename, reftid, reqfw, reqfh, reqcnew, reqaux, reqJPP, reqJPT);
82         }
83         else
84             jpipstream = super.requestViewWindow( reqfw, reqfh, refcid, reqcnew, reqaux, reqJPP, reqJPT);
85         
86         System.err.println( "decoding to PNM image");
87         if((pnmimage = imgdecoder.decode_jpipstream( jpipstream, j2kfilename, tid, cid, fw, fh))!=null){
88             System.err.println( "     done");
89             return pnmimage.createROIImage( rx, ry, rw, rh);
90         }
91         else{
92             System.err.println( "     failed");
93             return null;
94         }
95     }
96     
97     public Image getImage( int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh)
98     {
99         System.err.println();
100         
101         byte[] jpipstream = super.requestViewWindow( reqfw, reqfh, reqrx, reqry, reqrw, reqrh);
102
103         System.err.println( "decoding to PNM image");
104         if((pnmimage = imgdecoder.decode_jpipstream( jpipstream, tid, cid, fw, fh)) != null){
105             System.err.println( "     done");
106             return pnmimage.createROIImage( rx, ry, rw, rh);
107         }
108         else{
109             System.err.println( "     failed");
110             return null;
111         }
112     }
113     
114     public byte[] getXML()
115     {
116         System.err.println();
117         
118         byte []xmldata = null;
119         byte[] jpipstream = super.requestXML();
120         
121         if( jpipstream != null){
122             imgdecoder.send_JPIPstream( jpipstream);
123       
124             xmldata = imgdecoder.get_XMLstream( cid);    
125         }
126         return xmldata;
127     }
128
129     public void closeChannel()
130     {
131         if( cid != null){
132             imgdecoder.destroy_cid( cid);
133             super.closeChannel();
134         }
135     }
136 }