Skip to content.

AFNI/NIfTI Server

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

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

nifti1_io.h

Go to the documentation of this file.
00001 #ifndef _NIFTI_IO_HEADER_
00002 #define _NIFTI_IO_HEADER_
00003 
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <math.h>
00008 #include <ctype.h>
00009 
00010 #ifndef DONT_INCLUDE_ANALYZE_STRUCT
00011 #define DONT_INCLUDE_ANALYZE_STRUCT  /*** not needed herein ***/
00012 #endif
00013 #include "nifti1.h"                  /*** NIFTI-1 header specification ***/
00014 
00015 #include "znzlib/znzlib.h"
00016 
00017 /*=================*/
00018 #ifdef  __cplusplus
00019 extern "C" {
00020 #endif
00021 /*=================*/
00022 
00023 /*****===================================================================*****/
00024 /*****         File nifti1_io.h == Declarations for nifti1_io.c          *****/
00025 /*****...................................................................*****/
00026 /*****            This code is released to the public domain.            *****/
00027 /*****...................................................................*****/
00028 /*****  Author: Robert W Cox, SSCC/DIRP/NIMH/NIH/DHHS/USA/EARTH          *****/
00029 /*****  Date:   August 2003                                              *****/
00030 /*****...................................................................*****/
00031 /*****  Neither the National Institutes of Health (NIH), nor any of its  *****/
00032 /*****  employees imply any warranty of usefulness of this software for  *****/
00033 /*****  any purpose, and do not assume any liability for damages,        *****/
00034 /*****  incidental or otherwise, caused by any use of this document.     *****/
00035 /*****===================================================================*****/
00036 
00037 /* 
00038    Modified by: Mark Jenkinson (FMRIB Centre, University of Oxford, UK)
00039    Date: July/August 2004 
00040 
00041    Mainly adding low-level IO and changing things to allow gzipped files
00042    to be read and written
00043    Full backwards compatability should have been maintained
00044 */
00045 
00046 /********************** Some sample data structures **************************/
00047 
00048 typedef struct {                   /** 4x4 matrix struct **/
00049   float m[4][4] ;
00050 } mat44 ;
00051 
00052 typedef struct {                   /** 3x3 matrix struct **/
00053   float m[3][3] ;
00054 } mat33 ;
00055 
00056 /*...........................................................................*/
00057 
00058 typedef struct {                  /** Image storage struct **/
00059 
00060   int ndim ;                      /* last dimension greater than 1 (1..7) */
00061   int nx,ny,nz , nt,nu,nv,nw ;    /* dimensions of grid array */
00062   int dim[8] ;                    /* dim[0]=ndim, dim[1]=nx, etc. */
00063   int nvox ;                      /* number of voxels = nx*ny*nz*... */
00064   int nbyper ;                    /* bytes per voxel */
00065   int datatype ;                  /* type of data in voxels: DT_* code */
00066 
00067   float dx,dy,dz , dt,du,dv,dw ;  /* grid spacings */
00068   float pixdim[8] ;               /* pixdim[1]=dx, etc. */
00069 
00070   float scl_slope , scl_inter ;   /* scaling parameters */
00071 
00072   float cal_min , cal_max ;       /* calibration parameters */
00073 
00074   int qform_code , sform_code ;   /* codes for (x,y,z) space meaning */
00075 
00076   int freq_dim ,                  /* indexes (1,2,3, or 0) for MRI */
00077       phase_dim , slice_dim ;     /* directions in dim[]/pixdim[]  */
00078 
00079   int slice_code ;                /* code for slice timing pattern */
00080   int slice_start , slice_end ;   /* indexes for start & stop of slices */
00081   float slice_duration ;          /* time between individual slices */
00082 
00083   float quatern_b, quatern_c,     /* quaternion transform parameters   */
00084         quatern_d, qoffset_x,     /* [when writing a dataset,  these ] */
00085         qoffset_y, qoffset_z,     /* [are used for qform, NOT qto_xyz] */
00086         qfac                 ;
00087 
00088   mat44 qto_xyz ;                 /* qform: transform (i,j,k) to (x,y,z) */
00089   mat44 qto_ijk ;                 /* qform: transform (x,y,z) to (i,j,k) */
00090 
00091   mat44 sto_xyz ;                 /* sform: transform (i,j,k) to (x,y,z) */
00092   mat44 sto_ijk ;                 /* sform: transform (x,y,z) to (i,j,k) */
00093 
00094   float toffset ;                 /* time coordinate offset */
00095 
00096   int xyz_units , time_units ;    /* dx,dy,dz & dt units: NIFTI_UNITS_* code */
00097 
00098   int nifti_type ;                /* 0==ANALYZE, 2==NIFTI-1 (2 files),
00099                                                  1==NIFTI-1 (1 file) ,
00100                                                  3==NIFTI-ASCII (1 file) */
00101   int intent_code ;               /* statistic type (or something) */
00102   float intent_p1, intent_p2,     /* intent parameters */
00103         intent_p3 ;
00104   char intent_name[16] ;
00105 
00106   char descrip[80], aux_file[24];
00107 
00108   char *fname ;                   /* header filename (.hdr or .nii) */
00109   char *iname ;                   /* image filename (.img or .nii)  */
00110   int   iname_offset ;            /* offset into iname where data starts */
00111   int   swapsize ;                /* swapping unit in image data (might be 0) */
00112   int   byteorder ;               /* byte order on disk (MSB_ or LSB_FIRST) */
00113   void *data ;                    /* pointer to data: nbyper*nvox bytes */
00114 
00115   int                num_ext ;    /* number of extensions in ext_list */
00116   nifti1_extension * ext_list ;   /* array of extension structures (with data)*/
00117 
00118 } nifti_image ;
00119 
00120 
00121 
00122 /* struct for return from nifti_image_read_bricks() */
00123 typedef struct {
00124   int     nbricks;    /* the number of allocated pointers in 'bricks' */
00125   int     bsize;      /* the length of each data block, in bytes      */
00126   void ** bricks;     /* array of pointers to data blocks             */
00127 } nifti_brick_list;
00128 
00129 
00130 /*****************************************************************************/
00131 /*--------------- Prototypes of functions defined in this file --------------*/
00132 
00133 char *nifti_datatype_string   ( int dt ) ;
00134 char *nifti_units_string      ( int uu ) ;
00135 char *nifti_intent_string     ( int ii ) ;
00136 char *nifti_xform_string      ( int xx ) ;
00137 char *nifti_slice_string      ( int ss ) ;
00138 char *nifti_orientation_string( int ii ) ;
00139 
00140 int nifti_is_inttype( int dt ) ;
00141 
00142 mat44 mat44_inverse( mat44 R ) ;
00143 
00144 mat33 mat33_inverse( mat33 R ) ;
00145 mat33 mat33_polar  ( mat33 A ) ;
00146 float mat33_rownorm( mat33 A ) ;
00147 float mat33_colnorm( mat33 A ) ;
00148 float mat33_determ ( mat33 R ) ;
00149 mat33 mat33_mul    ( mat33 A , mat33 B ) ;
00150 
00151 void swap_2bytes ( int n , void *ar ) ;
00152 void swap_4bytes ( int n , void *ar ) ;
00153 void swap_8bytes ( int n , void *ar ) ;
00154 void swap_16bytes( int n , void *ar ) ;
00155 void swap_Nbytes ( int n , int siz , void *ar ) ;
00156 
00157 void swap_nifti_header( struct nifti_1_header *h , int is_nifti ) ;
00158 unsigned int get_filesize( char *pathname ) ;
00159 
00160 /* main routine for reading a dataset into a brick list */
00161 nifti_image *nifti_image_read_bricks(char *hname , int nbricks, int * blist,
00162                                      nifti_brick_list * NBL );
00163 int          nifti_image_load_bricks(nifti_image *nim , int nbricks, int *blist,
00164                                      nifti_brick_list * NBL );
00165 void         free_NBL( nifti_brick_list * NBL );
00166 
00167 /* main routine for reading a dataset into a single image */
00168 nifti_image *nifti_image_read    ( char *hname , int read_data ) ;
00169 int          nifti_image_load    ( nifti_image *nim ) ;
00170 void         nifti_image_unload  ( nifti_image *nim ) ;
00171 void         nifti_image_free    ( nifti_image *nim ) ;
00172 void         nifti_image_write   ( nifti_image *nim ) ;
00173 void         nifti_image_infodump( nifti_image *nim ) ;
00174 
00175 void         nifti_disp_lib_hist( void ) ;     /* to display library history */
00176 void         nifti_disp_lib_version( void ) ;  /* to display library version */
00177 int          nifti_disp_matrix_orient( char * mesg, mat44 mat );
00178 
00179 char *       nifti_image_to_ascii  ( nifti_image *nim ) ;
00180 nifti_image *nifti_image_from_ascii( char *str, int * bytes_read ) ;
00181 
00182 size_t       nifti_get_volsize(nifti_image *nim) ;
00183 
00184 /* basic file operations */
00185 int          is_nifti_file( char *hname ) ;
00186 char *       nifti_find_file_extension( char * name ) ;
00187 int          nifti_validfilename(char* fname) ;
00188 
00189 int          disp_nifti_1_header( char * info, nifti_1_header * hp ) ;
00190 void         nifti_set_debug_level( int level ) ;
00191 int          print_hex_vals( char * data, int nbytes, FILE * fp ) ;
00192 
00193 int valid_nifti_brick_list(nifti_image * nim , int nbricks, int * blist,
00194                            int disp_error);
00195 
00196 
00197 void nifti_datatype_sizes( int datatype , int *nbyper, int *swapsize ) ;
00198 
00199 void mat44_to_quatern( mat44 R ,
00200                        float *qb, float *qc, float *qd,
00201                        float *qx, float *qy, float *qz,
00202                        float *dx, float *dy, float *dz, float *qfac ) ;
00203 
00204 mat44 quatern_to_mat44( float qb, float qc, float qd,
00205                         float qx, float qy, float qz,
00206                         float dx, float dy, float dz, float qfac );
00207 
00208 mat44 make_orthog_mat44( float r11, float r12, float r13 ,
00209                          float r21, float r22, float r23 ,
00210                          float r31, float r32, float r33  ) ;
00211 
00212 int   unescape_string( char *str ) ;  /* string utility functions */
00213 char *escapize_string( char *str ) ;
00214 
00215 int short_order(void) ;              /* CPU byte order */
00216 
00217 
00218 /* Orientation codes that might be returned from mat44_to_orientation(). */
00219 
00220 #define NIFTI_L2R  1    /* Left to Right         */
00221 #define NIFTI_R2L  2    /* Right to Left         */
00222 #define NIFTI_P2A  3    /* Posterior to Anterior */
00223 #define NIFTI_A2P  4    /* Anterior to Posterior */
00224 #define NIFTI_I2S  5    /* Inferior to Superior  */
00225 #define NIFTI_S2I  6    /* Superior to Inferior  */
00226 
00227 void mat44_to_orientation( mat44 R , int *icod, int *jcod, int *kcod ) ;
00228 
00229 /*--------------------- Low level IO routines ------------------------------*/
00230 
00231 static char * nifti_findhdrname (char* fname);
00232 static char * nifti_findimgname (char* fname , int nifti_type);
00233 static int    nifti_is_gzfile   (char* fname);
00234 
00235 char * nifti_makebasename(char* fname);
00236 
00237 /* extension routines */
00238 static int  nifti_read_extensions( nifti_image *nim, znzFile fp );
00239 static int  nifti_read_next_extension( nifti1_extension * nex,
00240                                        nifti_image *nim, znzFile fp );
00241 static int  nifti_add_exten_to_list( nifti1_extension *  new_ext,
00242                                      nifti1_extension ** list, int new_length );
00243 static int  nifti_write_extensions(znzFile fp, nifti_image *nim);
00244 static int  nifti_valid_extension( nifti_image *nim, int size, int code );
00245 static void update_nifti_image_for_brick_list(nifti_image * nim , int nbricks);
00246 
00247 /* NBL routines */
00248 static int  nifti_load_NBL_bricks(nifti_image * nim , int * slist, int * sindex,
00249                                   nifti_brick_list * NBL, znzFile fp );
00250 static int  nifti_alloc_NBL_mem(  nifti_image * nim, int nbricks,
00251                                   nifti_brick_list * nbl);
00252 static int  nifti_copynsort(int nbricks, int *blist, int **slist, int **sindex);
00253 
00254 
00255 /* misc */
00256 static int int_force_positive( int * list, int nel );
00257 
00258 
00259 /* internal I/O routines */
00260 static znzFile nifti_image_write_hdr_img(nifti_image *nim , int write_data , 
00261                                          char* opts);
00262 static znzFile nifti_image_write_hdr_img2( nifti_image *nim , int write_data , 
00263                                            char* opts, znzFile *imgfile );
00264 static znzFile nifti_image_open(char *hname , char *opts , nifti_image **nim);
00265 static znzFile nifti_image_load_prep( nifti_image *nim );
00266 static size_t nifti_read_buffer(znzFile fp, void* datatptr, size_t ntot,
00267                                 nifti_image *nim);
00268 
00269 static void   nifti_write_all_data(znzFile fp, nifti_image *nim);
00270 static size_t nifti_write_buffer(znzFile fp, void *buffer, size_t numbytes);
00271 
00272 
00273 /* other routines */
00274 nifti_image *          nifti_copy_nim_info(nifti_image* src);
00275 nifti_image *          nifti_simple_init_nim();
00276 struct nifti_1_header  nifti_convert_nim2nhdr(nifti_image* nim);
00277 nifti_image *          nifti_convert_nhdr2nim(struct nifti_1_header nhdr,
00278                                               char* fname);
00279 void                   nifti_set_iname_offset(nifti_image *nim);
00280 
00281 /*-------------------- Some C convenience macros ----------------------------*/
00282 
00283 #undef  LNI_FERR /* local nifti file error, to be compact and repetative */
00284 #define LNI_FERR(func,msg,file)                                      \
00285             fprintf(stderr,"** ERROR (%s): %s '%s'\n",func,msg,file)
00286 
00287 #undef  swap_2
00288 #undef  swap_4
00289 #define swap_2(s) swap_2bytes(1,&(s))  /* s is a 2-byte short; swap in place */
00290 #define swap_4(v) swap_4bytes(1,&(v))  /* v is a 4-byte value; swap in place */
00291 
00292                         /***** isfinite() is a C99 macro, which is
00293                                present in many C implementations already *****/
00294 
00295 #undef IS_GOOD_FLOAT
00296 #undef FIXED_FLOAT
00297 
00298 #ifdef isfinite       /* use isfinite() to check floats/doubles for goodness */
00299 #  define IS_GOOD_FLOAT(x) isfinite(x)       /* check if x is a "good" float */
00300 #  define FIXED_FLOAT(x)   (isfinite(x) ? (x) : 0)           /* fixed if bad */
00301 #else
00302 #  define IS_GOOD_FLOAT(x) 1                               /* don't check it */
00303 #  define FIXED_FLOAT(x)   (x)                               /* don't fix it */
00304 #endif
00305 
00306 #undef  ASSIF                                 /* assign v to *p, if possible */
00307 #define ASSIF(p,v) if( (p)!=NULL ) *(p) = (v)
00308 
00309 #undef  MSB_FIRST
00310 #undef  LSB_FIRST
00311 #undef  REVERSE_ORDER
00312 #define LSB_FIRST 1
00313 #define MSB_FIRST 2
00314 #define REVERSE_ORDER(x) (3-(x))    /* convert MSB_FIRST <--> LSB_FIRST */
00315 
00316 #define LNI_MAX_NIA_EXT_LEN 100000  /* consider a longer extension invalid */
00317 /*=================*/
00318 #ifdef  __cplusplus
00319 }
00320 #endif
00321 /*=================*/
00322 
00323 #endif /* _NIFTI_IO_HEADER_ */
Powered by Plone

This site conforms to the following standards: