Bug 16594 - svg of rasterGrob objects creates files Inkscape and Firefox unnecessarily interpolate
Summary: svg of rasterGrob objects creates files Inkscape and Firefox unnecessarily in...
Status: UNCONFIRMED
Alias: None
Product: R
Classification: Unclassified
Component: Graphics (show other bugs)
Version: R-devel (trunk)
Hardware: All All
: P5 enhancement
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2015-11-08 12:52 UTC by Richard Cotton
Modified: 2015-11-08 12:53 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Cotton 2015-11-08 12:52:38 UTC
If I create a grid graphic containing a rasterGrob and save it to file using grDevices::svg, then the resulting file is interpolated to by Firefox and by Inkscape, becoming blurry.

To reproduce:

library(grid)
r <- as.raster(matrix(runif(25), 5, 5))
svg("test.svg")
grid.newpage()
grid.raster(r, interpolate = FALSE)
dev.off() 

The plot should display a 5 by 5 grid of grey squares, and does so in R's figure window.  The plot in Firefox or Inkscape is a blurry mess.

I realize that this sounds like "not R's problem", but Firefox and Inkscape are popular tools for viewing and editing SVG files respectively, so it is useful to generate files that work with these pieces of software.  Luckily, the fixes are straightforward.

Fix for Firefox:

Henrik Bengtsson pointed me to a Stack Overflow answer which explains that you need to add 

style="image-rendering: -moz-crisp-edges"

to the image element.

http://stackoverflow.com/a/767664/134830

Fix for Inkscape:

User suv-lp on the Inkscape bug tracker explains that you need to add

image-rendering="optimizeSpeed"

to the image element.

https://bugs.launchpad.net/inkscape/+bug/1418038

Here's the contents of the SVG file as created by R:

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="504pt" height="504pt" viewBox="0 0 504 504" version="1.1">
<defs>
<image id="image5" width="5" height="5" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAABmJLR0QA/wD/AP+gvaeTAAAAW0lEQVQImQFQAK//Ad3d3f39/fv7+76+vg8PDwCtra0bGxvh4eHh4eE0NDQBIiIiDAwM2NjY5OTku7u7AkNDQ87Ozurq6tTU1La2tgHh4eGurq4dHR3Q0NAICAhbsi0woroPHwAAAABJRU5ErkJggg=="/>
</defs>
<g id="surface1">
<rect x="0" y="0" width="504" height="504" style="fill:rgb(100%,100%,100%);fill-opacity:1;stroke:none;"/>
<use xlink:href="#image5" transform="matrix(100.8,0,0,100.8,0,0)"/>
</g>
</svg>

Here's the modified file that works with both Firefox and Inkscape. (The only difference is the inclusion of image-rendering="optimizeSpeed" style="image-rendering: -moz-crisp-edges".)

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="504pt" height="504pt" viewBox="0 0 504 504" version="1.1">
<defs>
<image id="image5" width="5" height="5" image-rendering="optimizeSpeed" style="image-rendering: -moz-crisp-edges" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAABmJLR0QA/wD/AP+gvaeTAAAAW0lEQVQImQFQAK//Ad3d3f39/fv7+76+vg8PDwCtra0bGxvh4eHh4eE0NDQBIiIiDAwM2NjY5OTku7u7AkNDQ87Ozurq6tTU1La2tgHh4eGurq4dHR3Q0NAICAhbsi0woroPHwAAAABJRU5ErkJggg=="/>
</defs>
<g id="surface1">
<rect x="0" y="0" width="504" height="504" style="fill:rgb(100%,100%,100%);fill-opacity:1;stroke:none;"/>
<use xlink:href="#image5" transform="matrix(100.8,0,0,100.8,0,0)"/>
</g>
</svg>


See also the discussion on R-devel that prompted this bug report:

http://r.789695.n4.nabble.com/PDFs-and-SVGs-containing-rasterGrobs-don-t-display-correctly-in-some-other-software-td4714393.html