[trunk] Sync OPJViewer with 1.5 version
[openjpeg.git] / applications / jpip / libopenjpip / index_manager.c
1 /*
2  * $Id: index_manager.c 53 2011-05-09 16:55:39Z kaori $
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 <math.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <string.h>
38
39 #include "bool.h"
40 #include "index_manager.h"
41 #include "box_manager.h"
42 #include "manfbox_manager.h"
43 #include "mhixbox_manager.h"
44 #include "codestream_manager.h"
45 #include "marker_manager.h"
46 #include "faixbox_manager.h"
47 #include "boxheader_manager.h"
48
49 #ifdef SERVER
50 #include "fcgi_stdio.h"
51 #define logstream FCGI_stdout
52 #else
53 #define FCGI_stdout stdout
54 #define FCGI_stderr stderr
55 #define logstream stderr
56 #endif //SERVER
57
58 /**
59  * chekc JP2 box indexing
60  *
61  * @param[in] toplev_boxlist top level box list
62  * @return                   if correct (true) or wrong (false)
63  */
64 bool check_JP2boxidx( boxlist_param_t *toplev_boxlist);
65
66 /**
67  * set code index parameters (parse cidx box)
68  * Annex I
69  *
70  * @param[in]  cidx_box pointer to the reference cidx_box
71  * @param[out] codeidx  pointer to index parameters
72  * @return              if succeeded (true) or failed (false)
73  */
74 bool set_cidxdata( box_param_t *cidx_box, index_param_t *codeidx);
75
76 index_param_t * parse_jp2file( int fd)
77 {
78   index_param_t *jp2idx;
79   box_param_t *cidx;
80   metadatalist_param_t *metadatalist;
81   boxlist_param_t *toplev_boxlist;
82   struct stat sb;
83   
84   if( fstat( fd, &sb) == -1){
85     fprintf( FCGI_stdout, "Reason: Target broken (fstat error)\r\n");
86     return NULL;
87   }
88
89   if( !(toplev_boxlist = get_boxstructure( fd, 0, sb.st_size))){
90     fprintf( FCGI_stderr, "Error: Not correctl JP2 format\n");
91     return NULL;
92   }
93   
94   if( !check_JP2boxidx( toplev_boxlist)){
95     fprintf( FCGI_stderr, "Index format not supported\n");
96     delete_boxlist( &toplev_boxlist);
97     return NULL;
98   }
99
100   if( !(cidx = search_box( "cidx", toplev_boxlist))){
101     fprintf( FCGI_stderr, "Box cidx not found\n");
102     delete_boxlist( &toplev_boxlist);
103     return NULL;
104   }
105
106   jp2idx = (index_param_t *)malloc( sizeof(index_param_t));
107   
108   if( !set_cidxdata( cidx, jp2idx)){
109     fprintf( FCGI_stderr, "Error: Not correctl format in cidx box\n");
110     free(jp2idx);
111     delete_boxlist( &toplev_boxlist);
112     return NULL;
113   }
114   delete_boxlist( &toplev_boxlist);
115   
116   metadatalist = const_metadatalist( fd);
117   jp2idx->metadatalist = metadatalist;
118
119 #ifndef SERVER
120     fprintf( logstream, "local log: code index created\n");
121 #endif
122   
123   return jp2idx;
124 }
125
126 void print_index( index_param_t index)
127 {
128   int i;
129
130   fprintf( logstream, "index info:\n");
131   fprintf( logstream, "\tCodestream  Offset: %#llx\n", index.offset);
132   fprintf( logstream, "\t            Length: %#llx\n", index.length);
133   fprintf( logstream, "\tMain header Length: %#llx\n", index.mhead_length);
134   
135   print_SIZ( index.SIZ);
136   print_COD( index.COD);
137   
138   fprintf( logstream, "Tile part information: \n");
139   print_faixbox( index.tilepart);
140
141   fprintf( logstream, "Tile header information: \n");
142   for( i=0; i<index.SIZ.XTnum*index.SIZ.YTnum ;i++)
143     print_mhixbox( index.tileheader[i]);
144
145   fprintf( logstream, "Precinct packet information: \n");
146   for( i=0; i<index.SIZ.Csiz; i++){
147     fprintf( logstream, "Component %d\n", i);
148     print_faixbox( index.precpacket[i]);
149   }
150
151   print_allmetadata( index.metadatalist);
152 }
153
154 void print_SIZ( SIZmarker_param_t SIZ)
155 {
156   int i;
157
158   fprintf( logstream, "\tImage and Tile SIZ parameters\n");
159   fprintf( logstream, "\t              Rsiz: %#x\n", SIZ.Rsiz);
160   fprintf( logstream, "\t        Xsiz, Ysiz: (%d,%d) = (%#x, %#x)\n", SIZ.Xsiz, SIZ.Ysiz, SIZ.Xsiz, SIZ.Ysiz);
161   fprintf( logstream, "\t      XOsiz, YOsiz: (%d,%d) = (%#x, %#x)\n", SIZ.XOsiz, SIZ.YOsiz, SIZ.XOsiz, SIZ.YOsiz);
162   fprintf( logstream, "\t      XTsiz, YTsiz: (%d,%d) = (%#x, %#x)\n", SIZ.XTsiz, SIZ.YTsiz, SIZ.XTsiz, SIZ.YTsiz);
163   fprintf( logstream, "\t    XTOsiz, YTOsiz: (%d,%d) = (%#x, %#x)\n", SIZ.XTOsiz, SIZ.YTOsiz, SIZ.XTOsiz, SIZ.YTOsiz);
164   fprintf( logstream, "\t    XTnum, YTnum: (%d,%d)\n", SIZ.XTnum, SIZ.YTnum);
165   fprintf( logstream, "\t Num of Components: %d\n", SIZ.Csiz);
166   
167   for( i=0; i<SIZ.Csiz; i++)
168     fprintf( logstream, "\t[%d] (Ssiz, XRsiz, YRsiz): (%d, %d, %d) = (%#x, %#x, %#x)\n", i, SIZ.Ssiz[i], SIZ.XRsiz[i], SIZ.YRsiz[i], SIZ.Ssiz[i], SIZ.XRsiz[i], SIZ.YRsiz[i]);
169 }
170
171 void print_COD( CODmarker_param_t COD)
172 {
173   int i;
174
175   fprintf( logstream, "\tCoding style default COD parameters\n");
176   fprintf( logstream, "\t Progression order: %d [ LRCP=0, RLCP=1, RPCL=2, PCRL=3, CPRL=4]\n", COD.prog_order);
177   fprintf( logstream, "\t     Num of layers: %d\n", COD.numOflayers);
178   fprintf( logstream, "\t Decomposition lvl: %d\n", COD.numOfdecomp);
179   
180   for( i=0; i<=((COD.Scod & 0x01) ? COD.numOfdecomp:0); i++){
181     fprintf( logstream, "\t  [%d] XPsiz, YPsiz: (%d,%d) = (%#x, %#x)\n",i, COD.XPsiz[i], COD.YPsiz[i], COD.XPsiz[i], COD.YPsiz[i]);
182   }
183 }
184
185 void delete_index( index_param_t **index)
186 {
187   int i;
188
189   delete_metadatalist( &((*index)->metadatalist));
190
191   free( (*index)->COD.XPsiz);
192   free( (*index)->COD.YPsiz);
193   
194   delete_faixbox( &((*index)->tilepart));
195
196   for( i=0; i< (*index)->SIZ.XTnum*(*index)->SIZ.YTnum ;i++)
197     delete_mhixbox( &((*index)->tileheader[i]));
198   free( (*index)->tileheader);
199   
200   for( i=0; i<(*index)->SIZ.Csiz; i++)
201     delete_faixbox( &((*index)->precpacket[i]));
202   free( (*index)->precpacket);
203   
204   free(*index);
205 }
206
207 bool check_JP2boxidx( boxlist_param_t *toplev_boxlist)
208 {
209   box_param_t *iptr, *fidx, *prxy;
210   box_param_t *cidx, *jp2c;
211
212   iptr = search_box( "iptr", toplev_boxlist);
213   fidx = search_box( "fidx", toplev_boxlist);
214   cidx = search_box( "cidx", toplev_boxlist);
215   jp2c = search_box( "jp2c", toplev_boxlist);
216   prxy = gene_childboxbyType( fidx, 0, "prxy");
217
218   Byte8_t off = fetch_DBox8bytebigendian( iptr, 0);
219   if( off != fidx->offset)
220     fprintf( FCGI_stderr, "Reference File Index box offset in Index Finder box not correct\n");
221
222   Byte8_t len = fetch_DBox8bytebigendian( iptr, 8);
223   if( len != fidx->length)
224     fprintf( FCGI_stderr, "Reference File Index box length in Index Finder box not correct\n");
225
226  
227   int pos = 0;
228   Byte8_t ooff = fetch_DBox8bytebigendian( prxy, pos);
229   if( ooff != jp2c->offset)
230     fprintf( FCGI_stderr, "Reference jp2c offset in prxy box not correct\n");
231   pos += 8;
232
233   boxheader_param_t *obh = gene_childboxheader( prxy, pos);
234   if( obh->length != jp2c->length || strncmp( obh->type, "jp2c",4)!=0)
235     fprintf( FCGI_stderr, "Reference jp2c header in prxy box not correct\n");
236   pos += obh->headlen;
237   free(obh);
238   
239   Byte_t ni = fetch_DBox1byte( prxy, pos);
240   if( ni != 1){
241     fprintf( FCGI_stderr, "Multiple indexes not supported\n");
242     return false;
243   }  
244   pos += 1;
245   
246   Byte8_t ioff = fetch_DBox8bytebigendian( prxy, pos);
247   if( ioff != cidx->offset)
248     fprintf( FCGI_stderr, "Reference cidx offset in prxy box not correct\n");
249   pos += 8;
250
251   boxheader_param_t *ibh = gene_childboxheader( prxy, pos);
252   if( ibh->length != cidx->length || strncmp( ibh->type, "cidx",4)!=0)
253     fprintf( FCGI_stderr, "Reference cidx header in prxy box not correct\n");
254   pos += ibh->headlen;
255   free(ibh);
256   
257   free(prxy);
258
259   return true;
260 }
261
262 /**
263  * set code index parameters from cptr box
264  * I.3.2.2 Codestream Finder box
265  *
266  * @param[in]  cidx_box pointer to the reference cidx_box
267  * @param[out] jp2idx   pointer to index parameters
268  * @return              if succeeded (true) or failed (false)
269  */
270 bool set_cptrdata( box_param_t *cidx_box, index_param_t *jp2idx);
271
272 /**
273  * set code index parameters from mhix box for main header
274  * I.3.2.4.3 Header Index Table box
275  *
276  * @param[in]  cidx_box   pointer to the reference cidx_box
277  * @param[in]  codestream codestream parameters
278  * @param[out] jp2idx     pointer to index parameters
279  * @return                if succeeded (true) or failed (false)
280  */
281 bool set_mainmhixdata( box_param_t *cidx_box, codestream_param_t codestream, index_param_t *jp2idx);
282
283 /**
284  * set code index parameters from tpix box
285  * I.3.2.4.4 Tile-part Index Table box
286  *
287  * @param[in]  cidx_box   pointer to the reference cidx_box
288  * @param[out] jp2idx     pointer to index parameters
289  * @return                if succeeded (true) or failed (false)
290  */
291 bool set_tpixdata( box_param_t *cidx_box, index_param_t *jp2idx);
292
293 /**
294  * set code index parameters from thix box
295  * I.3.2.4.5 Tile Header Index Table box
296  *
297  * @param[in]  cidx_box   pointer to the reference cidx_box
298  * @param[out] jp2idx     pointer to index parameters
299  * @return                if succeeded (true) or failed (false)
300  */
301 bool set_thixdata( box_param_t *cidx_box, index_param_t *jp2idx);
302
303 /**
304  * set code index parameters from ppix box
305  * I.3.2.4.6 Precinct Packet Index Table box
306  *
307  * @param[in]  cidx_box   pointer to the reference cidx_box
308  * @param[out] jp2idx     pointer to index parameters
309  * @return                if succeeded (true) or failed (false)
310  */
311 bool set_ppixdata( box_param_t *cidx_box, index_param_t *jp2idx);
312
313 bool set_cidxdata( box_param_t *cidx_box, index_param_t *jp2idx)
314 {
315   box_param_t *manf_box;
316   manfbox_param_t *manf;
317   codestream_param_t codestream;
318
319   set_cptrdata( cidx_box, jp2idx);
320
321   codestream = set_codestream( cidx_box->fd, jp2idx->offset, jp2idx->length);
322
323   manf_box = gene_boxbyType( cidx_box->fd, get_DBoxoff( cidx_box), get_DBoxlen( cidx_box), "manf");
324   manf = gene_manfbox( manf_box);
325
326   if( !search_boxheader( "mhix", manf)){
327     fprintf( FCGI_stderr, "Error: mhix box not present in manfbox\n");
328     free(jp2idx);
329     return false;
330   }
331   set_mainmhixdata( cidx_box, codestream, jp2idx);
332
333   if( !search_boxheader( "tpix", manf)){
334     fprintf( FCGI_stderr, "Error: tpix box not present in manfbox\n");
335     free(jp2idx);
336     return false;
337   }
338   set_tpixdata( cidx_box, jp2idx);
339
340   if( !search_boxheader( "thix", manf)){
341     fprintf( FCGI_stderr, "Error: thix box not present in manfbox\n");
342     free(jp2idx);
343     return false;
344   }
345   set_thixdata( cidx_box, jp2idx);
346
347   if( !search_boxheader( "ppix", manf)){
348     fprintf( FCGI_stderr, "Error: ppix box not present in manfbox\n");
349     free(jp2idx);
350     return false;
351   }
352   set_ppixdata( cidx_box, jp2idx);
353
354   delete_manfbox( &manf);
355   free( manf_box);
356
357   return true;
358 }
359
360 bool set_cptrdata( box_param_t *cidx_box, index_param_t *jp2idx)
361 {
362   box_param_t *box;   //!< cptr box
363   Byte2_t dr, cont;
364
365   if( !(box = gene_boxbyType( cidx_box->fd, get_DBoxoff( cidx_box), get_DBoxlen( cidx_box), "cptr")))
366     return false;
367   
368   // DR: Data Reference. 
369   // If 0, the codestream or its Fragment Table box exists in the current file
370   if(( dr = fetch_DBox2bytebigendian( box, 0))){
371     fprintf( FCGI_stderr, "Error: Codestream not present in current file\n");
372     free( box);
373     return false;  
374   }
375   
376   // CONT: Container Type
377   // If 0, the entire codestream appears as a contiguous range of
378   // bytes within its file or resource.
379   if(( cont = fetch_DBox2bytebigendian( box, 2))){
380     fprintf( FCGI_stderr, "Error: Can't cope with fragmented codestreams yet\n");
381     free( box);
382     return false;  
383   }
384     
385   jp2idx->offset = fetch_DBox8bytebigendian( box, 4);
386   jp2idx->length = fetch_DBox8bytebigendian( box, 12);
387
388   free( box);
389
390   return true;
391 }
392
393
394 /**
395  * set SIZ marker information
396  * A.5 Fixed information marker segment
397  * A.5.1 Image and tile size (SIZ)
398  *
399  * @param[in]  sizmkidx   pointer to SIZ marker index in mhix box
400  * @param[in]  codestream codestream parameters
401  * @param[out] SIZ        SIZ marker parameters pointer
402  * @return                if succeeded (true) or failed (false)
403  */
404 bool set_SIZmkrdata( markeridx_param_t *sizmkidx, codestream_param_t codestream, SIZmarker_param_t *SIZ);
405
406 /**
407  * set code index parameters from COD marker in codestream
408  * A.6 Functional marker segments
409  * A.6.1 Coding style default (COD)
410  *
411  * @param[in]  codmkidx   pointer to COD marker index in mhix box
412  * @param[in]  codestream codestream parameters
413  * @param[out] COD        COD marker parameters pointer
414  * @return                if succeeded (true) or failed (false)
415  */
416 bool set_CODmkrdata( markeridx_param_t *codmkidx, codestream_param_t codestream, CODmarker_param_t *COD);
417
418 bool set_mainmhixdata( box_param_t *cidx_box, codestream_param_t codestream, index_param_t *jp2idx)
419 {
420   box_param_t *mhix_box;
421   mhixbox_param_t *mhix;
422   markeridx_param_t *sizmkidx;
423   markeridx_param_t *codmkidx;
424
425   if( !(mhix_box = gene_boxbyType( cidx_box->fd, get_DBoxoff( cidx_box), get_DBoxlen( cidx_box), "mhix")))
426     return false;
427
428   jp2idx->mhead_length = fetch_DBox8bytebigendian( mhix_box, 0);
429
430   mhix = gene_mhixbox( mhix_box);
431   free( mhix_box);
432
433   sizmkidx = search_markeridx( 0xff51, mhix);
434   set_SIZmkrdata( sizmkidx, codestream, &(jp2idx->SIZ));
435
436   codmkidx = search_markeridx( 0xff52, mhix);
437   set_CODmkrdata( codmkidx, codestream, &(jp2idx->COD));
438
439   delete_mhixbox( &mhix);
440
441   return true;
442 }
443
444 bool set_tpixdata( box_param_t *cidx_box, index_param_t *jp2idx)
445 {
446   box_param_t *tpix_box;   //!< tpix box
447   box_param_t *faix_box;   //!< faix box
448   
449   if( !(tpix_box = gene_boxbyType( cidx_box->fd, get_DBoxoff( cidx_box), get_DBoxlen( cidx_box), "tpix"))){
450     fprintf( FCGI_stderr, "Error: tpix box not present in cidx box\n");
451     return false;
452   }
453
454   if( !(faix_box = gene_boxbyType( tpix_box->fd, get_DBoxoff( tpix_box), get_DBoxlen( tpix_box), "faix"))){
455     fprintf( FCGI_stderr, "Error: faix box not present in tpix box\n");
456     return false;
457   }
458
459   jp2idx->tilepart = gene_faixbox( faix_box);
460   
461   free( tpix_box);
462   free( faix_box);
463
464   return true;
465 }
466
467 bool set_thixdata( box_param_t *cidx_box, index_param_t *jp2idx)
468 {
469   box_param_t *thix_box, *manf_box, *mhix_box;
470   manfbox_param_t *manf;
471   boxheader_param_t *ptr;
472   mhixbox_param_t *mhix;
473   Byte8_t pos, mhixseqoff;
474   Byte2_t tile_no;
475   
476   if( !(thix_box = gene_boxbyType( cidx_box->fd, get_DBoxoff( cidx_box), get_DBoxlen( cidx_box), "thix"))){
477     fprintf( FCGI_stderr, "Error: thix box not present in cidx box\n");
478     return false;
479   }
480   
481   if( !(manf_box = gene_boxbyType( thix_box->fd, get_DBoxoff( thix_box), get_DBoxlen( thix_box), "manf"))){
482     fprintf( FCGI_stderr, "Error: manf box not present in thix box\n");
483     free( thix_box);
484     return false;
485   }
486   
487   manf = gene_manfbox( manf_box);
488   ptr = manf->first;
489   mhixseqoff = manf_box->offset+manf_box->length;
490   pos = 0;
491   tile_no = 0;
492   jp2idx->tileheader = (mhixbox_param_t **)malloc( jp2idx->SIZ.XTnum*jp2idx->SIZ.YTnum*sizeof(mhixbox_param_t *));
493     
494   while( ptr){
495     if( !(mhix_box = gene_boxbyType( thix_box->fd, mhixseqoff+pos, get_DBoxlen( thix_box)-manf_box->length-pos, "mhix"))){
496       fprintf( FCGI_stderr, "Error: mhix box not present in thix box\n");
497       delete_manfbox( &manf);
498       free( manf_box);
499       free( thix_box);
500       return false;
501     }
502     mhix = gene_mhixbox( mhix_box);
503
504     pos += mhix_box->length;
505     ptr = ptr->next;
506
507     free( mhix_box);
508     jp2idx->tileheader[tile_no++] = mhix;
509   }
510
511   delete_manfbox( &manf);
512   free( manf_box);
513   free( thix_box);
514
515   return true;
516 }
517
518 bool set_ppixdata( box_param_t *cidx_box, index_param_t *jp2idx)
519 {
520   box_param_t *ppix_box, *faix_box, *manf_box;
521   manfbox_param_t *manf;     //!< manf
522   boxheader_param_t *bh;     //!< box headers
523   faixbox_param_t *faix;     //!< faix
524   Byte8_t inbox_offset;
525   int comp_idx;
526
527   if( !(ppix_box = gene_boxbyType( cidx_box->fd, get_DBoxoff( cidx_box), get_DBoxlen( cidx_box), "ppix"))){
528     fprintf( FCGI_stderr, "Error: ppix box not present in cidx box\n");
529     return false;
530   }
531
532   inbox_offset = get_DBoxoff( ppix_box);
533   if( !(manf_box = gene_boxbyType( ppix_box->fd, inbox_offset, get_DBoxlen( ppix_box), "manf"))){
534     fprintf( FCGI_stderr, "Error: manf box not present in ppix box\n");
535     free( ppix_box);
536     return false;
537   }
538
539   free( ppix_box);
540
541   manf = gene_manfbox( manf_box);
542   bh = search_boxheader( "faix", manf);
543   inbox_offset = manf_box->offset + manf_box->length;
544   
545   free( manf_box);
546
547   jp2idx->precpacket = (faixbox_param_t **)malloc( jp2idx->SIZ.Csiz*sizeof(faixbox_param_t *));
548
549   for( comp_idx=0; bh!=NULL; bh=bh->next, comp_idx++){
550     if( jp2idx->SIZ.Csiz <= comp_idx ){
551       fprintf( FCGI_stderr, "Error: num of faix boxes is not identical to num of components in ppix box\n");
552       return false;
553     }
554
555     if( !(faix_box = gene_boxbyOffset( cidx_box->fd, inbox_offset))){
556       fprintf( FCGI_stderr, "Error: faix box not present in ppix box\n");
557       return false;
558     }
559   
560     faix = gene_faixbox( faix_box);
561     jp2idx->precpacket[comp_idx] = faix;
562
563     inbox_offset = faix_box->offset + faix_box->length;
564     free( faix_box);   
565   }
566   
567   delete_manfbox( &manf);
568
569   return true;
570 }
571
572
573 bool set_SIZmkrdata( markeridx_param_t *sizmkidx, codestream_param_t codestream, SIZmarker_param_t *SIZ)
574 {
575   marker_param_t sizmkr;
576   int i;
577
578   sizmkr = set_marker( codestream, sizmkidx->code, sizmkidx->offset, sizmkidx->length);
579
580   SIZ->Lsiz = fetch_marker2bytebigendian( sizmkr, 0);
581
582   if( sizmkidx->length != SIZ->Lsiz){
583     fprintf( FCGI_stderr, "Error: marker %#x index is not correct\n", sizmkidx->code);
584     return false;
585   }
586   
587   SIZ->Rsiz   = fetch_marker2bytebigendian( sizmkr, 2);
588   SIZ->Xsiz   = fetch_marker4bytebigendian( sizmkr, 4);
589   SIZ->Ysiz   = fetch_marker4bytebigendian( sizmkr, 8);
590   SIZ->XOsiz  = fetch_marker4bytebigendian( sizmkr, 12);
591   SIZ->YOsiz  = fetch_marker4bytebigendian( sizmkr, 16);
592   SIZ->XTsiz  = fetch_marker4bytebigendian( sizmkr, 20);
593   SIZ->YTsiz  = fetch_marker4bytebigendian( sizmkr, 24);
594   SIZ->XTOsiz = fetch_marker4bytebigendian( sizmkr, 28);
595   SIZ->YTOsiz = fetch_marker4bytebigendian( sizmkr, 32);
596   SIZ->Csiz   = fetch_marker2bytebigendian( sizmkr, 36);
597
598   SIZ->XTnum  = ( SIZ->Xsiz-SIZ->XTOsiz+SIZ->XTsiz-1)/SIZ->XTsiz;
599   SIZ->YTnum  = ( SIZ->Ysiz-SIZ->YTOsiz+SIZ->YTsiz-1)/SIZ->YTsiz;
600   
601   for( i=0; i<(int)SIZ->Csiz; i++){
602     SIZ->Ssiz[i]  = fetch_marker1byte( sizmkr, 38+i*3);
603     SIZ->XRsiz[i] = fetch_marker1byte( sizmkr, 39+i*3);
604     SIZ->YRsiz[i] = fetch_marker1byte( sizmkr, 40+i*3);
605   }
606   return true;
607 }
608
609 bool set_CODmkrdata( markeridx_param_t *codmkidx, codestream_param_t codestream, CODmarker_param_t *COD)
610 {
611   marker_param_t codmkr;
612   int i;
613
614   codmkr = set_marker( codestream, codmkidx->code, codmkidx->offset, codmkidx->length);
615
616   COD->Lcod = fetch_marker2bytebigendian( codmkr, 0);
617
618   if( codmkidx->length != COD->Lcod){
619     fprintf( FCGI_stderr, "Error: marker %#x index is not correct\n", codmkidx->code);
620     return false;
621   }
622
623   COD->Scod   = fetch_marker1byte( codmkr, 2);
624   COD->prog_order  = fetch_marker1byte( codmkr, 3);
625   COD->numOflayers = fetch_marker2bytebigendian( codmkr, 4);
626   COD->numOfdecomp = fetch_marker1byte( codmkr, 7);
627   
628   if(COD->Scod & 0x01){
629     COD->XPsiz = (Byte4_t *)malloc( (COD->numOfdecomp+1)*sizeof(Byte4_t));
630     COD->YPsiz = (Byte4_t *)malloc( (COD->numOfdecomp+1)*sizeof(Byte4_t));
631
632     for( i=0; i<=COD->numOfdecomp; i++){
633       //precinct size
634       COD->XPsiz[i] = pow( 2, fetch_marker1byte( codmkr, 12+i) & 0x0F);
635       COD->YPsiz[i] = pow( 2,(fetch_marker1byte( codmkr, 12+i) & 0xF0) >> 4);
636     }
637   }
638   else{
639     COD->XPsiz = (Byte4_t *)malloc( sizeof(Byte4_t));
640     COD->YPsiz = (Byte4_t *)malloc( sizeof(Byte4_t));
641
642     COD->XPsiz[0] = COD->YPsiz[0] = pow(2,15);
643   }
644   return true;
645 }
646
647
648 Byte4_t max( Byte4_t n1, Byte4_t n2);
649 Byte4_t min( Byte4_t n1, Byte4_t n2);
650
651 range_param_t get_tile_range( Byte4_t Osiz, Byte4_t siz, Byte4_t TOsiz, Byte4_t Tsiz, Byte4_t tile_XYid, int level);
652
653 range_param_t get_tile_Xrange( SIZmarker_param_t SIZ, Byte4_t tile_id, int level)
654 {
655   return get_tile_range( SIZ.XOsiz, SIZ.Xsiz, SIZ.XTOsiz, SIZ.XTsiz, tile_id%SIZ.XTnum, level);
656 }
657
658 range_param_t get_tile_Yrange( SIZmarker_param_t SIZ, Byte4_t tile_id, int level)
659 {
660   return get_tile_range( SIZ.YOsiz, SIZ.Ysiz, SIZ.YTOsiz, SIZ.YTsiz, tile_id/SIZ.XTnum, level);
661 }
662
663 range_param_t get_tile_range( Byte4_t Osiz, Byte4_t siz, Byte4_t TOsiz, Byte4_t Tsiz, Byte4_t tile_XYid, int level)
664 {
665   range_param_t range;
666   int n;
667
668   range.minvalue = max( Osiz, TOsiz+tile_XYid*Tsiz);
669   range.maxvalue = min( siz,  TOsiz+(tile_XYid+1)*Tsiz);
670
671   for( n=0; n<level; n++){
672     range.minvalue = ceil(range.minvalue/2.0);
673     range.maxvalue = ceil(range.maxvalue/2.0);
674   }
675   return range;
676 }
677
678 Byte4_t get_tile_XSiz( SIZmarker_param_t SIZ, Byte4_t tile_id, int level)
679 {
680   range_param_t tile_Xrange;
681
682   tile_Xrange = get_tile_Xrange( SIZ, tile_id, level);
683   return tile_Xrange.maxvalue - tile_Xrange.minvalue;
684 }
685
686 Byte4_t get_tile_YSiz( SIZmarker_param_t SIZ, Byte4_t tile_id, int level)
687 {
688   range_param_t tile_Yrange;
689
690   tile_Yrange = get_tile_Yrange( SIZ, tile_id, level);
691   return tile_Yrange.maxvalue - tile_Yrange.minvalue;
692 }
693
694 Byte4_t max( Byte4_t n1, Byte4_t n2)
695 {
696   if( n1 < n2)
697     return n2;
698   else
699     return n1;
700 }
701
702 Byte4_t min( Byte4_t n1, Byte4_t n2)
703 {
704   if( n1 < n2)
705     return n1;
706   else
707     return n2;
708 }
709
710 bool isJPTfeasible( index_param_t index)
711 {
712   if( 1 < get_nmax( index.tilepart))
713     return true;
714   else
715     return false;
716 }