diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..16402a8 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,20 @@ +# https://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Apply to all code files +[*] + +# A newline ending every file +insert_final_newline = true + +# Set default charset +charset = utf-8 + +# 3 space indentation +indent_style = space +indent_size = 3 + +# Various options +trim_trailing_whitespace = true diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..20ae9d2 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,14 @@ +# +# This file lists revisions that should be ignored when considering +# attribution for the actual code written. Code style changes should +# not be considered as modifications with regards to attribution. +# +# To see clean and meaningful blame information. +# $ git blame important.py --ignore-revs-file .git-blame-ignore-revs +# +# To configure git to automatically ignore revisions listed in a file on every call to git blame. +# $ git config blame.ignoreRevsFile .git-blame-ignore-revs +# + +# TODO: add reformatting commits + diff --git a/niftilib/nifti1.h b/niftilib/nifti1.h index c5677a3..90762e5 100644 --- a/niftilib/nifti1.h +++ b/niftilib/nifti1.h @@ -9,7 +9,7 @@ TIME_SERIES, NODE_INDEX, RGB_VECTOR, RGBA_VECTOR, SHAPE 08 Mar 2019 [PT,DRG] - - Updated to include [qs]form_code = 5 + - Updated to include [qs]form_code = 5 */ @@ -149,61 +149,61 @@ struct nifti_1_header { /* NIFTI-1 usage */ /* ANALYZE 7.5 field(s) */ /*************************/ /************************/ /*--- was header_key substruct ---*/ - int sizeof_hdr; /*!< MUST be 348 */ /* int sizeof_hdr; */ - char data_type[10]; /*!< ++UNUSED++ */ /* char data_type[10]; */ - char db_name[18]; /*!< ++UNUSED++ */ /* char db_name[18]; */ - int extents; /*!< ++UNUSED++ */ /* int extents; */ - short session_error; /*!< ++UNUSED++ */ /* short session_error; */ - char regular; /*!< ++UNUSED++ */ /* char regular; */ - char dim_info; /*!< MRI slice ordering. */ /* char hkey_un0; */ + int sizeof_hdr; /*!< MUST be 348 */ /* int sizeof_hdr; */ + char data_type[10];/*!< ++UNUSED++ */ /* char data_type[10]; */ + char db_name[18]; /*!< ++UNUSED++ */ /* char db_name[18]; */ + int extents; /*!< ++UNUSED++ */ /* int extents; */ + short session_error; /*!< ++UNUSED++ */ /* short session_error; */ + char regular; /*!< ++UNUSED++ */ /* char regular; */ + char dim_info; /*!< MRI slice ordering. */ /* char hkey_un0; */ /*--- was image_dimension substruct ---*/ - short dim[8]; /*!< Data array dimensions.*/ /* short dim[8]; */ - float intent_p1 ; /*!< 1st intent parameter. */ /* short unused8; */ + short dim[8]; /*!< Data array dimensions.*/ /* short dim[8]; */ + float intent_p1 ; /*!< 1st intent parameter. */ /* short unused8; */ /* short unused9; */ - float intent_p2 ; /*!< 2nd intent parameter. */ /* short unused10; */ + float intent_p2 ; /*!< 2nd intent parameter. */ /* short unused10; */ /* short unused11; */ - float intent_p3 ; /*!< 3rd intent parameter. */ /* short unused12; */ + float intent_p3 ; /*!< 3rd intent parameter. */ /* short unused12; */ /* short unused13; */ - short intent_code ; /*!< NIFTI_INTENT_* code. */ /* short unused14; */ - short datatype; /*!< Defines data type! */ /* short datatype; */ - short bitpix; /*!< Number bits/voxel. */ /* short bitpix; */ - short slice_start; /*!< First slice index. */ /* short dim_un0; */ - float pixdim[8]; /*!< Grid spacings. */ /* float pixdim[8]; */ - float vox_offset; /*!< Offset into .nii file */ /* float vox_offset; */ - float scl_slope ; /*!< Data scaling: slope. */ /* float funused1; */ - float scl_inter ; /*!< Data scaling: offset. */ /* float funused2; */ - short slice_end; /*!< Last slice index. */ /* float funused3; */ - char slice_code ; /*!< Slice timing order. */ - char xyzt_units ; /*!< Units of pixdim[1..4] */ - float cal_max; /*!< Max display intensity */ /* float cal_max; */ - float cal_min; /*!< Min display intensity */ /* float cal_min; */ - float slice_duration;/*!< Time for 1 slice. */ /* float compressed; */ - float toffset; /*!< Time axis shift. */ /* float verified; */ - int glmax; /*!< ++UNUSED++ */ /* int glmax; */ - int glmin; /*!< ++UNUSED++ */ /* int glmin; */ + short intent_code ; /*!< NIFTI_INTENT_* code. */ /* short unused14; */ + short datatype; /*!< Defines data type! */ /* short datatype; */ + short bitpix; /*!< Number bits/voxel. */ /* short bitpix; */ + short slice_start; /*!< First slice index. */ /* short dim_un0; */ + float pixdim[8]; /*!< Grid spacings. */ /* float pixdim[8]; */ + float vox_offset; /*!< Offset into .nii file */ /* float vox_offset; */ + float scl_slope ; /*!< Data scaling: slope. */ /* float funused1; */ + float scl_inter ; /*!< Data scaling: offset. */ /* float funused2; */ + short slice_end; /*!< Last slice index. */ /* float funused3; */ + char slice_code ; /*!< Slice timing order. */ + char xyzt_units ; /*!< Units of pixdim[1..4] */ + float cal_max; /*!< Max display intensity */ /* float cal_max; */ + float cal_min; /*!< Min display intensity */ /* float cal_min; */ + float slice_duration;/*!< Time for 1 slice. */ /* float compressed; */ + float toffset; /*!< Time axis shift. */ /* float verified; */ + int glmax; /*!< ++UNUSED++ */ /* int glmax; */ + int glmin; /*!< ++UNUSED++ */ /* int glmin; */ /*--- was data_history substruct ---*/ - char descrip[80]; /*!< any text you like. */ /* char descrip[80]; */ - char aux_file[24]; /*!< auxiliary filename. */ /* char aux_file[24]; */ + char descrip[80]; /*!< any text you like. */ /* char descrip[80]; */ + char aux_file[24];/*!< auxiliary filename. */ /* char aux_file[24]; */ - short qform_code ; /*!< NIFTI_XFORM_* code. */ /*-- all ANALYZE 7.5 ---*/ - short sform_code ; /*!< NIFTI_XFORM_* code. */ /* fields below here */ + short qform_code ; /*!< NIFTI_XFORM_* code. */ /*-- all ANALYZE 7.5 ---*/ + short sform_code ; /*!< NIFTI_XFORM_* code. */ /* fields below here */ /* are replaced */ - float quatern_b ; /*!< Quaternion b param. */ - float quatern_c ; /*!< Quaternion c param. */ - float quatern_d ; /*!< Quaternion d param. */ - float qoffset_x ; /*!< Quaternion x shift. */ - float qoffset_y ; /*!< Quaternion y shift. */ - float qoffset_z ; /*!< Quaternion z shift. */ + float quatern_b ; /*!< Quaternion b param. */ + float quatern_c ; /*!< Quaternion c param. */ + float quatern_d ; /*!< Quaternion d param. */ + float qoffset_x ; /*!< Quaternion x shift. */ + float qoffset_y ; /*!< Quaternion y shift. */ + float qoffset_z ; /*!< Quaternion z shift. */ - float srow_x[4] ; /*!< 1st row affine transform. */ - float srow_y[4] ; /*!< 2nd row affine transform. */ - float srow_z[4] ; /*!< 3rd row affine transform. */ + float srow_x[4] ; /*!< 1st row affine transform. */ + float srow_y[4] ; /*!< 2nd row affine transform. */ + float srow_z[4] ; /*!< 3rd row affine transform. */ - char intent_name[16];/*!< 'name' or meaning of data. */ + char intent_name[16];/*!< 'name' or meaning of data. */ - char magic[4] ; /*!< MUST be "ni1\0" or "n+1\0". */ + char magic[4] ; /*!< MUST be "ni1\0" or "n+1\0". */ } ; /**** 348 bytes total ****/ diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index 624c41e..6c314cc 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -36,377 +36,377 @@ /*! global history and version strings, for printing */ static char const * const gni_history[] = { - "----------------------------------------------------------------------\n" - "history (of nifti library changes):\n" - "\n", - "0.0 August, 2003 [rwcox]\n" - " (Robert W Cox of the National Institutes of Health, SSCC/DIRP/NIMH)\n" - " - initial version\n" - "\n", - "0.1 July/August, 2004 [Mark Jenkinson]\n" - " (FMRIB Centre, University of Oxford, UK)\n" - " - Mainly adding low-level IO and changing things to allow gzipped\n" - " files to be read and written\n" - " - Full backwards compatibility should have been maintained\n" - "\n", - "0.2 16 Nov 2004 [rickr]\n" - " (Rick Reynolds of the National Institutes of Health, SSCC/DIRP/NIMH)\n" - " - included Mark's changes in the AFNI distribution (including znzlib/)\n" - " (HAVE_ZLIB is commented out for the standard distribution)\n" - " - modified nifti_validfilename() and nifti_makebasename()\n" - " - added nifti_find_file_extension()\n" - "\n", - "0.3 3 Dec 2004 [rickr]\n" - " - note: header extensions are not yet checked for\n" - " - added formatted history as global string, for printing\n" - " - added nifti_disp_lib_hist(), to display the nifti library history\n" - " - added nifti_disp_lib_version(), to display the nifti library history\n", - " - re-wrote nifti_findhdrname()\n" - " o used nifti_find_file_extension()\n" - " o changed order of file tests (default is .nii, depends on input)\n" - " o free hdrname on failure\n" - " - made similar changes to nifti_findimgname()\n" - " - check for NULL return from nifti_findhdrname() calls\n", - " - removed most of ERREX() macros\n" - " - modified nifti_image_read()\n" - " o added debug info and error checking (on gni_debug > 0, only)\n" - " o fail if workingname is NULL\n" - " o check for failure to open header file\n" - " o free workingname on failure\n" - " o check for failure of nifti_image_load()\n" - " o check for failure of nifti_convert_nhdr2nim()\n", - " - changed nifti_image_load() to int, and check nifti_read_buffer return\n" - " - changed nifti_read_buffer() to fail on short read, and to count float\n" - " fixes (to print on debug)\n" - " - changed nifti_image_infodump to print to stderr\n" - " - updated function header comments, or moved comments above header\n" - " - removed const keyword\n" - " - added LNI_FERR() macro for error reporting on input files\n" - "\n", - "0.4 10 Dec 2004 [rickr] - added header extensions\n" - " - in nifti1_io.h:\n" - " o added num_ext and ext_list to the definition of nifti_image\n" - " o made many functions static (more to follow)\n" - " o added LNI_MAX_NIA_EXT_LEN, for max nifti_type 3 extension length\n", - " - added __DATE__ to version output in nifti_disp_lib_version()\n" - " - added nifti_disp_matrix_orient() to print orientation information\n" - " - added '.nia' as a valid file extension in nifti_find_file_extension()\n" - " - added much more debug output\n" - " - in nifti_image_read(), in the case of an ASCII header, check for\n" - " extensions after the end of the header\n", - " - added nifti_read_extensions() function\n" - " - added nifti_read_next_extension() function\n" - " - added nifti_add_exten_to_list() function\n" - " - added nifti_check_extension() function\n" - " - added nifti_write_extensions() function\n" - " - added nifti_extension_size() function\n" - " - in nifti_set_iname_offest():\n" - " o adjust offset by the extension size and the extender size\n", - " o fixed the 'ceiling modulo 16' computation\n" - " - in nifti_image_write_hdr_img2(): \n" - " o added extension writing\n" - " o check for NULL return from nifti_findimgname()\n" - " - include number of extensions in nifti_image_to_ascii() output\n" - " - in nifti_image_from_ascii():\n" - " o return bytes_read as a parameter, computed from the final spos\n" - " o extract num_ext from ASCII header\n" - "\n", - "0.5 14 Dec 2004 [rickr] - added sub-brick reading functions\n" - " - added nifti_brick_list type to nifti1_io.h, along with new prototypes\n" - " - added main nifti_image_read_bricks() function, with description\n" - " - added nifti_image_load_bricks() - library function (requires nim)\n" - " - added valid_nifti_brick_list() - library function\n" - " - added free_NBL() - library function\n", - " - added update_nifti_image_for_brick_list() for dimension update\n" - " - added nifti_load_NBL_bricks(), nifti_alloc_NBL_mem(),\n" - " nifti_copynsort() and force_positive() (static functions)\n" - " - in nifti_image_read(), check for failed load only if read_data is set\n" - " - broke most of nifti_image_load() into nifti_image_load_prep()\n" - "\n", - "0.6 15 Dec 2004 [rickr] - added sub-brick writing functionality\n" - " - in nifti1_io.h, removed znzlib directory from include - all nifti\n" - " library files are now under the nifti directory\n" - " - nifti_read_extensions(): print no offset warning for nifti_type 3\n" - " - nifti_write_all_data():\n" - " o pass nifti_brick_list * NBL, for optional writing\n" - " o if NBL, write each sub-brick, sequentially\n", - " - nifti_set_iname_offset(): case 1 must have sizeof() cast to int\n" - " - pass NBL to nifti_image_write_hdr_img2(), and allow NBL or data\n" - " - added nifti_image_write_bricks() wrapper for ...write_hdr_img2()\n" - " - included compression abilities\n" - "\n", - "0.7 16 Dec 2004 [rickr] - minor changes to extension reading\n" - "\n", - "0.8 21 Dec 2004 [rickr] - restrict extension reading, and minor changes\n" - " - in nifti_image_read(), compute bytes for extensions (see remaining)\n" - " - in nifti_read_extensions(), pass 'remain' as space for extensions,\n" - " pass it to nifti_read_next_ext(), and update for each one read \n" - " - in nifti_check_extension(), require (size <= remain)\n", - " - in update_nifti_image_brick_list(), update nvox\n" - " - in nifti_image_load_bricks(), make explicit check for nbricks <= 0\n" - " - in int_force_positive(), check for (!list)\n" - " - in swap_nifti_header(), swap sizeof_hdr, and reorder to struct order\n" - " - change get_filesize functions to signed ( < 0 is no file or error )\n", - " - in nifti_validfilename(), lose redundant (len < 0) check\n" - " - make print_hex_vals() static\n" - " - in disp_nifti_1_header, restrict string field widths\n" - "\n", - "0.9 23 Dec 2004 [rickr] - minor changes\n" - " - broke ASCII header reading out of nifti_image_read(), into new\n" - " functions has_ascii_header() and read_ascii_image()\n", - " - check image_read failure and znzseek failure\n" - " - altered some debug output\n" - " - nifti_write_all_data() now returns an int\n" - "\n", - "0.10 29 Dec 2004 [rickr]\n" - " - renamed nifti_valid_extension() to nifti_check_extension()\n" - " - added functions nifti_makehdrname() and nifti_makeimgname()\n" - " - added function valid_nifti_extensions()\n" - " - in nifti_write_extensions(), check for validity before writing\n", - " - rewrote nifti_image_write_hdr_img2():\n" - " o set write_data and leave_open flags from write_opts\n" - " o add debug print statements\n" - " o use nifti_write_ascii_image() for the ascii case\n" - " o rewrote the logic of all cases to be easier to follow\n", - " - broke out code as nifti_write_ascii_image() function\n" - " - added debug to top-level write functions, and free the znzFile\n" - " - removed unused internal function nifti_image_open()\n" - "\n", - "0.11 30 Dec 2004 [rickr] - small mods\n" - " - moved static function prototypes from header to C file\n" - " - free extensions in nifti_image_free()\n" - "\n", - "1.0 07 Jan 2005 [rickr] - INITIAL RELEASE VERSION\n" - " - added function nifti_set_filenames()\n" - " - added function nifti_read_header()\n" - " - added static function nhdr_looks_good()\n" - " - added static function need_nhdr_swap()\n" - " - exported nifti_add_exten_to_list symbol\n", - " - fixed #bytes written in nifti_write_extensions()\n" - " - only modify offset if it is too small (nifti_set_iname_offset)\n" - " - added nifti_type 3 to nifti_makehdrname and nifti_makeimgname\n" - " - added function nifti_set_filenames()\n" - "\n", - "1.1 07 Jan 2005 [rickr]\n" - " - in nifti_read_header(), swap if needed\n" - "\n", - "1.2 07 Feb 2005 [kate fissell c/o rickr] \n" - " - nifti1.h: added doxygen comments for main struct and #define groups\n" - " - nifti1_io.h: added doxygen comments for file and nifti_image struct\n" - " - nifti1_io.h: added doxygen comments for file and some functions\n" - " - nifti1_io.c: changed nifti_copy_nim_info to use memcpy\n" - "\n", - "1.3 09 Feb 2005 [rickr]\n" - " - nifti1.h: added doxygen comments for extension structs\n" - " - nifti1_io.h: put most #defines in #ifdef NIFTI1_IO_C block\n" - " - added a doxygen-style description to every exported function\n" - " - added doxygen-style comments within some functions\n" - " - re-exported many znzFile functions that I had made static\n" - " - re-added nifti_image_open (sorry, Mark)\n" - " - every exported function now has 'nifti' in the name (19 functions)\n", - " - made sure every alloc() has a failure test\n" - " - added nifti_copy_extensions function, for use in nifti_copy_nim_info\n" - " - nifti_is_gzfile: added initial strlen test\n" - " - nifti_set_filenames: added set_byte_order parameter option\n" - " (it seems appropriate to set the BO when new files are associated)\n" - " - disp_nifti_1_header: prints to stdout (a.o.t. stderr), with fflush\n" - "\n", - "1.4 23 Feb 2005 [rickr] - sourceforge merge\n" - " - merged into the nifti_io CVS directory structure at sourceforge.net\n" - " - merged in 4 changes by Mark, and re-added his const keywords\n" - " - cast some pointers to (void *) for -pedantic compile option\n" - " - added nifti_free_extensions()\n" - "\n", - "1.5 02 Mar 2005 [rickr] - started nifti global options\n" - " - gni_debug is now g_opts.debug\n" - " - added validity check parameter to nifti_read_header\n" - " - need_nhdr_swap no longer does test swaps on the stack\n" - "\n", - "1.6 05 April 2005 [rickr] - validation and collapsed_image_read\n" - " - added nifti_read_collapsed_image(), an interface for reading partial\n" - " datasets, specifying a subset of array indices\n" - " - for read_collapsed_image, added static functions: rci_read_data(),\n" - " rci_alloc_mem(), and make_pivot_list()\n", - " - added nifti_nim_is_valid() to check for consistency (more to do)\n" - " - added nifti_nim_has_valid_dims() to do many dimensions tests\n" - "\n", - "1.7 08 April 2005 [rickr]\n" - " - added nifti_update_dims_from_array() - to update dimensions\n" - " - modified nifti_makehdrname() and nifti_makeimgname():\n" - " if prefix has a valid extension, use it (else make one up)\n" - " - added nifti_get_intlist - for making an array of ints\n" - " - fixed init of NBL->bsize in nifti_alloc_NBL_mem() {thanks, Bob}\n" - "\n", - "1.8 14 April 2005 [rickr]\n" - " - added nifti_set_type_from_names(), for nifti_set_filenames()\n" - " (only updates type if number of files does not match it)\n" - " - added is_valid_nifti_type(), just to be sure\n" - " - updated description of nifti_read_collapsed_image() for *data change\n" - " (if *data is already set, assume memory exists for results)\n" - " - modified rci_alloc_mem() to allocate only if *data is NULL\n" - "\n", - "1.9 19 April 2005 [rickr]\n" - " - added extension codes NIFTI_ECODE_COMMENT and NIFTI_ECODE_XCEDE\n" - " - added nifti_type codes NIFTI_MAX_ECODE and NIFTI_MAX_FTYPE\n" - " - added nifti_add_extension() {exported}\n" - " - added nifti_fill_extension() as a static function\n" - " - added nifti_is_valid_ecode() {exported}\n", - " - nifti_type values are now NIFTI_FTYPE_* file codes\n" - " - in nifti_read_extensions(), decrement 'remain' by extender size, 4\n" - " - in nifti_set_iname_offset(), case 1, update if offset differs\n" - " - only output '-d writing nifti file' if debug > 1\n" - "\n", - "1.10 10 May 2005 [rickr]\n" - " - files are read using ZLIB only if they end in '.gz'\n" - "\n", - "1.11 12 August 2005 [kate fissell]\n" - " - Kate's 0.2 release packaging, for sourceforge\n" - "\n", - "1.12 17 August 2005 [rickr] - comment (doxygen) updates\n" - " - updated comments for most functions (2 updates from Cinly Ooi)\n" - " - added nifti_type_and_names_match()\n" - "\n", - "1.12a 24 August 2005 [rickr] - remove all tabs from Clibs/*/*.[ch]\n", - "1.12b 25 August 2005 [rickr] - changes by Hans Johnson\n", - "1.13 25 August 2005 [rickr]\n", - " - finished changes by Hans for Insight\n" - " - added const in all appropriate parameter locations (30-40)\n" - " (any pointer referencing data that will not change)\n" - " - shortened all string constants below 509 character limit\n" - "1.14 28 October 2005 [HJohnson]\n", - " - use nifti_set_filenames() in nifti_convert_nhdr2nim()\n" - "1.15 02 November 2005 [rickr]\n", - " - added skip_blank_ext to nifti_global_options\n" - " - added nifti_set_skip_blank_ext(), to set option\n" - " - if skip_blank_ext and no extensions, do not read/write extender\n" - "1.16 18 November 2005 [rickr]\n", - " - removed any test or access of dim[i], i>dim[0]\n" - " - do not set pixdim for collapsed dims to 1.0, leave them as they are\n" - " - added magic and dim[i] tests in nifti_hdr_looks_good()\n" - " - added 2 size_t casts\n" - "1.17 22 November 2005 [rickr]\n", - " - in hdr->nim, for i > dim[0], pass 0 or 1, else set to 1\n" - "1.18 02 March 2006 [rickr]\n", - " - in nifti_alloc_NBL_mem(), fixed nt=0 case from 1.17 change\n" - "1.19 23 May 2006 [HJohnson,rickr]\n", - " - nifti_write_ascii_image(): free(hstr)\n" - " - nifti_copy_extensions(): clear num_ext and ext_list\n" - "1.20 27 Jun 2006 [rickr]\n", - " - nifti_findhdrname(): fixed assign of efirst to match stated logic\n" - " (problem found by Atle Bjørnerud)\n" - "1.21 05 Sep 2006 [rickr] update for nifticlib-0.4 release\n", - " - was reminded to actually add nifti_set_skip_blank_ext()\n" - " - init g_opts.skip_blank_ext to 0\n" - "1.22 01 Jun 2007 nifticlib-0.5 release\n", - "1.23 05 Jun 2007 nifti_add_exten_to_list: revert on failure, free old list\n" - "1.24 07 Jun 2007 nifti_copy_extensions: use esize-8 for data size\n" - "1.25 12 Jun 2007 [rickr] EMPTY_IMAGE creation\n", - " - added nifti_make_new_header() - to create from dims/dtype\n" - " - added nifti_make_new_nim() - to create from dims/dtype/fill\n" - " - added nifti_is_valid_datatype(), and more debug info\n", - "1.26 27 Jul 2007 [rickr] handle single volumes > 2^31 bytes (but < 2^32)\n", - "1.27 28 Jul 2007 [rickr] nim->nvox, NBL-bsize are now type size_t\n" - "1.28 30 Jul 2007 [rickr] size_t updates\n", - "1.29 08 Aug 2007 [rickr] for list, valid_nifti_brick_list requires 3 dims\n" - "1.30 08 Nov 2007 [Yaroslav/rickr]\n" - " - fix ARM struct alignment problem in byte-swapping routines\n", - "1.31 29 Nov 2007 [rickr] for nifticlib-1.0.0\n" - " - added nifti_datatype_to/from_string routines\n" - " - added DT_RGBA32/NIFTI_TYPE_RGBA32 datatype macros (2304)\n" - " - added NIFTI_ECODE_FREESURFER (14)\n", - "1.32 08 Dec 2007 [rickr]\n" - " - nifti_hdr_looks_good() allows ANALYZE headers (req. by V. Luccio)\n" - " - added nifti_datatype_is_valid()\n", - "1.33 05 Feb 2008 [hansj,rickr] - block nia.gz use\n" - "1.34 13 Jun 2008 [rickr] - added nifti_compiled_with_zlib()\n" - "1.35 03 Aug 2008 [rickr]\n", - " - deal with swapping, so that CPU type does not affect output\n" - " (motivated by C Burns)\n" - " - added nifti_analyze75 structure and nifti_swap_as_analyze()\n" - " - previous swap_nifti_header is saved as old_swap_nifti_header\n" - " - also swap UNUSED fields in nifti_1_header struct\n", - "1.36 07 Oct 2008 [rickr]\n", - " - added nifti_NBL_matches_nim() check for write_bricks()\n" - "1.37 10 Mar 2009 [rickr]\n", - " - H Johnson cast updates (06 Feb)\n" - " - added NIFTI_ECODE_PYPICKLE for PyNIfTI (06 Feb)\n" - " - added NIFTI_ECODEs 18-28 for the LONI MiND group\n" - "1.38 28 Apr 2009 [rickr]\n", - " - uppercase extensions are now valid (requested by M. Coursolle)\n" - " - nifti_set_allow_upper_fext controls this option (req by C. Ooi)\n" - "1.39 23 Jun 2009 [rickr]: added 4 checks of alloc() returns\n", - "1.40 16 Mar 2010 [rickr]: added NIFTI_ECODE_VOXBO for D. Kimberg\n", - "1.41 28 Apr 2010 [rickr]: added NIFTI_ECODE_CARET for J. Harwell\n", - "1.42 06 Jul 2010 [rickr]: trouble with large (gz) files\n", - " - noted/investigated by M Hanke and Y Halchenko\n" - " - fixed znzread/write, noting example by M Adler\n" - " - changed nifti_swap_* routines/calls to take size_t (6)\n" - "1.43 07 Jul 2010 [rickr]: fixed znzR/W to again return nmembers\n", - "1.44 19 Jul 2013 [rickr]: ITK compatibility updates from H Johnson\n", - "1.45 10 May 2019 [rickr]: added NIFTI_ECODE_QUANTIPHYSE\n", - "1.46 26 Sep 2019 [rickr]:\n" - " - nifti_read_ascii_image no longer closes fp or free's fname\n", - "2.1.0 18 Jun 2020 [leej3,hmjohnson,rickr]:\n" - " - big version jump - changed to more formal library versioning\n", - "2.1.0.1 - non-release update - 16 Jun 2022 [rickr]:\n" - " - add nifti_image_write_status\n", - "----------------------------------------------------------------------\n" + "----------------------------------------------------------------------\n" + "history (of nifti library changes):\n" + "\n", + "0.0 August, 2003 [rwcox]\n" + " (Robert W Cox of the National Institutes of Health, SSCC/DIRP/NIMH)\n" + " - initial version\n" + "\n", + "0.1 July/August, 2004 [Mark Jenkinson]\n" + " (FMRIB Centre, University of Oxford, UK)\n" + " - Mainly adding low-level IO and changing things to allow gzipped\n" + " files to be read and written\n" + " - Full backwards compatibility should have been maintained\n" + "\n", + "0.2 16 Nov 2004 [rickr]\n" + " (Rick Reynolds of the National Institutes of Health, SSCC/DIRP/NIMH)\n" + " - included Mark's changes in the AFNI distribution (including znzlib/)\n" + " (HAVE_ZLIB is commented out for the standard distribution)\n" + " - modified nifti_validfilename() and nifti_makebasename()\n" + " - added nifti_find_file_extension()\n" + "\n", + "0.3 3 Dec 2004 [rickr]\n" + " - note: header extensions are not yet checked for\n" + " - added formatted history as global string, for printing\n" + " - added nifti_disp_lib_hist(), to display the nifti library history\n" + " - added nifti_disp_lib_version(), to display the nifti library history\n", + " - re-wrote nifti_findhdrname()\n" + " o used nifti_find_file_extension()\n" + " o changed order of file tests (default is .nii, depends on input)\n" + " o free hdrname on failure\n" + " - made similar changes to nifti_findimgname()\n" + " - check for NULL return from nifti_findhdrname() calls\n", + " - removed most of ERREX() macros\n" + " - modified nifti_image_read()\n" + " o added debug info and error checking (on gni_debug > 0, only)\n" + " o fail if workingname is NULL\n" + " o check for failure to open header file\n" + " o free workingname on failure\n" + " o check for failure of nifti_image_load()\n" + " o check for failure of nifti_convert_nhdr2nim()\n", + " - changed nifti_image_load() to int, and check nifti_read_buffer return\n" + " - changed nifti_read_buffer() to fail on short read, and to count float\n" + " fixes (to print on debug)\n" + " - changed nifti_image_infodump to print to stderr\n" + " - updated function header comments, or moved comments above header\n" + " - removed const keyword\n" + " - added LNI_FERR() macro for error reporting on input files\n" + "\n", + "0.4 10 Dec 2004 [rickr] - added header extensions\n" + " - in nifti1_io.h:\n" + " o added num_ext and ext_list to the definition of nifti_image\n" + " o made many functions static (more to follow)\n" + " o added LNI_MAX_NIA_EXT_LEN, for max nifti_type 3 extension length\n", + " - added __DATE__ to version output in nifti_disp_lib_version()\n" + " - added nifti_disp_matrix_orient() to print orientation information\n" + " - added '.nia' as a valid file extension in nifti_find_file_extension()\n" + " - added much more debug output\n" + " - in nifti_image_read(), in the case of an ASCII header, check for\n" + " extensions after the end of the header\n", + " - added nifti_read_extensions() function\n" + " - added nifti_read_next_extension() function\n" + " - added nifti_add_exten_to_list() function\n" + " - added nifti_check_extension() function\n" + " - added nifti_write_extensions() function\n" + " - added nifti_extension_size() function\n" + " - in nifti_set_iname_offest():\n" + " o adjust offset by the extension size and the extender size\n", + " o fixed the 'ceiling modulo 16' computation\n" + " - in nifti_image_write_hdr_img2(): \n" + " o added extension writing\n" + " o check for NULL return from nifti_findimgname()\n" + " - include number of extensions in nifti_image_to_ascii() output\n" + " - in nifti_image_from_ascii():\n" + " o return bytes_read as a parameter, computed from the final spos\n" + " o extract num_ext from ASCII header\n" + "\n", + "0.5 14 Dec 2004 [rickr] - added sub-brick reading functions\n" + " - added nifti_brick_list type to nifti1_io.h, along with new prototypes\n" + " - added main nifti_image_read_bricks() function, with description\n" + " - added nifti_image_load_bricks() - library function (requires nim)\n" + " - added valid_nifti_brick_list() - library function\n" + " - added free_NBL() - library function\n", + " - added update_nifti_image_for_brick_list() for dimension update\n" + " - added nifti_load_NBL_bricks(), nifti_alloc_NBL_mem(),\n" + " nifti_copynsort() and force_positive() (static functions)\n" + " - in nifti_image_read(), check for failed load only if read_data is set\n" + " - broke most of nifti_image_load() into nifti_image_load_prep()\n" + "\n", + "0.6 15 Dec 2004 [rickr] - added sub-brick writing functionality\n" + " - in nifti1_io.h, removed znzlib directory from include - all nifti\n" + " library files are now under the nifti directory\n" + " - nifti_read_extensions(): print no offset warning for nifti_type 3\n" + " - nifti_write_all_data():\n" + " o pass nifti_brick_list * NBL, for optional writing\n" + " o if NBL, write each sub-brick, sequentially\n", + " - nifti_set_iname_offset(): case 1 must have sizeof() cast to int\n" + " - pass NBL to nifti_image_write_hdr_img2(), and allow NBL or data\n" + " - added nifti_image_write_bricks() wrapper for ...write_hdr_img2()\n" + " - included compression abilities\n" + "\n", + "0.7 16 Dec 2004 [rickr] - minor changes to extension reading\n" + "\n", + "0.8 21 Dec 2004 [rickr] - restrict extension reading, and minor changes\n" + " - in nifti_image_read(), compute bytes for extensions (see remaining)\n" + " - in nifti_read_extensions(), pass 'remain' as space for extensions,\n" + " pass it to nifti_read_next_ext(), and update for each one read \n" + " - in nifti_check_extension(), require (size <= remain)\n", + " - in update_nifti_image_brick_list(), update nvox\n" + " - in nifti_image_load_bricks(), make explicit check for nbricks <= 0\n" + " - in int_force_positive(), check for (!list)\n" + " - in swap_nifti_header(), swap sizeof_hdr, and reorder to struct order\n" + " - change get_filesize functions to signed ( < 0 is no file or error )\n", + " - in nifti_validfilename(), lose redundant (len < 0) check\n" + " - make print_hex_vals() static\n" + " - in disp_nifti_1_header, restrict string field widths\n" + "\n", + "0.9 23 Dec 2004 [rickr] - minor changes\n" + " - broke ASCII header reading out of nifti_image_read(), into new\n" + " functions has_ascii_header() and read_ascii_image()\n", + " - check image_read failure and znzseek failure\n" + " - altered some debug output\n" + " - nifti_write_all_data() now returns an int\n" + "\n", + "0.10 29 Dec 2004 [rickr]\n" + " - renamed nifti_valid_extension() to nifti_check_extension()\n" + " - added functions nifti_makehdrname() and nifti_makeimgname()\n" + " - added function valid_nifti_extensions()\n" + " - in nifti_write_extensions(), check for validity before writing\n", + " - rewrote nifti_image_write_hdr_img2():\n" + " o set write_data and leave_open flags from write_opts\n" + " o add debug print statements\n" + " o use nifti_write_ascii_image() for the ascii case\n" + " o rewrote the logic of all cases to be easier to follow\n", + " - broke out code as nifti_write_ascii_image() function\n" + " - added debug to top-level write functions, and free the znzFile\n" + " - removed unused internal function nifti_image_open()\n" + "\n", + "0.11 30 Dec 2004 [rickr] - small mods\n" + " - moved static function prototypes from header to C file\n" + " - free extensions in nifti_image_free()\n" + "\n", + "1.0 07 Jan 2005 [rickr] - INITIAL RELEASE VERSION\n" + " - added function nifti_set_filenames()\n" + " - added function nifti_read_header()\n" + " - added static function nhdr_looks_good()\n" + " - added static function need_nhdr_swap()\n" + " - exported nifti_add_exten_to_list symbol\n", + " - fixed #bytes written in nifti_write_extensions()\n" + " - only modify offset if it is too small (nifti_set_iname_offset)\n" + " - added nifti_type 3 to nifti_makehdrname and nifti_makeimgname\n" + " - added function nifti_set_filenames()\n" + "\n", + "1.1 07 Jan 2005 [rickr]\n" + " - in nifti_read_header(), swap if needed\n" + "\n", + "1.2 07 Feb 2005 [kate fissell c/o rickr] \n" + " - nifti1.h: added doxygen comments for main struct and #define groups\n" + " - nifti1_io.h: added doxygen comments for file and nifti_image struct\n" + " - nifti1_io.h: added doxygen comments for file and some functions\n" + " - nifti1_io.c: changed nifti_copy_nim_info to use memcpy\n" + "\n", + "1.3 09 Feb 2005 [rickr]\n" + " - nifti1.h: added doxygen comments for extension structs\n" + " - nifti1_io.h: put most #defines in #ifdef NIFTI1_IO_C block\n" + " - added a doxygen-style description to every exported function\n" + " - added doxygen-style comments within some functions\n" + " - re-exported many znzFile functions that I had made static\n" + " - re-added nifti_image_open (sorry, Mark)\n" + " - every exported function now has 'nifti' in the name (19 functions)\n", + " - made sure every alloc() has a failure test\n" + " - added nifti_copy_extensions function, for use in nifti_copy_nim_info\n" + " - nifti_is_gzfile: added initial strlen test\n" + " - nifti_set_filenames: added set_byte_order parameter option\n" + " (it seems appropriate to set the BO when new files are associated)\n" + " - disp_nifti_1_header: prints to stdout (a.o.t. stderr), with fflush\n" + "\n", + "1.4 23 Feb 2005 [rickr] - sourceforge merge\n" + " - merged into the nifti_io CVS directory structure at sourceforge.net\n" + " - merged in 4 changes by Mark, and re-added his const keywords\n" + " - cast some pointers to (void *) for -pedantic compile option\n" + " - added nifti_free_extensions()\n" + "\n", + "1.5 02 Mar 2005 [rickr] - started nifti global options\n" + " - gni_debug is now g_opts.debug\n" + " - added validity check parameter to nifti_read_header\n" + " - need_nhdr_swap no longer does test swaps on the stack\n" + "\n", + "1.6 05 April 2005 [rickr] - validation and collapsed_image_read\n" + " - added nifti_read_collapsed_image(), an interface for reading partial\n" + " datasets, specifying a subset of array indices\n" + " - for read_collapsed_image, added static functions: rci_read_data(),\n" + " rci_alloc_mem(), and make_pivot_list()\n", + " - added nifti_nim_is_valid() to check for consistency (more to do)\n" + " - added nifti_nim_has_valid_dims() to do many dimensions tests\n" + "\n", + "1.7 08 April 2005 [rickr]\n" + " - added nifti_update_dims_from_array() - to update dimensions\n" + " - modified nifti_makehdrname() and nifti_makeimgname():\n" + " if prefix has a valid extension, use it (else make one up)\n" + " - added nifti_get_intlist - for making an array of ints\n" + " - fixed init of NBL->bsize in nifti_alloc_NBL_mem() {thanks, Bob}\n" + "\n", + "1.8 14 April 2005 [rickr]\n" + " - added nifti_set_type_from_names(), for nifti_set_filenames()\n" + " (only updates type if number of files does not match it)\n" + " - added is_valid_nifti_type(), just to be sure\n" + " - updated description of nifti_read_collapsed_image() for *data change\n" + " (if *data is already set, assume memory exists for results)\n" + " - modified rci_alloc_mem() to allocate only if *data is NULL\n" + "\n", + "1.9 19 April 2005 [rickr]\n" + " - added extension codes NIFTI_ECODE_COMMENT and NIFTI_ECODE_XCEDE\n" + " - added nifti_type codes NIFTI_MAX_ECODE and NIFTI_MAX_FTYPE\n" + " - added nifti_add_extension() {exported}\n" + " - added nifti_fill_extension() as a static function\n" + " - added nifti_is_valid_ecode() {exported}\n", + " - nifti_type values are now NIFTI_FTYPE_* file codes\n" + " - in nifti_read_extensions(), decrement 'remain' by extender size, 4\n" + " - in nifti_set_iname_offset(), case 1, update if offset differs\n" + " - only output '-d writing nifti file' if debug > 1\n" + "\n", + "1.10 10 May 2005 [rickr]\n" + " - files are read using ZLIB only if they end in '.gz'\n" + "\n", + "1.11 12 August 2005 [kate fissell]\n" + " - Kate's 0.2 release packaging, for sourceforge\n" + "\n", + "1.12 17 August 2005 [rickr] - comment (doxygen) updates\n" + " - updated comments for most functions (2 updates from Cinly Ooi)\n" + " - added nifti_type_and_names_match()\n" + "\n", + "1.12a 24 August 2005 [rickr] - remove all tabs from Clibs/*/*.[ch]\n", + "1.12b 25 August 2005 [rickr] - changes by Hans Johnson\n", + "1.13 25 August 2005 [rickr]\n", + " - finished changes by Hans for Insight\n" + " - added const in all appropriate parameter locations (30-40)\n" + " (any pointer referencing data that will not change)\n" + " - shortened all string constants below 509 character limit\n" + "1.14 28 October 2005 [HJohnson]\n", + " - use nifti_set_filenames() in nifti_convert_nhdr2nim()\n" + "1.15 02 November 2005 [rickr]\n", + " - added skip_blank_ext to nifti_global_options\n" + " - added nifti_set_skip_blank_ext(), to set option\n" + " - if skip_blank_ext and no extensions, do not read/write extender\n" + "1.16 18 November 2005 [rickr]\n", + " - removed any test or access of dim[i], i>dim[0]\n" + " - do not set pixdim for collapsed dims to 1.0, leave them as they are\n" + " - added magic and dim[i] tests in nifti_hdr_looks_good()\n" + " - added 2 size_t casts\n" + "1.17 22 November 2005 [rickr]\n", + " - in hdr->nim, for i > dim[0], pass 0 or 1, else set to 1\n" + "1.18 02 March 2006 [rickr]\n", + " - in nifti_alloc_NBL_mem(), fixed nt=0 case from 1.17 change\n" + "1.19 23 May 2006 [HJohnson,rickr]\n", + " - nifti_write_ascii_image(): free(hstr)\n" + " - nifti_copy_extensions(): clear num_ext and ext_list\n" + "1.20 27 Jun 2006 [rickr]\n", + " - nifti_findhdrname(): fixed assign of efirst to match stated logic\n" + " (problem found by Atle Bjørnerud)\n" + "1.21 05 Sep 2006 [rickr] update for nifticlib-0.4 release\n", + " - was reminded to actually add nifti_set_skip_blank_ext()\n" + " - init g_opts.skip_blank_ext to 0\n" + "1.22 01 Jun 2007 nifticlib-0.5 release\n", + "1.23 05 Jun 2007 nifti_add_exten_to_list: revert on failure, free old list\n" + "1.24 07 Jun 2007 nifti_copy_extensions: use esize-8 for data size\n" + "1.25 12 Jun 2007 [rickr] EMPTY_IMAGE creation\n", + " - added nifti_make_new_header() - to create from dims/dtype\n" + " - added nifti_make_new_nim() - to create from dims/dtype/fill\n" + " - added nifti_is_valid_datatype(), and more debug info\n", + "1.26 27 Jul 2007 [rickr] handle single volumes > 2^31 bytes (but < 2^32)\n", + "1.27 28 Jul 2007 [rickr] nim->nvox, NBL-bsize are now type size_t\n" + "1.28 30 Jul 2007 [rickr] size_t updates\n", + "1.29 08 Aug 2007 [rickr] for list, valid_nifti_brick_list requires 3 dims\n" + "1.30 08 Nov 2007 [Yaroslav/rickr]\n" + " - fix ARM struct alignment problem in byte-swapping routines\n", + "1.31 29 Nov 2007 [rickr] for nifticlib-1.0.0\n" + " - added nifti_datatype_to/from_string routines\n" + " - added DT_RGBA32/NIFTI_TYPE_RGBA32 datatype macros (2304)\n" + " - added NIFTI_ECODE_FREESURFER (14)\n", + "1.32 08 Dec 2007 [rickr]\n" + " - nifti_hdr_looks_good() allows ANALYZE headers (req. by V. Luccio)\n" + " - added nifti_datatype_is_valid()\n", + "1.33 05 Feb 2008 [hansj,rickr] - block nia.gz use\n" + "1.34 13 Jun 2008 [rickr] - added nifti_compiled_with_zlib()\n" + "1.35 03 Aug 2008 [rickr]\n", + " - deal with swapping, so that CPU type does not affect output\n" + " (motivated by C Burns)\n" + " - added nifti_analyze75 structure and nifti_swap_as_analyze()\n" + " - previous swap_nifti_header is saved as old_swap_nifti_header\n" + " - also swap UNUSED fields in nifti_1_header struct\n", + "1.36 07 Oct 2008 [rickr]\n", + " - added nifti_NBL_matches_nim() check for write_bricks()\n" + "1.37 10 Mar 2009 [rickr]\n", + " - H Johnson cast updates (06 Feb)\n" + " - added NIFTI_ECODE_PYPICKLE for PyNIfTI (06 Feb)\n" + " - added NIFTI_ECODEs 18-28 for the LONI MiND group\n" + "1.38 28 Apr 2009 [rickr]\n", + " - uppercase extensions are now valid (requested by M. Coursolle)\n" + " - nifti_set_allow_upper_fext controls this option (req by C. Ooi)\n" + "1.39 23 Jun 2009 [rickr]: added 4 checks of alloc() returns\n", + "1.40 16 Mar 2010 [rickr]: added NIFTI_ECODE_VOXBO for D. Kimberg\n", + "1.41 28 Apr 2010 [rickr]: added NIFTI_ECODE_CARET for J. Harwell\n", + "1.42 06 Jul 2010 [rickr]: trouble with large (gz) files\n", + " - noted/investigated by M Hanke and Y Halchenko\n" + " - fixed znzread/write, noting example by M Adler\n" + " - changed nifti_swap_* routines/calls to take size_t (6)\n" + "1.43 07 Jul 2010 [rickr]: fixed znzR/W to again return nmembers\n", + "1.44 19 Jul 2013 [rickr]: ITK compatibility updates from H Johnson\n", + "1.45 10 May 2019 [rickr]: added NIFTI_ECODE_QUANTIPHYSE\n", + "1.46 26 Sep 2019 [rickr]:\n" + " - nifti_read_ascii_image no longer closes fp or free's fname\n", + "2.1.0 18 Jun 2020 [leej3,hmjohnson,rickr]:\n" + " - big version jump - changed to more formal library versioning\n", + "2.1.0.1 - non-release update - 16 Jun 2022 [rickr]:\n" + " - add nifti_image_write_status\n", + "----------------------------------------------------------------------\n" }; static const char gni_version[] = NIFTI1_IO_SOURCE_VERSION " (16 Jun, 2022)"; /*! global nifti options structure - init with defaults */ static nifti_global_options g_opts = { - 1, /* debug level */ - 0, /* skip_blank_ext - skip extender if no extensions */ - 1 /* allow_upper_fext - allow uppercase file extensions */ + 1, /* debug level */ + 0, /* skip_blank_ext - skip extender if no extensions */ + 1 /* allow_upper_fext - allow uppercase file extensions */ }; /*! global nifti types structure list (per type, ordered oldest to newest) */ static const nifti_type_ele nifti_type_list[] = { /* type nbyper swapsize name */ - { 0, 0, 0, "DT_UNKNOWN" }, - { 0, 0, 0, "DT_NONE" }, - { 1, 0, 0, "DT_BINARY" }, /* not usable */ - { 2, 1, 0, "DT_UNSIGNED_CHAR" }, - { 2, 1, 0, "DT_UINT8" }, - { 2, 1, 0, "NIFTI_TYPE_UINT8" }, - { 4, 2, 2, "DT_SIGNED_SHORT" }, - { 4, 2, 2, "DT_INT16" }, - { 4, 2, 2, "NIFTI_TYPE_INT16" }, - { 8, 4, 4, "DT_SIGNED_INT" }, - { 8, 4, 4, "DT_INT32" }, - { 8, 4, 4, "NIFTI_TYPE_INT32" }, - { 16, 4, 4, "DT_FLOAT" }, - { 16, 4, 4, "DT_FLOAT32" }, - { 16, 4, 4, "NIFTI_TYPE_FLOAT32" }, - { 32, 8, 4, "DT_COMPLEX" }, - { 32, 8, 4, "DT_COMPLEX64" }, - { 32, 8, 4, "NIFTI_TYPE_COMPLEX64" }, - { 64, 8, 8, "DT_DOUBLE" }, - { 64, 8, 8, "DT_FLOAT64" }, - { 64, 8, 8, "NIFTI_TYPE_FLOAT64" }, - { 128, 3, 0, "DT_RGB" }, - { 128, 3, 0, "DT_RGB24" }, - { 128, 3, 0, "NIFTI_TYPE_RGB24" }, - { 255, 0, 0, "DT_ALL" }, - { 256, 1, 0, "DT_INT8" }, - { 256, 1, 0, "NIFTI_TYPE_INT8" }, - { 512, 2, 2, "DT_UINT16" }, - { 512, 2, 2, "NIFTI_TYPE_UINT16" }, - { 768, 4, 4, "DT_UINT32" }, - { 768, 4, 4, "NIFTI_TYPE_UINT32" }, - { 1024, 8, 8, "DT_INT64" }, - { 1024, 8, 8, "NIFTI_TYPE_INT64" }, - { 1280, 8, 8, "DT_UINT64" }, - { 1280, 8, 8, "NIFTI_TYPE_UINT64" }, - { 1536, 16, 16, "DT_FLOAT128" }, - { 1536, 16, 16, "NIFTI_TYPE_FLOAT128" }, - { 1792, 16, 8, "DT_COMPLEX128" }, - { 1792, 16, 8, "NIFTI_TYPE_COMPLEX128" }, - { 2048, 32, 16, "DT_COMPLEX256" }, - { 2048, 32, 16, "NIFTI_TYPE_COMPLEX256" }, - { 2304, 4, 0, "DT_RGBA32" }, - { 2304, 4, 0, "NIFTI_TYPE_RGBA32" }, + { 0, 0, 0, "DT_UNKNOWN" }, + { 0, 0, 0, "DT_NONE" }, + { 1, 0, 0, "DT_BINARY" }, /* not usable */ + { 2, 1, 0, "DT_UNSIGNED_CHAR" }, + { 2, 1, 0, "DT_UINT8" }, + { 2, 1, 0, "NIFTI_TYPE_UINT8" }, + { 4, 2, 2, "DT_SIGNED_SHORT" }, + { 4, 2, 2, "DT_INT16" }, + { 4, 2, 2, "NIFTI_TYPE_INT16" }, + { 8, 4, 4, "DT_SIGNED_INT" }, + { 8, 4, 4, "DT_INT32" }, + { 8, 4, 4, "NIFTI_TYPE_INT32" }, + { 16, 4, 4, "DT_FLOAT" }, + { 16, 4, 4, "DT_FLOAT32" }, + { 16, 4, 4, "NIFTI_TYPE_FLOAT32" }, + { 32, 8, 4, "DT_COMPLEX" }, + { 32, 8, 4, "DT_COMPLEX64" }, + { 32, 8, 4, "NIFTI_TYPE_COMPLEX64" }, + { 64, 8, 8, "DT_DOUBLE" }, + { 64, 8, 8, "DT_FLOAT64" }, + { 64, 8, 8, "NIFTI_TYPE_FLOAT64" }, + { 128, 3, 0, "DT_RGB" }, + { 128, 3, 0, "DT_RGB24" }, + { 128, 3, 0, "NIFTI_TYPE_RGB24" }, + { 255, 0, 0, "DT_ALL" }, + { 256, 1, 0, "DT_INT8" }, + { 256, 1, 0, "NIFTI_TYPE_INT8" }, + { 512, 2, 2, "DT_UINT16" }, + { 512, 2, 2, "NIFTI_TYPE_UINT16" }, + { 768, 4, 4, "DT_UINT32" }, + { 768, 4, 4, "NIFTI_TYPE_UINT32" }, + { 1024, 8, 8, "DT_INT64" }, + { 1024, 8, 8, "NIFTI_TYPE_INT64" }, + { 1280, 8, 8, "DT_UINT64" }, + { 1280, 8, 8, "NIFTI_TYPE_UINT64" }, + { 1536, 16, 16, "DT_FLOAT128" }, + { 1536, 16, 16, "NIFTI_TYPE_FLOAT128" }, + { 1792, 16, 8, "DT_COMPLEX128" }, + { 1792, 16, 8, "NIFTI_TYPE_COMPLEX128" }, + { 2048, 32, 16, "DT_COMPLEX256" }, + { 2048, 32, 16, "NIFTI_TYPE_COMPLEX256" }, + { 2304, 4, 0, "DT_RGBA32" }, + { 2304, 4, 0, "NIFTI_TYPE_RGBA32" }, }; /*---------------------------------------------------------------------------*/ @@ -469,7 +469,7 @@ void nifti_disp_lib_hist( void ) { int c, len = sizeof(gni_history)/sizeof(char *); for( c = 0; c < len; c++ ) - fputs(gni_history[c], stdout); + fputs(gni_history[c], stdout); } /*----------------------------------------------------------------------*/ @@ -607,7 +607,7 @@ static void update_nifti_image_for_brick_list( nifti_image * nim , int nbricks ) /* update the dimensions to 4 or lower */ for( ndim = 4; (ndim > 1) && (nim->dim[ndim] <= 1); ndim-- ) - ; + ; if( g_opts.debug > 2 ){ fprintf(stderr,"+d ndim = %d -> %d\n",nim->ndim, ndim); @@ -701,7 +701,7 @@ int nifti_update_dims_from_array( nifti_image * nim ) /* compute ndim, assuming it can be no larger than the old one */ for( ndim = nim->dim[0]; (ndim > 1) && (nim->dim[ndim] <= 1); ndim-- ) - ; + ; if( g_opts.debug > 2 ){ fprintf(stderr,"+d ndim = %d -> %d\n",nim->ndim, ndim); @@ -856,40 +856,40 @@ static int nifti_load_NBL_bricks( nifti_image * nim , const int * slist, const i prev = -1; /* use prev for previous sub-brick */ for( c = 0; c < NBL->nbricks; c++ ){ - isrc = slist[c]; /* this is original brick index (c is new one) */ - idest = sindex[c]; /* this is the destination index for this data */ + isrc = slist[c]; /* this is original brick index (c is new one) */ + idest = sindex[c]; /* this is the destination index for this data */ /* if this sub-brick is not the previous, we must read from disk */ - if( isrc != prev ){ + if( isrc != prev ){ /* if we are not looking at the correct sub-brick, scan forward */ - if( fposn != (oposn + isrc*NBL->bsize) ){ - fposn = oposn + isrc*NBL->bsize; - if( znzseek(fp, (long)fposn, SEEK_SET) < 0 ){ - fprintf(stderr,"** failed to locate brick %d in file '%s'\n", + if( fposn != (oposn + isrc*NBL->bsize) ){ + fposn = oposn + isrc*NBL->bsize; + if( znzseek(fp, (long)fposn, SEEK_SET) < 0 ){ + fprintf(stderr,"** failed to locate brick %d in file '%s'\n", isrc, nim->iname ? nim->iname : nim->fname); - return -1; - } - } + return -1; + } + } /* only 10,000 lines later and we're actually reading something! */ - rv = nifti_read_buffer(fp, NBL->bricks[idest], NBL->bsize, nim); - if( rv != NBL->bsize ){ - fprintf(stderr,"** failed to read brick %d from file '%s'\n", + rv = nifti_read_buffer(fp, NBL->bricks[idest], NBL->bsize, nim); + if( rv != NBL->bsize ){ + fprintf(stderr,"** failed to read brick %d from file '%s'\n", isrc, nim->iname ? nim->iname : nim->fname); - if( g_opts.debug > 1 ) - fprintf(stderr," (read %u of %u bytes)\n", + if( g_opts.debug > 1 ) + fprintf(stderr," (read %u of %u bytes)\n", (unsigned int)rv, (unsigned int)NBL->bsize); - return -1; - } - fposn += NBL->bsize; - } else { + return -1; + } + fposn += NBL->bsize; + } else { /* we have already read this sub-brick, just copy the previous one */ /* note that this works because they are sorted */ - memcpy(NBL->bricks[idest], NBL->bricks[sindex[c-1]], NBL->bsize); - } + memcpy(NBL->bricks[idest], NBL->bricks[sindex[c-1]], NBL->bsize); + } - prev = isrc; /* in any case, note the now previous sub-brick */ + prev = isrc; /* in any case, note the now previous sub-brick */ } return 0; @@ -911,7 +911,7 @@ static int nifti_alloc_NBL_mem(nifti_image * nim, int nbricks, else { /* I missed this one with the 1.17 change 02 Mar 2006 [rickr] */ nbl->nbricks = 1; for( c = 4; c <= nim->ndim; c++ ) - nbl->nbricks *= nim->dim[c]; + nbl->nbricks *= nim->dim[c]; } nbl->bsize = (size_t)nim->nx * nim->ny * nim->nz * nim->nbyper;/* bytes */ @@ -1012,11 +1012,11 @@ static int nifti_copynsort(int nbricks, const int * blist, int ** slist, /* check the sort (why not? I've got time...) */ for( c1 = 0; c1 < nbricks-1; c1++ ){ - if( (stmp[c1] > stmp[c1+1]) || (blist[itmp[c1]] != stmp[c1]) ){ - fprintf(stderr,"** sorting screw-up, way to go, rick!\n"); - free(stmp); free(itmp); *slist = NULL; *sindex = NULL; - return -1; - } + if( (stmp[c1] > stmp[c1+1]) || (blist[itmp[c1]] != stmp[c1]) ){ + fprintf(stderr,"** sorting screw-up, way to go, rick!\n"); + free(stmp); free(itmp); *slist = NULL; *sindex = NULL; + return -1; + } } if( g_opts.debug > 2 ) fprintf(stderr,"-d sorting is okay\n"); @@ -1172,18 +1172,18 @@ int nifti_disp_matrix_orient( const char * mesg, mat44 mat ) *//*--------------------------------------------------------------------*/ char *nifti_strdup(const char *str) { - char *dup; + char *dup; - if( !str ) return NULL; /* allow calls passing NULL */ + if( !str ) return NULL; /* allow calls passing NULL */ - dup = (char *)malloc(strlen(str) + 1); + dup = (char *)malloc(strlen(str) + 1); /* check for failure */ - if( dup ) strcpy(dup, str); - else fprintf(stderr,"** nifti_strdup: failed to alloc %u bytes\n", + if( dup ) strcpy(dup, str); + else fprintf(stderr,"** nifti_strdup: failed to alloc %u bytes\n", (unsigned int)strlen(str)+1); - return dup; + return dup; } @@ -1202,24 +1202,24 @@ char *nifti_strdup(const char *str) char const * nifti_datatype_string( int dt ) { switch( dt ){ - case DT_UNKNOWN: return "UNKNOWN" ; - case DT_BINARY: return "BINARY" ; - case DT_INT8: return "INT8" ; - case DT_UINT8: return "UINT8" ; - case DT_INT16: return "INT16" ; - case DT_UINT16: return "UINT16" ; - case DT_INT32: return "INT32" ; - case DT_UINT32: return "UINT32" ; - case DT_INT64: return "INT64" ; - case DT_UINT64: return "UINT64" ; - case DT_FLOAT32: return "FLOAT32" ; - case DT_FLOAT64: return "FLOAT64" ; - case DT_FLOAT128: return "FLOAT128" ; - case DT_COMPLEX64: return "COMPLEX64" ; - case DT_COMPLEX128: return "COMPLEX128" ; - case DT_COMPLEX256: return "COMPLEX256" ; - case DT_RGB24: return "RGB24" ; - case DT_RGBA32: return "RGBA32" ; + case DT_UNKNOWN: return "UNKNOWN" ; + case DT_BINARY: return "BINARY" ; + case DT_INT8: return "INT8" ; + case DT_UINT8: return "UINT8" ; + case DT_INT16: return "INT16" ; + case DT_UINT16: return "UINT16" ; + case DT_INT32: return "INT32" ; + case DT_UINT32: return "UINT32" ; + case DT_INT64: return "INT64" ; + case DT_UINT64: return "UINT64" ; + case DT_FLOAT32: return "FLOAT32" ; + case DT_FLOAT64: return "FLOAT64" ; + case DT_FLOAT128: return "FLOAT128" ; + case DT_COMPLEX64: return "COMPLEX64" ; + case DT_COMPLEX128: return "COMPLEX128" ; + case DT_COMPLEX256: return "COMPLEX256" ; + case DT_RGB24: return "RGB24" ; + case DT_RGBA32: return "RGBA32" ; } return "**ILLEGAL**" ; } @@ -1234,24 +1234,24 @@ char const * nifti_datatype_string( int dt ) int nifti_is_inttype( int dt ) { switch( dt ){ - case DT_UNKNOWN: return 0 ; - case DT_BINARY: return 0 ; - case DT_INT8: return 1 ; - case DT_UINT8: return 1 ; - case DT_INT16: return 1 ; - case DT_UINT16: return 1 ; - case DT_INT32: return 1 ; - case DT_UINT32: return 1 ; - case DT_INT64: return 1 ; - case DT_UINT64: return 1 ; - case DT_FLOAT32: return 0 ; - case DT_FLOAT64: return 0 ; - case DT_FLOAT128: return 0 ; - case DT_COMPLEX64: return 0 ; - case DT_COMPLEX128: return 0 ; - case DT_COMPLEX256: return 0 ; - case DT_RGB24: return 1 ; - case DT_RGBA32: return 1 ; + case DT_UNKNOWN: return 0 ; + case DT_BINARY: return 0 ; + case DT_INT8: return 1 ; + case DT_UINT8: return 1 ; + case DT_INT16: return 1 ; + case DT_UINT16: return 1 ; + case DT_INT32: return 1 ; + case DT_UINT32: return 1 ; + case DT_INT64: return 1 ; + case DT_UINT64: return 1 ; + case DT_FLOAT32: return 0 ; + case DT_FLOAT64: return 0 ; + case DT_FLOAT128: return 0 ; + case DT_COMPLEX64: return 0 ; + case DT_COMPLEX128: return 0 ; + case DT_COMPLEX256: return 0 ; + case DT_RGB24: return 1 ; + case DT_RGBA32: return 1 ; } return 0 ; } @@ -1271,15 +1271,15 @@ int nifti_is_inttype( int dt ) char const *nifti_units_string( int uu ) { switch( uu ){ - case NIFTI_UNITS_METER: return "m" ; - case NIFTI_UNITS_MM: return "mm" ; - case NIFTI_UNITS_MICRON: return "um" ; - case NIFTI_UNITS_SEC: return "s" ; - case NIFTI_UNITS_MSEC: return "ms" ; - case NIFTI_UNITS_USEC: return "us" ; - case NIFTI_UNITS_HZ: return "Hz" ; - case NIFTI_UNITS_PPM: return "ppm" ; - case NIFTI_UNITS_RADS: return "rad/s" ; + case NIFTI_UNITS_METER: return "m" ; + case NIFTI_UNITS_MM: return "mm" ; + case NIFTI_UNITS_MICRON: return "um" ; + case NIFTI_UNITS_SEC: return "s" ; + case NIFTI_UNITS_MSEC: return "ms" ; + case NIFTI_UNITS_USEC: return "us" ; + case NIFTI_UNITS_HZ: return "Hz" ; + case NIFTI_UNITS_PPM: return "ppm" ; + case NIFTI_UNITS_RADS: return "rad/s" ; } return "Unknown" ; } @@ -1299,10 +1299,10 @@ char const *nifti_units_string( int uu ) char const *nifti_xform_string( int xx ) { switch( xx ){ - case NIFTI_XFORM_SCANNER_ANAT: return "Scanner Anat" ; - case NIFTI_XFORM_ALIGNED_ANAT: return "Aligned Anat" ; - case NIFTI_XFORM_TALAIRACH: return "Talairach" ; - case NIFTI_XFORM_MNI_152: return "MNI_152" ; + case NIFTI_XFORM_SCANNER_ANAT: return "Scanner Anat" ; + case NIFTI_XFORM_ALIGNED_ANAT: return "Aligned Anat" ; + case NIFTI_XFORM_TALAIRACH: return "Talairach" ; + case NIFTI_XFORM_MNI_152: return "MNI_152" ; } return "Unknown" ; } @@ -1322,43 +1322,43 @@ char const *nifti_xform_string( int xx ) char const *nifti_intent_string( int ii ) { switch( ii ){ - case NIFTI_INTENT_CORREL: return "Correlation statistic" ; - case NIFTI_INTENT_TTEST: return "T-statistic" ; - case NIFTI_INTENT_FTEST: return "F-statistic" ; - case NIFTI_INTENT_ZSCORE: return "Z-score" ; - case NIFTI_INTENT_CHISQ: return "Chi-squared distribution" ; - case NIFTI_INTENT_BETA: return "Beta distribution" ; - case NIFTI_INTENT_BINOM: return "Binomial distribution" ; - case NIFTI_INTENT_GAMMA: return "Gamma distribution" ; - case NIFTI_INTENT_POISSON: return "Poisson distribution" ; - case NIFTI_INTENT_NORMAL: return "Normal distribution" ; - case NIFTI_INTENT_FTEST_NONC: return "F-statistic noncentral" ; - case NIFTI_INTENT_CHISQ_NONC: return "Chi-squared noncentral" ; - case NIFTI_INTENT_LOGISTIC: return "Logistic distribution" ; - case NIFTI_INTENT_LAPLACE: return "Laplace distribution" ; - case NIFTI_INTENT_UNIFORM: return "Uniform distribition" ; - case NIFTI_INTENT_TTEST_NONC: return "T-statistic noncentral" ; - case NIFTI_INTENT_WEIBULL: return "Weibull distribution" ; - case NIFTI_INTENT_CHI: return "Chi distribution" ; - case NIFTI_INTENT_INVGAUSS: return "Inverse Gaussian distribution" ; - case NIFTI_INTENT_EXTVAL: return "Extreme Value distribution" ; - case NIFTI_INTENT_PVAL: return "P-value" ; - - case NIFTI_INTENT_LOGPVAL: return "Log P-value" ; - case NIFTI_INTENT_LOG10PVAL: return "Log10 P-value" ; - - case NIFTI_INTENT_ESTIMATE: return "Estimate" ; - case NIFTI_INTENT_LABEL: return "Label index" ; - case NIFTI_INTENT_NEURONAME: return "NeuroNames index" ; - case NIFTI_INTENT_GENMATRIX: return "General matrix" ; - case NIFTI_INTENT_SYMMATRIX: return "Symmetric matrix" ; - case NIFTI_INTENT_DISPVECT: return "Displacement vector" ; - case NIFTI_INTENT_VECTOR: return "Vector" ; - case NIFTI_INTENT_POINTSET: return "Pointset" ; - case NIFTI_INTENT_TRIANGLE: return "Triangle" ; - case NIFTI_INTENT_QUATERNION: return "Quaternion" ; - - case NIFTI_INTENT_DIMLESS: return "Dimensionless number" ; + case NIFTI_INTENT_CORREL: return "Correlation statistic" ; + case NIFTI_INTENT_TTEST: return "T-statistic" ; + case NIFTI_INTENT_FTEST: return "F-statistic" ; + case NIFTI_INTENT_ZSCORE: return "Z-score" ; + case NIFTI_INTENT_CHISQ: return "Chi-squared distribution" ; + case NIFTI_INTENT_BETA: return "Beta distribution" ; + case NIFTI_INTENT_BINOM: return "Binomial distribution" ; + case NIFTI_INTENT_GAMMA: return "Gamma distribution" ; + case NIFTI_INTENT_POISSON: return "Poisson distribution" ; + case NIFTI_INTENT_NORMAL: return "Normal distribution" ; + case NIFTI_INTENT_FTEST_NONC: return "F-statistic noncentral" ; + case NIFTI_INTENT_CHISQ_NONC: return "Chi-squared noncentral" ; + case NIFTI_INTENT_LOGISTIC: return "Logistic distribution" ; + case NIFTI_INTENT_LAPLACE: return "Laplace distribution" ; + case NIFTI_INTENT_UNIFORM: return "Uniform distribition" ; + case NIFTI_INTENT_TTEST_NONC: return "T-statistic noncentral" ; + case NIFTI_INTENT_WEIBULL: return "Weibull distribution" ; + case NIFTI_INTENT_CHI: return "Chi distribution" ; + case NIFTI_INTENT_INVGAUSS: return "Inverse Gaussian distribution" ; + case NIFTI_INTENT_EXTVAL: return "Extreme Value distribution" ; + case NIFTI_INTENT_PVAL: return "P-value" ; + + case NIFTI_INTENT_LOGPVAL: return "Log P-value" ; + case NIFTI_INTENT_LOG10PVAL: return "Log10 P-value" ; + + case NIFTI_INTENT_ESTIMATE: return "Estimate" ; + case NIFTI_INTENT_LABEL: return "Label index" ; + case NIFTI_INTENT_NEURONAME: return "NeuroNames index" ; + case NIFTI_INTENT_GENMATRIX: return "General matrix" ; + case NIFTI_INTENT_SYMMATRIX: return "Symmetric matrix" ; + case NIFTI_INTENT_DISPVECT: return "Displacement vector" ; + case NIFTI_INTENT_VECTOR: return "Vector" ; + case NIFTI_INTENT_POINTSET: return "Pointset" ; + case NIFTI_INTENT_TRIANGLE: return "Triangle" ; + case NIFTI_INTENT_QUATERNION: return "Quaternion" ; + + case NIFTI_INTENT_DIMLESS: return "Dimensionless number" ; } return "Unknown" ; } @@ -1378,12 +1378,12 @@ char const *nifti_intent_string( int ii ) char const *nifti_slice_string( int ss ) { switch( ss ){ - case NIFTI_SLICE_SEQ_INC: return "sequential_increasing" ; - case NIFTI_SLICE_SEQ_DEC: return "sequential_decreasing" ; - case NIFTI_SLICE_ALT_INC: return "alternating_increasing" ; - case NIFTI_SLICE_ALT_DEC: return "alternating_decreasing" ; - case NIFTI_SLICE_ALT_INC2: return "alternating_increasing_2" ; - case NIFTI_SLICE_ALT_DEC2: return "alternating_decreasing_2" ; + case NIFTI_SLICE_SEQ_INC: return "sequential_increasing" ; + case NIFTI_SLICE_SEQ_DEC: return "sequential_decreasing" ; + case NIFTI_SLICE_ALT_INC: return "alternating_increasing" ; + case NIFTI_SLICE_ALT_DEC: return "alternating_decreasing" ; + case NIFTI_SLICE_ALT_INC2: return "alternating_increasing_2" ; + case NIFTI_SLICE_ALT_DEC2: return "alternating_decreasing_2" ; } return "Unknown" ; } @@ -1403,12 +1403,12 @@ char const *nifti_slice_string( int ss ) char const *nifti_orientation_string( int ii ) { switch( ii ){ - case NIFTI_L2R: return "Left-to-Right" ; - case NIFTI_R2L: return "Right-to-Left" ; - case NIFTI_P2A: return "Posterior-to-Anterior" ; - case NIFTI_A2P: return "Anterior-to-Posterior" ; - case NIFTI_I2S: return "Inferior-to-Superior" ; - case NIFTI_S2I: return "Superior-to-Inferior" ; + case NIFTI_L2R: return "Left-to-Right" ; + case NIFTI_R2L: return "Right-to-Left" ; + case NIFTI_P2A: return "Posterior-to-Anterior" ; + case NIFTI_A2P: return "Anterior-to-Posterior" ; + case NIFTI_I2S: return "Inferior-to-Superior" ; + case NIFTI_S2I: return "Superior-to-Inferior" ; } return "Unknown" ; } @@ -1428,33 +1428,34 @@ void nifti_datatype_sizes( int datatype , int *nbyper, int *swapsize ) { int nb=0, ss=0 ; switch( datatype ){ - case DT_INT8: - case DT_UINT8: nb = 1 ; ss = 0 ; break ; + case DT_INT8: + case DT_UINT8: nb = 1 ; ss = 0 ; break ; - case DT_INT16: - case DT_UINT16: nb = 2 ; ss = 2 ; break ; + case DT_INT16: + case DT_UINT16: nb = 2 ; ss = 2 ; break ; - case DT_RGB24: nb = 3 ; ss = 0 ; break ; - case DT_RGBA32: nb = 4 ; ss = 0 ; break ; + case DT_RGB24: nb = 3 ; ss = 0 ; break ; + case DT_RGBA32: nb = 4 ; ss = 0 ; break ; - case DT_INT32: - case DT_UINT32: - case DT_FLOAT32: nb = 4 ; ss = 4 ; break ; + case DT_INT32: + case DT_UINT32: + case DT_FLOAT32: nb = 4 ; ss = 4 ; break ; - case DT_COMPLEX64: nb = 8 ; ss = 4 ; break ; + case DT_COMPLEX64: nb = 8 ; ss = 4 ; break ; - case DT_FLOAT64: - case DT_INT64: - case DT_UINT64: nb = 8 ; ss = 8 ; break ; + case DT_FLOAT64: + case DT_INT64: + case DT_UINT64: nb = 8 ; ss = 8 ; break ; - case DT_FLOAT128: nb = 16 ; ss = 16 ; break ; + case DT_FLOAT128: nb = 16 ; ss = 16 ; break ; - case DT_COMPLEX128: nb = 16 ; ss = 8 ; break ; + case DT_COMPLEX128: nb = 16 ; ss = 8 ; break ; - case DT_COMPLEX256: nb = 32 ; ss = 16 ; break ; + case DT_COMPLEX256: nb = 32 ; ss = 16 ; break ; } - ASSIF(nbyper,nb) ; ASSIF(swapsize,ss) ; } + ASSIF(nbyper,nb) ; ASSIF(swapsize,ss) ; +} /*---------------------------------------------------------------------------*/ /*! Given the quaternion parameters (etc.), compute a transformation matrix. @@ -1491,11 +1492,11 @@ mat44 nifti_quatern_to_mat44( float qb, float qc, float qd, a = 1.0L - (b*b + c*c + d*d) ; if( a < 1.e-7L ){ /* special case */ - a = 1.0L / sqrt(b*b+c*c+d*d) ; - b *= a ; c *= a ; d *= a ; /* normalize (b,c,d) vector */ - a = 0.0L ; /* a = 0 ==> 180 degree rotation */ + a = 1.0L / sqrt(b*b+c*c+d*d) ; + b *= a ; c *= a ; d *= a ; /* normalize (b,c,d) vector */ + a = 0.0L ; /* a = 0 ==> 180 degree rotation */ } else{ - a = sqrt(a) ; /* angle = 2*arccos(a) */ + a = sqrt(a) ; /* angle = 2*arccos(a) */ } /* load rotation matrix, including scaling factors for voxel sizes */ @@ -1620,10 +1621,10 @@ void nifti_mat44_to_quatern( mat44 R , +r21*r32*r13+r31*r12*r23-r31*r22*r13 ; /* should be -1 or 1 */ if( zd > 0 ){ /* proper */ - ASSIF(qfac,1.0F) ; + ASSIF(qfac,1.0F) ; } else { /* improper ==> flip 3rd column */ - ASSIF(qfac,-1.0F) ; - r13 = -r13 ; r23 = -r23 ; r33 = -r33 ; + ASSIF(qfac,-1.0F) ; + r13 = -r13 ; r23 = -r23 ; r33 = -r33 ; } /* now, compute quaternion parameters */ @@ -1631,31 +1632,31 @@ void nifti_mat44_to_quatern( mat44 R , a = r11 + r22 + r33 + 1.0L ; if( a > 0.5L ){ /* simplest case */ - a = 0.5L * sqrt(a) ; - b = 0.25L * (r32-r23) / a ; - c = 0.25L * (r13-r31) / a ; - d = 0.25L * (r21-r12) / a ; + a = 0.5L * sqrt(a) ; + b = 0.25L * (r32-r23) / a ; + c = 0.25L * (r13-r31) / a ; + d = 0.25L * (r21-r12) / a ; } else { /* trickier case */ - xd = 1.0 + r11 - (r22+r33) ; /* 4*b*b */ - yd = 1.0 + r22 - (r11+r33) ; /* 4*c*c */ - zd = 1.0 + r33 - (r11+r22) ; /* 4*d*d */ - if( xd > 1.0 ){ - b = 0.5L * sqrt(xd) ; - c = 0.25L* (r12+r21) / b ; - d = 0.25L* (r13+r31) / b ; - a = 0.25L* (r32-r23) / b ; - } else if( yd > 1.0 ){ - c = 0.5L * sqrt(yd) ; - b = 0.25L* (r12+r21) / c ; - d = 0.25L* (r23+r32) / c ; - a = 0.25L* (r13-r31) / c ; - } else { - d = 0.5L * sqrt(zd) ; - b = 0.25L* (r13+r31) / d ; - c = 0.25L* (r23+r32) / d ; - a = 0.25L* (r21-r12) / d ; - } - if( a < 0.0L ){ b=-b ; c=-c ; d=-d;} + xd = 1.0 + r11 - (r22+r33) ; /* 4*b*b */ + yd = 1.0 + r22 - (r11+r33) ; /* 4*c*c */ + zd = 1.0 + r33 - (r11+r22) ; /* 4*d*d */ + if( xd > 1.0 ){ + b = 0.5L * sqrt(xd) ; + c = 0.25L* (r12+r21) / b ; + d = 0.25L* (r13+r31) / b ; + a = 0.25L* (r32-r23) / b ; + } else if( yd > 1.0 ){ + c = 0.5L * sqrt(yd) ; + b = 0.25L* (r12+r21) / c ; + d = 0.25L* (r23+r32) / c ; + a = 0.25L* (r13-r31) / c ; + } else { + d = 0.5L * sqrt(zd) ; + b = 0.25L* (r13+r31) / d ; + c = 0.25L* (r23+r32) / d ; + a = 0.25L* (r21-r12) / d ; + } + if( a < 0.0L ){ b=-b ; c=-c ; d=-d;} } ASSIF(qb,(float)b) ; ASSIF(qc,(float)c) ; ASSIF(qd,(float)d); @@ -1764,32 +1765,32 @@ mat44 nifti_make_orthog_mat44( float r11, float r12, float r13 , 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] ; if( val > 0.0L ){ - val = 1.0L / sqrt(val) ; - Q.m[0][0] *= (float)val ; Q.m[0][1] *= (float)val ; Q.m[0][2] *= (float)val ; + val = 1.0L / sqrt(val) ; + Q.m[0][0] *= (float)val ; Q.m[0][1] *= (float)val ; Q.m[0][2] *= (float)val ; } else { - Q.m[0][0] = 1.0L ; Q.m[0][1] = 0.0L ; Q.m[0][2] = 0.0L ; + Q.m[0][0] = 1.0L ; Q.m[0][1] = 0.0L ; Q.m[0][2] = 0.0L ; } /* normalize row 2 */ 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] ; if( val > 0.0L ){ - val = 1.0L / sqrt(val) ; - Q.m[1][0] *= (float)val ; Q.m[1][1] *= (float)val ; Q.m[1][2] *= (float)val ; + val = 1.0L / sqrt(val) ; + Q.m[1][0] *= (float)val ; Q.m[1][1] *= (float)val ; Q.m[1][2] *= (float)val ; } else { - Q.m[1][0] = 0.0L ; Q.m[1][1] = 1.0L ; Q.m[1][2] = 0.0L ; + Q.m[1][0] = 0.0L ; Q.m[1][1] = 1.0L ; Q.m[1][2] = 0.0L ; } /* normalize row 3 */ 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] ; if( val > 0.0L ){ - val = 1.0L / sqrt(val) ; - Q.m[2][0] *= (float)val ; Q.m[2][1] *= (float)val ; Q.m[2][2] *= (float)val ; + val = 1.0L / sqrt(val) ; + Q.m[2][0] *= (float)val ; Q.m[2][1] *= (float)val ; Q.m[2][2] *= (float)val ; } else { - Q.m[2][0] = Q.m[0][1]*Q.m[1][2] - Q.m[0][2]*Q.m[1][1] ; /* cross */ - Q.m[2][1] = Q.m[0][2]*Q.m[1][0] - Q.m[0][0]*Q.m[1][2] ; /* product */ - Q.m[2][2] = Q.m[0][0]*Q.m[1][1] - Q.m[0][1]*Q.m[1][0] ; + Q.m[2][0] = Q.m[0][1]*Q.m[1][2] - Q.m[0][2]*Q.m[1][1] ; /* cross */ + Q.m[2][1] = Q.m[0][2]*Q.m[1][0] - Q.m[0][0]*Q.m[1][2] ; /* product */ + Q.m[2][2] = Q.m[0][0]*Q.m[1][1] - Q.m[0][1]*Q.m[1][0] ; } P = nifti_mat33_polar(Q) ; /* P is orthog matrix closest to Q */ @@ -1885,8 +1886,8 @@ mat33 nifti_mat33_mul( mat33 A , mat33 B ) /* multiply 2 3x3 matrices */ { mat33 C ; int i,j ; for( i=0 ; i < 3 ; i++ ) - for( j=0 ; j < 3 ; j++ ) - C.m[i][j] = A.m[i][0] * B.m[0][j] + for( j=0 ; j < 3 ; j++ ) + C.m[i][j] = A.m[i][0] * B.m[0][j] + A.m[i][1] * B.m[1][j] + A.m[i][2] * B.m[2][j] ; return C ; @@ -1912,40 +1913,40 @@ mat33 nifti_mat33_polar( mat33 A ) gam = nifti_mat33_determ(X) ; while( gam == 0.0 ){ /* perturb matrix */ - gam = (float)( 0.00001 * ( 0.001 + nifti_mat33_rownorm(X) ) ) ; - X.m[0][0] += gam ; X.m[1][1] += gam ; X.m[2][2] += gam ; - gam = nifti_mat33_determ(X) ; + gam = (float)( 0.00001 * ( 0.001 + nifti_mat33_rownorm(X) ) ) ; + X.m[0][0] += gam ; X.m[1][1] += gam ; X.m[2][2] += gam ; + gam = nifti_mat33_determ(X) ; } while(1){ - Y = nifti_mat33_inverse(X) ; - if( dif > 0.3 ){ /* far from convergence */ - alp = (float)( sqrt( nifti_mat33_rownorm(X) * nifti_mat33_colnorm(X) ) ) ; - bet = (float)( sqrt( nifti_mat33_rownorm(Y) * nifti_mat33_colnorm(Y) ) ) ; - gam = (float)( sqrt( bet / alp ) ) ; - gmi = (float)( 1.0 / gam ) ; - } else { - gam = gmi = 1.0F ; /* close to convergence */ - } - Z.m[0][0] = (float)( 0.5 * ( gam*X.m[0][0] + gmi*Y.m[0][0] ) ) ; - Z.m[0][1] = (float)( 0.5 * ( gam*X.m[0][1] + gmi*Y.m[1][0] ) ) ; - Z.m[0][2] = (float)( 0.5 * ( gam*X.m[0][2] + gmi*Y.m[2][0] ) ) ; - Z.m[1][0] = (float)( 0.5 * ( gam*X.m[1][0] + gmi*Y.m[0][1] ) ) ; - Z.m[1][1] = (float)( 0.5 * ( gam*X.m[1][1] + gmi*Y.m[1][1] ) ) ; - Z.m[1][2] = (float)( 0.5 * ( gam*X.m[1][2] + gmi*Y.m[2][1] ) ) ; - Z.m[2][0] = (float)( 0.5 * ( gam*X.m[2][0] + gmi*Y.m[0][2] ) ) ; - Z.m[2][1] = (float)( 0.5 * ( gam*X.m[2][1] + gmi*Y.m[1][2] ) ) ; - Z.m[2][2] = (float)( 0.5 * ( gam*X.m[2][2] + gmi*Y.m[2][2] ) ) ; - - dif = (float)( fabs(Z.m[0][0]-X.m[0][0])+fabs(Z.m[0][1]-X.m[0][1]) + Y = nifti_mat33_inverse(X) ; + if( dif > 0.3 ){ /* far from convergence */ + alp = (float)( sqrt( nifti_mat33_rownorm(X) * nifti_mat33_colnorm(X) ) ) ; + bet = (float)( sqrt( nifti_mat33_rownorm(Y) * nifti_mat33_colnorm(Y) ) ) ; + gam = (float)( sqrt( bet / alp ) ) ; + gmi = (float)( 1.0 / gam ) ; + } else { + gam = gmi = 1.0F ; /* close to convergence */ + } + Z.m[0][0] = (float)( 0.5 * ( gam*X.m[0][0] + gmi*Y.m[0][0] ) ) ; + Z.m[0][1] = (float)( 0.5 * ( gam*X.m[0][1] + gmi*Y.m[1][0] ) ) ; + Z.m[0][2] = (float)( 0.5 * ( gam*X.m[0][2] + gmi*Y.m[2][0] ) ) ; + Z.m[1][0] = (float)( 0.5 * ( gam*X.m[1][0] + gmi*Y.m[0][1] ) ) ; + Z.m[1][1] = (float)( 0.5 * ( gam*X.m[1][1] + gmi*Y.m[1][1] ) ) ; + Z.m[1][2] = (float)( 0.5 * ( gam*X.m[1][2] + gmi*Y.m[2][1] ) ) ; + Z.m[2][0] = (float)( 0.5 * ( gam*X.m[2][0] + gmi*Y.m[0][2] ) ) ; + Z.m[2][1] = (float)( 0.5 * ( gam*X.m[2][1] + gmi*Y.m[1][2] ) ) ; + Z.m[2][2] = (float)( 0.5 * ( gam*X.m[2][2] + gmi*Y.m[2][2] ) ) ; + + dif = (float)( fabs(Z.m[0][0]-X.m[0][0])+fabs(Z.m[0][1]-X.m[0][1]) +fabs(Z.m[0][2]-X.m[0][2])+fabs(Z.m[1][0]-X.m[1][0]) +fabs(Z.m[1][1]-X.m[1][1])+fabs(Z.m[1][2]-X.m[1][2]) +fabs(Z.m[2][0]-X.m[2][0])+fabs(Z.m[2][1]-X.m[2][1]) +fabs(Z.m[2][2]-X.m[2][2]) ); - k = k+1 ; - if( k > 100 || dif < 3.e-6 ) break ; /* convergence or exhaustion */ - X = Z ; + k = k+1 ; + if( k > 100 || dif < 3.e-6 ) break ; /* convergence or exhaustion */ + X = Z ; } return Z ; @@ -2012,10 +2013,10 @@ void nifti_mat44_to_orientation( mat44 R , int *icod, int *jcod, int *kcod ) val = xi*xj + yi*yj + zi*zj ; /* dot product between i and j */ if( fabs(val) > 1.e-4 ){ - xj -= val*xi ; yj -= val*yi ; zj -= val*zi ; - val = (float)sqrt( xj*xj + yj*yj + zj*zj ) ; /* must renormalize */ - if( val == 0.0 ) return ; /* j was parallel to i? */ - xj /= val ; yj /= val ; zj /= val ; + xj -= val*xi ; yj -= val*yi ; zj -= val*zi ; + val = (float)sqrt( xj*xj + yj*yj + zj*zj ) ; /* must renormalize */ + if( val == 0.0 ) return ; /* j was parallel to i? */ + xj /= val ; yj /= val ; zj /= val ; } /* normalize k axis; if it is zero, make it the cross product i x j */ @@ -2028,20 +2029,20 @@ void nifti_mat44_to_orientation( mat44 R , int *icod, int *jcod, int *kcod ) val = xi*xk + yi*yk + zi*zk ; /* dot product between i and k */ if( fabs(val) > 1.e-4 ){ - xk -= val*xi ; yk -= val*yi ; zk -= val*zi ; - val = (float)sqrt( xk*xk + yk*yk + zk*zk ) ; - if( val == 0.0 ) return ; /* bad */ - xk /= val ; yk /= val ; zk /= val ; + xk -= val*xi ; yk -= val*yi ; zk -= val*zi ; + val = (float)sqrt( xk*xk + yk*yk + zk*zk ) ; + if( val == 0.0 ) return ; /* bad */ + xk /= val ; yk /= val ; zk /= val ; } /* orthogonalize k to j */ val = xj*xk + yj*yk + zj*zk ; /* dot product between j and k */ if( fabs(val) > 1.e-4 ){ - xk -= val*xj ; yk -= val*yj ; zk -= val*zj ; - val = (float)sqrt( xk*xk + yk*yk + zk*zk ) ; - if( val == 0.0 ) return ; /* bad */ - xk /= val ; yk /= val ; zk /= val ; + xk -= val*xj ; yk -= val*yj ; zk -= val*zj ; + val = (float)sqrt( xk*xk + yk*yk + zk*zk ) ; + if( val == 0.0 ) return ; /* bad */ + xk /= val ; yk /= val ; zk /= val ; } Q.m[0][0] = xi ; Q.m[0][1] = xj ; Q.m[0][2] = xk ; @@ -2062,31 +2063,36 @@ void nifti_mat44_to_orientation( mat44 R , int *icod, int *jcod, int *kcod ) vbest = -666.0F ; ibest=pbest=qbest=rbest=1 ; jbest=2 ; kbest=3 ; for( i=1 ; i <= 3 ; i++ ){ /* i = column number to use for row #1 */ - for( j=1 ; j <= 3 ; j++ ){ /* j = column number to use for row #2 */ - if( i == j ) continue ; - for( k=1 ; k <= 3 ; k++ ){ /* k = column number to use for row #3 */ - if( i == k || j == k ) continue ; - P.m[0][0] = P.m[0][1] = P.m[0][2] = + for( j=1 ; j <= 3 ; j++ ){ /* j = column number to use for row #2 */ + if( i == j ) continue ; + for( k=1 ; k <= 3 ; k++ ){ /* k = column number to use for row #3 */ + if( i == k || j == k ) continue ; + P.m[0][0] = P.m[0][1] = P.m[0][2] = P.m[1][0] = P.m[1][1] = P.m[1][2] = P.m[2][0] = P.m[2][1] = P.m[2][2] = 0.0F ; - for( p=-1 ; p <= 1 ; p+=2 ){ /* p,q,r are -1 or +1 */ - for( q=-1 ; q <= 1 ; q+=2 ){ /* and go into rows #1,2,3 */ - for( r=-1 ; r <= 1 ; r+=2 ){ - P.m[0][i-1] = p ; P.m[1][j-1] = q ; P.m[2][k-1] = r ; - detP = nifti_mat33_determ(P) ; /* sign of permutation */ - if( detP * detQ <= 0.0 ) continue ; /* doesn't match sign of Q */ - M = nifti_mat33_mul(P,Q) ; + for( p=-1 ; p <= 1 ; p+=2 ){ /* p,q,r are -1 or +1 */ + for( q=-1 ; q <= 1 ; q+=2 ){ /* and go into rows #1,2,3 */ + for( r=-1 ; r <= 1 ; r+=2 ){ + P.m[0][i-1] = p ; P.m[1][j-1] = q ; P.m[2][k-1] = r ; + detP = nifti_mat33_determ(P) ; /* sign of permutation */ + if( detP * detQ <= 0.0 ) continue ; /* doesn't match sign of Q */ + M = nifti_mat33_mul(P,Q) ; /* angle of M rotation = 2.0*acos(0.5*sqrt(1.0+trace(M))) */ /* we want largest trace(M) == smallest angle == M nearest to I */ - val = M.m[0][0] + M.m[1][1] + M.m[2][2] ; /* trace */ - if( val > vbest ){ - vbest = val ; - ibest = i ; jbest = j ; kbest = k ; - pbest = p ; qbest = q ; rbest = r ; - } - }}}}}} + val = M.m[0][0] + M.m[1][1] + M.m[2][2] ; /* trace */ + if( val > vbest ){ + vbest = val ; + ibest = i ; jbest = j ; kbest = k ; + pbest = p ; qbest = q ; rbest = r ; + } + } + } + } + } + } + } /* At this point ibest is 1 or 2 or 3; pbest is -1 or +1; etc. @@ -2103,33 +2109,33 @@ void nifti_mat44_to_orientation( mat44 R , int *icod, int *jcod, int *kcod ) the i axis. Mutatis mutandis for the j and k axes, of course. */ switch( ibest*pbest ){ - case 1: i = NIFTI_L2R ; break ; - case -1: i = NIFTI_R2L ; break ; - case 2: i = NIFTI_P2A ; break ; - case -2: i = NIFTI_A2P ; break ; - case 3: i = NIFTI_I2S ; break ; - case -3: i = NIFTI_S2I ; break ; - default: assert(0) ; break ; + case 1: i = NIFTI_L2R ; break ; + case -1: i = NIFTI_R2L ; break ; + case 2: i = NIFTI_P2A ; break ; + case -2: i = NIFTI_A2P ; break ; + case 3: i = NIFTI_I2S ; break ; + case -3: i = NIFTI_S2I ; break ; + default: assert(0) ; break ; } switch( jbest*qbest ){ - case 1: j = NIFTI_L2R ; break ; - case -1: j = NIFTI_R2L ; break ; - case 2: j = NIFTI_P2A ; break ; - case -2: j = NIFTI_A2P ; break ; - case 3: j = NIFTI_I2S ; break ; - case -3: j = NIFTI_S2I ; break ; - default: assert(0) ; break ; + case 1: j = NIFTI_L2R ; break ; + case -1: j = NIFTI_R2L ; break ; + case 2: j = NIFTI_P2A ; break ; + case -2: j = NIFTI_A2P ; break ; + case 3: j = NIFTI_I2S ; break ; + case -3: j = NIFTI_S2I ; break ; + default: assert(0) ; break ; } switch( kbest*rbest ){ - case 1: k = NIFTI_L2R ; break ; - case -1: k = NIFTI_R2L ; break ; - case 2: k = NIFTI_P2A ; break ; - case -2: k = NIFTI_A2P ; break ; - case 3: k = NIFTI_I2S ; break ; - case -3: k = NIFTI_S2I ; break ; - default: assert(0) ; break ; + case 1: k = NIFTI_L2R ; break ; + case -1: k = NIFTI_R2L ; break ; + case 2: k = NIFTI_P2A ; break ; + case -2: k = NIFTI_A2P ; break ; + case 3: k = NIFTI_I2S ; break ; + case -3: k = NIFTI_S2I ; break ; + default: assert(0) ; break ; } *icod = i ; *jcod = j ; *kcod = k ; @@ -2158,9 +2164,9 @@ void nifti_swap_2bytes( size_t n , void *ar ) /* 2 bytes at a time */ unsigned char tval; for( ii=0 ; ii < n ; ii++ ){ - cp2 = cp1 + 1; - tval = *cp1; *cp1 = *cp2; *cp2 = tval; - cp1 += 2; + cp2 = cp1 + 1; + tval = *cp1; *cp1 = *cp2; *cp2 = tval; + cp1 += 2; } } @@ -2174,11 +2180,11 @@ void nifti_swap_4bytes( size_t n , void *ar ) /* 4 bytes at a time */ unsigned char tval ; for( ii=0 ; ii < n ; ii++ ){ - cp1 = cp0; cp2 = cp0+3; - tval = *cp1; *cp1 = *cp2; *cp2 = tval; - cp1++; cp2--; - tval = *cp1; *cp1 = *cp2; *cp2 = tval; - cp0 += 4; + cp1 = cp0; cp2 = cp0+3; + tval = *cp1; *cp1 = *cp2; *cp2 = tval; + cp1++; cp2--; + tval = *cp1; *cp1 = *cp2; *cp2 = tval; + cp0 += 4; } } @@ -2194,13 +2200,13 @@ void nifti_swap_8bytes( size_t n , void *ar ) /* 8 bytes at a time */ unsigned char tval ; for( ii=0 ; ii < n ; ii++ ){ - cp1 = cp0; cp2 = cp0+7; - while ( cp2 > cp1 ) /* unroll? */ - { - tval = *cp1 ; *cp1 = *cp2 ; *cp2 = tval ; - cp1++; cp2--; - } - cp0 += 8; + cp1 = cp0; cp2 = cp0+7; + while ( cp2 > cp1 ) /* unroll? */ + { + tval = *cp1 ; *cp1 = *cp2 ; *cp2 = tval ; + cp1++; cp2--; + } + cp0 += 8; } } @@ -2214,13 +2220,13 @@ void nifti_swap_16bytes( size_t n , void *ar ) /* 16 bytes at a time */ unsigned char tval ; for( ii=0 ; ii < n ; ii++ ){ - cp1 = cp0; cp2 = cp0+15; - while ( cp2 > cp1 ) - { - tval = *cp1 ; *cp1 = *cp2 ; *cp2 = tval ; - cp1++; cp2--; - } - cp0 += 16; + cp1 = cp0; cp2 = cp0+15; + while ( cp2 > cp1 ) + { + tval = *cp1 ; *cp1 = *cp2 ; *cp2 = tval ; + cp1++; cp2--; + } + cp0 += 16; } } @@ -2236,13 +2242,13 @@ void nifti_swap_bytes( size_t n , int size , void *ar ) unsigned char tval ; for( ii=0 ; ii < n ; ii++ ){ - cp1 = cp0; cp2 = cp0+(size-1); - while ( cp2 > cp1 ) - { - tval = *cp1 ; *cp1 = *cp2 ; *cp2 = tval ; - cp1++; cp2--; - } - cp0 += size; + cp1 = cp0; cp2 = cp0+(size-1); + while ( cp2 > cp1 ) + { + tval = *cp1 ; *cp1 = *cp2 ; *cp2 = tval ; + cp1++; cp2--; + } + cp0 += size; } return ; } @@ -2256,13 +2262,13 @@ void nifti_swap_bytes( size_t n , int size , void *ar ) void nifti_swap_Nbytes( size_t n , int size , void *ar ) /* subsuming case */ { switch( size ){ - case 2: nifti_swap_2bytes ( n , ar ) ; break ; - case 4: nifti_swap_4bytes ( n , ar ) ; break ; - case 8: nifti_swap_8bytes ( n , ar ) ; break ; - case 16: nifti_swap_16bytes( n , ar ) ; break ; - default: /* nifti_swap_bytes ( n , size, ar ) ; */ - fprintf(stderr,"** NIfTI: cannot swap in %d byte blocks\n", size); - break ; + case 2: nifti_swap_2bytes ( n , ar ) ; break ; + case 4: nifti_swap_4bytes ( n , ar ) ; break ; + case 8: nifti_swap_8bytes ( n , ar ) ; break ; + case 16: nifti_swap_16bytes( n , ar ) ; break ; + default: /* nifti_swap_bytes ( n , size, ar ) ; */ + fprintf(stderr,"** NIfTI: cannot swap in %d byte blocks\n", size); + break ; } } @@ -2406,19 +2412,19 @@ void old_swap_nifti_header( struct nifti_1_header *h , int is_nifti ) /* this stuff is NIFTI specific */ if( is_nifti ){ - swap_4(h->intent_p1); swap_4(h->intent_p2); swap_4(h->intent_p3); - swap_2(h->intent_code); + swap_4(h->intent_p1); swap_4(h->intent_p2); swap_4(h->intent_p3); + swap_2(h->intent_code); - swap_2(h->slice_start); swap_2(h->slice_end); - swap_4(h->scl_slope); swap_4(h->scl_inter); - swap_4(h->slice_duration); swap_4(h->toffset); + swap_2(h->slice_start); swap_2(h->slice_end); + swap_4(h->scl_slope); swap_4(h->scl_inter); + swap_4(h->slice_duration); swap_4(h->toffset); - swap_2(h->qform_code); swap_2(h->sform_code); - swap_4(h->quatern_b); swap_4(h->quatern_c); swap_4(h->quatern_d); - swap_4(h->qoffset_x); swap_4(h->qoffset_y); swap_4(h->qoffset_z); - nifti_swap_4bytes(4,h->srow_x); - nifti_swap_4bytes(4,h->srow_y); - nifti_swap_4bytes(4,h->srow_z); + swap_2(h->qform_code); swap_2(h->sform_code); + swap_4(h->quatern_b); swap_4(h->quatern_c); swap_4(h->quatern_d); + swap_4(h->qoffset_x); swap_4(h->qoffset_y); swap_4(h->qoffset_z); + nifti_swap_4bytes(4,h->srow_x); + nifti_swap_4bytes(4,h->srow_y); + nifti_swap_4bytes(4,h->srow_z); } } @@ -2525,7 +2531,7 @@ int nifti_is_complete_filename(const char* fname) if ( ext == NULL ) { /*Invalid extension given */ if ( g_opts.debug > 0 ) fprintf(stderr,"-- no nifti valid extension for filename '%s'\n", fname); - return 0; + return 0; } if ( ext == fname ) { /* then no filename prefix */ @@ -2657,15 +2663,15 @@ const char * nifti_find_file_extension( const char * name ) int nifti_is_gzfile(const char* fname) { /* return true if the filename ends with .gz */ - if (fname == NULL) { return 0; } + if (fname == NULL) { return 0; } #ifdef HAVE_ZLIB - { /* just so len doesn't generate compile warning */ - size_t len = strlen(fname); - if (len < 3) return 0; /* so we don't search before the name */ - if (fileext_compare(fname + strlen(fname) - 3,".gz")==0) { return 1; } - } + { /* just so len doesn't generate compile warning */ + size_t len = strlen(fname); + if (len < 3) return 0; /* so we don't search before the name */ + if (fileext_compare(fname + strlen(fname) - 3,".gz")==0) { return 1; } + } #endif - return 0; + return 0; } /*----------------------------------------------------------------------*/ @@ -2674,9 +2680,9 @@ int nifti_is_gzfile(const char* fname) int nifti_compiled_with_zlib(void) { #ifdef HAVE_ZLIB - return 1; + return 1; #else - return 0; + return 0; #endif } @@ -2695,7 +2701,7 @@ char * nifti_makebasename(const char* fname) ext = nifti_find_file_extension(basename); if ( ext ) { - basename[strlen(basename)-strlen(ext)] = '\0'; /* clear out extension */ + basename[strlen(basename)-strlen(ext)] = '\0'; /* clear out extension */ } return basename; /* in either case */ @@ -2710,7 +2716,7 @@ char * nifti_makebasename(const char* fname) *//*--------------------------------------------------------------------*/ void nifti_set_debug_level( int level ) { - g_opts.debug = level; + g_opts.debug = level; } /*----------------------------------------------------------------------*/ @@ -2720,7 +2726,7 @@ void nifti_set_debug_level( int level ) *//*--------------------------------------------------------------------*/ void nifti_set_skip_blank_ext( int skip ) { - g_opts.skip_blank_ext = skip ? 1 : 0; + g_opts.skip_blank_ext = skip ? 1 : 0; } /*----------------------------------------------------------------------*/ @@ -2730,7 +2736,7 @@ void nifti_set_skip_blank_ext( int skip ) *//*--------------------------------------------------------------------*/ void nifti_set_allow_upper_fext( int allow ) { - g_opts.allow_upper_fext = allow ? 1 : 0; + g_opts.allow_upper_fext = allow ? 1 : 0; } /*----------------------------------------------------------------------*/ @@ -2768,12 +2774,12 @@ char * nifti_findhdrname(const char* fname) /* if the file exists and is a valid header name (not .img), return it */ if ( ext && nifti_fileexists(fname) ) { /* allow for uppercase extension */ - if ( fileext_n_compare(ext,".img",4) != 0 ){ - hdrname = nifti_strdup(fname); - free(basename); - return hdrname; - } else - efirst = 0; /* note for below */ + if ( fileext_n_compare(ext,".img",4) != 0 ){ + hdrname = nifti_strdup(fname); + free(basename); + return hdrname; + } else + efirst = 0; /* note for below */ } /* So the requested name is a basename, contains .img, or does not exist. */ @@ -3301,8 +3307,8 @@ static int is_uppercase(const char * str) if( !str || !*str ) return 0; for(c = 0; c < strlen(str); c++ ) { - if( islower((int) str[c]) ) return 0; - if( !hasupper && isupper((int) str[c]) ) hasupper = 1; + if( islower((int) str[c]) ) return 0; + if( !hasupper && isupper((int) str[c]) ) hasupper = 1; } return hasupper; @@ -3317,8 +3323,8 @@ static int is_mixedcase(const char * str) if( !str || !*str ) return 0; for(c = 0; c < strlen(str); c++ ) { - if( !haslower && islower((int) str[c]) ) haslower = 1; - if( !hasupper && isupper((int) str[c]) ) hasupper = 1; + if( !haslower && islower((int) str[c]) ) haslower = 1; + if( !hasupper && isupper((int) str[c]) ) hasupper = 1; if( haslower && hasupper ) return 1; } @@ -3334,7 +3340,7 @@ static int make_uppercase(char * str) if( !str || !*str ) return 0; for(c = 0; c < strlen(str); c++ ) - if( islower((int) str[c]) ) str[c] = toupper((int) str[c]); + if( islower((int) str[c]) ) str[c] = toupper((int) str[c]); return 0; } @@ -3346,7 +3352,7 @@ static int make_lowercase(char * str) if( !str || !*str ) return 0; for(c = 0; c < strlen(str); c++ ) - if( isupper((int) str[c]) ) str[c] = tolower((int) str[c]); + if( isupper((int) str[c]) ) str[c] = tolower((int) str[c]); return 0; } @@ -3437,7 +3443,7 @@ int nifti_set_type_from_names( nifti_image * nim ) ! nifti_validfilename ( nim->iname ) || ! nifti_find_file_extension( nim->fname ) || ! nifti_find_file_extension( nim->iname ) - ) { + ) { fprintf(stderr,"** NSTFN: invalid filename(s) fname='%s', iname='%s'\n", nim->fname, nim->iname); return -1; @@ -3513,7 +3519,7 @@ int is_nifti_file( const char *hname ) /* check for NIFTI-ness */ if( NIFTI_VERSION(nhdr) != 0 ){ - return ( NIFTI_ONEFILE(nhdr) ) ? 1 : 2 ; + return ( NIFTI_ONEFILE(nhdr) ) ? 1 : 2 ; } /* check for ANALYZE-ness (sizeof_hdr field == 348) */ @@ -3674,15 +3680,15 @@ nifti_image* nifti_convert_nhdr2nim(struct nifti_1_header nhdr, * before swapping header, record the Analyze75 orient code */ if(!is_nifti) - { + { /**- 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. */ - unsigned char c = *((char *)(&nhdr.qform_code)); - nim->analyze75_orient = (analyze_75_orient_code)c; - } + unsigned char c = *((char *)(&nhdr.qform_code)); + nim->analyze75_orient = (analyze_75_orient_code)c; + } if( doswap ) { if ( g_opts.debug > 3 ) disp_nifti_1_header("-d ni1 pre-swap: ", &nhdr); swap_nifti_header( &nhdr , is_nifti ) ; @@ -3692,231 +3698,231 @@ nifti_image* nifti_convert_nhdr2nim(struct nifti_1_header nhdr, if( nhdr.datatype == DT_BINARY || nhdr.datatype == DT_UNKNOWN ) { - free(nim); - ERREX("bad datatype") ; + free(nim); + ERREX("bad datatype") ; } if( nhdr.dim[1] <= 0 ) { - free(nim); - ERREX("bad dim[1]") ; + free(nim); + ERREX("bad dim[1]") ; } /* fix bad dim[] values in the defined dimension range */ for( ii=2 ; ii <= nhdr.dim[0] ; ii++ ) - if( nhdr.dim[ii] <= 0 ) nhdr.dim[ii] = 1 ; + if( nhdr.dim[ii] <= 0 ) nhdr.dim[ii] = 1 ; /* fix any remaining bad dim[] values, so garbage does not propagate */ /* (only values 0 or 1 seem rational, otherwise set to arbitrary 1) */ for( ii=nhdr.dim[0]+1 ; ii <= 7 ; ii++ ) - if( nhdr.dim[ii] != 1 && nhdr.dim[ii] != 0) nhdr.dim[ii] = 1 ; + if( nhdr.dim[ii] != 1 && nhdr.dim[ii] != 0) nhdr.dim[ii] = 1 ; #if 0 /* rely on dim[0], do not attempt to modify it 16 Nov 2005 [rickr] */ /**- get number of dimensions (ignoring dim[0] now) */ for( ii=7 ; ii >= 2 ; ii-- ) /* loop backwards until we */ - if( nhdr.dim[ii] > 1 ) break ; /* find a dim bigger than 1 */ + if( nhdr.dim[ii] > 1 ) break ; /* find a dim bigger than 1 */ ndim = ii ; #endif /**- set bad grid spacings to 1.0 */ for( ii=1 ; ii <= nhdr.dim[0] ; ii++ ){ - if( nhdr.pixdim[ii] == 0.0 || + if( nhdr.pixdim[ii] == 0.0 || !IS_GOOD_FLOAT(nhdr.pixdim[ii]) ) nhdr.pixdim[ii] = 1.0F ; } - is_onefile = is_nifti && NIFTI_ONEFILE(nhdr) ; + is_onefile = is_nifti && NIFTI_ONEFILE(nhdr) ; - if( is_nifti ) nim->nifti_type = (is_onefile) ? NIFTI_FTYPE_NIFTI1_1 + if( is_nifti ) nim->nifti_type = (is_onefile) ? NIFTI_FTYPE_NIFTI1_1 : NIFTI_FTYPE_NIFTI1_2 ; - else nim->nifti_type = NIFTI_FTYPE_ANALYZE ; + else nim->nifti_type = NIFTI_FTYPE_ANALYZE ; - ii = nifti_short_order() ; - if( doswap ) nim->byteorder = REVERSE_ORDER(ii) ; - else nim->byteorder = ii ; + ii = nifti_short_order() ; + if( doswap ) nim->byteorder = REVERSE_ORDER(ii) ; + else nim->byteorder = ii ; /**- set dimensions of data array */ - nim->ndim = nim->dim[0] = nhdr.dim[0]; - nim->nx = nim->dim[1] = nhdr.dim[1]; - nim->ny = nim->dim[2] = nhdr.dim[2]; - nim->nz = nim->dim[3] = nhdr.dim[3]; - nim->nt = nim->dim[4] = nhdr.dim[4]; - nim->nu = nim->dim[5] = nhdr.dim[5]; - nim->nv = nim->dim[6] = nhdr.dim[6]; - nim->nw = nim->dim[7] = nhdr.dim[7]; + nim->ndim = nim->dim[0] = nhdr.dim[0]; + nim->nx = nim->dim[1] = nhdr.dim[1]; + nim->ny = nim->dim[2] = nhdr.dim[2]; + nim->nz = nim->dim[3] = nhdr.dim[3]; + nim->nt = nim->dim[4] = nhdr.dim[4]; + nim->nu = nim->dim[5] = nhdr.dim[5]; + nim->nv = nim->dim[6] = nhdr.dim[6]; + nim->nw = nim->dim[7] = nhdr.dim[7]; - for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) - nim->nvox *= nhdr.dim[ii]; + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + nim->nvox *= nhdr.dim[ii]; /**- set the type of data in voxels and how many bytes per voxel */ - nim->datatype = nhdr.datatype ; + nim->datatype = nhdr.datatype ; - nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; - if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; + if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } /**- set the grid spacings */ - nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; - nim->dy = nim->pixdim[2] = nhdr.pixdim[2] ; - nim->dz = nim->pixdim[3] = nhdr.pixdim[3] ; - nim->dt = nim->pixdim[4] = nhdr.pixdim[4] ; - nim->du = nim->pixdim[5] = nhdr.pixdim[5] ; - nim->dv = nim->pixdim[6] = nhdr.pixdim[6] ; - nim->dw = nim->pixdim[7] = nhdr.pixdim[7] ; + nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; + nim->dy = nim->pixdim[2] = nhdr.pixdim[2] ; + nim->dz = nim->pixdim[3] = nhdr.pixdim[3] ; + nim->dt = nim->pixdim[4] = nhdr.pixdim[4] ; + nim->du = nim->pixdim[5] = nhdr.pixdim[5] ; + nim->dv = nim->pixdim[6] = nhdr.pixdim[6] ; + nim->dw = nim->pixdim[7] = nhdr.pixdim[7] ; /**- compute qto_xyz transformation from pixel indexes (i,j,k) to (x,y,z) */ - if( !is_nifti || nhdr.qform_code <= 0 ){ + if( !is_nifti || nhdr.qform_code <= 0 ){ /**- if not nifti or qform_code <= 0, use grid spacing for qto_xyz */ - nim->qto_xyz.m[0][0] = nim->dx ; /* grid spacings */ - nim->qto_xyz.m[1][1] = nim->dy ; /* along diagonal */ - nim->qto_xyz.m[2][2] = nim->dz ; + nim->qto_xyz.m[0][0] = nim->dx ; /* grid spacings */ + nim->qto_xyz.m[1][1] = nim->dy ; /* along diagonal */ + nim->qto_xyz.m[2][2] = nim->dz ; /* off diagonal is zero */ - nim->qto_xyz.m[0][1]=nim->qto_xyz.m[0][2]=nim->qto_xyz.m[0][3] = 0.0F; - nim->qto_xyz.m[1][0]=nim->qto_xyz.m[1][2]=nim->qto_xyz.m[1][3] = 0.0F; - nim->qto_xyz.m[2][0]=nim->qto_xyz.m[2][1]=nim->qto_xyz.m[2][3] = 0.0F; + nim->qto_xyz.m[0][1]=nim->qto_xyz.m[0][2]=nim->qto_xyz.m[0][3] = 0.0F; + nim->qto_xyz.m[1][0]=nim->qto_xyz.m[1][2]=nim->qto_xyz.m[1][3] = 0.0F; + nim->qto_xyz.m[2][0]=nim->qto_xyz.m[2][1]=nim->qto_xyz.m[2][3] = 0.0F; /* last row is always [ 0 0 0 1 ] */ - nim->qto_xyz.m[3][0]=nim->qto_xyz.m[3][1]=nim->qto_xyz.m[3][2] = 0.0F; - nim->qto_xyz.m[3][3]= 1.0F ; + nim->qto_xyz.m[3][0]=nim->qto_xyz.m[3][1]=nim->qto_xyz.m[3][2] = 0.0F; + nim->qto_xyz.m[3][3]= 1.0F ; - nim->qform_code = NIFTI_XFORM_UNKNOWN ; + nim->qform_code = NIFTI_XFORM_UNKNOWN ; - if( g_opts.debug > 1 ) fprintf(stderr,"-d no qform provided\n"); - } else { + if( g_opts.debug > 1 ) fprintf(stderr,"-d no qform provided\n"); + } else { /**- else NIFTI: use the quaternion-specified transformation */ - nim->quatern_b = FIXED_FLOAT( nhdr.quatern_b ) ; - nim->quatern_c = FIXED_FLOAT( nhdr.quatern_c ) ; - nim->quatern_d = FIXED_FLOAT( nhdr.quatern_d ) ; + nim->quatern_b = FIXED_FLOAT( nhdr.quatern_b ) ; + nim->quatern_c = FIXED_FLOAT( nhdr.quatern_c ) ; + nim->quatern_d = FIXED_FLOAT( nhdr.quatern_d ) ; - nim->qoffset_x = FIXED_FLOAT(nhdr.qoffset_x) ; - nim->qoffset_y = FIXED_FLOAT(nhdr.qoffset_y) ; - nim->qoffset_z = FIXED_FLOAT(nhdr.qoffset_z) ; + nim->qoffset_x = FIXED_FLOAT(nhdr.qoffset_x) ; + nim->qoffset_y = FIXED_FLOAT(nhdr.qoffset_y) ; + nim->qoffset_z = FIXED_FLOAT(nhdr.qoffset_z) ; - nim->qfac = (nhdr.pixdim[0] < 0.0) ? -1.0F : 1.0F ; /* left-handedness? */ + nim->qfac = (nhdr.pixdim[0] < 0.0) ? -1.0F : 1.0F ; /* left-handedness? */ - nim->qto_xyz = nifti_quatern_to_mat44( + nim->qto_xyz = nifti_quatern_to_mat44( nim->quatern_b, nim->quatern_c, nim->quatern_d, nim->qoffset_x, nim->qoffset_y, nim->qoffset_z, nim->dx , nim->dy , nim->dz , nim->qfac ) ; - nim->qform_code = nhdr.qform_code ; + nim->qform_code = nhdr.qform_code ; - if( g_opts.debug > 1 ) - nifti_disp_matrix_orient("-d qform orientations:\n", nim->qto_xyz); - } + if( g_opts.debug > 1 ) + nifti_disp_matrix_orient("-d qform orientations:\n", nim->qto_xyz); + } /**- load inverse transformation (x,y,z) -> (i,j,k) */ - nim->qto_ijk = nifti_mat44_inverse( nim->qto_xyz ) ; + nim->qto_ijk = nifti_mat44_inverse( nim->qto_xyz ) ; /**- load sto_xyz affine transformation, if present */ - if( !is_nifti || nhdr.sform_code <= 0 ){ + if( !is_nifti || nhdr.sform_code <= 0 ){ /**- if not nifti or sform_code <= 0, then no sto transformation */ - nim->sform_code = NIFTI_XFORM_UNKNOWN ; + nim->sform_code = NIFTI_XFORM_UNKNOWN ; - if( g_opts.debug > 1 ) fprintf(stderr,"-d no sform provided\n"); + if( g_opts.debug > 1 ) fprintf(stderr,"-d no sform provided\n"); - } else { + } else { /**- else set the sto transformation from srow_*[] */ - nim->sto_xyz.m[0][0] = nhdr.srow_x[0] ; - nim->sto_xyz.m[0][1] = nhdr.srow_x[1] ; - nim->sto_xyz.m[0][2] = nhdr.srow_x[2] ; - nim->sto_xyz.m[0][3] = nhdr.srow_x[3] ; + nim->sto_xyz.m[0][0] = nhdr.srow_x[0] ; + nim->sto_xyz.m[0][1] = nhdr.srow_x[1] ; + nim->sto_xyz.m[0][2] = nhdr.srow_x[2] ; + nim->sto_xyz.m[0][3] = nhdr.srow_x[3] ; - nim->sto_xyz.m[1][0] = nhdr.srow_y[0] ; - nim->sto_xyz.m[1][1] = nhdr.srow_y[1] ; - nim->sto_xyz.m[1][2] = nhdr.srow_y[2] ; - nim->sto_xyz.m[1][3] = nhdr.srow_y[3] ; + nim->sto_xyz.m[1][0] = nhdr.srow_y[0] ; + nim->sto_xyz.m[1][1] = nhdr.srow_y[1] ; + nim->sto_xyz.m[1][2] = nhdr.srow_y[2] ; + nim->sto_xyz.m[1][3] = nhdr.srow_y[3] ; - nim->sto_xyz.m[2][0] = nhdr.srow_z[0] ; - nim->sto_xyz.m[2][1] = nhdr.srow_z[1] ; - nim->sto_xyz.m[2][2] = nhdr.srow_z[2] ; - nim->sto_xyz.m[2][3] = nhdr.srow_z[3] ; + nim->sto_xyz.m[2][0] = nhdr.srow_z[0] ; + nim->sto_xyz.m[2][1] = nhdr.srow_z[1] ; + nim->sto_xyz.m[2][2] = nhdr.srow_z[2] ; + nim->sto_xyz.m[2][3] = nhdr.srow_z[3] ; /* last row is always [ 0 0 0 1 ] */ - nim->sto_xyz.m[3][0]=nim->sto_xyz.m[3][1]=nim->sto_xyz.m[3][2] = 0.0F; - nim->sto_xyz.m[3][3]= 1.0F ; + nim->sto_xyz.m[3][0]=nim->sto_xyz.m[3][1]=nim->sto_xyz.m[3][2] = 0.0F; + nim->sto_xyz.m[3][3]= 1.0F ; - nim->sto_ijk = nifti_mat44_inverse( nim->sto_xyz ) ; + nim->sto_ijk = nifti_mat44_inverse( nim->sto_xyz ) ; - nim->sform_code = nhdr.sform_code ; + nim->sform_code = nhdr.sform_code ; - if( g_opts.debug > 1 ) - nifti_disp_matrix_orient("-d sform orientations:\n", nim->sto_xyz); - } + if( g_opts.debug > 1 ) + nifti_disp_matrix_orient("-d sform orientations:\n", nim->sto_xyz); + } /**- set miscellaneous NIFTI stuff */ - if( is_nifti ){ - nim->scl_slope = FIXED_FLOAT( nhdr.scl_slope ) ; - nim->scl_inter = FIXED_FLOAT( nhdr.scl_inter ) ; + if( is_nifti ){ + nim->scl_slope = FIXED_FLOAT( nhdr.scl_slope ) ; + nim->scl_inter = FIXED_FLOAT( nhdr.scl_inter ) ; - nim->intent_code = nhdr.intent_code ; + nim->intent_code = nhdr.intent_code ; - nim->intent_p1 = FIXED_FLOAT( nhdr.intent_p1 ) ; - nim->intent_p2 = FIXED_FLOAT( nhdr.intent_p2 ) ; - nim->intent_p3 = FIXED_FLOAT( nhdr.intent_p3 ) ; + nim->intent_p1 = FIXED_FLOAT( nhdr.intent_p1 ) ; + nim->intent_p2 = FIXED_FLOAT( nhdr.intent_p2 ) ; + nim->intent_p3 = FIXED_FLOAT( nhdr.intent_p3 ) ; - nim->toffset = FIXED_FLOAT( nhdr.toffset ) ; + nim->toffset = FIXED_FLOAT( nhdr.toffset ) ; - memcpy(nim->intent_name,nhdr.intent_name,15); nim->intent_name[15] = '\0'; + memcpy(nim->intent_name,nhdr.intent_name,15); nim->intent_name[15] = '\0'; - nim->xyz_units = XYZT_TO_SPACE(nhdr.xyzt_units) ; - nim->time_units = XYZT_TO_TIME (nhdr.xyzt_units) ; + nim->xyz_units = XYZT_TO_SPACE(nhdr.xyzt_units) ; + nim->time_units = XYZT_TO_TIME (nhdr.xyzt_units) ; - nim->freq_dim = DIM_INFO_TO_FREQ_DIM ( nhdr.dim_info ) ; - nim->phase_dim = DIM_INFO_TO_PHASE_DIM( nhdr.dim_info ) ; - nim->slice_dim = DIM_INFO_TO_SLICE_DIM( nhdr.dim_info ) ; + nim->freq_dim = DIM_INFO_TO_FREQ_DIM ( nhdr.dim_info ) ; + nim->phase_dim = DIM_INFO_TO_PHASE_DIM( nhdr.dim_info ) ; + nim->slice_dim = DIM_INFO_TO_SLICE_DIM( nhdr.dim_info ) ; - nim->slice_code = nhdr.slice_code ; - nim->slice_start = nhdr.slice_start ; - nim->slice_end = nhdr.slice_end ; - nim->slice_duration = FIXED_FLOAT(nhdr.slice_duration) ; - } + nim->slice_code = nhdr.slice_code ; + nim->slice_start = nhdr.slice_start ; + nim->slice_end = nhdr.slice_end ; + nim->slice_duration = FIXED_FLOAT(nhdr.slice_duration) ; + } /**- set Miscellaneous ANALYZE stuff */ - nim->cal_min = FIXED_FLOAT(nhdr.cal_min) ; - nim->cal_max = FIXED_FLOAT(nhdr.cal_max) ; + nim->cal_min = FIXED_FLOAT(nhdr.cal_min) ; + nim->cal_max = FIXED_FLOAT(nhdr.cal_max) ; - memcpy(nim->descrip ,nhdr.descrip ,79) ; nim->descrip [79] = '\0' ; - memcpy(nim->aux_file,nhdr.aux_file,23) ; nim->aux_file[23] = '\0' ; + memcpy(nim->descrip ,nhdr.descrip ,79) ; nim->descrip [79] = '\0' ; + memcpy(nim->aux_file,nhdr.aux_file,23) ; nim->aux_file[23] = '\0' ; /**- set ioff from vox_offset (but at least sizeof(header)) */ is_onefile = is_nifti && NIFTI_ONEFILE(nhdr) ; if( is_onefile ){ - ioff = (int)nhdr.vox_offset ; - if( ioff < (int) sizeof(nhdr) ) ioff = (int) sizeof(nhdr) ; + ioff = (int)nhdr.vox_offset ; + if( ioff < (int) sizeof(nhdr) ) ioff = (int) sizeof(nhdr) ; } else { - ioff = (int)nhdr.vox_offset ; + ioff = (int)nhdr.vox_offset ; } nim->iname_offset = ioff ; /**- deal with file names if set */ if (fname!=NULL) { - nifti_set_filenames(nim,fname,0,0); - if (nim->iname==NULL) { ERREX("bad filename"); } + nifti_set_filenames(nim,fname,0,0); + if (nim->iname==NULL) { ERREX("bad filename"); } } else { - nim->fname = NULL; - nim->iname = NULL; + nim->fname = NULL; + nim->iname = NULL; } /* clear extension fields */ @@ -3957,19 +3963,19 @@ nifti_image* nifti_convert_nhdr2nim(struct nifti_1_header nhdr, */ znzFile nifti_image_open(const char * hname, const char * opts, nifti_image ** nim) { - znzFile fptr=NULL; + znzFile fptr=NULL; /* open the hdr and reading it in, but do not load the data */ - *nim = nifti_image_read(hname,0); + *nim = nifti_image_read(hname,0); /* open the image file, ready for reading (compressed works for all reads) */ - if( ((*nim) == NULL) || ((*nim)->iname == NULL) || + if( ((*nim) == NULL) || ((*nim)->iname == NULL) || ((*nim)->nbyper <= 0) || ((*nim)->nvox <= 0) ) - ERREX("bad header info") ; + ERREX("bad header info") ; /* open image data file */ - fptr = znzopen( (*nim)->iname, opts, nifti_is_gzfile((*nim)->iname) ); - if( znz_isnull(fptr) ) ERREX("Can't open data file") ; + fptr = znzopen( (*nim)->iname, opts, nifti_is_gzfile((*nim)->iname) ); + if( znz_isnull(fptr) ) ERREX("Can't open data file") ; - return fptr; + return fptr; } @@ -4119,7 +4125,7 @@ int nifti_hdr_looks_good(const nifti_1_header * hdr) if( ! nifti_datatype_is_valid(hdr->datatype, 0) ){ if( g_opts.debug > 0 ) - fprintf(stderr,"** bad ANALYZE datatype in hdr, %d\n",hdr->datatype); + fprintf(stderr,"** bad ANALYZE datatype in hdr, %d\n",hdr->datatype); errs++; } } @@ -4279,7 +4285,7 @@ nifti_image *nifti_image_read( const char *hname , int read_data ) if ((nim->datatype == DT_COMPLEX64) || (nim->datatype == DT_COMPLEX128) || (nim->datatype == DT_COMPLEX256)) { fprintf(stderr,"Image Exception Unsupported datatype (COMPLEX64): use fslcomplex to manipulate: %s\n", hname); exit(13); - } + } #endif if( nim == NULL ){ @@ -4362,14 +4368,14 @@ nifti_image * nifti_read_ascii_image(znzFile fp, char *fname, int flen, char * sbuf, lfunc[25] = { "nifti_read_ascii_image" }; if( nifti_is_gzfile(fname) ){ - LNI_FERR(lfunc,"compression not supported for file type NIFTI_FTYPE_ASCII", + LNI_FERR(lfunc,"compression not supported for file type NIFTI_FTYPE_ASCII", fname); - return NULL; + return NULL; } if( flen < 0 ){ - LNI_FERR(lfunc,"negative length not allowed", fname); - return NULL; + LNI_FERR(lfunc,"negative length not allowed", fname); + return NULL; } slen = flen; /* slen will be our buffer length */ @@ -4451,7 +4457,7 @@ static int nifti_read_extensions( nifti_image *nim, znzFile fp, int remain ) if( g_opts.debug > 2 ) fprintf(stderr,"-d nre: posn = %lld, offset = %d, type = %d," " remain = %d\n", - (long long)posn, nim->iname_offset, nim->nifti_type, remain); + (long long)posn, nim->iname_offset, nim->nifti_type, remain); if( remain < 16 ){ if( g_opts.debug > 2 ){ @@ -4878,29 +4884,29 @@ static znzFile nifti_image_load_prep( nifti_image *nim ) fp = znzopen(tmpimgname, "rb", nifti_is_gzfile(tmpimgname)); if (znz_isnull(fp)){ - if(g_opts.debug > 0) LNI_FERR(fname,"cannot open data file",tmpimgname); - free(tmpimgname); - return NULL; /* bad open? */ + if(g_opts.debug > 0) LNI_FERR(fname,"cannot open data file",tmpimgname); + free(tmpimgname); + return NULL; /* bad open? */ } free(tmpimgname); /**- get image offset: a negative offset means to figure from end of file */ if( nim->iname_offset < 0 ){ - if( nifti_is_gzfile(nim->iname) ){ - if( g_opts.debug > 0 ) - LNI_FERR(fname,"negative offset for compressed file",nim->iname); - znzclose(fp); - return NULL; - } - ii = nifti_get_filesize( nim->iname ) ; - if( ii == 0 ){ - if( g_opts.debug > 0 ) LNI_FERR(fname,"empty data file",nim->iname); - znzclose(fp); - return NULL; - } - ioff = (ii > ntot) ? ii-ntot : 0 ; + if( nifti_is_gzfile(nim->iname) ){ + if( g_opts.debug > 0 ) + LNI_FERR(fname,"negative offset for compressed file",nim->iname); + znzclose(fp); + return NULL; + } + ii = nifti_get_filesize( nim->iname ) ; + if( ii == 0 ){ + if( g_opts.debug > 0 ) LNI_FERR(fname,"empty data file",nim->iname); + znzclose(fp); + return NULL; + } + ioff = (ii > ntot) ? ii-ntot : 0 ; } else { /* non-negative offset */ - ioff = nim->iname_offset ; /* means use it directly */ + ioff = nim->iname_offset ; /* means use it directly */ } /**- seek to the appropriate read position */ @@ -4955,14 +4961,14 @@ int nifti_image_load( nifti_image *nim ) if( nim->data == NULL ) { - nim->data = (void *)calloc(1,ntot) ; /* create image memory */ - if( nim->data == NULL ){ - if( g_opts.debug > 0 ) - fprintf(stderr,"** failed to alloc %d bytes for image data\n", + nim->data = (void *)calloc(1,ntot) ; /* create image memory */ + if( nim->data == NULL ){ + if( g_opts.debug > 0 ) + fprintf(stderr,"** failed to alloc %d bytes for image data\n", (int)ntot); - znzclose(fp); - return -1; - } + znzclose(fp); + return -1; + } } /**- now that everything is set up, do the reading */ @@ -4999,80 +5005,80 @@ int nifti_image_load( nifti_image *nim ) size_t nifti_read_buffer(znzFile fp, void* dataptr, size_t ntot, nifti_image *nim) { - size_t ii; + size_t ii; - if( dataptr == NULL ){ - if( g_opts.debug > 0 ) - fprintf(stderr,"** ERROR: nifti_read_buffer: NULL dataptr\n"); - return -1; - } + if( dataptr == NULL ){ + if( g_opts.debug > 0 ) + fprintf(stderr,"** ERROR: nifti_read_buffer: NULL dataptr\n"); + return -1; + } - ii = znzread( dataptr , 1 , ntot , fp ) ; /* data input */ + ii = znzread( dataptr , 1 , ntot , fp ) ; /* data input */ /* if read was short, fail */ - if( ii < ntot ){ - if( g_opts.debug > 0 ) - fprintf(stderr,"++ WARNING: nifti_read_buffer(%s):\n" + if( ii < ntot ){ + if( g_opts.debug > 0 ) + fprintf(stderr,"++ WARNING: nifti_read_buffer(%s):\n" " data bytes needed = %u\n" " data bytes input = %u\n" " number missing = %u (set to 0)\n", nim->iname , (unsigned int)ntot , (unsigned int)ii , (unsigned int)(ntot-ii) ) ; /* memset( (char *)(dataptr)+ii , 0 , ntot-ii ) ; now failure [rickr] */ - return -1 ; - } + return -1 ; + } - if( g_opts.debug > 2 ) - fprintf(stderr,"+d nifti_read_buffer: read %u bytes\n", (unsigned)ii); + if( g_opts.debug > 2 ) + fprintf(stderr,"+d nifti_read_buffer: read %u bytes\n", (unsigned)ii); /* byte swap array if needed */ /* ntot/swapsize might not fit as int, use size_t 6 Jul 2010 [rickr] */ - if( nim->swapsize > 1 && nim->byteorder != nifti_short_order() ) { - if( g_opts.debug > 1 ) - fprintf(stderr,"+d nifti_read_buffer: swapping data bytes...\n"); - nifti_swap_Nbytes( (int)(ntot / nim->swapsize), nim->swapsize , dataptr ) ; - } + if( nim->swapsize > 1 && nim->byteorder != nifti_short_order() ) { + if( g_opts.debug > 1 ) + fprintf(stderr,"+d nifti_read_buffer: swapping data bytes...\n"); + nifti_swap_Nbytes( (int)(ntot / nim->swapsize), nim->swapsize , dataptr ) ; + } #ifdef isfinite -{ + { /* check input float arrays for goodness, and fix bad floats */ - int fix_count = 0 ; - - switch( nim->datatype ){ - - case NIFTI_TYPE_FLOAT32: - case NIFTI_TYPE_COMPLEX64:{ - float *far = (float *)dataptr ; size_t jj,nj ; - nj = ntot / sizeof(float) ; - for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ - if( !IS_GOOD_FLOAT(far[jj]) ){ - far[jj] = 0 ; - fix_count++ ; - } - } - break ; - - case NIFTI_TYPE_FLOAT64: - case NIFTI_TYPE_COMPLEX128:{ - double *far = (double *)dataptr ; size_t jj,nj ; - nj = ntot / sizeof(double) ; - for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ - if( !IS_GOOD_FLOAT(far[jj]) ){ - far[jj] = 0 ; - fix_count++ ; - } - } - break ; + int fix_count = 0 ; + + switch( nim->datatype ){ + + case NIFTI_TYPE_FLOAT32: + case NIFTI_TYPE_COMPLEX64: { + float *far = (float *)dataptr ; size_t jj,nj ; + nj = ntot / sizeof(float) ; + for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ + if( !IS_GOOD_FLOAT(far[jj]) ){ + far[jj] = 0 ; + fix_count++ ; + } + } + break ; + + case NIFTI_TYPE_FLOAT64: + case NIFTI_TYPE_COMPLEX128: { + double *far = (double *)dataptr ; size_t jj,nj ; + nj = ntot / sizeof(double) ; + for( jj=0 ; jj < nj ; jj++ ) /* count fixes 30 Nov 2004 [rickr] */ + if( !IS_GOOD_FLOAT(far[jj]) ){ + far[jj] = 0 ; + fix_count++ ; + } + } + break ; - } + } - if( g_opts.debug > 1 ) - fprintf(stderr,"+d in image, %d bad floats were set to 0\n", fix_count); -} + if( g_opts.debug > 1 ) + fprintf(stderr,"+d in image, %d bad floats were set to 0\n", fix_count); + } #endif - return ii; + return ii; } /*--------------------------------------------------------------------------*/ @@ -5081,9 +5087,9 @@ size_t nifti_read_buffer(znzFile fp, void* dataptr, size_t ntot, void nifti_image_unload( nifti_image *nim ) { if( nim != NULL && nim->data != NULL ){ - free(nim->data) ; nim->data = NULL ; - } + free(nim->data) ; nim->data = NULL ; } +} /*--------------------------------------------------------------------------*/ /*! free 'everything' about a nifti_image struct (including the passed struct) @@ -5102,7 +5108,8 @@ void nifti_image_free( nifti_image *nim ) if( nim->iname != NULL ) free(nim->iname) ; if( nim->data != NULL ) free(nim->data ) ; (void)nifti_free_extensions( nim ) ; - free(nim) ; } + free(nim) ; +} /*--------------------------------------------------------------------------*/ @@ -5148,7 +5155,7 @@ void nifti_image_infodump( const nifti_image *nim ) char *str = nifti_image_to_ascii( nim ) ; /* stdout -> stderr 2 Dec 2004 [rickr] */ if( str != NULL ){ fputs(str,stderr) ; free(str) ; } - } +} /*-------------------------------------------------------------------------- @@ -5228,7 +5235,7 @@ int nifti_write_all_data(znzFile fp, nifti_image * nim, if( ss < NBL->bsize ){ fprintf(stderr, "** NWAD ERROR: wrote %u of %u bytes of brick %d of %d to file", - (unsigned)ss, (unsigned)NBL->bsize, bnum+1, NBL->nbricks); + (unsigned)ss, (unsigned)NBL->bsize, bnum+1, NBL->nbricks); return -1; } } @@ -5308,9 +5315,9 @@ static int nifti_write_extensions(znzFile fp, nifti_image *nim) *//*--------------------------------------------------------------------*/ nifti_image* nifti_simple_init_nim(void) { - nifti_image *nim; - struct nifti_1_header nhdr; - int nbyper, swapsize; + nifti_image *nim; + struct nifti_1_header nhdr; + int nbyper, swapsize; memset(&nhdr,0,sizeof(nhdr)) ; /* zero out header, to be safe */ @@ -5499,84 +5506,84 @@ struct nifti_1_header nifti_convert_nim2nhdr(const nifti_image * nim) nhdr.bitpix = 8 * nim->nbyper ; if( nim->cal_max > nim->cal_min ){ - nhdr.cal_max = nim->cal_max ; - nhdr.cal_min = nim->cal_min ; + nhdr.cal_max = nim->cal_max ; + nhdr.cal_min = nim->cal_min ; } if( nim->scl_slope != 0.0 ){ - nhdr.scl_slope = nim->scl_slope ; - nhdr.scl_inter = nim->scl_inter ; + nhdr.scl_slope = nim->scl_slope ; + nhdr.scl_inter = nim->scl_inter ; } if( nim->descrip[0] != '\0' ){ - memcpy(nhdr.descrip ,nim->descrip ,79) ; nhdr.descrip[79] = '\0' ; + memcpy(nhdr.descrip ,nim->descrip ,79) ; nhdr.descrip[79] = '\0' ; } if( nim->aux_file[0] != '\0' ){ - memcpy(nhdr.aux_file ,nim->aux_file ,23) ; nhdr.aux_file[23] = '\0' ; + memcpy(nhdr.aux_file ,nim->aux_file ,23) ; nhdr.aux_file[23] = '\0' ; } /**- Load NIFTI specific stuff into the header */ if( nim->nifti_type > NIFTI_FTYPE_ANALYZE ){ /* then not ANALYZE */ - if( nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcpy(nhdr.magic,"n+1") ; - else strcpy(nhdr.magic,"ni1") ; - - nhdr.pixdim[1] = (float)fabs(nhdr.pixdim[1]) ; nhdr.pixdim[2] = (float)fabs(nhdr.pixdim[2]) ; - nhdr.pixdim[3] = (float)fabs(nhdr.pixdim[3]) ; nhdr.pixdim[4] = (float)fabs(nhdr.pixdim[4]) ; - nhdr.pixdim[5] = (float)fabs(nhdr.pixdim[5]) ; nhdr.pixdim[6] = (float)fabs(nhdr.pixdim[6]) ; - nhdr.pixdim[7] = (float)fabs(nhdr.pixdim[7]) ; - - nhdr.intent_code = nim->intent_code ; - nhdr.intent_p1 = nim->intent_p1 ; - nhdr.intent_p2 = nim->intent_p2 ; - nhdr.intent_p3 = nim->intent_p3 ; - if( nim->intent_name[0] != '\0' ){ - memcpy(nhdr.intent_name,nim->intent_name,15) ; - nhdr.intent_name[15] = '\0' ; - } - - nhdr.vox_offset = (float) nim->iname_offset ; - nhdr.xyzt_units = SPACE_TIME_TO_XYZT( nim->xyz_units, nim->time_units ) ; - nhdr.toffset = nim->toffset ; - - if( nim->qform_code > 0 ){ - nhdr.qform_code = nim->qform_code ; - nhdr.quatern_b = nim->quatern_b ; - nhdr.quatern_c = nim->quatern_c ; - nhdr.quatern_d = nim->quatern_d ; - nhdr.qoffset_x = nim->qoffset_x ; - nhdr.qoffset_y = nim->qoffset_y ; - nhdr.qoffset_z = nim->qoffset_z ; - nhdr.pixdim[0] = (nim->qfac >= 0.0) ? 1.0F : -1.0F ; - } + if( nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ) strcpy(nhdr.magic,"n+1") ; + else strcpy(nhdr.magic,"ni1") ; + + nhdr.pixdim[1] = (float)fabs(nhdr.pixdim[1]) ; nhdr.pixdim[2] = (float)fabs(nhdr.pixdim[2]) ; + nhdr.pixdim[3] = (float)fabs(nhdr.pixdim[3]) ; nhdr.pixdim[4] = (float)fabs(nhdr.pixdim[4]) ; + nhdr.pixdim[5] = (float)fabs(nhdr.pixdim[5]) ; nhdr.pixdim[6] = (float)fabs(nhdr.pixdim[6]) ; + nhdr.pixdim[7] = (float)fabs(nhdr.pixdim[7]) ; + + nhdr.intent_code = nim->intent_code ; + nhdr.intent_p1 = nim->intent_p1 ; + nhdr.intent_p2 = nim->intent_p2 ; + nhdr.intent_p3 = nim->intent_p3 ; + if( nim->intent_name[0] != '\0' ){ + memcpy(nhdr.intent_name,nim->intent_name,15) ; + nhdr.intent_name[15] = '\0' ; + } + + nhdr.vox_offset = (float) nim->iname_offset ; + nhdr.xyzt_units = SPACE_TIME_TO_XYZT( nim->xyz_units, nim->time_units ) ; + nhdr.toffset = nim->toffset ; + + if( nim->qform_code > 0 ){ + nhdr.qform_code = nim->qform_code ; + nhdr.quatern_b = nim->quatern_b ; + nhdr.quatern_c = nim->quatern_c ; + nhdr.quatern_d = nim->quatern_d ; + nhdr.qoffset_x = nim->qoffset_x ; + nhdr.qoffset_y = nim->qoffset_y ; + nhdr.qoffset_z = nim->qoffset_z ; + nhdr.pixdim[0] = (nim->qfac >= 0.0) ? 1.0F : -1.0F ; + } #ifdef FSLSTYLE - else //this helps for regression testing between this library and fsl, there is no other purpose. Without this you get false alarms - nhdr.pixdim[0] = 1.0; //default if unknown and not needed + else //this helps for regression testing between this library and fsl, there is no other purpose. Without this you get false alarms + nhdr.pixdim[0] = 1.0;//default if unknown and not needed #endif - if( nim->sform_code > 0 ){ - nhdr.sform_code = nim->sform_code ; - nhdr.srow_x[0] = nim->sto_xyz.m[0][0] ; - nhdr.srow_x[1] = nim->sto_xyz.m[0][1] ; - nhdr.srow_x[2] = nim->sto_xyz.m[0][2] ; - nhdr.srow_x[3] = nim->sto_xyz.m[0][3] ; - nhdr.srow_y[0] = nim->sto_xyz.m[1][0] ; - nhdr.srow_y[1] = nim->sto_xyz.m[1][1] ; - nhdr.srow_y[2] = nim->sto_xyz.m[1][2] ; - nhdr.srow_y[3] = nim->sto_xyz.m[1][3] ; - nhdr.srow_z[0] = nim->sto_xyz.m[2][0] ; - nhdr.srow_z[1] = nim->sto_xyz.m[2][1] ; - nhdr.srow_z[2] = nim->sto_xyz.m[2][2] ; - nhdr.srow_z[3] = nim->sto_xyz.m[2][3] ; - } - - nhdr.dim_info = FPS_INTO_DIM_INFO( nim->freq_dim , + if( nim->sform_code > 0 ){ + nhdr.sform_code = nim->sform_code ; + nhdr.srow_x[0] = nim->sto_xyz.m[0][0] ; + nhdr.srow_x[1] = nim->sto_xyz.m[0][1] ; + nhdr.srow_x[2] = nim->sto_xyz.m[0][2] ; + nhdr.srow_x[3] = nim->sto_xyz.m[0][3] ; + nhdr.srow_y[0] = nim->sto_xyz.m[1][0] ; + nhdr.srow_y[1] = nim->sto_xyz.m[1][1] ; + nhdr.srow_y[2] = nim->sto_xyz.m[1][2] ; + nhdr.srow_y[3] = nim->sto_xyz.m[1][3] ; + nhdr.srow_z[0] = nim->sto_xyz.m[2][0] ; + nhdr.srow_z[1] = nim->sto_xyz.m[2][1] ; + nhdr.srow_z[2] = nim->sto_xyz.m[2][2] ; + nhdr.srow_z[3] = nim->sto_xyz.m[2][3] ; + } + + nhdr.dim_info = FPS_INTO_DIM_INFO( nim->freq_dim , nim->phase_dim , nim->slice_dim ) ; - nhdr.slice_code = nim->slice_code ; - nhdr.slice_start = nim->slice_start ; - nhdr.slice_end = nim->slice_end ; - nhdr.slice_duration = nim->slice_duration ; + nhdr.slice_code = nim->slice_code ; + nhdr.slice_start = nim->slice_start ; + nhdr.slice_end = nim->slice_end ; + nhdr.slice_duration = nim->slice_duration ; } return nhdr; @@ -5688,28 +5695,28 @@ void nifti_set_iname_offset(nifti_image *nim) switch( nim->nifti_type ){ - default: /* writing into 2 files */ + default: /* writing into 2 files */ /* we only write files with 0 offset in the 2 file format */ - nim->iname_offset = 0 ; - break ; + nim->iname_offset = 0 ; + break ; - /* NIFTI-1 single binary file - always update */ - case NIFTI_FTYPE_NIFTI1_1: - offset = nifti_extension_size(nim)+sizeof(struct nifti_1_header)+4; + /* NIFTI-1 single binary file - always update */ + case NIFTI_FTYPE_NIFTI1_1: + offset = nifti_extension_size(nim)+sizeof(struct nifti_1_header)+4; /* be sure offset is aligned to a 16 byte boundary */ - if ( ( offset % 16 ) != 0 ) offset = ((offset + 0xf) & ~0xf); - if( nim->iname_offset != offset ){ - if( g_opts.debug > 1 ) - fprintf(stderr,"+d changing offset from %d to %d\n", + if ( ( offset % 16 ) != 0 ) offset = ((offset + 0xf) & ~0xf); + if( nim->iname_offset != offset ){ + if( g_opts.debug > 1 ) + fprintf(stderr,"+d changing offset from %d to %d\n", nim->iname_offset, offset); - nim->iname_offset = offset; - } - break ; + nim->iname_offset = offset; + } + break ; - /* non-standard case: NIFTI-1 ASCII header + binary data (single file) */ - case NIFTI_FTYPE_ASCII: - nim->iname_offset = -1 ; /* compute offset from filesize */ - break ; + /* non-standard case: NIFTI-1 ASCII header + binary data (single file) */ + case NIFTI_FTYPE_ASCII: + nim->iname_offset = -1 ; /* compute offset from filesize */ + break ; } } @@ -5729,7 +5736,7 @@ void nifti_set_iname_offset(nifti_image *nim) znzFile nifti_image_write_hdr_img( nifti_image *nim , int write_data , const char* opts ) { - return nifti_image_write_hdr_img2(nim,write_data,opts,NULL,NULL); + return nifti_image_write_hdr_img2(nim,write_data,opts,NULL,NULL); } /*----------------------------------------------------------------------*/ @@ -5758,67 +5765,67 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, #ifdef PIGZ #ifdef HAVE_ZLIB int doPigz2(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { - FILE *pigzPipe; - char command[768]; - strcpy(command, "pigz" ); - strcat(command, " -n -f > \""); - strcat(command, nim->fname); - strcat(command, "\""); - #ifdef _MSC_VER - if (( pigzPipe = _popen(command, "w")) == NULL) - return -1; - #else - if (( pigzPipe = popen(command, "w")) == NULL) - return -1; - #endif - znzFile fp; - fp = (znzFile) calloc(1,sizeof(struct znzptr)); - fp->zfptr = NULL; - fp->withz = 0; - fp->nzfptr = pigzPipe; - fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); - if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) - nifti_write_extensions(fp,nim); - nifti_write_all_data(fp,nim,NBL); - #ifdef _MSC_VER - _pclose(pigzPipe); - #else - pclose(pigzPipe); - #endif - free(fp); - return 0; + FILE *pigzPipe; + char command[768]; + strcpy(command, "pigz" ); + strcat(command, " -n -f > \""); + strcat(command, nim->fname); + strcat(command, "\""); + #ifdef _MSC_VER + if (( pigzPipe = _popen(command, "w")) == NULL) + return -1; + #else + if (( pigzPipe = popen(command, "w")) == NULL) + return -1; + #endif + znzFile fp; + fp = (znzFile) calloc(1,sizeof(struct znzptr)); + fp->zfptr = NULL; + fp->withz = 0; + fp->nzfptr = pigzPipe; + fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); + if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) + nifti_write_extensions(fp,nim); + nifti_write_all_data(fp,nim,NBL); + #ifdef _MSC_VER + _pclose(pigzPipe); + #else + pclose(pigzPipe); + #endif + free(fp); + return 0; } int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { - FILE *pigzPipe; - char command[768]; - strcpy(command, "pigz" ); - strcat(command, " -n -f > \""); - strcat(command, nim->fname); - strcat(command, "\""); - #ifdef _MSC_VER - if (( pigzPipe = _popen(command, "w")) == NULL) - return -1; - #else - if (( pigzPipe = popen(command, "w")) == NULL) - return -1; - #endif - znzFile fp; - fp = (znzFile) calloc(1,sizeof(struct znzptr)); - fp->zfptr = NULL; - fp->withz = 0; - fp->nzfptr = pigzPipe; - fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); - if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) - nifti_write_extensions(fp,nim); - nifti_write_all_data(fp,nim,NBL); - #ifdef _MSC_VER - _pclose(pigzPipe); - #else - pclose(pigzPipe); - #endif - free(fp); - return 0; + FILE *pigzPipe; + char command[768]; + strcpy(command, "pigz" ); + strcat(command, " -n -f > \""); + strcat(command, nim->fname); + strcat(command, "\""); + #ifdef _MSC_VER + if (( pigzPipe = _popen(command, "w")) == NULL) + return -1; + #else + if (( pigzPipe = popen(command, "w")) == NULL) + return -1; + #endif + znzFile fp; + fp = (znzFile) calloc(1,sizeof(struct znzptr)); + fp->zfptr = NULL; + fp->withz = 0; + fp->nzfptr = pigzPipe; + fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); + if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) + nifti_write_extensions(fp,nim); + nifti_write_all_data(fp,nim,NBL); + #ifdef _MSC_VER + _pclose(pigzPipe); + #else + pclose(pigzPipe); + #endif + free(fp); + return 0; } #endif //HAVE_ZLIB #endif //PIGZ @@ -5889,16 +5896,16 @@ static int nifti_image_write_engine(nifti_image *nim, int write_opts, /* if writing to 2 files, make sure iname is set and different from fname */ if( nim->nifti_type != NIFTI_FTYPE_NIFTI1_1 ){ - if( nim->iname && strcmp(nim->iname,nim->fname) == 0 ){ + if( nim->iname && strcmp(nim->iname,nim->fname) == 0 ){ free(nim->iname) ; nim->iname = NULL ; - } - if( nim->iname == NULL ){ /* then make a new one */ + } + if( nim->iname == NULL ){ /* then make a new one */ nim->iname = nifti_makeimgname(nim->fname,nim->nifti_type,0,0); if( nim->iname == NULL ) { *imgfile = NULL; return 1; } - } + } } /* if we have an imgfile and will write the header there, use it */ @@ -5914,15 +5921,15 @@ static int nifti_image_write_engine(nifti_image *nim, int write_opts, #ifdef PIGZ #ifdef HAVE_ZLIB if ((( nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 )) && (nifti_is_gzfile(nim->fname)) && (!leave_open) && (write_data) ) { - const char *key = "AFNI_COMPRESSOR"; - char *value; - value = getenv(key); + const char *key = "AFNI_COMPRESSOR"; + char *value; + value = getenv(key); //export AFNI_COMPRESSOR=PIGZ - char pigzKey[5] = "PIGZ"; - if ((value != NULL) && (strstr(value,pigzKey))) { - if (doPigz(nim, nhdr, NBL) == 0) //success writing with pigz - return 0; - } + char pigzKey[5] = "PIGZ"; + if ((value != NULL) && (strstr(value,pigzKey))) { + if (doPigz(nim, nhdr, NBL) == 0) //success writing with pigz + return 0; + } } #endif //HAVE_ZLIB #endif //PIGZ @@ -6116,23 +6123,23 @@ void nifti_image_write_bricks( nifti_image *nim, const nifti_brick_list * NBL ) *//*--------------------------------------------------------------------*/ nifti_image * nifti_copy_nim_info(const nifti_image * src) { - nifti_image *dest; - dest = (nifti_image *)calloc(1,sizeof(nifti_image)); - if( !dest ){ - fprintf(stderr,"** NCNI: failed to alloc nifti_image\n"); - return NULL; - } - memcpy(dest, src, sizeof(nifti_image)); - if( src->fname ) dest->fname = nifti_strdup(src->fname); - if( src->iname ) dest->iname = nifti_strdup(src->iname); - dest->num_ext = 0; - dest->ext_list = NULL; + nifti_image *dest; + dest = (nifti_image *)calloc(1,sizeof(nifti_image)); + if( !dest ){ + fprintf(stderr,"** NCNI: failed to alloc nifti_image\n"); + return NULL; + } + memcpy(dest, src, sizeof(nifti_image)); + if( src->fname ) dest->fname = nifti_strdup(src->fname); + if( src->iname ) dest->iname = nifti_strdup(src->iname); + dest->num_ext = 0; + dest->ext_list = NULL; /* errors will be printed in NCE(), continue in either case */ - (void)nifti_copy_extensions(dest, src); + (void)nifti_copy_extensions(dest, src); - dest->data = NULL; + dest->data = NULL; - return dest; + return dest; } @@ -6166,33 +6173,33 @@ static int unescape_string( char *str ) for( ii=jj=nn=0 ; ii': lout += 4 ; break ; /* replace '<' with "<" */ + case '<': + case '>': lout += 4 ; break ;/* replace '<' with "<" */ - case '"' : - case '\'': lout += 6 ; break ; /* replace '"' with """ */ + case '"': + case '\'': lout += 6 ; break ; /* replace '"' with """ */ - case CR: - case LF: lout += 6 ; break ; /* replace CR with " " + case CR: + case LF: lout += 6 ; break ;/* replace CR with " " LF with " " */ - default: lout++ ; break ; /* copy all other chars */ - } + default: lout++ ; break ; /* copy all other chars */ + } } out = (char *)calloc(1,lout) ; /* allocate output string */ if( !out ){ @@ -6294,7 +6301,7 @@ static char *escapize_string( const char * str ) case '<': memcpy(out+jj,"<",4) ; jj+=4 ; break ; case '>': memcpy(out+jj,">",4) ; jj+=4 ; break ; - case '"' : memcpy(out+jj,""",6) ; jj+=6 ; break ; + case '"': memcpy(out+jj,""",6) ; jj+=6 ; break ; case '\'': memcpy(out+jj,"'",6) ; jj+=6 ; break ; @@ -6353,30 +6360,30 @@ char *nifti_image_to_ascii( const nifti_image *nim ) snprintf(buf+strlen(buf), bufLen-strlen(buf), " ndim = '%d'\n", nim->ndim); snprintf(buf+strlen(buf), bufLen-strlen(buf), " nx = '%d'\n", nim->nx); if (nim->ndim > 1) - snprintf(buf+strlen(buf), bufLen-strlen(buf), " ny = '%d'\n", nim->ny); + snprintf(buf+strlen(buf), bufLen-strlen(buf), " ny = '%d'\n", nim->ny); if (nim->ndim > 2) - snprintf(buf+strlen(buf), bufLen-strlen(buf), " nz = '%d'\n", nim->nz); + snprintf(buf+strlen(buf), bufLen-strlen(buf), " nz = '%d'\n", nim->nz); if (nim->ndim > 3) - snprintf(buf+strlen(buf), bufLen-strlen(buf), " nt = '%d'\n", nim->nt); + snprintf(buf+strlen(buf), bufLen-strlen(buf), " nt = '%d'\n", nim->nt); if (nim->ndim > 4) - snprintf(buf+strlen(buf), bufLen-strlen(buf), " nu = '%d'\n", nim->nu); + snprintf(buf+strlen(buf), bufLen-strlen(buf), " nu = '%d'\n", nim->nu); if (nim->ndim > 5) - snprintf(buf+strlen(buf), bufLen-strlen(buf), " nv = '%d'\n", nim->nv); + snprintf(buf+strlen(buf), bufLen-strlen(buf), " nv = '%d'\n", nim->nv); if (nim->ndim > 6) - snprintf(buf+strlen(buf), bufLen-strlen(buf), " nw = '%d'\n", nim->nw); + snprintf(buf+strlen(buf), bufLen-strlen(buf), " nw = '%d'\n", nim->nw); snprintf(buf+strlen(buf), bufLen-strlen(buf), " dx = '%g'\n", nim->dx); if (nim->ndim > 1) - snprintf(buf+strlen(buf), bufLen-strlen(buf), " dy = '%g'\n", nim->dy); + snprintf(buf+strlen(buf), bufLen-strlen(buf), " dy = '%g'\n", nim->dy); if (nim->ndim > 2) - snprintf(buf+strlen(buf), bufLen-strlen(buf), " dz = '%g'\n", nim->dz); + snprintf(buf+strlen(buf), bufLen-strlen(buf), " dz = '%g'\n", nim->dz); if (nim->ndim > 3) - snprintf(buf+strlen(buf), bufLen-strlen(buf), " dt = '%g'\n", nim->dt); + snprintf(buf+strlen(buf), bufLen-strlen(buf), " dt = '%g'\n", nim->dt); if (nim->ndim > 4) - snprintf(buf+strlen(buf), bufLen-strlen(buf), " du = '%g'\n", nim->du); + snprintf(buf+strlen(buf), bufLen-strlen(buf), " du = '%g'\n", nim->du); if (nim->ndim > 5) - snprintf(buf+strlen(buf), bufLen-strlen(buf), " dv = '%g'\n", nim->dv); + snprintf(buf+strlen(buf), bufLen-strlen(buf), " dv = '%g'\n", nim->dv); if (nim->ndim > 6) - snprintf(buf+strlen(buf), bufLen-strlen(buf), " dw = '%g'\n", nim->dw); + snprintf(buf+strlen(buf), bufLen-strlen(buf), " dw = '%g'\n", nim->dw); snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " datatype = '%d'\n" , nim->datatype ) ; snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " datatype_name = '%s'\n" , @@ -6389,80 +6396,80 @@ char *nifti_image_to_ascii( const nifti_image *nim ) (nim->byteorder==MSB_FIRST) ? "MSB_FIRST" : "LSB_FIRST" ) ; if( nim->cal_min < nim->cal_max ){ - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " cal_min = '%g'\n", nim->cal_min ) ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " cal_max = '%g'\n", nim->cal_max ) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " cal_min = '%g'\n", nim->cal_min ) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " cal_max = '%g'\n", nim->cal_max ) ; } if( nim->scl_slope != 0.0 ){ - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " scl_slope = '%g'\n" , nim->scl_slope ) ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " scl_inter = '%g'\n" , nim->scl_inter ) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " scl_slope = '%g'\n" , nim->scl_slope ) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " scl_inter = '%g'\n" , nim->scl_inter ) ; } if( nim->intent_code > 0 ){ - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " intent_code = '%d'\n", nim->intent_code ) ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " intent_code_name = '%s'\n" , + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " intent_code = '%d'\n", nim->intent_code ) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " intent_code_name = '%s'\n" , nifti_intent_string(nim->intent_code) ) ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " intent_p1 = '%g'\n" , nim->intent_p1 ) ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " intent_p2 = '%g'\n" , nim->intent_p2 ) ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " intent_p3 = '%g'\n" , nim->intent_p3 ) ; - - if( nim->intent_name[0] != '\0' ){ - ebuf = escapize_string(nim->intent_name) ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " intent_name = %s\n",ebuf) ; - free(ebuf) ; - } + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " intent_p1 = '%g'\n" , nim->intent_p1 ) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " intent_p2 = '%g'\n" , nim->intent_p2 ) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " intent_p3 = '%g'\n" , nim->intent_p3 ) ; + + if( nim->intent_name[0] != '\0' ){ + ebuf = escapize_string(nim->intent_name) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " intent_name = %s\n",ebuf) ; + free(ebuf) ; + } } if( nim->toffset != 0.0 ) - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " toffset = '%g'\n",nim->toffset ) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " toffset = '%g'\n",nim->toffset ) ; if( nim->xyz_units > 0 ) - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " xyz_units = '%d'\n" " xyz_units_name = '%s'\n" , nim->xyz_units , nifti_units_string(nim->xyz_units) ) ; if( nim->time_units > 0 ) - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " time_units = '%d'\n" " time_units_name = '%s'\n" , nim->time_units , nifti_units_string(nim->time_units) ) ; if( nim->freq_dim > 0 ) - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " freq_dim = '%d'\n",nim->freq_dim ) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " freq_dim = '%d'\n",nim->freq_dim ) ; if( nim->phase_dim > 0 ) - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " phase_dim = '%d'\n",nim->phase_dim ) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " phase_dim = '%d'\n",nim->phase_dim ) ; if( nim->slice_dim > 0 ) - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " slice_dim = '%d'\n",nim->slice_dim ) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " slice_dim = '%d'\n",nim->slice_dim ) ; if( nim->slice_code > 0 ) - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " slice_code = '%d'\n" " slice_code_name = '%s'\n" , nim->slice_code , nifti_slice_string(nim->slice_code) ) ; if( nim->slice_start >= 0 && nim->slice_end > nim->slice_start ) - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " slice_start = '%d'\n" " slice_end = '%d'\n" , nim->slice_start , nim->slice_end ) ; if( nim->slice_duration != 0.0 ) - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " slice_duration = '%g'\n", + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " slice_duration = '%g'\n", nim->slice_duration ) ; if( nim->descrip[0] != '\0' ){ - ebuf = escapize_string(nim->descrip) ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " descrip = %s\n",ebuf) ; - free(ebuf) ; + ebuf = escapize_string(nim->descrip) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " descrip = %s\n",ebuf) ; + free(ebuf) ; } if( nim->aux_file[0] != '\0' ){ - ebuf = escapize_string(nim->aux_file) ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " aux_file = %s\n",ebuf) ; - free(ebuf) ; + ebuf = escapize_string(nim->aux_file) ; + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " aux_file = %s\n",ebuf) ; + free(ebuf) ; } if( nim->qform_code > 0 ){ - int i,j,k ; + int i,j,k ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " qform_code = '%d'\n" " qform_code_name = '%s'\n" " qto_xyz_matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" , @@ -6476,7 +6483,7 @@ char *nifti_image_to_ascii( const nifti_image *nim ) nim->qto_xyz.m[3][0] , nim->qto_xyz.m[3][1] , nim->qto_xyz.m[3][2] , nim->qto_xyz.m[3][3] ) ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " qto_ijk_matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" , nim->qto_ijk.m[0][0] , nim->qto_ijk.m[0][1] , nim->qto_ijk.m[0][2] , nim->qto_ijk.m[0][3] , @@ -6487,7 +6494,7 @@ char *nifti_image_to_ascii( const nifti_image *nim ) nim->qto_ijk.m[3][0] , nim->qto_ijk.m[3][1] , nim->qto_ijk.m[3][2] , nim->qto_ijk.m[3][3] ) ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " quatern_b = '%g'\n" " quatern_c = '%g'\n" " quatern_d = '%g'\n" @@ -6498,9 +6505,9 @@ char *nifti_image_to_ascii( const nifti_image *nim ) nim->quatern_b , nim->quatern_c , nim->quatern_d , nim->qoffset_x , nim->qoffset_y , nim->qoffset_z , nim->qfac ) ; - nifti_mat44_to_orientation( nim->qto_xyz , &i,&j,&k ) ; - if( i > 0 && j > 0 && k > 0 ) - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , + nifti_mat44_to_orientation( nim->qto_xyz , &i,&j,&k ) ; + if( i > 0 && j > 0 && k > 0 ) + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " qform_i_orientation = '%s'\n" " qform_j_orientation = '%s'\n" " qform_k_orientation = '%s'\n" , @@ -6510,9 +6517,9 @@ char *nifti_image_to_ascii( const nifti_image *nim ) } if( nim->sform_code > 0 ){ - int i,j,k ; + int i,j,k ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " sform_code = '%d'\n" " sform_code_name = '%s'\n" " sto_xyz_matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" , @@ -6526,7 +6533,7 @@ char *nifti_image_to_ascii( const nifti_image *nim ) nim->sto_xyz.m[3][0] , nim->sto_xyz.m[3][1] , nim->sto_xyz.m[3][2] , nim->sto_xyz.m[3][3] ) ; - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " sto_ijk matrix = '%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g'\n" , nim->sto_ijk.m[0][0] , nim->sto_ijk.m[0][1] , nim->sto_ijk.m[0][2] , nim->sto_ijk.m[0][3] , @@ -6537,9 +6544,9 @@ char *nifti_image_to_ascii( const nifti_image *nim ) nim->sto_ijk.m[3][0] , nim->sto_ijk.m[3][1] , nim->sto_ijk.m[3][2] , nim->sto_ijk.m[3][3] ) ; - nifti_mat44_to_orientation( nim->sto_xyz , &i,&j,&k ) ; - if( i > 0 && j > 0 && k > 0 ) - snprintf( buf+strlen(buf) , bufLen-strlen(buf) , + nifti_mat44_to_orientation( nim->sto_xyz , &i,&j,&k ) ; + if( i > 0 && j > 0 && k > 0 ) + snprintf( buf+strlen(buf) , bufLen-strlen(buf) , " sform_i_orientation = '%s'\n" " sform_j_orientation = '%s'\n" " sform_k_orientation = '%s'\n" , @@ -6632,9 +6639,9 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) } nim->nx = nim->ny = nim->nz = nim->nt - = nim->nu = nim->nv = nim->nw = 1 ; + = nim->nu = nim->nv = nim->nw = 1 ; nim->dx = nim->dy = nim->dz = nim->dt - = nim->du = nim->dv = nim->dw = 0 ; + = nim->du = nim->dv = nim->dw = 0 ; nim->qfac = 1.0F ; nim->byteorder = nifti_short_order() ; @@ -6645,55 +6652,55 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) while(1){ - while( isspace((int) str[spos]) ) spos++ ; /* skip whitespace */ - if( str[spos] == '\0' ) break ; /* end of string? */ + while( isspace((int) str[spos]) ) spos++ ; /* skip whitespace */ + if( str[spos] == '\0' ) break ; /* end of string? */ /* get lhs string */ - ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ; - if( ii == 0 || strcmp(lhs,"/>") == 0 ) break ; /* end of input? */ + ii = sscanf( str+spos , "%1023s%n" , lhs , &nn ) ; spos += nn ; + if( ii == 0 || strcmp(lhs,"/>") == 0 ) break ; /* end of input? */ /* skip whitespace and the '=' marker */ - while( isspace((int) str[spos]) || str[spos] == '=' ) spos++ ; - if( str[spos] == '\0' ) break ; /* end of string? */ + while( isspace((int) str[spos]) || str[spos] == '=' ) spos++ ; + if( str[spos] == '\0' ) break ; /* end of string? */ /* if next character is a quote ', copy everything up to next ' otherwise, copy everything up to next nonblank */ - if( str[spos] == '\'' ){ - ii = spos+1 ; - while( str[ii] != '\0' && str[ii] != '\'' ) ii++ ; - nn = ii-spos-1 ; if( nn > 1023 ) nn = 1023 ; - memcpy(rhs,str+spos+1,nn) ; rhs[nn] = '\0' ; - spos = (str[ii] == '\'') ? ii+1 : ii ; - } else { - ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; spos += nn ; - if( ii == 0 ) break ; /* nothing found? */ - } - unescape_string(rhs) ; /* remove any XML escape sequences */ + if( str[spos] == '\'' ){ + ii = spos+1 ; + while( str[ii] != '\0' && str[ii] != '\'' ) ii++ ; + nn = ii-spos-1 ; if( nn > 1023 ) nn = 1023 ; + memcpy(rhs,str+spos+1,nn) ; rhs[nn] = '\0' ; + spos = (str[ii] == '\'') ? ii+1 : ii ; + } else { + ii = sscanf( str+spos , "%1023s%n" , rhs , &nn ) ; spos += nn ; + if( ii == 0 ) break ; /* nothing found? */ + } + unescape_string(rhs) ; /* remove any XML escape sequences */ /* Now can do the assignment, based on lhs string. Start with special cases that don't fit the QNUM/QSTR macros. */ - if( strcmp(lhs,"nifti_type") == 0 ){ - if( strcmp(rhs,"ANALYZE-7.5") == 0 ) - nim->nifti_type = NIFTI_FTYPE_ANALYZE ; - else if( strcmp(rhs,"NIFTI-1+") == 0 ) - nim->nifti_type = NIFTI_FTYPE_NIFTI1_1 ; - else if( strcmp(rhs,"NIFTI-1") == 0 ) - nim->nifti_type = NIFTI_FTYPE_NIFTI1_2 ; - else if( strcmp(rhs,"NIFTI-1A") == 0 ) - nim->nifti_type = NIFTI_FTYPE_ASCII ; - } - else if( strcmp(lhs,"header_filename") == 0 ){ - nim->fname = nifti_strdup(rhs) ; - } - else if( strcmp(lhs,"image_filename") == 0 ){ - nim->iname = nifti_strdup(rhs) ; - } - else if( strcmp(lhs,"sto_xyz_matrix") == 0 ){ - sscanf( rhs , "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f" , + if( strcmp(lhs,"nifti_type") == 0 ){ + if( strcmp(rhs,"ANALYZE-7.5") == 0 ) + nim->nifti_type = NIFTI_FTYPE_ANALYZE ; + else if( strcmp(rhs,"NIFTI-1+") == 0 ) + nim->nifti_type = NIFTI_FTYPE_NIFTI1_1 ; + else if( strcmp(rhs,"NIFTI-1") == 0 ) + nim->nifti_type = NIFTI_FTYPE_NIFTI1_2 ; + else if( strcmp(rhs,"NIFTI-1A") == 0 ) + nim->nifti_type = NIFTI_FTYPE_ASCII ; + } + else if( strcmp(lhs,"header_filename") == 0 ){ + nim->fname = nifti_strdup(rhs) ; + } + else if( strcmp(lhs,"image_filename") == 0 ){ + nim->iname = nifti_strdup(rhs) ; + } + else if( strcmp(lhs,"sto_xyz_matrix") == 0 ){ + sscanf( rhs , "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f" , &(nim->sto_xyz.m[0][0]) , &(nim->sto_xyz.m[0][1]) , &(nim->sto_xyz.m[0][2]) , &(nim->sto_xyz.m[0][3]) , &(nim->sto_xyz.m[1][0]) , &(nim->sto_xyz.m[1][1]) , @@ -6702,59 +6709,59 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) &(nim->sto_xyz.m[2][2]) , &(nim->sto_xyz.m[2][3]) , &(nim->sto_xyz.m[3][0]) , &(nim->sto_xyz.m[3][1]) , &(nim->sto_xyz.m[3][2]) , &(nim->sto_xyz.m[3][3]) ) ; - } - else if( strcmp(lhs,"byteorder") == 0 ){ - if( strcmp(rhs,"MSB_FIRST") == 0 ) nim->byteorder = MSB_FIRST ; - if( strcmp(rhs,"LSB_FIRST") == 0 ) nim->byteorder = LSB_FIRST ; - } - else QQNUM(image_offset,iname_offset,int) ; - else QNUM(datatype,short int) ; - else QNUM(ndim,int) ; - else QNUM(nx,int) ; - else QNUM(ny,int) ; - else QNUM(nz,int) ; - else QNUM(nt,int) ; - else QNUM(nu,int) ; - else QNUM(nv,int) ; - else QNUM(nw,int) ; - else QNUM(dx,float) ; - else QNUM(dy,float) ; - else QNUM(dz,float) ; - else QNUM(dt,float) ; - else QNUM(du,float) ; - else QNUM(dv,float) ; - else QNUM(dw,float) ; - else QNUM(cal_min,float) ; - else QNUM(cal_max,float) ; - else QNUM(scl_slope,float) ; - else QNUM(scl_inter,float) ; - else QNUM(intent_code,short) ; - else QNUM(intent_p1,float) ; - else QNUM(intent_p2,float) ; - else QNUM(intent_p3,float) ; - else QSTR(intent_name,sizeof(nim->intent_name)) ; - else QNUM(toffset,float) ; - else QNUM(xyz_units,int) ; - else QNUM(time_units,int) ; - else QSTR(descrip,sizeof(nim->descrip)) ; - else QSTR(aux_file,sizeof(nim->aux_file)) ; - else QNUM(qform_code,int) ; - else QNUM(quatern_b,float) ; - else QNUM(quatern_c,float) ; - else QNUM(quatern_d,float) ; - else QNUM(qoffset_x,float) ; - else QNUM(qoffset_y,float) ; - else QNUM(qoffset_z,float) ; - else QNUM(qfac,float) ; - else QNUM(sform_code,int) ; - else QNUM(freq_dim,int) ; - else QNUM(phase_dim,int) ; - else QNUM(slice_dim,int) ; - else QNUM(slice_code,int) ; - else QNUM(slice_start,int) ; - else QNUM(slice_end,int) ; - else QNUM(slice_duration,float) ; - else QNUM(num_ext,int) ; + } + else if( strcmp(lhs,"byteorder") == 0 ){ + if( strcmp(rhs,"MSB_FIRST") == 0 ) nim->byteorder = MSB_FIRST ; + if( strcmp(rhs,"LSB_FIRST") == 0 ) nim->byteorder = LSB_FIRST ; + } + else QQNUM(image_offset,iname_offset,int) ; + else QNUM(datatype,short int) ; + else QNUM(ndim,int) ; + else QNUM(nx,int) ; + else QNUM(ny,int) ; + else QNUM(nz,int) ; + else QNUM(nt,int) ; + else QNUM(nu,int) ; + else QNUM(nv,int) ; + else QNUM(nw,int) ; + else QNUM(dx,float) ; + else QNUM(dy,float) ; + else QNUM(dz,float) ; + else QNUM(dt,float) ; + else QNUM(du,float) ; + else QNUM(dv,float) ; + else QNUM(dw,float) ; + else QNUM(cal_min,float) ; + else QNUM(cal_max,float) ; + else QNUM(scl_slope,float) ; + else QNUM(scl_inter,float) ; + else QNUM(intent_code,short) ; + else QNUM(intent_p1,float) ; + else QNUM(intent_p2,float) ; + else QNUM(intent_p3,float) ; + else QSTR(intent_name,sizeof(nim->intent_name)) ; + else QNUM(toffset,float) ; + else QNUM(xyz_units,int) ; + else QNUM(time_units,int) ; + else QSTR(descrip,sizeof(nim->descrip)) ; + else QSTR(aux_file,sizeof(nim->aux_file)) ; + else QNUM(qform_code,int) ; + else QNUM(quatern_b,float) ; + else QNUM(quatern_c,float) ; + else QNUM(quatern_d,float) ; + else QNUM(qoffset_x,float) ; + else QNUM(qoffset_y,float) ; + else QNUM(qoffset_z,float) ; + else QNUM(qfac,float) ; + else QNUM(sform_code,int) ; + else QNUM(freq_dim,int) ; + else QNUM(phase_dim,int) ; + else QNUM(slice_dim,int) ; + else QNUM(slice_code,int) ; + else QNUM(slice_start,int) ; + else QNUM(slice_end,int) ; + else QNUM(slice_duration,float) ; + else QNUM(num_ext,int) ; } /* end of while loop */ @@ -6780,13 +6787,13 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) * nim->nt * nim->nu * nim->nv * nim->nw ; if( nim->qform_code > 0 ) - nim->qto_xyz = nifti_quatern_to_mat44( + nim->qto_xyz = nifti_quatern_to_mat44( nim->quatern_b, nim->quatern_c, nim->quatern_d, nim->qoffset_x, nim->qoffset_y, nim->qoffset_z, nim->dx , nim->dy , nim->dz , nim->qfac ) ; else - nim->qto_xyz = nifti_quatern_to_mat44( + nim->qto_xyz = nifti_quatern_to_mat44( 0.0F , 0.0F , 0.0F , 0.0F , 0.0F , 0.0F , nim->dx , nim->dy , nim->dz , 0.0F ) ; @@ -6794,7 +6801,7 @@ nifti_image *nifti_image_from_ascii( const char *str, int * bytes_read ) nim->qto_ijk = nifti_mat44_inverse( nim->qto_xyz ) ; if( nim->sform_code > 0 ) - nim->sto_ijk = nifti_mat44_inverse( nim->sto_xyz ) ; + nim->sto_ijk = nifti_mat44_inverse( nim->sto_xyz ) ; return nim ; } @@ -6989,7 +6996,7 @@ int nifti_nim_has_valid_dims(nifti_image * nim, int complain) \sa nifti_image_read, nifti_image_free, nifti_image_read_bricks nifti_image_load *//*-------------------------------------------------------------------------*/ -int nifti_read_collapsed_image( nifti_image * nim, const int dims [8], +int nifti_read_collapsed_image( nifti_image * nim, const int dims[8], void ** data ) { znzFile fp; @@ -7057,12 +7064,12 @@ int nifti_read_collapsed_image( nifti_image * nim, const int dims [8], static void compute_strides(int64_t *strides,const int *size,int nbyper) { - int i; - strides[0] = nbyper; - for(i = 1; i < 7; i++) - { - strides[i] = size[i-1] * strides[i-1]; - } + int i; + strides[0] = nbyper; + for(i = 1; i < 7; i++) + { + strides[i] = size[i-1] * strides[i-1]; + } } /*---------------------------------------------------------------------------*/ @@ -7099,149 +7106,149 @@ int nifti_read_subregion_image( nifti_image * nim, const int *region_size, void ** data ) { - znzFile fp; /* file to read */ - int i,j,k,l,m,n; /* indices for dims */ - long int bytes = 0; /* total # bytes read */ - size_t total_alloc_size; /* size of buffer allocation */ - char *readptr; /* where in *data to read next */ - int64_t strides[7]; /* strides between dimensions */ - int collapsed_dims[8]; /* for read_collapsed_image */ - int *image_size; /* pointer to dimensions in header */ - long int initial_offset; - long int offset; /* seek offset for reading current row */ + znzFile fp; /* file to read */ + int i,j,k,l,m,n; /* indices for dims */ + long int bytes = 0; /* total # bytes read */ + size_t total_alloc_size; /* size of buffer allocation */ + char *readptr; /* where in *data to read next */ + int64_t strides[7]; /* strides between dimensions */ + int collapsed_dims[8]; /* for read_collapsed_image */ + int *image_size; /* pointer to dimensions in header */ + long int initial_offset; + long int offset; /* seek offset for reading current row */ /* probably ignored, but set to ndim for consistency*/ - collapsed_dims[0] = nim->ndim; + collapsed_dims[0] = nim->ndim; /* build a dims array for collapsed image read */ - for(i = 0; i < nim->ndim; i++) - { + for(i = 0; i < nim->ndim; i++) + { /* if you take the whole extent in this dimension */ - if(start_index[i] == 0 && + if(start_index[i] == 0 && region_size[i] == nim->dim[i+1]) { - collapsed_dims[i+1] = -1; + collapsed_dims[i+1] = -1; } /* if you specify a single element in this dimension */ - else if(region_size[i] == 1) + else if(region_size[i] == 1) { - collapsed_dims[i+1] = start_index[i]; + collapsed_dims[i+1] = start_index[i]; } - else + else { - collapsed_dims[i+1] = -2; /* sentinel value */ + collapsed_dims[i+1] = -2; /* sentinel value */ } - } + } /* fill out end of collapsed_dims */ - for(i = nim->ndim ; i < 7; i++) - { - collapsed_dims[i+1] = -1; - } + for(i = nim->ndim ; i < 7; i++) + { + collapsed_dims[i+1] = -1; + } /* check to see whether collapsed read is possible */ - for(i = 1; i <= nim->ndim; i++) - { - if(collapsed_dims[i] == -2) + for(i = 1; i <= nim->ndim; i++) + { + if(collapsed_dims[i] == -2) { - break; + break; } - } + } /* if you get through all the dimensions without hitting ** a subrange of size > 1, a collapsed read is possible */ - if(i > nim->ndim) - { - return nifti_read_collapsed_image(nim, collapsed_dims, data); - } + if(i > nim->ndim) + { + return nifti_read_collapsed_image(nim, collapsed_dims, data); + } /* point past first element of dim, which holds nim->ndim */ - image_size = &(nim->dim[1]); + image_size = &(nim->dim[1]); /* check region sizes for sanity */ - for(i = 0; i < nim->ndim; i++) - { - if(start_index[i] + region_size[i] > image_size[i]) + for(i = 0; i < nim->ndim; i++) + { + if(start_index[i] + region_size[i] > image_size[i]) { - if(g_opts.debug > 1) - { - fprintf(stderr,"region doesn't fit within image size\n"); - } - return -1; + if(g_opts.debug > 1) + { + fprintf(stderr,"region doesn't fit within image size\n"); + } + return -1; } - } + } /* get the file open */ - fp = nifti_image_load_prep( nim ); - if(znz_isnull(fp)) - { - if(g_opts.debug > 0) - fprintf(stderr,"** nifti_read_subregion_image, failed load_prep\n"); - return -1; - } + fp = nifti_image_load_prep( nim ); + if(znz_isnull(fp)) + { + if(g_opts.debug > 0) + fprintf(stderr,"** nifti_read_subregion_image, failed load_prep\n"); + return -1; + } /* the current offset is just past the nifti header, save * location so that SEEK_SET can be used below */ - initial_offset = znztell(fp); + initial_offset = znztell(fp); /* get strides*/ - compute_strides(strides,image_size,nim->nbyper); + compute_strides(strides,image_size,nim->nbyper); - total_alloc_size = nim->nbyper; /* size of pixel */ + total_alloc_size = nim->nbyper; /* size of pixel */ /* find alloc size */ - for(i = 0; i < nim->ndim; i++) - { - total_alloc_size *= region_size[i]; - } + for(i = 0; i < nim->ndim; i++) + { + total_alloc_size *= region_size[i]; + } /* allocate buffer, if necessary */ - if(*data == 0) - { - *data = (void *)malloc(total_alloc_size); - } - - if(*data == 0) - { - if(g_opts.debug > 1) + if(*data == 0) + { + *data = (void *)malloc(total_alloc_size); + } + + if(*data == 0) + { + if(g_opts.debug > 1) { - fprintf(stderr,"allocation of %zu bytes failed\n",total_alloc_size); + fprintf(stderr,"allocation of %zu bytes failed\n",total_alloc_size); } - znzclose(fp); - return -1; - } + znzclose(fp); + return -1; + } /* point to start of data buffer as char * */ - readptr = *((char **)data); - { + readptr = *((char **)data); + { /* can't assume that start_index and region_size have any more than ** nim->ndim elements so make local copies, filled out to seven elements */ - int si[7], rs[7]; - for(i = 0; i < nim->ndim; i++) - { - si[i] = start_index[i]; - rs[i] = region_size[i]; - } - for(i = nim->ndim; i < 7; i++) - { - si[i] = 0; - rs[i] = 1; - } + int si[7], rs[7]; + for(i = 0; i < nim->ndim; i++) + { + si[i] = start_index[i]; + rs[i] = region_size[i]; + } + for(i = nim->ndim; i < 7; i++) + { + si[i] = 0; + rs[i] = 1; + } /* loop through subregion and read a row at a time */ - for(i = si[6]; i < (si[6] + rs[6]); i++) - { - for(j = si[5]; j < (si[5] + rs[5]); j++) + for(i = si[6]; i < (si[6] + rs[6]); i++) { - for(k = si[4]; k < (si[4] + rs[4]); k++) - { - for(l = si[3]; l < (si[3] + rs[3]); l++) - { - for(m = si[2]; m < (si[2] + rs[2]); m++) + for(j = si[5]; j < (si[5] + rs[5]); j++) + { + for(k = si[4]; k < (si[4] + rs[4]); k++) { - for(n = si[1]; n < (si[1] + rs[1]); n++) - { - int nread,read_amount; - offset = initial_offset + + for(l = si[3]; l < (si[3] + rs[3]); l++) + { + for(m = si[2]; m < (si[2] + rs[2]); m++) + { + for(n = si[1]; n < (si[1] + rs[1]); n++) + { + int nread,read_amount; + offset = initial_offset + (i * strides[6]) + (j * strides[5]) + (k * strides[4]) + @@ -7249,29 +7256,29 @@ int nifti_read_subregion_image( nifti_image * nim, (m * strides[2]) + (n * strides[1]) + (si[0] * strides[0]); - znzseek(fp, offset, SEEK_SET); /* seek to current row */ - read_amount = rs[0] * nim->nbyper; /* read a row of the subregion*/ - nread = (int)nifti_read_buffer(fp, readptr, read_amount, nim); - if(nread != read_amount) - { - if(g_opts.debug > 0) - { - fprintf(stderr,"read of %d bytes failed\n",read_amount); + znzseek(fp, offset, SEEK_SET); /* seek to current row */ + read_amount = rs[0] * nim->nbyper; /* read a row of the subregion*/ + nread = (int)nifti_read_buffer(fp, readptr, read_amount, nim); + if(nread != read_amount) + { + if(g_opts.debug > 0) + { + fprintf(stderr,"read of %d bytes failed\n",read_amount); + } + znzclose(fp); + return -1; + } + bytes += nread; + readptr += read_amount; + } } - znzclose(fp); - return -1; - } - bytes += nread; - readptr += read_amount; - } + } } - } - } + } } - } - } - znzclose(fp); - return bytes; + } + znzclose(fp); + return bytes; } @@ -7369,7 +7376,7 @@ static int rci_alloc_mem(void ** data, const int prods[8], int nprods, int nbype } for( memindex = 0, size = 1; memindex < nprods; memindex++ ) - size *= prods[memindex]; + size *= prods[memindex]; size *= nbyper; @@ -7429,10 +7436,10 @@ static int make_pivot_list(nifti_image * nim, const int dims[], int pivots[], if( g_opts.debug > 2 ){ fprintf(stderr,"+d pivot list created, pivots :"); for(dim_index = 0; dim_index < len; dim_index++) - fprintf(stderr," %d", pivots[dim_index]); + fprintf(stderr," %d", pivots[dim_index]); fprintf(stderr,", prods :"); for(dim_index = 0; dim_index < len; dim_index++) - fprintf(stderr," %d", prods[dim_index]); + fprintf(stderr," %d", prods[dim_index]); fputc('\n',stderr); } @@ -7502,7 +7509,7 @@ int * nifti_get_intlist( int nvals , const char * str ) slen = (int)strlen(str) ; while( ipos < slen && !ISEND(str[ipos]) ){ - while( isspace((int) str[ipos]) ) ipos++ ; /* skip blanks */ + while( isspace((int) str[ipos]) ) ipos++ ; /* skip blanks */ if( ISEND(str[ipos]) ) break ; /* done */ /**- get starting value */ @@ -7520,17 +7527,17 @@ int * nifti_get_intlist( int nvals , const char * str ) if( ibot < 0 ){ fprintf(stderr,"** ERROR: list index %d is out of range 0..%d\n", ibot,nvals-1) ; - free(subv) ; return NULL ; + free(subv) ; return NULL ; } if( ibot >= nvals ){ fprintf(stderr,"** ERROR: list index %d is out of range 0..%d\n", ibot,nvals-1) ; - free(subv) ; return NULL ; + free(subv) ; return NULL ; } nused = (cpt-(str+ipos)) ; if( ibot == 0 && nused == 0 ){ - fprintf(stderr,"** ERROR: list syntax error '%s'\n",str+ipos) ; - free(subv) ; return NULL ; + fprintf(stderr,"** ERROR: list syntax error '%s'\n",str+ipos) ; + free(subv) ; return NULL ; } ipos += nused ; } @@ -7541,12 +7548,12 @@ int * nifti_get_intlist( int nvals , const char * str ) if( str[ipos] == ',' || ISEND(str[ipos]) ){ nout++ ; - subv_realloc = (int *)realloc( (char *)subv , sizeof(int) * (nout+1) ) ; + subv_realloc = (int *)realloc( (char *)subv , sizeof(int) * (nout+1) ) ; if( !subv_realloc ) { - free(subv); - fprintf(stderr,"** nifti_get_intlist: failed realloc of %d ints\n", + free(subv); + fprintf(stderr,"** nifti_get_intlist: failed realloc of %d ints\n", nout+1); - return NULL; + return NULL; } subv=subv_realloc; @@ -7581,19 +7588,19 @@ int * nifti_get_intlist( int nvals , const char * str ) } itop = (int)temp; if( itop < 0 ){ - fprintf(stderr,"** ERROR: index %d is out of range 0..%d\n", + fprintf(stderr,"** ERROR: index %d is out of range 0..%d\n", itop,nvals-1) ; - free(subv) ; return NULL ; + free(subv) ; return NULL ; } if( itop >= nvals ){ - fprintf(stderr,"** ERROR: index %d is out of range 0..%d\n", + fprintf(stderr,"** ERROR: index %d is out of range 0..%d\n", itop,nvals-1) ; - free(subv) ; return NULL ; + free(subv) ; return NULL ; } nused = (cpt-(str+ipos)) ; if( itop == 0 && nused == 0 ){ - fprintf(stderr,"** ERROR: index list syntax error '%s'\n",str+ipos) ; - free(subv) ; return NULL ; + fprintf(stderr,"** ERROR: index list syntax error '%s'\n",str+ipos) ; + free(subv) ; return NULL ; } ipos += nused ; } @@ -7616,14 +7623,14 @@ int * nifti_get_intlist( int nvals , const char * str ) } istep = (int)temp; if( istep == 0 ){ - fprintf(stderr,"** ERROR: index loop step is 0!\n") ; - free(subv) ; return NULL ; + fprintf(stderr,"** ERROR: index loop step is 0!\n") ; + free(subv) ; return NULL ; } nused = (cpt-(str+ipos)) ; ipos += nused ; if( str[ipos] == ')' ) ipos++ ; if( (ibot-itop)*istep > 0 ){ - fprintf(stderr,"** WARNING: index list '%d..%d(%d)' means nothing\n", + fprintf(stderr,"** WARNING: index list '%d..%d(%d)' means nothing\n", ibot,itop,istep ) ; } } @@ -7632,12 +7639,12 @@ int * nifti_get_intlist( int nvals , const char * str ) for( ii=ibot ; (ii-itop)*istep <= 0 ; ii += istep ){ nout++ ; - subv_realloc = (int *)realloc( (char *)subv , sizeof(int) * (nout+1) ) ; + subv_realloc = (int *)realloc( (char *)subv , sizeof(int) * (nout+1) ) ; if( !subv_realloc ) { - free(subv); - fprintf(stderr,"** nifti_get_intlist: failed realloc of %d ints\n", + free(subv); + fprintf(stderr,"** nifti_get_intlist: failed realloc of %d ints\n", nout+1); - return NULL; + return NULL; } subv=subv_realloc; subv[0] = nout ; @@ -7668,16 +7675,16 @@ int * nifti_get_intlist( int nvals , const char * str ) *//*-------------------------------------------------------------------*/ int nifti_datatype_from_string( const char * name ) { - int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); - int c; + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int c; - if( !name ) return DT_UNKNOWN; + if( !name ) return DT_UNKNOWN; - for( c = tablen-1; c > 0; c-- ) - if( !strcmp(name, nifti_type_list[c].name) ) - break; + for( c = tablen-1; c > 0; c-- ) + if( !strcmp(name, nifti_type_list[c].name) ) + break; - return nifti_type_list[c].type; + return nifti_type_list[c].type; } @@ -7688,14 +7695,14 @@ int nifti_datatype_from_string( const char * name ) *//*-------------------------------------------------------------------*/ const char * nifti_datatype_to_string( int dtype ) { - int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); - int c; + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int c; - for( c = tablen-1; c > 0; c-- ) - if( nifti_type_list[c].type == dtype ) - break; + for( c = tablen-1; c > 0; c-- ) + if( nifti_type_list[c].type == dtype ) + break; - return nifti_type_list[c].name; + return nifti_type_list[c].name; } @@ -7709,17 +7716,17 @@ const char * nifti_datatype_to_string( int dtype ) *//*-------------------------------------------------------------------*/ int nifti_datatype_is_valid( int dtype, int for_nifti ) { - int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); - int c; + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int c; /* special case */ - if( for_nifti && dtype == DT_BINARY ) return 0; + if( for_nifti && dtype == DT_BINARY ) return 0; - for( c = tablen-1; c > 0; c-- ) - if( nifti_type_list[c].type == dtype ) - return 1; + for( c = tablen-1; c > 0; c-- ) + if( nifti_type_list[c].type == dtype ) + return 1; - return 0; + return 0; } @@ -7732,33 +7739,33 @@ int nifti_datatype_is_valid( int dtype, int for_nifti ) *//*-------------------------------------------------------------------*/ int nifti_test_datatype_sizes(int verb) { - int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); - int nbyper, ssize; - int c, errs = 0; + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int nbyper, ssize; + int c, errs = 0; - for( c = 0; c < tablen; c++ ) - { - nbyper = ssize = -1; - nifti_datatype_sizes(nifti_type_list[c].type, &nbyper, &ssize); - if( nbyper < 0 || ssize < 0 || + for( c = 0; c < tablen; c++ ) + { + nbyper = ssize = -1; + nifti_datatype_sizes(nifti_type_list[c].type, &nbyper, &ssize); + if( nbyper < 0 || ssize < 0 || nbyper != nifti_type_list[c].nbyper || ssize != nifti_type_list[c].swapsize ) - { - if( verb || g_opts.debug > 2 ) - fprintf(stderr, "** type mismatch: %s, %d, %d, %d : %d, %d\n", + { + if( verb || g_opts.debug > 2 ) + fprintf(stderr, "** type mismatch: %s, %d, %d, %d : %d, %d\n", nifti_type_list[c].name, nifti_type_list[c].type, nifti_type_list[c].nbyper, nifti_type_list[c].swapsize, nbyper, ssize); - errs++; - } - } + errs++; + } + } - if( errs ) - fprintf(stderr,"** nifti_test_datatype_sizes: found %d errors\n",errs); - else if( verb || g_opts.debug > 1 ) - fprintf(stderr,"-- nifti_test_datatype_sizes: all OK\n"); + if( errs ) + fprintf(stderr,"** nifti_test_datatype_sizes: found %d errors\n",errs); + else if( verb || g_opts.debug > 1 ) + fprintf(stderr,"-- nifti_test_datatype_sizes: all OK\n"); - return errs; + return errs; } @@ -7771,26 +7778,26 @@ int nifti_test_datatype_sizes(int verb) *//*-------------------------------------------------------------------*/ int nifti_disp_type_list( int which ) { - const char * style; - int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); - int lwhich, c; + const char * style; + int tablen = sizeof(nifti_type_list)/sizeof(nifti_type_ele); + int lwhich, c; - if ( which == 1 ){ lwhich = 1; style = "DT_"; } - else if( which == 2 ){ lwhich = 2; style = "NIFTI_TYPE_"; } - else { lwhich = 3; style = "ALL"; } + if ( which == 1 ){ lwhich = 1; style = "DT_"; } + else if( which == 2 ){ lwhich = 2; style = "NIFTI_TYPE_"; } + else { lwhich = 3; style = "ALL"; } - printf("nifti_type_list entries (%s) :\n" + printf("nifti_type_list entries (%s) :\n" " name type nbyper swapsize\n" " --------------------- ---- ------ --------\n", style); - for( c = 0; c < tablen; c++ ) - if( (lwhich & 1 && nifti_type_list[c].name[0] == 'D') || + for( c = 0; c < tablen; c++ ) + if( (lwhich & 1 && nifti_type_list[c].name[0] == 'D') || (lwhich & 2 && nifti_type_list[c].name[0] == 'N') ) - printf(" %-22s %5d %3d %5d\n", + printf(" %-22s %5d %3d %5d\n", nifti_type_list[c].name, nifti_type_list[c].type, nifti_type_list[c].nbyper, nifti_type_list[c].swapsize); - return 0; + return 0; } diff --git a/niftilib/nifti1_io.h b/niftilib/nifti1_io.h index 10d1270..99ad7c2 100644 --- a/niftilib/nifti1_io.h +++ b/niftilib/nifti1_io.h @@ -80,11 +80,11 @@ extern "C" { /********************** Some sample data structures **************************/ typedef struct { /** 4x4 matrix struct **/ - float m[4][4] ; + float m[4][4] ; } mat44 ; typedef struct { /** 3x3 matrix struct **/ - float m[3][3] ; + float m[3][3] ; } mat33 ; /*...........................................................................*/ @@ -94,13 +94,13 @@ typedef struct { /** 3x3 matrix struct **/ * codes. */ typedef 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 + 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 } analyze_75_orient_code; /*! \struct nifti_image @@ -111,85 +111,85 @@ typedef enum _analyze75_orient_code { */ typedef struct { /*!< Image storage struct **/ - int ndim ; /*!< last dimension greater than 1 (1..7) */ - int nx ; /*!< dimensions of grid array */ - int ny ; /*!< dimensions of grid array */ - int nz ; /*!< dimensions of grid array */ - int nt ; /*!< dimensions of grid array */ - int nu ; /*!< dimensions of grid array */ - int nv ; /*!< dimensions of grid array */ - int nw ; /*!< dimensions of grid array */ - int dim[8] ; /*!< dim[0]=ndim, dim[1]=nx, etc. */ - size_t nvox ; /*!< number of voxels = nx*ny*nz*...*nw */ - int nbyper ; /*!< bytes per voxel, matches datatype */ - int datatype ; /*!< type of data in voxels: DT_* code */ - - float dx ; /*!< grid spacings */ - float dy ; /*!< grid spacings */ - float dz ; /*!< grid spacings */ - float dt ; /*!< grid spacings */ - float du ; /*!< grid spacings */ - float dv ; /*!< grid spacings */ - float dw ; /*!< grid spacings */ - float pixdim[8] ; /*!< pixdim[1]=dx, etc. */ - - float scl_slope ; /*!< scaling parameter - slope */ - float scl_inter ; /*!< scaling parameter - intercept */ - - float cal_min ; /*!< calibration parameter, minimum */ - float cal_max ; /*!< calibration parameter, maximum */ - - int qform_code ; /*!< codes for (x,y,z) space meaning */ - int sform_code ; /*!< codes for (x,y,z) space meaning */ - - int freq_dim ; /*!< indexes (1,2,3, or 0) for MRI */ - int phase_dim ; /*!< directions in dim[]/pixdim[] */ - int slice_dim ; /*!< directions in dim[]/pixdim[] */ - - int slice_code ; /*!< code for slice timing pattern */ - int slice_start ; /*!< index for start of slices */ - int slice_end ; /*!< index for end of slices */ - float slice_duration ; /*!< time between individual slices */ + int ndim ; /*!< last dimension greater than 1 (1..7) */ + int nx ; /*!< dimensions of grid array */ + int ny ; /*!< dimensions of grid array */ + int nz ; /*!< dimensions of grid array */ + int nt ; /*!< dimensions of grid array */ + int nu ; /*!< dimensions of grid array */ + int nv ; /*!< dimensions of grid array */ + int nw ; /*!< dimensions of grid array */ + int dim[8] ; /*!< dim[0]=ndim, dim[1]=nx, etc. */ + size_t nvox ; /*!< number of voxels = nx*ny*nz*...*nw */ + int nbyper ; /*!< bytes per voxel, matches datatype */ + int datatype ; /*!< type of data in voxels: DT_* code */ + + float dx ; /*!< grid spacings */ + float dy ; /*!< grid spacings */ + float dz ; /*!< grid spacings */ + float dt ; /*!< grid spacings */ + float du ; /*!< grid spacings */ + float dv ; /*!< grid spacings */ + float dw ; /*!< grid spacings */ + float pixdim[8] ; /*!< pixdim[1]=dx, etc. */ + + float scl_slope ; /*!< scaling parameter - slope */ + float scl_inter ; /*!< scaling parameter - intercept */ + + float cal_min ; /*!< calibration parameter, minimum */ + float cal_max ; /*!< calibration parameter, maximum */ + + int qform_code ; /*!< codes for (x,y,z) space meaning */ + int sform_code ; /*!< codes for (x,y,z) space meaning */ + + int freq_dim ; /*!< indexes (1,2,3, or 0) for MRI */ + int phase_dim ; /*!< directions in dim[]/pixdim[] */ + int slice_dim ; /*!< directions in dim[]/pixdim[] */ + + int slice_code ; /*!< code for slice timing pattern */ + int slice_start ; /*!< index for start of slices */ + int slice_end ; /*!< index for end of slices */ + float slice_duration ; /*!< time between individual slices */ /*! quaternion transform parameters [when writing a dataset, these are used for qform, NOT qto_xyz] */ - float quatern_b , quatern_c , quatern_d , + float quatern_b , quatern_c , quatern_d , qoffset_x , qoffset_y , qoffset_z , qfac ; - mat44 qto_xyz ; /*!< qform: transform (i,j,k) to (x,y,z) */ - mat44 qto_ijk ; /*!< qform: transform (x,y,z) to (i,j,k) */ + mat44 qto_xyz ; /*!< qform: transform (i,j,k) to (x,y,z) */ + mat44 qto_ijk ; /*!< qform: transform (x,y,z) to (i,j,k) */ - mat44 sto_xyz ; /*!< sform: transform (i,j,k) to (x,y,z) */ - mat44 sto_ijk ; /*!< sform: transform (x,y,z) to (i,j,k) */ + mat44 sto_xyz ; /*!< sform: transform (i,j,k) to (x,y,z) */ + mat44 sto_ijk ; /*!< sform: transform (x,y,z) to (i,j,k) */ - float toffset ; /*!< time coordinate offset */ + float toffset ; /*!< time coordinate offset */ - int xyz_units ; /*!< dx,dy,dz units: NIFTI_UNITS_* code */ - int time_units ; /*!< dt units: NIFTI_UNITS_* code */ + int xyz_units ; /*!< dx,dy,dz units: NIFTI_UNITS_* code */ + int time_units ; /*!< dt units: NIFTI_UNITS_* code */ - int nifti_type ; /*!< 0==ANALYZE, 1==NIFTI-1 (1 file), + int nifti_type ; /*!< 0==ANALYZE, 1==NIFTI-1 (1 file), 2==NIFTI-1 (2 files), 3==NIFTI-ASCII (1 file) */ - int intent_code ; /*!< statistic type (or something) */ - float intent_p1 ; /*!< intent parameters */ - float intent_p2 ; /*!< intent parameters */ - float intent_p3 ; /*!< intent parameters */ - char intent_name[16] ; /*!< optional description of intent data */ - - char descrip[80] ; /*!< optional text to describe dataset */ - char aux_file[24] ; /*!< auxiliary filename */ - - char *fname ; /*!< header filename (.hdr or .nii) */ - char *iname ; /*!< image filename (.img or .nii) */ - int iname_offset ; /*!< offset into iname where data starts */ - int swapsize ; /*!< swap unit in image data (might be 0) */ - int byteorder ; /*!< byte order on disk (MSB_ or LSB_FIRST) */ - void *data ; /*!< pointer to data: nbyper*nvox bytes */ - - int num_ext ; /*!< number of extensions in ext_list */ - nifti1_extension * ext_list ; /*!< array of extension structs (with data) */ - analyze_75_orient_code analyze75_orient; /*!< for old analyze files, orient */ + int intent_code ; /*!< statistic type (or something) */ + float intent_p1 ; /*!< intent parameters */ + float intent_p2 ; /*!< intent parameters */ + float intent_p3 ; /*!< intent parameters */ + char intent_name[16] ; /*!< optional description of intent data */ + + char descrip[80] ; /*!< optional text to describe dataset */ + char aux_file[24] ; /*!< auxiliary filename */ + + char *fname ; /*!< header filename (.hdr or .nii) */ + char *iname ; /*!< image filename (.img or .nii) */ + int iname_offset ; /*!< offset into iname where data starts */ + int swapsize ; /*!< swap unit in image data (might be 0) */ + int byteorder ; /*!< byte order on disk (MSB_ or LSB_FIRST) */ + void *data ; /*!< pointer to data: nbyper*nvox bytes */ + + int num_ext ; /*!< number of extensions in ext_list */ + nifti1_extension * ext_list ; /*!< array of extension structs (with data) */ + analyze_75_orient_code analyze75_orient; /*!< for old analyze files, orient */ } nifti_image ; @@ -197,9 +197,9 @@ typedef struct { /*!< Image storage struct **/ /* struct for return from nifti_image_read_bricks() */ typedef struct { - int nbricks; /* the number of allocated pointers in 'bricks' */ - size_t bsize; /* the length of each data block, in bytes */ - void ** bricks; /* array of pointers to data blocks */ + int nbricks; /* the number of allocated pointers in 'bricks' */ + size_t bsize; /* the length of each data block, in bytes */ + void ** bricks; /* array of pointers to data blocks */ } nifti_brick_list; @@ -211,57 +211,57 @@ typedef struct { typedef struct { /* header info fields - describes the header overlap with NIfTI */ /* ------------------ */ - int sizeof_hdr; /* 0 + 4 same */ - char data_type[10]; /* 4 + 10 same */ - char db_name[18]; /* 14 + 18 same */ - int extents; /* 32 + 4 same */ - short int session_error; /* 36 + 2 same */ - char regular; /* 38 + 1 same */ - char hkey_un0; /* 39 + 1 40 bytes */ + int sizeof_hdr; /* 0 + 4 same */ + char data_type[10]; /* 4 + 10 same */ + char db_name[18]; /* 14 + 18 same */ + int extents; /* 32 + 4 same */ + short int session_error; /* 36 + 2 same */ + char regular; /* 38 + 1 same */ + char hkey_un0; /* 39 + 1 40 bytes */ /* image dimension fields - describes image sizes */ - short int dim[8]; /* 0 + 16 same */ - short int unused8; /* 16 + 2 intent_p1... */ - short int unused9; /* 18 + 2 ... */ - short int unused10; /* 20 + 2 intent_p2... */ - short int unused11; /* 22 + 2 ... */ - short int unused12; /* 24 + 2 intent_p3... */ - short int unused13; /* 26 + 2 ... */ - short int unused14; /* 28 + 2 intent_code */ - short int datatype; /* 30 + 2 same */ - short int bitpix; /* 32 + 2 same */ - short int dim_un0; /* 34 + 2 slice_start */ - float pixdim[8]; /* 36 + 32 same */ - - float vox_offset; /* 68 + 4 same */ - float funused1; /* 72 + 4 scl_slope */ - float funused2; /* 76 + 4 scl_inter */ - float funused3; /* 80 + 4 slice_end, */ + short int dim[8]; /* 0 + 16 same */ + short int unused8; /* 16 + 2 intent_p1... */ + short int unused9; /* 18 + 2 ... */ + short int unused10; /* 20 + 2 intent_p2... */ + short int unused11; /* 22 + 2 ... */ + short int unused12; /* 24 + 2 intent_p3... */ + short int unused13; /* 26 + 2 ... */ + short int unused14; /* 28 + 2 intent_code */ + short int datatype; /* 30 + 2 same */ + short int bitpix; /* 32 + 2 same */ + short int dim_un0; /* 34 + 2 slice_start */ + float pixdim[8]; /* 36 + 32 same */ + + float vox_offset; /* 68 + 4 same */ + float funused1; /* 72 + 4 scl_slope */ + float funused2; /* 76 + 4 scl_inter */ + float funused3; /* 80 + 4 slice_end, */ /* slice_code, */ /* xyzt_units */ - float cal_max; /* 84 + 4 same */ - float cal_min; /* 88 + 4 same */ - float compressed; /* 92 + 4 slice_duration */ - float verified; /* 96 + 4 toffset */ - int glmax,glmin; /* 100 + 8 108 bytes */ + float cal_max; /* 84 + 4 same */ + float cal_min; /* 88 + 4 same */ + float compressed; /* 92 + 4 slice_duration */ + float verified; /* 96 + 4 toffset */ + int glmax,glmin; /* 100 + 8 108 bytes */ /* data history fields - optional */ - char descrip[80]; /* 0 + 80 same */ - char aux_file[24]; /* 80 + 24 same */ - char orient; /* 104 + 1 NO GOOD OVERLAP */ - char originator[10]; /* 105 + 10 FROM HERE DOWN... */ - char generated[10]; /* 115 + 10 */ - char scannum[10]; /* 125 + 10 */ - char patient_id[10]; /* 135 + 10 */ - char exp_date[10]; /* 145 + 10 */ - char exp_time[10]; /* 155 + 10 */ - char hist_un0[3]; /* 165 + 3 */ - int views; /* 168 + 4 */ - int vols_added; /* 172 + 4 */ - int start_field; /* 176 + 4 */ - int field_skip; /* 180 + 4 */ - int omax, omin; /* 184 + 8 */ - int smax, smin; /* 192 + 8 200 bytes */ + char descrip[80]; /* 0 + 80 same */ + char aux_file[24]; /* 80 + 24 same */ + char orient; /* 104 + 1 NO GOOD OVERLAP */ + char originator[10]; /* 105 + 10 FROM HERE DOWN... */ + char generated[10]; /* 115 + 10 */ + char scannum[10]; /* 125 + 10 */ + char patient_id[10]; /* 135 + 10 */ + char exp_date[10]; /* 145 + 10 */ + char exp_time[10]; /* 155 + 10 */ + char hist_un0[3]; /* 165 + 3 */ + int views; /* 168 + 4 */ + int vols_added; /* 172 + 4 */ + int start_field; /* 176 + 4 */ + int field_skip; /* 180 + 4 */ + int omax, omin; /* 184 + 8 */ + int smax, smin; /* 192 + 8 200 bytes */ } nifti_analyze75; /* total: 348 bytes */ @@ -315,7 +315,7 @@ NIO_API int nifti_image_load ( nifti_image *nim ) ; NIO_API void nifti_image_unload ( nifti_image *nim ) ; NIO_API void nifti_image_free ( nifti_image *nim ) ; -NIO_API int nifti_read_collapsed_image( nifti_image * nim, const int dims [8], +NIO_API int nifti_read_collapsed_image( nifti_image * nim, const int dims[8], void ** data ); NIO_API int nifti_read_subregion_image( nifti_image * nim, @@ -367,7 +367,7 @@ NIO_API znzFile nifti_image_open( const char * hname, const char * opts, nifti_i NIO_API znzFile nifti_image_write_hdr_img( nifti_image *nim, int write_data, const char* opts); NIO_API znzFile nifti_image_write_hdr_img2( nifti_image *nim , int write_opts , - const char* opts, znzFile imgfile, + const char* opts, znzFile imgfile, const nifti_brick_list * NBL); NIO_API size_t nifti_read_buffer( znzFile fp, void* dataptr, size_t ntot, nifti_image *nim); @@ -377,7 +377,7 @@ NIO_API size_t nifti_write_buffer( znzFile fp, const void * buffer, size_t numb NIO_API nifti_image *nifti_read_ascii_image( znzFile fp, char *fname, int flen, int read_data); NIO_API znzFile nifti_write_ascii_image( nifti_image *nim, const nifti_brick_list * NBL, - const char * opts, int write_data, + const char * opts, int write_data, int leave_open); @@ -533,16 +533,16 @@ NIO_API int valid_nifti_extensions(const nifti_image *nim); #ifdef NIFTI1_IO_C typedef struct { - int debug; /*!< debug level for status reports */ - int skip_blank_ext; /*!< skip extender if no extensions */ - int allow_upper_fext; /*!< allow uppercase file extensions */ + int debug; /*!< debug level for status reports */ + int skip_blank_ext; /*!< skip extender if no extensions */ + int allow_upper_fext; /*!< allow uppercase file extensions */ } nifti_global_options; typedef struct { - int type; /* should match the NIFTI_TYPE_ #define */ - int nbyper; /* bytes per value, matches nifti_image */ - int swapsize; /* bytes per swap piece, matches nifti_image */ - char const * const name; /* text string to match #define */ + int type; /* should match the NIFTI_TYPE_ #define */ + int nbyper; /* bytes per value, matches nifti_image */ + int swapsize; /* bytes per swap piece, matches nifti_image */ + char const * const name; /* text string to match #define */ } nifti_type_ele; #undef LNI_FERR /* local nifti file error, to be compact and repetitive */ diff --git a/niftilib/nifti1_test.c b/niftilib/nifti1_test.c index 1e2e439..1a56be7 100644 --- a/niftilib/nifti1_test.c +++ b/niftilib/nifti1_test.c @@ -3,9 +3,9 @@ static int local_fileexists(const char* fname) { - znzFile fp = znzopen( fname , "rb" , 1 ) ; - if( !znz_isnull(fp) ) { znzclose(fp); return 1; } - return 0; /* fp is NULL */ + znzFile fp = znzopen( fname , "rb" , 1 ) ; + if( !znz_isnull(fp) ) { znzclose(fp); return 1; } + return 0; /* fp is NULL */ } /*-----------------------------------------------*/ @@ -22,18 +22,18 @@ int main( int argc , char *argv[] ) size_t ll; if( argc < 2 || strcmp(argv[1],"-help") == 0 ){ - printf("Usage: nifti1_test [-n2|-n1|-na|-a2] infile [prefix]\n" + printf("Usage: nifti1_test [-n2|-n1|-na|-a2] infile [prefix]\n" "\n" " If prefix is given, then the options mean:\n" " -a2 ==> write an ANALYZE 7.5 file pair: prefix.hdr/prefix.img\n" " -n2 ==> write a NIFTI-1 file pair: prefix.hdr/prefix.img\n" " -n1 ==> write a NIFTI-1 single file: prefix.nii\n" " -na ==> write a NIFTI-1 ASCII+binary file: prefix.nia\n"); - printf(" -za2 => write an ANALYZE 7.5 file pair:\n" + printf(" -za2 => write an ANALYZE 7.5 file pair:\n" " prefix.hdr.gz/prefix.img.gz\n" " -zn2 => write a NIFTI-1 file pair: prefix.hdr.gz/prefix.img.gz\n" " -zn1 => write a NIFTI-1 single file: prefix.nii.gz\n"); - printf(" The default is '-n1'.\n" + printf(" The default is '-n1'.\n" "\n" " If prefix is not given, then the header info from infile\n" " file is printed to stdout.\n" @@ -42,32 +42,32 @@ int main( int argc , char *argv[] ) " NIFTI-1 specification, but is provided mostly for ease\n" " of visualization (e.g., you can edit a .nia file and\n" " change some header fields, then rewrite it as .nii)\n" - ) ; - printf("\nsizeof(nifti_1_header)=%u\n", + ) ; + printf("\nsizeof(nifti_1_header)=%u\n", (unsigned int)sizeof(nifti_1_header)) ; - exit(0) ; + exit(0) ; } if( argv[1][0] == '-' ){ - argn=1; - if (argv[1][1] == 'z' ) { - usegzip = 1; - argn=2; - } - if( argv[1][argn] == 'a' ){ - outmode = 0 ; - } else if( argv[1][argn] == 'n' ){ - switch( argv[1][argn+1] ){ - case '1': outmode = 1 ; break ; - default: outmode = 2 ; break ; - case 'a': outmode = 3 ; break ; - } - } - iarg++ ; + argn=1; + if (argv[1][1] == 'z' ) { + usegzip = 1; + argn=2; + } + if( argv[1][argn] == 'a' ){ + outmode = 0 ; + } else if( argv[1][argn] == 'n' ){ + switch( argv[1][argn+1] ){ + case '1': outmode = 1 ; break ; + default: outmode = 2 ; break ; + case 'a': outmode = 3 ; break ; + } + } + iarg++ ; } if( iarg >= argc ){ - fprintf(stderr,"** ERROR: no input file on command line!?\n"); exit(1); + fprintf(stderr,"** ERROR: no input file on command line!?\n"); exit(1); } nim = nifti_image_read( argv[iarg++] , 1 ) ; @@ -85,18 +85,18 @@ int main( int argc , char *argv[] ) nim->iname = (char *)calloc(1,ll+8) ; strcpy(nim->iname,tmpstr) ; free(tmpstr); if( nim->nifti_type == 1 ){ - strcat(nim->fname,".nii") ; - strcat(nim->iname,".nii") ; + strcat(nim->fname,".nii") ; + strcat(nim->iname,".nii") ; } else if ( nim->nifti_type == 3 ){ - strcat(nim->fname,".nia") ; - strcat(nim->iname,".nia") ; + strcat(nim->fname,".nia") ; + strcat(nim->iname,".nia") ; } else { - strcat(nim->fname,".hdr") ; - strcat(nim->iname,".img") ; + strcat(nim->fname,".hdr") ; + strcat(nim->iname,".img") ; } if (usegzip) { - strcat(nim->fname,".gz"); - strcat(nim->iname,".gz"); + strcat(nim->fname,".gz"); + strcat(nim->iname,".gz"); } if( nifti_image_write_status( nim ) ) { fprintf(stderr, "** failed to write nifti_image\n"); @@ -106,7 +106,7 @@ int main( int argc , char *argv[] ) if ( ! local_fileexists( nim->fname ) ) { - return EXIT_FAILURE; + return EXIT_FAILURE; } nifti_image_free( nim ) ; return EXIT_SUCCESS; diff --git a/niftilib/nifti1_tool.c b/niftilib/nifti1_tool.c index b439f98..fea3bed 100644 --- a/niftilib/nifti1_tool.c +++ b/niftilib/nifti1_tool.c @@ -65,99 +65,99 @@ /*! module history */ static const char * g_history[] = { - "----------------------------------------------------------------------\n" - "nifti_tool modification history:\n" - "\n", - "0.1 30 December 2004 [rickr]\n" - " (Rick Reynolds of the National Institutes of Health, SSCC/DIRP/NIMH)\n" - " - skeleton version: options read and printed\n" - "\n", - "1.0 07 January 2005 [rickr]\n" - " - initial release version\n" - "\n", - "1.1 14 January 2005 [rickr]\n" - " - changed all non-error/non-debug output from stderr to stdout\n" - " note: creates a mismatch between normal output and debug messages\n" - " - modified act_diff_hdrs and act_diff_nims to do the processing in\n" - " lower-level functions\n", - " - added functions diff_hdrs, diff_hdrs_list, diff_nims, diff_nims_list\n" - " - added function get_field, to return a struct pointer via a fieldname\n" - " - made 'quiet' output more quiet (no description on output)\n" - " - made hdr and nim_fields arrays global, so do not pass in main()\n" - " - return (from main()) after first act_diff() difference\n" - "\n", - "1.2 9 February 2005 [rickr] - minor\n" - " - defined a local NTL_FERR macro (so it does not come from nifti1_io.h)\n" - " - added new set_byte_order parameter to nifti_set_filenames\n" - "\n", - "1.3 23 February 2005 [rickr] - sourceforge.net merge\n" - " - moved to utils directory\n" - " - added simple casts of 3 pointers for -pedantic warnings\n" - " - added a doxygen comment for the file\n" - "\n", - "1.4 02 March 2005 [rickr] - small update\n" - " - no validation in nifti_read_header calls\n" - "\n", - "1.5 05 April 2005 [rickr] - small update\n" - " - refuse mod_hdr for gzipped files (we cannot do partial overwrites)\n" - "\n", - "1.6 08 April 2005 [rickr] - added cbl, cci and dts functionality\n" - " - added -cbl: 'copy brick list' dataset copy functionality\n" - " - added -ccd: 'copy collapsed data' dataset copy functionality\n" - " - added -disp_ts: 'disp time series' data display functionality\n" - " - moved raw data display to disp_raw_data()\n" - "\n", - "1.7 14 April 2005 [rickr] - added data display functionality\n" - " - added -dci: 'display collapsed image' functionality\n" - " - modified -dts to use -dci\n" - " - modified and updated the help in use_full()\n" - " - changed copy_collapsed_dims to copy_collapsed_image, etc.\n", - " - fixed problem in disp_raw_data() for printing NT_DT_CHAR_PTR\n" - " - modified act_disp_ci():\n" - " o was act_disp_ts(), now displays arbitrary collapsed image data\n" - " o added missed debug filename act_disp_ci()\n" - " o can now save free() of data pointer for end of file loop\n", - " - modified disp_raw_data()\n" - " o takes a flag for whether to print newline\n" - " o trailing spaces and zeros are removed from printing floats\n" - " - added clear_float_zeros(), to remove trailing zeros\n" - "\n", - "1.8 19 April 2005 [rickr] - COMMENT extensions\n" - " - added int_list struct, and keep_hist,etypes,command fields to nt_opts\n" - " - added -add_comment_ext action\n" - " - allowed for removal of multiple extensions, including option of ALL\n" - " - added -keep_hist option, to store the command as a COMMENT extension\n", - " (includes fill_cmd_string() and add_int(), is done for all actions)\n" - " - added remove_ext_list(), for removing a list of extensions by indices\n" - " - added -strip_extras action, to strip all exts and descrip fields\n" - "\n", - "1.9 25 Aug 2005 [rickr] - const/string cleanup for warnings\n", - "1.10 18 Nov 2005 [rickr] - added check_hdr and check_nim actions\n", - "1.11 31 Jan 2006 [rickr] - check for new vox_offset in act_mod_hdrs\n", - "1.12 02 Mar 2006 [rickr]\n" - " - in act_cbl(), check for nt = 0 because of niftilib update 1.17\n", - "1.13 24 Apr 2006 [rickr] - act_disp_ci(): remove time series length check\n", - "1.14 04 Jun 2007 [rickr] - free_opts_mem(), to appease valgrind\n", - "1.15 05 Jun 2007 [rickr] - act_check_hdrs: free(nim)->nifti_image_free()\n", - "1.16 12 Jun 2007 [rickr] - allow creation of datasets via MAKE_IM\n", - " - added nt_image_read, nt_read_header and nt_read_bricks\n" - " to wrap nifti read functions, allowing creation of new datasets\n" - " - added -make_im, -new_dim, -new_datatype and -copy_im\n" - "1.17 13 Jun 2007 [rickr] - added help for -copy_im, enumerate examples\n", - "1.18 23 Jun 2007 [rickr] - main returns 0 on -help, -hist, -ver\n" - "1.19 28 Nov 2007 [rickr] - added -help_datatypes\n", - "1.20 13 Jun 2008 [rickr]\n" - " - added -with_zlib\n" - " - added ability to create extension from text file (for J. Gunter)\n", - "1.21 03 Aug 2008 [rickr] - ANALYZE 7.5 support\n" - " - added -help_ana, -disp_ana,\n" - " -swap_as_analyze, -swap_as_nifti, -swap_as_old\n" - "1.22 08 Oct 2008 [rickr] - allow cbl with indices in 0..nt*nu*nv*nw-1\n" - "1.23 06 Jul 2010 [rickr]\n", - " - in nt_read_bricks, bsize computation should allow for large integers\n" - "1.24 26 Sep 2012 [rickr]\n", - " - changed ana originator from char to short\n" - "----------------------------------------------------------------------\n" + "----------------------------------------------------------------------\n" + "nifti_tool modification history:\n" + "\n", + "0.1 30 December 2004 [rickr]\n" + " (Rick Reynolds of the National Institutes of Health, SSCC/DIRP/NIMH)\n" + " - skeleton version: options read and printed\n" + "\n", + "1.0 07 January 2005 [rickr]\n" + " - initial release version\n" + "\n", + "1.1 14 January 2005 [rickr]\n" + " - changed all non-error/non-debug output from stderr to stdout\n" + " note: creates a mismatch between normal output and debug messages\n" + " - modified act_diff_hdrs and act_diff_nims to do the processing in\n" + " lower-level functions\n", + " - added functions diff_hdrs, diff_hdrs_list, diff_nims, diff_nims_list\n" + " - added function get_field, to return a struct pointer via a fieldname\n" + " - made 'quiet' output more quiet (no description on output)\n" + " - made hdr and nim_fields arrays global, so do not pass in main()\n" + " - return (from main()) after first act_diff() difference\n" + "\n", + "1.2 9 February 2005 [rickr] - minor\n" + " - defined a local NTL_FERR macro (so it does not come from nifti1_io.h)\n" + " - added new set_byte_order parameter to nifti_set_filenames\n" + "\n", + "1.3 23 February 2005 [rickr] - sourceforge.net merge\n" + " - moved to utils directory\n" + " - added simple casts of 3 pointers for -pedantic warnings\n" + " - added a doxygen comment for the file\n" + "\n", + "1.4 02 March 2005 [rickr] - small update\n" + " - no validation in nifti_read_header calls\n" + "\n", + "1.5 05 April 2005 [rickr] - small update\n" + " - refuse mod_hdr for gzipped files (we cannot do partial overwrites)\n" + "\n", + "1.6 08 April 2005 [rickr] - added cbl, cci and dts functionality\n" + " - added -cbl: 'copy brick list' dataset copy functionality\n" + " - added -ccd: 'copy collapsed data' dataset copy functionality\n" + " - added -disp_ts: 'disp time series' data display functionality\n" + " - moved raw data display to disp_raw_data()\n" + "\n", + "1.7 14 April 2005 [rickr] - added data display functionality\n" + " - added -dci: 'display collapsed image' functionality\n" + " - modified -dts to use -dci\n" + " - modified and updated the help in use_full()\n" + " - changed copy_collapsed_dims to copy_collapsed_image, etc.\n", + " - fixed problem in disp_raw_data() for printing NT_DT_CHAR_PTR\n" + " - modified act_disp_ci():\n" + " o was act_disp_ts(), now displays arbitrary collapsed image data\n" + " o added missed debug filename act_disp_ci()\n" + " o can now save free() of data pointer for end of file loop\n", + " - modified disp_raw_data()\n" + " o takes a flag for whether to print newline\n" + " o trailing spaces and zeros are removed from printing floats\n" + " - added clear_float_zeros(), to remove trailing zeros\n" + "\n", + "1.8 19 April 2005 [rickr] - COMMENT extensions\n" + " - added int_list struct, and keep_hist,etypes,command fields to nt_opts\n" + " - added -add_comment_ext action\n" + " - allowed for removal of multiple extensions, including option of ALL\n" + " - added -keep_hist option, to store the command as a COMMENT extension\n", + " (includes fill_cmd_string() and add_int(), is done for all actions)\n" + " - added remove_ext_list(), for removing a list of extensions by indices\n" + " - added -strip_extras action, to strip all exts and descrip fields\n" + "\n", + "1.9 25 Aug 2005 [rickr] - const/string cleanup for warnings\n", + "1.10 18 Nov 2005 [rickr] - added check_hdr and check_nim actions\n", + "1.11 31 Jan 2006 [rickr] - check for new vox_offset in act_mod_hdrs\n", + "1.12 02 Mar 2006 [rickr]\n" + " - in act_cbl(), check for nt = 0 because of niftilib update 1.17\n", + "1.13 24 Apr 2006 [rickr] - act_disp_ci(): remove time series length check\n", + "1.14 04 Jun 2007 [rickr] - free_opts_mem(), to appease valgrind\n", + "1.15 05 Jun 2007 [rickr] - act_check_hdrs: free(nim)->nifti_image_free()\n", + "1.16 12 Jun 2007 [rickr] - allow creation of datasets via MAKE_IM\n", + " - added nt_image_read, nt_read_header and nt_read_bricks\n" + " to wrap nifti read functions, allowing creation of new datasets\n" + " - added -make_im, -new_dim, -new_datatype and -copy_im\n" + "1.17 13 Jun 2007 [rickr] - added help for -copy_im, enumerate examples\n", + "1.18 23 Jun 2007 [rickr] - main returns 0 on -help, -hist, -ver\n" + "1.19 28 Nov 2007 [rickr] - added -help_datatypes\n", + "1.20 13 Jun 2008 [rickr]\n" + " - added -with_zlib\n" + " - added ability to create extension from text file (for J. Gunter)\n", + "1.21 03 Aug 2008 [rickr] - ANALYZE 7.5 support\n" + " - added -help_ana, -disp_ana,\n" + " -swap_as_analyze, -swap_as_nifti, -swap_as_old\n" + "1.22 08 Oct 2008 [rickr] - allow cbl with indices in 0..nt*nu*nv*nw-1\n" + "1.23 06 Jul 2010 [rickr]\n", + " - in nt_read_bricks, bsize computation should allow for large integers\n" + "1.24 26 Sep 2012 [rickr]\n", + " - changed ana originator from char to short\n" + "----------------------------------------------------------------------\n" }; static char g_version[] = "version 1.24 (September 26, 2012)"; static int g_debug = 1; @@ -634,11 +634,11 @@ int verify_opts( nt_opts * opts, char * prog ) /* -diff_... : require nfiles == 2 */ if( opts->diff_hdr || opts->diff_nim ) { - if( opts->infiles.len != 2 ) - { - fprintf(stderr,"** '-diff_XXX' options require exactly 2 inputs files\n"); - return 1; - } + if( opts->infiles.len != 2 ) + { + fprintf(stderr,"** '-diff_XXX' options require exactly 2 inputs files\n"); + return 1; + } } /* if we are making changes, but not overwriting... */ else if( (opts->elist.len > 0 || opts->mod_hdr || opts->mod_nim || @@ -808,7 +808,7 @@ int usage(const char * prog, int level) { len = sizeof(g_history)/sizeof(char *); for( c = 0; c < len; c++) - fputs(g_history[c], stdout); + fputs(g_history[c], stdout); } else if( level == USE_FIELD_HDR ) { @@ -1769,7 +1769,7 @@ int disp_nt_opts( const char *mesg, nt_opts * opts) " cbl, cci = %d, %d\n" " dts, dci_lines = %d, %d\n" " make_im = %d\n", - (void *)opts, + (void *)opts, opts->check_hdr, opts->check_nim, opts->diff_hdr, opts->diff_nim, opts->disp_hdr, opts->disp_nim, opts->disp_ana, opts->disp_exts, opts->add_exts, opts->rm_exts, @@ -1793,7 +1793,7 @@ int disp_nt_opts( const char *mesg, nt_opts * opts) fprintf(stderr," elist (length %d) :\n", opts->elist.len); for( c = 0; c < opts->elist.len; c++ ) - fprintf(stderr," %d : %s\n", c, opts->elist.list[c]); + fprintf(stderr," %d : %s\n", c, opts->elist.list[c]); fprintf(stderr," etypes (length %d) : ", opts->etypes.len); disp_raw_data(opts->etypes.list, DT_INT32, opts->etypes.len, ' ', 0); @@ -1801,15 +1801,15 @@ int disp_nt_opts( const char *mesg, nt_opts * opts) fprintf(stderr," flist (length %d) :\n", opts->flist.len); for( c = 0; c < opts->flist.len; c++ ) - fprintf(stderr," %d : %s\n", c, opts->flist.list[c]); + fprintf(stderr," %d : %s\n", c, opts->flist.list[c]); fprintf(stderr," vlist (length %d) :\n", opts->vlist.len); for( c = 0; c < opts->vlist.len; c++ ) - fprintf(stderr," %d : %s\n", c, opts->vlist.list[c]); + fprintf(stderr," %d : %s\n", c, opts->vlist.list[c]); fprintf(stderr," infiles (length %d) :\n", opts->infiles.len); for( c = 0; c < opts->infiles.len; c++ ) - fprintf(stderr," %d : %s\n", c, opts->infiles.list[c]); + fprintf(stderr," %d : %s\n", c, opts->infiles.list[c]); fprintf(stderr," command len : %d\n",(int)strlen(opts->command)); @@ -2128,7 +2128,7 @@ int remove_ext_list( nifti_image * nim, const char ** elist, int len ) if( extval == -1 ) { if( g_debug > 1 ) - fprintf(stderr,"+d removing ALL (%d) extensions from '%s'\n", + fprintf(stderr,"+d removing ALL (%d) extensions from '%s'\n", nim->num_ext, nim->fname ); nifti_free_extensions(nim); return 0; @@ -2320,37 +2320,37 @@ int act_check_hdrs( nt_opts * opts ) if( opts->check_hdr ) { - if( g_debug > 1 ) - fprintf(stdout,"\nchecking nifti_1_header for file '%s'\n", + if( g_debug > 1 ) + fprintf(stdout,"\nchecking nifti_1_header for file '%s'\n", opts->infiles.list[filenum]); - rv = nifti_hdr_looks_good(nhdr); + rv = nifti_hdr_looks_good(nhdr); - if( rv && g_debug > 0 ) /* if quiet, no GOOD response */ - printf("header IS GOOD for file %s\n",opts->infiles.list[filenum]); - else if( ! rv ) - printf("header FAILURE for file %s\n",opts->infiles.list[filenum]); + if( rv && g_debug > 0 ) /* if quiet, no GOOD response */ + printf("header IS GOOD for file %s\n",opts->infiles.list[filenum]); + else if( ! rv ) + printf("header FAILURE for file %s\n",opts->infiles.list[filenum]); } if( opts->check_nim ) { - nim = nifti_convert_nhdr2nim(*nhdr, opts->infiles.list[filenum]); - if( !nim ) continue; /* errors are printed from library */ + nim = nifti_convert_nhdr2nim(*nhdr, opts->infiles.list[filenum]); + if( !nim ) continue; /* errors are printed from library */ - if( g_debug > 1 ) - fprintf(stdout,"\nchecking nifti_image for file '%s'\n", + if( g_debug > 1 ) + fprintf(stdout,"\nchecking nifti_image for file '%s'\n", opts->infiles.list[filenum]); - rv = nifti_nim_is_valid(nim, 1); /* complain about errors */ + rv = nifti_nim_is_valid(nim, 1); /* complain about errors */ - if( rv && g_debug > 0 ) /* if quiet, no GOOD response */ - printf("nifti_image IS GOOD for file %s\n", + if( rv && g_debug > 0 ) /* if quiet, no GOOD response */ + printf("nifti_image IS GOOD for file %s\n", opts->infiles.list[filenum]); - else if( ! rv ) - printf("nifti_image FAILURE for file %s\n", + else if( ! rv ) + printf("nifti_image FAILURE for file %s\n", opts->infiles.list[filenum]); - nifti_image_free(nim); + nifti_image_free(nim); } free(nhdr); @@ -2607,7 +2607,7 @@ int act_mod_hdrs( nt_opts * opts ) } if( opts->keep_hist && nifti_add_extension(nim, opts->command, (int)strlen(opts->command), NIFTI_ECODE_COMMENT) ) - fprintf(stderr,"** failed to add command to image as extension\n"); + fprintf(stderr,"** failed to add command to image as extension\n"); if( nifti_set_filenames(nim, opts->prefix, 1, 1) ) { NTL_FERR(func,"failed to set prefix for new file: ",opts->prefix); @@ -2728,7 +2728,7 @@ int act_swap_hdrs( nt_opts * opts ) } if( opts->keep_hist && nifti_add_extension(nim, opts->command, (int)strlen(opts->command), NIFTI_ECODE_COMMENT) ) - fprintf(stderr,"** failed to add command to image as extension\n"); + fprintf(stderr,"** failed to add command to image as extension\n"); if( nifti_set_filenames(nim, opts->prefix, 1, 1) ) { NTL_FERR(func,"failed to set prefix for new file: ",opts->prefix); @@ -2912,16 +2912,16 @@ int modify_field(void * basep, field_s * field, const char * data) switch( field->type ) { - case DT_UNKNOWN: - case NT_DT_POINTER: - case NT_DT_CHAR_PTR: - case NT_DT_EXT_PTR: - default: - fprintf(stderr,"** refusing to modify a pointer field, '%s'\n", + case DT_UNKNOWN: + case NT_DT_POINTER: + case NT_DT_CHAR_PTR: + case NT_DT_EXT_PTR: + default: + fprintf(stderr,"** refusing to modify a pointer field, '%s'\n", field->name); - return 1; + return 1; - case DT_INT8: + case DT_INT8: { max = 127; for( fc = 0; fc < field->len; fc++ ) @@ -2949,7 +2949,7 @@ int modify_field(void * basep, field_s * field, const char * data) } break; - case DT_INT16: + case DT_INT16: { max = 32767; for( fc = 0; fc < field->len; fc++ ) @@ -2977,7 +2977,7 @@ int modify_field(void * basep, field_s * field, const char * data) } break; - case DT_INT32: + case DT_INT32: { for( fc = 0; fc < field->len; fc++ ) { @@ -2996,7 +2996,7 @@ int modify_field(void * basep, field_s * field, const char * data) } break; - case DT_FLOAT32: + case DT_FLOAT32: { for( fc = 0; fc < field->len; fc++ ) { @@ -3016,7 +3016,7 @@ int modify_field(void * basep, field_s * field, const char * data) } break; - case NT_DT_STRING: + case NT_DT_STRING: { char * dest = (char *)basep + field->offset; nchars = dataLength; @@ -3196,7 +3196,7 @@ int fill_nim_field_array( field_s * nim_fields ) } if( g_debug > 3 ) /* failure here is not an error condition */ - check_total_size("nifti_image test: ", nim_fields, + check_total_size("nifti_image test: ", nim_fields, NT_NIM_NUM_FIELDS, sizeof(nifti_image)); if( g_debug > 3 ) @@ -3321,10 +3321,12 @@ int check_total_size( const char *mesg, field_s * fields, int nfields, int tot_s if( g_debug > 1 || (g_debug > 0 && bad_offs > 0) ){ fputs(mesg, stderr); c = 0; if( bad_offs > 0 ){ - fprintf(stderr,"** found %d bad offsets\n", bad_offs); c++; } + fprintf(stderr,"** found %d bad offsets\n", bad_offs); c++; + } if( total != tot_size ){ fprintf(stderr,"** computed total %d not equal to struct size %d\n", - total, tot_size); c++; } + total, tot_size); c++; + } if( c == 0 ) fputs("... okay\n", stderr); } @@ -3801,15 +3803,15 @@ int act_disp_ci( nt_opts * opts ) case DT_INT8: case DT_INT16: case DT_INT32: case DT_UINT8: case DT_UINT16: case DT_UINT32: case DT_FLOAT32: case DT_FLOAT64: - if( g_debug > 1 ) - fprintf(stderr,"-d datatype %d of size %d\n", + if( g_debug > 1 ) + fprintf(stderr,"-d datatype %d of size %d\n", nim->datatype, nim->nbyper); - break; + break; default: - fprintf(stderr,"** dataset '%s' has unknown type %d\n", + fprintf(stderr,"** dataset '%s' has unknown type %d\n", nim->fname, nim->datatype); - err++; - break; + err++; + break; } if( err ) { nifti_image_free(nim); continue; } @@ -3859,64 +3861,64 @@ int disp_raw_data( void * data, int type, int nvals, char space, int newline ) switch( type ) { case DT_INT8: - printf("%d", *(char *)dp); - break; + printf("%d", *(char *)dp); + break; case DT_INT16: { - short temp; - memcpy(&temp, dp, sizeof(temp)); - printf("%d", temp); - break; + short temp; + memcpy(&temp, dp, sizeof(temp)); + printf("%d", temp); + break; } case DT_INT32: { - int temp; - memcpy(&temp, dp, sizeof(temp)); - printf("%d", temp); - break; + int temp; + memcpy(&temp, dp, sizeof(temp)); + printf("%d", temp); + break; } case DT_UINT8: { - unsigned char temp; - memcpy(&temp, dp, sizeof(temp)); - printf("%u", temp); - break; + unsigned char temp; + memcpy(&temp, dp, sizeof(temp)); + printf("%u", temp); + break; } case DT_UINT16: { - unsigned short temp; - memcpy(&temp, dp, sizeof(temp)); - printf("%u", temp); - break; + unsigned short temp; + memcpy(&temp, dp, sizeof(temp)); + printf("%u", temp); + break; } case DT_UINT32: { - unsigned int temp; - memcpy(&temp, dp, sizeof(temp)); - printf("%u", temp); - break; + unsigned int temp; + memcpy(&temp, dp, sizeof(temp)); + printf("%u", temp); + break; } case DT_FLOAT32: { - float temp; - memcpy(&temp, dp, sizeof(temp)); - snprintf(fbuf, NT_LOC_MAX_FLOAT_BUF,"%f", temp); - clear_float_zeros(fbuf); - printf("%s", fbuf); - break; + float temp; + memcpy(&temp, dp, sizeof(temp)); + snprintf(fbuf, NT_LOC_MAX_FLOAT_BUF,"%f", temp); + clear_float_zeros(fbuf); + printf("%s", fbuf); + break; } case DT_FLOAT64: { - double temp; - memcpy(&temp, dp, sizeof(temp)); - snprintf(fbuf, NT_LOC_MAX_FLOAT_BUF,"%f", temp); - clear_float_zeros(fbuf); - printf("%s", fbuf); - break; + double temp; + memcpy(&temp, dp, sizeof(temp)); + snprintf(fbuf, NT_LOC_MAX_FLOAT_BUF,"%f", temp); + clear_float_zeros(fbuf); + printf("%s", fbuf); + break; } default: - fprintf(stderr,"** disp_raw_data: unknown type %d\n", type); - return 1; + fprintf(stderr,"** disp_raw_data: unknown type %d\n", type); + return 1; } if( c < nvals - 1 ) fputc(space,stdout); } @@ -3943,7 +3945,7 @@ int clear_float_zeros( char * str ) /* never clear what is just to the right of '.' */ for( valp = dp+len-1; (valp > dp+1) && (*valp==' ' || *valp=='0'); valp-- ) - *valp = '\0'; /* clear, so we don't worry about break conditions */ + *valp = '\0'; /* clear, so we don't worry about break conditions */ if( valp < dp + len - 1 ) return 1; return 0; @@ -3957,7 +3959,7 @@ static int num_volumes(nifti_image * nim) if( nim->dim[0] < 1 ) return 0; for( ind = 4; ind <= nim->dim[0]; ind++ ) - nvols *= nim->dim[ind]; + nvols *= nim->dim[ind]; return nvols; } @@ -4119,15 +4121,15 @@ int act_cci( nt_opts * opts ) *----------------------------------------------------------------------*/ static int free_opts_mem( nt_opts * nopt ) { - if( !nopt ) return 1; + if( !nopt ) return 1; - free(nopt->elist.list); - free(nopt->etypes.list); - free(nopt->flist.list); - free(nopt->vlist.list); - free(nopt->infiles.list); + free(nopt->elist.list); + free(nopt->etypes.list); + free(nopt->flist.list); + free(nopt->vlist.list); + free(nopt->infiles.list); - return 0; + return 0; } @@ -4139,32 +4141,32 @@ static int free_opts_mem( nt_opts * nopt ) *----------------------------------------------------------------------*/ nifti_image * nt_image_read( nt_opts * opts, const char * fname, int doread ) { - if( !opts || !fname ) { - fprintf(stderr,"** nt_image_read: bad params (%p,%p)\n", + if( !opts || !fname ) { + fprintf(stderr,"** nt_image_read: bad params (%p,%p)\n", (void *)opts, (const void *)fname); - return NULL; - } + return NULL; + } /* if the user does not want an empty image, do normal image_read */ - if( strncmp(fname,NT_MAKE_IM_NAME,strlen(NT_MAKE_IM_NAME)) != 0 ) { - if(g_debug > 1) - fprintf(stderr,"-d calling nifti_image_read(%s,%d)\n",fname,doread); - return nifti_image_read(fname, doread); - } + if( strncmp(fname,NT_MAKE_IM_NAME,strlen(NT_MAKE_IM_NAME)) != 0 ) { + if(g_debug > 1) + fprintf(stderr,"-d calling nifti_image_read(%s,%d)\n",fname,doread); + return nifti_image_read(fname, doread); + } /* so generate an empty image */ - if(g_debug > 1) { - fprintf(stderr,"+d NT_IR: generating EMPTY IMAGE from %s...\n",fname); - if(g_debug > 2) { - printf(" new_dim[8] = "); - disp_raw_data(opts->new_dim, DT_INT32, 8, ' ', 1); - printf(" new_datatype = %d\n", opts->new_datatype); - fflush(stdout); - } - } + if(g_debug > 1) { + fprintf(stderr,"+d NT_IR: generating EMPTY IMAGE from %s...\n",fname); + if(g_debug > 2) { + printf(" new_dim[8] = "); + disp_raw_data(opts->new_dim, DT_INT32, 8, ' ', 1); + printf(" new_datatype = %d\n", opts->new_datatype); + fflush(stdout); + } + } /* create a new nifti_image, with data depending on doread */ - return nifti_make_new_nim(opts->new_dim, opts->new_datatype, doread); + return nifti_make_new_nim(opts->new_dim, opts->new_datatype, doread); } @@ -4178,32 +4180,32 @@ nifti_1_header * nt_read_header(nt_opts * opts, const char * fname, int * swappe int check) { /* swapped is not necessary */ - if( !opts || !fname ) { - fprintf(stderr,"** nt_read_header: bad params (%p,%p)\n", + if( !opts || !fname ) { + fprintf(stderr,"** nt_read_header: bad params (%p,%p)\n", (void *)opts,(const void *)fname); - return NULL; - } + return NULL; + } /* if the user does not want an empty image, do normal image_read */ - if( strncmp(fname,NT_MAKE_IM_NAME,strlen(NT_MAKE_IM_NAME)) != 0 ) { - if(g_debug > 1) - fprintf(stderr,"-d calling nifti_read_header(%s,...)\n", fname); - return nifti_read_header(fname, swapped, check); - } + if( strncmp(fname,NT_MAKE_IM_NAME,strlen(NT_MAKE_IM_NAME)) != 0 ) { + if(g_debug > 1) + fprintf(stderr,"-d calling nifti_read_header(%s,...)\n", fname); + return nifti_read_header(fname, swapped, check); + } /* so generate an empty image */ - if(g_debug > 1) { - fprintf(stderr,"+d NT_RH: generating EMPTY IMAGE from %s...\n",fname); - if(g_debug > 2) { - printf(" new_dim[8] = "); - disp_raw_data(opts->new_dim, DT_INT32, 8, ' ', 1); - printf(" new_datatype = %d\n", opts->new_datatype); - fflush(stdout); - } - } + if(g_debug > 1) { + fprintf(stderr,"+d NT_RH: generating EMPTY IMAGE from %s...\n",fname); + if(g_debug > 2) { + printf(" new_dim[8] = "); + disp_raw_data(opts->new_dim, DT_INT32, 8, ' ', 1); + printf(" new_datatype = %d\n", opts->new_datatype); + fflush(stdout); + } + } /* return creation of new header */ - return nifti_make_new_header(opts->new_dim, opts->new_datatype); + return nifti_make_new_header(opts->new_dim, opts->new_datatype); } @@ -4217,68 +4219,68 @@ nifti_1_header * nt_read_header(nt_opts * opts, const char * fname, int * swappe nifti_image * nt_read_bricks(nt_opts * opts, const char * fname, int len, int * list, nifti_brick_list * NBL) { - nifti_image * nim; - int c; + nifti_image * nim; + int c; /* swapped is not necessary */ - if( !opts || !fname || !NBL ) { - fprintf(stderr,"** nt_read_bricks: bad params (%p,%p,%p)\n", + if( !opts || !fname || !NBL ) { + fprintf(stderr,"** nt_read_bricks: bad params (%p,%p,%p)\n", (void *)opts, (const void *)fname, (void *)NBL); - return NULL; - } + return NULL; + } /* if the user does not want an empty image, do normal read_bricks */ - if( strncmp(fname,NT_MAKE_IM_NAME,strlen(NT_MAKE_IM_NAME)) != 0 ) { - if(g_debug > 1) - fprintf(stderr,"-d calling nifti_image_read_bricks(%s,...)\n",fname); - return nifti_image_read_bricks(fname, len, list, NBL); - } + if( strncmp(fname,NT_MAKE_IM_NAME,strlen(NT_MAKE_IM_NAME)) != 0 ) { + if(g_debug > 1) + fprintf(stderr,"-d calling nifti_image_read_bricks(%s,...)\n",fname); + return nifti_image_read_bricks(fname, len, list, NBL); + } /* so generate an empty image */ - if(g_debug > 1) { - fprintf(stderr,"+d NT_RB: generating EMPTY IMAGE from %s...\n",fname); - if(g_debug > 2) { - printf(" new_dim[8] = "); - disp_raw_data(opts->new_dim, DT_INT32, 8, ' ', 1); - printf(" new_datatype = %d\n", opts->new_datatype); - if( list && len > 0 ) { - printf(" brick_list[%d] = ", len); - disp_raw_data(list, DT_INT32, len, ' ', 1); - } - fflush(stdout); /* disp_raw_data uses stdout */ - } - } + if(g_debug > 1) { + fprintf(stderr,"+d NT_RB: generating EMPTY IMAGE from %s...\n",fname); + if(g_debug > 2) { + printf(" new_dim[8] = "); + disp_raw_data(opts->new_dim, DT_INT32, 8, ' ', 1); + printf(" new_datatype = %d\n", opts->new_datatype); + if( list && len > 0 ) { + printf(" brick_list[%d] = ", len); + disp_raw_data(list, DT_INT32, len, ' ', 1); + } + fflush(stdout); /* disp_raw_data uses stdout */ + } + } /* first, get nim struct without data */ - nim = nifti_make_new_nim(opts->new_dim, opts->new_datatype, 0); - if( !nim ) { - fprintf(stderr,"** nt_read_bricks, nifti_make_new_nim failure\n"); - return NULL; - } + nim = nifti_make_new_nim(opts->new_dim, opts->new_datatype, 0); + if( !nim ) { + fprintf(stderr,"** nt_read_bricks, nifti_make_new_nim failure\n"); + return NULL; + } /* now populate NBL (can be based only on len and nim) */ - NBL->nbricks = len; - NBL->bsize = (size_t)nim->nbyper * nim->nx * nim->ny * nim->nz; - NBL->bricks = (void **)calloc(NBL->nbricks, sizeof(void *)); - if( !NBL->bricks ){ - fprintf(stderr,"** NRB: failed to alloc %d pointers\n",NBL->nbricks); - nifti_image_free(nim); - return NULL; - } - - if(g_debug > 1) - fprintf(stderr,"+d NRB, allocating %d bricks of %u bytes...\n", + NBL->nbricks = len; + NBL->bsize = (size_t)nim->nbyper * nim->nx * nim->ny * nim->nz; + NBL->bricks = (void **)calloc(NBL->nbricks, sizeof(void *)); + if( !NBL->bricks ){ + fprintf(stderr,"** NRB: failed to alloc %d pointers\n",NBL->nbricks); + nifti_image_free(nim); + return NULL; + } + + if(g_debug > 1) + fprintf(stderr,"+d NRB, allocating %d bricks of %u bytes...\n", NBL->nbricks, (unsigned)NBL->bsize); /* now allocate the data pointers */ - for( c = 0; c < len; c++ ) { - NBL->bricks[c] = calloc(1, NBL->bsize); - if( !NBL->bricks[c] ){ - fprintf(stderr,"** NRB: failed to alloc brick %d of %u bytes\n", + for( c = 0; c < len; c++ ) { + NBL->bricks[c] = calloc(1, NBL->bsize); + if( !NBL->bricks[c] ){ + fprintf(stderr,"** NRB: failed to alloc brick %d of %u bytes\n", c, (unsigned)NBL->bsize); - nifti_free_NBL(NBL); nifti_image_free(nim); return NULL; - } - } + nifti_free_NBL(NBL); nifti_image_free(nim); return NULL; + } + } - return nim; + return nim; } diff --git a/niftilib/nifti1_tool.h b/niftilib/nifti1_tool.h index 035db3c..e5c4e1b 100644 --- a/niftilib/nifti1_tool.h +++ b/niftilib/nifti1_tool.h @@ -3,17 +3,17 @@ #define NT_CMD_LEN 2048 -typedef struct{ +typedef struct { int len; const char ** list; } str_list; -typedef struct{ +typedef struct { int len; int * list; } int_list; -typedef struct{ +typedef struct { /* action options (flags) */ int check_hdr, check_nim; int diff_hdr, diff_nim; diff --git a/niftilib/nifti_tester001.c b/niftilib/nifti_tester001.c index 6a62895..042ebb7 100644 --- a/niftilib/nifti_tester001.c +++ b/niftilib/nifti_tester001.c @@ -4,462 +4,462 @@ #include #include enum NIFTITEST_BOOL { - NIFTITEST_TRUE=1, - NIFTITEST_FALSE=0 + NIFTITEST_TRUE=1, + NIFTITEST_FALSE=0 }; static void PrintTest_eng(const int line,const char * message,const int FailureOccured, const enum NIFTITEST_BOOL isFatal,int *ErrorAccum,bool verbose) { - if(verbose || FailureOccured==NIFTITEST_TRUE) - { - char const * const PREFIX= (FailureOccured)?"==========ERROR":"..........SUCCESS"; - char const * const ISFATALPREFIX= (isFatal && FailureOccured)?" FATAL":""; - printf("%s%s (LINE %d): %s\n",PREFIX,ISFATALPREFIX,line,message); - fflush(stdout); - *ErrorAccum+=FailureOccured; - if(isFatal==NIFTITEST_TRUE && FailureOccured==NIFTITEST_TRUE) + if(verbose || FailureOccured==NIFTITEST_TRUE) + { + char const * const PREFIX= (FailureOccured)?"==========ERROR":"..........SUCCESS"; + char const * const ISFATALPREFIX= (isFatal && FailureOccured)?" FATAL":""; + printf("%s%s (LINE %d): %s\n",PREFIX,ISFATALPREFIX,line,message); + fflush(stdout); + *ErrorAccum+=FailureOccured; + if(isFatal==NIFTITEST_TRUE && FailureOccured==NIFTITEST_TRUE) { - printf("\n\nTOTAL ERRORS=%d\n",*ErrorAccum); - exit( *ErrorAccum); + printf("\n\nTOTAL ERRORS=%d\n",*ErrorAccum); + exit( *ErrorAccum); } - } - } + } +} /* Pass true instead of false for a more verbose output */ #define PrintTest(message,failure,isfailure,errorcount) \ PrintTest_eng(__LINE__,message,failure,isfailure,errorcount,false) static nifti_image * generate_reference_image( const char * write_image_filename , int * const Errors) { - nifti_1_header reference_header; - memset(&reference_header,0,sizeof(reference_header)); - reference_header.sizeof_hdr=sizeof(reference_header); - reference_header.regular='r'; - reference_header.extents=16384; - - reference_header.dim[0]=5; - reference_header.dim[1]=23; - reference_header.dim[2]=17; - reference_header.dim[3]=11; - reference_header.dim[4]=7; - reference_header.dim[5]=3; - reference_header.dim[6]=1; //This MUST be 1 anything else is invalid due to code that uses heuristics to fix other possible problems; - reference_header.dim[7]=1; //This MUST be 1 anything else is invalid due to code that uses heuristics to fix other possible problems; - reference_header.intent_p1=10101010.101F; - reference_header.intent_p2=987654321.0F; - reference_header.intent_p3=-1234.0F; - reference_header.intent_code=NIFTI_INTENT_ESTIMATE; - reference_header.datatype=DT_INT32; - reference_header.pixdim[0]=-1.0F; /* this is really qfac */ - reference_header.pixdim[1]=0.25F; - reference_header.pixdim[2]=0.5F; - reference_header.pixdim[3]=1.0F; - reference_header.pixdim[4]=2.0F; - reference_header.pixdim[5]=4.0F; - reference_header.pixdim[6]=-2.0e10F; - reference_header.pixdim[7]=-2.0e10F; - reference_header.vox_offset=0; - reference_header.scl_slope=0.25; - reference_header.scl_inter=128; - reference_header.qform_code=NIFTI_XFORM_SCANNER_ANAT; - reference_header.quatern_b=-0.5F; - reference_header.quatern_c= 0.5F; - reference_header.quatern_d=-0.5F; - reference_header.qoffset_x=reference_header.dim[1]/2.0F; - reference_header.qoffset_y=reference_header.dim[2]/2.0F; - reference_header.qoffset_z=reference_header.dim[3]/2.0F; - reference_header.sform_code=NIFTI_XFORM_SCANNER_ANAT; - reference_header.srow_x[0]=0.5; - reference_header.srow_x[1]=0.0; - reference_header.srow_x[2]=0.0; - reference_header.srow_x[3]=0.0; - reference_header.srow_y[0]=0.0; - reference_header.srow_y[1]=1.0; - reference_header.srow_y[2]=0.0; - reference_header.srow_y[3]=0.0; - reference_header.srow_z[0]=0.0; - reference_header.srow_z[1]=0.0; - reference_header.srow_z[2]=2.0; - reference_header.srow_z[3]=0.0; - reference_header.magic[0]='n'; - reference_header.magic[1]='+'; - reference_header.magic[2]='1'; - reference_header.magic[3]='\0'; + nifti_1_header reference_header; + memset(&reference_header,0,sizeof(reference_header)); + reference_header.sizeof_hdr=sizeof(reference_header); + reference_header.regular='r'; + reference_header.extents=16384; + + reference_header.dim[0]=5; + reference_header.dim[1]=23; + reference_header.dim[2]=17; + reference_header.dim[3]=11; + reference_header.dim[4]=7; + reference_header.dim[5]=3; + reference_header.dim[6]=1; //This MUST be 1 anything else is invalid due to code that uses heuristics to fix other possible problems; + reference_header.dim[7]=1; //This MUST be 1 anything else is invalid due to code that uses heuristics to fix other possible problems; + reference_header.intent_p1=10101010.101F; + reference_header.intent_p2=987654321.0F; + reference_header.intent_p3=-1234.0F; + reference_header.intent_code=NIFTI_INTENT_ESTIMATE; + reference_header.datatype=DT_INT32; + reference_header.pixdim[0]=-1.0F; /* this is really qfac */ + reference_header.pixdim[1]=0.25F; + reference_header.pixdim[2]=0.5F; + reference_header.pixdim[3]=1.0F; + reference_header.pixdim[4]=2.0F; + reference_header.pixdim[5]=4.0F; + reference_header.pixdim[6]=-2.0e10F; + reference_header.pixdim[7]=-2.0e10F; + reference_header.vox_offset=0; + reference_header.scl_slope=0.25; + reference_header.scl_inter=128; + reference_header.qform_code=NIFTI_XFORM_SCANNER_ANAT; + reference_header.quatern_b=-0.5F; + reference_header.quatern_c= 0.5F; + reference_header.quatern_d=-0.5F; + reference_header.qoffset_x=reference_header.dim[1]/2.0F; + reference_header.qoffset_y=reference_header.dim[2]/2.0F; + reference_header.qoffset_z=reference_header.dim[3]/2.0F; + reference_header.sform_code=NIFTI_XFORM_SCANNER_ANAT; + reference_header.srow_x[0]=0.5; + reference_header.srow_x[1]=0.0; + reference_header.srow_x[2]=0.0; + reference_header.srow_x[3]=0.0; + reference_header.srow_y[0]=0.0; + reference_header.srow_y[1]=1.0; + reference_header.srow_y[2]=0.0; + reference_header.srow_y[3]=0.0; + reference_header.srow_z[0]=0.0; + reference_header.srow_z[1]=0.0; + reference_header.srow_z[2]=2.0; + reference_header.srow_z[3]=0.0; + reference_header.magic[0]='n'; + reference_header.magic[1]='+'; + reference_header.magic[2]='1'; + reference_header.magic[3]='\0'; /* String is purposfully too long */ - strncpy(reference_header.intent_name,"PHANTOM_DATA to be used for regression testing the nifti reader/writer",sizeof(reference_header.intent_name)); - reference_header.intent_name[sizeof(reference_header.intent_name) - 1] = 0; - strncpy(reference_header.descrip,"This is a very long dialog here to use up more than 80 characters of space to test to see if the code is robust enough to deal appropriately with very long and obnoxious lines.",sizeof(reference_header.descrip)); - reference_header.descrip[sizeof(reference_header.descrip) - 1] = 0; - - { - int nbyper; - int swapsize; - nifti_datatype_sizes(reference_header.datatype ,&nbyper,&swapsize); - reference_header.bitpix=nbyper*8; - } - - nifti_image * reference_image=nifti_convert_nhdr2nim(reference_header,write_image_filename); - { - const unsigned int NumVoxels=reference_image->nx*reference_image->ny*reference_image->nz*reference_image->nt*reference_image->nu; - reference_image->data=(signed int *)calloc(NumVoxels,sizeof(signed int)) ; /*!< pointer to data: nbyper*nvox bytes */ - PrintTest("Checking memory allocation",reference_image->data ==0 ,NIFTITEST_TRUE,Errors); - { - if(reference_image->data) - { - signed int i=0; - for(; i < (signed int)NumVoxels ; i++) - { - ((signed int *)(reference_image->data))[i]=i; - } - } - } - } - PrintTest("Setting filenames",nifti_set_filenames( reference_image,write_image_filename, 0, 0 ) != 0, NIFTITEST_TRUE,Errors); - PrintTest("Setting type from names",nifti_set_type_from_names( reference_image ) != 0, NIFTITEST_TRUE,Errors); + strncpy(reference_header.intent_name,"PHANTOM_DATA to be used for regression testing the nifti reader/writer",sizeof(reference_header.intent_name)); + reference_header.intent_name[sizeof(reference_header.intent_name) - 1] = 0; + strncpy(reference_header.descrip,"This is a very long dialog here to use up more than 80 characters of space to test to see if the code is robust enough to deal appropriately with very long and obnoxious lines.",sizeof(reference_header.descrip)); + reference_header.descrip[sizeof(reference_header.descrip) - 1] = 0; + + { + int nbyper; + int swapsize; + nifti_datatype_sizes(reference_header.datatype ,&nbyper,&swapsize); + reference_header.bitpix=nbyper*8; + } + + nifti_image * reference_image=nifti_convert_nhdr2nim(reference_header,write_image_filename); + { + const unsigned int NumVoxels=reference_image->nx*reference_image->ny*reference_image->nz*reference_image->nt*reference_image->nu; + reference_image->data=(signed int *)calloc(NumVoxels,sizeof(signed int)) ; /*!< pointer to data: nbyper*nvox bytes */ + PrintTest("Checking memory allocation",reference_image->data ==0 ,NIFTITEST_TRUE,Errors); + { + if(reference_image->data) + { + signed int i=0; + for(; i < (signed int)NumVoxels ; i++) + { + ((signed int *)(reference_image->data))[i]=i; + } + } + } + } + PrintTest("Setting filenames",nifti_set_filenames( reference_image,write_image_filename, 0, 0 ) != 0, NIFTITEST_TRUE,Errors); + PrintTest("Setting type from names",nifti_set_type_from_names( reference_image ) != 0, NIFTITEST_TRUE,Errors); /* PrintTest("Checking type and names",nifti_type_and_names_match( reference_image , 1 ) != 1, NIFTITEST_TRUE,Errors); */ - PrintTest("Check reference_image data is non null",(reference_image->data==0),NIFTITEST_TRUE,Errors); - return reference_image; + PrintTest("Check reference_image data is non null",(reference_image->data==0),NIFTITEST_TRUE,Errors); + return reference_image; } static void compare_reference_image_values(nifti_image const * const reference_image, nifti_image const * const reloaded_image, int * const Errors) { - if( ! reference_image || ! reference_image->data ) - { - printf("ERROR: Reference image is NULL\n"); - exit(-1); - } - if( ! reloaded_image || ! reloaded_image->data ) - { - printf("ERROR: Reloaded image is NULL\n"); - exit(-1); - } - PrintTest("Checking nifti_type",(reference_image->nifti_type!=reloaded_image->nifti_type),NIFTITEST_FALSE,Errors); - PrintTest("Checking fname",(strcmp(reference_image->fname,reloaded_image->fname)),NIFTITEST_FALSE,Errors); - PrintTest("Checking iname",(strcmp(reference_image->iname,reloaded_image->iname)),NIFTITEST_FALSE,Errors); - PrintTest("Checking ndim",(reference_image->ndim!=reloaded_image->ndim),NIFTITEST_FALSE,Errors); - PrintTest("Checking nx",(reference_image->nx!=reloaded_image->nx),NIFTITEST_FALSE,Errors); - PrintTest("Checking ny",(reference_image->ny!=reloaded_image->ny),NIFTITEST_FALSE,Errors); - PrintTest("Checking nz",(reference_image->nz!=reloaded_image->nz),NIFTITEST_FALSE,Errors); - PrintTest("Checking nt",(reference_image->nt!=reloaded_image->nt),NIFTITEST_FALSE,Errors); - PrintTest("Checking nu",(reference_image->nu!=reloaded_image->nu),NIFTITEST_FALSE,Errors); - PrintTest("Checking dx",(reference_image->dx!=reloaded_image->dx),NIFTITEST_FALSE,Errors); - PrintTest("Checking dy",(reference_image->dy!=reloaded_image->dy),NIFTITEST_FALSE,Errors); - PrintTest("Checking dz",(reference_image->dz!=reloaded_image->dz),NIFTITEST_FALSE,Errors); - PrintTest("Checking dt",(reference_image->dt!=reloaded_image->dt),NIFTITEST_FALSE,Errors); - PrintTest("Checking du",(reference_image->du!=reloaded_image->du),NIFTITEST_FALSE,Errors); - PrintTest("Checking datatype",(reference_image->datatype!=reloaded_image->datatype),NIFTITEST_FALSE,Errors); - { - const unsigned int NumVoxels=reference_image->nx*reference_image->ny*reference_image->nz*reference_image->nt*reference_image->nu; - PrintTest("Check loaded data is non null",(reloaded_image->data==0),NIFTITEST_TRUE,Errors); - PrintTest("Check reference_image data is non null",(reference_image->data==0),NIFTITEST_TRUE,Errors); - { - unsigned int CurrVoxel=0; - for(; CurrVoxel < NumVoxels ; CurrVoxel++) + if( ! reference_image || ! reference_image->data ) + { + printf("ERROR: Reference image is NULL\n"); + exit(-1); + } + if( ! reloaded_image || ! reloaded_image->data ) + { + printf("ERROR: Reloaded image is NULL\n"); + exit(-1); + } + PrintTest("Checking nifti_type",(reference_image->nifti_type!=reloaded_image->nifti_type),NIFTITEST_FALSE,Errors); + PrintTest("Checking fname",(strcmp(reference_image->fname,reloaded_image->fname)),NIFTITEST_FALSE,Errors); + PrintTest("Checking iname",(strcmp(reference_image->iname,reloaded_image->iname)),NIFTITEST_FALSE,Errors); + PrintTest("Checking ndim",(reference_image->ndim!=reloaded_image->ndim),NIFTITEST_FALSE,Errors); + PrintTest("Checking nx",(reference_image->nx!=reloaded_image->nx),NIFTITEST_FALSE,Errors); + PrintTest("Checking ny",(reference_image->ny!=reloaded_image->ny),NIFTITEST_FALSE,Errors); + PrintTest("Checking nz",(reference_image->nz!=reloaded_image->nz),NIFTITEST_FALSE,Errors); + PrintTest("Checking nt",(reference_image->nt!=reloaded_image->nt),NIFTITEST_FALSE,Errors); + PrintTest("Checking nu",(reference_image->nu!=reloaded_image->nu),NIFTITEST_FALSE,Errors); + PrintTest("Checking dx",(reference_image->dx!=reloaded_image->dx),NIFTITEST_FALSE,Errors); + PrintTest("Checking dy",(reference_image->dy!=reloaded_image->dy),NIFTITEST_FALSE,Errors); + PrintTest("Checking dz",(reference_image->dz!=reloaded_image->dz),NIFTITEST_FALSE,Errors); + PrintTest("Checking dt",(reference_image->dt!=reloaded_image->dt),NIFTITEST_FALSE,Errors); + PrintTest("Checking du",(reference_image->du!=reloaded_image->du),NIFTITEST_FALSE,Errors); + PrintTest("Checking datatype",(reference_image->datatype!=reloaded_image->datatype),NIFTITEST_FALSE,Errors); + { + const unsigned int NumVoxels=reference_image->nx*reference_image->ny*reference_image->nz*reference_image->nt*reference_image->nu; + PrintTest("Check loaded data is non null",(reloaded_image->data==0),NIFTITEST_TRUE,Errors); + PrintTest("Check reference_image data is non null",(reference_image->data==0),NIFTITEST_TRUE,Errors); { + unsigned int CurrVoxel=0; + for(; CurrVoxel < NumVoxels ; CurrVoxel++) + { /*printf("%d ",CurrVoxel); fflush(stdout);*/ - if( ((int *)(reference_image->data))[CurrVoxel] != ((int *)(reloaded_image->data))[CurrVoxel]) - { - PrintTest("Incorrect Pixel Value Found",0,NIFTITEST_FALSE,Errors); - } + if( ((int *)(reference_image->data))[CurrVoxel] != ((int *)(reloaded_image->data))[CurrVoxel]) + { + PrintTest("Incorrect Pixel Value Found",0,NIFTITEST_FALSE,Errors); + } + } } - } - } - PrintTest("Checking xyz_units",(reference_image->xyz_units!=reloaded_image->xyz_units),NIFTITEST_FALSE,Errors); - PrintTest("Checking time_units",(reference_image->time_units!=reloaded_image->time_units),NIFTITEST_FALSE,Errors); - PrintTest("Checking intent_code",(reference_image->intent_code!=reloaded_image->intent_code),NIFTITEST_FALSE,Errors); - PrintTest("Checking intent_name",(strncmp(reference_image->intent_name,reloaded_image->intent_name,16) )!=0,NIFTITEST_FALSE,Errors); - PrintTest("Checking description",(strncmp(reference_image->descrip,reloaded_image->descrip,80))!=0,NIFTITEST_FALSE,Errors); + } + PrintTest("Checking xyz_units",(reference_image->xyz_units!=reloaded_image->xyz_units),NIFTITEST_FALSE,Errors); + PrintTest("Checking time_units",(reference_image->time_units!=reloaded_image->time_units),NIFTITEST_FALSE,Errors); + PrintTest("Checking intent_code",(reference_image->intent_code!=reloaded_image->intent_code),NIFTITEST_FALSE,Errors); + PrintTest("Checking intent_name",(strncmp(reference_image->intent_name,reloaded_image->intent_name,16) )!=0,NIFTITEST_FALSE,Errors); + PrintTest("Checking description",(strncmp(reference_image->descrip,reloaded_image->descrip,80))!=0,NIFTITEST_FALSE,Errors); } int main (int argc, const char *argv[]) { - if (argc > 1) - { - printf("The test program takes no arguments: %s", argv[0]); - return EXIT_FAILURE; - } - nifti_set_debug_level(3); - int Errors=0; - { - PrintTest("NOT REALLY AN ERROR, JUST TESTING THE ERROR TEST REPORTING MECHANISM",1,NIFTITEST_FALSE,&Errors); - PrintTest("NOT REALLY AN ERROR, JUST TESTING THE ERROR COUNTING MECHANISM",Errors==1,NIFTITEST_FALSE,&Errors); - Errors=0; - } - { - const char write_image_filename[6][64]={ - "ATestReferenceImageForReadingAndWriting.nii", - "ATestReferenceImageForReadingAndWriting.hdr", - "ATestReferenceImageForReadingAndWriting.img", - "ATestReferenceImageForReadingAndWriting.nii.gz", - "ATestReferenceImageForReadingAndWriting.hdr.gz", - "ATestReferenceImageForReadingAndWriting.img.gz" - }; - printf("======= Testing All Nifti Valid Names ======\n"); - fflush(stdout); - unsigned int filenameindex; - for(filenameindex=0;filenameindex<6; filenameindex++) - { - char buf[512]; - int CompressedTwoFile = strstr(write_image_filename[filenameindex],".img.gz") != 0 || + if (argc > 1) + { + printf("The test program takes no arguments: %s", argv[0]); + return EXIT_FAILURE; + } + nifti_set_debug_level(3); + int Errors=0; + { + PrintTest("NOT REALLY AN ERROR, JUST TESTING THE ERROR TEST REPORTING MECHANISM",1,NIFTITEST_FALSE,&Errors); + PrintTest("NOT REALLY AN ERROR, JUST TESTING THE ERROR COUNTING MECHANISM",Errors==1,NIFTITEST_FALSE,&Errors); + Errors=0; + } + { + const char write_image_filename[6][64]={ + "ATestReferenceImageForReadingAndWriting.nii", + "ATestReferenceImageForReadingAndWriting.hdr", + "ATestReferenceImageForReadingAndWriting.img", + "ATestReferenceImageForReadingAndWriting.nii.gz", + "ATestReferenceImageForReadingAndWriting.hdr.gz", + "ATestReferenceImageForReadingAndWriting.img.gz" + }; + printf("======= Testing All Nifti Valid Names ======\n"); + fflush(stdout); + unsigned int filenameindex; + for(filenameindex=0; filenameindex<6; filenameindex++) + { + char buf[512]; + int CompressedTwoFile = strstr(write_image_filename[filenameindex],".img.gz") != 0 || strstr(write_image_filename[filenameindex],".hdr.gz") != 0; - printf("======= Testing with filename: %s ======\n",write_image_filename[filenameindex]); - fflush(stdout); - nifti_image * reference_image = generate_reference_image(write_image_filename[filenameindex],&Errors); - if( ! reference_image ) - { - printf("ERROR: reference image not generated."); - fflush(stdout); - return EXIT_FAILURE; - } + printf("======= Testing with filename: %s ======\n",write_image_filename[filenameindex]); + fflush(stdout); + nifti_image * reference_image = generate_reference_image(write_image_filename[filenameindex],&Errors); + if( ! reference_image ) + { + printf("ERROR: reference image not generated."); + fflush(stdout); + return EXIT_FAILURE; + } /* * Add an extension to test extension reading */ - { - static char ext[] = "THIS IS A TEST"; - snprintf(buf,sizeof(buf),"nifti_add_extension %s",write_image_filename[filenameindex]); - PrintTest(buf, + { + static char ext[] = "THIS IS A TEST"; + snprintf(buf,sizeof(buf),"nifti_add_extension %s",write_image_filename[filenameindex]); + PrintTest(buf, nifti_add_extension(reference_image, ext,sizeof(ext), NIFTI_ECODE_COMMENT) == -1, NIFTITEST_FALSE,&Errors); - snprintf(buf,sizeof(buf),"valid_nifti_extension %s",write_image_filename[filenameindex]); - PrintTest("valid_nifti_extensions", + snprintf(buf,sizeof(buf),"valid_nifti_extension %s",write_image_filename[filenameindex]); + PrintTest("valid_nifti_extensions", valid_nifti_extensions(reference_image) == 0, NIFTITEST_FALSE,&Errors); - } - PrintTest("Create reference image",reference_image==0,NIFTITEST_TRUE,&Errors); - if( nifti_image_write_status( reference_image ) ) - { - printf("ERROR: failed to write nifti_image."); - fflush(stdout); - nifti_image_free(reference_image); - return EXIT_FAILURE; - } + } + PrintTest("Create reference image",reference_image==0,NIFTITEST_TRUE,&Errors); + if( nifti_image_write_status( reference_image ) ) + { + printf("ERROR: failed to write nifti_image."); + fflush(stdout); + nifti_image_free(reference_image); + return EXIT_FAILURE; + } /* * test nifti_copy_extension */ - { - nifti_image *nim = nifti_simple_init_nim(); - PrintTest("nifti_copy_extension", + { + nifti_image *nim = nifti_simple_init_nim(); + PrintTest("nifti_copy_extension", nifti_copy_extensions(nim,reference_image), NIFTITEST_FALSE,&Errors); - nifti_image_free(nim); - nim = nifti_copy_nim_info(reference_image); - PrintTest("nifti_copy_nim_info", + nifti_image_free(nim); + nim = nifti_copy_nim_info(reference_image); + PrintTest("nifti_copy_nim_info", nim == 0, NIFTITEST_FALSE,&Errors); - PrintTest("nifti_nim_is_valid", + PrintTest("nifti_nim_is_valid", nifti_nim_is_valid(nim,0) == 0, NIFTITEST_FALSE,&Errors); - nifti_image_free(nim); + nifti_image_free(nim); - } - { - nifti_image * reloaded_image = (reference_image->fname) ? nifti_image_read(reference_image->fname,1): NULL; - PrintTest("Reload of image ",reloaded_image==0,NIFTITEST_TRUE,&Errors); + } + { + nifti_image * reloaded_image = (reference_image->fname) ? nifti_image_read(reference_image->fname,1): NULL; + PrintTest("Reload of image ",reloaded_image==0,NIFTITEST_TRUE,&Errors); - { + { /* * if the file is named '.img', '.hdr', '.img.gz', or '.hdr.gz', then * the header extensions won't be saved with the file. * The test will fail if it finds an extension in a 2-file NIfTI, or * fails to find one in a '.nii' or '.nii.gz' file. */ - int result = valid_nifti_extensions(reloaded_image); - snprintf(buf,sizeof(buf),"reload valid_nifti_extensions %s",write_image_filename[filenameindex]); - PrintTest(buf, + int result = valid_nifti_extensions(reloaded_image); + snprintf(buf,sizeof(buf),"reload valid_nifti_extensions %s",write_image_filename[filenameindex]); + PrintTest(buf, CompressedTwoFile ? result != 0 : result == 0, NIFTITEST_FALSE,&Errors); - } - nifti_image_infodump(reloaded_image); - compare_reference_image_values(reference_image,reloaded_image,&Errors); - nifti_image_free(reloaded_image); - } + } + nifti_image_infodump(reloaded_image); + compare_reference_image_values(reference_image,reloaded_image,&Errors); + nifti_image_free(reloaded_image); + } - { - nifti_brick_list NB_orig, NB_select; + { + nifti_brick_list NB_orig, NB_select; - nifti_image * nim_orig, * nim_select; + nifti_image * nim_orig, * nim_select; - const int blist[5] = { 7, 0, 5, 5, 9 }; + const int blist[5] = { 7, 0, 5, 5, 9 }; /* * test some error paths in the nifti_image_read_bricks */ - nim_orig = nifti_image_read_bricks(reference_image->fname,0,blist, &NB_orig); - PrintTest("invalid arg bricked image read 1",nim_orig != 0,NIFTITEST_FALSE,&Errors); + nim_orig = nifti_image_read_bricks(reference_image->fname,0,blist, &NB_orig); + PrintTest("invalid arg bricked image read 1",nim_orig != 0,NIFTITEST_FALSE,&Errors); - nim_orig = nifti_image_read_bricks(reference_image->fname, 0, NULL, &NB_orig); - PrintTest("Reload of bricked image",nim_orig == 0,NIFTITEST_FALSE,&Errors); - nifti_free_NBL(&NB_orig); - nifti_image_free(nim_orig); + nim_orig = nifti_image_read_bricks(reference_image->fname, 0, NULL, &NB_orig); + PrintTest("Reload of bricked image",nim_orig == 0,NIFTITEST_FALSE,&Errors); + nifti_free_NBL(&NB_orig); + nifti_image_free(nim_orig); - nim_select = nifti_image_read_bricks(reference_image->fname, 5, blist, &NB_select); - PrintTest("Reload of bricked image with blist",nim_orig == 0,NIFTITEST_FALSE,&Errors); - nifti_free_NBL(&NB_select); - nifti_image_free(nim_select); + nim_select = nifti_image_read_bricks(reference_image->fname, 5, blist, &NB_select); + PrintTest("Reload of bricked image with blist",nim_orig == 0,NIFTITEST_FALSE,&Errors); + nifti_free_NBL(&NB_select); + nifti_image_free(nim_select); - } + } /* * test nifti_update_dims_from_array */ - PrintTest("nifti_update_dims_from_array -- valid dims", + PrintTest("nifti_update_dims_from_array -- valid dims", nifti_update_dims_from_array(reference_image) != 0, NIFTITEST_FALSE,&Errors); - reference_image->dim[0] = 8; - PrintTest("nifti_update_dims_from_array -- invalid dims", + reference_image->dim[0] = 8; + PrintTest("nifti_update_dims_from_array -- invalid dims", nifti_update_dims_from_array(reference_image) == 0, NIFTITEST_FALSE,&Errors); - { - nifti_1_header x = nifti_convert_nim2nhdr(reference_image); - char local_buffer[512]; - snprintf(local_buffer,sizeof(local_buffer),"nifti_hdr_looks_good %s",reference_image->fname); - PrintTest(local_buffer, + { + nifti_1_header x = nifti_convert_nim2nhdr(reference_image); + char local_buffer[512]; + snprintf(local_buffer,sizeof(local_buffer),"nifti_hdr_looks_good %s",reference_image->fname); + PrintTest(local_buffer, !nifti_hdr_looks_good(&x), NIFTITEST_FALSE,&Errors); - } + } - nifti_image_free(reference_image); - } + nifti_image_free(reference_image); + } /* * check nifti_findimgname */ - { - char *imgname = nifti_findimgname("ATestReferenceImageForReadingAndWriting.hdr",2); - PrintTest("nifti_findimgname", + { + char *imgname = nifti_findimgname("ATestReferenceImageForReadingAndWriting.hdr",2); + PrintTest("nifti_findimgname", imgname == 0 || strcmp(imgname,"ATestReferenceImageForReadingAndWriting.img") != 0, NIFTITEST_FALSE,&Errors); - free(imgname); - } - { - int IsNiftiFile; - IsNiftiFile = is_nifti_file(write_image_filename[0]); - PrintTest("is_nifti_file0", + free(imgname); + } + { + int IsNiftiFile; + IsNiftiFile = is_nifti_file(write_image_filename[0]); + PrintTest("is_nifti_file0", IsNiftiFile != 1,NIFTITEST_FALSE,&Errors); - IsNiftiFile = is_nifti_file(write_image_filename[1]); - PrintTest("is_nifti_file1", + IsNiftiFile = is_nifti_file(write_image_filename[1]); + PrintTest("is_nifti_file1", IsNiftiFile != 2,NIFTITEST_FALSE,&Errors); - IsNiftiFile = is_nifti_file(write_image_filename[3]); - PrintTest("is_nifti_file2", + IsNiftiFile = is_nifti_file(write_image_filename[3]); + PrintTest("is_nifti_file2", IsNiftiFile != 1,NIFTITEST_FALSE,&Errors); - IsNiftiFile = is_nifti_file(write_image_filename[4]); - PrintTest("is_nifti_file2", + IsNiftiFile = is_nifti_file(write_image_filename[4]); + PrintTest("is_nifti_file2", IsNiftiFile != 2,NIFTITEST_FALSE,&Errors); - } + } - } - { + } + { /* * test writing and reading an ascii file */ - nifti_image * reference_image = + nifti_image * reference_image = generate_reference_image("TestAsciiImage.nia",&Errors); - reference_image->nifti_type = 3; - if( nifti_image_write_status( reference_image ) ) - { - printf("ERROR: failed to write nifti_image."); - fflush(stdout); - } + reference_image->nifti_type = 3; + if( nifti_image_write_status( reference_image ) ) + { + printf("ERROR: failed to write nifti_image."); + fflush(stdout); + } - nifti_image * reloaded_image = nifti_image_read("TestAsciiImage.nia",1); - PrintTest("Read/Write Ascii image", + nifti_image * reloaded_image = nifti_image_read("TestAsciiImage.nia",1); + PrintTest("Read/Write Ascii image", reloaded_image == 0,NIFTITEST_FALSE,&Errors); - nifti_image_free(reference_image); - nifti_image_free(reloaded_image); - } - - { - enum { NUM_FILE_NAMES=8 }; - const char * FILE_NAMES[NUM_FILE_NAMES]={ - "myimage", - "myimage.tif", - "myimage.tif.gz", - "myimage.nii", - "myimage.img.gz", - ".nii", - ".myhiddenimage", - ".myhiddenimage.nii" - }; - const char * KNOWN_FILE_BASENAMES[NUM_FILE_NAMES]={ - "myimage", - "myimage.tif", - "myimage.tif.gz", - "myimage", - "myimage", - "", - ".myhiddenimage", - ".myhiddenimage" - }; - const int KNOWN_nifti_validfilename[NUM_FILE_NAMES]={ - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1 - }; - const int KNOWN_nifti_is_complete_filename[NUM_FILE_NAMES]={ - 0, - 0, - 0, - 1, - 1, - 0, - 0, - 1 - }; - unsigned int fni; - for(fni=0;fnidata != 0, NIFTITEST_FALSE,&Errors); - znzclose(f); - nifti_image_free(nim); - } + znzclose(f); + nifti_image_free(nim); + } /* * call various functions from nifti_stats */ - printf("\n\nTOTAL ERRORS=%d\n",Errors); - return Errors; + printf("\n\nTOTAL ERRORS=%d\n",Errors); + return Errors; } diff --git a/niftilib/nifti_tester002.c b/niftilib/nifti_tester002.c index 8a120df..e33f2a8 100644 --- a/niftilib/nifti_tester002.c +++ b/niftilib/nifti_tester002.c @@ -6,33 +6,33 @@ #include int main (int argc, const char *argv[]) { - if (argc > 1) - { - printf("The test program takes no arguments: %s", argv[0]); - return EXIT_FAILURE; - } + if (argc > 1) + { + printf("The test program takes no arguments: %s", argv[0]); + return EXIT_FAILURE; + } /* * create a 'dummy' image */ - nifti_image *i1 = nifti_simple_init_nim(); - nifti_image *i2; + nifti_image *i1 = nifti_simple_init_nim(); + nifti_image *i2; /* * add an extension to the dummy */ - static char ext[] = "THIS IS A TEST"; - nifti_add_extension(i1,ext,sizeof(ext),NIFTI_ECODE_COMMENT); + static char ext[] = "THIS IS A TEST"; + nifti_add_extension(i1,ext,sizeof(ext),NIFTI_ECODE_COMMENT); /* * make a new nim from the dummy */ - i2 = nifti_copy_nim_info(i1); + i2 = nifti_copy_nim_info(i1); /* * if the bug isn't fixed in niftilib, the second nifti_image_free * will fail because both nims point to the same extensions. With gcc * this will abort inside the standard library */ - nifti_image_free(i1); - nifti_image_free(i2); - return 0; + nifti_image_free(i1); + nifti_image_free(i2); + return 0; } diff --git a/niftilib/uncrustify.cfg b/niftilib/uncrustify.cfg new file mode 100644 index 0000000..a40e581 --- /dev/null +++ b/niftilib/uncrustify.cfg @@ -0,0 +1,20 @@ +cmt_indent_multi = false +disable_processing_nl_cont = true +indent_columns = 3 +indent_comment = false +indent_ignore_case_brace = true +indent_ignore_first_continue = true +indent_paren_close = 2 +indent_switch_case = 3 +indent_with_tabs = 0 +mod_enum_last_comma = ignore +sp_after_type = add +sp_skip_vbrace_tokens = true +use_indent_func_call_param = false +sp_before_semi = ignore +indent_comma_brace = -1 +indent_comma_paren = -1 +sp_before_comma = ignore +sp_after_comma = ignore +sp_paren_comma = ignore +sp_not = ignore