home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / rast / rdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-08  |  2.3 KB  |  98 lines

  1. /***********************************************************************
  2.  
  3.    rdemo - reads a rasterfile, computes the average of the colour
  4.    values (if applicable) of the pixels, and writes a new rasterfile
  5.    with the average.  An inverting colour map is added.
  6.  
  7. ***********************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include "rast.h"
  11.  
  12. main(argc,argv)
  13. int argc;
  14. char *argv[];
  15. {
  16.     int r, g, b, avg, row, col, i, pix, ifile, ofile;
  17.     RASTER *irast, *orast, *Ropen(), *Rdopen();
  18.  
  19.     ifile = 0;
  20.     ofile = 0;
  21.  
  22.     for (i = 1; i < argc; i++) {
  23.         if (argv[i][0] == '-') {
  24.             switch (argv[i][1]) {
  25.                 case 'i': ifile = ++i; break;
  26.                 case 'o': ofile = ++i; break;
  27.                 default:  usage();
  28.             }
  29.         }
  30.     }
  31.  
  32.     if (ifile) irast = Ropen(argv[ifile],R);
  33.     else irast = Rdopen(0,R);
  34.     if (irast == NULL) {
  35.         fprintf(stderr,"rdemo: input rasterfile open failed!\n");
  36.         exit(1);
  37.     }
  38.  
  39.     if (ofile) orast = Ropen(argv[ofile],W);
  40.     else orast = Rdopen(1,W);
  41.     if (irast == NULL) {
  42.         fprintf(stderr,"rdemo: output rasterfile open failed!\n");
  43.         exit(1);
  44.     }
  45.  
  46.     if (!Rgetheader(irast)) {
  47.         fprintf(stderr,"input file is not a raster file!\n");
  48.         exit(1);
  49.     }
  50.  
  51.     orast->height = irast->height;
  52.     orast->width  = irast->width;
  53.     orast->depth  = irast->depth;
  54.  
  55.     Rinitmap(orast,RMT_EQUAL_RGB,256*3);
  56.     for (i = 0; i < 256; i++) Rputmap(orast,255-i,i,i,i);
  57.     
  58.     Rputheader(orast);
  59.  
  60.     if (irast->maptype == RMT_EQUAL_RGB) {
  61.         for (row = 0; row < irast->height; row++) 
  62.             for (col = 0; col < irast->width; col++) {
  63.                 Rgetmappedpix(irast,&r,&g,&b);
  64.                 avg = (r + g + b) / 3;
  65.                 Rputpix(orast,avg);
  66.             }
  67.     }
  68.     else if (irast->maptype == RMT_RAW) {
  69.         for (row = 0; row < irast->height; row++) 
  70.             for (col = 0; col < irast->width; col++) {
  71.                 Rgetmappedpix(irast,&pix);
  72.                 Rputpix(orast,pix);
  73.             }
  74.     }
  75.     else if (irast->maptype == RMT_NONE) {
  76.         for (row = 0; row < irast->height; row++) 
  77.             for (col = 0; col < irast->width; col++) {
  78.                 pix = Rgetpix(irast);
  79.                 Rputpix(orast,pix);
  80.             }
  81.     }
  82.     else {
  83.         fprintf(stderr,"rdemo: unknown map type in input rasterfile!\n");
  84.         exit(1);
  85.     }
  86.     
  87.     Rclose(irast);
  88.     Rclose(orast);
  89. }
  90.  
  91. usage()
  92. {
  93.     fprintf(stderr,"usage: rdemo [-i file] [-o file]\n");
  94.     fprintf(stderr,"       -i  input rasterfile [default is stdin]\n");
  95.     fprintf(stderr,"       -o  output rasterfile [default is stdout]\n");
  96.     exit(0);
  97. }
  98.