PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/external/fieldtrip/trunk/fileio/private/avw_hdr_read.m

http://open-realtime-fmri.googlecode.com/
MATLAB | 389 lines | 139 code | 37 blank | 213 comment | 21 complexity | 48271159722bc225bff6496756cf9f5d MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0
  1. function [ avw, machine ] = avw_hdr_read(fileprefix, machine, verbose)
  2. % avw_hdr_read - read Analyze format data header (*.hdr)
  3. %
  4. % [ avw, machine ] = avw_hdr_read(fileprefix, [machine], [verbose])
  5. %
  6. % fileprefix - string filename (without .hdr); the file name
  7. % can be given as a full path or relative to the
  8. % current directory.
  9. %
  10. % machine - a string, see machineformat in fread for details.
  11. % The default here is 'ieee-le' but the routine
  12. % will automatically switch between little and big
  13. % endian to read any such Analyze header. It
  14. % reports the appropriate machine format and can
  15. % return the machine value.
  16. %
  17. % avw.hdr - a struct, all fields returned from the header.
  18. % For details, find a good description on the web
  19. % or see the Analyze File Format pdf in the
  20. % mri_toolbox doc folder or read this .m file.
  21. %
  22. % verbose - the default is to output processing information to the command
  23. % window. If verbose = 0, this will not happen.
  24. %
  25. % This function is called by avw_img_read
  26. %
  27. % See also avw_hdr_write, avw_hdr_make, avw_view_hdr, avw_view
  28. %
  29. % $Revision: 2885 $ $Date: 2009/01/14 09:24:45 $
  30. % Licence: GNU GPL, no express or implied warranties
  31. % History: 05/2002, Darren.Weber@flinders.edu.au
  32. % The Analyze format and c code below is copyright
  33. % (c) Copyright, 1986-1995
  34. % Biomedical Imaging Resource, Mayo Foundation
  35. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  36. if ~exist('verbose','var'), verbose = 1; end
  37. if verbose,
  38. version = '[$Revision: 2885 $]';
  39. fprintf('\nAVW_HDR_READ [v%s]\n',version(12:16)); tic;
  40. end
  41. if ~exist('fileprefix','var'),
  42. msg = sprintf('...no input fileprefix - see help avw_hdr_read\n\n');
  43. error(msg);
  44. end
  45. if ~exist('machine','var'), machine = 'ieee-le'; end
  46. if findstr('.hdr',fileprefix),
  47. % fprintf('...removing .hdr extension from ''%s''\n',fileprefix);
  48. fileprefix = strrep(fileprefix,'.hdr','');
  49. end
  50. if findstr('.img',fileprefix),
  51. % fprintf('...removing .img extension from ''%s''\n',fileprefix);
  52. fileprefix = strrep(fileprefix,'.img','');
  53. end
  54. file = sprintf('%s.hdr',fileprefix);
  55. if exist(file),
  56. if verbose,
  57. fprintf('...reading %s Analyze format',machine);
  58. end
  59. fid = fopen(file,'r',machine);
  60. avw.hdr = read_header(fid,verbose);
  61. avw.fileprefix = fileprefix;
  62. fclose(fid);
  63. if ~isequal(avw.hdr.hk.sizeof_hdr,348),
  64. if verbose, fprintf('...failed.\n'); end
  65. % first try reading the opposite endian to 'machine'
  66. switch machine,
  67. case 'ieee-le', machine = 'ieee-be';
  68. case 'ieee-be', machine = 'ieee-le';
  69. end
  70. if verbose, fprintf('...reading %s Analyze format',machine); end
  71. fid = fopen(file,'r',machine);
  72. avw.hdr = read_header(fid,verbose);
  73. avw.fileprefix = fileprefix;
  74. fclose(fid);
  75. end
  76. if ~isequal(avw.hdr.hk.sizeof_hdr,348),
  77. % Now throw an error
  78. if verbose, fprintf('...failed.\n'); end
  79. msg = sprintf('...size of header not equal to 348 bytes!\n\n');
  80. error(msg);
  81. end
  82. else
  83. msg = sprintf('...cannot find file %s.hdr\n\n',file);
  84. error(msg);
  85. end
  86. if verbose,
  87. t=toc; fprintf('...done (%5.2f sec).\n',t);
  88. end
  89. return
  90. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  91. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  92. function [ dsr ] = read_header(fid,verbose)
  93. % Original header structures - ANALYZE 7.5
  94. %struct dsr
  95. % {
  96. % struct header_key hk; /* 0 + 40 */
  97. % struct image_dimension dime; /* 40 + 108 */
  98. % struct data_history hist; /* 148 + 200 */
  99. % }; /* total= 348 bytes*/
  100. dsr.hk = header_key(fid);
  101. dsr.dime = image_dimension(fid,verbose);
  102. dsr.hist = data_history(fid);
  103. return
  104. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  105. function [hk] = header_key(fid)
  106. % The required elements in the header_key substructure are:
  107. %
  108. % int sizeof_header Must indicate the byte size of the header file.
  109. % int extents Should be 16384, the image file is created as
  110. % contiguous with a minimum extent size.
  111. % char regular Must be 'r' to indicate that all images and
  112. % volumes are the same size.
  113. % Original header structures - ANALYZE 7.5
  114. % struct header_key /* header key */
  115. % { /* off + size */
  116. % int sizeof_hdr /* 0 + 4 */
  117. % char data_type[10]; /* 4 + 10 */
  118. % char db_name[18]; /* 14 + 18 */
  119. % int extents; /* 32 + 4 */
  120. % short int session_error; /* 36 + 2 */
  121. % char regular; /* 38 + 1 */
  122. % char hkey_un0; /* 39 + 1 */
  123. % }; /* total=40 bytes */
  124. fseek(fid,0,'bof');
  125. hk.sizeof_hdr = fread(fid, 1,'*int32'); % should be 348!
  126. hk.data_type = fread(fid,10,'*char')';
  127. hk.db_name = fread(fid,18,'*char')';
  128. hk.extents = fread(fid, 1,'*int32');
  129. hk.session_error = fread(fid, 1,'*int16');
  130. hk.regular = fread(fid, 1,'*char')'; % might be uint8
  131. hk.hkey_un0 = fread(fid, 1,'*uint8')';
  132. % check if this value was a char zero
  133. if hk.hkey_un0 == 48,
  134. hk.hkey_un0 = 0;
  135. end
  136. return
  137. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  138. function [ dime ] = image_dimension(fid,verbose)
  139. %struct image_dimension
  140. % { /* off + size */
  141. % short int dim[8]; /* 0 + 16 */
  142. % /*
  143. % dim[0] Number of dimensions in database; usually 4.
  144. % dim[1] Image X dimension; number of *pixels* in an image row.
  145. % dim[2] Image Y dimension; number of *pixel rows* in slice.
  146. % dim[3] Volume Z dimension; number of *slices* in a volume.
  147. % dim[4] Time points; number of volumes in database
  148. % */
  149. % char vox_units[4]; /* 16 + 4 */
  150. % char cal_units[8]; /* 20 + 8 */
  151. % short int unused1; /* 28 + 2 */
  152. % short int datatype; /* 30 + 2 */
  153. % short int bitpix; /* 32 + 2 */
  154. % short int dim_un0; /* 34 + 2 */
  155. % float pixdim[8]; /* 36 + 32 */
  156. % /*
  157. % pixdim[] specifies the voxel dimensions:
  158. % pixdim[1] - voxel width, mm
  159. % pixdim[2] - voxel height, mm
  160. % pixdim[3] - slice thickness, mm
  161. % pixdim[4] - volume timing, in msec
  162. % ..etc
  163. % */
  164. % float vox_offset; /* 68 + 4 */
  165. % float roi_scale; /* 72 + 4 */
  166. % float funused1; /* 76 + 4 */
  167. % float funused2; /* 80 + 4 */
  168. % float cal_max; /* 84 + 4 */
  169. % float cal_min; /* 88 + 4 */
  170. % int compressed; /* 92 + 4 */
  171. % int verified; /* 96 + 4 */
  172. % int glmax; /* 100 + 4 */
  173. % int glmin; /* 104 + 4 */
  174. % }; /* total=108 bytes */
  175. dime.dim = fread(fid,8,'*int16')';
  176. dime.vox_units = fread(fid,4,'*char')';
  177. dime.cal_units = fread(fid,8,'*char')';
  178. dime.unused1 = fread(fid,1,'*int16');
  179. dime.datatype = fread(fid,1,'*int16');
  180. dime.bitpix = fread(fid,1,'*int16');
  181. dime.dim_un0 = fread(fid,1,'*int16');
  182. dime.pixdim = fread(fid,8,'*float')';
  183. dime.vox_offset = fread(fid,1,'*float');
  184. dime.roi_scale = fread(fid,1,'*float');
  185. dime.funused1 = fread(fid,1,'*float');
  186. dime.funused2 = fread(fid,1,'*float');
  187. dime.cal_max = fread(fid,1,'*float');
  188. dime.cal_min = fread(fid,1,'*float');
  189. dime.compressed = fread(fid,1,'*int32');
  190. dime.verified = fread(fid,1,'*int32');
  191. dime.glmax = fread(fid,1,'*int32');
  192. dime.glmin = fread(fid,1,'*int32');
  193. if dime.dim(1) < 4, % Number of dimensions in database; usually 4.
  194. if verbose,
  195. fprintf('...ensuring 4 dimensions in avw.hdr.dime.dim\n');
  196. end
  197. dime.dim(1) = int16(4);
  198. end
  199. if dime.dim(5) < 1, % Time points; number of volumes in database
  200. if verbose,
  201. fprintf('...ensuring at least 1 volume in avw.hdr.dime.dim(5)\n');
  202. end
  203. dime.dim(5) = int16(1);
  204. end
  205. return
  206. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  207. function [ hist ] = data_history(fid)
  208. % Original header structures - ANALYZE 7.5
  209. %struct data_history
  210. % { /* off + size */
  211. % char descrip[80]; /* 0 + 80 */
  212. % char aux_file[24]; /* 80 + 24 */
  213. % char orient; /* 104 + 1 */
  214. % char originator[10]; /* 105 + 10 */
  215. % char generated[10]; /* 115 + 10 */
  216. % char scannum[10]; /* 125 + 10 */
  217. % char patient_id[10]; /* 135 + 10 */
  218. % char exp_date[10]; /* 145 + 10 */
  219. % char exp_time[10]; /* 155 + 10 */
  220. % char hist_un0[3]; /* 165 + 3 */
  221. % int views /* 168 + 4 */
  222. % int vols_added; /* 172 + 4 */
  223. % int start_field; /* 176 + 4 */
  224. % int field_skip; /* 180 + 4 */
  225. % int omax; /* 184 + 4 */
  226. % int omin; /* 188 + 4 */
  227. % int smax; /* 192 + 4 */
  228. % int smin; /* 196 + 4 */
  229. % }; /* total=200 bytes */
  230. hist.descrip = fread(fid,80,'*char')';
  231. hist.aux_file = fread(fid,24,'*char')';
  232. hist.orient = fread(fid, 1,'*uint8'); % see note below on char
  233. hist.originator = fread(fid,10,'*char')';
  234. hist.generated = fread(fid,10,'*char')';
  235. hist.scannum = fread(fid,10,'*char')';
  236. hist.patient_id = fread(fid,10,'*char')';
  237. hist.exp_date = fread(fid,10,'*char')';
  238. hist.exp_time = fread(fid,10,'*char')';
  239. hist.hist_un0 = fread(fid, 3,'*char')';
  240. hist.views = fread(fid, 1,'*int32');
  241. hist.vols_added = fread(fid, 1,'*int32');
  242. hist.start_field = fread(fid, 1,'*int32');
  243. hist.field_skip = fread(fid, 1,'*int32');
  244. hist.omax = fread(fid, 1,'*int32');
  245. hist.omin = fread(fid, 1,'*int32');
  246. hist.smax = fread(fid, 1,'*int32');
  247. hist.smin = fread(fid, 1,'*int32');
  248. % check if hist.orient was saved as ascii char value
  249. switch hist.orient,
  250. case 48, hist.orient = uint8(0);
  251. case 49, hist.orient = uint8(1);
  252. case 50, hist.orient = uint8(2);
  253. case 51, hist.orient = uint8(3);
  254. case 52, hist.orient = uint8(4);
  255. case 53, hist.orient = uint8(5);
  256. end
  257. return
  258. % Note on using char:
  259. % The 'char orient' field in the header is intended to
  260. % hold simply an 8-bit unsigned integer value, not the ASCII representation
  261. % of the character for that value. A single 'char' byte is often used to
  262. % represent an integer value in Analyze if the known value range doesn't
  263. % go beyond 0-255 - saves a byte over a short int, which may not mean
  264. % much in today's computing environments, but given that this format
  265. % has been around since the early 1980's, saving bytes here and there on
  266. % older systems was important! In this case, 'char' simply provides the
  267. % byte of storage - not an indicator of the format for what is stored in
  268. % this byte. Generally speaking, anytime a single 'char' is used, it is
  269. % probably meant to hold an 8-bit integer value, whereas if this has
  270. % been dimensioned as an array, then it is intended to hold an ASCII
  271. % character string, even if that was only a single character.
  272. % Denny <hanson.dennis2@mayo.edu>
  273. % Comments
  274. % The header format is flexible and can be extended for new
  275. % user-defined data types. The essential structures of the header
  276. % are the header_key and the image_dimension.
  277. %
  278. % The required elements in the header_key substructure are:
  279. %
  280. % int sizeof_header Must indicate the byte size of the header file.
  281. % int extents Should be 16384, the image file is created as
  282. % contiguous with a minimum extent size.
  283. % char regular Must be 'r' to indicate that all images and
  284. % volumes are the same size.
  285. %
  286. % The image_dimension substructure describes the organization and
  287. % size of the images. These elements enable the database to reference
  288. % images by volume and slice number. Explanation of each element follows:
  289. %
  290. % short int dim[ ]; /* Array of the image dimensions */
  291. %
  292. % dim[0] Number of dimensions in database; usually 4.
  293. % dim[1] Image X dimension; number of pixels in an image row.
  294. % dim[2] Image Y dimension; number of pixel rows in slice.
  295. % dim[3] Volume Z dimension; number of slices in a volume.
  296. % dim[4] Time points; number of volumes in database.
  297. % dim[5] Undocumented.
  298. % dim[6] Undocumented.
  299. % dim[7] Undocumented.
  300. %
  301. % char vox_units[4] Specifies the spatial units of measure for a voxel.
  302. % char cal_units[8] Specifies the name of the calibration unit.
  303. % short int unused1 /* Unused */
  304. % short int datatype /* Datatype for this image set */
  305. % /*Acceptable values for datatype are*/
  306. % #define DT_NONE 0
  307. % #define DT_UNKNOWN 0 /*Unknown data type*/
  308. % #define DT_BINARY 1 /*Binary ( 1 bit per voxel)*/
  309. % #define DT_UNSIGNED_CHAR 2 /*Unsigned character ( 8 bits per voxel)*/
  310. % #define DT_SIGNED_SHORT 4 /*Signed short (16 bits per voxel)*/
  311. % #define DT_SIGNED_INT 8 /*Signed integer (32 bits per voxel)*/
  312. % #define DT_FLOAT 16 /*Floating point (32 bits per voxel)*/
  313. % #define DT_COMPLEX 32 /*Complex (64 bits per voxel; 2 floating point numbers)/*
  314. % #define DT_DOUBLE 64 /*Double precision (64 bits per voxel)*/
  315. % #define DT_RGB 128 /*A Red-Green-Blue datatype*/
  316. % #define DT_ALL 255 /*Undocumented*/
  317. %
  318. % short int bitpix; /* Number of bits per pixel; 1, 8, 16, 32, or 64. */
  319. % short int dim_un0; /* Unused */
  320. %
  321. % float pixdim[]; Parallel array to dim[], giving real world measurements in mm and ms.
  322. % pixdim[0]; Pixel dimensions?
  323. % pixdim[1]; Voxel width in mm.
  324. % pixdim[2]; Voxel height in mm.
  325. % pixdim[3]; Slice thickness in mm.
  326. % pixdim[4]; timeslice in ms (ie, TR in fMRI).
  327. % pixdim[5]; Undocumented.
  328. % pixdim[6]; Undocumented.
  329. % pixdim[7]; Undocumented.
  330. %
  331. % float vox_offset; Byte offset in the .img file at which voxels start. This value can be
  332. % negative to specify that the absolute value is applied for every image
  333. % in the file.
  334. %
  335. % float roi_scale; Specifies the Region Of Interest scale?
  336. % float funused1; Undocumented.
  337. % float funused2; Undocumented.
  338. %
  339. % float cal_max; Specifies the upper bound of the range of calibration values.
  340. % float cal_min; Specifies the lower bound of the range of calibration values.
  341. %
  342. % int compressed; Undocumented.
  343. % int verified; Undocumented.
  344. %
  345. % int glmax; The maximum pixel value for the entire database.
  346. % int glmin; The minimum pixel value for the entire database.
  347. %
  348. %