0001 function [vertices, label, colortable] = Read_Brain_Annotation(filename)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 fp = fopen(filename, 'r', 'b');
0040
0041 if(fp < 0)
0042 disp('Annotation file cannot be opened');
0043 return;
0044 end
0045
0046 A = fread(fp, 1, 'int');
0047
0048 tmp = fread(fp, 2*A, 'int');
0049 vertices = tmp(1:2:end);
0050 label = tmp(2:2:end);
0051
0052 bool = fread(fp, 1, 'int');
0053 if(isempty(bool))
0054 disp('No Colortable found.');
0055 colortable = struct([]);
0056 fclose(fp);
0057 return;
0058 end
0059
0060 if(bool)
0061
0062
0063 numEntries = fread(fp, 1, 'int');
0064
0065 if(numEntries > 0)
0066
0067 disp(['Reading from Original Version']);
0068 colortable.numEntries = numEntries;
0069 len = fread(fp, 1, 'int');
0070 colortable.orig_tab = fread(fp, len, '*char')';
0071 colortable.orig_tab = colortable.orig_tab(1:end-1);
0072
0073 colortable.struct_names = cell(numEntries,1);
0074 colortable.table = zeros(numEntries,5);
0075 for i = 1:numEntries
0076 len = fread(fp, 1, 'int');
0077 colortable.struct_names{i} = fread(fp, len, '*char')';
0078 colortable.struct_names{i} = colortable.struct_names{i}(1:end-1);
0079 colortable.table(i,1) = fread(fp, 1, 'int');
0080 colortable.table(i,2) = fread(fp, 1, 'int');
0081 colortable.table(i,3) = fread(fp, 1, 'int');
0082 colortable.table(i,4) = fread(fp, 1, 'int');
0083 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;
0084 end
0085 disp(['colortable with ' num2str(colortable.numEntries) ' entries read (originally ' colortable.orig_tab ')']);
0086
0087 else
0088 version = -numEntries;
0089 if(version~=2)
0090 disp(['Error! Does not handle version ' num2str(version)]);
0091 else
0092 disp(['Reading from version ' num2str(version)]);
0093 end
0094 numEntries = fread(fp, 1, 'int');
0095 colortable.numEntries = numEntries;
0096 len = fread(fp, 1, 'int');
0097 colortable.orig_tab = fread(fp, len, '*char')';
0098 colortable.orig_tab = colortable.orig_tab(1:end-1);
0099
0100 colortable.struct_names = cell(numEntries,1);
0101 colortable.table = zeros(numEntries,5);
0102
0103 numEntriesToRead = fread(fp, 1, 'int');
0104 for i = 1:numEntriesToRead
0105 structure = fread(fp, 1, 'int')+1;
0106 if (structure < 0)
0107 disp(['Error! Read entry, index ' num2str(structure)]);
0108 end
0109 if(~isempty(colortable.struct_names{structure}))
0110 disp(['Error! Duplicate Structure ' num2str(structure)]);
0111 end
0112 len = fread(fp, 1, 'int');
0113 colortable.struct_names{structure} = fread(fp, len, '*char')';
0114 colortable.struct_names{structure} = colortable.struct_names{structure}(1:end-1);
0115 colortable.table(structure,1) = fread(fp, 1, 'int');
0116 colortable.table(structure,2) = fread(fp, 1, 'int');
0117 colortable.table(structure,3) = fread(fp, 1, 'int');
0118 colortable.table(structure,4) = fread(fp, 1, 'int');
0119 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;
0120 end
0121 disp(['colortable with ' num2str(colortable.numEntries) ' entries read (originally ' colortable.orig_tab ')']);
0122 end
0123 else
0124 disp('Error! Should not be expecting bool = 0');
0125 end
0126
0127 fclose(fp);
0128
0129