## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5
)

## ----setup--------------------------------------------------------------------
library(ximage)

## ----xtext--------------------------------------------------------------------
(m <- matrix(1:12, nrow = 3, byrow = TRUE))

ximage(m)
xtext(m, add = TRUE)

## ----image-vs-----------------------------------------------------------------
op <- par(mfrow = c(1, 2))
image(m, main = "image()")
ximage(m, main = "ximage()")
par(op)

## ----roundtrip----------------------------------------------------------------
vals <- 1:12                # the pixel stream, raster order
dimension <- c(4L, 3L)      # (ncol, nrow), as a reader declares it

m <- matrix(vals, dimension[2L], byrow = TRUE)
m

## and back again
all(as.vector(t(m)) == vals)

## ----reader-------------------------------------------------------------------
l <- structure(list(vals), dimension = dimension, extent = c(0, 4, 0, 3))
ximage(l)
xtext(l, add = TRUE)

## ----bands--------------------------------------------------------------------
nbands <- 3L
stream <- runif(prod(dimension) * nbands)

a <- aperm(array(stream, c(dimension[1:2], nbands)), c(2, 1, 3))
dim(a)  ## (nrow, ncol, nbands)
ximage(a)

## ----extent-------------------------------------------------------------------
ximage(m, extent = c(140, 148, -44, -38))
xtext(m, extent = c(140, 148, -44, -38), add = TRUE)
axis(1); axis(2)
abline(v = seq(140, 148, by = 2), h = seq(-44, -38, by = 2), lty = 3)

## ----contour------------------------------------------------------------------
ximage(volcano)
xcontour(volcano, add = TRUE, col = "white")

