c_nifti 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +37 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/COPYING +621 -0
  7. data/COPYING.lesser +166 -0
  8. data/Gemfile +4 -0
  9. data/README.md +117 -0
  10. data/Rakefile +6 -0
  11. data/c_nifti.gemspec +31 -0
  12. data/ext/nifticlib/extconf.rb +30 -0
  13. data/ext/nifticlib/include/nifti_image.h +2 -0
  14. data/ext/nifticlib/include/nifti_image_converters.h +3 -0
  15. data/ext/nifticlib/include/nifti_image_dimensions.h +1 -0
  16. data/ext/nifticlib/include/nifti_image_intents.h +1 -0
  17. data/ext/nifticlib/include/nifti_image_metadata.h +1 -0
  18. data/ext/nifticlib/include/nifti_image_quaternions.h +1 -0
  19. data/ext/nifticlib/include/nifti_image_spacings.h +1 -0
  20. data/ext/nifticlib/include/nifti_image_timings.h +1 -0
  21. data/ext/nifticlib/include/nifti_image_transforms.h +1 -0
  22. data/ext/nifticlib/nifti_image.c +169 -0
  23. data/ext/nifticlib/nifti_image_converters.c +65 -0
  24. data/ext/nifticlib/nifti_image_dimensions.c +113 -0
  25. data/ext/nifticlib/nifti_image_intents.c +45 -0
  26. data/ext/nifticlib/nifti_image_metadata.c +133 -0
  27. data/ext/nifticlib/nifti_image_quaternions.c +59 -0
  28. data/ext/nifticlib/nifti_image_spacings.c +87 -0
  29. data/ext/nifticlib/nifti_image_timings.c +66 -0
  30. data/ext/nifticlib/nifti_image_transforms.c +38 -0
  31. data/ext/nifticlib/nifticlib-2.0.0/CMakeLists.txt +140 -0
  32. data/ext/nifticlib/nifticlib-2.0.0/CTestConfig.cmake +13 -0
  33. data/ext/nifticlib/nifticlib-2.0.0/LICENSE +9 -0
  34. data/ext/nifticlib/nifticlib-2.0.0/Makefile +265 -0
  35. data/ext/nifticlib/nifticlib-2.0.0/Makefile.cross_mingw32 +94 -0
  36. data/ext/nifticlib/nifticlib-2.0.0/README +79 -0
  37. data/ext/nifticlib/nifticlib-2.0.0/Testing/CMakeLists.txt +7 -0
  38. data/ext/nifticlib/nifticlib-2.0.0/Testing/Data/ATestReferenceImageForReadingAndWriting.nii.gz +0 -0
  39. data/ext/nifticlib/nifticlib-2.0.0/Testing/Makefile +21 -0
  40. data/ext/nifticlib/nifticlib-2.0.0/Testing/README_regress +50 -0
  41. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/@show.diffs +33 -0
  42. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/@test +80 -0
  43. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/CMakeLists.txt +47 -0
  44. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/cmake_testscripts/bricks_test.sh +32 -0
  45. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/cmake_testscripts/comment_test.sh +65 -0
  46. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/cmake_testscripts/dci_test.sh +46 -0
  47. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/cmake_testscripts/dsets_test.sh +61 -0
  48. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/cmake_testscripts/dts_test.sh +75 -0
  49. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/cmake_testscripts/fetch_data_test.sh +45 -0
  50. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/cmake_testscripts/mod_header_test.sh +60 -0
  51. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/cmake_testscripts/newfiles_test.sh +36 -0
  52. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c01.versions +10 -0
  53. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c02.nt.help +5 -0
  54. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c03.hist +5 -0
  55. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c04.disp.anat0.info +7 -0
  56. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c05.mod.hdr +9 -0
  57. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c06.add.ext +22 -0
  58. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c07.cbl.4bricks +8 -0
  59. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c08.dts.19.36.11 +4 -0
  60. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c09.dts4.compare +9 -0
  61. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c10.dci.ts4 +15 -0
  62. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c10a.dci.run.210 +16 -0
  63. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c11.add.comment +8 -0
  64. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c12.check.comments +7 -0
  65. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c13.check.hdrs +5 -0
  66. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c14.make.dsets +21 -0
  67. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c15.new.files +21 -0
  68. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c16.rand.swap +35 -0
  69. data/ext/nifticlib/nifticlib-2.0.0/Testing/nifti_regress_test/commands/c17.file.case +34 -0
  70. data/ext/nifticlib/nifticlib-2.0.0/Testing/niftilib/CMakeLists.txt +15 -0
  71. data/ext/nifticlib/nifticlib-2.0.0/Testing/niftilib/nifti_test.c +690 -0
  72. data/ext/nifticlib/nifticlib-2.0.0/Testing/niftilib/nifti_test2.c +32 -0
  73. data/ext/nifticlib/nifticlib-2.0.0/Updates.txt +110 -0
  74. data/ext/nifticlib/nifticlib-2.0.0/bin/.gitkeep +0 -0
  75. data/ext/nifticlib/nifticlib-2.0.0/docs/Doxy_nifti.txt +123 -0
  76. data/ext/nifticlib/nifticlib-2.0.0/docs/Doxyfile.ORIG +746 -0
  77. data/ext/nifticlib/nifticlib-2.0.0/examples/CMakeLists.txt +15 -0
  78. data/ext/nifticlib/nifticlib-2.0.0/examples/Makefile +48 -0
  79. data/ext/nifticlib/nifticlib-2.0.0/examples/clib_01_read_write.c +94 -0
  80. data/ext/nifticlib/nifticlib-2.0.0/examples/fsl_api_driver.c +142 -0
  81. data/ext/nifticlib/nifticlib-2.0.0/fsliolib/CMakeLists.txt +32 -0
  82. data/ext/nifticlib/nifticlib-2.0.0/fsliolib/Makefile +29 -0
  83. data/ext/nifticlib/nifticlib-2.0.0/fsliolib/fslio.c +2426 -0
  84. data/ext/nifticlib/nifticlib-2.0.0/fsliolib/fslio.tcl +83 -0
  85. data/ext/nifticlib/nifticlib-2.0.0/fsliolib/imcp +65 -0
  86. data/ext/nifticlib/nifticlib-2.0.0/fsliolib/imglob +59 -0
  87. data/ext/nifticlib/nifticlib-2.0.0/fsliolib/imln +37 -0
  88. data/ext/nifticlib/nifticlib-2.0.0/fsliolib/immv +64 -0
  89. data/ext/nifticlib/nifticlib-2.0.0/fsliolib/imrm +29 -0
  90. data/ext/nifticlib/nifticlib-2.0.0/fsliolib/imtest +53 -0
  91. data/ext/nifticlib/nifticlib-2.0.0/fsliolib/remove_ext +33 -0
  92. data/ext/nifticlib/nifticlib-2.0.0/include/.gitkeep +0 -0
  93. data/ext/nifticlib/nifticlib-2.0.0/nifticdf/CMakeLists.txt +28 -0
  94. data/ext/nifticlib/nifticlib-2.0.0/nifticdf/Makefile +28 -0
  95. data/ext/nifticlib/nifticlib-2.0.0/nifticdf/nifticdf.c +11107 -0
  96. data/ext/nifticlib/nifticlib-2.0.0/niftilib/CMakeLists.txt +33 -0
  97. data/ext/nifticlib/nifticlib-2.0.0/niftilib/Makefile +31 -0
  98. data/ext/nifticlib/nifticlib-2.0.0/niftilib/nifti1_io.c +7509 -0
  99. data/ext/nifticlib/nifticlib-2.0.0/packaging/DevPackage.template +18 -0
  100. data/ext/nifticlib/nifticlib-2.0.0/packaging/nifticlib.spec +62 -0
  101. data/ext/nifticlib/nifticlib-2.0.0/real_easy/nifti1_read_write.c +361 -0
  102. data/ext/nifticlib/nifticlib-2.0.0/utils/CMakeLists.txt +73 -0
  103. data/ext/nifticlib/nifticlib-2.0.0/utils/Makefile +55 -0
  104. data/ext/nifticlib/nifticlib-2.0.0/utils/nifti1_test.c +95 -0
  105. data/ext/nifticlib/nifticlib-2.0.0/utils/nifti_stats.c +95 -0
  106. data/ext/nifticlib/nifticlib-2.0.0/utils/nifti_tool.c +4193 -0
  107. data/ext/nifticlib/nifticlib-2.0.0/utils/nifti_tool.h +163 -0
  108. data/ext/nifticlib/nifticlib-2.0.0/znzlib/CMakeLists.txt +31 -0
  109. data/ext/nifticlib/nifticlib-2.0.0/znzlib/Makefile +33 -0
  110. data/ext/nifticlib/nifticlib-2.0.0/znzlib/znzlib.c +322 -0
  111. data/ext/nifticlib/nifticlib.c +107 -0
  112. data/ext/nifticlib/patches/nifticlib_fpic.patch +13 -0
  113. data/features/read_modify_write.feature +11 -0
  114. data/features/step_definitions/nifti_image_steps.rb +16 -0
  115. data/features/support/env.rb +14 -0
  116. data/features/support/fixtures/sample.nii.gz +0 -0
  117. data/lib/c_nifti.rb +9 -0
  118. data/lib/c_nifti/data.rb +70 -0
  119. data/lib/c_nifti/header.rb +16 -0
  120. data/lib/c_nifti/header_element.rb +13 -0
  121. data/lib/c_nifti/header_element/datatype.rb +49 -0
  122. data/lib/c_nifti/header_element/datatype/base.rb +9 -0
  123. data/lib/c_nifti/header_element/datatype/binary.rb +11 -0
  124. data/lib/c_nifti/header_element/datatype/double.rb +11 -0
  125. data/lib/c_nifti/header_element/datatype/float.rb +11 -0
  126. data/lib/c_nifti/header_element/datatype/long_double.rb +11 -0
  127. data/lib/c_nifti/header_element/datatype/long_long.rb +11 -0
  128. data/lib/c_nifti/header_element/datatype/signed_char.rb +11 -0
  129. data/lib/c_nifti/header_element/datatype/signed_int.rb +11 -0
  130. data/lib/c_nifti/header_element/datatype/signed_short.rb +11 -0
  131. data/lib/c_nifti/header_element/datatype/unsigned_char.rb +11 -0
  132. data/lib/c_nifti/header_element/datatype/unsigned_int.rb +11 -0
  133. data/lib/c_nifti/header_element/datatype/unsigned_long_long.rb +11 -0
  134. data/lib/c_nifti/header_element/datatype/unsigned_short.rb +11 -0
  135. data/lib/c_nifti/header_element/dimensions.rb +53 -0
  136. data/lib/c_nifti/header_element/intents.rb +25 -0
  137. data/lib/c_nifti/header_element/metadata.rb +49 -0
  138. data/lib/c_nifti/header_element/miscellaneous.rb +25 -0
  139. data/lib/c_nifti/header_element/quaternions.rb +33 -0
  140. data/lib/c_nifti/header_element/spacings.rb +45 -0
  141. data/lib/c_nifti/header_element/timings.rb +37 -0
  142. data/lib/c_nifti/header_element/transforms.rb +21 -0
  143. data/lib/c_nifti/image.rb +31 -0
  144. data/lib/c_nifti/version.rb +3 -0
  145. data/lib/nifticlib.rb +1 -0
  146. data/spec/data_spec.rb +112 -0
  147. data/spec/factories/nifti_images.rb +9 -0
  148. data/spec/header_element/datatype/base_spec.rb +9 -0
  149. data/spec/header_element/datatype/binary_spec.rb +9 -0
  150. data/spec/header_element/datatype/double_spec.rb +9 -0
  151. data/spec/header_element/datatype/float_spec.rb +9 -0
  152. data/spec/header_element/datatype/long_double_spec.rb +9 -0
  153. data/spec/header_element/datatype/long_long_spec.rb +9 -0
  154. data/spec/header_element/datatype/signed_char_spec.rb +9 -0
  155. data/spec/header_element/datatype/signed_int_spec.rb +9 -0
  156. data/spec/header_element/datatype/signed_short_spec.rb +9 -0
  157. data/spec/header_element/datatype/unsigned_char_spec.rb +9 -0
  158. data/spec/header_element/datatype/unsigned_int_spec.rb +9 -0
  159. data/spec/header_element/datatype/unsigned_long_long.rb +9 -0
  160. data/spec/header_element/datatype/unsigned_short_spec.rb +9 -0
  161. data/spec/header_element/datatype_spec.rb +109 -0
  162. data/spec/header_element/dimensions_spec.rb +105 -0
  163. data/spec/header_element/intents_spec.rb +48 -0
  164. data/spec/header_element/metadata_spec.rb +96 -0
  165. data/spec/header_element/miscellaneous_spec.rb +48 -0
  166. data/spec/header_element/quaternions_spec.rb +64 -0
  167. data/spec/header_element/spacings_spec.rb +88 -0
  168. data/spec/header_element/timings_spec.rb +72 -0
  169. data/spec/header_element/transforms_spec.rb +64 -0
  170. data/spec/image_spec.rb +39 -0
  171. data/spec/spec_helper.rb +85 -0
  172. metadata +363 -0
@@ -0,0 +1,15 @@
1
+ #File Generated by Hans J. Johnson
2
+ #Please contact hans-johnson@uiowa.edu for making enhancments/corrections
3
+ PROJECT(FSLEXAMPLES)
4
+
5
+ INCLUDE_DIRECTORIES(${NIFTILIB_SOURCE_DIR} ${ZNZLIB_SOURCE_DIR} ${FSLIOLIB_SOURCE_DIR} )
6
+
7
+ ADD_EXECUTABLE( ${PACKAGE_PREFIX}fsl_api_driver fsl_api_driver.c )
8
+ TARGET_LINK_LIBRARIES( fsl_api_driver ${PACKAGE_PREFIX}fslio)
9
+
10
+ IF(NOT NIFTI_INSTALL_NO_LIBRARIES)
11
+ INSTALL(TARGETS ${PACKAGE_PREFIX}fsl_api_driver
12
+ RUNTIME DESTINATION ${NIFTI_INSTALL_BIN_DIR} COMPONENT RuntimeLibraries
13
+ LIBRARY DESTINATION ${NIFTI_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
14
+ ARCHIVE DESTINATION ${NIFTI_INSTALL_LIB_DIR} COMPONENT Development)
15
+ ENDIF(NOT NIFTI_INSTALL_NO_LIBRARIES)
@@ -0,0 +1,48 @@
1
+ CC = gcc
2
+ CFLAGS = -ansi -pedantic
3
+
4
+ FSLIO_INCS = -I../include
5
+ NIFTI_INCS = -I../include
6
+ ZNZ_INCS = -I/usr/include
7
+
8
+ FSLIO_LIBS = -L../lib -lfslio
9
+ NIFTI_LIBS = -L../lib -lniftiio
10
+ ZNZ_LIBS = -L/usr/lib -L../lib -lznz -lm -lz
11
+
12
+
13
+ ##############################################################
14
+ # platform specific redefines (to use, set ARCH appropriately)
15
+
16
+ ## ARCH = X86_64
17
+
18
+ ifeq ($(ARCH),SGI) ## SGI 32bit
19
+ ZNZ_INCS = -I/usr/freeware/include
20
+ ZNZ_LIBS = -L/usr/freeware/lib32 -L../lib -lznz -lm -lz
21
+ else
22
+ ifeq ($(ARCH),I386) ## 32-bit Linux
23
+ ZNZ_INCS = -I/usr/include
24
+ ZNZ_LIBS = -L/usr/lib -L../lib -lznz -lm -lz
25
+ else
26
+ ifeq ($(ARCH),X86_64) ## 64-bit Linux
27
+ ZNZ_INCS =
28
+ ZNZ_LIBS = -L../lib -lznz -lm -lz
29
+ endif
30
+ endif
31
+ endif
32
+
33
+
34
+
35
+ all: fsl_api_driver clib_01_read_write
36
+
37
+ clean:
38
+ rm -f fsl_api_driver clib_01_read_write
39
+
40
+ fsl_api_driver: fsl_api_driver.c ../lib/libfslio.a
41
+ $(CC) $(CFLAGS) -o fsl_api_driver fsl_api_driver.c $(FSLIO_INCS) $(NIFTI_INCS) $(ZNZ_INCS) $(FSLIO_LIBS) $(NIFTI_LIBS) $(ZNZ_LIBS)
42
+
43
+ clib_01_read_write: clib_01_read_write.c
44
+ $(CC) $(CFLAGS) -o clib_01_read_write clib_01_read_write.c $(NIFTI_INCS) $(ZNZ_INCS) $(NIFTI_LIBS) $(ZNZ_LIBS)
45
+
46
+ help:
47
+ @echo "all: make the fsl_api_driver program"
48
+ @echo "clean: rm the fsl_api_driver program"
@@ -0,0 +1,94 @@
1
+ /* ----------------------------------------------------------------------
2
+ * A basic example to read/write a nifti dataset (e.g. cp command).
3
+ *
4
+ * compile example (consider -pedantic or -Wall):
5
+ *
6
+ * gcc -o clib_01_read_write clib_01_read_write.c \
7
+ * -I../include -L../lib -lniftiio -lznz -lz -lm
8
+ *
9
+ * R Reynolds 14 Apr 2009
10
+ *----------------------------------------------------------------------
11
+ */
12
+ #include <stdio.h>
13
+
14
+ #include <nifti1_io.h>
15
+
16
+ int show_help( void )
17
+ {
18
+ printf(
19
+ "cib_01_read_write: short exmample of reading/writing NIfTI\n"
20
+ "\n"
21
+ " This program is to demonstrate how to read a NIfTI dataset,\n"
22
+ " set output filenames and write a NIfTI dataset, all via the\n"
23
+ " standard NIfTI C library.\n"
24
+ "\n"
25
+ " basic usage: cib_01_read_write -input FILE_IN -output FILE_OUT\n"
26
+ "\n"
27
+ " options: -help : show this help\n"
28
+ " -verb LEVEL : the verbose level to LEVEL\n"
29
+ "\n");
30
+ return 0;
31
+ }
32
+
33
+ int main(int argc, char * argv[])
34
+ {
35
+ nifti_image * nim=NULL;
36
+ char * fin=NULL, * fout=NULL;
37
+ int ac;
38
+
39
+ if( argc < 2 ) return show_help(); /* typing '-help' is sooo much work */
40
+
41
+ /* process user options: 4 are valid presently */
42
+ for( ac = 1; ac < argc; ac++ ) {
43
+ if( ! strncmp(argv[ac], "-h", 2) ) {
44
+ return show_help();
45
+ }
46
+ else if( ! strcmp(argv[ac], "-input") ) {
47
+ if( ++ac >= argc ) {
48
+ fprintf(stderr, "** missing argument for -input\n");
49
+ return 1;
50
+ }
51
+ fin = argv[ac]; /* no string copy, just pointer assignment */
52
+ }
53
+ else if( ! strcmp(argv[ac], "-output") ) {
54
+ if( ++ac >= argc ) {
55
+ fprintf(stderr, "** missing argument for -output\n");
56
+ return 2;
57
+ }
58
+ fout = argv[ac];
59
+ }
60
+ else if( ! strcmp(argv[ac], "-verb") ) {
61
+ if( ++ac >= argc ) {
62
+ fprintf(stderr, "** missing argument for -verb\n");
63
+ return 2;
64
+ }
65
+ nifti_set_debug_level(atoi(argv[ac]));
66
+ }
67
+ else {
68
+ fprintf(stderr,"** invalid option, '%s'\n", argv[ac]);
69
+ return 1;
70
+ }
71
+ }
72
+
73
+ if( !fin ) { fprintf(stderr, "** missing option '-input'\n"); return 1; }
74
+ if( !fout ) { fprintf(stderr, "** missing option '-output'\n"); return 1; }
75
+
76
+ /* read input dataset, including data */
77
+ nim = nifti_image_read(fin, 1);
78
+ if( !nim ) {
79
+ fprintf(stderr,"** failed to read NIfTI image from '%s'\n", fin);
80
+ return 2;
81
+ }
82
+
83
+ /* assign nifti_image fname/iname pair, based on output filename
84
+ (request to 'check' image and 'set_byte_order' here) */
85
+ if( nifti_set_filenames(nim, fout, 1, 1) ) return 1;
86
+
87
+ /* if we get here, write the output dataset */
88
+ nifti_image_write( nim );
89
+
90
+ /* and clean up memory */
91
+ nifti_image_free( nim );
92
+
93
+ return 0;
94
+ }
@@ -0,0 +1,142 @@
1
+ /*******************************************************************
2
+ *
3
+ * fsl_api_driver.c
4
+ *
5
+ * Test fslio API
6
+ *
7
+ * Usage: fsl_api_driver <command> <dataset(s)> [params]
8
+ *
9
+ *
10
+ * print <dataset>
11
+ * print dataset header fields
12
+ * peek <dataset> X Y Z T
13
+ * print value at voxel location X Y Z T (0-based index)
14
+ * timecourse <dataset> X Y Z
15
+ * print timecourse at voxel location X Y Z (0-based index)
16
+ *
17
+ * Kate Fissell, University of Pittsburgh
18
+ * 12/04
19
+ *******************************************************************/
20
+
21
+
22
+ #include <stdio.h>
23
+ #include <strings.h>
24
+
25
+ #include <nifti1.h>
26
+ #include <fslio.h>
27
+
28
+ void pusage(char *cmd);
29
+
30
+ int main(int argc, char * argv[])
31
+ {
32
+
33
+ FSLIO *fslio;
34
+ void *buffer;
35
+ char *f1name;
36
+ double ***vol;
37
+ int x,y,z,t;
38
+
39
+
40
+ /*** process commandline parameters */
41
+ if (argc < 2) {
42
+ pusage(argv[0]);
43
+ exit(1);
44
+ }
45
+
46
+
47
+ /************************* PRINT ***************************/
48
+ if (!strncmp(argv[1],"print",5)) {
49
+ if (argc != 3) {
50
+ fprintf(stderr, "\nError, print command takes one parameter: print <dataset>\n");
51
+ exit(1);
52
+ }
53
+
54
+ f1name = argv[2];
55
+ /** open nifti dataset */
56
+ fslio = FslInit();
57
+ buffer = FslReadAllVolumes(fslio,f1name);
58
+ if (buffer == NULL) {
59
+ fprintf(stderr, "\nError opening and reading %s.\n",f1name);
60
+ exit(1);
61
+ }
62
+
63
+ nifti_image_infodump(fslio->niftiptr);
64
+
65
+ exit(0);
66
+ }
67
+
68
+
69
+
70
+ /************************* PEEK ***************************/
71
+ if (!strncmp(argv[1],"peek",4)) {
72
+ if (argc != 7) {
73
+ fprintf(stderr, "\nError, peek command takes five parameters: peek <dataset> X Y Z T\n");
74
+ exit(1);
75
+ }
76
+
77
+ /**** get inputs */
78
+ f1name = argv[2];
79
+ x = atoi(argv[3]);
80
+ y = atoi(argv[4]);
81
+ z = atoi(argv[5]);
82
+ t = atoi(argv[6]);
83
+
84
+ /** open nifti dataset header */
85
+ fslio = FslReadHeader(f1name);
86
+ if (fslio == NULL) {
87
+ fprintf(stderr, "\nError, could not read header info for %s.\n",f1name);
88
+ exit(1);
89
+ }
90
+
91
+ /**** check inputs */
92
+ if ( (x<0) || (x>=fslio->niftiptr->nx) ) {
93
+ fprintf(stderr, "\nError: x index (%d) out of range [0..%d]\n",x,fslio->niftiptr->nx-1);
94
+ exit(1);
95
+ }
96
+ if ( (y<0) || (y>=fslio->niftiptr->ny) ) {
97
+ fprintf(stderr, "\nError: y index (%d) out of range [0..%d]\n",y,fslio->niftiptr->ny-1);
98
+ exit(1);
99
+ }
100
+ if ( (z<0) || (z>=fslio->niftiptr->nz) ) {
101
+ fprintf(stderr, "\nError: z index (%d) out of range [0..%d]\n",z,fslio->niftiptr->nz-1);
102
+ exit(1);
103
+ }
104
+ if ( (t<0) || (t>=fslio->niftiptr->nt) ) {
105
+ fprintf(stderr, "\nError: t index (%d) out of range [0..%d]\n",t,fslio->niftiptr->nt-1);
106
+ exit(1);
107
+ }
108
+
109
+ /*** get volume data as scaled doubles */
110
+
111
+ vol = FslGetVolumeAsScaledDouble(fslio,t);
112
+ if (vol == NULL) {
113
+ fprintf(stderr, "\nError accessing %s\n",f1name);
114
+ exit(1);
115
+ }
116
+ else {
117
+ fprintf(stderr, "\nLocation %d %d %d %d: %.4f\n",x,y,z,t,vol[z][y][x]);
118
+ exit(0);
119
+ }
120
+
121
+ }
122
+
123
+
124
+
125
+ fprintf(stderr, "\nError, unrecognized command %s\n",argv[1]);
126
+ pusage(argv[0]);
127
+
128
+
129
+ exit(1);
130
+ }
131
+
132
+
133
+ void pusage(char *cmd) {
134
+ fprintf(stderr, "\n%s is a small driver program to test out the fslio API.\n",cmd);
135
+ fprintf(stderr, "\nUsage: %s <command> <dataset(s)> <parameters>",cmd);
136
+ fprintf(stderr, "\n\n\tCommands:");
137
+ fprintf(stderr, "\n\tprint <dataset>\t\t\tprint dataset header");
138
+ fprintf(stderr, "\n\tpeek <dataset> X Y Z T\t\tprint dataset value at location (0-based) (x,y,z,t).");
139
+
140
+ fprintf(stderr, "\n");
141
+ return;
142
+ }
@@ -0,0 +1,32 @@
1
+ #File Generated by Hans J. Johnson
2
+ #Please contact hans-johnson@uiowa.edu for making enhancments/corrections
3
+ PROJECT(FSLIOLIB)
4
+
5
+ INCLUDE_DIRECTORIES(${NIFTILIB_SOURCE_DIR} ${ZNZLIB_SOURCE_DIR})
6
+ SET(FSLIOLIB_SRC fslio.c)
7
+
8
+ # Michael Hanke 2004-04-25:
9
+ # Restructure the file to match those of niftilib and znzlib
10
+ SET(NIFTI_FSLIOLIB_NAME ${PACKAGE_PREFIX}fslio)
11
+
12
+ ADD_LIBRARY(${NIFTI_FSLIOLIB_NAME} ${FSLIOLIB_SRC} )
13
+ TARGET_LINK_LIBRARIES( ${NIFTI_FSLIOLIB_NAME} ${PACKAGE_PREFIX}niftiio)
14
+
15
+ # Set lib version when buildung shared libs.
16
+ IF (BUILD_SHARED_LIBS)
17
+ SET_TARGET_PROPERTIES(${NIFTI_FSLIOLIB_NAME} PROPERTIES ${NIFTI_LIBRARY_PROPERTIES})
18
+ ENDIF (BUILD_SHARED_LIBS)
19
+
20
+ IF(NOT NIFTI_INSTALL_NO_LIBRARIES)
21
+ INSTALL(TARGETS ${NIFTI_FSLIOLIB_NAME}
22
+ RUNTIME DESTINATION ${NIFTI_INSTALL_BIN_DIR} COMPONENT RuntimeLibraries
23
+ LIBRARY DESTINATION ${NIFTI_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
24
+ ARCHIVE DESTINATION ${NIFTI_INSTALL_LIB_DIR} COMPONENT Development)
25
+ ENDIF(NOT NIFTI_INSTALL_NO_LIBRARIES)
26
+
27
+ IF(NOT NIFTI_INSTALL_NO_DEVELOPMENT)
28
+ FILE(GLOB __files "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
29
+ INSTALL(FILES ${__files}
30
+ DESTINATION ${NIFTI_INSTALL_INCLUDE_DIR}
31
+ COMPONENT Development)
32
+ ENDIF(NOT NIFTI_INSTALL_NO_DEVELOPMENT)
@@ -0,0 +1,29 @@
1
+ include ../Makefile
2
+
3
+
4
+ PROJNAME = fslio
5
+
6
+ INCFLAGS = $(ZLIB_INC) $(ZNZ_INC) $(NIFTI_INC)
7
+ LIBS = $(ZLIB_LIBS) $(ZNZ_LIBS) $(NIFTI_LIBS) $(FSL_LIBS)
8
+
9
+ SRCS=fslio.c
10
+ OBJS=fslio.o
11
+
12
+ SCRIPTS = remove_ext fsloutputtype imtest imglob imcp imln imrm immv
13
+
14
+
15
+
16
+ depend:
17
+ $(RM) -f depend.mk
18
+ $(MAKE) depend.mk
19
+
20
+ depend.mk:
21
+ $(CC) $(DEPENDFLAGS) $(INCFLAGS) $(SRCS) >> depend.mk
22
+
23
+ lib: libfslio.a
24
+
25
+ libfslio.a: ${OBJS}
26
+ ${AR} -r libfslio.a ${OBJS}
27
+
28
+
29
+ include depend.mk
@@ -0,0 +1,2426 @@
1
+ /*
2
+ fslio.c (Input and output routines for images in FSL)
3
+
4
+ Mark Jenkinson
5
+ FMRIB Image Analysis Group
6
+ */
7
+
8
+
9
+
10
+ /*
11
+
12
+ The fslio.c file was originally part of FSL - FMRIB's Software Library
13
+ http://www.fmrib.ox.ac.uk/fsl
14
+ fslio.c has now been placed in the public domain.
15
+
16
+ Developed at FMRIB (Oxford Centre for Functional Magnetic Resonance
17
+ Imaging of the Brain), Department of Clinical Neurology, Oxford
18
+ University, Oxford, UK
19
+
20
+ */
21
+
22
+ /** \file fslio.c
23
+ \brief Main collection of FSL i/o routines, written by Mark Jenkinson, FMRIB
24
+
25
+ - updates by Rick Reynolds, SSCC, NIMH
26
+ */
27
+
28
+ #include "fslio.h"
29
+ #include "assert.h"
30
+
31
+ static int FslIgnoreMFQ=0;
32
+ static int FslOverrideOutputType=-1;
33
+
34
+ #define FSLIOERR(x) { fprintf(stderr,"Error:: %s\n",(x)); fflush(stderr); exit(EXIT_FAILURE); }
35
+
36
+
37
+ /************************************************************
38
+ * FslFileTypeString
39
+ ************************************************************/
40
+ /*! \fn char* FslFileTypeString(int filetype)
41
+ \brief Return a string describing the format of the dataset
42
+ \param filetype FSL data format code. Legal values are as defined
43
+ by FSL_TYPE.
44
+ \return A string with the data format name, e.g. "ANALYZE-7.5"
45
+ \sa FSL_TYPE
46
+ */
47
+ char* FslFileTypeString(int filetype)
48
+ {
49
+ if (filetype==FSL_TYPE_ANALYZE) return "ANALYZE-7.5";
50
+ if (filetype==FSL_TYPE_NIFTI) return "NIFTI-1+";
51
+ if (filetype==FSL_TYPE_NIFTI_PAIR) return "NIFTI-1";
52
+ if (filetype==FSL_TYPE_ANALYZE_GZ) return "ANALYZE-7.5";
53
+ if (filetype==FSL_TYPE_NIFTI_GZ) return "NIFTI-1+";
54
+ if (filetype==FSL_TYPE_NIFTI_PAIR_GZ) return "NIFTI-1";
55
+ return "UNKNOWN";
56
+ }
57
+
58
+
59
+ int FslIsValidFileType(int filetype)
60
+ {
61
+ if ( (filetype!=FSL_TYPE_ANALYZE) && (filetype!=FSL_TYPE_ANALYZE_GZ) &&
62
+ (filetype!=FSL_TYPE_NIFTI) && (filetype!=FSL_TYPE_NIFTI_GZ) &&
63
+ (filetype!=FSL_TYPE_NIFTI_PAIR) && (filetype!=FSL_TYPE_NIFTI_PAIR_GZ) &&
64
+ (filetype!=FSL_TYPE_MINC) && (filetype!=FSL_TYPE_MINC_GZ) ) {
65
+ fprintf(stderr,"Error: unrecognised file type: %d\n",filetype);
66
+ return 0;
67
+ }
68
+ return 1;
69
+ }
70
+
71
+
72
+ int FslBaseFileType(int filetype)
73
+ {
74
+ /* returns -1 to indicate error - unrecognised filetype */
75
+ if ( (filetype==FSL_TYPE_ANALYZE_GZ) || (filetype==FSL_TYPE_ANALYZE) )
76
+ return FSL_TYPE_ANALYZE;
77
+ if ( (filetype==FSL_TYPE_NIFTI_GZ) || (filetype==FSL_TYPE_NIFTI) )
78
+ return FSL_TYPE_NIFTI;
79
+ if ( (filetype==FSL_TYPE_NIFTI_PAIR_GZ) || (filetype==FSL_TYPE_NIFTI_PAIR) )
80
+ return FSL_TYPE_NIFTI_PAIR;
81
+ if ( (filetype==FSL_TYPE_MINC_GZ) || (filetype==FSL_TYPE_MINC) )
82
+ return FSL_TYPE_MINC;
83
+ fprintf(stderr,"Error: unrecognised file type (%d)\n",filetype);
84
+ return -1;
85
+ }
86
+
87
+
88
+ int FslGetFileType2(const FSLIO *fslio, int quiet)
89
+ {
90
+ FSLIO *mutablefslio;
91
+ if (fslio==NULL) FSLIOERR("FslGetFileType: Null pointer passed for FSLIO");
92
+ if ( (fslio->file_mode==FSL_TYPE_MINC) || (fslio->file_mode==FSL_TYPE_MINC_GZ) ) {
93
+ return fslio->file_mode;
94
+ }
95
+ if ( !FslIsValidFileType(fslio->file_mode) ) return -1;
96
+
97
+ if (fslio->niftiptr!=NULL) { /* check that it is nifti_type and filetype are consistent */
98
+ if (fslio->niftiptr->nifti_type != FslBaseFileType(fslio->file_mode)) {
99
+ if (!quiet) {
100
+ fprintf(stderr,"Warning: nifti structure and fsl structure disagree on file type\n");
101
+ fprintf(stderr,"nifti = %d and fslio = %d\n",fslio->niftiptr->nifti_type,fslio->file_mode);
102
+ }
103
+ mutablefslio = (FSLIO *) fslio; /* dodgy and will generate warnings */
104
+ mutablefslio->niftiptr->nifti_type = FslBaseFileType(fslio->file_mode);
105
+ return fslio->file_mode;
106
+ }
107
+ }
108
+ return fslio->file_mode;
109
+ }
110
+
111
+ int FslGetFileType(const FSLIO *fslio)
112
+ {
113
+ return FslGetFileType2(fslio,0);
114
+ }
115
+
116
+
117
+
118
+ void FslSetFileType(FSLIO *fslio, int filetype)
119
+ {
120
+ if (fslio==NULL) FSLIOERR("FslSetFileType: Null pointer passed for FSLIO");
121
+ if ( (filetype==FSL_TYPE_MINC) || (filetype==FSL_TYPE_MINC_GZ) ) {
122
+ fslio->file_mode = filetype;
123
+ return;
124
+ }
125
+ if (! FslIsValidFileType(filetype)) { return; }
126
+ fslio->file_mode = filetype; /* indicates general nifti - details in niftiptr */
127
+ if (fslio->niftiptr!=NULL) {
128
+ fslio->niftiptr->nifti_type = FslBaseFileType(filetype);
129
+ nifti_set_iname_offset(fslio->niftiptr);
130
+ }
131
+ }
132
+
133
+
134
+
135
+ int FslIsSingleFileType(int filetype)
136
+ {
137
+ if ( (filetype==FSL_TYPE_NIFTI) || (filetype==FSL_TYPE_NIFTI_GZ) ||
138
+ (filetype==FSL_TYPE_MINC) || (filetype==FSL_TYPE_MINC_GZ) )
139
+ return 1;
140
+ return 0;
141
+ }
142
+
143
+
144
+ int FslIsCompressedFileType(int filetype)
145
+ {
146
+ if ( filetype >=100 ) return 1;
147
+ return 0;
148
+ }
149
+
150
+
151
+ int FslGetWriteMode(const FSLIO *fslio)
152
+ {
153
+ if (fslio==NULL) FSLIOERR("FslGetWriteMode: Null pointer passed for FSLIO");
154
+ return fslio->write_mode;
155
+ }
156
+
157
+
158
+ void FslSetWriteMode(FSLIO *fslio, int mode)
159
+ {
160
+ if (fslio==NULL) FSLIOERR("FslSetWriteMode: Null pointer passed for FSLIO");
161
+ fslio->write_mode = mode;
162
+ }
163
+
164
+
165
+ int FslGetEnvOutputType(void)
166
+ {
167
+ /* return type is one of FSL_TYPE_* or -1 to indicate error */
168
+ char *otype;
169
+ if (FslOverrideOutputType>=0) return FslOverrideOutputType;
170
+ otype = getenv("FSLOUTPUTTYPE");
171
+ if (otype == NULL) {
172
+ fprintf(stderr,"ERROR:: Environment variable FSLOUTPUTTYPE is not set!\n");
173
+ fprintf(stderr,"Please make sure that the appropriate configuration file is sourced by your shell (e.g. by putting it in .profile).\n");
174
+ fprintf(stderr,"e.g. bash or sh users add the line \". ${FSLDIR}/etc/fslconf/fsl.sh\"\n");
175
+ fprintf(stderr,"e.g. tcsh or csh users add the line \"source ${FSLDIR}/etc/fslconf/fsl.csh\"\n");
176
+ exit(EXIT_FAILURE);
177
+ }
178
+ if (strcmp(otype,"ANALYZE")==0) { return FSL_TYPE_ANALYZE; }
179
+ if (strcmp(otype,"ANALYZE_GZ")==0) { return FSL_TYPE_ANALYZE_GZ; }
180
+ if (strcmp(otype,"NIFTI")==0) { return FSL_TYPE_NIFTI; }
181
+ if (strcmp(otype,"NIFTI_GZ")==0) { return FSL_TYPE_NIFTI_GZ; }
182
+ if (strcmp(otype,"NIFTI_PAIR")==0) { return FSL_TYPE_NIFTI_PAIR; }
183
+ if (strcmp(otype,"NIFTI_PAIR_GZ")==0) { return FSL_TYPE_NIFTI_PAIR_GZ; }
184
+ if (strcmp(otype,"MINC")==0) { return FSL_TYPE_MINC; }
185
+ if (strcmp(otype,"MINC_GZ")==0) { return FSL_TYPE_MINC_GZ; }
186
+ fprintf(stderr,"ERROR:: Unrecognised value (%s) of environment variable FSLOUTPUT\n",otype);
187
+ fprintf(stderr,"Legal values are: ANALYZE, NIFTI, NIFTI_PAIR, MINC, ANALYZE_GZ, NIFTI_GZ, NIFTI_PAIR_GZ, MINC_GZ\n");
188
+ exit(EXIT_FAILURE);
189
+ return -1;
190
+ }
191
+
192
+
193
+ int FslFileType(const char* fname)
194
+ {
195
+ /* return type is FSL_TYPE_* or -1 to indicate undetermined */
196
+ /* use name as first priority but if that is ambiguous then resolve using environment */
197
+ int flen;
198
+ int retval=-1;
199
+ if (fname==NULL) return retval;
200
+ flen = strlen(fname);
201
+ /* debian@onerussian.com had to group conditions to avoid possible
202
+ * illegal memory read-ins */
203
+ if (flen<5) return retval; /* smallest name + extension is a.nii */
204
+ if (strcmp(fname + flen - 4,".nii")==0) retval=FSL_TYPE_NIFTI;
205
+ if (strcmp(fname + flen - 4,".mnc")==0) retval=FSL_TYPE_MINC;
206
+ if (strcmp(fname + flen - 4,".hdr")==0) retval=FSL_TYPE_NIFTI_PAIR;
207
+ if (strcmp(fname + flen - 4,".img")==0) retval=FSL_TYPE_NIFTI_PAIR;
208
+ if ((retval==-1) && (flen<8)) return retval; /* small name + ext.gz is a.nii.gz */
209
+ if (strcmp(fname + flen - 7,".nii.gz")==0) retval=FSL_TYPE_NIFTI_GZ;
210
+ if (strcmp(fname + flen - 7,".mnc.gz")==0) retval=FSL_TYPE_MINC;
211
+ if (strcmp(fname + flen - 7,".hdr.gz")==0) retval=FSL_TYPE_NIFTI_PAIR_GZ;
212
+ if (strcmp(fname + flen - 7,".img.gz")==0) retval=FSL_TYPE_NIFTI_PAIR_GZ;
213
+ if ( (retval==FSL_TYPE_NIFTI_PAIR) || (retval==FSL_TYPE_NIFTI_PAIR_GZ) ) {
214
+ /* If it was hdr or img, check if Analyze was requested by environment */
215
+ if ( (FslGetEnvOutputType() == FSL_TYPE_ANALYZE) && (retval == FSL_TYPE_NIFTI_PAIR) )
216
+ retval=FSL_TYPE_ANALYZE;
217
+ if ( (FslGetEnvOutputType() == FSL_TYPE_ANALYZE_GZ) && (retval == FSL_TYPE_NIFTI_PAIR_GZ) )
218
+ retval=FSL_TYPE_ANALYZE_GZ;
219
+ }
220
+ return retval;
221
+ }
222
+
223
+
224
+ /************************************************************
225
+ * FslGetReadFileType
226
+ ************************************************************/
227
+ /*! \fn int FslGetReadFileType(const FSLIO *fslio)
228
+ \brief return the best estimate of the true file type
229
+
230
+ This function is used to return the best estimate of the true file type once
231
+ a simple open has occurred - for now it is used after a nifti open call is made
232
+
233
+ \param fslio data structure
234
+ \return FSL_TYPE filetype code
235
+ \sa FSL_TYPE
236
+ */
237
+ int FslGetReadFileType(const FSLIO *fslio)
238
+ {
239
+ int filetype=FSL_TYPE_ANALYZE; /* unused default */
240
+ if (fslio==NULL) FSLIOERR("FslReadGetFileType: Null pointer passed for FSLIO");
241
+ /* Don't use fslio->file_mode as it hasn't been set yet */
242
+ if (fslio->niftiptr!=NULL) {
243
+ /* use the nifti_type and hdr or img name to determine the actual type */
244
+ if (fslio->niftiptr->nifti_type == FSL_TYPE_ANALYZE) {
245
+ if (FslIsCompressedFileType(FslFileType(fslio->niftiptr->iname))) {
246
+ filetype = FSL_TYPE_ANALYZE_GZ;
247
+ } else {
248
+ filetype = FSL_TYPE_ANALYZE;
249
+ }
250
+ }
251
+ if (fslio->niftiptr->nifti_type == FSL_TYPE_NIFTI_PAIR) {
252
+ if (FslIsCompressedFileType(FslFileType(fslio->niftiptr->iname))) {
253
+ filetype = FSL_TYPE_NIFTI_PAIR_GZ;
254
+ } else {
255
+ filetype = FSL_TYPE_NIFTI_PAIR;
256
+ }
257
+ }
258
+ if (fslio->niftiptr->nifti_type == FSL_TYPE_NIFTI) {
259
+ if (FslIsCompressedFileType(FslFileType(fslio->niftiptr->fname))) {
260
+ filetype = FSL_TYPE_NIFTI_GZ;
261
+ } else {
262
+ filetype = FSL_TYPE_NIFTI;
263
+ }
264
+ }
265
+
266
+ }
267
+ if (fslio->mincptr!=NULL) {
268
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
269
+ filetype = FSL_TYPE_MINC;
270
+ }
271
+ return filetype;
272
+ }
273
+
274
+
275
+ int FslFileExists(const char *filename)
276
+ {
277
+ /* return 1 if file(s) exists, otherwise return 0 */
278
+ char *hdrname = nifti_findhdrname(filename);
279
+ char *imgname = NULL;
280
+ if (hdrname!=NULL){
281
+ imgname = nifti_findimgname(filename,
282
+ FslBaseFileType(FslFileType(hdrname)));
283
+ free(hdrname);
284
+ if (imgname != NULL) { free(imgname); return 1; }
285
+ }
286
+ return 0;
287
+ }
288
+
289
+ char *FslMakeBaseName(const char *fname)
290
+ {
291
+ char *basename;
292
+ int blen;
293
+ basename = nifti_makebasename(fname);
294
+ blen = strlen(basename);
295
+ #ifdef HAVE_ZLIB
296
+ if ((blen>7) && (strcmp(basename + blen-7,".mnc.gz") == 0))
297
+ { basename[blen-7]='\0'; return basename; }
298
+ #endif
299
+ if ((blen>4) && (strcmp(basename + blen-4,".mnc") == 0))
300
+ { basename[blen-4]='\0'; return basename; }
301
+ return basename;
302
+ }
303
+
304
+
305
+ void FslGetHdrImgNames(const char* filename, const FSLIO* fslio,
306
+ char** hdrname, char** imgname)
307
+ {
308
+ char *basename;
309
+ int filetype;
310
+ basename = FslMakeBaseName(filename);
311
+ *hdrname = (char *)calloc(sizeof(char),strlen(basename)+8);
312
+ *imgname = (char *)calloc(sizeof(char),strlen(basename)+8);
313
+ strcpy(*hdrname,basename);
314
+ strcpy(*imgname,basename);
315
+ filetype = FslGetFileType(fslio);
316
+ if (filetype==FSL_TYPE_NIFTI_GZ) {
317
+ strcat(*hdrname,".nii.gz");
318
+ strcat(*imgname,".nii.gz");
319
+ free(basename);
320
+ return;
321
+ }
322
+ if (filetype==FSL_TYPE_NIFTI) {
323
+ strcat(*hdrname,".nii");
324
+ strcat(*imgname,".nii");
325
+ free(basename);
326
+ return;
327
+ }
328
+ if (filetype==FSL_TYPE_MINC_GZ) {
329
+ strcat(*hdrname,".mnc.gz");
330
+ strcat(*imgname,".mnc.gz");
331
+ free(basename);
332
+ return;
333
+ }
334
+ if (filetype==FSL_TYPE_MINC) {
335
+ strcat(*hdrname,".mnc");
336
+ strcat(*imgname,".mnc");
337
+ free(basename);
338
+ return;
339
+ }
340
+ if ( (filetype==FSL_TYPE_NIFTI_PAIR_GZ) || (filetype==FSL_TYPE_ANALYZE_GZ) ) {
341
+ strcat(*hdrname,".hdr.gz");
342
+ strcat(*imgname,".img.gz");
343
+ free(basename);
344
+ return;
345
+ }
346
+ if ( (filetype==FSL_TYPE_NIFTI_PAIR) || (filetype==FSL_TYPE_ANALYZE) ) {
347
+ strcat(*hdrname,".hdr");
348
+ strcat(*imgname,".img");
349
+ free(basename);
350
+ return;
351
+ }
352
+
353
+ fprintf(stderr,"Error: Unrecognised filetype (%d)\n",FslGetFileType(fslio));
354
+ free(basename);
355
+
356
+ /* Failure */
357
+ *hdrname = NULL;
358
+ *imgname = NULL;
359
+ }
360
+
361
+
362
+
363
+ /***************************************************************
364
+ * FslInit()
365
+ ***************************************************************/
366
+ /*! \fn FSLIO *FslInit()
367
+ \brief allocate space for the FSLIO struct and set some sensible defaults
368
+ \return A pointer to an initialized FSLIO data structure
369
+ */
370
+ FSLIO *FslInit(void)
371
+ {
372
+ FSLIO *fslio;
373
+ fslio = (FSLIO *) calloc(1,sizeof(FSLIO));
374
+ FslSetInit(fslio);
375
+ return fslio;
376
+ }
377
+
378
+ void FslSetInit(FSLIO* fslio)
379
+ {
380
+ /* set some sensible defaults */
381
+ fslio->niftiptr = NULL;
382
+ fslio->mincptr = NULL;
383
+ FslSetFileType(fslio,FslGetEnvOutputType());
384
+ FslSetWriteMode(fslio,0);
385
+ fslio->written_hdr = 0;
386
+ }
387
+
388
+
389
+
390
+ void FslInit4Write(FSLIO* fslio, const char* filename, int ft)
391
+ {
392
+ /* ft determines filetype if ft>=0*/
393
+ int imgtype;
394
+
395
+ FslSetWriteMode(fslio,1);
396
+
397
+ /* Determine file type from image name (first priority) or environment (default) */
398
+ imgtype = FslFileType(filename);
399
+ if (imgtype<0) imgtype = FslGetEnvOutputType();
400
+
401
+ if (ft >= 0) imgtype = ft;
402
+
403
+ if (!FslIsValidFileType(imgtype)) {
404
+ fprintf(stderr,"Error: Failed to determine file type for writing in FslOpen()\n");
405
+ exit(EXIT_FAILURE);
406
+ }
407
+
408
+ if ( (FslBaseFileType(imgtype)!=FSL_TYPE_MINC) ) {
409
+ FslInitHeader(fslio, NIFTI_TYPE_FLOAT32,
410
+ 1, 1, 1, 3, 0.0, 0.0, 0.0, 0.0, 4, "mm");
411
+
412
+ FslSetFileType(fslio,imgtype); /* this is after InitHeader as niftiptr set there */
413
+
414
+ /* determine the header and image filename */
415
+ FslGetHdrImgNames(filename,fslio,&(fslio->niftiptr->fname),&(fslio->niftiptr->iname));
416
+ if ( (fslio->niftiptr->fname == NULL) || (fslio->niftiptr->iname == NULL) ) {
417
+ fprintf(stderr,"Error: cannot find filenames for %s\n",filename);
418
+ }
419
+
420
+ } else if (FslBaseFileType(imgtype)==FSL_TYPE_MINC) {
421
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
422
+ return;
423
+ } else {
424
+ fprintf(stderr,"Error:: unrecognised image type requested\n");
425
+ return;
426
+ }
427
+
428
+ return;
429
+ }
430
+
431
+
432
+
433
+ void FslInitHeader(FSLIO *fslio, short t,
434
+ size_t x, size_t y, size_t z, size_t v,
435
+ float vx, float vy, float vz, float tr,
436
+ size_t dim,
437
+ const char* units)
438
+ {
439
+ /* NB: This function does not set the file type or write mode*/
440
+
441
+ if (fslio==NULL) FSLIOERR("FslInitHeader: Null pointer passed for FSLIO");
442
+
443
+ fslio->niftiptr = nifti_simple_init_nim();
444
+ /* make nifti type consistent with fslio */
445
+ fslio->niftiptr->nifti_type = FslBaseFileType(fslio->file_mode);
446
+
447
+ fslio->mincptr = NULL;
448
+
449
+ FslSetDataType(fslio,t);
450
+ FslSetDim(fslio,x,y,z,v);
451
+ FslSetVoxDim(fslio,vx,vy,vz,tr);
452
+ FslSetTimeUnits(fslio,"s");
453
+ FslSetDimensionality(fslio,dim);
454
+ }
455
+
456
+
457
+ void FslCloneHeader(FSLIO *dest, const FSLIO *src)
458
+ {
459
+ /* only clone the information that is stored in the disk version of the header */
460
+ /* - therefore _not_ the filenames, output type, write mode, etc */
461
+
462
+ char *fname=NULL, *iname=NULL;
463
+ void *data=NULL;
464
+ int filetype, writemode;
465
+ int preserve_nifti_values = 0;
466
+ if (dest==NULL) FSLIOERR("FslCloneHeader: Null pointer passed for FSLIO");
467
+ if (src==NULL) FSLIOERR("FslCloneHeader: Null pointer passed for FSLIO");
468
+
469
+ if (src->niftiptr!=NULL) {
470
+ /* preserve the filenames, output type and write mode */
471
+ if (dest->niftiptr != NULL) {
472
+ fname = dest->niftiptr->fname;
473
+ iname = dest->niftiptr->iname;
474
+ data = dest->niftiptr->data;
475
+ preserve_nifti_values = 1;
476
+ }
477
+ filetype = FslGetFileType2(dest,1);
478
+ writemode = FslGetWriteMode(dest);
479
+
480
+ /* copy _all_ info across */
481
+ dest->niftiptr = nifti_copy_nim_info(src->niftiptr);
482
+
483
+ /* restore old values */
484
+ if (preserve_nifti_values) {
485
+ dest->niftiptr->data = data;
486
+ } else {
487
+ /* destroy the values that the nifti copy creates */
488
+ free(dest->niftiptr->fname);
489
+ free(dest->niftiptr->iname);
490
+ nifti_free_extensions(dest->niftiptr);
491
+
492
+ dest->niftiptr->fname = NULL;
493
+ dest->niftiptr->iname = NULL;
494
+ dest->niftiptr->data = NULL; /* should already be NULL */
495
+ }
496
+ FslSetFileType(dest,filetype);
497
+ FslSetWriteMode(dest,writemode);
498
+ }
499
+
500
+ if (src->mincptr!=NULL) {
501
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
502
+ }
503
+
504
+ }
505
+
506
+
507
+ int fsl_fileexists(const char* fname)
508
+ {
509
+ znzFile fp;
510
+ fp = znzopen( fname , "rb" , 1 ) ;
511
+ if( !znz_isnull(fp) ) { znzclose(fp); return 1; }
512
+ return 0;
513
+ }
514
+
515
+
516
+ int FslCheckForMultipleFileNames(const char* filename)
517
+ {
518
+ char *basename, *tmpname;
519
+ int singlecount=0, hdrcount=0, imgcount=0, ambiguous=0;
520
+ basename = nifti_makebasename(filename);
521
+ tmpname = (char *)calloc(strlen(basename) + 10,sizeof(char));
522
+
523
+ strcpy(tmpname,basename);
524
+ strcat(tmpname,".nii");
525
+ if (fsl_fileexists(tmpname)) { singlecount++; }
526
+ strcpy(tmpname,basename);
527
+ strcat(tmpname,".nii.gz");
528
+ if (fsl_fileexists(tmpname)) { singlecount++; }
529
+ strcpy(tmpname,basename);
530
+ strcat(tmpname,".mnc");
531
+ if (fsl_fileexists(tmpname)) { singlecount++; }
532
+ strcpy(tmpname,basename);
533
+ strcat(tmpname,".mnc.gz");
534
+ if (fsl_fileexists(tmpname)) { singlecount++; }
535
+
536
+ strcpy(tmpname,basename);
537
+ strcat(tmpname,".img");
538
+ if (fsl_fileexists(tmpname)) { imgcount++; }
539
+ strcpy(tmpname,basename);
540
+ strcat(tmpname,".img.gz");
541
+ if (fsl_fileexists(tmpname)) { imgcount++; }
542
+
543
+ strcpy(tmpname,basename);
544
+ strcat(tmpname,".hdr");
545
+ if (fsl_fileexists(tmpname)) { hdrcount++; }
546
+ strcpy(tmpname,basename);
547
+ strcat(tmpname,".hdr.gz");
548
+ if (fsl_fileexists(tmpname)) { hdrcount++; }
549
+
550
+ ambiguous = 1;
551
+ if ( (hdrcount==1) && (imgcount==1) && (singlecount==0) ) { ambiguous=0; }
552
+ if ( (hdrcount==0) && (imgcount==0) && (singlecount==1) ) { ambiguous=0; }
553
+
554
+ /* treat no image found as not ambiguous - want opening errors instead */
555
+ if ( (hdrcount==0) && (imgcount==0) && (singlecount==0) ) { ambiguous=0; }
556
+
557
+ free(tmpname);
558
+ free(basename);
559
+ return ambiguous;
560
+ }
561
+
562
+
563
+
564
+ int check_for_multiple_filenames(const char* filename)
565
+ {
566
+ char *basename, *tmpname;
567
+ char *otype;
568
+ if (FslCheckForMultipleFileNames(filename))
569
+ { /* take action */
570
+ basename = nifti_makebasename(filename);
571
+ tmpname = (char *)calloc(strlen(basename) + 10,sizeof(char));
572
+ fprintf(stderr,"\n\n\nWARNING!!!! Multiple image files detected:\n");
573
+ /* list the offending files */
574
+ strcpy(tmpname,basename);
575
+ strcat(tmpname,".nii");
576
+ if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
577
+ strcpy(tmpname,basename);
578
+ strcat(tmpname,".nii.gz");
579
+ if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
580
+ strcpy(tmpname,basename);
581
+ strcat(tmpname,".mnc");
582
+ if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
583
+ strcpy(tmpname,basename);
584
+ strcat(tmpname,".mnc.gz");
585
+ if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
586
+ strcpy(tmpname,basename);
587
+ strcat(tmpname,".img");
588
+ if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
589
+ strcpy(tmpname,basename);
590
+ strcat(tmpname,".img.gz");
591
+ if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
592
+ strcpy(tmpname,basename);
593
+ strcat(tmpname,".hdr");
594
+ if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
595
+ strcpy(tmpname,basename);
596
+ strcat(tmpname,".hdr.gz");
597
+ if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
598
+ fprintf(stderr,"\n\n");
599
+
600
+ if (!FslIgnoreMFQ) {
601
+ otype = getenv("FSLMULTIFILEQUIT");
602
+ if (otype!=NULL) {
603
+ fprintf(stderr,"STOPPING PROGRAM\n");
604
+ exit(EXIT_FAILURE);
605
+ }
606
+ }
607
+ return 1;
608
+ }
609
+ return 0;
610
+ }
611
+
612
+
613
+
614
+ /***************************************************************
615
+ * FslOpen
616
+ ***************************************************************/
617
+ /*! \fn FSLIO *FslOpen(const char *filename, const char *opts)
618
+ \brief Opens a file for either reading or writing.
619
+
620
+ The format of the output dataset is determined automatically by
621
+ passing filetype -1 to FslXOpen.
622
+ \sa FslXOpen
623
+ */
624
+ FSLIO *FslOpen(const char *filename, const char *opts)
625
+ {
626
+ /* Note: -1 for filetype indicates that FslXOpen should determine filetype for itself */
627
+ return FslXOpen(filename,opts,-1);
628
+ }
629
+
630
+
631
+ /***************************************************************
632
+ * FslXOpen
633
+ ***************************************************************/
634
+ /*! \fn FSLIO *FslXOpen(const char *filename, const char *opts, int filetype)
635
+ \brief Opens a file for either reading or writing
636
+
637
+ Files to be read are automatically read whether
638
+ compressed or not. Also, reading uses the file extension
639
+ and will fail if that file does not exist.
640
+ For a more robust read, pass the basename in as then all types
641
+ will be tried.
642
+ \param filename Name (or basename) of the file to open
643
+ \param opts Flags for fopen() of dataset, eg "r", "wb", etc.
644
+ \param filetype specifies the type of file to be written. Legal
645
+ values are as defined by FSL_TYPE. If filetype is less than
646
+ zero, then it is ignored and the type is determined by the
647
+ filename extension or, failing that, the environment default.
648
+ \return pointer to FSLIO dataset datastructure
649
+ \sa FSLIO
650
+ \sa FSL_TYPE
651
+ */
652
+ FSLIO *FslXOpen(const char *filename, const char *opts, int filetype)
653
+ {
654
+
655
+ FSLIO *fslio;
656
+ char bopts[1024];
657
+ size_t i, bi;
658
+ int imgtype;
659
+
660
+ fslio = FslInit();
661
+
662
+ bi=0;
663
+ for(i=0;i<strlen(opts);i++) {
664
+ if (opts[i]=='w') { FslSetWriteMode(fslio,1); }
665
+ if (opts[i]!='b' && opts[i]!='t') { bopts[bi++]=opts[i]; }
666
+ }
667
+ /* add in 'b' (at the end) for windows compatibility */
668
+ bopts[bi++]='b';
669
+ bopts[bi]='\0';
670
+
671
+
672
+ if (FslGetWriteMode(fslio)==1) {
673
+
674
+ /** ====================== Open file for writing ====================== **/
675
+
676
+ FslInit4Write(fslio,filename,filetype);
677
+ imgtype = FslGetFileType(fslio);
678
+ fslio->written_hdr = 0;
679
+
680
+ /* open the image file - not the header */
681
+ fslio->fileptr = znzopen(fslio->niftiptr->iname,bopts,FslIsCompressedFileType(imgtype));
682
+ if (znz_isnull(fslio->fileptr)) {
683
+ fprintf(stderr,"Error: failed to open file %s\n",fslio->niftiptr->iname);
684
+ return NULL;
685
+ }
686
+
687
+ if (!FslIsSingleFileType(imgtype)) {
688
+ /* set up pointer at end of iname_offset for dual file formats (not singles) */
689
+ FslSeekVolume(fslio,0);
690
+ }
691
+ return fslio;
692
+
693
+ }
694
+
695
+
696
+
697
+ /** ======================== Open file for reading ====================== **/
698
+
699
+ check_for_multiple_filenames(filename);
700
+
701
+ /* see if the extension indicates a minc file */
702
+ imgtype = FslFileType(filename);
703
+ if ((imgtype>=0) && (FslBaseFileType(imgtype)==FSL_TYPE_MINC)) {
704
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
705
+ return NULL;
706
+ }
707
+
708
+ /* otherwise open nifti file: read header and open img file (may be same file) */
709
+ fslio->fileptr = nifti_image_open(filename,bopts,&(fslio->niftiptr));
710
+ if (znz_isnull(fslio->fileptr)) {
711
+ fprintf(stderr,"Error: failed to open file %s\n",filename);
712
+ return NULL;
713
+ }
714
+
715
+ /* set the file type given what has been read - it uses nifti_type and filenames */
716
+ imgtype = FslGetReadFileType(fslio);
717
+ FslSetFileType(fslio,imgtype);
718
+ FslSetWriteMode(fslio,0);
719
+
720
+ if (FslBaseFileType(FslGetFileType(fslio))==FSL_TYPE_ANALYZE) {
721
+ /* For the ANALYZE case in FSL, must cheat and grab the originator field! */
722
+ /* Note that the header file is always separate here and closed by now */
723
+ struct dsr ahdr;
724
+ short orig[5];
725
+ FslReadRawHeader(&ahdr,fslio->niftiptr->fname);
726
+ if (fslio->niftiptr->byteorder != nifti_short_order()) {
727
+ AvwSwapHeader(&ahdr);
728
+ }
729
+ /* Read the origin and set the sform up (if origin is non-zero) */
730
+ /* Note that signed pixdims are passed in to set the LR orientation */
731
+ memcpy(orig,&(ahdr.hist.originator),10);
732
+ FslSetAnalyzeSform(fslio, orig, fslio->niftiptr->pixdim[1],
733
+ fslio->niftiptr->pixdim[2], fslio->niftiptr->pixdim[3]);
734
+ }
735
+
736
+ /* from now on force all vox dims to be positive - LR info is in sform */
737
+ if (fslio->niftiptr!=NULL) {
738
+ fslio->niftiptr->dx = fabs(fslio->niftiptr->dx);
739
+ fslio->niftiptr->dy = fabs(fslio->niftiptr->dy);
740
+ fslio->niftiptr->dz = fabs(fslio->niftiptr->dz);
741
+ fslio->niftiptr->pixdim[1] = fabs(fslio->niftiptr->pixdim[1]);
742
+ fslio->niftiptr->pixdim[2] = fabs(fslio->niftiptr->pixdim[2]);
743
+ fslio->niftiptr->pixdim[3] = fabs(fslio->niftiptr->pixdim[3]);
744
+ }
745
+ /* set up pointer at end of iname_offset , ready for reading */
746
+ FslSeekVolume(fslio,0);
747
+
748
+ return fslio;
749
+
750
+ }
751
+
752
+
753
+
754
+ /***************************************************************
755
+ * FslReadAllVolumes
756
+ ***************************************************************/
757
+ /*! \fn void* FslReadAllVolumes(FSLIO* fslio, char* filename)
758
+ \brief Read the header and all data into the FSLIO structure
759
+
760
+ There is no need for FslOpen or FslClose calls when FslReadAllVolumes()
761
+ is called.
762
+ <br>This routine allocates the buffer to hold the entire dataset.
763
+ <br>The data block returned will contain the data in whatever
764
+ datatype it is stored as on disk (therefore it is a void *).
765
+ <br>The data buffer will be byteswapped to native-endian.
766
+ <br>The data buffer will not be scaled.
767
+ <br>The best call to make before this is FslInit() or a calloc() for
768
+ fslio. (??? why calloc if this allocates the buffer ???)
769
+
770
+ \param fslio pointer to an open dataset
771
+ \param filename Name of the dataset to read.
772
+ \return A pointer to the data block buffer (allocated by this function).
773
+ <br> Return Null on error ??? is this true ???
774
+ <ul>
775
+ <li>Note this pointer is also in the FSLIO structure as
776
+ fslio->niftiptr->data.</li>
777
+ <li>Note a void pointer is returned, as the datablock is of
778
+ variable datatype.</li>
779
+ </ul>
780
+
781
+ */
782
+ void* FslReadAllVolumes(FSLIO* fslio, char* filename)
783
+ {
784
+
785
+ int imgtype;
786
+ if (fslio==NULL) FSLIOERR("FslReadAllVolumes: Null pointer passed for FSLIO");
787
+
788
+ /* see if the extension indicates a minc file */
789
+ imgtype = FslFileType(filename);
790
+ if ((imgtype>=0) && (FslBaseFileType(imgtype)==FSL_TYPE_MINC)) {
791
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
792
+ return NULL;
793
+ }
794
+
795
+ /** otherwise it is a nifti file - so read it! **/
796
+ fslio->mincptr = NULL;
797
+ /* make sure an FslOpen hasn't locked the file */
798
+ if (!znz_isnull(fslio->fileptr)) FslClose(fslio);
799
+
800
+ fslio->niftiptr = nifti_image_read(filename,1);
801
+
802
+ /* check for failure, from David Akers */
803
+ if (fslio->niftiptr == NULL) {
804
+ FSLIOERR("FslReadAllVolumes: error reading NIfTI image");
805
+ return(NULL);
806
+ }
807
+
808
+ FslSetFileType(fslio,fslio->niftiptr->nifti_type);
809
+ FslSetWriteMode(fslio,0);
810
+ return fslio->niftiptr->data;
811
+ }
812
+
813
+
814
+
815
+ /***************************************************************
816
+ * FslReadVolumes
817
+ ***************************************************************/
818
+ /*! \fn size_t FslReadVolumes(FSLIO *fslio, void *buffer, size_t nvols)
819
+ \brief Read the first nvols Volumes from a 4D dataset
820
+
821
+ \param fslio pointer to open dataset
822
+ \param buffer buffer to read data into, allocated by ???
823
+ \param nvols number of volumes to read
824
+ \return Number of volumes read.
825
+ */
826
+ size_t FslReadVolumes(FSLIO *fslio, void *buffer, size_t nvols)
827
+ {
828
+ int volbytes;
829
+ size_t retval=0;
830
+ if (fslio==NULL) FSLIOERR("FslReadVolumes: Null pointer passed for FSLIO");
831
+ if (znz_isnull(fslio->fileptr)) FSLIOERR("FslReadVolumes: Null file pointer");
832
+ if (fslio->niftiptr!=NULL) {
833
+ fslio->niftiptr->data = buffer;
834
+ volbytes = FslGetVolSize(fslio) * fslio->niftiptr->nbyper;
835
+ retval = nifti_read_buffer(fslio->fileptr,fslio->niftiptr->data,nvols*volbytes,fslio->niftiptr);
836
+ retval /= volbytes;
837
+ }
838
+
839
+ if (fslio->mincptr!=NULL) {
840
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
841
+ }
842
+ return retval;
843
+ }
844
+
845
+
846
+
847
+ /***************************************************************
848
+ * FslWriteAllVolumes
849
+ ***************************************************************/
850
+ /*! \fn void FslWriteAllVolumes(FSLIO *fslio, const void *buffer)
851
+ \brief Writes all data from buffer (using size info from fslio) to file.
852
+
853
+ Dimension and datatype of buffer are as is specified in nifti_image structure
854
+ fslio->niftiptr.
855
+ Note: If file format is Analyze (not nifti) and in Neurological order then
856
+ SWAP DATA into Radiological order.
857
+
858
+ \param fslio pointer to open dataset
859
+ \param buffer pointer to data array. Size and datatype of this buffer
860
+ */
861
+ void FslWriteAllVolumes(FSLIO *fslio, const void *buffer)
862
+ {
863
+ short x,y,z,t;
864
+
865
+ if (fslio==NULL) FSLIOERR("FslWriteAllVolumes: Null pointer passed for FSLIO");
866
+
867
+ FslGetDim(fslio,&x,&y,&z,&t);
868
+ FslWriteHeader(fslio);
869
+ FslWriteVolumes(fslio,buffer,t);
870
+ return;
871
+ }
872
+
873
+
874
+
875
+ /***************************************************************
876
+ * FslWriteVolumes
877
+ ***************************************************************/
878
+ /*! \fn size_t FslWriteVolumes(FSLIO *fslio, const void *buffer, size_t nvols)
879
+ \brief Write the first nvols volumes in buffer to disk.
880
+
881
+ Dimension and datatype of buffer are as is specified in nifti_image structure
882
+ fslio->niftiptr.
883
+ Note: If file format is Analyze (not nifti) and in Neurological order then
884
+ SWAP DATA into Radiological order.
885
+
886
+
887
+ \param fslio pointer to open dataset
888
+ \param buffer pointer to data array. Size and datatype of this buffer
889
+ \param nvols number of volumes to write
890
+ \return ??? looks like return of retval is missing ??? 0 on error.
891
+ */
892
+ size_t FslWriteVolumes(FSLIO *fslio, const void *buffer, size_t nvols)
893
+ {
894
+ /* The dimensions and datatype must be set before calling this function */
895
+ int retval;
896
+ if (fslio==NULL) FSLIOERR("FslWriteVolumes: Null pointer passed for FSLIO");
897
+ if ( (!fslio->written_hdr) && (FslIsSingleFileType(FslGetFileType(fslio))) &&
898
+ (FslIsCompressedFileType(FslGetFileType(fslio))) )
899
+ { FSLIOERR("FslWriteVolumes: header must be written before data for single compressed file types"); }
900
+
901
+ if (fslio->niftiptr!=NULL) {
902
+ long int nbytes, bpv;
903
+ bpv = fslio->niftiptr->nbyper; /* bytes per voxel */
904
+ nbytes = nvols * FslGetVolSize(fslio) * bpv;
905
+
906
+ if ( (FslBaseFileType(FslGetFileType(fslio))==FSL_TYPE_ANALYZE)
907
+ && (FslGetLeftRightOrder(fslio)==FSL_NEUROLOGICAL) ) {
908
+ /* If it is Analyze and Neurological order then SWAP DATA into Radiological order */
909
+ /* This is nasty - but what else can be done?!? */
910
+ char *tmpbuf, *inbuf;
911
+ long int x, b, n, nrows;
912
+ short nx, ny, nz, nv;
913
+ inbuf = (char *) buffer;
914
+ tmpbuf = (char *)calloc(nbytes,1);
915
+ FslGetDim(fslio,&nx,&ny,&nz,&nv);
916
+ nrows = nbytes / (nx * bpv);
917
+ for (n=0; n<nrows; n++) {
918
+ for (x=0; x<nx; x++) {
919
+ for (b=0; b<bpv; b++) {
920
+ tmpbuf[b + bpv * (n*nx + nx - 1 - x)] = inbuf[b + bpv * (n*nx + x)];
921
+ }
922
+ }
923
+ }
924
+ retval = nifti_write_buffer(fslio->fileptr, tmpbuf, nbytes);
925
+ free(tmpbuf);
926
+ } else {
927
+ retval = nifti_write_buffer(fslio->fileptr, buffer, nbytes);
928
+ }
929
+ }
930
+ if (fslio->mincptr!=NULL) {
931
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
932
+ }
933
+ return 0; /* failure */
934
+ }
935
+
936
+
937
+ /***************************************************************
938
+ * FslWriteHeader
939
+ ***************************************************************/
940
+ /*! \fn void FslWriteHeader(FSLIO *fslio)
941
+ \brief Writes nifti/anz header and opens img file ready for writing
942
+
943
+ \param fslio pointer to open dataset
944
+ */
945
+ void FslWriteHeader(FSLIO *fslio)
946
+ {
947
+ /* writes header and opens img file ready for writing */
948
+ if (fslio==NULL) FSLIOERR("FslWriteHeader: Null pointer passed for FSLIO");
949
+
950
+ if (fslio->niftiptr!=NULL) {
951
+ fslio->written_hdr = 1;
952
+ if (znz_isnull(fslio->fileptr)) FSLIOERR("FslWriteHeader: no file opened!");
953
+
954
+ strcpy(fslio->niftiptr->descrip,"FSL3.2beta");
955
+ if (FslIsSingleFileType(FslGetFileType(fslio))) {
956
+ /* write header info but don't close the file */
957
+ nifti_image_write_hdr_img2(fslio->niftiptr,2,"wb",fslio->fileptr,NULL);
958
+ /* set up pointer at end of iname_offset for single files only */
959
+ FslSeekVolume(fslio,0);
960
+ } else {
961
+ /* open a new hdr file, write it and close it */
962
+ nifti_image_write_hdr_img(fslio->niftiptr,0,"wb");
963
+ }
964
+ }
965
+
966
+ if (fslio->mincptr!=NULL) {
967
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
968
+ }
969
+ return;
970
+ }
971
+
972
+
973
+ /***************************************************************
974
+ * FslReadSliceSeries
975
+ ***************************************************************/
976
+ /*! \fn size_t FslReadSliceSeries(FSLIO *fslio, void *buffer, short slice, size_t nvols)
977
+ \brief Read one slice from each of the first nvols volumes in the dataset, ie get an xyt buffer.
978
+
979
+ Dimension and datatype of buffer are as is specified in nifti_image structure
980
+ fslio->niftiptr.
981
+ Note: filepointer in file data array is restored to its initial position.
982
+
983
+ \param fslio pointer to open dataset
984
+ \param buffer buffer large enough to hold 1 slice from each volume
985
+ \param slice slice number (0 based) to read [0 z-1]
986
+ \param nvols number of volumes to read a slice from
987
+ \return Number of volumes from which a slice was successfully read. 0 on error.
988
+ */
989
+ size_t FslReadSliceSeries(FSLIO *fslio, void *buffer, short slice, size_t nvols)
990
+ {
991
+ size_t slbytes,volbytes;
992
+ size_t n, orig_offset;
993
+ short x,y,z,v,type;
994
+
995
+ if (fslio==NULL) FSLIOERR("FslReadSliceSeries: Null pointer passed for FSLIO");
996
+ if (fslio->niftiptr!=NULL) {
997
+
998
+ FslGetDim(fslio,&x,&y,&z,&v);
999
+
1000
+ if ((slice<0) || (slice>=z)) FSLIOERR("FslReadSliceSeries: slice outside valid range");
1001
+
1002
+ slbytes = x * y * (FslGetDataType(fslio, &type) / 8);
1003
+ volbytes = slbytes * z;
1004
+
1005
+ orig_offset = znztell(fslio->fileptr);
1006
+ znzseek(fslio->fileptr, slbytes*slice, SEEK_CUR);
1007
+
1008
+ for (n=0; n<nvols; n++) {
1009
+ if (n>0) znzseek(fslio->fileptr, volbytes - slbytes, SEEK_CUR);
1010
+ if (znzread((char *)buffer+n*slbytes, 1, slbytes, fslio->fileptr) != slbytes)
1011
+ FSLIOERR("FslReadSliceSeries: failed to read values");
1012
+ if (fslio->niftiptr->byteorder != nifti_short_order())
1013
+ nifti_swap_Nbytes(slbytes / fslio->niftiptr->swapsize,
1014
+ fslio->niftiptr->swapsize, (char *)buffer+n*slbytes);
1015
+ }
1016
+
1017
+
1018
+ /* restore file pointer to original position */
1019
+ znzseek(fslio->fileptr,orig_offset,SEEK_SET);
1020
+ return n;
1021
+ }
1022
+ if (fslio->mincptr!=NULL) {
1023
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1024
+ }
1025
+ return 0;
1026
+ }
1027
+
1028
+
1029
+ /***************************************************************
1030
+ * FslReadRowSeries
1031
+ ***************************************************************/
1032
+ /*! \fn size_t FslReadRowSeries(FSLIO *fslio, void *buffer, short row, short slice, size_t nvols)
1033
+ \brief Read one row from one slice for first nvols volumes in dataset; ie get an xt buffer.
1034
+
1035
+ Dimension and datatype of buffer are as is specified in nifti_image structure
1036
+ fslio->niftiptr.
1037
+ Note: filepointer in file data array is restored to its initial position.
1038
+
1039
+ \param fslio pointer to open dataset
1040
+ \param buffer buffer to hold one row from each volume.
1041
+ \param row row number (0 based) to read [0 y-1]
1042
+ \param slice slice number (0 based) to read
1043
+ \param nvols number of volumes to read a row from
1044
+ \return Number of volumes from which a row was successfully read. 0 on error.
1045
+ */
1046
+ size_t FslReadRowSeries(FSLIO *fslio, void *buffer, short row, short slice, size_t nvols)
1047
+ {
1048
+ size_t rowbytes,slbytes,volbytes;
1049
+ size_t n, orig_offset;
1050
+ short x,y,z,v,type;
1051
+
1052
+ if (fslio==NULL) FSLIOERR("FslReadRowSeries: Null pointer passed for FSLIO");
1053
+ if (fslio->niftiptr!=NULL) {
1054
+
1055
+ FslGetDim(fslio,&x,&y,&z,&v);
1056
+
1057
+ if ((slice<0) || (slice>=z)) FSLIOERR("FslReadRowSeries: slice outside valid range");
1058
+ if ((row<0) || (row>=y)) FSLIOERR("FslReadRowSeries: row outside valid range");
1059
+
1060
+ rowbytes = x * (FslGetDataType(fslio, &type)) / 8;
1061
+ slbytes = rowbytes * y;
1062
+ volbytes = slbytes * z;
1063
+
1064
+ orig_offset = znztell(fslio->fileptr);
1065
+ znzseek(fslio->fileptr, rowbytes*row + slbytes*slice, SEEK_CUR);
1066
+
1067
+ for (n=0; n<nvols; n++){
1068
+ if (n>0) znzseek(fslio->fileptr, volbytes - rowbytes, SEEK_CUR);
1069
+ if (znzread((char *)buffer+n*rowbytes, 1, rowbytes, fslio->fileptr) != rowbytes)
1070
+ FSLIOERR("FslReadRowSeries: failed to read values");
1071
+ if (fslio->niftiptr->byteorder != nifti_short_order())
1072
+ nifti_swap_Nbytes(rowbytes / fslio->niftiptr->swapsize,
1073
+ fslio->niftiptr->swapsize, (char *)buffer+n*rowbytes);
1074
+ }
1075
+
1076
+ /* restore file pointer to original position */
1077
+ znzseek(fslio->fileptr,orig_offset,SEEK_SET);
1078
+ return n;
1079
+ }
1080
+ if (fslio->mincptr!=NULL) {
1081
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1082
+ }
1083
+ return 0;
1084
+ }
1085
+
1086
+
1087
+ /***************************************************************
1088
+ * FslReadTimeSeries
1089
+ ***************************************************************/
1090
+ /*! \fn size_t FslReadTimeSeries(FSLIO *fslio, void *buffer, short xVox, short yVox, short zVox, size_t nvols)
1091
+ \brief Read one voxel (xyz location) from first nvols volumes in dataset; ie get a t dim buffer.
1092
+
1093
+ Dimension and datatype of buffer are as is specified in nifti_image structure
1094
+ fslio->niftiptr.
1095
+ Note: filepointer in file data array is restored to its initial position.
1096
+
1097
+ \param fslio pointer to open dataset
1098
+ \param buffer buffer to hold one timeseries vector
1099
+ \param xVox x voxel [0 x-1]
1100
+ \param yVox y voxel [0 y-1]
1101
+ \param zVox z voxel [0 z-1]
1102
+ \param nvols number of volumes to read a voxel from
1103
+ \return Number of volumes from which a voxel was successfully read. 0 on error.
1104
+ */
1105
+ size_t FslReadTimeSeries(FSLIO *fslio, void *buffer, short xVox, short yVox, short zVox,
1106
+ size_t nvols)
1107
+ {
1108
+ size_t volbytes, offset, orig_offset;
1109
+ size_t n;
1110
+ short xdim,ydim,zdim,v,wordsize;
1111
+
1112
+ if (fslio==NULL) FSLIOERR("FslReadTimeSeries: Null pointer passed for FSLIO");
1113
+ if (fslio->niftiptr!=NULL) {
1114
+
1115
+ FslGetDim(fslio,&xdim,&ydim,&zdim,&v);
1116
+
1117
+ if ((xVox<0) || (xVox >=xdim)) FSLIOERR("FslReadTimeSeries: voxel outside valid range");
1118
+ if ((yVox<0) || (yVox >=ydim)) FSLIOERR("FslReadTimeSeries: voxel outside valid range");
1119
+ if ((zVox<0) || (zVox >=zdim)) FSLIOERR("FslReadTimeSeries: voxel outside valid range");
1120
+
1121
+ wordsize = fslio->niftiptr->nbyper;
1122
+ volbytes = xdim * ydim * zdim * wordsize;
1123
+
1124
+ orig_offset = znztell(fslio->fileptr);
1125
+ offset = ((ydim * zVox + yVox) * xdim + xVox) * wordsize;
1126
+ znzseek(fslio->fileptr,offset,SEEK_CUR);
1127
+
1128
+ for (n=0; n<nvols; n++) {
1129
+ if (n>0) znzseek(fslio->fileptr, volbytes - wordsize, SEEK_CUR);
1130
+ if (znzread((char *)buffer+(n*wordsize), 1, wordsize,fslio->fileptr) != wordsize)
1131
+ FSLIOERR("FslReadTimeSeries: failed to read values");
1132
+ if (fslio->niftiptr->byteorder != nifti_short_order())
1133
+ nifti_swap_Nbytes(1,fslio->niftiptr->swapsize,
1134
+ (char *)buffer+(n*wordsize));
1135
+ }
1136
+
1137
+ /* restore file pointer to original position */
1138
+ znzseek(fslio->fileptr,orig_offset,SEEK_SET);
1139
+ return n;
1140
+
1141
+ }
1142
+ if (fslio->mincptr!=NULL) {
1143
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1144
+ }
1145
+ return 0;
1146
+ }
1147
+
1148
+
1149
+ size_t FslReadCplxVolumes(FSLIO *fslio, void *buffer, size_t nvols, char mode)
1150
+ {
1151
+ if (fslio==NULL) FSLIOERR("FslReadCplxVolumes: Null pointer passed for FSLIO");
1152
+ fprintf(stderr,"Warning:: FslReadCplxVolumes is not yet supported\n");
1153
+ return 0;
1154
+ }
1155
+
1156
+ size_t FslWriteCplxVolumes(FSLIO *fslio, void *buffer, size_t nvols, char mode)
1157
+ {
1158
+ if (fslio==NULL) FSLIOERR("FslWriteCplxVolumes: Null pointer passed for FSLIO");
1159
+ fprintf(stderr,"Warning:: FslWriteCplxVolumes is not yet supported\n");
1160
+ return 0;
1161
+ }
1162
+
1163
+ int FslSeekVolume(FSLIO *fslio, size_t vols)
1164
+ {
1165
+ int offset;
1166
+ if (fslio==NULL) FSLIOERR("FslSeekVolume: Null pointer passed for FSLIO");
1167
+ if (fslio->niftiptr!=NULL) {
1168
+ offset = fslio->niftiptr->iname_offset +
1169
+ vols * FslGetVolSize(fslio) * fslio->niftiptr->nbyper;
1170
+ if (znz_isnull(fslio->fileptr)) FSLIOERR("FslSeekVolume: Null file pointer");
1171
+ return znzseek(fslio->fileptr,offset,SEEK_SET);
1172
+ }
1173
+ if (fslio->mincptr!=NULL) {
1174
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1175
+ }
1176
+ return 0;
1177
+ }
1178
+
1179
+
1180
+ size_t FslGetVolSize(FSLIO *fslio)
1181
+ {
1182
+ /* returns number of voxels per 3D volume */
1183
+ if (fslio==NULL) FSLIOERR("FslGetVolSize: Null pointer passed for FSLIO");
1184
+ if (fslio->niftiptr!=NULL) {
1185
+ return (fslio->niftiptr->nx * fslio->niftiptr->ny * fslio->niftiptr->nz);
1186
+ }
1187
+ if (fslio->mincptr!=NULL) {
1188
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1189
+ }
1190
+ return 0;
1191
+ }
1192
+
1193
+
1194
+ void FslSetDim(FSLIO *fslio, short x, short y, short z, short v)
1195
+ {
1196
+ int ndim;
1197
+ if (fslio==NULL) FSLIOERR("FslSetDim: Null pointer passed for FSLIO");
1198
+ if (fslio->niftiptr!=NULL) {
1199
+
1200
+ ndim=4;
1201
+ if (v<=1) {ndim--; if (z<=1) {ndim--; if (y<=1) {ndim--; if (x<=1) {ndim--;}}}}
1202
+
1203
+ fslio->niftiptr->ndim = ndim;
1204
+
1205
+ if (x>=1) fslio->niftiptr->nx = x; else fslio->niftiptr->nx=1;
1206
+ if (y>=1) fslio->niftiptr->ny = y; else fslio->niftiptr->ny=1;
1207
+ if (z>=1) fslio->niftiptr->nz = z; else fslio->niftiptr->nz=1;
1208
+ if (v>=1) fslio->niftiptr->nt = v; else fslio->niftiptr->nt=1;
1209
+ fslio->niftiptr->nu = 1;
1210
+ fslio->niftiptr->nv = 1;
1211
+ fslio->niftiptr->nw = 1;
1212
+
1213
+ /* deal with stupid redundancies */
1214
+ fslio->niftiptr->dim[0] = fslio->niftiptr->ndim ;
1215
+ fslio->niftiptr->dim[1] = fslio->niftiptr->nx;
1216
+ fslio->niftiptr->dim[2] = fslio->niftiptr->ny;
1217
+ fslio->niftiptr->dim[3] = fslio->niftiptr->nz;
1218
+ fslio->niftiptr->dim[4] = fslio->niftiptr->nt;
1219
+ fslio->niftiptr->dim[5] = fslio->niftiptr->nu;
1220
+ fslio->niftiptr->dim[6] = fslio->niftiptr->nv;
1221
+ fslio->niftiptr->dim[7] = fslio->niftiptr->nw;
1222
+
1223
+ fslio->niftiptr->nvox = fslio->niftiptr->nx * fslio->niftiptr->ny * fslio->niftiptr->nz
1224
+ * fslio->niftiptr->nt * fslio->niftiptr->nu * fslio->niftiptr->nv * fslio->niftiptr->nw ;
1225
+
1226
+ }
1227
+ if (fslio->mincptr!=NULL) {
1228
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1229
+ }
1230
+ }
1231
+
1232
+
1233
+ void FslGetDim(FSLIO *fslio, short *x, short *y, short *z, short *v)
1234
+ {
1235
+ if (fslio==NULL) FSLIOERR("FslGetDim: Null pointer passed for FSLIO");
1236
+ if (fslio->niftiptr!=NULL) {
1237
+ *x = fslio->niftiptr->nx;
1238
+ *y = fslio->niftiptr->ny;
1239
+ *z = fslio->niftiptr->nz;
1240
+ *v = fslio->niftiptr->nt;
1241
+ }
1242
+ if (fslio->mincptr!=NULL) {
1243
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1244
+ }
1245
+ }
1246
+
1247
+
1248
+ void FslSetDimensionality(FSLIO *fslio, size_t dim)
1249
+ {
1250
+ if (fslio==NULL) FSLIOERR("FslSetDimensionality: Null pointer passed for FSLIO");
1251
+ if (fslio->niftiptr!=NULL) {
1252
+ fslio->niftiptr->ndim = dim;
1253
+ fslio->niftiptr->dim[0] = dim;
1254
+ }
1255
+ if (fslio->mincptr!=NULL) {
1256
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1257
+ }
1258
+ }
1259
+
1260
+
1261
+ void FslGetDimensionality(FSLIO *fslio, size_t *dim)
1262
+ {
1263
+ if (fslio==NULL) FSLIOERR("FslGetDimensionality: Null pointer passed for FSLIO");
1264
+ if (fslio->niftiptr!=NULL) {
1265
+ *dim = fslio->niftiptr->ndim;
1266
+ }
1267
+ if (fslio->mincptr!=NULL) {
1268
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1269
+ }
1270
+ }
1271
+
1272
+
1273
+ void FslSetVoxDim(FSLIO *fslio, float x, float y, float z, float tr)
1274
+ {
1275
+ if (fslio==NULL) FSLIOERR("FslSetVoxDim: Null pointer passed for FSLIO");
1276
+ if (fslio->niftiptr!=NULL) {
1277
+ fslio->niftiptr->dx = fabs(x);
1278
+ fslio->niftiptr->dy = fabs(y);
1279
+ fslio->niftiptr->dz = fabs(z);
1280
+ fslio->niftiptr->dt = fabs(tr);
1281
+ fslio->niftiptr->pixdim[1] = fabs(x);
1282
+ fslio->niftiptr->pixdim[2] = fabs(y);
1283
+ fslio->niftiptr->pixdim[3] = fabs(z);
1284
+ fslio->niftiptr->pixdim[4] = fabs(tr);
1285
+ /* set the units to mm and seconds */
1286
+ fslio->niftiptr->xyz_units = NIFTI_UNITS_MM;
1287
+ fslio->niftiptr->time_units = NIFTI_UNITS_SEC;
1288
+ }
1289
+ if (fslio->mincptr!=NULL) {
1290
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1291
+ }
1292
+ }
1293
+
1294
+
1295
+ void FslGetVoxDim(FSLIO *fslio, float *x, float *y, float *z, float *tr)
1296
+ {
1297
+ if (fslio==NULL) FSLIOERR("FslGetVoxDim: Null pointer passed for FSLIO");
1298
+ if (fslio->niftiptr!=NULL) {
1299
+ *x = fabs(fslio->niftiptr->dx);
1300
+ *y = fabs(fslio->niftiptr->dy);
1301
+ *z = fabs(fslio->niftiptr->dz);
1302
+ *tr = fabs(fslio->niftiptr->dt);
1303
+ /* now check the units and convert to mm and sec */
1304
+ if (fslio->niftiptr->xyz_units == NIFTI_UNITS_METER)
1305
+ { *x *= 1000.0; *y *= 1000.0; *z *= 1000.0; }
1306
+ if (fslio->niftiptr->xyz_units == NIFTI_UNITS_MICRON)
1307
+ { *x /= 1000.0; *y /= 1000.0; *z /= 1000.0; }
1308
+ if (fslio->niftiptr->xyz_units == NIFTI_UNITS_MSEC)
1309
+ { *tr /= 1000.0; }
1310
+ if (fslio->niftiptr->xyz_units == NIFTI_UNITS_USEC)
1311
+ { *tr /= 1000000.0; }
1312
+ /* if it is Hz or other frequency then leave it */
1313
+ }
1314
+ if (fslio->mincptr!=NULL) {
1315
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1316
+ }
1317
+ }
1318
+
1319
+
1320
+ void FslGetCalMinMax(FSLIO *fslio, float *min, float *max)
1321
+ {
1322
+ if (fslio==NULL) FSLIOERR("FslGetCalMinMax: Null pointer passed for FSLIO");
1323
+ if (fslio->niftiptr!=NULL) {
1324
+ *min = fslio->niftiptr->cal_min;
1325
+ *max = fslio->niftiptr->cal_max;
1326
+ }
1327
+ if (fslio->mincptr!=NULL) {
1328
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1329
+ }
1330
+ }
1331
+
1332
+
1333
+ void FslSetCalMinMax(FSLIO *fslio, float min, float max)
1334
+ {
1335
+ if (fslio==NULL) FSLIOERR("FslSetCalMinMax: Null pointer passed for FSLIO");
1336
+ if (fslio->niftiptr!=NULL) {
1337
+ fslio->niftiptr->cal_min = min;
1338
+ fslio->niftiptr->cal_max = max;
1339
+ }
1340
+ if (fslio->mincptr!=NULL) {
1341
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1342
+ }
1343
+ }
1344
+
1345
+
1346
+ void FslGetAuxFile(FSLIO *fslio,char *aux_file)
1347
+ {
1348
+ if (fslio==NULL) FSLIOERR("FslGetAuxFile: Null pointer passed for FSLIO");
1349
+ if (fslio->niftiptr!=NULL) {
1350
+ strncpy(aux_file,fslio->niftiptr->aux_file, 24);
1351
+ aux_file[23] = '\0';
1352
+ }
1353
+ if (fslio->mincptr!=NULL) {
1354
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1355
+ }
1356
+ }
1357
+
1358
+
1359
+ void FslSetAuxFile(FSLIO *fslio,const char *aux_file)
1360
+ {
1361
+ if (fslio==NULL) FSLIOERR("FslSetAuxFile: Null pointer passed for FSLIO");
1362
+ if (fslio->niftiptr!=NULL) {
1363
+ strncpy(fslio->niftiptr->aux_file, aux_file, 24);
1364
+ }
1365
+ if (fslio->mincptr!=NULL) {
1366
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1367
+ }
1368
+ }
1369
+
1370
+
1371
+ void FslSetVoxUnits(FSLIO *fslio, const char *units)
1372
+ {
1373
+ int unitcode=0;
1374
+ if (fslio==NULL) FSLIOERR("FslSetVoxUnits: Null pointer passed for FSLIO");
1375
+ if (fslio->niftiptr!=NULL) {
1376
+ if (strcmp(units,nifti_units_string(NIFTI_UNITS_METER))==0) {
1377
+ unitcode = NIFTI_UNITS_METER;
1378
+ } else if (strcmp(units,nifti_units_string(NIFTI_UNITS_MM))==0) {
1379
+ unitcode = NIFTI_UNITS_MM;
1380
+ } else if (strcmp(units,nifti_units_string(NIFTI_UNITS_MICRON))==0) {
1381
+ unitcode = NIFTI_UNITS_MICRON;
1382
+ }
1383
+ fslio->niftiptr->xyz_units = unitcode;
1384
+ }
1385
+ if (fslio->mincptr!=NULL) {
1386
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1387
+ }
1388
+ }
1389
+
1390
+
1391
+ void FslGetVoxUnits(FSLIO *fslio, char *units)
1392
+ {
1393
+ if (fslio==NULL) FSLIOERR("FslGetVoxUnits: Null pointer passed for FSLIO");
1394
+ if (fslio->niftiptr!=NULL) {
1395
+ strcpy(units,nifti_units_string(fslio->niftiptr->xyz_units));
1396
+ }
1397
+ if (fslio->mincptr!=NULL) {
1398
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1399
+ }
1400
+ }
1401
+
1402
+ void FslSetTimeUnits(FSLIO *fslio, const char *units)
1403
+ {
1404
+ int unitcode=0;
1405
+ if (fslio==NULL) FSLIOERR("FslSetTimeUnits: Null pointer passed for FSLIO");
1406
+ if (fslio->niftiptr!=NULL) {
1407
+ if (strcmp(units,nifti_units_string(NIFTI_UNITS_HZ))==0) {
1408
+ unitcode = NIFTI_UNITS_HZ;
1409
+ } else if (strcmp(units,nifti_units_string(NIFTI_UNITS_PPM))==0) {
1410
+ unitcode = NIFTI_UNITS_PPM;
1411
+ } else if (strcmp(units,nifti_units_string(NIFTI_UNITS_RADS))==0) {
1412
+ unitcode = NIFTI_UNITS_RADS;
1413
+ } else if (strcmp(units,nifti_units_string(NIFTI_UNITS_SEC))==0) {
1414
+ unitcode = NIFTI_UNITS_SEC;
1415
+ } else if (strcmp(units,nifti_units_string(NIFTI_UNITS_MSEC))==0) {
1416
+ fprintf(stderr,"Warning::Setting time units to msec is not fully recommended in fslio\n");
1417
+ unitcode = NIFTI_UNITS_MSEC;
1418
+ } else if (strcmp(units,nifti_units_string(NIFTI_UNITS_USEC))==0) {
1419
+ fprintf(stderr,"Warning::Setting time units to msec is not fully recommended in fslio\n");
1420
+ unitcode = NIFTI_UNITS_USEC;
1421
+ }
1422
+ fslio->niftiptr->time_units = unitcode;
1423
+ }
1424
+ if (fslio->mincptr!=NULL) {
1425
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1426
+ }
1427
+ }
1428
+
1429
+
1430
+ void FslGetTimeUnits(FSLIO *fslio, char *units)
1431
+ {
1432
+ if (fslio==NULL) FSLIOERR("FslGetTimeUnits: Null pointer passed for FSLIO");
1433
+ if (fslio->niftiptr!=NULL) {
1434
+ strcpy(units,nifti_units_string(fslio->niftiptr->time_units));
1435
+ }
1436
+ if (fslio->mincptr!=NULL) {
1437
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1438
+ }
1439
+ }
1440
+
1441
+
1442
+ void FslSetDataType(FSLIO *fslio, short t)
1443
+ {
1444
+ int nbytepix=0, ss=0;
1445
+ if (fslio==NULL) FSLIOERR("FslSetDataType: Null pointer passed for FSLIO");
1446
+ if (fslio->niftiptr!=NULL) {
1447
+ fslio->niftiptr->datatype = t;
1448
+ nifti_datatype_sizes(t,&nbytepix,&ss);
1449
+ fslio->niftiptr->nbyper = nbytepix;
1450
+ }
1451
+ if (fslio->mincptr!=NULL) {
1452
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1453
+ }
1454
+ }
1455
+
1456
+ size_t FslGetDataType(FSLIO *fslio, short *t)
1457
+ {
1458
+ /* returns bits per pixel */
1459
+ int nbytepix=32, ss=0;
1460
+ if (fslio==NULL) FSLIOERR("FslGetDataType: Null pointer passed for FSLIO");
1461
+ if (fslio->niftiptr!=NULL) {
1462
+ *t = fslio->niftiptr->datatype;
1463
+ nifti_datatype_sizes(*t,&nbytepix,&ss);
1464
+ }
1465
+ if (fslio->mincptr!=NULL) {
1466
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1467
+ }
1468
+ return (size_t) 8 * nbytepix;
1469
+ }
1470
+
1471
+
1472
+ void FslGetMMCoord(mat44 stdmat, float voxx, float voxy, float voxz,
1473
+ float *mmx, float *mmy, float *mmz)
1474
+ {
1475
+ *mmx = stdmat.m[0][0] * voxx + stdmat.m[0][1] * voxy + stdmat.m[0][2] * voxz
1476
+ + stdmat.m[0][3];
1477
+ *mmy = stdmat.m[1][0] * voxx + stdmat.m[1][1] * voxy + stdmat.m[1][2] * voxz
1478
+ + stdmat.m[1][3];
1479
+ *mmz = stdmat.m[2][0] * voxx + stdmat.m[2][1] * voxy + stdmat.m[2][2] * voxz
1480
+ + stdmat.m[2][3];
1481
+ }
1482
+
1483
+
1484
+ void FslGetVoxCoord(mat44 stdmat, float mmx, float mmy, float mmz,
1485
+ float *voxx, float *voxy, float *voxz)
1486
+ {
1487
+ mat44 mm2vox;
1488
+
1489
+ mm2vox = nifti_mat44_inverse(stdmat);
1490
+ *voxx = mm2vox.m[0][0] * mmx + mm2vox.m[0][1] * mmy + mm2vox.m[0][2] * mmz
1491
+ + mm2vox.m[0][3];
1492
+ *voxy = mm2vox.m[1][0] * mmx + mm2vox.m[1][1] * mmy + mm2vox.m[1][2] * mmz
1493
+ + mm2vox.m[1][3];
1494
+ *voxz = mm2vox.m[2][0] * mmx + mm2vox.m[2][1] * mmy + mm2vox.m[2][2] * mmz
1495
+ + mm2vox.m[2][3];
1496
+ }
1497
+
1498
+
1499
+ void FslSetStdXform(FSLIO *fslio, short sform_code, mat44 stdmat)
1500
+ {
1501
+ /* NB: stdmat must point to a 4x4 array */
1502
+ if (fslio==NULL) FSLIOERR("FslSetStdXform: Null pointer passed for FSLIO");
1503
+ if (fslio->niftiptr!=NULL) {
1504
+ fslio->niftiptr->sform_code = sform_code;
1505
+ fslio->niftiptr->sto_xyz.m[0][0] = stdmat.m[0][0];
1506
+ fslio->niftiptr->sto_xyz.m[0][1] = stdmat.m[0][1];
1507
+ fslio->niftiptr->sto_xyz.m[0][2] = stdmat.m[0][2];
1508
+ fslio->niftiptr->sto_xyz.m[0][3] = stdmat.m[0][3];
1509
+ fslio->niftiptr->sto_xyz.m[1][0] = stdmat.m[1][0];
1510
+ fslio->niftiptr->sto_xyz.m[1][1] = stdmat.m[1][1];
1511
+ fslio->niftiptr->sto_xyz.m[1][2] = stdmat.m[1][2];
1512
+ fslio->niftiptr->sto_xyz.m[1][3] = stdmat.m[1][3];
1513
+ fslio->niftiptr->sto_xyz.m[2][0] = stdmat.m[2][0];
1514
+ fslio->niftiptr->sto_xyz.m[2][1] = stdmat.m[2][1];
1515
+ fslio->niftiptr->sto_xyz.m[2][2] = stdmat.m[2][2];
1516
+ fslio->niftiptr->sto_xyz.m[2][3] = stdmat.m[2][3];
1517
+ fslio->niftiptr->sto_xyz.m[3][0] = 0;
1518
+ fslio->niftiptr->sto_xyz.m[3][1] = 0;
1519
+ fslio->niftiptr->sto_xyz.m[3][2] = 0;
1520
+ fslio->niftiptr->sto_xyz.m[3][3] = 1;
1521
+ fslio->niftiptr->sto_ijk = nifti_mat44_inverse(fslio->niftiptr->sto_xyz);
1522
+ }
1523
+ if (fslio->mincptr!=NULL) {
1524
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1525
+ }
1526
+ }
1527
+
1528
+
1529
+ short FslGetStdXform(FSLIO *fslio, mat44 *stdmat)
1530
+ {
1531
+ /* returns sform code (NB: stdmat must point to a 4x4 array) */
1532
+ float dx,dy,dz,tr;
1533
+ if (fslio==NULL) FSLIOERR("FslGetStdXform: Null pointer passed for FSLIO");
1534
+ if (fslio->niftiptr!=NULL) {
1535
+ stdmat->m[0][0] = fslio->niftiptr->sto_xyz.m[0][0];
1536
+ stdmat->m[0][1] = fslio->niftiptr->sto_xyz.m[0][1];
1537
+ stdmat->m[0][2] = fslio->niftiptr->sto_xyz.m[0][2];
1538
+ stdmat->m[0][3] = fslio->niftiptr->sto_xyz.m[0][3];
1539
+ stdmat->m[1][0] = fslio->niftiptr->sto_xyz.m[1][0];
1540
+ stdmat->m[1][1] = fslio->niftiptr->sto_xyz.m[1][1];
1541
+ stdmat->m[1][2] = fslio->niftiptr->sto_xyz.m[1][2];
1542
+ stdmat->m[1][3] = fslio->niftiptr->sto_xyz.m[1][3];
1543
+ stdmat->m[2][0] = fslio->niftiptr->sto_xyz.m[2][0];
1544
+ stdmat->m[2][1] = fslio->niftiptr->sto_xyz.m[2][1];
1545
+ stdmat->m[2][2] = fslio->niftiptr->sto_xyz.m[2][2];
1546
+ stdmat->m[2][3] = fslio->niftiptr->sto_xyz.m[2][3];
1547
+ stdmat->m[3][0] = 0.0;
1548
+ stdmat->m[3][1] = 0.0;
1549
+ stdmat->m[3][2] = 0.0;
1550
+ stdmat->m[3][3] = 1.0;
1551
+
1552
+ /* the code below gives a default but it really should never be used */
1553
+ if (fslio->niftiptr->sform_code == NIFTI_XFORM_UNKNOWN) {
1554
+ FslGetVoxDim(fslio,&dx,&dy,&dz,&tr);
1555
+ stdmat->m[0][0] = -dx; /* default Radiological convention */
1556
+ stdmat->m[0][1] = 0;
1557
+ stdmat->m[0][2] = 0;
1558
+ stdmat->m[0][3] = 0;
1559
+ stdmat->m[1][0] = 0;
1560
+ stdmat->m[1][1] = dy;
1561
+ stdmat->m[1][2] = 0;
1562
+ stdmat->m[1][3] = 0;
1563
+ stdmat->m[2][0] = 0;
1564
+ stdmat->m[2][1] = 0;
1565
+ stdmat->m[2][2] = dz;
1566
+ stdmat->m[2][3] = 0;
1567
+ stdmat->m[3][0] = 0.0;
1568
+ stdmat->m[3][1] = 0.0;
1569
+ stdmat->m[3][2] = 0.0;
1570
+ stdmat->m[3][3] = 1.0;
1571
+ }
1572
+ return fslio->niftiptr->sform_code;
1573
+ }
1574
+ if (fslio->mincptr!=NULL) {
1575
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1576
+ }
1577
+ return NIFTI_XFORM_UNKNOWN;
1578
+ }
1579
+
1580
+
1581
+ void FslSetRigidXform(FSLIO *fslio, short qform_code, mat44 rigidmat)
1582
+ {
1583
+ /* NB: rigidmat must point to an allocated mat44 */
1584
+ float dx, dy, dz;
1585
+ if (fslio==NULL) FSLIOERR("FslSetRigidXform: Null pointer passed for FSLIO");
1586
+ if (fslio->niftiptr!=NULL) {
1587
+ fslio->niftiptr->qform_code = qform_code;
1588
+ fslio->niftiptr->qto_xyz.m[0][0] = rigidmat.m[0][0];
1589
+ fslio->niftiptr->qto_xyz.m[0][1] = rigidmat.m[0][1];
1590
+ fslio->niftiptr->qto_xyz.m[0][2] = rigidmat.m[0][2];
1591
+ fslio->niftiptr->qto_xyz.m[0][3] = rigidmat.m[0][3];
1592
+ fslio->niftiptr->qto_xyz.m[1][0] = rigidmat.m[1][0];
1593
+ fslio->niftiptr->qto_xyz.m[1][1] = rigidmat.m[1][1];
1594
+ fslio->niftiptr->qto_xyz.m[1][2] = rigidmat.m[1][2];
1595
+ fslio->niftiptr->qto_xyz.m[1][3] = rigidmat.m[1][3];
1596
+ fslio->niftiptr->qto_xyz.m[2][0] = rigidmat.m[2][0];
1597
+ fslio->niftiptr->qto_xyz.m[2][1] = rigidmat.m[2][1];
1598
+ fslio->niftiptr->qto_xyz.m[2][2] = rigidmat.m[2][2];
1599
+ fslio->niftiptr->qto_xyz.m[2][3] = rigidmat.m[2][3];
1600
+ fslio->niftiptr->qto_xyz.m[3][0] = 0;
1601
+ fslio->niftiptr->qto_xyz.m[3][1] = 0;
1602
+ fslio->niftiptr->qto_xyz.m[3][2] = 0;
1603
+ fslio->niftiptr->qto_xyz.m[3][3] = 1;
1604
+ nifti_mat44_to_quatern(
1605
+ fslio->niftiptr->qto_xyz,&(fslio->niftiptr->quatern_b),
1606
+ &(fslio->niftiptr->quatern_c),&(fslio->niftiptr->quatern_d),
1607
+ &(fslio->niftiptr->qoffset_x),&(fslio->niftiptr->qoffset_y),
1608
+ &(fslio->niftiptr->qoffset_z),&dx,&dy,&dz,&(fslio->niftiptr->qfac));
1609
+ fslio->niftiptr->qto_ijk = nifti_mat44_inverse(fslio->niftiptr->qto_xyz);
1610
+
1611
+ }
1612
+ if (fslio->mincptr!=NULL) {
1613
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1614
+ }
1615
+ }
1616
+
1617
+
1618
+ short FslGetRigidXform(FSLIO *fslio, mat44 *rigidmat)
1619
+ {
1620
+ /* returns qform code (NB: rigidmat must point to an allocated mat44) */
1621
+ float dx,dy,dz,tr;
1622
+ if (fslio==NULL) FSLIOERR("FslGetRigidXform: Null pointer passed for FSLIO");
1623
+ if (fslio->niftiptr!=NULL) {
1624
+ rigidmat->m[0][0] = fslio->niftiptr->qto_xyz.m[0][0];
1625
+ rigidmat->m[0][1] = fslio->niftiptr->qto_xyz.m[0][1];
1626
+ rigidmat->m[0][2] = fslio->niftiptr->qto_xyz.m[0][2];
1627
+ rigidmat->m[0][3] = fslio->niftiptr->qto_xyz.m[0][3];
1628
+ rigidmat->m[1][0] = fslio->niftiptr->qto_xyz.m[1][0];
1629
+ rigidmat->m[1][1] = fslio->niftiptr->qto_xyz.m[1][1];
1630
+ rigidmat->m[1][2] = fslio->niftiptr->qto_xyz.m[1][2];
1631
+ rigidmat->m[1][3] = fslio->niftiptr->qto_xyz.m[1][3];
1632
+ rigidmat->m[2][0] = fslio->niftiptr->qto_xyz.m[2][0];
1633
+ rigidmat->m[2][1] = fslio->niftiptr->qto_xyz.m[2][1];
1634
+ rigidmat->m[2][2] = fslio->niftiptr->qto_xyz.m[2][2];
1635
+ rigidmat->m[2][3] = fslio->niftiptr->qto_xyz.m[2][3];
1636
+ rigidmat->m[3][0] = 0.0;
1637
+ rigidmat->m[3][1] = 0.0;
1638
+ rigidmat->m[3][2] = 0.0;
1639
+ rigidmat->m[3][3] = 1.0;
1640
+
1641
+ /* the code gives a default but it should never really be used */
1642
+ if (fslio->niftiptr->sform_code == NIFTI_XFORM_UNKNOWN) {
1643
+ FslGetVoxDim(fslio,&dx,&dy,&dz,&tr);
1644
+ rigidmat->m[0][0] = dx;
1645
+ rigidmat->m[0][1] = 0;
1646
+ rigidmat->m[0][2] = 0;
1647
+ rigidmat->m[0][3] = 0;
1648
+ rigidmat->m[1][0] = 0;
1649
+ rigidmat->m[1][1] = dy;
1650
+ rigidmat->m[1][2] = 0;
1651
+ rigidmat->m[1][3] = 0;
1652
+ rigidmat->m[2][0] = 0;
1653
+ rigidmat->m[2][1] = 0;
1654
+ rigidmat->m[2][2] = dz;
1655
+ rigidmat->m[3][0] = 0.0;
1656
+ rigidmat->m[3][1] = 0.0;
1657
+ rigidmat->m[3][2] = 0.0;
1658
+ rigidmat->m[3][3] = 1.0;
1659
+ }
1660
+ return fslio->niftiptr->qform_code;
1661
+ }
1662
+ if (fslio->mincptr!=NULL) {
1663
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1664
+ }
1665
+ return NIFTI_XFORM_UNKNOWN;
1666
+ }
1667
+
1668
+
1669
+ void FslSetIntent(FSLIO *fslio, short intent_code, float p1, float p2, float p3)
1670
+ {
1671
+ if (fslio==NULL) FSLIOERR("FslSetIntent: Null pointer passed for FSLIO");
1672
+ if (fslio->niftiptr!=NULL) {
1673
+ fslio->niftiptr->intent_code = intent_code;
1674
+ fslio->niftiptr->intent_p1 = p1;
1675
+ fslio->niftiptr->intent_p2 = p2;
1676
+ fslio->niftiptr->intent_p3 = p3;
1677
+ }
1678
+ if (fslio->mincptr!=NULL) {
1679
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1680
+ }
1681
+ }
1682
+
1683
+
1684
+ short FslGetIntent(FSLIO *fslio, short *intent_code, float *p1, float *p2,
1685
+ float *p3)
1686
+ {
1687
+ /* also returns intent code */
1688
+ if (fslio==NULL) FSLIOERR("FslGetIntent: Null pointer passed for FSLIO");
1689
+ if (fslio->niftiptr!=NULL) {
1690
+ *intent_code = fslio->niftiptr->intent_code;
1691
+ *p1 = fslio->niftiptr->intent_p1;
1692
+ *p2 = fslio->niftiptr->intent_p2;
1693
+ *p3 = fslio->niftiptr->intent_p3;
1694
+ return fslio->niftiptr->intent_code;
1695
+ }
1696
+ if (fslio->mincptr!=NULL) {
1697
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1698
+ }
1699
+ return NIFTI_INTENT_NONE;
1700
+ }
1701
+
1702
+
1703
+
1704
+
1705
+ void FslSetIntensityScaling(FSLIO *fslio, float slope, float intercept)
1706
+ {
1707
+ if (fslio==NULL) FSLIOERR("FslSetIntensityScaling: Null pointer passed for FSLIO");
1708
+ if (fslio->niftiptr!=NULL) {
1709
+ fslio->niftiptr->scl_slope = slope;
1710
+ fslio->niftiptr->scl_inter = intercept;
1711
+ }
1712
+ if (fslio->mincptr!=NULL) {
1713
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1714
+ }
1715
+ }
1716
+
1717
+
1718
+ int FslGetIntensityScaling(FSLIO *fslio, float *slope, float *intercept)
1719
+ {
1720
+ /* returns 1 if scaling required or 0 otherwise */
1721
+ if (fslio==NULL) FSLIOERR("FslGetIntensityScaling: Null pointer passed for FSLIO");
1722
+ if (fslio->niftiptr!=NULL) {
1723
+ *slope = fslio->niftiptr->scl_slope;
1724
+ *intercept = fslio->niftiptr->scl_inter;
1725
+ if (fabs(*slope)<1e-30) {
1726
+ *slope = 1.0;
1727
+ *intercept = 0.0;
1728
+ return 0;
1729
+ }
1730
+ if ( (fabs(*slope - 1.0)>1e-30) || (fabs(*intercept)>1e-30) ) {
1731
+ return 1;
1732
+ } else {
1733
+ return 0;
1734
+ }
1735
+ }
1736
+ if (fslio->mincptr!=NULL) {
1737
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1738
+ }
1739
+ return 0;
1740
+
1741
+ }
1742
+
1743
+
1744
+ mat33 mat44_to_mat33(mat44 x)
1745
+ {
1746
+ mat33 y;
1747
+ int i,j;
1748
+ for (i=0; i<3; i++) {
1749
+ for (j=0; j<3; j++) {
1750
+ y.m[i][j] = x.m[i][j];
1751
+ }
1752
+ }
1753
+ return y;
1754
+ }
1755
+
1756
+
1757
+ int FslGetLeftRightOrder(FSLIO *fslio)
1758
+ {
1759
+ /* Determines if the image is stored in neurological or radiological convention */
1760
+ int order=FSL_RADIOLOGICAL, sform_code, qform_code;
1761
+ float det=-1.0;
1762
+ mat44 sform44, qform44;
1763
+ mat33 sform33, qform33;
1764
+ if (fslio==NULL) FSLIOERR("FslGetLeftRightOrder: Null pointer passed for FSLIO");
1765
+ if (fslio->niftiptr!=NULL) {
1766
+ sform_code = FslGetStdXform(fslio,&sform44);
1767
+ qform_code = FslGetRigidXform(fslio,&qform44);
1768
+ if (sform_code!=NIFTI_XFORM_UNKNOWN) {
1769
+ sform33 = mat44_to_mat33(sform44);
1770
+ det = nifti_mat33_determ(sform33);
1771
+ } else if (qform_code!=NIFTI_XFORM_UNKNOWN) {
1772
+ qform33 = mat44_to_mat33(qform44);
1773
+ det = nifti_mat33_determ(qform33);
1774
+ }
1775
+
1776
+ if (det<0.0) order=FSL_RADIOLOGICAL;
1777
+ else order=FSL_NEUROLOGICAL;
1778
+ }
1779
+ if (fslio->mincptr!=NULL) {
1780
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1781
+ }
1782
+ return order;
1783
+ }
1784
+
1785
+
1786
+
1787
+ void FslSetAnalyzeSform(FSLIO *fslio, const short *orig,
1788
+ float dx, float dy, float dz)
1789
+ {
1790
+ /* Creates an sform matrix for an Analyze file */
1791
+ /* THIS ALWAYS CREATES A RADIOLOGICAL ORDERED SFORM */
1792
+ /* NB: the origin passed in here is in Analyze convention - starting at 1, not 0 */
1793
+ float x, y, z;
1794
+ if (fslio==NULL) FSLIOERR("FslSetAnalyzeSform: Null pointer passed for FSLIO");
1795
+ if (fslio->niftiptr!=NULL) {
1796
+ if (FslBaseFileType(FslGetFileType(fslio))==FSL_TYPE_ANALYZE) {
1797
+ /* default case */
1798
+ fslio->niftiptr->sform_code = NIFTI_XFORM_UNKNOWN;
1799
+ }
1800
+ /* ignore all zero origins - really all serious coord stuff should
1801
+ be done via the FslSetStdCoord call */
1802
+ if ((orig[0]!=0) || (orig[1]!=0) || (orig[2]!=0))
1803
+ {
1804
+ short origx=0, origy=0, origz=0;
1805
+ if ((orig[0]!=0) || (orig[1]!=0) || (orig[2]!=0)) {
1806
+ /* convert to nifti conventions (start at 0 not 1) */
1807
+ origx = orig[0] - 1;
1808
+ origy = orig[1] - 1;
1809
+ origz = orig[2] - 1;
1810
+ }
1811
+ if ( dx * dy * dz > 0 ) {
1812
+ /* change neurological convention to radiological if necessary */
1813
+ dx = -dx;
1814
+ }
1815
+ if ( (FslBaseFileType(FslGetFileType(fslio))==FSL_TYPE_ANALYZE)
1816
+ || (fslio->niftiptr->sform_code == NIFTI_XFORM_UNKNOWN) ) {
1817
+ /* make a default transform with the requested origin at xyz=000 */
1818
+ fslio->niftiptr->sform_code = NIFTI_XFORM_ALIGNED_ANAT;
1819
+ fslio->niftiptr->sto_xyz.m[0][0] = dx;
1820
+ fslio->niftiptr->sto_xyz.m[0][1] = 0;
1821
+ fslio->niftiptr->sto_xyz.m[0][2] = 0;
1822
+ fslio->niftiptr->sto_xyz.m[0][3] = -(origx)*(dx);
1823
+ fslio->niftiptr->sto_xyz.m[1][0] = 0;
1824
+ fslio->niftiptr->sto_xyz.m[1][1] = dy;
1825
+ fslio->niftiptr->sto_xyz.m[1][2] = 0;
1826
+ fslio->niftiptr->sto_xyz.m[1][3] = -(origy)*(dy);
1827
+ fslio->niftiptr->sto_xyz.m[2][0] = 0;
1828
+ fslio->niftiptr->sto_xyz.m[2][1] = 0;
1829
+ fslio->niftiptr->sto_xyz.m[2][2] = dz;
1830
+ fslio->niftiptr->sto_xyz.m[2][3] = -(origz)*(dz);
1831
+ fslio->niftiptr->sto_xyz.m[3][0] = 0;
1832
+ fslio->niftiptr->sto_xyz.m[3][1] = 0;
1833
+ fslio->niftiptr->sto_xyz.m[3][2] = 0;
1834
+ fslio->niftiptr->sto_xyz.m[3][3] = 1;
1835
+ fslio->niftiptr->sto_ijk =
1836
+ nifti_mat44_inverse(fslio->niftiptr->sto_xyz);
1837
+ } else {
1838
+ /* update the existing origin */
1839
+ /* find out what the existing xyz of the requested origin is */
1840
+ x = fslio->niftiptr->sto_xyz.m[0][0] * origx
1841
+ + fslio->niftiptr->sto_xyz.m[0][1] * origy
1842
+ + fslio->niftiptr->sto_xyz.m[0][2] * origz
1843
+ + fslio->niftiptr->sto_xyz.m[0][3];
1844
+ y = fslio->niftiptr->sto_xyz.m[1][0] * origx
1845
+ + fslio->niftiptr->sto_xyz.m[1][1] * origy
1846
+ + fslio->niftiptr->sto_xyz.m[1][2] * origz
1847
+ + fslio->niftiptr->sto_xyz.m[1][3];
1848
+ z = fslio->niftiptr->sto_xyz.m[2][0] * origx
1849
+ + fslio->niftiptr->sto_xyz.m[2][1] * origy
1850
+ + fslio->niftiptr->sto_xyz.m[2][2] * origz
1851
+ + fslio->niftiptr->sto_xyz.m[2][3];
1852
+ /* subtract off whatever is currently the xyz of the origin */
1853
+ fslio->niftiptr->sto_xyz.m[0][3] -= x;
1854
+ fslio->niftiptr->sto_xyz.m[1][3] -= y;
1855
+ fslio->niftiptr->sto_xyz.m[2][3] -= z;
1856
+ fslio->niftiptr->sto_ijk =
1857
+ nifti_mat44_inverse(fslio->niftiptr->sto_xyz);
1858
+ }
1859
+
1860
+ }
1861
+
1862
+ }
1863
+ if (fslio->mincptr!=NULL) {
1864
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1865
+ }
1866
+ }
1867
+
1868
+
1869
+ void FslGetAnalyzeOrigin(FSLIO *fslio, short orig[5])
1870
+ {
1871
+ /* NB: orig returned here is in Analyze convention - starting at 1, not 0 */
1872
+ if (fslio==NULL) FSLIOERR("FslGetAnalyzeOrigin: Null pointer passed for FSLIO");
1873
+ if (fslio->niftiptr!=NULL) {
1874
+ /* Use sform or qform to determine the origin - default is zero */
1875
+ orig[0]=0;
1876
+ orig[1]=0;
1877
+ orig[2]=0;
1878
+ orig[3]=0;
1879
+ orig[4]=0;
1880
+
1881
+ if (fslio->niftiptr->qform_code != NIFTI_XFORM_UNKNOWN) {
1882
+ orig[0]=(short) fslio->niftiptr->qto_ijk.m[0][3] + 1;
1883
+ orig[1]=(short) fslio->niftiptr->qto_ijk.m[1][3] + 1;
1884
+ orig[2]=(short) fslio->niftiptr->qto_ijk.m[2][3] + 1;
1885
+ }
1886
+
1887
+ if (fslio->niftiptr->sform_code != NIFTI_XFORM_UNKNOWN) {
1888
+ orig[0]=(short) fslio->niftiptr->sto_ijk.m[0][3] + 1;
1889
+ orig[1]=(short) fslio->niftiptr->sto_ijk.m[1][3] + 1;
1890
+ orig[2]=(short) fslio->niftiptr->sto_ijk.m[2][3] + 1;
1891
+ }
1892
+ }
1893
+ if (fslio->mincptr!=NULL) {
1894
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1895
+ }
1896
+ }
1897
+
1898
+
1899
+
1900
+ /***************************************************************
1901
+ * FslClose
1902
+ ***************************************************************/
1903
+ /*! \fn int FslClose(FSLIO *fslio)
1904
+ \brief Write header and image data if this dataset was open for
1905
+ writing. Close the dataset header and data files.
1906
+
1907
+
1908
+ \param fslio pointer to FSLIO data structure
1909
+ \return -1 on error, 0 OK ???.
1910
+ */
1911
+ int FslClose(FSLIO *fslio)
1912
+ {
1913
+ int retval=0, filetype;
1914
+ struct dsr *hdr;
1915
+ znzFile hptr=NULL;
1916
+
1917
+ if (fslio==NULL) return 0;
1918
+
1919
+ /* close the (data) file */
1920
+ if (!znz_isnull(fslio->fileptr)) retval=znzclose(fslio->fileptr);
1921
+
1922
+ /** ----- if writing the image, need to worry about the header bit ----- **/
1923
+
1924
+ if ( (fslio->niftiptr!=NULL) && (FslGetWriteMode(fslio)==1)
1925
+ && (fslio->written_hdr==0) ) {
1926
+
1927
+ /* ensure that the type is set correctly */
1928
+ fslio->niftiptr->nifti_type = FslBaseFileType(FslGetFileType(fslio));
1929
+
1930
+ /* must write the header now */
1931
+ filetype = FslGetFileType(fslio);
1932
+ strcpy(fslio->niftiptr->descrip,"FSL3.2beta");
1933
+ if (!FslIsSingleFileType(filetype)) {
1934
+ /* for file pairs - open new header file and write it */
1935
+ nifti_image_write_hdr_img(fslio->niftiptr,0,"wb");
1936
+ } else {
1937
+ /* for single files it is more complicated */
1938
+ if (!FslIsCompressedFileType(filetype)) {
1939
+ /* noncompressed -> reopen this file in r+ mode and write the header part again */
1940
+ nifti_image_write_hdr_img(fslio->niftiptr,0,"r+b");
1941
+ } else {
1942
+ /* compressed mode -> not possible! */
1943
+ fprintf(stderr,"Error:: header must be written before writing any other data.\n");
1944
+ return -1;
1945
+ }
1946
+ }
1947
+ }
1948
+
1949
+ /* --- nasty hack to write the origin in Analyze files --- */
1950
+
1951
+ if ( (FslGetWriteMode(fslio)==1) && (fslio->niftiptr!=NULL) &&
1952
+ (FslBaseFileType(FslGetFileType(fslio))==FSL_TYPE_ANALYZE) ) {
1953
+
1954
+ /* read in the old header, change the origin and write it out again */
1955
+ hdr = (struct dsr *) calloc(1,sizeof(struct dsr));
1956
+ FslReadRawHeader(hdr,fslio->niftiptr->fname);
1957
+ if (fslio->niftiptr->byteorder != nifti_short_order()) {AvwSwapHeader(hdr);}
1958
+
1959
+ /* calculate origin from sform (if set) */
1960
+ {
1961
+ short blah[5];
1962
+ FslGetAnalyzeOrigin(fslio,blah);
1963
+ memcpy(hdr->hist.originator,blah,5*sizeof(short));
1964
+
1965
+ /* Write out in radiological order if origin is non-zero */
1966
+ /* set negative pixdim if needed to keep LR orientation consistent */
1967
+ if ( (blah[0]!=0) || (blah[1]!=0) || (blah[2]!=0) ) {
1968
+ if (hdr->dime.pixdim[1] * hdr->dime.pixdim[2] * hdr->dime.pixdim[3] > 0) {
1969
+ hdr->dime.pixdim[1] = - hdr->dime.pixdim[1];
1970
+ }
1971
+ }
1972
+ }
1973
+
1974
+ /* swap back byte order and write out */
1975
+ if (fslio->niftiptr->byteorder != nifti_short_order()) {AvwSwapHeader(hdr);}
1976
+ hptr = znzopen(fslio->niftiptr->fname,"wb",FslIsCompressedFileType(FslGetFileType(fslio)));
1977
+ if (znz_isnull(hptr)) {
1978
+ fprintf(stderr,"Error:: Could not write origin data to header file %s.\n",
1979
+ fslio->niftiptr->fname);
1980
+ return -1;
1981
+ };
1982
+
1983
+ znzwrite(hdr,1,sizeof(struct dsr),hptr);
1984
+ znzclose(hptr);
1985
+ free(hdr);
1986
+ }
1987
+
1988
+ if (fslio->mincptr!=NULL) {
1989
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
1990
+ return -1;
1991
+ }
1992
+
1993
+ return retval;
1994
+ }
1995
+
1996
+
1997
+ void AvwSwapHeader(struct dsr *avw)
1998
+ {
1999
+ char *ptr;
2000
+
2001
+ ptr = (char *) &(avw->hk);
2002
+ nifti_swap_4bytes(1,ptr); /* sizeof_hdr */
2003
+ ptr += 32;
2004
+ nifti_swap_4bytes(1,ptr); /* extents */
2005
+ ptr += 4;
2006
+ nifti_swap_2bytes(1,ptr); /* session_error */
2007
+
2008
+ ptr = (char *) &(avw->dime);
2009
+ nifti_swap_2bytes(8,ptr); /* dims */
2010
+ ptr += 28;
2011
+ nifti_swap_2bytes(4,ptr); /* unused1, datatype, bitpix, dim_un0 */
2012
+ ptr += 8;
2013
+ nifti_swap_4bytes(18,ptr); /* pixdim, vox_offset, ... */
2014
+ /* cal_min, compressed, ... glmin */
2015
+
2016
+ ptr = (char *) &(avw->hist);
2017
+ ptr += 105;
2018
+ nifti_swap_2bytes(5,ptr); /* originator (used to store origin) */
2019
+ ptr += 63;
2020
+ nifti_swap_4bytes(8,ptr); /* views, ... smin */
2021
+ }
2022
+
2023
+
2024
+ int FslReadRawHeader(void *buffer, const char* filename)
2025
+ {
2026
+ znzFile fp;
2027
+ int retval;
2028
+ fp = znzopen(filename,"rb",1);
2029
+ if (znz_isnull(fp)) {
2030
+ fprintf(stderr,"Could not open header %s\n",filename);
2031
+ return 0;
2032
+ }
2033
+ retval = znzread(buffer,1,348,fp);
2034
+ znzclose(fp);
2035
+ if (retval != 348) {
2036
+ fprintf(stderr,"Could not read header %s\n",filename);
2037
+ return retval;
2038
+ }
2039
+ return retval;
2040
+ }
2041
+
2042
+ void FslSetOverrideOutputType(int type)
2043
+ {
2044
+ if ( (type==-1) || (FslIsValidFileType(type)) ) {
2045
+ FslOverrideOutputType=type;
2046
+ } else {
2047
+ fprintf(stderr,"Invalid file type (%d) requested - ignoring this\n",type);
2048
+ }
2049
+ }
2050
+
2051
+ int FslGetOverrideOutputType(void)
2052
+ {
2053
+ return FslOverrideOutputType;
2054
+ }
2055
+
2056
+ void FslSetIgnoreMFQ(int flag)
2057
+ {
2058
+ assert((flag==0) || (flag==1));
2059
+ FslIgnoreMFQ=flag;
2060
+ }
2061
+
2062
+
2063
+ int FslGetIgnoreMFQ(void)
2064
+ {
2065
+ return FslIgnoreMFQ;
2066
+ }
2067
+
2068
+ /***************************************************************
2069
+ * FslReadHeader
2070
+ ***************************************************************/
2071
+ /*! \fn FSLIO * FslReadHeader(char *fname)
2072
+ \brief Reads nifti/anz header, no data is read
2073
+
2074
+ \param fname filename specification (could be .img,.hdr,.nii, or no ext
2075
+ \return FSLIO data structure with the nifti_image structure fields filled
2076
+ as per fname header.
2077
+ NULL on error
2078
+ */
2079
+ FSLIO * FslReadHeader(char *fname)
2080
+ {
2081
+ char *hdrname, *imgname;
2082
+ FSLIO *fslio;
2083
+
2084
+
2085
+ fslio = FslInit();
2086
+
2087
+ /** get header file name */
2088
+ FslGetHdrImgNames(fname, fslio, &hdrname, &imgname);
2089
+
2090
+ /** read header information */
2091
+ fslio->niftiptr = nifti_image_read(hdrname, 0);
2092
+
2093
+ if (fslio->niftiptr == NULL) {
2094
+ FSLIOERR("FslReadHeader: error reading header information");
2095
+ return(NULL);
2096
+ }
2097
+
2098
+ fslio->file_mode = FslGetReadFileType(fslio);
2099
+
2100
+ return(fslio);
2101
+ }
2102
+
2103
+
2104
+ /***************************************************************
2105
+ * FslGetVolumeAsScaledDouble
2106
+ ***************************************************************/
2107
+ /*! \fn double *** FslGetVolumeAsScaledDouble(FSLIO *fslio, int vol)
2108
+ \brief Return volume #vol (0-based) as a 3D array of scaled doubles.
2109
+
2110
+ Volume Array is indexed as [0..zdim-1][0..ydim-1][0..xdim-1].
2111
+ <br>The array will be byteswapped to native-endian.
2112
+ <br>Array values are scaled as per fslio header slope and intercept fields.
2113
+
2114
+ \param fslio pointer to open dataset
2115
+ \param vol volume number to read (legal range [0..tdim-1])
2116
+ \return Pointer to 3D double array, NULL on error
2117
+ */
2118
+ double ***FslGetVolumeAsScaledDouble(FSLIO *fslio, int vol)
2119
+ {
2120
+ double ***newbuf;
2121
+ void *diskbuf;
2122
+ int xx,yy,zz;
2123
+ int ret;
2124
+ float inter, slope;
2125
+ int dims_to_get[8];
2126
+ int i;
2127
+
2128
+ if (fslio==NULL) FSLIOERR("FslGetVolumeAsScaledDouble: Null pointer passed for FSLIO");
2129
+
2130
+ if ((fslio->niftiptr->dim[0] < 3) || (fslio->niftiptr->dim[0] > 4))
2131
+ FSLIOERR("FslGetVolumeAsScaledDouble: Incorrect dataset dimension, 3D-4D needed");
2132
+
2133
+ /***** nifti dataset */
2134
+ if (fslio->niftiptr!=NULL) {
2135
+ xx = (fslio->niftiptr->nx == 0 ? 1 : (long)fslio->niftiptr->nx);
2136
+ yy = (fslio->niftiptr->ny == 0 ? 1 : (long)fslio->niftiptr->ny);
2137
+ zz = (fslio->niftiptr->nz == 0 ? 1 : (long)fslio->niftiptr->nz);
2138
+
2139
+ if (fslio->niftiptr->scl_slope == 0) {
2140
+ slope = 1.0;
2141
+ inter = 0.0;
2142
+ }
2143
+ else {
2144
+ slope = fslio->niftiptr->scl_slope;
2145
+ inter = fslio->niftiptr->scl_inter;
2146
+ }
2147
+
2148
+
2149
+ /** allocate new 3D buffer */
2150
+ newbuf = d3matrix(zz-1,yy-1,xx-1);
2151
+
2152
+
2153
+ /** read in the data in disk format */
2154
+ dims_to_get[0] = 0;
2155
+ for (i=1; i<8; i++)
2156
+ dims_to_get[i] = -1;
2157
+ dims_to_get[4] = vol;
2158
+
2159
+
2160
+ diskbuf = NULL;
2161
+ ret = nifti_read_collapsed_image(fslio->niftiptr, dims_to_get, &diskbuf );
2162
+ if (ret <= 0) {
2163
+ fprintf(stderr,"ERROR:: read of disk buffer for volume %d from %s failed.\n",vol,fslio->niftiptr->iname);
2164
+ return(NULL);
2165
+ }
2166
+
2167
+
2168
+ /** cvt disk buffer to scaled double buffer */
2169
+ ret = convertBufferToScaledDouble(newbuf[0][0], diskbuf, (long)(xx*yy*zz), slope, inter, fslio->niftiptr->datatype);
2170
+
2171
+ free(diskbuf);
2172
+
2173
+ if (ret == 0)
2174
+ return(newbuf);
2175
+ else
2176
+ return(NULL);
2177
+
2178
+ } /* nifti data */
2179
+
2180
+
2181
+ if (fslio->mincptr!=NULL) {
2182
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
2183
+ }
2184
+
2185
+ return(NULL);
2186
+ }
2187
+
2188
+
2189
+
2190
+
2191
+ /***************************************************************
2192
+ * FslGetBufferAsScaledDouble
2193
+ ***************************************************************/
2194
+ /*! \fn double **** FslGetBufferAsScaledDouble(FSLIO *fslio)
2195
+ \brief Return the fslio data buffer of a 1-4D dataset as a 4D array of
2196
+ scaled doubles.
2197
+
2198
+ Array is indexed as buf[0..tdim-1][0..zdim-1][0..ydim-1][0..xdim-1].
2199
+ <br>The array will be byteswapped to native-endian.
2200
+ <br>Array values are scaled as per fslio header slope and intercept fields.
2201
+
2202
+ \param fslio pointer to open dataset
2203
+ \return Pointer to 4D double array, NULL on error
2204
+ */
2205
+ double ****FslGetBufferAsScaledDouble(FSLIO *fslio)
2206
+ {
2207
+ double ****newbuf;
2208
+ int xx,yy,zz,tt;
2209
+ int ret;
2210
+ float inter, slope;
2211
+
2212
+ if (fslio==NULL) FSLIOERR("FslGetBufferAsScaledDouble: Null pointer passed for FSLIO");
2213
+
2214
+ if ((fslio->niftiptr->dim[0] <= 0) || (fslio->niftiptr->dim[0] > 4))
2215
+ FSLIOERR("FslGetBufferAsScaledDouble: Incorrect dataset dimension, 1-4D needed");
2216
+
2217
+ /***** nifti dataset */
2218
+ if (fslio->niftiptr!=NULL) {
2219
+ xx = (fslio->niftiptr->nx == 0 ? 1 : (long)fslio->niftiptr->nx);
2220
+ yy = (fslio->niftiptr->ny == 0 ? 1 : (long)fslio->niftiptr->ny);
2221
+ zz = (fslio->niftiptr->nz == 0 ? 1 : (long)fslio->niftiptr->nz);
2222
+ tt = (fslio->niftiptr->nt == 0 ? 1 : (long)fslio->niftiptr->nt);
2223
+
2224
+ if (fslio->niftiptr->scl_slope == 0) {
2225
+ slope = 1.0;
2226
+ inter = 0.0;
2227
+ }
2228
+ else {
2229
+ slope = fslio->niftiptr->scl_slope;
2230
+ inter = fslio->niftiptr->scl_inter;
2231
+ }
2232
+
2233
+
2234
+ /** allocate new 4D buffer */
2235
+ newbuf = d4matrix(tt-1,zz-1,yy-1,xx-1);
2236
+
2237
+ /** cvt it */
2238
+ ret = convertBufferToScaledDouble(newbuf[0][0][0], fslio->niftiptr->data, (long)(xx*yy*zz*tt), slope, inter, fslio->niftiptr->datatype);
2239
+
2240
+ if (ret == 0)
2241
+ return(newbuf);
2242
+ else
2243
+ return(NULL);
2244
+
2245
+ } /* nifti data */
2246
+
2247
+
2248
+ if (fslio->mincptr!=NULL) {
2249
+ fprintf(stderr,"Warning:: Minc is not yet supported\n");
2250
+ }
2251
+
2252
+ return(NULL);
2253
+ }
2254
+
2255
+ /***************************************************************
2256
+ * convertBufferToScaledDouble
2257
+ ***************************************************************/
2258
+ /*! \fn int convertBufferToScaledDouble(double *outbuf, void *inbuf, long len, float slope, float inter, int nifti_datatype )
2259
+ \brief allocate a 4D buffer, use 1 contiguous buffer for the data
2260
+
2261
+ Array is indexed as buf[0..th-1][0..zh-1][0..yh-1][0..xh-1].
2262
+ <br>To access all elements as a vector, use buf[0][0][0][i] where
2263
+ i can range from 0 to th*zh*yh*xh - 1.
2264
+
2265
+ \param outbuf pointer to array of doubles of size len
2266
+ \param inbuf void pointer to an array of len items of datatype nifti_datatype
2267
+ \param len number of elements in outbuf and inbuf
2268
+ \param slope slope term of scaling to be applied
2269
+ \param inter intercept term of scaling to be applied: out = (in*slope)+inter
2270
+ \param nifti_datatype NIFTI datatype code for the datatype of the elements in inbuf
2271
+ \return error code: 0=OK -1=error
2272
+ */
2273
+ int convertBufferToScaledDouble(double *outbuf, void *inbuf, long len, float slope, float inter, int nifti_datatype )
2274
+ {
2275
+
2276
+ long i;
2277
+
2278
+
2279
+ /** fill the buffer */
2280
+ for (i=0; i<len; i++)
2281
+ switch(nifti_datatype) {
2282
+ case NIFTI_TYPE_UINT8:
2283
+ outbuf[i] = (double) ( *((THIS_UINT8 *)(inbuf)+i) * slope + inter);
2284
+ break;
2285
+ case NIFTI_TYPE_INT8:
2286
+ outbuf[i] = (double) ( *((THIS_INT8 *)(inbuf)+i) * slope + inter);
2287
+ break;
2288
+ case NIFTI_TYPE_UINT16:
2289
+ outbuf[i] = (double) ( *((THIS_UINT16 *)(inbuf)+i) * slope + inter);
2290
+ break;
2291
+ case NIFTI_TYPE_INT16:
2292
+ outbuf[i] = (double) ( *((THIS_INT16 *)(inbuf)+i) * slope + inter);
2293
+ break;
2294
+ case NIFTI_TYPE_UINT64:
2295
+ outbuf[i] = (double) ( *((THIS_UINT64 *)(inbuf)+i) * slope + inter);
2296
+ break;
2297
+ case NIFTI_TYPE_INT64:
2298
+ outbuf[i] = (double) ( *((THIS_INT64 *)(inbuf)+i) * slope + inter);
2299
+ break;
2300
+ case NIFTI_TYPE_UINT32:
2301
+ outbuf[i] = (double) ( *((THIS_UINT32 *)(inbuf)+i) * slope + inter);
2302
+ break;
2303
+ case NIFTI_TYPE_INT32:
2304
+ outbuf[i] = (double) ( *((THIS_INT32 *)(inbuf)+i) * slope + inter);
2305
+ break;
2306
+ case NIFTI_TYPE_FLOAT32:
2307
+ outbuf[i] = (double) ( *((THIS_FLOAT32 *)(inbuf)+i) * slope + inter);
2308
+ break;
2309
+ case NIFTI_TYPE_FLOAT64:
2310
+ outbuf[i] = (double) ( *((THIS_FLOAT64 *)(inbuf)+i) * slope + inter);
2311
+ break;
2312
+
2313
+ case NIFTI_TYPE_FLOAT128:
2314
+ case NIFTI_TYPE_COMPLEX128:
2315
+ case NIFTI_TYPE_COMPLEX256:
2316
+ case NIFTI_TYPE_COMPLEX64:
2317
+ default:
2318
+ fprintf(stderr, "\nWarning, cannot support %s yet.\n",nifti_datatype_string(nifti_datatype));
2319
+ return(-1);
2320
+ }
2321
+
2322
+ return(0);
2323
+ }
2324
+
2325
+ /***************************************************************
2326
+ * d3matrix
2327
+ ***************************************************************/
2328
+ /*! \fn double ****d3matrix(int zh, int yh, int xh)
2329
+ \brief allocate a 3D buffer, use 1 contiguous buffer for the data
2330
+
2331
+ Array is indexed as buf[0..zh][0..yh][0..xh].
2332
+ <br>To access all elements as a vector, use buf[0][0][i] where
2333
+ i can range from 0 to zh*yh*xh - 1.
2334
+ Adaptation of Numerical Recipes in C nrutil.c allocation routines.
2335
+
2336
+ \param zh slowest changing dimension
2337
+ \param yh 2nd fastest changing dimension
2338
+ \param xh fastest changing dimension
2339
+ \return Pointer to 3D double array
2340
+ */
2341
+ double ***d3matrix(int zh, int yh, int xh)
2342
+ {
2343
+
2344
+ int j;
2345
+ int nslice = zh+1;
2346
+ int nrow = yh+1;
2347
+ int ncol = xh+1;
2348
+ double ***t;
2349
+
2350
+
2351
+ /** allocate pointers to slices */
2352
+ t=(double ***) malloc((size_t)((nslice)*sizeof(double**)));
2353
+ if (!t) FSLIOERR("d3matrix: allocation failure");
2354
+
2355
+ /** allocate pointers for ydim */
2356
+ t[0]=(double **) malloc((size_t)((nslice*nrow)*sizeof(double*)));
2357
+ if (!t[0]) FSLIOERR("d3matrix: allocation failure");
2358
+
2359
+
2360
+ /** allocate the data blob */
2361
+ t[0][0]=(double *) malloc((size_t)((nslice*nrow*ncol)*sizeof(double)));
2362
+ if (!t[0][0]) FSLIOERR("d3matrix: allocation failure");
2363
+
2364
+
2365
+ /** point everything to the data blob */
2366
+ for(j=1;j<nrow*nslice;j++) t[0][j]=t[0][j-1]+ncol;
2367
+ for(j=1;j<nslice;j++) t[j]=t[j-1]+nrow;
2368
+
2369
+ return t;
2370
+ }
2371
+
2372
+
2373
+ /***************************************************************
2374
+ * d4matrix
2375
+ ***************************************************************/
2376
+ /*! \fn double ****d4matrix(int th, int zh, int yh, int xh)
2377
+ \brief allocate a 4D buffer, use 1 contiguous buffer for the data
2378
+
2379
+ Array is indexed as buf[0..th][0..zh][0..yh][0..xh].
2380
+ <br>To access all elements as a vector, use buf[0][0][0][i] where
2381
+ i can range from 0 to th*zh*yh*xh - 1.
2382
+ Adaptation of Numerical Recipes in C nrutil.c allocation routines.
2383
+
2384
+ \param th slowest changing dimension
2385
+ \param zh 2nd slowest changing dimension
2386
+ \param yh 2nd fastest changing dimension
2387
+ \param xh fastest changing dimension
2388
+ \return Pointer to 4D double array
2389
+ */
2390
+ double ****d4matrix(int th, int zh, int yh, int xh)
2391
+ {
2392
+
2393
+ int j;
2394
+ int nvol = th+1;
2395
+ int nslice = zh+1;
2396
+ int nrow = yh+1;
2397
+ int ncol = xh+1;
2398
+ double ****t;
2399
+
2400
+
2401
+ /** allocate pointers to vols */
2402
+ t=(double ****) malloc((size_t)((nvol)*sizeof(double***)));
2403
+ if (!t) FSLIOERR("d4matrix: allocation failure");
2404
+
2405
+ /** allocate pointers to slices */
2406
+ t[0]=(double ***) malloc((size_t)((nvol*nslice)*sizeof(double**)));
2407
+ if (!t[0]) FSLIOERR("d4matrix: allocation failure");
2408
+
2409
+ /** allocate pointers for ydim */
2410
+ t[0][0]=(double **) malloc((size_t)((nvol*nslice*nrow)*sizeof(double*)));
2411
+ if (!t[0][0]) FSLIOERR("d4matrix: allocation failure");
2412
+
2413
+
2414
+ /** allocate the data blob */
2415
+ t[0][0][0]=(double *) malloc((size_t)((nvol*nslice*nrow*ncol)*sizeof(double)));
2416
+ if (!t[0][0][0]) FSLIOERR("d4matrix: allocation failure");
2417
+
2418
+
2419
+ /** point everything to the data blob */
2420
+ for(j=1;j<nrow*nslice*nvol;j++) t[0][0][j]=t[0][0][j-1]+ncol;
2421
+ for(j=1;j<nslice*nvol;j++) t[0][j]=t[0][j-1]+nrow;
2422
+ for(j=1;j<nvol;j++) t[j]=t[j-1]+nslice;
2423
+
2424
+ return t;
2425
+ }
2426
+