From bbolker at gmail.com  Mon Feb 14 22:17:14 2011
From: bbolker at gmail.com (Ben Bolker)
Date: Mon, 14 Feb 2011 16:17:14 -0500
Subject: [Rd] using rasterImage within image
In-Reply-To: <0478466E-4EDF-4594-B571-7CF36CB95A6C@r-project.org>
References: <4D51F2A7.8010505@mcmaster.ca>	<D8F5413E-025A-4511-BCE1-7ED06CBE99A0@r-project.org>	<4D52AD7B.6070402@gmail.com>	<E86B0A90-F63F-4537-A7FB-1AA84D685154@r-project.org>	<AANLkTi=yRP56wACtOs+Ouc70EV57rw9RKYmfLAh2hQ8a@mail.gmail.com>	<825F0B1E-EF80-48F5-AFF1-5832092450AB@r-project.org>	<AANLkTi=0K6hCP0tptSf6h4S6au+-SmahDm983zRdZEgb@mail.gmail.com>	<4D531558.7010709@gmail.com>	<AANLkTi=nwF-3UjtoFAaLdTwphmezvuk57HaaJd57=am2@mail.gmail.com>	<AANLkTimHAmzq4CEJF-auHRSA8745x2yb8r_GK9+vTrKJ@mail.gmail.com>	<4D5493BD.4050801@auckland.ac.nz>	<AANLkTikiFnaCPzBxDdDGV0+jhvo-bwo067Fnx1tSzW3=@mail.gmail.com>	<4D5844DB.4040801@auckland.ac.nz>	<loom.20110214T182423-339@post.gmane.org>
	<058A2CD6-E303-4ABD-AD16-23E82F8513A0@r-project.org>
	<4D598263.9020700@auckland.ac.nz>
	<0478466E-4EDF-4594-B571-7CF36CB95A6C@r-project.org>
Message-ID: <4D599BDA.5030708@gmail.com>

On 02/14/2011 02:59 PM, Simon Urbanek wrote:
> 
> On Feb 14, 2011, at 2:28 PM, Paul Murrell wrote:
> 
>> Hi
>> 
>> On 15/02/2011 8:11 a.m., Simon Urbanek wrote:
>>> 
>>> On Feb 14, 2011, at 12:26 PM, Ben Bolker wrote:
>>> 
>>>> Paul Murrell<p.murrell<at>  auckland.ac.nz>  writes:
>>>> 
>>>>> 
>>>>> Hi
>>>>> 
>>>>> On 12/02/2011 7:22 p.m., Michael Sumner wrote:
>>>>>> Hello, that appears to have fixed it. Thank you very much.
>>>>>> 
>>>>>> I can now repeat the reported workflow and the image
>>>>>> appears on the fifth (and subsequent) calls.
>>>>> 
>>>>> Great. Thanks for checking.
>>>>> 
>>>>> Paul
>>>>> 
>>>> 
>>>> 
>>>> That's great.
>>>> 
>>>> Just a little bump: I would encourage Simon (in his copious
>>>> spare time), or other interested members of R-core, to decide
>>>> on a good name for the argument (as a reminder, I prefer 
>>>> 'method=c("raster","image")'). Furthermore, I would strongly 
>>>> encourage that "raster" be made the default behavior for the 
>>>> development release of R ...
>>>> 
>>> 
>>> 
>>> Unfortunately I just encountered a speed bump today - I had no
>>> idea that the Windows device doesn't even support transparent
>>> pixels. That needs to be fixed before we can make it the
>>> default...
>> 
>> See the table at the bottom of
>> http://developer.r-project.org/Raster/raster-RFC.html for the full
>> list of limitations.
>> 
> 
> Mea culpa - I had forgotten that the default for interpolate=TRUE
> (unfortunately) so all those dozens of warnings came from the fact
> that R was trying to interpolate on a device that doesn't support it.
> I stand corrected, transparent pixels are supported if
> interpolate=FALSE so image() should be safe.
> 
> Thanks, Simon
> 

  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



>



[Rd] using rasterImage within image
Ben Bolker