Home > vbmeg > external > NIFTI > save_nii_ana_cbi.m

save_nii_ana_cbi

PURPOSE ^

Save NIFTI dataset. Support both *.nii and *.hdr/*.img file extension.

SYNOPSIS ^

function save_nii_ana_cbi(nii, fileprefix, old_RGB)

DESCRIPTION ^

  Save NIFTI dataset. Support both *.nii and *.hdr/*.img file extension.
  If file extension is not provided, *.hdr/*.img will be used as default.
  For *.hdr/*.img file, Analize format is used

  Usage: save_nii_ana_cbi(nii, filename, [old_RGB])
  
  nii.hdr - struct with NIFTI header fields.
  nii.img - 3D (or 4D) matrix of NIFTI data.
  filename - NIFTI file name.

  old_RGB    - an optional boolean variable to handle special RGB data 
       sequence [R1 R2 ... G1 G2 ... B1 B2 ...] that is used only by 
       AnalyzeDirect (Analyze Software). Since both NIfTI and Analyze
       file format use RGB triple [R1 G1 B1 R2 G2 B2 ...] sequentially
       for each voxel, this variable is set to FALSE by default. If you
       would like the saved image only to be opened by AnalyzeDirect 
       Software, set old_RGB to TRUE (or 1).
  
  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)
  - "old_RGB" related codes in "save_nii.m" are added by Mike Harms (2006.06.28)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function save_nii_ana_cbi(nii, fileprefix, old_RGB)
0002 %  Save NIFTI dataset. Support both *.nii and *.hdr/*.img file extension.
0003 %  If file extension is not provided, *.hdr/*.img will be used as default.
0004 %  For *.hdr/*.img file, Analize format is used
0005 %
0006 %  Usage: save_nii_ana_cbi(nii, filename, [old_RGB])
0007 %
0008 %  nii.hdr - struct with NIFTI header fields.
0009 %  nii.img - 3D (or 4D) matrix of NIFTI data.
0010 %  filename - NIFTI file name.
0011 %
0012 %  old_RGB    - an optional boolean variable to handle special RGB data
0013 %       sequence [R1 R2 ... G1 G2 ... B1 B2 ...] that is used only by
0014 %       AnalyzeDirect (Analyze Software). Since both NIfTI and Analyze
0015 %       file format use RGB triple [R1 G1 B1 R2 G2 B2 ...] sequentially
0016 %       for each voxel, this variable is set to FALSE by default. If you
0017 %       would like the saved image only to be opened by AnalyzeDirect
0018 %       Software, set old_RGB to TRUE (or 1).
0019 %
0020 %  Part of this file is copied and modified under GNU license from
0021 %  MRI_TOOLBOX developed by CNSP in Flinders University, Australia
0022 %
0023 %  NIFTI data format can be found on: http://nifti.nimh.nih.gov
0024 %
0025 %  - Jimmy Shen (pls@rotman-baycrest.on.ca)
0026 %  - "old_RGB" related codes in "save_nii.m" are added by Mike Harms (2006.06.28)
0027 %
0028    
0029    if ~exist('nii','var') | isempty(nii) | ~isfield(nii,'hdr') | ...
0030     ~isfield(nii,'img') | ~exist('fileprefix','var') | isempty(fileprefix)
0031 
0032       error('Usage: save_nii_ana_cbi(nii, filename, [old_RGB])');
0033    end
0034 
0035    if ~exist('old_RGB','var'), old_RGB = 0; end
0036    
0037    filetype = 1;
0038 
0039    %  Note: fileprefix is actually the filename you want to save
0040    %
0041    if findstr('.nii',fileprefix)
0042       filetype = 2;
0043       fileprefix = strrep(fileprefix,'.nii','');
0044    end
0045    
0046    if findstr('.hdr',fileprefix)
0047       fileprefix = strrep(fileprefix,'.hdr','');
0048    end
0049    
0050    if findstr('.img',fileprefix)
0051       fileprefix = strrep(fileprefix,'.img','');
0052    end
0053    
0054    write_nii(nii, filetype, fileprefix, old_RGB);
0055 
0056 %    if filetype == 1
0057 %
0058 %       %  So earlier versions of SPM can also open it with correct originator
0059 %       %
0060 %       M=[[diag(nii.hdr.dime.pixdim(2:4)) -[nii.hdr.hist.originator(1:3).*nii.hdr.dime.pixdim(2:4)]'];[0 0 0 1]];
0061 %       save(fileprefix, 'M');
0062 %    end
0063    
0064    return                    % save_nii_ana_cbi
0065 
0066 
0067 %-----------------------------------------------------------------------------------
0068 function write_nii(nii, filetype, fileprefix, old_RGB)
0069 
0070    hdr = nii.hdr;
0071 
0072    switch double(hdr.dime.datatype),
0073    case   1,
0074       hdr.dime.bitpix = int16(1 ); precision = 'ubit1';
0075    case   2,
0076       hdr.dime.bitpix = int16(8 ); precision = 'uint8';
0077    case   4,
0078       hdr.dime.bitpix = int16(16); precision = 'int16';
0079    case   8,
0080       hdr.dime.bitpix = int16(32); precision = 'int32';
0081    case  16,
0082       hdr.dime.bitpix = int16(32); precision = 'float32';
0083    case  32,
0084       hdr.dime.bitpix = int16(64); precision = 'float32';
0085    case  64,
0086       hdr.dime.bitpix = int16(64); precision = 'float64';
0087    case 128,
0088       hdr.dime.bitpix = int16(24); precision = 'uint8';
0089    case 256 
0090       hdr.dime.bitpix = int16(8 ); precision = 'int8';
0091    case 512 
0092       hdr.dime.bitpix = int16(16); precision = 'uint16';
0093    case 768 
0094       hdr.dime.bitpix = int16(32); precision = 'uint32';
0095    case 1024
0096       hdr.dime.bitpix = int16(64); precision = 'int64';
0097    case 1280
0098       hdr.dime.bitpix = int16(64); precision = 'uint64';
0099    case 1792,
0100       hdr.dime.bitpix = int16(128); precision = 'float64';
0101    otherwise
0102       error('This datatype is not supported');
0103    end
0104    
0105    hdr.dime.glmax = round(double(max(nii.img(:))));
0106    hdr.dime.glmin = round(double(min(nii.img(:))));
0107    
0108    if filetype == 2
0109       fid = fopen(sprintf('%s.nii',fileprefix),'w');
0110       
0111       if fid < 0,
0112          msg = sprintf('Cannot open file %s.nii.',fileprefix);
0113          error(msg);
0114       end
0115       
0116       hdr.dime.vox_offset = 352;
0117       hdr.hist.magic = 'n+1';
0118       save_nii_hdr_cbi(hdr, fid);
0119    else
0120       fid = fopen(sprintf('%s.hdr',fileprefix),'w');
0121       
0122       if fid < 0,
0123          msg = sprintf('Cannot open file %s.hdr.',fileprefix);
0124          error(msg);
0125       end
0126       
0127       hdr.dime.vox_offset = 0;
0128       hdr.hist.magic = 'aaa';
0129       save_nii_ana_hdr_cbi(hdr, fid);
0130       
0131       fclose(fid);
0132       fid = fopen(sprintf('%s.img',fileprefix),'w');
0133    end
0134 
0135    ScanDim = double(hdr.dime.dim(5));        % t
0136    SliceDim = double(hdr.dime.dim(4));        % z
0137    RowDim   = double(hdr.dime.dim(3));        % y
0138    PixelDim = double(hdr.dime.dim(2));        % x
0139    SliceSz  = double(hdr.dime.pixdim(4));
0140    RowSz    = double(hdr.dime.pixdim(3));
0141    PixelSz  = double(hdr.dime.pixdim(2));
0142    
0143    x = 1:PixelDim;
0144    
0145    if filetype == 2
0146       skip_bytes = double(hdr.dime.vox_offset) - 348;
0147    else
0148       skip_bytes = 0;
0149    end
0150 
0151    if double(hdr.dime.datatype) == 128
0152 
0153       %  RGB planes are expected to be in the 4th dimension of nii.img
0154       %
0155       if(size(nii.img,4)~=3)
0156          error(['The NII structure does not appear to have 3 RGB color planes in the 4th dimension']);
0157       end
0158 
0159       if old_RGB
0160          nii.img = permute(nii.img, [1 2 4 3 5]);
0161       else
0162          nii.img = permute(nii.img, [4 1 2 3 5]);
0163       end
0164    end
0165 
0166    %  For complex float32 or complex float64, voxel values
0167    %  include [real, imag]
0168    %
0169    if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792
0170       real_img = real(nii.img(:))';
0171       nii.img = imag(nii.img(:))';
0172       nii.img = [real_img; nii.img];
0173    end
0174 
0175    if skip_bytes
0176       fwrite(fid, ones(1,skip_bytes), 'uint8');
0177    end
0178 
0179    fwrite(fid, nii.img, precision);
0180 %   fwrite(fid, nii.img, precision, skip_bytes);        % error using skip
0181    fclose(fid);
0182 
0183    return;                    % write_nii
0184

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