Last edit: 05-08-29 Graham Wideman |
FreeSurfer |
FreeSurfer Surface File Formats Article created: 2003-01-13 |
To find new additions, search for "Update"
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.
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.
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.
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:
|
||||||||||||||||||||||||||||||||||||||||||||||||
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. |
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.
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)
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)
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)
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)
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.
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)
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.)
See Geo Triangle File
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.)
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.)
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).
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)
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.
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)
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 )
Info Item |
Description |
Filename Pattern | xxx.geo |
FileNameType Const | MRIS_GEO_TRIANGLE_FILE |
Need MRISwriteGeo
Info Item |
Description |
Filename Pattern | xxx.ico |
FileNameType Const | MRIS_ICO_FILE |
"Icosahedra" surface format -- same as Triangular Surface format.
Go to: or back to FreeSurfer home