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 public static PnmImage decode_jpipstream( byte[] jpipstream, String tid, String cid, int fw, int fh)
38 if( jpipstream != null)
39 send_JPIPstream( jpipstream);
40 return get_PNMstream( cid, tid, fw, fh);
43 public static PnmImage decode_jpipstream( byte[] jpipstream, String j2kfilename, String tid, String cid, int fw, int fh)
45 send_JPIPstream( jpipstream, j2kfilename, tid, cid);
46 return get_PNMstream( cid, tid, fw, fh);
49 public static void send_JPIPstream( byte[] jpipstream)
52 Socket imgdecSocket = new Socket( "localhost", 50000);
53 DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
54 DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
56 System.err.println("Sending " + jpipstream.length + "Data Bytes to decodingServer");
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);
63 byte signal = is.readByte();
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);
74 public static void send_JPIPstream( byte[] jpipstream, String j2kfilename, String tid, String cid)
77 Socket imgdecSocket = new Socket( "localhost", 50000);
78 DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
79 DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
82 if( jpipstream != null)
83 length = jpipstream.length;
85 System.err.println("Sending " + length + "Data Bytes to decodingServer");
87 os.writeBytes("JPIP-stream\n");
88 os.writeBytes("version 1.2\n");
89 os.writeBytes( j2kfilename + "\n");
91 os.writeBytes( "0\n");
93 os.writeBytes( tid + "\n");
95 os.writeBytes( "0\n");
97 os.writeBytes( cid + "\n");
98 os.writeBytes( length + "\n");
99 os.write( jpipstream, 0, length);
101 byte signal = is.readByte();
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);
112 public static PnmImage get_PNMstream( String cid, String tid, int fw, int fh)
114 PnmImage pnmstream = null;
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];
122 os.writeBytes("PNM request\n");
124 os.writeBytes( cid + "\n");
127 os.writeBytes( tid + "\n");
129 os.writeBytes( "0\n");
130 os.writeBytes( fw + "\n");
131 os.writeBytes( fh + "\n");
133 read_stream( is, header, 7);
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;
145 if( maxval == 255 && length != 0){
146 pnmstream = new PnmImage( c, w, h);
147 read_stream( is, pnmstream.get_data(), length);
150 System.err.println("Error in get_PNMstream(), only 255 is accepted");
153 System.err.println("Error in get_PNMstream(), wrong magick number" + header[1]);
156 System.err.println("Error in get_PNMstream(), Not starting with P");
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);
169 public static byte [] get_XMLstream( String cid)
171 byte []xmldata = null;
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];
179 os.writeBytes("XML request\n");
180 os.writeBytes( cid + "\n");
182 read_stream( is, header, 5);
184 if( header[0] == 88 && header[1] == 77 && header[2] == 76){
185 int length = (header[3]&0xff)<<8 | (header[4]&0xff);
187 xmldata = new byte[ length];
188 read_stream( is, xmldata, length);
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);
200 public static String query_cid( String j2kfilename)
202 int []retmsglabel = new int[3];
207 return query_id( "CID request", j2kfilename, retmsglabel);
210 public static String query_tid( String j2kfilename)
212 int []retmsglabel = new int[3];
217 return query_id( "TID request", j2kfilename, retmsglabel);
220 public static String query_id( String reqmsghead, String j2kfilename, int[] retmsglabel)
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];
230 os.writeBytes( reqmsghead + "\n");
231 os.writeBytes( j2kfilename + "\n");
233 read_stream( is, header, 4);
235 if( header[0] == retmsglabel[0] && header[1] == retmsglabel[1] && header[2] == retmsglabel[2]){
236 int length = header[3]&0xff;
240 byte []iddata = new byte[ length];
241 read_stream( is, iddata, length);
242 id = new String( iddata);
246 System.err.println("Error in query_id("+ reqmsghead + "), wrong to start with " + header);
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);
257 public static java.awt.Dimension query_imagesize( String cid, String tid)
259 java.awt.Dimension dim = null;
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];
267 os.writeBytes( "SIZ request\n");
269 os.writeBytes( "0\n");
271 os.writeBytes( tid + "\n");
273 os.writeBytes( "0\n");
275 os.writeBytes( cid + "\n");
277 read_stream( is, header, 3);
279 if( header[0] == 83 && header[1] == 73 && header[2] == 90){
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);
289 System.err.println("Error in query_imagesize("+ cid + ", " + tid + "), wrong to start with " + header);
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);
300 public static void read_stream( DataInputStream is, byte []stream, int length)
307 int redlen = is.read( stream, off, remlen);
310 System.err.println(" failed to read_stream()");
316 } catch (IOException e) {
317 System.err.println("IOException: " + e);
321 public static void destroy_cid( String cid)
324 Socket imgdecSocket = new Socket( "localhost", 50000);
325 DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
326 DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
328 os.writeBytes("CID destroy\n");
329 os.writeBytes( cid + "\n");
331 byte signal = is.readByte();
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);