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

read_hdr

PURPOSE ^

Get a variety of information from a NIFTI-1 header.

SYNOPSIS ^

function vol = read_hdr(fname)

DESCRIPTION ^

 Get a variety of information from a NIFTI-1 header.
 FORMAT vol = read_hdr(fname)
 fname - filename of image
 vol   - various bits of information
 _______________________________________________________________________
 Copyright (C) 2005 Wellcome Department of Imaging Neuroscience

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

read_hdr.m

SOURCE CODE ^

0001 function vol = read_hdr(fname)
0002 % Get a variety of information from a NIFTI-1 header.
0003 % FORMAT vol = read_hdr(fname)
0004 % fname - filename of image
0005 % vol   - various bits of information
0006 % _______________________________________________________________________
0007 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
0008 
0009 %
0010 % $Id: read_hdr.m 253 2005-10-13 15:31:34Z guillaume $
0011 
0012 
0013 persistent d
0014 if isempty(d), d = getdict; end;
0015 
0016 [pth,nam,ext] = fileparts(fname);
0017 switch ext
0018 case '.hdr'
0019     hname = fullfile(pth,[nam '.hdr']);
0020 case '.HDR'
0021     hname = fullfile(pth,[nam '.HDR']);
0022 case '.img'
0023     hname = fullfile(pth,[nam '.hdr']);
0024 case '.IMG'
0025     hname = fullfile(pth,[nam '.HDR']);
0026 case '.nii'
0027     hname = fullfile(pth,[nam '.nii']);
0028 case '.NII'
0029     hname = fullfile(pth,[nam '.NII']);
0030 otherwise
0031     hname = fullfile(pth,[nam ext]);
0032 end
0033 [hdr,be] = read_hdr_raw(hname);
0034 if isempty(hdr)
0035     error(['Error reading header file "' hname '"']);
0036 end;
0037 
0038 if ~isfield(hdr,'magic'),
0039     % A patch for some possible SPM2 datatypes
0040     switch hdr.datatype,
0041     case 130, hdr.datatype = 256; %  int8
0042     case 132, hdr.datatype = 512; % uint16
0043     case 136, hdr.datatype = 768; % uint32
0044     end;
0045 end;
0046 
0047 dt = [];
0048 for i=1:length(d.dtype)
0049     if hdr.datatype == d.dtype(i).code
0050         dt = d.dtype(i);
0051         break;
0052     end;
0053 end;
0054 if isempty(dt)
0055     error(['Unrecognised datatype (' num2str(double(hdr.datatype)) ') for "' fname '.'] );
0056 end
0057 if isfield(hdr,'magic')
0058     switch deblank(hdr.magic)
0059     case {'n+1'}
0060         iname = hname;
0061         if hdr.vox_offset < hdr.sizeof_hdr
0062             error(['Bad vox_offset (' num2str(double(hdr.vox_offset)) ') for "' fname '.'] );
0063         end
0064     case {'ni1'}
0065         if strcmp(ext,lower(ext)),
0066             iname = fullfile(pth,[nam '.img']);
0067         else
0068             iname = fullfile(pth,[nam '.IMG']);
0069         end;
0070     otherwise
0071         error(['Bad magic (' hdr.magic ') for "' fname '.'] );
0072     end
0073 else
0074     if strcmp(ext,lower(ext)),
0075         iname = fullfile(pth,[nam '.img']);
0076     else
0077         iname = fullfile(pth,[nam '.IMG']);
0078     end;
0079 end
0080 if rem(double(hdr.vox_offset),dt.size)
0081    error(['Bad alignment of voxels (' num2str(double(hdr.vox_offset)) '/' num2str(double(dt.size)) ') for "' fname '.'] );
0082 end;
0083 
0084 vol      = struct('hdr',hdr,'be',be,'hname',hname,'iname',iname);
0085 return

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