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  const ElementType *castSource = (const ElementType *) *source.m;
719  std::copy(castSource, castSource + Order*Order, this->elements);
720  }
721 
722 #ifdef USING_R
723 
726  SquareMatrix (SEXP source)
727  {
728  Rcpp::NumericMatrix matrix(source);
729  if (matrix.cols() != Order && matrix.rows() != Order)
730  throw std::runtime_error("Matrix does not have the expected dimensions");
731  for (int i=0; i<Order; i++)
732  {
733  for (int j=0; j<Order; j++)
734  elements[j + i*Order] = matrix(i,j);
735  }
736  }
737 #endif
738 
743  operator const NiftiType () const { return niftiCopy(); }
744 
749  operator NiftiType () { return niftiCopy(); }
750 
755  const ElementType * begin () const { return elements; }
756 
761  ElementType * begin () { return elements; }
762 
766  const ElementType * end () const { return elements + Order*Order; }
767 
771  ElementType * end () { return elements + Order*Order; }
772 
776  static MatrixType eye ()
777  {
778  MatrixType matrix;
779  for (int i=0; i<Order; i++)
780  matrix.elements[i + i*Order] = 1.0;
781  return matrix;
782  }
783 
784  MatrixType inverse () const;
785  MatrixType polar () const;
786  ElementType colnorm () const;
787  ElementType rownorm () const;
788  ElementType determ () const;
789  MatrixType multiply (const MatrixType &other) const;
790  VectorType multiply (const VectorType &vec) const;
792  MatrixType operator* (const MatrixType &other) const { return multiply(other); }
793  VectorType operator* (const VectorType &vec) const { return multiply(vec); }
798  const ElementType & operator() (const int i, const int j) const { return elements[j + i*Order]; }
799 
803  ElementType & operator() (const int i, const int j) { return elements[j + i*Order]; }
804 
805 #ifdef USING_R
806 
809  operator SEXP () const
810  {
811  Rcpp::NumericMatrix result(Order, Order);
812  for (int i=0; i<Order; i++)
813  {
814  for (int j=0; j<Order; j++)
815  result(i,j) = elements[j + i*Order];
816  }
817  return result;
818  }
819 #endif
820 };
821 
822 
823 // Include matrix implementations
824 #include "RNifti/NiftiImage_matrix.h"
825 
826 
834 {
835 public:
836 #if RNIFTI_NIFTILIB_VERSION == 1
837  typedef int dim_t;
838  typedef float pixdim_t;
839  typedef float scale_t;
840 #elif RNIFTI_NIFTILIB_VERSION == 2
841  typedef int64_t dim_t;
842  typedef double pixdim_t;
843  typedef double scale_t;
844 #endif
845 
850  struct Block
851  {
852  const NiftiImage &image;
853  const int dimension;
854  const dim_t index;
863  Block (const NiftiImage &image, const int dimension, const dim_t index)
865  {
866  if (dimension != image->ndim)
867  throw std::runtime_error("Blocks must be along the last dimension in the image");
868  }
869 
878  Block & operator= (const NiftiImage &source)
879  {
880  if (source->datatype != image->datatype)
881  throw std::runtime_error("New data does not have the same datatype as the target block");
882  if (source->scl_slope != image->scl_slope || source->scl_inter != image->scl_inter)
883  throw std::runtime_error("New data does not have the same scale parameters as the target block");
884 
885  size_t blockSize = 1;
886  for (int i=1; i<dimension; i++)
887  blockSize *= image->dim[i];
888 
889  if (blockSize != source->nvox)
890  throw std::runtime_error("New data does not have the same size as the target block");
891 
892  blockSize *= image->nbyper;
893  memcpy(static_cast<char*>(image->data) + blockSize*index, source->data, blockSize);
894  return *this;
895  }
896 
902  {
903  if (image.isNull())
904  return NiftiImageData();
905  else
906  {
907  size_t blockSize = 1;
908  for (int i=1; i<dimension; i++)
909  blockSize *= image->dim[i];
910  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));
911  }
912  }
913 
921  template <typename TargetType>
922  std::vector<TargetType> getData (const bool useSlope = true) const;
923  };
924 
931  class Xform
932  {
933  public:
934 #if RNIFTI_NIFTILIB_VERSION == 1
935  typedef float Element;
936  typedef Vector<float,4> Vector4;
937  typedef Vector<float,3> Vector3;
938  typedef SquareMatrix<mat44,float,4> Matrix;
939  typedef SquareMatrix<mat33,float,3> Submatrix;
940 #elif RNIFTI_NIFTILIB_VERSION == 2
941  typedef double Element;
942  typedef Vector<double,4> Vector4;
943  typedef Vector<double,3> Vector3;
944  typedef SquareMatrix<nifti_dmat44,double,4> Matrix;
945  typedef SquareMatrix<nifti_dmat33,double,3> Submatrix;
946 #endif
947 
948  protected:
949  Element *forward, *inverse, *qparams;
950  Matrix mat;
956  void replace (const Matrix &source);
957 
958  public:
962  Xform ()
963  : forward(NULL), inverse(NULL), qparams(NULL), mat() {}
964 
968  Xform (const Matrix &source)
969  : forward(NULL), inverse(NULL), qparams(NULL), mat(source) {}
970 
974  Xform (const Matrix::NativeType &source)
975  : forward(NULL), inverse(NULL), qparams(NULL), mat(source) {}
976 
981  Xform (Matrix::NativeType &source)
982  : forward(*source.m), inverse(NULL), qparams(NULL), mat(source) {}
983 
988  Xform (Matrix::NativeType &source, Matrix::NativeType &inverse, Element *qparams = NULL)
989  : forward(*source.m), inverse(*inverse.m), qparams(qparams), mat(source) {}
990 
991 #ifdef USING_R
992 
995  Xform (SEXP source)
996  : forward(NULL), inverse(NULL), qparams(NULL), mat(Matrix(source)) {}
997 #endif
998 
1003  operator const Matrix::NativeType () const { return mat; }
1004 
1009  operator Matrix::NativeType () { return mat; }
1010 
1014  Xform & operator= (const Xform &source)
1015  {
1016  replace(source.mat);
1017  return *this;
1018  }
1019 
1023  Xform & operator= (const Matrix &source)
1024  {
1025  replace(source);
1026  return *this;
1027  }
1028 
1032  const Matrix & matrix () const { return mat; }
1033 
1037  Submatrix submatrix () const;
1038 
1043  Submatrix rotation () const;
1044 
1050  Element handedness () const;
1051 
1055  Vector4 quaternion () const;
1056 
1060  Vector3 offset () const;
1061 
1065  Vector3 spacing () const;
1066 
1074  std::string orientation () const;
1075  };
1076 
1077 #ifdef USING_R
1078 
1084  static int sexpTypeToNiftiType (const int sexpType)
1085  {
1086  if (sexpType == INTSXP || sexpType == LGLSXP)
1087  return DT_INT32;
1088  else if (sexpType == REALSXP)
1089  return DT_FLOAT64;
1090  else if (sexpType == CPLXSXP)
1091  return DT_COMPLEX128;
1092  else
1093  throw std::runtime_error("Array elements must be numeric");
1094  }
1095 #endif
1096 
1103  static int fileVersion (const std::string &path);
1104 
1105 
1106 protected:
1107  nifti_image *image;
1108  int *refCount;
1116  void acquire (nifti_image * const image);
1117 
1122  void acquire (const NiftiImage &source)
1123  {
1124  refCount = source.refCount;
1125  acquire(source.image);
1126  }
1127 
1132  void release ();
1133 
1138  void copy (const nifti_image *source);
1139 
1144  void copy (const NiftiImage &source);
1145 
1150  void copy (const Block &source);
1151 
1152 
1153 #ifdef USING_R
1154 
1160  void initFromNiftiS4 (const Rcpp::RObject &object, const bool copyData = true);
1161 
1167  void initFromMriImage (const Rcpp::RObject &object, const bool copyData = true);
1168 
1173  void initFromList (const Rcpp::RObject &object);
1174 
1180  void initFromArray (const Rcpp::RObject &object, const bool copyData = true);
1181 
1182 #endif
1183 
1189  void initFromDims (const std::vector<dim_t> &dim, const int datatype);
1190 
1195  void updatePixdim (const std::vector<pixdim_t> &pixdim);
1196 
1201  void setPixunits (const std::vector<std::string> &pixunits);
1202 
1203 public:
1208  : image(NULL), refCount(NULL) {}
1209 
1216  NiftiImage (const NiftiImage &source, const bool copy = true)
1217  : image(NULL), refCount(NULL)
1218  {
1219  if (copy)
1220  this->copy(source);
1221  else
1222  acquire(source);
1223 #ifndef NDEBUG
1224  Rc_printf("Creating NiftiImage (v%d) with pointer %p (from NiftiImage)\n", RNIFTI_NIFTILIB_VERSION, this->image);
1225 #endif
1226  }
1227 
1232  NiftiImage (const Block &source)
1233  : image(NULL), refCount(NULL)
1234  {
1235  this->copy(source);
1236 #ifndef NDEBUG
1237  Rc_printf("Creating NiftiImage (v%d) with pointer %p (from Block)\n", RNIFTI_NIFTILIB_VERSION, this->image);
1238 #endif
1239  }
1240 
1247  NiftiImage (nifti_image * const image, const bool copy = false)
1248  : image(NULL), refCount(NULL)
1249  {
1250  if (copy)
1251  this->copy(image);
1252  else
1253  acquire(image);
1254 #ifndef NDEBUG
1255  Rc_printf("Creating NiftiImage (v%d) with pointer %p (from pointer)\n", RNIFTI_NIFTILIB_VERSION, this->image);
1256 #endif
1257  }
1258 
1264  NiftiImage (const std::vector<dim_t> &dim, const int datatype);
1265 
1271  NiftiImage (const std::vector<dim_t> &dim, const std::string &datatype);
1272 
1279  NiftiImage (const std::string &path, const bool readData = true);
1280 
1287  NiftiImage (const std::string &path, const std::vector<dim_t> &volumes);
1288 
1289 #ifdef USING_R
1290 
1300  NiftiImage (const SEXP object, const bool readData = true, const bool readOnly = false);
1301 #endif
1302 
1307  virtual ~NiftiImage () { release(); }
1308 
1312  operator const nifti_image* () const { return image; }
1313 
1317  operator nifti_image* () { return image; }
1318 
1322  const nifti_image * operator-> () const { return image; }
1323 
1327  nifti_image * operator-> () { return image; }
1328 
1334  {
1335  copy(source);
1336 #ifndef NDEBUG
1337  Rc_printf("Creating NiftiImage (v%d), with pointer %p (from NiftiImage)\n", RNIFTI_NIFTILIB_VERSION, this->image);
1338 #endif
1339  return *this;
1340  }
1341 
1347  NiftiImage & operator= (const Block &source)
1348  {
1349  copy(source);
1350 #ifndef NDEBUG
1351  Rc_printf("Creating NiftiImage (v%d) with pointer %p (from Block)\n", RNIFTI_NIFTILIB_VERSION, this->image);
1352 #endif
1353  return *this;
1354  }
1355 
1363  NiftiImage & setPersistence (const bool persistent) { return *this; }
1364 
1369  bool isNull () const { return (image == NULL); }
1370 
1375  bool isShared () const { return (refCount != NULL && *refCount > 1); }
1376 
1383  bool isPersistent () const { return false; }
1384 
1390  bool isDataScaled () const { return (image != NULL && image->scl_slope != 0.0 && (image->scl_slope != 1.0 || image->scl_inter != 0.0)); }
1391 
1396  int nDims () const
1397  {
1398  if (image == NULL)
1399  return 0;
1400  else
1401  return image->ndim;
1402  }
1403 
1408  std::vector<dim_t> dim () const
1409  {
1410  if (image == NULL)
1411  return std::vector<dim_t>();
1412  else
1413  return std::vector<dim_t>(image->dim+1, image->dim+image->ndim+1);
1414  }
1415 
1420  std::vector<pixdim_t> pixdim () const
1421  {
1422  if (image == NULL)
1423  return std::vector<pixdim_t>();
1424  else
1425  return std::vector<pixdim_t>(image->pixdim+1, image->pixdim+image->ndim+1);
1426  }
1427 
1435  {
1436  int ndim = image->ndim;
1437  while (image->dim[ndim] < 2)
1438  ndim--;
1439  image->dim[0] = image->ndim = ndim;
1440 
1441  return *this;
1442  }
1443 
1448  const NiftiImageData data () const { return NiftiImageData(image); }
1449 
1455 
1465  template <typename TargetType>
1466  std::vector<TargetType> getData (const bool useSlope = true) const;
1467 
1475  NiftiImage & changeDatatype (const int datatype, const bool useSlope = false);
1476 
1484  NiftiImage & changeDatatype (const std::string &datatype, const bool useSlope = false);
1485 
1494  template <typename SourceType>
1495  NiftiImage & replaceData (const std::vector<SourceType> &data, const int datatype = DT_NONE);
1496 
1504 
1511  {
1512 #if RNIFTI_NIFTILIB_VERSION == 1
1513  nifti_image_unload(image);
1514 #elif RNIFTI_NIFTILIB_VERSION == 2
1515  nifti2_image_unload(image);
1516 #endif
1517  return *this;
1518  }
1519 
1526  NiftiImage & rescale (const std::vector<pixdim_t> &scales);
1527 
1536  NiftiImage & reorient (const int i, const int j, const int k);
1537 
1546  NiftiImage & reorient (const std::string &orientation);
1547 
1548 #ifdef USING_R
1549 
1554  NiftiImage & update (const Rcpp::RObject &object);
1555 #endif
1556 
1563  const Xform xform (const bool preferQuaternion = true) const;
1564 
1569  const Xform qform () const { return (image == NULL ? Xform() : Xform(image->qto_xyz)); }
1570 
1575  Xform qform () { return (image == NULL ? Xform() : Xform(image->qto_xyz, image->qto_ijk, &image->quatern_b)); }
1576 
1581  const Xform sform () const { return (image == NULL ? Xform() : Xform(image->sto_xyz)); }
1582 
1587  Xform sform () { return (image == NULL ? Xform() : Xform(image->sto_xyz, image->sto_ijk)); }
1588 
1593  dim_t nBlocks () const { return (image == NULL ? 0 : image->dim[image->ndim]); }
1594 
1602  const Block block (const int i) const { return Block(*this, nDims(), i); }
1603 
1611  Block block (const int i) { return Block(*this, nDims(), i); }
1612 
1618  const Block slice (const int i) const { return Block(*this, 3, i); }
1619 
1625  Block slice (const int i) { return Block(*this, 3, i); }
1626 
1632  const Block volume (const int i) const { return Block(*this, 4, i); }
1633 
1639  Block volume (const int i) { return Block(*this, 4, i); }
1640 
1646  int nChannels () const
1647  {
1648  if (image == NULL)
1649  return 0;
1650  else
1651  {
1652  switch (image->datatype)
1653  {
1654  case DT_NONE: return 0;
1655  case DT_RGB24: return 3;
1656  case DT_RGBA32: return 4;
1657  default: return 1;
1658  }
1659  }
1660  }
1661 
1666  size_t nVoxels () const { return (image == NULL ? 0 : image->nvox); }
1667 
1676  std::pair<std::string,std::string> toFile (const std::string fileName, const int datatype = DT_NONE, const int filetype = -1) const;
1677 
1686  std::pair<std::string,std::string> toFile (const std::string fileName, const std::string &datatype, const int filetype = -1) const;
1687 
1688 #ifdef USING_R
1689 
1694  Rcpp::RObject toArray () const;
1695 
1701  Rcpp::RObject toPointer (const std::string label) const;
1702 
1709  Rcpp::RObject toArrayOrPointer (const bool internal, const std::string label) const;
1710 
1711 #endif
1712 
1713 };
1714 
1715 // Include image implementations
1716 #include "RNifti/NiftiImage_impl.h"
1717 
1718 } // main namespace
1719 
1720 #endif
RNifti::NiftiImage::sform
const Xform sform() const
Access the sform matrix.
Definition: NiftiImage.h:1581
RNifti::SquareMatrix::operator*
MatrixType operator*(const MatrixType &other) const
Matrix-matrix multiplication (infix shorthand)
Definition: NiftiImage.h:792
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:851
RNifti::NiftiImage::NiftiImage
NiftiImage(const Block &source)
Initialise from a block, copying in the data.
Definition: NiftiImage.h:1232
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:1408
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:988
RNifti::NiftiImage::image
nifti_image * image
The wrapped nifti_image pointer.
Definition: NiftiImage.h:1107
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:798
RNifti::SquareMatrix::SquareMatrix
SquareMatrix(SEXP source)
Initialise from an R object representing a numeric matrix.
Definition: NiftiImage.h:726
RNifti::NiftiImage::qform
Xform qform()
Access the qform matrix.
Definition: NiftiImage.h:1575
RNifti::NiftiImage::block
Block block(const int i)
Extract a block from the image.
Definition: NiftiImage.h:1611
RNifti::NiftiImage::volume
const Block volume(const int i) const
Extract a volume block from a 4D image.
Definition: NiftiImage.h:1632
RNifti::NiftiImage::Xform::Xform
Xform(const Matrix::NativeType &source)
Initialise from a constant NIfTI mat44 or dmat44.
Definition: NiftiImage.h:974
RNifti::NiftiImage::dropData
NiftiImage & dropData()
Drop the data from the image, retaining only the metadata.
Definition: NiftiImage.h:1510
RNifti::NiftiImage::refCount
int * refCount
A reference counter, shared with other objects wrapping the same pointer.
Definition: NiftiImage.h:1108
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:1333
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:1216
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:834
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:1369
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:1625
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:776
RNifti::NiftiImage::acquire
void acquire(const NiftiImage &source)
Acquire the same pointer as another NiftiImage, incrementing the shared reference count.
Definition: NiftiImage.h:1122
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:1375
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:1593
RNifti::NiftiImage::Xform::matrix
const Matrix & matrix() const
Access the xform matrix as an immutable SquareMatrix object.
Definition: NiftiImage.h:1032
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:968
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:1448
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:1247
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:901
RNifti::NiftiImage::block
const Block block(const int i) const
Extract a block from the image.
Definition: NiftiImage.h:1602
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:1363
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:1639
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:1307
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:949
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:1390
RNifti::NiftiImage::Xform::mat
Matrix mat
The full xform matrix underpinning this object.
Definition: NiftiImage.h:950
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:854
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:962
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:1207
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:1322
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:1014
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:878
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:1666
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:932
RNifti::SquareMatrix::end
ElementType * end()
Return a pointer/iterator to the end of the data.
Definition: NiftiImage.h:771
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:852
RNifti::NiftiImage::Xform::Xform
Xform(SEXP source)
Initialise from an R numeric matrix object.
Definition: NiftiImage.h:995
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:1420
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:981
RNifti::NiftiImage::nDims
int nDims() const
Return the number of dimensions in the image.
Definition: NiftiImage.h:1396
RNifti::SquareMatrix::begin
ElementType * begin()
Return a pointer/iterator to the beginning of the data.
Definition: NiftiImage.h:761
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:1434
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:1587
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:1454
RNifti::NiftiImage::nChannels
int nChannels() const
Return the number of colour channels used by the image.
Definition: NiftiImage.h:1646
RNifti::SquareMatrix::begin
const ElementType * begin() const
Return a pointer/iterator to the beginning of the data.
Definition: NiftiImage.h:755
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:1569
RNifti::NiftiImage::Block::dimension
const int dimension
The dimension along which the block applies (which should be the last)
Definition: NiftiImage.h:853
RNifti::NiftiImage::Block::Block
Block(const NiftiImage &image, const int dimension, const dim_t index)
Standard constructor for this class.
Definition: NiftiImage.h:863
RNifti::NiftiImage::slice
const Block slice(const int i) const
Extract a slice block from a 3D image.
Definition: NiftiImage.h:1618
RNifti::NiftiImage::isPersistent
bool isPersistent() const
Determine whether or not the image is marked as persistent.
Definition: NiftiImage.h:1383
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:766
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:1084
RNifti::Vector::Vector
Vector(const ElementType *source)
Initialise from a C-style array of the appropriate type and length.
Definition: NiftiImage.h:642