Home > rest_20090422 > rest_spm5_files > @read_nifti > create.m

create

PURPOSE ^

Create a NIFTI-1 file

SYNOPSIS ^

function create(obj,wrt)

DESCRIPTION ^

 Create a NIFTI-1 file
 FORMAT create(obj)
 This writes out the header information for the nifti object

 create(obj,wrt)
 This also writes out an empty image volume if wrt==1
 _______________________________________________________________________
 Copyright (C) 2005 Wellcome Department of Imaging Neuroscience

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

create.m

SOURCE CODE ^

0001 function create(obj,wrt)
0002 % Create a NIFTI-1 file
0003 % FORMAT create(obj)
0004 % This writes out the header information for the nifti object
0005 %
0006 % create(obj,wrt)
0007 % This also writes out an empty image volume if wrt==1
0008 % _______________________________________________________________________
0009 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
0010 
0011 %
0012 % $Id: create.m 433 2006-02-09 11:45:02Z john $
0013 
0014 for i=1:numel(obj)
0015     create_each(obj(i));
0016 end;
0017 
0018 function create_each(obj)
0019 if ~isa(obj.dat,'create_file_array'),
0020     error('Data must be a create_file_array');
0021 end;
0022 fname = obj.dat.fname;
0023 if isempty(fname),
0024     error('No filename to write to.');
0025 end;
0026 dt = obj.dat.dtype;
0027 ok = write_hdr_raw(fname,obj.hdr,dt(end-1)=='B');
0028 if ~ok,
0029     error(['Unable to write header for "' fname '".']);
0030 end;
0031 
0032 write_extras(fname,obj.extras);
0033 
0034 if nargin>2 && any(wrt==1),
0035     % Create an empty image file if necessary
0036     d   = findindict(obj.hdr.datatype, 'dtype');
0037     dim = double(obj.hdr.dim(2:end));
0038     dim((double(obj.hdr.dim(1))+1):end) = 1;
0039     nbytes = ceil(d.size*d.nelem*prod(dim(1:2)))*prod(dim(3:end))+double(obj.hdr.vox_offset);
0040     [pth,nam,ext] = fileparts(obj.dat.fname);
0041 
0042     if any(strcmp(deblank(obj.hdr.magic),{'n+1','nx1'})),
0043         ext = '.nii';
0044     else
0045         ext = '.img';
0046     end;
0047     iname = fullfile(pth,[nam ext]);
0048     fp    = fopen(iname,'a+');
0049     if fp==-1,
0050         error(['Unable to create image for "' fname '".']);
0051     end;
0052 
0053     fseek(fp,0,'eof');
0054     pos = ftell(fp);
0055     if pos<nbytes,
0056         bs      = 2048; % Buffer-size
0057         nbytes  = nbytes - pos;
0058         buf     = uint8(0);
0059         buf(bs) = 0;
0060         while(nbytes>0)
0061             if nbytes<bs, buf = buf(1:nbytes); end;
0062             nw = fwrite(fp,buf,'uint8');
0063             if nw<min(bs,nbytes),
0064                 fclose(fp);
0065                 error(['Problem while creating image for "' fname '".']);
0066             end;
0067             nbytes = nbytes - nw;
0068         end;
0069     end;
0070     fclose(fp);
0071 end;
0072 
0073 return;
0074

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005