# MINC/SoftwareDevelopment/MINC1-volumeio-programmers-reference

This document describes a library of routines available at the McConnell Brain Imaging Centre (BIC) at the Montreal Neurological Institute. It was developed as part of a medical imaging software testbed by David MacDonald, with source code and considerable input from Peter Neelin, Louis Collins, and others at the Centre.
The library, which is referred to as the BIC Volume IO Library, comprises a set of functions for reading and writing volumes of medical imaging data, as well as some general support routines useful to many programming tasks. Images at the Brain Imaging Centre are stored on disk in a format called MINC, which stands for Medical Image Net CDF. More information is available in related documentation specifically concerning this format and the MINC library of routines used to read and write MINC files.
The BIC Volume IO Library is built on top of the MINC library to provide easy access to MINC files for most general operations, without having to learn too much of the details of the MINC library, which is a comprehensive system for handling most conceivable cases. The BIC Volume IO Library provides a structure for internal storage of volumes and routines to access and modify volumes. In addition, it provides routines to manipulate tag points and transformations and to perform input and output on these objects in the standardized formats of the Brain Imaging Centre.
This document describes where to find the BIC Volume IO Library, what functionality is provided, and how to integrate it into a user's programs. The library is written in C source code, and is designed to be linked with C source. It makes calls to two other libraries: MINC (the BIC file format library) and netcdf (a portable file format manager).

# Compiling and Linking

The library for linking is libvolume_io.a, which is in /usr/local/lib, and the related include files are in the directory, /usr/local/include under volume_io. Both directories are usually in the compiler search path. Source files making calls to Volume IO functions must have the following line at the top, to include the relevant type definitions and prototypes:
In order to link with BIC Volume IO Library, the relevant libraries must be specified:
The two libraries, minc and netcdf are usually in /usr/local/lib, which is automatically in the search path. The -lm option includes the math library, which is sometimes called by the BIC Volume IO Library. The -lmalloc library provides faster and more robust memory allocation than the default.

# Style

In order to use this library, one must become accustomed to certain subtleties of style, some of which are peculiar to the author of this software and some of which have evolved in ways dependent on history of the software. Therefore, it is important to note the following style-related issues, for which no apologies are forthcoming.

## Global Variables

The philosophy that global variables are generally a bad idea has been adopted. In many cases, using global variables within functions hides the behaviour of a function, and therefore, it is preferable to specify all relevant information to a function in the argument list, rather than relying on any global variables. Secondly, modification of a global variable by a given function precludes the use of this function in a multi-threaded environment (two processes may attempt to modify the global variable at the same time). As a result of adopting the anti-global variable philosophy, the user of the BIC Volume IO Library often must specify many arguments to a function. This seems a small price to pay, in return for the ability to see all factors controlling the behaviour of each function in the argument list.

## Identifier Names

The implementer of this library has an affinity to typing large amounts of text, and generally dislikes abbreviations. As a result, the function and variable names occurring in the BIC Volume IO Library may be quite verbose, almost to the point of being complete grammatically correct sentences. In general, one will never see functions with names such as rem_garb. Instead, functions are more likely to have names such as remove_garbage, and, in many cases, even take_garbage_to_curb. As a result, the user of the BIC Volume IO Library will have to type longer-than-usual function and variable names, but hopefully will have a clearer insight into the associated functionality.

## Structure Arguments

There are many objects defined throughout the library as structures of varying and possibly large sizes. Due to the inefficiency of passing structures by value (copying the whole structure to a function), structures are generally passed to functions by reference. Because of this, there is always a potential to pass an invalid pointer to a function which expects a pointer to a structure. The following examples illustrate the right and wrong ways to use such a function. Given a structure and a library function which initializes it,
The correct method of using this function
The incorrect method of using this function
because the variable s is an uninitialized pointer. The correct method is to define a structure variable, not a pointer to a structure, and pass a pointer to the structure:
Alternately, the incorrect example above could have been corrected by allocating the s pointer before calling initialize.

# Types and Macros

There are several types and macros defined for use with the BIC Volume IO Library. All function declarations in the library are preceded with either the word public or private, which indicates whether the function is accessible from outside the file in which it resides. Users of the library will only be interested in those functions preceded by public. They are defined as follows:
A type for logical values is defined:
Other useful types defined include:
Some macros useful for general programming include:
A constant equal to 3, the number of dimensions in the real world.
A constant equal to 0, used as an index into various XYZ structures.
A constant equal to 1, used as an index into various XYZ structures.
A constant equal to 2, used as an index into various XYZ structures.
returns the number of elements in a statically allocated array, for example,
returns the nearest integer to the x. If halfway in between two integers, returns the higher of the two. Works correctly for negative, zero, and positive numbers.
returns TRUE if the real argument is exactly an integer.
returns the fractional part of the argument.
returns the largest integer less than or equal to the argument.
returns the smallest integer greater than or equal to the argument.
returns the absolute value of an integer or real.
returns the maximum of two integers or reals.
returns the maximum of three integers or reals.
returns the minimum of two integers or reals.
returns the minimum of three integers or reals.
returns the interpolation between a and b, where alpha is in the range zero to one.
returns the value of Π
returns the number of radians per degrees, used to multiply an angle in degrees to result in radians.
returns the number of degrees per radian, used to multiply an angle in radians to result in degrees.
converts the indices [i, j] of a 2-D ni by nj array into a single index, based on row-major order.
converts the indices [i, j, k] of a 3-D ni by nj by nk array into a single index, based on row-major order.
performs a for loop where i starts at start and increments until it is greater than or equal to end.
Equivalent to for( i = start; i < end; ++i ).
performs a for loop where i starts at start and increments until it is greater than end.
Equivalent to for( i = start; i <= end; ++i ).
Special C source macro to stick two different identifiers together, i.e., GLUE(colour,_name) results in colour_name.
Special C source macro to stick three different identifiers together, i.e., GLUE(a,b,c) results in abc.

# Programming Utilities

A set of functions which are useful for general purpose, portable programming is provided. These include such basic areas as strings, time, file IO, etc. Many parts of the BIC Volume IO Library refer to the programming utilities, and it is advisable that users of the BIC Volume IO Library try to use these functions whenever convenient. The following is a list of all the programming utilities, grouped into related areas, and ordered alphabetically.

## Strings

Some simple string manipulation techniques are provided. Strings are arbitrarily long NULL-terminated character arrays, which are allocated and deleted as needed. The type STRING is defined as:
The basic string creation and deletion routines are:
The function alloc_string allocates storage for a string of the desired length (by allocating length+1 bytes), without assigning any value to the string. The function create_string creates an allocated string with the value equal to initial. If initial is a NULL pointer an empty string (``) is created.
The storage associated with a string may be deleted with this function.
Returns the length of the string.
Returns TRUE if the two strings are exactly equal.
A convenience function which deletes the string argument, and reassigns it to the new_string. It does not copy the value of new_string, but merely sets the string to the pointer new_string.
Concatenates a character to the end of the string.
Creates and returns a string which is the concatenation of the two arguments, without changing the string arguments.
Replaces the argument string with the concatenation of the arguments string and str2.
Returns TRUE if the character is a lower case letter.
Returns TRUE if the character is an upper case letter.
If the character is upper case, returns the lower case version of the character, otherwise, returns the character itself.
If the character is lower case, returns the upper case version of the character, otherwise, returns the character itself.
Determines if the string ends in the specified ending. For instance, passing the arguments, "rainfall" and "fall" returns TRUE.
Returns a string which is the argument str without any leading or trailing blanks.
Converts the string to an all upper case string. Modifies the string in place.
Searches for the given character in the given string, returning the index where it was found, or -1 if it was not found.
Returns true if the string is empty or consists of only space, tab, and newline characters.

## General File IO

Although one may use the standard UNIX file interface (fprintf, for example), the BIC Volume IO Library contains a set of routines for doing all file operations, with the potential to be portable to other operating systems. There are some other minor advantages, including the automatic expansion of <function>~ </function> and environment variables. Also, automatic decompression of compressed files is provided.
Searches the argument filename for certain patterns, and expands them appropriately, returning the resulting expanded filename. Any sequence of characters starting with a ~ up to but not including a slash, /, will be changed to the specified user's home directory. Any occurrence of a dollar sign, $, will result in the following text, up to the next slash, being expanded to the environment variable specified by the text. This expansion can be avoided by placing a backslash, \ before any ~ or $. In all the following functions which take filenames, automatic expansion of filenames is performed.
Returns TRUE if the file exists.
Checks to see if the file exists, and if so, prompts the user as to whether it may be overwritten. Returns TRUE if either the file does not exist, or if it does and the user answers `y' to the prompt. The second form of the file appends a default suffix to the filename, if it does not yet have a suffix.
Removes the specified file, which results in the file being removed only after all references to it are closed.
Returns TRUE if the file ends in the given extension, preceded by a period. e.g., <function>filename_extension_matches( "volume.mnc" , "mnc" )</function> returns TRUE. Correctly handles compressed files, e.g., passing the arguments, "volume.mnc.Z" and "mnc" returns TRUE.
Strips the directories from the filename, returning the result.
Extracts the directory from the filename, returning the result.
Given a filename and a current directory, returns an absolute filename (one starting with a slash, /.
The function open_file opens the specified file, where io_type must be one of WRITE_FILE or READ_FILE, and file_format must be one of ASCII_FORMAT or BINARY_FORMAT. If successful, the file pointer is passed back in the last argument and a status of OK is returned. Otherwise, a null pointer is passed back, and a status of ERROR is returned. Filename expansion is automatically performed. The second function performs the same task as open_file with the addition that it automatically adds the specified suffix extension, if needed. On input, if the specified file does not exist and the file does not have an extension, then it looks for the specified file with the default extension.
Closes the file, returning OK if successful.
Seeks to the specified position in the file, where 0 corresponds to the beginning of the file, returning OK if successful.
Flushes the file buffer of any pending output, returning OK if successful.
Inputs a character from the file and passes it back as the second argument. If the character is the end of file character, returns a status of ERROR, otherwise, OK.
Places the character back on the input queue.
Inputs the next non-white character, i.e. the next character that is neither a blank, a tab, or a newline character.
Inputs characters from the file until the specified search character is found.
Inputs a string from the file. The file is read into the string until either the termination character or a newline character is found. If the string ends at a newline character and the termination character is not a newline, the next character available from the file will be the newline character. Otherwise, the next character available is the one just after the termination character found.
Inputs a string from a file, delimited by quotation marks, returning OK or ERROR. After successful reading of a string, the next character available from the file is the one just after the ending quotation mark.
Inputs a string from a file, delimited by quotation marks or white space, returning OK or ERROR. After successful reading of a string, the next character available from the file is the one just after the ending quotation mark or last non-white space character.
Inputs characters from the file into the argument line, up until the next newline character. If a newline was found, the next available character will be the one after the newline. The newline character is not stored in the string.
Inputs the next non-white character. If it is a ``f or ``F, then the value FALSE is passed back. If it is a ``t or ``T, then the value TRUE is passed back. Otherwise ERROR is returned.
Inputs a value of the specified type from an ascii file.
Inputs and discards characters from the file up to and including the next newline character. Returns OK if a newline was found, or ERROR if the end of file was reached first.
Inputs an array of data from a file, in binary format. The array contains n elements, each of size element_size. Returns either OK or ERROR.
Outputs a character to the file, returning OK or ERROR.
Outputs the specified string to the file, returning OK or ERROR.
Outputs a quotation mark, the specified string, and a closing quotation mark.
Outputs a newline character, returning OK or ERROR.
If the argument is TRUE, then a space and the letter ``T is output, otherwise, a space and the letter ``F is output.
Outputs a space and then the specified value to an ascii file.
Outputs an array of data to a file, in binary format. The array contains n elements, each of size element_size. Returns either OK or ERROR.
Inputs or outputs the specified binary data, depending on whether the argument io_flag is READ_FILE or WRITE_FILE.
Inputs or outputs an ascii newline character, depending on whether the argument io_flag is READ_FILE or WRITE_FILE. If the format argument is BINARY_FORMAT, this function does nothing.
If the format argument is ASCII_FORMAT, inputs or outputs a quotation mark delimited string, depending on whether the argument io_flag is READ_FILE or WRITE_FILE. IF the format argument is BINARY_FORMAT, inputs or outputs a string preceded by an integer indicating the length of the string.
Inputs or outputs a binary or ascii value of the specified type.
Inputs or outputs an array of integers in binary or ascii format.
Inputs or outputs an array of unsigned characters in binary or ascii format.

## Memory Allocation

A set of macros is provided to allow easy allocation and deallocation of memory, with up to 5 dimensional arrays. Memory allocation checking is also performed, to catch errors such as freeing memory that was not allocated. Also, the memory allocation automatically tracks all memory allocated, so that detection of memory leaks (orphaned memory) is possible.

### Basic Memory Allocation

The basic macros are as follows:
Allocates n_items elements of the correct type, assigning the result to the argument ptr.
Frees the memory pointed to by the argument ptr.
Changes the size of the memory pointed to by ptr to be of size n_items elements, possibly changing the value of ptr in the process.
Allocates a variable sized structure, which must be of a specific form. The last element of the structure must be an array of size 1, and this array will constitute the variable-sized part of the structure. The argument element_type must be the type of this last element, and the argument n_elements is the desired number of elements to allocate for this array, in addition to the memory for the first part of the structure. An example of usage is:
Allocates a 2 to 5 dimensional array of size n1 by n2, etc. and stores the result in the specified pointer, ptr. In the 2 dimensional case, this is accomplished with only 2 memory allocations, one to allocate n1 times n2 elements for the storage, and the second to allocate n1 pointers into the first memory area. In general, there is one memory allocation for each dimension required.
Frees the memory associated with the multi-dimensional array.
Enables or disables allocation checking. It is usually called at the beginning of a program to provide double-checking of allocation. It incurs an extra allocation every time one of the previous allocation macros is invoked. If this function is not called, allocation checking can be turned on by setting the environment variable DEBUG_ALLOC to anything.
Allocation checking detects three types of memory programming errors: freeing a pointer that was not allocated, by checking that memory chunks returned from the system malloc routine do not overlap existing ones, sometimes detects when the system memory has become corrupted, and by recording all memory allocated and printing out what is currently allocated, can detect memory leaks.
If memory checking was enabled, this function can be used to detect memory leaks. At the end of a program, the programmer should free up all memory that is known, then call this function. Any remaining allocated memory is printed out to a file, indicating the file and line number where the memory was allocated.

### Higher Level Array Allocation

In addition to the basic memory allocation macros described previously, a set of useful macros for dealing with arrays of dynamically changing size are provided:
This macro increases or decreases the size of an array, by specifying the number of elements previously allocated to the array (starts out at zero). The chunk_size argument defines the size of the memory chunks which are allocated. For instance, if chunk_size is 100, then this macro will only reallocate the array if the size change crosses to a different multiple of 100, thus avoiding memory allocation every time it is called. This specification of the granularity of the memory allocation must be consistently specified; if this macro is called with a given variable and chunk size, then subsequent calls to this macro with the same variable must specify the same chunk size. Note also that the number passed in as new_n_elems must be passed in as previous_n_elems on the next call to this macro.
Adds the argument elem_to_add to the array at the n_elems'th index, then increments n_elems. The argument chunk_size specifies the granularity of memory allocation.
Deletes the index_to_remove'th element from the array, decreasing the number of elements in the array (n_elems) and decreasing the memory allocation, if crossing a multiple of chunk_size. Again, chunk_size must be specified the same for all invocations of the previous three macros involving a given pointer.
Adds an element (elem_to_add) to the array, incrementing n_elems. If necessary, the memory is increased by the amount specified in chunk_size and the n_alloced variable is incremented by this amount. The usage of this differs from the use of ADD_ELEMENT_TO_ARRAY in that the number of elements (n_elems) can be decreased arbitrarily, without causing memory to be deallocated.

## Progress Reports

In order to provide simple monitoring of the progress of a particular processing task, a progress reporting module is available. While a task is in progress, the progress report prints dots across the line indicating how close to finished the task is. If the task is going to take very long, (greater than 2 minutes), the progress report periodically prints the current percentage done and the estimated time remaining. An example of usage, followed by function descriptions, is presented:
Initializes a progress report struct, specifying the number of steps that will occur in the processing, and the title to print out for the progress report. During progress report, the display will automatically switch from the short-job mode of printing a single row of dots across a line to the long-job mode of periodically printing the percentage done and estimated time remaining. If the one_line_only flag is TRUE, this is disabled and only a single row of dots will be displayed.
Tells the progress reporting module how many steps have been done, and causes update of the display of dots on the line or estimated time remaining.
Terminates the progress report.

## Text Output

Rather than using the standard UNIX function printf for routine text output, a module which is similar in appearance is provided, which allows for installing arbitrary printing output functions. This can be used, for instance, to mirror output to a log file for instance, or to send text to an X window. The primary change is to use a function called print or print_error with exactly the same arguments as for the standard printf function.
Takes the same arguments as printf, but allows installing of a user output function for the final stage of output.
Sets the output function, where all text from calls to print or print_error will be sent. By default, there is no print function, and output is sent to printf.
Temporarily sets the print or error printing function to go to standard out, and allows the user to set another print function, which will disappear when the corresponding pop function is called.
Restores the previous user print or error printing function.

## Time

Some basic utilities relating to time, date, and CPU time are provided.
Returns the number of CPU seconds used by the current process so far.
Returns the number of seconds that the current process has been running.
Returns a string representing the current clock time (hours and minutes), for which the caller is responsible for deleting.
Returns a string representing the current date, for which the caller is responsible for deleting.
Takes a time in seconds and a format string which has format components, e.g. ``%g %s, and prints the time into a string in appropriate units of milliseconds, seconds, minutes, days, etc. The string is returned and the calling program is responsible for deleting the string.
Same as format_time, but calls print with the result.
Suspends the program for the specified number of seconds. Note that on most systems, this will only be performed to the nearest multiple of some particular time increment. On Silicon Graphics Systems this will be to the nearest hundredth of a second.

# Volumes

Processing tasks within the lab where this software was developed deal with multi-dimensional volumes of data such as created by Magnetic Resonance and PET scanners. Therefore, an extensive set of routines is provided to represent volumes, and to read and write volumes in the MINC format.
The basic type of a volume is Volume, which is actually a pointer to an allocated structure which contains all the information about the type of volume, number of dimensions, voxel values, etc. In order to use a volume structure, the volume must first be created, then the size of the volume set, then the large array of voxel values is allocated. Alternately, a volume may be automatically created by calling the appropriate function to read a MINC file and create a volume.
A volume has an associated number of dimensions, which must be in the range from one to five, but typically is three. The volume is thought of as a multi-dimensional array of any of a variety of types, including all sizes of integer and real types. Even though a volume may be stored in say, a 1 byte type, with values from zero to 255, there is a real value range which provides mapping a mapping from voxel values to any arbitrary real range. In this way, the real range may be any valid real interval and is independent of the particular storage type.
Since most volumes will be created by reading from a MINC file, this method will be presented first, followed by a description of how to create a volume from scratch. Finally a lower level of MINC input and output for more advanced applications will be presented.

## Volume Input

This routine reads a volume from a MINC file, first creating the volume if the create_volume_flag is specified as TRUE (the usual case). The number of dimensions is the desired number of dimensions for the volume. If this is less than the number of dimensions in the file, then only the first part of the file, corresponding to this number of dimensions, is read. If the number of dimensions specified is less than 1, then the number of dimensions in the file is used. The argument dim_names specifies in what order the volume is to be stored in the volume variable. For each dimension in the stored volume, there is a corresponding name, which is one of MIxspace, MIyspace, MIzspace, ANY_SPATIAL_DIMENSION, or an empty string. These are matched up with corresponding dimensions in the file and the dimension ordering of the volume array is reordered on input. So, if the user wishes to represent the volume in X-Z-Y order, then the value passed as the dim_names array should be the three strings, MIxspace, MIzspace, and MIyspace. This choice of ordering is important, as it defines the order of any subsequent reference to voxel indices.
The four arguments, volume_nc_data_type through volume_voxel_max can be used to specified the desired storage type within the volume variable, automatically converted from the storage type in the file. The volume_nc_data_type is one of MI_ORIGINAL_TYPE, NC_BYTE, NC_SHORT, NC_LONG, NC_FLOAT, or NC_DOUBLE. For the integer types, the volume_signed_flag is TRUE if a signed type is desired, otherwise, it is FALSE. The volume_voxel_min and volume_voxel_max specify the range of valid voxel values, and are usually set equal to indicate to use the full range of the type, e.g. zero to 255.0 for unsigned NC_BYTE. If MI_ORIGINAL_TYPE is passed, then the type, sign, and voxel range in the file are used.
If the create_volume_flag is TRUE, the usual case, then the volume is automatically created. Otherwise, it is assumed that the volume already exists and will only be recreated if its current size is different from the new size resulting from the file, thus reducing the amount of memory allocation when reading multiple files.
The options argument specifies some special options to the input process, and is usually passed just a NULL pointer, indicating that the default options should be used. Currently the possible options are:
Fills in the default options into an options structure which will subsequently be passed to input_volume.
By default, any voxel value which is outside the valid range of voxel values is promoted to the minimum valid voxel value. If this flag is set to FALSE, this promotion is disabled.
By default, any volume which contains a dimension which is of type vector, for instance, an RGB colour volume, is converted to a scalar volume by averaging the components of each vector. This can be disabled by passing the flag as FALSE.
By default, any volume which contains a dimension which is of type vector, for instance, an RGB colour volume, is converted to a scalar volume by averaging the components of each vector. If this option is set to true, any vector volume with the number of components specified by set_minc_input_colour_dimension_size (default 3) is converted to a colour volume, using the indices specified by set_minc_input_colour_indices (default 0, 1, 2).
By default, files read into an integer-type volume will be scaled so that the real range is the full range of the data in the input file. The user can define another range by calling this function with an appropriate real minimum and maximum. For float-type Volumes, setting the range will change the real range of the volume, but not change the actual values in the volume. If minimum is greater than or equal to maximum, then the default behaviour is restored.

## Alternative Volume Input Methods

Rather than using the input_volume function to input a volume in one shot, there is another method which will allow interspersing volume input with other processing tasks. The following is the set of routines which the input_volume function calls in order to input a volume.
This initializes a filename for inputting the volume incrementally. The arguments to this are identical to those for the input_volume function, with the addition of the input_info structure which is initialized for use in the remaining functions. The volume is created if necessary, but not allocated, and no part of the volume is read in.
Closes the file and terminates the input of the volume. By calling the start_volume_input function followed by the delete_volume_input function, a volume can be created that has all the information about the file volume, without the voxels being allocated.
Closes the file, terminates the input of the volume, and deletes the volume. Simply calls delete_volume_input followed by delete_volume.
Inputs a small chunk of the volume, calculated to be efficient, yet not be so large that one cannot usably intersperse calls to do other processing with calls to this function. Returns TRUE if there is more to do, i.e., this function should be called again. The fraction done is passed back as a number between 0 and 1.
The Minc_file structure from the volume input is passed back, to allow use of this in routines that require this type.

## Volume Output

Volume output is accomplished by one of two routines, depending on whether or not the volume is treated as a modified version of another volume or is an independent volume on its own.
Outputs the specified volume to the specified filename. If the argument file_nc_data_type is MI_ORIGINAL_TYPE then the volume is stored in the MINC file in the same type as in the volume variable. Otherwise, the four arguments, file_nc_data_type, file_signed_flag, file_voxel_min, and file_voxel_max, specify the type and valid voxel range to store the volume in the file. If the history argument is non-null, then it represents a description of the volume, and will be placed in the MINC volume. If the options argument is NULL, then the default options will be used. Otherwise, some specific output options can be set through this parameter, and the following functions:
Sets the options structure to the default values. The user can then override the default values and pass the structure to the output_volume function. Currently, there is only one output option:
Deletes the options data.
Defines the output order of the file. Each dimension name string must have a matching dimension name in the volume, which defines the order of the dimensions in the output file. For instance, one may have input the volume in <function>(x, y, z)</function> order. To get it output in (z, y, x) order, set dimension_names to the three strings MIzspace, MIyspace, and MIxspace.
Defines the image range that will appear in the file. By default, none is specified, and output_volume uses the real minimum and maximum of the volume. To set the real range of the volume, see the relevant documentation for set_volume_real_range.
If the volume is a modification of another volume currently stored in a file, then it is more appropriate to use the following function to output the volume:
The only difference between this function and the other method of volume output (output_volume), is that this function copies auxiliary data from the original file (original_filename) to the new file. This auxiliary data includes such items as patient name and other scanning data, and is not read into a volume, so the only way to correctly pass it along to a new MINC file is to use this function.

## Volume Manipulation

Once a volume has been created and allocated, there are many functions for manipulating the volume. Note that associated with each volume is a valid voxel range indicating the range of values actually stored in the volume, for instance, zero to 200 is one possible range for an unsigned byte volume. There is a second range, the real range, which is the mapping of the valid voxel range to an arbitrary real range, for instance, the zero to 200 of valid voxels could map to -1.5 to 1.5 in the real range. When dealing with volumes, one is generally interested in the real range.
Given a volume and from one to five voxel indices, depending on the volume dimensions, returns the corresponding voxel value. For instance, if the volume is three dimensional, then the final two arguments are ignored.
Given a volume and a voxel value, converts this to a value in the real range, and returns it.
Given a volume and a real value, converts this to a voxel value in the valid voxel range, and returns it.
Given a volume and from one to five voxel indices, depending on the volume dimensions, returns the corresponding real value. For instance, if the volume is three dimensional, then the final two arguments are ignored.
Given a volume, one to five voxel indices, and a voxel value, assigns this value to the corresponding voxel in the volume. Note that no conversion from the valid real range to the valid voxel range is performed, so the user may need to use the convert_value_to_voxel function first or use the set_volume_real_value function.
Given a volume, one to five voxel indices, and a value, assigns this value to the corresponding voxel in the volume, after converting to a voxel value.
Deletes all the memory associated with an volume.
Returns the storage type of the volume (for instance, NC_SHORT), and passes back an indication of whether it is signed or unsigned.
Returns the number of dimensions of the volume.
Stores the size of each dimension in the array sizes. This is the number of voxels in each dimension. The array sizes must be at least as large as the number of dimensions of the volume.
Stores the slice separation for each dimension in the array separations. The array separations must be at least as large as the number of dimensions of the volume.
The first two functions return the minimum or maximum allowable voxel value. The third function passes back both values.
The first two functions return the minimum or maximum real value. The third function passes back both values. The mapping to this real space linearly maps the minimum voxel value to the minimum real value and the maximum voxel value to the maximum real value.
Returns a pointer to the list of names of each dimension. This memory must be freed by the calling function using the following function.
Frees the memory allocated for the dimension names.
Sets the volume flag indicating whether the volume is of type red-green-blue colour. Only volumes whose data type is unsigned long may be rgb volumes.
Returns TRUE if the volume is a red-green-blue colour volume, as can be created on volume input.

### Volume Coordinate Systems

A volume has two coordinate systems. The voxel coordinate system is simply the n-dimensional indexing coordinate system for a volume. A voxel coordinate of (0.0, 0.0, 0.0), for instance, corresponds to the centre of the first voxel in a three dimensional volume. A voxel coordinate of ( 99.0, 0.0, 0.0 ) corresponds to the centre of the last voxel in the first direction of a three dimensional volume of size ( 100, 200, 150 ). The second coordinate system is an arbitrary three dimensional coordinate system, usually referred to as the world coordinate system, often the Talairach coordinate system. The following functions provide conversion to and from these two coordinate systems:
Given a volume and a real valued voxel index, passes back the corresponding world coordinate.
Same as convert_voxel_to_world except it applies only to three dimensional volumes.
Converts a world coordinate into a voxel. In order to use these voxel coordinates as integer indices, for instance, as arguments to the GET_VALUE macros, each component of the argument voxel must first be rounded to the nearest integer.
Same as convert_world_to_voxel except it applies only to three dimensional volumes.
These two functions convert vectors between the voxel and world spaces. This is done by transforming the point (0,0,0) to the other space, in addition to treating the vector as a point and transforming it to the other space, and passing back the vector between the two results.
Converts a vector which is assumed to be a surface normal to the world coordinate system.

### Volume Interpolation

In addition to the routines for accessing particular voxel values, the volume can be interpolated using nearest neighbour, linear, or cubic interpolation, specified in either voxel or world space.
Interpolates a volume at the specified voxel, where degrees_continuity has values -1, 0, or 2, for nearest neighbour, linear, or cubic interpolation. If use_linear_at_edge is true, then any cubic interpolation will be downgraded to linear interpolation near the edge of the volume. This option is only needed in special cases. The parameter outside_value is the value for any point outside the volume. The argument interpolating_dimensions indicates which dimensions are being interpolated, typically a NULL pointer indicating that all dimensions are being interpolated. The interpolated values are placed in the values array. The number of values is equal to the number of values in the non-interpolated dimensions, which is one, if all dimensions are being interpolated. If the derivative arguments are non-null, then the resulting derivatives are placed in the appropriate places. The first_deriv is a two dimensional array of size number of values by number of interpolating dimensions. The second_deriv is a three dimensional array of size number of values by number of interpolating dimensions by number of interpolating dimensions.
Interpolates a volume at the specified world position, where degrees_continuity has values -1, 0, or 2, for nearest neighbour, linear, or cubic interpolation. If use_linear_at_edge is true, then any cubic interpolation will be downgraded to linear interpolation near the edge of the volume. This option is only needed in special cases. The parameter outside_value is the value for any point outside the volume. The interpolated values are placed in the values array. The number of values is equal to the number of values in the non-spatial dimensions, which is one if all dimensions are spatial. If the derivative arguments are non-null, then the resulting derivatives are placed in the appropriate places.
For speed considerations, if a volume is evaluated at or near a voxel centre, then no interpolation is performed, and the voxel value is returned. The tolerance defines how close to the voxel centre this occurs, and defaults to 0. Note that if derivatives are desired and the degree of continuity specified is 0 or greater, then interpolation will be performed, even when within the specified tolerance of the voxel.

## Volume Creation from Scratch

In some circumstances, it is desirable to create volumes in ways other than reading from a file. The following functions provide methods to create a volume from scratch or to create a volume which is similar to an existing volume.
Creates and returns a volume of the given type (for instance, NC_BYTE, signed_flag equal FALSE), and given valid voxel range. The dimension_names is used to describe each dimension of the volume and is currently only used when writing the volume to a file.
After creation of a volume, the valid voxel range or valid real range can subsequently be changed by using these functions.
Sets the sizes of the volume, the number of voxels in each dimension. Note that this must be done before calling the function alloc_volume_data to allocate the voxels.
After the volume has been created, and its size has been set, then this function allocates the memory for the voxels. Note that the voxel values are not initialized, and the user must fill the volume with desired values.
Associated with each volume is a transformation from voxel space to world space. There are several ways to define this transformation. The simplest is just to specify it directly:
Assigns the given transformation to the volume.
Returns a pointer to the voxel to world transform of the volume.
Sets the inter-voxel separations in each of the volume dimensions. Note that this will cause the voxel-to-world transformation to be updated accordingly.
Sets the translation portion of the voxel-to-world transformation. A voxel coordinate is specified (voxel), as well as a real world position to which it is desired that this voxel map (world_space_voxel_maps_to). The voxel-to-world transformation is updated to provide this mapping.
Sets the real world axis for a specific voxel dimension. For instance if dimension is 1, and dir is (0.0, 1.0, 1.0), then voxels along the second dimension of the volume will map to the real world axis (0.0, 1.0, 1.0) normalized to unit length, then scaled by the volume separation for the second volume dimension.
Passes back the real world axis for a specific voxel dimension. Note that dimension must be a spatial dimension in the volume.
Sets the coordinate system type of the volume in terms of a string name. This can be any string, but a set of defined constants is given by MINC: MI_NATIVE, MI_TALAIRACH, and MI_CALLOSAL.
Returns a copy of the coordinate system type of the volume in terms of a string name. The calling function must free this string when finished with it.

### Copying Volumes

Another method of creating a volume is to simply copy the entire volume definition from an existing volume:
Both functions creates and returns a new volume which has the same definition (sizes, voxel-to-world space transform, etc.), as an existing volume. If the argument nc_data_type is not MI_ORIGINAL_TYPE, then the storage type of the new volume differs from the original and is specified by nc_data_type, signed_flag, voxel_min, and voxel_max. In the first function, the voxel values are allocated but not initialized. In the second function, they are not allocated.
Creates an exact copy of a volume.

### Volume Caching

In order to efficiently handle volumes that are too large for virtual memory, a simple caching scheme has been implemented. Disk files are read and written on demand to provide the appearance of having large volumes fully resident in memory. All the volume routines described previously support this feature transparently, but there are a few routines for configuration of the volume caching.
Sets the threshold number of bytes for volume caching. If a volume larger than this is created, either explicitly or by input from a file, then that volume will be internally represented by the caching scheme. If this function is not called, the default is either 80 megabytes, or the value of the environment variable VOLUME_CACHE_THRESHOLD, if present. A threshold of zero will cause all volumes to be cached. A threshold less than zero will result in no volumes being cached.
When a voxel is accessed in a cached volume, the corresponding block containing the voxel is read from or written to the disk as necessary. These two functions control the default size of the blocks and therefore the tradeoff between fast access of adjacent voxels (using large block sizes) and fast access of arbitrarily distributed voxels (using small block sizes). The function set_cache_block_sizes_hint indicates that the default block sizes for volumes created in the future should be computed based on the assumption that the application will be accessing volume voxels mostly in a slice-by-slice manner (SLICE_ACCESS) or in an unpredictable order (RANDOM_VOLUME_ACCESS). The second function, set_default_cache_block_sizes, provides an alternative where the default block sizes are explicitly set. A call to either function overrides any previous call to the other function. If neither of these functions is called, the default value is a block size of 8 voxels per dimension, or the value specified by the environment variable VOLUME_CACHE_BLOCK_SIZE. These functions only affect volumes created afterwards. In order to change the value for a given volume, the following function may be used:
Sets the size of the cache block for the given volume. Because this function causes the cache to be completely flushed, in order to change the block size, it may incur a temporary speed penalty during subsequent voxel accesses which cause the cache to read blocks from a file
Sets the default maximum number of bytes allowed in the cache for each volume. A higher number may provide faster access, due to a greater chance of finding a particular voxel within the cache, so this value should be as large as reasonable, given the amount of virtual memory available. If this function is not called, the default value is 80 megabytes, or the value of the environment variable VOLUME_CACHE_SIZE, if present. Setting a value of 0 will result in cached volumes having exactly one block in their cache. This function only affects volumes created subsequently. In order to change this value for a specific volume, the following function may be used:
Sets the maximum number of bytes allowed in the cache for a particular volume. Calling this function flushes the cache, in order to reallocate the data structures to account for the new size. As in calling the function set_volume_cache_block_sizes, there may be a temporary speed loss in accessing pixels.
When a cached volume is modified, a temporary file is created to contain the voxel values. When the volume is deleted this file is also deleted. When a cached volume is output, the temporary file is copied to the output file, which results in two copies of the volume existing at once, which may incur unacceptable demands on disk storage, especially for large volumes. To avoid this, an application can specify a filename where to place the voxel values, which overrides the use of a temporary file. When the volume is deleted, this file is closed and remains on the disk, obviating the need for the application to output the volume.
If the application later calls output_volume to output this volume to a file of the same name as that set by this function, the request to output will be ignored, because the volume already exists in this file. Basically, the programmer should call set_cache_output_volume_parameters with the same parameters that will be passed to the eventual call to output_volume. If the volume is a cached volume, then the output file will be created as soon as a volume voxel is set, and the later call to output_volume will not recreate the file, but simply flush the cache buffer to make the file fully up to date. If the volume is not a cached volume, the call to set_cache_output_volume_parameters will be ignored, and the later call to output_volume will create the file as specified. Note that when calling this function, it is important to delete the volume before exiting the program, so that cached volumes will flush their buffer and close the file, Otherwise, the most recent changes to the volume will not be written to the file.

## Volume Source Code Example

An examples of reading, writing, and manipulating volumes is presented here.
a complete program to read a MINC volume, change all values over 100.0 to 100.0, then write out the result in a new file.

## Multiple Volume Input

The input_volume function described previously is intended to be a simple single-function interface for reading volumes for most applications. Occasionally, however, an application may not want to read the whole volume at once, or may need to break the input file into several volumes, for instance, the slices of a 3D volume. This can be facilitated by the following functions:
This functions opens a MINC file for input to the specified volume. The number of dimensions in the file must be greater than or equal to the number of dimensions in the volume, and each dimension name in the volume must have a matching dimension name in the file. A file handle is returned, for use in the following routines.
This performs the same task as initialize_minc_input, except it takes a previously opened MINC file as an argument, rather than a filename.
Returns the MINC id of the file, a handle which can be used to perform MINC functions on the file.
Determines the total number of volumes in the opened MINC file. For instance, if the file contains x, y, z data of size 100 by 200 by 300 and the file was opened with a two dimensional x-z volume, then the number of volumes returned will be 200, the number of y slices.
Reads part of the MINC file to the volume specified in initialize_minc_input. This function must be called until it returns FALSE, at which point the volume has been fully read in. In order to read the next volume in the file, if any, the following function must be called.
Advances the input file to the next volume.
Resets the input to the beginning of the first volume in the file.
Closes the MINC file.

## Multiple Volume Output

Similarly to the multiple volume input, there are routines for providing output to a file by subvolumes.
Opens a MINC file for creation. The arguments specify the number of dimensions and the names of each dimension, as well as the sizes in each dimensions. The type of the file is controlled by the four parameters, file_nc_data_type, file_signed_flag, file_voxel_min, and file_voxel_max. The transformation to world coordinates is specified by the voxel_to_world_transform argument. The volume is attached to file for output. The volume must have no more dimensions than the file and the names of each dimension must match with one of the dimensions in the file. The options are as specified for the output_volume function. Note that, unlike the output_volume function, if the image minimum and maximum are not specified, then the output file will have a separate image minimum and maximum for each image.
Returns the MINC id of the file, a handle which can be used to perform MINC functions on the file.
Copies the auxiliary information from the MINC file specified in the filename argument to the open MINC file.
Outputs the attached volume to the MINC file in the current file position. The file position is advanced by the size of the volume.
More general routine to output the specified volume to the MINC file in the specified position. Since this routine does not update the file position, it does not interfere with normal volume output using the function output_minc_volume. The volume hyperslab defined by volume_count is written to the file starting at the given file position.
Closes the MINC file.

# Tag Points

Tag points are sets of three dimensional points which are used to mark positions of interest. Generally these result from manually locating corresponding positions in multiple volumes to provide a mapping among the volumes. Tag points are stored in an ascii format devised at the McConnell Brain Imaging Centre, with a convention of filenames ending in .tag. A tag file can contain either sets of single tag points or two tag points, depending on whether the file corresponds to one or two volumes. Each tag point consists of three coordinates, x, y, and z. Each set of one or two tag points has additional optional information stored with it. A tag point set may or may not include a set of three values: a real valued weight, an integer structure id, and an integer patient id. A tag point may or may not include a label string. Functions to read and write files of this format are described:
These two functions read a set of tag points from a file. The first form assumes the file is already open and will be closed by the calling function, while the second form opens and closes the file specified by a filename, with a default extension of .tag. The number of volumes (number of tags in a set, currently either one or two) is passed back in the n_volumes argument. The number of tag points is passed back in the n_tag_points argument. The three dimensional coordinates of the first tag point in each set is passed back in the argument tags_volume1. If the number of volumes is two, then the second tag point in each set is passed back in the argument tags_volume2. The final four arguments pass back the auxiliary information associated with each tag point set. If the calling program is not interested in any one of the four data, then it can pass in a NULL pointer and the values in the file will not be passed back.
When finished with a list of tag points, the associated memory may be freed by calling this function.
These two routines provide a more memory efficient method to input tag points. After opening a file, the first routine is called to initialize the input of tags. The next routine is repeatedly called until it returns FALSE, reading one tag at a time.
These two functions write a list of tag points to a tag point file. The first form assumes that the file has already been opened and will be closed later by the calling function, while the second form opens and closes the file, with the default extension of .tag. The comments argument is any arbitrary string documenting the contents of this file. The number of volumes (n_volumes) must be either one or two. The number of tag points is specified by the argument n_tag_points. The positions of the sets of one or two tag points are in the arguments tags_volume1 and tags_volume2. If any of the three arguments, weights, structure_ids, or patient_ids, are specified as NULL, then none of these three pieces or information is written to the file. Similarly, if the labels argument is NULL, then no labels are written to the file.
Returns a pointer to a string consisting of the default suffix for tag point files, currently ``tag. This pointer should not be freed or its contents modified.
These two routines provide a more memory efficient method to output tag points. After opening a file, the first routine is called to initialize the output of tags. The next routine is repeatedly called to output a single tag point each time. The third routine is called to indicate the end of tag files.

## Tag Points Source Code Example

The following is an example which reads a tag volume, removes all tags which have negative x positions, ignores the second tag point in each set, if present, and writes the result to a new file.

# Transforms

In dealing with such tasks as inter- and intra-subject registration both within and across imaging modalities, the concept of transformations between different coordinate systems arises. A module is provided to handle both linear (affine) and non-linear transformations, and to provide input and output in a standardized Brain Imaging Centre format, usually to filenames with the extension .xfm.
To support these functions, two structure types are provided. The first (Transform) is a four by four linear (affine) transform, which facilitates rigid transformations, consisting of scaling, rotation, translation, and shearing. The second is a higher level transform (General_transform), which represents either a linear transform, a non-linear transform (thin-plate spline), a smoothly interpolated grid transform, a user definable transform, or a concatenation of two or more of these.

## Linear Transforms

The linear transform functions all deal with objects whose type is Transform.
Is used to set or retrieve the value of the i'th row and j'th column of the transform, where <function>0<=i, j<=3</function>
Creates a four by four identity transform.
Transforms a three dimensional point by the given transform, passing back the three transformed coordinates.
Transforms a three dimensional vector by the given transform, passing back the three transformed coordinates. The only difference between transforming a point and a vector is that transforming a vector does not involve the translational component of the transform.
Assuming the transform is an orthogonal transform (no shear components), the point is transformed by the inverse of the transform.
Assuming the transform is an orthogonal transform (no shear components), the vector is transformed by the inverse of the transform.
Multiplies the transform t2 by the transform t1, storing the product in result. Transforming a point by result is equivalent to transforming the point by t1, then transforming the result by t2.

## General Transforms

General transforms can represent linear transforms, thin-plate spline transforms, grid transforms, and user defined transforms, and concatenations of these. All functions dealing with general transforms involve objects of type General_transform.
Creates a general transform consisting of a single linear transform, specified by linear_transform. If a NULL is passed as the argument linear_transform, then an identity transform is created.
Creates a general transform consisting of a thin plate spline, which provides a smooth non-linear mapping of a multidimensional space. The points argument is an array of size n_points by n_dimensions, representing a set of points. The displacements is a (n_points + n_dimensions + 1) by n_dimensions array, which is created by the function get_nonlinear_warp, which is not included in this library.
Creates a general transform consisting of a grid transform, which provides a smooth non-linear mapping of a multidimensional space. The displacment_volume argument is a four dimensional volume representing the displacements for the x, y, and z directions. Three of the dimensions of the volume must correspond to the x, y, and z spatial dimensions, and the fourth must have exactly three components, the displacements in each direction in world space. The dimensions may be in any order.
Creates a user defined transformation, by copying the user data (size_user_data bytes of data starting at user_data). Two function pointers are also required, to specify the method of transforming points and inversely transforming points. These functions are of the type User_transform_function, which is specified above.
Frees up the memory stored in the general transform structure.
Returns the general transform type, one of LINEAR, THIN_PLATE_SPLINE, USER_TRANSFORM, or CONCATENATED_TRANSFORM.
If the general transform is of type LINEAR, then returns a pointer to the linear transform (of type Transform), for use with the routines specific to linear transforms, described earlier. Otherwise prints an error message.
Concatenates two general transforms. If both transforms are of type LINEAR, then the result is also of this type, being the matrix product of the two. Otherwise, the resulting transform is simply the concatenation of the two transforms.
Returns the number of concatenated transforms in the given transform. If the type of the transform is CONCATENATED_TRANSFORM, then the number returned is the number of transforms, otherwise it is one.
If the transform is of type CONCATENATED_TRANSFORM, then a pointer to the n'th transform is returned, where n is greater than or equal to zero and less than the number of transforms in the concatenated transform.

## Using General Transforms

Transforms a three dimensional point by a general transform, passing back the result in the last three arguments.
Transforms a three dimensional point by the inverse of the general transform, passing back the result in the last three arguments.
Creates a copy of the general transform, allocating memory within the structure as required.
Creates a new general transform that is the inverse of the given one.
Changes the transform to be its inverse. Calling it twice on the same transform is equivalent to not calling the function at all.

## Reading and Writing General Transforms

General transforms are stored in files in an ascii format devised at the McConnell Brain Imaging Centre, and usually have a filename extension of .xfm. The input and output functions are:
These two functions write the general transform to a file, in the appropriate format. The comments line is an arbitrary string which is stored in the file for documentation purposes. Newline characters in the comments correctly result in a multi-line comment with a comment character inserted at the beginning of each line. The first form opens the file, with a default extension of .xfm, writes the transform, then closes the file. The second form of the function assumes the file is already open and will later be closed. Because the transform may contain pointers to MINC files that define the transform, the name of the file must be passed in to this function, to be used to create the name of the MINC files. The volume_count_ptr must point to an integer that has been initialized. Each time an auxiliary MINC file of the transform is created, the integer is used to create a unique filename, and then the volume_count_ptr is incremented. Both functions return ERROR or OK.
Inputs a general transform from a file. The first form assumes the file has already been opened for input, and will later be closed. The second form opens the file for input, with a default extension of .xfm, reads the transform, then closes the file. Both functions return ERROR or OK.
Returns a pointer to a string consisting of the default suffix for transform files, currently ``xfm. This pointer should not be freed or its contents modified.

# Final Source Code Example

This is an example which attempts to illustrate a typical processing task incorporating use of volumes, tag points, and transformations. This is a full program which, when linked to the BIC Volume IO Library, reads two volumes in MINC format, a set of tag points from file, and a transformation from file. The tag points are assumed to be in the world space of the first volume, and the transformation is assumed to transform points in the world space of the first volume to the world space of the second volume. The program transforms each tag point by the transformation input (presumably transforming from the world space of the first volume to that of the second volume), then transforms the result to the voxel space of the second volume. If the voxel coordinate is within the second volume, then the value of the corresponding voxel is printed.