¢£ INTRODUCTION TO NIFTI-1:
Most of the unused fields in the ANALYZE 7.5 header have been taken,
and some of the lesser-used fields have been co-opted for other purposes.

The header consists of the 348 byte as the ANALYZE 7.5 format

¢£NIFTI-1 FLAG (MAGIC STRINGS):
----------------------------
To flag such a struct as being conformant to the NIFTI-1 spec, 
the last 4 bytes of the header must be either the C String "ni1" or "n+1";

%      nii = load_nii(filename)

%  --- File type check code
%  if strcmp(hdr.hist.magic, 'n+1')      %  NIFTI  .nii
%     filetype = 2; 
%  elseif strcmp(hdr.hist.magic, 'ni1')  %  NIFTI  .hdr/.img
%     filetype = 1;
%  else                                  % Analyze .hdr/.img
%     filetype = 0;
%  end

%  --- File type check field
%  nii.filetype =
%     0 : Analyze .hdr/.img , (.hdr : 348 byte)
%     1 : NIFTI   .hdr/.img , (.hdr : 348 byte)
%         hdr.dime.vox_offset = 0;
%     2 : NIFTI   .nii      , (.nii : 348 byte hdr + 4 bite skip + image data)
%         hdr.dime.vox_offset = 352;

%  --- Data structure
%  nii.hdr - struct with NIFTI header fields.
%  nii.fileprefix - NIFTI filename without extension.
%  nii.machine - machine string variable.
%  nii.img - 3D (or 4D) matrix of NIFTI data.

%  --- Nifti & Analyze hdr-structure
%     Nifti                                    Analyze
% header_key
hk.sizeof_hdr    = ( 1,'int32')         hk.sizeof_hdr    = ( 1,'*int32')
hk.data_type     = (10,'*char')         hk.data_type     = (10,'*char')
hk.db_name       = (18,'*char')         hk.db_name       = (18,'*char')
hk.extents       = ( 1,'int32')         hk.extents       = ( 1,'*int32')
hk.session_error = ( 1,'int16')         hk.session_error = ( 1,'*int16')
hk.regular       = ( 1,'*char')         hk.regular       = ( 1,'*char')
hk.dim_info      = ( 1,'char')     >     hk.hkey_un0      = ( 1,'*uint8')

% image_dimension
dime.dim        = (8,'int16')           dime.dim        = (8,'*int16')
dime.intent_p1  = (1,'float32')   >      dime.vox_units  = (4,'*char')
dime.intent_p2  = (1,'float32')   >      dime.cal_units  = (8,'*char')
dime.intent_p3  = (1,'float32')   >      
dime.intent_code = (1,'int16')    >      dime.unused1    = (1,'*int16')
dime.datatype   = (1,'int16')           dime.datatype   = (1,'*int16')
dime.bitpix     = (1,'int16')           dime.bitpix     = (1,'*int16')
dime.slice_start = (1,'int16')    >      dime.dim_un0    = (1,'*int16')
dime.pixdim     = (8,'float32')         dime.pixdim     = (8,'*float')
dime.vox_offset = (1,'float32')         dime.vox_offset = (1,'*float')
dime.scl_slope  = (1,'float32')   >      dime.roi_scale  = (1,'*float')
dime.scl_inter  = (1,'float32')   >      dime.funused1   = (1,'*float')
dime.slice_end  = (1,'int16')     >      dime.funused2   = (1,'*float')
dime.slice_code = (1,'char')      >      
dime.xyzt_units = (1,'char')      >      
dime.cal_max    = (1,'float32')         dime.cal_max    = (1,'*float')
dime.cal_min    = (1,'float32')         dime.cal_min    = (1,'*float')
dime.slice_duration = (1,'float32')  >   dime.compressed = (1,'*int32')
dime.toffset    = (1,'float32')      >   dime.verified   = (1,'*int32')
dime.glmax      = (1,'int32')           dime.glmax      = (1,'*int32')
dime.glmin      = (1,'int32')           dime.glmin      = (1,'*int32')

% data_history
hist.descrip     = (80,'*char')         hist.descrip     = (80,'*char')
hist.aux_file    = (24,'*char')         hist.aux_file    = (24,'*char')
hist.qform_code  = (1,'int16')          hist.orient      = ( 1,'*uint8')
hist.sform_code  = (1,'int16')          hist.originator  = ( 5,'int16')
hist.quatern_b   = (1,'float32')
hist.quatern_c   = (1,'float32')

hist.quatern_d   = (1,'float32')        hist.generated   = (10,'*char')';
hist.qoffset_x   = (1,'float32')        hist.scannum     = (10,'*char')';
hist.qoffset_y   = (1,'float32')        hist.patient_id  = (10,'*char')';
hist.qoffset_z   = (1,'float32')        hist.exp_date    = (10,'*char')';
hist.srow_x      = (4,'float32')        hist.exp_time    = (10,'*char')';
hist.srow_y      = (4,'float32')        hist.hist_un0    = ( 3,'*char')';
hist.srow_z      = (4,'float32')        hist.views       = ( 1,'*int32');
hist.intent_name = (16,'*char')         hist.vols_added  = ( 1,'*int32');
hist.magic       = (4,'*char')          hist.start_field = ( 1,'*int32');
                                        hist.field_skip  = ( 1,'*int32');
	                                    hist.omax        = ( 1,'*int32');
	                                    hist.omin        = ( 1,'*int32');
	                                    hist.smax        = ( 1,'*int32');
	                                    hist.smin        = ( 1,'*int32');

% For SPM Analyze compatibility
fseek(fid,253,'bof');
hist.originator  = fread(fid, 5,'int16')';

% --- The following fields are common with Analyze 

% dim[0]      Number of dimensions in database; usually 4. 
% dim[1]      Image X dimension; number of pixels in an image row. 
% dim[2]      Image Y dimension; number of pixel rows in slice. 
% dim[3]      Volume Z dimension; number of slices in a volume. 
% dim[4]      Time points; number of volumes in database.
% 
% datatype         /* Datatype for this image set */ 
% bitpix;          /* Number of bits per pixel; 1, 8, 16, 32, or 64. */ 
% 
% pixdim[0];    Pixel dimensions? 
% pixdim[1];    Voxel width in mm. 
% pixdim[2];    Voxel height in mm. 
% pixdim[3];    Slice thickness in mm. 
% pixdim[4];    timeslice in ms (ie, TR in fMRI). 
% 
% vox_offset;   Byte offset in the .img file at which voxels data start.
% 
% int glmax;    The maximum pixel value for the entire database. 
% int glmin;    The minimum pixel value for the entire database. 

% --- SPM variables for Analyze header
%DIM    	= dim(2:4)';	
%VOX       	= pixdim(2:4)';	
%TYPE     	= datatype;
%OFFSET	  	= vox_offset;
%ORIGIN    	= originator(1:3)';
%DESCRIP   	= descrip(1:max(find(descrip)));
%SCALE     	= funused1; -> Changed

% --- Orientation information in NIFTI
% hist.qform_code  
% hist.sform_code  
% hist.quatern_b   hist.quatern_c   hist.quatern_d   
% hist.qoffset_x   hist.qoffset_y   hist.qoffset_z   
% hist.srow_x      hist.srow_y      hist.srow_z      

% --- sform 
% i = 0 .. dim[1]-1
% j = 0 .. dim[2]-1
% k = 0 .. dim[3]-1
% x = srow_x[0] * i + srow_x[1] * j + srow_x[2] * k + srow_x[3]
% y = srow_y[0] * i + srow_y[1] * j + srow_y[2] * k + srow_y[3]
% z = srow_z[0] * i + srow_z[1] * j + srow_z[2] * k + srow_z[3]
% --- matrix form
% [x ; y ; z] = R * [i ; j ; k] + T

