Skip to content

extent for rasterImage angle #8

@mdsumner

Description

@mdsumner
angle_extent <- function(dimension, extent = NULL, angle = 0){

  ## Adapted from rasterImageAdj in Ecfun by Spencer Graves
  ## https://CRAN.R-project.org/package=Ecfun
  imagePixels <- dimension
  if (is.null(extent)) extent <- c(0, dimension[1], 0, dimension[2])
  xleft <- extent[1]
  xright <- extent[2]
  ybottom <- extent[3]
  ytop <- extent[4]

  ##
  ## 2.  x, y pixels per inch
  ##
  imageUnits.x <- (xright-xleft)
  imageUnits.y <- (ytop-ybottom)
  #  2.2.   plot units per inch (we should just use the scale of extent/dimension)
  u <- par("usr")
  xyinches <-  c(u[2L] - u[1L], u[4L] - u[3L])/par("pin")
  #print(xyinches)
  #print(dimension / diff(extent)[c(1, 3)])

  #  2.3.  x, y pixels per inch in image region
  pixelsPerInch.x <- (imagePixels[1]*xyinches[1] /
                        as.numeric(imageUnits.x))
  pixelsPerInch.y <- (imagePixels[2]*xyinches[2] /
                        imageUnits.y)
  ##
  ## 3.  Shrink imageUnits to max(PixelsPerInch)
  ##
  maxPPI <- max(pixelsPerInch.x, pixelsPerInch.y)
  imageUAdj.x <- (imagePixels[1] * xyinches[1] / maxPPI)
  imageUAdj.y <- (imagePixels[2] * xyinches[2] / maxPPI)
  ##
  ## 4.  (dX, dY) = imageUnitsAdj/2
  ##              = half of the (width, height) in plotting units.
  ##
  dX <- imageUAdj.x/2
  dY <- imageUAdj.y/2
  # lower left = 45 degrees from center
  # Therefore, max deviation of a corner from the center:  at 45 degrees
  dX. <- dX*sqrt(2)
  dY. <- dY*sqrt(2)
  ##
  ## 5.  cntr = (xleft, ybottom) + imageUnits/2
  ##
  cntr.x <- (xleft+(imageUnits.x/2))
  cntr.y <- (ybottom+(imageUnits.y/2))
  ##
  ## 6.  (x, y) location of the nominal lower left corner
  ##     after rotation
  ##
  adj.x <- sin((angle-45)*pi/180)*dX.
  adj.y <- cos((angle-45)*pi/180)*dY.
  xleft0 <- (cntr.x + adj.x)
  xright0 <- (xleft0 + imageUAdj.x)
  ybottom0 <- (cntr.y - adj.y)
  ytop0 <- (ybottom0 + imageUAdj.y)

  c(xleft0, xright0, ybottom0, ytop0)
}


library(png)
img <- readPNG(system.file("img", "Rlogo.png", package="png"), T)

imrot <- function(x, extent = c(0, ncol(x), 0, nrow(x)), interpolate = FALSE, add = FALSE, angle = 0, ...) {

  if (!add) {
    plot.new()
    plot.window(xlim = extent[1:2], ylim = extent[3:4], asp = 1)
  }
  ## use of par in here means plot must already be set up
   extent <- angle_extent(dim(x), extent, angle = angle)

  rasterImage(x, extent[1], extent[3], extent[2], extent[4], interpolate = interpolate, angle = angle, ...)
  invisible(extent)
}


library(png)
img <- readPNG(system.file("img", "Rlogo.png", package="png"), T)
imrot(img)
imrot(img, angle = -90, add = TRUE)

image

should be able to unpick all the silly graphics device stuff and just use dimension and extent (and this possibly belongs in vaster not here)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions