home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / seq / src / seqp.c < prev   
C/C++ Source or Header  |  1990-01-07  |  7KB  |  268 lines

  1. /*
  2. *
  3. *  Raster image display program for the IBM PC  
  4. *
  5. *    This program takes binary images, one byte per pixel, and displays them
  6. *  on the PC's color graphics screen.  The dimensions of the image are
  7. *  specified on the command line, each image is read from the file 
  8. *  with block reads, then displayed in the specified position on the screen.
  9. *  Portions of the image that do not fit on the screen are clipped.
  10. *
  11. *  example use:    rev  256 256 -c -m pal.pal list.dat
  12. *     the file 'list.dat' must contain a list of file names of the images
  13. *     that are to be displayed in sequence.
  14. *
  15. *  The space bar pauses and restarts the display.  Return exits after any
  16. *  image.
  17. *
  18. *  the mapfile is a sequence of bytes from 0 - 255.  There are 256 red values
  19. *  followed by 256 green values followed by 256 blue values.  They are
  20. *  divided by 16 before use because the PGA only has 16 intensities per gun.
  21. *
  22. *
  23. *  National Center for Supercomputing Applications, University of Illinois
  24. *  153 Water Resources Building
  25. *  605 E. Springfield Ave.
  26. *  Champaign, IL  61820     (217)244-0072
  27. *
  28. *  Tim Krauskopf            July 1986
  29. *
  30. */
  31. #include "stdio.h"
  32. #include "fcntl.h"
  33.  
  34. #define USAGE  printf("\nUsage: %s xdim ydim [-c][-p xwhere ywhere][-m mapfile] fileofnames\n",argv[0])
  35.  
  36. #define RAWSIZE 32000
  37. #define SCRX 640
  38. #define SCRY 480
  39. #define LINEPUT    pgalinea(xwhere,ywhere+i,store[i],xshow,1)
  40.  
  41.  
  42. FILE *fp;
  43. int        i,j,c,xdim,ydim,file,xshow,xwhere=0,ywhere=0;
  44.  
  45. unsigned char raw[RAWSIZE],             /* for file buffering */
  46.     rmap[256],gmap[256],bmap[256];        /* color map, if specified */
  47. unsigned char filename[128];
  48. unsigned char palfile[128] = {0,0};     /* starts out = NULL */
  49.  
  50.  
  51. char *store[1024],              /* one per line of image */
  52.     *malloc(),*p;  
  53.  
  54. main(argc,argv)
  55.     int argc;
  56.     char *argv[];
  57.     {
  58.     if (argc < 4 ) {               /*  requires at least 2 parameters */
  59.         USAGE;
  60.         exit(1);
  61.     }
  62.  
  63.     xdim = atoi(argv[1]);       /* required parms of x and y dimensions */
  64.     if (xdim > SCRX)
  65.         xshow = SCRX;           /* screen limits  */
  66.     else
  67.         xshow = xdim;
  68.  
  69.     ydim = atoi(argv[2]);
  70.  
  71.     if (xdim < 10 || ydim < 10) {
  72.         puts("\n Huh? ");
  73.         exit(1);
  74.     }
  75.  
  76.     for (i=3; i<argc; i++) {      /* look at each parm */
  77.         if (*argv[i] == '-') 
  78.             switch ( *(argv[i]+1)) {
  79.                 case 'c':                    /*  centering  */
  80.                     xwhere = (SCRX-xdim)/2;
  81.                     ywhere = (SCRY-ydim)/2;
  82.                     if (xwhere < 0) 
  83.                         xwhere = 0;            /* if larger than screen, */
  84.                     if (ywhere < 0)            /* don't center it */
  85.                         ywhere = 0;
  86.                     break;
  87.                 case 'p':                    /* special position */
  88.                     xwhere = atoi(argv[++i]);
  89.                     ywhere = atoi(argv[++i]);
  90.                     if (xwhere > SCRX || ywhere > SCRY) {
  91.                         puts("\n Invalid position ");
  92.                         USAGE;
  93.                         exit(1);
  94.                     }
  95.                     break;
  96.                 case 'm':                   /* color map definition */
  97.                     strcpy(palfile,argv[i+1]);
  98.                     break;
  99.                 default:
  100.                     USAGE;
  101.                     exit(1);
  102.             }
  103.     }
  104.  
  105. /*
  106. *  allocate memory for the image, if not enough, then error
  107. */
  108.     if (enoughspace(xdim,ydim)) {
  109.         puts("\n Not enough memory for image to be stored");
  110.         exit(1);
  111.     }
  112.  
  113. /*
  114. *  open file with list of file names to display
  115. */
  116.     if ( NULL == (fp = fopen(argv[argc-1],"ra" ))) {
  117.         printf("\n%s: Error opening %s",argv[0],argv[3]);
  118.         exit(1);
  119.     }
  120.  
  121. /*
  122. *  graphics initialization
  123. */
  124.     initpga();
  125.  
  126.     if (*palfile)                        /* if there is a palette specified */
  127.         loadpal(palfile);
  128.  
  129.  
  130. /*
  131. *  cycle through all of the file names
  132. */
  133.     *filename = '\0';
  134.  
  135.     while (NULL != fgets(filename,100,fp)) {     /* while there are files */
  136.  
  137.         if (*filename < 33)
  138.             break;               /* exit the program if end of list */
  139.  
  140.         p = filename;
  141.  
  142.         while (*p > 32)          /*  find the first non printable char or sp*/
  143.             p++;
  144.         *p = '\0';               /*  truncate the file name for use by open */
  145.  
  146.  
  147.         if ( 0 > (file = open(filename,O_RDONLY | O_RAW))) {
  148.             printf("\n%s: Error opening image file: %s",argv[0],filename);
  149.             restpga();
  150.             exit(1);
  151.         }
  152.  
  153.         readpic(file,xdim,ydim);        /* block read from disk */
  154.  
  155.         close(file);
  156.  
  157.         for (i=0; i<ydim; i++)          /* display the image */
  158.             LINEPUT;
  159.  
  160. /*
  161. *  see if we should pause because of keypress
  162. */
  163.         if (kbhit()) {                     /* if key(s) is pressed */
  164.             while (kbhit())        
  165.                 c = getch();            /* empty backlog */
  166.  
  167.             if (c == ' ') {
  168.                 while (!kbhit()) ;      /* wait for another to restart */
  169.                 c = getch();            /* clear buffer */
  170.             }
  171.  
  172.             if (c != ' ') {             /* RETURN will exit program */
  173.                 fclose(fp);
  174.                 restpga();
  175.                 exit(0);
  176.             }
  177.         }
  178.  
  179.     }
  180.  
  181.     fclose(fp);
  182.     restpga();
  183.     exit(0);
  184. }
  185.  
  186. /*************************************************************************/
  187. /*  readpic
  188. *
  189. *      block read the file and copy to main memory storage
  190. *   Block reading the hard disk (or floppy) saves enough time to pay for
  191. *   the extra memory-to-memory move and then some.
  192. *
  193. */
  194. readpic(file,len,lines)
  195.     int file,len,lines;
  196.     {
  197.     int i,lno,readsize,readfact;
  198.     char *p;
  199.  
  200.     readfact = RAWSIZE/len - 5;
  201.     readsize =  readfact*len;      /* get good blocking factor */
  202.  
  203.     lno = 0;
  204.     do {
  205.         read(file,raw,readsize);        /* read one block */
  206.         p = raw;                        /* point to that block */
  207.  
  208. /*
  209. *  for this block, copy each line into it's own reserved memory
  210. */
  211.         for (i=0; i < readfact && lno < lines; i++) {
  212.             movmem(p,store[lno++],len);
  213.             p += len;
  214.         }
  215.  
  216.     } while (lno < lines);
  217.  
  218. }
  219.         
  220. /**********************************************************************/
  221. /*  enoughspace
  222. *
  223. *   make enough room for maximum row,col size
  224. *
  225. *   Image memory is allocated line by line because of 64K data limitation
  226. *   of the PC.  
  227. *
  228. *   returns error code of 1 if not enough memory
  229. */
  230. enoughspace(c,l)
  231.     int l,c;
  232.     {
  233.     int i;
  234.  
  235.        for (i=0; i < l; i++) {                /* allocate each line separately */
  236.         store[i] = malloc(c);
  237.  
  238.         if (store[i] == NULL)           /* malloc returned not enough */
  239.             return(1);
  240.     }
  241.  
  242.     return(0);                             /* successful return */
  243.  
  244. }
  245.  
  246.  
  247.  
  248. loadpal(s)
  249.     char *s;
  250.     {
  251.     int i;
  252.     FILE *pfp;
  253.  
  254.     if (NULL == (pfp = fopen(s,"rb"))) {        puts("Error on palette file open ");
  255.         restpga();        exit(2);    }
  256.     for (i=0; i < 256 ; i++) {        fread(rmap,1,256,pfp);        fread(gmap,1,256,pfp);        fread(bmap,1,256,pfp);   }
  257.    fclose(pfp);
  258. /*
  259. *  divide by 16 to fit PGA
  260. */
  261.  
  262.     for (i=0; i< 256; i++) {
  263.         mapcolor(i,rmap[i]>>4,gmap[i]>>4,bmap[i]>>4);
  264.     }
  265.  
  266.  
  267. }
  268.