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