RNifti
Fast R and C++ Access to NIfTI Images
NiftiImage.h
1 #ifndef _NIFTI_IMAGE_H_
2 #define _NIFTI_IMAGE_H_
3 
4 
5 #ifdef USING_R
6 
7 #include <Rcpp.h>
8 
9 // Defined since R 3.1.0, according to Tomas Kalibera, but there's no reason to break
10 // compatibility with 3.0.x
11 #ifndef MAYBE_SHARED
12 #define MAYBE_SHARED(x) (NAMED(x) > 1)
13 #endif
14 
15 #else
16 
17 #define R_NegInf -INFINITY
18 
19 #include <stdint.h>
20 #include <cstddef>
21 #include <cmath>
22 #include <string>
23 #include <sstream>
24 #include <vector>
25 #include <complex>
26 #include <stdexcept>
27 #include <algorithm>
28 #include <map>
29 #include <locale>
30 #include <limits>
31 
32 #endif
33 
42 namespace RNifti {
43 
44 typedef std::complex<float> complex64_t;
45 typedef std::complex<double> complex128_t;
46 
53 struct rgba32_t
54 {
55  union ValueType {
56  int packed;
57  unsigned char bytes[4];
58  };
59  ValueType value;
60  rgba32_t () { value.packed = 0; }
61 };
62 
71 {
72 public:
73  double slope;
74  double intercept;
76 protected:
80  struct TypeHandler
81  {
82  virtual ~TypeHandler() {}
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; }
88  virtual rgba32_t getRgb (void *ptr) const { return rgba32_t(); }
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; }
94  };
95 
99  template <typename Type, bool alpha = false>
101  {
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
108  {
109  *(static_cast<Type*>(ptr)) = Type(value.real());
110  *(static_cast<Type*>(ptr) + 1) = Type(0);
111  }
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;
115  };
116 
117  template <typename ElementType>
118  struct ConcreteTypeHandler<std::complex<ElementType>,false> : public TypeHandler
119  {
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
123  {
124  const ElementType real = *static_cast<ElementType*>(ptr);
125  const ElementType imag = *(static_cast<ElementType*>(ptr) + 1);
126  return std::complex<ElementType>(real, imag);
127  }
128  void setNative (void *ptr, const std::complex<ElementType> native) const
129  {
130  *(static_cast<ElementType*>(ptr)) = native.real();
131  *(static_cast<ElementType*>(ptr) + 1) = native.imag();
132  }
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;
140  };
141 
142  template <bool alpha>
143  struct ConcreteTypeHandler<rgba32_t,alpha> : public TypeHandler
144  {
145  size_t size () const { return alpha ? 4 : 3; }
146  int getInt (void *ptr) const { return getRgb(ptr).value.packed; }
147  rgba32_t getRgb (void *ptr) const
148  {
149  rgba32_t value;
150  unsigned char *source = static_cast<unsigned char *>(ptr);
151  std::copy(source, source + (alpha ? 4 : 3), value.value.bytes);
152  return value;
153  }
154  void setInt (void *ptr, const int value) const
155  {
156  rgba32_t native;
157  native.value.packed = value;
158  setRgb(ptr, native);
159  }
160  void setRgb (void *ptr, const rgba32_t value) const
161  {
162  unsigned char *target = static_cast<unsigned char *>(ptr);
163  std::copy(value.value.bytes, value.value.bytes + (alpha ? 4 : 3), target);
164  }
165  void minmax (void *ptr, const size_t length, double *min, double *max) const { *min = 0.0; *max = 255.0; }
166  };
167 
174  {
175  if (_datatype == DT_NONE)
176  return NULL;
177 
178  switch (_datatype)
179  {
180  case DT_UINT8: return new ConcreteTypeHandler<uint8_t>(); break;
181  case DT_INT16: return new ConcreteTypeHandler<int16_t>(); break;
182  case DT_INT32: return new ConcreteTypeHandler<int32_t>(); break;
183  case DT_FLOAT32: return new ConcreteTypeHandler<float>(); break;
184  case DT_FLOAT64: return new ConcreteTypeHandler<double>(); break;
185  case DT_INT8: return new ConcreteTypeHandler<int8_t>(); break;
186  case DT_UINT16: return new ConcreteTypeHandler<uint16_t>(); break;
187  case DT_UINT32: return new ConcreteTypeHandler<uint32_t>(); break;
188  case DT_INT64: return new ConcreteTypeHandler<int64_t>(); break;
189  case DT_UINT64: return new ConcreteTypeHandler<uint64_t>(); break;
190  case DT_COMPLEX64: return new ConcreteTypeHandler<complex64_t>(); break;
191  case DT_COMPLEX128: return new ConcreteTypeHandler<complex128_t>(); break;
192  case DT_RGB24: return new ConcreteTypeHandler<rgba32_t,false>(); break;
193  case DT_RGBA32: return new ConcreteTypeHandler<rgba32_t,true>(); break;
194 
195  default:
196  throw std::runtime_error("Unsupported data type (" + std::string(nifti_datatype_string(_datatype)) + ")");
197  }
198  }
199 
200  void *dataPtr;
201  int _datatype;
203  size_t _length;
204  bool owner;
216  void init (void *data, const size_t length, const int datatype, const double slope, const double intercept, const bool alloc = true)
217  {
218  this->_length = length;
219  this->_datatype = datatype;
220  this->slope = slope;
221  this->intercept = intercept;
222 
223  owner = false;
225  if (handler == NULL)
226  dataPtr = NULL;
227  else if (alloc && data == NULL)
228  {
229  dataPtr = calloc(length, handler->size());
230  owner = true;
231  }
232  else
233  dataPtr = data;
234  }
235 
242  void calibrateFrom (const NiftiImageData &data)
243  {
244  slope = 1.0;
245  intercept = 0.0;
246 
247  if (this->isInteger())
248  {
249  double dataMin, dataMax, typeMin, typeMax;
250  data.minmax(&dataMin, &dataMax);
251  handler->minmax(NULL, 0, &typeMin, &typeMax);
252 
253  // If the source type is floating-point but values are in range, we will just round them
254  if (dataMin < typeMin || dataMax > typeMax)
255  {
256  slope = (dataMax - dataMin) / (typeMax - typeMin);
257  intercept = dataMin - (slope) * typeMin;
258  }
259  }
260  }
261 
262 public:
266  struct Element
267  {
268  private:
269  const NiftiImageData &parent;
270  void *ptr;
271 
272  public:
279  Element (const NiftiImageData &parent, void *ptr = NULL)
280  : parent(parent)
281  {
282  this->ptr = (ptr == NULL ? parent.dataPtr : ptr);
283  }
284 
292  template <typename SourceType>
293  Element & operator= (const SourceType &value);
294 
300  Element & operator= (const Element &other);
301 
305  template <typename TargetType>
306  operator TargetType() const
307  {
308  if (parent.isScaled())
309  return TargetType(parent.handler->getDouble(ptr) * parent.slope + parent.intercept);
310  else if (std::numeric_limits<TargetType>::is_integer)
311  return TargetType(parent.handler->getInt(ptr));
312  else
313  return TargetType(parent.handler->getDouble(ptr));
314  }
315 
316  template <typename ElementType>
317  operator std::complex<ElementType>() const
318  {
319  if (parent.isScaled())
320  return std::complex<ElementType>(parent.handler->getComplex(ptr) * parent.slope + complex128_t(parent.intercept, parent.intercept));
321  else
322  return std::complex<ElementType>(parent.handler->getComplex(ptr));
323  }
324 
325 #ifdef USING_R
326 
329  operator Rcomplex() const
330  {
331  const complex128_t value = parent.handler->getComplex(ptr);
332  Rcomplex rValue = { value.real(), value.imag() };
333  if (parent.isScaled())
334  {
335  rValue.r = rValue.r * parent.slope + parent.intercept;
336  rValue.i = rValue.i * parent.slope + parent.intercept;
337  }
338  return rValue;
339  }
340 #endif
341 
342  operator rgba32_t() const
343  {
344  return parent.handler->getRgb(ptr);
345  }
346  };
347 
351  class Iterator : public std::iterator<std::random_access_iterator_tag, Element>
352  {
353  private:
354  const NiftiImageData &parent;
355  void *ptr;
356  size_t step;
357 
358  public:
366  Iterator (const NiftiImageData &parent, void *ptr = NULL, const size_t step = 0)
367  : parent(parent)
368  {
369  this->ptr = (ptr == NULL ? parent.dataPtr : ptr);
370  this->step = (step == 0 ? parent.handler->size() : step);
371  }
372 
377  Iterator (const Iterator &other)
378  : parent(other.parent), ptr(other.ptr), step(other.step) {}
379 
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
383  {
384  void *newptr = static_cast<char*>(ptr) + (n * step);
385  return Iterator(parent, newptr, step);
386  }
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
390  {
391  void *newptr = static_cast<char*>(ptr) - (n * step);
392  return Iterator(parent, newptr, step);
393  }
394 
395  ptrdiff_t operator- (const Iterator &other) const
396  {
397  const ptrdiff_t difference = static_cast<char*>(ptr) - static_cast<char*>(other.ptr);
398  return difference / step;
399  }
400 
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); }
405 
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)); }
410  };
411 
416  : slope(1.0), intercept(0.0), dataPtr(NULL), _datatype(DT_NONE), handler(NULL), _length(0), owner(false) {}
417 
427  NiftiImageData (void *data, const size_t length, const int datatype, const double slope = 1.0, const double intercept = 0.0)
428  {
429  init(data, length, datatype, slope, intercept);
430  }
431 
436  NiftiImageData (nifti_image *image)
437  {
438  if (image == NULL)
439  init(NULL, 0, DT_NONE, 0.0, 0.0, false);
440  else
441  init(image->data, image->nvox, image->datatype, static_cast<double>(image->scl_slope), static_cast<double>(image->scl_inter), false);
442  }
443 
451  NiftiImageData (const NiftiImageData &source, const int datatype = DT_NONE)
452  {
453  init(NULL, source.length(), datatype == DT_NONE ? source.datatype() : datatype, source.slope, source.intercept);
454 
455  if (datatype == DT_NONE || datatype == source.datatype())
456  memcpy(dataPtr, source.dataPtr, source.totalBytes());
457  else
458  {
459  calibrateFrom(source);
460  std::copy(source.begin(), source.end(), this->begin());
461  }
462  }
463 
470  template <class InputIterator>
471  NiftiImageData (InputIterator from, InputIterator to, const int datatype)
472  {
473  const size_t length = static_cast<size_t>(std::distance(from, to));
474  init(NULL, length, datatype, 1.0, 0.0);
475  std::copy(from, to, this->begin());
476  }
477 
481  virtual ~NiftiImageData ()
482  {
483  delete handler;
484  if (owner)
485  free(dataPtr);
486  }
487 
494  {
495  if (source.dataPtr != NULL)
496  {
497  // Free the old data, if we allocated it
498  if (owner)
499  free(dataPtr);
500  init(NULL, source.length(), source.datatype(), source.slope, source.intercept);
501  memcpy(dataPtr, source.dataPtr, source.totalBytes());
502  }
503  return *this;
504  }
505 
506  void * blob () const { return dataPtr; }
507  int datatype () const { return _datatype; }
508  size_t length () const { return _length; }
509  size_t size () const { return _length; }
512  size_t bytesPerPixel () const { return (handler == NULL ? 0 : handler->size()); }
513 
515  size_t totalBytes () const { return _length * bytesPerPixel(); }
516 
521  bool isEmpty () const { return (dataPtr == NULL); }
522 
528  bool isScaled () const { return (slope != 0.0 && (slope != 1.0 || intercept != 0.0)); }
529 
534  bool isComplex () const { return (_datatype == DT_COMPLEX64 || _datatype == DT_COMPLEX128); }
535 
541  bool isFloatingPoint () const { return (_datatype == DT_FLOAT32 || _datatype == DT_FLOAT64); }
542 
547  bool isInteger () const { return nifti_is_inttype(_datatype); }
548 
553  bool isRgb () const { return (_datatype == DT_RGB24 || _datatype == DT_RGBA32); }
554 
560 
565  NiftiImageData & disown () { this->owner = false; return *this; }
566 
568  const Iterator begin () const { return Iterator(*this); }
569 
571  const Iterator end () const { return Iterator(*this, static_cast<char*>(dataPtr) + totalBytes()); }
572 
574  Iterator begin () { return Iterator(*this); }
575 
577  Iterator end () { return Iterator(*this, static_cast<char*>(dataPtr) + totalBytes()); }
578 
584  const Element operator[] (const size_t i) const { return Element(*this, static_cast<char*>(dataPtr) + (i * bytesPerPixel())); }
585 
591  Element operator[] (const size_t i) { return Element(*this, static_cast<char*>(dataPtr) + (i * bytesPerPixel())); }
592 
600  void minmax (double *min, double *max) const
601  {
602  if (handler == NULL)
603  {
604  *min = 0.0;
605  *max = 0.0;
606  }
607  else
608  handler->minmax(dataPtr, _length, min, max);
609  }
610 };
611 
612 
613 // R provides an NaN (NA) value for integers
614 #ifdef USING_R
615 template <>
616 inline bool NiftiImageData::ConcreteTypeHandler<int>::hasNaN () const { return true; }
617 #endif
618 
619 
624 template <typename ElementType, int Length>
625 class Vector
626 {
627 protected:
628  ElementType elements[Length];
629 
630 public:
634  Vector (const ElementType value = 0.0)
635  {
636  std::fill(elements, elements + Length, value);
637  }
638 
642  Vector (const ElementType * source)
643  {
644  std::copy(source, source + Length, this->elements);
645  }
646 
651  {
653  for (int i=0; i<Length; i++)
654  result.elements[i] = -elements[i];
655  return result;
656  }
657 
658  const ElementType & operator[] (const size_t i) const { return elements[i]; }
659 
660  ElementType & operator[] (const size_t i) { return elements[i]; }
661 };
662 
663 
670 template <class NiftiType, typename ElementType, int Order>
672 {
673 protected:
674  ElementType elements[Order*Order];
680  NiftiType * niftiPointer () const { return (NiftiType *) elements; }
681 
685  NiftiType niftiCopy () const
686  {
687  NiftiType value;
688  std::copy(elements, elements + Order*Order, *value.m);
689  return value;
690  }
691 
692 public:
693  typedef NiftiType NativeType;
700  SquareMatrix (const ElementType value = 0.0)
701  {
702  std::fill(elements, elements + Order*Order, value);
703  }
704 
708  SquareMatrix (const ElementType * source)
709  {
710  std::copy(source, source + Order*Order, this->elements);
711  }
712 
716  SquareMatrix (const NiftiType &source)
717  {
718  std::copy(*source.m, *source.m + Order*Order, this->elements);
719  }
720 
721 #ifdef USING_R
722 
725  SquareMatrix (SEXP source)
726  {
727  Rcpp::NumericMatrix matrix(source);
728  if (matrix.cols() != Order && matrix.rows() != Order)
729  throw std::runtime_error("Matrix does not have the expected dimensions");
730  for (int i=0; i<Order; i++)
731  {
732  for (int j=0; j<Order; j++)
733  elements[j + i*Order] = matrix(i,j);
734  }
735  }
736 #endif
737 
742  operator const NiftiType () const { return niftiCopy(); }
743 
748  operator NiftiType () { return niftiCopy(); }
749 
754  const ElementType * begin () const { return elements; }
755 
760  ElementType * begin () { return elements; }
761 
765  const ElementType * end () const { return elements + Order*Order; }
766 
770  ElementType * end () { return elements + Order*Order; }
771 
775  static MatrixType eye ()
776  {
777  MatrixType matrix;
778  for (int i=0; i<Order; i++)
779  matrix.elements[i + i*Order] = 1.0;
780  return matrix;
781  }
782 
783  MatrixType inverse () const;
784  MatrixType polar () const;
785  ElementType colnorm () const;
786  ElementType rownorm () const;
787  ElementType determ () const;
788  MatrixType multiply (const MatrixType &other) const;
789  VectorType multiply (const VectorType &vec) const;
791  MatrixType operator* (const MatrixType &other) const { return multiply(other); }
792  VectorType operator* (const VectorType &vec) const { return multiply(vec); }
797  const ElementType & operator() (const int i, const int j) const { return elements[j + i*Order]; }
798 
802  ElementType & operator() (const int i, const int j) { return elements[j + i*Order]; }
803 
804 #ifdef USING_R
805 
808  operator SEXP () const
809  {
810  Rcpp::NumericMatrix result(Order, Order);
811  for (int i=0; i<Order; i++)
812  {
813  for (int j=0; j<Order; j++)
814  result(i,j) = elements[j + i*Order];
815  }
816  return result;
817  }
818 #endif
819 };
820 
821 
822 // Include matrix implementations
823 #include "RNifti/NiftiImage_matrix.h"
824 
825 
833 {
834 public:
835 #if RNIFTI_NIFTILIB_VERSION == 1
836  typedef int dim_t;
837  typedef float pixdim_t;
838  typedef float scale_t;
839 #elif RNIFTI_NIFTILIB_VERSION == 2
840  typedef int64_t dim_t;
841  typedef double pixdim_t;
842  typedef double scale_t;
843 #endif
844 
849  struct Block
850  {
851  const NiftiImage &image;
852  const int dimension;
853  const dim_t index;
862  Block (const NiftiImage &image, const int dimension, const dim_t index)
864  {
865  if (dimension != image->ndim)
866  throw std::runtime_error("Blocks must be along the last dimension in the image");
867  }
868 
877  Block & operator= (const NiftiImage &source)
878  {
879  if (source->datatype != image->datatype)
880  throw std::runtime_error("New data does not have the same datatype as the target block");
881  if (source->scl_slope != image->scl_slope || source->scl_inter != image->scl_inter)
882  throw std::runtime_error("New data does not have the same scale parameters as the target block");
883 
884  size_t blockSize = 1;
885  for (int i=1; i<dimension; i++)
886  blockSize *= image->dim[i];
887 
888  if (blockSize != source->nvox)
889  throw std::runtime_error("New data does not have the same size as the target block");
890 
891  blockSize *= image->nbyper;
892  memcpy(static_cast<char*>(image->data) + blockSize*index, source->data, blockSize);
893  return *this;
894  }
895 
901  {
902  if (image.isNull())
903  return NiftiImageData();
904  else
905  {
906  size_t blockSize = 1;
907  for (int i=1; i<dimension; i++)
908  blockSize *= image->dim[i];
909  return NiftiImageData(static_cast<char*>(image->data) + blockSize * index * image->nbyper, blockSize, image->datatype, static_cast<double>(image->scl_slope), static_cast<double>(image->scl_inter));
910  }
911  }
912 
920  template <typename TargetType>
921  std::vector<TargetType> getData (const bool useSlope = true) const;
922  };
923 
930  class Xform
931  {
932  public:
933 #if RNIFTI_NIFTILIB_VERSION == 1
934  typedef float Element;
935  typedef Vector<float,4> Vector4;
936  typedef Vector<float,3> Vector3;
937  typedef SquareMatrix<mat44,float,4> Matrix;
938  typedef SquareMatrix<mat33,float,3> Submatrix;
939 #elif RNIFTI_NIFTILIB_VERSION == 2
940  typedef double Element;
941  typedef Vector<double,4> Vector4;
942  typedef Vector<double,3> Vector3;
943  typedef SquareMatrix<nifti_dmat44,double,4> Matrix;
944  typedef SquareMatrix<nifti_dmat33,double,3> Submatrix;
945 #endif
946 
947  protected:
948  Element *forward, *inverse, *qparams;
949  Matrix mat;
955  void replace (const Matrix &source);
956 
957  public:
961  Xform ()
962  : forward(NULL), inverse(NULL), qparams(NULL), mat() {}
963 
967  Xform (const Matrix &source)
968  : forward(NULL), inverse(NULL), qparams(NULL), mat(source) {}
969 
973  Xform (const Matrix::NativeType &source)
974  : forward(NULL), inverse(NULL), qparams(NULL), mat(source) {}
975 
980  Xform (Matrix::NativeType &source)
981  : forward(*source.m), inverse(NULL), qparams(NULL), mat(source) {}
982 
987  Xform (Matrix::NativeType &source, Matrix::NativeType &inverse, Element *qparams = NULL)
988  : forward(*source.m), inverse(*inverse.m), qparams(qparams), mat(source) {}
989 
990 #ifdef USING_R
991 
994  Xform (SEXP source)
995  : forward(NULL), inverse(NULL), qparams(NULL), mat(Matrix(source)) {}
996 #endif
997 
1002  operator const Matrix::NativeType () const { return mat; }
1003 
1008  operator Matrix::NativeType () { return mat; }
1009 
1013  Xform & operator= (const Xform &source)
1014  {
1015  replace(source.mat);
1016  return *this;
1017  }
1018 
1022  Xform & operator= (const Matrix &source)
1023  {
1024  replace(source);
1025  return *this;
1026  }
1027 
1031  const Matrix & matrix () const { return mat; }
1032 
1036  Submatrix submatrix () const;
1037 
1042  Submatrix rotation () const;
1043 
1049  Element handedness () const;
1050 
1054  Vector4 quaternion () const;
1055 
1059  Vector3 offset () const;
1060 
1064  Vector3 spacing () const;
1065 
1073  std::string orientation () const;
1074  };
1075 
1076 #ifdef USING_R
1077 
1083  static int sexpTypeToNiftiType (const int sexpType)
1084  {
1085  if (sexpType == INTSXP || sexpType == LGLSXP)
1086  return DT_INT32;
1087  else if (sexpType == REALSXP)
1088  return DT_FLOAT64;
1089  else if (sexpType == CPLXSXP)
1090  return DT_COMPLEX128;
1091  else
1092  throw std::runtime_error("Array elements must be numeric");
1093  }
1094 #endif
1095 
1102  static int fileVersion (const std::string &path);
1103 
1104 
1105 protected:
1106  nifti_image *image;
1107  int *refCount;
1115  void acquire (nifti_image * const image);
1116 
1121  void acquire (const NiftiImage &source)
1122  {
1123  refCount = source.refCount;
1124  acquire(source.image);
1125  }
1126 
1131  void release ();
1132 
1137  void copy (const nifti_image *source);
1138 
1143  void copy (const NiftiImage &source);
1144 
1149  void copy (const Block &source);
1150 
1151 
1152 #ifdef USING_R
1153 
1159  void initFromNiftiS4 (const Rcpp::RObject &object, const bool copyData = true);
1160 
1166  void initFromMriImage (const Rcpp::RObject &object, const bool copyData = true);
1167 
1172  void initFromList (const Rcpp::RObject &object);
1173 
1179  void initFromArray (const Rcpp::RObject &object, const bool copyData = true);
1180 
1181 #endif
1182 
1188  void initFromDims (const std::vector<dim_t> &dim, const int datatype);
1189 
1194  void updatePixdim (const std::vector<pixdim_t> &pixdim);
1195 
1200  void setPixunits (const std::vector<std::string> &pixunits);
1201 
1202 public:
1207  : image(NULL), refCount(NULL) {}
1208 
1215  NiftiImage (const NiftiImage &source, const bool copy = true)
1216  : image(NULL), refCount(NULL)
1217  {
1218  if (copy)
1219  this->copy(source);
1220  else
1221  acquire(source);
1222 #ifndef NDEBUG
1223  Rc_printf("Creating NiftiImage (v%d) with pointer %p (from NiftiImage)\n", RNIFTI_NIFTILIB_VERSION, this->image);
1224 #endif
1225  }
1226 
1231  NiftiImage (const Block &source)
1232  : image(NULL), refCount(NULL)
1233  {
1234  this->copy(source);
1235 #ifndef NDEBUG
1236  Rc_printf("Creating NiftiImage (v%d) with pointer %p (from Block)\n", RNIFTI_NIFTILIB_VERSION, this->image);
1237 #endif
1238  }
1239 
1246  NiftiImage (nifti_image * const image, const bool copy = false)
1247  : image(NULL), refCount(NULL)
1248  {
1249  if (copy)
1250  this->copy(image);
1251  else
1252  acquire(image);
1253 #ifndef NDEBUG
1254  Rc_printf("Creating NiftiImage (v%d) with pointer %p (from pointer)\n", RNIFTI_NIFTILIB_VERSION, this->image);
1255 #endif
1256  }
1257 
1263  NiftiImage (const std::vector<dim_t> &dim, const int datatype);
1264 
1270  NiftiImage (const std::vector<dim_t> &dim, const std::string &datatype);
1271 
1278  NiftiImage (const std::string &path, const bool readData = true);
1279 
1286  NiftiImage (const std::string &path, const std::vector<dim_t> &volumes);
1287 
1288 #ifdef USING_R
1289 
1299  NiftiImage (const SEXP object, const bool readData = true, const bool readOnly = false);
1300 #endif
1301 
1306  virtual ~NiftiImage () { release(); }
1307 
1311  operator const nifti_image* () const { return image; }
1312 
1316  operator nifti_image* () { return image; }
1317 
1321  const nifti_image * operator-> () const { return image; }
1322 
1326  nifti_image * operator-> () { return image; }
1327 
1333  {
1334  copy(source);
1335 #ifndef NDEBUG
1336  Rc_printf("Creating NiftiImage (v%d), with pointer %p (from NiftiImage)\n", RNIFTI_NIFTILIB_VERSION, this->image);
1337 #endif
1338  return *this;
1339  }
1340 
1346  NiftiImage & operator= (const Block &source)
1347  {
1348  copy(source);
1349 #ifndef NDEBUG
1350  Rc_printf("Creating NiftiImage (v%d) with pointer %p (from Block)\n", RNIFTI_NIFTILIB_VERSION, this->image);
1351 #endif
1352  return *this;
1353  }
1354 
1362  NiftiImage & setPersistence (const bool persistent) { return *this; }
1363 
1368  bool isNull () const { return (image == NULL); }
1369 
1374  bool isShared () const { return (refCount != NULL && *refCount > 1); }
1375 
1382  bool isPersistent () const { return false; }
1383 
1389  bool isDataScaled () const { return (image != NULL && image->scl_slope != 0.0 && (image->scl_slope != 1.0 || image->scl_inter != 0.0)); }
1390 
1395  int nDims () const
1396  {
1397  if (image == NULL)
1398  return 0;
1399  else
1400  return image->ndim;
1401  }
1402 
1407  std::vector<dim_t> dim () const
1408  {
1409  if (image == NULL)
1410  return std::vector<dim_t>();
1411  else
1412  return std::vector<dim_t>(image->dim+1, image->dim+image->ndim+1);
1413  }
1414 
1419  std::vector<pixdim_t> pixdim () const
1420  {
1421  if (image == NULL)
1422  return std::vector<pixdim_t>();
1423  else
1424  return std::vector<pixdim_t>(image->pixdim+1, image->pixdim+image->ndim+1);
1425  }
1426 
1434  {
1435  int ndim = image->ndim;
1436  while (image->dim[ndim] < 2)
1437  ndim--;
1438  image->dim[0] = image->ndim = ndim;
1439 
1440  return *this;
1441  }
1442 
1447  const NiftiImageData data () const { return NiftiImageData(image); }
1448 
1454 
1464  template <typename TargetType>
1465  std::vector<TargetType> getData (const bool useSlope = true) const;
1466 
1474  NiftiImage & changeDatatype (const int datatype, const bool useSlope = false);
1475 
1483  NiftiImage & changeDatatype (const std::string &datatype, const bool useSlope = false);
1484 
1493  template <typename SourceType>
1494  NiftiImage & replaceData (const std::vector<SourceType> &data, const int datatype = DT_NONE);
1495 
1503 
1510  {
1511 #if RNIFTI_NIFTILIB_VERSION == 1
1512  nifti_image_unload(image);
1513 #elif RNIFTI_NIFTILIB_VERSION == 2
1514  nifti2_image_unload(image);
1515 #endif
1516  return *this;
1517  }
1518 
1525  NiftiImage & rescale (const std::vector<pixdim_t> &scales);
1526 
1535  NiftiImage & reorient (const int i, const int j, const int k);
1536 
1545  NiftiImage & reorient (const std::string &orientation);
1546 
1547 #ifdef USING_R
1548 
1553  NiftiImage & update (const Rcpp::RObject &object);
1554 #endif
1555 
1562  const Xform xform (const bool preferQuaternion = true) const;
1563 
1568  const Xform qform () const { return (image == NULL ? Xform() : Xform(image->qto_xyz)); }
1569 
1574  Xform qform () { return (image == NULL ? Xform() : Xform(image->qto_xyz, image->qto_ijk, &image->quatern_b)); }
1575 
1580  const Xform sform () const { return (image == NULL ? Xform() : Xform(image->sto_xyz)); }
1581 
1586  Xform sform () { return (image == NULL ? Xform() : Xform(image->sto_xyz, image->sto_ijk)); }
1587 
1592  dim_t nBlocks () const { return (image == NULL ? 0 : image->dim[image->ndim]); }
1593 
1601  const Block block (const int i) const { return Block(*this, nDims(), i); }
1602 
1610  Block block (const int i) { return Block(*this, nDims(), i); }
1611 
1617  const Block slice (const int i) const { return Block(*this, 3, i); }
1618 
1624  Block slice (const int i) { return Block(*this, 3, i); }
1625 
1631  const Block volume (const int i) const { return Block(*this, 4, i); }
1632 
1638  Block volume (const int i) { return Block(*this, 4, i); }
1639 
1645  int nChannels () const
1646  {
1647  if (image == NULL)
1648  return 0;
1649  else
1650  {
1651  switch (image->datatype)
1652  {
1653  case DT_NONE: return 0;
1654  case DT_RGB24: return 3;
1655  case DT_RGBA32: return 4;
1656  default: return 1;
1657  }
1658  }
1659  }
1660 
1665  size_t nVoxels () const { return (image == NULL ? 0 : image->nvox); }
1666 
1675  std::pair<std::string,std::string> toFile (const std::string fileName, const int datatype = DT_NONE, const int filetype = -1) const;
1676 
1685  std::pair<std::string,std::string> toFile (const std::string fileName, const std::string &datatype, const int filetype = -1) const;
1686 
1687 #ifdef USING_R
1688 
1693  Rcpp::RObject toArray () const;
1694 
1700  Rcpp::RObject toPointer (const std::string label) const;
1701 
1708  Rcpp::RObject toArrayOrPointer (const bool internal, const std::string label) const;
1709 
1710 #endif
1711 
1712 };
1713 
1714 // Include image implementations
1715 #include "RNifti/NiftiImage_impl.h"
1716 
1717 } // main namespace
1718 
1719 #endif
RNifti::NiftiImage::sform
const Xform sform() const
Access the sform matrix.
Definition: NiftiImage.h:1580
RNifti::SquareMatrix::operator*
MatrixType operator*(const MatrixType &other) const
Matrix-matrix multiplication (infix shorthand)
Definition: NiftiImage.h:791
RNifti::NiftiImage::NiftiImage
NiftiImage(const std::string &path, const std::vector< dim_t > &volumes)
Initialise using a path string and sequence of required volumes.
RNifti::NiftiImage::rescale
NiftiImage & rescale(const std::vector< pixdim_t > &scales)
Rescale the image, changing its image dimensions and pixel dimensions.
RNifti::NiftiImage::release
void release()
Release the currently wrapped pointer, if it is not NULL, decrementing the reference count and releas...
RNifti::NiftiImageData::isScaled
bool isScaled() const
Determine whether the object uses data scaling.
Definition: NiftiImage.h:528
RNifti::NiftiImage::Block
Inner class referring to a subset of an image.
Definition: NiftiImage.h:850
RNifti::NiftiImage::NiftiImage
NiftiImage(const Block &source)
Initialise from a block, copying in the data.
Definition: NiftiImage.h:1231
RNifti::NiftiImage::toFile
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.
RNifti::SquareMatrix::colnorm
ElementType colnorm() const
Maximum column norm, as implemented in niftilib (Order 3 only)
RNifti::NiftiImage::dim
std::vector< dim_t > dim() const
Return the dimensions of the image.
Definition: NiftiImage.h:1407
RNifti::NiftiImageData::isComplex
bool isComplex() const
Determine whether the datatype is complex.
Definition: NiftiImage.h:534
RNifti::NiftiImageData::operator=
NiftiImageData & operator=(const NiftiImageData &source)
Copy assignment operator.
Definition: NiftiImage.h:493
RNifti::NiftiImage::Xform::Xform
Xform(Matrix::NativeType &source, Matrix::NativeType &inverse, Element *qparams=NULL)
Initialise from forward and backward matrices, and optionally quaternion parameters.
Definition: NiftiImage.h:987
RNifti::NiftiImage::image
nifti_image * image
The wrapped nifti_image pointer.
Definition: NiftiImage.h:1106
RNifti::SquareMatrix::SquareMatrix
SquareMatrix(const ElementType *source)
Initialise from a C-style array of the appropriate type and length.
Definition: NiftiImage.h:708
RNifti::NiftiImageData::Iterator
Iterator type for NiftiImageData, with Element as its value type.
Definition: NiftiImage.h:352
RNifti::SquareMatrix::operator()
const ElementType & operator()(const int i, const int j) const
Indexing operator.
Definition: NiftiImage.h:797
RNifti::SquareMatrix::SquareMatrix
SquareMatrix(SEXP source)
Initialise from an R object representing a numeric matrix.
Definition: NiftiImage.h:725
RNifti::NiftiImage::qform
Xform qform()
Access the qform matrix.
Definition: NiftiImage.h:1574
RNifti::NiftiImage::block
Block block(const int i)
Extract a block from the image.
Definition: NiftiImage.h:1610
RNifti::NiftiImage::volume
const Block volume(const int i) const
Extract a volume block from a 4D image.
Definition: NiftiImage.h:1631
RNifti::NiftiImage::Xform::Xform
Xform(const Matrix::NativeType &source)
Initialise from a constant NIfTI mat44 or dmat44.
Definition: NiftiImage.h:973
RNifti::NiftiImage::dropData
NiftiImage & dropData()
Drop the data from the image, retaining only the metadata.
Definition: NiftiImage.h:1509
RNifti::NiftiImage::refCount
int * refCount
A reference counter, shared with other objects wrapping the same pointer.
Definition: NiftiImage.h:1107
RNifti::NiftiImage::Xform::rotation
Submatrix rotation() const
Obtain the 3x3 rotation matrix from the xform matrix, with scale and skew components removed.
RNifti::NiftiImage::Xform::orientation
std::string orientation() const
Obtain the approximate orientation of the image's coordinate frame, as a three-character string consi...
RNifti::NiftiImageData::Element::Element
Element(const NiftiImageData &parent, void *ptr=NULL)
Primary constructor.
Definition: NiftiImage.h:279
RNifti::NiftiImage::operator=
NiftiImage & operator=(const NiftiImage &source)
Copy assignment operator, which copies from its argument.
Definition: NiftiImage.h:1332
RNifti::NiftiImage::NiftiImage
NiftiImage(const std::string &path, const bool readData=true)
Initialise using a path string.
RNifti::NiftiImage::NiftiImage
NiftiImage(const NiftiImage &source, const bool copy=true)
Copy constructor.
Definition: NiftiImage.h:1215
RNifti::NiftiImage::changeDatatype
NiftiImage & changeDatatype(const int datatype, const bool useSlope=false)
Change the datatype of the image, casting the pixel data if present.
RNifti::NiftiImageData::NiftiImageData
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
RNifti::NiftiImage::Xform::spacing
Vector3 spacing() const
Obtain the pixel spacing of the image in each spatial dimension.
RNifti::NiftiImage::updatePixdim
void updatePixdim(const std::vector< pixdim_t > &pixdim)
Modify the pixel dimensions, and potentially the xform matrices to match.
RNifti::NiftiImageData::TypeHandler
Abstract inner class defining the type-specific functions required in concrete subclasses.
Definition: NiftiImage.h:81
RNifti::NiftiImageData::end
const Iterator end() const
Obtain a constant iterator corresponding to the end of the blob.
Definition: NiftiImage.h:571
RNifti::Vector::operator-
Vector< ElementType, Length > operator-() const
Unary negation operator, which reverses the signs of all elements.
Definition: NiftiImage.h:650
RNifti::NiftiImage
Thin wrapper around a C-style nifti_image struct that allows C++-style destruction.
Definition: NiftiImage.h:833
RNifti::NiftiImage::Xform::offset
Vector3 offset() const
Obtain the translation component of the xform matrix.
RNifti::rgba32_t
Simple RGB(A) type encapsulating an 8-bit colour value with optional opacity, which can also be set o...
Definition: NiftiImage.h:54
RNifti::NiftiImageData::NiftiImageData
NiftiImageData(InputIterator from, InputIterator to, const int datatype)
Iterator-based constructor.
Definition: NiftiImage.h:471
RNifti::NiftiImageData::isFloatingPoint
bool isFloatingPoint() const
Determine whether the datatype is floating point.
Definition: NiftiImage.h:541
RNifti::SquareMatrix::multiply
VectorType multiply(const VectorType &vec) const
Matrix-vector multiplication.
RNifti::NiftiImageData::size
size_t size() const
Return the number of elements in the data.
Definition: NiftiImage.h:509
RNifti::SquareMatrix::polar
MatrixType polar() const
Polar decomposition, as implemented in niftilib (Order 3 only)
RNifti::NiftiImage::isNull
bool isNull() const
Determine whether or not the wrapped pointer is NULL.
Definition: NiftiImage.h:1368
RNifti::NiftiImage::copy
void copy(const nifti_image *source)
Copy the contents of a nifti_image to create a new image, acquiring the new pointer.
RNifti::NiftiImage::setPixunits
void setPixunits(const std::vector< std::string > &pixunits)
Modify the pixel dimension units.
RNifti::SquareMatrix::SquareMatrix
SquareMatrix(const NiftiType &source)
Initialise from the appropriate niftilib type.
Definition: NiftiImage.h:716
RNifti::NiftiImage::replaceData
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.
RNifti::NiftiImageData::length
size_t length() const
Return the number of elements in the data.
Definition: NiftiImage.h:508
RNifti::NiftiImageData::slope
double slope
The slope term used to scale data values.
Definition: NiftiImage.h:73
RNifti::NiftiImage::slice
Block slice(const int i)
Extract a slice block from a 3D image.
Definition: NiftiImage.h:1624
RNifti::NiftiImage::toPointer
Rcpp::RObject toPointer(const std::string label) const
Create an internal image to pass back to R.
RNifti::NiftiImageData::operator[]
const Element operator[](const size_t i) const
Indexing operator, returning a constant element.
Definition: NiftiImage.h:584
RNifti::SquareMatrix::eye
static MatrixType eye()
Construct an identity matrix of the appropriate size.
Definition: NiftiImage.h:775
RNifti::NiftiImage::acquire
void acquire(const NiftiImage &source)
Acquire the same pointer as another NiftiImage, incrementing the shared reference count.
Definition: NiftiImage.h:1121
RNifti::NiftiImageData::NiftiImageData
NiftiImageData(const NiftiImageData &source, const int datatype=DT_NONE)
Copy constructor with optional type conversion.
Definition: NiftiImage.h:451
RNifti::NiftiImage::isShared
bool isShared() const
Determine whether the wrapped pointer is shared with another NiftiImage.
Definition: NiftiImage.h:1374
RNifti::NiftiImageData::intercept
double intercept
The intercept term used to scale data values.
Definition: NiftiImage.h:74
RNifti::NiftiImage::nBlocks
dim_t nBlocks() const
Return the number of blocks in the image.
Definition: NiftiImage.h:1592
RNifti::NiftiImage::Xform::matrix
const Matrix & matrix() const
Access the xform matrix as an immutable SquareMatrix object.
Definition: NiftiImage.h:1031
RNifti::NiftiImageData::end
Iterator end()
Obtain a mutable iterator corresponding to the end of the blob.
Definition: NiftiImage.h:577
RNifti::NiftiImage::toArrayOrPointer
Rcpp::RObject toArrayOrPointer(const bool internal, const std::string label) const
A conditional method that calls either toArray or toPointer.
RNifti::NiftiImage::toFile
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.
RNifti::NiftiImage::reorient
NiftiImage & reorient(const std::string &orientation)
Reorient the image by permuting dimensions and potentially reversing some.
RNifti::SquareMatrix::rownorm
ElementType rownorm() const
Maximum row norm, as implemented in niftilib (Order 3 only)
RNifti::NiftiImage::Xform::Xform
Xform(const Matrix &source)
Initialise from a 4x4 SquareMatrix.
Definition: NiftiImage.h:967
RNifti::NiftiImage::getData
std::vector< TargetType > getData(const bool useSlope=true) const
Extract a vector of data from the image, casting it to any required element type.
RNifti::NiftiImageData::Element
Inner class representing a single element in the data blob.
Definition: NiftiImage.h:267
RNifti::NiftiImage::Xform::submatrix
Submatrix submatrix() const
Obtain the upper left 3x3 submatrix from the xform matrix.
RNifti::NiftiImage::Xform::handedness
Element handedness() const
Returns the qfac value, which should be 1 where the xform matrix represents a right-handed coordinate...
RNifti::NiftiImage::update
NiftiImage & update(const Rcpp::RObject &object)
Update the image from an R array.
RNifti::NiftiImage::Xform::quaternion
Vector4 quaternion() const
Obtain the quaternion representation of the xform's rotation component.
RNifti::NiftiImage::data
const NiftiImageData data() const
Obtain the pixel data within the image.
Definition: NiftiImage.h:1447
RNifti::NiftiImage::reorient
NiftiImage & reorient(const int i, const int j, const int k)
Reorient the image by permuting dimensions and potentially reversing some.
RNifti::NiftiImageData::_length
size_t _length
The number of data elements in the blob.
Definition: NiftiImage.h:203
RNifti::NiftiImageData::isRgb
bool isRgb() const
Determine whether the datatype corresponds to an RGB type.
Definition: NiftiImage.h:553
RNifti::SquareMatrix::inverse
MatrixType inverse() const
Matrix inverse.
RNifti::SquareMatrix::NativeType
NiftiType NativeType
The niftilib structure type corresponding to this matrix.
Definition: NiftiImage.h:693
RNifti::NiftiImage::NiftiImage
NiftiImage(nifti_image *const image, const bool copy=false)
Initialise using an existing nifti_image pointer.
Definition: NiftiImage.h:1246
RNifti::SquareMatrix::MatrixType
SquareMatrix< NiftiType, ElementType, Order > MatrixType
Type alias for the current specialisation.
Definition: NiftiImage.h:694
RNifti::NiftiImageData
Wrapper class encapsulating a NIfTI data blob, with responsibility for handling data scaling and poly...
Definition: NiftiImage.h:71
RNifti::NiftiImage::toArray
Rcpp::RObject toArray() const
Create an R array from the image.
RNifti::NiftiImage::Block::data
NiftiImageData data() const
Obtain the data within the block.
Definition: NiftiImage.h:900
RNifti::NiftiImage::block
const Block block(const int i) const
Extract a block from the image.
Definition: NiftiImage.h:1601
RNifti::Vector
A simple object-oriented wrapper around a fixed-length array.
Definition: NiftiImage.h:626
RNifti::NiftiImageData::isInteger
bool isInteger() const
Determine whether the datatype is an integer type.
Definition: NiftiImage.h:547
RNifti::SquareMatrix::niftiPointer
NiftiType * niftiPointer() const
Obtain a pointer to a NIfTI-style mat44 or dmat44 encapsulating the same data as this object.
Definition: NiftiImage.h:680
RNifti::SquareMatrix::SquareMatrix
SquareMatrix(const ElementType value=0.0)
Initialise with a fixed element value, defaulting to zero.
Definition: NiftiImage.h:700
RNifti::NiftiImage::setPersistence
NiftiImage & setPersistence(const bool persistent)
Mark the image as persistent, so that it can be passed back to R.
Definition: NiftiImage.h:1362
RNifti::NiftiImage::replaceData
NiftiImage & replaceData(const NiftiImageData &data)
Replace the pixel data in the image with the contents of a NiftiImageData object.
RNifti::NiftiImage::volume
Block volume(const int i)
Extract a volume block from a 4D image.
Definition: NiftiImage.h:1638
RNifti::NiftiImageData::unscaled
NiftiImageData unscaled() const
Return a similar object to the callee, but with the slope and intercept values reset.
Definition: NiftiImage.h:559
RNifti::NiftiImage::~NiftiImage
virtual ~NiftiImage()
Destructor which decrements the reference counter, and releases the wrapped pointer if the counter dr...
Definition: NiftiImage.h:1306
RNifti::Vector::Vector
Vector(const ElementType value=0.0)
Initialise with a fixed element value, defaulting to zero.
Definition: NiftiImage.h:634
RNifti::NiftiImage::changeDatatype
NiftiImage & changeDatatype(const std::string &datatype, const bool useSlope=false)
Change the datatype of the image, casting the pixel data if present.
RNifti::NiftiImage::Xform::qparams
Element * qparams
Pointers to linked C-style arrays.
Definition: NiftiImage.h:948
RNifti::NiftiImageData::disown
NiftiImageData & disown()
Disown the data blob, removing responsibility for freeing it upon destruction.
Definition: NiftiImage.h:565
RNifti::NiftiImage::isDataScaled
bool isDataScaled() const
Determine whether nontrivial scale and slope parameters are set.
Definition: NiftiImage.h:1389
RNifti::NiftiImage::Xform::mat
Matrix mat
The full xform matrix underpinning this object.
Definition: NiftiImage.h:949
RNifti::NiftiImage::acquire
void acquire(nifti_image *const image)
Acquire the specified pointer to a nifti_image struct, taking (possibly shared) responsibility for fr...
RNifti::NiftiImageData::begin
Iterator begin()
Obtain a mutable iterator corresponding to the start of the blob.
Definition: NiftiImage.h:574
RNifti::NiftiImage::xform
const Xform xform(const bool preferQuaternion=true) const
Obtain an xform matrix, indicating the orientation of the image.
RNifti::NiftiImageData::ConcreteTypeHandler
Concrete inner class template defining behaviour specific to individual data types.
Definition: NiftiImage.h:101
RNifti::NiftiImage::Block::index
const dim_t index
The location along dimension.
Definition: NiftiImage.h:853
RNifti::NiftiImageData::createHandler
TypeHandler * createHandler()
Create a concrete type handler appropriate to the datatype code stored with the data.
Definition: NiftiImage.h:173
RNifti::rgba32_t::ValueType
Definition: NiftiImage.h:55
RNifti::NiftiImage::fileVersion
static int fileVersion(const std::string &path)
Get the NIfTI format version used by the file at the specified path.
RNifti::NiftiImage::Xform::Xform
Xform()
Default constructor.
Definition: NiftiImage.h:961
RNifti::NiftiImage::Block::getData
std::vector< TargetType > getData(const bool useSlope=true) const
Extract a vector of data from a block, casting it to any required element type.
RNifti::NiftiImage::NiftiImage
NiftiImage()
Default constructor.
Definition: NiftiImage.h:1206
RNifti::NiftiImage::operator->
const nifti_image * operator->() const
Allows a NiftiImage object to be treated as a pointer to a const nifti_image.
Definition: NiftiImage.h:1321
RNifti::NiftiImage::Xform::replace
void replace(const Matrix &source)
Replace the current matrix with a new one.
RNifti::NiftiImageData::dataPtr
void * dataPtr
Opaque pointer to the underlying data blob.
Definition: NiftiImage.h:200
RNifti::NiftiImageData::datatype
int datatype() const
Return stored datatype code.
Definition: NiftiImage.h:507
RNifti::NiftiImage::Xform::operator=
Xform & operator=(const Xform &source)
Copy assignment operator, taking an Xform and replacing linked data.
Definition: NiftiImage.h:1013
RNifti::NiftiImage::Block::operator=
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:877
RNifti::NiftiImageData::handler
TypeHandler * handler
Type handler, which is created to match the datatype.
Definition: NiftiImage.h:202
RNifti::NiftiImage::nVoxels
size_t nVoxels() const
Return the number of voxels in the image.
Definition: NiftiImage.h:1665
RNifti::SquareMatrix::multiply
MatrixType multiply(const MatrixType &other) const
Matrix-matrix multiplication.
RNifti::NiftiImageData::totalBytes
size_t totalBytes() const
Return the total size of the data blob, in bytes.
Definition: NiftiImage.h:515
RNifti::SquareMatrix::VectorType
Vector< ElementType, Order > VectorType
Type of vectors for which this matrix is a linear operator.
Definition: NiftiImage.h:695
RNifti::NiftiImageData::calibrateFrom
void calibrateFrom(const NiftiImageData &data)
Update the slope and intercept to cover the range of another data object.
Definition: NiftiImage.h:242
RNifti::NiftiImage::initFromMriImage
void initFromMriImage(const Rcpp::RObject &object, const bool copyData=true)
Initialise the object from a reference object of class "MriImage".
RNifti::SquareMatrix::elements
ElementType elements[Order *Order]
The underlying raw data elements, stored row-major for consistency with niftilib.
Definition: NiftiImage.h:674
RNifti::NiftiImageData::bytesPerPixel
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
RNifti::NiftiImageData::init
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
RNifti::NiftiImage::Xform
Inner class representing an xform matrix, which indicates the orientation and other spatial propertie...
Definition: NiftiImage.h:931
RNifti::SquareMatrix::end
ElementType * end()
Return a pointer/iterator to the end of the data.
Definition: NiftiImage.h:770
RNifti::NiftiImageData::owner
bool owner
An indicator of whether this object is responsible for cleaning up the data.
Definition: NiftiImage.h:204
RNifti::SquareMatrix
Class representing a numeric square matrix of a fixed order.
Definition: NiftiImage.h:672
RNifti::NiftiImage::Block::image
const NiftiImage & image
The parent image.
Definition: NiftiImage.h:851
RNifti::NiftiImage::Xform::Xform
Xform(SEXP source)
Initialise from an R numeric matrix object.
Definition: NiftiImage.h:994
RNifti::NiftiImageData::NiftiImageData
NiftiImageData(nifti_image *image)
Convenience constructor for a nifti_image.
Definition: NiftiImage.h:436
RNifti::NiftiImageData::blob
void * blob() const
Return an opaque pointer to the blob.
Definition: NiftiImage.h:506
RNifti::NiftiImage::NiftiImage
NiftiImage(const std::vector< dim_t > &dim, const std::string &datatype)
Initialise from basic metadata, allocating and zeroing pixel data.
RNifti::NiftiImage::pixdim
std::vector< pixdim_t > pixdim() const
Return the dimensions of the pixels or voxels in the image.
Definition: NiftiImage.h:1419
RNifti::NiftiImageData::minmax
void minmax(double *min, double *max) const
Calculate the minimum and maximum values in the blob, as doubles.
Definition: NiftiImage.h:600
RNifti::SquareMatrix::determ
ElementType determ() const
Matrix determinant, as implemented in niftilib (Order 3 only)
RNifti::NiftiImage::Xform::Xform
Xform(Matrix::NativeType &source)
Initialise from a NIfTI mat44 or dmat44.
Definition: NiftiImage.h:980
RNifti::NiftiImage::nDims
int nDims() const
Return the number of dimensions in the image.
Definition: NiftiImage.h:1395
RNifti::SquareMatrix::begin
ElementType * begin()
Return a pointer/iterator to the beginning of the data.
Definition: NiftiImage.h:760
RNifti::NiftiImageData::Iterator::Iterator
Iterator(const Iterator &other)
Copy constructor.
Definition: NiftiImage.h:377
RNifti::NiftiImage::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 avail...
RNifti::NiftiImageData::Iterator::Iterator
Iterator(const NiftiImageData &parent, void *ptr=NULL, const size_t step=0)
Primary constructor.
Definition: NiftiImage.h:366
RNifti::NiftiImage::drop
NiftiImage & drop()
Drop unitary dimensions.
Definition: NiftiImage.h:1433
RNifti::NiftiImage::initFromNiftiS4
void initFromNiftiS4(const Rcpp::RObject &object, const bool copyData=true)
Initialise the object from an S4 object of class "nifti".
RNifti::NiftiImage::sform
Xform sform()
Access the sform matrix.
Definition: NiftiImage.h:1586
RNifti::NiftiImage::initFromArray
void initFromArray(const Rcpp::RObject &object, const bool copyData=true)
Initialise the object from an R array.
RNifti::NiftiImage::initFromList
void initFromList(const Rcpp::RObject &object)
Initialise the object from an R list with named elements, which can only contain metadata.
RNifti::NiftiImage::data
NiftiImageData data()
Obtain the pixel data within the image.
Definition: NiftiImage.h:1453
RNifti::NiftiImage::nChannels
int nChannels() const
Return the number of colour channels used by the image.
Definition: NiftiImage.h:1645
RNifti::SquareMatrix::begin
const ElementType * begin() const
Return a pointer/iterator to the beginning of the data.
Definition: NiftiImage.h:754
RNifti::NiftiImage::copy
void copy(const NiftiImage &source)
Copy the contents of another NiftiImage to create a new image, acquiring a new pointer.
RNifti::NiftiImage::qform
const Xform qform() const
Access the qform matrix.
Definition: NiftiImage.h:1568
RNifti::NiftiImage::Block::dimension
const int dimension
The dimension along which the block applies (which should be the last)
Definition: NiftiImage.h:852
RNifti::NiftiImage::Block::Block
Block(const NiftiImage &image, const int dimension, const dim_t index)
Standard constructor for this class.
Definition: NiftiImage.h:862
RNifti::NiftiImage::slice
const Block slice(const int i) const
Extract a slice block from a 3D image.
Definition: NiftiImage.h:1617
RNifti::NiftiImage::isPersistent
bool isPersistent() const
Determine whether or not the image is marked as persistent.
Definition: NiftiImage.h:1382
RNifti::NiftiImageData::Element::operator=
Element & operator=(const SourceType &value)
Copy assignment operator.
RNifti::NiftiImageData::~NiftiImageData
virtual ~NiftiImageData()
Destructor which frees the type handler, and the data blob if it is owned by this object.
Definition: NiftiImage.h:481
RNifti::NiftiImageData::isEmpty
bool isEmpty() const
Determine whether or not the object is empty.
Definition: NiftiImage.h:521
RNifti::NiftiImageData::begin
const Iterator begin() const
Obtain a constant iterator corresponding to the start of the blob.
Definition: NiftiImage.h:568
RNifti::NiftiImageData::NiftiImageData
NiftiImageData()
Default constructor, creating an empty data object.
Definition: NiftiImage.h:415
RNifti::SquareMatrix::niftiCopy
NiftiType niftiCopy() const
Copy the data elements into a new NIfTI-style mat44 or dmat44.
Definition: NiftiImage.h:685
RNifti::SquareMatrix::end
const ElementType * end() const
Return a pointer/iterator to the end of the data.
Definition: NiftiImage.h:765
RNifti::NiftiImage::initFromDims
void initFromDims(const std::vector< dim_t > &dim, const int datatype)
Initialise an empty object from basic metadata.
RNifti::NiftiImage::copy
void copy(const Block &source)
Copy the contents of a Block to create a new image, acquiring a new pointer.
RNifti::NiftiImage::NiftiImage
NiftiImage(const std::vector< dim_t > &dim, const int datatype)
Initialise from basic metadata, allocating and zeroing pixel data.
RNifti::NiftiImageData::_datatype
int _datatype
Datatype code indicating the actual type of the elements.
Definition: NiftiImage.h:201
RNifti::NiftiImage::sexpTypeToNiftiType
static int sexpTypeToNiftiType(const int sexpType)
Convert between R SEXP object type and nifti_image datatype codes.
Definition: NiftiImage.h:1083
RNifti::Vector::Vector
Vector(const ElementType *source)
Initialise from a C-style array of the appropriate type and length.
Definition: NiftiImage.h:642