R/resize.R

Defines functions resize resize_arr resize_matrix

Documented in resize

#' Resize an image
#'
#' Decreases the side of an image so it can be used smoothly by other functions.
#' Returns a raster.
#' @param img source of any supported type, see ?convert
#' @param resize resize factor between 0 and 1
#' @param from source format, guessed by default
resize <- function(img,resize,
                   from = c("auto","raster","url","path",
                            "array3","matrix","long")){
  from  <-match.arg(from)
  if(from == "auto") from <- guess_format(img)
  if(from == "array3"){
    img <- convert(resize_arr(img,resize),to="raster")
  } else
  {
    if (from != "marix") img <- convert(img,to="matrix")
    img <- as.raster(resize_mat(img,resize))
  }
  img
}

resize_arr <- function(img,resize){
  coords <- lapply(dim(img)[-3],function(x){
    n <-round(x*resize)
    round(seq(1,x,(x-1)/(n-1)))
  })
  img <- img[coords[[1]],coords[[2]],]
  img
}

resize_matrix <- function(img,resize){
  coords <- lapply(dim(img),function(x){
    n <-round(x*resize)
    round(seq(1,x,(x-1)/(n-1)))
  })
  img <- img[coords[[1]],coords[[2]]]
  img
}
moodymudskipper/tweak documentation built on May 20, 2019, 8:49 a.m.