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