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

William Simpson william.a.simpson at gmail.com
Mon Jan 2 09:41:11 CET 2012


Thanks Duncan for your help.
Bill

On 1/1/12, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
> On 12-01-01 9:05 AM, William Simpson wrote:
>> When using bmp() under Windows XP, I find that the saved image is a
>> shifted version of the correct image. Try this:
>
> The image() function isn't designed to be able to do pixel-level
> addressing, so it's not too surprising that some rounding error
> somewhere leads to this.  You could look through the Windows graphics
> device code to fix it.
>
> However, if you really need pixel level addressing, you should be using
> raster objects.  I don't know if someone has written code to output a
> .bmp file, but it's a very simple format, so it shouldn't be too hard,
> especially if you only need a limited range of pixel formats (e.g.
> grayscale).
>
> Duncan Murdoch
>
>>
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list