[R] Display Pictures in R

Knut Suebert knsn at gmx.de
Thu Nov 11 22:50:27 CET 1999


R.E.Darnell at newcastle.ac.uk schrieb:

> Sigurd
> 
> I am curious? Why you would like to do this?

Maybe to draw a nice legend, grid and Gauß-Krüger-coordinates in the
same style like the other graphs in a document (and maybe doing it all by
make-controlled scripts)? 

For that I converted a bitmap in a matrix to be displayed by image(). A
small image in 600dpi needs some RAM (not more than 512MB swap-space) and
a little bit of time... 
________

To prepare the image-data (.pnm ascii, NOT raw) it needs

  system("cat map.pnm | pnm2r > map.R")
  x <- as.matrix(read.table("map.R"))

image-data is orientated differend to .pnm, so "just"

  for (i in 1:ncol(x)){x[,i] <- rev(x[,i])} # upside down
  x <- t(x)                                 # turn around
  
to display the picture
  
  image(x, axes=F, col=rainbow(x,start=0,end=1/6.66) )
  
the resulting red-to-yellow picture looks fine on a non-color-printer...
_______

The source for pnm2r is 

  #include <stdio.h>

  /* Converts a PNM-image (ASCII and GRAYSCALE!!!) to a table */

  int main(){
    int width, height, x, y, r, g, b;
    char *s, *t, *line;
    int buffer_size=1024;
  
    if ( (line = (char *) malloc(buffer_size))){
      fgets(line, buffer_size, stdin); // P2 for gray,  P4 colour?
      fgets(line, buffer_size, stdin);
      fgets(line, buffer_size, stdin); // the third line has...
      sscanf(line, "%i %i", &width, &height);
      fgets(line, buffer_size, stdin);

      fprintf(stderr, "Size of picture: %ix%i\n", height, width);
    
      for( y=0 ; y<height ; y++ ){
        for( x=0 ; x<width ; x++ ){  
          fgets(line, buffer_size, stdin); 
          sscanf(line, "%i", &r);
          printf("%d ",r);
        }
        printf("\n");
      }
    }
  }
_____

I don't think pnmr.c is well programmed (could it be that I forgot to free
the memory somewhere?) or pnm is the best gfx-format, pnm was just the only
one I understood by just looking into the files generated by the gimp.

But I am sure that was one of the silliest ways to reach my goal ;-)

Bye,
Knut
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list