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 File Reference

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include "nifti1.h"
#include "znzlib/znzlib.h"

Go to the source code of this file.


Data Structures

struct  mat33
struct  mat44
struct  nifti_brick_list
struct  nifti_image

Defines

#define DONT_INCLUDE_ANALYZE_STRUCT   /*** not needed herein ***/
#define NIFTI_L2R   1
#define NIFTI_R2L   2
#define NIFTI_P2A   3
#define NIFTI_A2P   4
#define NIFTI_I2S   5
#define NIFTI_S2I   6
#define LNI_FERR(func, msg, file)   fprintf(stderr,"** ERROR (%s): %s '%s'\n",func,msg,file)
#define swap_2(s)   swap_2bytes(1,&(s))
#define swap_4(v)   swap_4bytes(1,&(v))
#define IS_GOOD_FLOAT(x)   1
#define FIXED_FLOAT(x)   (x)
#define ASSIF(p, v)   if( (p)!=NULL ) *(p) = (v)
#define LSB_FIRST   1
#define MSB_FIRST   2
#define REVERSE_ORDER(x)   (3-(x))
#define LNI_MAX_NIA_EXT_LEN   100000

Functions

char * nifti_datatype_string (int dt)
char * nifti_units_string (int uu)
char * nifti_intent_string (int ii)
char * nifti_xform_string (int xx)
char * nifti_slice_string (int ss)
char * nifti_orientation_string (int ii)
int nifti_is_inttype (int dt)
mat44 mat44_inverse (mat44 R)
mat33 mat33_inverse (mat33 R)
mat33 mat33_polar (mat33 A)
float mat33_rownorm (mat33 A)
float mat33_colnorm (mat33 A)
float mat33_determ (mat33 R)
mat33 mat33_mul (mat33 A, mat33 B)
void swap_2bytes (int n, void *ar)
void swap_4bytes (int n, void *ar)
void swap_8bytes (int n, void *ar)
void swap_16bytes (int n, void *ar)
void swap_Nbytes (int n, int siz, void *ar)
void swap_nifti_header (struct nifti_1_header *h, int is_nifti)
unsigned int get_filesize (char *pathname)
nifti_image * nifti_image_read_bricks (char *hname, int nbricks, int *blist, nifti_brick_list *NBL)
int nifti_image_load_bricks (nifti_image *nim, int nbricks, int *blist, nifti_brick_list *NBL)
void free_NBL (nifti_brick_list *NBL)
nifti_image * nifti_image_read (char *hname, int read_data)
int nifti_image_load (nifti_image *nim)
void nifti_image_unload (nifti_image *nim)
void nifti_image_free (nifti_image *nim)
void nifti_image_write (nifti_image *nim)
void nifti_image_infodump (nifti_image *nim)
void nifti_disp_lib_hist (void)
void nifti_disp_lib_version (void)
int nifti_disp_matrix_orient (char *mesg, mat44 mat)
char * nifti_image_to_ascii (nifti_image *nim)
nifti_image * nifti_image_from_ascii (char *str, int *bytes_read)
size_t nifti_get_volsize (nifti_image *nim)
int is_nifti_file (char *hname)
char * nifti_find_file_extension (char *name)
int nifti_validfilename (char *fname)
int disp_nifti_1_header (char *info, nifti_1_header *hp)
void nifti_set_debug_level (int level)
int print_hex_vals (char *data, int nbytes, FILE *fp)
int valid_nifti_brick_list (nifti_image *nim, int nbricks, int *blist, int disp_error)
void nifti_datatype_sizes (int datatype, int *nbyper, int *swapsize)
void mat44_to_quatern (mat44 R, float *qb, float *qc, float *qd, float *qx, float *qy, float *qz, float *dx, float *dy, float *dz, float *qfac)
mat44 quatern_to_mat44 (float qb, float qc, float qd, float qx, float qy, float qz, float dx, float dy, float dz, float qfac)
mat44 make_orthog_mat44 (float r11, float r12, float r13, float r21, float r22, float r23, float r31, float r32, float r33)
int unescape_string (char *str)
char * escapize_string (char *str)
int short_order (void)
void mat44_to_orientation (mat44 R, int *icod, int *jcod, int *kcod)
char * nifti_findhdrname (char *fname)
char * nifti_findimgname (char *fname, int nifti_type)
int nifti_is_gzfile (char *fname)
char * nifti_makebasename (char *fname)
int nifti_read_extensions (nifti_image *nim, znzFile fp)
int nifti_read_next_extension (nifti1_extension *nex, nifti_image *nim, znzFile fp)
int nifti_add_exten_to_list (nifti1_extension *new_ext, nifti1_extension **list, int new_length)
int nifti_write_extensions (znzFile fp, nifti_image *nim)
int nifti_valid_extension (nifti_image *nim, int size, int code)
void update_nifti_image_for_brick_list (nifti_image *nim, int nbricks)
int nifti_load_NBL_bricks (nifti_image *nim, int *slist, int *sindex, nifti_brick_list *NBL, znzFile fp)
int nifti_alloc_NBL_mem (nifti_image *nim, int nbricks, nifti_brick_list *nbl)
int nifti_copynsort (int nbricks, int *blist, int **slist, int **sindex)
int int_force_positive (int *list, int nel)
znzFile nifti_image_write_hdr_img (nifti_image *nim, int write_data, char *opts)
znzFile nifti_image_write_hdr_img2 (nifti_image *nim, int write_data, char *opts, znzFile *imgfile)
znzFile nifti_image_open (char *hname, char *opts, nifti_image **nim)
znzFile nifti_image_load_prep (nifti_image *nim)
size_t nifti_read_buffer (znzFile fp, void *datatptr, size_t ntot, nifti_image *nim)
void nifti_write_all_data (znzFile fp, nifti_image *nim)
size_t nifti_write_buffer (znzFile fp, void *buffer, size_t numbytes)
nifti_image * nifti_copy_nim_info (nifti_image *src)
nifti_image * nifti_simple_init_nim ()
nifti_1_header nifti_convert_nim2nhdr (nifti_image *nim)
nifti_image * nifti_convert_nhdr2nim (struct nifti_1_header nhdr, char *fname)
void nifti_set_iname_offset (nifti_image *nim)

Define Documentation

#define ASSIF (  p,
v    )     if( (p)!=NULL ) *(p) = (v)
 

Definition at line 307 of file nifti1_io.h.

Referenced by mat44_to_quatern(), MCW_widget_geom(), and nifti_datatype_sizes().

#define DONT_INCLUDE_ANALYZE_STRUCT   /*** not needed herein ***/
 

Definition at line 11 of file nifti1_io.h.

#define FIXED_FLOAT (  x    )     (x)
 

Definition at line 303 of file nifti1_io.h.

Referenced by nifti_convert_nhdr2nim().

#define IS_GOOD_FLOAT (  x    )     1
 

Definition at line 302 of file nifti1_io.h.

Referenced by nifti_convert_nhdr2nim(), nifti_read_buffer(), thd_complexscan(), and thd_floatscan().

#define LNI_FERR (  func,
msg,
file    )     fprintf(stderr,"** ERROR (%s): %s '%s'\n",func,msg,file)
 

Definition at line 284 of file nifti1_io.h.

Referenced by nifti_image_load_prep(), and nifti_image_read().

#define LNI_MAX_NIA_EXT_LEN   100000
 

Definition at line 316 of file nifti1_io.h.

Referenced by nifti_valid_extension().

#define LSB_FIRST   1
 

Definition at line 312 of file nifti1_io.h.

#define MSB_FIRST   2
 

Definition at line 313 of file nifti1_io.h.

#define NIFTI_A2P   4
 

Definition at line 223 of file nifti1_io.h.

Referenced by mat44_to_orientation(), and nifti_orientation_string().

#define NIFTI_I2S   5
 

Definition at line 224 of file nifti1_io.h.

Referenced by mat44_to_orientation(), and nifti_orientation_string().

#define NIFTI_L2R   1
 

Definition at line 220 of file nifti1_io.h.

Referenced by mat44_to_orientation(), and nifti_orientation_string().

#define NIFTI_P2A   3
 

Definition at line 222 of file nifti1_io.h.

Referenced by mat44_to_orientation(), and nifti_orientation_string().

#define NIFTI_R2L   2
 

Definition at line 221 of file nifti1_io.h.

Referenced by mat44_to_orientation(), and nifti_orientation_string().

#define NIFTI_S2I   6
 

Definition at line 225 of file nifti1_io.h.

Referenced by mat44_to_orientation(), and nifti_orientation_string().

#define REVERSE_ORDER (  x    )     (3-(x))
 

Definition at line 314 of file nifti1_io.h.

#define swap_2 (  s    )     swap_2bytes(1,&(s))
 

Definition at line 289 of file nifti1_io.h.

Referenced by ART_send_volume(), disp_numeric_data(), ge4_swap_all_bytes(), main(), mri_read3D_analyze75(), mri_read_analyze75(), nifti_convert_nhdr2nim(), swap_analyze_hdr(), swap_nifti_header(), THD_open_analyze(), THD_open_ctfmri(), and write_data_to_file().

#define swap_4 (  v    )     swap_4bytes(1,&(v))
 

Definition at line 290 of file nifti1_io.h.

Referenced by disp_numeric_data(), ge4_swap_all_bytes(), ge_header(), is_nifti_file(), main(), mri_imcount_siemens(), mri_read(), mri_read_siemens(), nifti_convert_nhdr2nim(), read_ge_header(), read_ge_image(), read_socket(), swap_analyze_hdr(), swap_nifti_header(), THD_open_ctfmri(), THD_open_ctfsam(), and write_data_to_file().


Function Documentation

int disp_nifti_1_header (  char *    info,
nifti_1_header *    hp
) 
 

Definition at line 1947 of file nifti1_io.c.

References nifti_1_header::aux_file, nifti_1_header::bitpix, c, nifti_1_header::cal_max, nifti_1_header::cal_min, nifti_1_header::data_type, nifti_1_header::datatype, nifti_1_header::db_name, nifti_1_header::descrip, nifti_1_header::dim, nifti_1_header::dim_info, nifti_1_header::extents, nifti_1_header::glmax, nifti_1_header::glmin, nifti_1_header::intent_code, nifti_1_header::intent_name, nifti_1_header::intent_p1, nifti_1_header::intent_p2, nifti_1_header::intent_p3, nifti_1_header::magic, nifti_1_header::pixdim, print_hex_vals(), nifti_1_header::qform_code, nifti_1_header::qoffset_x, nifti_1_header::qoffset_y, nifti_1_header::qoffset_z, nifti_1_header::quatern_b, nifti_1_header::quatern_c, nifti_1_header::quatern_d, nifti_1_header::regular, nifti_1_header::scl_inter, nifti_1_header::scl_slope, nifti_1_header::session_error, nifti_1_header::sform_code, nifti_1_header::sizeof_hdr, nifti_1_header::slice_code, nifti_1_header::slice_duration, nifti_1_header::slice_end, nifti_1_header::slice_start, nifti_1_header::srow_x, nifti_1_header::srow_y, nifti_1_header::srow_z, nifti_1_header::toffset, nifti_1_header::vox_offset, and nifti_1_header::xyzt_units.

Referenced by nifti_convert_nhdr2nim().

01948 {
01949    int c;
01950 
01951    fputs( "-------------------------------------------------------\n", stderr );
01952    if ( info )  fputs( info, stderr );
01953    if ( !hp  ){ fputs(" ** no nifti_1_header to display!\n",stderr); return 1; }
01954 
01955    fprintf(stderr," nifti_1_header :\n"
01956            "    sizeof_hdr     = %d\n"
01957            "    data_type[10]  = ", hp->sizeof_hdr);
01958    print_hex_vals(hp->data_type, 10, stderr);
01959    fprintf(stderr, "\n"
01960            "    db_name[18]    = ");
01961    print_hex_vals(hp->db_name, 18, stderr);
01962    fprintf(stderr, "\n"
01963            "    extents        = %d\n"
01964            "    session_error  = %d\n"
01965            "    regular        = 0x%x\n"
01966            "    dim_info       = 0x%x\n",
01967       hp->extents, hp->session_error, hp->regular, hp->dim_info );
01968    fprintf(stderr, "    dim[8]         =");
01969    for ( c = 0; c < 8; c++ ) fprintf(stderr," %d", hp->dim[c]);
01970    fprintf(stderr, "\n"
01971            "    intent_p1      = %f\n"
01972            "    intent_p2      = %f\n"
01973            "    intent_p3      = %f\n"
01974            "    intent_code    = %d\n"
01975            "    datatype       = %d\n"
01976            "    bitpix         = %d\n"
01977            "    slice_start    = %d\n"
01978            "    pixdim[8]      =",
01979            hp->intent_p1, hp->intent_p2, hp->intent_p3, hp->intent_code,
01980            hp->datatype, hp->bitpix, hp->slice_start);
01981    /* break pixdim over 2 lines */
01982    for ( c = 0; c < 4; c++ ) fprintf(stderr," %f", hp->pixdim[c]);
01983    fprintf(stderr, "\n                    ");
01984    for ( c = 4; c < 8; c++ ) fprintf(stderr," %f", hp->pixdim[c]);
01985    fprintf(stderr, "\n"
01986            "    vox_offset     = %f\n"
01987            "    scl_slope      = %f\n"
01988            "    scl_inter      = %f\n"
01989            "    slice_end      = %d\n"
01990            "    slice_code     = %d\n"
01991            "    xyzt_units     = 0x%x\n"
01992            "    cal_max        = %f\n"
01993            "    cal_min        = %f\n"
01994            "    slice_duration = %f\n"
01995            "    toffset        = %f\n"
01996            "    glmax          = %d\n"
01997            "    glmin          = %d\n",
01998            hp->vox_offset, hp->scl_slope, hp->scl_inter, hp->slice_end,
01999            hp->slice_code, hp->xyzt_units, hp->cal_max, hp->cal_min,
02000            hp->slice_duration, hp->toffset, hp->glmax, hp->glmin);
02001    fprintf(stderr,
02002            "    descrip        = '%s'\n"
02003            "    aux_file       = '%s'\n"
02004            "    qform_code     = %d\n"
02005            "    sform_code     = %d\n"
02006            "    quatern_b      = %f\n"
02007            "    quatern_c      = %f\n"
02008            "    quatern_d      = %f\n"
02009            "    qoffset_x      = %f\n"
02010            "    qoffset_y      = %f\n"
02011            "    qoffset_z      = %f\n"
02012            "    srow_x[4]      = %f, %f, %f, %f\n"
02013            "    srow_y[4]      = %f, %f, %f, %f\n"
02014            "    srow_z[4]      = %f, %f, %f, %f\n"
02015            "    intent_name    = %-16s\n"
02016            "    magic          = %-4s\n",
02017            hp->descrip, hp->aux_file, hp->qform_code, hp->sform_code,
02018            hp->quatern_b, hp->quatern_c, hp->quatern_d,
02019            hp->qoffset_x, hp->qoffset_y, hp->qoffset_z,
02020            hp->srow_x[0], hp->srow_x[1], hp->srow_x[2], hp->srow_x[3],
02021            hp->srow_y[0], hp->srow_y[1], hp->srow_y[2], hp->srow_y[3],
02022            hp->srow_z[0], hp->srow_z[1], hp->srow_z[2], hp->srow_z[3],
02023            hp->intent_name, hp->magic);
02024    fputs( "-------------------------------------------------------\n", stderr );
02025 
02026    return 0;
02027 }

char* escapize_string (  char *    str ) 
 

Definition at line 3573 of file nifti1_io.c.

References calloc, CR, LF, and nifti_strdup().

Referenced by nifti_image_to_ascii().

03574 {
03575    int ii,jj , lstr,lout ;
03576    char *out ;
03577 
03578    if( str == NULL || (lstr=strlen(str)) == 0 ){      /* 0 length */
03579      out = nifti_strdup("''") ; return out ;                /* string?? */
03580    }
03581 
03582    lout = 4 ;                      /* initialize length of output */
03583    for( ii=0 ; ii < lstr ; ii++ ){ /* count characters for output */
03584      switch( str[ii] ){
03585        case '&':  lout += 5 ; break ;  /* replace '&' with "&amp;" */
03586 
03587        case '<':
03588        case '>':  lout += 4 ; break ;  /* replace '<' with "&lt;" */
03589 
03590        case '"' :
03591        case '\'': lout += 6 ; break ;  /* replace '"' with "&quot;" */
03592 
03593        case CR:
03594        case LF:   lout += 6 ; break ;  /* replace CR with "&#x0d;"
03595                                                   LF with "&#x0a;" */
03596 
03597        default: lout++ ; break ;      /* copy all other chars */
03598      }
03599    }
03600    out = (char *)calloc(1,lout) ;     /* allocate output string */
03601    out[0] = '\'' ;                    /* opening quote mark */
03602    for( ii=0,jj=1 ; ii < lstr ; ii++ ){
03603       switch( str[ii] ){
03604          default: out[jj++] = str[ii] ; break ;  /* normal characters */
03605 
03606          case '&':  memcpy(out+jj,"&amp;",5)  ; jj+=5 ; break ;
03607 
03608          case '<':  memcpy(out+jj,"&lt;",4)   ; jj+=4 ; break ;
03609          case '>':  memcpy(out+jj,"&gt;",4)   ; jj+=4 ; break ;
03610 
03611          case '"' : memcpy(out+jj,"&quot;",6) ; jj+=6 ; break ;
03612 
03613          case '\'': memcpy(out+jj,"&apos;",6) ; jj+=6 ; break ;
03614 
03615          case CR:   memcpy(out+jj,"&#x0d;",6) ; jj+=6 ; break ;
03616          case LF:   memcpy(out+jj,"&#x0a;",6) ; jj+=6 ; break ;
03617       }
03618    }
03619    out[jj++] = '\''  ;  /* closing quote mark */
03620    out[jj]   = '\0' ;  /* terminate the string */
03621    return out ;
03622 }

void free_NBL (  nifti_brick_list *    NBL ) 
 

Definition at line 317 of file nifti1_io.c.

References nifti_brick_list::bricks, nifti_brick_list::bsize, c, free, and nifti_brick_list::nbricks.

Referenced by nifti_image_load_bricks().

00318 {
00319    int c;
00320 
00321    if( NBL->bricks ){
00322       for( c = 0; c < NBL->nbricks; c++ )
00323          if( NBL->bricks[c] ) free(NBL->bricks[c]);
00324       free(NBL->bricks);
00325       NBL->bricks = NULL;
00326    }
00327 
00328    NBL->nbricks = NBL->bsize = 0;
00329 }

unsigned int get_filesize (  char *    pathname ) 
 

Definition at line 1628 of file nifti1_io.c.

Referenced by nifti_image_load_prep(), and nifti_image_read().

01629 {
01630    struct stat buf ; int ii ;
01631 
01632    if( pathname == NULL || *pathname == '\0' ) return 0 ;
01633    ii = stat( pathname , &buf ); if( ii != 0 ) return 0 ;
01634    return (unsigned int)buf.st_size ;
01635 }

int int_force_positive (  int *    list,
int    nel
)  [static]
 

Referenced by valid_nifti_brick_list().

int is_nifti_file (  char *    hname ) 
 

if using compression libraries, .gz files are looked for first *

Definition at line 1886 of file nifti1_io.c.

References free, gni_debug, nifti_findhdrname(), NIFTI_ONEFILE, nifti_validfilename(), NIFTI_VERSION, nifti_1_header::sizeof_hdr, and swap_4.

01887 {
01888    struct nifti_1_header nhdr ;
01889    znzFile fp ;
01890    int ii ;
01891    char *tmpname;
01892 
01893    /* bad input name? */
01894 
01895    if( !nifti_validfilename(hname) ) return -1 ;
01896 
01897    /* open file */
01898 
01899    tmpname = nifti_findhdrname(hname);
01900    if( tmpname == NULL ){
01901       if( gni_debug > 0 )
01902          fprintf(stderr,"** no header file found for '%s'\n",hname);
01903       return -1;
01904    }
01905    fp = znzopen( tmpname , "rb" , 1 ) ;
01906    free(tmpname);
01907    if (znz_isnull(fp))                      return -1 ;  /* bad open? */
01908 
01909    /* read header, close file */
01910 
01911    ii = znzread( &nhdr , 1 , sizeof(nhdr) , fp ) ;
01912    znzclose( fp ) ;
01913    if( ii < sizeof(nhdr) )               return -1 ;  /* bad read? */
01914 
01915    /* check for NIFTI-ness */
01916 
01917    if( NIFTI_VERSION(nhdr) != 0 ){
01918      return ( NIFTI_ONEFILE(nhdr) ) ? 1 : 2 ;
01919    }
01920 
01921    /* check for ANALYZE-ness (sizeof_hdr field == 348) */
01922 
01923    ii = nhdr.sizeof_hdr ;
01924    if( ii == sizeof(nhdr) ) return 0 ;  /* matches */
01925 
01926    /* try byte-swapping header */
01927 
01928    swap_4(ii) ;
01929    if( ii == sizeof(nhdr) ) return 0 ;  /* matches */
01930 
01931    return -1 ;                          /* not good */
01932 }

mat44 make_orthog_mat44 (  float    r11,
float    r12,
float    r13,
float    r21,
float    r22,
float    r23,
float    r31,
float    r32,
float    r33
) 
 

INPUT MATRIX IS: *

Definition at line 1104 of file nifti1_io.c.

References mat33::m, mat44::m, and mat33_polar().

01107 {
01108    mat44 R ;
01109    mat33 Q , P ;
01110    double val ;
01111 
01112    R.m[3][0] = R.m[3][1] = R.m[3][2] = 0.0l ; R.m[3][3] = 1.0l ;
01113 
01114    Q.m[0][0] = r11 ; Q.m[0][1] = r12 ; Q.m[0][2] = r13 ; /* load Q */
01115    Q.m[1][0] = r21 ; Q.m[1][1] = r22 ; Q.m[1][2] = r23 ;
01116    Q.m[2][0] = r31 ; Q.m[2][1] = r32 ; Q.m[2][2] = r33 ;
01117 
01118    /* normalize row 1 */
01119 
01120    val = Q.m[0][0]*Q.m[0][0] + Q.m[0][1]*Q.m[0][1] + Q.m[0][2]*Q.m[0][2] ;
01121    if( val > 0.0l ){
01122      val = 1.0l / sqrt(val) ;
01123      Q.m[0][0] *= val ; Q.m[0][1] *= val ; Q.m[0][2] *= val ;
01124    } else {
01125      Q.m[0][0] = 1.0l ; Q.m[0][1] = 0.0l ; Q.m[0][2] = 0.0l ;
01126    }
01127 
01128    /* normalize row 2 */
01129 
01130    val = Q.m[1][0]*Q.m[1][0] + Q.m[1][1]*Q.m[1][1] + Q.m[1][2]*Q.m[1][2] ;
01131    if( val > 0.0l ){
01132      val = 1.0l / sqrt(val) ;
01133      Q.m[1][0] *= val ; Q.m[1][1] *= val ; Q.m[1][2] *= val ;
01134    } else {
01135      Q.m[1][0] = 0.0l ; Q.m[1][1] = 1.0l ; Q.m[1][2] = 0.0l ;
01136    }
01137 
01138    /* normalize row 3 */
01139 
01140    val = Q.m[2][0]*Q.m[2][0] + Q.m[2][1]*Q.m[2][1] + Q.m[2][2]*Q.m[2][2] ;
01141    if( val > 0.0l ){
01142      val = 1.0l / sqrt(val) ;
01143      Q.m[2][0] *= val ; Q.m[2][1] *= val ; Q.m[2][2] *= val ;
01144    } else {
01145      Q.m[2][0] = Q.m[0][1]*Q.m[1][2] - Q.m[0][2]*Q.m[1][1] ;  /* cross */
01146      Q.m[2][1] = Q.m[0][2]*Q.m[1][0] - Q.m[0][0]*Q.m[1][2] ;  /* product */
01147      Q.m[2][2] = Q.m[0][0]*Q.m[1][1] - Q.m[0][1]*Q.m[1][0] ;
01148    }
01149 
01150    P = mat33_polar(Q) ;  /* P is orthog matrix closest to Q */
01151 
01152    R.m[0][0] = P.m[0][0] ; R.m[0][1] = P.m[0][1] ; R.m[0][2] = P.m[0][2] ;
01153    R.m[1][0] = P.m[1][0] ; R.m[1][1] = P.m[1][1] ; R.m[1][2] = P.m[1][2] ;
01154    R.m[2][0] = P.m[2][0] ; R.m[2][1] = P.m[2][1] ; R.m[2][2] = P.m[2][2] ;
01155 
01156    R.m[0][3] = R.m[1][3] = R.m[2][3] = 0.0 ; return R ;
01157 }

float mat33_colnorm (  mat33    A ) 
 

Definition at line 1220 of file nifti1_io.c.

References mat33::m.

Referenced by mat33_polar().

01221 {
01222    float r1,r2,r3 ;
01223 
01224    r1 = fabs(A.m[0][0])+fabs(A.m[1][0])+fabs(A.m[2][0]) ;
01225    r2 = fabs(A.m[0][1])+fabs(A.m[1][1])+fabs(A.m[2][1]) ;
01226    r3 = fabs(A.m[0][2])+fabs(A.m[1][2])+fabs(A.m[2][2]) ;
01227    if( r1 < r2 ) r1 = r2 ;
01228    if( r1 < r3 ) r1 = r3 ;
01229    return r1 ;
01230 }

float mat33_determ (  mat33    R ) 
 

INPUT MATRIX: *

Definition at line 1192 of file nifti1_io.c.

References mat33::m.

Referenced by mat33_polar(), and mat44_to_orientation().

01193 {
01194    double r11,r12,r13,r21,r22,r23,r31,r32,r33 ;
01195                                                        /** INPUT MATRIX: **/
01196    r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2];  /* [ r11 r12 r13 ] */
01197    r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2];  /* [ r21 r22 r23 ] */
01198    r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2];  /* [ r31 r32 r33 ] */
01199 
01200    return r11*r22*r33-r11*r32*r23-r21*r12*r33
01201          +r21*r32*r13+r31*r12*r23-r31*r22*r13 ;
01202 }

mat33 mat33_inverse (  mat33    R ) 
 

Definition at line 1161 of file nifti1_io.c.

References mat33::m.

Referenced by mat33_polar().

01162 {
01163    double r11,r12,r13,r21,r22,r23,r31,r32,r33 , deti ;
01164    mat33 Q ;
01165                                                        /** INPUT MATRIX: **/
01166    r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2];  /* [ r11 r12 r13 ] */
01167    r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2];  /* [ r21 r22 r23 ] */
01168    r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2];  /* [ r31 r32 r33 ] */
01169 
01170    deti = r11*r22*r33-r11*r32*r23-r21*r12*r33
01171          +r21*r32*r13+r31*r12*r23-r31*r22*r13 ;
01172 
01173    if( deti != 0.0l ) deti = 1.0l / deti ;
01174 
01175    Q.m[0][0] = deti*( r22*r33-r32*r23) ;
01176    Q.m[0][1] = deti*(-r12*r33+r32*r13) ;
01177    Q.m[0][2] = deti*( r12*r23-r22*r13) ;
01178 
01179    Q.m[1][0] = deti*(-r21*r33+r31*r23) ;
01180    Q.m[1][1] = deti*( r11*r33-r31*r13) ;
01181    Q.m[1][2] = deti*(-r11*r23+r21*r13) ;
01182 
01183    Q.m[2][0] = deti*( r21*r32-r31*r22) ;
01184    Q.m[2][1] = deti*(-r11*r32+r31*r12) ;
01185    Q.m[2][2] = deti*( r11*r22-r21*r12) ;
01186 
01187    return Q ;
01188 }

mat33 mat33_mul (  mat33    A,
mat33    B
) 
 

Definition at line 1234 of file nifti1_io.c.

References i, and mat33::m.

Referenced by mat44_to_orientation().

01235 {
01236    mat33 C ; int i,j ;
01237    for( i=0 ; i < 3 ; i++ )
01238     for( j=0 ; j < 3 ; j++ )
01239       C.m[i][j] =  A.m[i][0] * B.m[0][j]
01240                  + A.m[i][1] * B.m[1][j]
01241                  + A.m[i][2] * B.m[2][j] ;
01242    return C ;
01243 }

mat33 mat33_polar (  mat33    A ) 
 

Definition at line 1251 of file nifti1_io.c.

References mat33::m, mat33_colnorm(), mat33_determ(), mat33_inverse(), and mat33_rownorm().

Referenced by make_orthog_mat44(), and mat44_to_quatern().

01252 {
01253    mat33 X , Y , Z ;
01254    float alp,bet,gam,gmi , dif=1.0 ;
01255    int k=0 ;
01256 
01257    X = A ;
01258 
01259    /* force matrix to be nonsingular */
01260 
01261    gam = mat33_determ(X) ;
01262    while( gam == 0.0 ){        /* perturb matrix */
01263      gam = 0.00001 * ( 0.001 + mat33_rownorm(X) ) ;
01264      X.m[0][0] += gam ; X.m[1][1] += gam ; X.m[2][2] += gam ;
01265      gam = mat33_determ(X) ;
01266    }
01267 
01268    while(1){
01269      Y = mat33_inverse(X) ;
01270      if( dif > 0.3 ){     /* far from convergence */
01271        alp = sqrt( mat33_rownorm(X) * mat33_colnorm(X) ) ;
01272        bet = sqrt( mat33_rownorm(Y) * mat33_colnorm(Y) ) ;
01273        gam = sqrt( bet / alp ) ;
01274        gmi = 1.0 / gam ;
01275      } else {
01276        gam = gmi = 1.0 ;  /* close to convergence */
01277      }
01278      Z.m[0][0] = 0.5 * ( gam*X.m[0][0] + gmi*Y.m[0][0] ) ;
01279      Z.m[0][1] = 0.5 * ( gam*X.m[0][1] + gmi*Y.m[1][0] ) ;
01280      Z.m[0][2] = 0.5 * ( gam*X.m[0][2] + gmi*Y.m[2][0] ) ;
01281      Z.m[1][0] = 0.5 * ( gam*X.m[1][0] + gmi*Y.m[0][1] ) ;
01282      Z.m[1][1] = 0.5 * ( gam*X.m[1][1] + gmi*Y.m[1][1] ) ;
01283      Z.m[1][2] = 0.5 * ( gam*X.m[1][2] + gmi*Y.m[2][1] ) ;
01284      Z.m[2][0] = 0.5 * ( gam*X.m[2][0] + gmi*Y.m[0][2] ) ;
01285      Z.m[2][1] = 0.5 * ( gam*X.m[2][1] + gmi*Y.m[1][2] ) ;
01286      Z.m[2][2] = 0.5 * ( gam*X.m[2][2] + gmi*Y.m[2][2] ) ;
01287 
01288      dif = fabs(Z.m[0][0]-X.m[0][0])+fabs(Z.m[0][1]-X.m[0][1])
01289           +fabs(Z.m[0][2]-X.m[0][2])+fabs(Z.m[1][0]-X.m[1][0])
01290           +fabs(Z.m[1][1]-X.m[1][1])+fabs(Z.m[1][2]-X.m[1][2])
01291           +fabs(Z.m[2][0]-X.m[2][0])+fabs(Z.m[2][1]-X.m[2][1])
01292           +fabs(Z.m[2][2]-X.m[2][2])                          ;
01293 
01294      k = k+1 ;
01295      if( k > 100 || dif < 3.e-6 ) break ;  /* convergence or exhaustion */
01296      X = Z ;
01297    }
01298 
01299    return Z ;
01300 }

float mat33_rownorm (  mat33    A ) 
 

INPUT MATRIX: *

Definition at line 1206 of file nifti1_io.c.

References mat33::m.

Referenced by mat33_polar().

01207 {
01208    float r1,r2,r3 ;
01209 
01210    r1 = fabs(A.m[0][0])+fabs(A.m[0][1])+fabs(A.m[0][2]) ;
01211    r2 = fabs(A.m[1][0])+fabs(A.m[1][1])+fabs(A.m[1][2]) ;
01212    r3 = fabs(A.m[2][0])+fabs(A.m[2][1])+fabs(A.m[2][2]) ;
01213    if( r1 < r2 ) r1 = r2 ;
01214    if( r1 < r3 ) r1 = r3 ;
01215    return r1 ;
01216 }

mat44 mat44_inverse (  mat44    R ) 
 

Definition at line 1040 of file nifti1_io.c.

References mat44::m, and v1.

Referenced by nifti_convert_nhdr2nim(), and nifti_image_from_ascii().

01041 {
01042    double r11,r12,r13,r21,r22,r23,r31,r32,r33,v1,v2,v3 , deti ;
01043    mat44 Q ;
01044                                                        /** INPUT MATRIX IS: **/
01045    r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2];  /* [ r11 r12 r13 v1 ] */
01046    r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2];  /* [ r21 r22 r23 v2 ] */
01047    r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2];  /* [ r31 r32 r33 v3 ] */
01048    v1  = R.m[0][3]; v2  = R.m[1][3]; v3  = R.m[2][3];  /* [  0   0   0   1 ] */
01049 
01050    deti = r11*r22*r33-r11*r32*r23-r21*r12*r33
01051          +r21*r32*r13+r31*r12*r23-r31*r22*r13 ;
01052 
01053    if( deti != 0.0l ) deti = 1.0l / deti ;
01054 
01055    Q.m[0][0] = deti*( r22*r33-r32*r23) ;
01056    Q.m[0][1] = deti*(-r12*r33+r32*r13) ;
01057    Q.m[0][2] = deti*( r12*r23-r22*r13) ;
01058    Q.m[0][3] = deti*(-r12*r23*v3+r12*v2*r33+r22*r13*v3
01059                      -r22*v1*r33-r32*r13*v2+r32*v1*r23) ;
01060 
01061    Q.m[1][0] = deti*(-r21*r33+r31*r23) ;
01062    Q.m[1][1] = deti*( r11*r33-r31*r13) ;
01063    Q.m[1][2] = deti*(-r11*r23+r21*r13) ;
01064    Q.m[1][3] = deti*( r11*r23*v3-r11*v2*r33-r21*r13*v3
01065                      +r21*v1*r33+r31*r13*v2-r31*v1*r23) ;
01066 
01067    Q.m[2][0] = deti*( r21*r32-r31*r22) ;
01068    Q.m[2][1] = deti*(-r11*r32+r31*r12) ;
01069    Q.m[2][2] = deti*( r11*r22-r21*r12) ;
01070    Q.m[2][3] = deti*(-r11*r22*v3+r11*r32*v2+r21*r12*v3
01071                      -r21*r32*v1-r31*r12*v2+r31*r22*v1) ;
01072 
01073    Q.m[3][0] = Q.m[3][1] = Q.m[3][2] = 0.0l ;
01074    Q.m[3][3] = (deti == 0.0l) ? 0.0l : 1.0l ; /* failure flag if deti == 0 */
01075 
01076    return Q ;
01077 }

void mat44_to_orientation (  mat44    R,
int *    icod,
int *    jcod,
int *    kcod
) 
 

Definition at line 1318 of file nifti1_io.c.

References i, mat33::m, mat44::m, mat33_determ(), mat33_mul(), NIFTI_A2P, NIFTI_I2S, NIFTI_L2R, NIFTI_P2A, NIFTI_R2L, NIFTI_S2I, p, and r.

Referenced by nifti_disp_matrix_orient(), and nifti_image_to_ascii().

01319 {
01320    float xi,xj,xk , yi,yj,yk , zi,zj,zk , val,detQ,detP ;
01321    mat33 P , Q , M ;
01322    int i,j,k=0,p,q,r , ibest,jbest,kbest,pbest,qbest,rbest ;
01323    float vbest ;
01324 
01325    if( icod == NULL || jcod == NULL || kcod == NULL ) return ; /* bad */
01326 
01327    *icod = *jcod = *kcod = 0 ; /* error returns, if sh*t happens */
01328 
01329    /* load column vectors for each (i,j,k) direction from matrix */
01330 
01331    /*-- i axis --*/ /*-- j axis --*/ /*-- k axis --*/
01332 
01333    xi = R.m[0][0] ; xj = R.m[0][1] ; xk = R.m[0][2] ;
01334    yi = R.m[1][0] ; yj = R.m[1][1] ; yk = R.m[1][2] ;
01335    zi = R.m[2][0] ; zj = R.m[2][1] ; zk = R.m[2][2] ;
01336 
01337    /* normalize column vectors to get unit vectors along each ijk-axis */
01338 
01339    /* normalize i axis */
01340 
01341    val = sqrt( xi*xi + yi*yi + zi*zi ) ;
01342    if( val == 0.0 ) return ;                 /* stupid input */
01343    xi /= val ; yi /= val ; zi /= val ;
01344 
01345    /* normalize j axis */
01346 
01347    val = sqrt( xj*xj + yj*yj + zj*zj ) ;
01348    if( val == 0.0 ) return ;                 /* stupid input */
01349    xj /= val ; yj /= val ; zj /= val ;
01350 
01351    /* orthogonalize j axis to i axis, if needed */
01352 
01353    val = xi*xj + yi*yj + zi*zj ;    /* dot product between i and j */
01354    if( fabs(val) > 1.e-4 ){
01355      xj -= val*xi ; yj -= val*yi ; zj -= val*zi ;
01356      val = sqrt( xj*xj + yj*yj + zj*zj ) ;  /* must renormalize */
01357      if( val == 0.0 ) return ;              /* j was parallel to i? */
01358      xj /= val ; yj /= val ; zj /= val ;
01359    }
01360 
01361    /* normalize k axis; if it is zero, make it the cross product i x j */
01362 
01363    val = sqrt( xk*xk + yk*yk + zk*zk ) ;
01364    if( val == 0.0 ){ xk = yi*zj-zi*yj; yk = zi*xj-zj*xi ; zk=xi*yj-yi*xj ; }
01365    else            { xk /= val ; yk /= val ; zk /= val ; }
01366 
01367    /* orthogonalize k to i */
01368 
01369    val = xi*xk + yi*yk + zi*zk ;    /* dot product between i and k */
01370    if( fabs(val) > 1.e-4 ){
01371      xk -= val*xi ; yk -= val*yi ; zk -= val*zi ;
01372      val = sqrt( xk*xk + yk*yk + zk*zk ) ;
01373      if( val == 0.0 ) return ;      /* bad */
01374      xk /= val ; yk /= val ; zk /= val ;
01375    }
01376 
01377    /* orthogonalize k to j */
01378 
01379    val = xj*xk + yj*yk + zj*zk ;    /* dot product between j and k */
01380    if( fabs(val) > 1.e-4 ){
01381      xk -= val*xj ; yk -= val*yj ; zk -= val*zj ;
01382      val = sqrt( xk*xk + yk*yk + zk*zk ) ;
01383      if( val == 0.0 ) return ;      /* bad */
01384      xk /= val ; yk /= val ; zk /= val ;
01385    }
01386 
01387    Q.m[0][0] = xi ; Q.m[0][1] = xj ; Q.m[0][2] = xk ;
01388    Q.m[1][0] = yi ; Q.m[1][1] = yj ; Q.m[1][2] = yk ;
01389    Q.m[2][0] = zi ; Q.m[2][1] = zj ; Q.m[2][2] = zk ;
01390 
01391    /* at this point, Q is the rotation matrix from the (i,j,k) to (x,y,z) axes */
01392 
01393    detQ = mat33_determ( Q ) ;
01394    if( detQ == 0.0 ) return ; /* shouldn't happen unless user is a DUFIS */
01395 
01396    /* Build and test all possible +1/-1 coordinate permutation matrices P;
01397       then find the P such that the rotation matrix M=PQ is closest to the
01398       identity, in the sense of M having the smallest total rotation angle. */
01399 
01400    /* Despite the formidable looking 6 nested loops, there are
01401       only 3*3*3*2*2*2 = 216 passes, which will run very quickly. */
01402 
01403    vbest = -666.0 ; ibest=pbest=qbest=rbest=1 ; jbest=2 ; kbest=3 ;
01404    for( i=1 ; i <= 3 ; i++ ){     /* i = column number to use for row #1 */
01405     for( j=1 ; j <= 3 ; j++ ){    /* j = column number to use for row #2 */
01406      if( i == j ) continue ;
01407       for( k=1 ; k <= 3 ; k++ ){  /* k = column number to use for row #3 */
01408        if( i == k || j == k ) continue ;
01409        P.m[0][0] = P.m[0][1] = P.m[0][2] =
01410         P.m[1][0] = P.m[1][1] = P.m[1][2] =
01411          P.m[2][0] = P.m[2][1] = P.m[2][2] = 0.0 ;
01412        for( p=-1 ; p <= 1 ; p+=2 ){    /* p,q,r are -1 or +1      */
01413         for( q=-1 ; q <= 1 ; q+=2 ){   /* and go into rows #1,2,3 */
01414          for( r=-1 ; r <= 1 ; r+=2 ){
01415            P.m[0][i-1] = p ; P.m[1][j-1] = q ; P.m[2][k-1] = r ;
01416            detP = mat33_determ(P) ;             /* sign of permutation */
01417            if( detP * detQ <= 0.0 ) continue ;  /* doesn't match sign of Q */
01418            M = mat33_mul(P,Q) ;
01419 
01420            /* angle of M rotation = 2.0*acos(0.5*sqrt(1.0+trace(M)))       */
01421            /* we want largest trace(M) == smallest angle == M nearest to I */
01422 
01423            val = M.m[0][0] + M.m[1][1] + M.m[2][2] ; /* trace */
01424            if( val > vbest ){
01425              vbest = val ;
01426              ibest = i ; jbest = j ; kbest = k ;
01427              pbest = p ; qbest = q ; rbest = r ;
01428            }
01429    }}}}}}
01430 
01431    /* At this point ibest is 1 or 2 or 3; pbest is -1 or +1; etc.
01432 
01433       The matrix P that corresponds is the best permutation approximation
01434       to Q-inverse; that is, P (approximately) takes (x,y,z) coordinates
01435       to the (i,j,k) axes.
01436 
01437       For example, the first row of P (which contains pbest in column ibest)
01438       determines the way the i axis points relative to the anatomical
01439       (x,y,z) axes.  If ibest is 2, then the i axis is along the y axis,
01440       which is direction P2A (if pbest > 0) or A2P (if pbest < 0).
01441 
01442       So, using ibest and pbest, we can assign the output code for
01443       the i axis.  Mutatis mutandis for the j and k axes, of course. */
01444 
01445    switch( ibest*pbest ){
01446      case  1: i = NIFTI_L2R ; break ;
01447      case -1: i = NIFTI_R2L ; break ;
01448      case  2: i = NIFTI_P2A ; break ;
01449      case -2: i = NIFTI_A2P ; break ;
01450      case  3: i = NIFTI_I2S ; break ;
01451      case -3: i = NIFTI_S2I ; break ;
01452    }
01453 
01454    switch( jbest*qbest ){
01455      case  1: j = NIFTI_L2R ; break ;
01456      case -1: j = NIFTI_R2L ; break ;
01457      case  2: j = NIFTI_P2A ; break ;
01458      case -2: j = NIFTI_A2P ; break ;
01459      case  3: j = NIFTI_I2S ; break ;
01460      case -3: j = NIFTI_S2I ; break ;
01461    }
01462 
01463    switch( kbest*rbest ){
01464      case  1: k = NIFTI_L2R ; break ;
01465      case -1: k = NIFTI_R2L ; break ;
01466      case  2: k = NIFTI_P2A ; break ;
01467      case -2: k = NIFTI_A2P ; break ;
01468      case  3: k = NIFTI_I2S ; break ;
01469      case -3: k = NIFTI_S2I ; break ;
01470    }
01471 
01472    *icod = i ; *jcod = j ; *kcod = k ; return ;
01473 }

void mat44_to_quatern (  mat44    R,
float *    qb,
float *    qc,
float *    qd,
float *    qx,
float *    qy,
float *    qz,
float *    dx,
float *    dy,
float *    dz,
float *    qfac
) 
 

Definition at line 917 of file nifti1_io.c.

References a, ASSIF, c, mat33::m, mat44::m, and mat33_polar().

00921 {
00922    double r11,r12,r13 , r21,r22,r23 , r31,r32,r33 ;
00923    double xd,yd,zd , a,b,c,d ;
00924    mat33 P,Q ;
00925 
00926    /* offset outputs are read write out of input matrix  */
00927 
00928    ASSIF(qx,R.m[0][3]) ; ASSIF(qy,R.m[1][3]) ; ASSIF(qz,R.m[2][3]) ;
00929 
00930    /* load 3x3 matrix into local variables */
00931 
00932    r11 = R.m[0][0] ; r12 = R.m[0][1] ; r13 = R.m[0][2] ;
00933    r21 = R.m[1][0] ; r22 = R.m[1][1] ; r23 = R.m[1][2] ;
00934    r31 = R.m[2][0] ; r32 = R.m[2][1] ; r33 = R.m[2][2] ;
00935 
00936    /* compute lengths of each column; these determine grid spacings  */
00937 
00938    xd = sqrt( r11*r11 + r21*r21 + r31*r31 ) ;
00939    yd = sqrt( r12*r12 + r22*r22 + r32*r32 ) ;
00940    zd = sqrt( r13*r13 + r23*r23 + r33*r33 ) ;
00941 
00942    /* if a column length is zero, patch the trouble */
00943 
00944    if( xd == 0.0l ){ r11 = 1.0l ; r21 = r31 = 0.0l ; xd = 1.0l ; }
00945    if( yd == 0.0l ){ r22 = 1.0l ; r12 = r32 = 0.0l ; yd = 1.0l ; }
00946    if( zd == 0.0l ){ r33 = 1.0l ; r13 = r23 = 0.0l ; zd = 1.0l ; }
00947 
00948    /* assign the output lengths */
00949 
00950    ASSIF(dx,xd) ; ASSIF(dy,yd) ; ASSIF(dz,zd) ;
00951 
00952    /* normalize the columns */
00953 
00954    r11 /= xd ; r21 /= xd ; r31 /= xd ;
00955    r12 /= yd ; r22 /= yd ; r32 /= yd ;
00956    r13 /= zd ; r23 /= zd ; r33 /= zd ;
00957 
00958    /* At this point, the matrix has normal columns, but we have to allow
00959       for the fact that the hideous user may not have given us a matrix
00960       with orthogonal columns.
00961 
00962       So, now find the orthogonal matrix closest to the current matrix.
00963 
00964       One reason for using the polar decomposition to get this
00965       orthogonal matrix, rather than just directly orthogonalizing
00966       the columns, is so that inputting the inverse matrix to R
00967       will result in the inverse orthogonal matrix at this point.
00968       If we just orthogonalized the columns, this wouldn't necessarily hold. */
00969 
00970    Q.m[0][0] = r11 ; Q.m[0][1] = r12 ; Q.m[0][2] = r13 ; /* load Q */
00971    Q.m[1][0] = r21 ; Q.m[1][1] = r22 ; Q.m[1][2] = r23 ;
00972    Q.m[2][0] = r31 ; Q.m[2][1] = r32 ; Q.m[2][2] = r33 ;
00973 
00974    P = mat33_polar(Q) ;  /* P is orthog matrix closest to Q */
00975 
00976    r11 = P.m[0][0] ; r12 = P.m[0][1] ; r13 = P.m[0][2] ; /* unload */
00977    r21 = P.m[1][0] ; r22 = P.m[1][1] ; r23 = P.m[1][2] ;
00978    r31 = P.m[2][0] ; r32 = P.m[2][1] ; r33 = P.m[2][2] ;
00979 
00980    /*                            [ r11 r12 r13 ]               */
00981    /* at this point, the matrix  [ r21 r22 r23 ] is orthogonal */
00982    /*                            [ r31 r32 r33 ]               */
00983 
00984    /* compute the determinant to determine if it is proper */
00985 
00986    zd = r11*r22*r33-r11*r32*r23-r21*r12*r33
00987        +r21*r32*r13+r31*r12*r23-r31*r22*r13 ;  /* should be -1 or 1 */
00988 
00989    if( zd > 0 ){             /* proper */
00990      ASSIF(qfac,1.0) ;
00991    } else {                  /* improper ==> flip 3rd column */
00992      ASSIF(qfac,-1.0) ;
00993      r13 = -r13 ; r23 = -r23 ; r33 = -r33 ;
00994    }
00995 
00996    /* now, compute quaternion parameters */
00997 
00998    a = r11 + r22 + r33 + 1.0l ;
00999 
01000    if( a > 0.5l ){                /* simplest case */
01001      a = 0.5l * sqrt(a) ;
01002      b = 0.25l * (r32-r23) / a ;
01003      c = 0.25l * (r13-r31) / a ;
01004      d = 0.25l * (r21-r12) / a ;
01005    } else {                       /* trickier case */
01006      xd = 1.0 + r11 - (r22+r33) ;  /* 4*b*b */
01007      yd = 1.0 + r22 - (r11+r33) ;  /* 4*c*c */
01008      zd = 1.0 + r33 - (r11+r22) ;  /* 4*d*d */
01009      if( xd > 1.0 ){
01010        b = 0.5l * sqrt(xd) ;
01011        c = 0.25l* (r12+r21) / b ;
01012        d = 0.25l* (r13+r31) / b ;
01013        a = 0.25l* (r32-r23) / b ;
01014      } else if( yd > 1.0 ){
01015        c = 0.5l * sqrt(yd) ;
01016        b = 0.25l* (r12+r21) / c ;
01017        d = 0.25l* (r23+r32) / c ;
01018        a = 0.25l* (r13-r31) / c ;
01019      } else {
01020        d = 0.5l * sqrt(zd) ;
01021        b = 0.25l* (r13+r31) / d ;
01022        c = 0.25l* (r23+r32) / d ;
01023        a = 0.25l* (r21-r12) / d ;
01024      }
01025      if( a < 0.0l ){ b=-b ; c=-c ; d=-d; a=-a; }
01026    }
01027 
01028    ASSIF(qb,b) ; ASSIF(qc,c) ; ASSIF(qd,d) ;
01029    return ;
01030 }

int nifti_add_exten_to_list (  nifti1_extension *    new_ext,
nifti1_extension **    list,
int    new_length
)  [static]
 

Referenced by nifti_read_extensions().

int nifti_alloc_NBL_mem (  nifti_image *    nim,
int    nbricks,
nifti_brick_list *    nbl
)  [static]
 

Referenced by nifti_image_load_bricks().

nifti_image* nifti_convert_nhdr2nim (  struct nifti_1_header    nhdr,
char *    fname
) 
 

Definition at line 2035 of file nifti1_io.c.

References nifti_1_header::aux_file, nifti_image::aux_file, nifti_image::byteorder, nifti_1_header::cal_max, nifti_image::cal_max, nifti_1_header::cal_min, nifti_image::cal_min, calloc, nifti_image::datatype, nifti_1_header::datatype, nifti_1_header::descrip, nifti_image::descrip, nifti_image::dim, nifti_1_header::dim, nifti_1_header::dim_info, DIM_INFO_TO_FREQ_DIM, DIM_INFO_TO_PHASE_DIM, DIM_INFO_TO_SLICE_DIM, disp_nifti_1_header(), nifti_image::dt, DT_BINARY, DT_UNKNOWN, nifti_image::du, nifti_image::dv, nifti_image::dw, nifti_image::dx, nifti_image::dy, nifti_image::dz, ERREX, nifti_image::ext_list, FIXED_FLOAT, nifti_image::fname, free, nifti_image::freq_dim, gni_debug, nifti_image::iname, nifti_image::iname_offset, nifti_1_header::intent_code, nifti_image::intent_code, nifti_1_header::intent_name, nifti_image::intent_name, nifti_1_header::intent_p1, nifti_image::intent_p1, nifti_1_header::intent_p2, nifti_image::intent_p2, nifti_1_header::intent_p3, nifti_image::intent_p3, IS_GOOD_FLOAT, mat44::m, mat44_inverse(), nifti_image::nbyper, nifti_image::ndim, nifti_datatype_sizes(), nifti_disp_matrix_orient(), nifti_findimgname(), NIFTI_ONEFILE, nifti_strdup(), nifti_image::nifti_type, NIFTI_VERSION, NIFTI_XFORM_UNKNOWN, nifti_image::nt, nifti_image::nu, nifti_image::num_ext, nifti_image::nv, nifti_image::nvox, nifti_image::nw, nifti_image::nx, nifti_image::ny, nifti_image::nz, nifti_image::phase_dim, nifti_image::pixdim, nifti_1_header::pixdim, nifti_image::qfac, nifti_image::qform_code, nifti_1_header::qform_code, nifti_1_header::qoffset_x, nifti_image::qoffset_x, nifti_1_header::qoffset_y, nifti_image::qoffset_y, nifti_1_header::qoffset_z, nifti_image::qoffset_z, nifti_image::qto_ijk, nifti_image::qto_xyz, nifti_1_header::quatern_b, nifti_image::quatern_b, nifti_1_header::quatern_c, nifti_image::quatern_c, nifti_1_header::quatern_d, nifti_image::quatern_d, quatern_to_mat44(), REVERSE_ORDER, nifti_1_header::scl_inter, nifti_image::scl_inter, nifti_1_header::scl_slope, nifti_image::scl_slope, nifti_image::sform_code, nifti_1_header::sform_code, short_order(), nifti_1_header::sizeof_hdr, nifti_1_header::slice_code, nifti_image::slice_code, nifti_image::slice_dim, nifti_1_header::slice_duration, nifti_image::slice_duration, nifti_1_header::slice_end, nifti_image::slice_end, nifti_1_header::slice_start, nifti_image::slice_start, nifti_1_header::srow_x, nifti_1_header::srow_y, nifti_1_header::srow_z, nifti_image::sto_ijk, nifti_image::sto_xyz, swap_2, swap_4, swap_nifti_header(), nifti_image::swapsize, nifti_image::time_units, nifti_1_header::toffset, nifti_image::toffset, nifti_1_header::vox_offset, nifti_image::xyz_units, XYZT_TO_SPACE, XYZT_TO_TIME, and nifti_1_header::xyzt_units.

Referenced by nifti_copy_nim_info(), nifti_image_read(), and nifti_simple_init_nim().

02036 {
02037   int   ii , doswap , ioff, ndim, nvox ;
02038   int   is_nifti , is_onefile ;
02039   short ss ;
02040   char *iname=NULL;
02041   nifti_image *nim;
02042 
02043    nim = (nifti_image *) calloc( 1 , sizeof(nifti_image) ) ;
02044    if( !nim ) ERREX("failed to allocate nifti image");
02045 
02046    nim->fname=NULL;
02047    nim->iname=NULL;
02048 
02049   /** check if have to swap bytes **/
02050   
02051    doswap = 0 ;                                           /* swap data flag */
02052    ss = nhdr.dim[0] ;
02053    if( ss != 0 ){                            /* check dim[0] for good value */
02054      if( ss < 0 || ss > 7 ){
02055        swap_2(ss) ;
02056        if( ss < 0 || ss > 7 )            ERREX("bad dim[0]") ;
02057        doswap = 1 ;
02058      }
02059    } else {                       /* dim[0] == 0 is illegal, but does occur */
02060      ii = nhdr.sizeof_hdr ;            /* so check sizeof_hdr field instead */
02061 
02062      if( ii != sizeof(nhdr) ){
02063        swap_4(ii) ;
02064        if( ii != sizeof(nhdr) )          ERREX("bad sizeof_hdr") ;
02065        doswap = 1 ;
02066      }
02067    }
02068 
02069    /** determine if this is a NIFTI-1 compliant header **/
02070 
02071    is_nifti = NIFTI_VERSION(nhdr) ;
02072    if( doswap ) {
02073       if ( gni_debug > 2 ) disp_nifti_1_header("-d ni1 pre-swap: ", &nhdr);
02074       swap_nifti_header( &nhdr , is_nifti ) ;
02075    }
02076 
02077    if ( gni_debug > 1 ) disp_nifti_1_header("-d nhdr2nim : ", &nhdr);
02078 
02079    if( nhdr.datatype == DT_BINARY ||
02080        nhdr.datatype == DT_UNKNOWN  )    ERREX("bad datatype") ;
02081 
02082    if( nhdr.dim[1] <= 0 )                ERREX("bad dim[1]") ;
02083 
02084    for( ii=2 ; ii <= 7 ; ii++ )
02085      if( nhdr.dim[ii] <= 0 ) nhdr.dim[ii] = 1 ;  /* fix bad dim[] values */
02086 
02087    /** get number of dimensions (ignoring dim[0] now) **/
02088 
02089    for( ii=7 ; ii >= 2 ; ii-- )            /* loop backwards until we  */
02090      if( nhdr.dim[ii] > 1 ) break ;        /* find a dim bigger than 1 */
02091    ndim = ii ;
02092 
02093    /* set bad grid spacings to 1.0 */
02094 
02095    for( ii=1 ; ii <= 7 ; ii++ ){
02096      if( nhdr.pixdim[ii] == 0.0         ||
02097          !IS_GOOD_FLOAT(nhdr.pixdim[ii])  ) nhdr.pixdim[ii] = 1.0 ;
02098    }
02099 
02100   is_onefile = is_nifti && NIFTI_ONEFILE(nhdr) ;
02101 
02102   if( is_nifti ) nim->nifti_type = (is_onefile) ? 1 : 2 ;
02103   else           nim->nifti_type = 0 ;
02104   
02105   ii = short_order() ;
02106   if( doswap )   nim->byteorder = REVERSE_ORDER(ii) ;
02107   else           nim->byteorder = ii ;
02108   
02109 
02110   /** dimensions of data array **/
02111   
02112   nim->ndim = nim->dim[0] = ndim ;
02113   nim->nx   = nim->dim[1] = nhdr.dim[1]; nvox  = nim->nx;
02114   nim->ny   = nim->dim[2] = nhdr.dim[2]; nvox *= nim->ny;
02115   nim->nz   = nim->dim[3] = nhdr.dim[3]; nvox *= nim->nz;
02116   nim->nt   = nim->dim[4] = nhdr.dim[4]; nvox *= nim->nt;
02117   nim->nu   = nim->dim[5] = nhdr.dim[5]; nvox *= nim->nu;
02118   nim->nv   = nim->dim[6] = nhdr.dim[6]; nvox *= nim->nv;
02119   nim->nw   = nim->dim[7] = nhdr.dim[7]; nvox *= nim->nw; nim->nvox = nvox;
02120   
02121   /** type of data in voxels and how many bytes per voxel */
02122   
02123   nim->datatype = nhdr.datatype ;
02124   
02125   nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ;
02126   if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); }
02127   
02128   /** grid spacings **/
02129   
02130   nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ;
02131   nim->dy = nim->pixdim[2] = nhdr.pixdim[2] ;
02132   nim->dz = nim->pixdim[3] = nhdr.pixdim[3] ;
02133   nim->dt = nim->pixdim[4] = nhdr.pixdim[4] ;
02134   nim->du = nim->pixdim[5] = nhdr.pixdim[5] ;
02135   nim->dv = nim->pixdim[6] = nhdr.pixdim[6] ;
02136   nim->dw = nim->pixdim[7] = nhdr.pixdim[7] ;
02137   
02138   /** compute qto_xyz transformation from pixel indexes (i,j,k) to (x,y,z) **/
02139   
02140   if( !is_nifti || nhdr.qform_code <= 0 ){ /** default transformation **/
02141     
02142     nim->qto_xyz.m[0][0] = nim->dx ;  /* grid spacings */
02143     nim->qto_xyz.m[1][1] = nim->dy ;  /* along diagonal */
02144     nim->qto_xyz.m[2][2] = nim->dz ;
02145     
02146     /* off diagonal is zero */
02147     
02148     nim->qto_xyz.m[0][1]=nim->qto_xyz.m[0][2]=nim->qto_xyz.m[0][3] = 0.0;
02149     nim->qto_xyz.m[1][0]=nim->qto_xyz.m[1][2]=nim->qto_xyz.m[1][3] = 0.0;
02150     nim->qto_xyz.m[2][0]=nim->qto_xyz.m[2][1]=nim->qto_xyz.m[2][3] = 0.0;
02151     
02152     /* last row is always [ 0 0 0 1 ] */
02153     
02154     nim->qto_xyz.m[3][0]=nim->qto_xyz.m[3][1]=nim->qto_xyz.m[3][2] = 0.0;
02155     nim->qto_xyz.m[3][3]= 1.0 ;
02156     
02157     nim->qform_code = NIFTI_XFORM_UNKNOWN ;
02158     
02159   } else {                 /** NIFTI: quaternion-specified transformation **/
02160     
02161     nim->quatern_b = FIXED_FLOAT( nhdr.quatern_b ) ;
02162     nim->quatern_c = FIXED_FLOAT( nhdr.quatern_c ) ;
02163     nim->quatern_d = FIXED_FLOAT( nhdr.quatern_d ) ;
02164     
02165     nim->qoffset_x = FIXED_FLOAT(nhdr.qoffset_x) ;
02166     nim->qoffset_y = FIXED_FLOAT(nhdr.qoffset_y) ;
02167     nim->qoffset_z = FIXED_FLOAT(nhdr.qoffset_z) ;
02168     
02169     nim->qfac = (nhdr.pixdim[0] < 0.0) ? -1.0 : 1.0 ;  /* left-handedness? */
02170     
02171     nim->qto_xyz = quatern_to_mat44(
02172                       nim->quatern_b, nim->quatern_c, nim->quatern_d,
02173                       nim->qoffset_x, nim->qoffset_y, nim->qoffset_z,
02174                       nim->dx       , nim->dy       , nim->dz       ,
02175                       nim->qfac                                      ) ;
02176 
02177     nim->qform_code = nhdr.qform_code ;
02178 
02179     if( gni_debug > 1 )
02180        nifti_disp_matrix_orient("-d qform orientations:\n", nim->qto_xyz);
02181   }
02182   
02183   /** load inverse transformation (x,y,z) -> (i,j,k) **/
02184   
02185   nim->qto_ijk = mat44_inverse( nim->qto_xyz ) ;
02186   
02187   /** load sto_xyz affine transformation, if present **/
02188   
02189   if( !is_nifti || nhdr.sform_code <= 0 ){ /** no sto transformation **/
02190     
02191     nim->sform_code = NIFTI_XFORM_UNKNOWN ;
02192     
02193   } else {                            /** sto transformation from srow_*[] **/
02194     
02195     nim->sto_xyz.m[0][0] = nhdr.srow_x[0] ;
02196     nim->sto_xyz.m[0][1] = nhdr.srow_x[1] ;
02197     nim->sto_xyz.m[0][2] = nhdr.srow_x[2] ;
02198     nim->sto_xyz.m[0][3] = nhdr.srow_x[3] ;
02199     
02200     nim->sto_xyz.m[1][0] = nhdr.srow_y[0] ;
02201     nim->sto_xyz.m[1][1] = nhdr.srow_y[1] ;
02202     nim->sto_xyz.m[1][2] = nhdr.srow_y[2] ;
02203     nim->sto_xyz.m[1][3] = nhdr.srow_y[3] ;
02204     
02205     nim->sto_xyz.m[2][0] = nhdr.srow_z[0] ;
02206     nim->sto_xyz.m[2][1] = nhdr.srow_z[1] ;
02207     nim->sto_xyz.m[2][2] = nhdr.srow_z[2] ;
02208     nim->sto_xyz.m[2][3] = nhdr.srow_z[3] ;
02209     
02210     /* last row is always [ 0 0 0 1 ] */
02211     
02212     nim->sto_xyz.m[3][0]=nim->sto_xyz.m[3][1]=nim->sto_xyz.m[3][2] = 0.0;
02213     nim->sto_xyz.m[3][3]= 1.0 ;
02214     
02215     nim->sto_ijk = mat44_inverse( nim->sto_xyz ) ;
02216     
02217     nim->sform_code = nhdr.sform_code ;
02218 
02219     if( gni_debug > 1 )
02220        nifti_disp_matrix_orient("-d qform orientations:\n", nim->sto_xyz);
02221   }
02222   
02223   /* miscellaneous NIFTI stuff */
02224   
02225   if( is_nifti ){
02226     nim->scl_slope   = FIXED_FLOAT( nhdr.scl_slope ) ;
02227     nim->scl_inter   = FIXED_FLOAT( nhdr.scl_inter ) ;
02228     
02229     nim->intent_code = nhdr.intent_code ;
02230     
02231     nim->intent_p1 = FIXED_FLOAT( nhdr.intent_p1 ) ;
02232     nim->intent_p2 = FIXED_FLOAT( nhdr.intent_p2 ) ;
02233     nim->intent_p3 = FIXED_FLOAT( nhdr.intent_p3 ) ;
02234     
02235     nim->toffset   = FIXED_FLOAT( nhdr.toffset ) ;
02236     
02237     memcpy(nim->intent_name,nhdr.intent_name,15); nim->intent_name[15] = '\0';
02238     
02239     nim->xyz_units  = XYZT_TO_SPACE(nhdr.xyzt_units) ;
02240     nim->time_units = XYZT_TO_TIME (nhdr.xyzt_units) ;
02241     
02242     nim->freq_dim  = DIM_INFO_TO_FREQ_DIM ( nhdr.dim_info ) ;
02243     nim->phase_dim = DIM_INFO_TO_PHASE_DIM( nhdr.dim_info ) ;
02244     nim->slice_dim = DIM_INFO_TO_SLICE_DIM( nhdr.dim_info ) ;
02245     
02246     nim->slice_code     = nhdr.slice_code  ;
02247     nim->slice_start    = nhdr.slice_start ;
02248     nim->slice_end      = nhdr.slice_end   ;
02249     nim->slice_duration = FIXED_FLOAT(nhdr.slice_duration) ;
02250   }
02251   
02252   /* Miscellaneous ANALYZE stuff */
02253   
02254   nim->cal_min = FIXED_FLOAT(nhdr.cal_min) ;
02255   nim->cal_max = FIXED_FLOAT(nhdr.cal_max) ;
02256   
02257   memcpy(nim->descrip ,nhdr.descrip ,79) ; nim->descrip [79] = '\0' ;
02258   memcpy(nim->aux_file,nhdr.aux_file,23) ; nim->aux_file[23] = '\0' ;
02259   
02260    /** will read image data from file 'iname' starting at offset 'ioff' **/
02261 
02262    is_onefile = is_nifti && NIFTI_ONEFILE(nhdr) ;
02263 
02264    if( is_onefile ){
02265      ioff = (int)nhdr.vox_offset ;
02266      if( ioff < sizeof(nhdr) ) ioff = sizeof(nhdr) ;
02267    } else {
02268      ioff = (int)nhdr.vox_offset ;
02269    }
02270    nim->iname_offset = ioff ;
02271 
02272 
02273    /* deal with file names if set */
02274    if (fname!=NULL) {
02275      nim->fname = nifti_strdup(fname);
02276      /* determine name of image, if not already set */
02277      if (nim->iname==NULL) {
02278        if (is_onefile) {
02279          iname = nifti_strdup(nim->fname);
02280        } else {
02281          iname = nifti_findimgname(nim->fname,nim->nifti_type);
02282          if (iname==NULL)  ERREX("bad filename");
02283        }
02284        /* don't free iname, as now nim->iname is using this storage */
02285        nim->iname        = iname ;          /* save image filename */
02286      }
02287    } else { 
02288      nim->fname = NULL;  
02289      nim->iname = NULL; 
02290    }
02291 
02292    /* clear extension fields */
02293    nim->num_ext = 0;
02294    nim->ext_list = NULL;
02295 
02296    return nim;
02297 }

struct nifti_1_header nifti_convert_nim2nhdr (  nifti_image *    nim ) 
 

load the ANALYZE-7.5 generic parts of the header struct *

Definition at line 3119 of file nifti1_io.c.

References nifti_1_header::aux_file, nifti_1_header::bitpix, nifti_1_header::cal_max, nifti_1_header::cal_min, nifti_1_header::datatype, nifti_1_header::descrip, nifti_1_header::dim, nifti_1_header::dim_info, FPS_INTO_DIM_INFO, nifti_1_header::intent_code, nifti_1_header::intent_name, nifti_1_header::intent_p1, nifti_1_header::intent_p2, nifti_1_header::intent_p3, nifti_1_header::magic, nifti_1_header::pixdim, nifti_1_header::qform_code, nifti_1_header::qoffset_x, nifti_1_header::qoffset_y, nifti_1_header::qoffset_z, nifti_1_header::quatern_b, nifti_1_header::quatern_c, nifti_1_header::quatern_d, nifti_1_header::regular, nifti_1_header::scl_inter, nifti_1_header::scl_slope, nifti_1_header::sform_code, nifti_1_header::sizeof_hdr, nifti_1_header::slice_code, nifti_1_header::slice_duration, nifti_1_header::slice_end, nifti_1_header::slice_start, SPACE_TIME_TO_XYZT, nifti_1_header::srow_x, nifti_1_header::srow_y, nifti_1_header::srow_z, nifti_1_header::toffset, nifti_1_header::vox_offset, and nifti_1_header::xyzt_units.

Referenced by nifti_copy_nim_info(), and nifti_image_write_hdr_img2().

03120 {
03121   struct nifti_1_header nhdr;
03122 
03123    memset(&nhdr,0,sizeof(nhdr)) ;  /* zero out header, to be safe */
03124 
03125 
03126    /** load the ANALYZE-7.5 generic parts of the header struct **/
03127 
03128    nhdr.sizeof_hdr = sizeof(nhdr) ;
03129    nhdr.regular    = 'r' ;             /* for some stupid reason */
03130 
03131    nhdr.dim[0] = nim->ndim ;
03132    nhdr.dim[1] = nim->nx ; nhdr.dim[2] = nim->ny ; nhdr.dim[3] = nim->nz ;
03133    nhdr.dim[4] = nim->nt ; nhdr.dim[5] = nim->nu ; nhdr.dim[6] = nim->nv ;
03134    nhdr.dim[7] = nim->nw ;
03135 
03136    nhdr.pixdim[0] = 0.0 ;
03137    nhdr.pixdim[1] = nim->dx ; nhdr.pixdim[2] = nim->dy ;
03138    nhdr.pixdim[3] = nim->dz ; nhdr.pixdim[4] = nim->dt ;
03139    nhdr.pixdim[5] = nim->du ; nhdr.pixdim[6] = nim->dv ;
03140    nhdr.pixdim[7] = nim->dw ;
03141 
03142    nhdr.datatype = nim->datatype ;
03143    nhdr.bitpix   = 8 * nim->nbyper ;
03144 
03145    if( nim->cal_max > nim->cal_min ){
03146      nhdr.cal_max = nim->cal_max ;
03147      nhdr.cal_min = nim->cal_min ;
03148    }
03149 
03150    if( nim->scl_slope != 0.0 ){
03151      nhdr.scl_slope = nim->scl_slope ;
03152      nhdr.scl_inter = nim->scl_inter ;
03153    }
03154 
03155    if( nim->descrip[0] != '\0' ){
03156      memcpy(nhdr.descrip ,nim->descrip ,79) ; nhdr.descrip[79] = '\0' ;
03157    }
03158    if( nim->aux_file[0] != '\0' ){
03159      memcpy(nhdr.aux_file ,nim->aux_file ,23) ; nhdr.aux_file[23] = '\0' ;
03160    }
03161 
03162    /** Load NIFTI specific stuff into the header **/
03163 
03164    if( nim->nifti_type > 0 ){
03165 
03166      if( nim->nifti_type == 1 ) strcpy(nhdr.magic,"n+1") ;   /* 1 file */
03167      else                       strcpy(nhdr.magic,"ni1") ;   /* 2 files */
03168 
03169      /* for nifti files, enforce positive pixdims! */
03170      nhdr.pixdim[1] = fabs(nhdr.pixdim[1]) ; nhdr.pixdim[2] = fabs(nhdr.pixdim[2]) ;
03171      nhdr.pixdim[3] = fabs(nhdr.pixdim[3]) ; nhdr.pixdim[4] = fabs(nhdr.pixdim[4]) ;
03172      nhdr.pixdim[5] = fabs(nhdr.pixdim[5]) ; nhdr.pixdim[6] = fabs(nhdr.pixdim[6]) ;
03173      nhdr.pixdim[7] = fabs(nhdr.pixdim[7]) ;
03174 
03175      nhdr.intent_code = nim->intent_code ;
03176      nhdr.intent_p1   = nim->intent_p1 ;
03177      nhdr.intent_p2   = nim->intent_p2 ;
03178      nhdr.intent_p3   = nim->intent_p3 ;
03179      if( nim->intent_name[0] != '\0' ){
03180        memcpy(nhdr.intent_name,nim->intent_name,15) ;
03181        nhdr.intent_name[15] = '\0' ;
03182      }
03183 
03184      nhdr.vox_offset  = (float) nim->iname_offset ;
03185      nhdr.xyzt_units  = SPACE_TIME_TO_XYZT( nim->xyz_units, nim->time_units ) ;
03186      nhdr.toffset     = nim->toffset ;
03187 
03188      if( nim->qform_code > 0 ){
03189        nhdr.qform_code = nim->qform_code ;
03190        nhdr.quatern_b  = nim->quatern_b ;
03191        nhdr.quatern_c  = nim->quatern_c ;
03192        nhdr.quatern_d  = nim->quatern_d ;
03193        nhdr.qoffset_x  = nim->qoffset_x ;
03194        nhdr.qoffset_y  = nim->qoffset_y ;
03195        nhdr.qoffset_z  = nim->qoffset_z ;
03196        nhdr.pixdim[0]  = (nim->qfac >= 0.0) ? 1.0 : -1.0 ;
03197      }
03198 
03199      if( nim->sform_code > 0 ){
03200        nhdr.sform_code = nim->sform_code ;
03201        nhdr.srow_x[0]  = nim->sto_xyz.m[0][0] ;
03202        nhdr.srow_x[1]  = nim->sto_xyz.m[0][1] ;
03203        nhdr.srow_x[2]  = nim->sto_xyz.m[0][2] ;
03204        nhdr.srow_x[3]  = nim->sto_xyz.m[0][3] ;
03205        nhdr.srow_y[0]  = nim->sto_xyz.m[1][0] ;
03206        nhdr.srow_y[1]  = nim->sto_xyz.m[1][1] ;
03207        nhdr.srow_y[2]  = nim->sto_xyz.m[1][2] ;
03208        nhdr.srow_y[3]  = nim->sto_xyz.m[1][3] ;
03209        nhdr.srow_z[0]  = nim->sto_xyz.m[2][0] ;
03210        nhdr.srow_z[1]  = nim->sto_xyz.m[2][1] ;
03211        nhdr.srow_z[2]  = nim->sto_xyz.m[2][2] ;
03212        nhdr.srow_z[3]  = nim->sto_xyz.m[2][3] ;
03213      }
03214 
03215      nhdr.dim_info = FPS_INTO_DIM_INFO( nim->freq_dim ,
03216                                         nim->phase_dim , nim->slice_dim ) ;
03217      nhdr.slice_code     = nim->slice_code ;
03218      nhdr.slice_start    = nim->slice_start ;
03219      nhdr.slice_end      = nim->slice_end ;
03220      nhdr.slice_duration = nim->slice_duration ;
03221    }
03222 
03223    return nhdr;
03224 }

nifti_image* nifti_copy_nim_info (  nifti_image *    src ) 
 

Definition at line 3446 of file nifti1_io.c.

References calloc, nifti_image::fname, nifti_convert_nhdr2nim(), and nifti_convert_nim2nhdr().

03447 {
03448   struct nifti_1_header nhdr;
03449   nifti_image *dest;
03450   dest = (nifti_image *)calloc(1,sizeof(nifti_image));
03451   nhdr = nifti_convert_nim2nhdr(src);
03452   dest = nifti_convert_nhdr2nim(nhdr,src->fname);
03453   return dest;
03454 }

int nifti_copynsort (  int    nbricks,
int *    blist,
int **    slist,
int **    sindex
)  [static]
 

Referenced by nifti_image_load_bricks().

void nifti_datatype_sizes (  int    datatype,
int *    nbyper,
int *    swapsize
) 
 

Definition at line 807 of file nifti1_io.c.

References ASSIF, DT_COMPLEX128, DT_COMPLEX256, DT_COMPLEX64, DT_FLOAT128, DT_FLOAT32, DT_FLOAT64, DT_INT16, DT_INT32, DT_INT64, DT_INT8, DT_RGB24, DT_UINT16, DT_UINT32, DT_UINT64, and DT_UINT8.

Referenced by nifti_convert_nhdr2nim(), nifti_image_from_ascii(), and nifti_simple_init_nim().

00808 {
00809    int nb=0, ss=0 ;
00810    switch( datatype ){
00811      case DT_INT8:
00812      case DT_UINT8:       nb =  1 ; ss =  0 ; break ;
00813 
00814      case DT_INT16:
00815      case DT_UINT16:      nb =  2 ; ss =  2 ; break ;
00816 
00817      case DT_RGB24:       nb =  3 ; ss =  0 ; break ;
00818 
00819      case DT_INT32:
00820      case DT_UINT32:
00821      case DT_FLOAT32:     nb =  4 ; ss =  4 ; break ;
00822 
00823      case DT_COMPLEX64:   nb =  8 ; ss =  4 ; break ;
00824 
00825      case DT_FLOAT64:
00826      case DT_INT64:
00827      case DT_UINT64:      nb =  8 ; ss =  8 ; break ;
00828 
00829      case DT_FLOAT128:    nb = 16 ; ss = 16 ; break ;
00830 
00831      case DT_COMPLEX128:  nb = 16 ; ss =  8 ; break ;
00832 
00833      case DT_COMPLEX256:  nb = 32 ; ss = 16 ; break ;
00834    }
00835 
00836    ASSIF(nbyper,nb) ; ASSIF(swapsize,ss) ; return ;
00837 }

char* nifti_datatype_string (  int    dt ) 
 

Definition at line 630 of file nifti1_io.c.

References dt, DT_BINARY, DT_COMPLEX128, DT_COMPLEX256, DT_COMPLEX64, DT_FLOAT128, DT_FLOAT32, DT_FLOAT64, DT_INT16, DT_INT32, DT_INT64, DT_INT8, DT_RGB24, DT_UINT16, DT_UINT32, DT_UINT64, DT_UINT8, and DT_UNKNOWN.

Referenced by nifti_image_to_ascii().

00631 {
00632    switch( dt ){
00633      case DT_UNKNOWN:    return "UNKNOWN"    ;
00634      case DT_BINARY:     return "BINARY"     ;
00635      case DT_INT8:       return "INT8"       ;
00636      case DT_UINT8:      return "UINT8"      ;
00637      case DT_INT16:      return "INT16"      ;
00638      case DT_UINT16:     return "UINT16"     ;
00639      case DT_INT32:      return "INT32"      ;
00640      case DT_UINT32:     return "UINT32"     ;
00641      case DT_INT64:      return "INT64"      ;
00642      case DT_UINT64:     return "UINT64"     ;
00643      case DT_FLOAT32:    return "FLOAT32"    ;
00644      case DT_FLOAT64:    return "FLOAT64"    ;
00645      case DT_FLOAT128:   return "FLOAT128"   ;
00646      case DT_COMPLEX64:  return "COMPLEX64"  ;
00647      case DT_COMPLEX128: return "COMPLEX128" ;
00648      case DT_COMPLEX256: return "COMPLEX256" ;
00649      case DT_RGB24:      return "RGB24"      ;
00650    }
00651    return "**ILLEGAL**" ;
00652 }

void nifti_disp_lib_hist (  void    ) 
 

Definition at line 120 of file nifti1_io.c.

References gni_history.

Referenced by main().

00121 {
00122    fputs(gni_history, stdout);
00123 }

void nifti_disp_lib_version (  void    ) 
 

Definition at line 125 of file nifti1_io.c.

References gni_version.

Referenced by main().

00126 {
00127    printf("%s, compiled %s\n", gni_version, __DATE__);
00128 }

int nifti_disp_matrix_orient (  char *    mesg,
mat44    mat
) 
 

Definition at line 600 of file nifti1_io.c.

References i, mat, mat44_to_orientation(), and nifti_orientation_string().

Referenced by nifti_convert_nhdr2nim().

00601 {
00602    int i, j, k;
00603 
00604    if ( mesg ) fputs( mesg, stderr );  /* use stdout? */
00605 
00606    mat44_to_orientation( mat, &i,&j,&k );
00607    if ( i <= 0 || j <= 0 || k <= 0 ) return -1;
00608 
00609    /* so we have good codes */
00610    fprintf(stderr, "  i orientation = '%s'\n"
00611                    "  j orientation = '%s'\n"
00612                    "  k orientation = '%s'\n",
00613                    nifti_orientation_string(i),
00614                    nifti_orientation_string(j),
00615                    nifti_orientation_string(k) );
00616    return 0;
00617 }

char* nifti_find_file_extension (  char *    name ) 
 

check input file(s) for sanity *

Definition at line 1699 of file nifti1_io.c.

References name.

Referenced by nifti_findhdrname(), nifti_makebasename(), and nifti_validfilename().

01700 {
01701    char * ext;
01702    int    len;
01703 
01704    if ( ! name ) return NULL;
01705 
01706    len = strlen(name);
01707    if ( len < 4 ) return NULL;
01708 
01709    ext = name + len - 4;
01710 
01711    if ( (strcmp(ext, ".hdr") == 0) || (strcmp(ext, ".img") == 0) ||
01712         (strcmp(ext, ".nia") == 0) || (strcmp(ext, ".nii") == 0) )
01713       return ext;
01714 
01715 #ifdef HAVE_ZLIB
01716    if ( len < 7 ) return NULL;
01717 
01718    ext = name - 7;
01719    if ( (strcmp(ext, ".hdr.gz") == 0) || (strcmp(ext, ".img.gz") == 0) ||
01720         (strcmp(ext, ".nia.gz") == 0) || (strcmp(ext, ".nii.gz") == 0) )
01721       return ext;
01722 #endif
01723 
01724    return NULL;
01725 }

char* nifti_findhdrname (  char *    fname )  [static]
 

Referenced by is_nifti_file(), and nifti_image_read().

char* nifti_findimgname (  char *    fname,
int    nifti_type
)  [static]
 

Referenced by nifti_convert_nhdr2nim(), and nifti_image_write_hdr_img2().

size_t nifti_get_volsize (  nifti_image *    nim ) 
 

Definition at line 1652 of file nifti1_io.c.

References nifti_image::nbyper, and nifti_image::nvox.

Referenced by nifti_image_load(), and nifti_image_load_prep().

01653 {
01654    return (size_t)(nim->nbyper) * (size_t)(nim->nvox) ; /* total bytes */
01655 }

void nifti_image_free (  nifti_image *    nim ) 
 

Definition at line 2966 of file nifti1_io.c.

References nifti_image::data, nifti_image::fname, free, and nifti_image::iname.

Referenced by nifti_image_from_ascii(), nifti_image_read_bricks(), THD_load_nifti(), THD_open_analyze(), and THD_open_nifti().

02967 {
02968    if( nim == NULL ) return ;
02969    if( nim->fname != NULL ) free(nim->fname) ;
02970    if( nim->iname != NULL ) free(nim->iname) ;
02971    if( nim->data  != NULL ) free(nim->data ) ;
02972    free(nim) ; return ;
02973 }

nifti_image* nifti_image_from_ascii (  char *    str,
int *    bytes_read
) 
 

Definition at line 3899 of file nifti1_io.c.

References nifti_image::byteorder, calloc, nifti_image::datatype, nifti_image::dim, dt, nifti_image::dt, nifti_image::du, nifti_image::dv, nifti_image::dw, nifti_image::dx, nifti_image::dy, nifti_image::dz, nifti_image::fname, nifti_image::iname, mat44::m, mat44_inverse(), nifti_image::nbyper, nifti_image::ndim, nifti_datatype_sizes(), nifti_image_free(), nifti_strdup(), nifti_image::nifti_type, nifti_image::nt, nifti_image::nu, nifti_image::nv, nifti_image::nvox, nifti_image::nw, nifti_image::nx, nifti_image::ny, nz, nifti_image::nz, nifti_image::pixdim, nifti_image::qfac, nifti_image::qform_code, QNUM, nifti_image::qoffset_x, nifti_image::qoffset_y, nifti_image::qoffset_z, QQNUM, QSTR, nifti_image::qto_ijk, nifti_image::qto_xyz, nifti_image::quatern_b, nifti_image::quatern_c, nifti_image::quatern_d, quatern_to_mat44(), nifti_image::sform_code, short_order(), nifti_image::sto_ijk, nifti_image::sto_xyz, nifti_image::swapsize, and unescape_string().

Referenced by nifti_image_read().

03900 {
03901    char lhs[1024] , rhs[1024] ;
03902    int ii , spos, nn , slen ;
03903    nifti_image *nim ;              /* will be output */
03904 
03905    if( str == NULL || *str == '\0' ) return NULL ;  /* bad input!? */
03906 
03907    /* scan for opening string */
03908 
03909    spos = 0 ; slen = strlen(str) ;
03910    ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ;
03911    if( ii == 0 || strcmp(lhs,"<nifti_image") != 0 ) return NULL ;
03912 
03913    /* create empty image struct */
03914 
03915    nim = (nifti_image *) calloc( 1 , sizeof(nifti_image) ) ;
03916 
03917    nim->nx = nim->ny = nim->nz = nim->nt
03918            = nim->nu = nim->nv = nim->nw = 1 ;
03919    nim->dx = nim->dy = nim->dz = nim->dt
03920            = nim->du = nim->dv = nim->dw = nim->qfac = 1.0 ;
03921 
03922    nim->byteorder = short_order() ;
03923 
03924    /* starting at str[spos], scan for "equations" of the form
03925          lhs = 'rhs'
03926       and assign rhs values into the struct component named by lhs */
03927 
03928    while(1){
03929 
03930      while( isspace(str[spos]) ) spos++ ;  /* skip whitespace */
03931      if( str[spos] == '\0' ) break ;       /* end of string? */
03932 
03933      /* get lhs string */
03934 
03935      ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ;
03936      if( ii == 0 || strcmp(lhs,"/>") == 0 ) break ;  /* end of input? */
03937 
03938      /* skip whitespace and the '=' marker */
03939 
03940      while( isspace(str[spos]) || str[spos] == '=' ) spos++ ;
03941      if( str[spos] == '\0' ) break ;       /* end of string? */
03942 
03943      /* if next character is a quote ', copy everything up to next '
03944         otherwise, copy everything up to next nonblank              */
03945 
03946      if( str[spos] == '\'' ){
03947         ii = spos+1 ;
03948         while( str[ii] != '\0' && str[ii] != '\'' ) ii++ ;
03949         nn = ii-spos-1 ; if( nn > 1023 ) nn = 1023 ;
03950         memcpy(rhs,str+spos+1,nn) ; rhs[nn] = '\0' ;
03951         spos = (str[ii] == '\'') ? ii+1 : ii ;
03952      } else {
03953         ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; spos += nn ;
03954         if( ii == 0 ) break ;  /* nothing found? */
03955      }
03956      unescape_string(rhs) ;  /* remove any XML escape sequences */
03957 
03958      /* Now can do the assignment, based on lhs string.
03959         Start with special cases that don't fit the QNUM/QSTR macros. */
03960 
03961      if( strcmp(lhs,"nifti_type") == 0 ){
03962             if( strcmp(rhs,"ANALYZE-7.5") == 0 ) nim->nifti_type = 0 ;
03963        else if( strcmp(rhs,"NIFTI-1+")    == 0 ) nim->nifti_type = 1 ;
03964        else if( strcmp(rhs,"NIFTI-1")     == 0 ) nim->nifti_type = 2 ;
03965        else if( strcmp(rhs,"NIFTI-1A")    == 0 ) nim->nifti_type = 3 ;
03966      }
03967      else if( strcmp(lhs,"header_filename") == 0 ){
03968        nim->fname = nifti_strdup(rhs) ;
03969      }
03970      else if( strcmp(lhs,"image_filename") == 0 ){
03971        nim->iname = nifti_strdup(rhs) ;
03972      }
03973      else if( strcmp(lhs,"sto_xyz_matrix") == 0 ){
03974        sscanf( rhs , "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f" ,
03975                &(nim->sto_xyz.m[0][0]) , &(nim->sto_xyz.m[0][1]) ,
03976                &(nim->sto_xyz.m[0][2]) , &(nim->sto_xyz.m[0][3]) ,
03977                &(nim->sto_xyz.m[1][0]) , &(nim->sto_xyz.m[1][1]) ,
03978                &(nim->sto_xyz.m[1][2]) , &(nim->sto_xyz.m[1][3]) ,
03979                &(nim->sto_xyz.m[2][0]) , &(nim->sto_xyz.m[2][1]) ,
03980                &(nim->sto_xyz.m[2][2]) , &(nim->sto_xyz.m[2][3]) ,
03981                &(nim->sto_xyz.m[3][0]) , &(nim->sto_xyz.m[3][1]) ,
03982                &(nim->sto_xyz.m[3][2]) , &(nim->sto_xyz.m[3][3])  ) ;
03983      }
03984      else if( strcmp(lhs,"byteorder") == 0 ){
03985        if( strcmp(rhs,"MSB_FIRST") == 0 ) nim->byteorder = MSB_FIRST ;
03986        if( strcmp(rhs,"LSB_FIRST") == 0 ) nim->byteorder = LSB_FIRST ;
03987      }
03988      else QQNUM(image_offset,iname_offset) ;
03989      else QNUM(datatype) ;
03990      else QNUM(ndim) ;
03991      else QNUM(nx) ;
03992      else QNUM(ny) ;
03993      else QNUM(nz) ;
03994      else QNUM(nt) ;
03995      else QNUM(nu) ;
03996      else QNUM(nv) ;
03997      else QNUM(nw) ;
03998      else QNUM(dx) ;
03999      else QNUM(dy) ;
04000      else QNUM(dz) ;
04001      else QNUM(dt) ;
04002      else QNUM(du) ;
04003      else QNUM(dv) ;
04004      else QNUM(dw) ;
04005      else QNUM(cal_min) ;
04006      else QNUM(cal_max) ;
04007      else QNUM(scl_slope) ;
04008      else QNUM(scl_inter) ;
04009      else QNUM(intent_code) ;
04010      else QNUM(intent_p1) ;
04011      else QNUM(intent_p2) ;
04012      else QNUM(intent_p3) ;
04013      else QSTR(intent_name,15) ;
04014      else QNUM(toffset) ;
04015      else QNUM(xyz_units) ;
04016      else QNUM(time_units) ;
04017      else QSTR(descrip,79) ;
04018      else QSTR(aux_file,23) ;
04019      else QNUM(qform_code) ;
04020      else QNUM(quatern_b) ;
04021      else QNUM(quatern_c) ;
04022      else QNUM(quatern_d) ;
04023      else QNUM(qoffset_x) ;
04024      else QNUM(qoffset_y) ;
04025      else QNUM(qoffset_z) ;
04026      else QNUM(qfac) ;
04027      else QNUM(sform_code) ;
04028      else QNUM(freq_dim) ;
04029      else QNUM(phase_dim) ;
04030      else QNUM(slice_dim) ;
04031      else QNUM(slice_code) ;
04032      else QNUM(slice_start) ;
04033      else QNUM(slice_end) ;
04034      else QNUM(slice_duration) ;
04035      else QNUM(num_ext) ;
04036 
04037    } /* end of while loop */
04038 
04039    if( bytes_read ) *bytes_read = spos+1;         /* "process" last '\n' */
04040 
04041    /** do miscellaneous checking and cleanup **/
04042 
04043    if( nim->ndim <= 0 ){ nifti_image_free(nim); return NULL; } /** bad! **/
04044 
04045    nifti_datatype_sizes( nim->datatype, &(nim->nbyper), &(nim->swapsize) );
04046    if( nim->nbyper == 0 ){ nifti_image_free(nim); return NULL; } /** bad! **/
04047 
04048    nim->dim[0] = nim->ndim ;
04049    nim->dim[1] = nim->nx ; nim->pixdim[1] = nim->dx ;
04050    nim->dim[2] = nim->ny ; nim->pixdim[2] = nim->dy ;
04051    nim->dim[3] = nim->nz ; nim->pixdim[3] = nim->dz ;
04052    nim->dim[4] = nim->nt ; nim->pixdim[4] = nim->dt ;
04053    nim->dim[5] = nim->nu ; nim->pixdim[5] = nim->du ;
04054    nim->dim[6] = nim->nv ; nim->pixdim[6] = nim->dv ;
04055    nim->dim[7] = nim->nw ; nim->pixdim[7] = nim->dw ;
04056 
04057    nim->nvox =  nim->nx * nim->ny * nim->nz
04058               * nim->nt * nim->nu * nim->nv * nim->nw ;
04059 
04060    if( nim->qform_code > 0 )
04061      nim->qto_xyz = quatern_to_mat44(
04062                       nim->quatern_b, nim->quatern_c, nim->quatern_d,
04063                       nim->qoffset_x, nim->qoffset_y, nim->qoffset_z,
04064                       nim->dx       , nim->dy       , nim->dz       ,
04065                       nim->qfac                                      ) ;
04066    else
04067      nim->qto_xyz = quatern_to_mat44(
04068                       0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 ,
04069                       nim->dx , nim->dy , nim->dz , 0.0 ) ;
04070 
04071 
04072    nim->qto_ijk = mat44_inverse( nim->qto_xyz ) ;
04073 
04074    if( nim->sform_code > 0 )
04075      nim->sto_ijk = mat44_inverse( nim->sto_xyz ) ;
04076 
04077    return nim ;
04078 }

void nifti_image_infodump (  nifti_image *    nim ) 
 

Definition at line 2979 of file nifti1_io.c.

References free, and nifti_image_to_ascii().

Referenced by main(), and nifti_image_read().

02980 {
02981    char *str = nifti_image_to_ascii( nim ) ;
02982    /* stdout -> stderr   2 Dec 2004 [rickr] */
02983    if( str != NULL ){ fputs(str,stderr) ; free(str) ; }
02984    return ;
02985 }

int nifti_image_load (  nifti_image *    nim ) 
 

open image data file *

Definition at line 2821 of file nifti1_io.c.

References calloc, nifti_image::data, free, gni_debug, nifti_get_volsize(), nifti_image_load_prep(), and nifti_read_buffer().

Referenced by nifti_image_read().

02822 {
02823    /* set up data space, open data file and seek, then call nifti_read_buffer */
02824    size_t ntot , ii ;
02825    znzFile fp ;
02826 
02827    fp = nifti_image_load_prep( nim );
02828 
02829    if( fp == NULL ){
02830       if( gni_debug > 0 )
02831          fprintf(stderr,"** nifti_image_load, failed load_prep\n");
02832       return -1;
02833    }
02834 
02835    ntot = nifti_get_volsize(nim);
02836 
02837    /* if the data pointer is already set, use the given space */
02838 
02839    if( nim->data == NULL )
02840    {
02841      nim->data = (void *)calloc(1,ntot) ;  /* create image memory */
02842      if( nim->data == NULL ){
02843         if( gni_debug > 0 )
02844            fprintf(stderr,"** failed to alloc %d bytes for image data\n",
02845                    (int)ntot);
02846         znzclose(fp);
02847         return -1;
02848      }
02849    }
02850 
02851    /* now that everything is set up - do the reading */
02852    ii = nifti_read_buffer(fp,nim->data,ntot,nim);
02853    if( ii < ntot ){
02854       znzclose(fp) ;
02855       free(nim->data) ;
02856       nim->data = NULL ;
02857       return -1 ;  /* errors were printed in nifti_read_buffer() */
02858    }
02859 
02860    znzclose( fp ) ;
02861 
02862    return 0 ;
02863 }

int nifti_image_load_bricks (  nifti_image *    nim,
int    nbricks,
int *    blist,
nifti_brick_list *    NBL
) 
 

Definition at line 260 of file nifti1_io.c.

References free, free_NBL(), gni_debug, nifti_brick_list::nbricks, nifti_alloc_NBL_mem(), nifti_copynsort(), nifti_image_load_prep(), nifti_load_NBL_bricks(), and valid_nifti_brick_list().

Referenced by nifti_image_read_bricks().

00262 {
00263    int     * slist = NULL, * sindex = NULL, rv;
00264    znzFile   fp;
00265 
00266    /* we can have blist == NULL */
00267    if( !nim || !NBL ){
00268       fprintf(stderr,"** nifti_image_load_bricks, bad params (%p,%p)\n",
00269               nim, NBL);
00270       return -1;
00271    }
00272 
00273    if( blist && ! valid_nifti_brick_list( nim, nbricks, blist, 1 ) )
00274       return -1;
00275 
00276    /* for efficiency, let's read the file in order */
00277    if( blist && nifti_copynsort( nbricks, blist, &slist, &sindex ) != 0 )
00278       return -1;
00279 
00280    /* open the file and position the FILE pointer */
00281    fp = nifti_image_load_prep( nim );
00282    if( !fp ){
00283       if( gni_debug > 0 )
00284          fprintf(stderr,"** nifti_image_load_bricks, failed load_prep\n");
00285       if( blist ){ free(slist); free(sindex); }
00286       return -1;
00287    }
00288 
00289    /* this will flag to allocate defaults */
00290    if( !blist ) nbricks = 0;
00291    if( nifti_alloc_NBL_mem( nim, nbricks, NBL ) != 0 ){
00292       if( blist ){ free(slist); free(sindex); }
00293       znzclose(fp);
00294       return -1;
00295    }
00296 
00297    rv = nifti_load_NBL_bricks(nim, slist, sindex, NBL, fp);
00298 
00299    if( rv != 0 ){
00300       free_NBL( NBL );  /* failure! */
00301       NBL->nbricks = 0; /* repetative, but clear */
00302    }
00303 
00304    if( slist ){ free(slist); free(sindex); }
00305 
00306    znzclose(fp);
00307 
00308    return NBL->nbricks;
00309 }

znzFile nifti_image_load_prep (  nifti_image *    nim )  [static]
 

Referenced by nifti_image_load(), and nifti_image_load_bricks().

znzFile nifti_image_open (  char *    hname,
char *    opts,
nifti_image **    nim
)  [static]
 

nifti_image* nifti_image_read (  char *    hname,
int    read_data
) 
 

open image data file *

Definition at line 2349 of file nifti1_io.c.

References calloc, free, get_filesize(), gni_debug, ilen, LNI_FERR, nifti_convert_nhdr2nim(), nifti_findhdrname(), nifti_image_from_ascii(), nifti_image_infodump(), nifti_image_load(), nifti_is_gzfile(), and nifti_read_extensions().

Referenced by main(), nifti_image_open(), nifti_image_read_bricks(), THD_load_nifti(), THD_open_analyze(), and THD_open_nifti().

02350 {
02351    struct nifti_1_header  nhdr ;
02352    nifti_image           *nim ;
02353    znzFile                fp ;
02354    int                    ii , ilen;
02355    char                   fname[] = { "nifti_image_read" };
02356    char                   buf[16] , *workingname=NULL;
02357    
02358    /* determine file name to use for header */
02359    workingname = nifti_findhdrname(hname);
02360    if( workingname == NULL ){
02361       if( gni_debug > 0 )
02362          LNI_FERR(fname,"failed to find header file for", hname);
02363       return NULL;  /* check return */
02364    } else if( gni_debug > 1 )
02365       fprintf(stderr,"-d %s: found header filename '%s'\n",fname,workingname);
02366 
02367    fp = znzopen(workingname,"rb",1);
02368    if( znz_isnull(fp) ){
02369       if( gni_debug > 0 )
02370          LNI_FERR(fname,"failed to open header file",workingname);
02371       free(workingname);
02372       return NULL;
02373    }
02374 
02375    /** test if header file starts with ASCII string "<nifti_image";
02376        if so, read the dataset that special way and return now
02377        (this is NOT part of the NIFTI-1 standard!)                **/
02378 
02379    ii = znzread( buf , 1 , 12 , fp ) ;
02380    if( ii < 12 ){
02381       znzclose( fp ) ;
02382       if( gni_debug > 0 )
02383          LNI_FERR(fname,"short header read",workingname);
02384          free(workingname);
02385          return NULL;
02386    }
02387 
02388    znzrewind(fp) ;
02389    buf[12] = '\0' ;
02390    if( strcmp(buf,"<nifti_image") == 0 ){  /* have an ASCII header! */
02391      int slen, txt_size;
02392      char  *sbuf ;
02393      if( nifti_is_gzfile(workingname) ){
02394         LNI_FERR(fname, "compressed file with negative offset", workingname);
02395         free(workingname);
02396         return NULL;
02397      }
02398      slen = get_filesize( workingname ) ;
02399 
02400      if( gni_debug > 1 )
02401         fprintf(stderr,"-d %s: have ASCII image of size %d\n",fname,slen);
02402 
02403      if( slen > 65530 ) slen = 65530 ;
02404      sbuf = (char *)calloc(sizeof(char),slen+1) ;
02405      znzread( sbuf , 1 , slen , fp ) ;
02406      nim = nifti_image_from_ascii( sbuf, &txt_size ) ; free( sbuf ) ;
02407      if( nim == NULL ){
02408         LNI_FERR(fname,"failed nifti_image_from_ascii()",workingname);
02409         free(workingname);
02410         return NULL;
02411      }
02412      nim->nifti_type = 3 ;
02413 
02414      /* read extensions (but reposition file pointer, first */
02415      znzseek(fp, txt_size, SEEK_SET);
02416      (void) nifti_read_extensions(nim, fp);
02417 
02418      znzclose( fp ) ;
02419 
02420      nim->iname_offset = -1 ;  /* now check from the end of the file */
02421 
02422      if( read_data ) ii = nifti_image_load( nim ) ;
02423      else            nim->data = NULL ;
02424 
02425      free(workingname);
02426 
02427      /* check for nifti_image_load() failure, maybe bail out */
02428      if( read_data && ii != 0 ){
02429         if( gni_debug > 1 )
02430            fprintf(stderr,"-d failed image_load, free nifti image struct\n");
02431         free(nim);
02432         return NULL;
02433      }
02434 
02435      return nim ;
02436    }
02437 
02438    /**================ Normal case:  read binary header ===================*/
02439 
02440    ii = znzread( &nhdr , 1 , sizeof(nhdr) , fp ) ;       /* read the thing */
02441 
02442 /* rcr - test header struct for consistency here? */
02443 
02444    /* keep the file open for a while so we can check for extensions
02445     * after nifti_convert_nhdr2nim() */
02446 
02447    if( ii < sizeof(nhdr) ){
02448       if( gni_debug > 0 ){
02449          LNI_FERR(fname,"bad binary header read for file", workingname);
02450          fprintf(stderr,"  - read %d of %d bytes\n",ii, (int)sizeof(nhdr));
02451       }
02452       free(workingname);
02453       return NULL;
02454    }
02455 
02456    /*=== create output image struct and set it up ===*/
02457 
02458    /** convert all nhdr fields to nifti_image fields **/
02459    nim = nifti_convert_nhdr2nim(nhdr,workingname);
02460 
02461    if( nim == NULL ){
02462       znzclose( fp ) ;                                   /* close the file */
02463       if( gni_debug > 0 )
02464          LNI_FERR(fname,"failed to create nifti image from header",workingname);
02465       free(workingname); /* had to save this for debug message */
02466       return NULL;
02467    }
02468 
02469    if( gni_debug > 1 ){
02470       fprintf(stderr,"+d nifti_image_read(), have nifti image:\n");
02471       nifti_image_infodump(nim);
02472    }
02473 
02474    /* check for extensions, let us just ignore any errors here */
02475    (void) nifti_read_extensions(nim, fp);
02476 
02477    znzclose( fp ) ;                                      /* close the file */
02478    free(workingname);
02479    /* check that the data file is OK */
02480    if (!nifti_is_gzfile(nim->iname)) {
02481      /* can't use get_filesize on a compressed file   :( */
02482      ilen = get_filesize(nim->iname) ;
02483      if( ilen <= nim->iname_offset ){
02484         fprintf(stderr,"ERROR %s: file size less than offset (%d < %d)\n",
02485                 fname,ilen,nim->iname_offset);
02486         free(nim);
02487         return NULL;
02488      }
02489    }
02490 
02491    /** read the data if desired, then bug out **/
02492    if( read_data ) ii = nifti_image_load( nim ) ;
02493    else            nim->data = NULL ;
02494 
02495    return nim ;
02496 }

nifti_image* nifti_image_read_bricks (  char *    hname,
int    nbricks,
int *    blist,
nifti_brick_list *    NBL
) 
 

Definition at line 178 of file nifti1_io.c.

References nifti_image_free(), nifti_image_load_bricks(), nifti_image_read(), and update_nifti_image_for_brick_list().

00180 {
00181    nifti_image * nim;
00182 
00183    if( !hname || !NBL ){
00184       fprintf(stderr,"** nifti_image_read_bricks: bad params (%p,%p)\n",
00185               hname, NBL);
00186       return NULL;
00187    }
00188 
00189    if( blist && nbricks <= 0 ){
00190       fprintf(stderr,"** nifti_image_read_bricks: bad nbricks, %d\n", nbricks);
00191       return NULL;
00192    }
00193 
00194    nim = nifti_image_read(hname, 0);  /* read header, but not data */
00195 
00196    if( !nim ) return NULL;   /* errors were already printed */
00197 
00198    /* if we fail, free image and return */
00199    if( nifti_image_load_bricks(nim, nbricks, blist, NBL) <= 0 ){
00200       nifti_image_free(nim);
00201       return NULL;
00202    }
00203 
00204    if( blist ) update_nifti_image_for_brick_list(nim, nbricks);
00205 
00206    return nim;
00207 }

char* nifti_image_to_ascii (  nifti_image *    nim ) 
 

Definition at line 3631 of file nifti1_io.c.

References nifti_image::aux_file, nifti_image::byteorder, nifti_image::cal_max, nifti_image::cal_min, calloc, nifti_image::datatype, nifti_image::descrip, nifti_image::dt, nifti_image::du, nifti_image::dv, nifti_image::dw, nifti_image::dx, nifti_image::dy, nifti_image::dz, escapize_string(), nifti_image::fname, free, nifti_image::freq_dim, i, nifti_image::iname, nifti_image::iname_offset, nifti_image::intent_code, nifti_image::intent_name, nifti_image::intent_p1, nifti_image::intent_p2, nifti_image::intent_p3, mat44::m, mat44_to_orientation(), nifti_image::nbyper, nifti_image::ndim, nifti_datatype_string(), nifti_intent_string(), nifti_orientation_string(), nifti_slice_string(), nifti_image::nifti_type, nifti_units_string(), nifti_xform_string(), nifti_image::nt, nifti_image::nu, nifti_image::num_ext, nifti_image::nv, nifti_image::nvox, nifti_image::nw, nifti_image::nx, nifti_image::ny, nifti_image::nz, nifti_image::phase_dim, nifti_image::qfac, nifti_image::qform_code, nifti_image::qoffset_x, nifti_image::qoffset_y, nifti_image::qoffset_z, nifti_image::qto_ijk, nifti_image::qto_xyz, nifti_image::quatern_b, nifti_image::quatern_c, nifti_image::quatern_d, realloc, nifti_image::scl_inter, nifti_image::scl_slope, nifti_image::sform_code, nifti_image::slice_code, nifti_image::slice_dim, nifti_image::slice_duration, nifti_image::slice_end, nifti_image::slice_start, nifti_image::sto_ijk, nifti_image::sto_xyz, nifti_image::time_units, nifti_image::toffset, and nifti_image::xyz_units.

Referenced by nifti_image_infodump(), and nifti_image_write_hdr_img2().

03632 {
03633    char *buf , *ebuf ; int nbuf ;
03634 
03635    if( nim == NULL ) return NULL ;   /* stupid caller */
03636 
03637    buf = (char *)calloc(1,65534); nbuf = 0;  /* longer than needed, to be safe */
03638 
03639    sprintf( buf , "<nifti_image\n" ) ;   /* XML-ish opener */
03640 
03641    sprintf( buf+strlen(buf) , "  nifti_type = '%s'\n" ,
03642               (nim->nifti_type == 1) ? "NIFTI-1+"
03643              :(nim->nifti_type == 2) ? "NIFTI-1"
03644              :(nim->nifti_type == 3) ? "NIFTI-1A"
03645              :                         "ANALYZE-7.5" ) ;
03646 
03647    /** Strings that we don't control (filenames, etc.) that might
03648        contain "weird" characters (like quotes) are "escaped":
03649        - A few special characters are replaced by XML-style escapes, using
03650          the function escapize_string().
03651        - On input, function unescape_string() reverses this process.
03652        - The result is that the NIFTI ASCII-format header is XML-compliant. **/
03653 
03654    ebuf = escapize_string(nim->fname) ;
03655    sprintf( buf+strlen(buf) , "  header_filename = %s\n",ebuf); free(ebuf);
03656 
03657    ebuf = escapize_string(nim->iname) ;
03658    sprintf( buf+strlen(buf) , "  image_filename = %s\n", ebuf); free(ebuf);
03659 
03660    sprintf( buf+strlen(buf) , "  image_offset = '%d'\n" , nim->iname_offset );
03661 
03662                        sprintf( buf+strlen(buf), "  ndim = '%d'\n", nim->ndim);
03663                        sprintf( buf+strlen(buf), "  nx = '%d'\n",   nim->nx  );
03664    if( nim->ndim > 1 ) sprintf( buf+strlen(buf), "  ny = '%d'\n",   nim->ny  );
03665    if( nim->ndim > 2 ) sprintf( buf+strlen(buf), "  nz = '%d'\n",   nim->nz  );
03666    if( nim->ndim > 3 ) sprintf( buf+strlen(buf), "  nt = '%d'\n",   nim->nt  );
03667    if( nim->ndim > 4 ) sprintf( buf+strlen(buf), "  nu = '%d'\n",   nim->nu  );
03668    if( nim->ndim > 5 ) sprintf( buf+strlen(buf), "  nv = '%d'\n",   nim->nv  );
03669    if( nim->ndim > 6 ) sprintf( buf+strlen(buf), "  nw = '%d'\n",   nim->nw  );
03670                        sprintf( buf+strlen(buf), "  dx = '%g'\n",   nim->dx  );
03671    if( nim->ndim > 1 ) sprintf( buf+strlen(buf), "  dy = '%g'\n",   nim->dy  );
03672    if( nim->ndim > 2 ) sprintf( buf+strlen(buf), "  dz = '%g'\n",   nim->dz  );
03673    if( nim->ndim > 3 ) sprintf( buf+strlen(buf), "  dt = '%g'\n",   nim->dt  );
03674    if( nim->ndim > 4 ) sprintf( buf+strlen(buf), "  du = '%g'\n",   nim->du  );
03675    if( nim->ndim > 5 ) sprintf( buf+strlen(buf), "  dv = '%g'\n",   nim->dv  );
03676    if( nim->ndim > 6 ) sprintf( buf+strlen(buf), "  dw = '%g'\n",   nim->dw  );
03677 
03678    sprintf( buf+strlen(buf) , "  datatype = '%d'\n" , nim->datatype ) ;
03679    sprintf( buf+strlen(buf) , "  datatype_name = '%s'\n" ,
03680                               nifti_datatype_string(nim->datatype) ) ;
03681 
03682    sprintf( buf+strlen(buf) , "  nvox = '%d'\n" , nim->nvox ) ;
03683    sprintf( buf+strlen(buf) , "  nbyper = '%d'\n" , nim->nbyper ) ;
03684 
03685    sprintf( buf+strlen(buf) , "  byteorder = '%s'\n" ,
03686             (nim->byteorder==MSB_FIRST) ? "MSB_FIRST" : "LSB_FIRST" ) ;
03687 
03688    if( nim->cal_min < nim->cal_max ){
03689      sprintf( buf+strlen(buf) , "  cal_min = '%g'\n", nim->cal_min ) ;
03690      sprintf( buf+strlen(buf) , "  cal_max = '%g'\n", nim->cal_max ) ;
03691    }
03692 
03693    if( nim->scl_slope != 0.0 ){
03694      sprintf( buf+strlen(buf) , "  scl_slope = '%g'\n" , nim->scl_slope ) ;
03695      sprintf( buf+strlen(buf) , "  scl_inter = '%g'\n" , nim->scl_inter ) ;
03696    }
03697 
03698    if( nim->intent_code > 0 ){
03699      sprintf( buf+strlen(buf) , "  intent_code = '%d'\n", nim->intent_code ) ;
03700      sprintf( buf+strlen(buf) , "  intent_code_name = '%s'\n" ,
03701                                 nifti_intent_string(nim->intent_code) ) ;
03702      sprintf( buf+strlen(buf) , "  intent_p1 = '%g'\n" , nim->intent_p1 ) ;
03703      sprintf( buf+strlen(buf) , "  intent_p2 = '%g'\n" , nim->intent_p2 ) ;
03704      sprintf( buf+strlen(buf) , "  intent_p3 = '%g'\n" , nim->intent_p3 ) ;
03705 
03706      if( nim->intent_name[0] != '\0' ){
03707        ebuf = escapize_string(nim->intent_name) ;
03708        sprintf( buf+strlen(buf) , "  intent_name = %s\n",ebuf) ;
03709        free(ebuf) ;
03710      }
03711    }
03712 
03713    if( nim->toffset != 0.0 )
03714      sprintf( buf+strlen(buf) , "  toffset = '%g'\n",nim->toffset ) ;
03715 
03716    if( nim->xyz_units > 0 )
03717      sprintf( buf+strlen(buf) ,
03718               "  xyz_units = '%d'\n"
03719               "  xyz_units_name = '%s'\n" ,
03720               nim->xyz_units , nifti_units_string(nim->xyz_units) ) ;
03721 
03722    if( nim->time_units > 0 )
03723      sprintf( buf+strlen(buf) ,
03724               "  time_units = '%d'\n"
03725               "  time_units_name = '%s'\n" ,
03726               nim->time_units , nifti_units_string(nim->time_units) ) ;
03727 
03728    if( nim->freq_dim > 0 )
03729      sprintf( buf+strlen(buf) , "  freq_dim = '%d'\n",nim->freq_dim ) ;
03730    if( nim->phase_dim > 0 )
03731      sprintf( buf+strlen(buf) , "  phase_dim = '%d'\n",nim->phase_dim ) ;
03732    if( nim->slice_dim > 0 )
03733      sprintf( buf+strlen(buf) , "  slice_dim = '%d'\n",nim->slice_dim ) ;
03734    if( nim->slice_code > 0 )
03735      sprintf( buf+strlen(buf) ,
03736               "  slice_code = '%d'\n"
03737               "  slice_code_name = '%s'\n" ,
03738               nim->slice_code , nifti_slice_string(nim->slice_code) ) ;
03739    if( nim->slice_start >= 0 && nim->slice_end > nim->slice_start )
03740      sprintf( buf+strlen(buf) ,
03741               "  slice_start = '%d'\n"
03742               "  slice_end = '%d'\n"  , nim->slice_start , nim->slice_end ) ;
03743    if( nim->slice_duration != 0.0 )
03744      sprintf( buf+strlen(buf) , "  slice_duration = '%g'\n",
03745               nim->slice_duration ) ;
03746 
03747    if( nim->descrip[0] != '\0' ){
03748      ebuf = escapize_string(nim->descrip) ;
03749      sprintf( buf+strlen(buf) , "  descrip = %s\n",ebuf) ;
03750      free(ebuf) ;
03751    }
03752 
03753    if( nim->aux_file[0] != '\0' ){
03754      ebuf = escapize_string(nim->aux_file) ;
03755      sprintf( buf+strlen(buf) , "  aux_file = %s\n",ebuf) ;
03756      free(ebuf) ;
03757    }
03758 
03759    if( nim->qform_code > 0 ){
03760      int i,j,k ;
03761 
03762      sprintf( buf+strlen(buf) ,
03763               "  qform_code = '%d'\n"
03764               "  qform_code_name = '%s'\n"
03765      "  qto_xyz_matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" ,
03766          nim->qform_code      , nifti_xform_string(nim->qform_code) ,
03767          nim->qto_xyz.m[0][0] , nim->qto_xyz.m[0][1] ,
03768          nim->qto_xyz.m[0][2] , nim->qto_xyz.m[0][3] ,
03769          nim->qto_xyz.m[1][0] , nim->qto_xyz.m[1][1] ,
03770          nim->qto_xyz.m[1][2] , nim->qto_xyz.m[1][3] ,
03771          nim->qto_xyz.m[2][0] , nim->qto_xyz.m[2][1] ,
03772          nim->qto_xyz.m[2][2] , nim->qto_xyz.m[2][3] ,
03773          nim->qto_xyz.m[3][0] , nim->qto_xyz.m[3][1] ,
03774          nim->qto_xyz.m[3][2] , nim->qto_xyz.m[3][3]  ) ;
03775 
03776      sprintf( buf+strlen(buf) ,
03777      "  qto_ijk_matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" ,
03778          nim->qto_ijk.m[0][0] , nim->qto_ijk.m[0][1] ,
03779          nim->qto_ijk.m[0][2] , nim->qto_ijk.m[0][3] ,
03780          nim->qto_ijk.m[1][0] , nim->qto_ijk.m[1][1] ,
03781          nim->qto_ijk.m[1][2] , nim->qto_ijk.m[1][3] ,
03782          nim->qto_ijk.m[2][0] , nim->qto_ijk.m[2][1] ,
03783          nim->qto_ijk.m[2][2] , nim->qto_ijk.m[2][3] ,
03784          nim->qto_ijk.m[3][0] , nim->qto_ijk.m[3][1] ,
03785          nim->qto_ijk.m[3][2] , nim->qto_ijk.m[3][3]  ) ;
03786 
03787      sprintf( buf+strlen(buf) ,
03788               "  quatern_b = '%g'\n"
03789               "  quatern_c = '%g'\n"
03790               "  quatern_d = '%g'\n"
03791               "  qoffset_x = '%g'\n"
03792               "  qoffset_y = '%g'\n"
03793               "  qoffset_z = '%g'\n"
03794               "  qfac = '%g'\n" ,
03795          nim->quatern_b , nim->quatern_c , nim->quatern_d ,
03796          nim->qoffset_x , nim->qoffset_y , nim->qoffset_z , nim->qfac ) ;
03797 
03798      mat44_to_orientation( nim->qto_xyz , &i,&j,&k ) ;
03799      if( i > 0 && j > 0 && k > 0 )
03800        sprintf( buf+strlen(buf) ,
03801                 "  qform_i_orientation = '%s'\n"
03802                 "  qform_j_orientation = '%s'\n"
03803                 "  qform_k_orientation = '%s'\n" ,
03804                 nifti_orientation_string(i) ,
03805                 nifti_orientation_string(j) ,
03806                 nifti_orientation_string(k)  ) ;
03807    }
03808 
03809    if( nim->sform_code > 0 ){
03810      int i,j,k ;
03811 
03812      sprintf( buf+strlen(buf) ,
03813               "  sform_code = '%d'\n"
03814               "  sform_code_name = '%s'\n"
03815      "  sto_xyz_matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" ,
03816          nim->sform_code      , nifti_xform_string(nim->sform_code) ,
03817          nim->sto_xyz.m[0][0] , nim->sto_xyz.m[0][1] ,
03818          nim->sto_xyz.m[0][2] , nim->sto_xyz.m[0][3] ,
03819          nim->sto_xyz.m[1][0] , nim->sto_xyz.m[1][1] ,
03820          nim->sto_xyz.m[1][2] , nim->sto_xyz.m[1][3] ,
03821          nim->sto_xyz.m[2][0] , nim->sto_xyz.m[2][1] ,
03822          nim->sto_xyz.m[2][2] , nim->sto_xyz.m[2][3] ,
03823          nim->sto_xyz.m[3][0] , nim->sto_xyz.m[3][1] ,
03824          nim->sto_xyz.m[3][2] , nim->sto_xyz.m[3][3]  ) ;
03825 
03826      sprintf( buf+strlen(buf) ,
03827      "  sto_ijk matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" ,
03828          nim->sto_ijk.m[0][0] , nim->sto_ijk.m[0][1] ,
03829          nim->sto_ijk.m[0][2] , nim->sto_ijk.m[0][3] ,
03830          nim->sto_ijk.m[1][0] , nim->sto_ijk.m[1][1] ,
03831          nim->sto_ijk.m[1][2] , nim->sto_ijk.m[1][3] ,
03832          nim->sto_ijk.m[2][0] , nim->sto_ijk.m[2][1] ,
03833          nim->sto_ijk.m[2][2] , nim->sto_ijk.m[2][3] ,
03834          nim->sto_ijk.m[3][0] , nim->sto_ijk.m[3][1] ,
03835          nim->sto_ijk.m[3][2] , nim->sto_ijk.m[3][3]  ) ;
03836 
03837      mat44_to_orientation( nim->sto_xyz , &i,&j,&k ) ;
03838      if( i > 0 && j > 0 && k > 0 )
03839        sprintf( buf+strlen(buf) ,
03840                 "  sform_i_orientation = '%s'\n"
03841                 "  sform_j_orientation = '%s'\n"
03842                 "  sform_k_orientation = '%s'\n" ,
03843                 nifti_orientation_string(i) ,
03844                 nifti_orientation_string(j) ,
03845                 nifti_orientation_string(k)  ) ;
03846    }
03847 
03848    sprintf( buf+strlen(buf) , "  num_ext = '%d'\n", nim->num_ext ) ;
03849 
03850    sprintf( buf+strlen(buf) , "/>\n" ) ;   /* XML-ish closer */
03851 
03852    nbuf = strlen(buf) ;
03853    buf  = (char *)realloc((void *)buf, nbuf+1); /* cut back to proper length */
03854    return buf ;
03855 }

void nifti_image_unload (  nifti_image *    nim ) 
 

Definition at line 2954 of file nifti1_io.c.

References nifti_image::data, and free.

02955 {
02956    if( nim != NULL && nim->data != NULL ){
02957      free(nim->data) ; nim->data = NULL ;
02958    }
02959    return ;
02960 }

void nifti_image_write (  nifti_image *    nim ) 
 

Definition at line 3436 of file nifti1_io.c.

References nifti_image_write_hdr_img().

Referenced by main().

03437 {
03438   nifti_image_write_hdr_img(nim,1,"wb");
03439 }

znzFile nifti_image_write_hdr_img (  nifti_image *    nim,
int    write_data,
char *    opts
)  [static]
 

Referenced by nifti_image_write().

znzFile nifti_image_write_hdr_img2 (  nifti_image *    nim,
int    write_data,
char *    opts,
znzFile *    imgfile
)  [static]
 

Referenced by nifti_image_write_hdr_img().

char* nifti_intent_string (  int    ii ) 
 

Definition at line 724 of file nifti1_io.c.

References NIFTI_INTENT_BETA, NIFTI_INTENT_BINOM, NIFTI_INTENT_CHI, NIFTI_INTENT_CHISQ, NIFTI_INTENT_CHISQ_NONC, NIFTI_INTENT_CORREL, NIFTI_INTENT_DIMLESS, NIFTI_INTENT_DISPVECT, NIFTI_INTENT_ESTIMATE, NIFTI_INTENT_EXTVAL, NIFTI_INTENT_FTEST, NIFTI_INTENT_FTEST_NONC, NIFTI_INTENT_GAMMA, NIFTI_INTENT_GENMATRIX, NIFTI_INTENT_INVGAUSS, NIFTI_INTENT_LABEL, NIFTI_INTENT_LAPLACE, NIFTI_INTENT_LOG10PVAL, NIFTI_INTENT_LOGISTIC, NIFTI_INTENT_LOGPVAL, NIFTI_INTENT_NEURONAME, NIFTI_INTENT_NORMAL, NIFTI_INTENT_POINTSET, NIFTI_INTENT_POISSON, NIFTI_INTENT_PVAL, NIFTI_INTENT_QUATERNION, NIFTI_INTENT_SYMMATRIX, NIFTI_INTENT_TRIANGLE, NIFTI_INTENT_TTEST, NIFTI_INTENT_TTEST_NONC, NIFTI_INTENT_UNIFORM, NIFTI_INTENT_VECTOR, NIFTI_INTENT_WEIBULL, and NIFTI_INTENT_ZSCORE.

Referenced by nifti_image_to_ascii().

00725 {
00726    switch( ii ){
00727      case NIFTI_INTENT_CORREL:     return "Correlation statistic" ;
00728      case NIFTI_INTENT_TTEST:      return "T-statistic" ;
00729      case NIFTI_INTENT_FTEST:      return "F-statistic" ;
00730      case NIFTI_INTENT_ZSCORE:     return "Z-score"     ;
00731      case NIFTI_INTENT_CHISQ:      return "Chi-squared distribution" ;
00732      case NIFTI_INTENT_BETA:       return "Beta distribution" ;
00733      case NIFTI_INTENT_BINOM:      return "Binomial distribution" ;
00734      case NIFTI_INTENT_GAMMA:      return "Gamma distribution" ;
00735      case NIFTI_INTENT_POISSON:    return "Poisson distribution" ;
00736      case NIFTI_INTENT_NORMAL:     return "Normal distribution" ;
00737      case NIFTI_INTENT_FTEST_NONC: return "F-statistic noncentral" ;
00738      case NIFTI_INTENT_CHISQ_NONC: return "Chi-squared noncentral" ;
00739      case NIFTI_INTENT_LOGISTIC:   return "Logistic distribution" ;
00740      case NIFTI_INTENT_LAPLACE:    return "Laplace distribution" ;
00741      case NIFTI_INTENT_UNIFORM:    return "Uniform distribition" ;
00742      case NIFTI_INTENT_TTEST_NONC: return "T-statistic noncentral" ;
00743      case NIFTI_INTENT_WEIBULL:    return "Weibull distribution" ;
00744      case NIFTI_INTENT_CHI:        return "Chi distribution" ;
00745      case NIFTI_INTENT_INVGAUSS:   return "Inverse Gaussian distribution" ;
00746      case NIFTI_INTENT_EXTVAL:     return "Extreme Value distribution" ;
00747      case NIFTI_INTENT_PVAL:       return "P-value" ;
00748 
00749      case NIFTI_INTENT_LOGPVAL:    return "Log P-value" ;
00750      case NIFTI_INTENT_LOG10PVAL:  return "Log10 P-value" ;
00751 
00752      case NIFTI_INTENT_ESTIMATE:   return "Estimate" ;
00753      case NIFTI_INTENT_LABEL:      return "Label index" ;
00754      case NIFTI_INTENT_NEURONAME:  return "NeuroNames index" ;
00755      case NIFTI_INTENT_GENMATRIX:  return "General matrix" ;
00756      case NIFTI_INTENT_SYMMATRIX:  return "Symmetric matrix" ;
00757      case NIFTI_INTENT_DISPVECT:   return "Displacement vector" ;
00758      case NIFTI_INTENT_VECTOR:     return "Vector" ;
00759      case NIFTI_INTENT_POINTSET:   return "Pointset" ;
00760      case NIFTI_INTENT_TRIANGLE:   return "Triangle" ;
00761      case NIFTI_INTENT_QUATERNION: return "Quaternion" ;
00762 
00763      case NIFTI_INTENT_DIMLESS:    return "Dimensionless number" ;
00764    }
00765    return "Unknown" ;
00766 }

int nifti_is_gzfile (  char *    fname )  [static]
 

Referenced by nifti_image_load_prep(), and nifti_image_read().

int nifti_is_inttype (  int    dt ) 
 

Definition at line 658 of file nifti1_io.c.

References dt, DT_BINARY, DT_COMPLEX128, DT_COMPLEX256, DT_COMPLEX64, DT_FLOAT128, DT_FLOAT32, DT_FLOAT64, DT_INT16, DT_INT32, DT_INT64, DT_INT8, DT_RGB24, DT_UINT16, DT_UINT32, DT_UINT64, DT_UINT8, and DT_UNKNOWN.

00659 {
00660    switch( dt ){
00661      case DT_UNKNOWN:    return 0 ;
00662      case DT_BINARY:     return 0 ;
00663      case DT_INT8:       return 1 ;
00664      case DT_UINT8:      return 1 ;
00665      case DT_INT16:      return 1 ;
00666      case DT_UINT16:     return 1 ;
00667      case DT_INT32:      return 1 ;
00668      case DT_UINT32:     return 1 ;
00669      case DT_INT64:      return 1 ;
00670      case DT_UINT64:     return 1 ;
00671      case DT_FLOAT32:    return 0 ;
00672      case DT_FLOAT64:    return 0 ;
00673      case DT_FLOAT128:   return 0 ;
00674      case DT_COMPLEX64:  return 0 ;
00675      case DT_COMPLEX128: return 0 ;
00676      case DT_COMPLEX256: return 0 ;
00677      case DT_RGB24:      return 1 ;
00678    }
00679    return 0 ;
00680 }

int nifti_load_NBL_bricks (  nifti_image *    nim,
int *    slist,
int *    sindex,
nifti_brick_list *    NBL,
znzFile    fp
)  [static]
 

Referenced by nifti_image_load_bricks().

char* nifti_makebasename (  char *    fname ) 
 

Definition at line 1738 of file nifti1_io.c.

References nifti_find_file_extension(), and nifti_strdup().

Referenced by main(), nifti_findhdrname(), and nifti_findimgname().

01739 {
01740    /*
01741       NB: it allocates memory for basename which should be freed
01742         when no longer required
01743    */
01744    char *basename, *ext;
01745 
01746    basename=nifti_strdup(fname);
01747 
01748    ext = nifti_find_file_extension(basename);
01749    if ( ext ) *ext = '\0';  /* clear out extension */
01750    
01751    return basename;  /* in either case */
01752 }

char* nifti_orientation_string (  int    ii ) 
 

Definition at line 789 of file nifti1_io.c.

References NIFTI_A2P, NIFTI_I2S, NIFTI_L2R, NIFTI_P2A, NIFTI_R2L, and NIFTI_S2I.

Referenced by nifti_disp_matrix_orient(), and nifti_image_to_ascii().

00790 {
00791    switch( ii ){
00792      case NIFTI_L2R: return "Left-to-Right" ;
00793      case NIFTI_R2L: return "Right-to-Left" ;
00794      case NIFTI_P2A: return "Posterior-to-Anterior" ;
00795      case NIFTI_A2P: return "Anterior-to-Posterior" ;
00796      case NIFTI_I2S: return "Inferior-to-Superior" ;
00797      case NIFTI_S2I: return "Superior-to-Inferior" ;
00798    }
00799    return "Unknown" ;
00800 }

size_t nifti_read_buffer (  znzFile    fp,
void *    datatptr,
size_t    ntot,
nifti_image *    nim
)  [static]
 

Referenced by nifti_image_load(), and nifti_load_NBL_bricks().

int nifti_read_extensions (  nifti_image *    nim,
znzFile    fp
)  [static]
 

Referenced by nifti_image_read().

int nifti_read_next_extension (  nifti1_extension *    nex,
nifti_image *    nim,
znzFile    fp
)  [static]
 

Referenced by nifti_read_extensions().

void nifti_set_debug_level (  int    level ) 
 

Definition at line 1754 of file nifti1_io.c.

References gni_debug.

Referenced by main().

01755 {
01756     gni_debug = level;
01757 }

void nifti_set_iname_offset (  nifti_image *    nim ) 
 

Definition at line 3246 of file nifti1_io.c.

References gni_debug, nifti_image::iname_offset, nifti_extension_size(), nifti_image::nifti_type, and offset.

Referenced by nifti_image_write_hdr_img2().

03247 {
03248    int offset = nifti_extension_size(nim);  /* init based on extensions */
03249 
03250 
03251    switch( nim->nifti_type ){
03252 
03253      default:  /* writing into 2 files */
03254        /* we only write files with 0 offset in the 2 file format */
03255        nim->iname_offset = 0 ;
03256      break ;
03257 
03258      case 1:   /* NIFTI-1 single binary file */
03259        if (nim->iname_offset < sizeof(struct nifti_1_header))  {
03260            offset += sizeof(struct nifti_1_header) + 4 ;
03261            /* be sure offset is aligned to a 16 byte word boundary */
03262            if ( ( offset % 16 ) != 0 )  offset = ((offset + 0xf) & ~0xf);
03263 
03264            if( nim->iname_offset != offset && gni_debug > 1 ){
03265               fprintf(stderr,"+d changing offset from %d to %d\n",
03266                       nim->iname_offset, offset);
03267            }
03268            nim->iname_offset = offset;
03269        }
03270      break ;
03271 
03272                /* non-standard case: */
03273      case 3:{  /* NIFTI-1 ASCII header + binary data (single file) */
03274        nim->iname_offset = -1 ;              /* compute offset from filesize */
03275      }
03276    }
03277 }

nifti_image* nifti_simple_init_nim (    ) 
 

Definition at line 3085 of file nifti1_io.c.

References nifti_1_header::bitpix, nifti_1_header::datatype, nifti_1_header::dim, nifti_image::fname, nifti_image::iname, nifti_convert_nhdr2nim(), nifti_datatype_sizes(), NIFTI_TYPE_FLOAT32, nifti_1_header::pixdim, nifti_1_header::regular, and nifti_1_header::sizeof_hdr.

03086 {
03087   nifti_image *nim;
03088   struct nifti_1_header nhdr;
03089   int nbyper, swapsize;
03090   
03091   memset(&nhdr,0,sizeof(nhdr)) ;  /* zero out header, to be safe */
03092   
03093    /** load the ANALYZE-7.5 generic parts of the header struct **/
03094 
03095    nhdr.sizeof_hdr = sizeof(nhdr) ;
03096    nhdr.regular    = 'r' ;             /* for some stupid reason */
03097 
03098    nhdr.dim[0] = 3 ;
03099    nhdr.dim[1] = 1 ; nhdr.dim[2] = 1 ; nhdr.dim[3] = 1 ;
03100    nhdr.dim[4] = 0 ;
03101 
03102    nhdr.pixdim[0] = 0.0 ;
03103    nhdr.pixdim[1] = 1.0 ; nhdr.pixdim[2] = 1.0 ;
03104    nhdr.pixdim[3] = 1.0 ;
03105 
03106    nhdr.datatype = NIFTI_TYPE_FLOAT32 ;
03107    nifti_datatype_sizes( nhdr.datatype , &nbyper, &swapsize );
03108    nhdr.bitpix   = 8 * nbyper ;
03109 
03110    nim = nifti_convert_nhdr2nim(nhdr,NULL);
03111    nim->fname = NULL;
03112    nim->iname = NULL;
03113    return nim;
03114 }

char* nifti_slice_string (  int    ss ) 
 

Definition at line 773 of file nifti1_io.c.

References NIFTI_SLICE_ALT_DEC, NIFTI_SLICE_ALT_INC, NIFTI_SLICE_SEQ_DEC, and NIFTI_SLICE_SEQ_INC.

Referenced by nifti_image_to_ascii().

00774 {
00775    switch( ss ){
00776      case NIFTI_SLICE_SEQ_INC: return "sequential_increasing"  ;
00777      case NIFTI_SLICE_SEQ_DEC: return "sequential_decreasing"  ;
00778      case NIFTI_SLICE_ALT_INC: return "alternating_increasing" ;
00779      case NIFTI_SLICE_ALT_DEC: return "alternating_decreasing" ;
00780    }
00781    return "Unknown" ;
00782 }

char* nifti_units_string (  int    uu ) 
 

Definition at line 687 of file nifti1_io.c.

References NIFTI_UNITS_HZ, NIFTI_UNITS_METER, NIFTI_UNITS_MICRON, NIFTI_UNITS_MM, NIFTI_UNITS_MSEC, NIFTI_UNITS_PPM, NIFTI_UNITS_RADS, NIFTI_UNITS_SEC, and NIFTI_UNITS_USEC.

Referenced by nifti_image_to_ascii().

00688 {
00689    switch( uu ){
00690      case NIFTI_UNITS_METER:  return "m" ;
00691      case NIFTI_UNITS_MM:     return "mm" ;
00692      case NIFTI_UNITS_MICRON: return "um" ;
00693      case NIFTI_UNITS_SEC:    return "s" ;
00694      case NIFTI_UNITS_MSEC:   return "ms" ;
00695      case NIFTI_UNITS_USEC:   return "us" ;
00696      case NIFTI_UNITS_HZ:     return "Hz" ;
00697      case NIFTI_UNITS_PPM:    return "ppm" ;
00698      case NIFTI_UNITS_RADS:   return "rad/s" ;
00699    }
00700    return "Unknown" ;
00701 }

int nifti_valid_extension (  nifti_image *    nim,
int    size,
int    code
)  [static]
 

Referenced by nifti_read_next_extension().

int nifti_validfilename (  char *    fname ) 
 

Definition at line 1672 of file nifti1_io.c.

References gni_debug, and nifti_find_file_extension().

Referenced by is_nifti_file(), nifti_findhdrname(), nifti_findimgname(), and nifti_image_write_hdr_img2().

01673 {
01674    char * ext;
01675    int                         len;
01676 
01677    /** check input file(s) for sanity **/
01678    if( fname == NULL || *fname == '\0' ) return 0;
01679 
01680    len = strlen(fname);
01681 
01682    if( len < 1 ) {
01683       if ( gni_debug > 0 )
01684          fprintf(stderr,"-- empty filename in nifti_validfilename()\n");
01685       return 0;
01686    }
01687 
01688    ext = nifti_find_file_extension((char *)fname);
01689 
01690    if ( ext && ext == fname ) {   /* then no filename prefix */
01691       if ( gni_debug > 0 )
01692          fprintf(stderr,"-- no prefix for filename '%s'\n", fname);
01693       return 0;
01694    }
01695 
01696    return 1;
01697 }

void nifti_write_all_data (  znzFile    fp,
nifti_image *    nim
)  [static]
 

Referenced by nifti_image_write_hdr_img2().

size_t nifti_write_buffer (  znzFile    fp,
void *    buffer,
size_t    numbytes
)  [static]
 

Referenced by nifti_write_all_data(), and nifti_write_extensions().

int nifti_write_extensions (  znzFile    fp,
nifti_image *    nim
)  [static]
 

Referenced by nifti_image_write_hdr_img2().

char* nifti_xform_string (  int    xx ) 
 

Definition at line 708 of file nifti1_io.c.

References NIFTI_XFORM_ALIGNED_ANAT, NIFTI_XFORM_MNI_152, NIFTI_XFORM_SCANNER_ANAT, and NIFTI_XFORM_TALAIRACH.

Referenced by nifti_image_to_ascii().

00709 {
00710    switch( xx ){
00711      case NIFTI_XFORM_SCANNER_ANAT:  return "Scanner Anat" ;
00712      case NIFTI_XFORM_ALIGNED_ANAT:  return "Aligned Anat" ;
00713      case NIFTI_XFORM_TALAIRACH:     return "Talairach" ;
00714      case NIFTI_XFORM_MNI_152:       return "MNI_152" ;
00715    }
00716    return "Unknown" ;
00717 }

int print_hex_vals (  char *    data,
int    nbytes,
FILE *    fp
) 
 

Definition at line 1934 of file nifti1_io.c.

References c.

Referenced by disp_nifti_1_header().

01935 {
01936    int c;
01937 
01938    if ( !data || nbytes < 1 || !fp ) return -1;
01939 
01940    fputs("0x", fp);
01941    for ( c = 0; c < nbytes; c++ )
01942       fprintf(fp, " %x", data[c]);
01943 
01944    return 0;
01945 }

mat44 quatern_to_mat44 (  float    qb,
float    qc,
float    qd,
float    qx,
float    qy,
float    qz,
float    dx,
float    dy,
float    dz,
float    qfac
) 
 

Definition at line 851 of file nifti1_io.c.

References a, c, l, and mat44::m.

Referenced by nifti_convert_nhdr2nim(), and nifti_image_from_ascii().

00854 {
00855    mat44 R ;
00856    double a,b=qb,c=qc,d=qd , xd,yd,zd ;
00857 
00858    /* last row is always [ 0 0 0 1 ] */
00859 
00860    R.m[3][0]=R.m[3][1]=R.m[3][2] = 0.0 ; R.m[3][3]= 1.0 ;
00861 
00862    /* compute a parameter from b,c,d */
00863 
00864    a = 1.0l - (b*b + c*c + d*d) ;
00865    if( a < 1.e-7l ){                   /* special case */
00866      a = 1.0l / sqrt(b*b+c*c+d*d) ;
00867      b *= a ; c *= a ; d *= a ;        /* normalize (b,c,d) vector */
00868      a = 0.0l ;                        /* a = 0 ==> 180 degree rotation */
00869    } else{
00870      a = sqrt(a) ;                     /* angle = 2*arccos(a) */
00871    }
00872 
00873    /* load rotation matrix, including scaling factors for voxel sizes */
00874 
00875    xd = (dx > 0.0) ? dx : 1.0l ;       /* make sure are positive */
00876    yd = (dy > 0.0) ? dy : 1.0l ;
00877    zd = (dz > 0.0) ? dz : 1.0l ;
00878 
00879    if( qfac < 0.0 ) zd = -zd ;         /* left handedness? */
00880 
00881    R.m[0][0] =        (a*a+b*b-c*c-d*d) * xd ;
00882    R.m[0][1] = 2.0l * (b*c-a*d        ) * yd ;
00883    R.m[0][2] = 2.0l * (b*d+a*c        ) * zd ;
00884    R.m[1][0] = 2.0l * (b*c+a*d        ) * xd ;
00885    R.m[1][1] =        (a*a+c*c-b*b-d*d) * yd ;
00886    R.m[1][2] = 2.0l * (c*d-a*b        ) * zd ;
00887    R.m[2][0] = 2.0l * (b*d-a*c        ) * xd ;
00888    R.m[2][1] = 2.0l * (c*d+a*b        ) * yd ;
00889    R.m[2][2] =        (a*a+d*d-c*c-b*b) * zd ;
00890 
00891    /* load offsets */
00892 
00893    R.m[0][3] = qx ; R.m[1][3] = qy ; R.m[2][3] = qz ;
00894 
00895    return R ;
00896 }

int short_order (  void    ) 
 

Strings that we don't control (filenames, etc.) that might contain "weird" characters (like quotes) are "escaped":

  • A few special characters are replaced by XML-style escapes, using the function escapize_string().
  • On input, function unescape_string() reverses this process.
  • The result is that the NIFTI ASCII-format header is XML-compliant. *

Definition at line 3859 of file nifti1_io.c.

Referenced by nifti_convert_nhdr2nim(), nifti_image_from_ascii(), nifti_image_write_hdr_img2(), nifti_read_buffer(), nifti_read_next_extension(), and nifti_write_all_data().

03860 {
03861    union { unsigned char bb[2] ;
03862            short         ss    ; } fred ;
03863 
03864    fred.bb[0] = 1 ; fred.bb[1] = 0 ;
03865 
03866    return (fred.ss == 1) ? LSB_FIRST : MSB_FIRST ;
03867 }

void swap_16bytes (  int    n,
void *    ar
) 
 

Definition at line 1538 of file nifti1_io.c.

References sixteenbytes::A, sixteenbytes::a, sixteenbytes::B, sixteenbytes::b, sixteenbytes::C, sixteenbytes::c, sixteenbytes::D, sixteenbytes::d, sixteenbytes::E, sixteenbytes::e, sixteenbytes::F, sixteenbytes::f, sixteenbytes::G, sixteenbytes::g, sixteenbytes::H, sixteenbytes::h, and tt.

Referenced by swap_Nbytes().

01539 {
01540    register int ii ;
01541    register sixteenbytes *tb = (sixteenbytes *)ar ;
01542    register unsigned char tt ;
01543 
01544    for( ii=0 ; ii < n ; ii++ ){
01545      tt = tb[ii].a ; tb[ii].a = tb[ii].A ; tb[ii].A = tt ;
01546      tt = tb[ii].b ; tb[ii].b = tb[ii].B ; tb[ii].B = tt ;
01547      tt = tb[ii].c ; tb[ii].c = tb[ii].C ; tb[ii].C = tt ;
01548      tt = tb[ii].d ; tb[ii].d = tb[ii].D ; tb[ii].D = tt ;
01549 
01550      tt = tb[ii].e ; tb[ii].e = tb[ii].E ; tb[ii].E = tt ;
01551      tt = tb[ii].f ; tb[ii].f = tb[ii].F ; tb[ii].F = tt ;
01552      tt = tb[ii].g ; tb[ii].g = tb[ii].G ; tb[ii].G = tt ;
01553      tt = tb[ii].h ; tb[ii].h = tb[ii].H ; tb[ii].H = tt ;
01554    }
01555    return ;
01556 }

void swap_2bytes (  int    n,
void *    ar
) 
 

Definition at line 1485 of file nifti1_io.c.

References twobytes::a, twobytes::b, and tt.

Referenced by swap_Nbytes(), and swap_nifti_header().

01486 {
01487    register int ii ;
01488    register twobytes *tb = (twobytes *)ar ;
01489    register unsigned char tt ;
01490 
01491    for( ii=0 ; ii < n ; ii++ ){
01492      tt = tb[ii].a ; tb[ii].a = tb[ii].b ; tb[ii].b = tt ;
01493    }
01494    return ;
01495 }

void swap_4bytes (  int    n,
void *    ar
) 
 

Definition at line 1501 of file nifti1_io.c.

References fourbytes::a, fourbytes::b, fourbytes::c, fourbytes::d, and tt.

Referenced by nifti_read_next_extension(), swap_Nbytes(), and swap_nifti_header().

01502 {
01503    register int ii ;
01504    register fourbytes *tb = (fourbytes *)ar ;
01505    register unsigned char tt ;
01506 
01507    for( ii=0 ; ii < n ; ii++ ){
01508      tt = tb[ii].a ; tb[ii].a = tb[ii].d ; tb[ii].d = tt ;
01509      tt = tb[ii].b ; tb[ii].b = tb[ii].c ; tb[ii].c = tt ;
01510    }
01511    return ;
01512 }

void swap_8bytes (  int    n,
void *    ar
) 
 

Definition at line 1518 of file nifti1_io.c.

References eightbytes::A, eightbytes::a, eightbytes::B, eightbytes::b, eightbytes::C, eightbytes::c, eightbytes::D, eightbytes::d, and tt.

Referenced by swap_Nbytes().

01519 {
01520    register int ii ;
01521    register eightbytes *tb = (eightbytes *)ar ;
01522    register unsigned char tt ;
01523 
01524    for( ii=0 ; ii < n ; ii++ ){
01525      tt = tb[ii].a ; tb[ii].a = tb[ii].A ; tb[ii].A = tt ;
01526      tt = tb[ii].b ; tb[ii].b = tb[ii].B ; tb[ii].B = tt ;
01527      tt = tb[ii].c ; tb[ii].c = tb[ii].C ; tb[ii].C = tt ;
01528      tt = tb[ii].d ; tb[ii].d = tb[ii].D ; tb[ii].D = tt ;
01529    }
01530    return ;
01531 }

void swap_Nbytes (  int    n,
int    siz,
void *    ar
) 
 

Definition at line 1560 of file nifti1_io.c.

References swap_16bytes(), swap_2bytes(), swap_4bytes(), and swap_8bytes().

Referenced by nifti_read_buffer(), and THD_load_nifti().

01561 {
01562    switch( siz ){
01563      case 2:  swap_2bytes ( n , ar ) ; break ;
01564      case 4:  swap_4bytes ( n , ar ) ; break ;
01565      case 8:  swap_8bytes ( n , ar ) ; break ;
01566      case 16: swap_16bytes( n , ar ) ; break ;
01567    }
01568    return ;
01569 }

void swap_nifti_header (  struct nifti_1_header *    h,
int    is_nifti
) 
 

Definition at line 1578 of file nifti1_io.c.

References nifti_1_header::bitpix, nifti_1_header::cal_max, nifti_1_header::cal_min, nifti_1_header::datatype, nifti_1_header::dim, nifti_1_header::extents, nifti_1_header::glmax, nifti_1_header::glmin, nifti_1_header::intent_code, nifti_1_header::intent_p1, nifti_1_header::intent_p2, nifti_1_header::intent_p3, nifti_1_header::pixdim, nifti_1_header::qform_code, nifti_1_header::qoffset_x, nifti_1_header::qoffset_y, nifti_1_header::qoffset_z, nifti_1_header::quatern_b, nifti_1_header::quatern_c, nifti_1_header::quatern_d, nifti_1_header::scl_inter, nifti_1_header::scl_slope, nifti_1_header::session_error, nifti_1_header::sform_code, nifti_1_header::sizeof_hdr, nifti_1_header::slice_duration, nifti_1_header::slice_end, nifti_1_header::slice_start, nifti_1_header::srow_x, nifti_1_header::srow_y, nifti_1_header::srow_z, swap_2, swap_2bytes(), swap_4, swap_4bytes(), nifti_1_header::toffset, and nifti_1_header::vox_offset.

Referenced by nifti_convert_nhdr2nim().

01579 {
01580 
01581 #if 0                /* ANALYZE fields not used by this software */
01582    swap_4(h->sizeof_hdr) ;
01583    swap_4(h->extents) ;
01584    swap_2(h->session_error) ;
01585    swap_4(h->compressed) ;
01586    swap_4(h->glmax) ; swap_4(h->glmin) ;
01587 #endif
01588 
01589    /* this stuff is always present, for ANALYZE and NIFTI */
01590 
01591    swap_2bytes( 8 , h->dim ) ;
01592    swap_4bytes( 8 , h->pixdim ) ;
01593 
01594    swap_2(h->datatype) ;
01595    swap_2(h->bitpix) ;
01596 
01597    swap_4(h->vox_offset); swap_4(h->cal_max); swap_4(h->cal_min);
01598 
01599    /* this stuff is NIFTI specific */
01600 
01601    if( is_nifti ){
01602      swap_2(h->qform_code); swap_2(h->sform_code);
01603      swap_4(h->quatern_b); swap_4(h->quatern_c); swap_4(h->quatern_d);
01604      swap_4(h->qoffset_x); swap_4(h->qoffset_y); swap_4(h->qoffset_z);
01605      swap_4(h->intent_p1); swap_4(h->intent_p2); swap_4(h->intent_p3);
01606      swap_4(h->scl_slope); swap_4(h->scl_inter);
01607      swap_4bytes(4,h->srow_x);
01608      swap_4bytes(4,h->srow_y);
01609      swap_4bytes(4,h->srow_z);
01610      swap_2(h->intent_code); swap_4(h->toffset);
01611      swap_2(h->slice_start); swap_2(h->slice_end);
01612      swap_4(h->slice_duration);
01613    }
01614    return ;
01615 }

int unescape_string (  char *    str ) 
 

Definition at line 3476 of file nifti1_io.c.

References CR, and LF.

Referenced by nifti_image_from_ascii().

03477 {
03478    int ii,jj , nn,ll ;
03479 
03480    if( str == NULL ) return 0 ;                /* no string? */
03481    ll = strlen(str) ; if( ll == 0 ) return 0 ;
03482 
03483    /* scan for escapes: &something; */
03484 
03485    for( ii=jj=nn=0 ; ii<ll ; ii++,jj++ ){ /* scan at ii; results go in at jj */
03486 
03487      if( str[ii] == '&' ){  /* start of escape? */
03488 
03489              if( ii+3 < ll        &&   /* &lt; */
03490                  str[ii+1] == 'l' &&
03491                  str[ii+2] == 't' &&
03492                  str[ii+3] == ';'   ){ str[jj] = '<' ; ii += 3 ; nn++ ; }
03493 
03494         else if( ii+3 < ll        &&   /* &gt; */
03495                  str[ii+1] == 'g' &&
03496                  str[ii+2] == 't' &&
03497                  str[ii+3] == ';'   ){ str[jj] = '>' ; ii += 3 ; nn++ ; }
03498 
03499         else if( ii+5 < ll        &&   /* &quot; */
03500                  str[ii+1] == 'q' &&
03501                  str[ii+2] == 'u' &&
03502                  str[ii+3] == 'o' &&
03503                  str[ii+4] == 't' &&
03504                  str[ii+5] == ';'   ){ str[jj] = '"' ; ii += 5 ; nn++ ; }
03505 
03506         else if( ii+5 < ll        &&   /* &apos; */
03507                  str[ii+1] == 'a' &&
03508                  str[ii+2] == 'p' &&
03509                  str[ii+3] == 'o' &&
03510                  str[ii+4] == 's' &&
03511                  str[ii+5] == ';'   ){ str[jj] = '\'' ; ii += 5 ; nn++ ; }
03512 
03513         else if( ii+4 < ll        &&  /* &amp; */
03514                  str[ii+1] == 'a' &&
03515                  str[ii+2] == 'm' &&
03516                  str[ii+3] == 'p' &&
03517                  str[ii+4] == ';'   ){ str[jj] = '&' ; ii += 4 ; nn++ ; }
03518 
03519         /* although the comments above don't mention it,
03520            we also look for XML style numeric escapes
03521            of the forms &#32; (decimal) and &#xfd; (hex) */
03522 
03523         else if( ii+3 < ll        &&
03524                  str[ii+1] == '#' &&
03525                  isdigit(str[ii+2]) ){   /* &#dec; */
03526 
03527            unsigned int val='?' ; int kk=ii+3 ;
03528            while( kk < ll && kk != ';' ) kk++ ;
03529            sscanf( str+ii+2 , "%u" , &val ) ;
03530            str[jj] = (char) val ; ii = kk ; nn++ ;
03531         }
03532 
03533         else if( ii+4 < ll        &&
03534                  str[ii+1] == '#' &&
03535                  str[ii+2] == 'x' &&
03536                  isxdigit(str[ii+3]) ){   /* &#hex; */
03537 
03538            unsigned int val='?' ; int kk=ii+4 ;
03539            while( kk < ll && kk != ';' ) kk++ ;
03540            sscanf( str+ii+3 , "%x" , &val ) ;
03541            str[jj] = (char) val ; ii = kk ; nn++ ;
03542         }
03543 
03544         /* didn't start a recognized escape, so just copy as normal */
03545 
03546         else if( jj < ii ){ str[jj] = str[ii] ; }
03547 
03548      } else if( str[ii] == CR ) {  /* is a carriage return */
03549 
03550         if( str[ii+1] == LF ){ str[jj] = LF ; ii++ ; nn++ ; }  /* CR LF */
03551         else                 { str[jj] = LF ;      ; nn++ ; }  /* CR only */
03552 
03553      } else { /* is a normal character, just copy to output */
03554 
03555              if( jj < ii ){ str[jj] = str[ii] ; }
03556      }
03557 
03558      /* at this point, ii=index of last character used up in scan
03559                        jj=index of last character written to (jj <= ii) */
03560    }
03561 
03562    if( jj < ll ) str[jj] = '\0' ; /* end string properly */
03563 
03564    return nn ;
03565 }

void update_nifti_image_for_brick_list (  nifti_image *    nim,
int    nbricks
)  [static]
 

Referenced by nifti_image_read_bricks().

int valid_nifti_brick_list (  nifti_image *    nim,
int    nbricks,
int *    blist,
int    disp_error
) 
 

Definition at line 541 of file nifti1_io.c.

References c, nifti_image::dim, gni_debug, int_force_positive(), and nsubs().

Referenced by nifti_image_load_bricks().

00543 {
00544    int c, nsubs;
00545 
00546    if( !nim ){
00547       if( disp_error || gni_debug > 0 )
00548          fprintf(stderr,"** valid_nifti_brick_list: missing nifti image\n");
00549       return 0;
00550    }
00551 
00552    if( nbricks <= 0 || !blist ){
00553       if( disp_error || gni_debug > 1 )
00554          fprintf(stderr,"** valid_nifti_brick_list: no brick list to check\n");
00555       return 0;
00556    }
00557 
00558    /* nsubs sub-brick is nt*nu*nv*nw */
00559    nsubs = nim->dim[4] * nim->dim[5] * nim->dim[6] * nim->dim[7];
00560 
00561    if( nsubs <= 0 ){
00562       fprintf(stderr,"** VNBL warning: bad dim list (%d,%d,%d,%d)\n",
00563                      nim->dim[4], nim->dim[5], nim->dim[6], nim->dim[7]);
00564       int_force_positive(nim->dim+4, 4);
00565       nsubs = nim->dim[4] * nim->dim[5] * nim->dim[6] * nim->dim[7];
00566    }
00567 
00568    for( c = 0; c < nbricks; c++ )
00569       if( (blist[c] < 0) || (blist[c] >= nsubs) ){
00570          if( disp_error || gni_debug > 1 )
00571             fprintf(stderr,
00572                "-d ** bad sub-brick chooser %d (#%d), valid range is [0,%d]\n",
00573                blist[c], c, nsubs-1);
00574          return 0;
00575       }
00576 
00577    return 1;  /* all is well */
00578 }
Powered by Plone

This site conforms to the following standards: