gw_logo_08.gif (1982 bytes)  
Last edit: 05-08-29 Graham Wideman

FreeSurfer

FreeSurfer Surface File Formats
Article created: 2003-01-13

To find new additions, search for "Update"

Overview: Volumes and Surfaces

This page provides some documentation for surface file formats used with the FreeSurfer suite of programs.

Loosely speaking, the FreeSurfer suite of programs deals with two main kinds of data. Volumes of voxels, as for example from an MRI scanner, provide one source of raw input. Some FreeSurfer programs deal with manipulating and viewing these volumes and also with deriving from those volumes various surfaces.

The surface data consists of lists of vertices (and their positions in space), and faces (usually triangles, sometimes quadrangles) and the vertices associated with them. A number of FreeSurfer programs are thus devoted to manipulating, viewing and analyzing these surface files. FreeSurfer can derive some per-vertex or per-face data items, and also provides ways for you to add your own per-vertex data. This kind of associated data can also be viewed and analyzed with some FreeSurfer tools.

Vertices and Patches

FreeSurfer surface data involves vertices which will be subjected to various transformations. Consequently it's necessary to be able to track each vertex through this process, and for this purpose FreeSurfer assigns each vertex an id or "vertex number". For a set of data derived from a particular original surface analysis, a particular vertex number always refers to the same vertex, though in different surfaces the (x,y,z) values for that vertex may have been transformed. It is to this vertex number that additional data items (such as curvature, thickness or functional activity) can be attached.

Patches are subsections of complete surfaces, hence containing only a subset of the vertices. Patches retain the same vertex numbering, so that a patch's vertices can be cross-referenced to the complete set. Since a patch does not contain the complete set of vertices, the numbering of vertices in a patch will be non-contiguous.

Motivatation for This Page

In my view, to work with a suite of data-manipulating programs, it's essential to have a clear mental model of the conceptual structure of that data. Since the data digested by FreeSurfer is mostly in binary form, the structure of the data is not readily amenable to inspection.

In order to obtain a clear model, this page is an effort to document the data format of the various files that FreeSurfer employs. This should reveal the structures that are at the heart of the matter.

A caution is in order however: as with any research software, exact data formats need to be changed as the software is used and new challenges addressed. The knowledge of the conceptual structure of the data may have a lengthy shelf life, but code written to these exact binary format specs may be vulnerable to changes. The program mris_convert, which will presumably be updated to work with revised formats, may permit your programs to read/write ASCII files that are less disrupted by binary format changes.

Preliminary Notes

Issue

Discussion
Version Info Much of this information was derived from inspecting sections of mris_convert source code, which I think was as of mid Jan 2003.
Status There are several formats I can't complete for lack of source code for the routines that read or write them. These are highlighted in yellow.
Data Sizes Where I know the data sizes (for binary files), I write them like Int2 to mean a 2-byte integer. There are several places where an Int3 (3-byte integer) is used -- this is not an error. There are also numerous places where I know the type but not the size, which I have written like Int? and F?.
Correctness As of this writing, I've not cross-checked these format specs against actual files, or even against file sizes. Use at own risk!
"Magic Number" Format Id A number of the binary file formats contain an initial number that identifies the file format. See individual binary file formats
FileNameType The format of some data files can be identified from the filename extension, with corresponding constant in mris_convert source code:
 
Constants Sort of Used in the Neighborhood of
 MRISfileNameType()
Value Ext'n Magic Format Id number? Desciption
MRIS_BINARY_FILE 0? none Y This is default returned by MRISfileNameType when there's no special fle extension. This seems to pertain to most/all FreeSurfer proprietary binary formats. Most of those formats start with a "magic" format id number that allows code to distinguish which FreeSurfer format this uses, primarily distinguishing between FreeSurfer Triangle binary format (2 variants?) and FreeSurfer Quadrangle binary format (2 variants?)
MRIS_BINARY_QUADRANGLE_FILE 0 none Y Not returned by MRISfileNameType, but used by caller to specify FreeSurfer Quadrangle Binary format once the magic format id number has been checked.
MRIS_ASCII_TRIANGLE_FILE 1 ASC n/a FreeSurfer Triangle ASCII format
MRIS_ASCII_FILE 1 ASC n/a ASCII format, not necessarily triangles
MRIS_GEO_TRIANGLE_FILE 2 GEO N movie.byu format
MRIS_ICO_SURFACE 3 n/a N Icosahedra surface. Used for mris->type, but not by MRISfileNameType.
MRIS_ICO_FILE 4 ICO Y? ICO = Icosahedra binary format. TRI = Triangular Surface binary format

mris_convert [MRISfileNameType] does not distinguish between ICO and TRI. mris_convert reads ICO/TRI files using  read_icosahedron() which I don't have.

However,  mris_convert writes ICO/TRI files using MRISwriteTriangularSurface, which produces the same format that mris_convert reads from files with no extension and magic number indicating FreeSurfer Triangle binary file.

Thus, I conclude that ICO format is the same as TRI format, which in turn is the same as FreeSurfer Triangle binary format.

MRIS_TRIANGULAR_SURFACE 4 TRI
MRIS_VTK_FILE 5 VTK N "Visualization Toolkit" format (vtk is a popular pubic domain visualization language)
Index from 0/1 Where FreeSurfer cares about numbering of items (primarily VertexNumber) these are numbered from zero. (Exceptions noted.)
Byte Order FreeSurfer programs assume that the seevral bytes of a number are organized in a file Big-Endian style -- that is most-significant byte first. This is opposite order to Intel-processor-equipped system.

Surface File Formats

FreeSurfer works with a number of different file formats which define a surface. Surface files consist of:

a) a list of vertices, each with a Vertex Number, which is a unique id within this surface, and (x, y, z) positions.
b) a list of faces, giving the vertices which define the face. (Faces do not have an id.)

The main variation is whether the surface file employs triangle faces (three vertices) or quadrangle faces (four vertices). mris_convert's input code seems happy with either as input, don't know if this is the case with other FreeSurfer software.

There are other minor variations in format, and in addition some FreeSurfer programs can read surfaces from an ASCII file, or from some foreign binary formats.

Updated 2003-05-14: Note that the surface files always seems to use RAS (Right, Anterior, Superior) coordinate system. See Understanding FreeSurfer Coordinates.

FreeSurfer Triangle Surface Binary Format

Updated 2004-02-24 Verified with actual code.

Info Item

Description
Filename Pattern none
FileNameType Const MRIS_TRIANGULAR_SURFACE 
 

Location (byte)

Item

Type

Comment
0 FileTypeId Int3 "Magic" number identifies file format.
TRIANGLE_FILE_MAGIC_NUMBER = (-2 & 0x00ffffff)
3 Creator, User String "created by %s on %s\n", user, time_str (Note: this field is variable length, which means remainder of data is in positions that differ from one such file to another).
Update 2004-02-25: I had previously thought this field was null terminated, as it's always followed by a null (0) byte. However, files I produce with this fulfilled are incorrectly read by various FreeSurfer software. It appears that files generated by FS programs have this comment terminated by two "\n" characters (0x0A), and that FS's loader must be looking for one or two of those.
This, by the way, makes the following field more sensible.
? VertexCount Int4 Count of vertices.
Note: I previously thought this field was an Int3, preceded by the null terminator of the preceding string field. However this appears not to be the case (see description of preceding field.)
? FaceCount Int4 Count of triangles
VertexCount of R A S F4, F4, F4 4-byte floats specifying Right, Anterior, Superior

FaceCount of

v0 v1 v2 Int4, Int4, Int4  

(From MRISread->MRISreadOverAlloc; MRISwriteTriangularSurface)

Triangle Surface File - ASCII Version

Updated 2003-05-14: Not tested, but (x y z) is probably actually (R A S).

Info Item

Description
Filename Pattern xxx.asc
FileNameType Const MRIS_ASCII_TRIANGLE_FILE (or MRIS_ASCII_FILE?)
 

Line

Item

Types

Format, Comment
0 Comment   "#!ascii version of %s\n", mris->fname
1 VertexCount FaceCount Int, Int "%d %d\n", mris->nvertices, mris->nfaces
VertexCount lines of x y z IsInPatch F, F, F, 0/1 "%f %f %f %d\n", v->x, v->y, v->z, v->ripflag; 

FaceCount lines of

v0 v1 v2 [v3] IsInPatch Int, Int, Int, [Int,] 0/1 (VERTICES_PER_FACE of "%d ", face->v[n]), "%d\n", face->ripflag
v0..v3 are the vertex numbers of the face's corners.
Const VERTICES_PER_FACE could be 3 or 4, but currently set to 3

(From mrisWriteASCII, mrisReadAsciiFile)

Quadrangle File

Updated 2003-05-14 Verified with actual code

Info Item

Description
Filename Pattern none
FileNameType Const MRIS_BINARY_QUADRANGLE_FILE

Location (byte)

Item

Type

Comment
0 FileTypeId Int3 "Magic" number identifies file format.
QUAD_FILE_MAGIC_NUMBER = (-1 & 0x00ffffff)
3 VertexCount Int3 Count of vertices
6 Quad FaceCount Int3 Ie: There are two triangles per quadrangle, so multiply this by two to get triangle count. (Updated 2003-05-29)
VertexCount of R*100 A*100 S*100 Int2, Int2, Int2 R, A, S  are scaled by 100 so that an int can represent a position with 2 decimal places

QuadFaceCount of

v0 v1 v2 v3 Int3, Int3, Int3, Int3 v0..v3 are four vertex numbers.
Picks 3 vertices from face 2n, and one vertex from face 2n+1.
To create triangles, use in consistent order: (v0, v1, v2) and (v2, v3, v0).  (Updated 2003-07-06).

(From MRISwrite default code)

"New" Quadrangle File

The "new" quadrangle code in mris_convert is #if'ed out, but if it was operational it would be the same as the "old" format, with the following exceptions:

Location (byte)

Item

Type

Comment
0 FileTypeId Int3 NEW_QUAD_FILE_MAGIC_NUMBER = (-3 & 0x00ffffff)
3 same    
6 same    
VertexCount of x y z F?, F?, F? Probably R A S

FaceCount/2 of

same    

(From MRISwrite default code)

Icosahedron File

Info Item

Description
Filename Pattern xxx.ico
xxx.tri
FileNameType Const MRIS_TRIANGULAR_SURFACE =
MRIS_ICO_FILE

Need read_icosahedron( ) to determine this for sure, but I believe this is really just the same as FreeSurfer Triangle Surface Binary Format.

Patch Format

Patch File -Binary

Location (byte)

Item

Types

Comment
0 PointCount Int4 Count of points to follow (Updated 2003-07-06)
? + ?n vtx x y z Int4, Int2, Int2, Int2 vtx is special:
(mris->vertices[k].border)?-(k+1):k+1;
See below for explanation
(Updated 2003-07-06)

The vtx data item provides the Vertex Number as follows:

If the vertex is not on the border of the patch, then vtx = -(VertexNum+1)
If the vertex is part of the border, then vtx =VertexNum+1.

Ie: the magnitude of vtx is VertexNum+1, and the sign indicates on or off the border (negative means not on border). (The one is added to avoid a problem with the sign of vertex zero.)  (To-do: Check that sense of vertex.border is 1=border!)

(From MRISreadPatch and MRISwritePatch)

Patch File - ASCII

Info Item

Description
Filename Pattern xxx.asc
FileNameType Const MRIS_ASCII_FILE?

Very similar to FreeSurfer Triangle Surface ASCII format, except that the file only includes vertices and faces that are included in the patch, and thus omits the flag showing wheher the vertex/face is included in the patch.

Line

Item

Types

Format, Comment
0 Comment   "#!ascii version of patch %s\n", mris->fname
1 VertexCount FaceCount Int, Int "%d %d\n", nvertices, nfaces
VertexCount lines of x y z F, F, F "%f %f %f\n", v->x, v->y, v->z

FaceCount lines of

v0 v1 v2 [v3] Int, Int, Int, [Int,] 0/1 (VERTICES_PER_FACE of "%d ", face->v[n]),
v0..v3 are the vertex numbers of the face's corners.
Const VERTICES_PER_FACE could be 3 or 4, but currently set to 3

(From MRISwritePatchASCII.)

Patch File - Geo

See Geo Triangle File

"Additional Data" File Formats

W ("Weight") Format

Info Item

Description
Filename Pattern xxx.w
FileNameType Const none 

A W file provides values to associate with vertices. ("W" originally stood for "weight", but now is used for generic surface overlay). Since a W file includes both a VertexNum and Value for each item, it can specify value for non-contiguously-numbered vertices, possibly providing values for only a subset of a surface's vertices, and possibly not in sequential order.

Location (byte)

Item

Type

Comment
0 Latency Int2  
2 Count Int3 Count of vertex-value pairs to follow

5 + 7n

VertexNum

Int3

 

5 + 7n + 3

VertexVal

Float4

mris_convert uses %f

(From convertTo/FromWFile.)

W File - ASCII

Info Item

Description
Filename Pattern xxx.asc
FileNameType Const MRIS_ASCII_FILE

Line

Item

Type

Comment
0 Latency Int2 mris_convert ignores this on input
1 Count Int3 Count of vertex-value pairs to follow.
mris_convert copies this without checking.

2...

VertexNum, VertexVal

Int3, Float4

 

(From convertTo/FromWFile.)

"Curvature" (Per-Vertex Data) Files

Notes:

 In general "curvature" files are used to associate some additional value with a vertex. Although this additional value might be a curvature as calculated by FreeSurfer, it is also useful to derive or supply other data values per vertex, and curvature-format files can be used to do this also. Thus, it might be more helpful to think of these files as "additional-value-per-vertex" files.

Also, a Curvature file is semantically very similar to a W file. The difference is that a curvature file is obliged to supply a value for every vertex, while a W file can supply a value only for a subset of vertices. (Updated 2003-07-06).

"Old" Curv File

Location (byte)

Item

Type

Comment
0 VertexCount Int3 Count of vertices (must match that of surface file to which this data is being associated.)
3 FaceCount Int3 Count of faces (not useful?)

6 + 2n

VertexValue

Int2

fread2(&i,fp);
curv = i/100.0; (Note divide by hundred!)

(From MRISreadCurvatureFile)

"New" Curv File

Updated 2003-05-29

Location (byte)

Item

Type

Comment
0 FileTypeId Int3 "Magic" number identifies file format. For this file type, this is a 3-byte -1 = 0x00ffffff = 16777215
3 VertexCount Int4 Count of vertices (must match that of surface file to which this data is being associated.)
7 FaceCount Int4 Count of faces (not useful?)
11 ValsPerVertex Int4 Number of values supplied per vertex (currently only 1 is supported).

15 + 4n

VertexValue

Float4

 

(From MRISreadNewCurvatureFile, MRISwriteCurvatureFile)

Examples of this kind of file: the lh.curv and rh.curv files produced by FreeSurfer, along with thickness and area files.

Curv File - ASCII

This file format provides a list of vertices, giving vertex number, (x,y,z) position and a single additional data value per vertex. Although this additional value can represent curvature, it can also be used to mean something else entirely.

Line

Item

Types

Comment
0... VertexNum, x, y, z, curv Int, F, F, F, F mris_convert uses:
%3.3d %2.5f %2.5f %2.5f %2.5f

(From writeAsciiCurvFile)

Foreign Formats

VTK File

Info Item

Description
Filename Pattern xxx.vtk
FileNameType Const MRIS_VTK_FILE

"Visualization Toolkit" format (vtk is a popular pubic domain visualization language)

Line

Item

Types

Format, Comment
0 Comment   "# vtk DataFile Version 1.0\n"
1 vtk format description   "vtk output\nASCII\nDATASET POLYDATA\nPOINTS %d float\n", mris->nvertices
VertexCount lines of x y z F, F, F "%f %f %f\n", v->x, v->y, v->z
  vtk format description   "POLYGONS %d %d\n", mris->nfaces, mris->nfaces*4

FaceCount lines of

VertPerFace v0 v1 v2 [v3] Int, Int, Int, [Int,] 0/1 VERTICES_PER_FACE;, (VERTICES_PER_FACE of "%d ", face->v[n])";
v0..v3 are the vertex numbers of the face's corners.
Const VERTICES_PER_FACE could be 3 or 4, but currently set to 3

(From MRISwriteVTK )

Geo Triangle File (movie.byu format)

Info Item

Description
Filename Pattern xxx.geo
FileNameType Const MRIS_GEO_TRIANGLE_FILE

Need MRISwriteGeo

ICO Surface Format

Info Item

Description
Filename Pattern xxx.ico
FileNameType Const MRIS_ICO_FILE

"Icosahedra" surface format -- same as Triangular Surface format.


Go to:  gw_logo_08.gif (1982 bytes)  or back to FreeSurfer home