home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / vorttoppm.c < prev    next >
C/C++ Source or Header  |  1992-01-29  |  2KB  |  81 lines

  1. /*
  2.  * vorttoppm.c - read a VORT raster file and produce a portable pixmap *
  3.  * This is public domain.
  4.  * 
  5.  * Compile with: (In the tools directory)
  6.  *
  7.  *     cc -o vorttoppm vorttoppm.c -I../lib ../lib/libvort.a
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include "vort.h"
  12.  
  13. main(argc, argv)
  14.     int    argc;
  15.     char    *argv[];
  16. {
  17.     int        x, y, w, h;
  18.     unsigned char    *line, *red, *green, *blue;
  19.     image        *im;
  20.     char        *fname;
  21.     char        buf[128];
  22.  
  23.     if (argc == 1)
  24.         fname = "-";
  25.     else
  26.         fname = argv[1];
  27.  
  28.     if ((im = openimage(fname, "r")) == (image *)NULL) {
  29.         fprintf(stderr, "Unable to open VORT file");
  30.         exit(1);
  31.     }
  32.  
  33.     w = imagewidth(im);
  34.     h = imageheight(im);
  35.  
  36.     if (!colormapped(im)) {
  37.         if ((red = (unsigned char *)malloc((unsigned)w)) == NULL) {
  38.             fprintf(stderr, "Out of mem.\n");
  39.             exit(1);
  40.         }
  41.         if ((green = (unsigned char *)malloc((unsigned)w)) == NULL) {
  42.             fprintf(stderr, "Out of mem.\n");
  43.             exit(1);
  44.         }
  45.         if ((blue = (unsigned char *)malloc((unsigned)w)) == NULL) {
  46.             fprintf(stderr, "Out of mem.\n");
  47.             exit(1);
  48.         }
  49.     } else
  50.         if ((line = (unsigned char *)malloc((unsigned)w)) == NULL) {
  51.             fprintf(stderr, "Out of mem.\n");
  52.             exit(1);
  53.         }
  54.  
  55.  
  56.     /*
  57.      * The header...
  58.      */
  59.     printf("P6\n%d %d\n255\n", w, h);
  60.  
  61.     for (y = 0; y < h; y++) {
  62.         if (colormapped(im)) {
  63.             readmappedline(im, line);
  64.             for (x = 0; x < w; x++) {
  65.                 putchar(redmap(im)[line[x]]);
  66.                 putchar(greenmap(im)[line[x]]);
  67.                 putchar(bluemap(im)[line[x]]);
  68.             }
  69.         } else {
  70.             readrgbline(im, red, green, blue);
  71.             for (x = 0; x < w; x++) {
  72.                 putchar(red[x]);
  73.                 putchar(green[x]);
  74.                 putchar(blue[x]);
  75.             }
  76.         }
  77.     }
  78.  
  79.     exit(0);
  80. }
  81.