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

William Simpson william.a.simpson at gmail.com
Mon Jan 2 15:04:01 CET 2012


I have figured out what I wanted to do using pixmap. Pixmap writes
.pgm files which I will batch convert to .bmp using Irfanview.

Thanks for your help.

Cheers,
Bill

On 1/2/12, William Simpson <william.a.simpson at gmail.com> wrote:
> Duncan, I checked out as.raster as you suggested. However, I can't
> find info on how to display and save a raster object. What a raster
> object is eludes me. These were fruitless
>  ?as.raster
>  ?grDevices
>  library(help="grDevices")
>  ?windows
>  ?savePlot
>
> x<-as.raster(matrix(runif(n*n),nrow=n))
>
> How to display x? plot(x) and image(x) don't work.
> How to save the bitmap image x?
>
> Not sure if I need to scale x to be in range 0-255 before doing
> as.raster().
>
> I see that x contains elements like "#676767".
> col2rgb("#676767")
>       [,1]
> red    103
> green  103
> blue   103
> That doesn't help me display and save bitmap images.
>
> I see there is a package called raster but I'd rather avoid it, given
> my very simple needs. Maybe package pixmap will work, then convert the
> saved images to .bmp (e.g. using Irfanview in batch mode).
>
> Thanks very much for any 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