Home > marsbar > spm99 > spm_read_hdr.m

spm_read_hdr

PURPOSE ^

SPM2 routine to read (SPM customised) Analyze header

SYNOPSIS ^

function [hdr,otherendian] = spm_read_hdr(fname)

DESCRIPTION ^

 SPM2 routine to read (SPM customised) Analyze header 
 (Needed for compatibility with SPM99)
 FORMAT [hdr,otherendian] = spm_read_hdr(fname)
 fname       - .hdr filename
 hdr         - structure containing Analyze header
 otherendian - byte swapping necessary flag
_______________________________________________________________________
 @(#)spm_read_hdr.m    2.2 John Ashburner 03/07/17
 
 $Id$

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [hdr,otherendian] = spm_read_hdr(fname)
0002 % SPM2 routine to read (SPM customised) Analyze header
0003 % (Needed for compatibility with SPM99)
0004 % FORMAT [hdr,otherendian] = spm_read_hdr(fname)
0005 % fname       - .hdr filename
0006 % hdr         - structure containing Analyze header
0007 % otherendian - byte swapping necessary flag
0008 %_______________________________________________________________________
0009 % @(#)spm_read_hdr.m    2.2 John Ashburner 03/07/17
0010 %
0011 % $Id$
0012   
0013 fid         = fopen(fname,'r','native');
0014 otherendian = 0;
0015 if (fid > 0)
0016     dime = read_dime(fid);
0017     if dime.dim(1)<0 | dime.dim(1)>15, % Appears to be other-endian
0018         % Re-open other-endian
0019         fclose(fid);
0020         if spm_platform('bigend'), fid = fopen(fname,'r','ieee-le');
0021         else,                      fid = fopen(fname,'r','ieee-be'); end;
0022         otherendian = 1;
0023         dime = read_dime(fid);
0024     end;
0025     hk       = read_hk(fid);
0026     hist     = read_hist(fid);
0027     hdr.hk   = hk;
0028     hdr.dime = dime;
0029     hdr.hist = hist;
0030 
0031     % SPM specific bit - unused
0032     %if hdr.hk.sizeof_hdr > 348,
0033     %    spmf = read_spmf(fid,dime.dim(5));
0034     %    if ~isempty(spmf),
0035     %        hdr.spmf = spmf;
0036     %    end;
0037     %end;
0038 
0039     fclose(fid);
0040 else,
0041     hdr = [];
0042     otherendian = NaN;
0043     %error(['Problem opening header file (' fopen(fid) ').']);
0044 end;
0045 return;
0046 %_______________________________________________________________________
0047 %_______________________________________________________________________
0048 function hk = read_hk(fid)
0049 % read (struct) header_key
0050 %-----------------------------------------------------------------------
0051 fseek(fid,0,'bof');
0052 hk.sizeof_hdr         = fread(fid,1,'int32');
0053 hk.data_type          = mysetstr(fread(fid,10,'uchar'))';
0054 hk.db_name            = mysetstr(fread(fid,18,'uchar'))';
0055 hk.extents            = fread(fid,1,'int32');
0056 hk.session_error    = fread(fid,1,'int16');
0057 hk.regular            = mysetstr(fread(fid,1,'uchar'))';
0058 hk.hkey_un0            = mysetstr(fread(fid,1,'uchar'))';
0059 if isempty(hk.hkey_un0), error(['Problem reading "hk" of header file (' fopen(fid) ').']); end;
0060 return;
0061 %_______________________________________________________________________
0062 %_______________________________________________________________________
0063 function dime = read_dime(fid)
0064 % read (struct) image_dimension
0065 %-----------------------------------------------------------------------
0066 fseek(fid,40,'bof');
0067 dime.dim        = fread(fid,8,'int16')';
0068 dime.vox_units    = mysetstr(fread(fid,4,'uchar'))';
0069 dime.cal_units    = mysetstr(fread(fid,8,'uchar'))';
0070 dime.unused1    = fread(fid,1,'int16');
0071 dime.datatype    = fread(fid,1,'int16');
0072 dime.bitpix        = fread(fid,1,'int16');
0073 dime.dim_un0    = fread(fid,1,'int16');
0074 dime.pixdim        = fread(fid,8,'float')';
0075 dime.vox_offset    = fread(fid,1,'float');
0076 dime.funused1    = fread(fid,1,'float');
0077 dime.funused2    = fread(fid,1,'float');
0078 dime.funused3    = fread(fid,1,'float');
0079 dime.cal_max    = fread(fid,1,'float');
0080 dime.cal_min    = fread(fid,1,'float');
0081 dime.compressed    = fread(fid,1,'int32');
0082 dime.verified    = fread(fid,1,'int32');
0083 dime.glmax        = fread(fid,1,'int32');
0084 dime.glmin        = fread(fid,1,'int32');
0085 if isempty(dime.glmin), error(['Problem reading "dime" of header file (' fopen(fid) ').']); end;
0086 return;
0087 %_______________________________________________________________________
0088 %_______________________________________________________________________
0089 function hist = read_hist(fid)
0090 % read (struct) data_history
0091 %-----------------------------------------------------------------------
0092 fseek(fid,148,'bof');
0093 hist.descrip    = mysetstr(fread(fid,80,'uchar'))';
0094 hist.aux_file    = mysetstr(fread(fid,24,'uchar'))';
0095 hist.orient        = fread(fid,1,'uchar');
0096 hist.origin        = fread(fid,5,'int16')';
0097 hist.generated    = mysetstr(fread(fid,10,'uchar'))';
0098 hist.scannum    = mysetstr(fread(fid,10,'uchar'))';
0099 hist.patient_id    = mysetstr(fread(fid,10,'uchar'))';
0100 hist.exp_date    = mysetstr(fread(fid,10,'uchar'))';
0101 hist.exp_time    = mysetstr(fread(fid,10,'uchar'))';
0102 hist.hist_un0    = mysetstr(fread(fid,3,'uchar'))';
0103 hist.views        = fread(fid,1,'int32');
0104 hist.vols_added    = fread(fid,1,'int32');
0105 hist.start_field= fread(fid,1,'int32');
0106 hist.field_skip    = fread(fid,1,'int32');
0107 hist.omax        = fread(fid,1,'int32');
0108 hist.omin        = fread(fid,1,'int32');
0109 hist.smax        = fread(fid,1,'int32');
0110 hist.smin        = fread(fid,1,'int32');
0111 if isempty(hist.smin), error(['Problem reading "hist" of header file (' fopen(fid) ').']); end;
0112 return;
0113 %_______________________________________________________________________
0114 %_______________________________________________________________________
0115 function spmf = read_spmf(fid,n)
0116 % Read SPM specific fields
0117 % This bit may be used in the future for extending the Analyze header.
0118 
0119 fseek(fid,348,'bof');
0120 mgc = fread(fid,1,'int32');    % Magic number
0121 if mgc ~= 20020417, spmf = []; return; end;
0122 
0123 for j=1:n,
0124     spmf(j).mat    = fread(fid,16,'double'); % Orientation information
0125     spmf(j).unused = fread(fid,384,'uchar'); % Extra unused stuff
0126     if length(spmf(j).unused)<384,
0127         error(['Problem reading "spmf" of header file (' fopen(fid) ').']);
0128     end;
0129      spmf(j).mat = reshape(spmf(j).mat,[4 4]);
0130 end;
0131 return;
0132 %_______________________________________________________________________
0133 %_______________________________________________________________________
0134 function out = mysetstr(in)
0135 tmp = find(in == 0);
0136 tmp = min([min(tmp) length(in)]);
0137 out = setstr([in(1:tmp)' zeros(1,length(in)-(tmp))])';
0138 return;
0139 %_______________________________________________________________________
0140 %_______________________________________________________________________

Generated on Wed 11-May-2022 16:26:09 by m2html © 2003-2019