start using API style in openJPIP library
[openjpeg.git] / applications / jpip / libopenjpip / openjpip.h
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 #ifndef         OPENJPIP_H_
32 # define        OPENJPIP_H_
33
34 #include "session_manager.h"
35 #include "target_manager.h"
36 #include "query_parser.h"
37 #include "msgqueue_manager.h"
38 #include "bool.h"
39
40 #include "cache_manager.h"
41 #include "byte_manager.h"
42 #include "imgsock_manager.h"
43
44 #include "metadata_manager.h"
45 #include "ihdrbox_manager.h"
46 #include "index_manager.h"
47
48 #ifdef SERVER
49 #include "fcgi_stdio.h"
50 #define logstream FCGI_stdout
51 #else
52 #define FCGI_stdout stdout
53 #define FCGI_stderr stderr
54 #define logstream stderr
55 #endif //SERVER
56
57 /* 
58  *==========================================================
59  * JPIP server API
60  *==========================================================
61  */
62
63 //! Server static records
64 typedef struct server_record{
65   sessionlist_param_t *sessionlist; //!< list of session records
66   targetlist_param_t *targetlist;   //!< list of target records
67 } server_record_t;
68
69 //! Query/response data for each client
70 typedef struct QR{
71   query_param_t *query;
72   msgqueue_param_t *msgqueue;
73 } QR_t;
74
75 /**
76  * Initialize the JPIP server
77  *
78  * @return intialized server record pointer
79  */
80 server_record_t * init_JPIPserver();
81
82 /**
83  * Terminate the JPIP server
84  *
85  * @param[in] rec address of deleting server static record pointer
86  */
87 void terminate_JPIPserver( server_record_t **rec);
88
89 /**
90  * 1st process per client request; parse query string
91  *
92  * @param[in]  query_string request query string
93  * @return     initialized query/response data pointer
94  */
95 QR_t * parse_querystring( char *query_string);
96
97 /**
98  * 2nd process; process JPIP request and construct message queue
99  *
100  * @param[in]  rec server static record pointer
101  * @param[in]  qr  query/response data pointer
102  * @return     true if succeed, otherwise false 
103  */
104 bool process_JPIPrequest( server_record_t *rec, QR_t *qr);
105
106 /**
107  * 3rd process; send response data JPT/JPP-stream
108  *
109  * @param[in]  qr  query/response data pointer
110  */
111 void send_responsedata( QR_t *qr);
112
113 /**
114  * 4th (last) process; 
115  *
116  * @param[in]  rec server static record pinter
117  * @param[in]  qr  address of query/response data pointer
118  */
119 void end_QRprocess( server_record_t *rec, QR_t **qr);
120
121 #ifndef SERVER
122
123 /**
124  * Option for local tests; print out parameter values to logstream (stderr)
125  *
126  * @param[in]  query    true if query parameters are to be printed out
127  * @param[in]  messages true if queue of messages is to be printed out
128  * @param[in]  sessions true if session list      is to be printed out
129  * @param[in]  targets  true if target list       is  to be printed out
130  * @param[in]  qr       query/response data pointer
131  * @param[in]  rec      server static record pinter
132  */
133 void local_log( bool query, bool messages, bool sessions, bool targets, QR_t *qr, server_record_t *rec);
134
135 #endif //SERVER
136
137 /* 
138  *==========================================================
139  *      JPIP decoding server API
140  *==========================================================
141  */
142
143 #ifndef SERVER
144
145 //! Decoding server static records
146 typedef struct dec_server_record{
147   cachelist_param_t *cachelist; //!< cache list
148   Byte_t *jpipstream;           //!< JPT/JPP stream
149   int jpipstreamlen;            //!< length of jpipstream
150   msgqueue_param_t *msgqueue;   //!< parsed message queue of jpipstream
151   SOCKET listening_socket;      //!< listenning socket
152 } dec_server_record_t;
153
154
155 //! Client socket identifier
156 typedef SOCKET client_t;
157
158 /**
159  * Initialize the image decoding server
160  *
161  * @return intialized decoding server record pointer
162  */
163 dec_server_record_t * init_dec_server();
164
165 /**
166  * Terminate the  image decoding server
167  *
168  * @param[in] rec address of deleting decoding server static record pointer
169  */
170 void terminate_dec_server( dec_server_record_t **rec);
171
172 /**
173  * Accept client connection
174  *
175  * @param[in] rec decoding server static record pointer
176  * @return        client socket ID, -1 if failed
177  */
178 client_t accept_connection( dec_server_record_t *rec);
179
180  /**
181   * Handle client request
182   *
183   * @param[in] client client socket ID
184   * @param[in] rec    decoding server static record pointer
185   * @return           true if succeed
186   */
187 bool handle_clientreq( client_t client, dec_server_record_t *rec);
188
189  #endif //SERVER
190
191 /* 
192  *==========================================================
193  *     JPIP tool API
194  *==========================================================
195  */
196
197 #ifndef SERVER
198
199 /*
200  * jpip to JP2 or J2K
201  */
202
203 //! JPIP decoding parameters
204 typedef struct jpip_dec_param{
205   Byte_t *jpipstream;                 //!< JPT/JPP-stream
206   Byte8_t jpiplen;                    //!< length of jpipstream
207   msgqueue_param_t *msgqueue;         //!< message queue
208   metadatalist_param_t *metadatalist; //!< metadata list going into JP2 file
209   ihdrbox_param_t *ihdrbox;           //!< ihdr box going into JP2 file
210   Byte_t *jp2kstream;                 //!< J2K codestream or JP2 file codestream
211   Byte8_t jp2klen;                    //!< length of j2kstream or JP2 file
212 } jpip_dec_param_t;
213
214 /**
215  * Initialize jpip decoder
216  *
217  * @param[in] jp2 true in case of jp2 file encoding, else j2k file encoding
218  * @return        JPIP decoding parameters pointer
219  */
220 jpip_dec_param_t * init_jpipdecoder( bool jp2);
221
222 /**
223  * Destroy jpip decoding parameters
224  *
225  * @param[in]  dec  address of JPIP decoding parameters pointer
226  */
227 void destroy_jpipdecoder( jpip_dec_param_t **dec);
228
229 /**
230  * Read jpip codestream from a file
231  *
232  * @param[in]  fname file name
233  * @param[in]  dec   JPIP decoding parameters pointer
234  * @return           true if succeed
235  */
236 bool fread_jpip( char fname[], jpip_dec_param_t *dec);
237
238 /**
239  * Decode jpip codestream
240  *
241  * @param[in]  dec   JPIP decoding parameters pointer
242  */
243 void decode_jpip( jpip_dec_param_t *dec);
244
245 /**
246  * Write J2K/JP2 codestream to a file
247  *
248  * @param[in]  fname file name
249  * @param[in]  dec   JPIP decoding parameters pointer
250  * @return           true if succeed
251  */
252 bool fwrite_jp2k( char fname[], jpip_dec_param_t *dec);
253
254 /**
255  * Option; print out parameter values to stderr
256  *
257  * @param[in]  messages true if queue of messages is to be printed out
258  * @param[in]  metadata true if metadata          is to be printed out
259  * @param[in]  ihdrbox  true if image header data is to be printed out
260  * @param[in]  dec   JPIP decoding parameters pointer
261  */
262 void output_log( bool messages, bool metadata, bool ihdrbox, jpip_dec_param_t *dec);
263
264 /*
265  *  test the format of index (cidx) box in JP2 file
266  */
267
268 //! Redefinition of index parameters
269 typedef index_param_t index_t;
270
271 /**
272  * Parse JP2 file and get index information from cidx box inside
273  * 
274  * @param[in] fd file descriptor of the JP2 file
275  * @return       pointer to the generated structure of index parameters
276  */
277 index_t * get_index_from_JP2file( int fd);
278
279 /**
280  * Destroy index parameters
281  *
282  * @param[in,out] idx addressof the index pointer
283  */
284 void destroy_index( index_t **idx);
285
286
287 /**
288  * print index parameters
289  *
290  * @param[in] index index parameters
291  */
292 void output_index( index_t *index);
293
294 #endif //SERVER
295
296 #endif /* !OPENJPIP_H_ */