shuffleIm <- function (X,nx,ny) {
verifyclass(X,"im")
ddd <- dim(X)
if(ddd[1]%%ny != 0) stop("Argument \"ny\" must divide first dimension of image.\n")
if(ddd[2]%%nx != 0) stop("Argument \"nx\" must divide second dimension of image.\n")
ky <- ddd[1]/ny
kx <- ddd[2]/nx
n  <- nx*ny
ind <- sample(1:n,n)
V <- matrix(NA,ddd[1],ddd[2])
for(k in 1:n) {
	j  <- k%%nx
	if(j==0) j <- nx
	i  <- (k-j)/nx + 1
	j1 <- (j-1)*kx + 1:kx
	i1 <- (i-1)*ky + 1:ky
	kr <- ind[k]
	j  <- kr%%nx
	if(j==0) j <- nx
	i  <- (kr-j)/nx + 1
	j2 <- (j-1)*kx + 1:kx
	i2 <- (i-1)*ky + 1:ky
	V[i1,j1] <- X$v[i2,j2]
}
X$v <- V
X}
