0001 function GE_convert(inDir,outStem)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 if (nargin < 2)
0020 error('Not enough input arguments.')
0021 return
0022 end
0023
0024
0025 firstfile = fullfile(inDir,'I.001');
0026
0027
0028 [header, orient, im_offset, adwcount] = GE_createSPMHeader(firstfile);
0029 volSize = [header.dim(2) header.dim(3) header.dim(4)];
0030
0031 header = GE_reorientHeader(header, orient);
0032
0033
0034
0035
0036
0037
0038 done = 0;
0039 runnum = 1;
0040 passnum = 0;
0041
0042 while ~done
0043
0044 for i = 1:adwcount
0045 passnum = passnum + 1;
0046
0047
0048 series_str = sprintf('0%d',runnum);
0049 series_str = series_str(length(series_str)-1:length(series_str));
0050 vol_str = sprintf('000%d',i);
0051 vol_str = vol_str(length(vol_str)-3:length(vol_str));
0052 outName = [outStem sprintf('_s%s_i%s',series_str,vol_str)];
0053
0054
0055 [imageVol, lastfile] = GE_readVolume(inDir, passnum, volSize, header.bitpix, im_offset);
0056
0057 if isempty(imageVol)
0058 error('Failed to read in volume ...')
0059 end
0060
0061
0062 imageVol = GE_reorientImage(imageVol, orient);
0063
0064
0065 outFile = strcat(outName,'.hdr');
0066 status = GE_writeSPMHeader(outFile,header);
0067
0068
0069 outFile = [outName sprintf('.img')];
0070 [fid,message] = fopen(outFile,'w','l');
0071 if (fid == -1)
0072 fprintf('Cannot Open %s for writing.\n',outFile);
0073 error(message);
0074 end
0075
0076 fwrite(fid,reshape(imageVol,1,prod(size(imageVol))),'int16');
0077 status = fclose(fid);
0078
0079
0080 fprintf('Wrote %s\n',outName);
0081
0082 if runnum == 1 & adwcount > 1 & passnum == 1
0083 break
0084 end
0085
0086 end
0087
0088
0089 [path_stem, path_start] = fileparts(inDir);
0090
0091 filep = ceil((passnum+1)*volSize(3)/999) - 1;
0092 filen = (passnum+1)*volSize(3) - 999*filep;
0093 path_num = str2num(path_start) + 20*filep;
0094 path_now = sprintf('00%d',path_num);
0095 path_now = path_now(length(path_now)-2:length(path_now));
0096 path = fullfile(path_stem, path_now);
0097 stub = sprintf('00%d',filen);
0098 stub = stub(length(stub)-2:length(stub));
0099 nextFile = fullfile(path,['I.' stub]);
0100 [fid,message] = fopen(nextFile,'r');
0101
0102 if (fid == -1)
0103 done = 1;
0104 else
0105 fclose(fid);
0106 runnum = runnum + 1;
0107 end
0108
0109 end
0110
0111
0112 fprintf('\nConversion Finished. \n');
0113
0114 return