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