Skip to content

bmp() shifts the image (Windows XP)

6 messages · Duncan Murdoch, William Simpson, Ben Tupper

#
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
#
On 12-01-01 9:05 AM, William Simpson wrote:
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
#
Thanks Duncan for your help.
Bill
On 1/1/12, Duncan Murdoch <murdoch.duncan 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:
#
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:
#
Hi,
On Jan 2, 2012, at 7:00 AM, William Simpson wrote:

            
I agree that it can be very confusing to navigate the many tricks of working with images in R.  I don't know if this helps in your particular case, but for display purposes, the newish rasterImage() is the way to go.  Raster graphics in R got a big boost with the addition of native raster support - see http://journal.r-project.org/archive/2011-1/RJournal_2011-1_Murrell.pdf  Here's a very simple example...

n <- 50
x <- seq(from = -5, to = 20, length = n)
y <- seq(from = 38, to = 45, length = n)
z <- matrix(runif(n*n, min = 0, max = 1), nrow = n, ncol = n)
plot(x,y, typ = "n")
rasterImage(z, x[1], y[1], x[n], y[n])


For pixel-to-pixel output, you might check out the savemat() function in the package 'squash'.

Cheers,
Ben
Ben Tupper
Bigelow Laboratory for Ocean Sciences
180 McKown Point Rd. P.O. Box 475
West Boothbay Harbor, Maine   04575-0475 
http://www.bigelow.org