Home > rest_20090422 > rest_spm5_files > @read_nifti > private > read_hdr_raw.m

read_hdr_raw

PURPOSE ^

Read a NIFTI-1 hdr file

SYNOPSIS ^

function [hdr,be] = read_hdr_raw(fname)

DESCRIPTION ^

 Read a NIFTI-1 hdr file
 FORMAT [hdr,be] = read_hdr_raw(fname)
 fname - filename of image
 hdr   - a structure containing hdr info
 be    - whether big-endian or not
 _______________________________________________________________________
 Copyright (C) 2005 Wellcome Department of Imaging Neuroscience

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

read_hdr_raw.m

SOURCE CODE ^

0001 function [hdr,be] = read_hdr_raw(fname)
0002 % Read a NIFTI-1 hdr file
0003 % FORMAT [hdr,be] = read_hdr_raw(fname)
0004 % fname - filename of image
0005 % hdr   - a structure containing hdr info
0006 % be    - whether big-endian or not
0007 % _______________________________________________________________________
0008 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
0009 
0010 %
0011 % $Id: read_hdr_raw.m 253 2005-10-13 15:31:34Z guillaume $
0012 
0013 
0014 hdr = [];
0015 be  = [];
0016 ok  = true;
0017 
0018 % Name of header file
0019 [pth,nam,ext] = fileparts(fname);
0020 switch ext
0021 case {'.img','.hdr'}
0022     hname = fullfile(pth,[nam '.hdr']);
0023 case {'.nii'}
0024     hname = fullfile(pth,[nam '.nii']);
0025 otherwise
0026     hname = fullfile(pth,[nam '.hdr']);
0027 end;
0028 
0029 % Open it if possible
0030 fp  = fopen(hname,'r','native');
0031 if fp==-1
0032     hdr = [];
0033     return;
0034 end;
0035 
0036 % Sort out endian issues of header
0037 [unused,unused,mach] = fopen(fp);
0038 if strcmp(mach,'ieee-be')
0039     be = true;
0040 else
0041     be = false;
0042 end;
0043 fseek(fp,0,'bof');
0044 fseek(fp,40,'bof');
0045 nd = fread(fp,1,'int16')';
0046 if isempty(nd),
0047     fclose(fp);
0048     hdr = [];
0049     return;
0050 elseif nd<1 || nd>7
0051     be = ~be;
0052     fclose(fp);
0053     if be, mach = 'ieee-be';
0054     else   mach = 'ieee-le';
0055     end;
0056     fp = fopen(hname,'r',mach);
0057     if fp==-1
0058         hdr = [];
0059         return;
0060     end;
0061 end;
0062 
0063 % Is it NIFTI or not
0064 fseek(fp,0,'bof');
0065 fseek(fp,344,'bof');
0066 mgc = deblank(char(fread(fp,4,'uint8')'));
0067 switch mgc
0068 case {'ni1','n+1'}
0069     org = niftistruc;
0070 otherwise
0071     org = mayostruc;
0072 end;
0073 fseek(fp,0,'bof');
0074 % Read the fields
0075 for i=1:length(org)
0076     tmp = fread(fp,org(i).len,['*' org(i).dtype.prec])';
0077     if length(tmp) ~= org(i).len
0078 disp([length(tmp) org(i).len]);
0079         tmp = org(i).def;
0080         ok  = false;
0081     end;
0082     tmp = feval(org(i).dtype.conv,tmp);
0083     hdr.(org(i).label) = tmp;
0084 end;
0085 
0086 fclose(fp);
0087 if ~ok,
0088      fprintf('There was a problem reading the header of\n');
0089      fprintf('"%s".\n', fname);
0090      fprintf('It may be corrupted in some way.');
0091 end;
0092 return;
0093

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