Attachment 'save_mgh.m'

Download

   1 function r = save_mgh(vol, fname, M, mr_parms);
   2 %
   3 % save_mgh(vol,fname, M, <mr_parms>);
   4 %
   5 % M is the 4x4 vox2ras transform such that
   6 % y(i1,i2,i3), xyz = M*[i1 i2 i3 1] where the
   7 % indicies are 0-based
   8 %
   9 % mr_parms = [tr flipangle te ti]
  10 %
  11 % See also: load_mgh, vox2ras_0to1
  12 %
  13 % $Id: save_mgh.m,v 1.6 2004/07/26 14:08:50 fischl Exp $
  14 %
  15 
  16 r = 1;
  17 
  18 if(nargin < 2 | nargin > 4)
  19   msg = 'USAGE: save_mgh2(vol,fname,M)';
  20   return;
  21 end
  22 
  23 if(exist('mr_parms')~=1) mr_parms = []; end
  24 if(isempty(mr_parms))   mr_parms = [0 0 0 0]; end
  25 if(length(mr_parms) < 4)
  26   fprintf('ERROR: mr_parms length = %d, must be 4 or 5\n', ...
  27 	  length(mr_parms));
  28   return;
  29 end
  30 
  31 % These dont appear to be used %
  32 MRI_UCHAR =  0 ;
  33 MRI_INT =    1 ;
  34 MRI_LONG =   2 ;
  35 MRI_FLOAT =  3 ;
  36 MRI_SHORT =  4 ;
  37 MRI_BITMAP = 5 ;
  38 MRI_TENSOR = 6 ;
  39 
  40 fid = fopen(fname, 'wb', 'b') ;
  41 if(fid == -1)
  42   fprintf('ERROR: could not open %s for writing\n',fname);
  43   return;
  44 end
  45 
  46 
  47 [ndim1,ndim2,ndim3,frames] = size(vol) ;
  48 fwrite(fid, 1, 'int') ;		% magic #
  49 fwrite(fid, ndim1, 'int') ; 
  50 fwrite(fid, ndim2, 'int') ; 
  51 fwrite(fid, ndim3, 'int') ; 
  52 fwrite(fid, frames, 'int') ;	% # of frames
  53 if(ndims(vol) == 5)
  54   is_tensor = 1 ;
  55   fwrite(fid, MRI_TENSOR, 'int') ; % type = MRI_TENSOR
  56 else
  57   is_tensor = 0 ;
  58   fwrite(fid, MRI_FLOAT, 'int') ;  % type = MRI_FLOAT
  59 end
  60 
  61 %%?????????????%%%
  62 fwrite(fid, 1, 'int') ;          % dof (not used)
  63 dof = fread(fid, 1, 'int') ; 
  64 
  65 UNUSED_SPACE_SIZE= 256;
  66 USED_SPACE_SIZE = (3*4+4*3*4);  % space for ras transform
  67 
  68 MdcD = M(1:3,1:3);
  69 delta = sqrt(sum(MdcD.^2));
  70 
  71 Mdc = MdcD./repmat(delta,[3 1]);
  72 Pcrs_c = [ndim1/2 ndim2/2 ndim3/2 1]'; %'
  73 Pxyz_c = M*Pcrs_c;
  74 Pxyz_c = Pxyz_c(1:3);
  75 
  76 fwrite(fid, 1,      'short') ;       % ras_good_flag = 1
  77 fwrite(fid, delta,  'float32') ; 
  78 fwrite(fid, Mdc,    'float32') ; 
  79 fwrite(fid, Pxyz_c, 'float32') ; 
  80 
  81 unused_space_size = UNUSED_SPACE_SIZE-2 ;
  82 unused_space_size = unused_space_size - USED_SPACE_SIZE ;
  83 fwrite(fid, zeros(unused_space_size,1), 'char') ;
  84 
  85 fwrite(fid,vol,'float32');
  86 
  87 fwrite(fid, mr_parms, 'float32') ; 
  88 fclose(fid) ;
  89 
  90 r = 0;
  91 
  92 if (strcmpi(fname((length(fname)-3):length(fname)), '.MGZ') | ...
  93 		strcmpi(fname((length(fname)-3):length(fname)), '.GZ'))
  94 
  95 	gzipped =  round(rand(1)*10000000);
  96 	ind = findstr(fname, '.');
  97 	new_fname = sprintf('/tmp/tmp%d.mgh', gzipped);
  98 	unix(sprintf('mv %s %s ; gzip %s ; mv %s.gz %s', fname, new_fname, new_fname, new_fname, fname)) ;
  99 	fname = new_fname ;
 100 end	
 101 return;

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-01-26 22:35:32, 12.9 KB) [[attachment:fio.c]]
  • [get | view] (2009-01-26 22:35:32, 1.3 KB) [[attachment:fio.h]]
  • [get | view] (2009-01-26 22:35:32, 5.9 KB) [[attachment:load_mgh.m]]
  • [get | view] (2009-01-26 22:35:32, 9.3 KB) [[attachment:matrix.h]]
  • [get | view] (2009-01-26 22:35:32, 390.2 KB) [[attachment:mri.c]]
  • [get | view] (2009-01-26 22:35:32, 48.5 KB) [[attachment:mri.h]]
  • [get | view] (2009-01-26 22:35:32, 94.4 KB) [[attachment:mri_convert.c]]
  • [get | view] (2009-01-26 22:35:32, 14.1 KB) [[attachment:mri_info.c]]
  • [get | view] (2009-01-26 22:35:32, 395.4 KB) [[attachment:mriio.c]]
  • [get | view] (2009-01-26 22:35:32, 2.4 KB) [[attachment:save_mgh.m]]
  • [get | view] (2009-01-26 22:35:32, 2.2 KB) [[attachment:tags.c]]
  • [get | view] (2009-01-26 22:35:32, 0.9 KB) [[attachment:tags.h]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.