[1.5] backport r1017, r1030:1032 from trunk + additional fix for cmake and autotools
[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
41 //! maximum length of channel identifier
42 #define MAX_LENOFCID 30
43
44 void handle_JPIPstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist,
45                            Byte_t **jpipstream, int *streamlen, msgqueue_param_t *msgqueue)
46 {
47   Byte_t *newjpipstream;
48   int newstreamlen = 0;
49   cache_param_t *cache;
50   char target[MAX_LENOFTARGET], tid[MAX_LENOFTID], cid[MAX_LENOFCID];
51   metadatalist_param_t *metadatalist;
52   
53   newjpipstream = receive_JPIPstream( connected_socket, target, tid, cid, &newstreamlen);
54
55   parse_JPIPstream( newjpipstream, newstreamlen, *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   // cid registration
64   if( target[0] != 0){
65     if((cache = search_cache( target, cachelist))){
66       if( tid[0] != 0)
67         update_cachetid( tid, cache);
68       if( cid[0] != 0)
69         add_cachecid( cid, cache);
70     }
71     else{
72       cache = gene_cache( target, msgqueue->last->csn, tid, cid);
73       insert_cache_into_list( cache, cachelist);
74     }
75   }
76   else
77     cache = search_cacheBycsn( msgqueue->last->csn, cachelist);
78
79   if( cache->metadatalist)
80     delete_metadatalist( &cache->metadatalist);
81   cache->metadatalist = metadatalist;
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 cid[MAX_LENOFCID], tmp[10];
91   cache_param_t *cache;
92   int fw, fh;
93
94   receive_line( connected_socket, cid);
95   if(!(cache = search_cacheBycid( cid, cachelist)))
96     if(!(cache = search_cacheBytid( cid, cachelist)))
97       return;
98
99   receive_line( connected_socket, tmp);
100   fw = atoi( tmp);
101
102   receive_line( connected_socket, tmp);
103   fh = atoi( tmp);
104
105   pnmstream = jpipstream_to_pnm( jpipstream, msgqueue, cache->csn, fw, fh, &cache->ihdrbox);
106   ihdrbox = cache->ihdrbox;
107
108   send_PNMstream( connected_socket, pnmstream, ihdrbox->width, ihdrbox->height, ihdrbox->nc, ihdrbox->bpc > 8 ? 255 : (1 << ihdrbox->bpc) - 1);
109
110   free( pnmstream);
111 }
112
113 void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jpipstream, cachelist_param_t *cachelist)
114 {
115   char cid[MAX_LENOFCID];
116   cache_param_t *cache;
117
118   receive_line( connected_socket, cid);
119   if(!(cache = search_cacheBycid( cid, cachelist)))
120     return;
121   
122   boxcontents_param_t *boxcontents = cache->metadatalist->last->boxcontents;
123   Byte_t *xmlstream = (Byte_t *)malloc( boxcontents->length);
124   memcpy( xmlstream, jpipstream+boxcontents->offset, boxcontents->length);
125   send_XMLstream( connected_socket, xmlstream, boxcontents->length);
126   free( xmlstream);
127 }
128
129 void handle_TIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
130 {
131   char target[MAX_LENOFTARGET], *tid = NULL;
132   cache_param_t *cache;
133   int tidlen = 0;
134
135   receive_line( connected_socket, target);
136   cache = search_cache( target, cachelist);
137   
138   if( cache){
139     tid = cache->tid;
140     tidlen = strlen(tid);
141   }
142   send_TIDstream( connected_socket, tid, tidlen);
143 }
144
145 void handle_CIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
146 {
147   char target[MAX_LENOFTARGET], *cid = NULL;
148   cache_param_t *cache;
149   int cidlen = 0;
150
151   receive_line( connected_socket, target);
152   cache = search_cache( target, cachelist);
153   
154   if( cache){
155     if( cache->numOfcid > 0){
156       cid = cache->cid[ cache->numOfcid-1];
157       cidlen = strlen(cid);
158     }
159   }
160   send_CIDstream( connected_socket, cid, cidlen);
161 }
162
163 void handle_dstCIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
164 {
165   char cid[MAX_LENOFCID];
166
167   receive_line( connected_socket, cid);
168   remove_cachecid( cid, cachelist);
169   response_signal( connected_socket, true);
170 }
171
172 void handle_JP2saveMSG( SOCKET connected_socket, cachelist_param_t *cachelist, msgqueue_param_t *msgqueue, Byte_t *jpipstream)
173 {
174   char cid[MAX_LENOFCID];
175   cache_param_t *cache;
176   Byte_t *jp2stream;
177   Byte8_t jp2len;
178
179   receive_line( connected_socket, cid);
180   if(!(cache = search_cacheBycid( cid, cachelist)))
181     return;
182   
183   jp2stream = recons_jp2( msgqueue, jpipstream, cache->csn, &jp2len);
184
185   if( jp2stream){
186     save_codestream( jp2stream, jp2len, "jp2");
187     free( jp2stream);
188   }
189 }