@@ -318,51 +318,78 @@ def read_annot(filepath, orig_ids=False):
318
318
The names of the labels. The length of the list is n_labels.
319
319
"""
320
320
with open (filepath , "rb" ) as fobj :
321
+ # all data (apart from strings) in an .annot file is stored as
322
+ # big-endian int32
321
323
dt = ">i4"
324
+
325
+ # number of vertices
322
326
vnum = np .fromfile (fobj , dt , 1 )[0 ]
327
+
328
+ # vertex ids + annotation values
323
329
data = np .fromfile (fobj , dt , vnum * 2 ).reshape (vnum , 2 )
324
330
labels = data [:, 1 ]
325
331
332
+ # is there a colour table?
326
333
ctab_exists = np .fromfile (fobj , dt , 1 )[0 ]
327
334
if not ctab_exists :
328
335
raise Exception ('Color table not found in annotation file' )
336
+
337
+ # in old-format files, the next field will contain the number of
338
+ # entries in the colour table. In new-format files, this will be
339
+ # equal to -2
329
340
n_entries = np .fromfile (fobj , dt , 1 )[0 ]
341
+
342
+ # We've got an old-format .annot file.
330
343
if n_entries > 0 :
344
+
345
+ # orig_tab string length + string
331
346
length = np .fromfile (fobj , dt , 1 )[0 ]
332
347
orig_tab = np .fromfile (fobj , '>c' , length )
333
348
orig_tab = orig_tab [:- 1 ]
334
-
335
349
names = list ()
336
- ctab = np .zeros ((n_entries , 5 ), np .int )
350
+ ctab = np .zeros ((n_entries , 5 ), np .int32 )
337
351
for i in xrange (n_entries ):
352
+ # structure name length + string
338
353
name_length = np .fromfile (fobj , dt , 1 )[0 ]
339
354
name = np .fromfile (fobj , "|S%d" % name_length , 1 )[0 ]
340
355
names .append (name )
356
+ # RGBA
341
357
ctab [i , :4 ] = np .fromfile (fobj , dt , 4 )
342
- ctab [i , 4 ] = (ctab [i , 0 ] + ctab [i , 1 ] * (2 ** 8 ) +
358
+ # generate the annotation value
359
+ ctab [i , 4 ] = (ctab [i , 0 ] +
360
+ ctab [i , 1 ] * (2 ** 8 ) +
343
361
ctab [i , 2 ] * (2 ** 16 ) +
344
362
ctab [i , 3 ] * (2 ** 24 ))
363
+ # We've got a new-format .annot file
345
364
else :
365
+ # file version number
346
366
ctab_version = - n_entries
347
367
if ctab_version != 2 :
348
368
raise Exception ('Color table version not supported' )
349
- n_entries = np .fromfile (fobj , dt , 1 )[0 ]
350
- ctab = np .zeros ((n_entries , 5 ), np .int )
369
+ # maximum colour table index used
370
+ max_index = np .fromfile (fobj , dt , 1 )[0 ]
371
+ ctab = np .zeros ((max_index , 5 ), np .int32 )
372
+ # orig_tab string length + string
351
373
length = np .fromfile (fobj , dt , 1 )[0 ]
352
374
np .fromfile (fobj , "|S%d" % length , 1 )[0 ] # Orig table path
375
+ # number of entries to read from the file
353
376
entries_to_read = np .fromfile (fobj , dt , 1 )[0 ]
354
377
names = list ()
355
378
for i in xrange (entries_to_read ):
356
- np .fromfile (fobj , dt , 1 )[0 ] # Structure
379
+ # index of this entry
380
+ idx = np .fromfile (fobj , dt , 1 )[0 ]
381
+ # structure name length + string
357
382
name_length = np .fromfile (fobj , dt , 1 )[0 ]
358
383
name = np .fromfile (fobj , "|S%d" % name_length , 1 )[0 ]
359
384
names .append (name )
385
+ # RGBA
360
386
ctab [i , :4 ] = np .fromfile (fobj , dt , 4 )
361
- ctab [i , 4 ] = (ctab [i , 0 ] + ctab [i , 1 ] * (2 ** 8 ) +
362
- ctab [i , 2 ] * (2 ** 16 ))
363
- ctab [:, 3 ] = 255
387
+ ctab [i , 4 ] = (ctab [i , 0 ] +
388
+ ctab [i , 1 ] * (2 ** 8 ) +
389
+ ctab [i , 2 ] * (2 ** 16 ) +
390
+ ctab [i , 3 ] * (2 ** 24 ))
364
391
365
- labels = labels .astype (np .int )
392
+ labels = labels .astype (np .int32 )
366
393
367
394
if not orig_ids :
368
395
ord = np .argsort (ctab [:, - 1 ])
0 commit comments