0001 function avw_img_write(avw, fileprefix, IMGorient, machine, verbose)
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
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068 if ~exist('avw','var'),
0069 doc avw_img_write;
0070 error('...no input avw.');
0071 elseif isempty(avw),
0072 error('...empty input avw.');
0073 elseif ~isfield(avw,'img'),
0074 error('...empty input avw.img');
0075 end
0076
0077 if ~exist('fileprefix','var'),
0078 if isfield(avw,'fileprefix'),
0079 if ~isempty(avw.fileprefix),
0080 fileprefix = avw.fileprefix;
0081 else
0082 fileprefix = [];
0083 end
0084 else
0085 fileprefix = [];
0086 end
0087 end
0088 if isempty(fileprefix),
0089 [fileprefix, pathname, filterindex] = uiputfile('*.hdr','Specify an output Analyze .hdr file');
0090 if pathname, cd(pathname); end
0091 if ~fileprefix,
0092 doc avw_img_write;
0093 error('no output .hdr file specified');
0094 end
0095 end
0096
0097 if findstr('.hdr',fileprefix),
0098
0099 fileprefix = strrep(fileprefix,'.hdr','');
0100 end
0101 if findstr('.img',fileprefix),
0102
0103 fileprefix = strrep(fileprefix,'.img','');
0104 end
0105
0106 if ~exist('IMGorient','var'), IMGorient = ''; end
0107 if ~exist('machine','var'), machine = 'ieee-le'; end
0108 if ~exist('verbose','var'), verbose = 1; end
0109
0110 if isempty(IMGorient), IMGorient = ''; end
0111 if isempty(machine), machine = 'ieee-le'; end
0112 if isempty(verbose), verbose = 1; end
0113
0114
0115
0116
0117
0118 if verbose,
0119 version = '[$Revision: 1.1 $]';
0120 fprintf('\nAVW_IMG_WRITE [v%s]\n',version(12:16)); tic;
0121 end
0122
0123 fid = fopen(sprintf('%s.img',fileprefix),'w',machine);
0124 if fid < 0,
0125 msg = sprintf('Cannot open file %s.img\n',fileprefix);
0126 error(msg);
0127 else
0128 avw = write_image(fid,avw,fileprefix,IMGorient,machine,verbose);
0129 end
0130
0131 if verbose,
0132 t=toc; fprintf('...done (%5.2f sec).\n\n',t);
0133 end
0134
0135
0136
0137
0138 avw_hdr_write(avw,fileprefix,machine,verbose);
0139
0140 return
0141
0142
0143
0144
0145
0146
0147 function avw = write_image(fid,avw,fileprefix,IMGorient,machine,verbose)
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164 switch double(avw.hdr.dime.datatype),
0165 case 1,
0166 avw.hdr.dime.bitpix = int16( 1); precision = 'bit1';
0167 case 2,
0168 avw.hdr.dime.bitpix = int16( 8); precision = 'uchar';
0169 case 4,
0170 avw.hdr.dime.bitpix = int16(16); precision = 'int16';
0171 case 8,
0172 avw.hdr.dime.bitpix = int16(32); precision = 'int32';
0173 case 16,
0174 avw.hdr.dime.bitpix = int16(32); precision = 'single';
0175 case 32,
0176 error('...complex datatype not yet supported.\n');
0177 case 64,
0178 avw.hdr.dime.bitpix = int16(64); precision = 'double';
0179 case 128,
0180 error('...RGB datatype not yet supported.\n');
0181 otherwise
0182 warning('...unknown datatype, using type 16 (32 bit floats).\n');
0183 avw.hdr.dime.datatype = int16(16);
0184 avw.hdr.dime.bitpix = int16(32); precision = 'single';
0185 end
0186
0187
0188
0189 if verbose,
0190 fprintf('...writing %s precision Analyze image (%s).\n',precision,machine);
0191 end
0192
0193 fseek(fid,0,'bof');
0194
0195
0196 if isempty(avw.hdr.hist.orient),
0197 msg = [ '...avw.hdr.hist.orient ~= 0.\n',...
0198 ' This function assumes the input avw.img is\n',...
0199 ' in axial unflipped orientation in memory. This is\n',...
0200 ' created by the avw_img_read function, which converts\n',...
0201 ' any input file image to axial unflipped in memory.\n'];
0202 warning(msg)
0203 end
0204
0205 if isempty(IMGorient),
0206 if verbose,
0207 fprintf('...no IMGorient specified, using avw.hdr.hist.orient value.\n');
0208 end
0209 IMGorient = double(avw.hdr.hist.orient);
0210 end
0211
0212 if ~isfinite(IMGorient),
0213 if verbose,
0214 fprintf('...IMGorient is not finite!\n');
0215 end
0216 IMGorient = 99;
0217 end
0218
0219 switch IMGorient,
0220
0221 case 0,
0222
0223
0224
0225
0226
0227
0228 if verbose, fprintf('...writing axial unflipped\n'); end
0229
0230 avw.hdr.hist.orient = uint8(0);
0231
0232 SliceDim = double(avw.hdr.dime.dim(4));
0233 RowDim = double(avw.hdr.dime.dim(3));
0234 PixelDim = double(avw.hdr.dime.dim(2));
0235 SliceSz = double(avw.hdr.dime.pixdim(4));
0236 RowSz = double(avw.hdr.dime.pixdim(3));
0237 PixelSz = double(avw.hdr.dime.pixdim(2));
0238
0239 x = 1:PixelDim;
0240 for z = 1:SliceDim,
0241 for y = 1:RowDim,
0242 fwrite(fid,avw.img(x,y,z),precision);
0243 end
0244 end
0245
0246 case 1,
0247
0248
0249
0250
0251
0252
0253 if verbose, fprintf('...writing coronal unflipped\n'); end
0254
0255 avw.hdr.hist.orient = uint8(1);
0256
0257 SliceDim = double(avw.hdr.dime.dim(3));
0258 RowDim = double(avw.hdr.dime.dim(4));
0259 PixelDim = double(avw.hdr.dime.dim(2));
0260 SliceSz = double(avw.hdr.dime.pixdim(3));
0261 RowSz = double(avw.hdr.dime.pixdim(4));
0262 PixelSz = double(avw.hdr.dime.pixdim(2));
0263
0264 x = 1:PixelDim;
0265 for y = 1:SliceDim,
0266 for z = 1:RowDim,
0267 fwrite(fid,avw.img(x,y,z),precision);
0268 end
0269 end
0270
0271 case 2,
0272
0273
0274
0275
0276
0277
0278 if verbose, fprintf('...writing sagittal unflipped\n'); end
0279
0280 avw.hdr.hist.orient = uint8(2);
0281
0282 SliceDim = double(avw.hdr.dime.dim(2));
0283 RowDim = double(avw.hdr.dime.dim(4));
0284 PixelDim = double(avw.hdr.dime.dim(3));
0285 SliceSz = double(avw.hdr.dime.pixdim(2));
0286 RowSz = double(avw.hdr.dime.pixdim(4));
0287 PixelSz = double(avw.hdr.dime.pixdim(3));
0288
0289 y = 1:PixelDim;
0290 for x = 1:SliceDim,
0291 for z = 1:RowDim,
0292 fwrite(fid,avw.img(x,y,z),precision);
0293 end
0294 end
0295
0296 case 3,
0297
0298
0299
0300
0301
0302
0303 if verbose,
0304 fprintf('...writing axial flipped (+Y from Anterior to Posterior)\n');
0305 end
0306
0307 avw.hdr.hist.orient = uint8(3);
0308
0309 SliceDim = double(avw.hdr.dime.dim(4));
0310 RowDim = double(avw.hdr.dime.dim(3));
0311 PixelDim = double(avw.hdr.dime.dim(2));
0312 SliceSz = double(avw.hdr.dime.pixdim(4));
0313 RowSz = double(avw.hdr.dime.pixdim(3));
0314 PixelSz = double(avw.hdr.dime.pixdim(2));
0315
0316 x = 1:PixelDim;
0317 for z = 1:SliceDim,
0318 for y = RowDim:-1:1,
0319 fwrite(fid,avw.img(x,y,z),precision);
0320 end
0321 end
0322
0323 case 4,
0324
0325
0326
0327
0328
0329
0330 if verbose,
0331 fprintf('...writing coronal flipped (+Z from Superior to Inferior)\n');
0332 end
0333
0334 avw.hdr.hist.orient = uint8(4);
0335
0336 SliceDim = double(avw.hdr.dime.dim(3));
0337 RowDim = double(avw.hdr.dime.dim(4));
0338 PixelDim = double(avw.hdr.dime.dim(2));
0339 SliceSz = double(avw.hdr.dime.pixdim(3));
0340 RowSz = double(avw.hdr.dime.pixdim(4));
0341 PixelSz = double(avw.hdr.dime.pixdim(2));
0342
0343 x = 1:PixelDim;
0344 for y = 1:SliceDim,
0345 for z = RowDim:-1:1,
0346 fwrite(fid,avw.img(x,y,z),precision);
0347 end
0348 end
0349
0350 case 5,
0351
0352
0353
0354
0355
0356
0357 if verbose,
0358 fprintf('...writing sagittal flipped (+Z from Superior to Inferior)\n');
0359 end
0360
0361 avw.hdr.hist.orient = uint8(5);
0362
0363 SliceDim = double(avw.hdr.dime.dim(2));
0364 RowDim = double(avw.hdr.dime.dim(4));
0365 PixelDim = double(avw.hdr.dime.dim(3));
0366 SliceSz = double(avw.hdr.dime.pixdim(2));
0367 RowSz = double(avw.hdr.dime.pixdim(4));
0368 PixelSz = double(avw.hdr.dime.pixdim(3));
0369
0370 y = 1:PixelDim;
0371 for x = 1:SliceDim,
0372 for z = RowDim:-1:1,
0373 fwrite(fid,avw.img(x,y,z),precision);
0374 end
0375 end
0376
0377 otherwise,
0378
0379
0380
0381
0382
0383
0384 if verbose,
0385 fprintf('...unknown orientation specified, assuming default axial unflipped\n');
0386 end
0387
0388 avw.hdr.hist.orient = uint8(0);
0389
0390 SliceDim = double(avw.hdr.dime.dim(4));
0391 RowDim = double(avw.hdr.dime.dim(3));
0392 PixelDim = double(avw.hdr.dime.dim(2));
0393 SliceSz = double(avw.hdr.dime.pixdim(4));
0394 RowSz = double(avw.hdr.dime.pixdim(3));
0395 PixelSz = double(avw.hdr.dime.pixdim(2));
0396
0397 x = 1:PixelDim;
0398 for z = 1:SliceDim,
0399 for y = 1:RowDim,
0400 fwrite(fid,avw.img(x,y,z),precision);
0401 end
0402 end
0403
0404 end
0405
0406 fclose(fid);
0407
0408
0409 avw.hdr.dime.dim(2:4) = int16([PixelDim,RowDim,SliceDim]);
0410 avw.hdr.dime.pixdim(2:4) = single([PixelSz,RowSz,SliceSz]);
0411
0412 return