0001 function save_nii_ana_cbi(nii, fileprefix, old_RGB)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
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
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
0057
0058
0059
0060
0061
0062
0063
0064 return
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));
0136 SliceDim = double(hdr.dime.dim(4));
0137 RowDim = double(hdr.dime.dim(3));
0138 PixelDim = double(hdr.dime.dim(2));
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
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
0167
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
0181 fclose(fid);
0182
0183 return;
0184