R/rotate_reverse.r

Defines functions rotate3 rotate2 rotate1 reverse_slash reverse_backslash reverse_v reverse_h reverse rotate

Documented in reverse rotate

#' Rotate or reverse picture
#'
#' @param img raster object
#' @param rot rotation (integer clockwise)
#' @export
rotate <- function(img,rot = 1){
  assertthat::assert_that(class(img) == "raster")
  assertthat::assert_that(is.numeric(rot) && (rot %% 4) %in% (0:3))
  rot <- rot %% 4
  if("character" %in% class(img)) img  <- urlpath2array(img)
  img <- as.raster(c(identity,rotate1,rotate2,rotate3)[[rot+1]](as.matrix(img)))
}

#' Rotate or reverse picture
#'
#' @param img img
#' @param dir direction to reverse the picture
#' @export
reverse <- function(img,dir = c("h","v","slash","backslash")){
  dir = match.arg(dir)
  img <- as.raster(c(reverse_h,reverse_v,reverse_slash,reverse_backslash)[[
    match(dir,c("h","v","slash","backslash"))]](as.matrix(img)))
}

reverse_h <- function(mat) mat[nrow(mat):1,,drop=FALSE]
reverse_v <- function(mat) mat[,ncol(mat):1,drop=FALSE]
reverse_backslash <- function(mat) t(mat)
reverse_slash <- function(mat) t(mat[nrow(mat):1,ncol(mat):1,drop=FALSE])
rotate1  <- function(mat) t(mat[nrow(mat):1,,drop=FALSE])
rotate2  <- function(mat) mat[nrow(mat):1,ncol(mat):1,drop=FALSE]
rotate3  <- function(mat) t(mat[,ncol(mat):1,drop=FALSE])
moodymudskipper/tweak documentation built on Dec. 5, 2017, 12:33 a.m.