Skip to content
Prev 39178 / 63424 Next

using rasterImage within image

On 02/14/2011 02:59 PM, Simon Urbanek wrote:
Yes, but: according to the table of limitations, this

image(matrix(1:9,3),
      col=rgb(rep(1,9),rep(0,9),rep(0,9),alpha=seq(0,1,length=9)))

 shouldn't work with raster under Windows.

"** The Windows device can do semitransparent raster images, but ONLY if
there is a constant alpha across the entire image, i.e., it CANNOT do
per-pixel alpha (the Windows API AlphaBlend can, but GraphApp's bitmap
structures do not support "shades of alpha" - it's either transparent or
opaque). The Windows device DOES support fully transparent pixels in an
image (possibly in addition to a single level of semitransparency). In
other words, the image can have up to three different levels of alpha,
as long as fully transparent and fully opaque are two of the three. "

  I *think* the following function should provide a test of when we have
to fall back to method="image" if the current device is "windows" ...

diff_alpha <- function(cstr) {
  alphastr <- substr(cstr[nchar(cstr)==9],8,9)
  length(unique(alphastr[!alphastr %in% c("00","FF")]))>1
}

tst2 <- c(rgb(1,0,0,1),rgb(1,0,0),rgb(1,0,0,0.5),rgb(1,1,0,0.5),
          rgb(1,0,0,0.7))

diff_alpha(tst2)
diff_alpha(tst2[1:4])


  (I would argue that this situation is sufficiently rare that we should
still make "raster" the default, with a fallback to "image" if
diff_alpha() && windows is TRUE [perhaps a warning if missing(method)
and an error otherwise?])

  Ben Bolker