Merge pull request #545 from mayeut/master
[openjpeg.git] / src / lib / openjpip / index_manager.c
index 6f8357e9c338ba410660b4d786e7388e997a9484..b26d6d2a8c1785a857fc2320e52d966d56a855bb 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * $Id: index_manager.c 53 2011-05-09 16:55:39Z kaori $
+ * $Id$
  *
- * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
- * Copyright (c) 2002-2011, Professor Benoit Macq
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
  * Copyright (c) 2010-2011, Kaori Hagihara
  * All rights reserved.
  *
@@ -33,7 +33,6 @@
 #include <math.h>
 #include <string.h>
 
-#include "bool.h"
 #include "opj_inttypes.h"
 #include "index_manager.h"
 #include "box_manager.h"
@@ -59,7 +58,7 @@
  * @param[in] toplev_boxlist top level box list
  * @return                   if correct (true) or wrong (false)
  */
-bool check_JP2boxidx( boxlist_param_t *toplev_boxlist);
+OPJ_BOOL check_JP2boxidx( boxlist_param_t *toplev_boxlist);
 
 /**
  * set code index parameters (parse cidx box)
@@ -69,7 +68,7 @@ bool check_JP2boxidx( boxlist_param_t *toplev_boxlist);
  * @param[out] codeidx  pointer to index parameters
  * @return              if succeeded (true) or failed (false)
  */
-bool set_cidxdata( box_param_t *cidx_box, index_param_t *codeidx);
+OPJ_BOOL set_cidxdata( box_param_t *cidx_box, index_param_t *codeidx);
 
 index_param_t * parse_jp2file( int fd)
 {
@@ -99,11 +98,11 @@ index_param_t * parse_jp2file( int fd)
     return NULL;
   }
 
-  jp2idx = (index_param_t *)malloc( sizeof(index_param_t));
+  jp2idx = (index_param_t *)opj_malloc( sizeof(index_param_t));
   
   if( !set_cidxdata( cidx, jp2idx)){
     fprintf( FCGI_stderr, "Error: Not correctl format in cidx box\n");
-    free(jp2idx);
+    opj_free(jp2idx);
     delete_boxlist( &toplev_boxlist);
     return NULL;
   }
@@ -190,22 +189,22 @@ void delete_index( index_param_t **index)
 
   for( i=0; i< (int)((*index)->SIZ.XTnum*(*index)->SIZ.YTnum);i++)
     delete_mhixbox( &((*index)->tileheader[i]));
-  free( (*index)->tileheader);
+  opj_free( (*index)->tileheader);
   
   for( i=0; i<(*index)->SIZ.Csiz; i++)
     delete_faixbox( &((*index)->precpacket[i]));
-  free( (*index)->precpacket);
+  opj_free( (*index)->precpacket);
   
-  free(*index);
+  opj_free(*index);
 }
 
 void delete_COD( CODmarker_param_t COD)
 {
-  if( COD.XPsiz)    free( COD.XPsiz);
-  if( COD.YPsiz)    free( COD.YPsiz);
+  if( COD.XPsiz)    opj_free( COD.XPsiz);
+  if( COD.YPsiz)    opj_free( COD.YPsiz);
 }
 
-bool check_JP2boxidx( boxlist_param_t *toplev_boxlist)
+OPJ_BOOL check_JP2boxidx( boxlist_param_t *toplev_boxlist)
 {
   box_param_t *iptr, *fidx, *prxy;
   box_param_t *cidx, *jp2c;
@@ -242,12 +241,12 @@ bool check_JP2boxidx( boxlist_param_t *toplev_boxlist)
   if( obh->length != jp2c->length || strncmp( obh->type, "jp2c",4)!=0)
     fprintf( FCGI_stderr, "Reference jp2c header in prxy box not correct\n");
   pos += obh->headlen;
-  free(obh);
+  opj_free(obh);
   
   ni = fetch_DBox1byte( prxy, pos);
   if( ni != 1){
     fprintf( FCGI_stderr, "Multiple indexes not supported\n");
-    return false;
+    return OPJ_FALSE;
   }  
   pos += 1;
   
@@ -260,11 +259,11 @@ bool check_JP2boxidx( boxlist_param_t *toplev_boxlist)
   if( ibh->length != cidx->length || strncmp( ibh->type, "cidx",4)!=0)
     fprintf( FCGI_stderr, "Reference cidx header in prxy box not correct\n");
   pos += ibh->headlen;
-  free(ibh);
+  opj_free(ibh);
   
-  free(prxy);
+  opj_free(prxy);
 
-  return true;
+  return OPJ_TRUE;
 }
 
 /**
@@ -275,7 +274,7 @@ bool check_JP2boxidx( boxlist_param_t *toplev_boxlist)
  * @param[out] jp2idx   pointer to index parameters
  * @return              if succeeded (true) or failed (false)
  */
-bool set_cptrdata( box_param_t *cidx_box, index_param_t *jp2idx);
+OPJ_BOOL set_cptrdata( box_param_t *cidx_box, index_param_t *jp2idx);
 
 /**
  * set code index parameters from mhix box for main header
@@ -286,7 +285,7 @@ bool set_cptrdata( box_param_t *cidx_box, index_param_t *jp2idx);
  * @param[out] jp2idx     pointer to index parameters
  * @return                if succeeded (true) or failed (false)
  */
-bool set_mainmhixdata( box_param_t *cidx_box, codestream_param_t codestream, index_param_t *jp2idx);
+OPJ_BOOL set_mainmhixdata( box_param_t *cidx_box, codestream_param_t codestream, index_param_t *jp2idx);
 
 /**
  * set code index parameters from tpix box
@@ -296,7 +295,7 @@ bool set_mainmhixdata( box_param_t *cidx_box, codestream_param_t codestream, ind
  * @param[out] jp2idx     pointer to index parameters
  * @return                if succeeded (true) or failed (false)
  */
-bool set_tpixdata( box_param_t *cidx_box, index_param_t *jp2idx);
+OPJ_BOOL set_tpixdata( box_param_t *cidx_box, index_param_t *jp2idx);
 
 /**
  * set code index parameters from thix box
@@ -306,7 +305,7 @@ bool set_tpixdata( box_param_t *cidx_box, index_param_t *jp2idx);
  * @param[out] jp2idx     pointer to index parameters
  * @return                if succeeded (true) or failed (false)
  */
-bool set_thixdata( box_param_t *cidx_box, index_param_t *jp2idx);
+OPJ_BOOL set_thixdata( box_param_t *cidx_box, index_param_t *jp2idx);
 
 /**
  * set code index parameters from ppix box
@@ -316,9 +315,9 @@ bool set_thixdata( box_param_t *cidx_box, index_param_t *jp2idx);
  * @param[out] jp2idx     pointer to index parameters
  * @return                if succeeded (true) or failed (false)
  */
-bool set_ppixdata( box_param_t *cidx_box, index_param_t *jp2idx);
+OPJ_BOOL set_ppixdata( box_param_t *cidx_box, index_param_t *jp2idx);
 
-bool set_cidxdata( box_param_t *cidx_box, index_param_t *jp2idx)
+OPJ_BOOL set_cidxdata( box_param_t *cidx_box, index_param_t *jp2idx)
 {
   box_param_t *manf_box;
   manfbox_param_t *manf;
@@ -333,52 +332,52 @@ bool set_cidxdata( box_param_t *cidx_box, index_param_t *jp2idx)
 
   if( !search_boxheader( "mhix", manf)){
     fprintf( FCGI_stderr, "Error: mhix box not present in manfbox\n");
-    free(jp2idx);
-    return false;
+    opj_free(jp2idx);
+    return OPJ_FALSE;
   }
   set_mainmhixdata( cidx_box, codestream, jp2idx);
 
   if( !search_boxheader( "tpix", manf)){
     fprintf( FCGI_stderr, "Error: tpix box not present in manfbox\n");
-    free(jp2idx);
-    return false;
+    opj_free(jp2idx);
+    return OPJ_FALSE;
   }
   set_tpixdata( cidx_box, jp2idx);
 
   if( !search_boxheader( "thix", manf)){
     fprintf( FCGI_stderr, "Error: thix box not present in manfbox\n");
-    free(jp2idx);
-    return false;
+    opj_free(jp2idx);
+    return OPJ_FALSE;
   }
   set_thixdata( cidx_box, jp2idx);
 
   if( !search_boxheader( "ppix", manf)){
     fprintf( FCGI_stderr, "Error: ppix box not present in manfbox\n");
-    free(jp2idx);
-    return false;
+    opj_free(jp2idx);
+    return OPJ_FALSE;
   }
   set_ppixdata( cidx_box, jp2idx);
 
   delete_manfbox( &manf);
-  free( manf_box);
+  opj_free( manf_box);
 
-  return true;
+  return OPJ_TRUE;
 }
 
-bool set_cptrdata( box_param_t *cidx_box, index_param_t *jp2idx)
+OPJ_BOOL set_cptrdata( box_param_t *cidx_box, index_param_t *jp2idx)
 {
   box_param_t *box;   /**< cptr box*/
   Byte2_t dr, cont;
 
   if( !(box = gene_boxbyType( cidx_box->fd, get_DBoxoff( cidx_box), get_DBoxlen( cidx_box), "cptr")))
-    return false;
+    return OPJ_FALSE;
   
   /* DR: Data Reference. */
   /* If 0, the codestream or its Fragment Table box exists in the current file*/
   if(( dr = fetch_DBox2bytebigendian( box, 0))){
     fprintf( FCGI_stderr, "Error: Codestream not present in current file\n");
-    free( box);
-    return false;  
+    opj_free( box);
+    return OPJ_FALSE;  
   }
   
   /* CONT: Container Type*/
@@ -386,16 +385,16 @@ bool set_cptrdata( box_param_t *cidx_box, index_param_t *jp2idx)
   /* bytes within its file or resource.*/
   if(( cont = fetch_DBox2bytebigendian( box, 2))){
     fprintf( FCGI_stderr, "Error: Can't cope with fragmented codestreams yet\n");
-    free( box);
-    return false;  
+    opj_free( box);
+    return OPJ_FALSE;  
   }
     
   jp2idx->offset = (OPJ_OFF_T)fetch_DBox8bytebigendian( box, 4);
   jp2idx->length = fetch_DBox8bytebigendian( box, 12);
 
-  free( box);
+  opj_free( box);
 
-  return true;
+  return OPJ_TRUE;
 }
 
 
@@ -409,7 +408,7 @@ bool set_cptrdata( box_param_t *cidx_box, index_param_t *jp2idx)
  * @param[out] SIZ        SIZ marker parameters pointer
  * @return                if succeeded (true) or failed (false)
  */
-bool set_SIZmkrdata( markeridx_param_t *sizmkidx, codestream_param_t codestream, SIZmarker_param_t *SIZ);
+OPJ_BOOL set_SIZmkrdata( markeridx_param_t *sizmkidx, codestream_param_t codestream, SIZmarker_param_t *SIZ);
 
 /**
  * set code index parameters from COD marker in codestream
@@ -421,9 +420,9 @@ bool set_SIZmkrdata( markeridx_param_t *sizmkidx, codestream_param_t codestream,
  * @param[out] COD        COD marker parameters pointer
  * @return                if succeeded (true) or failed (false)
  */
-bool set_CODmkrdata( markeridx_param_t *codmkidx, codestream_param_t codestream, CODmarker_param_t *COD);
+OPJ_BOOL set_CODmkrdata( markeridx_param_t *codmkidx, codestream_param_t codestream, CODmarker_param_t *COD);
 
-bool set_mainmhixdata( box_param_t *cidx_box, codestream_param_t codestream, index_param_t *jp2idx)
+OPJ_BOOL set_mainmhixdata( box_param_t *cidx_box, codestream_param_t codestream, index_param_t *jp2idx)
 {
   box_param_t *mhix_box;
   mhixbox_param_t *mhix;
@@ -431,12 +430,12 @@ bool set_mainmhixdata( box_param_t *cidx_box, codestream_param_t codestream, ind
   markeridx_param_t *codmkidx;
 
   if( !(mhix_box = gene_boxbyType( cidx_box->fd, get_DBoxoff( cidx_box), get_DBoxlen( cidx_box), "mhix")))
-    return false;
+    return OPJ_FALSE;
 
   jp2idx->mhead_length = fetch_DBox8bytebigendian( mhix_box, 0);
 
   mhix = gene_mhixbox( mhix_box);
-  free( mhix_box);
+  opj_free( mhix_box);
 
   sizmkidx = search_markeridx( 0xff51, mhix);
   set_SIZmkrdata( sizmkidx, codestream, &(jp2idx->SIZ));
@@ -446,33 +445,33 @@ bool set_mainmhixdata( box_param_t *cidx_box, codestream_param_t codestream, ind
 
   delete_mhixbox( &mhix);
 
-  return true;
+  return OPJ_TRUE;
 }
 
-bool set_tpixdata( box_param_t *cidx_box, index_param_t *jp2idx)
+OPJ_BOOL set_tpixdata( box_param_t *cidx_box, index_param_t *jp2idx)
 {
   box_param_t *tpix_box;   /**< tpix box*/
   box_param_t *faix_box;   /**< faix box*/
   
   if( !(tpix_box = gene_boxbyType( cidx_box->fd, get_DBoxoff( cidx_box), get_DBoxlen( cidx_box), "tpix"))){
     fprintf( FCGI_stderr, "Error: tpix box not present in cidx box\n");
-    return false;
+    return OPJ_FALSE;
   }
 
   if( !(faix_box = gene_boxbyType( tpix_box->fd, get_DBoxoff( tpix_box), get_DBoxlen( tpix_box), "faix"))){
     fprintf( FCGI_stderr, "Error: faix box not present in tpix box\n");
-    return false;
+    return OPJ_FALSE;
   }
 
   jp2idx->tilepart = gene_faixbox( faix_box);
   
-  free( tpix_box);
-  free( faix_box);
+  opj_free( tpix_box);
+  opj_free( faix_box);
 
-  return true;
+  return OPJ_TRUE;
 }
 
-bool set_thixdata( box_param_t *cidx_box, index_param_t *jp2idx)
+OPJ_BOOL set_thixdata( box_param_t *cidx_box, index_param_t *jp2idx)
 {
   box_param_t *thix_box, *manf_box, *mhix_box;
   manfbox_param_t *manf;
@@ -484,13 +483,13 @@ bool set_thixdata( box_param_t *cidx_box, index_param_t *jp2idx)
   
   if( !(thix_box = gene_boxbyType( cidx_box->fd, get_DBoxoff( cidx_box), get_DBoxlen( cidx_box), "thix"))){
     fprintf( FCGI_stderr, "Error: thix box not present in cidx box\n");
-    return false;
+    return OPJ_FALSE;
   }
   
   if( !(manf_box = gene_boxbyType( thix_box->fd, get_DBoxoff( thix_box), get_DBoxlen( thix_box), "manf"))){
     fprintf( FCGI_stderr, "Error: manf box not present in thix box\n");
-    free( thix_box);
-    return false;
+    opj_free( thix_box);
+    return OPJ_FALSE;
   }
   
   manf = gene_manfbox( manf_box);
@@ -498,33 +497,33 @@ bool set_thixdata( box_param_t *cidx_box, index_param_t *jp2idx)
   mhixseqoff = manf_box->offset+(OPJ_OFF_T)manf_box->length;
   pos = 0;
   tile_no = 0;
-  jp2idx->tileheader = (mhixbox_param_t **)malloc( jp2idx->SIZ.XTnum*jp2idx->SIZ.YTnum*sizeof(mhixbox_param_t *));
+  jp2idx->tileheader = (mhixbox_param_t **)opj_malloc( jp2idx->SIZ.XTnum*jp2idx->SIZ.YTnum*sizeof(mhixbox_param_t *));
     
   while( ptr){
     if( !(mhix_box = gene_boxbyType( thix_box->fd, mhixseqoff+(OPJ_OFF_T)pos, get_DBoxlen( thix_box)-manf_box->length-pos, "mhix"))){
       fprintf( FCGI_stderr, "Error: mhix box not present in thix box\n");
       delete_manfbox( &manf);
-      free( manf_box);
-      free( thix_box);
-      return false;
+      opj_free( manf_box);
+      opj_free( thix_box);
+      return OPJ_FALSE;
     }
     mhix = gene_mhixbox( mhix_box);
 
     pos += mhix_box->length;
     ptr = ptr->next;
 
-    free( mhix_box);
+    opj_free( mhix_box);
     jp2idx->tileheader[tile_no++] = mhix;
   }
 
   delete_manfbox( &manf);
-  free( manf_box);
-  free( thix_box);
+  opj_free( manf_box);
+  opj_free( thix_box);
 
-  return true;
+  return OPJ_TRUE;
 }
 
-bool set_ppixdata( box_param_t *cidx_box, index_param_t *jp2idx)
+OPJ_BOOL set_ppixdata( box_param_t *cidx_box, index_param_t *jp2idx)
 {
   box_param_t *ppix_box, *faix_box, *manf_box;
   manfbox_param_t *manf;     /**< manf*/
@@ -535,50 +534,50 @@ bool set_ppixdata( box_param_t *cidx_box, index_param_t *jp2idx)
 
   if( !(ppix_box = gene_boxbyType( cidx_box->fd, get_DBoxoff( cidx_box), get_DBoxlen( cidx_box), "ppix"))){
     fprintf( FCGI_stderr, "Error: ppix box not present in cidx box\n");
-    return false;
+    return OPJ_FALSE;
   }
 
   inbox_offset = get_DBoxoff( ppix_box);
   if( !(manf_box = gene_boxbyType( ppix_box->fd, inbox_offset, get_DBoxlen( ppix_box), "manf"))){
     fprintf( FCGI_stderr, "Error: manf box not present in ppix box\n");
-    free( ppix_box);
-    return false;
+    opj_free( ppix_box);
+    return OPJ_FALSE;
   }
 
-  free( ppix_box);
+  opj_free( ppix_box);
 
   manf = gene_manfbox( manf_box);
   bh = search_boxheader( "faix", manf);
   inbox_offset = manf_box->offset + (OPJ_OFF_T)manf_box->length;
   
-  free( manf_box);
+  opj_free( manf_box);
 
-  jp2idx->precpacket = (faixbox_param_t **)malloc( jp2idx->SIZ.Csiz*sizeof(faixbox_param_t *));
+  jp2idx->precpacket = (faixbox_param_t **)opj_malloc( jp2idx->SIZ.Csiz*sizeof(faixbox_param_t *));
 
   for( comp_idx=0; bh!=NULL; bh=bh->next, comp_idx++){
     if( jp2idx->SIZ.Csiz <= comp_idx ){
       fprintf( FCGI_stderr, "Error: num of faix boxes is not identical to num of components in ppix box\n");
-      return false;
+      return OPJ_FALSE;
     }
 
     if( !(faix_box = gene_boxbyOffset( cidx_box->fd, inbox_offset))){
       fprintf( FCGI_stderr, "Error: faix box not present in ppix box\n");
-      return false;
+      return OPJ_FALSE;
     }
   
     faix = gene_faixbox( faix_box);
     jp2idx->precpacket[comp_idx] = faix;
 
     inbox_offset = faix_box->offset + (OPJ_OFF_T)faix_box->length;
-    free( faix_box);   
+    opj_free( faix_box);   
   }
   
   delete_manfbox( &manf);
 
-  return true;
+  return OPJ_TRUE;
 }
 
-bool set_SIZmkrdata( markeridx_param_t *sizmkidx, codestream_param_t codestream, SIZmarker_param_t *SIZ)
+OPJ_BOOL set_SIZmkrdata( markeridx_param_t *sizmkidx, codestream_param_t codestream, SIZmarker_param_t *SIZ)
 {
   marker_param_t sizmkr;
   int i;
@@ -589,7 +588,7 @@ bool set_SIZmkrdata( markeridx_param_t *sizmkidx, codestream_param_t codestream,
 
   if( sizmkidx->length != SIZ->Lsiz){
     fprintf( FCGI_stderr, "Error: marker %#x index is not correct\n", sizmkidx->code);
-    return false;
+    return OPJ_FALSE;
   }
   
   SIZ->Rsiz   = fetch_marker2bytebigendian( sizmkr, 2);
@@ -611,10 +610,10 @@ bool set_SIZmkrdata( markeridx_param_t *sizmkidx, codestream_param_t codestream,
     SIZ->XRsiz[i] = fetch_marker1byte( sizmkr, 39+i*3);
     SIZ->YRsiz[i] = fetch_marker1byte( sizmkr, 40+i*3);
   }
-  return true;
+  return OPJ_TRUE;
 }
 
-bool set_CODmkrdata( markeridx_param_t *codmkidx, codestream_param_t codestream, CODmarker_param_t *COD)
+OPJ_BOOL set_CODmkrdata( markeridx_param_t *codmkidx, codestream_param_t codestream, CODmarker_param_t *COD)
 {
   marker_param_t codmkr;
   int i;
@@ -625,7 +624,7 @@ bool set_CODmkrdata( markeridx_param_t *codmkidx, codestream_param_t codestream,
 
   if( codmkidx->length != COD->Lcod){
     fprintf( FCGI_stderr, "Error: marker %#x index is not correct\n", codmkidx->code);
-    return false;
+    return OPJ_FALSE;
   }
 
   COD->Scod   = fetch_marker1byte( codmkr, 2);
@@ -634,8 +633,8 @@ bool set_CODmkrdata( markeridx_param_t *codmkidx, codestream_param_t codestream,
   COD->numOfdecomp = fetch_marker1byte( codmkr, 7);
   
   if(COD->Scod & 0x01){
-    COD->XPsiz = (Byte4_t *)malloc( (OPJ_SIZE_T)(COD->numOfdecomp+1)*sizeof(Byte4_t));
-    COD->YPsiz = (Byte4_t *)malloc( (OPJ_SIZE_T)(COD->numOfdecomp+1)*sizeof(Byte4_t));
+    COD->XPsiz = (Byte4_t *)opj_malloc( (OPJ_SIZE_T)(COD->numOfdecomp+1)*sizeof(Byte4_t));
+    COD->YPsiz = (Byte4_t *)opj_malloc( (OPJ_SIZE_T)(COD->numOfdecomp+1)*sizeof(Byte4_t));
 
     for( i=0; i<=COD->numOfdecomp; i++){
       /*precinct size*/
@@ -644,12 +643,12 @@ bool set_CODmkrdata( markeridx_param_t *codmkidx, codestream_param_t codestream,
     }
   }
   else{
-    COD->XPsiz = (Byte4_t *)malloc( sizeof(Byte4_t));
-    COD->YPsiz = (Byte4_t *)malloc( sizeof(Byte4_t));
+    COD->XPsiz = (Byte4_t *)opj_malloc( sizeof(Byte4_t));
+    COD->YPsiz = (Byte4_t *)opj_malloc( sizeof(Byte4_t));
 
-    COD->XPsiz[0] = COD->YPsiz[0] = pow(2,15);
+    COD->XPsiz[0] = COD->YPsiz[0] = 1 << 15; /* pow(2,15); */
   }
-  return true;
+  return OPJ_TRUE;
 }
 
 
@@ -723,10 +722,10 @@ Byte4_t min( Byte4_t n1, Byte4_t n2)
     return n2;
 }
 
-bool isJPTfeasible( index_param_t index)
+OPJ_BOOL isJPTfeasible( index_param_t index)
 {
   if( 1 < get_nmax( index.tilepart))
-    return true;
+    return OPJ_TRUE;
   else
-    return false;
+    return OPJ_FALSE;
 }