0001 function eidors_saveimg( img, fname, format )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 switch nargin
0024 case 2
0025 fmt = detect_format(fname);
0026 if isempty( fmt )
0027 error('file format unspecified, can`t autodetect');
0028 end
0029 case 3
0030 fmt1 = detect_format(fname);
0031 fmt = lower(format);
0032 if isempty(fmt1);
0033 fname = [fname '.' fmt];
0034 else
0035 if ~strcmp(fmt1, fmt)
0036 error('The extension specified in file name doesn''t match the file format');
0037 end
0038 end
0039 otherwise
0040 error('Usage: eidors_saveimg( img , fname, format )');
0041 end
0042
0043
0044 switch fmt
0045 case 'igt'
0046 mceit_saveimg( img, fname );
0047 case 'e3d'
0048 native_saveimg( img, fname);
0049 otherwise
0050 error('eidors_readdata: file "%s" format unknown', fmt);
0051 end
0052
0053
0054
0055
0056
0057 function fmt = detect_format( fname )
0058
0059 dotpos = find(fname == '.');
0060 if isempty( dotpos )
0061 fmt = [];
0062 else
0063 dotpos= dotpos(end);
0064 format= fname( dotpos+1:end );
0065 fmt= lower(format);
0066 end
0067
0068
0069
0070
0071 function fid = open_file( fname );
0072
0073 if exist(fname,'file')
0074 disp('File already exists.');
0075 reply = input('Overwrite? Y/N [Y]: ', 's');
0076 if isempty(reply), reply = 'Y'; end
0077 reply = lower(reply);
0078
0079 if ~strcmp(reply,'y');
0080 fid = -1;
0081 return;
0082 end
0083 end
0084 fid = fopen( fname ,'w');
0085
0086
0087
0088
0089
0090
0091
0092 function mceit_saveimg( img, fname );
0093
0094
0095 fid = open_file( fname );
0096 if fid < 0
0097 error('Cannot open file.');
0098 end
0099
0100 n = size(img.elem_data,1);
0101 if n == 912
0102
0103 fwrite(fid,img.elem_data','4*float');
0104 else
0105 data = img2igt(img);
0106 fwrite(fid, data , '4*float');
0107 end
0108
0109 fclose(fid);
0110
0111
0112 function native_saveimg( img, fname )
0113
0114
0115
0116
0117
0118 if ~exist('OCTAVE_VERSION') && str2double(version('-release')) < 14
0119 save('e3d.temp', 'img');
0120 else
0121 save('e3d.temp', 'img', '-v6');
0122 end
0123 zip('temp.zip','e3d.temp');
0124 movefile('temp.zip',fname);
0125 delete e3d.temp