## Library to handle Amiga files
library(AmigaFFH)
## Library to handle png-images
library(png)

## Download an image of the Amiga boing ball:
con <- url("http://www.f1-software.com/images/icons/amiga-boing-ball.png", "rb")
logo <- readBin(con, "raw", 100000L)
close(con)
logo <- as.raster(readPNG(logo))

## Setup arguments for dithering the image in 15 different ways
args <- data.frame(length.out = c(rep(2^(2:6), 2), rep(2, 5)),
                   palette    = c(rep(NA, 10), rep("bw", 5)),
                   dither     = c(rep("none", 5),
                                  rep("atkinson", 5),
                                  c("none", "floyd-steinberg", "JJN", "stucki", "atkinson")))

## Mapply the arguments as listed above
result <- mapply(function(length.out, dither, palette) {
  if (!is.na(palette) && palette == "bw") palette <- c("black", "white")
  if (any(is.na(palette))) palette <- NULL
  logo.out <- index.colours(logo,
                            colour.depth = "24 bit",
                            length.out   = length.out,
                            dither       = dither,
                            palette      = palette)
  logo.out <- as.raster(apply(logo.out, 2,
                              function(x) attributes(logo.out)$palette[x]))
},
length.out = args$length.out,
dither     = as.character(args$dither),
palette    = args$palette,
SIMPLIFY   = F)

## Plot the images and save as png:
png("test.png", width = dim(result[[1]])[[1]]*5, height = dim(result[[1]])[[1]]*3)
par(mar = rep(0, 4), mfrow = c(3,5))
## plot all results without interpolation and remove all margin space:
lapply(result, plot, interpolate = F, xaxs = "i", yaxs = "i")
dev.off()