RNifti
Fast R and C++ Access to NIfTI Images
|
1 #ifndef _NIFTI_IMAGE_H_
2 #define _NIFTI_IMAGE_H_
12 #define MAYBE_SHARED(x) (NAMED(x) > 1)
17 #define R_NegInf -INFINITY
44 typedef std::complex<float> complex64_t;
45 typedef std::complex<double> complex128_t;
57 unsigned char bytes[4];
83 virtual size_t size ()
const {
return 0; }
84 virtual bool hasNaN ()
const {
return false; }
85 virtual complex128_t getComplex (
void *ptr)
const {
return complex128_t(0.0, 0.0); }
86 virtual double getDouble (
void *ptr)
const {
return 0.0; }
87 virtual int getInt (
void *ptr)
const {
return 0; }
89 virtual void setComplex (
void *ptr,
const complex128_t value)
const {}
90 virtual void setDouble (
void *ptr,
const double value)
const {}
91 virtual void setInt (
void *ptr,
const int value)
const {}
92 virtual void setRgb (
void *ptr,
const rgba32_t value)
const {}
93 virtual void minmax (
void *ptr,
const size_t length,
double *min,
double *max)
const { *min = 0.0; *max = 0.0; }
99 template <
typename Type,
bool alpha = false>
102 size_t size ()
const {
return (
sizeof(Type)); }
103 bool hasNaN ()
const {
return std::numeric_limits<Type>::has_quiet_NaN; }
104 complex128_t getComplex (
void *ptr)
const {
return complex128_t(
static_cast<double>(*
static_cast<Type*
>(ptr)), 0.0); }
105 double getDouble (
void *ptr)
const {
return static_cast<double>(*
static_cast<Type*
>(ptr)); }
106 int getInt (
void *ptr)
const {
return static_cast<int>(*
static_cast<Type*
>(ptr)); }
107 void setComplex (
void *ptr,
const complex128_t value)
const
109 *(
static_cast<Type*
>(ptr)) = Type(value.real());
110 *(
static_cast<Type*
>(ptr) + 1) = Type(0);
112 void setDouble (
void *ptr,
const double value)
const { *(
static_cast<Type*
>(ptr)) = Type(value); }
113 void setInt (
void *ptr,
const int value)
const { *(
static_cast<Type*
>(ptr)) = Type(value); }
114 void minmax (
void *ptr,
const size_t length,
double *min,
double *max)
const;
117 template <
typename ElementType>
120 size_t size ()
const {
return (
sizeof(ElementType) * 2); }
121 bool hasNaN ()
const {
return std::numeric_limits<ElementType>::has_quiet_NaN; }
122 std::complex<ElementType> getNative (
void *ptr)
const
124 const ElementType real = *
static_cast<ElementType*
>(ptr);
125 const ElementType imag = *(
static_cast<ElementType*
>(ptr) + 1);
126 return std::complex<ElementType>(real, imag);
128 void setNative (
void *ptr,
const std::complex<ElementType> native)
const
130 *(
static_cast<ElementType*
>(ptr)) = native.real();
131 *(
static_cast<ElementType*
>(ptr) + 1) = native.imag();
133 complex128_t getComplex (
void *ptr)
const {
return complex128_t(getNative(ptr)); }
134 double getDouble (
void *ptr)
const {
return static_cast<double>(getNative(ptr).real()); }
135 int getInt (
void *ptr)
const {
return static_cast<int>(getNative(ptr).real()); }
136 void setComplex (
void *ptr,
const complex128_t value)
const { setNative(ptr, std::complex<ElementType>(value)); }
137 void setDouble (
void *ptr,
const double value)
const { setNative(ptr, std::complex<ElementType>(value, 0.0)); }
138 void setInt (
void *ptr,
const int value)
const { setNative(ptr, std::complex<ElementType>(
static_cast<ElementType
>(value), 0.0)); }
139 void minmax (
void *ptr,
const size_t length,
double *min,
double *max)
const;
142 template <
bool alpha>
145 size_t size ()
const {
return alpha ? 4 : 3; }
146 int getInt (
void *ptr)
const {
return getRgb(ptr).value.packed; }
150 unsigned char *source =
static_cast<unsigned char *
>(ptr);
151 std::copy(source, source + (alpha ? 4 : 3), value.value.bytes);
154 void setInt (
void *ptr,
const int value)
const
157 native.value.packed = value;
160 void setRgb (
void *ptr,
const rgba32_t value)
const
162 unsigned char *target =
static_cast<unsigned char *
>(ptr);
163 std::copy(value.value.bytes, value.value.bytes + (alpha ? 4 : 3), target);
165 void minmax (
void *ptr,
const size_t length,
double *min,
double *max)
const { *min = 0.0; *max = 255.0; }
196 throw std::runtime_error(
"Unsupported data type (" + std::string(nifti_datatype_string(
_datatype)) +
")");
227 else if (alloc && data == NULL)
249 double dataMin, dataMax, typeMin, typeMax;
250 data.
minmax(&dataMin, &dataMax);
251 handler->minmax(NULL, 0, &typeMin, &typeMax);
254 if (dataMin < typeMin || dataMax > typeMax)
256 slope = (dataMax - dataMin) / (typeMax - typeMin);
282 this->ptr = (ptr == NULL ? parent.
dataPtr : ptr);
292 template <
typename SourceType>
305 template <
typename TargetType>
306 operator TargetType()
const
310 else if (std::numeric_limits<TargetType>::is_integer)
311 return TargetType(parent.
handler->getInt(ptr));
313 return TargetType(parent.
handler->getDouble(ptr));
316 template <
typename ElementType>
317 operator std::complex<ElementType>()
const
322 return std::complex<ElementType>(parent.
handler->getComplex(ptr));
329 operator Rcomplex()
const
331 const complex128_t value = parent.
handler->getComplex(ptr);
332 Rcomplex rValue = { value.real(), value.imag() };
344 return parent.
handler->getRgb(ptr);
351 class Iterator :
public std::iterator<std::random_access_iterator_tag, Element>
369 this->ptr = (ptr == NULL ? parent.
dataPtr : ptr);
370 this->step = (step == 0 ? parent.
handler->size() : step);
378 : parent(other.parent), ptr(other.ptr), step(other.step) {}
380 Iterator & operator++ () { ptr =
static_cast<char*
>(ptr) + step;
return *
this; }
381 Iterator operator++ (
int) {
Iterator copy(*
this); ptr =
static_cast<char*
>(ptr) + step;
return copy; }
382 Iterator operator+ (ptrdiff_t n)
const
384 void *newptr =
static_cast<char*
>(ptr) + (n * step);
385 return Iterator(parent, newptr, step);
387 Iterator & operator-- () { ptr =
static_cast<char*
>(ptr) - step;
return *
this; }
388 Iterator operator-- (
int) {
Iterator copy(*
this); ptr =
static_cast<char*
>(ptr) - step;
return copy; }
389 Iterator operator- (ptrdiff_t n)
const
391 void *newptr =
static_cast<char*
>(ptr) - (n * step);
392 return Iterator(parent, newptr, step);
395 ptrdiff_t operator- (
const Iterator &other)
const
397 const ptrdiff_t difference =
static_cast<char*
>(ptr) -
static_cast<char*
>(other.ptr);
398 return difference / step;
401 bool operator== (
const Iterator &other)
const {
return (ptr==other.ptr && step==other.step); }
402 bool operator!= (
const Iterator &other)
const {
return (ptr!=other.ptr || step!=other.step); }
403 bool operator> (
const Iterator &other)
const {
return (ptr > other.ptr); }
404 bool operator< (
const Iterator &other)
const {
return (ptr < other.ptr); }
406 const Element operator* ()
const {
return Element(parent, ptr); }
407 Element operator* () {
return Element(parent, ptr); }
408 const Element operator[] (
const size_t i)
const {
return Element(parent,
static_cast<char*
>(ptr) + (i * step)); }
409 Element operator[] (
const size_t i) {
return Element(parent,
static_cast<char*
>(ptr) + (i * step)); }
439 init(NULL, 0, DT_NONE, 0.0, 0.0,
false);
441 init(image->data, image->nvox, image->datatype,
static_cast<double>(image->scl_slope),
static_cast<double>(image->scl_inter),
false);
460 std::copy(source.
begin(), source.
end(), this->begin());
470 template <
class InputIterator>
473 const size_t length =
static_cast<size_t>(std::distance(from, to));
475 std::copy(from, to, this->
begin());
600 void minmax (
double *min,
double *max)
const
616 inline bool NiftiImageData::ConcreteTypeHandler<int>::hasNaN ()
const {
return true; }
620 template <
typename ElementType,
int Length>
624 ElementType elements[Length];
627 Vector (
const ElementType value = 0.0)
629 std::fill(elements, elements + Length, value);
632 Vector (
const ElementType * source)
634 std::copy(source, source + Length, this->elements);
640 for (
int i=0; i<Length; i++)
641 result.elements[i] = -elements[i];
645 const ElementType & operator[] (
const size_t i)
const {
return elements[i]; }
647 ElementType & operator[] (
const size_t i) {
return elements[i]; }
650 template <
class NiftiType,
typename ElementType,
int Order>
654 ElementType elements[Order*Order];
656 NiftiType * niftiPointer ()
const {
return (NiftiType *) elements; }
657 NiftiType niftiCopy ()
const
660 std::copy(elements, elements + Order*Order, *value.m);
665 typedef NiftiType NativeType;
671 std::fill(elements, elements + Order*Order, value);
676 std::copy(source, source + Order*Order, this->elements);
681 std::copy(*source.m, *source.m + Order*Order, this->elements);
684 operator const NiftiType ()
const {
return niftiCopy(); }
686 operator NiftiType () {
return niftiCopy(); }
688 const ElementType * begin ()
const {
return elements; }
690 ElementType * begin () {
return elements; }
692 const ElementType * end ()
const {
return elements + Order*Order; }
694 ElementType * end () {
return elements + Order*Order; }
699 for (
int i=0; i<Order; i++)
700 matrix.elements[i + i*Order] = 1.0;
706 ElementType colnorm ()
const;
707 ElementType rownorm ()
const;
708 ElementType determ ()
const;
715 const ElementType & operator() (
const int i,
const int j)
const {
return elements[j + i*Order]; }
716 ElementType & operator() (
const int i,
const int j) {
return elements[j + i*Order]; }
721 #include "RNifti/NiftiImage_matrix.h"
733 #if RNIFTI_NIFTILIB_VERSION == 1
735 typedef float pixdim_t;
736 typedef float scale_t;
737 #elif RNIFTI_NIFTILIB_VERSION == 2
738 typedef int64_t dim_t;
739 typedef double pixdim_t;
740 typedef double scale_t;
764 throw std::runtime_error(
"Blocks must be along the last dimension in the image");
777 if (source->datatype !=
image->datatype)
778 throw std::runtime_error(
"New data does not have the same datatype as the target block");
779 if (source->scl_slope !=
image->scl_slope || source->scl_inter !=
image->scl_inter)
780 throw std::runtime_error(
"New data does not have the same scale parameters as the target block");
782 size_t blockSize = 1;
786 if (blockSize != source->nvox)
787 throw std::runtime_error(
"New data does not have the same size as the target block");
789 blockSize *=
image->nbyper;
804 size_t blockSize = 1;
818 template <
typename TargetType>
819 std::vector<TargetType>
getData (
const bool useSlope =
true)
const;
825 #if RNIFTI_NIFTILIB_VERSION == 1
826 typedef float Element;
831 #elif RNIFTI_NIFTILIB_VERSION == 2
832 typedef double Element;
840 Element *forward, *inverse, *qparams;
843 void replace (
const Matrix &source);
847 : forward(NULL), inverse(NULL), qparams(NULL), mat() {}
849 Xform (
const Matrix &source)
850 : forward(NULL), inverse(NULL), qparams(NULL), mat(source) {}
852 Xform (
const Matrix::NativeType &source)
853 : forward(NULL), inverse(NULL), qparams(NULL), mat(source) {}
855 Xform (Matrix::NativeType &source)
856 : forward(*source.m), inverse(NULL), qparams(NULL), mat(source) {}
858 Xform (Matrix::NativeType &source, Matrix::NativeType &inverse, Element *qparams = NULL)
859 : forward(*source.m), inverse(*inverse.m), qparams(qparams), mat(source) {}
861 operator const Matrix::NativeType ()
const {
return mat; }
863 operator Matrix::NativeType () {
return mat; }
871 Xform & operator= (
const Matrix &source)
877 const Matrix & matrix ()
const {
return mat; }
878 Submatrix submatrix ()
const;
879 Submatrix rotation ()
const;
880 Vector4 quaternion ()
const;
881 Vector3 offset ()
const;
882 std::string orientation ()
const;
894 if (sexpType == INTSXP || sexpType == LGLSXP)
896 else if (sexpType == REALSXP)
898 else if (sexpType == CPLXSXP)
899 return DT_COMPLEX128;
901 throw std::runtime_error(
"Array elements must be numeric");
946 void copy (
const nifti_image *source);
988 void initFromArray (
const Rcpp::RObject &
object,
const bool copyData =
true);
1032 Rc_printf(
"Creating NiftiImage with pointer %p (from NiftiImage)\n", this->image);
1045 Rc_printf(
"Creating NiftiImage with pointer %p (from Block)\n", this->image);
1063 Rc_printf(
"Creating NiftiImage with pointer %p (from pointer)\n", this->image);
1087 NiftiImage (
const std::string &path,
const bool readData =
true);
1095 NiftiImage (
const std::string &path,
const std::vector<dim_t> &volumes);
1108 NiftiImage (
const SEXP
object,
const bool readData =
true,
const bool readOnly =
false);
1120 operator const nifti_image* ()
const {
return image; }
1125 operator nifti_image* () {
return image; }
1145 Rc_printf(
"Creating NiftiImage with pointer %p (from NiftiImage)\n", this->image);
1159 Rc_printf(
"Creating NiftiImage with pointer %p (from Block)\n", this->image);
1216 std::vector<dim_t>
dim ()
const
1219 return std::vector<dim_t>();
1231 return std::vector<pixdim_t>();
1233 return std::vector<pixdim_t>(
image->pixdim+1,
image->pixdim+
image->ndim+1);
1244 int ndim =
image->ndim;
1245 while (
image->dim[ndim] < 2)
1273 template <
typename TargetType>
1274 std::vector<TargetType>
getData (
const bool useSlope =
true)
const;
1302 template <
typename SourceType>
1319 #if RNIFTI_NIFTILIB_VERSION == 1
1320 nifti_image_unload(
image);
1321 #elif RNIFTI_NIFTILIB_VERSION == 2
1322 nifti2_image_unload(
image);
1373 Xform qform () {
return (
image == NULL ? Xform() : Xform(
image->qto_xyz,
image->qto_ijk, &
image->quatern_b)); }
1375 const Xform sform ()
const {
return (
image == NULL ? Xform() : Xform(
image->sto_xyz)); }
1377 Xform sform () {
return (
image == NULL ? Xform() : Xform(
image->sto_xyz,
image->sto_ijk)); }
1442 switch (
image->datatype)
1444 case DT_NONE:
return 0;
1445 case DT_RGB24:
return 3;
1446 case DT_RGBA32:
return 4;
1464 std::pair<std::string,std::string>
toFile (
const std::string fileName,
const int datatype = DT_NONE)
const;
1472 std::pair<std::string,std::string>
toFile (
const std::string fileName,
const std::string &datatype)
const;
1508 #include "RNifti/NiftiImage_impl.h"
NiftiImage(const std::string &path, const std::vector< dim_t > &volumes)
Initialise using a path string and sequence of required volumes.
NiftiImage & rescale(const std::vector< pixdim_t > &scales)
Rescale the image, changing its image dimensions and pixel dimensions.
void release()
Release the currently wrapped pointer, if it is not NULL, decrementing the reference count and releas...
bool isScaled() const
Determine whether the object uses data scaling.
Definition: NiftiImage.h:528
Inner class referring to a subset of an image.
Definition: NiftiImage.h:748
NiftiImage(const Block &source)
Initialise from a block, copying in the data.
Definition: NiftiImage.h:1040
std::vector< dim_t > dim() const
Return the dimensions of the image.
Definition: NiftiImage.h:1216
bool isComplex() const
Determine whether the datatype is complex.
Definition: NiftiImage.h:534
NiftiImageData & operator=(const NiftiImageData &source)
Copy assignment operator.
Definition: NiftiImage.h:493
nifti_image * image
The wrapped nifti_image pointer.
Definition: NiftiImage.h:915
Iterator type for NiftiImageData, with Element as its value type.
Definition: NiftiImage.h:352
Block block(const int i)
Extract a block from the image.
Definition: NiftiImage.h:1401
const Block volume(const int i) const
Extract a volume block from a 4D image.
Definition: NiftiImage.h:1422
NiftiImage & dropData()
Drop the data from the image, retaining only the metadata.
Definition: NiftiImage.h:1317
int * refCount
A reference counter, shared with other objects wrapping the same pointer.
Definition: NiftiImage.h:916
Element(const NiftiImageData &parent, void *ptr=NULL)
Primary constructor.
Definition: NiftiImage.h:279
NiftiImage & operator=(const NiftiImage &source)
Copy assignment operator, which copies from its argument.
Definition: NiftiImage.h:1141
NiftiImage(const std::string &path, const bool readData=true)
Initialise using a path string.
NiftiImage(const NiftiImage &source, const bool copy=true)
Copy constructor.
Definition: NiftiImage.h:1024
NiftiImage & changeDatatype(const int datatype, const bool useSlope=false)
Change the datatype of the image, casting the pixel data if present.
NiftiImageData(void *data, const size_t length, const int datatype, const double slope=1.0, const double intercept=0.0)
Primary constructor.
Definition: NiftiImage.h:427
void updatePixdim(const std::vector< pixdim_t > &pixdim)
Modify the pixel dimensions, and potentially the xform matrices to match.
Abstract inner class defining the type-specific functions required in concrete subclasses.
Definition: NiftiImage.h:81
const Iterator end() const
Obtain a constant iterator corresponding to the end of the blob.
Definition: NiftiImage.h:571
Thin wrapper around a C-style nifti_image struct that allows C++-style destruction.
Definition: NiftiImage.h:731
Simple RGB(A) type encapsulating an 8-bit colour value with optional opacity, which can also be set o...
Definition: NiftiImage.h:54
NiftiImageData(InputIterator from, InputIterator to, const int datatype)
Iterator-based constructor.
Definition: NiftiImage.h:471
bool isFloatingPoint() const
Determine whether the datatype is floating point.
Definition: NiftiImage.h:541
size_t size() const
Return the number of elements in the data.
Definition: NiftiImage.h:509
bool isNull() const
Determine whether or not the wrapped pointer is NULL.
Definition: NiftiImage.h:1177
void copy(const nifti_image *source)
Copy the contents of a nifti_image to create a new image, acquiring the new pointer.
void setPixunits(const std::vector< std::string > &pixunits)
Modify the pixel dimension units.
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.
size_t length() const
Return the number of elements in the data.
Definition: NiftiImage.h:508
double slope
The slope term used to scale data values.
Definition: NiftiImage.h:73
Block slice(const int i)
Extract a slice block from a 3D image.
Definition: NiftiImage.h:1415
Rcpp::RObject toPointer(const std::string label) const
Create an internal image to pass back to R.
const Element operator[](const size_t i) const
Indexing operator, returning a constant element.
Definition: NiftiImage.h:584
void acquire(const NiftiImage &source)
Acquire the same pointer as another NiftiImage, incrementing the shared reference count.
Definition: NiftiImage.h:930
NiftiImageData(const NiftiImageData &source, const int datatype=DT_NONE)
Copy constructor with optional type conversion.
Definition: NiftiImage.h:451
bool isShared() const
Determine whether the wrapped pointer is shared with another NiftiImage.
Definition: NiftiImage.h:1183
double intercept
The intercept term used to scale data values.
Definition: NiftiImage.h:74
dim_t nBlocks() const
Return the number of blocks in the image.
Definition: NiftiImage.h:1383
Iterator end()
Obtain a mutable iterator corresponding to the end of the blob.
Definition: NiftiImage.h:577
Rcpp::RObject toArrayOrPointer(const bool internal, const std::string label) const
A conditional method that calls either toArray or toPointer.
std::pair< std::string, std::string > toFile(const std::string fileName, const int datatype=DT_NONE) const
Write the image to a NIfTI-1 file.
NiftiImage & reorient(const std::string &orientation)
Reorient the image by permuting dimensions and potentially reversing some.
std::vector< TargetType > getData(const bool useSlope=true) const
Extract a vector of data from the image, casting it to any required element type.
Inner class representing a single element in the data blob.
Definition: NiftiImage.h:267
NiftiImage & update(const Rcpp::RObject &object)
Update the image from an R array.
const NiftiImageData data() const
Obtain the pixel data within the image.
Definition: NiftiImage.h:1256
NiftiImage & reorient(const int i, const int j, const int k)
Reorient the image by permuting dimensions and potentially reversing some.
size_t _length
The number of data elements in the blob.
Definition: NiftiImage.h:203
bool isRgb() const
Determine whether the datatype corresponds to an RGB type.
Definition: NiftiImage.h:553
NiftiImage(nifti_image *const image, const bool copy=false)
Initialise using an existing nifti_image pointer.
Definition: NiftiImage.h:1055
Wrapper class encapsulating a NIfTI data blob, with responsibility for handling data scaling and poly...
Definition: NiftiImage.h:71
Rcpp::RObject toArray() const
Create an R array from the image.
NiftiImageData data() const
Obtain the data within the block.
Definition: NiftiImage.h:798
const Block block(const int i) const
Extract a block from the image.
Definition: NiftiImage.h:1392
Definition: NiftiImage.h:622
bool isInteger() const
Determine whether the datatype is an integer type.
Definition: NiftiImage.h:547
NiftiImage & setPersistence(const bool persistent)
Mark the image as persistent, so that it can be passed back to R.
Definition: NiftiImage.h:1171
NiftiImage & replaceData(const NiftiImageData &data)
Replace the pixel data in the image with the contents of a NiftiImageData object.
Block volume(const int i)
Extract a volume block from a 4D image.
Definition: NiftiImage.h:1429
NiftiImageData unscaled() const
Return a similar object to the callee, but with the slope and intercept values reset.
Definition: NiftiImage.h:559
virtual ~NiftiImage()
Destructor which decrements the reference counter, and releases the wrapped pointer if the counter dr...
Definition: NiftiImage.h:1115
NiftiImage & changeDatatype(const std::string &datatype, const bool useSlope=false)
Change the datatype of the image, casting the pixel data if present.
NiftiImageData & disown()
Disown the data blob, removing responsibility for freeing it upon destruction.
Definition: NiftiImage.h:565
bool isDataScaled() const
Determine whether nontrivial scale and slope parameters are set.
Definition: NiftiImage.h:1198
void acquire(nifti_image *const image)
Acquire the specified pointer to a nifti_image struct, taking (possibly shared) responsibility for fr...
Iterator begin()
Obtain a mutable iterator corresponding to the start of the blob.
Definition: NiftiImage.h:574
const Xform xform(const bool preferQuaternion=true) const
Obtain an xform matrix, indicating the orientation of the image.
Concrete inner class template defining behaviour specific to individual data types.
Definition: NiftiImage.h:101
const dim_t index
The location along dimension.
Definition: NiftiImage.h:751
TypeHandler * createHandler()
Create a concrete type handler appropriate to the datatype code stored with the data.
Definition: NiftiImage.h:173
Definition: NiftiImage.h:55
static int fileVersion(const std::string &path)
Get the NIfTI format version used by the file at the specified path.
std::vector< TargetType > getData(const bool useSlope=true) const
Extract a vector of data from a block, casting it to any required element type.
NiftiImage()
Default constructor.
Definition: NiftiImage.h:1015
const nifti_image * operator->() const
Allows a NiftiImage object to be treated as a pointer to a const nifti_image.
Definition: NiftiImage.h:1130
void * dataPtr
Opaque pointer to the underlying data blob.
Definition: NiftiImage.h:200
int datatype() const
Return stored datatype code.
Definition: NiftiImage.h:507
Block & operator=(const NiftiImage &source)
Copy assignment operator, which allows a block in one image to be replaced with the contents of anoth...
Definition: NiftiImage.h:775
TypeHandler * handler
Type handler, which is created to match the datatype.
Definition: NiftiImage.h:202
size_t nVoxels() const
Return the number of voxels in the image.
Definition: NiftiImage.h:1456
size_t totalBytes() const
Return the total size of the data blob, in bytes.
Definition: NiftiImage.h:515
void calibrateFrom(const NiftiImageData &data)
Update the slope and intercept to cover the range of another data object.
Definition: NiftiImage.h:242
void initFromMriImage(const Rcpp::RObject &object, const bool copyData=true)
Initialise the object from a reference object of class "MriImage".
size_t bytesPerPixel() const
Return the number of bytes used per element, or zero if the datatype is undefined or the blob is NULL...
Definition: NiftiImage.h:512
void init(void *data, const size_t length, const int datatype, const double slope, const double intercept, const bool alloc=true)
Initialiser method, used by constructors.
Definition: NiftiImage.h:216
Definition: NiftiImage.h:823
bool owner
An indicator of whether this object is responsible for cleaning up the data.
Definition: NiftiImage.h:204
std::pair< std::string, std::string > toFile(const std::string fileName, const std::string &datatype) const
Write the image to a NIfTI-1 file.
Definition: NiftiImage.h:652
const NiftiImage & image
The parent image.
Definition: NiftiImage.h:749
NiftiImageData(nifti_image *image)
Convenience constructor for a nifti_image.
Definition: NiftiImage.h:436
void * blob() const
Return an opaque pointer to the blob.
Definition: NiftiImage.h:506
NiftiImage(const std::vector< dim_t > &dim, const std::string &datatype)
Initialise from basic metadata, allocating and zeroing pixel data.
std::vector< pixdim_t > pixdim() const
Return the dimensions of the pixels or voxels in the image.
Definition: NiftiImage.h:1228
void minmax(double *min, double *max) const
Calculate the minimum and maximum values in the blob, as doubles.
Definition: NiftiImage.h:600
int nDims() const
Return the number of dimensions in the image.
Definition: NiftiImage.h:1204
Iterator(const Iterator &other)
Copy constructor.
Definition: NiftiImage.h:377
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 avail...
Iterator(const NiftiImageData &parent, void *ptr=NULL, const size_t step=0)
Primary constructor.
Definition: NiftiImage.h:366
NiftiImage & drop()
Drop unitary dimensions.
Definition: NiftiImage.h:1242
void initFromNiftiS4(const Rcpp::RObject &object, const bool copyData=true)
Initialise the object from an S4 object of class "nifti".
void initFromArray(const Rcpp::RObject &object, const bool copyData=true)
Initialise the object from an R array.
void initFromList(const Rcpp::RObject &object)
Initialise the object from an R list with named elements, which can only contain metadata.
NiftiImageData data()
Obtain the pixel data within the image.
Definition: NiftiImage.h:1262
int nChannels() const
Return the number of colour channels used by the image.
Definition: NiftiImage.h:1436
void copy(const NiftiImage &source)
Copy the contents of another NiftiImage to create a new image, acquiring a new pointer.
const int dimension
The dimension along which the block applies (which should be the last)
Definition: NiftiImage.h:750
Block(const NiftiImage &image, const int dimension, const dim_t index)
Standard constructor for this class.
Definition: NiftiImage.h:760
const Block slice(const int i) const
Extract a slice block from a 3D image.
Definition: NiftiImage.h:1408
Rcpp::RObject headerToList() const
Create an R list containing raw image metadata.
bool isPersistent() const
Determine whether or not the image is marked as persistent.
Definition: NiftiImage.h:1191
Element & operator=(const SourceType &value)
Copy assignment operator.
virtual ~NiftiImageData()
Destructor which frees the type handler, and the data blob if it is owned by this object.
Definition: NiftiImage.h:481
bool isEmpty() const
Determine whether or not the object is empty.
Definition: NiftiImage.h:521
const Iterator begin() const
Obtain a constant iterator corresponding to the start of the blob.
Definition: NiftiImage.h:568
NiftiImageData()
Default constructor, creating an empty data object.
Definition: NiftiImage.h:415
void initFromDims(const std::vector< dim_t > &dim, const int datatype)
Initialise an empty object from basic metadata.
void copy(const Block &source)
Copy the contents of a Block to create a new image, acquiring a new pointer.
NiftiImage(const std::vector< dim_t > &dim, const int datatype)
Initialise from basic metadata, allocating and zeroing pixel data.
int _datatype
Datatype code indicating the actual type of the elements.
Definition: NiftiImage.h:201
static int sexpTypeToNiftiType(const int sexpType)
Convert between R SEXP object type and nifti_image datatype codes.
Definition: NiftiImage.h:892