forked from fangq/iso2mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readinr.m
93 lines (79 loc) · 1.75 KB
/
readinr.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function dat=readinr(fname)
%
% 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)
%
fid=fopen(fname,'rb');
s=fread(fid,256,'uchar');
s=char(s)';
if(regexp(s,'#INRIMAGE-4')~=1)
error('INRIMAGE header was not found')
end
nx=regexp(s,'XDIM\s*=\s*([0-9]+)','tokens');
if(length(nx))
nx=str2num(nx{1}{1});
else
error('no XDIM found');
end
ny=regexp(s,'YDIM\s*=\s*([0-9]+)','tokens');
if(length(ny))
ny=str2num(ny{1}{1});
else
error('no YDIM found');
end
nz=regexp(s,'ZDIM\s*=\s*([0-9]+)','tokens');
if(length(nz))
nz=str2num(nz{1}{1});
else
error('no ZDIM found');
end
nv=regexp(s,'VDIM\s*=\s*([0-9]+)','tokens');
if(length(nv))
nv=str2num(nv{1}{1});
else
nv=1;
end
type=regexp(s,'TYPE=([a-z ]+)','tokens');
if(length(type))
type=type{1}{1};
else
error('no TYPE found');
end
pixel=regexp(s,'PIXSIZE=([0-9]+)','tokens');
if(length(pixel))
pixel=str2num(pixel{1}{1});
else
error('no PIXSIZE found');
end
%header=sprintf(['#INRIMAGE-4#{\nXDIM=%d\nYDIM=%d\nZDIM=%d\nVDIM=1\nTYPE=%s\n' ...
% 'PIXSIZE=%d bits\nCPU=decm\nVX=1\nVY=1\nVZ=1\n'],size(vol),btype,bitlen);
if(strcmp(type,'unsigned fixed') & pixel==8)
dtype='uint8';
elseif(strcmp(type,'unsigned fixed') & pixel==16)
dtype='uint16';
elseif(strcmp(type,'float') & pixel==32)
dtype='float';
elseif(strcmp(type,'float') & pixel==64)
dtype='double';
else
error('volume format not supported');
end
dat=fread(fid,nx*ny*nz*nv,dtype);
fclose(fid);
if(nv==1)
dat=reshape(dat,[nx,ny,nz]);
else
dat=reshape(dat,[nx,ny,nz,nv]);
end