1 #ifndef _NIFTI_IMAGE_H_ 2 #define _NIFTI_IMAGE_H_ 6 #include "niftilib/nifti1_io.h" 41 : image(image), dimension(dimension), index(index)
43 if (dimension != image->ndim)
44 throw std::runtime_error(
"Blocks must be along the last dimension in the image");
57 if (source->datatype != image->datatype)
58 throw std::runtime_error(
"New data does not have the same datatype as the target block");
62 blockSize *= image->dim[i];
64 if (blockSize != source->nvox)
65 throw std::runtime_error(
"New data does not have the same size as the target block");
67 blockSize *= image->nbyper;
68 memcpy(static_cast<char*>(image->data) + blockSize*index, source->data, blockSize);
81 if (sexpType == INTSXP || sexpType == LGLSXP)
83 else if (sexpType == REALSXP)
86 throw std::runtime_error(
"Array elements must be numeric");
97 void copy (nifti_image *
const source);
116 void initFromNiftiS4 (
const Rcpp::RObject &
object,
const bool copyData =
true);
123 void initFromMriImage (
const Rcpp::RObject &
object,
const bool copyData =
true);
136 void initFromArray (
const Rcpp::RObject &
object,
const bool copyData =
true);
148 void setPixunits (
const std::vector<std::string> &pixunits);
155 : image(NULL), persistent(false) {}
162 : image(NULL), persistent(false)
166 Rprintf(
"Creating NiftiImage with pointer %p (from NiftiImage)\n", this->image);
177 : image(NULL), persistent(false)
184 Rprintf(
"Creating NiftiImage with pointer %p (from pointer)\n", this->image);
194 NiftiImage (
const std::string &path,
const bool readData =
true)
197 this->image = nifti_image_read(path.c_str(), readData);
198 if (this->image == NULL)
199 throw std::runtime_error(
"Failed to read image from path " + path);
201 Rprintf(
"Creating NiftiImage with pointer %p (from string)\n", this->image);
210 NiftiImage (
const SEXP
object,
const bool readData =
true);
220 Rprintf(
"Freeing NiftiImage with pointer %p\n", this->image);
222 nifti_image_free(image);
229 operator nifti_image* ()
const {
return image; }
244 Rprintf(
"Creating NiftiImage with pointer %p (from NiftiImage)\n", this->image);
258 Rprintf(
"Creating NiftiImage with pointer %p (from Block)\n", this->image);
272 Rprintf(
"Setting NiftiImage with pointer %p to be persistent\n", this->image);
279 bool isNull ()
const {
return (image == NULL); }
305 int ndim = image->ndim;
306 while (image->dim[ndim] < 2)
308 image->dim[0] = image->ndim = ndim;
318 void rescale (
const std::vector<float> &scales);
324 void update (
const SEXP array);
331 mat44
xform (
const bool preferQuaternion =
true)
const;
366 void toFile (
const std::string fileName,
const short datatype)
const;
373 void toFile (
const std::string fileName,
const std::string &datatype)
const;
379 Rcpp::RObject
toArray ()
const;
386 Rcpp::RObject
toPointer (
const std::string label)
const;
394 Rcpp::RObject
toArrayOrPointer (
const bool internal,
const std::string label)
const;
406 nifti_image_free(
image);
412 image = nifti_copy_nim_info(source);
413 if (source->data != NULL)
415 size_t dataSize = nifti_get_volsize(source);
416 image->data = calloc(1, dataSize);
417 memcpy(
image->data, source->data, dataSize);
424 nifti_image *sourceStruct = source;
431 nifti_image_free(
image);
433 nifti_image *sourceStruct = source.
image;
434 if (sourceStruct == NULL)
438 image = nifti_copy_nim_info(sourceStruct);
442 nifti_update_dims_from_array(
image);
444 if (sourceStruct->data != NULL)
446 size_t blockSize = nifti_get_volsize(
image);
447 image->data = calloc(1, blockSize);
448 memcpy(
image->data, static_cast<char*>(source.
image->data) + blockSize*source.
index, blockSize);
456 nifti_1_header header;
457 header.sizeof_hdr = 348;
459 const std::vector<short> dims =
object.slot(
"dim_");
460 for (
int i=0; i<8; i++)
461 header.dim[i] = dims[i];
463 header.intent_p1 =
object.slot(
"intent_p1");
464 header.intent_p2 =
object.slot(
"intent_p2");
465 header.intent_p3 =
object.slot(
"intent_p3");
466 header.intent_code =
object.slot(
"intent_code");
468 header.datatype =
object.slot(
"datatype");
469 header.bitpix =
object.slot(
"bitpix");
471 header.slice_start =
object.slot(
"slice_start");
472 header.slice_end =
object.slot(
"slice_end");
473 header.slice_code = Rcpp::as<int>(
object.slot(
"slice_code"));
474 header.slice_duration =
object.slot(
"slice_duration");
476 const std::vector<float> pixdims =
object.slot(
"pixdim");
477 for (
int i=0; i<8; i++)
478 header.pixdim[i] = pixdims[i];
479 header.xyzt_units = Rcpp::as<int>(
object.slot(
"xyzt_units"));
481 header.vox_offset =
object.slot(
"vox_offset");
483 header.scl_slope =
object.slot(
"scl_slope");
484 header.scl_inter =
object.slot(
"scl_inter");
485 header.toffset =
object.slot(
"toffset");
487 header.cal_max =
object.slot(
"cal_max");
488 header.cal_min =
object.slot(
"cal_min");
489 header.glmax = header.glmin = 0;
491 strncpy(header.descrip, Rcpp::as<std::string>(
object.slot(
"descrip")).c_str(), 79);
492 header.descrip[79] =
'\0';
493 strncpy(header.aux_file, Rcpp::as<std::string>(
object.slot(
"aux_file")).c_str(), 23);
494 header.aux_file[23] =
'\0';
495 strncpy(header.intent_name, Rcpp::as<std::string>(
object.slot(
"intent_name")).c_str(), 15);
496 header.intent_name[15] =
'\0';
497 strncpy(header.magic, Rcpp::as<std::string>(
object.slot(
"magic")).c_str(), 3);
498 header.magic[3] =
'\0';
500 header.qform_code =
object.slot(
"qform_code");
501 header.sform_code =
object.slot(
"sform_code");
503 header.quatern_b =
object.slot(
"quatern_b");
504 header.quatern_c =
object.slot(
"quatern_c");
505 header.quatern_d =
object.slot(
"quatern_d");
506 header.qoffset_x =
object.slot(
"qoffset_x");
507 header.qoffset_y =
object.slot(
"qoffset_y");
508 header.qoffset_z =
object.slot(
"qoffset_z");
510 const std::vector<float> srow_x =
object.slot(
"srow_x");
511 const std::vector<float> srow_y =
object.slot(
"srow_y");
512 const std::vector<float> srow_z =
object.slot(
"srow_z");
513 for (
int i=0; i<4; i++)
515 header.srow_x[i] = srow_x[i];
516 header.srow_y[i] = srow_y[i];
517 header.srow_z[i] = srow_z[i];
520 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)
521 header.datatype = DT_INT32;
522 else if (header.datatype == DT_FLOAT32 || header.datatype == DT_FLOAT64)
523 header.datatype = DT_FLOAT64;
525 throw std::runtime_error(
"Data type is not supported");
527 this->
image = nifti_convert_nhdr2nim(header, NULL);
529 const SEXP data = PROTECT(
object.slot(
".Data"));
530 if (!copyData || Rf_length(data) <= 1)
531 this->
image->data = NULL;
534 const size_t dataSize = nifti_get_volsize(this->
image);
535 this->
image->data = calloc(1, dataSize);
536 if (header.datatype == DT_INT32)
538 Rcpp::IntegerVector intData(data);
539 std::copy(intData.begin(), intData.end(),
static_cast<int32_t*
>(this->
image->data));
543 Rcpp::DoubleVector doubleData(data);
544 std::copy(doubleData.begin(), doubleData.end(),
static_cast<double*
>(this->
image->data));
552 Rcpp::Reference mriImage(
object);
553 Rcpp::Function getXform = mriImage.field(
"getXform");
554 Rcpp::NumericMatrix
xform = getXform();
558 if (Rf_length(mriImage.field(
"tags")) > 0)
561 Rcpp::RObject data = mriImage.field(
"data");
562 if (data.inherits(
"SparseArray"))
564 Rcpp::Language call(
"as.array", data);
568 const short datatype = (Rf_isNull(data) ? DT_INT32 :
sexpTypeToNiftiType(data.sexp_type()));
570 int dims[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
571 const std::vector<int> dimVector = mriImage.field(
"imageDims");
572 const int nDims = std::min(7,
int(dimVector.size()));
575 for (
int i=0; i<
nDims; i++)
577 dims[i+1] = dimVector[i];
578 nVoxels *= dimVector[i];
581 if (this->
image == NULL)
582 this->
image = nifti_make_new_nim(dims, datatype, FALSE);
585 std::copy(dims, dims+8, this->
image->dim);
586 this->
image->datatype = datatype;
587 nifti_datatype_sizes(
image->datatype, &
image->nbyper, NULL);
590 if (copyData && !Rf_isNull(data))
593 const size_t dataSize = nVoxels *
image->nbyper;
594 this->
image->data = calloc(1, dataSize);
595 if (datatype == DT_INT32)
596 memcpy(this->
image->data, INTEGER(data), dataSize);
598 memcpy(this->
image->data, REAL(data), dataSize);
601 this->
image->data = NULL;
603 const std::vector<float> pixdimVector = mriImage.field(
"voxelDims");
604 const int pixdimLength = pixdimVector.size();
605 for (
int i=0; i<std::min(pixdimLength,nDims); i++)
606 this->
image->pixdim[i+1] = std::abs(pixdimVector[i]);
608 const std::vector<std::string> pixunitsVector = mriImage.field(
"voxelDimUnits");
611 if (xform.rows() != 4 || xform.cols() != 4)
612 this->
image->qform_code = this->
image->sform_code = 0;
616 for (
int i=0; i<4; i++)
618 for (
int j=0; j<4; j++)
619 matrix.m[i][j] = static_cast<float>(xform(i,j));
622 this->
image->qto_xyz = matrix;
623 this->
image->qto_ijk = nifti_mat44_inverse(
image->qto_xyz);
624 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);
626 this->
image->sto_xyz = matrix;
627 this->
image->sto_ijk = nifti_mat44_inverse(
image->sto_xyz);
629 this->
image->qform_code = this->
image->sform_code = 2;
633 template <
typename TargetType>
634 inline void copyIfPresent (
const Rcpp::List &list,
const std::set<std::string> names,
const std::string &name, TargetType &target)
636 if (names.count(name) == 1)
637 target = Rcpp::as<TargetType>(list[name]);
642 inline void copyIfPresent (
const Rcpp::List &list,
const std::set<std::string> names,
const std::string &name,
char &target)
644 if (names.count(name) == 1)
645 target = static_cast<char>(Rcpp::as<int>(list[name]));
650 Rcpp::List list(
object);
651 nifti_1_header *header = nifti_make_new_header(NULL, DT_FLOAT64);
653 const Rcpp::CharacterVector _names = list.names();
654 std::set<std::string> names;
655 for (Rcpp::CharacterVector::const_iterator it=_names.begin(); it!=_names.end(); it++)
656 names.insert(Rcpp::as<std::string>(*it));
658 copyIfPresent(list, names,
"sizeof_hdr", header->sizeof_hdr);
660 copyIfPresent(list, names,
"dim_info", header->dim_info);
661 if (names.count(
"dim") == 1)
663 std::vector<short> dim = list[
"dim"];
664 for (
int i=0; i<std::min(dim.size(),size_t(8)); i++)
665 header->dim[i] = dim[i];
668 copyIfPresent(list, names,
"intent_p1", header->intent_p1);
669 copyIfPresent(list, names,
"intent_p2", header->intent_p2);
670 copyIfPresent(list, names,
"intent_p3", header->intent_p3);
671 copyIfPresent(list, names,
"intent_code", header->intent_code);
673 copyIfPresent(list, names,
"datatype", header->datatype);
674 copyIfPresent(list, names,
"bitpix", header->bitpix);
676 copyIfPresent(list, names,
"slice_start", header->slice_start);
677 if (names.count(
"pixdim") == 1)
679 std::vector<float> pixdim = list[
"pixdim"];
680 for (
int i=0; i<std::min(pixdim.size(),size_t(8)); i++)
681 header->pixdim[i] = pixdim[i];
683 copyIfPresent(list, names,
"vox_offset", header->vox_offset);
684 copyIfPresent(list, names,
"scl_slope", header->scl_slope);
685 copyIfPresent(list, names,
"scl_inter", header->scl_inter);
686 copyIfPresent(list, names,
"slice_end", header->slice_end);
687 copyIfPresent(list, names,
"slice_code", header->slice_code);
688 copyIfPresent(list, names,
"xyzt_units", header->xyzt_units);
689 copyIfPresent(list, names,
"cal_max", header->cal_max);
690 copyIfPresent(list, names,
"cal_min", header->cal_min);
691 copyIfPresent(list, names,
"slice_duration", header->slice_duration);
692 copyIfPresent(list, names,
"toffset", header->toffset);
694 if (names.count(
"descrip") == 1)
695 strcpy(header->descrip, Rcpp::as<std::string>(list[
"descrip"]).substr(0,79).c_str());
696 if (names.count(
"aux_file") == 1)
697 strcpy(header->aux_file, Rcpp::as<std::string>(list[
"aux_file"]).substr(0,23).c_str());
699 copyIfPresent(list, names,
"qform_code", header->qform_code);
700 copyIfPresent(list, names,
"sform_code", header->sform_code);
701 copyIfPresent(list, names,
"quatern_b", header->quatern_b);
702 copyIfPresent(list, names,
"quatern_c", header->quatern_c);
703 copyIfPresent(list, names,
"quatern_d", header->quatern_d);
704 copyIfPresent(list, names,
"qoffset_x", header->qoffset_x);
705 copyIfPresent(list, names,
"qoffset_y", header->qoffset_y);
706 copyIfPresent(list, names,
"qoffset_z", header->qoffset_z);
708 if (names.count(
"srow_x") == 1)
710 std::vector<float> srow_x = list[
"srow_x"];
711 for (
int i=0; i<std::min(srow_x.size(),size_t(4)); i++)
712 header->srow_x[i] = srow_x[i];
714 if (names.count(
"srow_y") == 1)
716 std::vector<float> srow_y = list[
"srow_y"];
717 for (
int i=0; i<std::min(srow_y.size(),size_t(4)); i++)
718 header->srow_y[i] = srow_y[i];
720 if (names.count(
"srow_z") == 1)
722 std::vector<float> srow_z = list[
"srow_z"];
723 for (
int i=0; i<std::min(srow_z.size(),size_t(4)); i++)
724 header->srow_z[i] = srow_z[i];
727 if (names.count(
"intent_name") == 1)
728 strcpy(header->intent_name, Rcpp::as<std::string>(list[
"intent_name"]).substr(0,15).c_str());
729 if (names.count(
"magic") == 1)
730 strcpy(header->magic, Rcpp::as<std::string>(list[
"magic"]).substr(0,3).c_str());
732 this->
image = nifti_convert_nhdr2nim(*header, NULL);
733 this->
image->data = NULL;
739 int dims[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
740 const std::vector<int> dimVector =
object.attr(
"dim");
742 const int nDims = std::min(7,
int(dimVector.size()));
744 for (
int i=0; i<
nDims; i++)
745 dims[i+1] = dimVector[i];
748 this->
image = nifti_make_new_nim(dims, datatype,
int(copyData));
752 const size_t dataSize = nifti_get_volsize(
image);
753 if (datatype == DT_INT32)
754 memcpy(this->
image->data, INTEGER(
object), dataSize);
756 memcpy(this->
image->data, REAL(
object), dataSize);
759 this->
image->data = NULL;
761 if (
object.hasAttribute(
"pixdim"))
763 const std::vector<float> pixdimVector =
object.attr(
"pixdim");
764 const int pixdimLength = pixdimVector.size();
765 for (
int i=0; i<std::min(pixdimLength,nDims); i++)
766 this->
image->pixdim[i+1] = pixdimVector[i];
769 if (
object.hasAttribute(
"pixunits"))
771 const std::vector<std::string> pixunitsVector =
object.attr(
"pixunits");
779 Rcpp::RObject imageObject(
object);
780 bool resolved =
false;
782 if (imageObject.hasAttribute(
".nifti_image_ptr"))
784 Rcpp::XPtr<NiftiImage> imagePtr(SEXP(imageObject.attr(
".nifti_image_ptr")));
785 if (imagePtr.get() != NULL)
787 this->
image = *imagePtr;
791 if (imageObject.hasAttribute(
"dim"))
794 else if (Rf_isString(
object))
795 throw std::runtime_error(
"Internal image is not valid");
797 Rf_warning(
"Ignoring invalid internal pointer");
802 if (Rf_isNull(
object))
804 else if (Rf_isString(
object))
806 const std::string path = Rcpp::as<std::string>(object);
807 this->
image = nifti_image_read(path.c_str(), readData);
808 if (this->image == NULL)
809 throw std::runtime_error(
"Failed to read image from path " + path);
811 else if (imageObject.inherits(
"nifti"))
813 else if (imageObject.inherits(
"MriImage"))
815 else if (Rf_isVectorList(
object))
817 else if (imageObject.hasAttribute(
"dim"))
820 throw std::runtime_error(
"Cannot convert object of class \"" + Rcpp::as<std::string>(imageObject.attr(
"class")) +
"\" to a nifti_image");
823 if (this->
image != NULL)
824 nifti_update_dims_from_array(this->
image);
827 Rprintf(
"Creating NiftiImage with pointer %p (from SEXP)\n", this->
image);
831 inline mat33 topLeftCorner (
const mat44 &matrix)
834 for (
int i=0; i<3; i++)
836 for (
int j=0; j<3; j++)
837 newMatrix.m[i][j] = matrix.m[i][j];
846 const std::vector<float> origPixdim(
image->pixdim+1,
image->pixdim+4);
848 for (
int i=1; i<8; i++)
849 image->pixdim[i] = 0.0;
851 const int pixdimLength = pixdim.size();
852 for (
int i=0; i<std::min(pixdimLength,nDims); i++)
853 image->pixdim[i+1] = pixdim[i];
855 if (!std::equal(origPixdim.begin(), origPixdim.begin() + std::min(3,nDims), pixdim.begin()))
858 for (
int i=0; i<3; i++)
860 for (
int j=0; j<3; j++)
863 scaleMatrix.m[i][j] = 0.0;
865 scaleMatrix.m[i][j] = 1.0;
867 scaleMatrix.m[i][j] = pixdim[i] / origPixdim[i];
871 if (
image->qform_code > 0)
873 mat33 prod = nifti_mat33_mul(scaleMatrix, topLeftCorner(
image->qto_xyz));
874 for (
int i=0; i<3; i++)
876 for (
int j=0; j<3; j++)
877 image->qto_xyz.m[i][j] = prod.m[i][j];
879 image->qto_ijk = nifti_mat44_inverse(
image->qto_xyz);
880 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);
883 if (
image->sform_code > 0)
885 mat33 prod = nifti_mat33_mul(scaleMatrix, topLeftCorner(
image->sto_xyz));
886 for (
int i=0; i<3; i++)
888 for (
int j=0; j<3; j++)
889 image->sto_xyz.m[i][j] = prod.m[i][j];
891 image->sto_ijk = nifti_mat44_inverse(
image->sto_xyz);
898 for (
int i=0; i<pixunits.size(); i++)
900 if (pixunits[i] ==
"m")
901 image->xyz_units = NIFTI_UNITS_METER;
902 else if (pixunits[i] ==
"mm")
903 image->xyz_units = NIFTI_UNITS_MM;
904 else if (pixunits[i] ==
"um")
905 image->xyz_units = NIFTI_UNITS_MICRON;
906 else if (pixunits[i] ==
"s")
907 image->time_units = NIFTI_UNITS_SEC;
908 else if (pixunits[i] ==
"ms")
909 image->time_units = NIFTI_UNITS_MSEC;
910 else if (pixunits[i] ==
"us")
911 image->time_units = NIFTI_UNITS_USEC;
912 else if (pixunits[i] ==
"Hz")
913 image->time_units = NIFTI_UNITS_HZ;
914 else if (pixunits[i] ==
"ppm")
915 image->time_units = NIFTI_UNITS_PPM;
916 else if (pixunits[i] ==
"rad/s")
917 image->time_units = NIFTI_UNITS_RADS;
923 std::vector<float> pixdim(
image->pixdim+1,
image->pixdim+4);
925 for (
int i=0; i<std::min(3,
int(scales.size())); i++)
927 if (scales[i] != 1.0)
929 pixdim[i] /= scales[i];
930 image->dim[i+1] =
static_cast<int>(std::floor(
image->dim[i+1] * scales[i]));
935 nifti_update_dims_from_array(
image);
943 Rcpp::RObject object(array);
944 if (!
object.hasAttribute(
"dim"))
947 for (
int i=0; i<8; i++)
949 const std::vector<int> dimVector =
object.attr(
"dim");
951 const int nDims = std::min(7,
int(dimVector.size()));
953 for (
int i=0; i<
nDims; i++)
954 image->dim[i+1] = dimVector[i];
956 if (
object.hasAttribute(
"pixdim"))
958 const std::vector<float> pixdimVector =
object.attr(
"pixdim");
962 if (
object.hasAttribute(
"pixunits"))
964 const std::vector<std::string> pixunitsVector =
object.attr(
"pixunits");
969 nifti_update_dims_from_array(
image);
973 nifti_datatype_sizes(
image->datatype, &
image->nbyper, NULL);
977 const size_t dataSize = nifti_get_volsize(
image);
978 image->data = calloc(1, dataSize);
979 if (
image->datatype == DT_INT32)
980 memcpy(
image->data, INTEGER(
object), dataSize);
982 memcpy(
image->data, REAL(
object), dataSize);
990 for (
int i=0; i<4; i++)
992 for (
int j=0; j<4; j++)
993 matrix.m[i][j] = 0.0;
997 else if (
image->qform_code <= 0 &&
image->sform_code <= 0)
1001 for (
int i=0; i<4; i++)
1003 for (
int j=0; j<4; j++)
1006 matrix.m[i][j] = 0.0;
1008 matrix.m[3][3] = 1.0;
1010 matrix.m[i][j] = (
image->pixdim[i+1]==0.0 ? 1.0 :
image->pixdim[i+1]);
1015 else if ((preferQuaternion &&
image->qform_code > 0) ||
image->sform_code <= 0)
1016 return image->qto_xyz;
1018 return image->sto_xyz;
1021 template <
typename SourceType,
typename TargetType>
1022 inline TargetType convertValue (SourceType value)
1024 return static_cast<TargetType
>(value);
1027 template <
typename SourceType,
typename TargetType>
1028 inline void convertArray (
const SourceType *source,
const size_t length, TargetType *target)
1030 std::transform(source, source + length, target, convertValue<SourceType,TargetType>);
1033 template <
typename TargetType>
1034 inline void changeDatatype (nifti_image *
image,
const short datatype)
1037 const size_t dataSize =
image->nvox *
sizeof(TargetType);
1038 data =
static_cast<TargetType *
>(calloc(1, dataSize));
1040 switch (
image->datatype)
1043 convertArray(static_cast<uint8_t *>(
image->data),
image->nvox, data);
1047 convertArray(static_cast<int16_t *>(
image->data),
image->nvox, data);
1051 convertArray(static_cast<int32_t *>(
image->data),
image->nvox, data);
1055 convertArray(static_cast<float *>(
image->data),
image->nvox, data);
1059 convertArray(static_cast<double *>(
image->data),
image->nvox, data);
1063 convertArray(static_cast<int8_t *>(
image->data),
image->nvox, data);
1067 convertArray(static_cast<uint16_t *>(
image->data),
image->nvox, data);
1071 convertArray(static_cast<uint32_t *>(
image->data),
image->nvox, data);
1075 convertArray(static_cast<int64_t *>(
image->data),
image->nvox, data);
1079 convertArray(static_cast<uint64_t *>(
image->data),
image->nvox, data);
1083 throw std::runtime_error(
"Unsupported data type (" + std::string(nifti_datatype_string(
image->datatype)) +
")");
1088 image->datatype = datatype;
1089 nifti_datatype_sizes(datatype, &
image->nbyper, &
image->swapsize);
1104 changeDatatype<uint8_t>(imageToWrite, datatype);
1108 changeDatatype<int16_t>(imageToWrite, datatype);
1112 changeDatatype<int32_t>(imageToWrite, datatype);
1116 changeDatatype<float>(imageToWrite, datatype);
1120 changeDatatype<double>(imageToWrite, datatype);
1124 changeDatatype<int8_t>(imageToWrite, datatype);
1128 changeDatatype<uint16_t>(imageToWrite, datatype);
1132 changeDatatype<uint32_t>(imageToWrite, datatype);
1136 changeDatatype<int64_t>(imageToWrite, datatype);
1140 changeDatatype<uint64_t>(imageToWrite, datatype);
1144 throw std::runtime_error(
"Unsupported data type (" + std::string(nifti_datatype_string(datatype)) +
")");
1147 const int status = nifti_set_filenames(imageToWrite, fileName.c_str(),
false,
true);
1149 throw std::runtime_error(
"Failed to set filenames for NIfTI object");
1150 nifti_image_write(imageToWrite);
1155 static std::map<std::string,short> datatypeCodes;
1156 if (datatypeCodes.empty())
1158 datatypeCodes[
"auto"] = DT_NONE;
1159 datatypeCodes[
"none"] = DT_NONE;
1160 datatypeCodes[
"unknown"] = DT_NONE;
1161 datatypeCodes[
"uint8"] = DT_UINT8;
1162 datatypeCodes[
"char"] = DT_UINT8;
1163 datatypeCodes[
"int16"] = DT_INT16;
1164 datatypeCodes[
"short"] = DT_INT16;
1165 datatypeCodes[
"int32"] = DT_INT32;
1166 datatypeCodes[
"int"] = DT_INT32;
1167 datatypeCodes[
"float32"] = DT_FLOAT32;
1168 datatypeCodes[
"float"] = DT_FLOAT32;
1169 datatypeCodes[
"float64"] = DT_FLOAT64;
1170 datatypeCodes[
"double"] = DT_FLOAT64;
1171 datatypeCodes[
"int8"] = DT_INT8;
1172 datatypeCodes[
"uint16"] = DT_UINT16;
1173 datatypeCodes[
"uint32"] = DT_UINT32;
1174 datatypeCodes[
"int64"] = DT_INT64;
1175 datatypeCodes[
"uint64"] = DT_UINT64;
1178 if (datatypeCodes.count(datatype) == 0)
1180 std::ostringstream message;
1181 message <<
"Datatype \"" << datatype <<
"\" is not valid";
1182 Rf_warning(message.str().c_str());
1183 toFile(fileName, DT_NONE);
1186 toFile(fileName, datatypeCodes[datatype]);
1189 template <
typename SourceType,
int SexpType>
1190 inline Rcpp::RObject imageDataToArray (
const nifti_image *source)
1193 return Rcpp::RObject();
1194 else if (source->data == NULL)
1196 Rf_warning(
"Internal image contains no data - filling array with NAs");
1197 Rcpp::Vector<SexpType> array(static_cast<int>(source->nvox));
1199 std::fill(array.begin(), array.end(), NA_REAL);
1204 SourceType *original =
static_cast<SourceType *
>(source->data);
1205 Rcpp::Vector<SexpType> array(static_cast<int>(source->nvox));
1207 if (SexpType == INTSXP || SexpType == LGLSXP)
1208 std::transform(original, original + source->nvox, array.begin(), convertValue<SourceType,int>);
1209 else if (SexpType == REALSXP)
1210 std::transform(original, original + source->nvox, array.begin(), convertValue<SourceType,double>);
1212 throw std::runtime_error(
"Only numeric arrays can be created");
1218 inline void finaliseNiftiImage (SEXP xptr)
1223 R_ClearExternalPtr(xptr);
1226 inline void addAttributes (Rcpp::RObject &
object, nifti_image *source,
const bool realDim =
true)
1228 const int nDims = source->dim[0];
1229 Rcpp::IntegerVector dim(source->dim+1, source->dim+1+nDims);
1232 object.attr(
"dim") = dim;
1234 object.attr(
"imagedim") = dim;
1236 Rcpp::DoubleVector pixdim(source->pixdim+1, source->pixdim+1+nDims);
1237 object.attr(
"pixdim") = pixdim;
1239 if (source->xyz_units == NIFTI_UNITS_UNKNOWN && source->time_units == NIFTI_UNITS_UNKNOWN)
1240 object.attr(
"pixunits") =
"Unknown";
1243 Rcpp::CharacterVector pixunits(2);
1244 pixunits[0] = nifti_units_string(source->xyz_units);
1245 pixunits[1] = nifti_units_string(source->time_units);
1246 object.attr(
"pixunits") = pixunits;
1251 Rcpp::XPtr<NiftiImage> xptr(wrappedSource);
1252 R_RegisterCFinalizerEx(SEXP(xptr), &finaliseNiftiImage, FALSE);
1253 object.attr(
".nifti_image_ptr") = xptr;
1258 Rcpp::RObject array;
1263 switch (
image->datatype)
1266 array = imageDataToArray<uint8_t,INTSXP>(
image);
1270 array = imageDataToArray<int16_t,INTSXP>(
image);
1274 array = imageDataToArray<int32_t,INTSXP>(
image);
1278 array = imageDataToArray<float,REALSXP>(
image);
1282 array = imageDataToArray<double,REALSXP>(
image);
1286 array = imageDataToArray<int8_t,INTSXP>(
image);
1290 array = imageDataToArray<uint16_t,INTSXP>(
image);
1294 array = imageDataToArray<uint32_t,INTSXP>(
image);
1298 array = imageDataToArray<int64_t,INTSXP>(
image);
1302 array = imageDataToArray<uint64_t,INTSXP>(
image);
1306 throw std::runtime_error(
"Unsupported data type (" + std::string(nifti_datatype_string(
image->datatype)) +
")");
1309 addAttributes(array,
image);
1310 const Rcpp::IntegerVector dim = array.attr(
"dim");
1311 array.attr(
"class") = Rcpp::CharacterVector::create(
"niftiImage");
1318 return Rcpp::RObject();
1321 Rcpp::RObject
string = Rcpp::wrap(label);
1322 addAttributes(
string,
image,
false);
1323 string.attr(
"class") = Rcpp::CharacterVector::create(
"internalImage",
"niftiImage");
1335 if (this->
image == NULL)
1336 return Rcpp::RObject();
1338 nifti_1_header header = nifti_convert_nim2nhdr(this->
image);
1341 result[
"sizeof_hdr"] = header.sizeof_hdr;
1343 result[
"dim_info"] = int(header.dim_info);
1344 result[
"dim"] = std::vector<short>(header.dim, header.dim+8);
1346 result[
"intent_p1"] = header.intent_p1;
1347 result[
"intent_p2"] = header.intent_p2;
1348 result[
"intent_p3"] = header.intent_p3;
1349 result[
"intent_code"] = header.intent_code;
1351 result[
"datatype"] = header.datatype;
1352 result[
"bitpix"] = header.bitpix;
1354 result[
"slice_start"] = header.slice_start;
1355 result[
"pixdim"] = std::vector<float>(header.pixdim, header.pixdim+8);
1356 result[
"vox_offset"] = header.vox_offset;
1357 result[
"scl_slope"] = header.scl_slope;
1358 result[
"scl_inter"] = header.scl_inter;
1359 result[
"slice_end"] = header.slice_end;
1360 result[
"slice_code"] = int(header.slice_code);
1361 result[
"xyzt_units"] = int(header.xyzt_units);
1362 result[
"cal_max"] = header.cal_max;
1363 result[
"cal_min"] = header.cal_min;
1364 result[
"slice_duration"] = header.slice_duration;
1365 result[
"toffset"] = header.toffset;
1366 result[
"descrip"] = std::string(header.descrip, 80);
1367 result[
"aux_file"] = std::string(header.aux_file, 24);
1369 result[
"qform_code"] = header.qform_code;
1370 result[
"sform_code"] = header.sform_code;
1371 result[
"quatern_b"] = header.quatern_b;
1372 result[
"quatern_c"] = header.quatern_c;
1373 result[
"quatern_d"] = header.quatern_d;
1374 result[
"qoffset_x"] = header.qoffset_x;
1375 result[
"qoffset_y"] = header.qoffset_y;
1376 result[
"qoffset_z"] = header.qoffset_z;
1377 result[
"srow_x"] = std::vector<float>(header.srow_x, header.srow_x+4);
1378 result[
"srow_y"] = std::vector<float>(header.srow_y, header.srow_y+4);
1379 result[
"srow_z"] = std::vector<float>(header.srow_z, header.srow_z+4);
1381 result[
"intent_name"] = std::string(header.intent_name, 16);
1382 result[
"magic"] = std::string(header.magic, 4);
1384 result.attr(
"class") = Rcpp::CharacterVector::create(
"niftiHeader");
Block(const NiftiImage &image, const int dimension, const int index)
Standard constructor for this class.
Definition: NiftiImage.h:40
NiftiImage(const std::string &path, const bool readData=true)
Initialise using a path string.
Definition: NiftiImage.h:194
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:55
NiftiImage(const NiftiImage &source)
Copy constructor.
Definition: NiftiImage.h:161
Block slice(const int i)
Extract a slice block from a 3D image.
Definition: NiftiImage.h:345
Thin wrapper around a C-style nifti_image struct that allows C++-style destruction.
Definition: NiftiImage.h:20
const int index
The location along dimension.
Definition: NiftiImage.h:31
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:267
bool isPersistent() const
Determines whether or not the image is marked as persistent.
Definition: NiftiImage.h:284
nifti_image * image
The wrapped nifti_image pointer.
Definition: NiftiImage.h:90
void initFromNiftiS4(const Rcpp::RObject &object, const bool copyData=true)
Initialise the object from an S4 object of class "nifti".
Definition: NiftiImage.h:454
Inner class referring to a subset of an image.
Definition: NiftiImage.h:27
void copy(nifti_image *const source)
Copy the contents of a nifti_image to create a new image.
Definition: NiftiImage.h:403
NiftiImage()
Default constructor.
Definition: NiftiImage.h:154
Block volume(const int i)
Extract a volume block from a 4D image.
Definition: NiftiImage.h:359
bool persistent
Marker of persistence, which determines whether the nifti_image should be freed on destruction...
Definition: NiftiImage.h:91
const int dimension
The dimension along which the block applies (which should be the last)
Definition: NiftiImage.h:30
const NiftiImage & image
The parent image.
Definition: NiftiImage.h:29
void toFile(const std::string fileName, const short datatype) const
Write the image to a NIfTI-1 file.
Definition: NiftiImage.h:1092
void update(const SEXP array)
Update the image from an R array.
Definition: NiftiImage.h:941
NiftiImage(nifti_image *const image, const bool copy=false)
Initialise using an existing nifti_image pointer.
Definition: NiftiImage.h:176
static short sexpTypeToNiftiType(const int sexpType)
Convert between R SEXP object type and nifti_image datatype codes.
Definition: NiftiImage.h:79
void initFromArray(const Rcpp::RObject &object, const bool copyData=true)
Initialise the object from an R array.
Definition: NiftiImage.h:737
Rcpp::RObject headerToList() const
Create an R list containing raw image metadata.
Definition: NiftiImage.h:1333
~NiftiImage()
Destructor which frees the wrapped pointer, unless the object is marked as persistent.
Definition: NiftiImage.h:215
mat44 xform(const bool preferQuaternion=true) const
Obtain an xform matrix, indicating the orientation of the image.
Definition: NiftiImage.h:985
void rescale(const std::vector< float > &scales)
Rescales the image, changing its image dimensions and pixel dimensions.
Definition: NiftiImage.h:921
nifti_image * operator->() const
Allows a NiftiImage object to be treated as a pointer to a nifti_image.
Definition: NiftiImage.h:234
bool isNull() const
Determines whether or not the internal pointer is NULL.
Definition: NiftiImage.h:279
void updatePixdim(const std::vector< float > &pixdim)
Modify the pixel dimensions, and potentially the xform matrices to match.
Definition: NiftiImage.h:843
const Block slice(const int i) const
Extract a slice block from a 3D image.
Definition: NiftiImage.h:338
int nDims() const
Returns the number of dimensions in the image.
Definition: NiftiImage.h:289
Rcpp::RObject toArray() const
Create an R array from the image.
Definition: NiftiImage.h:1256
Rcpp::RObject toArrayOrPointer(const bool internal, const std::string label) const
A conditional method that calls either toArray or toPointer.
Definition: NiftiImage.h:1328
void initFromList(const Rcpp::RObject &object)
Initialise the object from an R list with named elements, which can only contain metadata.
Definition: NiftiImage.h:648
void initFromMriImage(const Rcpp::RObject &object, const bool copyData=true)
Initialise the object from a reference object of class "MriImage".
Definition: NiftiImage.h:550
NiftiImage & drop()
Drop unitary dimensions.
Definition: NiftiImage.h:303
const Block volume(const int i) const
Extract a volume block from a 4D image.
Definition: NiftiImage.h:352
Rcpp::RObject toPointer(const std::string label) const
Create an internal image to pass back to R.
Definition: NiftiImage.h:1315
void setPixunits(const std::vector< std::string > &pixunits)
Modify the pixel dimension units.
Definition: NiftiImage.h:896