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", 5000);
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", 5000);
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 = new PnmImage();
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];
121 os.writeBytes("PNM request\n");
123 os.writeBytes( cid + "\n");
126 os.writeBytes( tid + "\n");
128 os.writeBytes( "0\n");
129 os.writeBytes( fw + "\n");
130 os.writeBytes( fh + "\n");
132 read_stream( is, header, 7);
134 if( header[0] == 80){
135 // P5: gray, P6: color
136 byte magicknum = header[1];
137 if( magicknum == 5 || magicknum == 6){
139 boolean iscolor = magicknum==6 ? true:false;
141 pnmstream.channel = 3;
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;
149 length = pnmstream.width*pnmstream.height*pnmstream.channel;
150 pnmstream.data = new byte [ length];
151 read_stream( is, pnmstream.data, length);
154 System.err.println("Error in get_PNMstream(), only 255 is accepted");
157 System.err.println("Error in get_PNMstream(), wrong magick number" + header[1]);
160 System.err.println("Error in get_PNMstream(), Not starting with P");
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);
172 public static byte [] get_XMLstream( String cid)
174 byte []xmldata = null;
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];
182 os.writeBytes("XML request\n");
183 os.writeBytes( cid + "\n");
185 read_stream( is, header, 5);
187 if( header[0] == 88 && header[1] == 77 && header[2] == 76){
188 int length = (header[3]&0xff)<<8 | (header[4]&0xff);
190 xmldata = new byte[ length];
191 read_stream( is, xmldata, length);
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);
203 public static String query_cid( String j2kfilename)
205 int []retmsglabel = new int[3];
210 return query_id( "CID request", j2kfilename, retmsglabel);
213 public static String query_tid( String j2kfilename)
215 int []retmsglabel = new int[3];
220 return query_id( "TID request", j2kfilename, retmsglabel);
223 public static String query_id( String reqmsghead, String j2kfilename, int[] retmsglabel)
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];
233 os.writeBytes( reqmsghead + "\n");
234 os.writeBytes( j2kfilename + "\n");
236 read_stream( is, header, 4);
238 if( header[0] == retmsglabel[0] && header[1] == retmsglabel[1] && header[2] == retmsglabel[2]){
239 int length = header[3]&0xff;
243 byte []iddata = new byte[ length];
244 read_stream( is, iddata, length);
245 id = new String( iddata);
249 System.err.println("Error in query_id("+ reqmsghead + "), wrong to start with " + header);
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);
260 public static void read_stream( DataInputStream is, byte []stream, int length)
267 int redlen = is.read( stream, off, remlen);
270 System.err.println(" failed to read_stream()");
276 } catch (IOException e) {
277 System.err.println("IOException: " + e);
281 public static void destroy_cid( String cid)
284 Socket imgdecSocket = new Socket( "localhost", 5000);
285 DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream());
286 DataInputStream is = new DataInputStream( imgdecSocket.getInputStream());
288 os.writeBytes("CID destroy\n");
289 os.writeBytes( cid + "\n");
291 byte signal = is.readByte();
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);