Skip to content

Commit

Permalink
Add indexing and as.raster methods for rgbArray
Browse files Browse the repository at this point in the history
  • Loading branch information
jonclayden committed Apr 3, 2024
1 parent 450797d commit 7805ba8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
S3method("$",niftiImage)
S3method("$<-",niftiImage)
S3method("[",internalImage)
S3method("[",rgbArray)
S3method("[<-",internalImage)
S3method("dim<-",internalImage)
S3method("pixdim<-",default)
S3method("pixunits<-",default)
S3method(as.array,internalImage)
S3method(as.character,rgbArray)
S3method(as.raster,rgbArray)
S3method(asNifti,default)
S3method(dim,internalImage)
S3method(pixdim,default)
Expand Down
36 changes: 33 additions & 3 deletions R/rgb.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' attribute (with value 3 or 4) indicates how many channels are being used.
#' The resulting array can be used to construct an RGB(A) NIfTI image, or
#' converted to standard R colour strings using the \code{as.character} method.
#' The indexing method returns another object of the same type.
#'
#' @param red A numeric vector (or array) of red channel values. If this is the
#' only channel argument, it can also be a character vector of colour values
Expand All @@ -28,9 +29,11 @@
#' will be retained in the result. The default is \code{TRUE}, for
#' consistency with the usual behaviour of \code{as.character}, which strips
#' all attributes.
#' @return \code{rgbArray} returns an integer-mode array of class
#' \code{"rgbArray"}. The \code{as.character} method returns a character-mode
#' vector of colour strings.
#' @return \code{rgbArray} and the indexing (\code{[}) method return an
#' integer-mode array of class \code{"rgbArray"}. The \code{as.raster}
#' method returns a \code{raster} object, valid for 2D arrays only. The
#' \code{as.character} method returns a character-mode vector of colour
#' strings with or without dimensions.
#'
#' @note The values of an \code{"rgbArray"} are not easily interpreted, and
#' may depend on the endianness of the platform. For manipulation or use as
Expand Down Expand Up @@ -87,6 +90,33 @@ rgbArray <- function (red, green, blue, alpha, max = NULL, dim = NULL, ...)
return (structure(result, ..., channels=channels, dim=dim, class="rgbArray"))
}

#' @rdname rgbArray
#' @export
"[.rgbArray" <- function (x, i, j, ..., drop = TRUE)
{
result <- NextMethod()
if (is.null(dim(result)))
dim(result) <- length(result)
return (structure(result, channels=attr(x,"channels"), class="rgbArray"))
}

#' @rdname rgbArray
#' @export
as.raster.rgbArray <- function (x, ...)
{
dims <- dim(x)
nDims <- length(dims)
if (nDims > 2L)
stop("Raster objects cannot have more than two dimensions")

result <- .Call("rgbToStrings", x, PACKAGE="RNifti")
if (nDims == 2L)
result <- matrix(result, nrow=dims[1], ncol=dims[2], byrow=TRUE)
else
result <- matrix(result, ncol=1L)
return (structure(result, class="raster"))
}

#' @rdname rgbArray
#' @export
as.character.rgbArray <- function (x, flatten = TRUE, ...)
Expand Down
15 changes: 12 additions & 3 deletions man/rgbArray.Rd

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

0 comments on commit 7805ba8

Please sign in to comment.