27750de7af87f744ab94574544539bbdb07c435e
[openjpeg.git] / applications / jpip / util / 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", 50000);
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", 50000);
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 = null;
115         
116         try {
117             Socket imgdecSocket = new Socket( "localhost", 50000);
118             DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
119             DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
120             byte []header = new byte[7];
121             
122             os.writeBytes("PNM request\n");
123             if( cid != null)
124                 os.writeBytes( cid + "\n");
125             else
126                 if( tid != null)
127                     os.writeBytes( tid + "\n");
128                 else
129                     os.writeBytes( "0\n");
130             os.writeBytes( fw + "\n");
131             os.writeBytes( fh + "\n");
132
133             read_stream( is, header, 7);
134             
135             if( header[0] == 80){
136                 // P5: gray, P6: color  
137                 byte magicknum = header[1];
138                 if( magicknum == 5 || magicknum == 6){
139                     int c = magicknum==6 ? 3: 1;
140                     int w = (header[2]&0xff)<<8 | (header[3]&0xff);
141                     int h = (header[4]&0xff)<<8 | (header[5]&0xff);
142                     int maxval = header[6]&0xff;
143                     int length = w*h*c;
144                     
145                     if( maxval == 255 && length != 0){
146                         pnmstream = new PnmImage( c, w, h);
147                         read_stream( is, pnmstream.get_data(), length);
148                     }
149                     else
150                         System.err.println("Error in get_PNMstream(), only 255 is accepted");
151                 }
152                 else
153                     System.err.println("Error in get_PNMstream(), wrong magick number" + header[1]);
154             }
155             else
156                 System.err.println("Error in get_PNMstream(), Not starting with P");
157             
158             os.close();
159             is.close();
160             imgdecSocket.close();
161         } catch (UnknownHostException e) {
162             System.err.println("Trying to connect to unknown host: " + e);
163         } catch (IOException e) {
164             System.err.println("IOException: " + e);
165         }
166         return pnmstream;
167     }
168
169     public static byte [] get_XMLstream( String cid)
170     {
171         byte []xmldata = null;
172
173         try{
174             Socket imgdecSocket = new Socket( "localhost", 50000);
175             DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
176             DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
177             byte []header = new byte[5];
178             
179             os.writeBytes("XML request\n");
180             os.writeBytes( cid + "\n");
181       
182             read_stream( is, header, 5);
183             
184             if( header[0] == 88 && header[1] == 77 && header[2] == 76){
185                 int length = (header[3]&0xff)<<8 | (header[4]&0xff);
186         
187                 xmldata = new byte[ length];
188                 read_stream( is, xmldata, length);
189             }
190             else
191                 System.err.println("Error in get_XMLstream(), not starting with XML");
192         } catch (UnknownHostException e) {
193             System.err.println("Trying to connect to unknown host: " + e);
194         } catch (IOException e) {
195             System.err.println("IOException: " + e);
196         }
197         return xmldata;
198     }
199
200     public static String query_cid( String j2kfilename)
201     {
202         int []retmsglabel = new int[3];
203         retmsglabel[0] = 67;
204         retmsglabel[1] = 73;
205         retmsglabel[2] = 68;
206
207         return query_id( "CID request", j2kfilename, retmsglabel);
208     }
209
210     public static String query_tid( String j2kfilename)
211     {
212         int []retmsglabel = new int[3];
213         retmsglabel[0] = 84;
214         retmsglabel[1] = 73;
215         retmsglabel[2] = 68;
216
217         return query_id( "TID request", j2kfilename, retmsglabel);
218     }
219
220     public static String query_id( String reqmsghead, String j2kfilename, int[] retmsglabel)
221     {
222         String id = null;
223         
224         try{
225             Socket imgdecSocket = new Socket( "localhost", 50000);
226             DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
227             DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
228             byte []header = new byte[4];
229
230             os.writeBytes( reqmsghead + "\n");
231             os.writeBytes( j2kfilename + "\n");
232
233             read_stream( is, header, 4);
234             
235             if( header[0] == retmsglabel[0] && header[1] == retmsglabel[1] && header[2] == retmsglabel[2]){
236                 int length = header[3]&0xff;
237
238                 if( length > 0){
239                 
240                     byte []iddata = new byte[ length];
241                     read_stream( is, iddata, length);
242                     id = new String( iddata);
243                 }
244             }
245             else
246                 System.err.println("Error in query_id("+ reqmsghead + "), wrong to start with " + header);
247         }
248         catch (UnknownHostException e) {
249             System.err.println("Trying to connect to unknown host: " + e);
250         } catch (IOException e) {
251             System.err.println("IOException: " + e);
252         }
253
254         return id;      
255     }
256
257     public static java.awt.Dimension query_imagesize( String cid, String tid)
258     {
259         java.awt.Dimension dim = null;
260
261         try{
262             Socket imgdecSocket = new Socket( "localhost", 50000);
263             DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
264             DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
265             byte []header = new byte[3];
266
267             os.writeBytes( "SIZ request\n");
268             if( tid == null)
269                 os.writeBytes( "0\n");
270             else
271                 os.writeBytes( tid + "\n");
272             if( cid == null)
273                 os.writeBytes( "0\n");
274             else
275                 os.writeBytes( cid + "\n");
276
277             read_stream( is, header, 3);
278             
279             if( header[0] == 83 && header[1] == 73 && header[2] == 90){
280                 
281                 byte []data = new byte[ 3];
282                 read_stream( is, data, 3);
283                 int w = (data[0]&0xff)<<16 | (data[1]&0xff)<<8 | (data[2]&0xff);
284                 read_stream( is, data, 3);
285                 int h = (data[0]&0xff)<<16 | (data[1]&0xff)<<8 | (data[2]&0xff);
286                 dim = new java.awt.Dimension( w, h);
287             }
288             else
289                 System.err.println("Error in query_imagesize("+ cid + ", " + tid + "), wrong to start with " + header);
290         }
291         catch (UnknownHostException e) {
292             System.err.println("Trying to connect to unknown host: " + e);
293         } catch (IOException e) {
294             System.err.println("IOException: " + e);
295         }
296
297         return dim;
298     }
299   
300     public static void read_stream( DataInputStream is, byte []stream, int length)
301     {
302         int remlen = length;
303         int off = 0;
304
305         try{
306             while( remlen > 0){
307                 int redlen = is.read( stream, off, remlen);
308                 
309                 if( redlen == -1){
310                     System.err.println("    failed to read_stream()");
311                     break;
312                 }
313                 off += redlen;
314                 remlen -= redlen;
315             }
316         } catch (IOException e) {
317             System.err.println("IOException: " + e);
318         }
319     }
320
321     public static void destroy_cid( String cid)
322     {
323         try{
324             Socket imgdecSocket = new Socket( "localhost", 50000);
325             DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
326             DataInputStream  is = new DataInputStream( imgdecSocket.getInputStream());
327             
328             os.writeBytes("CID destroy\n");
329             os.writeBytes( cid + "\n");
330             
331             byte signal = is.readByte();
332       
333             if( signal == 0)
334                 System.err.println("    failed");
335         } catch (UnknownHostException e) {
336             System.err.println("Trying to connect to unknown host: " + e);
337         } catch (IOException e) {
338             System.err.println("IOException: " + e);
339         }
340     }
341 }