Skip to content

Instantly share code, notes, and snippets.

@statistic-on-air
Created August 29, 2011 08:46
Show Gist options
  • Save statistic-on-air/1178030 to your computer and use it in GitHub Desktop.
Save statistic-on-air/1178030 to your computer and use it in GitHub Desktop.
# Bill Atkinson dithering
AtkinConvolution <- function(a){
c <- matrix(0, nrow=dim(a)[1], ncol=dim(a)[2])
for(i in 2:(dim(a)[1]-2)){
for(j in 2:(dim(a)[2]-2)){
P <- trunc(a[i,j]+0.5)
e <- a[i,j] - P
a[i,j] <- P
a[i,j+1] <- a[i,j+1] + (e * 1/8)
a[i,j+2] <- a[i,j+2] + (e * 1/8)
a[i+1,j-1] <- a[i+1,j-1] + (e * 1/8)
a[i+1,j] <- a[i+1,j] + (e * 1/8)
a[i+1,j+1] <- a[i+1,j+1] + (e * 1/8)
a[i+2,j] <- a[i+2,j] + (e * 1/8)
}
}
a
}
grey2ATKdith <- function(img){
greyMatrix <- img[1:nrow(img),1:ncol(img)]
dim1 <- 2
dim2 <- 2
dim1x <- 2
dim2x <- 2
dim1a <- dim(greyMatrix)[1]
dim2a <- dim(greyMatrix)[2]
tempMatrix <- matrix(0.5, nrow=nrow(greyMatrix)+2*2, ncol=ncol(greyMatrix)+2*2)
tempMatrix[3:(nrow(tempMatrix)-2),3:(ncol(tempMatrix)-2)] <- greyMatrix
igrey <- AtkinConvolution(tempMatrix)
imagematrix(igrey[3:(nrow(igrey)-2),3:(ncol(igrey)-2)], type="grey", noclipping=TRUE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment