Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

thd_initdblk.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006 
00007 #include "mrilib.h"
00008 #include "thd.h"
00009 
00010 /*----------------------------------------------------------------
00011   Given a directory name, and a header filename, create a
00012   datablock that corresponds (return NULL if impossible).
00013 ------------------------------------------------------------------*/
00014 
00015 static int native_order = -1 ;
00016 static int no_mmap      = -1 ;
00017 static int no_ordwarn   = -1 ;
00018 
00019 THD_datablock * THD_init_one_datablock( char *dirname , char *headname )
00020 {
00021    THD_datablock *dblk ;
00022    THD_diskptr   *dkptr ;
00023    int ii ;
00024    char prefix[THD_MAX_NAME] = "\0" ;
00025    int default_order ;   /* 21 Jun 2000 */
00026 
00027 ENTRY("THD_init_one_datablock") ;
00028 
00029    /*-- sanity checks --*/
00030 
00031    if( dirname  == NULL || strlen(dirname)  == 0 ||
00032        headname == NULL || strlen(headname) == 0   ) RETURN( NULL ) ;
00033 
00034    FILENAME_TO_PREFIX(headname,prefix) ;
00035    if( strlen(prefix) == 0 ||
00036        strstr(headname,DATASET_HEADER_SUFFIX) == NULL ) RETURN( NULL ) ;
00037 
00038    /*-- byte ordering stuff --*/
00039 
00040    if( native_order < 0 ) native_order = mri_short_order() ;
00041 
00042    no_mmap    = AFNI_yesenv("AFNI_NOMMAP") ;
00043    no_ordwarn = AFNI_yesenv("AFNI_NO_BYTEORDER_WARNING") ;
00044 
00045    { char *hh = getenv("AFNI_BYTEORDER_INPUT") ;    /* 21 Jun 2000 */
00046      default_order = native_order ;
00047      if( hh != NULL ){
00048        if( strncmp(hh,LSB_FIRST_STRING,ORDER_LEN) == 0 )
00049          default_order = LSB_FIRST ;
00050        else if( strncmp(hh,MSB_FIRST_STRING,ORDER_LEN) == 0 )
00051          default_order = MSB_FIRST ;
00052      }
00053    }
00054 
00055 #if 1
00056 
00057    dblk  = EDIT_empty_datablock() ;    /* 11 Mar 2005 -- the new way */
00058    dkptr = dblk->diskptr ;
00059 
00060 #else
00061    /*-- create output datablock (the old way) --*/
00062 
00063    dblk              = myXtNew( THD_datablock ) ;
00064    dblk->type        = DATABLOCK_TYPE ;
00065    dblk->brick       = NULL ;  /* will be filled in below */
00066    dblk->brick_bytes = NULL ;  /* ditto */
00067    dblk->brick_fac   = NULL ;  /* ditto */
00068    dblk->total_bytes = 0 ;     /* ditto */
00069    dblk->malloc_type = DATABLOCK_MEM_UNDEFINED ;
00070    dblk->parent      = NULL ;
00071 
00072    dblk->brick_lab      = NULL ;  /* 30 Nov 1997 */
00073    dblk->brick_keywords = NULL ;
00074    dblk->brick_statcode = NULL ;
00075    dblk->brick_stataux  = NULL ;
00076 
00077    dblk->master_nvals = 0 ;     /* 11 Jan 1999 */
00078    dblk->master_ival  = NULL ;
00079    dblk->master_bytes = NULL ;
00080 
00081    dblk->master_bot = 1.0 ;     /* 21 Feb 2001 */
00082    dblk->master_top = 0.0 ;
00083 
00084    DBLK_unlock(dblk) ;  /* Feb 1998 */
00085 
00086    dblk->shm_idcode[0] = '\0' ;  /* 02 May 2003 */
00087 
00088    INIT_KILL(dblk->kl) ;
00089 
00090    dblk->diskptr       = dkptr = myXtNew( THD_diskptr ) ;
00091    dkptr->type         = DISKPTR_TYPE ;
00092    dkptr->storage_mode = STORAGE_UNDEFINED ;
00093 #if 0
00094    dkptr->byte_order   = native_order ;  /* 25 April 1998 */
00095 #else
00096    dkptr->byte_order   = default_order;  /* 21 June 2000 */
00097 #endif
00098 
00099    ADDTO_KILL(dblk->kl,dkptr) ;
00100 
00101 #endif  /* end of initializing empty datablock and diskptr */
00102 
00103    /*-- read attributes from disk, store in the datablock --*/
00104 
00105    THD_read_all_atr( headname , dblk ) ;
00106 
00107    /*-- 09 Mar 2005: all the attribute processing is moved away --*/
00108 
00109    ii = THD_datablock_from_atr( dblk, dirname, headname ) ;
00110    if( ii == 0 ){
00111      THD_delete_datablock( dblk ) ;
00112      myXtFree(dblk) ;
00113      RETURN( NULL ) ;
00114    }
00115 
00116 #if 0
00117    if( PRINT_TRACING ){
00118      char str[256] ;
00119      sprintf(str,"rank=%d nvals=%d dim[0]=%d dim[1]=%d dim[2]=%d",
00120              dkptr->rank , dkptr->nvals ,
00121              dkptr->dimsizes[0] , dkptr->dimsizes[1] , dkptr->dimsizes[2] ) ;
00122      STATUS(str) ;
00123    }
00124 #endif
00125 
00126    RETURN( dblk ) ;
00127 }
00128 
00129 /*-----*/
00130 
00131 #undef  MYHEAD
00132 #define MYHEAD ((headname==NULL) ? "UNKNOWN" : headname)
00133 
00134 /*---------------------------------------------------------------------------*/
00135 /*! Take the internal attributes and load the datablock struct up.
00136 -----------------------------------------------------------------------------*/
00137 
00138 int THD_datablock_from_atr( THD_datablock *dblk, char *dirname, char *headname )
00139 {
00140    THD_diskptr       *dkptr ;
00141    ATR_int           *atr_rank , *atr_dimen , *atr_scene , *atr_btype ;
00142    ATR_float         *atr_flt ;
00143    ATR_string        *atr_labs ;
00144    int ii , view_type , func_type , dset_type , nx,ny,nz,nvox , nvals , ibr,typ ;
00145    Boolean ok ;
00146    char prefix[THD_MAX_NAME]="Unknown" ;
00147    MRI_IMAGE *qim ;
00148    int brick_ccode ;
00149 
00150 ENTRY("THD_datablock_from_atr") ;
00151 
00152    if( dblk == NULL || dblk->natr <= 0 ) RETURN(0) ; /* bad input */
00153 
00154    dkptr = dblk->diskptr ;
00155 
00156    /*-- get relevant attributes: rank, dimensions, view_type & func_type --*/
00157 
00158    atr_rank  = THD_find_int_atr( dblk , ATRNAME_DATASET_RANK ) ;
00159    atr_dimen = THD_find_int_atr( dblk , ATRNAME_DATASET_DIMENSIONS ) ;
00160    atr_scene = THD_find_int_atr( dblk , ATRNAME_SCENE_TYPE ) ;
00161 
00162    /*-- missing an attribute ==> quit now --*/
00163 
00164    if( atr_rank == NULL || atr_dimen == NULL || atr_scene == NULL ) RETURN(0) ;
00165 
00166    /*-- load type codes from SCENE attribute --*/
00167 
00168    STATUS("loading *_type from SCENE") ;
00169 
00170    view_type = atr_scene->in[0] ;
00171    func_type = atr_scene->in[1] ;
00172    dset_type = atr_scene->in[2] ;
00173 
00174    /*-- load other values from attributes into relevant places --*/
00175 
00176    ok   = True ;
00177    nvox = 1 ;
00178 
00179    STATUS("loading from RANK") ;
00180 
00181    dkptr->rank = atr_rank->in[0] ;                /* N.B.: rank isn't used much */
00182    dkptr->nvals = dblk->nvals = nvals = atr_rank->in[1] ;  /* but nvals is used */
00183 
00184    STATUS("loading from DIMENSIONS") ;
00185 
00186    for( ii=0 ; ii < dkptr->rank ; ii++ ){
00187      dkptr->dimsizes[ii] = atr_dimen->in[ii] ;
00188      ok                  = ( ok && dkptr->dimsizes[ii] >= 1 ) ;
00189      nvox               *= dkptr->dimsizes[ii] ;
00190    }
00191 
00192 #if 0
00193    if( PRINT_TRACING ){
00194      char str[256] ;
00195      sprintf(str,"rank=%d nvals=%d dim[0]=%d dim[1]=%d dim[2]=%d nvox=%d",
00196              dkptr->rank , dkptr->nvals ,
00197              dkptr->dimsizes[0] , dkptr->dimsizes[1] , dkptr->dimsizes[2] , nvox ) ;
00198      STATUS(str) ;
00199    }
00200 #endif
00201 
00202    if( !ok || nvals < 1 ||
00203        dkptr->rank < THD_MIN_RANK || dkptr->rank > THD_MAX_RANK ){
00204      STATUS("bad rank!!??") ;
00205      RETURN(0) ;
00206    }
00207 
00208    /*-- create the storage filenames --*/
00209 
00210    STATUS("creating storage filenames") ;
00211 
00212    if( headname != NULL && strchr(headname,'+') != NULL ){
00213      FILENAME_TO_PREFIX(headname,prefix) ;
00214      THD_init_diskptr_names( dkptr, dirname,NULL,prefix , view_type , True ) ;
00215    } else {
00216      if( headname != NULL ) MCW_strncpy(prefix,headname,THD_MAX_NAME) ;
00217      THD_init_diskptr_names( dkptr, dirname,NULL,prefix , view_type , True ) ;
00218    }
00219 
00220    /*-- determine if the BRIK file exists --*/
00221 
00222    STATUS("checking if .BRIK file exists") ;
00223 
00224    brick_ccode = COMPRESS_filecode(dkptr->brick_name) ;
00225    if( brick_ccode != COMPRESS_NOFILE )
00226      dkptr->storage_mode = STORAGE_BY_BRICK ;  /* a .BRIK file */
00227 
00228    /*-- if VOLUME_FILENAMES attribute exists, make it so [20 Jun 2002] --*/
00229 
00230    if( headname != NULL && dkptr->storage_mode == STORAGE_UNDEFINED ){
00231      atr_labs = THD_find_string_atr(dblk,"VOLUME_FILENAMES") ;
00232      if( atr_labs != NULL ){
00233        dkptr->storage_mode = STORAGE_BY_VOLUMES ;
00234        dblk->malloc_type   = DATABLOCK_MEM_MALLOC ;
00235      }
00236    }
00237 
00238    /*-- now set the memory allocation codes, etc. --*/
00239 
00240    dblk->brick_fac = (float *) XtMalloc( sizeof(float) * nvals ) ;
00241    for( ibr=0 ; ibr < nvals ; ibr++ ) dblk->brick_fac[ibr] = 0.0 ;
00242 
00243    /* scaling factors from short type to float type, if nonzero */
00244 
00245    atr_flt = THD_find_float_atr( dblk , ATRNAME_BRICK_FLTFAC ) ;
00246    if( atr_flt != NULL ){
00247      for( ibr=0 ; ibr < nvals && ibr < atr_flt->nfl ; ibr++ )
00248        dblk->brick_fac[ibr] = atr_flt->fl[ibr] ;
00249    }
00250 
00251    /** Now create an empty shell of the "brick" == the data structure
00252        that will hold all the voxel data.  Note that all datablocks
00253        will have a brick, even if they never actually contain data
00254        themselves (are only warp-on-demand).
00255 
00256        If the BRICK_TYPES input attribute doesn't exist, then all
00257        sub-bricks are shorts.  This makes the code work with old-style
00258        datasets, which were always made up of shorts.
00259    **/
00260 
00261    atr_btype = THD_find_int_atr( dblk , ATRNAME_BRICK_TYPES ) ;
00262 
00263    if( atr_btype == NULL ){
00264      THD_init_datablock_brick( dblk , MRI_short , NULL ) ;
00265    } else {
00266      THD_init_datablock_brick( dblk , atr_btype->nin , atr_btype->in ) ;
00267    }
00268 
00269    if( !THD_datum_constant(dblk) ){ /* 15 Sep 2004 */
00270      fprintf(stderr,
00271              "\n** WARNING: File %s has mixed-type sub-bricks. ", MYHEAD ) ;
00272    }
00273 
00274    /* 25 April 1998: check if the byte order is stored inside */
00275 
00276    atr_labs = THD_find_string_atr( dblk , ATRNAME_BYTEORDER ) ;
00277    if( atr_labs != NULL && atr_labs->nch > 0 ){
00278 
00279      if( strncmp(atr_labs->ch,LSB_FIRST_STRING,ORDER_LEN) == 0 )
00280        dkptr->byte_order = LSB_FIRST ;
00281      else if( strncmp(atr_labs->ch,MSB_FIRST_STRING,ORDER_LEN) == 0 )
00282        dkptr->byte_order = MSB_FIRST ;
00283      else
00284        fprintf(stderr,"*** Unknown %s found in dataset %s\n",
00285                ATRNAME_BYTEORDER , MYHEAD ) ;
00286 
00287    } else if( !no_ordwarn                         &&
00288               DBLK_BRICK_TYPE(dblk,0) != MRI_byte &&
00289               dblk->diskptr->storage_mode == STORAGE_BY_BRICK ){ /* 20 Sep 1999 */
00290 
00291      static int first=1 ;
00292      if( first ){
00293        fprintf(stderr,
00294          "\n*** The situation below can be rectified with program '3drefit -byteorder':\n");
00295        first = 0 ;
00296      }
00297      fprintf(stderr," ** Dataset %s: assuming byteorder %s\n",
00298              MYHEAD , BYTE_ORDER_STRING(dkptr->byte_order)  ) ;
00299    }
00300 
00301    /* if the data is not on disk, the flag remains at DATABLOCK_MEM_UNDEFINED,
00302       otherwise the flag says how the memory for the bricks is to be created. */
00303 
00304    if( dkptr->storage_mode == STORAGE_BY_BRICK ){
00305 #if MMAP_THRESHOLD > 0
00306      dblk->malloc_type = (dblk->total_bytes > MMAP_THRESHOLD)
00307                          ? DATABLOCK_MEM_MMAP : DATABLOCK_MEM_MALLOC ;
00308      DBLK_mmapfix(dblk) ;  /* 18 Mar 2005 */
00309 #else
00310      dblk->malloc_type = DATABLOCK_MEM_MALLOC ;
00311 #endif
00312 
00313      /* must be malloc-ed if:
00314            data is compressed,
00315            data is not in native byte order, or
00316            user explicity forbids use of mmap   */
00317 
00318      if( brick_ccode >= 0 || dkptr->byte_order != native_order || no_mmap )
00319         dblk->malloc_type = DATABLOCK_MEM_MALLOC ;
00320    }
00321 
00322    /* 30 Nov 1997: create the labels for sub-bricks */
00323 
00324    THD_init_datablock_labels( dblk ) ;
00325 
00326    atr_labs = THD_find_string_atr( dblk , ATRNAME_BRICK_LABS ) ;
00327 
00328    if( atr_labs != NULL && atr_labs->nch > 0 ){  /* create labels from attribute */
00329      int ipos = -1 , ipold , ngood ;
00330 
00331      for( ibr=0 ; ibr < nvals ; ibr++ ){  /* loop over bricks */
00332 
00333        for( ipold = ipos++ ;                                     /* skip to */
00334             ipos < atr_labs->nch && atr_labs->ch[ipos] != '\0' ; /* next \0 */
00335             ipos++ ) /* nada */ ;                                /* or end. */
00336 
00337        ngood = ipos - ipold - 1 ;                   /* number of good chars */
00338        if( ngood > 0 ){
00339          XtFree(dblk->brick_lab[ibr]) ;
00340          if( ngood > 32 ) ngood = 32 ;      /* 02 Sep 2004 */
00341          dblk->brick_lab[ibr] = (char *) XtMalloc(sizeof(char)*(ngood+2)) ;
00342          memcpy( dblk->brick_lab[ibr] , atr_labs->ch+(ipold+1) , ngood ) ;
00343          dblk->brick_lab[ibr][ngood] = '\0' ;
00344        }
00345 
00346         if( ipos >= atr_labs->nch ) break ;  /* nothing more to do */
00347      } /* end of loop over sub-bricks */
00348    }
00349 
00350    /* create the keywords for sub-bricks */
00351 
00352    THD_init_datablock_keywords( dblk ) ;
00353 
00354    atr_labs = THD_find_string_atr( dblk , ATRNAME_BRICK_KEYWORDS ) ;
00355 
00356    if( atr_labs != NULL && atr_labs->nch > 0 ){  /* create keywords from attribute */
00357      int ipos = -1 , ipold , ngood ;
00358 
00359      for( ibr=0 ; ibr < nvals ; ibr++ ){  /* loop over bricks */
00360 
00361        for( ipold = ipos++ ;                                     /* skip to */
00362             ipos < atr_labs->nch && atr_labs->ch[ipos] != '\0' ; /* next \0 */
00363             ipos++ ) /* nada */ ;                                /* or end. */
00364 
00365        ngood = ipos - ipold - 1 ;                   /* number of good chars */
00366        if( ngood > 0 ){
00367          XtFree(dblk->brick_keywords[ibr]) ;
00368          dblk->brick_keywords[ibr] = (char *) XtMalloc(sizeof(char)*(ngood+2)) ;
00369          memcpy( dblk->brick_keywords[ibr] , atr_labs->ch+(ipold+1) , ngood ) ;
00370          dblk->brick_keywords[ibr][ngood] = '\0' ;
00371        }
00372 
00373        if( ipos >= atr_labs->nch ) break ;  /* nothing more to do */
00374      } /* end of loop over sub-bricks */
00375    }
00376 
00377    /* create the auxiliary statistics stuff for each brick, if present */
00378 
00379    atr_labs = THD_find_string_atr( dblk , "BRICK_STATSYM" ) ;  /* 01 Jun 2005 */
00380    if( atr_labs != NULL && atr_labs->nch > 0 ){
00381      NI_str_array *sar ; int scode,np ; float parm[3] ;
00382      sar = NI_decode_string_list( atr_labs->ch , ";" ) ;
00383      if( sar != NULL && sar->num > 0 ){
00384        for( ibr=0 ; ibr < nvals && ibr < sar->num ; ibr++ ){
00385          NI_stat_decode( sar->str[ibr] , &scode , parm,parm+1,parm+2 ) ;
00386          if( scode >= AFNI_FIRST_STATCODE && scode <= AFNI_LAST_STATCODE ){
00387            np = NI_stat_numparam(scode) ;
00388            THD_store_datablock_stataux( dblk , ibr,scode,np,parm ) ;
00389          }
00390        }
00391        NI_delete_str_array(sar) ;
00392      }
00393    } else {          /*--- the olde way to get ye brick stataux parameters ---*/
00394      atr_flt = THD_find_float_atr( dblk , ATRNAME_BRICK_STATAUX ) ;
00395      if( atr_flt != NULL && atr_flt->nfl >= 3 ){
00396        int ipos=0 , iv,nv,jv ;
00397 
00398        /* attribute stores all stataux stuff as follows:
00399             sub-brick-index  statcode  no.-of-values value ... value
00400             sub-brick-index  statcode  no.-of-values value ... value, etc. */
00401 
00402        while( ipos <= atr_flt->nfl - 3 ){
00403          iv = (int) ( atr_flt->fl[ipos++] ) ;  /* which sub-brick */
00404          jv = (int) ( atr_flt->fl[ipos++] ) ;  /* statcode */
00405          nv = (int) ( atr_flt->fl[ipos++] ) ;  /* # of values that follow */
00406 
00407          if( nv > atr_flt->nfl - ipos ) nv = atr_flt->nfl - ipos ;
00408 
00409          THD_store_datablock_stataux( dblk , iv , jv , nv , atr_flt->fl + ipos ) ;
00410          ipos += nv ;
00411        }
00412      }
00413    }
00414 #if 0
00415    if( PRINT_TRACING ){
00416      char str[256] ;
00417      sprintf(str,"rank=%d nvals=%d dim[0]=%d dim[1]=%d dim[2]=%d",
00418              dkptr->rank , dkptr->nvals ,
00419              dkptr->dimsizes[0] , dkptr->dimsizes[1] , dkptr->dimsizes[2] ) ;
00420      STATUS(str) ;
00421    }
00422 #endif
00423 
00424    RETURN(1) ;
00425 }
00426 
00427 /*---------------------------------------------------------------------------*/
00428 /* Macros to fetch an attribute named nnn and test if it exists. */
00429 
00430 #define ATR_IS_STR(nnn) ( (atr_str = THD_find_string_atr(blk,nnn)) != NULL )
00431 #define ATR_IS_FLT(nnn) ( (atr_flt = THD_find_float_atr (blk,nnn)) != NULL )
00432 #define ATR_IS_INT(nnn) ( (atr_int = THD_find_int_atr   (blk,nnn)) != NULL )
00433 
00434 /*---------------------------------------------------------------------------*/
00435 /*! Apply attributes to modify an existing datablock.
00436     Only some attributes have an effect.
00437     09 May 2005 -- written to support NIfTI-ization, by allowing
00438                    attributes to be applied AFTER a dataset is created.
00439 -----------------------------------------------------------------------------*/
00440 
00441 void THD_datablock_apply_atr( THD_3dim_dataset *dset )
00442 {
00443    THD_datablock     *blk ;
00444    THD_diskptr       *dkptr ;
00445    THD_dataxes       *daxes ;
00446    ATR_int           *atr_int = NULL ;
00447    ATR_float         *atr_flt = NULL;
00448    ATR_string        *atr_str = NULL ;
00449    int ii , view_type , func_type , dset_type , nx,ny,nz,nvox , nvals , ibr,typ ;
00450    Boolean ok ;
00451    char prefix[THD_MAX_NAME]="Unknown" ;
00452    MRI_IMAGE *qim ;
00453    int brick_ccode ;
00454 
00455 ENTRY("THD_datablock_apply_atr") ;
00456 
00457    if( !ISVALID_DSET(dset) ) EXRETURN ; /* bad input */
00458 
00459    blk   = dset->dblk   ;  if( blk == NULL   ) EXRETURN ;
00460    nvals = blk->nvals   ;  if( nvals <= 0    ) EXRETURN ;
00461    daxes = dset->daxes  ;  if( daxes == NULL ) EXRETURN ;
00462    dkptr = blk->diskptr ;
00463 
00464    /*-- brick labels --*/
00465 
00466    if( ATR_IS_STR(ATRNAME_BRICK_LABS) ){
00467      int ipos = -1 , ipold , ngood ;
00468 
00469      STATUS("brick labels") ;
00470      if( blk->brick_lab == NULL ) THD_init_datablock_labels( blk ) ;
00471 
00472      for( ibr=0 ; ibr < nvals ; ibr++ ){  /* loop over bricks */
00473 
00474        for( ipold = ipos++ ;                                   /* skip to */
00475             ipos < atr_str->nch && atr_str->ch[ipos] != '\0' ; /* next \0 */
00476             ipos++ ) /* nada */ ;                              /* or end. */
00477 
00478        ngood = ipos - ipold - 1 ;                 /* number of good chars */
00479        if( ngood > 0 ){
00480          XtFree(blk->brick_lab[ibr]) ;
00481          if( ngood > 32 ) ngood = 32 ;
00482          blk->brick_lab[ibr] = (char *) XtMalloc(sizeof(char)*(ngood+2)) ;
00483          memcpy( blk->brick_lab[ibr] , atr_str->ch+(ipold+1) , ngood ) ;
00484          blk->brick_lab[ibr][ngood] = '\0' ;
00485        }
00486 
00487         if( ipos >= atr_str->nch ) break ;  /* nothing more to do */
00488      } /* end of loop over sub-bricks */
00489    }
00490 
00491    /*-- keywords for the dataset itself --*/
00492 
00493    if( ATR_IS_STR(ATRNAME_KEYWORDS) ){
00494      STATUS("dataset keywords") ;
00495      dset->keywords = XtNewString( atr_str->ch ) ;
00496    }
00497 
00498    /*-- keywords for sub-bricks --*/
00499 
00500    if( ATR_IS_STR(ATRNAME_BRICK_KEYWORDS) ){
00501      int ipos = -1 , ipold , ngood ;
00502 
00503      STATUS("brick keywords") ;
00504      if( blk->brick_keywords == NULL ) THD_init_datablock_keywords( blk ) ;
00505 
00506      for( ibr=0 ; ibr < nvals ; ibr++ ){  /* loop over bricks */
00507 
00508        for( ipold = ipos++ ;                                   /* skip to */
00509             ipos < atr_str->nch && atr_str->ch[ipos] != '\0' ; /* next \0 */
00510             ipos++ ) /* nada */ ;                              /* or end. */
00511 
00512        ngood = ipos - ipold - 1 ;                 /* number of good chars */
00513        if( ngood > 0 ){
00514          XtFree(blk->brick_keywords[ibr]) ;
00515          blk->brick_keywords[ibr] = (char *) XtMalloc(sizeof(char)*(ngood+2)) ;
00516          memcpy( blk->brick_keywords[ibr] , atr_str->ch+(ipold+1) , ngood ) ;
00517          blk->brick_keywords[ibr][ngood] = '\0' ;
00518        }
00519 
00520        if( ipos >= atr_str->nch ) break ;  /* nothing more to do */
00521      } /* end of loop over sub-bricks */
00522    }
00523 
00524    /*-- auxiliary statistics stuff for each brick --*/
00525 
00526    if( ATR_IS_FLT(ATRNAME_BRICK_STATAUX) ){
00527      int ipos=0 , iv,nv,jv ;
00528 
00529      STATUS("brick stataux") ;
00530 
00531      /* attribute stores all stataux stuff as follows:
00532           sub-brick-index  statcode  no.-of-values value ... value
00533           sub-brick-index  statcode  no.-of-values value ... value, etc. */
00534 
00535      while( ipos <= atr_flt->nfl - 3 ){
00536        iv = (int) ( atr_flt->fl[ipos++] ) ;  /* which sub-brick */
00537        jv = (int) ( atr_flt->fl[ipos++] ) ;  /* statcode */
00538        nv = (int) ( atr_flt->fl[ipos++] ) ;  /* # of values that follow */
00539 
00540        if( nv > atr_flt->nfl - ipos ) nv = atr_flt->nfl - ipos ;
00541 
00542        THD_store_datablock_stataux( blk , iv , jv , nv , atr_flt->fl + ipos ) ;
00543        ipos += nv ;
00544      }
00545    }
00546 
00547    /*-- ID codes --*/
00548 
00549    if( ATR_IS_STR(ATRNAME_IDSTRING) )
00550      MCW_strncpy( dset->idcode.str , atr_str->ch , MCW_IDSIZE ) ;
00551 
00552    if( ATR_IS_STR(ATRNAME_IDDATE) )
00553      MCW_strncpy( dset->idcode.date , atr_str->ch , MCW_IDDATE ) ;
00554 
00555    if( ATR_IS_STR(ATRNAME_IDANATPAR) )
00556      MCW_strncpy( dset->anat_parent_idcode.str , atr_str->ch , MCW_IDSIZE ) ;
00557 
00558    if( ATR_IS_STR(ATRNAME_IDWARPPAR) )
00559      MCW_strncpy( dset->warp_parent_idcode.str , atr_str->ch , MCW_IDSIZE ) ;
00560 
00561    /*-- parent names --*/
00562 
00563    if( ATR_IS_STR(ATRNAME_ANATOMY_PARENT) &&
00564        ISZERO_IDCODE(dset->anat_parent_idcode) )
00565      MCW_strncpy( dset->anat_parent_name , atr_str->ch , THD_MAX_NAME ) ;
00566 
00567    if( ATR_IS_STR(ATRNAME_WARP_PARENT) &&
00568        ISZERO_IDCODE(dset->warp_parent_idcode) )
00569      MCW_strncpy( dset->warp_parent_name , atr_str->ch , THD_MAX_NAME ) ;
00570 
00571    if( ATR_IS_STR(ATRNAME_DATANAME) )
00572      MCW_strncpy( dset->self_name , atr_str->ch , THD_MAX_NAME ) ;
00573 
00574    /*-- markers --*/
00575 
00576    if( ATR_IS_FLT(ATRNAME_MARKSXYZ) && ATR_IS_STR(ATRNAME_MARKSLAB) ){
00577      int im , llen ;
00578      THD_ivec3 iv ;
00579      float xxdown,xxup , yydown,yyup , zzdown,zzup ;
00580 
00581      STATUS("markers") ;
00582 
00583      if( dset->markers == NULL ){
00584        dset->markers = myXtNew( THD_marker_set ) ;  /* new set */
00585        ADDTO_KILL(dset->kl , dset->markers) ;
00586      }
00587 
00588      COPY_INTO_STRUCT( *(dset->markers) ,  /* actual struct */
00589                        MARKS_FSTART ,      /* byte offset */
00590                        float ,             /* type being copied */
00591                        atr_flt->fl ,       /* start of source */
00592                        MARKS_FSIZE  ) ;    /* number of floats */
00593 
00594      COPY_INTO_STRUCT( *(dset->markers) ,
00595                        MARKS_LSTART ,
00596                        char ,
00597                        atr_str->ch ,
00598                        MARKS_LSIZE  ) ;
00599 
00600      xxdown = daxes->xxmin ; xxup = daxes->xxmax ;
00601      yydown = daxes->yymin ; yyup = daxes->yymax ;
00602      zzdown = daxes->zzmin ; zzup = daxes->zzmax ;
00603 
00604      dset->markers->numdef = dset->markers->numset = 0 ;
00605 
00606      for( im=0 ; im < MARKS_MAXNUM ; im++ ){
00607        llen = strlen( &(dset->markers->label[im][0]) ) ;
00608        dset->markers->valid[im]   =
00609           (llen > 0) &&
00610           ( dset->markers->xyz[im][0] >= xxdown ) &&
00611           ( dset->markers->xyz[im][0] <= xxup   ) &&
00612           ( dset->markers->xyz[im][1] >= yydown ) &&
00613           ( dset->markers->xyz[im][1] <= yyup   ) &&
00614           ( dset->markers->xyz[im][2] >= zzdown ) &&
00615           ( dset->markers->xyz[im][2] <= zzup   )    ;
00616 
00617        if( dset->markers->valid[im] ) (dset->markers->numset)++ ;
00618 
00619        if( llen > 0 ) (dset->markers->numdef)++ ;
00620 
00621        dset->markers->ovcolor[im] = -1 ;  /* default color */
00622      }
00623 
00624      if( ATR_IS_STR(ATRNAME_MARKSHELP) ){
00625        COPY_INTO_STRUCT( *(dset->markers) ,
00626                           MARKS_HSTART ,
00627                           char ,
00628                           atr_str->ch ,
00629                           MARKS_HSIZE  ) ;
00630      } else {
00631        for( im=0 ; im < MARKS_MAXNUM ; im++ )   /* no help */
00632          dset->markers->help[im][0] = '\0' ;
00633      }
00634 
00635      if( ATR_IS_INT(ATRNAME_MARKSFLAG) ){
00636        COPY_INTO_STRUCT( *(dset->markers) ,
00637                          MARKS_ASTART ,
00638                          int ,
00639                          atr_int->in ,
00640                          MARKS_ASIZE  ) ;
00641        dset->markers->type = dset->markers->aflags[0] ;
00642      } else {
00643        for( im=0 ; im < MARKS_MAXFLAG ; im++ )
00644          dset->markers->aflags[im] = -1 ;
00645      }
00646    }
00647 
00648    /*-- warp --*/
00649 
00650    if( ATR_IS_INT(ATRNAME_WARP_TYPE) && ATR_IS_FLT(ATRNAME_WARP_DATA) ){
00651      int wtype = atr_int->in[0] , rtype = atr_int->in[1]  ;
00652 
00653      STATUS("warp") ;
00654 
00655      dset->warp = myXtNew( THD_warp ) ;
00656      ADDTO_KILL( dset->kl , dset->warp ) ;
00657      switch( wtype ){
00658        case WARP_AFFINE_TYPE:{
00659          THD_affine_warp *ww = (THD_affine_warp *) dset->warp ;
00660          ww->type       = wtype ;
00661          ww->resam_type = rtype ;
00662          ww->warp.type  = MAPPING_LINEAR_TYPE ;
00663 
00664          COPY_INTO_STRUCT( ww->warp ,
00665                            MAPPING_LINEAR_FSTART ,
00666                            float ,
00667                            atr_flt->fl ,
00668                            MAPPING_LINEAR_FSIZE ) ;
00669        }
00670        break ;  /* end affine warp */
00671 
00672        case WARP_TALAIRACH_12_TYPE:{
00673          THD_talairach_12_warp *ww =
00674             (THD_talairach_12_warp *) dset->warp ;
00675          int iw , ioff ;
00676          ww->type       = wtype ;
00677          ww->resam_type = rtype ;
00678          for( iw=0 ; iw < 12 ; iw++ ){
00679             ww->warp[iw].type = MAPPING_LINEAR_TYPE ;
00680 
00681             ioff = iw * MAPPING_LINEAR_FSIZE ;
00682 
00683             COPY_INTO_STRUCT( ww->warp[iw] ,
00684                               MAPPING_LINEAR_FSTART ,
00685                               float ,
00686                               &(atr_flt->fl[ioff]) ,
00687                               MAPPING_LINEAR_FSIZE ) ;
00688 
00689          }  /* end loop over 12 warps */
00690        }
00691        break ;  /* end talairach_12 warp */
00692 
00693      } /* end of switch on warp type */
00694    }
00695 
00696    /*-- brick stats --*/
00697 
00698    if( ATR_IS_FLT(ATRNAME_BRICK_STATS) ){
00699      int qq ;
00700      STATUS("brick statistics") ;
00701      dset->stats         = myXtNew( THD_statistics ) ;
00702      dset->stats->type   = STATISTICS_TYPE ;
00703      dset->stats->parent = (XtPointer) dset ;
00704      dset->stats->nbstat = blk->nvals ;
00705      dset->stats->bstat  = (THD_brick_stats *)
00706                               XtMalloc( sizeof(THD_brick_stats) * blk->nvals ) ;
00707      for( qq=0 ; qq < blk->nvals ; qq++ ){
00708        if( 2*qq+1 < atr_flt->nfl ){
00709            dset->stats->bstat[qq].min = atr_flt->fl[2*qq] ;
00710            dset->stats->bstat[qq].max = atr_flt->fl[2*qq+1] ;
00711        } else {
00712            INVALIDATE_BSTAT( dset->stats->bstat[qq] ) ;
00713        }
00714      }
00715      ADDTO_KILL( dset->kl , dset->stats->bstat ) ;
00716      ADDTO_KILL( dset->kl , dset->stats ) ;
00717    }
00718 
00719    /*-- tagset --*/
00720 
00721    if( ATR_IS_INT(ATRNAME_TAGSET_NUM)    &&
00722        ATR_IS_FLT(ATRNAME_TAGSET_FLOATS) &&
00723        ATR_IS_STR(ATRNAME_TAGSET_LABELS)    ){
00724 
00725      int nin=atr_int->nin , nfl=atr_flt->nfl , nch=atr_str->nch ;
00726      int ii , ntag , nfper , jj , kk ;
00727 
00728      STATUS("tagset") ;
00729      ntag  = atr_int->in[0] ;  /* number of tags */
00730      nfper = atr_int->in[1] ;  /* number of floats per tag */
00731 
00732      if( ntag > MAX_TAG_NUM ) ntag = MAX_TAG_NUM ;
00733 
00734      dset->tagset = myXtNew( THD_usertaglist ) ;  /* create tagset */
00735      ADDTO_KILL( dset->kl , dset->tagset ) ;
00736 
00737      dset->tagset->num = ntag ;
00738      strcpy( dset->tagset->label , "Bebe Rebozo" ) ;  /* not used */
00739 
00740      /* read out tag values; allow for chance there isn't enough data */
00741 
00742 #undef  TF
00743 #define TF(i,j) \
00744   ( ((j)<nfper && (i)*nfper+(j)<nfl) ? atr_flt->fl[(i)*nfper+(j)] : -666.0 )
00745 
00746      for( ii=0 ; ii < ntag ; ii++ ){
00747        dset->tagset->tag[ii].x   = TF(ii,0) ; /* coords */
00748        dset->tagset->tag[ii].y   = TF(ii,1) ;
00749        dset->tagset->tag[ii].z   = TF(ii,2) ;
00750        dset->tagset->tag[ii].val = TF(ii,3) ; /* value */
00751        dset->tagset->tag[ii].ti  = TF(ii,4) ; /* time index: if < 0, not set */
00752        if( dset->tagset->tag[ii].ti >= 0 ){
00753          dset->tagset->tag[ii].set = 1 ;
00754        } else {
00755          dset->tagset->tag[ii].set = 0 ; dset->tagset->tag[ii].ti = 0 ;
00756        }
00757      }
00758 #undef TF
00759 
00760      /* read out tag labels; allow for empty labels */
00761 
00762      jj = 0 ;
00763      for( ii=0 ; ii < ntag ; ii++ ){
00764        if( jj < nch ){
00765          kk = strlen( atr_str->ch + jj ) ;
00766          if( kk > 0 ) TAG_SETLABEL( dset->tagset->tag[ii] , atr_str->ch + jj );
00767          else         sprintf( dset->tagset->tag[ii].label , "Tag %d" , ii+1 );
00768          jj += kk+1 ;
00769        } else {
00770          sprintf( dset->tagset->tag[ii].label , "Tag %d" , ii+1 ) ;
00771        }
00772      }
00773    }
00774 
00775    EXRETURN ;
00776 }
00777 
00778 /*----------------------------------------------------------------
00779   Initialize the brick structure of a datablock.  This will
00780   contain no data for now, but provides a place for it to go.
00781   This routine presumes that the datablock is already set up
00782   with the dimension information, etc.
00783 
00784   Inputs ntype and btype determine the types of data in the
00785   sub-bricks.
00786     If btype == NULL, then the type code for all sub-bricks
00787                         is given by ntype;
00788     If ntype < 0,     then the type code for all sub-bricks
00789                         is taken from the datablock pointed
00790                         to by (THD_datablock *) btype;
00791     If btype != NULL  then the type code for each sub-brick
00792       && ntype > 0      is taken from the array pointed to
00793                         by (int *) btype.
00794 ------------------------------------------------------------------*/
00795 
00796 void THD_init_datablock_brick( THD_datablock *dblk, int ntype, void *btype )
00797 {
00798    int ibr , nx,ny,nz , typ , nvals ;
00799    MRI_IMAGE *qim ;
00800    THD_datablock *pblk = NULL ;
00801    int *itype = NULL ;
00802 
00803 ENTRY("THD_init_datablock_brick") ;
00804 
00805    if( ! ISVALID_DATABLOCK(dblk)   ) EXRETURN ;   /* bad inputs */
00806    if( ntype <  0 && btype == NULL ) EXRETURN ;
00807    if( ntype == 0 && btype != NULL ) EXRETURN ;
00808 
00809    if( ntype < 0 ){                            /* copy types from */
00810      pblk = (THD_datablock *) btype ;          /* datablock pblk  */
00811      if( ! ISVALID_DATABLOCK(pblk) ) EXRETURN ;
00812    } else {
00813      itype = (int *) btype ;
00814    }
00815 
00816    nx    = dblk->diskptr->dimsizes[0] ;
00817    ny    = dblk->diskptr->dimsizes[1] ;
00818    nz    = dblk->diskptr->dimsizes[2] ;
00819    nvals = dblk->nvals ; if( nvals < 1 ) EXRETURN ; /* something wrong */
00820 
00821    /** make brick information arrays, if not pre-existing **/
00822 
00823    if( dblk->brick_bytes == NULL ){
00824 STATUS("making dblk->brick_bytes") ;
00825      dblk->brick_bytes = (int *) XtMalloc( sizeof(int) * nvals ) ;
00826    }
00827 
00828    if( dblk->brick_fac == NULL ){
00829 STATUS("making dblk->brick_fac") ;
00830      dblk->brick_fac = (float *) XtMalloc( sizeof(float) * nvals ) ;
00831      for( ibr=0 ; ibr < nvals ; ibr++ )
00832        dblk->brick_fac[ibr] = (ntype < 0) ? pblk->brick_fac[ibr] : 0.0 ;
00833    }
00834 
00835    dblk->total_bytes = 0 ;
00836 
00837    if( dblk->brick != NULL ){
00838 STATUS("destroying old dblk->brick") ;
00839      DESTROY_IMARR( dblk->brick ) ;
00840    }
00841 
00842 STATUS("making new dblk->brick") ;
00843    INIT_IMARR( dblk->brick ) ;  /* make the new brick */
00844 
00845    /** set up each sub-brick **/
00846 
00847 STATUS("starting sub-brick creations") ;
00848    for( ibr=0 ; ibr < nvals ; ibr++ ){
00849       if( ntype < 0 ){     typ = DBLK_BRICK_TYPE(pblk,ibr) ;
00850       } else if( itype == NULL ){
00851                            typ = ntype ;  /* all types are the same */
00852       } else {
00853          if( ibr < ntype ) typ = itype[ibr] ;     /* types may vary */
00854          else              typ = itype[ntype-1] ;
00855       }
00856 
00857       qim = mri_new_vol_empty( nx,ny,nz , typ ) ;  /* image with no data */
00858       ADDTO_IMARR( dblk->brick , qim ) ;
00859 
00860       dblk->brick_bytes[ibr] = qim->pixel_size * qim->nvox ;
00861       dblk->total_bytes     += dblk->brick_bytes[ibr] ;
00862    }
00863 
00864 STATUS("exiting") ;
00865    EXRETURN ;
00866 }
00867 
00868 /*----------------------------------------------------------------
00869   Determine if the brick factors are needed.
00870 ------------------------------------------------------------------*/
00871 
00872 int THD_need_brick_factor( THD_3dim_dataset * dset )
00873 {
00874    int ii , nval ;
00875 
00876 ENTRY("THD_need_brick_factor") ;
00877 
00878    if( ! ISVALID_DSET(dset)            ) RETURN( 0 ) ;
00879    if( ! ISVALID_DATABLOCK(dset->dblk) ) RETURN( 0 ) ;
00880    if( dset->dblk->brick_fac == NULL   ) RETURN( 0 ) ;
00881 
00882    nval = DSET_NVALS(dset) ;
00883    for( ii=0 ; ii < nval ; ii++ )
00884       if( DSET_BRICK_FACTOR(dset,ii) != 0.0 &&
00885           DSET_BRICK_FACTOR(dset,ii) != 1.0   ) RETURN( 1 ) ;
00886 
00887    RETURN( 0 ) ;
00888 }
 

Powered by Plone

This site conforms to the following standards: