[Rd] raster support in graphics devices

Paul Murrell p.murrell at auckland.ac.nz
Mon Dec 14 03:50:20 CET 2009


Hi


baptiste auguie wrote:
> Hi,
> 
> Thanks for the fix. I hope the quartz bugs are not related to a bad
> configuration on my side (I don't have access to another Mac to test
> it).


I have not been able to get hold of a 10.5 (Leopard) Mac either.  Keen 
to hear confirmation from anyone else with this problem.


> I was quite happy with this proof-of-concept of filling patterns for
> rectangles, and I was wondering if perhaps you had considered adding a
> similar tiling option to grid.raster (at the C level)?


I have thought a bit about drawing the same image multiple times (more 
in the context of using a bitmap for a plotting symbol).  I imagine 
something like being able to "add" a raster image to a device and then 
simply "refer" to that image (e.g., by name) is needed.  Still thinking 
about the right way to go about that though.

Paul


> Best regards,
> 
> baptiste
> 
> 2009/12/6 Paul Murrell <p.murrell at auckland.ac.nz>:
>> Hi
>>
>>
>> baptiste auguie wrote:
>>> Hi again,
>>>
>>> I found two possible bugs related to grid.raster, one with the quartz
>>> device and the other with pdf.
>>>
>>> In my example I was playing with the idea of using grid.raster to
>>> create a filling pattern for rectangles. The pdf output does not seem
>>> to respect the clipping (it may have nothing to do with grid.raster
>>> though), whilst the quartz device with pdf file output often crashes
>>> for more than 4 different raster objects (but not always). I'm afraid
>>> I couldn't pinpoint the exact circumstance of the crash with a more
>>> concise example.
>>
>> Thanks again for the report.
>>
>> I have committed a fix for the PDF clipping.
>>
>> Still looking at the Quartz crashes.
>>
>> Paul
>>
>>
>>> Thanks in advance for any insights,
>>>
>>> baptiste
>>>
>>> ### Start example ###
>>>
>>> library(grid)
>>>
>>> ## create a motif
>>> grid45 <- function(..., width=0.5, height=0.5){
>>>  x11(width=width, height=height)
>>>  grid.polygon(...)
>>>  m <- grid.cap()
>>>  dev.off()
>>>  invisible(m)
>>> }
>>>
>>> .grid45 <- grid45()
>>> ## grid.raster(.grid45)
>>>
>>> tile.motif <- function(m, nx=10, ny=nx){
>>>  cols <- matrix(rep(m, nx), ncol=ncol(m)*nx, byrow=F)
>>>  matrix(rep(t(cols), ny), nrow=nrow(cols)*ny, byrow=T)
>>> }
>>>
>>> ## quartz()
>>> ## grid.raster(tile.motif(.grid45, 2, 3))
>>>
>>> patternGrob <- function(x=unit(0.5, "npc"), y=unit(0.5, "npc"),
>>>                        width=unit(1, "npc"), height=unit(1, "npc"),
>>>                        motif=matrix("white"), AR=1,
>>>                        motif.width=unit(5, "mm"),
>>>                        motif.height=AR*motif.width,
>>>                        pattern.offset=c(0, 0), # unimplemented
>>>                        default.units="npc",
>>>                        clip=TRUE, # testing purposes
>>>                        gp=gpar(fill=NA), ...)
>>>  {
>>>    grob(x=x, y=y, width=width, height=height,
>>>         motif=motif, motif.width=motif.width,
>>>         motif.height=motif.height, clip=clip, gp=gp, ..., cl="pattern")
>>>  }
>>>
>>> widthDetails.pattern <- function(x) x$width
>>> heightDetails.pattern <- function(x) x$height
>>>
>>> drawDetails.pattern <- function(x, recording=TRUE){
>>>
>>> ##   calculate the number of tiles
>>>  nx <- ceiling(convertUnit(x$width, "in", value=TRUE) /
>>>                convertUnit(x$motif.width, "in", value=TRUE)) + 1
>>>  ny <- ceiling(convertUnit(x$height, "in", axisFrom = "y", value=TRUE) /
>>>                convertUnit(x$motif.height, "in", axisFrom = "y",
>>> value=TRUE)) + 1
>>>
>>>  width <- convertUnit(x$width, "in")
>>>  height <- convertUnit(x$height, "in", axisFrom = "y")
>>>
>>> ##   clip the raster
>>>  pushViewport(viewport(x=x$x, y=x$y,
>>>          width=x$width, height=x$height, clip=x$clip))
>>>
>>>  grid.raster(tile.motif(x$motif, nx, ny), width=nx*x$motif.width,
>>>                         height=ny*x$motif.height)
>>>  upViewport()
>>>
>>> ##   overlay the rectangle
>>>  grid.rect(x=x$x, y=x$y,
>>>          width=x$width, height=x$height,
>>>          just="center", gp=x$gp)
>>> }
>>>
>>>
>>> g <- patternGrob(x=0.7, width=unit(0.3, "npc"),
>>>                  height=unit(5.2, "cm"),
>>>                  clip=TRUE, motif=.grid45)
>>>
>>> ## interactive use: OK
>>> quartz()
>>> grid.newpage()
>>> grid.draw(g)
>>>
>>> ## png: OK
>>> png(file="pngClip.png")
>>> grid.newpage()
>>> grid.draw(g)
>>> dev.off()
>>>
>>> ## pdf: clipping does not occur
>>> pdf(file="pdfClip.pdf")
>>> grid.newpage()
>>> grid.draw(g)
>>> dev.off()
>>>
>>> ## quartz pdf: OK, but see below
>>> quartz(file="quartzClip.pdf", type="pdf")
>>> grid.newpage()
>>> grid.draw(g)
>>> dev.off()
>>>
>>> g1 <- patternGrob(x=0.2, width=unit(0.2, "npc"),
>>>                  height=unit(5.2, "cm"),
>>>                  clip=TRUE, motif=.grid45)
>>>
>>> g2 <- patternGrob(x=0.4, width=unit(0.2, "npc"),
>>>                  height=unit(5.2, "cm"),
>>>                  clip=TRUE, motif=.grid45)
>>>
>>> g3 <- patternGrob(x=0.6, width=unit(0.2, "npc"),
>>>                  height=unit(5.2, "cm"),
>>>                  clip=TRUE, motif=.grid45)
>>>
>>> g4 <- patternGrob(x=0.8, width=unit(0.2, "npc"),
>>>                  height=unit(5.2, "cm"),
>>>                  clip=TRUE, motif=.grid45)
>>>
>>> quartz(file="quartzClip2.pdf", type="pdf")
>>> grid.newpage()
>>> grid.draw(g1)
>>> grid.draw(g2)
>>> grid.draw(g3)
>>> grid.draw(g4)
>>> dev.off()
>>>
>>>  *** caught segfault ***
>>> address 0x15dda018, cause 'memory not mapped'
>>>
>>> Traceback:
>>>  1: dev.off()
>>>
>>>  sessionInfo()
>>> R version 2.11.0 Under development (unstable) (2009-11-30 r50622)
>>> i386-apple-darwin9.8.0
>>>
>>> locale:
>>> [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8
>>>
>>> attached base packages:
>>> [1] grid      stats     graphics  grDevices utils     datasets  methods
>>> [8] base
>>>
>>>
>>>
>>> 2009/12/2 Paul Murrell <p.murrell at auckland.ac.nz>:
>>>> Hi
>>>>
>>>>
>>>> baptiste auguie wrote:
>>>>> Very nice, thank you for this great addition to R graphics! I can't
>>>>> wait to see lattice and ggplot2 functions that use rasterGrob to
>>>>> display images. The pdf output is so much better in every way!
>>>>>
>>>>> Incidentally, I ran into a segfault with grid.cap on the quartz
>>>>> device, but maybe it's normal at this stage.
>>>> This may be due to the fact that I tested the changes on Mac OS X 10.6
>>>> (looks like you have 10.5 ?), plus the fact that I am feeling my way a
>>>> bit
>>>> on the Mac.  I have access to a 10.4 machine so I will try to take a look
>>>> there.  Thanks for the report.
>>>>
>>>> Paul
>>>>
>>>>
>>>>> This works fine:
>>>>>
>>>>> library(grid)
>>>>> x11()
>>>>> grid.text("test")
>>>>> cap <- grid.cap()
>>>>>
>>>>> This doesn't:
>>>>>
>>>>> library(grid)
>>>>> quartz()
>>>>> grid.text("test")
>>>>> cap <- grid.cap()
>>>>>
>>>>>  *** caught segfault ***
>>>>> address 0x18330001, cause 'memory not mapped'
>>>>>
>>>>> Traceback:
>>>>>  1: .Call(fnname, ..., PACKAGE = "grid")
>>>>>  2: grid.Call("L_cap")
>>>>>  3: grid.cap()
>>>>>
>>>>> Possible actions:
>>>>> 1: abort (with core dump, if enabled)
>>>>> 2: normal R exit
>>>>> 3: exit R without saving workspace
>>>>> 4: exit R saving workspace
>>>>>
>>>>> [R.app GUI 1.30 (5527) i386-apple-darwin9.8.0]
>>>>>
>>>>>> sessionInfo()
>>>>> R version 2.11.0 Under development (unstable) (2009-11-30 r50622)
>>>>> i386-apple-darwin9.8.0
>>>>>
>>>>> locale:
>>>>> [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8
>>>>>
>>>>> attached base packages:
>>>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>>>
>>>>> Best regards,
>>>>>
>>>>> baptiste
>>>>>
>>>>> 2009/12/1 Paul Murrell <p.murrell at auckland.ac.nz>:
>>>>>> Hi
>>>>>>
>>>>>> This is for developers of extension packages that provide extra
>>>>>> *graphics
>>>>>> devices* for R.
>>>>>>
>>>>>> In the *development* version of R, support has been added to the
>>>>>> graphics
>>>>>> engine for sending raster images (bitmaps) to a graphics device.  This
>>>>>> consists mainly of two new device functions:  dev_Raster() and
>>>>>> dev_Cap().
>>>>>>
>>>>>> The R_GE_version constant (in GraphicsEngine.h) has been bumped up to 6
>>>>>> as a
>>>>>> marker of this change.
>>>>>>
>>>>>> This means that, at a minimum, all graphics devices should be updated
>>>>>> to
>>>>>> provide dummy implementations of these new functions that just say the
>>>>>> feature is not yet implemented (see for example the PicTeX and XFig
>>>>>> devices
>>>>>> in the 'grDevices' package).
>>>>>>
>>>>>> A full implementation of dev_Raster() should be able to draw a raster
>>>>>> image
>>>>>> (provided as an array of 32-bit R colors) at any size, possibly
>>>>>> (bilinear)
>>>>>> interpolated (otherwise nearest-neighbour), at any orientation, and
>>>>>> with
>>>>>> a
>>>>>> per-pixel alpha channel.  Where these are not natively supported by a
>>>>>> device, the graphics engine provides some routines for scaling and
>>>>>> rotating
>>>>>> raster images (see for example the X11 device).  The dev_Cap() function
>>>>>> should return a representation of a raster image captured from the
>>>>>> current
>>>>>> device.  This will only make sense for some devices (see for example
>>>>>> the
>>>>>> Cairo device in the 'grDevices' package).
>>>>>>
>>>>>> A little more information and a couple of small examples are provided
>>>>>> at
>>>>>> http://developer.r-project.org/Raster/raster-RFC.html
>>>>>>
>>>>>> Paul
>>>>>> --
>>>>>> Dr Paul Murrell
>>>>>> Department of Statistics
>>>>>> The University of Auckland
>>>>>> Private Bag 92019
>>>>>> Auckland
>>>>>> New Zealand
>>>>>> 64 9 3737599 x85392
>>>>>> paul at stat.auckland.ac.nz
>>>>>> http://www.stat.auckland.ac.nz/~paul/
>>>>>>
>>>>>> ______________________________________________
>>>>>> R-devel at r-project.org mailing list
>>>>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>>>>
>>>> --
>>>> Dr Paul Murrell
>>>> Department of Statistics
>>>> The University of Auckland
>>>> Private Bag 92019
>>>> Auckland
>>>> New Zealand
>>>> 64 9 3737599 x85392
>>>> paul at stat.auckland.ac.nz
>>>> http://www.stat.auckland.ac.nz/~paul/
>>>>
>> --
>> Dr Paul Murrell
>> Department of Statistics
>> The University of Auckland
>> Private Bag 92019
>> Auckland
>> New Zealand
>> 64 9 3737599 x85392
>> paul at stat.auckland.ac.nz
>> http://www.stat.auckland.ac.nz/~paul/
>>

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-devel mailing list