nifti1_io.h(3)
| /usr/src/RPM/BUILD/niftilib-0.20100720/niftilib/nifti1_io.h(3) | Library Functions Manual | /usr/src/RPM/BUILD/niftilib-0.20100720/niftilib/nifti1_io.h(3) |
NAME
/usr/src/RPM/BUILD/niftilib-0.20100720/niftilib/nifti1_io.h -
Data structures for using nifti1_io API.
SYNOPSIS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include 'nifti1.h'
#include <znzlib.h>
Classes
struct mat44
struct mat33
struct nifti_image
High level data structure for open nifti datasets in the nifti1_io API.
Note that this structure is not part of the nifti1 format definition; it is
used to implement one API for reading/writing formats in the nifti1 format.
struct nifti_brick_list
struct nifti_analyze75
Defines
#define DONT_INCLUDE_ANALYZE_STRUCT
#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 NIFTI_ECODE_IGNORE 0
#define NIFTI_ECODE_DICOM 2
#define NIFTI_ECODE_AFNI 4
#define NIFTI_ECODE_COMMENT 6
#define NIFTI_ECODE_XCEDE 8
#define NIFTI_ECODE_JIMDIMINFO 10
#define NIFTI_ECODE_WORKFLOW_FWDS 12
#define NIFTI_ECODE_FREESURFER 14
#define NIFTI_ECODE_PYPICKLE 16
#define NIFTI_ECODE_MIND_IDENT 18
#define NIFTI_ECODE_B_VALUE 20
#define NIFTI_ECODE_SPHERICAL_DIRECTION 22
#define NIFTI_ECODE_DT_COMPONENT 24
#define NIFTI_ECODE_SHC_DEGREEORDER 26
#define NIFTI_ECODE_VOXBO 28
#define NIFTI_ECODE_CARET 30
#define NIFTI_MAX_ECODE 30
#define NIFTI_FTYPE_ANALYZE 0
#define NIFTI_FTYPE_NIFTI1_1 1
#define NIFTI_FTYPE_NIFTI1_2 2
#define NIFTI_FTYPE_ASCII 3
#define NIFTI_MAX_FTYPE 3
Typedefs
typedef enum _analyze75_orient_code analyze_75_orient_code
Enumerations
enum _analyze75_orient_code {
a75_transverse_unflipped = 0, a75_coronal_unflipped = 1,
a75_sagittal_unflipped = 2, a75_transverse_flipped = 3,
a75_coronal_flipped = 4, a75_sagittal_flipped = 5,
a75_orient_unknown = 6 }
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 nifti_mat44_inverse (mat44 R)
mat33 nifti_mat33_inverse (mat33 R)
mat33 nifti_mat33_polar (mat33 A)
float nifti_mat33_rownorm (mat33 A)
float nifti_mat33_colnorm (mat33 A)
float nifti_mat33_determ (mat33 R)
mat33 nifti_mat33_mul (mat33 A, mat33 B)
void nifti_swap_2bytes (size_t n, void *ar)
void nifti_swap_4bytes (size_t n, void *ar)
void nifti_swap_8bytes (size_t n, void *ar)
void nifti_swap_16bytes (size_t n, void *ar)
void nifti_swap_Nbytes (size_t n, int siz, void *ar)
int nifti_datatype_is_valid (int dtype, int for_nifti)
int nifti_datatype_from_string (const char *name)
char * nifti_datatype_to_string (int dtype)
int nifti_get_filesize (const char *pathname)
void swap_nifti_header (struct nifti_1_header *h, int is_nifti)
void old_swap_nifti_header (struct nifti_1_header *h, int
is_nifti)
int nifti_swap_as_analyze (nifti_analyze75 *h)
nifti_image * nifti_image_read_bricks (const char *hname, int
nbricks, const int *blist, nifti_brick_list *NBL)
int nifti_image_load_bricks (nifti_image *nim, int nbricks,
const int *blist, nifti_brick_list *NBL)
void nifti_free_NBL (nifti_brick_list *NBL)
nifti_image * nifti_image_read (const char *hname, int
read_data)
Read a nifti header and optionally the data, creating a
nifti_image. int nifti_image_load
(nifti_image *nim)
Load the image blob into a previously initialized
nifti_image. void nifti_image_unload
(nifti_image *nim)
void nifti_image_free (nifti_image *nim)
int nifti_read_collapsed_image (nifti_image *nim, const int
dims[8], void **data)
int nifti_read_subregion_image (nifti_image *nim, int
*start_index, int *region_size, void **data)
void nifti_image_write (nifti_image *nim)
void nifti_image_write_bricks (nifti_image *nim, const
nifti_brick_list *NBL)
void nifti_image_infodump (const nifti_image *nim)
void nifti_disp_lib_hist (void)
void nifti_disp_lib_version (void)
int nifti_disp_matrix_orient (const char *mesg, mat44 mat)
int nifti_disp_type_list (int which)
char * nifti_image_to_ascii (const nifti_image *nim)
nifti_image * nifti_image_from_ascii (const char *str, int
*bytes_read)
size_t nifti_get_volsize (const nifti_image *nim)
int nifti_set_filenames (nifti_image *nim, const char *prefix,
int check, int set_byte_order)
char * nifti_makehdrname (const char *prefix, int nifti_type, int
check, int comp)
char * nifti_makeimgname (const char *prefix, int nifti_type, int
check, int comp)
int is_nifti_file (const char *hname)
char * nifti_find_file_extension (const char *name)
int nifti_is_complete_filename (const char *fname)
int nifti_validfilename (const char *fname)
int disp_nifti_1_header (const char *info, const nifti_1_header
*hp)
void nifti_set_debug_level (int level)
void nifti_set_skip_blank_ext (int skip)
void nifti_set_allow_upper_fext (int allow)
int valid_nifti_brick_list (nifti_image *nim, int nbricks, const
int *blist, int disp_error)
znzFile nifti_image_open (const char *hname, char *opts,
nifti_image **nim)
Read in NIFTI-1 or ANALYZE-7.5 file (pair) header information into a
nifti_image struct. znzFile
nifti_image_write_hdr_img (nifti_image *nim, int write_data,
const char *opts)
znzFile nifti_image_write_hdr_img2 (nifti_image *nim, int
write_opts, const char *opts, znzFile imgfile, const
nifti_brick_list *NBL)
size_t nifti_read_buffer (znzFile fp, void *datatptr, size_t
ntot, nifti_image *nim)
int nifti_write_all_data (znzFile fp, nifti_image *nim,
const nifti_brick_list *NBL)
size_t nifti_write_buffer (znzFile fp, const void *buffer,
size_t numbytes)
nifti_image * nifti_read_ascii_image (znzFile fp, char
*fname, int flen, int read_data)
znzFile nifti_write_ascii_image (nifti_image *nim, const
nifti_brick_list *NBL, const char *opts, int write_data, int
leave_open)
void nifti_datatype_sizes (int datatype, int *nbyper, int *swapsize)
void nifti_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 nifti_quatern_to_mat44 (float qb, float qc, float qd,
float qx, float qy, float qz, float dx, float dy, float dz, float qfac)
mat44 nifti_make_orthog_mat44 (float r11, float r12, float r13,
float r21, float r22, float r23, float r31, float r32, float r33)
int nifti_short_order (void)
void nifti_mat44_to_orientation (mat44 R, int *icod, int *jcod,
int *kcod)
char * nifti_findhdrname (const char *fname)
char * nifti_findimgname (const char *fname, int nifti_type)
int nifti_is_gzfile (const char *fname)
char * nifti_makebasename (const char *fname)
struct nifti_1_header nifti_convert_nim2nhdr (const
nifti_image *nim)
nifti_1_header * nifti_make_new_header (const int arg_dims[],
int arg_dtype)
nifti_1_header * nifti_read_header (const char *hname, int
*swapped, int check)
nifti_image * nifti_copy_nim_info (const nifti_image
*src)
nifti_image * nifti_make_new_nim (const int dims[], int
datatype, int data_fill)
nifti_image * nifti_simple_init_nim (void)
nifti_image * nifti_convert_nhdr2nim (struct
nifti_1_header nhdr, const char *fname)
int nifti_hdr_looks_good (const nifti_1_header *hdr)
int nifti_is_valid_datatype (int dtype)
int nifti_is_valid_ecode (int ecode)
int nifti_nim_is_valid (nifti_image *nim, int complain)
int nifti_nim_has_valid_dims (nifti_image *nim, int complain)
int is_valid_nifti_type (int nifti_type)
int nifti_test_datatype_sizes (int verb)
int nifti_type_and_names_match (nifti_image *nim, int show_warn)
int nifti_update_dims_from_array (nifti_image *nim)
void nifti_set_iname_offset (nifti_image *nim)
int nifti_set_type_from_names (nifti_image *nim)
int nifti_add_extension (nifti_image *nim, const char *data, int
len, int ecode)
int nifti_compiled_with_zlib (void)
int nifti_copy_extensions (nifti_image *nim_dest, const
nifti_image *nim_src)
int nifti_free_extensions (nifti_image *nim)
int * nifti_get_intlist (int nvals, const char *str)
char * nifti_strdup (const char *str)
int valid_nifti_extensions (const nifti_image *nim)
Detailed Description
Data structures for using nifti1_io API.
- Written by Bob Cox, SSCC NIMH
- Revisions by Rick Reynolds, SSCC NIMH
Function Documentation
int disp_nifti_1_header (const char *info, const nifti_1_header *hp) display the contents of the nifti_1_header (send to stdout)
Parameters:
hp pointer to nifti_1_header
int is_nifti_file (const char *hname) Determine if this is a NIFTI-formatted file.
Returns:
0 if file looks like ANALYZE 7.5 [checks sizeof_hdr field == 348]
1 if file marked as NIFTI (header+data in 1 file)
2 if file marked as NIFTI (header+data in 2 files)
-1 if it can't tell, file doesn't exist, etc.
int is_valid_nifti_type (intnifti_type) check whether the given type is on the 'approved' list
The code is valid if it is non-negative, and does not exceed NIFTI_MAX_FTYPE.
Returns:
See also:
int nifti_add_extension (nifti_image *nim, const char *data, intlen, intecode) nifti_add_extension - add an extension, with a copy of the data
Add an extension to the nim->ext_list array. Fill this extension with a copy of the data, noting the length and extension code.
Parameters:
data - raw extension data
length - length of raw extension data
ecode - extension code
See also:
nifti_free_extensions, valid_nifti_extensions, nifti_copy_extensions
Returns:
int nifti_compiled_with_zlib (void) return whether the given library was compiled with HAVE_ZLIB set
nifti_image* nifti_convert_nhdr2nim (struct nifti_1_headernhdr, const char *fname) convert a nifti_1_header into a nift1_image
Returns:
- check if we must swap bytes
- determine if this is a NIFTI-1 compliant header
- in analyze75, the orient code is at the same address as qform_code, but it's just one byte the qform_code will be zero, at which point you can check analyze75_orient if you care to.
- set bad grid spacings to 1.0
- set dimensions of data array
- set the type of data in voxels and how many bytes per voxel
- set the grid spacings
- compute qto_xyz transformation from pixel indexes (i,j,k) to (x,y,z)
- if not nifti or qform_code <= 0, use grid spacing for qto_xyz
- else NIFTI: use the quaternion-specified transformation
- load inverse transformation (x,y,z) -> (i,j,k)
- load sto_xyz affine transformation, if present
- if not nifti or sform_code <= 0, then no sto transformation
- else set the sto transformation from srow_*[]
- set miscellaneous NIFTI stuff
- set Miscellaneous ANALYZE stuff
- set ioff from vox_offset (but at least sizeof(header))
- deal with file names if set
struct nifti_1_header nifti_convert_nim2nhdr (const nifti_image *nim) [read] convert a nifti_image structure to a nifti_1_header struct
No allocation is done, this should be used via structure copy. As in:
nifti_1_header my_header;
my_header = nifti_convert_nim2nhdr(my_nim_pointer);
- load the ANALYZE-7.5 generic parts of the header struct
- Load NIFTI specific stuff into the header
nifti_image* nifti_copy_nim_info (const nifti_image *src) copy the nifti_image structure, without data
Duplicate the structure, including fname, iname and extensions. Leave the data pointer as NULL.
int nifti_datatype_from_string (const char *name) Given a NIFTI_TYPE string, such as 'NIFTI_TYPE_INT16', return the corresponding integral type code. The type code is the macro value defined in nifti1.h.
int nifti_datatype_is_valid (intdtype, intfor_nifti) Determine whether dtype is a valid NIFTI_TYPE.
DT_UNKNOWN is considered invalid
The only difference 'for_nifti' makes is that DT_BINARY should be invalid for a NIfTI dataset.
void nifti_datatype_sizes (intdatatype, int *nbyper, int *swapsize) Given a datatype code, set number of bytes per voxel and the swapsize.
Parameters:
nbyper pointer to return value: number of bytes per voxel
swapsize pointer to return value: size of swap blocks
Returns:
The swapsize is set to 0 if this datatype doesn't ever need swapping.
See also:
char* nifti_datatype_string (intdt) Return a pointer to a string holding the name of a NIFTI datatype.
Parameters:
Returns:
Warning:
See also:
char* nifti_datatype_to_string (intdtype) Given a NIFTI_TYPE value, such as NIFTI_TYPE_INT16, return the corresponding macro label as a string. The dtype code is the macro value defined in nifti1.h.
void nifti_disp_lib_hist (void) display the nifti library module history (via stdout)
void nifti_disp_lib_version (void) display the nifti library version (via stdout)
int nifti_disp_matrix_orient (const char *mesg, mat44mat) display the orientation from the quaternian fields
Parameters:
mat the matrix to convert to 'nearest' orientation
Returns:
int nifti_disp_type_list (intwhich) Display the nifti_type_list table.
if which == 1 : display DT_* if which == 2 : display NIFTI_TYPE* else : display all
char* nifti_find_file_extension (const char *name) check the end of the filename for a valid nifti extension
Valid extensions are currently .nii, .hdr, .img, .nia, or any of them followed by .gz. Note that '.' is part of the extension.
Uppercase extensions are also valid, but not mixed case.
Returns:
char* nifti_findhdrname (const char *fname) check current directory for existing header file
Returns:
If fname has an uppercase extension, check for uppercase files.
NB: it allocates memory for hdrname which should be freed when no longer required
- check input file(s) for sanity
- return filename if it has a valid extension and exists (except if it is an .img file (and maybe .gz))
- if .img, look for .hdr, .hdr.gz, .nii, .nii.gz, in that order
- else, look for .nii, .nii.gz, .hdr, .hdr.gz, in that order
- if nothing has been found, return NULL
char* nifti_findimgname (const char *fname, intnifti_type) check current directory for existing image file
Parameters:
Returns:
If fname has a valid, uppercase extension, apply all extensions as uppercase.
NB: it allocates memory for the image filename, which should be freed when no longer required
- test for .nii and .img (don't assume input type from image type)
- if nifti_type = 1, check for .nii first, else .img first
- if nothing has been found, return NULL
int nifti_free_extensions (nifti_image *nim) free the nifti extensions
- If any edata pointer is set in the extension list, free() it.
- Free ext_list, if it is set.
- Clear num_ext and ext_list from nim.
Returns:
See also:
void nifti_free_NBL (nifti_brick_list *NBL) nifti_free_NBL - free all pointers and clear structure
note: this does not presume to free the structure pointer
int nifti_get_filesize (const char *pathname) return the size of a file, in bytes
Returns:
changed to return int, -1 means no file or error 20 Dec 2004 [rickr]
int* nifti_get_intlist (intnvals, const char *str) Get an integer list in the range 0..(nvals-1), from the character string str. If we call the output pointer fred, then fred[0] = number of integers in the list (> 0), and fred[i] = i-th integer in the list for i=1..fred[0]. If on return, fred == NULL or fred[0] == 0, then something is wrong, and the caller must deal with that.
Syntax of input string:
- initial '{' or '[' is skipped, if present
- ends when '}' or ']' or end of string is found
- contains entries separated by commas
- entries have one of these forms:
- •
- a single number
- •
- a dollar sign '$', which means nvals-1
- •
- a sequence of consecutive numbers in the form 'a..b' or 'a-b', where 'a' and 'b' are single numbers (or '$')
- •
- a sequence of evenly spaced numbers in the form 'a..b(c)' or 'a-b(c)', where 'c' encodes the step
- Example: '[2,7..4,3..9(2)]' decodes to the list 2 7 6 5 4 3 5 7 9
- entries should be in the range 0..nvals-1
(borrowed, with permission, from thd_intlist.c)
- for each sub-selector until end of input...
- get starting value
- if that's it for this sub-selector, add one value to list
- otherwise, must have '..' or '-' as next inputs
- get ending value for loop now
- set default loop step
- check if we have a non-default loop step
- add values to output
- check if we have a comma to skip over
size_t nifti_get_volsize (const nifti_image *nim) return the total volume size, in bytes
This is computed as nvox * nbyper.
int nifti_hdr_looks_good (const nifti_1_header *hdr) decide if this nifti_1_header structure looks reasonable
Check dim[0], dim[1], sizeof_hdr, and datatype. Check magic string for 'n+1'. Maybe more tests will follow.
Returns:
See also:
void nifti_image_free (nifti_image *nim) free 'everything' about a nifti_image struct (including the passed struct)
free (only fields which are not NULL):
- fname and iname
- data
- any ext_list[i].edata
- ext_list
- nim
nifti_image* nifti_image_from_ascii (const char *str, int *bytes_read) Take an XML-ish ASCII string and create a NIFTI image header to match.
NULL is returned if enough information isn't present in the input string.
- The image data can later be loaded with nifti_image_load().
- The struct returned here can be liberated with nifti_image_free().
- Not a lot of error checking is done here to make sure that the input values are reasonable!
void nifti_image_infodump (const nifti_image *nim) Print to stdout some info about a nifti_image struct.
int nifti_image_load (nifti_image *nim)
Load the image blob into a previously initialized nifti_image. .IP "•" 2 If not yet set, the data buffer is allocated with calloc().
- The data buffer will be byteswapped if necessary.
- The data buffer will not be scaled.
This function is used to read the image from disk. It should be used after a function such as nifti_image_read(), so that the nifti_image structure is already initialized.
Parameters:
Returns:
See also:
- open the file and position the FILE pointer
- if the data pointer is not yet set, get memory space for the image
- now that everything is set up, do the reading
- close the file
int nifti_image_load_bricks (nifti_image *nim, intnbricks, const int *blist, nifti_brick_list *NBL) Load the image data from disk into an already-prepared image struct.
Parameters:
nbricks - the length of blist (must be 0 if blist is NULL)
blist - an array of xyz volume indices to read (can be NULL)
NBL - pointer to struct where resulting data will be stored
If blist is NULL, read all sub-bricks.
Returns:
NOTE: it is likely that another function will copy the data pointers out of NBL, in which case the only pointer the calling function will want to free is NBL->bricks (not each NBL->bricks[i]).
znzFile nifti_image_open (const char *hname, char *opts, nifti_image **nim)
Read in NIFTI-1 or ANALYZE-7.5 file (pair) header information into a nifti_image struct. znzFile nifti_image_open( char *hname, char *opts , nifti_image **nim)
- The image data is not read from disk (it may be read later using nifti_image_load(), for example).
- The image data will be stored in whatever data format the input data is; no scaling will be applied.
- DT_BINARY data is not supported.
- nifti_image_free() can be used to delete the returned struct, when you are done with it.
Parameters:
opts options string for opening the header file
nim pointer to pointer to nifti_image struct (this routine allocates the nifti_image struct)
Returns:
NULL if something fails badly.
See also:
nifti_image* nifti_image_read (const char *hname, intread_data)
Read a nifti header and optionally the data, creating a nifti_image. .IP "•" 2 The data buffer will be byteswapped if necessary.
- The data buffer will not be scaled.
- The data buffer is allocated with calloc().
Parameters:
read_data Flag, true=read data blob, false=don't read blob.
Returns:
See also:
- determine filename to use for header
- read binary header
- convert all nhdr fields to nifti_image fields
- check for extensions (any errors here means no extensions)
- read the data if desired, then bug out
nifti_image* nifti_image_read_bricks (const char *hname, intnbricks, const int *blist, nifti_brick_list *NBL) nifti_image_read_bricks - read nifti data as array of bricks
13 Dec 2004 [rickr]
Parameters:
nbricks - number of sub-bricks to read (if blist is valid, nbricks must be > 0)
blist - list of sub-bricks to read (can be NULL; if NULL, read complete dataset)
NBL - pointer to empty nifti_brick_list struct (must be a valid pointer)
Returns:
nim - same as nifti_image_read, but nim->nt = NBL->nbricks (or
nt*nu*nv*nw) nim->nu,nv,nw = 1 nim->data = NULL
NBL - filled with data volumes
By default, this function will read the nifti dataset and break the data into a list of nt*nu*nv*nw sub-bricks, each having size nx*ny*nz elements. That is to say, instead of reading the entire dataset as a single array, break it up into sub-bricks (volumes), each of size nx*ny*nz elements.
Note: in the returned nifti_image, nu, nv and nw will always be 1. The intention of this function is to collapse the dataset into a single array of volumes (of length nbricks or nt*nu*nv*nw).
If 'blist' is valid, it is taken to be a list of sub-bricks, of length 'nbricks'. The data will still be separated into sub-bricks of size nx*ny*nz elements, but now 'nbricks' sub-bricks will be returned, of the caller's choosing via 'blist'.
E.g. consider a dataset with 12 sub-bricks (numbered 0..11), and the following code:
{ nifti_brick_list NB_orig, NB_select;
nifti_image * nim_orig, * nim_select;
int blist[5] = { 7, 0, 5, 5, 9 };
nim_orig = nifti_image_read_bricks('myfile.nii', 0, NULL, &NB_orig);
nim_select = nifti_image_read_bricks('myfile.nii', 5, blist, &NB_select);
}
Here, nim_orig gets the entire dataset, where NB_orig.nbricks = 12. But nim_select has NB_select.nbricks = 5.
Note that the first case is not quite the same as just calling the nifti_image_read function, as here the data is separated into sub-bricks.
Note that valid blist elements are in [0..nt*nu*nv*nw-1], or written [ 0 .. (dim[4]*dim[5]*dim[6]*dim[7] - 1) ].
Note that, as is the case with all of the reading functions, the data will be allocated, read in, and properly byte-swapped, if necessary.
See also:
char* nifti_image_to_ascii (const nifti_image *nim) Dump the information in a NIFTI image header to an XML-ish ASCII string that can later be converted back into a NIFTI header in nifti_image_from_ascii().
The resulting string can be free()-ed when you are done with it.
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.
void nifti_image_unload (nifti_image *nim) Unload the data in a nifti_image struct, but keep the metadata.
void nifti_image_write (nifti_image *nim) Write a nifti_image to disk.
Since data is properly byte-swapped upon reading, it is assumed to be in the byte-order of the current CPU at write time. Thus, nim->byte_order should match that of the current CPU. Note that the nifti_set_filenames() function takes the flag, set_byte_order.
The following fields of nim affect how the output appears:
- nifti_type = 0 ==> ANALYZE-7.5 format file pair will be written
- nifti_type = 1 ==> NIFTI-1 format single file will be written (data offset will be 352+extensions)
- nifti_type = 2 ==> NIFTI_1 format file pair will be written
- nifti_type = 3 ==> NIFTI_1 ASCII single file will be written
- fname is the name of the output file (header or header+data)
- if a file pair is being written, iname is the name of the data file
- existing files WILL be overwritten with extreme prejudice
- if qform_code > 0, the quatern_*, qoffset_*, and qfac fields determine the qform output, NOT the qto_xyz matrix; if you want to compute these fields from the qto_xyz matrix, you can use the utility function nifti_mat44_to_quatern()
See also:
void nifti_image_write_bricks (nifti_image *nim, const nifti_brick_list *NBL) similar to nifti_image_write, but data is in NBL struct, not nim->data
See also:
znzFile nifti_image_write_hdr_img (nifti_image *nim, intwrite_data, const char *opts) write the nifti_image dataset to disk, optionally including data
This is just a front-end for nifti_image_write_hdr_img2.
Parameters:
write_data write options (see nifti_image_write_hdr_img2)
opts file open options ('wb' from nifti_image_write)
See also:
znzFile nifti_image_write_hdr_img2 (nifti_image *nim, intwrite_opts, const char *opts, znzFileimgfile, const nifti_brick_list *NBL) This writes the header (and optionally the image data) to file
If the image data file is left open it returns a valid znzFile handle. It also uses imgfile as the open image file is not null, and modifies it inside.
Parameters:
write_opts flags whether to write data and/or close file (see below)
opts file-open options, probably 'wb' from nifti_image_write()
imgfile optional open znzFile struct, for writing image data (may be NULL)
NBL optional nifti_brick_list, containing the image data (may be NULL)
Values for write_opts mode are based on two binary flags ( 0/1 for no-write/write data, and 0/2 for close/leave-open files ) :
- 0 = do not write data and close (do not open data file)
- 1 = write data and close
- 2 = do not write data and leave data file open
- 3 = write data and leave data file open
See also:
char* nifti_intent_string (intii) Return a pointer to a string holding the name of a NIFTI intent type.
Parameters:
Returns:
Warning:
See also:
int nifti_is_complete_filename (const char *fname) return whether the filename is valid
Note: uppercase extensions are now valid. 27 Apr 2009 [rickr]
The name is considered valid if the file basename has length greater than zero, AND one of the valid nifti extensions is provided. fname input | return | =============================== 'myimage' | 0 | 'myimage.tif' | 0 | 'myimage.tif.gz' | 0 | 'myimage.nii' | 1 | '.nii' | 0 | '.myhiddenimage' | 0 | '.myhiddenimage.nii' | 1 |
int nifti_is_gzfile (const char *fname) return whether the filename ends in '.gz'
int nifti_is_inttype (intdt) Determine if the datatype code dt is an integer type (1=YES, 0=NO).
Returns:
See also:
int nifti_is_valid_datatype (intdtype) check whether the given type is on the 'approved' list
The type is explicitly checked against the NIFTI_TYPE_* list in nifti1.h.
Returns:
See also:
int nifti_is_valid_ecode (intecode) check whether the extension code is valid
Returns:
nifti_1_header* nifti_make_new_header (const intarg_dims[], intarg_dtype) basic initialization of a nifti_1_header struct (with given dimensions)
Return an allocated nifti_1_header struct, based on the given dimensions and datatype.
Parameters:
arg_dtype : optional datatype (default DT_FLOAT32)
Returns:
nifti_image* nifti_make_new_nim (const intdims[], intdatatype, intdata_fill) basic creation of a nifti_image struct
Create a nifti_image from the given dimensions and data type. Optinally, allocate zero-filled data.
Parameters:
datatype : optional datatype (default DT_FLOAT32)
data_fill : if flag is set, allocate zero-filled data for image
Returns:
mat44 nifti_make_orthog_mat44 (floatr11, floatr12, floatr13, floatr21, floatr22, floatr23, floatr31, floatr32, floatr33) Input 9 floats and make an orthgonal mat44 out of them.
Each row is normalized, then nifti_mat33_polar() is used to orthogonalize them. If row #3 (r31,r32,r33) is input as zero, then it will be taken to be the cross product of rows #1 and #2.
This function can be used to create a rotation matrix for transforming an oblique volume to anatomical coordinates. For this application:
- row #1 (r11,r12,r13) is the direction vector along the image i-axis
- row #2 (r21,r22,r23) is the direction vector along the image j-axis
- row #3 (r31,r32,r33) is the direction vector along the slice direction (if available; otherwise enter it as 0's)
The first 2 rows can be taken from the DICOM attribute (0020,0037) 'Image Orientation (Patient)'.
After forming the rotation matrix, the complete affine transformation from (i,j,k) grid indexes to (x,y,z) spatial coordinates can be computed by multiplying each column by the appropriate grid spacing:
- column #1 (R.m[0][0],R.m[1][0],R.m[2][0]) by delta-x
- column #2 (R.m[0][1],R.m[1][1],R.m[2][1]) by delta-y
- column #3 (R.m[0][2],R.m[1][2],R.m[2][2]) by delta-z
and by then placing the center (x,y,z) coordinates of voxel (0,0,0) into the column #4 (R.m[0][3],R.m[1][3],R.m[2][3]).
See also:
char* nifti_makebasename (const char *fname) duplicate the filename, while clearing any extension
This allocates memory for basename which should eventually be freed.
char* nifti_makehdrname (const char *prefix, intnifti_type, intcheck, intcomp) creates a filename for storing the header, based on nifti_type
Parameters:
nifti_type - determines the extension, unless one is in prefix
check - check for existence (fail condition)
comp - add .gz for compressed name
Note that if prefix provides a file suffix, nifti_type is not used.
NB: this allocates memory which should be freed
See also:
char* nifti_makeimgname (const char *prefix, intnifti_type, intcheck, intcomp) creates a filename for storing the image, based on nifti_type
Parameters:
nifti_type - determines the extension, unless provided by prefix
check - check for existence (fail condition)
comp - add .gz for compressed name
Note that if prefix provides a file suffix, nifti_type is not used.
NB: it allocates memory which should be freed
See also:
float nifti_mat33_colnorm (mat33A) compute the max column norm of a 3x3 matrix
float nifti_mat33_determ (mat33R) compute the determinant of a 3x3 matrix
mat33 nifti_mat33_inverse (mat33R) compute the inverse of a 3x3 matrix
mat33 nifti_mat33_mul (mat33A, mat33B) multiply 2 3x3 matrices
mat33 nifti_mat33_polar (mat33A) polar decomposition of a 3x3 matrix
This finds the closest orthogonal matrix to input A (in both the Frobenius and L2 norms).
Algorithm is that from NJ Higham, SIAM J Sci Stat Comput, 7:1160-1174.
float nifti_mat33_rownorm (mat33A) compute the max row norm of a 3x3 matrix
mat44 nifti_mat44_inverse (mat44R) Compute the inverse of a bordered 4x4 matrix.
-
Some numerical code fragments were generated by Maple 8.
-
If a singular matrix is input, the output matrix will be all zero.
-
You can check for this by examining the [3][3] element, which will
be 1.0 for the normal case and 0.0 for the bad case.
The input matrix should have the form:
[ r11 r12 r13 v1 ]
[ r21 r22 r23 v2 ]
[ r31 r32 r33 v3 ]
[ 0 0 0 1 ]
void nifti_mat44_to_orientation (mat44R, int *icod, int *jcod, int *kcod) compute the (closest) orientation from a 4x4 ijk->xyz tranformation matrix
Input: 4x4 matrix that transforms (i,j,k) indexes to (x,y,z) coordinates,
where +x=Right, +y=Anterior, +z=Superior.
(Only the upper-left 3x3 corner of R is used herein.)
Output: 3 orientation codes that correspond to the closest 'standard'
anatomical orientation of the (i,j,k) axes.
Method: Find which permutation of (x,y,z) has the smallest angle to the
(i,j,k) axes directions, which are the columns of the R matrix.
Errors: The codes returned will be zero.
For example, an axial volume might get return values of icod = NIFTI_R2L (i axis is mostly Right to Left) jcod = NIFTI_P2A (j axis is mostly Posterior to Anterior) kcod = NIFTI_I2S (k axis is mostly Inferior to Superior)
See also:
nifti_quatern_to_mat44, nifti_mat44_to_quatern, nifti_make_orthog_mat44
void nifti_mat44_to_quatern (mat44R, float *qb, float *qc, float *qd, float *qx, float *qy, float *qz, float *dx, float *dy, float *dz, float *qfac) Given the 3x4 upper corner of the matrix R, compute the quaternion parameters that fit it.
- Any NULL pointer on input won't get assigned (e.g., if you don't want dx,dy,dz, just pass NULL in for those pointers).
- If the 3 input matrix columns are NOT orthogonal, they will be orthogonalized prior to calculating the parameters, using the polar decomposition to find the orthogonal matrix closest to the column-normalized input matrix.
- However, if the 3 input matrix columns are NOT orthogonal, then the matrix produced by nifti_quatern_to_mat44 WILL have orthogonal columns, so it won't be the same as the matrix input here. This 'feature' is because the NIFTI 'qform' transform is deliberately not fully general -- it is intended to model a volume with perpendicular axes.
- If the 3 input matrix columns are not even linearly independent, you'll just have to take your luck, won't you?
See also:
nifti_quatern_to_mat44, nifti_make_orthog_mat44, nifti_mat44_to_orientation
int nifti_nim_has_valid_dims (nifti_image *nim, intcomplain) validate nifti dimensions
Returns:
See also:
rely on dim[] as the master
- start with dim[0]: failure here is considered terminal
- check whether ndim equals dim[0]
- compare each dim[i] to the proper nx, ny, ...
- check the dimensions, and that their product matches nvox
- if debug, warn about any remaining dim that is neither 0, nor 1
- return invalid or valid
int nifti_nim_is_valid (nifti_image *nim, intcomplain) validate the nifti_image
Returns:
See also:
- check that dim[] matches the individual values ndim, nx, ny, ...
- be explicit in return of 0 or 1
char* nifti_orientation_string (intii) Return a pointer to a string holding the name of a NIFTI orientation.
Parameters:
Returns:
Warning:
See also:
mat44 nifti_quatern_to_mat44 (floatqb, floatqc, floatqd, floatqx, floatqy, floatqz, floatdx, floatdy, floatdz, floatqfac) Given the quaternion parameters (etc.), compute a transformation matrix.
See comments in nifti1.h for details.
- qb,qc,qd = quaternion parameters
- qx,qy,qz = offset parameters
- dx,dy,dz = grid stepsizes (non-negative inputs are set to 1.0)
- qfac = sign of dz step (< 0 is negative; >= 0 is positive)
If qx=qy=qz=0, dx=dy=dz=1, then the output is a rotation matrix.
For qfac >= 0, the rotation is proper.
For qfac < 0, the rotation is improper.
See also:
nifti_mat44_to_quatern, nifti_make_orthog_mat44, nifti_mat44_to_orientation
nifti_image* nifti_read_ascii_image (znzFilefp, char *fname, intflen, intread_data) nifti_read_ascii_image - process as a type-3 .nia image file
return NULL on failure
NOTE: this is NOT part of the NIFTI-1 standard
size_t nifti_read_buffer (znzFilefp, void *dataptr, size_tntot, nifti_image *nim) read ntot bytes of data from an open file and byte swaps if necessary
note that nifti_image is required for information on datatype, bsize (for any needed byte swapping), etc.
This function does not allocate memory, so dataptr must be valid.
int nifti_read_collapsed_image (nifti_image *nim, const intdims[8], void **data) read a nifti image, collapsed across dimensions according to dims[8]
This function may be used to read parts of a nifti dataset, such as
the time series for a single voxel, or perhaps a slice. It is similar
to nifti_image_load(), though the passed 'data' parameter is used for
returning the image, not nim->data.
Parameters:
nim given nifti_image struct, corresponding to the data file
dims given list of dimensions (see below)
data pointer to data pointer (if *data is NULL, data will be
allocated, otherwise not)
Here, dims is an array of 8 ints, similar to nim->dim[8]. While dims[0]
is unused at this point, the other indices specify which dimensions to
collapse (and at which index), and which not to collapse. If dims[i] is
set to -1, then that entire dimension will be read in, from index 0 to
index (nim->dim[i] - 1). If dims[i] >= 0, then only that index will be
read in (so dims[i] must also be < nim->dim[i]).
Example: given nim->dim[8] = { 4, 64, 64, 21, 80, 1, 1, 1 } (4-D dataset)
if dims[8] = { 0, 5, 4, 17, -1, -1, -1, -1 }
-> read time series for voxel i,j,k = 5,4,17
if dims[8] = { 0, -1, -1, -1, 17, -1, -1, -1 }
-> read single volume at time point 17
Example: given nim->dim[8] = { 6, 64, 64, 21, 80, 4, 3, 1 } (6-D dataset)
if dims[8] = { 0, 5, 4, 17, -1, 2, 1, 0 }
-> read time series for the voxel i,j,k = 5,4,17, and dim 5,6 = 2,1
if dims[8] = { 0, 5, 4, -1, -1, 0, 0, 0 }
-> read time series for slice at i,j = 5,4, and dim 5,6,7 = 0,0,0
(note that dims[7] is not relevant, but must be 0 or -1)
If *data is NULL, then *data will be set as a pointer to new memory,
allocated here for the resulting collapsed image data.
e.g. { int dims[8] = { 0, 5, 4, 17, -1, -1, -1, -1 };
void * data = NULL;
ret_val = nifti_read_collapsed_image(nim, dims, &data);
if( ret_val > 0 ){
process_time_series(data);
if( data != NULL ) free(data);
}
}
NOTE: If *data is not NULL, then it will be assumed that it points to
valid memory, sufficient to hold the results. This is done for
speed and possibly repeated calls to this function.
e.g. { int dims[8] = { 0, -1, -1, -1, -1, -1, -1, -1 };
void * data = NULL;
for( zslice = 0; zslice < nzslices; zslice++ ){
dims[3] = zslice;
ret_val = nifti_read_collapsed_image(nim, dims, &data);
if( ret_val > 0 ) process_slice(zslice, data);
}
if( data != NULL ) free(data);
}
Returns:
-
the total number of bytes read, or < 0 on failure
-
the read and byte-swapped data, in 'data'
See also:
nifti_image_read, nifti_image_free, nifti_image_read_bricks
nifti_image_load
- check pointers for sanity
- verify that dim[] makes sense
- verify that dims[] makes sense for this dataset
- prepare pivot list - pivots are fixed indices
- open the image file for reading at the appropriate offset
- call the recursive reading function, passing nim, the pivot info, location to store memory, and file pointer and position
nifti_1_header* nifti_read_header (const char *hname, int *swapped, intcheck) return an allocated and filled nifti_1_header struct
Read the binary header from disk, and swap bytes if necessary.
Returns:
Parameters:
swapped if not NULL, return whether header bytes were swapped
check flag to check for invalid nifti_1_header
Warning:
See also:
int nifti_read_subregion_image (nifti_image *nim, int *start_index, int *region_size, void **data) read an arbitrary subregion from a nifti image
This function may be used to read a single arbitary subregion of any rectangular size from a nifti dataset, such as a small 5x5x5 subregion around the center of a 3D image.
Parameters:
start_index the index location of the first voxel that will be returned
region_size the size of the subregion to be returned
data pointer to data pointer (if *data is NULL, data will be allocated, otherwise not)
Example: given nim->dim[8] = {3, 64, 64, 64, 1, 1, 1, 1 } (3-D dataset)
if start_index[7] = { 29, 29, 29, 0, 0, 0, 0 } and region_size[7] = { 5, 5, 5, 1, 1, 1, 1 } -> read 5x5x5 region starting with the first voxel location at (29,29,29)
NOTE: If *data is not NULL, then it will be assumed that it points to valid memory, sufficient to hold the results. This is done for speed and possibly repeated calls to this function.
Returns:
- the total number of bytes read, or < 0 on failure
- the read and byte-swapped data, in 'data'
See also:
void nifti_set_allow_upper_fext (intallow) set nifti's global allow_upper_fext flag 28 Apr 2009 [rickr]
explicitly set to 0 or 1
void nifti_set_debug_level (intlevel) set nifti's global debug level, for status reporting
- 0 : quiet, nothing is printed to the terminal, but errors
- 1 : normal execution (the default)
- 2, 3 : more details
int nifti_set_filenames (nifti_image *nim, const char *prefix, intcheck, intset_byte_order) create and set new filenames, based on prefix and image type
Parameters:
prefix (required) prefix for output filenames
check check for previous existence of filename (existence is an error condition)
set_byte_order flag to set nim->byteorder here (this is probably a logical place to do so)
Returns:
Warning:
See also:
void nifti_set_iname_offset (nifti_image *nim) set the nifti_image iname_offset field, based on nifti_type
- if writing to 2 files, set offset to 0
- if writing to a single NIFTI-1 file, set the offset to 352 + total extension size, then align to 16-byte boundary
- if writing an ASCII header, set offset to -1
void nifti_set_skip_blank_ext (intskip) set nifti's global skip_blank_ext flag 5 Sep 2006 [rickr]
explicitly set to 0 or 1
int nifti_set_type_from_names (nifti_image *nim) set the nifti_type field based on fname and iname
Note that nifti_type is changed only when it does not match the filenames.
Returns:
See also:
int nifti_short_order (void) get the byte order for this CPU
- LSB_FIRST means least significant byte, first (little endian)
- MSB_FIRST means most significant byte, first (big endian)
nifti_image* nifti_simple_init_nim (void) basic initialization of a nifti_image struct (to a 1x1x1 image)
char* nifti_slice_string (intss) Return a pointer to a string holding the name of a NIFTI slice_code.
Parameters:
Returns:
Warning:
See also:
char* nifti_strdup (const char *str) duplicate the given string (alloc length+1)
Returns:
void nifti_swap_16bytes (size_tn, void *ar) swap 16 bytes at a time from the given list of n sets of 16 bytes
void nifti_swap_2bytes (size_tn, void *ar) swap each byte pair from the given list of n pairs
Due to alignment of structures at some architectures (e.g. on ARM), stick to char varaibles. Fixes http://bugs.debian.org/446893 Yaroslav <debian@onerussian.com>
void nifti_swap_4bytes (size_tn, void *ar) swap 4 bytes at a time from the given list of n sets of 4 bytes
void nifti_swap_8bytes (size_tn, void *ar) swap 8 bytes at a time from the given list of n sets of 8 bytes
perhaps use this style for the general Nbytes, as Yaroslav suggests
int nifti_swap_as_analyze (nifti_analyze75 *h) Byte swap as an ANALYZE 7.5 header
return non-zero on failure
void nifti_swap_Nbytes (size_tn, intsiz, void *ar) based on siz, call the appropriate nifti_swap_Nbytes() function
int nifti_test_datatype_sizes (intverb) Only as a test, verify that the new nifti_type_list table matches the the usage of nifti_datatype_sizes (which could be changed to use the table, if there were interest).
return the number of errors (so 0 is success, as usual)
int nifti_type_and_names_match (nifti_image *nim, intshow_warn) check whether nifti_type matches fname and iname for the nifti_image
- if type 0 or 2, expect .hdr/.img pair
- if type 1, expect .nii (and names must match)
Parameters:
show_warn if set, print a warning message for any mis-match
Returns:
- 1 if the values seem to match
- 0 if there is a mis-match
- -1 if there is not sufficient information to create file(s)
See also:
nifti_set_type_from_names, is_valid_nifti_type
char* nifti_units_string (intuu) Return a pointer to a string holding the name of a NIFTI units type.
Parameters:
Returns:
Warning:
See also:
int nifti_update_dims_from_array (nifti_image *nim) nifti_update_dims_from_array - update nx, ny, ... from nim->dim[]
Fix all the dimension information, based on a new nim->dim[].
Note: we assume that dim[0] will not increase.
Check for updates to pixdim[], dx,..., nx,..., nvox, ndim, dim[0].
int nifti_validfilename (const char *fname) return whether the filename is valid
Allow uppercase extensions as valid. 27 Apr 2009 [rickr] Any .gz extension case must match the base extension case.
The name is considered valid if its length is positive, excluding any nifti filename extension. fname input | return | result of nifti_makebasename ==================================================================== 'myimage' | 1 | 'myimage' 'myimage.tif' | 1 | 'myimage.tif' 'myimage.tif.gz' | 1 | 'myimage.tif' 'myimage.nii' | 1 | 'myimage' '.nii' | 0 | <ERROR - basename has zero length> '.myhiddenimage' | 1 | '.myhiddenimage' '.myhiddenimage.nii | 1 | '.myhiddenimage"
int nifti_write_all_data (znzFilefp, nifti_image *nim, const nifti_brick_list *NBL) write the nifti_image data to file (from nim->data or from NBL)
If NBL is not NULL, write the data from that structure. Otherwise, write it out from nim->data. No swapping is done here.
Parameters:
nim : nifti_image corresponding to the data
NBL : optional source of write data (if NULL use nim->data)
Returns:
Note: the nifti_image byte_order is set as that of the current CPU. This is because such a conversion was made to the data upon reading, while byte_order was not set (so the programs would know what format the data was on disk). Effectively, since byte_order should match what is on disk, it should bet set to that of the current CPU whenever new filenames are assigned.
znzFile nifti_write_ascii_image (nifti_image *nim, const nifti_brick_list *NBL, const char *opts, intwrite_data, intleave_open) write a nifti_image to disk in ASCII format
char* nifti_xform_string (intxx) Return a pointer to a string holding the name of a NIFTI transform type.
Parameters:
Returns:
Warning:
See also:
void old_swap_nifti_header (struct nifti_1_header *h, intis_nifti) OLD VERSION of swap_nifti_header (left for undo/compare operations)
Byte swap NIFTI-1 file header in various places and ways.
If is_nifti is nonzero, will also swap the NIFTI-specific components of the header; otherwise, only the components common to NIFTI and ANALYZE will be swapped.
void swap_nifti_header (struct nifti_1_header *h, intis_nifti) Byte swap NIFTI-1 file header in various places and ways.
If is_nifti, swap all (even UNUSED) fields of NIfTI header. Else, swap as a nifti_analyze75 struct.
int valid_nifti_brick_list (nifti_image *nim, intnbricks, const int *blist, intdisp_error) valid_nifti_brick_list - check sub-brick list for image
This function verifies that nbricks and blist are appropriate for use with this nim, based on the dimensions.
Parameters:
nbricks number of brick indices in blist
blist list of brick indices to check in nim
disp_error if this flag is set, report errors to user
Returns:
int valid_nifti_extensions (const nifti_image *nim) for each extension, check code, size and data pointer
Author
Generated automatically by Doxygen for nifti1_io from the source code.
| Sat Apr 16 2011 | nifti1_io |
