1 #ifndef _NIFTI_IMAGE_H_ 2 #define _NIFTI_IMAGE_H_ 6 #include "niftilib/nifti1_io.h" 43 : image(image), dimension(dimension), index(index)
45 if (dimension != image->ndim)
46 throw std::runtime_error(
"Blocks must be along the last dimension in the image");
59 if (source->datatype != image->datatype)
60 throw std::runtime_error(
"New data does not have the same datatype as the target block");
64 blockSize *= image->dim[i];
66 if (blockSize != source->nvox)
67 throw std::runtime_error(
"New data does not have the same size as the target block");
69 blockSize *= image->nbyper;
70 memcpy(static_cast<char*>(image->data) + blockSize*index, source->data, blockSize);
83 if (sexpType == INTSXP || sexpType == LGLSXP)
85 else if (sexpType == REALSXP)
88 throw std::runtime_error(
"Array elements must be numeric");
99 void copy (nifti_image *
const source);
118 void initFromNiftiS4 (
const Rcpp::RObject &
object,
const bool copyData =
true);
125 void initFromMriImage (
const Rcpp::RObject &
object,
const bool copyData =
true);
138 void initFromArray (
const Rcpp::RObject &
object,
const bool copyData =
true);
150 void setPixunits (
const std::vector<std::string> &pixunits);
157 : image(NULL), persistent(false) {}
164 : image(NULL), persistent(false)
168 Rprintf(
"Creating NiftiImage with pointer %p (from NiftiImage)\n", this->image);
179 : image(NULL), persistent(false)
186 Rprintf(
"Creating NiftiImage with pointer %p (from pointer)\n", this->image);
196 NiftiImage (
const std::string &path,
const bool readData =
true)
199 this->image = nifti_image_read(path.c_str(), readData);
200 if (this->image == NULL)
201 throw std::runtime_error(
"Failed to read image from path " + path);
203 Rprintf(
"Creating NiftiImage with pointer %p (from string)\n", this->image);
212 NiftiImage (
const SEXP
object,
const bool readData =
true);
222 Rprintf(
"Freeing NiftiImage with pointer %p\n", this->image);
224 nifti_image_free(image);
231 operator nifti_image* ()
const {
return image; }
246 Rprintf(
"Creating NiftiImage with pointer %p (from NiftiImage)\n", this->image);
260 Rprintf(
"Creating NiftiImage with pointer %p (from Block)\n", this->image);
274 Rprintf(
"Setting NiftiImage with pointer %p to be persistent\n", this->image);
281 bool isNull ()
const {
return (image == NULL); }
307 int ndim = image->ndim;
308 while (image->dim[ndim] < 2)
310 image->dim[0] = image->ndim = ndim;
320 void rescale (
const std::vector<float> &scales);
326 void update (
const SEXP array);
333 mat44
xform (
const bool preferQuaternion =
true)
const;
368 void toFile (
const std::string fileName,
const short datatype)
const;
375 void toFile (
const std::string fileName,
const std::string &datatype)
const;
381 Rcpp::RObject
toArray ()
const;
388 Rcpp::RObject
toPointer (
const std::string label)
const;
396 Rcpp::RObject
toArrayOrPointer (
const bool internal,
const std::string label)
const;
408 nifti_image_free(
image);
414 image = nifti_copy_nim_info(source);
415 if (source->data != NULL)
417 size_t dataSize = nifti_get_volsize(source);
418 image->data = calloc(1, dataSize);
419 memcpy(
image->data, source->data, dataSize);
426 nifti_image *sourceStruct = source;
433 nifti_image_free(
image);
435 nifti_image *sourceStruct = source.
image;
436 if (sourceStruct == NULL)
440 image = nifti_copy_nim_info(sourceStruct);
444 nifti_update_dims_from_array(
image);
446 if (sourceStruct->data != NULL)
448 size_t blockSize = nifti_get_volsize(
image);
449 image->data = calloc(1, blockSize);
450 memcpy(
image->data, static_cast<char*>(source.
image->data) + blockSize*source.
index, blockSize);
458 nifti_1_header header;
459 header.sizeof_hdr = 348;
461 const std::vector<short> dims =
object.slot(
"dim_");
462 for (
int i=0; i<8; i++)
463 header.dim[i] = dims[i];
465 header.intent_p1 =
object.slot(
"intent_p1");
466 header.intent_p2 =
object.slot(
"intent_p2");
467 header.intent_p3 =
object.slot(
"intent_p3");
468 header.intent_code =
object.slot(
"intent_code");
470 header.datatype =
object.slot(
"datatype");
471 header.bitpix =
object.slot(
"bitpix");
473 header.slice_start =
object.slot(
"slice_start");
474 header.slice_end =
object.slot(
"slice_end");
475 header.slice_code = Rcpp::as<int>(
object.slot(
"slice_code"));
476 header.slice_duration =
object.slot(
"slice_duration");
478 const std::vector<float> pixdims =
object.slot(
"pixdim");
479 for (
int i=0; i<8; i++)
480 header.pixdim[i] = pixdims[i];
481 header.xyzt_units = Rcpp::as<int>(
object.slot(
"xyzt_units"));
483 header.vox_offset =
object.slot(
"vox_offset");
485 header.scl_slope =
object.slot(
"scl_slope");
486 header.scl_inter =
object.slot(
"scl_inter");
487 header.toffset =
object.slot(
"toffset");
489 header.cal_max =
object.slot(
"cal_max");
490 header.cal_min =
object.slot(
"cal_min");
491 header.glmax = header.glmin = 0;
493 strncpy(header.descrip, Rcpp::as<std::string>(
object.slot(
"descrip")).c_str(), 79);
494 header.descrip[79] =
'\0';
495 strncpy(header.aux_file, Rcpp::as<std::string>(
object.slot(
"aux_file")).c_str(), 23);
496 header.aux_file[23] =
'\0';
497 strncpy(header.intent_name, Rcpp::as<std::string>(
object.slot(
"intent_name")).c_str(), 15);
498 header.intent_name[15] =
'\0';
499 strncpy(header.magic, Rcpp::as<std::string>(
object.slot(
"magic")).c_str(), 3);
500 header.magic[3] =
'\0';
502 header.qform_code =
object.slot(
"qform_code");
503 header.sform_code =
object.slot(
"sform_code");
505 header.quatern_b =
object.slot(
"quatern_b");
506 header.quatern_c =
object.slot(
"quatern_c");
507 header.quatern_d =
object.slot(
"quatern_d");
508 header.qoffset_x =
object.slot(
"qoffset_x");
509 header.qoffset_y =
object.slot(
"qoffset_y");
510 header.qoffset_z =
object.slot(
"qoffset_z");
512 const std::vector<float> srow_x =
object.slot(
"srow_x");
513 const std::vector<float> srow_y =
object.slot(
"srow_y");
514 const std::vector<float> srow_z =
object.slot(
"srow_z");
515 for (
int i=0; i<4; i++)
517 header.srow_x[i] = srow_x[i];
518 header.srow_y[i] = srow_y[i];
519 header.srow_z[i] = srow_z[i];
522 if (header.datatype == DT_UINT8 || header.datatype == DT_INT16 || header.datatype == DT_INT32 || header.datatype == DT_INT8 || header.datatype == DT_UINT16 || header.datatype == DT_UINT32)
523 header.datatype = DT_INT32;
524 else if (header.datatype == DT_FLOAT32 || header.datatype == DT_FLOAT64)
525 header.datatype = DT_FLOAT64;
527 throw std::runtime_error(
"Data type is not supported");
529 this->
image = nifti_convert_nhdr2nim(header, NULL);
531 const SEXP data = PROTECT(
object.slot(
".Data"));
532 if (!copyData || Rf_length(data) <= 1)
533 this->
image->data = NULL;
536 const size_t dataSize = nifti_get_volsize(this->
image);
537 this->
image->data = calloc(1, dataSize);
538 if (header.datatype == DT_INT32)
540 Rcpp::IntegerVector intData(data);
541 std::copy(intData.begin(), intData.end(),
static_cast<int32_t*
>(this->
image->data));
545 Rcpp::DoubleVector doubleData(data);
546 std::copy(doubleData.begin(), doubleData.end(),
static_cast<double*
>(this->
image->data));
554 Rcpp::Reference mriImage(
object);
555 Rcpp::Function getXform = mriImage.field(
"getXform");
556 Rcpp::NumericMatrix
xform = getXform();
560 if (Rf_length(mriImage.field(
"tags")) > 0)
563 Rcpp::RObject data = mriImage.field(
"data");
564 if (data.inherits(
"SparseArray"))
566 Rcpp::Language call(
"as.array", data);
570 const short datatype = (Rf_isNull(data) ? DT_INT32 :
sexpTypeToNiftiType(data.sexp_type()));
572 int dims[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
573 const std::vector<int> dimVector = mriImage.field(
"imageDims");
574 const int nDims = std::min(7,
int(dimVector.size()));
577 for (
int i=0; i<
nDims; i++)
579 dims[i+1] = dimVector[i];
580 nVoxels *= dimVector[i];
583 if (this->
image == NULL)
584 this->
image = nifti_make_new_nim(dims, datatype, FALSE);
587 std::copy(dims, dims+8, this->
image->dim);
588 this->
image->datatype = datatype;
589 nifti_datatype_sizes(
image->datatype, &
image->nbyper, NULL);
592 if (copyData && !Rf_isNull(data))
595 const size_t dataSize = nVoxels *
image->nbyper;
596 this->
image->data = calloc(1, dataSize);
597 if (datatype == DT_INT32)
598 memcpy(this->
image->data, INTEGER(data), dataSize);
600 memcpy(this->
image->data, REAL(data), dataSize);
603 this->
image->data = NULL;
605 const std::vector<float> pixdimVector = mriImage.field(
"voxelDims");
606 const int pixdimLength = pixdimVector.size();
607 for (
int i=0; i<std::min(pixdimLength,nDims); i++)
608 this->
image->pixdim[i+1] = std::abs(pixdimVector[i]);
610 const std::vector<std::string> pixunitsVector = mriImage.field(
"voxelDimUnits");
613 if (xform.rows() != 4 || xform.cols() != 4)
614 this->
image->qform_code = this->
image->sform_code = 0;
618 for (
int i=0; i<4; i++)
620 for (
int j=0; j<4; j++)
621 matrix.m[i][j] = static_cast<float>(xform(i,j));
624 this->
image->qto_xyz = matrix;
625 this->
image->qto_ijk = nifti_mat44_inverse(
image->qto_xyz);
626 nifti_mat44_to_quatern(
image->qto_xyz, &
image->quatern_b, &
image->quatern_c, &
image->quatern_d, &
image->qoffset_x, &
image->qoffset_y, &
image->qoffset_z, NULL, NULL, NULL, &
image->qfac);
628 this->
image->sto_xyz = matrix;
629 this->
image->sto_ijk = nifti_mat44_inverse(
image->sto_xyz);
631 this->
image->qform_code = this->
image->sform_code = 2;
635 template <
typename TargetType>
636 inline void copyIfPresent (
const Rcpp::List &list,
const std::set<std::string> names,
const std::string &name, TargetType &target)
638 if (names.count(name) == 1)
639 target = Rcpp::as<TargetType>(list[name]);
644 inline void copyIfPresent (
const Rcpp::List &list,
const std::set<std::string> names,
const std::string &name,
char &target)
646 if (names.count(name) == 1)
647 target = static_cast<char>(Rcpp::as<int>(list[name]));
652 Rcpp::List list(
object);
653 nifti_1_header *header = nifti_make_new_header(NULL, DT_FLOAT64);
655 const Rcpp::CharacterVector _names = list.names();
656 std::set<std::string> names;
657 for (Rcpp::CharacterVector::const_iterator it=_names.begin(); it!=_names.end(); it++)
658 names.insert(Rcpp::as<std::string>(*it));
660 copyIfPresent(list, names,
"sizeof_hdr", header->sizeof_hdr);
662 copyIfPresent(list, names,
"dim_info", header->dim_info);
663 if (names.count(
"dim") == 1)
665 std::vector<short> dim = list[
"dim"];
666 for (
int i=0; i<std::min(dim.size(),size_t(8)); i++)
667 header->dim[i] = dim[i];
670 copyIfPresent(list, names,
"intent_p1", header->intent_p1);
671 copyIfPresent(list, names,
"intent_p2", header->intent_p2);
672 copyIfPresent(list, names,
"intent_p3", header->intent_p3);
673 copyIfPresent(list, names,
"intent_code", header->intent_code);
675 copyIfPresent(list, names,
"datatype", header->datatype);
676 copyIfPresent(list, names,
"bitpix", header->bitpix);
678 copyIfPresent(list, names,
"slice_start", header->slice_start);
679 if (names.count(
"pixdim") == 1)
681 std::vector<float> pixdim = list[
"pixdim"];
682 for (
int i=0; i<std::min(pixdim.size(),size_t(8)); i++)
683 header->pixdim[i] = pixdim[i];
685 copyIfPresent(list, names,
"vox_offset", header->vox_offset);
686 copyIfPresent(list, names,
"scl_slope", header->scl_slope);
687 copyIfPresent(list, names,
"scl_inter", header->scl_inter);
688 copyIfPresent(list, names,
"slice_end", header->slice_end);
689 copyIfPresent(list, names,
"slice_code", header->slice_code);
690 copyIfPresent(list, names,
"xyzt_units", header->xyzt_units);
691 copyIfPresent(list, names,
"cal_max", header->cal_max);
692 copyIfPresent(list, names,
"cal_min", header->cal_min);
693 copyIfPresent(list, names,
"slice_duration", header->slice_duration);
694 copyIfPresent(list, names,
"toffset", header->toffset);
696 if (names.count(
"descrip") == 1)
697 strcpy(header->descrip, Rcpp::as<std::string>(list[
"descrip"]).substr(0,79).c_str());
698 if (names.count(
"aux_file") == 1)
699 strcpy(header->aux_file, Rcpp::as<std::string>(list[
"aux_file"]).substr(0,23).c_str());
701 copyIfPresent(list, names,
"qform_code", header->qform_code);
702 copyIfPresent(list, names,
"sform_code", header->sform_code);
703 copyIfPresent(list, names,
"quatern_b", header->quatern_b);
704 copyIfPresent(list, names,
"quatern_c", header->quatern_c);
705 copyIfPresent(list, names,
"quatern_d", header->quatern_d);
706 copyIfPresent(list, names,
"qoffset_x", header->qoffset_x);
707 copyIfPresent(list, names,
"qoffset_y", header->qoffset_y);
708 copyIfPresent(list, names,
"qoffset_z", header->qoffset_z);
710 if (names.count(
"srow_x") == 1)
712 std::vector<float> srow_x = list[
"srow_x"];
713 for (
int i=0; i<std::min(srow_x.size(),size_t(4)); i++)
714 header->srow_x[i] = srow_x[i];
716 if (names.count(
"srow_y") == 1)
718 std::vector<float> srow_y = list[
"srow_y"];
719 for (
int i=0; i<std::min(srow_y.size(),size_t(4)); i++)
720 header->srow_y[i] = srow_y[i];
722 if (names.count(
"srow_z") == 1)
724 std::vector<float> srow_z = list[
"srow_z"];
725 for (
int i=0; i<std::min(srow_z.size(),size_t(4)); i++)
726 header->srow_z[i] = srow_z[i];
729 if (names.count(
"intent_name") == 1)
730 strcpy(header->intent_name, Rcpp::as<std::string>(list[
"intent_name"]).substr(0,15).c_str());
731 if (names.count(
"magic") == 1)
732 strcpy(header->magic, Rcpp::as<std::string>(list[
"magic"]).substr(0,3).c_str());
734 this->
image = nifti_convert_nhdr2nim(*header, NULL);
735 this->
image->data = NULL;
741 int dims[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
742 const std::vector<int> dimVector =
object.attr(
"dim");
744 const int nDims = std::min(7,
int(dimVector.size()));
746 for (
int i=0; i<
nDims; i++)
747 dims[i+1] = dimVector[i];
750 this->
image = nifti_make_new_nim(dims, datatype,
int(copyData));
754 const size_t dataSize = nifti_get_volsize(
image);
755 if (datatype == DT_INT32)
756 memcpy(this->
image->data, INTEGER(
object), dataSize);
758 memcpy(this->
image->data, REAL(
object), dataSize);
761 this->
image->data = NULL;
763 if (
object.hasAttribute(
"pixdim"))
765 const std::vector<float> pixdimVector =
object.attr(
"pixdim");
766 const int pixdimLength = pixdimVector.size();
767 for (
int i=0; i<std::min(pixdimLength,nDims); i++)
768 this->
image->pixdim[i+1] = pixdimVector[i];
771 if (
object.hasAttribute(
"pixunits"))
773 const std::vector<std::string> pixunitsVector =
object.attr(
"pixunits");
781 Rcpp::RObject imageObject(
object);
782 bool resolved =
false;
784 if (imageObject.hasAttribute(
".nifti_image_ptr"))
786 Rcpp::XPtr<NiftiImage> imagePtr(SEXP(imageObject.attr(
".nifti_image_ptr")));
787 if (imagePtr.get() != NULL)
789 this->
image = *imagePtr;
793 if (imageObject.hasAttribute(
"dim"))
796 else if (Rf_isString(
object))
797 throw std::runtime_error(
"Internal image is not valid");
799 Rf_warning(
"Ignoring invalid internal pointer");
804 if (Rf_isNull(
object))
806 else if (Rf_isString(
object))
808 const std::string path = Rcpp::as<std::string>(object);
809 this->
image = nifti_image_read(path.c_str(), readData);
810 if (this->image == NULL)
811 throw std::runtime_error(
"Failed to read image from path " + path);
813 else if (imageObject.inherits(
"nifti"))
815 else if (imageObject.inherits(
"MriImage"))
817 else if (Rf_isVectorList(
object))
819 else if (imageObject.hasAttribute(
"dim"))
822 throw std::runtime_error(
"Cannot convert object of class \"" + Rcpp::as<std::string>(imageObject.attr(
"class")) +
"\" to a nifti_image");
825 if (this->
image != NULL)
826 nifti_update_dims_from_array(this->
image);
829 Rprintf(
"Creating NiftiImage with pointer %p (from SEXP)\n", this->
image);
833 inline mat33 topLeftCorner (
const mat44 &matrix)
836 for (
int i=0; i<3; i++)
838 for (
int j=0; j<3; j++)
839 newMatrix.m[i][j] = matrix.m[i][j];
848 const std::vector<float> origPixdim(
image->pixdim+1,
image->pixdim+4);
850 for (
int i=1; i<8; i++)
851 image->pixdim[i] = 0.0;
853 const int pixdimLength = pixdim.size();
854 for (
int i=0; i<std::min(pixdimLength,nDims); i++)
855 image->pixdim[i+1] = pixdim[i];
857 if (!std::equal(origPixdim.begin(), origPixdim.begin() + std::min(3,nDims), pixdim.begin()))
860 for (
int i=0; i<3; i++)
862 for (
int j=0; j<3; j++)
865 scaleMatrix.m[i][j] = 0.0;
867 scaleMatrix.m[i][j] = 1.0;
869 scaleMatrix.m[i][j] = pixdim[i] / origPixdim[i];
873 if (
image->qform_code > 0)
875 mat33 prod = nifti_mat33_mul(scaleMatrix, topLeftCorner(
image->qto_xyz));
876 for (
int i=0; i<3; i++)
878 for (
int j=0; j<3; j++)
879 image->qto_xyz.m[i][j] = prod.m[i][j];
881 image->qto_ijk = nifti_mat44_inverse(
image->qto_xyz);
882 nifti_mat44_to_quatern(
image->qto_xyz, &
image->quatern_b, &
image->quatern_c, &
image->quatern_d, &
image->qoffset_x, &
image->qoffset_y, &
image->qoffset_z, NULL, NULL, NULL, &
image->qfac);
885 if (
image->sform_code > 0)
887 mat33 prod = nifti_mat33_mul(scaleMatrix, topLeftCorner(
image->sto_xyz));
888 for (
int i=0; i<3; i++)
890 for (
int j=0; j<3; j++)
891 image->sto_xyz.m[i][j] = prod.m[i][j];
893 image->sto_ijk = nifti_mat44_inverse(
image->sto_xyz);
900 for (
int i=0; i<pixunits.size(); i++)
902 if (pixunits[i] ==
"m")
903 image->xyz_units = NIFTI_UNITS_METER;
904 else if (pixunits[i] ==
"mm")
905 image->xyz_units = NIFTI_UNITS_MM;
906 else if (pixunits[i] ==
"um")
907 image->xyz_units = NIFTI_UNITS_MICRON;
908 else if (pixunits[i] ==
"s")
909 image->time_units = NIFTI_UNITS_SEC;
910 else if (pixunits[i] ==
"ms")
911 image->time_units = NIFTI_UNITS_MSEC;
912 else if (pixunits[i] ==
"us")
913 image->time_units = NIFTI_UNITS_USEC;
914 else if (pixunits[i] ==
"Hz")
915 image->time_units = NIFTI_UNITS_HZ;
916 else if (pixunits[i] ==
"ppm")
917 image->time_units = NIFTI_UNITS_PPM;
918 else if (pixunits[i] ==
"rad/s")
919 image->time_units = NIFTI_UNITS_RADS;
925 std::vector<float> pixdim(
image->pixdim+1,
image->pixdim+4);
927 for (
int i=0; i<std::min(3,
int(scales.size())); i++)
929 if (scales[i] != 1.0)
931 pixdim[i] /= scales[i];
932 image->dim[i+1] =
static_cast<int>(std::floor(
image->dim[i+1] * scales[i]));
937 nifti_update_dims_from_array(
image);
945 Rcpp::RObject object(array);
946 if (!
object.hasAttribute(
"dim"))
949 for (
int i=0; i<8; i++)
951 const std::vector<int> dimVector =
object.attr(
"dim");
953 const int nDims = std::min(7,
int(dimVector.size()));
955 for (
int i=0; i<
nDims; i++)
956 image->dim[i+1] = dimVector[i];
958 if (
object.hasAttribute(
"pixdim"))
960 const std::vector<float> pixdimVector =
object.attr(
"pixdim");
964 if (
object.hasAttribute(
"pixunits"))
966 const std::vector<std::string> pixunitsVector =
object.attr(
"pixunits");
971 nifti_update_dims_from_array(
image);
975 nifti_datatype_sizes(
image->datatype, &
image->nbyper, NULL);
979 const size_t dataSize = nifti_get_volsize(
image);
980 image->data = calloc(1, dataSize);
981 if (
image->datatype == DT_INT32)
982 memcpy(
image->data, INTEGER(
object), dataSize);
984 memcpy(
image->data, REAL(
object), dataSize);
992 for (
int i=0; i<4; i++)
994 for (
int j=0; j<4; j++)
995 matrix.m[i][j] = 0.0;
999 else if (
image->qform_code <= 0 &&
image->sform_code <= 0)
1003 for (
int i=0; i<4; i++)
1005 for (
int j=0; j<4; j++)
1008 matrix.m[i][j] = 0.0;
1010 matrix.m[3][3] = 1.0;
1012 matrix.m[i][j] = (
image->pixdim[i+1]==0.0 ? 1.0 :
image->pixdim[i+1]);
1017 else if ((preferQuaternion &&
image->qform_code > 0) ||
image->sform_code <= 0)
1018 return image->qto_xyz;
1020 return image->sto_xyz;
1023 template <
typename SourceType,
typename TargetType>
1024 inline TargetType convertValue (SourceType value)
1026 return static_cast<TargetType
>(value);
1029 template <
typename SourceType,
typename TargetType>
1030 inline void convertArray (
const SourceType *source,
const size_t length, TargetType *target)
1032 std::transform(source, source + length, target, convertValue<SourceType,TargetType>);
1035 template <
typename TargetType>
1036 inline void changeDatatype (nifti_image *
image,
const short datatype)
1039 const size_t dataSize =
image->nvox *
sizeof(TargetType);
1040 data =
static_cast<TargetType *
>(calloc(1, dataSize));
1042 switch (
image->datatype)
1045 convertArray(static_cast<uint8_t *>(
image->data),
image->nvox, data);
1049 convertArray(static_cast<int16_t *>(
image->data),
image->nvox, data);
1053 convertArray(static_cast<int32_t *>(
image->data),
image->nvox, data);
1057 convertArray(static_cast<float *>(
image->data),
image->nvox, data);
1061 convertArray(static_cast<double *>(
image->data),
image->nvox, data);
1065 convertArray(static_cast<int8_t *>(
image->data),
image->nvox, data);
1069 convertArray(static_cast<uint16_t *>(
image->data),
image->nvox, data);
1073 convertArray(static_cast<uint32_t *>(
image->data),
image->nvox, data);
1077 convertArray(static_cast<int64_t *>(
image->data),
image->nvox, data);
1081 convertArray(static_cast<uint64_t *>(
image->data),
image->nvox, data);
1085 throw std::runtime_error(
"Unsupported data type (" + std::string(nifti_datatype_string(
image->datatype)) +
")");
1090 image->datatype = datatype;
1091 nifti_datatype_sizes(datatype, &
image->nbyper, &
image->swapsize);
1106 changeDatatype<uint8_t>(imageToWrite, datatype);
1110 changeDatatype<int16_t>(imageToWrite, datatype);
1114 changeDatatype<int32_t>(imageToWrite, datatype);
1118 changeDatatype<float>(imageToWrite, datatype);
1122 changeDatatype<double>(imageToWrite, datatype);
1126 changeDatatype<int8_t>(imageToWrite, datatype);
1130 changeDatatype<uint16_t>(imageToWrite, datatype);
1134 changeDatatype<uint32_t>(imageToWrite, datatype);
1138 changeDatatype<int64_t>(imageToWrite, datatype);
1142 changeDatatype<uint64_t>(imageToWrite, datatype);
1146 throw std::runtime_error(
"Unsupported data type (" + std::string(nifti_datatype_string(datatype)) +
")");
1149 const int status = nifti_set_filenames(imageToWrite, fileName.c_str(),
false,
true);
1151 throw std::runtime_error(
"Failed to set filenames for NIfTI object");
1152 nifti_image_write(imageToWrite);
1157 static std::map<std::string,short> datatypeCodes;
1158 if (datatypeCodes.empty())
1160 datatypeCodes[
"auto"] = DT_NONE;
1161 datatypeCodes[
"none"] = DT_NONE;
1162 datatypeCodes[
"unknown"] = DT_NONE;
1163 datatypeCodes[
"uint8"] = DT_UINT8;
1164 datatypeCodes[
"char"] = DT_UINT8;
1165 datatypeCodes[
"int16"] = DT_INT16;
1166 datatypeCodes[
"short"] = DT_INT16;
1167 datatypeCodes[
"int32"] = DT_INT32;
1168 datatypeCodes[
"int"] = DT_INT32;
1169 datatypeCodes[
"float32"] = DT_FLOAT32;
1170 datatypeCodes[
"float"] = DT_FLOAT32;
1171 datatypeCodes[
"float64"] = DT_FLOAT64;
1172 datatypeCodes[
"double"] = DT_FLOAT64;
1173 datatypeCodes[
"int8"] = DT_INT8;
1174 datatypeCodes[
"uint16"] = DT_UINT16;
1175 datatypeCodes[
"uint32"] = DT_UINT32;
1176 datatypeCodes[
"int64"] = DT_INT64;
1177 datatypeCodes[
"uint64"] = DT_UINT64;
1180 if (datatypeCodes.count(datatype) == 0)
1182 std::ostringstream message;
1183 message <<
"Datatype \"" << datatype <<
"\" is not valid";
1184 Rf_warning(message.str().c_str());
1185 toFile(fileName, DT_NONE);
1188 toFile(fileName, datatypeCodes[datatype]);
1191 template <
typename SourceType,
int SexpType>
1192 inline Rcpp::RObject imageDataToArray (
const nifti_image *source)
1195 return Rcpp::RObject();
1196 else if (source->data == NULL)
1198 Rf_warning(
"Internal image contains no data - filling array with NAs");
1199 Rcpp::Vector<SexpType> array(static_cast<int>(source->nvox));
1201 std::fill(array.begin(), array.end(), NA_REAL);
1206 SourceType *original =
static_cast<SourceType *
>(source->data);
1207 Rcpp::Vector<SexpType> array(static_cast<int>(source->nvox));
1209 if (SexpType == INTSXP || SexpType == LGLSXP)
1210 std::transform(original, original + source->nvox, array.begin(), convertValue<SourceType,int>);
1211 else if (SexpType == REALSXP)
1212 std::transform(original, original + source->nvox, array.begin(), convertValue<SourceType,double>);
1214 throw std::runtime_error(
"Only numeric arrays can be created");
1220 inline void finaliseNiftiImage (SEXP xptr)
1225 R_ClearExternalPtr(xptr);
1228 inline void addAttributes (Rcpp::RObject &
object, nifti_image *source,
const bool realDim =
true)
1230 const int nDims = source->dim[0];
1231 Rcpp::IntegerVector dim(source->dim+1, source->dim+1+nDims);
1234 object.attr(
"dim") = dim;
1236 object.attr(
"imagedim") = dim;
1238 Rcpp::DoubleVector pixdim(source->pixdim+1, source->pixdim+1+nDims);
1239 object.attr(
"pixdim") = pixdim;
1241 if (source->xyz_units == NIFTI_UNITS_UNKNOWN && source->time_units == NIFTI_UNITS_UNKNOWN)
1242 object.attr(
"pixunits") =
"Unknown";
1245 Rcpp::CharacterVector pixunits(2);
1246 pixunits[0] = nifti_units_string(source->xyz_units);
1247 pixunits[1] = nifti_units_string(source->time_units);
1248 object.attr(
"pixunits") = pixunits;
1253 Rcpp::XPtr<NiftiImage> xptr(wrappedSource);
1254 R_RegisterCFinalizerEx(SEXP(xptr), &finaliseNiftiImage, FALSE);
1255 object.attr(
".nifti_image_ptr") = xptr;
1260 Rcpp::RObject array;
1265 switch (
image->datatype)
1268 array = imageDataToArray<uint8_t,INTSXP>(
image);
1272 array = imageDataToArray<int16_t,INTSXP>(
image);
1276 array = imageDataToArray<int32_t,INTSXP>(
image);
1280 array = imageDataToArray<float,REALSXP>(
image);
1284 array = imageDataToArray<double,REALSXP>(
image);
1288 array = imageDataToArray<int8_t,INTSXP>(
image);
1292 array = imageDataToArray<uint16_t,INTSXP>(
image);
1296 array = imageDataToArray<uint32_t,INTSXP>(
image);
1300 array = imageDataToArray<int64_t,INTSXP>(
image);
1304 array = imageDataToArray<uint64_t,INTSXP>(
image);
1308 throw std::runtime_error(
"Unsupported data type (" + std::string(nifti_datatype_string(
image->datatype)) +
")");
1311 addAttributes(array,
image);
1312 const Rcpp::IntegerVector dim = array.attr(
"dim");
1313 array.attr(
"class") = Rcpp::CharacterVector::create(
"niftiImage");
1320 return Rcpp::RObject();
1323 Rcpp::RObject
string = Rcpp::wrap(label);
1324 addAttributes(
string,
image,
false);
1325 string.attr(
"class") = Rcpp::CharacterVector::create(
"internalImage",
"niftiImage");
1337 if (this->
image == NULL)
1338 return Rcpp::RObject();
1340 nifti_1_header header = nifti_convert_nim2nhdr(this->
image);
1343 result[
"sizeof_hdr"] = header.sizeof_hdr;
1345 result[
"dim_info"] = int(header.dim_info);
1346 result[
"dim"] = std::vector<short>(header.dim, header.dim+8);
1348 result[
"intent_p1"] = header.intent_p1;
1349 result[
"intent_p2"] = header.intent_p2;
1350 result[
"intent_p3"] = header.intent_p3;
1351 result[
"intent_code"] = header.intent_code;
1353 result[
"datatype"] = header.datatype;
1354 result[
"bitpix"] = header.bitpix;
1356 result[
"slice_start"] = header.slice_start;
1357 result[
"pixdim"] = std::vector<float>(header.pixdim, header.pixdim+8);
1358 result[
"vox_offset"] = header.vox_offset;
1359 result[
"scl_slope"] = header.scl_slope;
1360 result[
"scl_inter"] = header.scl_inter;
1361 result[
"slice_end"] = header.slice_end;
1362 result[
"slice_code"] = int(header.slice_code);
1363 result[
"xyzt_units"] = int(header.xyzt_units);
1364 result[
"cal_max"] = header.cal_max;
1365 result[
"cal_min"] = header.cal_min;
1366 result[
"slice_duration"] = header.slice_duration;
1367 result[
"toffset"] = header.toffset;
1368 result[
"descrip"] = std::string(header.descrip, 80);
1369 result[
"aux_file"] = std::string(header.aux_file, 24);
1371 result[
"qform_code"] = header.qform_code;
1372 result[
"sform_code"] = header.sform_code;
1373 result[
"quatern_b"] = header.quatern_b;
1374 result[
"quatern_c"] = header.quatern_c;
1375 result[
"quatern_d"] = header.quatern_d;
1376 result[
"qoffset_x"] = header.qoffset_x;
1377 result[
"qoffset_y"] = header.qoffset_y;
1378 result[
"qoffset_z"] = header.qoffset_z;
1379 result[
"srow_x"] = std::vector<float>(header.srow_x, header.srow_x+4);
1380 result[
"srow_y"] = std::vector<float>(header.srow_y, header.srow_y+4);
1381 result[
"srow_z"] = std::vector<float>(header.srow_z, header.srow_z+4);
1383 result[
"intent_name"] = std::string(header.intent_name, 16);
1384 result[
"magic"] = std::string(header.magic, 4);
1386 result.attr(
"class") = Rcpp::CharacterVector::create(
"niftiHeader");
NiftiImage()
Default constructor.
Definition: NiftiImage.h:156
const int index
The location along dimension.
Definition: NiftiImage.h:33
Rcpp::RObject headerToList() const
Create an R list containing raw image metadata.
Definition: NiftiImage.h:1335
bool persistent
Marker of persistence, which determines whether the nifti_image should be freed on destruction...
Definition: NiftiImage.h:93
void toFile(const std::string fileName, const short datatype) const
Write the image to a NIfTI-1 file.
Definition: NiftiImage.h:1094
NiftiImage(const std::string &path, const bool readData=true)
Initialise using a path string.
Definition: NiftiImage.h:196
NiftiImage(nifti_image *const image, const bool copy=false)
Initialise using an existing nifti_image pointer.
Definition: NiftiImage.h:178
void rescale(const std::vector< float > &scales)
Rescales the image, changing its image dimensions and pixel dimensions.
Definition: NiftiImage.h:923
Definition: NiftiImage.h:16
void initFromMriImage(const Rcpp::RObject &object, const bool copyData=true)
Initialise the object from a reference object of class "MriImage".
Definition: NiftiImage.h:552
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:57
bool isNull() const
Determines whether or not the internal pointer is NULL.
Definition: NiftiImage.h:281
Inner class referring to a subset of an image.
Definition: NiftiImage.h:29
Block slice(const int i)
Extract a slice block from a 3D image.
Definition: NiftiImage.h:347
void update(const SEXP array)
Update the image from an R array.
Definition: NiftiImage.h:943
Rcpp::RObject toPointer(const std::string label) const
Create an internal image to pass back to R.
Definition: NiftiImage.h:1317
void initFromArray(const Rcpp::RObject &object, const bool copyData=true)
Initialise the object from an R array.
Definition: NiftiImage.h:739
void initFromNiftiS4(const Rcpp::RObject &object, const bool copyData=true)
Initialise the object from an S4 object of class "nifti".
Definition: NiftiImage.h:456
const Block slice(const int i) const
Extract a slice block from a 3D image.
Definition: NiftiImage.h:340
const Block volume(const int i) const
Extract a volume block from a 4D image.
Definition: NiftiImage.h:354
bool isPersistent() const
Determines whether or not the image is marked as persistent.
Definition: NiftiImage.h:286
void setPixunits(const std::vector< std::string > &pixunits)
Modify the pixel dimension units.
Definition: NiftiImage.h:898
Thin wrapper around a C-style nifti_image struct that allows C++-style destruction.
Definition: NiftiImage.h:22
void setPersistence(const bool persistent)
Allows the image to be marked as persistent, so that it can be passed back to R.
Definition: NiftiImage.h:269
NiftiImage(const NiftiImage &source)
Copy constructor.
Definition: NiftiImage.h:163
nifti_image * operator->() const
Allows a NiftiImage object to be treated as a pointer to a nifti_image.
Definition: NiftiImage.h:236
const int dimension
The dimension along which the block applies (which should be the last)
Definition: NiftiImage.h:32
void copy(nifti_image *const source)
Copy the contents of a nifti_image to create a new image.
Definition: NiftiImage.h:405
const NiftiImage & image
The parent image.
Definition: NiftiImage.h:31
void initFromList(const Rcpp::RObject &object)
Initialise the object from an R list with named elements, which can only contain metadata.
Definition: NiftiImage.h:650
void updatePixdim(const std::vector< float > &pixdim)
Modify the pixel dimensions, and potentially the xform matrices to match.
Definition: NiftiImage.h:845
~NiftiImage()
Destructor which frees the wrapped pointer, unless the object is marked as persistent.
Definition: NiftiImage.h:217
mat44 xform(const bool preferQuaternion=true) const
Obtain an xform matrix, indicating the orientation of the image.
Definition: NiftiImage.h:987
nifti_image * image
The wrapped nifti_image pointer.
Definition: NiftiImage.h:92
Rcpp::RObject toArrayOrPointer(const bool internal, const std::string label) const
A conditional method that calls either toArray or toPointer.
Definition: NiftiImage.h:1330
static short sexpTypeToNiftiType(const int sexpType)
Convert between R SEXP object type and nifti_image datatype codes.
Definition: NiftiImage.h:81
int nDims() const
Returns the number of dimensions in the image.
Definition: NiftiImage.h:291
Block volume(const int i)
Extract a volume block from a 4D image.
Definition: NiftiImage.h:361
NiftiImage & drop()
Drop unitary dimensions.
Definition: NiftiImage.h:305
Rcpp::RObject toArray() const
Create an R array from the image.
Definition: NiftiImage.h:1258
Block(const NiftiImage &image, const int dimension, const int index)
Standard constructor for this class.
Definition: NiftiImage.h:42