[trunk][JPIP] enabled OpenJPEG V2 API
[openjpeg.git] / applications / jpip / libopenjpip / 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 <unistd.h>
35 #include "dec_clientmsg_handler.h"
36 #include "ihdrbox_manager.h"
37 #include "jpipstream_manager.h"
38 #include "jp2k_encoder.h"
39
40 void handle_JPIPstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist,
41                            Byte_t **jpipstream, int *streamlen, msgqueue_param_t *msgqueue)
42 {
43   Byte_t *newjpipstream;
44   int newstreamlen = 0;
45   cache_param_t *cache;
46   char *target, *tid, *cid;
47   metadatalist_param_t *metadatalist;
48   
49   newjpipstream = receive_JPIPstream( connected_socket, &target, &tid, &cid, &newstreamlen);
50
51   parse_JPIPstream( newjpipstream, newstreamlen, *streamlen, msgqueue);
52
53   *jpipstream = update_JPIPstream( newjpipstream, newstreamlen, *jpipstream, streamlen);
54   free( newjpipstream);
55
56   metadatalist = gene_metadatalist();
57   parse_metamsg( msgqueue, *jpipstream, *streamlen, metadatalist);
58
59   // cid registration
60   if( target != NULL){
61     if((cache = search_cache( target, cachelist))){
62       if( tid != NULL)
63         update_cachetid( tid, cache);
64       if( cid != NULL)
65         add_cachecid( cid, cache);
66     }
67     else{
68       cache = gene_cache( target, msgqueue->last->csn, tid, cid);
69       insert_cache_into_list( cache, cachelist);
70     }
71   }
72   else
73     cache = search_cacheBycsn( msgqueue->last->csn, cachelist);
74
75   if( cache->metadatalist)
76     delete_metadatalist( &cache->metadatalist);
77   cache->metadatalist = metadatalist;
78
79   if( target)    free( target);
80   if( tid)    free( tid);
81   if( cid)    free( cid);
82
83   response_signal( connected_socket, true);
84 }
85
86 void handle_PNMreqMSG( SOCKET connected_socket, Byte_t *jpipstream, msgqueue_param_t *msgqueue, cachelist_param_t *cachelist)
87 {
88   Byte_t *pnmstream;
89   ihdrbox_param_t *ihdrbox;
90   char *CIDorTID, tmp[10];
91   cache_param_t *cache;
92   int fw, fh;
93
94   CIDorTID = receive_string( connected_socket);
95   
96   if(!(cache = search_cacheBycid( CIDorTID, cachelist)))
97     if(!(cache = search_cacheBytid( CIDorTID, cachelist))){
98       free( CIDorTID);
99       return;
100     }
101   
102   free( CIDorTID);
103
104   receive_line( connected_socket, tmp);
105   fw = atoi( tmp);
106
107   receive_line( connected_socket, tmp);
108   fh = atoi( tmp);
109
110   pnmstream = jpipstream_to_pnm( jpipstream, msgqueue, cache->csn, fw, fh, &cache->ihdrbox);
111   ihdrbox = cache->ihdrbox;
112
113   send_PNMstream( connected_socket, pnmstream, ihdrbox->width, ihdrbox->height, ihdrbox->nc, ihdrbox->bpc > 8 ? 255 : (1 << ihdrbox->bpc) - 1);
114
115   if( pnmstream)
116     free( pnmstream);
117 }
118
119 void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jpipstream, cachelist_param_t *cachelist)
120 {
121   char *cid;
122   cache_param_t *cache;
123
124   cid = receive_string( connected_socket);
125
126   if(!(cache = search_cacheBycid( cid, cachelist))){
127     free( cid);
128     return;
129   }
130
131   free( cid);
132   
133   boxcontents_param_t *boxcontents = cache->metadatalist->last->boxcontents;
134   Byte_t *xmlstream = (Byte_t *)malloc( boxcontents->length);
135   memcpy( xmlstream, jpipstream+boxcontents->offset, boxcontents->length);
136   send_XMLstream( connected_socket, xmlstream, boxcontents->length);
137   free( xmlstream);
138 }
139
140 void handle_TIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
141 {
142   char *target, *tid = NULL;
143   cache_param_t *cache;
144   int tidlen = 0;
145
146   target = receive_string( connected_socket);
147   cache = search_cache( target, cachelist);
148
149   free( target);
150   
151   if( cache){
152     tid = cache->tid;
153     tidlen = strlen(tid);
154   }
155   send_TIDstream( connected_socket, tid, tidlen);
156 }
157
158 void handle_CIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
159 {
160   char *target, *cid = NULL;
161   cache_param_t *cache;
162   int cidlen = 0;
163
164   target = receive_string( connected_socket);
165   cache = search_cache( target, cachelist);
166   
167   free( target);
168
169   if( cache){
170     if( cache->numOfcid > 0){
171       cid = cache->cid[ cache->numOfcid-1];
172       cidlen = strlen(cid);
173     }
174   }
175   send_CIDstream( connected_socket, cid, cidlen);
176 }
177
178 void handle_dstCIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
179 {
180   char *cid;
181
182   cid = receive_string( connected_socket);
183   remove_cachecid( cid, cachelist);
184   response_signal( connected_socket, true);
185   
186   free( cid);
187 }
188
189 void handle_JP2saveMSG( SOCKET connected_socket, cachelist_param_t *cachelist, msgqueue_param_t *msgqueue, Byte_t *jpipstream)
190 {
191   char *cid;
192   cache_param_t *cache;
193   Byte_t *jp2stream;
194   Byte8_t jp2len;
195
196   cid = receive_string( connected_socket);
197   if(!(cache = search_cacheBycid( cid, cachelist))){
198     free( cid);
199     return;
200   }
201   
202   free( cid);
203   
204   jp2stream = recons_jp2( msgqueue, jpipstream, cache->csn, &jp2len);
205
206   if( jp2stream){
207     save_codestream( jp2stream, jp2len, "jp2");
208     free( jp2stream);
209   }
210 }