nifti1_io
Defines | Functions
/builddir/build/BUILD/nifticlib-2.0.0/fsliolib/fslio.c File Reference

Main collection of FSL i/o routines, written by Mark Jenkinson, FMRIB. More...

#include "fslio.h"
#include "assert.h"

Defines

#define FSLIOERR(x)   { fprintf(stderr,"Error:: %s\n",(x)); fflush(stderr); exit(EXIT_FAILURE); }

Functions

char * FslFileTypeString (int filetype)
 Return a string describing the format of the dataset.
int FslIsValidFileType (int filetype)
int FslBaseFileType (int filetype)
int FslGetFileType2 (const FSLIO *fslio, int quiet)
int FslGetFileType (const FSLIO *fslio)
void FslSetFileType (FSLIO *fslio, int filetype)
int FslIsSingleFileType (int filetype)
int FslIsCompressedFileType (int filetype)
int FslGetWriteMode (const FSLIO *fslio)
void FslSetWriteMode (FSLIO *fslio, int mode)
int FslGetEnvOutputType (void)
int FslFileType (const char *fname)
int FslGetReadFileType (const FSLIO *fslio)
 return the best estimate of the true file type
int FslFileExists (const char *filename)
char * FslMakeBaseName (const char *fname)
void FslGetHdrImgNames (const char *filename, const FSLIO *fslio, char **hdrname, char **imgname)
FSLIOFslInit (void)
 allocate space for the FSLIO struct and set some sensible defaults
void FslSetInit (FSLIO *fslio)
void FslInit4Write (FSLIO *fslio, const char *filename, int ft)
void FslInitHeader (FSLIO *fslio, short t, size_t x, size_t y, size_t z, size_t v, float vx, float vy, float vz, float tr, size_t dim, const char *units)
void FslCloneHeader (FSLIO *dest, const FSLIO *src)
int fsl_fileexists (const char *fname)
int FslCheckForMultipleFileNames (const char *filename)
int check_for_multiple_filenames (const char *filename)
FSLIOFslOpen (const char *filename, const char *opts)
 Opens a file for either reading or writing.
FSLIOFslXOpen (const char *filename, const char *opts, int filetype)
 Opens a file for either reading or writing.
void * FslReadAllVolumes (FSLIO *fslio, char *filename)
 Read the header and all data into the FSLIO structure.
size_t FslReadVolumes (FSLIO *fslio, void *buffer, size_t nvols)
 Read the first nvols Volumes from a 4D dataset.
void FslWriteAllVolumes (FSLIO *fslio, const void *buffer)
 Writes all data from buffer (using size info from fslio) to file.
size_t FslWriteVolumes (FSLIO *fslio, const void *buffer, size_t nvols)
 Write the first nvols volumes in buffer to disk.
void FslWriteHeader (FSLIO *fslio)
 Writes nifti/anz header and opens img file ready for writing.
size_t FslReadSliceSeries (FSLIO *fslio, void *buffer, short slice, size_t nvols)
 Read one slice from each of the first nvols volumes in the dataset, ie get an xyt buffer.
size_t FslReadRowSeries (FSLIO *fslio, void *buffer, short row, short slice, size_t nvols)
 Read one row from one slice for first nvols volumes in dataset; ie get an xt buffer.
size_t FslReadTimeSeries (FSLIO *fslio, void *buffer, short xVox, short yVox, short zVox, size_t nvols)
 Read one voxel (xyz location) from first nvols volumes in dataset; ie get a t dim buffer.
size_t FslReadCplxVolumes (FSLIO *fslio, void *buffer, size_t nvols, char mode)
size_t FslWriteCplxVolumes (FSLIO *fslio, void *buffer, size_t nvols, char mode)
int FslSeekVolume (FSLIO *fslio, size_t vols)
size_t FslGetVolSize (FSLIO *fslio)
void FslSetDim (FSLIO *fslio, short x, short y, short z, short v)
void FslGetDim (FSLIO *fslio, short *x, short *y, short *z, short *v)
void FslSetDimensionality (FSLIO *fslio, size_t dim)
void FslGetDimensionality (FSLIO *fslio, size_t *dim)
void FslSetVoxDim (FSLIO *fslio, float x, float y, float z, float tr)
void FslGetVoxDim (FSLIO *fslio, float *x, float *y, float *z, float *tr)
void FslGetCalMinMax (FSLIO *fslio, float *min, float *max)
void FslSetCalMinMax (FSLIO *fslio, float min, float max)
void FslGetAuxFile (FSLIO *fslio, char *aux_file)
void FslSetAuxFile (FSLIO *fslio, const char *aux_file)
void FslSetVoxUnits (FSLIO *fslio, const char *units)
void FslGetVoxUnits (FSLIO *fslio, char *units)
void FslSetTimeUnits (FSLIO *fslio, const char *units)
void FslGetTimeUnits (FSLIO *fslio, char *units)
void FslSetDataType (FSLIO *fslio, short t)
size_t FslGetDataType (FSLIO *fslio, short *t)
void FslGetMMCoord (mat44 stdmat, float voxx, float voxy, float voxz, float *mmx, float *mmy, float *mmz)
void FslGetVoxCoord (mat44 stdmat, float mmx, float mmy, float mmz, float *voxx, float *voxy, float *voxz)
void FslSetStdXform (FSLIO *fslio, short sform_code, mat44 stdmat)
short FslGetStdXform (FSLIO *fslio, mat44 *stdmat)
void FslSetRigidXform (FSLIO *fslio, short qform_code, mat44 rigidmat)
short FslGetRigidXform (FSLIO *fslio, mat44 *rigidmat)
void FslSetIntent (FSLIO *fslio, short intent_code, float p1, float p2, float p3)
short FslGetIntent (FSLIO *fslio, short *intent_code, float *p1, float *p2, float *p3)
void FslSetIntensityScaling (FSLIO *fslio, float slope, float intercept)
int FslGetIntensityScaling (FSLIO *fslio, float *slope, float *intercept)
mat33 mat44_to_mat33 (mat44 x)
int FslGetLeftRightOrder (FSLIO *fslio)
void FslSetAnalyzeSform (FSLIO *fslio, const short *orig, float dx, float dy, float dz)
void FslGetAnalyzeOrigin (FSLIO *fslio, short orig[5])
int FslClose (FSLIO *fslio)
 Write header and image data if this dataset was open for writing. Close the dataset header and data files.
void AvwSwapHeader (struct dsr *avw)
int FslReadRawHeader (void *buffer, const char *filename)
void FslSetOverrideOutputType (int type)
int FslGetOverrideOutputType (void)
void FslSetIgnoreMFQ (int flag)
int FslGetIgnoreMFQ (void)
FSLIOFslReadHeader (char *fname)
 Reads nifti/anz header, no data is read.
double *** FslGetVolumeAsScaledDouble (FSLIO *fslio, int vol)
 Return volume #vol (0-based) as a 3D array of scaled doubles.
double **** FslGetBufferAsScaledDouble (FSLIO *fslio)
 Return the fslio data buffer of a 1-4D dataset as a 4D array of scaled doubles.
int convertBufferToScaledDouble (double *outbuf, void *inbuf, long len, float slope, float inter, int nifti_datatype)
 allocate a 4D buffer, use 1 contiguous buffer for the data
double *** d3matrix (int zh, int yh, int xh)
 allocate a 3D buffer, use 1 contiguous buffer for the data
double **** d4matrix (int th, int zh, int yh, int xh)
 allocate a 4D buffer, use 1 contiguous buffer for the data

Detailed Description

Main collection of FSL i/o routines, written by Mark Jenkinson, FMRIB.


Function Documentation

int convertBufferToScaledDouble ( double *  outbuf,
void *  inbuf,
long  len,
float  slope,
float  inter,
int  nifti_datatype 
)

allocate a 4D buffer, use 1 contiguous buffer for the data

Array is indexed as buf[0..th-1][0..zh-1][0..yh-1][0..xh-1].
To access all elements as a vector, use buf[0][0][0][i] where i can range from 0 to th*zh*yh*xh - 1.

Parameters:
outbufpointer to array of doubles of size len
inbufvoid pointer to an array of len items of datatype nifti_datatype
lennumber of elements in outbuf and inbuf
slopeslope term of scaling to be applied
interintercept term of scaling to be applied: out = (in*slope)+inter
nifti_datatypeNIFTI datatype code for the datatype of the elements in inbuf
Returns:
error code: 0=OK -1=error

fill the buffer

double **** d3matrix ( int  zh,
int  yh,
int  xh 
)

allocate a 3D buffer, use 1 contiguous buffer for the data

Array is indexed as buf[0..zh][0..yh][0..xh].
To access all elements as a vector, use buf[0][0][i] where i can range from 0 to zh*yh*xh - 1. Adaptation of Numerical Recipes in C nrutil.c allocation routines.

Parameters:
zhslowest changing dimension
yh2nd fastest changing dimension
xhfastest changing dimension
Returns:
Pointer to 3D double array

allocate pointers to slices

allocate pointers for ydim

allocate the data blob

point everything to the data blob

double **** d4matrix ( int  th,
int  zh,
int  yh,
int  xh 
)

allocate a 4D buffer, use 1 contiguous buffer for the data

Array is indexed as buf[0..th][0..zh][0..yh][0..xh].
To access all elements as a vector, use buf[0][0][0][i] where i can range from 0 to th*zh*yh*xh - 1. Adaptation of Numerical Recipes in C nrutil.c allocation routines.

Parameters:
thslowest changing dimension
zh2nd slowest changing dimension
yh2nd fastest changing dimension
xhfastest changing dimension
Returns:
Pointer to 4D double array

allocate pointers to vols

allocate pointers to slices

allocate pointers for ydim

allocate the data blob

point everything to the data blob

int FslClose ( FSLIO fslio)

Write header and image data if this dataset was open for writing. Close the dataset header and data files.

Parameters:
fsliopointer to FSLIO data structure
Returns:
-1 on error, 0 OK ???.

----- if writing the image, need to worry about the header bit -----

char * FslFileTypeString ( int  filetype)

Return a string describing the format of the dataset.

Parameters:
filetypeFSL data format code. Legal values are as defined by FSL_TYPE.
Returns:
A string with the data format name, e.g. "ANALYZE-7.5"
See also:
FSL_TYPE
double **** FslGetBufferAsScaledDouble ( FSLIO fslio)

Return the fslio data buffer of a 1-4D dataset as a 4D array of scaled doubles.

Array is indexed as buf[0..tdim-1][0..zdim-1][0..ydim-1][0..xdim-1].
The array will be byteswapped to native-endian.
Array values are scaled as per fslio header slope and intercept fields.

Parameters:
fsliopointer to open dataset
Returns:
Pointer to 4D double array, NULL on error

allocate new 4D buffer

cvt it

int FslGetReadFileType ( const FSLIO fslio)

return the best estimate of the true file type

This function is used to return the best estimate of the true file type once a simple open has occurred - for now it is used after a nifti open call is made

Parameters:
fsliodata structure
Returns:
FSL_TYPE filetype code
See also:
FSL_TYPE
double *** FslGetVolumeAsScaledDouble ( FSLIO fslio,
int  vol 
)

Return volume #vol (0-based) as a 3D array of scaled doubles.

Volume Array is indexed as [0..zdim-1][0..ydim-1][0..xdim-1].
The array will be byteswapped to native-endian.
Array values are scaled as per fslio header slope and intercept fields.

Parameters:
fsliopointer to open dataset
volvolume number to read (legal range [0..tdim-1])
Returns:
Pointer to 3D double array, NULL on error

allocate new 3D buffer

read in the data in disk format

cvt disk buffer to scaled double buffer

FSLIO * FslInit ( void  )

allocate space for the FSLIO struct and set some sensible defaults

Returns:
A pointer to an initialized FSLIO data structure
FSLIO * FslOpen ( const char *  filename,
const char *  opts 
)

Opens a file for either reading or writing.

The format of the output dataset is determined automatically by passing filetype -1 to FslXOpen.

See also:
FslXOpen
void * FslReadAllVolumes ( FSLIO fslio,
char *  filename 
)

Read the header and all data into the FSLIO structure.

There is no need for FslOpen or FslClose calls when FslReadAllVolumes() is called.
This routine allocates the buffer to hold the entire dataset.
The data block returned will contain the data in whatever datatype it is stored as on disk (therefore it is a void *).
The data buffer will be byteswapped to native-endian.
The data buffer will not be scaled.
The best call to make before this is FslInit() or a calloc() for fslio. (??? why calloc if this allocates the buffer ???)

Parameters:
fsliopointer to an open dataset
filenameName of the dataset to read.
Returns:
A pointer to the data block buffer (allocated by this function).
Return Null on error ??? is this true ???
  • Note this pointer is also in the FSLIO structure as fslio->niftiptr->data.
  • Note a void pointer is returned, as the datablock is of variable datatype.

otherwise it is a nifti file - so read it!

FSLIO * FslReadHeader ( char *  fname)

Reads nifti/anz header, no data is read.

Parameters:
fnamefilename specification (could be .img,.hdr,.nii, or no ext
Returns:
FSLIO data structure with the nifti_image structure fields filled as per fname header. NULL on error

get header file name

read header information

size_t FslReadRowSeries ( FSLIO fslio,
void *  buffer,
short  row,
short  slice,
size_t  nvols 
)

Read one row from one slice for first nvols volumes in dataset; ie get an xt buffer.

Dimension and datatype of buffer are as is specified in nifti_image structure fslio->niftiptr. Note: filepointer in file data array is restored to its initial position.

Parameters:
fsliopointer to open dataset
bufferbuffer to hold one row from each volume.
rowrow number (0 based) to read [0 y-1]
sliceslice number (0 based) to read
nvolsnumber of volumes to read a row from
Returns:
Number of volumes from which a row was successfully read. 0 on error.
size_t FslReadSliceSeries ( FSLIO fslio,
void *  buffer,
short  slice,
size_t  nvols 
)

Read one slice from each of the first nvols volumes in the dataset, ie get an xyt buffer.

Dimension and datatype of buffer are as is specified in nifti_image structure fslio->niftiptr. Note: filepointer in file data array is restored to its initial position.

Parameters:
fsliopointer to open dataset
bufferbuffer large enough to hold 1 slice from each volume
sliceslice number (0 based) to read [0 z-1]
nvolsnumber of volumes to read a slice from
Returns:
Number of volumes from which a slice was successfully read. 0 on error.
size_t FslReadTimeSeries ( FSLIO fslio,
void *  buffer,
short  xVox,
short  yVox,
short  zVox,
size_t  nvols 
)

Read one voxel (xyz location) from first nvols volumes in dataset; ie get a t dim buffer.

Dimension and datatype of buffer are as is specified in nifti_image structure fslio->niftiptr. Note: filepointer in file data array is restored to its initial position.

Parameters:
fsliopointer to open dataset
bufferbuffer to hold one timeseries vector
xVoxx voxel [0 x-1]
yVoxy voxel [0 y-1]
zVoxz voxel [0 z-1]
nvolsnumber of volumes to read a voxel from
Returns:
Number of volumes from which a voxel was successfully read. 0 on error.
size_t FslReadVolumes ( FSLIO fslio,
void *  buffer,
size_t  nvols 
)

Read the first nvols Volumes from a 4D dataset.

Parameters:
fsliopointer to open dataset
bufferbuffer to read data into, allocated by ???
nvolsnumber of volumes to read
Returns:
Number of volumes read.
void FslWriteAllVolumes ( FSLIO fslio,
const void *  buffer 
)

Writes all data from buffer (using size info from fslio) to file.

Dimension and datatype of buffer are as is specified in nifti_image structure fslio->niftiptr. Note: If file format is Analyze (not nifti) and in Neurological order then SWAP DATA into Radiological order.

Parameters:
fsliopointer to open dataset
bufferpointer to data array. Size and datatype of this buffer
void FslWriteHeader ( FSLIO fslio)

Writes nifti/anz header and opens img file ready for writing.

Parameters:
fsliopointer to open dataset
size_t FslWriteVolumes ( FSLIO fslio,
const void *  buffer,
size_t  nvols 
)

Write the first nvols volumes in buffer to disk.

Dimension and datatype of buffer are as is specified in nifti_image structure fslio->niftiptr. Note: If file format is Analyze (not nifti) and in Neurological order then SWAP DATA into Radiological order.

Parameters:
fsliopointer to open dataset
bufferpointer to data array. Size and datatype of this buffer
nvolsnumber of volumes to write
Returns:
??? looks like return of retval is missing ??? 0 on error.
FSLIO * FslXOpen ( const char *  filename,
const char *  opts,
int  filetype 
)

Opens a file for either reading or writing.

Files to be read are automatically read whether compressed or not. Also, reading uses the file extension and will fail if that file does not exist. For a more robust read, pass the basename in as then all types will be tried.

Parameters:
filenameName (or basename) of the file to open
optsFlags for fopen() of dataset, eg "r", "wb", etc.
filetypespecifies the type of file to be written. Legal values are as defined by FSL_TYPE. If filetype is less than zero, then it is ignored and the type is determined by the filename extension or, failing that, the environment default.
Returns:
pointer to FSLIO dataset datastructure
See also:
FSLIO
FSL_TYPE

====================== Open file for writing ======================

======================== Open file for reading ======================

 All Classes Files Functions Variables Defines