diff --git a/NAMESPACE b/NAMESPACE index aaf9dd3..ff8f478 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -23,8 +23,7 @@ export(nr_polygon) export(nr_polyline) export(nr_rect) export(nr_replace) -export(nr_resize_bilinear) -export(nr_resize_nn) +export(nr_resize) export(nr_scale) export(nr_text_basic) export(nr_to_array) @@ -36,4 +35,6 @@ export(packed_cols_to_hex_cols) export(raster_to_nr) export(str_cols_to_packed_cols) import(grid) +importFrom(grDevices,dev.flush) +importFrom(grDevices,dev.hold) useDynLib(nara, .registration=TRUE) diff --git a/R/nr-scale.R b/R/nr-scale.R index 2e453e4..0ca93d3 100644 --- a/R/nr-scale.R +++ b/R/nr-scale.R @@ -6,94 +6,55 @@ #' #' @inheritParams nr_fill #' @param scale scale factor +#' @param algo 'nn' for nearest neighbour (the default), or 'bilinear' for +#' bilinear interpolation. #' #' @return New \code{nativeRaster} -#' -#' @examples -#' nr1 <- nr_new(200, 100, 'white') -#' nr2 <- nr_scale(nr1, 2) -#' -#' @export -#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -nr_scale <- function(nr, scale) { - nr_resize_nn(nr, scale * ncol(nr), scale * nrow(nr)) -} - - -#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -#' Scale a nativeRaster using nearest-neighbour interpolation -#' -#' @param nr native raster -#' @param width,height new dimensions -#' @return new nativeRaster #' @examples -#' stretched <- nr_resize_nn(deer_sprites[[1]], 100, 40) -#' plot(stretched) +#' big <- nr_scale(deer_sprites[[1]], 2) +#' plot(big) #' @export #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -nr_resize_nn <- function(nr, width, height) { - .Call(resize_nn_, nr, width, height) +nr_scale <- function(nr, scale, algo = 'nn') { + nr_resize(nr, width = scale * ncol(nr), height = scale * nrow(nr), algo = algo) } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -#' Scale with bilinear interpolation +#' Scale a nativeRaster #' #' @param nr native raster +#' @param algo 'nn' for nearest neighbour (the default), or 'bilinear' for +#' bilinear interpolation. #' @param width,height new dimensions -#' @return new nativeRaster +#' @return New \code{nativeRaster} #' @examples -#' stretched <- nr_resize_bilinear(deer_sprites[[1]], 100, 40) +#' stretched <- nr_resize(deer_sprites[[1]], 100, 40, algo = 'nn') #' plot(stretched) #' @export #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -nr_resize_bilinear <- function(nr, width, height) { - .Call(resize_bilinear_, nr, width, height) +nr_resize <- function(nr, width, height, algo = 'nn') { + if (algo == 'nn') { + .Call(resize_nn_, nr, width, height) + } else { + .Call(resize_bilinear_, nr, width, height) + } } if (FALSE) { - - # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - # There's an off-by-one error in nr_resize_nn - # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! nr <- png::readPNG(system.file("img", "Rlogo.png", package="png"), native = TRUE) dim(nr) - nr_scale (nr, 7) |> plot(T) - nr_resize_nn (nr, 7 * ncol(nr), 7 * nrow(nr)) |> plot(T) - - nr_scale (nr, 4) |> bench::mark() - nr_resize_nn (nr, 4 * ncol(nr), 4 * nrow(nr)) |> bench::mark() - - - nr_resize_bilinear(nr, 300, 150) |> plot(T) - - - nr_resize_nn (nr, 300, 150) |> plot(T) - nr_resize_bilinear(nr, 300, 150) |> plot(T) - - - nr_resize_bilinear(nr, 800, 800) |> plot(T) - nr_resize_bilinear(nr, 800, 800) |> bench::mark() - library(narascale) - nr_resize(nr, 800, 800) |> plot(T) - nr_resize(nr, 800, 800) |> bench::mark() - - - library(magick) - # plot(nr, T) - im <- nr_to_magick(nr) - im2 <- image_resize(im, geometry = geometry_size_pixels(300, 150, preserve_aspect = FALSE)) - # image_resize(im, geometry = geometry_size_pixels(300, 150, preserve_aspect = FALSE)) |> bench::mark() - # im2 - nr2 <- magick_to_nr(im2) - plot(nr2, T) + nr_scale(nr, 7) |> plot(T) + nr_resize(nr, 7 * ncol(nr), 7 * nrow(nr)) |> plot(T) + nr_scale (nr, 4) |> bench::mark() + nr_resize(nr, 4 * ncol(nr), 4 * nrow(nr)) |> bench::mark() + nr_resize(nr, 4 * ncol(nr), 4 * nrow(nr), algo = 'bilinear') |> bench::mark() + } diff --git a/R/utils.R b/R/utils.R index 15dee6f..7881b3e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -31,12 +31,13 @@ is_nativeraster <- function(x) { #' plot(nr) #' #' @import grid +#' @importFrom grDevices dev.flush dev.hold #' @export #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ plot.nativeRaster <- function(x, y, ...) { if (!missing(y)) grid::grid.newpage() - dev.hold() + grDevices::dev.hold() grid::grid.raster(x, interpolate = FALSE, ...) - dev.flush() + grDevices::dev.flush() invisible(x) } \ No newline at end of file diff --git a/man/nr_resize.Rd b/man/nr_resize.Rd new file mode 100644 index 0000000..7f6bd0f --- /dev/null +++ b/man/nr_resize.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/nr-scale.R +\name{nr_resize} +\alias{nr_resize} +\title{Scale a nativeRaster} +\usage{ +nr_resize(nr, width, height, algo = "nn") +} +\arguments{ +\item{nr}{native raster} + +\item{width, height}{new dimensions} + +\item{algo}{'nn' for nearest neighbour (the default), or 'bilinear' for +bilinear interpolation.} +} +\value{ +New \code{nativeRaster} +} +\description{ +Scale a nativeRaster +} +\examples{ +stretched <- nr_resize(deer_sprites[[1]], 100, 40, algo = 'nn') +plot(stretched) +} diff --git a/man/nr_resize_bilinear.Rd b/man/nr_resize_bilinear.Rd deleted file mode 100644 index a77b2ac..0000000 --- a/man/nr_resize_bilinear.Rd +++ /dev/null @@ -1,23 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/nr-scale.R -\name{nr_resize_bilinear} -\alias{nr_resize_bilinear} -\title{Scale with bilinear interpolation} -\usage{ -nr_resize_bilinear(nr, width, height) -} -\arguments{ -\item{nr}{native raster} - -\item{width, height}{new dimensions} -} -\value{ -new nativeRaster -} -\description{ -Scale with bilinear interpolation -} -\examples{ -stretched <- nr_resize_bilinear(deer_sprites[[1]], 100, 40) -plot(stretched) -} diff --git a/man/nr_resize_nn.Rd b/man/nr_resize_nn.Rd deleted file mode 100644 index 764540a..0000000 --- a/man/nr_resize_nn.Rd +++ /dev/null @@ -1,23 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/nr-scale.R -\name{nr_resize_nn} -\alias{nr_resize_nn} -\title{Scale a nativeRaster using nearest-neighbour interpolation} -\usage{ -nr_resize_nn(nr, width, height) -} -\arguments{ -\item{nr}{native raster} - -\item{width, height}{new dimensions} -} -\value{ -new nativeRaster -} -\description{ -Scale a nativeRaster using nearest-neighbour interpolation -} -\examples{ -stretched <- nr_resize_nn(deer_sprites[[1]], 100, 40) -plot(stretched) -} diff --git a/man/nr_scale.Rd b/man/nr_scale.Rd index 561e56d..041a05e 100644 --- a/man/nr_scale.Rd +++ b/man/nr_scale.Rd @@ -4,12 +4,15 @@ \alias{nr_scale} \title{Scale the size of a \code{nativeRaster} using Nearest Neighbour resizinng} \usage{ -nr_scale(nr, scale) +nr_scale(nr, scale, algo = "nn") } \arguments{ \item{nr}{\code{nativeRaster}} \item{scale}{scale factor} + +\item{algo}{'nn' for nearest neighbour (the default), or 'bilinear' for +bilinear interpolation.} } \value{ New \code{nativeRaster} @@ -18,7 +21,6 @@ New \code{nativeRaster} Scale the size of a \code{nativeRaster} using Nearest Neighbour resizinng } \examples{ -nr1 <- nr_new(200, 100, 'white') -nr2 <- nr_scale(nr1, 2) - +big <- nr_scale(deer_sprites[[1]], 2) +plot(big) }