function header=tfiReadAnalyzeHeader(filename,usespm_orig);
% header=tfiReadAnalyzeHeader(filename,usespm_orig);
%
% Loads an Analyze file header with TFI extensions (see tfiWriteAnalyze for info)
%
% Arguments: 
% filename:     Analyze file name (.hdr or .img extension is optional)
% use_spm_orig: if defined and ~=0, will derive origin from the SPM originator field
%               instead of pixdim(6:8) as used by TFI programs. Note that the SPM origin 
%               is integer-based, 1-offset, and in voxel units, whereas the TFI origin 
%               is floating point, 0-offset, and in world units (mm).
% 
% JJL 4/1/2003. Based on spm_hread from SPM99 distribution.

[pathstr,name,ext,versn] = fileparts( filename );
hdrfilename = fullfile( pathstr, [name '.hdr']);

% open header file
%---------------------------------------------------------------------------
fid   = fopen(hdrfilename,'r','native');

if (fid == -1) error('Could not open header file!'); end

% read (struct) header_key
%---------------------------------------------------------------------------
fseek(fid,0,'bof');

otherendian = 0;
sizeof_hdr 	= fread(fid,1,'int32');
if sizeof_hdr~=348, % Appears to be other-endian
	% Re-open other-endian
	fclose(fid);
	[c,mxsz,endian]=computer;
	if (endian=='B')
		fid = fopen(hdrfilename,'r','ieee-le');
	else,
		fid = fopen(hdrfilename,'r','ieee-be');
	end;
	fseek(fid,0,'bof');
	sizeof_hdr = fread(fid,1,'int32');
	otherendian = 1;
end;

data_type  	= mysetstr(fread(fid,10,'uchar'))';
db_name    	= mysetstr(fread(fid,18,'uchar'))';
extents    	= fread(fid,1,'int32');
session_error   = fread(fid,1,'int16');
regular    	= mysetstr(fread(fid,1,'uchar'))';
hkey_un0    	= mysetstr(fread(fid,1,'uchar'))';

% read (struct) image_dimension
%---------------------------------------------------------------------------
fseek(fid,40,'bof');

dim    		= fread(fid,8,'int16');
vox_units    	= mysetstr(fread(fid,4,'uchar'))';
cal_units    	= mysetstr(fread(fid,8,'uchar'))';
unused1		= fread(fid,1,'int16');
datatype	= fread(fid,1,'int16');
bitpix		= fread(fid,1,'int16');
dim_un0		= fread(fid,1,'int16');
pixdim		= fread(fid,8,'float');
vox_offset	= fread(fid,1,'float');
funused1	= fread(fid,1,'float');
funused2	= fread(fid,1,'float');
funused3	= fread(fid,1,'float');
cal_max		= fread(fid,1,'float');
cal_min		= fread(fid,1,'float');
compressed	= fread(fid,1,'int32');
verified	= fread(fid,1,'int32');
glmax		= fread(fid,1,'int32');
glmin		= fread(fid,1,'int32');

% read (struct) data_history
%---------------------------------------------------------------------------
fseek(fid,148,'bof');

descrip		= mysetstr(fread(fid,80,'uchar'))';
aux_file	= mysetstr(fread(fid,24,'uchar'))';
orient		= fread(fid,1,'uchar');
origin		= fread(fid,5,'int16');
generated	= mysetstr(fread(fid,10,'uchar'))';
scannum		= mysetstr(fread(fid,10,'uchar'))';
patient_id	= mysetstr(fread(fid,10,'uchar'))';
exp_date	= mysetstr(fread(fid,10,'uchar'))';
exp_time	= mysetstr(fread(fid,10,'uchar'))';
hist_un0	= mysetstr(fread(fid,3,'uchar'))';
views		= fread(fid,1,'int32');
vols_added	= fread(fid,1,'int32');
start_field	= fread(fid,1,'int32');
field_skip	= fread(fid,1,'int32');
omax		= fread(fid,1,'int32');
omin		= fread(fid,1,'int32');
smax		= fread(fid,1,'int32');
smin		= fread(fid,1,'int32');

fclose(fid);

if ( ~exist( 'usespm_orig' )) usespm_orig=0; end
if ( usespm_orig | pixdim(6:8)'==[0 0 0] ) 
  header.origin=(origin(1:3)'-1).*pixdim(2:4)';
else
  header.origin=pixdim(6:8)';
end
header.datasize=dim(2:5)';
if (header.datasize(4)<2) 
  header.datasize=header.datasize(1:3);
end
header.voxelsize=pixdim(2:5)';
header.scale=funused1;
switch (datatype)
 case 2
  header.datatype='uint8';
  header.nBytes=1;
 case 4
  header.datatype='int16';
  header.nBytes=2;
 case 8
  header.datatype='int32';
  header.nBytes=4;
 case 16
  header.datatype='float';
  header.nBytes=4;
 case 64
  header.datatype='double';
  header.nBytes=8;
 otherwise
  error('Unsupported Analyze data type');
end

header.descrip=descrip;

[c,mxsz,endian]=computer;
switch (endian) 
 case 'L'
  if (otherendian) 
    header.byteorder='ieee-be';
  else
    header.byteorder='ieee-le';
  end
 case 'B'   
  if (otherendian) 
    header.byteorder='ieee-le';
  else
    header.byteorder='ieee-be';
  end
 otherwise
    header.byteorder='unknown';  
end

return

function out = mysetstr(in)
tmp = find(in == 0);
tmp = min([min(tmp) length(in)]);
out = setstr(in(1:tmp));
return;
