Home > rest_20090422 > rest_writefile.m

rest_writefile

PURPOSE ^

Write ANALYZE 7.5 or NIFTI file by YAN Chao-Gan

SYNOPSIS ^

function [] = rest_writefile(data,filename,isize,vsize,Header,dtype)

DESCRIPTION ^

Write ANALYZE 7.5 or NIFTI file by YAN Chao-Gan
 FORMAT function [] = rest_writefile(data,filename,isize,vsize,dtype)
              data  - data file.   
           filename - Analyze or NIFTI file (*.{hdr, img, nii})
              isize - the size of the data
              vsize - the size of the voxel
              dtype - the data type.
             Header - It will decide to write ANALYZE7.5 or NIFTI file by
                      whether Header has the field 'mat'.
                      Analyze format - Header just has one subfield: Header.Origin - the origin of the images.
                      NIFTI format   -   Head.fname - the filename of the image.
                                         Head.dim   - the x, y and z dimensions of the volume
                                         Head.dt    - A 1x2 array.  First element is datatype (see spm_type).
                                                      The second is 1 or 0 depending on the endian-ness. (must)
                                         Head.mat   - a 4x4 affine transformation matrix mapping from
                                                      voxel coordinates to real world coordinates. (must)
                                         Head.pinfo - plane info for each plane of the volume.
                                         Head.pinfo(1,:) - scale for each plane
                                         Head.pinfo(2,:) - offset for each plane
                                                      The true voxel intensities of the jth image are given
                                                      by: val*Head.pinfo(1,j) + Head.pinfo(2,j)
                                         Head.pinfo(3,:) - offset into image (in bytes).
                                                     If the size of pinfo is 3x1, then the volume is assumed
                                                     to be contiguous and each plane has the same scalefactor
                                                     and offset.
                                         Head.private - a structure containing complete information in the 
                                                     header
                                         Header.Origin - the origin of the image;
-----------------------------------------------------------
    Copyright(c) 2008~2010
    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
    Written by YAN Chao-Gan
    http://resting-fmri.sourceforge.net
     Mail to Authors:  <a href="Dawnwei.Song@gmail.com">Xiaowei Song</a>; <a href="ycg.yan@gmail.com">Chaogan Yan</a> 
    Version=1.3;
    Release=20080420;
   Revised by YAN Chao-Gan 080807: The NIFTI images would be saved in RPI coordination as the same as SPM5 usually did.
   Last Revised by YAN Chao-Gan, 090420. rest_WriteAnalyzeImage.m only recognize 'float', thus change the parameter 'single' to 'float'.
-----------------------------------------------------------

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

rest_writefile.m

SOURCE CODE ^

0001 function [] = rest_writefile(data,filename,isize,vsize,Header,dtype)
0002 %Write ANALYZE 7.5 or NIFTI file by YAN Chao-Gan
0003 % FORMAT function [] = rest_writefile(data,filename,isize,vsize,dtype)
0004 %              data  - data file.
0005 %           filename - Analyze or NIFTI file (*.{hdr, img, nii})
0006 %              isize - the size of the data
0007 %              vsize - the size of the voxel
0008 %              dtype - the data type.
0009 %             Header - It will decide to write ANALYZE7.5 or NIFTI file by
0010 %                      whether Header has the field 'mat'.
0011 %                      Analyze format - Header just has one subfield: Header.Origin - the origin of the images.
0012 %                      NIFTI format   -   Head.fname - the filename of the image.
0013 %                                         Head.dim   - the x, y and z dimensions of the volume
0014 %                                         Head.dt    - A 1x2 array.  First element is datatype (see spm_type).
0015 %                                                      The second is 1 or 0 depending on the endian-ness. (must)
0016 %                                         Head.mat   - a 4x4 affine transformation matrix mapping from
0017 %                                                      voxel coordinates to real world coordinates. (must)
0018 %                                         Head.pinfo - plane info for each plane of the volume.
0019 %                                         Head.pinfo(1,:) - scale for each plane
0020 %                                         Head.pinfo(2,:) - offset for each plane
0021 %                                                      The true voxel intensities of the jth image are given
0022 %                                                      by: val*Head.pinfo(1,j) + Head.pinfo(2,j)
0023 %                                         Head.pinfo(3,:) - offset into image (in bytes).
0024 %                                                     If the size of pinfo is 3x1, then the volume is assumed
0025 %                                                     to be contiguous and each plane has the same scalefactor
0026 %                                                     and offset.
0027 %                                         Head.private - a structure containing complete information in the
0028 %                                                     header
0029 %                                         Header.Origin - the origin of the image;
0030 %-----------------------------------------------------------
0031 %    Copyright(c) 2008~2010
0032 %    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
0033 %    Written by YAN Chao-Gan
0034 %    http://resting-fmri.sourceforge.net
0035 %     Mail to Authors:  <a href="Dawnwei.Song@gmail.com">Xiaowei Song</a>; <a href="ycg.yan@gmail.com">Chaogan Yan</a>
0036 %    Version=1.3;
0037 %    Release=20080420;
0038 %   Revised by YAN Chao-Gan 080807: The NIFTI images would be saved in RPI coordination as the same as SPM5 usually did.
0039 %   Last Revised by YAN Chao-Gan, 090420. rest_WriteAnalyzeImage.m only recognize 'float', thus change the parameter 'single' to 'float'.
0040 %-----------------------------------------------------------
0041 
0042 if isfield(Header,'mat')
0043     switch lower(dtype)
0044         case 'uint32'
0045             datatype = 768;
0046         case 'uint16'
0047             datatype = 512;
0048         case 'int8'
0049             datatype = 256;
0050         case {'float64','double'}
0051             datatype = 64;
0052         case {'float32','single'}
0053             datatype = 16;
0054         case 'int32'
0055             datatype = 8;
0056         case 'int16'
0057             datatype = 4;
0058         case 'uint8'
0059             datatype = 2;
0060         otherwise % need to add other decription
0061             error('unsupported data format now.');
0062     end
0063     if Header.mat(1,1)>0 %The NIFTI images would be saved in RPI coordination as the same as SPM5 usually did. YAN Chao-Gan 080807 % <0 % I'd like to save the file in LPI coordination. Chaogan Yan 080610
0064         data = flipdim(data,1);
0065         Header.mat(1,:) = -1*Header.mat(1,:);
0066     end
0067     if Header.mat(2,2)<0
0068         data = flipdim(data,2);
0069         Header.mat(2,:) = -1*Header.mat(2,:);
0070     end
0071     if Header.mat(3,3)<0
0072         data = flipdim(data,3);
0073         Header.mat(3,:) = -1*Header.mat(3,:);
0074     end
0075     Header.dt    =[datatype,0];
0076     Header.vox   =vsize;
0077     Header.dim   =isize;
0078     Header.pinfo = [1;0;0];
0079     rest_WriteNiftiImage(data,Header,filename);
0080 else
0081     if (dtype=='single')
0082         dtype='float';  %Revised by YAN Chao-Gan, 090420. rest_WriteAnalyzeImage.m only recognize 'float', thus change the parameter 'single' to 'float'.
0083     end
0084     rest_WriteAnalyzeImage(data,filename,isize,vsize,Header.Origin,dtype);
0085 end
0086     
0087

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