Home > bioelectromagnetism > LoadAVW.m

LoadAVW

PURPOSE ^

SYNOPSIS ^

function avw = LoadAVW ( Filename )

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function avw = LoadAVW ( Filename )
0002   fid = fopen ( Filename, 'r' );
0003   
0004   line = fgetl ( fid );
0005   if ~strmatch ( line, 'AVW_ImageFile' )
0006     error ( sprintf ( '%s is not in AVW format', Filename ) );
0007   end
0008   
0009   Info = sscanf ( line, 'AVW_ImageFile %f %d' );
0010   Offset = Info(2);
0011   
0012   avw = struct ( 'Image', 0 );
0013   
0014   % Read until 'EndInformation'
0015   while isempty ( strmatch ( line, 'EndInformation' ) )
0016     line = trim ( fgetl ( fid ) );
0017     if ~isempty ( strfind ( line, '=' ) )
0018       idx = strfind ( line, '=' );
0019       name = trim ( line(1:idx-1) );
0020       value = line(idx+1:end);
0021       
0022       if strfind ( name, '|' )
0023     name = ['DICOMTag_' strrep(name,'|','_')];
0024       end
0025       
0026       if ~isempty ( str2num ( value ) )
0027         value = str2num ( value );
0028       else
0029         % Strip out '"'
0030         value = strrep ( value, '"', '' );
0031       end
0032       
0033       name = trim ( validname ( name ) );
0034       try
0035         avw = setfield ( avw, name, value );
0036       catch
0037         % disp ( lasterr )
0038         warning ( sprintf ( 'Failed to set %s to %s', name, num2str ( value ) ) );
0039       end
0040     end
0041   end
0042   
0043   % How much do we read
0044   DataSize = avw.Width * avw.Height * avw.Depth * avw.NumVols;
0045   fclose ( fid );
0046   if strcmp ( avw.Endian, 'Little' )
0047     % Not exactly sure what's up with this being switched...
0048     fid = fopen ( Filename, 'r', 'l' );
0049   else
0050     fid = fopen ( Filename, 'r', 'b' );
0051   end
0052   fseek ( fid, Offset, 'bof' );
0053   % Figure out the datatype
0054   switch upper(avw.DataType)
0055     case 'AVW_UNSIGNED_SHORT'
0056      DataType = 'uint16';
0057     case 'AVW_SIGNED_SHORT'
0058      DataType = 'int16';
0059     case 'AVW_UNSIGNED_CHAR'
0060      DataType = 'uchar';
0061     case 'AVW_SIGNED_CHAR'
0062      DataType = 'schar';
0063    otherwise
0064      error ( sprintf ( 'Unknown datatype: %s', avw.DataType ) );
0065   end
0066 
0067 
0068   Data = reshape ( fread ( fid, DataSize, DataType ), avw.Width, avw.Height, avw.Depth, avw.NumVols );
0069   % The image needs to be rotated
0070   avw.Image = zeros ( avw.Height, avw.Width, avw.Depth, avw.NumVols );
0071   yy = avw.Height:-1:1;
0072   for vol = 1:avw.NumVols
0073     for dd = 1:avw.Depth
0074       t = Data(:,:,dd,vol)';
0075       avw.Image(:,:,dd,vol) = t(yy,:);
0076     end
0077   end
0078   clear Data;
0079   fclose ( fid );
0080   avw.Image = squeeze ( avw.Image );
0081   
0082   
0083 function sout = trim ( s )
0084   [r,c] = find ( ~isspace ( s ) );
0085   if isempty ( c )
0086     sout = s([]);
0087   else
0088     sout = s(:,min(c):end);
0089   end
0090   sout = deblank ( sout );
0091   
0092   
0093 function s = validname ( s )
0094   ns = '';
0095   for ii = 1:length ( s )
0096     if ~isnumeric ( s(ii) )
0097       ns = [ns s(ii)];
0098     end
0099   end
0100   s = deblank ( ns );
0101

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003