Home > vbmeg > external > bioelectromagnetism > spm_hread.m

spm_hread

PURPOSE ^

reads a header

SYNOPSIS ^

function [DIM,VOX,SCALE,TYPE,OFFSET,ORIGIN,DESCRIP] = spm_hread(P)

DESCRIPTION ^

 reads a header
 FORMAT [DIM VOX SCALE TYPE OFFSET ORIGIN DESCRIP] = spm_hread(P);

 P       - filename          (e.g spm or spm.img)
 DIM     - image size       [i j k [l]] (voxels)
 VOX     - voxel size       [x y z [t]] (mm [secs])
 SCALE   - scale factor
 TYPE    - datatype (integer - see spm_type)
 OFFSET  - offset (bytes)
 ORIGIN  - origin [i j k]
 DESCRIP - description string
___________________________________________________________________________

 spm_hread reads variables into working memory from a SPM/ANALYZE
 compatible header file.  If the header does not exist global defaults
 are used.  The 'originator' field of the ANALYZE format has been
 changed to ORIGIN in the SPM version of the header.  funused1 of the
 ANALYZE format is used for SCALE

 see also dbh.h (ANALYZE) spm_hwrite.m and spm_type.m

__________________________________________________________________________
 @(#)spm_hread.m    2.7 99/10/29

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [DIM,VOX,SCALE,TYPE,OFFSET,ORIGIN,DESCRIP] = spm_hread(P)
0002 % reads a header
0003 % FORMAT [DIM VOX SCALE TYPE OFFSET ORIGIN DESCRIP] = spm_hread(P);
0004 %
0005 % P       - filename          (e.g spm or spm.img)
0006 % DIM     - image size       [i j k [l]] (voxels)
0007 % VOX     - voxel size       [x y z [t]] (mm [secs])
0008 % SCALE   - scale factor
0009 % TYPE    - datatype (integer - see spm_type)
0010 % OFFSET  - offset (bytes)
0011 % ORIGIN  - origin [i j k]
0012 % DESCRIP - description string
0013 %___________________________________________________________________________
0014 %
0015 % spm_hread reads variables into working memory from a SPM/ANALYZE
0016 % compatible header file.  If the header does not exist global defaults
0017 % are used.  The 'originator' field of the ANALYZE format has been
0018 % changed to ORIGIN in the SPM version of the header.  funused1 of the
0019 % ANALYZE format is used for SCALE
0020 %
0021 % see also dbh.h (ANALYZE) spm_hwrite.m and spm_type.m
0022 %
0023 %__________________________________________________________________________
0024 % @(#)spm_hread.m    2.7 99/10/29
0025 
0026 % ensure correct suffix {.hdr}
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 % open header file
0034 %---------------------------------------------------------------------------
0035 fid   = fopen(P,'r','ieee-le');
0036 
0037 if (fid > 0)
0038 
0039 % read (struct) header_key
0040 %---------------------------------------------------------------------------
0041 fseek(fid,0,'bof');
0042 
0043 otherendian = 0;
0044 sizeof_hdr     = fread(fid,1,'*int32');
0045 
0046 % size of header must be 348 bytes
0047 if ~isequal(sizeof_hdr,348),
0048     % Re-open other-endian
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     % Now throw an error
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 % read (struct) image_dimension
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 % read (struct) data_history
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 % convert to SPM global variables
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;

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005