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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
34 public class ImgdecClient{
36 private String hostname;
39 public ImgdecClient( String host, int port)
45 public PnmImage decode_jpipstream( byte[] jpipstream, String tid, String cid, int fw, int fh)
47 if( jpipstream != null)
48 send_JPIPstream( jpipstream);
49 return get_PNMstream( cid, tid, fw, fh);
52 public PnmImage decode_jpipstream( byte[] jpipstream, String j2kfilename, String tid, String cid, int fw, int fh)
54 send_JPIPstream( jpipstream, j2kfilename, tid, cid);
55 return get_PNMstream( cid, tid, fw, fh);
58 public void send_JPIPstream( byte[] jpipstream)
61 Socket imgdecSocket = new Socket( hostname, portNo);
62 DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
63 DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
65 System.err.println("Sending " + jpipstream.length + "Data Bytes to decodingServer");
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);
72 byte signal = is.readByte();
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);
83 public void send_JPIPstream( byte[] jpipstream, String j2kfilename, String tid, String cid)
86 Socket imgdecSocket = new Socket( hostname, portNo);
87 DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
88 DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
91 if( jpipstream != null)
92 length = jpipstream.length;
94 System.err.println("Sending " + length + "Data Bytes to decodingServer");
96 os.writeBytes("JPIP-stream\n");
97 os.writeBytes("version 1.2\n");
98 os.writeBytes( j2kfilename + "\n");
100 os.writeBytes( "0\n");
102 os.writeBytes( tid + "\n");
104 os.writeBytes( "0\n");
106 os.writeBytes( cid + "\n");
107 os.writeBytes( length + "\n");
108 os.write( jpipstream, 0, length);
110 byte signal = is.readByte();
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);
121 public PnmImage get_PNMstream( String cid, String tid, int fw, int fh)
123 PnmImage pnmstream = null;
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];
131 os.writeBytes("PNM request\n");
133 os.writeBytes( cid + "\n");
136 os.writeBytes( tid + "\n");
138 os.writeBytes( "0\n");
139 os.writeBytes( fw + "\n");
140 os.writeBytes( fh + "\n");
142 read_stream( is, header, 7);
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;
154 if( maxval == 255 && length != 0){
155 pnmstream = new PnmImage( c, w, h);
156 read_stream( is, pnmstream.get_data(), length);
159 System.err.println("Error in get_PNMstream(), only 255 is accepted");
162 System.err.println("Error in get_PNMstream(), wrong magick number" + header[1]);
165 System.err.println("Error in get_PNMstream(), Not starting with P");
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);
178 public byte [] get_XMLstream( String cid)
180 byte []xmldata = null;
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];
188 os.writeBytes("XML request\n");
189 os.writeBytes( cid + "\n");
191 read_stream( is, header, 5);
193 if( header[0] == 88 && header[1] == 77 && header[2] == 76){
194 int length = (header[3]&0xff)<<8 | (header[4]&0xff);
196 xmldata = new byte[ length];
197 read_stream( is, xmldata, length);
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);
209 public String query_cid( String j2kfilename)
211 int []retmsglabel = new int[3];
216 return query_id( "CID request", j2kfilename, retmsglabel);
219 public String query_tid( String j2kfilename)
221 int []retmsglabel = new int[3];
226 return query_id( "TID request", j2kfilename, retmsglabel);
229 public String query_id( String reqmsghead, String j2kfilename, int[] retmsglabel)
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];
239 os.writeBytes( reqmsghead + "\n");
240 os.writeBytes( j2kfilename + "\n");
242 read_stream( is, header, 4);
244 if( header[0] == retmsglabel[0] && header[1] == retmsglabel[1] && header[2] == retmsglabel[2]){
245 int length = header[3]&0xff;
249 byte []iddata = new byte[ length];
250 read_stream( is, iddata, length);
251 id = new String( iddata);
255 System.err.println("Error in query_id("+ reqmsghead + "), wrong to start with " + header);
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);
266 public java.awt.Dimension query_imagesize( String cid, String tid)
268 java.awt.Dimension dim = null;
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];
276 os.writeBytes( "SIZ request\n");
278 os.writeBytes( "0\n");
280 os.writeBytes( tid + "\n");
282 os.writeBytes( "0\n");
284 os.writeBytes( cid + "\n");
286 read_stream( is, header, 3);
288 if( header[0] == 83 && header[1] == 73 && header[2] == 90){
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);
298 System.err.println("Error in query_imagesize("+ cid + ", " + tid + "), wrong to start with " + header);
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);
309 private static void read_stream( DataInputStream is, byte []stream, int length)
316 int redlen = is.read( stream, off, remlen);
319 System.err.println(" failed to read_stream()");
325 } catch (IOException e) {
326 System.err.println("IOException: " + e);
330 public void destroy_cid( String cid)
333 Socket imgdecSocket = new Socket( hostname, portNo);
334 DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
335 DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
337 os.writeBytes("CID destroy\n");
338 os.writeBytes( cid + "\n");
340 byte signal = is.readByte();
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);