Skip to content

Instantly share code, notes, and snippets.

@pepijn-devries
Created March 27, 2018 20:04
Show Gist options
  • Save pepijn-devries/a59c6e82636d3ac0c925d535bbefd102 to your computer and use it in GitHub Desktop.
Save pepijn-devries/a59c6e82636d3ac0c925d535bbefd102 to your computer and use it in GitHub Desktop.
dither image
## 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment