rxmlplotghostscriptgrimport

R: Importing vector graphics to a plot - Cannot get grImport to produce meaningful XML


I am trying to import a vector graphic using the grImport package but have encountered a problem that I cannot solve (despite spending all day trawling the internet, including here!)...

I have installed Ghostscript, and converted my .png image to a .ps file (and have checked whether this is the cause of failure by converting .jpg and .pdf images as well - but with the same outcome).

Here is an example of my .png file: simple cross png

And an example of the .ps file after I converted it: simple cross ps

I receive no error messages when I run:

PostScriptTrace("cross.ps")
img <- readPicture("cross.ps.xml")

If I then try to use the picture in a plot, however, there is no image plotted.

After tracing this back, I found that the .XML file produced by PlotScriptTrace does not contain any paths:

str(img)
>Formal class 'Picture' [package "grImport"] with 2 slots
..@ paths  : Named list()
..@ summary:Formal class 'PictureSummary' [package "grImport"] with 3 slots
.. .. ..@ numPaths: num 0
.. .. ..@ xscale  : Named num [1:2] 99999 -99999
.. .. .. ..- attr(*, "names")= chr [1:2] "xmin" "xmax"
.. .. ..@ yscale  : Named num [1:2] 99999 -99999
.. .. .. ..- attr(*, "names")= chr [1:2] "ymin" "ymax"

So therefore, I think that there is a problem either when I call PostScriptTrace() or readPicture(), but I don't know how to figure out which or how to proceed from here.

Here is my sessionInfo() in case this helps:

R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] grImport_0.9-0 XML_3.98-1.4  

Note that my problem is identical to: R: Importing vector graphics to a plot - Cannot get grImport to work, but the answer to this previous question did not help me - it points out that the XML file has not been compiled correctly, which I've already figured out.

Can anyone point out where I'm going wrong, please??


Solution

  • Moved to an answer, too large for comments.

    I'm trying to import the .ps file, which I think is a vector graphic?

    OK That's a misapprehension, PostScript programs can contain both vector and bitmap data. So obviously if you start from bitmap data (PNG, JPEG, TIFF etc) then the PostScript equivalent program will simply contain the same (modulo compression) bitmap data.

    Looking inside the PostScript program you posted we can see the usual comprehensive PostScript header from Ghostscript's ps2write device followed by the actual program:

    %%EndPageSetup
    5 0 obj
    <</Length 156041>>stream
    q 0.12 0 0 0.12 0 0 cm
    q 3950 0 0 5341.67 508.333 833.333 cm
    BI
    /CS/RGB
    /W 1422
    /H 1923
    /BPC 8
    /F[/A85
    /LZW]
    ID
    J3Vsg3$]7K#D>EP:q1$o*=mro@So+\<\5,H7Uo<*jE<[.O@Wn[3@'nb-^757;Rp>H>q_R=AlC^c
    ...
    ...
    =Z;P44-S>^PSliQ=\"^E99e+*Po3Mc=]UKU!W~>
    EI Q
    Q
    
    endstream
    endobj
    pagesave restore
    %%PageTrailer
    %%Trailer
    end
    %%EOF
    

    The ID function (defined in the prolog) draws the following inline data as a bitmap image. That's then followed by the bitmap data, and the program emits the page, cleans up and exits.

    So there is no vector data there at all, not surprisingly since the source data wasn't vector.

    I don't know anything about R, but from the questions I see I assume that it can't handle bitmap data, which is why you get blank output.