[trunk] Start FolderReorgProposal task
[openjpeg.git] / src / lib / openjpip / dec_clientmsg_handler.c
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 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <limits.h>
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"
41
42 void handle_JPIPstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist,
43                            Byte_t **jpipstream, OPJ_SIZE_T *streamlen, msgqueue_param_t *msgqueue)
44 {
45   Byte_t *newjpipstream;
46   OPJ_SIZE_T newstreamlen = 0;
47   cache_param_t *cache;
48   char *target, *tid, *cid;
49   metadatalist_param_t *metadatalist;
50   
51   newjpipstream = receive_JPIPstream( connected_socket, &target, &tid, &cid, &newstreamlen);
52
53   fprintf( stderr, "newjpipstream length: %" PRIu64 "\n", newstreamlen);
54   
55   parse_JPIPstream( newjpipstream, newstreamlen, (OPJ_OFF_T)*streamlen, msgqueue);
56
57   *jpipstream = update_JPIPstream( newjpipstream, newstreamlen, *jpipstream, streamlen);
58   free( newjpipstream);
59
60   metadatalist = gene_metadatalist();
61   parse_metamsg( msgqueue, *jpipstream, *streamlen, metadatalist);
62
63   assert( msgqueue->last );
64   assert( msgqueue->last->csn < INT_MAX );
65   /* cid registration*/
66   if( target != NULL){
67     if((cache = search_cache( target, cachelist))){
68       if( tid != NULL)
69         update_cachetid( tid, cache);
70       if( cid != NULL)
71         add_cachecid( cid, cache);
72     }
73     else{
74       cache = gene_cache( target, (int)msgqueue->last->csn, tid, cid);
75       insert_cache_into_list( cache, cachelist);
76     }
77   }
78   else
79     cache = search_cacheBycsn( (int)msgqueue->last->csn, cachelist);
80
81   if( cache->metadatalist)
82     delete_metadatalist( &cache->metadatalist);
83   cache->metadatalist = metadatalist;
84
85   if( target)    free( target);
86   if( tid)    free( tid);
87   if( cid)    free( cid);
88
89   response_signal( connected_socket, true);
90 }
91
92 void handle_PNMreqMSG( SOCKET connected_socket, Byte_t *jpipstream, msgqueue_param_t *msgqueue, cachelist_param_t *cachelist)
93 {
94   Byte_t *pnmstream;
95   ihdrbox_param_t *ihdrbox;
96   char *CIDorTID, tmp[10];
97   cache_param_t *cache;
98   int fw, fh;
99   int maxval;
100   
101   CIDorTID = receive_string( connected_socket);
102   
103   if(!(cache = search_cacheBycid( CIDorTID, cachelist)))
104     if(!(cache = search_cacheBytid( CIDorTID, cachelist))){
105       free( CIDorTID);
106       return;
107     }
108   
109   free( CIDorTID);
110
111   receive_line( connected_socket, tmp);
112   fw = atoi( tmp);
113
114   receive_line( connected_socket, tmp);
115   fh = atoi( tmp);
116
117   ihdrbox = NULL;
118   assert( cache->csn >= 0 );
119   pnmstream = jpipstream_to_pnm( jpipstream, msgqueue, (Byte8_t)cache->csn, fw, fh, &ihdrbox);
120
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 );
123
124   free( ihdrbox);
125   free( pnmstream);
126 }
127
128 void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jpipstream, cachelist_param_t *cachelist)
129 {
130   char *cid;
131   cache_param_t *cache;
132   boxcontents_param_t *boxcontents;
133   Byte_t *xmlstream;
134
135   cid = receive_string( connected_socket);
136
137   if(!(cache = search_cacheBycid( cid, cachelist))){
138     free( cid);
139     return;
140   }
141
142   free( cid);
143   
144   boxcontents = cache->metadatalist->last->boxcontents;
145   xmlstream = (Byte_t *)malloc( boxcontents->length);
146   memcpy( xmlstream, jpipstream+boxcontents->offset, boxcontents->length);
147   send_XMLstream( connected_socket, xmlstream, boxcontents->length);
148   free( xmlstream);
149 }
150
151 void handle_TIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
152 {
153   char *target, *tid = NULL;
154   cache_param_t *cache;
155   OPJ_SIZE_T tidlen = 0;
156
157   target = receive_string( connected_socket);
158   cache = search_cache( target, cachelist);
159
160   free( target);
161   
162   if( cache){
163     tid = cache->tid;
164     tidlen = strlen(tid);
165   }
166   send_TIDstream( connected_socket, tid, tidlen);
167 }
168
169 void handle_CIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
170 {
171   char *target, *cid = NULL;
172   cache_param_t *cache;
173   OPJ_SIZE_T cidlen = 0;
174
175   target = receive_string( connected_socket);
176   cache = search_cache( target, cachelist);
177   
178   free( target);
179
180   if( cache){
181     if( cache->numOfcid > 0){
182       cid = cache->cid[ cache->numOfcid-1];
183       cidlen = strlen(cid);
184     }
185   }
186   send_CIDstream( connected_socket, cid, cidlen);
187 }
188
189 void handle_dstCIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
190 {
191   char *cid;
192
193   cid = receive_string( connected_socket);
194   remove_cachecid( cid, cachelist);
195   response_signal( connected_socket, true);
196   
197   free( cid);
198 }
199
200 void handle_SIZreqMSG( SOCKET connected_socket, Byte_t *jpipstream, msgqueue_param_t *msgqueue, cachelist_param_t *cachelist)
201 {
202   char *tid, *cid;
203   cache_param_t *cache;
204   Byte4_t width, height;
205   
206   tid = receive_string( connected_socket);
207   cid = receive_string( connected_socket);
208   
209   cache = NULL;
210
211   if( tid[0] != '0')
212     cache = search_cacheBytid( tid, cachelist);
213   
214   if( !cache && cid[0] != '0')
215     cache = search_cacheBycid( cid, cachelist);
216
217   free( tid);
218   free( cid);
219   
220   width = height = 0;
221   if( cache){
222     assert( cache->csn >= 0);
223     if( !cache->ihdrbox)
224       cache->ihdrbox = get_SIZ_from_jpipstream( jpipstream, msgqueue, (Byte8_t)cache->csn);
225     width  = cache->ihdrbox->width;
226     height = cache->ihdrbox->height;
227   }
228   send_SIZstream( connected_socket, width, height);
229 }
230
231 void handle_JP2saveMSG( SOCKET connected_socket, cachelist_param_t *cachelist, msgqueue_param_t *msgqueue, Byte_t *jpipstream)
232 {
233   char *cid;
234   cache_param_t *cache;
235   Byte_t *jp2stream;
236   Byte8_t jp2len;
237
238   cid = receive_string( connected_socket);
239   if(!(cache = search_cacheBycid( cid, cachelist))){
240     free( cid);
241     return;
242   }
243   
244   free( cid);
245   
246   assert( cache->csn >= 0);
247   jp2stream = recons_jp2( msgqueue, jpipstream, (Byte8_t)cache->csn, &jp2len);
248
249   if( jp2stream){
250     save_codestream( jp2stream, jp2len, "jp2");
251     free( jp2stream);
252   }
253 }