@@ -81,24 +81,24 @@ def _read_volume_info(fobj):
81
81
return volume_info
82
82
83
83
84
- def _pack_rgba ( rgba ):
85
- """Pack an RGBA sequence into a single integer.
84
+ def _pack_rgb ( rgb ):
85
+ """Pack an RGB sequence into a single integer.
86
86
87
87
Used by :func:`read_annot` and :func:`write_annot` to generate
88
88
"annotation values" for a Freesurfer ``.annot`` file.
89
89
90
90
Parameters
91
91
----------
92
- rgba : ndarray, shape (n, 4 )
93
- RGBA colors
92
+ rgb : ndarray, shape (n, 3 )
93
+ RGB colors
94
94
95
95
Returns
96
96
-------
97
97
out : ndarray, shape (n, 1)
98
98
Annotation values for each color.
99
99
"""
100
- bitshifts = 2 ** np .array ([[0 ], [8 ], [16 ], [ 24 ]] , dtype = rgba .dtype )
101
- return rgba .dot (bitshifts )
100
+ bitshifts = 2 ** np .array ([[0 ], [8 ], [16 ]] , dtype = rgb .dtype )
101
+ return rgb .dot (bitshifts )
102
102
103
103
104
104
def read_geometry (filepath , read_metadata = False , read_stamp = False ):
@@ -333,9 +333,13 @@ def read_annot(filepath, orig_ids=False):
333
333
Annotation file format versions 1 and 2 are supported, corresponding to
334
334
the "old-style" and "new-style" color table layout.
335
335
336
+ Note that the output color table ``ctab`` is in RGBT form, where T
337
+ (transparency) is 255 - alpha.
338
+
336
339
See:
337
340
* https://surfer.nmr.mgh.harvard.edu/fswiki/LabelsClutsAnnotationFiles#Annotation
338
341
* https://github.com/freesurfer/freesurfer/blob/dev/matlab/read_annotation.m
342
+ * https://github.com/freesurfer/freesurfer/blob/8b88b34/utils/colortab.c
339
343
340
344
Parameters
341
345
----------
@@ -352,7 +356,7 @@ def read_annot(filepath, orig_ids=False):
352
356
Annotation id at each vertex. If a vertex does not belong
353
357
to any label and orig_ids=False, its id will be set to -1.
354
358
ctab : ndarray, shape (n_labels, 5)
355
- RGBA + label id colortable array.
359
+ RGBT + label id colortable array.
356
360
names : list of str (python 2), list of bytes (python 3)
357
361
The names of the labels. The length of the list is n_labels.
358
362
"""
@@ -384,7 +388,7 @@ def read_annot(filepath, orig_ids=False):
384
388
ctab , names = _read_annot_ctab_new_format (fobj , - n_entries )
385
389
386
390
# generate annotation values for each LUT entry
387
- ctab [:, [4 ]] = _pack_rgba (ctab [:, :4 ])
391
+ ctab [:, [4 ]] = _pack_rgb (ctab [:, :3 ])
388
392
389
393
if not orig_ids :
390
394
ord = np .argsort (ctab [:, - 1 ])
@@ -397,6 +401,9 @@ def read_annot(filepath, orig_ids=False):
397
401
def _read_annot_ctab_old_format (fobj , n_entries ):
398
402
"""Read in an old-style Freesurfer color table from `fobj`.
399
403
404
+ Note that the output color table ``ctab`` is in RGBT form, where T
405
+ (transparency) is 255 - alpha.
406
+
400
407
This function is used by :func:`read_annot`.
401
408
402
409
Parameters
@@ -412,7 +419,7 @@ def _read_annot_ctab_old_format(fobj, n_entries):
412
419
-------
413
420
414
421
ctab : ndarray, shape (n_entries, 5)
415
- RGBA colortable array - the last column contains all zeros.
422
+ RGBT colortable array - the last column contains all zeros.
416
423
names : list of str
417
424
The names of the labels. The length of the list is n_entries.
418
425
"""
@@ -430,7 +437,7 @@ def _read_annot_ctab_old_format(fobj, n_entries):
430
437
name_length = np .fromfile (fobj , dt , 1 )[0 ]
431
438
name = np .fromfile (fobj , "|S%d" % name_length , 1 )[0 ]
432
439
names .append (name )
433
- # read RGBA for this entry
440
+ # read RGBT for this entry
434
441
ctab [i , :4 ] = np .fromfile (fobj , dt , 4 )
435
442
436
443
return ctab , names
@@ -439,6 +446,9 @@ def _read_annot_ctab_old_format(fobj, n_entries):
439
446
def _read_annot_ctab_new_format (fobj , ctab_version ):
440
447
"""Read in a new-style Freesurfer color table from `fobj`.
441
448
449
+ Note that the output color table ``ctab`` is in RGBT form, where T
450
+ (transparency) is 255 - alpha.
451
+
442
452
This function is used by :func:`read_annot`.
443
453
444
454
Parameters
@@ -454,7 +464,7 @@ def _read_annot_ctab_new_format(fobj, ctab_version):
454
464
-------
455
465
456
466
ctab : ndarray, shape (n_labels, 5)
457
- RGBA colortable array - the last column contains all zeros.
467
+ RGBT colortable array - the last column contains all zeros.
458
468
names : list of str
459
469
The names of the labels. The length of the list is n_labels.
460
470
"""
@@ -480,7 +490,7 @@ def _read_annot_ctab_new_format(fobj, ctab_version):
480
490
name_length = np .fromfile (fobj , dt , 1 )[0 ]
481
491
name = np .fromfile (fobj , "|S%d" % name_length , 1 )[0 ]
482
492
names .append (name )
483
- # RGBA
493
+ # RGBT
484
494
ctab [idx , :4 ] = np .fromfile (fobj , dt , 4 )
485
495
486
496
return ctab , names
@@ -489,9 +499,13 @@ def _read_annot_ctab_new_format(fobj, ctab_version):
489
499
def write_annot (filepath , labels , ctab , names , fill_ctab = True ):
490
500
"""Write out a "new-style" Freesurfer annotation file.
491
501
502
+ Note that the color table ``ctab`` is in RGBT form, where T (transparency)
503
+ is 255 - alpha.
504
+
492
505
See:
493
506
* https://surfer.nmr.mgh.harvard.edu/fswiki/LabelsClutsAnnotationFiles#Annotation
494
507
* https://github.com/freesurfer/freesurfer/blob/dev/matlab/write_annotation.m
508
+ * https://github.com/freesurfer/freesurfer/blob/8b88b34/utils/colortab.c
495
509
496
510
Parameters
497
511
----------
@@ -500,7 +514,7 @@ def write_annot(filepath, labels, ctab, names, fill_ctab=True):
500
514
labels : ndarray, shape (n_vertices,)
501
515
Annotation id at each vertex.
502
516
ctab : ndarray, shape (n_labels, 5)
503
- RGBA + label id colortable array.
517
+ RGBT + label id colortable array.
504
518
names : list of str
505
519
The names of the labels. The length of the list is n_labels.
506
520
fill_ctab : {True, False} optional
@@ -523,8 +537,8 @@ def write_string(s):
523
537
524
538
# Generate annotation values for each ctab entry
525
539
if fill_ctab :
526
- ctab = np .hstack ((ctab [:, :4 ], _pack_rgba (ctab [:, :4 ])))
527
- elif not np .array_equal (ctab [:, [4 ]], _pack_rgba (ctab [:, :4 ])):
540
+ ctab = np .hstack ((ctab [:, :4 ], _pack_rgb (ctab [:, :3 ])))
541
+ elif not np .array_equal (ctab [:, [4 ]], _pack_rgb (ctab [:, :3 ])):
528
542
warnings .warn ('Annotation values in {} will be incorrect' .format (
529
543
filepath ))
530
544
0 commit comments