-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_read_annotation.m
185 lines (167 loc) · 6.5 KB
/
fs_read_annotation.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
function [vertices, label, colortable] = fs_read_annotation(filename, varargin)
%
% NAME
%
% function [vertices, label, colortable] = ...
% read_annotation(filename [, verbosity])
%
% ARGUMENTS
% INPUT
% filename string name of annotation file to read
%
% OPTIONAL
% verbosity int if true (>0), disp running output
% + if false (==0), be quiet and do not
% + display any running output
%
% OUTPUT
% vertices vector vector with values running from 0 to
% + size(vertices)-1
% label vector lookup of annotation values for
% + corresponding vertex index.
% colortable struct structure of annotation data
% + see below
%
% DESCRIPTION
%
% This function essentially reads in a FreeSurfer annotation file
% <filename> and returns structures and vectors that together
% assign each index in the surface vector to one of several
% structure names.
%
% COLORTABLE STRUCTURE
%
% Consists of the following fields:
% o numEntries: number of entries
% o orig_tab: filename of original colortable file
% o struct_names: cell array of structure names
% o table: n x 5 matrix
% Columns 1,2,3 are RGB values for struct color
% Column 4 is a flag (usually 0)
% Column 5 is the structure ID, calculated from
% R + G*2^8 + B*2^16 + flag*2^24
%
% LABEL VECTOR
%
% Each component of the <label> vector has a structureID value. To
% match the structureID value with a structure name, lookup the row
% index of the structureID in the 5th column of the colortable.table
% matrix. Use this index as an offset into the struct_names field
% to match the structureID with a string name.
%
% PRECONDITIONS
%
% o <filename> must be a valid FreeSurfer annotation file.
%
% POSTCONDITIONS
%
% o <colortable> will be an empty struct if not embedded in a
% FreeSurfer annotation file.
%
%
% read_annotation.m
% Original Author: Bruce Fischl
% CVS Revision Info:
% $Author: nicks $
% $Date: 2011/03/02 00:04:12 $
% $Revision: 1.7 $
%
% Copyright ?? 2011 The General Hospital Corporation (Boston, MA) "MGH"
%
% Terms and conditions for use, reproduction, distribution and contribution
% are found in the 'FreeSurfer Software License Agreement' contained
% in the file 'LICENSE' found in the FreeSurfer distribution, and here:
%
% https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense
%
% Reporting: freesurfer@nmr.mgh.harvard.edu
%
%
fp = fopen(filename, 'r', 'b');
verbosity = 1;
if ~isempty(varargin)
verbosity = varargin{1};
end;
if(fp < 0)
if verbosity, disp('Annotation file cannot be opened'); end;
return;
end
A = fread(fp, 1, 'int');
tmp = fread(fp, 2*A, 'int');
vertices = tmp(1:2:end);
label = tmp(2:2:end);
bool = fread(fp, 1, 'int');
if(isempty(bool)) %means no colortable
if verbosity, disp('No Colortable found.'); end;
colortable = struct([]);
fclose(fp);
return;
end
if(bool)
%Read colortable
numEntries = fread(fp, 1, 'int');
if(numEntries > 0)
if verbosity, disp('Reading from Original Version'); end;
colortable.numEntries = numEntries;
len = fread(fp, 1, 'int');
colortable.orig_tab = fread(fp, len, '*char')';
colortable.orig_tab = colortable.orig_tab(1:end-1);
colortable.struct_names = cell(numEntries,1);
colortable.table = zeros(numEntries,5);
for i = 1:numEntries
len = fread(fp, 1, 'int');
colortable.struct_names{i} = fread(fp, len, '*char')';
colortable.struct_names{i} = colortable.struct_names{i}(1:end-1);
colortable.table(i,1) = fread(fp, 1, 'int');
colortable.table(i,2) = fread(fp, 1, 'int');
colortable.table(i,3) = fread(fp, 1, 'int');
colortable.table(i,4) = fread(fp, 1, 'int');
colortable.table(i,5) = colortable.table(i,1) + colortable.table(i,2)*2^8 + colortable.table(i,3)*2^16 + colortable.table(i,4)*2^24;
end
if verbosity
disp(['colortable with ' num2str(colortable.numEntries) ' entries read (originally ' colortable.orig_tab ')']);
end
else
version = -numEntries;
if verbosity
if(version~=2)
disp(['Error! Does not handle version ' num2str(version)]);
else
disp(['Reading from version ' num2str(version)]);
end
end
numEntries = fread(fp, 1, 'int');
colortable.numEntries = numEntries;
len = fread(fp, 1, 'int');
colortable.orig_tab = fread(fp, len, '*char')';
colortable.orig_tab = colortable.orig_tab(1:end-1);
colortable.struct_names = cell(numEntries,1);
colortable.table = zeros(numEntries,5);
numEntriesToRead = fread(fp, 1, 'int');
for i = 1:numEntriesToRead
structure = fread(fp, 1, 'int')+1;
if (structure < 0)
if verbosity, disp(['Error! Read entry, index ' num2str(structure)]); end;
end
if(~isempty(colortable.struct_names{structure}))
if verbosity, disp(['Error! Duplicate Structure ' num2str(structure)]); end;
end
len = fread(fp, 1, 'int');
colortable.struct_names{structure} = fread(fp, len, '*char')';
colortable.struct_names{structure} = colortable.struct_names{structure}(1:end-1);
colortable.table(structure,1) = fread(fp, 1, 'int');
colortable.table(structure,2) = fread(fp, 1, 'int');
colortable.table(structure,3) = fread(fp, 1, 'int');
colortable.table(structure,4) = fread(fp, 1, 'int');
colortable.table(structure,5) = colortable.table(structure,1) + colortable.table(structure,2)*2^8 + colortable.table(structure,3)*2^16 + colortable.table(structure,4)*2^24;
end
if verbosity
disp(['colortable with ' num2str(colortable.numEntries) ' entries read (originally ' colortable.orig_tab ')']);
end
end
else
if verbosity
disp('Error! Should not be expecting bool = 0');
end;
end
fclose(fp);