
require(grDevices) # for colours

## read modified image.R file, from wherever, if necessary
## source("~/R/r-devel/R/src/library/graphics/R/image.R")

tmpf <- function(ifun=image) {
  ifun(matrix(1:25,5),col=1:25)
  x <- y <- seq(-4*pi, 4*pi, len=27)
  r <- sqrt(outer(x^2, y^2, "+"))
  ifun(z = z <- cos(r^2)*exp(-r/6), col=gray((0:32)/32))
  ifun(z, axes = FALSE, main = "Math can be beautiful ...",
        xlab = expression(cos(r^2) * e^{-r/6}))
  contour(z, add = TRUE, drawlabels = FALSE)
  
  ## Volcano data visualized as matrix. Need to transpose and flip
  ## matrix horizontally.
  ifun(t(volcano)[ncol(volcano):1,])

  ## A prettier display of the volcano
  x <- 10*(1:nrow(volcano))
  y <- 10*(1:ncol(volcano))
  ifun(x, y, volcano, col = terrain.colors(100), axes = FALSE)
  contour(x, y, volcano, levels = seq(90, 200, by = 5),
          add = TRUE, col = "peru")
  axis(1, at = seq(100, 800, by = 100))
  axis(2, at = seq(100, 600, by = 100))
  box()
  title(main = "Maunga Whau Volcano", font.main = 4)
}

pdf("image1.pdf")
tmpf()
dev.off()

pdf("image2.pdf")
tmpf(ifun=function(...) image(...,useRaster=TRUE))
dev.off()

st1 <- system.time(replicate(1000,image(matrix(1:25,5),col=1:25)))
st2 <- system.time(replicate(1000,image(matrix(1:25,5),col=1:25,
                                        useRaster=TRUE)))
## 21.3 (standard) vs 22.9 (raster) seconds

st3 <- system.time(replicate(1000,image(matrix(1:10000,100))))
st4 <- system.time(replicate(1000,image(matrix(1:10000,100),
                                        useRaster=TRUE)))
## 149 (standard) vs 31 (raster) seconds
