0001 function [DIM,VOX,SCALE,TYPE,OFFSET,ORIGIN,DESCRIP] = spm_hread(P)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 P = deblank(P);
0029 q = length(P);
0030 if q>=4 & P(q - 3) == '.'; P = P(1:(q - 4)); end
0031 P = [P '.hdr'];
0032
0033
0034
0035 fid = fopen(P,'r','ieee-le');
0036
0037 if (fid > 0)
0038
0039
0040
0041 fseek(fid,0,'bof');
0042
0043 otherendian = 0;
0044 sizeof_hdr = fread(fid,1,'*int32');
0045
0046
0047 if ~isequal(sizeof_hdr,348),
0048
0049 fclose(fid);
0050 fid = fopen(P,'r','ieee-be');
0051 fseek(fid,0,'bof');
0052 sizeof_hdr = fread(fid,1,'*int32');
0053 otherendian = 1;
0054 end;
0055
0056 if ~isequal(sizeof_hdr,348),
0057
0058 msg = '...size of header not equal to 348 bytes!\n\n';
0059 error(msg);
0060 end
0061
0062 data_type = mysetstr(fread(fid,10,'uchar'))';
0063 db_name = mysetstr(fread(fid,18,'uchar'))';
0064 extents = fread(fid,1,'int32');
0065 session_error = fread(fid,1,'int16');
0066 regular = mysetstr(fread(fid,1,'uchar'))';
0067 hkey_un0 = mysetstr(fread(fid,1,'uchar'))';
0068
0069
0070
0071
0072
0073 fseek(fid,40,'bof');
0074
0075 dim = fread(fid,8,'int16');
0076 vox_units = mysetstr(fread(fid,4,'uchar'))';
0077 cal_units = mysetstr(fread(fid,8,'uchar'))';
0078 unused1 = fread(fid,1,'int16');
0079 datatype = fread(fid,1,'int16');
0080 bitpix = fread(fid,1,'int16');
0081 dim_un0 = fread(fid,1,'int16');
0082 pixdim = fread(fid,8,'float');
0083 vox_offset = fread(fid,1,'float');
0084 funused1 = fread(fid,1,'float');
0085 funused2 = fread(fid,1,'float');
0086 funused3 = fread(fid,1,'float');
0087 cal_max = fread(fid,1,'float');
0088 cal_min = fread(fid,1,'float');
0089 compressed = fread(fid,1,'int32');
0090 verified = fread(fid,1,'int32');
0091 glmax = fread(fid,1,'int32');
0092 glmin = fread(fid,1,'int32');
0093
0094
0095
0096 fseek(fid,148,'bof');
0097
0098 descrip = mysetstr(fread(fid,80,'uchar'))';
0099 aux_file = mysetstr(fread(fid,24,'uchar'))';
0100 orient = fread(fid,1,'uchar');
0101 origin = fread(fid,5,'int16');
0102 generated = mysetstr(fread(fid,10,'uchar'))';
0103 scannum = mysetstr(fread(fid,10,'uchar'))';
0104 patient_id = mysetstr(fread(fid,10,'uchar'))';
0105 exp_date = mysetstr(fread(fid,10,'uchar'))';
0106 exp_time = mysetstr(fread(fid,10,'uchar'))';
0107 hist_un0 = mysetstr(fread(fid,3,'uchar'))';
0108 views = fread(fid,1,'int32');
0109 vols_added = fread(fid,1,'int32');
0110 start_field = fread(fid,1,'int32');
0111 field_skip = fread(fid,1,'int32');
0112 omax = fread(fid,1,'int32');
0113 omin = fread(fid,1,'int32');
0114 smax = fread(fid,1,'int32');
0115 smin = fread(fid,1,'int32');
0116
0117 fclose(fid);
0118
0119 if isempty(smin)
0120 error(['There is a problem with the header file ' P '.']);
0121 end
0122
0123
0124
0125 DIM = dim(2:4)';
0126 VOX = pixdim(2:4)';
0127 SCALE = funused1;
0128 SCALE = ~SCALE + SCALE;
0129 TYPE = datatype;
0130 if otherendian == 1 & datatype ~= 2,
0131 TYPE = TYPE*256;
0132 end;
0133 OFFSET = vox_offset;
0134 ORIGIN = origin(1:3)';
0135 DESCRIP = descrip(1:max(find(descrip)));
0136
0137 else
0138 global DIM VOX SCALE TYPE OFFSET ORIGIN
0139 DESCRIP = ['defaults'];
0140 end
0141 return;
0142
0143
0144 function out = mysetstr(in)
0145 tmp = find(in == 0);
0146 tmp = min([min(tmp) length(in)]);
0147 out = setstr(in(1:tmp));
0148 return;