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
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,
54 fprintf(stderr, "newjpipstream length: %" PRIu64 "\n", newstreamlen);
56 parse_JPIPstream(newjpipstream, newstreamlen, (OPJ_OFF_T)*streamlen, msgqueue);
58 *jpipstream = update_JPIPstream(newjpipstream, newstreamlen, *jpipstream,
60 opj_free(newjpipstream);
62 metadatalist = gene_metadatalist();
63 parse_metamsg(msgqueue, *jpipstream, *streamlen, metadatalist);
65 assert(msgqueue->last);
66 assert(msgqueue->last->csn < INT_MAX);
69 if ((cache = search_cache(target, cachelist))) {
71 update_cachetid(tid, cache);
74 add_cachecid(cid, cache);
77 cache = gene_cache(target, (int)msgqueue->last->csn, tid, cid);
78 insert_cache_into_list(cache, cachelist);
81 cache = search_cacheBycsn((int)msgqueue->last->csn, cachelist);
84 if (cache->metadatalist) {
85 delete_metadatalist(&cache->metadatalist);
87 cache->metadatalist = metadatalist;
99 response_signal(connected_socket, OPJ_TRUE);
102 void handle_PNMreqMSG(SOCKET connected_socket, Byte_t *jpipstream,
103 msgqueue_param_t *msgqueue, cachelist_param_t *cachelist)
106 ihdrbox_param_t *ihdrbox;
107 char *CIDorTID, tmp[10];
108 cache_param_t *cache;
112 CIDorTID = receive_string(connected_socket);
114 if (!(cache = search_cacheBycid(CIDorTID, cachelist)))
115 if (!(cache = search_cacheBytid(CIDorTID, cachelist))) {
122 receive_line(connected_socket, tmp);
125 receive_line(connected_socket, tmp);
129 assert(cache->csn >= 0);
130 pnmstream = jpipstream_to_pnm(jpipstream, msgqueue, (Byte8_t)cache->csn, fw, fh,
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);
141 void handle_XMLreqMSG(SOCKET connected_socket, Byte_t *jpipstream,
142 cachelist_param_t *cachelist)
145 cache_param_t *cache;
146 boxcontents_param_t *boxcontents;
149 cid = receive_string(connected_socket);
151 if (!(cache = search_cacheBycid(cid, cachelist))) {
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);
165 void handle_TIDreqMSG(SOCKET connected_socket, cachelist_param_t *cachelist)
167 char *target, *tid = NULL;
168 cache_param_t *cache;
169 OPJ_SIZE_T tidlen = 0;
171 target = receive_string(connected_socket);
172 cache = search_cache(target, cachelist);
178 tidlen = strlen(tid);
180 send_TIDstream(connected_socket, tid, tidlen);
183 void handle_CIDreqMSG(SOCKET connected_socket, cachelist_param_t *cachelist)
185 char *target, *cid = NULL;
186 cache_param_t *cache;
187 OPJ_SIZE_T cidlen = 0;
189 target = receive_string(connected_socket);
190 cache = search_cache(target, cachelist);
195 if (cache->numOfcid > 0) {
196 cid = cache->cid[ cache->numOfcid - 1];
197 cidlen = strlen(cid);
200 send_CIDstream(connected_socket, cid, cidlen);
203 void handle_dstCIDreqMSG(SOCKET connected_socket, cachelist_param_t *cachelist)
207 cid = receive_string(connected_socket);
208 remove_cachecid(cid, cachelist);
209 response_signal(connected_socket, OPJ_TRUE);
214 void handle_SIZreqMSG(SOCKET connected_socket, Byte_t *jpipstream,
215 msgqueue_param_t *msgqueue, cachelist_param_t *cachelist)
218 cache_param_t *cache;
219 Byte4_t width, height;
221 tid = receive_string(connected_socket);
222 cid = receive_string(connected_socket);
227 cache = search_cacheBytid(tid, cachelist);
230 if (!cache && cid[0] != '0') {
231 cache = search_cacheBycid(cid, cachelist);
239 assert(cache->csn >= 0);
240 if (!cache->ihdrbox) {
241 cache->ihdrbox = get_SIZ_from_jpipstream(jpipstream, msgqueue,
242 (Byte8_t)cache->csn);
244 width = cache->ihdrbox->width;
245 height = cache->ihdrbox->height;
247 send_SIZstream(connected_socket, width, height);
250 void handle_JP2saveMSG(SOCKET connected_socket, cachelist_param_t *cachelist,
251 msgqueue_param_t *msgqueue, Byte_t *jpipstream)
254 cache_param_t *cache;
258 cid = receive_string(connected_socket);
259 if (!(cache = search_cacheBycid(cid, cachelist))) {
266 assert(cache->csn >= 0);
267 jp2stream = recons_jp2(msgqueue, jpipstream, (Byte8_t)cache->csn, &jp2len);
270 save_codestream(jp2stream, jp2len, "jp2");