Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
LICENSE.md
.github
.git
^\.positai$
^\.claude$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.Ruserdata
inst/.DS_Store
*.Rproj
.positai
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Imports:
Rarr,
ZarrArray,
magick,
tools
tools,
BiocGenerics
Suggests:
testthat (>= 3.0.0),
knitr,
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ exportMethods("[")
exportMethods("[[")
exportMethods("[[<-")
exportMethods("path<-")
exportMethods(affine)
exportMethods(aperm)
exportMethods(axes)
exportMethods(crop)
exportMethods(dim)
exportMethods(extent)
exportMethods(flip)
exportMethods(flop)
exportMethods(length)
Expand All @@ -25,8 +27,11 @@ exportMethods(negate)
exportMethods(path)
exportMethods(realize)
exportMethods(rotate)
exportMethods(scale)
exportMethods(translation)
exportMethods(type)
import(DelayedArray)
importFrom(BiocGenerics,scale)
importFrom(DelayedArray,DelayedArray)
importFrom(DelayedArray,path)
importFrom(EBImage,flip)
Expand Down
33 changes: 9 additions & 24 deletions R/ImageArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#' }
#' }
#' @param drop ignored
#' @param angle value between 0 and 360 for degrees to rotate
#' @param brightness the brightness of the new image in percentage, e.g. 120
#' @param perm perm
#' @param index a named or unnamed list of indices for cropping/subsetting the
Expand All @@ -30,14 +29,8 @@
#' @aliases
#' [[,ImageArray,numeric-method
#' [[<-,ImageArray,numeric-method
#' rotate
#' rotate,ImageArray-method
#' crop
#' crop,ImageArray-method
#' flip
#' flip,ImageArray-method
#' flop
#' flop,ImageArray-method
#' negate
#' negate,ImageArray-method
#' modulate
Expand All @@ -52,6 +45,8 @@
#' as.raster,ImageArray-method
#' path
#' path,ImageArray-method
#' extent
#' extent,ImageArray-method
#'
#' @examples
#' # get image
Expand Down Expand Up @@ -89,6 +84,9 @@
#'
#' # realize
#' imgarray <- realize(imgarray)
#'
#' # extent
#' extent(imgarray)
NULL

#' @describeIn ImageArray-methods subset and crop
Expand All @@ -99,12 +97,6 @@ setMethod(
f = "[",
signature = c("ImageArray"),
function(x, i, j, ..., drop = FALSE) {
if (missing(x)) {
stop("'x' is missing")
}
if (!.isTRUEorFALSE(drop)) {
stop("'drop' must be TRUE or FALSE")
}
Nindex <- S4Arrays:::extract_Nindex_from_syscall(sys.call(), parent.frame())
crop(x, index = Nindex)
}
Expand Down Expand Up @@ -580,17 +572,10 @@ writeImageArray <- function(
if(format == "hdf5") format <- "h5"
if(fileext != format && format != "in-memory") {
warning(
sprintf(
paste(
"The file extension of the output path%s does",
"not match the specified format (%s),",
"The object will be written as (%s).",
sep = " "
),
if (fileext == "") "" else paste0(" (", fileext, ")"),
format,
format
)
"The file extension of the output path",
if (fileext == "") "" else sprintf(" '%s'", fileext),
" does not match the specified format (", format, "). ",
"The object will be saved as ", format, " format. "
)
}
}
Expand Down
9 changes: 9 additions & 0 deletions R/allgenerics.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#' @importFrom BiocGenerics scale

# generics of ImageArray
setGeneric("crop", function(object, ...) standardGeneric("crop"))
setGeneric("negate", function(object, ...) standardGeneric("negate"))
setGeneric("modulate", function(object, ...) standardGeneric("modulate"))
setGeneric("meta", function(object, ...) standardGeneric("meta"))
setGeneric("axes", function(object, ...) standardGeneric("axes"))
setGeneric("extent", \(x, ...) standardGeneric("extent"))

# generics from EBImage
setGeneric("rotate")
setGeneric("flip")
setGeneric("flop")

# transformations
setGeneric("affine", \(x, ...) standardGeneric("affine"))
setGeneric("translation", \(x, ...) standardGeneric("translation"))
setGeneric("rotate_transform",
\(x, ...) standardGeneric("rotate_transform"))
71 changes: 0 additions & 71 deletions R/manipulation.R
Original file line number Diff line number Diff line change
@@ -1,43 +1,3 @@
#' @importFrom EBImage rotate flip flop
NULL

#' @describeIn ImageArray-methods rotate image array to 90, 180, 270 degrees
#' @export
setMethod("rotate", signature = "ImageArray", function(x, angle) {
# validate rotation
if (!angle %in% c(0, 90, 180, 270, 360)) {
stop("Only rotations of 0,90,180,270,360 degrees are supported!")
}

# check dimensions
.check_dim(x)
dim_img <- dim(x[[1]])
ax <- axes(x)

# array perm.
if (angle %in% c(90, 270)) {
cur_perm <- .swap(
seq_along(dim_img),
which(ax == "x"),
which(ax == "y")
)
x <- aperm(x, perm = cur_perm)
}

# flop
if (angle %in% c(90, 180)) {
x <- flop(x)
}

# flip
if (angle %in% c(180, 270)) {
x <- flip(x)
}

# return
x
})

#' @describeIn ImageArray-methods permute image
#' @exportMethod aperm
setMethod("aperm", signature = "ImageArray", function(a, perm) {
Expand Down Expand Up @@ -74,37 +34,6 @@ setMethod("modulate", signature = "ImageArray", function(object, brightness) {
object
})

#' @importFrom stats setNames
#' @noRd
.flipflop <- function(object, direction = "x") {
ax <- axes(object)

# check dim
.check_dim(object)

# flip all
for (i in seq_along(object@levels)) {
img <- object[[i]]
dim_img <- stats::setNames(dim(img), ax)
cur_ind <- stats::setNames(lapply(dim_img, seq_len), ax)
cur_ind[[direction]] <- rev(cur_ind[[direction]])
object[[i]] <- .subset_array(object[[i]], cur_ind, drop = FALSE)
}
object
}

#' @describeIn ImageArray-methods vertical flipping image
#' @export
setMethod("flip", signature = "ImageArray", function(x) {
.flipflop(x, direction = "y")
})

#' @describeIn ImageArray-methods horizontal flipping image
#' @export
setMethod("flop", signature = "ImageArray", function(x) {
.flipflop(x, direction = "x")
})

#' @describeIn ImageArray-methods cropping image
#' @importFrom utils head tail
#' @importFrom stats setNames
Expand Down
Loading
Loading