Skip to content
Prev 294028 / 398503 Next

PPM to BMP converter

You could write it to BMP with rgdal, i.e. with dummy "world" coordinates:

x1 <- list(x = 1:5, y = 1:4, z = matrix(1:20, 5, 4))

require(sp)
x2 <- image2Grid(x1)

require(rgdal)
writeGDAL(x2, "file.bmp", driver = "BMP", type = "Byte", mvFlag = 255)

(I am not familiar with the details of what BMP can support, so I just
limit to Byte and stay safe. )

To show the round trip, convert back to "image" xyz list:

as.image.SpatialGridDataFrame(readGDAL("file.bmp"))$z
file.bmp has GDAL driver BMP
and has 4 rows and 5 columns
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20

I was a bit surprised to find that there seem to be no coercion
functions to convert to/from pixmap within the family of packages that
include maptools, sp, spatstat  and raster , they would be useful. I
would say that raster would be the best bet in the first instance
since it can store the different colour models directly. There is also
new "rasterImage" support in the graphics package that is worth
mentioning. All of these packages have different data structures, but
there is a growing set of coercion functions and it's not too hard
once you understand them to do it yourself.

Cheers, Mike.
On Wed, May 9, 2012 at 8:31 AM, ZHANG Yingqi <zhangyingqi at ivpp.ac.cn> wrote: