Home > rest_20090422 > rest_spm5_files > nic_spm_read_hdr.m

nic_spm_read_hdr

PURPOSE ^

Read (SPM customised) Analyze header

SYNOPSIS ^

function [hdr,otherendian] = nic_spm_read_hdr(fname)

DESCRIPTION ^

 Read (SPM customised) Analyze header
 FORMAT [hdr,otherendian] = spm_read_hdr(fname)
 fname       - .hdr filename
 hdr         - structure containing Analyze header
 otherendian - byte swapping necessary flag
_______________________________________________________________________
 Copyright (C) 2005 Wellcome Department of Imaging Neuroscienic_e

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

nic_spm_read_hdr.m

SOURCE CODE ^

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

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