0001 function SaveAVW ( Filename, avw )
0002
0003
0004
0005
0006 HeaderNames = { 'DataType', 'Width', 'Height', 'Depth', 'NumVols', 'Endian', 'ColormapSize' };
0007 avw.MinimumDataValue = floor ( min ( avw.Image(:)) );
0008 avw.MaximumDataValue = floor ( max ( avw.Image(:)) );
0009
0010 NonHeaderNames = GetNonHeaderNames ( avw, HeaderNames );
0011
0012 HeaderInfo = FormatValues ( avw, HeaderNames, 0 );
0013 Info = FormatValues ( avw, NonHeaderNames, 1 );
0014
0015 Header = HeaderInfo;
0016 Header = [Header sprintf('BeginInformation\n') Info sprintf('EndInformation\n') sprintf('MoreInformation=-1\n')];
0017 Header = [Header sprintf('Vol Slc Offset Length Cmp Format\n')];
0018 Header = [Header sprintf('.CONTIG\n')];
0019 Header = [Header sprintf('EndSliceTable\n')];
0020
0021 HeaderSize = 4096 * ceil ( (100+length(Header)) / 4096 );
0022
0023 Header = [sprintf('AVW_ImageFile 1.00 %d\n',HeaderSize) Header];
0024 l = length ( Header );
0025 b = strrep ( blanks ( HeaderSize - l ), ' ', '#' );
0026 Header = [Header b];
0027
0028 Endian = 'b';
0029 if strcmp ( avw.Endian, 'Little' )
0030 Endian = 'l';
0031 end
0032 fid = fopen ( Filename, 'w', Endian );
0033
0034 fprintf ( fid, '%s', Header );
0035 switch upper(avw.DataType)
0036 case 'AVW_UNSIGNED_SHORT'
0037 DataType = 'uint16';
0038 case 'AVW_SIGNED_SHORT'
0039 DataType = 'int16';
0040 case 'AVW_UNSIGNED_CHAR'
0041 DataType = 'uchar';
0042 case 'AVW_SIGNED_CHAR'
0043 DataType = 'schar';
0044 case 'AVW_FLOAT'
0045 DataType = 'float';
0046 otherwise
0047 error ( sprintf ( 'Unknown datatype: %s', avw.DataType ) );
0048 end
0049
0050
0051 yy = size(avw.Image,1):-1:1;
0052 for vol = 1:avw.NumVols
0053 for dd = 1:size(avw.Image,3)
0054 t = squeeze(avw.Image(:,:,dd,vol))';
0055 t = t(:,yy);
0056 fwrite ( fid, t(:), DataType );
0057 end
0058 end
0059 fclose ( fid );
0060
0061
0062 function Header = FormatValues ( avw, Names, Quote )
0063 Header = '';
0064 for hidx = 1:length(Names)
0065 Name = Names{hidx};
0066 Value = getfield ( avw, Name );
0067
0068 if ~isempty ( findstr ( Name, 'DICOMTag_' ) )
0069 idx = strfind ( Name, '_' );
0070 idx = idx + 1;
0071 Name = [Name(idx(1):idx(1)+3) '|' Name(idx(2):idx(2)+3)];
0072 end
0073
0074 if isstr ( Value ) & Quote
0075 Value = sprintf ( '"%s"', Value );
0076 end
0077 if isnumeric ( Value )
0078 Value = num2str ( Value );
0079 end
0080 if Quote
0081 t = sprintf ( ' %s=%s\n', Name, Value );
0082 else
0083 t = sprintf ( ' %s=%s\n', Name, Value );
0084 end
0085 Header = [Header t];
0086 end
0087
0088
0089 function n = GetNonHeaderNames ( avw, HeaderNames )
0090 names = fieldnames ( avw );
0091 n = {};
0092 for nn = 1:length(names)
0093 if strcmp ( names{nn}, 'Image' )
0094 continue
0095 end
0096
0097 IsHeader = 0;
0098 for hidx = 1:length(HeaderNames)
0099 if strcmp ( names{nn}, HeaderNames{hidx} )
0100 IsHeader = 1;
0101 end
0102 end
0103
0104 if ~IsHeader
0105 n{length(n)+1} = names{nn};
0106 end
0107 end
0108
0109
0110