#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include "nifti1.h"
#include <znzlib.h>
Go to the source code of this file.
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. More... | |
struct | nifti_brick_list |
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_MAX_ECODE 14 |
#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 |
Old-style analyze75 orientation codes. | |
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 (int n, void *ar) |
void | nifti_swap_4bytes (int n, void *ar) |
void | nifti_swap_8bytes (int n, void *ar) |
void | nifti_swap_16bytes (int n, void *ar) |
void | nifti_swap_Nbytes (int n, int siz, void *ar) |
int | nifti_datatype_from_string (const char *name) |
char * | nifti_datatype_to_string (int dtype) |
void | swap_nifti_header (struct nifti_1_header *h, int is_nifti) |
int | nifti_get_filesize (const char *pathname) |
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) |
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) |
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_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) |
|
display the contents of the nifti_1_header (send to stdout)
|
|
Determine if this is a NIFTI-formatted file.
|
|
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.
|
|
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.
|
|
convert a nifti_1_header into a nift1_image
|
|
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);
|
|
copy the nifti_image structure, without data Duplicate the structure, including fname, iname and extensions. Leave the data pointer as NULL. |
|
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. |
|
Given a datatype code, set number of bytes per voxel and the swapsize.
|
|
Return a pointer to a string holding the name of a NIFTI datatype.
|
|
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. |
|
display the nifti library module history (via stdout) |
|
display the nifti library version (via stdout) |
|
display the orientation from the quaternian fields
|
|
Display the nifti_type_list table. if which == 1 : display DT_* if which == 2 : display NIFTI_TYPE* else : display all |
|
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.
|
|
check current directory for existing header file
|
|
check current directory for existing image file
|
|
free the nifti extensions
|
|
nifti_free_NBL - free all pointers and clear structure note: this does not presume to free the structure pointer |
|
return the size of a file, in bytes
|
|
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:
(borrowed, with permission, from thd_intlist.c)
|
|
return the total volume size, in bytes This is computed as nvox * nbyper. |
|
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.
|
|
free 'everything' about a nifti_image struct (including the passed struct) free (only fields which are not NULL):
|
|
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.
|
|
Print to stdout some info about a nifti_image struct. |
|
Load the image blob into a previously initialized nifti_image.
|
|
Load the image data from disk into an already-prepared image struct.
|
|
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).
|
|
Read a nifti header and optionally the data, creating a nifti_image.
|
|
nifti_image_read_bricks - read nifti data as array of bricks 13 Dec 2004 [rickr]
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 = 11. 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.
|
|
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":
|
|
Unload the data in a nifti_image struct, but keep the metadata. |
|
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:
|
|
similar to nifti_image_write, but data is in NBL struct, not nim->data
|
|
write the nifti_image dataset to disk, optionally including data This is just a front-end for nifti_image_write_hdr_img2.
|
|
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.
|
|
Return a pointer to a string holding the name of a NIFTI intent type.
|
|
return whether the filename is valid 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 | |
|
return whether the filename ends in ".gz" |
|
Determine if the datatype code dt is an integer type (1=YES, 0=NO).
|
|
check whether the given type is on the "approved" list The type is explicitly checked against the NIFTI_TYPE_* list in nifti1.h.
|
|
check whether the extension code is valid
|
|
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.
|
|
basic creation of a nifti_image struct Create a nifti_image from the given dimensions and data type. Optinally, allocate zero-filled data.
|
|
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:
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:
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]).
|
|
duplicate the filename, while clearing any extension This allocates memory for basename which should eventually be freed. |
|
creates a filename for storing the header, based on nifti_type
NB: this allocates memory which should be freed
|
|
creates a filename for storing the image, based on nifti_type
NB: it allocates memory which should be freed
|
|
compute the max column norm of a 3x3 matrix |
|
compute the determinant of a 3x3 matrix |
|
compute the inverse of a 3x3 matrix |
|
multiply 2 3x3 matrices |
|
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. |
|
compute the max row norm of a 3x3 matrix |
|
Compute the inverse of a bordered 4x4 matrix.
The input matrix should have the form: [ r11 r12 r13 v1 ] [ r21 r22 r23 v2 ] [ r31 r32 r33 v3 ] [ 0 0 0 1 ] |
|
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)
|
|
Given the 3x4 upper corner of the matrix R, compute the quaternion parameters that fit it.
|
|
validate nifti dimensions
|
|
validate the nifti_image
|
|
Return a pointer to a string holding the name of a NIFTI orientation.
|
|
Given the quaternion parameters (etc.), compute a transformation matrix. See comments in nifti1.h for details.
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.
|
|
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 |
|
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. |
|
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.
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); }
|
|
return an allocated and filled nifti_1_header struct Read the binary header from disk, and swap bytes if necessary.
|
|
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.
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.
|
|
set nifti's global debug level, for status reporting
|
|
create and set new filenames, based on prefix and image type
|
|
set the nifti_image iname_offset field, based on nifti_type
|
|
set nifti's global skip_blank_ext flag 5 Sep 2006 [rickr] explicitly set to 0 or 1 |
|
set the nifti_type field based on fname and iname Note that nifti_type is changed only when it does not match the filenames.
|
|
get the byte order for this CPU
|
|
basic initialization of a nifti_image struct (to a 1x1x1 image) |
|
Return a pointer to a string holding the name of a NIFTI slice_code.
|
|
duplicate the given string (alloc length+1)
|
|
swap 16 bytes at a time from the given list of n sets of 16 bytes |
|
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> |
|
swap 4 bytes at a time from the given list of n sets of 4 bytes |
|
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 |
|
based on siz, call the appropriate nifti_swap_Nbytes() function |
|
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) |
|
check whether nifti_type matches fname and iname for the nifti_image
|
|
Return a pointer to a string holding the name of a NIFTI units type.
|
|
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]. |
|
return whether the filename is valid 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" |
|
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.
|
|
write a nifti_image to disk in ASCII format |
|
Return a pointer to a string holding the name of a NIFTI transform type.
|
|
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. |
|
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.
|
|
for each extension, check code, size and data pointer |