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.
36 #include "dec_clientmsg_handler.h"
37 #include "ihdrbox_manager.h"
38 #include "jpipstream_manager.h"
39 #include "jp2k_encoder.h"
40 #include "opj_inttypes.h"
42 void handle_JPIPstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist,
43 Byte_t **jpipstream, OPJ_SIZE_T *streamlen, msgqueue_param_t *msgqueue)
45 Byte_t *newjpipstream;
46 OPJ_SIZE_T newstreamlen = 0;
48 char *target, *tid, *cid;
49 metadatalist_param_t *metadatalist;
51 newjpipstream = receive_JPIPstream( connected_socket, &target, &tid, &cid, &newstreamlen);
53 fprintf( stderr, "newjpipstream length: %" PRIu64 "\n", newstreamlen);
55 parse_JPIPstream( newjpipstream, newstreamlen, (OPJ_OFF_T)*streamlen, msgqueue);
57 *jpipstream = update_JPIPstream( newjpipstream, newstreamlen, *jpipstream, streamlen);
58 opj_free( newjpipstream);
60 metadatalist = gene_metadatalist();
61 parse_metamsg( msgqueue, *jpipstream, *streamlen, metadatalist);
63 assert( msgqueue->last );
64 assert( msgqueue->last->csn < INT_MAX );
67 if((cache = search_cache( target, cachelist))){
69 update_cachetid( tid, cache);
71 add_cachecid( cid, cache);
74 cache = gene_cache( target, (int)msgqueue->last->csn, tid, cid);
75 insert_cache_into_list( cache, cachelist);
79 cache = search_cacheBycsn( (int)msgqueue->last->csn, cachelist);
81 if( cache->metadatalist)
82 delete_metadatalist( &cache->metadatalist);
83 cache->metadatalist = metadatalist;
85 if( target) opj_free( target);
86 if( tid) opj_free( tid);
87 if( cid) opj_free( cid);
89 response_signal( connected_socket, OPJ_TRUE);
92 void handle_PNMreqMSG( SOCKET connected_socket, Byte_t *jpipstream, msgqueue_param_t *msgqueue, cachelist_param_t *cachelist)
95 ihdrbox_param_t *ihdrbox;
96 char *CIDorTID, tmp[10];
101 CIDorTID = receive_string( connected_socket);
103 if(!(cache = search_cacheBycid( CIDorTID, cachelist)))
104 if(!(cache = search_cacheBytid( CIDorTID, cachelist))){
111 receive_line( connected_socket, tmp);
114 receive_line( connected_socket, tmp);
118 assert( cache->csn >= 0 );
119 pnmstream = jpipstream_to_pnm( jpipstream, msgqueue, (Byte8_t)cache->csn, fw, fh, &ihdrbox);
121 maxval = ihdrbox->bpc > 8 ? 255 : (1 << ihdrbox->bpc) - 1;
122 send_PNMstream( connected_socket, pnmstream, ihdrbox->width, ihdrbox->height, ihdrbox->nc, (Byte_t)maxval );
125 opj_free( pnmstream);
128 void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jpipstream, cachelist_param_t *cachelist)
131 cache_param_t *cache;
132 boxcontents_param_t *boxcontents;
135 cid = receive_string( connected_socket);
137 if(!(cache = search_cacheBycid( cid, cachelist))){
144 boxcontents = cache->metadatalist->last->boxcontents;
145 xmlstream = (Byte_t *)opj_malloc( boxcontents->length);
146 memcpy( xmlstream, jpipstream+boxcontents->offset, boxcontents->length);
147 send_XMLstream( connected_socket, xmlstream, boxcontents->length);
148 opj_free( xmlstream);
151 void handle_TIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
153 char *target, *tid = NULL;
154 cache_param_t *cache;
155 OPJ_SIZE_T tidlen = 0;
157 target = receive_string( connected_socket);
158 cache = search_cache( target, cachelist);
164 tidlen = strlen(tid);
166 send_TIDstream( connected_socket, tid, tidlen);
169 void handle_CIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
171 char *target, *cid = NULL;
172 cache_param_t *cache;
173 OPJ_SIZE_T cidlen = 0;
175 target = receive_string( connected_socket);
176 cache = search_cache( target, cachelist);
181 if( cache->numOfcid > 0){
182 cid = cache->cid[ cache->numOfcid-1];
183 cidlen = strlen(cid);
186 send_CIDstream( connected_socket, cid, cidlen);
189 void handle_dstCIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
193 cid = receive_string( connected_socket);
194 remove_cachecid( cid, cachelist);
195 response_signal( connected_socket, OPJ_TRUE);
200 void handle_SIZreqMSG( SOCKET connected_socket, Byte_t *jpipstream, msgqueue_param_t *msgqueue, cachelist_param_t *cachelist)
203 cache_param_t *cache;
204 Byte4_t width, height;
206 tid = receive_string( connected_socket);
207 cid = receive_string( connected_socket);
212 cache = search_cacheBytid( tid, cachelist);
214 if( !cache && cid[0] != '0')
215 cache = search_cacheBycid( cid, cachelist);
222 assert( cache->csn >= 0);
224 cache->ihdrbox = get_SIZ_from_jpipstream( jpipstream, msgqueue, (Byte8_t)cache->csn);
225 width = cache->ihdrbox->width;
226 height = cache->ihdrbox->height;
228 send_SIZstream( connected_socket, width, height);
231 void handle_JP2saveMSG( SOCKET connected_socket, cachelist_param_t *cachelist, msgqueue_param_t *msgqueue, Byte_t *jpipstream)
234 cache_param_t *cache;
238 cid = receive_string( connected_socket);
239 if(!(cache = search_cacheBycid( cid, cachelist))){
246 assert( cache->csn >= 0);
247 jp2stream = recons_jp2( msgqueue, jpipstream, (Byte8_t)cache->csn, &jp2len);
250 save_codestream( jp2stream, jp2len, "jp2");
251 opj_free( jp2stream);