d44c84c83f55e55076503c984852db024e38c137
[openjpeg.git] / src / lib / openjpip / jpip_parser.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  * Copyright (c) 2011,      Lucian Corlaciu, GSoC
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <math.h>
35 #include "jpip_parser.h"
36 #include "channel_manager.h"
37 #include "imgreg_manager.h"
38
39 #ifdef SERVER
40 #include "fcgi_stdio.h"
41 #define logstream FCGI_stdout
42 #else
43 #define FCGI_stdout stdout
44 #define FCGI_stderr stderr
45 #define logstream stderr
46 #endif /*SERVER*/
47
48
49 OPJ_BOOL identify_target( query_param_t query_param, targetlist_param_t *targetlist, target_param_t **target)
50 {
51   if( query_param.tid){
52     if( strcmp( query_param.tid, "0") != 0 ){
53       if( query_param.cid[0] != '\0'){
54         fprintf( FCGI_stdout, "Reason: Target can not be specified both through tid and cid\r\n");
55         fprintf( FCGI_stdout, "Status: 400\r\n");
56         return OPJ_FALSE;
57       }
58       if( ( *target = search_targetBytid( query_param.tid, targetlist)))
59         return OPJ_TRUE;
60     }
61   }
62
63   if( query_param.target)
64     if( !( *target = search_target( query_param.target, targetlist)))
65       if(!( *target = gene_target( targetlist, query_param.target)))
66         return OPJ_FALSE;
67
68   if( *target){
69     fprintf( FCGI_stdout, "JPIP-tid: %s\r\n", (*target)->tid);
70     return OPJ_TRUE;
71   }
72   else{
73     fprintf( FCGI_stdout, "Reason: target not found\r\n");
74     fprintf( FCGI_stdout, "Status: 400\r\n");
75     return OPJ_FALSE;
76   }
77 }
78
79 OPJ_BOOL associate_channel( query_param_t    query_param, 
80                         sessionlist_param_t *sessionlist,
81                         session_param_t **cursession, 
82                         channel_param_t **curchannel)
83 {
84   if( search_session_and_channel( query_param.cid, sessionlist, cursession, curchannel)){
85     
86     if( !query_param.cnew)
87       set_channel_variable_param( query_param, *curchannel);
88   }
89   else{
90     fprintf( FCGI_stderr, "Error: process canceled\n");
91     return OPJ_FALSE;
92   }
93   return OPJ_TRUE;
94 }
95
96 OPJ_BOOL open_channel( query_param_t query_param, 
97                    sessionlist_param_t *sessionlist,
98                    auxtrans_param_t auxtrans,
99                    target_param_t *target,
100                    session_param_t **cursession, 
101                    channel_param_t **curchannel)
102 {
103   cachemodel_param_t *cachemodel = NULL;
104
105   if( target){
106     if( !(*cursession))
107       *cursession = gene_session( sessionlist);
108     if( !( cachemodel = search_cachemodel( target, (*cursession)->cachemodellist)))
109       if( !(cachemodel = gene_cachemodel( (*cursession)->cachemodellist, target, query_param.return_type==JPPstream)))
110         return OPJ_FALSE;
111   }
112   else
113     if( *curchannel)
114       cachemodel = (*curchannel)->cachemodel;
115
116   *curchannel = gene_channel( query_param, auxtrans, cachemodel, (*cursession)->channellist);
117   if( *curchannel == NULL)
118     return OPJ_FALSE;
119
120   return OPJ_TRUE;
121 }
122
123 OPJ_BOOL close_channel( query_param_t query_param, 
124                     sessionlist_param_t *sessionlist,
125                     session_param_t **cursession, 
126                     channel_param_t **curchannel)
127 {
128   char *cclose;
129   int i;
130   
131   if( query_param.cclose[0] =='*'){
132 #ifndef SERVER
133     fprintf( logstream, "local log: close all\n");
134 #endif
135     /* all channels associatd with the session will be closed */
136     if( !delete_session( cursession, sessionlist))
137       return OPJ_FALSE;
138   }
139   else{
140     /* check if all entry belonging to the same session */
141     
142     for( i=0, cclose=query_param.cclose; i<query_param.numOfcclose; i++, cclose += (strlen(cclose)+1)){
143       
144       /* In case of the first entry of close cid */
145       if( *cursession == NULL){
146         if( !search_session_and_channel( cclose, sessionlist, cursession, curchannel))
147           return OPJ_FALSE;
148       }
149       else /* second or more entry of close cid */
150         if( !(*curchannel=search_channel( cclose, (*cursession)->channellist))){
151           fprintf( FCGI_stdout, "Reason: Cclose id %s is from another session\r\n", cclose); 
152           return OPJ_FALSE;
153         }
154     }
155
156     /* delete channels */
157     for( i=0, cclose=query_param.cclose; i<query_param.numOfcclose; i++, cclose += (strlen(cclose)+1)){
158       *curchannel = search_channel( cclose, (*cursession)->channellist);
159       delete_channel( curchannel, (*cursession)->channellist);
160     }
161     
162     if( (*cursession)->channellist->first == NULL || (*cursession)->channellist->last == NULL)
163       /* In case of empty session */
164       delete_session( cursession, sessionlist);
165   }
166   return OPJ_TRUE;
167 }
168
169
170 /**
171  * enqueue tiles or precincts into the message queue
172  *
173  * @param[in] query_param structured query
174  * @param[in] msgqueue    message queue pointer  
175  */
176 void enqueue_imagedata( query_param_t query_param, msgqueue_param_t *msgqueue);
177
178 /**
179  * enqueue metadata bins into the message queue
180  *
181  * @param[in]     query_param  structured query
182  * @param[in]     metadatalist pointer to metadata bin list
183  * @param[in,out] msgqueue     message queue pointer  
184  * @return                     if succeeded (true) or failed (false)
185  */
186 OPJ_BOOL enqueue_metabins( query_param_t query_param, metadatalist_param_t *metadatalist, msgqueue_param_t *msgqueue);
187
188
189 OPJ_BOOL gene_JPIPstream( query_param_t query_param,
190                       target_param_t *target,
191                       session_param_t *cursession, 
192                       channel_param_t *curchannel,
193                       msgqueue_param_t **msgqueue)
194 {
195   index_param_t *codeidx;
196   cachemodel_param_t *cachemodel;
197   
198   if( !cursession || !curchannel){ /* stateless */
199     if( !target)
200       return OPJ_FALSE;
201     if( !(cachemodel = gene_cachemodel( NULL, target, query_param.return_type==JPPstream)))
202       return OPJ_FALSE;
203     *msgqueue = gene_msgqueue( OPJ_TRUE, cachemodel);
204   }
205   else{ /* session */
206     cachemodel  = curchannel->cachemodel;
207     target = cachemodel->target;
208     *msgqueue = gene_msgqueue( OPJ_FALSE, cachemodel);
209   }
210   
211   codeidx = target->codeidx;
212
213   if( cachemodel->jppstream)
214     fprintf( FCGI_stdout, "Content-type: image/jpp-stream\r\n");
215   else
216     fprintf( FCGI_stdout, "Content-type: image/jpt-stream\r\n");
217   
218   if( query_param.layers != -1){
219     if( query_param.layers  > codeidx->COD.numOflayers){
220       fprintf( FCGI_stdout, "JPIP-layers: %d\r\n", codeidx->COD.numOflayers);
221       query_param.layers = codeidx->COD.numOflayers;
222     }
223   }
224
225   /*meta*/
226   if( query_param.box_type[0][0] != 0  && query_param.len != 0)
227     if( !enqueue_metabins( query_param, codeidx->metadatalist, *msgqueue))
228       return OPJ_FALSE;
229   
230   if( query_param.metadata_only)
231     return OPJ_TRUE;
232
233   /* main header */
234   if( !cachemodel->mhead_model && query_param.len != 0)
235       enqueue_mainheader( *msgqueue);
236
237   /* image codestream */
238   if( (query_param.fx > 0 && query_param.fy > 0))
239     enqueue_imagedata( query_param, *msgqueue);
240   
241   return OPJ_TRUE;
242 }
243
244
245 /**
246  * enqueue precinct data-bins into the queue
247  *
248  * @param[in] xmin      min x coordinate in the tile at the decomposition level
249  * @param[in] xmax      max x coordinate in the tile at the decomposition level
250  * @param[in] ymin      min y coordinate in the tile at the decomposition level
251  * @param[in] ymax      max y coordinate in the tile at the decomposition level
252  * @param[in] tile_id   tile index
253  * @param[in] level     decomposition level
254  * @param[in] lastcomp  last component number
255  * @param[in] comps     pointer to the array that stores the requested components
256  * @param[in] layers    number of quality layers
257  * @param[in] msgqueue  message queue
258  * @return
259  */
260 void enqueue_precincts( int xmin, int xmax, int ymin, int ymax, int tile_id, int level, int lastcomp, OPJ_BOOL *comps, int layers, msgqueue_param_t *msgqueue);
261
262 /**
263  * enqueue all precincts inside a tile into the queue
264  *
265  * @param[in] tile_id   tile index
266  * @param[in] level     decomposition level
267  * @param[in] lastcomp  last component number
268  * @param[in] comps     pointer to the array that stores the requested components
269  * @param[in] layers    number of quality layers
270  * @param[in] msgqueue  message queue
271  * @return
272  */
273 void enqueue_allprecincts( int tile_id, int level, int lastcomp, OPJ_BOOL *comps, int layers, msgqueue_param_t *msgqueue);
274
275 void enqueue_imagedata( query_param_t query_param, msgqueue_param_t *msgqueue)
276 {
277   index_param_t *codeidx;
278   imgreg_param_t imgreg;
279   range_param_t tile_Xrange, tile_Yrange;
280   Byte4_t u, v, tile_id;
281   int xmin, xmax, ymin, ymax;
282   int numOfreslev;
283
284   codeidx = msgqueue->cachemodel->target->codeidx;
285
286   if( !(msgqueue->cachemodel->jppstream)  &&  get_nmax( codeidx->tilepart) == 1) /* normally not the case */
287     numOfreslev = 1;
288   else
289     numOfreslev = codeidx->COD.numOfdecomp+1;
290
291   imgreg  = map_viewin2imgreg( query_param.fx, query_param.fy, 
292                                query_param.rx, query_param.ry, query_param.rw, query_param.rh,
293                                (int)codeidx->SIZ.XOsiz, (int)codeidx->SIZ.YOsiz, (int)codeidx->SIZ.Xsiz, (int)codeidx->SIZ.Ysiz, 
294                                numOfreslev );
295
296   if( query_param.len == 0)
297     return;
298
299   for( u=0, tile_id=0; u<codeidx->SIZ.YTnum; u++){
300     tile_Yrange = get_tile_Yrange( codeidx->SIZ, tile_id, imgreg.level);
301     
302     for( v=0; v<codeidx->SIZ.XTnum; v++, tile_id++){
303       tile_Xrange = get_tile_Xrange( codeidx->SIZ, tile_id, imgreg.level);
304         
305       if( tile_Xrange.minvalue < tile_Xrange.maxvalue && tile_Yrange.minvalue < tile_Yrange.maxvalue){
306         if( tile_Xrange.maxvalue <= (Byte4_t)(imgreg.xosiz + imgreg.ox) || 
307             tile_Xrange.minvalue >= (Byte4_t)(imgreg.xosiz + imgreg.ox + imgreg.sx) ||
308             tile_Yrange.maxvalue <= (Byte4_t)(imgreg.yosiz + imgreg.oy) || 
309             tile_Yrange.minvalue >= (Byte4_t)(imgreg.yosiz + imgreg.oy + imgreg.sy)) {
310           /*printf("Tile completely excluded from view-window %d\n", tile_id);*/
311           /* Tile completely excluded from view-window */
312         }
313         else if( tile_Xrange.minvalue >= (Byte4_t)(imgreg.xosiz + imgreg.ox) && 
314                        tile_Xrange.maxvalue <= (Byte4_t)(imgreg.xosiz + imgreg.ox + imgreg.sx) && 
315                        tile_Yrange.minvalue >= (Byte4_t)(imgreg.yosiz + imgreg.oy) && 
316                        tile_Yrange.maxvalue <= (Byte4_t)(imgreg.yosiz + imgreg.oy + imgreg.sy)) {
317           /* Tile completely contained within view-window */
318           /* high priority */
319           /*printf("Tile completely contained within view-window %d\n", tile_id);*/
320           if( msgqueue->cachemodel->jppstream){
321             enqueue_tileheader( (int)tile_id, msgqueue);
322             enqueue_allprecincts( (int)tile_id, imgreg.level, query_param.lastcomp, query_param.comps, query_param.layers, msgqueue);
323           }
324           else
325             enqueue_tile( tile_id, imgreg.level, msgqueue);
326         }
327         else{
328           /* Tile partially overlaps view-window */
329           /* low priority */
330           /*printf("Tile partially overlaps view-window %d\n", tile_id);*/
331           if( msgqueue->cachemodel->jppstream){
332             enqueue_tileheader( (int)tile_id, msgqueue);
333
334       /* FIXME: The following code is suspicious it implicitely cast an unsigned int to int, which truncates values */
335             xmin = tile_Xrange.minvalue >= (Byte4_t)(imgreg.xosiz + imgreg.ox) ? 0 : imgreg.xosiz + imgreg.ox - (int)tile_Xrange.minvalue;
336             xmax = tile_Xrange.maxvalue <= (Byte4_t)(imgreg.xosiz + imgreg.ox + imgreg.sx) ? (int)(tile_Xrange.maxvalue - tile_Xrange.minvalue -1) : (int)(imgreg.xosiz + imgreg.ox + imgreg.sx - (int)tile_Xrange.minvalue - 1);
337             ymin = tile_Yrange.minvalue >= (Byte4_t)(imgreg.yosiz + imgreg.oy) ? 0 : imgreg.yosiz + imgreg.oy - (int)tile_Yrange.minvalue;
338             ymax = tile_Yrange.maxvalue <= (Byte4_t)(imgreg.yosiz + imgreg.oy + imgreg.sy) ? (int)(tile_Yrange.maxvalue - tile_Yrange.minvalue - 1) : (int)(imgreg.yosiz + imgreg.oy + imgreg.sy - (int)tile_Yrange.minvalue - 1);
339             enqueue_precincts( xmin, xmax, ymin, ymax, (int)tile_id, imgreg.level, query_param.lastcomp, query_param.comps, query_param.layers, msgqueue);
340           }
341           else
342             enqueue_tile( tile_id, imgreg.level, msgqueue);
343         }
344       }
345     }
346   }
347 }
348
349
350 void enqueue_precincts( int xmin, int xmax, int ymin, int ymax, int tile_id, int level, int lastcomp, OPJ_BOOL *comps, int layers, msgqueue_param_t *msgqueue)
351 {
352   index_param_t *codeidx;
353   int c, u, v, res_lev, dec_lev;
354   int seq_id;
355   Byte4_t XTsiz, YTsiz;
356   Byte4_t XPsiz, YPsiz;
357   Byte4_t xminP, xmaxP, yminP, ymaxP;
358
359   codeidx  = msgqueue->cachemodel->target->codeidx;
360   /* MM: shouldn't xmin/xmax be Byte4_t instead ? */
361   if( xmin < 0 || xmax < 0 || ymin < 0 || ymax < 0)
362     return;
363   /* MM: I think the API should not really be int should it ? */
364   if( tile_id < 0 )
365     return;
366
367   for( c=0; c<codeidx->SIZ.Csiz; c++)
368     if( lastcomp == -1 /*all*/ || ( c<=lastcomp && comps[c])){
369       seq_id = 0;
370       for( res_lev=0, dec_lev=codeidx->COD.numOfdecomp; dec_lev>=level; res_lev++, dec_lev--){
371         
372         XTsiz = get_tile_XSiz( codeidx->SIZ, (Byte4_t)tile_id, dec_lev);
373         YTsiz = get_tile_YSiz( codeidx->SIZ, (Byte4_t)tile_id, dec_lev);
374         
375         XPsiz = ( codeidx->COD.Scod & 0x01) ? codeidx->COD.XPsiz[ res_lev] : XTsiz;
376         YPsiz = ( codeidx->COD.Scod & 0x01) ? codeidx->COD.YPsiz[ res_lev] : YTsiz;
377           
378         for( u=0; u<ceil((double)YTsiz/(double)YPsiz); u++){
379           yminP = (Byte4_t)u*YPsiz;
380           ymaxP = (Byte4_t)(u+1)*YPsiz-1;
381           if( YTsiz <= ymaxP)
382             ymaxP = YTsiz-1;
383           
384           for( v=0; v<ceil((double)XTsiz/(double)XPsiz); v++, seq_id++){
385             xminP = (Byte4_t)v*XPsiz;
386             xmaxP = (Byte4_t)(v+1)*XPsiz-1;
387             if( XTsiz <= xmaxP)
388               xmaxP = XTsiz-1;
389             
390             if( xmaxP < (Byte4_t)xmin || xminP > (Byte4_t)xmax || ymaxP < (Byte4_t)ymin || yminP > (Byte4_t)ymax){
391               /* Precinct completely excluded from view-window */
392             }
393             else if( xminP >= (Byte4_t)xmin && xmaxP <= (Byte4_t)xmax && yminP >= (Byte4_t)ymin && ymaxP <= (Byte4_t)ymax){
394               /* Precinct completely contained within view-window
395                high priority */
396               enqueue_precinct( seq_id, tile_id, c, (dec_lev>level)?-1:layers, msgqueue);
397             }
398             else{
399               /* Precinct partially overlaps view-window
400                low priority */
401               enqueue_precinct( seq_id, tile_id, c, (dec_lev>level)?-1:layers, msgqueue);
402             }
403           }
404         }
405       }
406     }
407 }
408
409 void enqueue_allprecincts( int tile_id, int level, int lastcomp, OPJ_BOOL *comps, int layers, msgqueue_param_t *msgqueue)
410 {
411   index_param_t *codeidx;
412   int c, i, res_lev, dec_lev;
413   int seq_id;
414   Byte4_t XTsiz, YTsiz;
415   Byte4_t XPsiz, YPsiz;
416
417   codeidx  = msgqueue->cachemodel->target->codeidx;
418   if( tile_id < 0 )
419     return;
420
421   for( c=0; c<codeidx->SIZ.Csiz; c++)
422     if( lastcomp == -1 /*all*/ || ( c<=lastcomp && comps[c])){
423       seq_id = 0;
424       for( res_lev=0, dec_lev=codeidx->COD.numOfdecomp; dec_lev>=level; res_lev++, dec_lev--){
425         
426         XTsiz = get_tile_XSiz( codeidx->SIZ, (Byte4_t)tile_id, dec_lev);
427         YTsiz = get_tile_YSiz( codeidx->SIZ, (Byte4_t)tile_id, dec_lev);
428         
429         XPsiz = ( codeidx->COD.Scod & 0x01) ? codeidx->COD.XPsiz[ res_lev] : XTsiz;
430         YPsiz = ( codeidx->COD.Scod & 0x01) ? codeidx->COD.YPsiz[ res_lev] : YTsiz;
431         
432         for( i=0; i<ceil((double)YTsiz/(double)YPsiz)*ceil((double)XTsiz/(double)XPsiz); i++, seq_id++)
433           enqueue_precinct( seq_id, tile_id, c, (dec_lev>level)?-1:layers, msgqueue);
434       }
435     }
436 }
437
438 OPJ_BOOL enqueue_metabins( query_param_t query_param, metadatalist_param_t *metadatalist, msgqueue_param_t *msgqueue)
439 {
440   int i;
441   for( i=0; query_param.box_type[i][0]!=0 && i<MAX_NUMOFBOX; i++){
442     if( query_param.box_type[i][0] == '*'){
443       fprintf( FCGI_stdout, "Status: 501\r\n");
444       fprintf( FCGI_stdout, "Reason: metareq with all box-property * not implemented\r\n");
445       return OPJ_FALSE;
446     }
447     else{
448       Byte8_t idx = search_metadataidx( query_param.box_type[i], metadatalist);
449
450       if( idx != (Byte8_t)-1)
451         enqueue_metadata( idx, msgqueue);
452       else{
453         fprintf( FCGI_stdout, "Status: 400\r\n");
454         fprintf( FCGI_stdout, "Reason: box-type %.4s not found\r\n", query_param.box_type[i]);
455         return OPJ_FALSE;
456       }
457     }
458   }
459   return OPJ_TRUE;
460 }