RNifti
Fast R and C++ Access to NIfTI Images
|
Thin wrapper around a C-style nifti_image
struct that allows C++-style destruction.
More...
#include <NiftiImage.h>
Classes | |
struct | Block |
Inner class referring to a subset of an image. More... | |
class | Extension |
Inner class wrapping a NIfTI extension, a weakly-specified standard for attaching additional metadata to NIfTI-1 and NIfTI-2 images. More... | |
class | Xform |
Inner class representing an xform matrix, which indicates the orientation and other spatial properties of an image. More... | |
Public Member Functions | |
NiftiImage () | |
Default constructor. | |
NiftiImage (const NiftiImage &source, const bool copy=true) | |
Copy constructor. More... | |
NiftiImage (const Block &source) | |
Initialise from a block, copying in the data. More... | |
NiftiImage (nifti_image *const image, const bool copy=false) | |
Initialise using an existing nifti_image pointer. More... | |
NiftiImage (const std::vector< dim_t > &dim, const int datatype) | |
Initialise from basic metadata, allocating and zeroing pixel data. More... | |
NiftiImage (const std::vector< dim_t > &dim, const std::string &datatype) | |
Initialise from basic metadata, allocating and zeroing pixel data. More... | |
NiftiImage (const std::string &path, const bool readData=true) | |
Initialise using a path string. More... | |
NiftiImage (const std::string &path, const std::vector< dim_t > &volumes) | |
Initialise using a path string and sequence of required volumes. More... | |
NiftiImage (const SEXP object, const bool readData=true, const bool readOnly=false) | |
Initialise from an R object, retrieving an existing image from an external pointer attribute if available; otherwise constructing a new one from the R object itself. More... | |
virtual | ~NiftiImage () |
Destructor which decrements the reference counter, and releases the wrapped pointer if the counter drops to zero. | |
operator const nifti_image * () const | |
Allows a NiftiImage object to be treated as a pointer to a const nifti_image . | |
operator nifti_image * () | |
Allows a NiftiImage object to be treated as a pointer to a nifti_image . | |
const nifti_image * | operator-> () const |
Allows a NiftiImage object to be treated as a pointer to a const nifti_image . | |
nifti_image * | operator-> () |
Allows a NiftiImage object to be treated as a pointer to a nifti_image . | |
NiftiImage & | operator= (const NiftiImage &source) |
Copy assignment operator, which copies from its argument. More... | |
NiftiImage & | operator= (const Block &source) |
Copy assignment operator, which allows a block to be used to replace the contents of a suitably sized image. More... | |
NiftiImage & | setPersistence (const bool persistent) |
Mark the image as persistent, so that it can be passed back to R. More... | |
bool | isNull () const |
Determine whether or not the wrapped pointer is NULL . More... | |
bool | isShared () const |
Determine whether the wrapped pointer is shared with another NiftiImage . More... | |
bool | isPersistent () const |
Determine whether or not the image is marked as persistent. More... | |
bool | isDataScaled () const |
Determine whether nontrivial scale and slope parameters are set. More... | |
int | nDims () const |
Return the number of dimensions in the image. More... | |
std::vector< dim_t > | dim () const |
Return the dimensions of the image. More... | |
std::vector< pixdim_t > | pixdim () const |
Return the dimensions of the pixels or voxels in the image. More... | |
NiftiImage & | drop () |
Drop unitary dimensions. More... | |
const NiftiImageData | data () const |
Obtain the pixel data within the image. More... | |
NiftiImageData | data () |
Obtain the pixel data within the image. More... | |
template<typename TargetType > | |
std::vector< TargetType > | getData (const bool useSlope=true) const |
Extract a vector of data from the image, casting it to any required element type. More... | |
NiftiImage & | changeDatatype (const int datatype, const bool useSlope=false) |
Change the datatype of the image, casting the pixel data if present. More... | |
NiftiImage & | changeDatatype (const std::string &datatype, const bool useSlope=false) |
Change the datatype of the image, casting the pixel data if present. More... | |
template<typename SourceType > | |
NiftiImage & | replaceData (const std::vector< SourceType > &data, const int datatype=DT_NONE) |
Replace the pixel data in the image with the contents of a vector. More... | |
NiftiImage & | replaceData (const NiftiImageData &data) |
Replace the pixel data in the image with the contents of a NiftiImageData object. More... | |
NiftiImage & | dropData () |
Drop the data from the image, retaining only the metadata. More... | |
NiftiImage & | rescale (const std::vector< pixdim_t > &scales) |
Rescale the image, changing its image dimensions and pixel dimensions. More... | |
NiftiImage & | reorient (const int i, const int j, const int k) |
Reorient the image by permuting dimensions and potentially reversing some. More... | |
NiftiImage & | reorient (const std::string &orientation) |
Reorient the image by permuting dimensions and potentially reversing some. More... | |
NiftiImage & | update (const Rcpp::RObject &object) |
Update the image from an R array. More... | |
const Xform | xform (const bool preferQuaternion=true) const |
Obtain an xform matrix, indicating the orientation of the image. More... | |
const Xform | qform () const |
Access the qform matrix. More... | |
Xform | qform () |
Access the qform matrix. More... | |
const Xform | sform () const |
Access the sform matrix. More... | |
Xform | sform () |
Access the sform matrix. More... | |
dim_t | nBlocks () const |
Return the number of blocks in the image. More... | |
const Block | block (const int i) const |
Extract a block from the image. More... | |
Block | block (const int i) |
Extract a block from the image. More... | |
const Block | slice (const int i) const |
Extract a slice block from a 3D image. More... | |
Block | slice (const int i) |
Extract a slice block from a 3D image. More... | |
const Block | volume (const int i) const |
Extract a volume block from a 4D image. More... | |
Block | volume (const int i) |
Extract a volume block from a 4D image. More... | |
int | nChannels () const |
Return the number of colour channels used by the image. More... | |
size_t | nVoxels () const |
Return the number of voxels in the image. More... | |
int | nExtensions () const |
Return the number of extensions associated with the image. More... | |
std::list< Extension > | extensions (const int code=-1) const |
Return a list of the extensions associated with the image. More... | |
NiftiImage & | addExtension (const Extension &extension) |
Add an extension to the image. More... | |
NiftiImage & | replaceExtensions (const std::list< Extension > extensions) |
Replace all extensions with new ones. More... | |
NiftiImage & | dropExtensions () |
Remove any extensions from the image. More... | |
std::pair< std::string, std::string > | toFile (const std::string fileName, const int datatype=DT_NONE, const int filetype=-1) const |
Write the image to a NIfTI-1 file. More... | |
std::pair< std::string, std::string > | toFile (const std::string fileName, const std::string &datatype, const int filetype=-1) const |
Write the image to a NIfTI-1 file. More... | |
Rcpp::RObject | toArray () const |
Create an R array from the image. More... | |
Rcpp::RObject | toPointer (const std::string label) const |
Create an internal image to pass back to R. More... | |
Rcpp::RObject | toArrayOrPointer (const bool internal, const std::string label) const |
A conditional method that calls either toArray or toPointer. More... | |
Static Public Member Functions | |
static int | sexpTypeToNiftiType (const int sexpType) |
Convert between R SEXP object type and nifti_image datatype codes. More... | |
static int | fileVersion (const std::string &path) |
Get the NIfTI format version used by the file at the specified path. More... | |
Protected Member Functions | |
void | acquire (nifti_image *const image) |
Acquire the specified pointer to a nifti_image struct , taking (possibly shared) responsibility for freeing the associated memory. More... | |
void | acquire (const NiftiImage &source) |
Acquire the same pointer as another NiftiImage , incrementing the shared reference count. More... | |
void | release () |
Release the currently wrapped pointer, if it is not NULL , decrementing the reference count and releasing memory if there are no remaining references to the pointer. | |
void | copy (const nifti_image *source) |
Copy the contents of a nifti_image to create a new image, acquiring the new pointer. More... | |
void | copy (const NiftiImage &source) |
Copy the contents of another NiftiImage to create a new image, acquiring a new pointer. More... | |
void | copy (const Block &source) |
Copy the contents of a Block to create a new image, acquiring a new pointer. More... | |
void | initFromNiftiS4 (const Rcpp::RObject &object, const bool copyData=true) |
Initialise the object from an S4 object of class "nifti" . More... | |
void | initFromMriImage (const Rcpp::RObject &object, const bool copyData=true) |
Initialise the object from a reference object of class "MriImage" . More... | |
void | initFromList (const Rcpp::RObject &object) |
Initialise the object from an R list with named elements, which can only contain metadata. More... | |
void | initFromArray (const Rcpp::RObject &object, const bool copyData=true) |
Initialise the object from an R array. More... | |
void | initFromDims (const std::vector< dim_t > &dim, const int datatype) |
Initialise an empty object from basic metadata. More... | |
void | updatePixdim (const std::vector< pixdim_t > &pixdim) |
Modify the pixel dimensions, and potentially the xform matrices to match. More... | |
void | setPixunits (const std::vector< std::string > &pixunits) |
Modify the pixel dimension units. More... | |
Protected Attributes | |
nifti_image * | image |
The wrapped nifti_image pointer. | |
int * | refCount |
A reference counter, shared with other objects wrapping the same pointer. | |
Thin wrapper around a C-style nifti_image
struct that allows C++-style destruction.
Reference counting is used to allow multiple NiftiImage
objects to wrap the same nifti_image
pointer, akin to a std::shared_ptr
(but without requiring C++11).
|
inline |
Copy constructor.
source | Another NiftiImage object |
copy | If true , the underlying nifti_image will be copied; otherwise the new object wraps the same nifti_image and increments the shared reference count |
|
inline |
Initialise from a block, copying in the data.
source | A Block object, referring to part of another NiftiImage |
|
inline |
Initialise using an existing nifti_image
pointer.
image | An existing nifti_image pointer, possibly NULL |
copy | If true , the image data will be copied; otherwise this object just wraps the pointer passed to it |
RNifti::NiftiImage::NiftiImage | ( | const std::vector< dim_t > & | dim, |
const int | datatype | ||
) |
Initialise from basic metadata, allocating and zeroing pixel data.
dim | A vector of image dimensions |
datatype | A datatype code for the image data |
RNifti::NiftiImage::NiftiImage | ( | const std::vector< dim_t > & | dim, |
const std::string & | datatype | ||
) |
Initialise from basic metadata, allocating and zeroing pixel data.
dim | A vector of image dimensions |
datatype | A datatype string for the image data |
RNifti::NiftiImage::NiftiImage | ( | const std::string & | path, |
const bool | readData = true |
||
) |
Initialise using a path string.
path | A string specifying a path to a valid NIfTI-1 file, possibly gzipped |
readData | If true , the data will be read as well as the metadata |
runtime_error | If reading from the file fails |
RNifti::NiftiImage::NiftiImage | ( | const std::string & | path, |
const std::vector< dim_t > & | volumes | ||
) |
Initialise using a path string and sequence of required volumes.
path | A string specifying a path to a valid NIfTI-1 file, possibly gzipped |
volumes | The volumes to read in (squashing all dimensions above the third together) |
runtime_error | If reading from the file fails, or volumes is empty |
RNifti::NiftiImage::NiftiImage | ( | const SEXP | object, |
const bool | readData = true , |
||
const bool | readOnly = false |
||
) |
Initialise from an R object, retrieving an existing image from an external pointer attribute if available; otherwise constructing a new one from the R object itself.
object | The source object |
readData | If true , the data will be retrieved as well as the metadata |
readOnly | If true , the caller asserts that its intent is read-only. Otherwise, if the SEXP may have multiple names at the R level (according to the MAYBE_SHARED R macro), an image retrieved from an external pointer will be duplicated to preserve R's usual semantics |
|
inlineprotected |
Acquire the same pointer as another NiftiImage
, incrementing the shared reference count.
source | A reference to a NiftiImage |
|
protected |
Acquire the specified pointer to a nifti_image
struct
, taking (possibly shared) responsibility for freeing the associated memory.
If the object currently wraps another pointer, it will be released
image | The pointer to wrap |
|
inline |
Add an extension to the image.
The | new image extension, an Extension object |
|
inline |
|
inline |
NiftiImage& RNifti::NiftiImage::changeDatatype | ( | const int | datatype, |
const bool | useSlope = false |
||
) |
Change the datatype of the image, casting the pixel data if present.
datatype | A NIfTI datatype code |
useSlope | If true , and conversion is to an integer type, the data will be rescaled and the image's slope and intercept set to capture the full range of original values |
NiftiImage& RNifti::NiftiImage::changeDatatype | ( | const std::string & | datatype, |
const bool | useSlope = false |
||
) |
Change the datatype of the image, casting the pixel data if present.
datatype | A string specifying the new datatype |
useSlope | If true , and conversion is to an integer type, the data will be rescaled and the image's slope and intercept set to capture the full range of original values |
|
protected |
|
protected |
Copy the contents of a nifti_image
to create a new image, acquiring the new pointer.
source | A pointer to a nifti_image |
|
protected |
Copy the contents of another NiftiImage
to create a new image, acquiring a new pointer.
source | A reference to a NiftiImage |
|
inline |
Obtain the pixel data within the image.
NiftiImageData
object encapsulating the data
|
inline |
Obtain the pixel data within the image.
NiftiImageData
object encapsulating the data
|
inline |
Return the dimensions of the image.
|
inline |
Drop unitary dimensions.
|
inline |
Drop the data from the image, retaining only the metadata.
This method invalidates any NiftiImageData objects referencing the old data
|
inline |
Remove any extensions from the image.
|
inline |
Return a list of the extensions associated with the image.
code | Integer specifying the code corresponding to the extensions required. If -1, the default, all extensions are returned. There may be more than one extension with a given code |
|
static |
Get the NIfTI format version used by the file at the specified path.
path | A string specifying a file path |
std::vector<TargetType> RNifti::NiftiImage::getData | ( | const bool | useSlope = true | ) | const |
Extract a vector of data from the image, casting it to any required element type.
useSlope | If true , the default, then the data will be adjusted for the slope and intercept stored with the image, if any |
|
protected |
Initialise the object from an R array.
object | The source object |
copyData | If true , the data are copied in; otherwise just the metadata is extracted |
|
protected |
Initialise an empty object from basic metadata.
dim | A vector of image dimensions |
datatype | A datatype code for the image data |
|
protected |
Initialise the object from an R list with named elements, which can only contain metadata.
object | The source object |
|
protected |
Initialise the object from a reference object of class "MriImage"
.
object | The source object |
copyData | If true , the data are copied in; otherwise just the metadata is extracted |
|
protected |
Initialise the object from an S4 object of class "nifti"
.
object | The source object |
copyData | If true , the data are copied in; otherwise just the metadata is extracted |
|
inline |
Determine whether nontrivial scale and slope parameters are set.
true
if the object wraps an image pointer, its slope is not zero and the slope and intercept are not exactly one and zero; false
otherwise
|
inline |
Determine whether or not the wrapped pointer is NULL
.
true
if the wrapped pointer is NULL
; false
otherwise
|
inline |
Determine whether or not the image is marked as persistent.
false
, always false
. Use isShared instead.
|
inline |
Determine whether the wrapped pointer is shared with another NiftiImage
.
true
if the reference count is greater than 1; false
otherwise
|
inline |
Return the number of blocks in the image.
|
inline |
Return the number of colour channels used by the image.
|
inline |
Return the number of dimensions in the image.
|
inline |
Return the number of extensions associated with the image.
|
inline |
Return the number of voxels in the image.
|
inline |
Copy assignment operator, which allows a block to be used to replace the contents of a suitably sized image.
source | A reference to a suitable Block object |
|
inline |
Copy assignment operator, which copies from its argument.
source | Another NiftiImage |
|
inline |
Return the dimensions of the pixels or voxels in the image.
|
inline |
Access the qform matrix.
NiftiImage& RNifti::NiftiImage::reorient | ( | const int | i, |
const int | j, | ||
const int | k | ||
) |
Reorient the image by permuting dimensions and potentially reversing some.
i,j,k | Constants such as NIFTI_L2R , NIFTI_P2A and NIFTI_I2S , giving the canonical axes to reorient to |
NiftiImage& RNifti::NiftiImage::reorient | ( | const std::string & | orientation | ) |
Reorient the image by permuting dimensions and potentially reversing some.
orientation | A string containing some permutation of the letters L or R , P or A , I or S , giving the canonical axes to reorient to |
NiftiImage& RNifti::NiftiImage::replaceData | ( | const NiftiImageData & | data | ) |
Replace the pixel data in the image with the contents of a NiftiImageData
object.
data | A data object, whose elements will be case to match the datatype of the image |
runtime_error | If the length of the new data does not match the image |
NiftiImage& RNifti::NiftiImage::replaceData | ( | const std::vector< SourceType > & | data, |
const int | datatype = DT_NONE |
||
) |
Replace the pixel data in the image with the contents of a vector.
data | A data vector, whose elements will be used to replace the image data |
datatype | The final datatype required. By default the existing datatype of the image is used |
runtime_error | If the length of the new data does not match the image |
|
inline |
Replace all extensions with new ones.
A | list of Extension objects |
NiftiImage& RNifti::NiftiImage::rescale | ( | const std::vector< pixdim_t > & | scales | ) |
Rescale the image, changing its image dimensions and pixel dimensions.
scales | Vector of scale factors along each dimension |
|
inline |
Mark the image as persistent, so that it can be passed back to R.
persistent | The new persistence state of the object |
*this
, unmodified.
|
protected |
Modify the pixel dimension units.
pixunits | Vector of new pixel units, specified using their standard abbreviations |
|
inlinestatic |
Convert between R SEXP
object type and nifti_image
datatype codes.
sexpType | A numeric R SEXP type code |
nifti_image
datatype code runtime_error | If a non-numeric type is passed |
|
inline |
Access the sform matrix.
|
inline |
|
inline |
Rcpp::RObject RNifti::NiftiImage::toArray | ( | ) | const |
Create an R array from the image.
Rcpp::RObject RNifti::NiftiImage::toArrayOrPointer | ( | const bool | internal, |
const std::string | label | ||
) | const |
std::pair<std::string,std::string> RNifti::NiftiImage::toFile | ( | const std::string | fileName, |
const int | datatype = DT_NONE , |
||
const int | filetype = -1 |
||
) | const |
Write the image to a NIfTI-1 file.
fileName | The file name to write to, with appropriate suffix (e.g. ".nii.gz") |
datatype | The datatype to use when writing the file |
filetype | The file type to create: a NIFTI_FTYPE constant or -1. In the latter case the file name is used to determine the file type |
std::pair<std::string,std::string> RNifti::NiftiImage::toFile | ( | const std::string | fileName, |
const std::string & | datatype, | ||
const int | filetype = -1 |
||
) | const |
Write the image to a NIfTI-1 file.
fileName | The file name to write to, with appropriate suffix (e.g. ".nii.gz") |
datatype | The datatype to use when writing the file, or "auto" |
filetype | The file type to create: a NIFTI_FTYPE constant or -1. In the latter case the file name is used to determine the file type |
Rcpp::RObject RNifti::NiftiImage::toPointer | ( | const std::string | label | ) | const |
Create an internal image to pass back to R.
label | A string labelling the image |
NiftiImage& RNifti::NiftiImage::update | ( | const Rcpp::RObject & | object | ) |
Update the image from an R array.
object | An R array or list object |
|
protected |
Modify the pixel dimensions, and potentially the xform matrices to match.
pixdim | Vector of new pixel dimensions |
|
inline |
Extract a volume block from a 4D image.
i | The volume number required |
|
inline |
Extract a volume block from a 4D image.
i | The volume number required |
const Xform RNifti::NiftiImage::xform | ( | const bool | preferQuaternion = true | ) | const |
Obtain an xform matrix, indicating the orientation of the image.
preferQuaternion | If true , use the qform matrix in preference to the sform; otherwise prefer the sform |