opj_viewer enabled stateless requests
[openjpeg.git] / applications / jpip / opj_client / opj_viewer / src / ImgdecClient.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.io.*;
32 import java.net.*;
33
34 public class ImgdecClient{
35
36     public static PnmImage decode_jpipstream( byte[] jpipstream, String tid, String cid, int fw, int fh)
37     {
38         if( jpipstream != null)
39             send_JPIPstream( jpipstream);
40         return get_PNMstream( cid, tid, fw, fh);
41     }
42
43     public static PnmImage decode_jpipstream( byte[] jpipstream, String j2kfilename, String tid, String cid, int fw, int fh)
44     {
45         send_JPIPstream( jpipstream, j2kfilename, tid, cid);
46         return get_PNMstream( cid, tid, fw, fh);
47     }
48     
49     public static void send_JPIPstream( byte[] jpipstream)
50     {
51         try{
52             Socket imgdecSocket = new Socket( "localhost", 5000);
53             DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
54             DataInputStream  is = new DataInputStream( imgdecSocket.getInputStream());
55       
56             System.err.println("Sending " + jpipstream.length + "Data Bytes to decodingServer");
57             
58             os.writeBytes("JPIP-stream\n");
59             os.writeBytes("version 1.2\n");
60             os.writeBytes( jpipstream.length + "\n"); 
61             os.write( jpipstream, 0, jpipstream.length);
62       
63             byte signal = is.readByte();
64       
65             if( signal == 0)
66                 System.err.println("    failed");
67         } catch (UnknownHostException e) {
68             System.err.println("Trying to connect to unknown host: " + e);
69         } catch (IOException e) {
70             System.err.println("IOException: " + e);
71         }
72     }
73
74     public static void send_JPIPstream( byte[] jpipstream, String j2kfilename, String tid, String cid)
75     {
76         try{
77             Socket imgdecSocket = new Socket( "localhost", 5000);
78             DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
79             DataInputStream  is = new DataInputStream( imgdecSocket.getInputStream());
80             int length = 0;
81             
82             if( jpipstream != null)
83                 length = jpipstream.length;
84             
85             System.err.println("Sending " + length + "Data Bytes to decodingServer");
86       
87             os.writeBytes("JPIP-stream\n");
88             os.writeBytes("version 1.2\n");
89             os.writeBytes( j2kfilename + "\n");
90             if( tid == null)
91                 os.writeBytes( "0\n");
92             else
93                 os.writeBytes( tid + "\n");
94             if( cid == null)
95                 os.writeBytes( "0\n");
96             else
97                 os.writeBytes( cid + "\n");
98             os.writeBytes( length + "\n");
99             os.write( jpipstream, 0, length);
100             
101             byte signal = is.readByte();
102       
103             if( signal == 0)
104                 System.err.println("    failed");
105         } catch (UnknownHostException e) {
106             System.err.println("Trying to connect to unknown host: " + e);
107         } catch (IOException e) {
108             System.err.println("IOException: " + e);
109         }
110     }
111
112     public static PnmImage get_PNMstream( String cid, String tid, int fw, int fh)
113     {
114         PnmImage pnmstream = new PnmImage();
115         try {
116             Socket imgdecSocket = new Socket( "localhost", 5000);
117             DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
118             DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
119             byte []header = new byte[7];
120             
121             os.writeBytes("PNM request\n");
122             if( cid != null)
123                 os.writeBytes( cid + "\n");
124             else
125                 if( tid != null)
126                     os.writeBytes( tid + "\n");
127                 else
128                     os.writeBytes( "0\n");
129             os.writeBytes( fw + "\n");
130             os.writeBytes( fh + "\n");
131
132             read_stream( is, header, 7);
133             
134             if( header[0] == 80){
135                 // P5: gray, P6: color  
136                 byte magicknum = header[1];
137                 if( magicknum == 5 || magicknum == 6){
138                     int length;
139                     boolean iscolor = magicknum==6 ? true:false;
140                     if( iscolor)
141                         pnmstream.channel = 3;
142                     else
143                         pnmstream.channel = 1;
144                     pnmstream.width  = (header[2]&0xff)<<8 | (header[3]&0xff);
145                     pnmstream.height = (header[4]&0xff)<<8 | (header[5]&0xff);
146                     int maxval = header[6]&0xff;
147           
148                     if( maxval == 255){
149                         length = pnmstream.width*pnmstream.height*pnmstream.channel;
150                         pnmstream.data = new byte [ length];
151                         read_stream( is, pnmstream.data, length);
152                     }
153                     else
154                         System.err.println("Error in get_PNMstream(), only 255 is accepted");
155                 }
156                 else
157                     System.err.println("Error in get_PNMstream(), wrong magick number" + header[1]);
158             }
159             else
160                 System.err.println("Error in get_PNMstream(), Not starting with P");
161             os.close();
162             is.close();
163             imgdecSocket.close();
164         } catch (UnknownHostException e) {
165             System.err.println("Trying to connect to unknown host: " + e);
166         } catch (IOException e) {
167             System.err.println("IOException: " + e);
168         }
169         return pnmstream;
170     }
171
172     public static byte [] get_XMLstream( String cid)
173     {
174         byte []xmldata = null;
175
176         try{
177             Socket imgdecSocket = new Socket( "localhost", 5000);
178             DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
179             DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
180             byte []header = new byte[5];
181             
182             os.writeBytes("XML request\n");
183             os.writeBytes( cid + "\n");
184       
185             read_stream( is, header, 5);
186             
187             if( header[0] == 88 && header[1] == 77 && header[2] == 76){
188                 int length = (header[3]&0xff)<<8 | (header[4]&0xff);
189         
190                 xmldata = new byte[ length];
191                 read_stream( is, xmldata, length);
192             }
193             else
194                 System.err.println("Error in get_XMLstream(), not starting with XML");
195         } catch (UnknownHostException e) {
196             System.err.println("Trying to connect to unknown host: " + e);
197         } catch (IOException e) {
198             System.err.println("IOException: " + e);
199         }
200         return xmldata;
201     }
202
203     public static String query_cid( String j2kfilename)
204     {
205         int []retmsglabel = new int[3];
206         retmsglabel[0] = 67;
207         retmsglabel[1] = 73;
208         retmsglabel[2] = 68;
209
210         return query_id( "CID request", j2kfilename, retmsglabel);
211     }
212
213     public static String query_tid( String j2kfilename)
214     {
215         int []retmsglabel = new int[3];
216         retmsglabel[0] = 84;
217         retmsglabel[1] = 73;
218         retmsglabel[2] = 68;
219
220         return query_id( "TID request", j2kfilename, retmsglabel);
221     }
222
223     public static String query_id( String reqmsghead, String j2kfilename, int[] retmsglabel)
224     {
225         String id = null;
226         
227         try{
228             Socket imgdecSocket = new Socket( "localhost", 5000);
229             DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
230             DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
231             byte []header = new byte[4];
232
233             os.writeBytes( reqmsghead + "\n");
234             os.writeBytes( j2kfilename + "\n");
235
236             read_stream( is, header, 4);
237             
238             if( header[0] == retmsglabel[0] && header[1] == retmsglabel[1] && header[2] == retmsglabel[2]){
239                 int length = header[3]&0xff;
240
241                 if( length > 0){
242                 
243                     byte []iddata = new byte[ length];
244                     read_stream( is, iddata, length);
245                     id = new String( iddata);
246                 }
247             }
248             else
249                 System.err.println("Error in query_id("+ reqmsghead + "), wrong to start with " + header);
250         }
251         catch (UnknownHostException e) {
252             System.err.println("Trying to connect to unknown host: " + e);
253         } catch (IOException e) {
254             System.err.println("IOException: " + e);
255         }
256
257         return id;      
258     }
259   
260     public static void read_stream( DataInputStream is, byte []stream, int length)
261     {
262         int remlen = length;
263         int off = 0;
264
265         try{
266             while( remlen > 0){
267                 int redlen = is.read( stream, off, remlen);
268
269                 if( redlen == -1){
270                     System.err.println("    failed to read_stream()");
271                     break;
272                 }
273                 off += redlen;
274                 remlen -= redlen;
275             }
276         } catch (IOException e) {
277             System.err.println("IOException: " + e);
278         }
279     }
280
281     public static void destroy_cid( String cid)
282     {
283         try{
284             Socket imgdecSocket = new Socket( "localhost", 5000);
285             DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
286             DataInputStream  is = new DataInputStream( imgdecSocket.getInputStream());
287             
288             os.writeBytes("CID destroy\n");
289             os.writeBytes( cid + "\n");
290             
291             byte signal = is.readByte();
292       
293             if( signal == 0)
294                 System.err.println("    failed");
295         } catch (UnknownHostException e) {
296             System.err.println("Trying to connect to unknown host: " + e);
297         } catch (IOException e) {
298             System.err.println("IOException: " + e);
299         }
300     }
301 }