[R] bmp() shifts the image (Windows XP)

William Simpson william.a.simpson at gmail.com
Sun Jan 1 15:05:31 CET 2012


When using bmp() under Windows XP, I find that the saved image is a
shifted version of the correct image. Try this:

n<-5
fn<-"01.bmp"
x<-matrix(runif(n*n),nrow=n)
image(x,col=gray(0:255/255),axes=F,frame.plot=F)
bmp(filename = fn,width = n, height = n, units = "px")
par(mar=c(0,0,0,0),pty="s")
image(x,col=gray(0:255/255),axes=F,frame.plot=F)
dev.off()

The image 01.bmp is like this:
22 23 24 25 w
32 33 34 35 w
42 43 44 45 w
52 53 54 55 w
w w w w w w
Where 22 represents x[2,2], etc; w represents a white pixel.

For my application, the image has to be .bmp format. The same shifting
behaviour is seen for large values of n. It is not just due to the
small n value.

For my application, this shifting is important and has to be
eliminated. Please help.

On an unrelated note, I found out that the bmp() code is "smart"
enough to write my image as 8-bit using a palette instead of 24-bit
with 0:255 grey levels if the image being saved does not use all 256
grey levels. I would love to hear it if somebody knows a good way to
make bmp() stupid and always save as 24-bit. My kludge, using 256x256
pixel images, is to tack on an extra row with grey levels 0:255. Then
when displaying, I have to crop the image to get rid of that bogus
row.

Thanks very much for any help!
Bill



More information about the R-help mailing list