Home > vbmeg > external > iso2mesh > readinr.m

readinr

PURPOSE ^

SYNOPSIS ^

function dat=readinr(fname)

DESCRIPTION ^

 vol=readinr(fname)

 load a volume from an INR file

 author: Qianqian Fang, <q.fang at neu.edu>
 date: 2009/05/03

 input:
      fname: input file name

 output:
      dat: output, data read from the inr file

 -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function dat=readinr(fname)
0002 %
0003 % vol=readinr(fname)
0004 %
0005 % load a volume from an INR file
0006 %
0007 % author: Qianqian Fang, <q.fang at neu.edu>
0008 % date: 2009/05/03
0009 %
0010 % input:
0011 %      fname: input file name
0012 %
0013 % output:
0014 %      dat: output, data read from the inr file
0015 %
0016 % -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
0017 %
0018 
0019 fid=fopen(fname,'rb');
0020 s=fread(fid,256,'uchar');
0021 
0022 s=char(s)';
0023 
0024 if(regexp(s,'#INRIMAGE-4')~=1)
0025    error('INRIMAGE header was not found')
0026 end
0027 
0028 nx=regexp(s,'XDIM\s*=\s*([0-9]+)','tokens');
0029 if(length(nx)) 
0030    nx=str2num(nx{1}{1});
0031 else
0032    error('no XDIM found');
0033 end
0034 
0035 ny=regexp(s,'YDIM\s*=\s*([0-9]+)','tokens');
0036 if(length(ny)) 
0037    ny=str2num(ny{1}{1});
0038 else
0039    error('no YDIM found');
0040 end
0041 
0042 nz=regexp(s,'ZDIM\s*=\s*([0-9]+)','tokens');
0043 if(length(nz)) 
0044    nz=str2num(nz{1}{1});
0045 else
0046    error('no ZDIM found');
0047 end
0048 
0049 nv=regexp(s,'VDIM\s*=\s*([0-9]+)','tokens');
0050 if(length(nv))
0051    nv=str2num(nv{1}{1});
0052 else
0053    nv=1;
0054 end
0055 
0056 type=regexp(s,'TYPE=([a-z ]+)','tokens');
0057 if(length(type))
0058    type=type{1}{1};
0059 else
0060    error('no TYPE found');
0061 end
0062 
0063 pixel=regexp(s,'PIXSIZE=([0-9]+)','tokens');
0064 if(length(pixel))
0065    pixel=str2num(pixel{1}{1});
0066 else
0067    error('no PIXSIZE found');
0068 end
0069 
0070 %header=sprintf(['#INRIMAGE-4#{\nXDIM=%d\nYDIM=%d\nZDIM=%d\nVDIM=1\nTYPE=%s\n' ...
0071 %  'PIXSIZE=%d bits\nCPU=decm\nVX=1\nVY=1\nVZ=1\n'],size(vol),btype,bitlen);
0072 
0073 if(strcmp(type,'unsigned fixed') & pixel==8)
0074    dtype='uint8';
0075 elseif(strcmp(type,'unsigned fixed') & pixel==16)
0076    dtype='uint16';
0077 elseif(strcmp(type,'float') & pixel==32)
0078    dtype='float';
0079 elseif(strcmp(type,'float') & pixel==64)
0080    dtype='double';
0081 else
0082    error('volume format not supported');
0083 end
0084 
0085 
0086 dat=fread(fid,nx*ny*nz*nv,dtype);
0087 fclose(fid);
0088 
0089 if(nv==1)
0090    dat=reshape(dat,[nx,ny,nz]);
0091 else
0092    dat=reshape(dat,[nx,ny,nz,nv]);
0093 end

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005