Skip to content

Commit

Permalink
Reinstating special treatment of string arguments to analyzeHeader(),…
Browse files Browse the repository at this point in the history
… but now explicitly ignoring internal images
  • Loading branch information
jonclayden committed Sep 5, 2020
1 parent 1069962 commit 1c725b6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
14 changes: 13 additions & 1 deletion R/nifti.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ updateNifti <- function (image, template = NULL, datatype = "auto")
#' ANALYZE \code{originator} field to store a coordinate origin. This
#' interpretation is also returned, in the \code{origin} field.
#'
#' Both of these functions call \code{\link{asNifti}} on their arguments to
#' coerce it to NIfTI, except in one specific circumstance: when
#' \code{analyzeHeader} is called with a single-element character-mode
#' argument that is not an \code{"internalImage"} object. In this case the
#' string is taken to be a path and the header is reported as stored on disk.
#' This is because otherwise the header may be changed by the process of
#' converting it to NIfTI and back.
#'
#' @examples
#' niftiHeader(system.file("extdata", "example.nii.gz", package="RNifti"))
#'
Expand All @@ -238,7 +246,11 @@ niftiHeader <- dumpNifti <- function (image = list())
#' @export
analyzeHeader <- function (image = list())
{
.Call("analyzeHeader", asNifti(image,internal=TRUE), PACKAGE="RNifti")
# Special case needed to avoid converting to NIfTI internally first
if (is.character(image) && length(image) == 1 && !inherits(image,"internalImage"))
.Call("analyzeHeader", image, PACKAGE="RNifti")
else
.Call("analyzeHeader", asNifti(image,internal=TRUE), PACKAGE="RNifti")
}

#' @rdname niftiHeader
Expand Down
8 changes: 8 additions & 0 deletions man/niftiHeader.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 25 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,32 @@ END_RCPP
RcppExport SEXP analyzeHeader (SEXP _image)
{
BEGIN_RCPP
const NiftiImage image(_image, false, true);
if (image.isNull())
return R_NilValue;

RObject object(_image);
nifti_1_header header;
nifti_convert_nim2n1hdr(image, &header);

// This special-case treatment of strings is important, because converting
// ANALYZE files into NIfTI objects and back is somewhat destructive. It
// mustn't pick up internal images by accident, though
if (Rf_isString(object) && !object.hasAttribute(".nifti_image_ptr"))
{
const std::string path = as<std::string>(object);
int version;
void *ptr = nifti2_read_header(RNifti::internal::stringToPath(path), &version, true);
if (ptr == NULL)
return R_NilValue;
else if (version < 0 || version > 1)
Rf_error("File is not in ANALYZE-7.5 or NIfTI-1 format");
header = *((nifti_1_header *) ptr);
free(ptr);
}
else
{
const NiftiImage image(_image, false, true);
if (image.isNull())
return R_NilValue;
nifti_convert_nim2n1hdr(image, &header);
}

nifti_analyze75 *analyze = (nifti_analyze75 *) &header;
List result;

Expand Down

0 comments on commit 1c725b6

Please sign in to comment.