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