[Rd] Inferring dimensions on bitmap device from par()

Henrik Bengtsson hb at stat.berkeley.edu
Fri Jan 26 02:35:15 CET 2007


I am trying to infer the dimension of an opened bitmap (png, jpeg,
bitmap device...) from par() parmeters.  From the help on par(), I
found that:

> dim <- c(400, 200)
> png("foo.png", width=dim[1], height=dim[2])
> dim2 <- par("din") * par("cra") / par("cin")
> dev.off()
> dim2
[1] 399.9999 199.9999

I've tried the above on Rv2.4.1 on WinXP and Linux with png() and
jpeg() and the precision is good enough.  However, when I turn to the
bitmap() device, I found the following:

> s <- 3;  # Multiple of 72pt (default value of argument 'res' of bitmap())
> res <- s*72;
> bitmap("foo.png", width=dim[1]/res, height=dim[2]/res, res=res)
> dim2 <- par("din") * par("cra") / par("cin")
> dev.off()
> dim/dim2
[1] 3 3

That is, dim/dim2 is not one, but equal to the multiple 's'.   I
understand that this is because bitmap() is not a device per se and
utilized postscript() plus a call to ghostscript.  But still, does
anyone know if it is possible to infer the correct resolution

 res2 <- s * par("cra") / par("cin")

(or ultimately the dimension of the generated plot in pixels) without
know which of png(), jpeg() or bitmap() was used?  I know of
par("ps"), but its "unit" is device specific.



More information about the R-devel mailing list