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