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.h

Go to the documentation of this file.
00001 #ifndef _NIFTI_HEADER_
00002 #define _NIFTI_HEADER_
00003 
00004 /*****************************************************************************
00005       ** This file defines the "NIFTI-1" header format.               **
00006       ** It is derived from 2 meetings at the NIH (31 Mar 2003 and    **
00007       ** 02 Sep 2003) of the Data Format Working Group (DFWG),        **
00008       ** chartered by the NIfTI (Neuroimaging Informatics Technology  **
00009       ** Initiative) at the National Institutes of Health (NIH).      **
00010       **--------------------------------------------------------------**
00011       ** Neither the National Institutes of Health (NIH), the DFWG,   **
00012       ** nor any of the members or employees of these institutions    **
00013       ** imply any warranty of usefulness of this material for any    **
00014       ** purpose, and do not assume any liability for damages,        **
00015       ** incidental or otherwise, caused by any use of this document. **
00016       ** If these conditions are not acceptable, do not use this!     **
00017       **--------------------------------------------------------------**
00018       ** Author:   Robert W Cox (NIMH, Bethesda)                      **
00019       ** Advisors: John Ashburner (FIL, London),                      **
00020       **           Stephen Smith (FMRIB, Oxford),                     **
00021       **           Mark Jenkinson (FMRIB, Oxford)                     **
00022 ******************************************************************************/
00023 
00024 /*---------------------------------------------------------------------------*/
00025 /* Note that the ANALYZE 7.5 file header (dbh.h) is
00026          (c) Copyright 1986-1995
00027          Biomedical Imaging Resource
00028          Mayo Foundation
00029    Incorporation of components of dbh.h are by permission of the
00030    Mayo Foundation.
00031 
00032    Changes from the ANALYZE 7.5 file header in this file are released to the
00033    public domain, including the functional comments and any amusing asides.
00034 -----------------------------------------------------------------------------*/
00035 
00036 /*---------------------------------------------------------------------------*/
00037 /*! INTRODUCTION TO NIFTI-1:
00038    ------------------------
00039    The twin (and somewhat conflicting) goals of this modified ANALYZE 7.5
00040    format are:
00041     (a) To add information to the header that will be useful for functional
00042         neuroimaging data analysis and display.  These additions include:
00043         - More basic data types.
00044         - Two affine transformations to specify voxel coordinates.
00045         - "Intent" codes and parameters to describe the meaning of the data.
00046         - Affine scaling of the stored data values to their "true" values.
00047         - Optional storage of the header and image data in one file (.nii).
00048     (b) To maintain compatibility with non-NIFTI-aware ANALYZE 7.5 compatible
00049         software (i.e., such a program should be able to do something useful
00050         with a NIFTI-1 dataset -- at least, with one stored in a traditional
00051         .img/.hdr file pair).
00052 
00053    Most of the unused fields in the ANALYZE 7.5 header have been taken,
00054    and some of the lesser-used fields have been co-opted for other purposes.
00055    Notably, most of the data_history substructure has been co-opted for
00056    other purposes, since the ANALYZE 7.5 format describes this substructure
00057    as "not required".
00058 
00059    NIFTI-1 FLAG (MAGIC STRINGS):
00060    ----------------------------
00061    To flag such a struct as being conformant to the NIFTI-1 spec, the last 4
00062    bytes of the header must be either the C String "ni1" or "n+1";
00063    in hexadecimal, the 4 bytes
00064      6E 69 31 00   or   6E 2B 31 00
00065    (in any future version of this format, the '1' will be upgraded to '2',
00066    etc.).  Normally, such a "magic number" or flag goes at the start of the
00067    file, but trying to avoid clobbering widely-used ANALYZE 7.5 fields led to
00068    putting this marker last.  However, recall that "the last shall be first"
00069    (Matthew 20:16).
00070 
00071    If a NIFTI-aware program reads a header file that is NOT marked with a
00072    NIFTI magic string, then it should treat the header as an ANALYZE 7.5
00073    structure.
00074 
00075    NIFTI-1 FILE STORAGE:
00076    --------------------
00077    "ni1" means that the image data is stored in the ".img" file corresponding
00078    to the header file (starting at file offset 0).
00079 
00080    "n+1" means that the image data is stored in the same file as the header
00081    information.  We recommend that the combined header+data filename suffix
00082    be ".nii".  When the dataset is stored in one file, the first byte of image
00083    data is stored at byte location (int)vox_offset in this combined file.
00084    The minimum allowed value of vox_offset is 352; for compatibility with
00085    some software, vox_offset should be an integral multiple of 16.
00086 
00087    GRACE UNDER FIRE:
00088    ----------------
00089    Most NIFTI-aware programs will only be able to handle a subset of the full
00090    range of datasets possible with this format.  All NIFTI-aware programs
00091    should take care to check if an input dataset conforms to the program's
00092    needs and expectations (e.g., check datatype, intent_code, etc.).  If the
00093    input dataset can't be handled by the program, the program should fail
00094    gracefully (e.g., print a useful warning; not crash).
00095 
00096    SAMPLE CODES:
00097    ------------
00098    The associated files nifti1_io.h and nifti1_io.c provide a sample
00099    implementation in C of a set of functions to read, write, and manipulate
00100    NIFTI-1 files.  The file nifti1_test.c is a sample program that uses
00101    the nifti1_io.c functions.
00102 -----------------------------------------------------------------------------*/
00103 
00104 /*---------------------------------------------------------------------------*/
00105 /* HEADER STRUCT DECLARATION:
00106    -------------------------
00107    In the comments below for each field, only NIFTI-1 specific requirements
00108    or changes from the ANALYZE 7.5 format are described.  For convenience,
00109    the 348 byte header is described as a single struct, rather than as the
00110    ANALYZE 7.5 group of 3 substructs.
00111 
00112    Further comments about the interpretation of various elements of this
00113    header are after the data type definition itself.  Fields that are
00114    marked as ++UNUSED++ have no particular interpretation in this standard.
00115    (Also see the UNUSED FIELDS comment section, far below.)
00116 
00117    The presumption below is that the various C types have particular sizes:
00118      sizeof(int) = sizeof(float) = 4 ;  sizeof(short) = 2
00119 -----------------------------------------------------------------------------*/
00120 
00121 /*=================*/
00122 #ifdef  __cplusplus
00123 extern "C" {
00124 #endif
00125 /*=================*/
00126                         /*************************/  /************************/
00127 struct nifti_1_header { /* NIFTI-1 usage         */  /* ANALYZE 7.5 field(s) */
00128                         /*************************/  /************************/
00129 
00130                                            /*--- was header_key substruct ---*/
00131  int   sizeof_hdr;    /*!< MUST be 348           */  /* int sizeof_hdr;      */
00132  char  data_type[10]; /*!< ++UNUSED++            */  /* char data_type[10];  */
00133  char  db_name[18];   /*!< ++UNUSED++            */  /* char db_name[18];    */
00134  int   extents;       /*!< ++UNUSED++            */  /* int extents;         */
00135  short session_error; /*!< ++UNUSED++            */  /* short session_error; */
00136  char  regular;       /*!< ++UNUSED++            */  /* char regular;        */
00137  char  dim_info;      /*!< MRI slice ordering.   */  /* char hkey_un0;       */
00138 
00139                                       /*--- was image_dimension substruct ---*/
00140  short dim[8];        /*!< Data array dimensions.*/  /* short dim[8];        */
00141  float intent_p1 ;    /*!< 1st intent parameter. */  /* short unused8;       */
00142                                                      /* short unused9;       */
00143  float intent_p2 ;    /*!< 2nd intent parameter. */  /* short unused10;      */
00144                                                      /* short unused11;      */
00145  float intent_p3 ;    /*!< 3rd intent parameter. */  /* short unused12;      */
00146                                                      /* short unused13;      */
00147  short intent_code ;  /*!< NIFTI_INTENT_* code.  */  /* short unused14;      */
00148  short datatype;      /*!< Defines data type!    */  /* short datatype;      */
00149  short bitpix;        /*!< Number bits/voxel.    */  /* short bitpix;        */
00150  short slice_start;   /*!< First slice index.    */  /* short dim_un0;       */
00151  float pixdim[8];     /*!< Grid spacings.        */  /* float pixdim[8];     */
00152  float vox_offset;    /*!< Offset into .nii file */  /* float vox_offset;    */
00153  float scl_slope ;    /*!< Data scaling: slope.  */  /* float funused1;      */
00154  float scl_inter ;    /*!< Data scaling: offset. */  /* float funused2;      */
00155  short slice_end;     /*!< Last slice index.     */  /* float funused3;      */
00156  char  slice_code ;   /*!< Slice timing order.   */
00157  char  xyzt_units ;   /*!< Units of pixdim[1..4] */
00158  float cal_max;       /*!< Max display intensity */  /* float cal_max;       */
00159  float cal_min;       /*!< Min display intensity */  /* float cal_min;       */
00160  float slice_duration;/*!< Time for 1 slice.     */  /* float compressed;    */
00161  float toffset;       /*!< Time axis shift.      */  /* float verified;      */
00162  int   glmax;         /*!< ++UNUSED++            */  /* int glmax;           */
00163  int   glmin;         /*!< ++UNUSED++            */  /* int glmin;           */
00164 
00165                                          /*--- was data_history substruct ---*/
00166  char  descrip[80];   /*!< any text you like.    */  /* char descrip[80];    */
00167  char  aux_file[24];  /*!< auxiliary filename.   */  /* char aux_file[24];   */
00168 
00169  short qform_code ;   /*!< NIFTI_XFORM_* code.   */  /*-- all ANALYZE 7.5 ---*/
00170  short sform_code ;   /*!< NIFTI_XFORM_* code.   */  /*   fields below here  */
00171                                                      /*   are replaced       */
00172  float quatern_b ;    /*!< Quaternion b param.   */
00173  float quatern_c ;    /*!< Quaternion c param.   */
00174  float quatern_d ;    /*!< Quaternion d param.   */
00175  float qoffset_x ;    /*!< Quaternion x shift.   */
00176  float qoffset_y ;    /*!< Quaternion y shift.   */
00177  float qoffset_z ;    /*!< Quaternion z shift.   */
00178 
00179  float srow_x[4] ;    /*!< 1st row affine transform.   */
00180  float srow_y[4] ;    /*!< 2nd row affine transform.   */
00181  float srow_z[4] ;    /*!< 3rd row affine transform.   */
00182 
00183  char intent_name[16];/*!< 'name' or meaning of data.  */
00184 
00185  char magic[4] ;      /*!< MUST be "ni1\0" or "n+1\0". */
00186 
00187 } ;                   /**** 348 bytes total ****/
00188 
00189 typedef struct nifti_1_header nifti_1_header ;
00190 
00191 /*---------------------------------------------------------------------------*/
00192 /* HEADER EXTENSIONS:
00193    -----------------
00194    After the end of the 348 byte header (e.g., after the magic field),
00195    the next 4 bytes are a char array field named "extension". By default,
00196    all 4 bytes of this array should be set to zero. In a .nii file, these
00197    4 bytes will always be present, since the earliest start point for
00198    the image data is byte #352. In a separate .hdr file, these bytes may
00199    or may not be present. If not present (i.e., if the length of the .hdr
00200    file is 348 bytes), then a NIfTI-1 compliant program should use the
00201    default value of extension={0,0,0,0}. The first byte (extension[0])
00202    is the only value of this array that is specified at present. The other
00203    3 bytes are reserved for future use.
00204 
00205    If extension[0] is nonzero, it indicates that extended header information
00206    is present in the bytes following the extension array. In a .nii file,
00207    this extended header data is before the image data (and vox_offset
00208    must be set correctly to allow for this). In a .hdr file, this extended
00209    data follows extension and proceeds (potentially) to the end of the file.
00210 
00211    The format of extended header data is weakly specified. Each extension
00212    must be an integer multiple of 16 bytes long. The first 8 bytes of each
00213    extension comprise 2 integers:
00214       int esize , ecode ;
00215    These values may need to be byte-swapped, as indicated by dim[0] for
00216    the rest of the header.
00217      * esize is the number of bytes that form the extended header data
00218        + esize must be a positive integral multiple of 16
00219        + this length includes the 8 bytes of esize and ecode themselves
00220      * ecode is a non-negative integer that indicates the format of the
00221        extended header data that follows
00222        + different ecode values are assigned to different developer groups
00223        + at present, the "registered" values for code are
00224          = 0 = unknown private format (not recommended!)
00225          = 2 = DICOM format (i.e., attribute tags and values)
00226          = 4 = AFNI group (i.e., ASCII XML-ish elements)
00227    In the interests of interoperability (a primary rationale for NIfTI),
00228    groups developing software that uses this extension mechanism are
00229    encouraged to document and publicize the format of their extensions.
00230    To this end, the NIfTI DFWG will assign even numbered codes upon request
00231    to groups submitting at least rudimentary documentation for the format
00232    of their extension; at present, the contact is mailto:rwcox@nih.gov.
00233    The assigned codes and documentation will be posted on the NIfTI
00234    website. All odd values of ecode (and 0) will remain unassigned;
00235    at least, until the even ones are used up, when we get to 2,147,483,646.
00236 
00237    Note that the other contents of the extended header data section are
00238    totally unspecified by the NIfTI-1 standard. In particular, if binary
00239    data is stored in such a section, its byte order is not necessarily
00240    the same as that given by examining dim[0]; it is incumbent on the
00241    programs dealing with such data to determine the byte order of binary
00242    extended header data.
00243 
00244    Multiple extended header sections are allowed, each starting with an
00245    esize,ecode value pair. The first esize value, as described above,
00246    is at bytes #352-355 in the .hdr or .nii file (files start at byte #0).
00247    If this value is positive, then the second (esize2) will be found
00248    starting at byte #352+esize1 , the third (esize3) at byte #352+esize1+esize2,
00249    et cetera.  Of course, in a .nii file, the value of vox_offset must
00250    be compatible with these extensions. If a malformed file indicates
00251    that an extended header data section would run past vox_offset, then
00252    the entire extended header section should be ignored. In a .hdr file,
00253    if an extended header data section would run past the end-of-file,
00254    that extended header data should also be ignored.
00255 
00256    With the above scheme, a program can successively examine the esize
00257    and ecode values, and skip over each extended header section if the
00258    program doesn't know how to interpret the data within. Of course, any
00259    program can simply ignore all extended header sections simply by jumping
00260    straight to the image data using vox_offset.
00261 -----------------------------------------------------------------------------*/
00262    
00263 struct nifti1_extender { char extension[4] ; } ;
00264 typedef struct nifti1_extender nifti1_extender ;
00265 
00266 struct nifti1_extension {
00267    int esize , ecode ;
00268    char *edata ;
00269 } ;
00270 typedef struct nifti1_extension nifti1_extension ;
00271 
00272 /*---------------------------------------------------------------------------*/
00273 /* DATA DIMENSIONALITY (as in ANALYZE 7.5):
00274    ---------------------------------------
00275      dim[0] = number of dimensions;
00276               - if dim[0] is outside range 1..7, then the header information
00277                 needs to be byte swapped appropriately
00278               - ANALYZE supports dim[0] up to 7, but NIFTI-1 reserves
00279                 dimensions 1,2,3 for space (x,y,z), 4 for time (t), and
00280                 5,6,7 for anything else needed.
00281 
00282      dim[i] = length of dimension #i, for i=1..dim[0]  (must be positive)
00283               - also see the discussion of intent_code, far below
00284 
00285      pixdim[i] = voxel width along dimension #i, i=1..dim[0] (positive)
00286                  - cf. ORIENTATION section below for use of pixdim[0]
00287                  - the units of pixdim can be specified with the xyzt_units
00288                    field (also described far below).
00289 
00290    Number of bits per voxel value is in bitpix, which MUST correspond with
00291    the datatype field.  The total number of bytes in the image data is
00292      dim[1] * ... * dim[dim[0]] * bitpix / 8
00293 
00294    In NIFTI-1 files, dimensions 1,2,3 are for space, dimension 4 is for time,
00295    and dimension 5 is for storing multiple values at each spatiotemporal
00296    voxel.  Some examples:
00297      - A typical whole-brain FMRI experiment's time series:
00298         - dim[0] = 4
00299         - dim[1] = 64   pixdim[1] = 3.75 xyzt_units =  NIFTI_UNITS_MM
00300         - dim[2] = 64   pixdim[2] = 3.75             | NIFTI_UNITS_SEC
00301         - dim[3] = 20   pixdim[3] = 5.0
00302         - dim[4] = 120  pixdim[4] = 2.0
00303      - A typical T1-weighted anatomical volume:
00304         - dim[0] = 3
00305         - dim[1] = 256  pixdim[1] = 1.0  xyzt_units = NIFTI_UNITS_MM
00306         - dim[2] = 256  pixdim[2] = 1.0
00307         - dim[3] = 128  pixdim[3] = 1.1
00308      - A single slice EPI time series:
00309         - dim[0] = 4
00310         - dim[1] = 64   pixdim[1] = 3.75 xyzt_units =  NIFTI_UNITS_MM
00311         - dim[2] = 64   pixdim[2] = 3.75             | NIFTI_UNITS_SEC
00312         - dim[3] = 1    pixdim[3] = 5.0
00313         - dim[4] = 1200 pixdim[4] = 0.2
00314      - A 3-vector stored at each point in a 3D volume:
00315         - dim[0] = 5
00316         - dim[1] = 256  pixdim[1] = 1.0  xyzt_units = NIFTI_UNITS_MM
00317         - dim[2] = 256  pixdim[2] = 1.0
00318         - dim[3] = 128  pixdim[3] = 1.1
00319         - dim[4] = 1    pixdim[4] = 0.0
00320         - dim[5] = 3                     intent_code = NIFTI_INTENT_VECTOR
00321      - A single time series with a 3x3 matrix at each point:
00322         - dim[0] = 5
00323         - dim[1] = 1                     xyzt_units = NIFTI_UNITS_SEC
00324         - dim[2] = 1
00325         - dim[3] = 1
00326         - dim[4] = 1200 pixdim[4] = 0.2
00327         - dim[5] = 9                     intent_code = NIFTI_INTENT_GENMATRIX
00328         - intent_p1 = intent_p2 = 3.0    (indicates matrix dimensions)
00329 -----------------------------------------------------------------------------*/
00330 
00331 /*---------------------------------------------------------------------------*/
00332 /* DATA STORAGE:
00333    ------------
00334    If the magic field is "n+1", then the voxel data is stored in the
00335    same file as the header.  In this case, the voxel data starts at offset
00336    (int)vox_offset into the header file.  Thus, vox_offset=352.0 means that
00337    the data starts immediately after the NIFTI-1 header.  If vox_offset is
00338    greater than 352, the NIFTI-1 format does not say much about the
00339    contents of the dataset file between the end of the header and the
00340    start of the data.
00341 
00342    FILES:
00343    -----
00344    If the magic field is "ni1", then the voxel data is stored in the
00345    associated ".img" file, starting at offset 0 (i.e., vox_offset is not
00346    used in this case, and should be set to 0.0).
00347 
00348    When storing NIFTI-1 datasets in pairs of files, it is customary to name
00349    the files in the pattern "name.hdr" and "name.img", as in ANALYZE 7.5.
00350    When storing in a single file ("n+1"), the file name should be in
00351    the form "name.nii" (the ".nft" and ".nif" suffixes are already taken;
00352    cf. http://www.icdatamaster.com/n.html ).
00353 
00354    BYTE ORDERING:
00355    -------------
00356    The byte order of the data arrays is presumed to be the same as the byte
00357    order of the header (which is determined by examining dim[0]).
00358 
00359    Floating point types are presumed to be stored in IEEE-754 format.
00360 -----------------------------------------------------------------------------*/
00361 
00362 /*---------------------------------------------------------------------------*/
00363 /* DETAILS ABOUT vox_offset:
00364    ------------------------
00365    In a .nii file, the vox_offset field value is interpreted as the start
00366    location of the image data bytes in that file. In a .hdr/.img file pair,
00367    the vox_offset field value is the start location of the image data
00368    bytes in the .img file.
00369     * If vox_offset is less than 352 in a .nii file, it is equivalent
00370       to 352 (i.e., image data never starts before byte #352 in a .nii file).
00371     * The default value for vox_offset in a .nii file is 352.
00372     * In a .hdr file, the default value for vox_offset is 0.
00373     * vox_offset should be an integer multiple of 16; otherwise, some
00374       programs may not work properly (e.g., SPM). This is to allow
00375       memory-mapped input to be properly byte-aligned.
00376    Note that since vox_offset is an IEEE-754 32 bit float (for compatibility
00377    with the ANALYZE-7.5 format), it effectively has a 24 bit mantissa. All
00378    integers from 0 to 2^24 can be represented exactly in this format, but not
00379    all larger integers are exactly storable as IEEE-754 32 bit floats. However,
00380    unless you plan to have vox_offset be potentially larger than 16 MB, this
00381    should not be an issue. (Actually, any integral multiple of 16 up to 2^27
00382    can be represented exactly in this format, which allows for up to 128 MB
00383    of random information before the image data.  If that isn't enough, then
00384    perhaps this format isn't right for you.)
00385 
00386    In a .img file (i.e., image data stored separately from the NIfTI-1
00387    header), data bytes between #0 and #vox_offset-1 (inclusive) are completely
00388    undefined and unregulated by the NIfTI-1 standard. One potential use of
00389    having vox_offset > 0 in the .hdr/.img file pair storage method is to make
00390    the .img file be a copy of (or link to) a pre-existing image file in some
00391    other format, such as DICOM; then vox_offset would be set to the offset of
00392    the image data in this file. (It may not be possible to follow the
00393    "multiple-of-16 rule" with an arbitrary external file; using the NIfTI-1
00394    format in such a case may lead to a file that is incompatible with software
00395    that relies on vox_offset being a multiple of 16.)
00396 
00397    In a .nii file, data bytes between #348 and #vox_offset-1 (inclusive) may
00398    be used to store user-defined extra information; similarly, in a .hdr file,
00399    any data bytes after byte #347 are available for user-defined extra
00400    information. The (very weak) regulation of this extra header data is
00401    described elsewhere.
00402 -----------------------------------------------------------------------------*/
00403 
00404 /*---------------------------------------------------------------------------*/
00405 /* DATA SCALING:
00406    ------------
00407    If the scl_slope field is nonzero, then each voxel value in the dataset
00408    should be scaled as
00409       y = scl_slope * x + scl_inter
00410    where x = voxel value stored
00411          y = "true" voxel value
00412    Normally, we would expect this scaling to be used to store "true" floating
00413    values in a smaller integer datatype, but that is not required.  That is,
00414    it is legal to use scaling even if the datatype is a float type (crazy,
00415    perhaps, but legal).
00416     - However, the scaling is to be ignored if datatype is DT_RGB24.
00417     - If datatype is a complex type, then the scaling is to be
00418       applied to both the real and imaginary parts.
00419 
00420    The cal_min and cal_max fields (if nonzero) are used for mapping (possibly
00421    scaled) dataset values to display colors:
00422     - Minimum display intensity (black) corresponds to dataset value cal_min.
00423     - Maximum display intensity (white) corresponds to dataset value cal_max.
00424     - Dataset values below cal_min should display as black also, and values
00425       above cal_max as white.
00426     - Colors "black" and "white", of course, may refer to any scalar display
00427       scheme (e.g., a color lookup table specified via aux_file).
00428     - cal_min and cal_max only make sense when applied to scalar-valued
00429       datasets (i.e., dim[0] < 5 or dim[5] = 1).
00430 -----------------------------------------------------------------------------*/
00431 
00432 /*---------------------------------------------------------------------------*/
00433 /* TYPE OF DATA (acceptable values for datatype field):
00434    ---------------------------------------------------
00435    Values of datatype smaller than 256 are ANALYZE 7.5 compatible.
00436    Larger values are NIFTI-1 additions.  These are all multiples of 256, so
00437    that no bits below position 8 are set in datatype.  But there is no need
00438    to use only powers-of-2, as the original ANALYZE 7.5 datatype codes do.
00439 
00440    The additional codes are intended to include a complete list of basic
00441    scalar types, including signed and unsigned integers from 8 to 64 bits,
00442    floats from 32 to 128 bits, and complex (float pairs) from 64 to 256 bits.
00443 
00444    Note that most programs will support only a few of these datatypes!
00445    A NIFTI-1 program should fail gracefully (e.g., print a warning message)
00446    when it encounters a dataset with a type it doesn't like.
00447 -----------------------------------------------------------------------------*/
00448 
00449 #undef DT_UNKNOWN  /* defined in dirent.h on some Unix systems */
00450 
00451                             /*--- the original ANALYZE 7.5 type codes ---*/
00452 #define DT_NONE                    0
00453 #define DT_UNKNOWN                 0     /* what it says, dude           */
00454 #define DT_BINARY                  1     /* binary (1 bit/voxel)         */
00455 #define DT_UNSIGNED_CHAR           2     /* unsigned char (8 bits/voxel) */
00456 #define DT_SIGNED_SHORT            4     /* signed short (16 bits/voxel) */
00457 #define DT_SIGNED_INT              8     /* signed int (32 bits/voxel)   */
00458 #define DT_FLOAT                  16     /* float (32 bits/voxel)        */
00459 #define DT_COMPLEX                32     /* complex (64 bits/voxel)      */
00460 #define DT_DOUBLE                 64     /* double (64 bits/voxel)       */
00461 #define DT_RGB                   128     /* RGB triple (24 bits/voxel)   */
00462 #define DT_ALL                   255     /* not very useful (?)          */
00463 
00464                             /*----- another set of names for the same ---*/
00465 #define DT_UINT8                   2
00466 #define DT_INT16                   4
00467 #define DT_INT32                   8
00468 #define DT_FLOAT32                16
00469 #define DT_COMPLEX64              32
00470 #define DT_FLOAT64                64
00471 #define DT_RGB24                 128
00472 
00473                             /*------------------- new codes for NIFTI ---*/
00474 #define DT_INT8                  256     /* signed char (8 bits)         */
00475 #define DT_UINT16                512     /* unsigned short (16 bits)     */
00476 #define DT_UINT32                768     /* unsigned int (32 bits)       */
00477 #define DT_INT64                1024     /* long long (64 bits)          */
00478 #define DT_UINT64               1280     /* unsigned long long (64 bits) */
00479 #define DT_FLOAT128             1536     /* long double (128 bits)       */
00480 #define DT_COMPLEX128           1792     /* double pair (128 bits)       */
00481 #define DT_COMPLEX256           2048     /* long double pair (256 bits)  */
00482 
00483                             /*------- aliases for all the above codes ---*/
00484 
00485                                        /*! unsigned char. */
00486 #define NIFTI_TYPE_UINT8           2
00487                                        /*! signed short. */
00488 #define NIFTI_TYPE_INT16           4
00489                                        /*! signed int. */
00490 #define NIFTI_TYPE_INT32           8
00491                                        /*! 32 bit float. */
00492 #define NIFTI_TYPE_FLOAT32        16
00493                                        /*! 64 bit complex = 2 32 bit floats. */
00494 #define NIFTI_TYPE_COMPLEX64      32
00495                                        /*! 64 bit float = double. */
00496 #define NIFTI_TYPE_FLOAT64        64
00497                                        /*! 3 8 bit bytes. */
00498 #define NIFTI_TYPE_RGB24         128
00499                                        /*! signed char. */
00500 #define NIFTI_TYPE_INT8          256
00501                                        /*! unsigned short. */
00502 #define NIFTI_TYPE_UINT16        512
00503                                        /*! unsigned int. */
00504 #define NIFTI_TYPE_UINT32        768
00505                                        /*! signed long long. */
00506 #define NIFTI_TYPE_INT64        1024
00507                                        /*! unsigned long long. */
00508 #define NIFTI_TYPE_UINT64       1280
00509                                        /*! 128 bit float = long double. */
00510 #define NIFTI_TYPE_FLOAT128     1536
00511                                        /*! 128 bit complex = 2 64 bit floats. */
00512 #define NIFTI_TYPE_COMPLEX128   1792
00513                                        /*! 256 bit complex = 2 128 bit floats */
00514 #define NIFTI_TYPE_COMPLEX256   2048
00515 
00516                      /*-------- sample typedefs for complicated types ---*/
00517 #if 0
00518 typedef struct { float       r,i;     } complex_float ;
00519 typedef struct { double      r,i;     } complex_double ;
00520 typedef struct { long double r,i;     } complex_longdouble ;
00521 typedef struct { unsigned char r,g,b; } rgb_byte ;
00522 #endif
00523 
00524 /*---------------------------------------------------------------------------*/
00525 /* INTERPRETATION OF VOXEL DATA:
00526    ----------------------------
00527    The intent_code field can be used to indicate that the voxel data has
00528    some particular meaning.  In particular, a large number of codes is
00529    given to indicate that the the voxel data should be interpreted as
00530    being drawn from a given probability distribution.
00531 
00532    VECTOR-VALUED DATASETS:
00533    ----------------------
00534    The 5th dimension of the dataset, if present (i.e., dim[0]=5 and
00535    dim[5] > 1), contains multiple values (e.g., a vector) to be stored
00536    at each spatiotemporal location.  For example, the header values
00537     - dim[0] = 5
00538     - dim[1] = 64
00539     - dim[2] = 64
00540     - dim[3] = 20
00541     - dim[4] = 1     (indicates no time axis)
00542     - dim[5] = 3
00543     - datatype = DT_FLOAT
00544     - intent_code = NIFTI_INTENT_VECTOR
00545    mean that this dataset should be interpreted as a 3D volume (64x64x20),
00546    with a 3-vector of floats defined at each point in the 3D grid.
00547 
00548    A program reading a dataset with a 5th dimension may want to reformat
00549    the image data to store each voxels' set of values together in a struct
00550    or array.  This programming detail, however, is beyond the scope of the
00551    NIFTI-1 file specification!  Uses of dimensions 6 and 7 are also not
00552    specified here.
00553 
00554    STATISTICAL PARAMETRIC DATASETS (i.e., SPMs):
00555    --------------------------------------------
00556    Values of intent_code from NIFTI_FIRST_STATCODE to NIFTI_LAST_STATCODE
00557    (inclusive) indicate that the numbers in the dataset should be interpreted
00558    as being drawn from a given distribution.  Most such distributions have
00559    auxiliary parameters (e.g., NIFTI_INTENT_TTEST has 1 DOF parameter).
00560 
00561    If the dataset DOES NOT have a 5th dimension, then the auxiliary parameters
00562    are the same for each voxel, and are given in header fields intent_p1,
00563    intent_p2, and intent_p3.
00564 
00565    If the dataset DOES have a 5th dimension, then the auxiliary parameters
00566    are different for each voxel.  For example, the header values
00567     - dim[0] = 5
00568     - dim[1] = 128
00569     - dim[2] = 128
00570     - dim[3] = 1      (indicates a single slice)
00571     - dim[4] = 1      (indicates no time axis)
00572     - dim[5] = 2
00573     - datatype = DT_FLOAT
00574     - intent_code = NIFTI_INTENT_TTEST
00575    mean that this is a 2D dataset (128x128) of t-statistics, with the
00576    t-statistic being in the first "plane" of data and the degrees-of-freedom
00577    parameter being in the second "plane" of data.
00578 
00579    If the dataset 5th dimension is used to store the voxel-wise statistical
00580    parameters, then dim[5] must be 1 plus the number of parameters required
00581    by that distribution (e.g., intent_code=NIFTI_INTENT_TTEST implies dim[5]
00582    must be 2, as in the example just above).
00583 
00584    Note: intent_code values 2..10 are compatible with AFNI 1.5x (which is
00585    why there is no code with value=1, which is obsolescent in AFNI).
00586 
00587    OTHER INTENTIONS:
00588    ----------------
00589    The purpose of the intent_* fields is to help interpret the values
00590    stored in the dataset.  Some non-statistical values for intent_code
00591    and conventions are provided for storing other complex data types.
00592 
00593    The intent_name field provides space for a 15 character (plus 0 byte)
00594    'name' string for the type of data stored. Examples:
00595     - intent_code = NIFTI_INTENT_ESTIMATE; intent_name = "T1";
00596        could be used to signify that the voxel values are estimates of the
00597        NMR parameter T1.
00598     - intent_code = NIFTI_INTENT_TTEST; intent_name = "House";
00599        could be used to signify that the voxel values are t-statistics
00600        for the significance of 'activation' response to a House stimulus.
00601     - intent_code = NIFTI_INTENT_DISPVECT; intent_name = "ToMNI152";
00602        could be used to signify that the voxel values are a displacement
00603        vector that transforms each voxel (x,y,z) location to the
00604        corresponding location in the MNI152 standard brain.
00605     - intent_code = NIFTI_INTENT_SYMMATRIX; intent_name = "DTI";
00606        could be used to signify that the voxel values comprise a diffusion
00607        tensor image.
00608 
00609    If no data name is implied or needed, intent_name[0] should be set to 0.
00610 -----------------------------------------------------------------------------*/
00611 
00612  /*! default: no intention is indicated in the header. */
00613 
00614 #define NIFTI_INTENT_NONE        0
00615 
00616     /*-------- These codes are for probability distributions ---------------*/
00617     /* Most distributions have a number of parameters,
00618        below denoted by p1, p2, and p3, and stored in
00619         - intent_p1, intent_p2, intent_p3 if dataset doesn't have 5th dimension
00620         - image data array                if dataset does have 5th dimension
00621 
00622        Functions to compute with many of the distributions below can be found
00623        in the CDF library from U Texas.
00624 
00625        Formulas for and discussions of these distributions can be found in the
00626        following books:
00627 
00628         [U] Univariate Discrete Distributions,
00629             NL Johnson, S Kotz, AW Kemp.
00630 
00631         [C1] Continuous Univariate Distributions, vol. 1,
00632              NL Johnson, S Kotz, N Balakrishnan.
00633 
00634         [C2] Continuous Univariate Distributions, vol. 2,
00635              NL Johnson, S Kotz, N Balakrishnan.                            */
00636     /*----------------------------------------------------------------------*/
00637 
00638   /*! [C2, chap 32] Correlation coefficient R (1 param):
00639        p1 = degrees of freedom
00640        R/sqrt(1-R*R) is t-distributed with p1 DOF. */
00641 
00642 #define NIFTI_INTENT_CORREL      2
00643 
00644   /*! [C2, chap 28] Student t statistic (1 param): p1 = DOF. */
00645 
00646 #define NIFTI_INTENT_TTEST       3
00647 
00648   /*! [C2, chap 27] Fisher F statistic (2 params):
00649        p1 = numerator DOF, p2 = denominator DOF. */
00650 
00651 #define NIFTI_INTENT_FTEST       4
00652 
00653   /*! [C1, chap 13] Standard normal (0 params): Density = N(0,1). */
00654 
00655 #define NIFTI_INTENT_ZSCORE      5
00656 
00657   /*! [C1, chap 18] Chi-squared (1 param): p1 = DOF.
00658       Density(x) proportional to exp(-x/2) * x^(p1/2-1). */
00659 
00660 #define NIFTI_INTENT_CHISQ       6
00661 
00662   /*! [C2, chap 25] Beta distribution (2 params): p1=a, p2=b.
00663       Density(x) proportional to x^(a-1) * (1-x)^(b-1). */
00664 
00665 #define NIFTI_INTENT_BETA        7
00666 
00667   /*! [U, chap 3] Binomial distribution (2 params):
00668        p1 = number of trials, p2 = probability per trial.
00669       Prob(x) = (p1 choose x) * p2^x * (1-p2)^(p1-x), for x=0,1,...,p1. */
00670 
00671 #define NIFTI_INTENT_BINOM       8
00672 
00673   /*! [C1, chap 17] Gamma distribution (2 params):
00674        p1 = shape, p2 = scale.
00675       Density(x) proportional to x^(p1-1) * exp(-p2*x). */
00676 
00677 #define NIFTI_INTENT_GAMMA       9
00678 
00679   /*! [U, chap 4] Poisson distribution (1 param): p1 = mean.
00680       Prob(x) = exp(-p1) * p1^x / x! , for x=0,1,2,.... */
00681 
00682 #define NIFTI_INTENT_POISSON    10
00683 
00684   /*! [C1, chap 13] Normal distribution (2 params):
00685        p1 = mean, p2 = standard deviation. */
00686 
00687 #define NIFTI_INTENT_NORMAL     11
00688 
00689   /*! [C2, chap 30] Noncentral F statistic (3 params):
00690        p1 = numerator DOF, p2 = denominator DOF,
00691        p3 = numerator noncentrality parameter.  */
00692 
00693 #define NIFTI_INTENT_FTEST_NONC 12
00694 
00695   /*! [C2, chap 29] Noncentral chi-squared statistic (2 params):
00696        p1 = DOF, p2 = noncentrality parameter.     */
00697 
00698 #define NIFTI_INTENT_CHISQ_NONC 13
00699 
00700   /*! [C2, chap 23] Logistic distribution (2 params):
00701        p1 = location, p2 = scale.
00702       Density(x) proportional to sech^2((x-p1)/(2*p2)). */
00703 
00704 #define NIFTI_INTENT_LOGISTIC   14
00705 
00706   /*! [C2, chap 24] Laplace distribution (2 params):
00707        p1 = location, p2 = scale.
00708       Density(x) proportional to exp(-abs(x-p1)/p2). */
00709 
00710 #define NIFTI_INTENT_LAPLACE    15
00711 
00712   /*! [C2, chap 26] Uniform distribution: p1 = lower end, p2 = upper end. */
00713 
00714 #define NIFTI_INTENT_UNIFORM    16
00715 
00716   /*! [C2, chap 31] Noncentral t statistic (2 params):
00717        p1 = DOF, p2 = noncentrality parameter. */
00718 
00719 #define NIFTI_INTENT_TTEST_NONC 17
00720 
00721   /*! [C1, chap 21] Weibull distribution (3 params):
00722        p1 = location, p2 = scale, p3 = power.
00723       Density(x) proportional to
00724        ((x-p1)/p2)^(p3-1) * exp(-((x-p1)/p2)^p3) for x > p1. */
00725 
00726 #define NIFTI_INTENT_WEIBULL    18
00727 
00728   /*! [C1, chap 18] Chi distribution (1 param): p1 = DOF.
00729       Density(x) proportional to x^(p1-1) * exp(-x^2/2) for x > 0.
00730        p1 = 1 = 'half normal' distribution
00731        p1 = 2 = Rayleigh distribution
00732        p1 = 3 = Maxwell-Boltzmann distribution.                  */
00733 
00734 #define NIFTI_INTENT_CHI        19
00735 
00736   /*! [C1, chap 15] Inverse Gaussian (2 params):
00737        p1 = mu, p2 = lambda
00738       Density(x) proportional to
00739        exp(-p2*(x-p1)^2/(2*p1^2*x)) / x^3  for x > 0. */
00740 
00741 #define NIFTI_INTENT_INVGAUSS   20
00742 
00743   /*! [C2, chap 22] Extreme value type I (2 params):
00744        p1 = location, p2 = scale
00745       cdf(x) = exp(-exp(-(x-p1)/p2)). */
00746 
00747 #define NIFTI_INTENT_EXTVAL     21
00748 
00749   /*! Data is a 'p-value' (no params). */
00750 
00751 #define NIFTI_INTENT_PVAL       22
00752 
00753   /*! Data is ln(p-value) (no params). */
00754 
00755 #define NIFTI_INTENT_LOGPVAL    23
00756 
00757   /*! Data is log10(p-value) (no params). */
00758 
00759 #define NIFTI_INTENT_LOG10PVAL  24
00760 
00761   /*! Smallest intent_code that indicates a statistic. */
00762 
00763 #define NIFTI_FIRST_STATCODE     2
00764 
00765   /*! Largest intent_code that indicates a statistic. */
00766 
00767 #define NIFTI_LAST_STATCODE     24
00768 
00769  /*---------- these values for intent_code aren't for statistics ----------*/
00770 
00771  /*! To signify that the value at each voxel is an estimate
00772      of some parameter, set intent_code = NIFTI_INTENT_ESTIMATE.
00773      The name of the parameter may be stored in intent_name.     */
00774 
00775 #define NIFTI_INTENT_ESTIMATE  1001
00776 
00777  /*! To signify that the value at each voxel is an index into
00778      some set of labels, set intent_code = NIFTI_INTENT_LABEL.
00779      The filename with the labels may stored in aux_file.        */
00780 
00781 #define NIFTI_INTENT_LABEL     1002
00782 
00783  /*! To signify that the value at each voxel is an index into the
00784      NeuroNames labels set, set intent_code = NIFTI_INTENT_NEURONAME. */
00785 
00786 #define NIFTI_INTENT_NEURONAME 1003
00787 
00788  /*! To store an M x N matrix at each voxel:
00789        - dataset must have a 5th dimension (dim[0]=5 and dim[5]>1)
00790        - intent_code must be NIFTI_INTENT_GENMATRIX
00791        - dim[5] must be M*N
00792        - intent_p1 must be M (in float format)
00793        - intent_p2 must be N (ditto)
00794        - the matrix values A[i][[j] are stored in row-order:
00795          - A[0][0] A[0][1] ... A[0][N-1]
00796          - A[1][0] A[1][1] ... A[1][N-1]
00797          - etc., until
00798          - A[M-1][0] A[M-1][1] ... A[M-1][N-1]        */
00799 
00800 #define NIFTI_INTENT_GENMATRIX 1004
00801 
00802  /*! To store an NxN symmetric matrix at each voxel:
00803        - dataset must have a 5th dimension
00804        - intent_code must be NIFTI_INTENT_SYMMATRIX
00805        - dim[5] must be N*(N+1)/2
00806        - intent_p1 must be N (in float format)
00807        - the matrix values A[i][[j] are stored in row-order:
00808          - A[0][0]
00809          - A[1][0] A[1][1]
00810          - A[2][0] A[2][1] A[2][2]
00811          - etc.: row-by-row                           */
00812 
00813 #define NIFTI_INTENT_SYMMATRIX 1005
00814 
00815  /*! To signify that the vector value at each voxel is to be taken
00816      as a displacement field or vector:
00817        - dataset must have a 5th dimension
00818        - intent_code must be NIFTI_INTENT_DISPVECT
00819        - dim[5] must be the dimensionality of the displacment
00820          vector (e.g., 3 for spatial displacement, 2 for in-plane) */
00821 
00822 #define NIFTI_INTENT_DISPVECT  1006   /* specifically for displacements */
00823 #define NIFTI_INTENT_VECTOR    1007   /* for any other type of vector */
00824 
00825  /*! To signify that the vector value at each voxel is really a
00826      spatial coordinate (e.g., the vertices or nodes of a surface mesh):
00827        - dataset must have a 5th dimension
00828        - intent_code must be NIFTI_INTENT_POINTSET
00829        - dim[0] = 5
00830        - dim[1] = number of points
00831        - dim[2] = dim[3] = dim[4] = 1
00832        - dim[5] must be the dimensionality of space (e.g., 3 => 3D space).
00833        - intent_name may describe the object these points come from
00834          (e.g., "pial", "gray/white" , "EEG", "MEG").                   */
00835 
00836 #define NIFTI_INTENT_POINTSET  1008
00837 
00838  /*! To signify that the vector value at each voxel is really a triple
00839      of indexes (e.g., forming a triangle) from a pointset dataset:
00840        - dataset must have a 5th dimension
00841        - intent_code must be NIFTI_INTENT_TRIANGLE
00842        - dim[0] = 5
00843        - dim[1] = number of triangles
00844        - dim[2] = dim[3] = dim[4] = 1
00845        - dim[5] = 3
00846        - datatype should be an integer type (preferably DT_INT32)
00847        - the data values are indexes (0,1,...) into a pointset dataset. */
00848 
00849 #define NIFTI_INTENT_TRIANGLE  1009
00850 
00851  /*! To signify that the vector value at each voxel is a quaternion:
00852        - dataset must have a 5th dimension
00853        - intent_code must be NIFTI_INTENT_QUATERNION
00854        - dim[0] = 5
00855        - dim[5] = 4
00856        - datatype should be a floating point type     */
00857 
00858 #define NIFTI_INTENT_QUATERNION 1010
00859 
00860  /*! Dimensionless value - no params - although, as in _ESTIMATE 
00861      the name of the parameter may be stored in intent_name.     */
00862 
00863 #define NIFTI_INTENT_DIMLESS    1011
00864 
00865 /*---------------------------------------------------------------------------*/
00866 /* 3D IMAGE (VOLUME) ORIENTATION AND LOCATION IN SPACE:
00867    ---------------------------------------------------
00868    There are 3 different methods by which continuous coordinates can
00869    attached to voxels.  The discussion below emphasizes 3D volumes, and
00870    the continuous coordinates are referred to as (x,y,z).  The voxel
00871    index coordinates (i.e., the array indexes) are referred to as (i,j,k),
00872    with valid ranges:
00873      i = 0 .. dim[1]-1
00874      j = 0 .. dim[2]-1  (if dim[0] >= 2)
00875      k = 0 .. dim[3]-1  (if dim[0] >= 3)
00876    The (x,y,z) coordinates refer to the CENTER of a voxel.  In methods
00877    2 and 3, the (x,y,z) axes refer to a subject-based coordinate system,
00878    with
00879      +x = Right  +y = Anterior  +z = Superior.
00880    This is a right-handed coordinate system.  However, the exact direction
00881    these axes point with respect to the subject depends on qform_code
00882    (Method 2) and sform_code (Method 3).
00883 
00884    N.B.: The i index varies most rapidly, j index next, k index slowest.
00885     Thus, voxel (i,j,k) is stored starting at location
00886       (i + j*dim[1] + k*dim[1]*dim[2]) * (bitpix/8)
00887     into the dataset array.
00888 
00889    N.B.: The ANALYZE 7.5 coordinate system is
00890       +x = Left  +y = Anterior  +z = Superior
00891     which is a left-handed coordinate system.  This backwardness is
00892     too difficult to tolerate, so this NIFTI-1 standard specifies the
00893     coordinate order which is most common in functional neuroimaging.
00894 
00895    N.B.: The 3 methods below all give the locations of the voxel centers
00896     in the (x,y,z) coordinate system.  In many cases, programs will wish
00897     to display image data on some other grid.  In such a case, the program
00898     will need to convert its desired (x,y,z) values into (i,j,k) values
00899     in order to extract (or interpolate) the image data.  This operation
00900     would be done with the inverse transformation to those described below.
00901 
00902    N.B.: Method 2 uses a factor 'qfac' which is either -1 or 1; qfac is
00903     stored in the otherwise unused pixdim[0].  If pixdim[0]=0.0 (which
00904     should not occur), we take qfac=1.  Of course, pixdim[0] is only used
00905     when reading a NIFTI-1 header, not when reading an ANALYZE 7.5 header.
00906 
00907    N.B.: The units of (x,y,z) can be specified using the xyzt_units field.
00908 
00909    METHOD 1 (the "old" way, used only when qform_code = 0):
00910    -------------------------------------------------------
00911    The coordinate mapping from (i,j,k) to (x,y,z) is the ANALYZE
00912    7.5 way.  This is a simple scaling relationship:
00913 
00914      x = pixdim[1] * i
00915      y = pixdim[2] * j
00916      z = pixdim[3] * k
00917 
00918    No particular spatial orientation is attached to these (x,y,z)
00919    coordinates.  (NIFTI-1 does not have the ANALYZE 7.5 orient field,
00920    which is not general and is often not set properly.)  This method
00921    is not recommended, and is present mainly for compatibility with
00922    ANALYZE 7.5 files.
00923 
00924    METHOD 2 (used when qform_code > 0, which should be the "normal" case):
00925    ---------------------------------------------------------------------
00926    The (x,y,z) coordinates are given by the pixdim[] scales, a rotation
00927    matrix, and a shift.  This method is intended to represent
00928    "scanner-anatomical" coordinates, which are often embedded in the
00929    image header (e.g., DICOM fields (0020,0032), (0020,0037), (0028,0030),
00930    and (0018,0050)), and represent the nominal orientation and location of
00931    the data.  This method can also be used to represent "aligned"
00932    coordinates, which would typically result from some post-acquisition
00933    alignment of the volume to a standard orientation (e.g., the same
00934    subject on another day, or a rigid rotation to true anatomical
00935    orientation from the tilted position of the subject in the scanner).
00936    The formula for (x,y,z) in terms of header parameters and (i,j,k) is:
00937 
00938      [ x ]   [ R11 R12 R13 ] [        pixdim[1] * i ]   [ qoffset_x ]
00939      [ y ] = [ R21 R22 R23 ] [        pixdim[2] * j ] + [ qoffset_y ]
00940      [ z ]   [ R31 R32 R33 ] [ qfac * pixdim[3] * k ]   [ qoffset_z ]
00941 
00942    The qoffset_* shifts are in the NIFTI-1 header.  Note that the center
00943    of the (i,j,k)=(0,0,0) voxel (first value in the dataset array) is
00944    just (x,y,z)=(qoffset_x,qoffset_y,qoffset_z).
00945 
00946    The rotation matrix R is calculated from the quatern_* parameters.
00947    This calculation is described below.
00948 
00949    The scaling factor qfac is either 1 or -1.  The rotation matrix R
00950    defined by the quaternion parameters is "proper" (has determinant 1).
00951    This may not fit the needs of the data; for example, if the image
00952    grid is
00953      i increases from Left-to-Right
00954      j increases from Anterior-to-Posterior
00955      k increases from Inferior-to-Superior
00956    Then (i,j,k) is a left-handed triple.  In this example, if qfac=1,
00957    the R matrix would have to be
00958 
00959      [  1   0   0 ]
00960      [  0  -1   0 ]  which is "improper" (determinant = -1).
00961      [  0   0   1 ]
00962 
00963    If we set qfac=-1, then the R matrix would be
00964 
00965      [  1   0   0 ]
00966      [  0  -1   0 ]  which is proper.
00967      [  0   0  -1 ]
00968 
00969    This R matrix is represented by quaternion [a,b,c,d] = [0,1,0,0]
00970    (which encodes a 180 degree rotation about the x-axis).
00971 
00972    METHOD 3 (used when sform_code > 0):
00973    -----------------------------------
00974    The (x,y,z) coordinates are given by a general affine transformation
00975    of the (i,j,k) indexes:
00976 
00977      x = srow_x[0] * i + srow_x[1] * j + srow_x[2] * k + srow_x[3]
00978      y = srow_y[0] * i + srow_y[1] * j + srow_y[2] * k + srow_y[3]
00979      z = srow_z[0] * i + srow_z[1] * j + srow_z[2] * k + srow_z[3]
00980 
00981    The srow_* vectors are in the NIFTI_1 header.  Note that no use is
00982    made of pixdim[] in this method.
00983 
00984    WHY 3 METHODS?
00985    --------------
00986    Method 1 is provided only for backwards compatibility.  The intention
00987    is that Method 2 (qform_code > 0) represents the nominal voxel locations
00988    as reported by the scanner, or as rotated to some fiducial orientation and
00989    location.  Method 3, if present (sform_code > 0), is to be used to give
00990    the location of the voxels in some standard space.  The sform_code
00991    indicates which standard space is present.  Both methods 2 and 3 can be
00992    present, and be useful in different contexts (method 2 for displaying the
00993    data on its original grid; method 3 for displaying it on a standard grid).
00994 
00995    In this scheme, a dataset would originally be set up so that the
00996    Method 2 coordinates represent what the scanner reported.  Later,
00997    a registration to some standard space can be computed and inserted
00998    in the header.  Image display software can use either transform,
00999    depending on its purposes and needs.
01000 
01001    In Method 2, the origin of coordinates would generally be whatever
01002    the scanner origin is; for example, in MRI, (0,0,0) is the center
01003    of the gradient coil.
01004 
01005    In Method 3, the origin of coordinates would depend on the value
01006    of sform_code; for example, for the Talairach coordinate system,
01007    (0,0,0) corresponds to the Anterior Commissure.
01008 
01009    QUATERNION REPRESENTATION OF ROTATION MATRIX (METHOD 2)
01010    -------------------------------------------------------
01011    The orientation of the (x,y,z) axes relative to the (i,j,k) axes
01012    in 3D space is specified using a unit quaternion [a,b,c,d], where
01013    a*a+b*b+c*c+d*d=1.  The (b,c,d) values are all that is needed, since
01014    we require that a = sqrt(1.0-b*b-c*c-d*d) be nonnegative.  The (b,c,d)
01015    values are stored in the (quatern_b,quatern_c,quatern_d) fields.
01016 
01017    The quaternion representation is chosen for its compactness in
01018    representing rotations. The (proper) 3x3 rotation matrix that
01019    corresponds to [a,b,c,d] is
01020 
01021          [ a*a+b*b-c*c-d*d   2*b*c-2*a*d       2*b*d+2*a*c     ]
01022      R = [ 2*b*c+2*a*d       a*a+c*c-b*b-d*d   2*c*d-2*a*b     ]
01023          [ 2*b*d-2*a*c       2*c*d+2*a*b       a*a+d*d-c*c-b*b ]
01024 
01025          [ R11               R12               R13             ]
01026        = [ R21               R22               R23             ]
01027          [ R31               R32               R33             ]
01028 
01029    If (p,q,r) is a unit 3-vector, then rotation of angle h about that
01030    direction is represented by the quaternion
01031 
01032      [a,b,c,d] = [cos(h/2), p*sin(h/2), q*sin(h/2), r*sin(h/2)].
01033 
01034    Requiring a >= 0 is equivalent to requiring -Pi <= h <= Pi.  (Note that
01035    [-a,-b,-c,-d] represents the same rotation as [a,b,c,d]; there are 2
01036    quaternions that can be used to represent a given rotation matrix R.)
01037    To rotate a 3-vector (x,y,z) using quaternions, we compute the
01038    quaternion product
01039 
01040      [0,x',y',z'] = [a,b,c,d] * [0,x,y,z] * [a,-b,-c,-d]
01041 
01042    which is equivalent to the matrix-vector multiply
01043 
01044      [ x' ]     [ x ]
01045      [ y' ] = R [ y ]   (equivalence depends on a*a+b*b+c*c+d*d=1)
01046      [ z' ]     [ z ]
01047 
01048    Multiplication of 2 quaternions is defined by the following:
01049 
01050      [a,b,c,d] = a*1 + b*I + c*J + d*K
01051      where
01052        I*I = J*J = K*K = -1 (I,J,K are square roots of -1)
01053        I*J =  K    J*K =  I    K*I =  J
01054        J*I = -K    K*J = -I    I*K = -J  (not commutative!)
01055      For example
01056        [a,b,0,0] * [0,0,0,1] = [0,0,-b,a]
01057      since this expands to
01058        (a+b*I)*(K) = (a*K+b*I*K) = (a*K-b*J).
01059 
01060    The above formula shows how to go from quaternion (b,c,d) to
01061    rotation matrix and direction cosines.  Conversely, given R,
01062    we can compute the fields for the NIFTI-1 header by
01063 
01064      a = 0.5  * sqrt(1+R11+R22+R33)    (not stored)
01065      b = 0.25 * (R32-R23) / a       => quatern_b
01066      c = 0.25 * (R13-R31) / a       => quatern_c
01067      d = 0.25 * (R21-R12) / a       => quatern_d
01068 
01069    If a=0 (a 180 degree rotation), alternative formulas are needed.
01070    See the nifti1_io.c function mat44_to_quatern() for an implementation
01071    of the various cases in converting R to [a,b,c,d].
01072 
01073    Note that R-transpose (= R-inverse) would lead to the quaternion
01074    [a,-b,-c,-d].
01075 
01076    The choice to specify the qoffset_x (etc.) values in the final
01077    coordinate system is partly to make it easy to convert DICOM images to
01078    this format.  The DICOM attribute "Image Position (Patient)" (0020,0032)
01079    stores the (Xd,Yd,Zd) coordinates of the center of the first voxel.
01080    Here, (Xd,Yd,Zd) refer to DICOM coordinates, and Xd=-x, Yd=-y, Zd=z,
01081    where (x,y,z) refers to the NIFTI coordinate system discussed above.
01082    (i.e., DICOM +Xd is Left, +Yd is Posterior, +Zd is Superior,
01083         whereas +x is Right, +y is Anterior  , +z is Superior. )
01084    Thus, if the (0020,0032) DICOM attribute is extracted into (px,py,pz), then
01085      qoffset_x = -px   qoffset_y = -py   qoffset_z = pz
01086    is a reasonable setting when qform_code=NIFTI_XFORM_SCANNER_ANAT.
01087 
01088    That is, DICOM's coordinate system is 180 degrees rotated about the z-axis
01089    from the neuroscience/NIFTI coordinate system.  To transform between DICOM
01090    and NIFTI, you just have to negate the x- and y-coordinates.
01091 
01092    The DICOM attribute (0020,0037) "Image Orientation (Patient)" gives the
01093    orientation of the x- and y-axes of the image data in terms of 2 3-vectors.
01094    The first vector is a unit vector along the x-axis, and the second is
01095    along the y-axis.  If the (0020,0037) attribute is extracted into the
01096    value (xa,xb,xc,ya,yb,yc), then the first two columns of the R matrix
01097    would be
01098               [ -xa  -ya ]
01099               [ -xb  -yb ]
01100               [  xc   yc ]
01101    The negations are because DICOM's x- and y-axes are reversed relative
01102    to NIFTI's.  The third column of the R matrix gives the direction of
01103    displacement (relative to the subject) along the slice-wise direction.
01104    This orientation is not encoded in the DICOM standard in a simple way;
01105    DICOM is mostly concerned with 2D images.  The third column of R will be
01106    either the cross-product of the first 2 columns or its negative.  It is
01107    possible to infer the sign of the 3rd column by examining the coordinates
01108    in DICOM attribute (0020,0032) "Image Position (Patient)" for successive
01109    slices.  However, this method occasionally fails for reasons that I
01110    (RW Cox) do not understand.
01111 -----------------------------------------------------------------------------*/
01112 
01113    /* [qs]form_code value:  */      /* x,y,z coordinate system refers to:    */
01114    /*-----------------------*/      /*---------------------------------------*/
01115 
01116                                     /*! Arbitrary coordinates (Method 1). */
01117 
01118 #define NIFTI_XFORM_UNKNOWN      0
01119 
01120                                     /*! Scanner-based anatomical coordinates */
01121 
01122 #define NIFTI_XFORM_SCANNER_ANAT 1
01123 
01124                                     /*! Coordinates aligned to another file's,
01125                                         or to anatomical "truth".            */
01126 
01127 #define NIFTI_XFORM_ALIGNED_ANAT 2
01128 
01129                                     /*! Coordinates aligned to Talairach-
01130                                         Tournoux Atlas; (0,0,0)=AC, etc. */
01131 
01132 #define NIFTI_XFORM_TALAIRACH    3
01133 
01134                                     /*! MNI 152 normalized coordinates. */
01135 
01136 #define NIFTI_XFORM_MNI_152      4
01137 
01138 /*---------------------------------------------------------------------------*/
01139 /* UNITS OF SPATIAL AND TEMPORAL DIMENSIONS:
01140    ----------------------------------------
01141    The codes below can be used in xyzt_units to indicate the units of pixdim.
01142    As noted earlier, dimensions 1,2,3 are for x,y,z; dimension 4 is for
01143    time (t).
01144     - If dim[4]=1 or dim[0] < 4, there is no time axis.
01145     - A single time series (no space) would be specified with
01146       - dim[0] = 4 (for scalar data) or dim[0] = 5 (for vector data)
01147       - dim[1] = dim[2] = dim[3] = 1
01148       - dim[4] = number of time points
01149       - pixdim[4] = time step
01150       - xyzt_units indicates units of pixdim[4]
01151       - dim[5] = number of values stored at each time point
01152 
01153    Bits 0..2 of xyzt_units specify the units of pixdim[1..3]
01154     (e.g., spatial units are values 1..7).
01155    Bits 3..5 of xyzt_units specify the units of pixdim[4]
01156     (e.g., temporal units are multiples of 8).
01157 
01158    This compression of 2 distinct concepts into 1 byte is due to the
01159    limited space available in the 348 byte ANALYZE 7.5 header.  The
01160    macros XYZT_TO_SPACE and XYZT_TO_TIME can be used to mask off the
01161    undesired bits from the xyzt_units fields, leaving "pure" space
01162    and time codes.  Inversely, the macro SPACE_TIME_TO_XYZT can be
01163    used to assemble a space code (0,1,2,...,7) with a time code
01164    (0,8,16,32,...,56) into the combined value for xyzt_units.
01165 
01166    Note that codes are provided to indicate the "time" axis units are
01167    actually frequency in Hertz (_HZ) or in part-per-million (_PPM).
01168 
01169    The toffset field can be used to indicate a nonzero start point for
01170    the time axis.  That is, time point #m is at t=toffset+m*pixdim[4]
01171    for m=0..dim[4]-1.
01172 -----------------------------------------------------------------------------*/
01173 
01174                                /*! NIFTI code for unspecified units. */
01175 #define NIFTI_UNITS_UNKNOWN 0
01176 
01177                                /** Space codes are multiples of 1. **/
01178                                /*! NIFTI code for meters. */
01179 #define NIFTI_UNITS_METER   1
01180                                /*! NIFTI code for millimeters. */
01181 #define NIFTI_UNITS_MM      2
01182                                /*! NIFTI code for micrometers. */
01183 #define NIFTI_UNITS_MICRON  3
01184 
01185                                /** Time codes are multiples of 8. **/
01186                                /*! NIFTI code for seconds. */
01187 #define NIFTI_UNITS_SEC     8
01188                                /*! NIFTI code for milliseconds. */
01189 #define NIFTI_UNITS_MSEC   16
01190                                /*! NIFTI code for microseconds. */
01191 #define NIFTI_UNITS_USEC   24
01192 
01193                                /*** These units are for spectral data: ***/
01194                                /*! NIFTI code for Hertz. */
01195 #define NIFTI_UNITS_HZ     32
01196                                /*! NIFTI code for ppm. */
01197 #define NIFTI_UNITS_PPM    40
01198                                /*! NIFTI code for radians. */
01199 #define NIFTI_UNITS_RADS   48
01200 
01201 #undef  XYZT_TO_SPACE
01202 #undef  XYZT_TO_TIME
01203 #define XYZT_TO_SPACE(xyzt)       ( (xyzt) & 0x07 )
01204 #define XYZT_TO_TIME(xyzt)        ( (xyzt) & 0x38 )
01205 
01206 #undef  SPACE_TIME_TO_XYZT
01207 #define SPACE_TIME_TO_XYZT(ss,tt) (  (((char)(ss)) & 0x07)   \
01208                                    | (((char)(tt)) & 0x38) )
01209 
01210 /*---------------------------------------------------------------------------*/
01211 /* MRI-SPECIFIC SPATIAL AND TEMPORAL INFORMATION:
01212    ---------------------------------------------
01213    A few fields are provided to store some extra information
01214    that is sometimes important when storing the image data
01215    from an FMRI time series experiment.  (After processing such
01216    data into statistical images, these fields are not likely
01217    to be useful.)
01218 
01219   { freq_dim  } = These fields encode which spatial dimension (1,2, or 3)
01220   { phase_dim } = corresponds to which acquisition dimension for MRI data.
01221   { slice_dim } =
01222     Examples:
01223       Rectangular scan multi-slice EPI:
01224         freq_dim = 1  phase_dim = 2  slice_dim = 3  (or some permutation)
01225       Spiral scan multi-slice EPI:
01226         freq_dim = phase_dim = 0  slice_dim = 3
01227         since the concepts of frequency- and phase-encoding directions
01228         don't apply to spiral scan
01229 
01230     slice_duration = If this is positive, AND if slice_dim is nonzero,
01231                      indicates the amount of time used to acquire 1 slice.
01232                      slice_duration*dim[slice_dim] can be less than pixdim[4]
01233                      with a clustered acquisition method, for example.
01234 
01235     slice_code = If this is nonzero, AND if slice_dim is nonzero, AND
01236                  if slice_duration is positive, indicates the timing
01237                  pattern of the slice acquisition.  The following codes
01238                  are defined:
01239                    NIFTI_SLICE_SEQ_INC
01240                    NIFTI_SLICE_SEQ_DEC
01241                    NIFTI_SLICE_ALT_INC
01242                    NIFTI_SLICE_ALT_DEC
01243   { slice_start } = Indicates the start and end of the slice acquisition
01244   { slice_end   } = pattern, when slice_code is nonzero.  These values
01245                     are present to allow for the possible addition of
01246                     "padded" slices at either end of the volume, which
01247                     don't fit into the slice timing pattern.  If there
01248                     are no padding slices, then slice_start=0 and
01249                     slice_end=dim[slice_dim]-1 are the correct values.
01250                     For these values to be meaningful, slice_start must
01251                     be non-negative and slice_end must be greater than
01252                     slice_start.
01253 
01254   The following table indicates the slice timing pattern, relative to
01255   time=0 for the first slice acquired, for some sample cases.  Here,
01256   dim[slice_dim]=7 (there are 7 slices, labeled 0..6), slice_duration=0.1,
01257   and slice_start=1, slice_end=5 (1 padded slice on each end).
01258 
01259     slice
01260     index   SEQ_INC SEQ_DEC ALT_INC ALT_DEC
01261       6  --   n/a     n/a     n/a     n/a     n/a = not applicable
01262       5  --   0.4     0.0     0.2     0.0           (slice time offset
01263       4  --   0.3     0.1     0.4     0.3            doesn't apply to
01264       3  --   0.2     0.2     0.1     0.1            slices outside range
01265       2  --   0.1     0.3     0.3     0.4            slice_start..slice_end)
01266       1  --   0.0     0.4     0.0     0.2
01267       0  --   n/a     n/a     n/a     n/a
01268 
01269   The fields freq_dim, phase_dim, slice_dim are all squished into the single
01270   byte field dim_info (2 bits each, since the values for each field are
01271   limited to the range 0..3).  This unpleasantness is due to lack of space
01272   in the 348 byte allowance.
01273 
01274   The macros DIM_INFO_TO_FREQ_DIM, DIM_INFO_TO_PHASE_DIM, and
01275   DIM_INFO_TO_SLICE_DIM can be used to extract these values from the
01276   dim_info byte.
01277 
01278   The macro FPS_INTO_DIM_INFO can be used to put these 3 values
01279   into the dim_info byte.
01280 -----------------------------------------------------------------------------*/
01281 
01282 #undef  DIM_INFO_TO_FREQ_DIM
01283 #undef  DIM_INFO_TO_PHASE_DIM
01284 #undef  DIM_INFO_TO_SLICE_DIM
01285 
01286 #define DIM_INFO_TO_FREQ_DIM(di)   ( ((di)     ) & 0x03 )
01287 #define DIM_INFO_TO_PHASE_DIM(di)  ( ((di) >> 2) & 0x03 )
01288 #define DIM_INFO_TO_SLICE_DIM(di)  ( ((di) >> 4) & 0x03 )
01289 
01290 #undef  FPS_INTO_DIM_INFO
01291 #define FPS_INTO_DIM_INFO(fd,pd,sd) ( ( ( ((char)(fd)) & 0x03)      ) |  \
01292                                       ( ( ((char)(pd)) & 0x03) << 2 ) |  \
01293                                       ( ( ((char)(sd)) & 0x03) << 4 )  )
01294 
01295 #define NIFTI_SLICE_UNKNOWN  0
01296 #define NIFTI_SLICE_SEQ_INC  1
01297 #define NIFTI_SLICE_SEQ_DEC  2
01298 #define NIFTI_SLICE_ALT_INC  3
01299 #define NIFTI_SLICE_ALT_DEC  4
01300 
01301 /*---------------------------------------------------------------------------*/
01302 /* UNUSED FIELDS:
01303    -------------
01304    Some of the ANALYZE 7.5 fields marked as ++UNUSED++ may need to be set
01305    to particular values for compatibility with other programs.  The issue
01306    of interoperability of ANALYZE 7.5 files is a murky one -- not all
01307    programs require exactly the same set of fields.  (Unobscuring this
01308    murkiness is a principal motivation behind NIFTI-1.)
01309 
01310    Some of the fields that may need to be set for other (non-NIFTI aware)
01311    software to be happy are:
01312 
01313      extents    dbh.h says this should be 16384
01314      regular    dbh.h says this should be the character 'r'
01315      glmin,   } dbh.h says these values should be the min and max voxel
01316       glmax   }  values for the entire dataset
01317 
01318    It is best to initialize ALL fields in the NIFTI-1 header to 0
01319    (e.g., with calloc()), then fill in what is needed.
01320 -----------------------------------------------------------------------------*/
01321 
01322 /*---------------------------------------------------------------------------*/
01323 /* MISCELLANEOUS C MACROS
01324 -----------------------------------------------------------------------------*/
01325 
01326 /*.................*/
01327 /*! Given a nifti_1_header struct, check if it has a good magic number.
01328     Returns NIFTI version number (1..9) if magic is good, 0 if it is not. */
01329 
01330 #define NIFTI_VERSION(h)                               \
01331  ( ( (h).magic[0]=='n' && (h).magic[3]=='\0'    &&     \
01332      ( (h).magic[1]=='i' || (h).magic[1]=='+' ) &&     \
01333      ( (h).magic[2]>='1' && (h).magic[2]<='9' )   )    \
01334  ? (h).magic[2]-'0' : 0 )
01335 
01336 /*.................*/
01337 /*! Check if a nifti_1_header struct says if the data is stored in the
01338     same file or in a separate file.  Returns 1 if the data is in the same
01339     file as the header, 0 if it is not.                                   */
01340 
01341 #define NIFTI_ONEFILE(h) ( (h).magic[1] == '+' )
01342 
01343 /*.................*/
01344 /*! Check if a nifti_1_header struct needs to be byte swapped.
01345     Returns 1 if it needs to be swapped, 0 if it does not.     */
01346 
01347 #define NIFTI_NEEDS_SWAP(h) ( (h).dim[0] < 0 || (h).dim[0] > 7 )
01348 
01349 /*.................*/
01350 /*! Check if a nifti_1_header struct contains a 5th (vector) dimension.
01351     Returns size of 5th dimension if > 1, returns 0 otherwise.         */
01352 
01353 #define NIFTI_5TH_DIM(h) ( ((h).dim[0]>4 && (h).dim[5]>1) ? (h).dim[5] : 0 )
01354 
01355 /*****************************************************************************/
01356 
01357 /*=================*/
01358 #ifdef  __cplusplus
01359 }
01360 #endif
01361 /*=================*/
01362 
01363 #endif /* _NIFTI_HEADER_ */
Powered by Plone

This site conforms to the following standards: