Home > rest_20090422 > rest_spm5_files > nic_spm_hread.m

nic_spm_hread

PURPOSE ^

reads a header

SYNOPSIS ^

function [DIM,VOX,SCALE,TYPE,OFFSET,ORIGIN,DESCRIP] = nic_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 ^

DOWNLOAD ^

nic_spm_hread.m

SOURCE CODE ^

0001 function [DIM,VOX,SCALE,TYPE,OFFSET,ORIGIN,DESCRIP] = nic_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','native');
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 if sizeof_hdr==1543569408, % Appears to be other-endian
0046     % Re-open other-endian
0047     fclose(fid);
0048     if nic_spm_platform('bigend'),
0049         fid = fopen(P,'r','ieee-le');
0050     else,
0051         fid = fopen(P,'r','ieee-be');
0052     end;
0053     fseek(fid,0,'bof');
0054     sizeof_hdr = fread(fid,1,'int32');
0055     otherendian = 1;
0056 end;
0057 
0058 data_type      = mysetstr(fread(fid,10,'uchar'))';
0059 db_name        = mysetstr(fread(fid,18,'uchar'))';
0060 extents        = fread(fid,1,'int32');
0061 session_error   = fread(fid,1,'int16');
0062 regular        = mysetstr(fread(fid,1,'uchar'))';
0063 hkey_un0        = mysetstr(fread(fid,1,'uchar'))';
0064 
0065 
0066 
0067 % read (struct) image_dimension
0068 %---------------------------------------------------------------------------
0069 fseek(fid,40,'bof');
0070 
0071 dim            = fread(fid,8,'int16');
0072 vox_units        = mysetstr(fread(fid,4,'uchar'))';
0073 cal_units        = mysetstr(fread(fid,8,'uchar'))';
0074 unused1        = fread(fid,1,'int16');
0075 datatype    = fread(fid,1,'int16');
0076 bitpix        = fread(fid,1,'int16');
0077 dim_un0        = fread(fid,1,'int16');
0078 pixdim        = fread(fid,8,'float');
0079 vox_offset    = fread(fid,1,'float');
0080 funused1    = fread(fid,1,'float');
0081 funused2    = fread(fid,1,'float');
0082 funused3    = fread(fid,1,'float');
0083 cal_max        = fread(fid,1,'float');
0084 cal_min        = fread(fid,1,'float');
0085 compressed    = fread(fid,1,'int32');
0086 verified    = fread(fid,1,'int32');
0087 glmax        = fread(fid,1,'int32');
0088 glmin        = fread(fid,1,'int32');
0089 
0090 % read (struct) data_history
0091 %---------------------------------------------------------------------------
0092 fseek(fid,148,'bof');
0093 
0094 descrip        = mysetstr(fread(fid,80,'uchar'))';
0095 aux_file    = mysetstr(fread(fid,24,'uchar'))';
0096 orient        = fread(fid,1,'uchar');
0097 origin        = fread(fid,5,'int16');
0098 generated    = mysetstr(fread(fid,10,'uchar'))';
0099 scannum        = mysetstr(fread(fid,10,'uchar'))';
0100 patient_id    = mysetstr(fread(fid,10,'uchar'))';
0101 exp_date    = mysetstr(fread(fid,10,'uchar'))';
0102 exp_time    = mysetstr(fread(fid,10,'uchar'))';
0103 hist_un0    = mysetstr(fread(fid,3,'uchar'))';
0104 views        = fread(fid,1,'int32');
0105 vols_added    = fread(fid,1,'int32');
0106 start_field    = fread(fid,1,'int32');
0107 field_skip    = fread(fid,1,'int32');
0108 omax        = fread(fid,1,'int32');
0109 omin        = fread(fid,1,'int32');
0110 smax        = fread(fid,1,'int32');
0111 smin        = fread(fid,1,'int32');
0112 
0113 fclose(fid);
0114 
0115 if isempty(smin)
0116     error(['There is a problem with the header file ' P '.']);
0117 end
0118 
0119 % convert to SPM global variables
0120 %---------------------------------------------------------------------------
0121 DIM              = dim(2:4)';
0122 VOX           = pixdim(2:4)';
0123 SCALE         = funused1;
0124 SCALE        = ~SCALE + SCALE;
0125 TYPE         = datatype;
0126 if otherendian == 1 & datatype ~= 2,
0127     TYPE = TYPE*256;
0128 end;
0129 OFFSET          = vox_offset;
0130 ORIGIN        = origin(1:3)';
0131 DESCRIP       = descrip(1:max(find(descrip)));
0132 
0133 else
0134     global DIM VOX SCALE TYPE OFFSET ORIGIN
0135     DESCRIP = ['defaults'];
0136 end
0137 return;
0138 %_______________________________________________________________________
0139 
0140 function out = mysetstr(in)
0141 tmp = find(in == 0);
0142 tmp = min([min(tmp) length(in)]);
0143 out = setstr(in(1:tmp));
0144 return;

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005