Home > vbmeg > external > NIFTI > load_nii_hdr_cbi.m

load_nii_hdr_cbi

PURPOSE ^

Load NIFTI dataset header. Support both *.nii and *.hdr/*.img file

SYNOPSIS ^

function [hdr, filetype, fileprefix, machine] = load_nii_hdr_cbi(fileprefix)

DESCRIPTION ^

  Load NIFTI dataset header. Support both *.nii and *.hdr/*.img file
  extension. If file extension is not provided, *.hdr/*.img will be 
  used as default.
  
  Usage: [hdr, filetype, fileprefix, machine] = load_nii_hdr_cbi(filename)
  
  filename - NIFTI file name.
  
  Returned values:
  
  hdr - struct with NIFTI header fields.
  
  filetype    - 0 for Analyze format (*.hdr/*.img);
          1 for NIFTI format in 2 files (*.hdr/*.img);
          2 for NIFTI format in 1 file (*.nii).
  
  fileprefix - NIFTI file name without extension.
  
  machine    - a string, see below for details. The default here is 'ieee-le'.

    'native'      or 'n' - local machine format - the default
    'ieee-le'     or 'l' - IEEE floating point with little-endian
                           byte ordering
    'ieee-be'     or 'b' - IEEE floating point with big-endian
                           byte ordering
    'vaxd'        or 'd' - VAX D floating point and VAX ordering
    'vaxg'        or 'g' - VAX G floating point and VAX ordering
    'cray'        or 'c' - Cray floating point with big-endian
                           byte ordering
    'ieee-le.l64' or 'a' - IEEE floating point with little-endian
                           byte ordering and 64 bit long data type
    'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
                           ordering and 64 bit long data type.
  
  Number of scanned images in the file can be obtained by:
  num_scan = hdr.dime.dim(5)

  Part of this file is copied and modified under GNU license from
  MRI_TOOLBOX developed by CNSP in Flinders University, Australia

  NIFTI data format can be found on: http://nifti.nimh.nih.gov

  - Jimmy Shen (pls@rotman-baycrest.on.ca)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 %  Load NIFTI dataset header. Support both *.nii and *.hdr/*.img file
0002 %  extension. If file extension is not provided, *.hdr/*.img will be
0003 %  used as default.
0004 %
0005 %  Usage: [hdr, filetype, fileprefix, machine] = load_nii_hdr_cbi(filename)
0006 %
0007 %  filename - NIFTI file name.
0008 %
0009 %  Returned values:
0010 %
0011 %  hdr - struct with NIFTI header fields.
0012 %
0013 %  filetype    - 0 for Analyze format (*.hdr/*.img);
0014 %          1 for NIFTI format in 2 files (*.hdr/*.img);
0015 %          2 for NIFTI format in 1 file (*.nii).
0016 %
0017 %  fileprefix - NIFTI file name without extension.
0018 %
0019 %  machine    - a string, see below for details. The default here is 'ieee-le'.
0020 %
0021 %    'native'      or 'n' - local machine format - the default
0022 %    'ieee-le'     or 'l' - IEEE floating point with little-endian
0023 %                           byte ordering
0024 %    'ieee-be'     or 'b' - IEEE floating point with big-endian
0025 %                           byte ordering
0026 %    'vaxd'        or 'd' - VAX D floating point and VAX ordering
0027 %    'vaxg'        or 'g' - VAX G floating point and VAX ordering
0028 %    'cray'        or 'c' - Cray floating point with big-endian
0029 %                           byte ordering
0030 %    'ieee-le.l64' or 'a' - IEEE floating point with little-endian
0031 %                           byte ordering and 64 bit long data type
0032 %    'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
0033 %                           ordering and 64 bit long data type.
0034 %
0035 %  Number of scanned images in the file can be obtained by:
0036 %  num_scan = hdr.dime.dim(5)
0037 %
0038 %  Part of this file is copied and modified under GNU license from
0039 %  MRI_TOOLBOX developed by CNSP in Flinders University, Australia
0040 %
0041 %  NIFTI data format can be found on: http://nifti.nimh.nih.gov
0042 %
0043 %  - Jimmy Shen (pls@rotman-baycrest.on.ca)
0044 %
0045 function [hdr, filetype, fileprefix, machine] = load_nii_hdr_cbi(fileprefix)
0046 
0047    if ~exist('fileprefix','var'),
0048       error('Usage: [hdr, filetype, fileprefix, machine] = load_nii_hdr_cbi(filename)');
0049    end
0050 
0051    if ~exist('machine','var'), machine = 'ieee-le'; end
0052 
0053    new_ext = 0;
0054 
0055    if findstr('.nii',fileprefix)
0056       new_ext = 1;
0057       fileprefix = strrep(fileprefix,'.nii','');
0058    end
0059 
0060    if findstr('.hdr',fileprefix)
0061       fileprefix = strrep(fileprefix,'.hdr','');
0062    end
0063 
0064    if findstr('.img',fileprefix)
0065       fileprefix = strrep(fileprefix,'.img','');
0066    end
0067 
0068    if new_ext
0069       fn = sprintf('%s.nii',fileprefix);
0070 
0071       if ~exist(fn)
0072          msg = sprintf('Cannot find file "%s.nii".', fileprefix);
0073          error(msg);
0074       end
0075    else
0076       fn = sprintf('%s.hdr',fileprefix);
0077 
0078       if ~exist(fn)
0079          msg = sprintf('Cannot find file "%s.hdr".', fileprefix);
0080          error(msg);
0081       end
0082    end
0083 
0084    % rhayashi added encoding
0085    fid = fopen_ascii_encoding(fn, 'r', machine);
0086     
0087    if fid < 0,
0088       msg = sprintf('Cannot open file %s.',fn);
0089       error(msg);
0090    else
0091       hdr = read_header(fid);
0092       fclose(fid);
0093    end
0094     
0095    if hdr.hk.sizeof_hdr ~= 348
0096       % first try reading the opposite endian to 'machine'
0097       switch machine,
0098       case 'ieee-le', machine = 'ieee-be';
0099       case 'ieee-be', machine = 'ieee-le';
0100       end
0101 
0102       % rhayashi added encoding
0103       fid = fopen_ascii_encoding(fn, 'r', machine);
0104         
0105       if fid < 0,
0106          msg = sprintf('Cannot open file %s.',fn);
0107          error(msg);
0108       else
0109          hdr = read_header(fid);
0110          fclose(fid);
0111       end
0112    end
0113 
0114    if hdr.hk.sizeof_hdr ~= 348
0115       % Now throw an error
0116       msg = sprintf('File "%s" is corrupted.',fn);
0117       error(msg);
0118    end
0119 
0120    if strcmp(hdr.hist.magic, 'n+1')
0121       filetype = 2;
0122    elseif strcmp(hdr.hist.magic, 'ni1')
0123       filetype = 1;
0124    else
0125       filetype = 0;
0126    end
0127 
0128    return                    % load_nii_hdr_cbi
0129 
0130 function [ fid ] = fopen_ascii_encoding(file, mode, machine)
0131 
0132     if vb_matlab_version('>=', '7.2')
0133         fid = fopen(file, mode, machine, 'US-ASCII');
0134     else
0135         fid = fopen(file, mode, machine);
0136     end
0137 
0138 %---------------------------------------------------------------------
0139 function [ dsr ] = read_header(fid)
0140 
0141         %  Original header structures
0142     %  struct dsr
0143     %       {
0144     %       struct header_key hk;            /*   0 +  40       */
0145     %       struct image_dimension dime;     /*  40 + 108       */
0146     %       struct data_history hist;        /* 148 + 200       */
0147     %       };                               /* total= 348 bytes*/
0148 
0149     dsr.hk   = header_key(fid);
0150     dsr.dime = image_dimension(fid);
0151     dsr.hist = data_history(fid);
0152 
0153     %  For Analyze data format
0154     %
0155     if ~strcmp(dsr.hist.magic, 'n+1') & ~strcmp(dsr.hist.magic, 'ni1')
0156         dsr.hist.qform_code = 0;
0157         dsr.hist.sform_code = 0;
0158     end
0159 
0160     return                    % read_header
0161 
0162 
0163 %---------------------------------------------------------------------
0164 function [ hk ] = header_key(fid)
0165 
0166     fseek(fid,0,'bof');
0167     
0168     %  Original header structures
0169     %  struct header_key                     /* header key      */
0170     %       {                                /* off + size      */
0171     %       int sizeof_hdr                   /*  0 +  4         */
0172     %       char data_type[10];              /*  4 + 10         */
0173     %       char db_name[18];                /* 14 + 18         */
0174     %       int extents;                     /* 32 +  4         */
0175     %       short int session_error;         /* 36 +  2         */
0176     %       char regular;                    /* 38 +  1         */
0177     %       char dim_info;   % char hkey_un0;        /* 39 +  1 */
0178     %       };                               /* total=40 bytes  */
0179     %
0180     % int sizeof_header   Should be 348.
0181     % char regular        Must be 'r' to indicate that all images and
0182     %                     volumes are the same size.
0183     
0184     hk.sizeof_hdr    = fread(fid, 1,'int32')';    % should be 348!
0185     hk.data_type     = deblank(fread(fid,10,'*char')');
0186     hk.db_name       = deblank(fread(fid,18,'*char')');
0187     hk.extents       = fread(fid, 1,'int32')';
0188     hk.session_error = fread(fid, 1,'int16')';
0189     hk.regular       = fread(fid, 1,'*char')';
0190     hk.dim_info      = fread(fid, 1,'char')';
0191     
0192     return                    % header_key
0193 
0194 
0195 %---------------------------------------------------------------------
0196 function [ dime ] = image_dimension(fid)
0197 
0198     %  Original header structures
0199     %  struct image_dimension
0200     %       {                                /* off + size      */
0201     %       short int dim[8];                /* 0 + 16          */
0202         %       /*
0203         %           dim[0]      Number of dimensions in database; usually 4.
0204         %           dim[1]      Image X dimension;  number of *pixels* in an image row.
0205         %           dim[2]      Image Y dimension;  number of *pixel rows* in slice.
0206         %           dim[3]      Volume Z dimension; number of *slices* in a volume.
0207         %           dim[4]      Time points; number of volumes in database
0208         %       */
0209     %       float intent_p1;   % char vox_units[4];   /* 16 + 4       */
0210     %       float intent_p2;   % char cal_units[8];   /* 20 + 4       */
0211     %       float intent_p3;   % char cal_units[8];   /* 24 + 4       */
0212     %       short int intent_code;   % short int unused1;   /* 28 + 2 */
0213     %       short int datatype;              /* 30 + 2          */
0214     %       short int bitpix;                /* 32 + 2          */
0215     %       short int slice_start;   % short int dim_un0;   /* 34 + 2 */
0216     %       float pixdim[8];                 /* 36 + 32         */
0217     %    /*
0218     %        pixdim[] specifies the voxel dimensions:
0219     %        pixdim[1] - voxel width, mm
0220     %        pixdim[2] - voxel height, mm
0221     %        pixdim[3] - slice thickness, mm
0222     %        pixdim[4] - volume timing, in msec
0223     %                    ..etc
0224     %    */
0225     %       float vox_offset;                /* 68 + 4          */
0226     %       float scl_slope;   % float roi_scale;     /* 72 + 4 */
0227     %       float scl_inter;   % float funused1;      /* 76 + 4 */
0228     %       short slice_end;   % float funused2;      /* 80 + 2 */
0229     %       char slice_code;   % float funused2;      /* 82 + 1 */
0230     %       char xyzt_units;   % float funused2;      /* 83 + 1 */
0231     %       float cal_max;                   /* 84 + 4          */
0232     %       float cal_min;                   /* 88 + 4          */
0233     %       float slice_duration;   % int compressed; /* 92 + 4 */
0234     %       float toffset;   % int verified;          /* 96 + 4 */
0235     %       int glmax;                       /* 100 + 4         */
0236     %       int glmin;                       /* 104 + 4         */
0237     %       };                               /* total=108 bytes */
0238     
0239     dime.dim        = fread(fid,8,'int16')';
0240     dime.intent_p1  = fread(fid,1,'float32')';
0241     dime.intent_p2  = fread(fid,1,'float32')';
0242     dime.intent_p3  = fread(fid,1,'float32')';
0243     dime.intent_code = fread(fid,1,'int16')';
0244     dime.datatype   = fread(fid,1,'int16')';
0245     dime.bitpix     = fread(fid,1,'int16')';
0246     dime.slice_start = fread(fid,1,'int16')';
0247     dime.pixdim     = fread(fid,8,'float32')';
0248     dime.vox_offset = fread(fid,1,'float32')';
0249     dime.scl_slope  = fread(fid,1,'float32')';
0250     dime.scl_inter  = fread(fid,1,'float32')';
0251     dime.slice_end  = fread(fid,1,'int16')';
0252     dime.slice_code = fread(fid,1,'char')';
0253     dime.xyzt_units = fread(fid,1,'char')';
0254     dime.cal_max    = fread(fid,1,'float32')';
0255     dime.cal_min    = fread(fid,1,'float32')';
0256     dime.slice_duration = fread(fid,1,'float32')';
0257     dime.toffset    = fread(fid,1,'float32')';
0258     dime.glmax      = fread(fid,1,'int32')';
0259     dime.glmin      = fread(fid,1,'int32')';
0260         
0261     return                    % image_dimension
0262 
0263 
0264 %---------------------------------------------------------------------
0265 function [ hist ] = data_history(fid)
0266         
0267     %  Original header structures
0268     %  struct data_history
0269     %       {                                /* off + size      */
0270     %       char descrip[80];                /* 0 + 80          */
0271     %       char aux_file[24];               /* 80 + 24         */
0272     %       short int qform_code;            /* 104 + 2         */
0273     %       short int sform_code;            /* 106 + 2         */
0274     %       float quatern_b;                 /* 108 + 4         */
0275     %       float quatern_c;                 /* 112 + 4         */
0276     %       float quatern_d;                 /* 116 + 4         */
0277     %       float qoffset_x;                 /* 120 + 4         */
0278     %       float qoffset_y;                 /* 124 + 4         */
0279     %       float qoffset_z;                 /* 128 + 4         */
0280     %       float srow_x[4];                 /* 132 + 16        */
0281     %       float srow_y[4];                 /* 148 + 16        */
0282     %       float srow_z[4];                 /* 164 + 16        */
0283     %       char intent_name[16];            /* 180 + 16        */
0284     %       char magic[4];   % int smin;     /* 196 + 4         */
0285     %       };                               /* total=200 bytes */
0286     
0287     hist.descrip     = deblank(fread(fid,80,'*char')');
0288     hist.aux_file    = deblank(fread(fid,24,'*char')');
0289     hist.qform_code  = fread(fid,1,'int16')';
0290     hist.sform_code  = fread(fid,1,'int16')';
0291     hist.quatern_b   = fread(fid,1,'float32')';
0292     hist.quatern_c   = fread(fid,1,'float32')';
0293     hist.quatern_d   = fread(fid,1,'float32')';
0294     hist.qoffset_x   = fread(fid,1,'float32')';
0295     hist.qoffset_y   = fread(fid,1,'float32')';
0296     hist.qoffset_z   = fread(fid,1,'float32')';
0297     hist.srow_x      = fread(fid,4,'float32')';
0298     hist.srow_y      = fread(fid,4,'float32')';
0299     hist.srow_z      = fread(fid,4,'float32')';
0300     hist.intent_name = deblank(fread(fid,16,'*char')');
0301     hist.magic       = deblank(fread(fid,4,'*char')');
0302 
0303     fseek(fid,253,'bof');
0304     hist.originator  = fread(fid, 5,'int16')';
0305     
0306     return                    % data_history
0307

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005