diff options
| author | Kaori Hagihara <khagihara@users.noreply.github.com> | 2011-10-19 23:29:57 +0000 |
|---|---|---|
| committer | Kaori Hagihara <khagihara@users.noreply.github.com> | 2011-10-19 23:29:57 +0000 |
| commit | 255fcbc3a5fc991316e9fda1a3398c55e82d0f21 (patch) | |
| tree | 108619e23e356373adaa35118f9290ff34be71ac /applications/jpip/util/opj_viewer/src/ImgdecClient.java | |
| parent | 47d93279ff813e0d22bfe83b881d1cd5045835db (diff) | |
start using API style in openJPIP library
Diffstat (limited to 'applications/jpip/util/opj_viewer/src/ImgdecClient.java')
| -rw-r--r-- | applications/jpip/util/opj_viewer/src/ImgdecClient.java | 298 |
1 files changed, 298 insertions, 0 deletions
diff --git a/applications/jpip/util/opj_viewer/src/ImgdecClient.java b/applications/jpip/util/opj_viewer/src/ImgdecClient.java new file mode 100644 index 00000000..35c97128 --- /dev/null +++ b/applications/jpip/util/opj_viewer/src/ImgdecClient.java @@ -0,0 +1,298 @@ +/* + * $Id$ + * + * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium + * Copyright (c) 2002-2011, Professor Benoit Macq + * Copyright (c) 2010-2011, Kaori Hagihara + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +import java.io.*; +import java.net.*; + +public class ImgdecClient{ + + public static PnmImage decode_jpipstream( byte[] jpipstream, String tid, String cid, int fw, int fh) + { + if( jpipstream != null) + send_JPIPstream( jpipstream); + return get_PNMstream( cid, tid, fw, fh); + } + + public static PnmImage decode_jpipstream( byte[] jpipstream, String j2kfilename, String tid, String cid, int fw, int fh) + { + send_JPIPstream( jpipstream, j2kfilename, tid, cid); + return get_PNMstream( cid, tid, fw, fh); + } + + public static void send_JPIPstream( byte[] jpipstream) + { + try{ + Socket imgdecSocket = new Socket( "localhost", 5000); + DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream()); + DataInputStream is = new DataInputStream( imgdecSocket.getInputStream()); + + System.err.println("Sending " + jpipstream.length + "Data Bytes to decodingServer"); + + os.writeBytes("JPIP-stream\n"); + os.writeBytes("version 1.2\n"); + os.writeBytes( jpipstream.length + "\n"); + os.write( jpipstream, 0, jpipstream.length); + + byte signal = is.readByte(); + + if( signal == 0) + System.err.println(" failed"); + } catch (UnknownHostException e) { + System.err.println("Trying to connect to unknown host: " + e); + } catch (IOException e) { + System.err.println("IOException: " + e); + } + } + + public static void send_JPIPstream( byte[] jpipstream, String j2kfilename, String tid, String cid) + { + try{ + Socket imgdecSocket = new Socket( "localhost", 5000); + DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream()); + DataInputStream is = new DataInputStream( imgdecSocket.getInputStream()); + int length = 0; + + if( jpipstream != null) + length = jpipstream.length; + + System.err.println("Sending " + length + "Data Bytes to decodingServer"); + + os.writeBytes("JPIP-stream\n"); + os.writeBytes("version 1.2\n"); + os.writeBytes( j2kfilename + "\n"); + if( tid == null) + os.writeBytes( "0\n"); + else + os.writeBytes( tid + "\n"); + if( cid == null) + os.writeBytes( "0\n"); + else + os.writeBytes( cid + "\n"); + os.writeBytes( length + "\n"); + os.write( jpipstream, 0, length); + + byte signal = is.readByte(); + + if( signal == 0) + System.err.println(" failed"); + } catch (UnknownHostException e) { + System.err.println("Trying to connect to unknown host: " + e); + } catch (IOException e) { + System.err.println("IOException: " + e); + } + } + + public static PnmImage get_PNMstream( String cid, String tid, int fw, int fh) + { + PnmImage pnmstream = null; + + try { + Socket imgdecSocket = new Socket( "localhost", 5000); + DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream()); + DataInputStream is = new DataInputStream( imgdecSocket.getInputStream()); + byte []header = new byte[7]; + + os.writeBytes("PNM request\n"); + if( cid != null) + os.writeBytes( cid + "\n"); + else + if( tid != null) + os.writeBytes( tid + "\n"); + else + os.writeBytes( "0\n"); + os.writeBytes( fw + "\n"); + os.writeBytes( fh + "\n"); + + read_stream( is, header, 7); + + if( header[0] == 80){ + // P5: gray, P6: color + byte magicknum = header[1]; + if( magicknum == 5 || magicknum == 6){ + int c = magicknum==6 ? 3: 1; + int w = (header[2]&0xff)<<8 | (header[3]&0xff); + int h = (header[4]&0xff)<<8 | (header[5]&0xff); + int maxval = header[6]&0xff; + int length = w*h*c; + + if( maxval == 255 && length != 0){ + pnmstream = new PnmImage( c, w, h); + read_stream( is, pnmstream.get_data(), length); + } + else + System.err.println("Error in get_PNMstream(), only 255 is accepted"); + } + else + System.err.println("Error in get_PNMstream(), wrong magick number" + header[1]); + } + else + System.err.println("Error in get_PNMstream(), Not starting with P"); + + os.close(); + is.close(); + imgdecSocket.close(); + } catch (UnknownHostException e) { + System.err.println("Trying to connect to unknown host: " + e); + } catch (IOException e) { + System.err.println("IOException: " + e); + } + return pnmstream; + } + + public static byte [] get_XMLstream( String cid) + { + byte []xmldata = null; + + try{ + Socket imgdecSocket = new Socket( "localhost", 5000); + DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream()); + DataInputStream is = new DataInputStream( imgdecSocket.getInputStream()); + byte []header = new byte[5]; + + os.writeBytes("XML request\n"); + os.writeBytes( cid + "\n"); + + read_stream( is, header, 5); + + if( header[0] == 88 && header[1] == 77 && header[2] == 76){ + int length = (header[3]&0xff)<<8 | (header[4]&0xff); + + xmldata = new byte[ length]; + read_stream( is, xmldata, length); + } + else + System.err.println("Error in get_XMLstream(), not starting with XML"); + } catch (UnknownHostException e) { + System.err.println("Trying to connect to unknown host: " + e); + } catch (IOException e) { + System.err.println("IOException: " + e); + } + return xmldata; + } + + public static String query_cid( String j2kfilename) + { + int []retmsglabel = new int[3]; + retmsglabel[0] = 67; + retmsglabel[1] = 73; + retmsglabel[2] = 68; + + return query_id( "CID request", j2kfilename, retmsglabel); + } + + public static String query_tid( String j2kfilename) + { + int []retmsglabel = new int[3]; + retmsglabel[0] = 84; + retmsglabel[1] = 73; + retmsglabel[2] = 68; + + return query_id( "TID request", j2kfilename, retmsglabel); + } + + public static String query_id( String reqmsghead, String j2kfilename, int[] retmsglabel) + { + String id = null; + + try{ + Socket imgdecSocket = new Socket( "localhost", 5000); + DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream()); + DataInputStream is = new DataInputStream( imgdecSocket.getInputStream()); + byte []header = new byte[4]; + + os.writeBytes( reqmsghead + "\n"); + os.writeBytes( j2kfilename + "\n"); + + read_stream( is, header, 4); + + if( header[0] == retmsglabel[0] && header[1] == retmsglabel[1] && header[2] == retmsglabel[2]){ + int length = header[3]&0xff; + + if( length > 0){ + + byte []iddata = new byte[ length]; + read_stream( is, iddata, length); + id = new String( iddata); + } + } + else + System.err.println("Error in query_id("+ reqmsghead + "), wrong to start with " + header); + } + catch (UnknownHostException e) { + System.err.println("Trying to connect to unknown host: " + e); + } catch (IOException e) { + System.err.println("IOException: " + e); + } + + return id; + } + + public static void read_stream( DataInputStream is, byte []stream, int length) + { + int remlen = length; + int off = 0; + + try{ + while( remlen > 0){ + int redlen = is.read( stream, off, remlen); + + if( redlen == -1){ + System.err.println(" failed to read_stream()"); + break; + } + off += redlen; + remlen -= redlen; + } + } catch (IOException e) { + System.err.println("IOException: " + e); + } + } + + public static void destroy_cid( String cid) + { + try{ + Socket imgdecSocket = new Socket( "localhost", 5000); + DataOutputStream os = new DataOutputStream( imgdecSocket.getOutputStream()); + DataInputStream is = new DataInputStream( imgdecSocket.getInputStream()); + + os.writeBytes("CID destroy\n"); + os.writeBytes( cid + "\n"); + + byte signal = is.readByte(); + + if( signal == 0) + System.err.println(" failed"); + } catch (UnknownHostException e) { + System.err.println("Trying to connect to unknown host: " + e); + } catch (IOException e) { + System.err.println("IOException: " + e); + } + } +} |
