home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / pcshow / src / lattice / pcshow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-06  |  72.1 KB  |  2,087 lines

  1. /*
  2. *
  3. *  Raster image display program for the IBM PC  
  4. *
  5. *    This program takes HDF file images or binary images, one byte per pixel,
  6. *  and displays them on the PC's color graphics screen.  The dimensions of the
  7. *  image are specified on the command line for the binary images, or are in the
  8. *  HDF file.  Each image is read from the file, then displayed in the specified
  9. *  position on the screen.  Portions of the image that do not fit on the screen
  10. *  are clipped.
  11. *
  12. *    Command line options are as follows:
  13. *        -b xdim ydim        : The dimensions of the binary file, and specifies 
  14. *                                binary file type of data
  15. *        -m mapfile            : The name of a palette for a binary file
  16. *        -v                     : Sets VGA display mode
  17. *        -e                    : Sets EGA display mode (default)
  18. *        -9                    : Sets Number 9 display mode
  19. *        -h                    : Specifies reading from an HDF file (default)
  20. *        -c                    : Center the image on the screen
  21. *        -p xwhere ywhere     : Specify an x & y position for the upper left hand
  22. *                                corner of the screen
  23. *        -a                    : Set animation mode
  24. *        -A                    : Set animation mode and specify animation of multiple
  25. *                                images from only one hdf file
  26. *        -s                    : Specifies only one binary image and name is filename
  27. *        filename            : The file containing a list of binary images or
  28. *                                an HDF file containing images
  29. *  example use:    display -b 256 256 -c -m palette.pal list.dat
  30. *     the file 'list.dat' must contain a list of file names of the images
  31. *     that are to be displayed in sequence.
  32. *
  33. *  Note:  for binary file usage, both the -b xdim ydim and the -m mapfile
  34. *        options must be used for the correct displaying of the image.
  35. *        The file name at the end of the command line is a mandatory command
  36. *        line parameter, the rest of the command line parameters are optional.
  37. *
  38. *  The options available while an image is being displayed can be shown by
  39. *        typing '?'
  40. *
  41. *  The rest of the external C functions can be found in "options.c"
  42. *
  43. *  National Center for Supercomputing Applications, University of Illinois
  44. *  153 Computing Applications Building
  45. *  605 E. Springfield Ave.
  46. *  Champaign, IL  61820     (217)244-0072
  47. *
  48. *  Quincey Koziol            August 1988
  49. *
  50. */
  51.  
  52. #include "show.h"        /* include file for other include files and variables */
  53.  
  54. /* declarations of types for routines used */
  55.  
  56. extern long DFgetelement();    /* HDF call to get an element from a file */
  57. extern long DFputelement();    /* HDF call to put an element in a file */
  58. extern long unrleit();            /* call to un-run length encode a line of an image */
  59. extern int    DFsetfind();        /* HDF call to start a wildcard find operation */
  60. extern int    DFfind();            /* HDF call to perform the wildcard find */
  61. extern int    DFupdate();            /* HDF call to update a file after changes have been written */
  62. extern int    DFaccess();            /* HDF call to initiate reads or writes on a file */
  63. extern int    DFdup();            /* HDF call to create a new tag/ref indentical to an old one */
  64. extern int    DFclose();            /* HDF call to close an HDF file */
  65. extern int expand();    /* routine to expand a given box in the image to fill the screen */
  66. extern int interpolate();    /* routine to expand and smooth a given box in the image to fill the screen */
  67. extern char *malloc();            /* memory allocation procedure */
  68. extern char *calloc();            /* another memory allocation procedure */
  69. #ifdef QAK
  70. extern unsigned char pixelcolor();    /* returns the color of a pixel specified */
  71. #endif
  72. extern DF    *DFopen();            /* HDF call to open a file and return a pointer to that file */
  73. extern void unimcomp();        /* call to uncompress an imcomp compressed line */
  74. extern void read_pic();        /* routine to read in an imcomp compressed HDF image in ega mode */
  75. extern void readpic();            /* routine to read in a raster image from an HDF file */
  76. extern void readcpic();        /* routine to read in a run-length-encoded image from an HDF file */
  77. extern void readipic();        /* routine to read in an imcomp compressed HDF image for other display modes */
  78. extern void loadega();            /* procedure to read in an HDF palette in ega mode */
  79. extern void select_pal();        /* procedure to select a palette of 16 colors out of 256 */
  80. extern void grafmode();        /* routine to go into the correct graphics mode */
  81. extern void textmode();        /* routine to go into text mode */
  82. extern void showpic();            /* routine to display the picture on the screen when in graphics mode */
  83. extern void loadpal();            /* procedure to load the palette for other display modes */
  84. extern void showpal();    /* routine to show the palette on the screen */
  85. extern void nopal();    /* routine to remove the palette from the screen */
  86. extern void options();    /* main routine to execute runtime options from */
  87. extern void putpal();            /* routine reload the internal palette which the values from current palette arrays */
  88. extern void findpal();    /* routine to get a new palette from an HDF file */
  89. extern void newpal();            /* routine to get a new palette in binary mode */
  90. extern void initpal();            /* procedure to load in the initial palette */
  91. extern void rawpic();            /* call to read in the binary image */
  92. extern void printdir();        /* routine to display a list of palettes in the current directory */
  93. extern void swap();            /* swap two character values */
  94. #ifdef QAK
  95. extern void slidepal();        /* slide the palette left or right */
  96. #endif
  97. extern void squishpal();    /* compress or expand the palette */
  98. #ifdef MOUSE
  99. extern void mousefunc();    /* mouse functions for screen manipulation */
  100. #endif
  101. extern void updatescrn();        /* updates the screen with the image, color bar and mouse cursor */
  102. /*void t16line();*/                /* blast a targa line out to the targa board */
  103. /*void pline();*/
  104. /*void GraphInit();*/            /* routine in 'tarinit.c' to initialize the targa board */
  105. extern void makepal();            /* make a grey scale palette as the first palette */
  106. extern void displayerr();        /* display a string on the text screen */
  107. extern void usage();            /* display the command line parameters */
  108. extern void debug();            /* display debugging variables */
  109. extern void debugf();            /* display float debugging variables */
  110. extern void waitq();    /* routine to wait for a keypress */
  111. #ifdef QAK
  112. extern void bounce();    /* routine to bounce a ball on the screen until keyboard or mouse status is changed */
  113. #endif
  114. extern void swapint();    /* routine to swap two integer values */
  115. extern void parse();    /* parse the commands generated by options() */
  116. extern void NO9setup();        /* number nine routines to initialize the screen */
  117. extern void clrbuf();
  118. extern void RG9gmode();
  119. extern void RG9clrscr();
  120. extern void RG9init();
  121. extern void do_hdf();
  122. extern void do_raster();
  123. extern void draw_mouse();
  124. extern void erase_mouse();
  125. extern void read_mouse();
  126.  
  127. void main(argc,argv)        /* main routine */
  128.     int argc;        /* argument count */
  129.     char *argv[];    /* pointer to the argument list */
  130. {
  131.     mode=EGA;        /*default mode for displaying images */
  132.     maxx=640;
  133.     maxy=350;
  134.     pal_height=7;
  135.     if(argc<2){        /* if there are no command line parameters then print them */
  136.         usage();
  137.         exit(0);
  138.       }    /* end if */
  139.     for (i=1; i<argc; i++) {      /* look at each parm */
  140.         if (*argv[i]=='-') {
  141.             switch ( *(argv[i]+1)) { 
  142.                 case 'c':    /*  centering  */
  143.                     center=1;                /* set centering to calculate after the dimensions have been found */
  144.                     break;
  145.  
  146.                 case 'p':    /* special position */
  147.                     xwhere=atoi(argv[++i]);
  148.                     ywhere=atoi(argv[++i]);
  149.                     if (xwhere > maxx || ywhere > maxy) {
  150.                         puts("Invalid position\n");
  151.                         usage();
  152.                         exit(1);
  153.                       }    /* end if */
  154.                     break;
  155.  
  156.                 case 'h':    /* the file type specified in HDF */
  157.                     filetype='h';
  158.                     break;
  159.  
  160.                 case 'a':    /* set animation mode */
  161.                     animate=1;
  162.                     one_file=0;
  163.                     break;
  164.  
  165.                 case 'A':    /* set one file animation mode for hdf files */
  166.                     animate=1;
  167.                     one_file=1;
  168.                     break;
  169.  
  170.                 case 's':    /* set single binary image mode */
  171.                     oneimage=1;
  172.                     break;
  173.  
  174.                 case 'b':    /* the files are going to be binary raster 8 */
  175.                     filetype='b';
  176.                     xdim=atoi(argv[++i]);
  177.                     ydim=atoi(argv[++i]);
  178.                     if (xdim < 1 || ydim < 1) {
  179.                         puts("Invalid dimensions\n");
  180.                         usage();
  181.                         exit(1);
  182.                       } /* end if */
  183.                     break;
  184.  
  185.                 case VGA:    /* display mode is vga */
  186.                     mode=*(argv[i]+1);
  187.                     maxx=320;
  188.                     maxy=200;
  189.                     pal_height=7;
  190.                     break;
  191.  
  192.                 case EGA:    /* display mode is ega */
  193.                     mode=*(argv[i]+1);
  194.                     maxx=640;
  195.                     maxy=350;
  196.                     pal_height=7;
  197.                     break;
  198.  
  199. #ifdef FUTURE
  200.                  case TARGA:    /* display mode is t16 */
  201.                      mode=*(argv[i]+1);
  202.                      maxx=512;
  203.                      maxy=485;
  204.                      break;
  205. #endif
  206.  
  207.                 case NO9:    /* display mode is no9 */
  208.                     mode=*(argv[i]+1);
  209.                     maxx=512;
  210.                     maxy=500;
  211.                     pal_height=15;
  212.                     break;
  213.  
  214.                 case 'm':    /* name of palette file for binary images */
  215.                     strcpy(palfile,argv[i+1]);
  216.                     break;
  217.  
  218.                 default:    /* not a valid command parameter, so tell them the parameters */
  219.                     usage();
  220.                     exit(1);
  221.               } /* end switch */
  222.           } /* end if */
  223.       } /* end for */
  224.     pal_yoff=0;
  225.  
  226. /* set the directory from the command */
  227.     getcd((int)(getdsk()+1),filename);            /* get the current directory */
  228.     strcpy(old_path,"\\");                        /* insert the slah in front of the directory */
  229.     strcat(old_path,filename);
  230.     strsfn(argv[argc-1],drive,path,node,ext);        /* convert the filename on the command line into a drive, path, node, and extension */
  231.     strcpy(filename,drive);                            /* concatenate the drive and path names together for the setdir call */
  232.     strcat(filename,path);
  233.     if((i=strcmp(filename,""))!=0)        /* check for null directory */
  234.         if((i=chdir(filename))!=0) {        /* set the directory and check for a bad directory */
  235.             printf("Bad Path Specified, Error=%d\n",i);
  236.             exit(1);
  237.           }    /* end if */
  238.  
  239. /* start doing stuff with the image(s) */
  240.  
  241.     if(animate==1) {        /* do both hdf and raster animations here */
  242.         if(filetype==HDF) {        /* do a hdf animation */
  243.             if(one_file==1) {    /* animate all the images from just one hdf file */
  244.                 grafmode();        /* go to correct graphics screen type and load palette */
  245.                 do {            /* cycle through until the user wants to get out */
  246.                     strcpy(filename,argv[argc-1]);        /* get a copy of the file to be read */
  247.                     file_attr=0;                        /* just look for normal files */
  248.                     file_err=find_1st(filename,file_attr);    /* get the first file to display */
  249.  
  250.                     do {                                /* display all the files */
  251.  
  252.                         strcpy(filename,DTAPTR+30);            /* get the filename from the find routine */
  253.  
  254.                         do_hdf(filename);                /* display all the images in and hdf file */
  255. #ifdef QAK
  256. /* Open DF file for reading */
  257.  
  258.                         if(NULL==(dff=DFopen(filename,DFACC_ALL,16))) {
  259.                             printf("Cannot open HDF file, HDF error:%d\n", DFerror);
  260.                             chdir(old_path);                /* restore the old path */
  261.                             exit(1);
  262.                           }    /* end if */
  263.  
  264. /* get the dimensions */
  265.  
  266.                         if(first_hdf==1) {
  267.                             DFsetfind(dff,DFTG_ID8,DFREF_WILDCARD);    /* set wildcard find to image dimension searching */
  268.  
  269.                             if(!DFfind(dff,&ddstr)) {            /* look for image dimensions */
  270.                                 if(0>DFgetelement(dff,ddstr.tag,ddstr.ref,&dims)) {
  271.                                     printf("Error getting dimensions from file: %d\n",DFerror);
  272.                                     chdir(old_path);                /* restore the old path */
  273.                                     exit(1);
  274.                                   }    /* end if */
  275.                               }    /* end if */
  276.                             else {
  277.                                 printf("error from DFfind %d\n",DFerror);
  278.                                 chdir(old_path);                /* restore the old path */
  279.                                 exit(1);
  280.                               }    /* end else */
  281.  
  282.                             xdim=intswap(dims.xdim);
  283.                             ydim=intswap(dims.ydim);
  284.  
  285.                             if(center) {                /* find the image's position on the screen */
  286.                                 xwhere=(maxx-xdim)/2;
  287.                                 ywhere=(maxy-ydim)/2;
  288.                                 if(xwhere<0) 
  289.                                     xwhere=0;            /* if larger than screen, */
  290.                                 if(ywhere<0)            /* don't center it */
  291.                                     ywhere=0;
  292.                               }    /* end if */
  293.  
  294. /* allocate memory for the image, if not enough, then error */
  295.  
  296.                             if(enoughspace(xdim,ydim)) {
  297.                                 printf(mem_error);
  298.                                 chdir(old_path);                /* restore the old path */
  299.                                 exit(1);
  300.                               } /* end if */
  301.                             first_hdf=0;                /* reset the first_hdf bit */
  302.                           }    /* end if */
  303.  
  304. /* cycle through all of the raster images in the file */
  305.  
  306.                         c=' ';
  307.                         cycled=0;
  308.                         DFsetfind(dff,DFTG_RI8,DFREF_WILDCARD);        /* set wildcard search for raster 8 images */
  309.     
  310.                         while(!DFfind(dff,&ddstr) && animate && !getout) {        /* look for the raster 8 images */
  311.                             imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  312.                             imref=ddstr.ref;
  313.                             readpic(xdim,ydim);        /* block read from disk */
  314.                             if(!cycled)
  315.                                 initpal();            /* loads in the palette from disk */
  316.                             showpic();        /* put the picture on the screen */
  317.                             cycled=1;        /* set flag to display that an animation has occured */
  318.                             if(kbhit()) {    /* check for keypress to interupt animation */
  319.                                 i=getch();    /* flush the keyboard buffer */
  320.                                 if(i==0)                /* get extended code if necessary */
  321.                                     i=getch();
  322.                                 if(show)        /* if palette is supposed to be display then display it again */
  323.                                     showpal();
  324.                                 options();
  325.                               }    /* end if */
  326.                           } /* end while */
  327.     
  328. /* cycle through all of the run length compressed images in the file */
  329.  
  330.                         c=' ';
  331.                         DFsetfind(dff,DFTG_CI8,DFREF_WILDCARD);
  332.  
  333.                         while(!DFfind(dff,&ddstr) && animate && !getout) {
  334.                             imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  335.                             imref=ddstr.ref;
  336.                                 readcpic(xdim,ydim);        /* block read run length compressed image from disk */
  337.                             if(!cycled)
  338.                                 initpal();            /* loads in the palette from disk */
  339.                             showpic();            /* put image on the screen */
  340.                             cycled=1;        /* set flag to display that an animation has occured */
  341.                             if(kbhit()) {    /* check for keypress to interupt animation */
  342.                                 i=getch();    /* flush the keyboard buffer */
  343.                                 if(i==0)                /* get extended code if necessary */
  344.                                     i=getch();
  345.                                 if(show)        /* if palette is supposed to be display then display it again */
  346.                                     showpal();
  347.                                 options();
  348.                               }    /* end if */
  349.                           } /* end while */
  350.  
  351. /* cycle through all of the imcomp compressed images in the file */
  352.  
  353.                         c=' ';
  354.                         DFsetfind(dff,DFTG_II8,DFREF_WILDCARD);    /* set wildcard search for imcomp compressed images */
  355.  
  356.                         while(!DFfind(dff,&ddstr) && animate && !getout) {        /* look for imcomp compressed images */
  357.                             imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  358.                             imref=ddstr.ref;
  359.                             if(!doneinit) {        /* if the palette hasn't been initialized */
  360.                                 initpal();        /* then initialize it for incomp compressed images */
  361.                                 doneinit=1;
  362.                               }    /* end if */
  363.                             if(mode==EGA) {            /* initialize things a certain way for ega mode */
  364.                                  read_pic(xdim,ydim);        /* block read from disk for ega mode */
  365.                                 select_pal(xdim,ydim,0);     /* set up the translation table for ega */
  366.                                 memcpy(egapals[0],regrs,16);
  367.                               }    /* end if */
  368.                             else
  369.                                 readipic(xdim,ydim);    /* block read from disk for other modes */
  370.                             showpic();            /* put the image on the screen */
  371.                             cycled=1;        /* set flag to display that an animation has occured */
  372.                             if(kbhit()) {    /* check for keypress to interupt animation */
  373.                                 i=getch();    /* flush the keyboard buffer */
  374.                                 if(i==0)                /* get extended code if necessary */
  375.                                     i=getch();
  376.                                 if(show)        /* if palette is supposed to be display then display it again */
  377.                                     showpal();
  378.                                 options();
  379.                               }    /* end if */
  380.                           } /* end while */
  381.                         DFclose(dff);            /* close the hdf file */
  382. #endif
  383.                       }    while((file_err=findnext())==0);    /* keep reading files until an error is returned (no more files) */
  384.                     for(i=anispeed; i>0; i--);    /* delay loop */
  385.                     if(cycled) {
  386.                         showpic();
  387.                         if(show)        /* if palette is supposed to be display then display it again */
  388.                             showpal();
  389.                         options();
  390.                       }    /* end if */
  391.                   }    while(c!='q' && animate && cycled && !getout);    /* keep cycling until the user wants out */
  392.               }    /* end if */
  393.             else {            /* animate from a list of hdf files */
  394.                 grafmode();            /* go into correct graphics mode */
  395.                 c=' ';
  396.                 while(c!='q' && animate && !getout) {        /* open file with list of file names to display */
  397.                     if(NULL==(fp=fopen(argv[argc-1],"ra"))) {
  398.                         textmode();
  399.                         printf("\n%s: Error opening %s",argv[0],argv[argc-1]);
  400.                         chdir(old_path);                /* restore the old path */
  401.                         exit(0);
  402.                       } /* end if */
  403.     
  404.                     if(palmax==-1 && *palfile!=NULL)    /* if there is a palette specified then load it */
  405.                         newpal(palfile);
  406.                     putpal();
  407.  
  408. /* cycle through all of the file names */
  409.  
  410.                     *filename='\0';
  411.  
  412.                     while((NULL!=fgets(filename,100,fp)) && animate && !getout) {
  413.                         if(*filename<33)
  414.                             break;                /* exit the program if end of list */
  415.                         p=filename;
  416.                         while(*p>32)            /*  find the first non printable char or space */
  417.                             p++;
  418.                         *p='\0';                /*  truncate the file name for use by open */
  419.  
  420.                         file_attr=0;                        /* just look for normal files */
  421.                         file_err=find_1st(filename,file_attr);    /* get the first file to display */
  422.  
  423.                         do {                                /* display all the files */
  424.  
  425.                             strcpy(filename,DTAPTR+30);            /* get the filename from the find routine */
  426.                             do_hdf(filename);
  427. #ifdef QAK
  428. /* Open DF file for reading */
  429.  
  430.                             if(NULL==(dff=DFopen(filename,DFACC_ALL,16))) {
  431.                                 printf("Cannot open HDF file=%s, HDF error:%d\n",filename,DFerror);
  432.                                 chdir(old_path);                /* restore the old path */
  433.                                 exit(1);
  434.                               }    /* end if */
  435.  
  436.                             if(first_hdf==1) {        /* the first time through an hdf animation, get the dimensions and allocate spcae, etc. */
  437.  
  438. /* get the dimensions */
  439.  
  440.                                 DFsetfind(dff,DFTG_ID8,DFREF_WILDCARD);    /* set wildcard find to image dimension searching */
  441.  
  442.                                 if(!DFfind(dff,&ddstr)) {            /* look for image dimensions */
  443.                                     if(0>DFgetelement(dff,ddstr.tag,ddstr.ref,&dims)) {
  444.                                         printf("Error getting dimensions from file: %d\n",DFerror);
  445.                                         chdir(old_path);                /* restore the old path */
  446.                                         exit(1);
  447.                                       }    /* end if */
  448.                                   }    /* end if */
  449.                                 else {
  450.                                     printf("error from DFfind %d\n",DFerror);
  451.                                     chdir(old_path);                /* restore the old path */
  452.                                     exit(1);
  453.                                   }    /* end else */
  454.  
  455.                                 xdim=intswap(dims.xdim);
  456.                                 ydim=intswap(dims.ydim);
  457.  
  458.                                 if(center) {                /* find the image's position on the screen */
  459.                                     xwhere=(maxx-xdim)/2;
  460.                                     ywhere=(maxy-ydim)/2;
  461.                                     if(xwhere<0) 
  462.                                         xwhere=0;            /* if larger than screen, */
  463.                                     if(ywhere<0)            /* don't center it */
  464.                                         ywhere=0;
  465.                                   }    /* end if */
  466.  
  467. /* allocate memory for the image, if not enough, then error */
  468.  
  469.                                 if(enoughspace(xdim,ydim)) {
  470.                                     printf(mem_error);
  471.                                     chdir(old_path);                /* restore the old path */
  472.                                     exit(1);
  473.                                   } /* end if */
  474.  
  475.                                 first_hdf=0;
  476.                               }    /* end if */
  477.  
  478. /* cycle through all of the raster images in the file */
  479.  
  480.                             c=' ';
  481.                             cycled=0;
  482.                             DFsetfind(dff,DFTG_RI8,DFREF_WILDCARD);        /* set wildcard search for raster 8 images */
  483.  
  484.                             while(!DFfind(dff,&ddstr) && animate && !getout) {        /* look for the raster 8 images */
  485.                                 imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  486.                                 imref=ddstr.ref;
  487.                                 readpic(xdim,ydim);        /* block read from disk */
  488.                                 if(!cycled)
  489.                                     initpal();            /* loads in the palette from disk */
  490.                                 showpic();        /* put the picture on the screen */
  491.                                 cycled=1;        /* set flag to display that an animation has occured */
  492.                                 if(kbhit()) {    /* check for keypress to interupt animation */
  493.                                     i=getch();    /* flush the keyboard buffer */
  494.                                     if(i==0)                /* get extended code if necessary */
  495.                                         i=getch();
  496.                                     if(show)        /* if palette is supposed to be display then display it again */
  497.                                         showpal();
  498.                                     options();
  499.                                   }    /* end if */
  500.                               } /* end while */
  501.  
  502. /* cycle through all of the run length compressed images in the file */
  503.  
  504.                             c=' ';
  505.                             DFsetfind(dff,DFTG_CI8,DFREF_WILDCARD);
  506.  
  507.                             while(!DFfind(dff,&ddstr) && animate && !getout) {
  508.                                 imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  509.                                 imref=ddstr.ref;
  510.                                 readcpic(xdim,ydim);        /* block read run length compressed image from disk */
  511.                                 if(!cycled)
  512.                                 initpal();            /* loads in the palette from disk */
  513.                                 showpic();            /* put image on the screen */
  514.                                 cycled=1;        /* set flag to display that an animation has occured */
  515.                                 if(kbhit()) {    /* check for keypress to interupt animation */
  516.                                     i=getch();    /* flush the keyboard buffer */
  517.                                     if(i==0)                /* get extended code if necessary */
  518.                                         i=getch();
  519.                                     if(show)        /* if palette is supposed to be display then display it again */
  520.                                         showpal();
  521.                                     options();
  522.                                   }    /* end if */
  523.                               } /* end while */
  524.  
  525. /* cycle through all of the imcomp compressed images in the file */
  526.  
  527.                             c=' ';
  528.                             DFsetfind(dff,DFTG_II8,DFREF_WILDCARD);    /* set wildcard search for imcomp compressed images */
  529.  
  530.                             while(!DFfind(dff,&ddstr) && animate && !getout) {        /* look for imcomp compressed images */
  531.                                 imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  532.                                 imref=ddstr.ref;
  533.                                 if(!doneinit) {        /* if the palette hasn't been initialized */
  534.                                     initpal();        /* then initialize it for incomp compressed images */
  535.                                     doneinit=1;
  536.                                   }    /* end if */
  537.  
  538.                                 if(mode==EGA) {            /* initialize things a certain way for ega mode */
  539.                                      read_pic(xdim,ydim);        /* block read from disk for ega mode */
  540.                                     select_pal(xdim,ydim,0);     /* set up the translation table for ega */
  541.                                     memcpy(egapals[0],regrs,16);
  542.                                   }    /* end if */
  543.                                 else
  544.                                     readipic(xdim,ydim);    /* block read from disk for other modes */
  545.                                 showpic();            /* put the image on the screen */
  546.                                 cycled=1;        /* set flag to display that an animation has occured */
  547.                                 if(kbhit()) {    /* check for keypress to interupt animation */
  548.                                     i=getch();    /* flush the keyboard buffer */
  549.                                     if(i==0)                /* get extended code if necessary */
  550.                                         i=getch();
  551.                                     if(show)        /* if palette is supposed to be display then display it again */
  552.                                         showpal();
  553.                                     options();
  554.                                   }    /* end if */
  555.                               } /* end while */
  556.                             DFclose(dff);            /* close the hdf file */
  557. #endif
  558.                           }    while((file_err=findnext())==0);    /* keep going until no more files with that name */
  559.                       }    /* end while */
  560.                     for(i=anispeed; i>0; i--);    /* delay loop */
  561.                     if(cycled) {
  562.                         showpic();
  563.                         if(show)        /* if palette is supposed to be display then display it again */
  564.                             showpal();
  565.                         options();
  566.                       }    /* end if */
  567.                   }    /* end while */
  568.               }    /* end else */
  569.           }    /* end if */
  570.         else {                    /* do a raster eight animation */
  571.  
  572. /* allocate memory for the image, if not enough, then error */
  573.  
  574.             if(enoughspace(xdim,ydim)) {
  575.                 printf(mem_error);
  576.                 chdir(old_path);                /* restore the old path */
  577.                 exit(1);
  578.               }    /* end if */
  579.  
  580.             if(center) {                /* find the image's position on the screen */
  581.                 xwhere=(maxx-xdim)/2;
  582.                 ywhere=(maxy-ydim)/2;
  583.                 if(xwhere<0) 
  584.                     xwhere=0;            /* if larger than screen, */
  585.                 if(ywhere<0)            /* don't center it */
  586.                     ywhere=0;
  587.               }    /* end if */
  588.               
  589.             grafmode();            /* go into correct graphics mode */
  590.  
  591. /* open file with list of file names to display */
  592.  
  593.             if(one_file==1) {    /* if there is only one file name specified for the animation */
  594.                 c=' ';
  595.                 while(c!='q' && animate && !getout) {
  596.  
  597.                     if(palmax==-1 && *palfile!=NULL)    /* if there is a palette specified then load it */
  598.                         newpal(palfile);
  599.                     putpal();
  600.  
  601. /* cycle through all of the file names */
  602.  
  603.                     file_attr=0;            /* look for normal files */
  604.                     file_err=find_1st(argv[argc-1],file_attr);    /* get the name of the first file which matches */
  605.  
  606.                     do {
  607.                         strcpy(filename,DTAPTR+30);
  608.                         do_raster(filename);
  609.  
  610. #ifdef QAK
  611.                         if(0>(file=open(filename,O_RDONLY | O_RAW))) {        /* open the image file */
  612.                             textmode();
  613.                             printf("\n%s: Error opening image file: %s",argv[0],filename);
  614.                             chdir(old_path);                /* restore the old path */
  615.                             exit(1);
  616.                           }    /* end if */
  617.                         rawpic(file,xdim,ydim);            /* block read image from from disk */
  618.                         close(file);                    /* close the image file */
  619.                         showpic();            /* put the image on the screen */
  620.                         if(kbhit()) {    /* check for keypress to interupt animation */
  621.                             i=getch();    /* flush the keyboard buffer */
  622.                             if(i==0)                /* get extended code if necessary */
  623.                                 i=getch();
  624.                             if(show)        /* if palette is supposed to be display then display it again */
  625.                                 showpal();
  626.                             options();
  627.                           }    /* end if */
  628. #endif
  629.                         for(i=anispeed; i>0; i--);    /* delay loop */
  630.                       }    while((file_err=findnext())==0);    /* keep cycling through the files until all the filenames are exhausted */
  631.                     showpic();
  632.                     if(show)        /* if palette is supposed to be display then display it again */
  633.                         showpal();
  634.                     options();            /* do the fancy things on the screen */
  635.                   }    /* end while */
  636.               }    /* end if */
  637.             else {                /* animate from a file of filenames */
  638.                 c=' ';
  639.                 while(c!='q' && animate && !getout) {
  640.                     if(NULL==(fp=fopen(argv[argc-1],"ra"))) {
  641.                         printf("\n%s: Error opening %s",argv[0],argv[argc-1]);
  642.                         chdir(old_path);                /* restore the old path */
  643.                         exit(0);
  644.                       } /* end if */
  645.  
  646.                     if(palmax==-1 && *palfile!=NULL)    /* if there is a palette specified then load it */
  647.                         newpal(palfile);
  648.                     putpal();
  649.  
  650. /* cycle through all of the file names */
  651.  
  652.                     *filename='\0';
  653.  
  654.                     while((NULL!=fgets(filename,100,fp)) && animate && !getout) {
  655.                         if(*filename<33)
  656.                             break;                /* exit the program if end of list */
  657.                         p=filename;
  658.                         while(*p>32)            /*  find the first non printable char or space */
  659.                             p++;
  660.                         *p='\0';                /*  truncate the file name for use by open */
  661.  
  662.                         file_attr=0;            /* look for normal files */
  663.                         file_err=find_1st(filename,file_attr);    /* get the name of the first file which matches */
  664.  
  665.                         do {
  666.                             strcpy(filename,DTAPTR+30);
  667.                             do_raster(filename);
  668.  
  669. #ifdef QAK
  670.                             if(0>(file=open(filename,O_RDONLY | O_RAW))) {        /* open the image file */
  671.                                 textmode();
  672.                                 printf("\n%s: Error opening image file: %s",argv[0],filename);
  673.                                 chdir(old_path);                /* restore the old path */
  674.                                 exit(1);
  675.                               }    /* end if */
  676.                             rawpic(file,xdim,ydim);            /* block read image from from disk */
  677.                             close(file);                    /* close the image file */
  678.                             showpic();            /* put the image on the screen */
  679.                             if(kbhit()) {    /* check for keypress to interupt animation */
  680.                                 i=getch();    /* flush the keyboard buffer */
  681.                                 if(i==0)                /* get extended code if necessary */
  682.                                     i=getch();
  683.                                 if(show)        /* if palette is supposed to be display then display it again */
  684.                                     showpal();
  685.                                 options();
  686.                               }    /* end if */
  687. #endif
  688.                             for(i=anispeed; i>0; i--);    /* delay loop */
  689.                           }    while((file_err=findnext())==0);    /* keep cycling through the files until all the filenames are exhausted */
  690.                       } /* end while */
  691.                     showpic();
  692.                     if(show)        /* if palette is supposed to be display then display it again */
  693.                         showpal();
  694.                     options();            /* do the fancy things on the screen */
  695.                   }    /* end while */
  696.               }    /* end else */
  697.           }    /* end else */
  698.       }    /* end if */
  699.     else {    /* regular non-animation file loading */
  700.         if(filetype==HDF) {        /* hdf files specified */
  701.  
  702.         do_hdf(argv[argc-1]);
  703. #ifdef QAK
  704. /* Open DF file for reading */
  705.  
  706.             if(NULL==(dff=DFopen(argv[argc-1],DFACC_ALL,16))) {
  707.                 printf("Cannot open HDF file, HDF error:%d\n", DFerror);
  708.                 chdir(old_path);                /* restore the old path */
  709.                 exit(1);
  710.               }    /* end if */
  711.  
  712. /* get the dimensions */
  713.  
  714.             DFsetfind(dff,DFTG_ID8,DFREF_WILDCARD);    /* set wildcard find to image dimension searching */
  715.  
  716.             if(!DFfind(dff,&ddstr)) {            /* look for image dimensions */
  717.                 if(0>DFgetelement(dff,ddstr.tag,ddstr.ref,&dims)) {
  718.                     printf("Error getting dimensions from file: %d\n",DFerror);
  719.                     chdir(old_path);                /* restore the old path */
  720.                     exit(1);
  721.                   }    /* end if */
  722.               }    /* end if */
  723.             else{
  724.                 printf("error from DFfind %d\n",DFerror);
  725.                 chdir(old_path);                /* restore the old path */
  726.                 exit(1);
  727.               }    /* end else */
  728.         
  729.             xdim=intswap(dims.xdim);
  730.             ydim=intswap(dims.ydim);
  731.  
  732.             if(center) {                /* find the image's position on the screen */
  733.                 xwhere=(maxx-xdim)/2;
  734.                 ywhere=(maxy-ydim)/2;
  735.                 if(xwhere<0) 
  736.                     xwhere=0;            /* if larger than screen, */
  737.                 if(ywhere<0)            /* don't center it */
  738.                     ywhere=0;
  739.               }    /* end if */
  740.  
  741. /* allocate memory for the image, if not enough, then error */
  742.  
  743.             if(enoughspace(xdim,ydim)) {
  744.                 printf(mem_error);
  745.                 chdir(old_path);                /* restore the old path */
  746.                 exit(1);
  747.               } /* end if */
  748.  
  749.             grafmode();        /* go to correct graphics screen type and load palette */
  750.  
  751. /* cycle through all of the raster images in the file */
  752.  
  753.             DFsetfind(dff,DFTG_RI8,DFREF_WILDCARD);        /* set wildcard search for raster 8 images */
  754.  
  755.             while(!DFfind(dff,&ddstr) && !getout) {        /* look for the raster 8 images */
  756.                 imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  757.                 imref=ddstr.ref;
  758.                 readpic(xdim,ydim);        /* block read from disk */
  759.                 initpal();            /* loads in the palette from disk */
  760.                 showpic();        /* put the picture on the screen */
  761.                 if(show)        /* if palette is supposed to be display then display it again */
  762.                     showpal();
  763.                 options();        /* call routine for screen options */
  764.               } /* end while */
  765.  
  766. /* cycle through all of the run length compressed images in the file */
  767.  
  768.             DFsetfind( dff, DFTG_CI8, DFREF_WILDCARD);
  769.  
  770.             while(!DFfind( dff, &ddstr) && !getout) {
  771.                 imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  772.                 imref=ddstr.ref;
  773.                  readcpic(xdim,ydim);        /* block read run length compressed image from disk */
  774.                 initpal();            /* loads in the palette from disk */
  775.                 showpic();            /* put image on the screen */
  776.                 if(show)        /* if palette is supposed to be display then display it again */
  777.                     showpal();
  778.                 options();            /* call routine for screen options */
  779.               } /* end while */
  780.  
  781. /* cycle through all of the imcomp compressed images in the file */
  782.  
  783.             DFsetfind(dff,DFTG_II8,DFREF_WILDCARD);    /* set wildcard search for imcomp compressed images */
  784.  
  785.             while(!DFfind(dff,&ddstr) && !getout) {        /* look for imcomp compressed images */
  786.                 imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  787.                 imref=ddstr.ref;
  788.                 initpal();                /* neccessary for imcomp compressed images */
  789.                 if(mode==EGA){            /* initialize things a certain way for ega mode */
  790.                      read_pic(xdim,ydim);        /* block read from disk for ega mode */
  791.                     select_pal(xdim,ydim,0);     /* set up the translation table for ega */
  792.                     memcpy(egapals[0],regrs,16);
  793.                   }    /* end if */
  794.                 else
  795.                     readipic(xdim,ydim);    /* block read from disk for other modes */
  796.                 showpic();            /* put the image on the screen */
  797.                 if(show)        /* if palette is supposed to be display then display it again */
  798.                     showpal();
  799.                 options();            /* call routine for screen options */
  800.               } /* end while */
  801.             DFclose(dff);            /* close the hdf file */
  802. #endif
  803.           } /* end if */
  804.         else {        /* do raster images */
  805.  
  806. /* allocate memory for the image, if not enough, then error */
  807.  
  808.             if(enoughspace(xdim,ydim)) {
  809.                 printf(mem_error);
  810.                 chdir(old_path);                /* restore the old path */
  811.                 exit(1);
  812.               }    /* end if */
  813.  
  814.             if(center) {                /* find the image's position on the screen */
  815.                 xwhere=(maxx-xdim)/2;
  816.                 ywhere=(maxy-ydim)/2;
  817.                 if(xwhere<0) 
  818.                     xwhere=0;            /* if larger than screen, */
  819.                 if(ywhere<0)            /* don't center it */
  820.                     ywhere=0;
  821.               }    /* end if */
  822.  
  823. /* open file with list of file names to display */
  824.             if(oneimage) {    /* only read in one image and display it */
  825.                 do_raster(argv[argc-1]);
  826. #ifdef QAK
  827.                 if(0>(file=open(argv[argc-1],O_RDONLY|O_RAW))) {        /* open the image file */
  828.                     printf("\n%s: Error opening image file: %s",argv[0],argv[argc-1]);
  829.                     chdir(old_path);                /* restore the old path */
  830.                     exit(0);
  831.                   }    /* end if */
  832.                 rawpic(file,xdim,ydim);            /* block read image from from disk */
  833.                 close(file);                    /* close the image file */
  834.                 grafmode();            /* go into correct graphics mode */
  835.                 newpal(palfile);    /* load the new palette */
  836.                 putpal();
  837.                 showpic();            /* put the image on the screen */
  838.                 options();            /* do the fancy things on the screen */
  839. #endif
  840.               }    /* end if */
  841.             else {        /* read in a list of image names to display */
  842.                 if(NULL==(fp=fopen(argv[argc-1],"ra"))) {
  843.                     printf("\n%s: Error opening %s",argv[0],argv[argc-1]);
  844.                     chdir(old_path);                /* restore the old path */
  845.                     exit(1);
  846.                   } /* end if */
  847.  
  848. /* cycle through all of the file names */
  849.  
  850.                 *filename='\0';
  851.  
  852.                 while(NULL!=fgets(filename,100,fp) && !getout) {
  853.                     if(*filename<33)
  854.                         break;                /* exit the program if end of list */
  855.                     p=filename;
  856.                     while(*p>32)            /*  find the first non printable char or space */
  857.                         p++;
  858.                     *p='\0';                /*  truncate the file name for use by open */
  859.                     do_raster(filename);
  860. #ifdef QAK
  861.                     if(0>(file=open(filename,O_RDONLY|O_RAW))) {        /* open the image file */
  862.                         textmode();
  863.                         printf("\n%s: Error opening image file: %s",argv[0],filename);
  864.                         chdir(old_path);                /* restore the old path */
  865.                         exit(1);
  866.                       }    /* end if */
  867.                     rawpic(file,xdim,ydim);            /* block read image from from disk */
  868.                     close(file);                    /* close the image file */
  869.                     grafmode();                        /* go into correct graphics mode */
  870.                     if(palmax==-1 && *palfile!=NULL)    /* if there is a palette specified then load it */
  871.                         newpal(palfile);
  872.                     putpal();
  873.                     showpic();            /* put the image on the screen */
  874.                     options();            /* do the fancy things on the screen */
  875. #endif
  876. /*
  877. *  insert each image functions here(click or gwrite)
  878. */
  879. /*             gwrite(filename); */
  880.                   } /* end while */
  881.               }    /* end else */
  882.           }    /* end else */
  883.       } /* end else */
  884.     textmode();
  885.     chdir(old_path);                /* restore the old path */
  886. } /* end main */
  887.  
  888. /**********************************************************************
  889. *  Function    :    enoughspace
  890. *  Purpose    :    make enough room for the maximum row and column size.
  891. *            Image memory is allocated line by line because of 64k data
  892. *            limitation of the PC.
  893. *  Parameters    :
  894. *            c - the number of columns in the image
  895. *            l - the number of lines in the image
  896. *  Returns    :
  897. *            1 if not enough memory
  898. *            0 if enough memory
  899. *  Calls    :    max()
  900. *  Called by    :    main()
  901. **********************************************************************/
  902. int enoughspace(c,l)
  903. int l,    /* the number of lines in image */
  904.     c;    /* the number of columns in the image */
  905. {
  906.     int i;    /* local counting variable */
  907.  
  908.        for (i=0; i < l; i++) {                /* allocate each line separately */
  909.         store[i]=malloc(max(c,256)); /* with a minimum of 256 pixels per line allocated */
  910.  
  911.         if (store[i]==NULL)           /* malloc returned not enough */
  912.             return(1);
  913.     }    /* end for */
  914.     return(0);                             /* successful return */
  915. } /* end enoughspace() */
  916.  
  917. /**********************************************************************
  918. *  Function    :    rawpic
  919. *  Purpose    :    block read the binary image from disk and copy to main memory
  920. *                    storage.
  921. *  Parameters    :
  922. *            file - the file pointer
  923. *            len     - the length of a line (xdim)
  924. *            lines - the number of lines (ydim)
  925. *  Returns    :    none
  926. *  Calls    :    none
  927. *  Called by    :    main()
  928. **********************************************************************/
  929. void rawpic(file,len,lines)
  930. int file,    /* the file pointer */
  931.     len,    /* the length of a line of the binary image */
  932.     lines;    /* the number of lines in the image */
  933. {
  934.     int i,    /* local temporary counting variable */
  935.         lno,    /* the current line number */
  936.         readsize,    /* the size to block read */
  937.         readfact;    /* the block reading factor */
  938.     char *p;    /* pointer to the buffer */
  939.  
  940.     readfact=RAWSIZE/len-5;
  941.     readsize=readfact*len;      /* get good blocking factor */
  942.  
  943.     lno=0;
  944.     do {
  945.         read(file,raw,readsize);        /* read one block */
  946.         p=raw;                        /* point to that block */
  947.  
  948. /*
  949. *  for this block, copy each line into it's own reserved memory
  950. */
  951.         for (i=0; i < readfact && lno < lines; i++) {
  952.             memcpy(store[lno++],p,len);
  953.             p += len;
  954.         } /* end for */
  955.  
  956.     } while (lno < lines);
  957.  
  958. } /* end rawpic() */
  959.  
  960. /**********************************************************************
  961. *  Function    :    readpic
  962. *  Purpose    :    block read the image from the HDf file into main memory
  963. *                    storage
  964. *  Parameters    :
  965. *            len - the length of a line (xdim)
  966. *            lines - the number of lines (ydim)
  967. *  Returns    :    none
  968. *  Calls    :    DFaccess(), DFread()
  969. *  Called by    :    main()
  970. **********************************************************************/
  971. void readpic(len,lines)
  972. int len,    /* the length of a line */
  973.     lines;    /* the number of lines */
  974. {
  975.     int i,j,    /* local temporary counting variable */
  976.         lno,    /* the current line number */
  977.         readsize,    /* the size to block read */
  978.         readfact;    /* the block reading factor */
  979.     char *p;    /* pointer to the buffer */
  980.  
  981. /* prime DF routines */
  982.  
  983.     DFaccess( dff, ddstr.tag, ddstr.ref, "r");    /* tell HDF to start reading */
  984.  
  985.     readfact=RAWSIZE/len - 5;
  986.     readsize= readfact*len;      /* get good blocking factor */
  987.     lno=0;
  988.     do {
  989.         j=(int) DFread(dff,raw,(long)readsize);        /* read one block */
  990.         p=raw;                        /* point to that block */
  991.  
  992. /* for this block, copy each line into it's own reserved memory */
  993.  
  994.         for (i=0; i*len < j && lno < lines; i++) {
  995.             memcpy(store[lno++],p,len);
  996.             p += len;
  997.           } /* end for */
  998.       } while (lno < lines);
  999. } /* end readpic() */
  1000.  
  1001. /**********************************************************************
  1002. *  Function    :    readcpic
  1003. *  Purpose    :    bloack read a run-length compressed image from an HDF
  1004. *            file and store it in main memory, doing run length 
  1005. *            decompression as it goes
  1006. *  Parameters    :
  1007. *            len - the length of a line of the image (xdim)
  1008. *            lines - the number of lines in the image (ydim)
  1009. *  Returns    :    none
  1010. *  Calls    :    DFaccess(),DFread(),unrleit()
  1011. *  Called by    :    main()
  1012. **********************************************************************/
  1013. void readcpic(len,lines)
  1014. int len,    /* the length of a line */
  1015.     lines;    /* the number of lines */
  1016. {
  1017.     int    i;    /* temporary counting variable */
  1018.     long n,    /* the number of bytes read into the buffer */
  1019.         bufleft,    /* the number of bytes left in the buffer */
  1020.         crowsize,    /* the maximum compressed row size */
  1021.         readsize,    /* the size of the block to read in */
  1022.         totalread;    /* also the number of bytes read into the buffer */
  1023.     unsigned char *in;    /* pointer to the buffer for data */
  1024.  
  1025. /* prime DF routines */
  1026.  
  1027.     DFaccess( dff, ddstr.tag, ddstr.ref, "r");    /* set HDF to begin reading */
  1028.  
  1029.     readsize=RAWSIZE - 16;
  1030.     in=raw;
  1031.     n=DFread(dff, in,readsize);        /* block read in data from HDF file */
  1032.     totalread=n;
  1033.     crowsize=(long)xdim*121/120 +1;        /* maximum rowsize of compressed data */
  1034.     bufleft=n;
  1035.     for (i=0; i<ydim; i++) {        /* uncompress until end of file or buffer */
  1036.         n=unrleit(in, store[i], (long) xdim);
  1037.         in += n;
  1038.         bufleft -= n;
  1039.         if (bufleft<crowsize) {        /* if row is chopped then read more in */
  1040.             memcpy(raw, in, bufleft);
  1041.             in=raw;
  1042.             n=DFread(dff,&in[bufleft],readsize-bufleft);
  1043.             totalread += n;
  1044.             bufleft += n;
  1045.         } /* end if */
  1046.     } /* end for */
  1047. } /* end readcpic() */
  1048.  
  1049. /**********************************************************************
  1050. *  Function    :    readipic
  1051. *  Purpose    :    block read in HDF image and do imcomp decompression as
  1052. *            it goes
  1053. *  Parameters    :
  1054. *            len - the length of a line (xdim)
  1055. *            lines - the number of lines in image (ydim)
  1056. *  Returns    :    none
  1057. *  Calls    :    DFaccess(), DFread(), unimcomp(), memcpy()
  1058. *  Called by    :    main()
  1059. **********************************************************************/
  1060. void readipic(len,lines)
  1061. int len,    /* the length of a line of the image */
  1062.     lines;    /* the number of lines in the image */
  1063. {
  1064.     int i,    /* local temporary counting variable */
  1065.         lno,    /* the current line number */
  1066.         readsize,    /* the size to block read */
  1067.         readline,    /* the block reading factor */
  1068.         itread;        /* the number of bytes read in */
  1069.     char *p,    /* pointer to the buffer */
  1070.         *endp;    /* pointer to the end of the buffer */
  1071.  
  1072. /* prime DF routines */
  1073.  
  1074.     DFaccess( dff, ddstr.tag, ddstr.ref, "r");
  1075.  
  1076.     readline=RAWSIZE /xdim -5;
  1077.     readsize=readline*xdim;    
  1078.     lno=0;
  1079.     do {
  1080.         itread=DFread(dff,raw,(long)readsize);    /* block read in imcomp compressed image */
  1081.         p=raw;
  1082.         endp=raw + itread;
  1083.  
  1084. /* take each imcomp compressed line and uncompress it */
  1085.  
  1086.         while(p<endp && lno<(ydim/4)){
  1087.             unimcomp(xdim,1,p,bigstore);
  1088.             for (i=0;i<4 ;i++)
  1089.                 memcpy(store[lno*4+i],&bigstore[i*xdim],xdim);
  1090.             p += xdim;
  1091.             lno++;
  1092.           } /* end while */
  1093.     } while (lno<(ydim/4));
  1094. } /* end readipic() */
  1095.  
  1096. /**********************************************************************
  1097. *  Function    : read_pic
  1098. *  Purpose    : Reads in the raw image in big blocks from an HDF file
  1099. *                Performs decompression and store it in array 'store'
  1100. *  Parameters    :
  1101. *    xdim,ydim  - x and y dimension of image
  1102. *  Returns    : none
  1103. *  Calls    : DFaccess(), DFread(), unimcomp(), choose_rgb()
  1104. *  Called by    : main()
  1105. **********************************************************************/
  1106. void read_pic(xdim,ydim)
  1107. int xdim, ydim;    /* x & y dimensions of the image to be read in */
  1108. {
  1109.     int i,j,k,cnt,    /* more local counting variables */
  1110.         readline,    /* the number of lines the storage array can hold */
  1111.         readsize,    /* the size of the block to be read in */
  1112.         nlines;        /* the current line count into the image */
  1113.     char *p;        /* character pointer to the beginning of the array read in */
  1114.     unsigned char *out,        /* character pointer to the unimcomp compressed lines */
  1115.                      color; /* color value for a pixel */
  1116.     void unimcomp();    /* routine to uncompress imcomp line compression */
  1117.     int choose_rgb();    /* routine to get 6 bit color values from 24 bit palettes */
  1118.  
  1119.     for (j=0; j<64; j++)    /* clear out the frequency count array */
  1120.         count[j]=0;
  1121.     
  1122. /* read in image */
  1123.  
  1124.     DFaccess( dff, ddstr.tag, ddstr.ref, "r");
  1125.  
  1126.     readline=RAWSIZE / xdim - 5;
  1127.     readsize=readline * xdim;
  1128.     out=(unsigned char *)malloc(4*xdim);
  1129.     nlines=0;
  1130.     do {
  1131.         cnt=DFread(dff,raw,(long)readsize);    /* get a chunk of the image */
  1132.     
  1133. /* count the frequency of colors */
  1134.  
  1135.         for (i=0; i<cnt; i=i+4)
  1136.            for (j=HI; j<=LO; j++) {
  1137.                color=raw[i+j];
  1138.                k=3 * color;
  1139.                count[choose_rgb(pal[k],pal[k+1],pal[k+2])]++;    /* get the correct color setting and increment it */
  1140.               } /* end of for j */
  1141.         
  1142. /* decompress the raw image */
  1143.  
  1144.         for (j=0; j<readline; j++) {
  1145.             unimcomp(xdim,1,&(raw[xdim*j]),out);    /* uncompress each compressed line into four normal lines */
  1146.             p=out;
  1147.             for (i=0; i< 4 && nlines < ydim; i++) {    /* copy the four lines into memory */
  1148.                 memcpy(store[nlines],p,xdim);
  1149.                 nlines++;
  1150.                 p=p + xdim;
  1151.                 }    /* end of for i */
  1152.           }    /* end of for j */
  1153.     } while (nlines < ydim);
  1154. } /* end of read_pic() */
  1155.  
  1156. /**********************************************************************
  1157. *  Function    :    palloc
  1158. *  Purpose    :    allocate room in dynamic memory for a 256 byte palette
  1159. *  Parameters    :    
  1160. *            x - the number of the palette to try to allocate
  1161. *  Returns    :
  1162. *            1 if a palette was allocated
  1163. *            0 if no memory
  1164. *  Calls    :    none
  1165. *  Called by    :    loadpal(), findpal(), newpal()
  1166. **********************************************************************/
  1167. int palloc(x)
  1168. int x;    /* the number of the palette to try to allocate */
  1169. {
  1170.     if(x<16){    /* the legal number of palettes in memory */
  1171.         rpal[x]=malloc(256);
  1172.         gpal[x]=malloc(256);
  1173.         bpal[x]=malloc(256);
  1174.  
  1175.         if (rpal[x]==NULL || gpal[x]==NULL || bpal[x]==NULL)    /* if any of the palettes were not able to be allocated then return 0 */
  1176.             return(0);
  1177.         else{                /* otherwise, copy in the current palette */
  1178.             memcpy(rpal[x],rmap,256);
  1179.             memcpy(gpal[x],gmap,256);
  1180.             memcpy(bpal[x],bmap,256);
  1181.             return(1);
  1182.           }    /* end else */
  1183.       }    /* end if */
  1184.     else
  1185.         return(0);                /* if too many palettes then return 0 */
  1186. } /* end palloc() */
  1187.  
  1188. /**********************************************************************
  1189. *  Function    :    palloce
  1190. *  Purpose    :    allocate room for an ega palette and copy the current
  1191. *            one into that memory
  1192. *  Parameters    :
  1193. *            x - the number of the palette to allocate memory for
  1194. *  Returns    :
  1195. *            1 if the memory was allocated ok
  1196. *            0 if the memory was not available
  1197. *  Calls    :    none
  1198. *  Called by    :    loadega(), findpal(), newpal()
  1199. **********************************************************************/
  1200. int palloce(x)
  1201. int x;    /* the number of the palette to be allocated */
  1202. {
  1203.     if(x<16){    /* if it is a legal palette value */
  1204.         egapals[x]=malloc(16);
  1205.     
  1206.         if (egapals[x]==NULL)    /* if no memory available, then return 0 */
  1207.             return(0);
  1208.         else{                    /* otherwise, return 1 and copy the palette into the new memory */
  1209.             memcpy(egapals[x],regrs,16);
  1210.             return(1);
  1211.           }    /* end else */
  1212.       } /* end if */
  1213.     else
  1214.         return(0);                /* return 0 if too many palettes */
  1215. } /* end palloce() */
  1216.  
  1217. /**********************************************************************
  1218. *  Function    :    loadpal
  1219. *  Purpose    :    load in a palette from an HDF file and set the vga or
  1220. *            no. 9 palette to it.
  1221. *  Parameters    :    none
  1222. *  Returns    :    none
  1223. *  Calls    :    DFsetfind(), DFfind, DFgetelement(), palloc(), putmap9(),
  1224. *             putmapv()
  1225. *  Called by    :    initpal()
  1226. **********************************************************************/
  1227. void loadpal()
  1228. {
  1229.     int i;    /* local counting variable */
  1230.     char *pi;    /* pointer to the chracter array read in */
  1231.  
  1232.     DFgetelement( dff, DFTG_IP8, imref, pal);    /* get palette associated with the image */
  1233.  
  1234.     pi=pal;
  1235.     for (i=0; i<256; i++) {
  1236.         rmap[i]=*pi++;
  1237.         gmap[i]=*pi++;
  1238.         bmap[i]=*pi++;
  1239.     } /* end for */
  1240.  
  1241.     palnum=0;
  1242.     palmax=0;
  1243.     if(!palloc(palmax)) {    /* allocate room for another palette */
  1244.         palmax=-1;        /* bad allocation */
  1245.         displayerr("No Room For Another Palette");
  1246.       }    /* end if */
  1247.     else                /* ok allocation for palette */
  1248.         switch (mode) {        /* switch routine to put the correct palette */
  1249.             case VGA:
  1250.                 putmapv(&rmap[0],&gmap[0],&bmap[0]);
  1251.                 break;
  1252.  
  1253.             case NO9:
  1254.                 putmap9(&rmap[0],&gmap[0],&bmap[0]);
  1255.                 break;
  1256.           }    /* end switch */
  1257. } /* end loadpal() */
  1258.  
  1259. /**********************************************************************
  1260. *  Function    :    loadega
  1261. *  Purpose    :    load in the ega palette from an HDF file
  1262. *  Parameters    :    none
  1263. *  Returns    :    none
  1264. *  Calls    :    DFsetfind(), DFfind(), DFgetelement(), select_pal(),
  1265. *            palloce()
  1266. *  Called by    :    initpal()
  1267. **********************************************************************/
  1268. void loadega()
  1269. {
  1270.     int    i;    /* local counting variable */
  1271.  
  1272.     DFgetelement( dff, DFTG_IP8, imref, pal);    /* get palette associated with the image */
  1273.  
  1274.     palnum=0;
  1275.     palmax=0;
  1276.     select_pal(xdim,ydim,1);    /* perform a frequency count on it */
  1277.     if(!palloce(palmax)) {    /* allocate room for the first palette */
  1278.         palmax=-1;
  1279.         displayerr("No Room For Another Palette");
  1280.       }    /* end if */
  1281.     else 
  1282.         for(i=0; i<16; i++)
  1283.             egapal(i,(int)regrs[i]);
  1284. } /* end loadega() */
  1285.  
  1286. /**********************************************************************
  1287. *  Function    :    unrleit
  1288. *  Purpose    :    decompress run length encoding of a line of an image
  1289. *  Parameters    :
  1290. *            buf - the character string to be unencoded
  1291. *            bufto - the character string to be unencoded into
  1292. *            outlen - the length of the final, unencoded string
  1293. *  Returns    :    the length of the encoded line
  1294. *  Calls    :    none
  1295. *  Called by    :    readcpic()
  1296. **********************************************************************/
  1297. long unrleit(buf,bufto,outlen)
  1298. long outlen;    /* the long value of the final unencoded line length */
  1299. unsigned char *buf,*bufto;    /* the character pointers to the initial and final character buffers */
  1300. {
  1301.     register int cnt;
  1302.     register unsigned char *p,*q;
  1303.     unsigned char *endp;
  1304.     static unsigned char save[255], *savestart=NULL, *saveend=NULL;
  1305.                 /* save has a list of decompressed bytes not returned in
  1306.                     previous call.  savestart and saveend specify the position
  1307.                     at which this list starts and ends in the array save */
  1308.     
  1309.     p=buf;
  1310.     endp=bufto+outlen;
  1311.     q=bufto;
  1312.     while((saveend>savestart) && (q<endp))            /* copy saved stuff */
  1313.         *q++=*savestart++;
  1314.     if(savestart>=saveend)
  1315.         savestart=saveend=save;        /* all copied */
  1316.     while(q<endp) {
  1317.         cnt=*p++;            /* count field */
  1318.         if(!(cnt&128))    /* is set of uniques */
  1319.             while(cnt--)
  1320.                 if(q<endp)
  1321.                     *q++=*p++;    /* copy unmodified */
  1322.                 else
  1323.                     *saveend++=*p++;
  1324.         else {
  1325.             cnt&=127;            /* strip high bit */
  1326.             while(cnt--)
  1327.                 if(q<endp)
  1328.                     *q++=*p;    /* copy unmodified */
  1329.                 else
  1330.                     *saveend++=*p;
  1331.             p++;                /* skip that character */
  1332.           }    /* end else */
  1333.       }    /* end while */
  1334.     return((long)(p-buf));
  1335. }    /* end unrleit() */
  1336.  
  1337. /************************************************************************
  1338. *  Function    : unimcomp
  1339. *  Purpose    : 'Decompresses' the compressed image
  1340. *  Parameter    :
  1341. *    xdim       - x dimensions of image
  1342. *    lines      - number of lines of compressed image
  1343. *    in, out    - Input buffer and output buffer. Size of input buffer
  1344. *           is xdim*lines. Size of output buffer is 4 times
  1345. *           that. It 'restores' images into seq-type files
  1346. *  Returns      : none
  1347. *  Called by   : readipic(), read_pic()
  1348. *  Calls       : none
  1349. ************************************************************************/
  1350. void unimcomp(xdim,lines,in,out)
  1351. int xdim,    /* width of the image */
  1352.     lines;    /* number of lines of the compressed image */
  1353. unsigned char in[], /* input buffer */
  1354.             out[];    /* output buffer (4 times as large as the input buffer) */
  1355. {
  1356.   int bitmap,    /* integer value of two side-by-side pixels */
  1357.         temp;    /* temporary var. to hold working stuff */
  1358.   int i, j, k,    /* local counting variables */
  1359.         x, y;    /* counting variables for the x & y coor. of the image */
  1360.   unsigned char hi_color,    /* value of the highest color */
  1361.                 lo_color;    /* value of the lowest color */
  1362.  
  1363.   /* go over the compressed image */
  1364.   for (y=0; y<lines; y++)
  1365.     for (x=0; x<xdim; x=x+4) {
  1366.       k=y*xdim + x;
  1367.       hi_color=in[k+2]; 
  1368.       lo_color=in[k+3];
  1369.  
  1370.       bitmap=(in[k] << 8) | in[k+1];
  1371.  
  1372.       /* store in the out buffer */
  1373.       for (i=(y*4); i<(y*4+4); i++) {
  1374.         temp=bitmap >> (3 + y*4 - i)*4;
  1375.         for (j=x; j<(x+4); j++) {
  1376.            if ((temp & 8)==8)
  1377.             out[i*xdim+j]=hi_color;
  1378.           else
  1379.             out[i*xdim+j]=lo_color;
  1380.           temp=temp << 1;
  1381.         } /* end of for j */
  1382.       }    /* end of for i */
  1383.     } /* end of for x */
  1384. } /* end of unimcomp() */
  1385.  
  1386. /************************************************************************
  1387. *  Function   : select_pal
  1388. *  Purpose    : Selects a palette of 16 colors out of 256 colors.
  1389. *               Each of the 256 original colors are 24 bit-rgb values
  1390. *               The routine truncates the 24 bit values to 6 bits and
  1391. *               selects the most popular 16 colors
  1392. *  Parameters :
  1393. *    xdim,ydim - x and y dimension of image
  1394. *    mode      - '1' implies program will count the frequency of colors
  1395. *                '0' implies the frequency count has alreay been done
  1396. *  Returns    : none
  1397. *  Called By  : showega()
  1398. *  Calls      : choose_rgb(), map()
  1399. ************************************************************************/
  1400. void select_pal(xdim,ydim,mode)
  1401. int xdim,ydim,    /* x & y dimensions */
  1402.         mode;    /* whether or not to perform frequency count */
  1403. {
  1404.     int i,j,k,    /* local counting variables */
  1405.         x,y,    /* variables for x & y dimensions */
  1406.         indx;    /* index to the current maximum value in the frequency table */
  1407.     long maxfreq;    /* the current highest frequency */
  1408.     unsigned char color;    /* the color of a pixel */
  1409.  
  1410.     int choose_rgb();    /* routine to choose a corresponding color from a mixture of red, green, and blue palettes */
  1411.     int map();    /* routine to map from 16 colors into the 256 color vga table */
  1412.  
  1413.     /* count the frequency of each color used */
  1414.  
  1415.     textmode();
  1416.     printf("Performing Frequency Count on The Image Data, Please be Patient\n");
  1417.     if (mode==1) {    /* if mode!=1 then count has already been performed, so no need to do it again */
  1418.          for (i=0; i<64; i++)    /* clear out the count array */
  1419.             count[i]=0;
  1420.           for (y=0; y<ydim; y++)
  1421.             for (x=0; x<xdim; x++) {
  1422.                 color=store[y][x];
  1423.                 j=3 * color;
  1424.                 k=choose_rgb(pal[j],pal[j+1],pal[j+2]);
  1425.                 count[k]++;
  1426.              }    /* end of for x */
  1427.       }    /* end of if mode==1 */
  1428.  
  1429. /* select the 16 most frequently used colors */
  1430.  
  1431.     for (i=0; i<16; i++) {
  1432.         maxfreq=count[0];
  1433.         indx=0;
  1434.         for (j=1; j<64; j++)
  1435.             if (maxfreq < count[j]) {
  1436.                 maxfreq=count[j];
  1437.                 indx=j;
  1438.               }    /* end of for j */
  1439.         count[indx]=0;
  1440.         regrs[i]=indx;
  1441.         egapal(i,indx);
  1442.       }    /* end of for i */
  1443.  
  1444. /*  set up translation table */
  1445.  
  1446.     for (i=0; i<768; i=i+3) {
  1447.         j=choose_rgb(pal[i],pal[i+1],pal[i+2]);
  1448.         trans[i/3]=map(regrs,j);
  1449.       }    /* end of for i */
  1450.     grafmode();
  1451. } /* end of select_pal() */
  1452.  
  1453. /************************************************************************
  1454. *  Function   : map
  1455. *  Purpose    : Maps a 6 bit rgb value to one of the 16 colors chosen
  1456. *               Selects the one with the least bit difference
  1457. *  Parameters :
  1458. *    regrs     - stores the 16 colors chosen
  1459. *    color     - the color to be mapped
  1460. *  Returns    : register with nearest color
  1461. *  Called By  : select_pal()
  1462. *  Calls      : none
  1463. ************************************************************************/
  1464. int map(regrs,color)
  1465. char regrs[16];    /* array of the current ega palette values */
  1466. int color;    /* the color to map into the large 256 color palette */
  1467. {
  1468.     int i,j,    /* counting variables */
  1469.         minval,    /* the smallest value in the 256 color palette which correspond to the color chosen */
  1470.         temp,    /* temporary variable */
  1471.         diff,    /* the amount of difference between a color and the ega palette value */
  1472.         reg;    /* the register number of the ega palette which is closest to the color wanted */
  1473.  
  1474.     minval=999;
  1475.     for (i=0; i<16; i++) {    /* check through all the registers in the ega palette */
  1476.         diff=0;
  1477.         temp=1;
  1478.         for (j=0; j<6; j++) {    /* check through all the bits in the color */
  1479.             if ((temp & color) != (regrs[i] & temp))    /* if the bits don't match then increment the difference variable */
  1480.                 diff++;
  1481.             temp=temp * 2;
  1482.           }    /* end for j */
  1483.         if (minval > diff) {
  1484.             reg=i;
  1485.             minval=diff;
  1486.           }    /* end if */
  1487.       }    /* end for i */
  1488.     return reg;    /* return the corresponding register value to the color given */
  1489. } /* end of map() */
  1490.  
  1491. /************************************************************************
  1492. *  Function   : choose_rgb
  1493. *  Purpose    : Selects a 6 bit rgb value for a 24 bit number. Takes
  1494. *               the 2 most sign. bit of each 8 bit component
  1495. *  Parameters :
  1496. *    r,g,b    - rgb components
  1497. *  Returns    : 6 bit rgb value
  1498. *  Called By  : select_pal()
  1499. *  Calls      : none
  1500. ************************************************************************/
  1501. int choose_rgb(r,g,b)
  1502. int r,g,b;    /* red, green, and blue components for the 24 bit value */
  1503. {
  1504.     int color;    /* 6 bit color value */
  1505.  
  1506.     color=((r & 128) >> 5) | ((r & 64) >> 1) |
  1507.             ((g & 128) >> 6) | ((g & 64) >> 2) |
  1508.             ((b & 128) >> 7) | ((b & 64) >> 3);
  1509.  
  1510.     return color;    /* return the 6 bit color value */
  1511. } /* end choose_rgb() */
  1512.  
  1513. /**********************************************************************
  1514. *  Function    :    newpal
  1515. *  Purpose    :    read a new palette from a binary file and switch to it
  1516. *  Parameters    :
  1517. *            s - name of a file
  1518. *  Returns    :    none
  1519. *  Calls    :    palloc(), palloce(), select_pal()
  1520. *  Called by    :    main(), options()
  1521. **********************************************************************/
  1522. void newpal(s)
  1523. char *s;    /* pointer to a file name */
  1524. {
  1525.     FILE *fp;    /* file pointer for the palette */
  1526.     
  1527.     if (NULL==(fp=fopen(s,"rb")))    /* open the palette file */
  1528.         makepal();
  1529.     else {    /* read in the palettes */
  1530.         fread(rmap,1,256,fp);
  1531.         fread(gmap,1,256,fp);
  1532.         fread(bmap,1,256,fp);
  1533.         fclose(fp);
  1534.  
  1535. /* attempt to allocate room for the palette and switch to it */
  1536.  
  1537.         palmax++;
  1538.         if(mode!=EGA) {
  1539.             if(!palloc(palmax)) {    /*allocate the new palette memory */
  1540.                 palmax--;            /* bad allocation, fix old palette */
  1541.                 displayerr("No Room For Another Palette");
  1542.                 memcpy(rmap,rpal[palnum],256);
  1543.                 memcpy(gmap,gpal[palnum],256);
  1544.                 memcpy(bmap,bpal[palnum],256);
  1545.               } /* end if */
  1546.             else 
  1547.                 palnum=palmax;    /* good allocation, set palnum to new palette */
  1548.           } /* end if */
  1549.         else {
  1550.             for(i=0; i<256; i++) {    /* put individual palettes into the big one */
  1551.                 pal[i*3]=rmap[i];
  1552.                 pal[i*3+1]=gmap[i];
  1553.                 pal[i*3+2]=bmap[i];
  1554.               } /* end for */
  1555.             select_pal(xdim,ydim,1);    /* do a frequency count on it */
  1556.             if(!palloce(palmax)) {
  1557.                 displayerr("No Room For Another Palette");
  1558.                 palmax--;
  1559.                 memcpy(regrs,egapals[palnum],16);
  1560.               } /* end if */
  1561.             else 
  1562.                 palnum=palmax;
  1563.           } /* end else */
  1564.       } /* end else */    
  1565. } /* end newpal() */
  1566.  
  1567. /**********************************************************************
  1568. *  Function    :    textmode
  1569. *  Purpose    :    switch into text mode
  1570. *  Parameters    :    none
  1571. *  Returns    :    none
  1572. *  Calls    :    resetega(), vgamode(3)
  1573. *  Called by    :    main(), options()
  1574. **********************************************************************/
  1575. void textmode()
  1576. {
  1577.     switch(mode){    /* depending on the monitor type, switch to text display mode */
  1578.         case VGA:
  1579.             vgamode(3);
  1580.             break;
  1581.  
  1582.         case NO9:
  1583.             vgamode(3);
  1584.             break;
  1585.  
  1586.         case EGA:
  1587.         default:
  1588.             resetega();
  1589.             break;
  1590.       } /* end switch */
  1591. } /* end textmode() */
  1592.  
  1593. /**********************************************************************
  1594. *  Function    :    grafmode
  1595. *  Purpose    :    switch into correct graphics mode
  1596. *  Parameters    :    none
  1597. *  Returns    :    none
  1598. *  Calls    :    vgamode(), initega(), putpal()
  1599. *  Called by    :    main(), options()
  1600. **********************************************************************/
  1601. void grafmode()
  1602. {
  1603.     switch (mode){    /* depending on the monitor type, switch to graphics display mode */
  1604.         case VGA:
  1605.             vgamode(19);
  1606.             break;
  1607. /*
  1608.         case TARGA:
  1609.             GraphInit(16);
  1610.             break;
  1611. */
  1612.         case NO9:
  1613.             RG9clrscr();
  1614.             break;
  1615.  
  1616.         case EGA:
  1617.         default:
  1618.             initega();
  1619.             break;
  1620.       }    /* end switch */
  1621.     putpal();    /* replace the palette */
  1622. } /* end grafmode() */
  1623.  
  1624. /**********************************************************************
  1625. *  Function    :    putpal
  1626. *  Purpose    :    renew the palette on the graphics card with the current
  1627. *            palette in memory
  1628. *  Parameters    :    none
  1629. *  Returns    :    none
  1630. *  Calls    :    putmapv(), putmap9(), egapal(),
  1631. *  Called by    :    grafmode(), options()
  1632. **********************************************************************/
  1633. void putpal()
  1634. {
  1635.     switch (mode){        /* different calls for different monitor types */
  1636.         case VGA:
  1637.             putmapv(rmap,gmap,bmap);
  1638.             break;
  1639.  
  1640.         case NO9:
  1641.             putmap9(rmap,gmap,bmap);
  1642.             break;
  1643.  
  1644.         case EGA:
  1645.         default:
  1646.             for(i=0; i<16; i++)
  1647.                 egapal(i,(int)regrs[i]);
  1648.             break;
  1649.       } /* end switch */
  1650. } /* end putpal() */
  1651.  
  1652. /**********************************************************************
  1653. *  Function    :    showpic
  1654. *  Purpose    :    display the current portion of the image
  1655. *  Parameters    :    none
  1656. *  Returns    :    none
  1657. *  Calls    :    vgaline(), egaline(), no9line()
  1658. *  Called by    :    main(), options()
  1659. **********************************************************************/
  1660. void showpic()
  1661. {
  1662.     switch(mode){        /* output to screen differently for each different type of monitor */
  1663.         case VGA:
  1664.             for(i=0; (i+ywhere)<ydim && (i+ywhere)<200; i++)
  1665.                 LINEVGA;
  1666.             break;
  1667.  
  1668.         case NO9:
  1669.             for(i=0; (i+ywhere)<ydim && (i+ywhere)<500; i++)
  1670.                 LINENO9;
  1671.             break;
  1672. /*
  1673.          case TARGA:
  1674.              for(i=0; (i+ywhere)<ydim && (i+ywhere)<500; i++)
  1675.                  LINET16;
  1676.              break;            
  1677. */
  1678.         case EGA:
  1679.         default:
  1680.             for (i=0; (i+ywhere)<ydim && (i+ywhere)<350; i++)           /* display the image */
  1681.                 LINEEGA;
  1682.             break;
  1683.       }    /* end switch */
  1684. } /* end showpic() */
  1685.  
  1686. /**********************************************************************
  1687. *  Function    :    initpal
  1688. *  Purpose    :    to load in the first palette for the image
  1689. *  Parameters    :    none
  1690. **  Returns    :    none
  1691. *  Calls    :    loadpal(), loadega()
  1692. *  Called by    :    main()
  1693. **********************************************************************/
  1694. void initpal()
  1695. {
  1696.         switch(mode){        /* VGA and No9 use 256 byte palettes and EGA uses 16 byte palettes */
  1697.             case VGA:
  1698.             case NO9:
  1699.                 loadpal();
  1700.                 break;
  1701.  
  1702.              case TARGA:
  1703.                  loadpal();
  1704.                  break;
  1705.  
  1706.             case EGA:
  1707.             default:
  1708.                 loadega();
  1709.                 break;
  1710.           }    /* end switch(mode) */
  1711. } /* end initpal() */
  1712.  
  1713. /**********************************************************************
  1714. *  Function    :    makepal
  1715. *  Purpose    :    make a grey scale palette as the first palette
  1716. *  Parameters    :    none
  1717. *  Returns    :    none
  1718. *  Calls    :    select_pal(), palloc(), palloce(), putpal9(), putpalv(),
  1719. *                memcpy()
  1720. *  Called by    :    newpal()
  1721. **********************************************************************/
  1722. void makepal()
  1723. {
  1724.     if(mode!=EGA) {        /* for vga and no9 palettes */
  1725.         for (i=0; i<256; i++)        /* make a grey scale palette */
  1726.             rmap[i]=gmap[i]=bmap[i]=i;
  1727.  
  1728.         palnum=0;
  1729.         palmax=0;
  1730.         if(!palloc(palmax)) {    /* allocate room for another palette */
  1731.             palmax=-1;
  1732.             displayerr("No Room For Another Palette");
  1733.           }    /* end if */
  1734.         else                /* load the grey scale data into the arrays */
  1735.             switch (mode) {        /* switch routine to put the correct palette */
  1736.                 case VGA:
  1737.                     putmapv(&rmap[0],&gmap[0],&bmap[0]);
  1738.                     break;
  1739.  
  1740.                 case NO9:
  1741.                     putmap9(&rmap[0],&gmap[0],&bmap[0]);
  1742.                     break;
  1743.               }    /* end switch */
  1744.       }    /* end if */
  1745.     else {        /* for ega palettes */
  1746.         for(i=0; i<256; i++)    /* make an ega grey scale big palette */
  1747.             pal[i*3]=pal[i*3+1]=pal[i*3+2]=i;
  1748.         palnum=0;
  1749.         palmax=0;
  1750.         select_pal(xdim,ydim,1);    /* perform a frequency count on it */
  1751.         if(!palloce(palmax)) {    /* allocate room for the first palette */
  1752.             palmax=-1;
  1753.             displayerr("No Room For Another Palette");
  1754.           }    /* end if */
  1755.         else 
  1756.             for(i=0; i<16; i++)
  1757.                 egapal(i,(int)regrs[i]);
  1758.       }    /* end else */
  1759.     if(palmax==-1)            /* grey scale not loaded properly */
  1760.         displayerr("Palette Not Specified, No Room For Any Palettes");
  1761.     else                        /* successful grey scale load */        
  1762.         displayerr("Palette Not Specified, Grey Scale Palette Loaded");
  1763. }    /* end makepal() */
  1764.  
  1765. /**********************************************************************
  1766. *  Function    :    displayerr
  1767. *  Purpose    :    display a string in textmode, wait for keypress and
  1768. *                to graphics mode
  1769. *  Parameters    :
  1770. *            errstr -    the string to be displayed on the screen
  1771. *  Returns    :    none
  1772. *  Calls    :    textmode, updatescrn()
  1773. *  Called by    :    everywhere...
  1774. **********************************************************************/
  1775. void displayerr(errstr)
  1776. char *errstr;
  1777. {
  1778.     if(mouse && mode==NO9)
  1779.         makecur9(mx,my);
  1780.     textmode();
  1781.     printf("%s\n",errstr);
  1782.     printf("Hit any key to continue\n");
  1783.     waitq();
  1784.     grafmode();
  1785.     updatescrn();
  1786. }    /* end displayerr() */
  1787.  
  1788. /**********************************************************************
  1789. *  Function    :    debug
  1790. *  Purpose    :    print debugging variables
  1791. *  Parameters    :
  1792. *            s1,s2,s3,s4 -    names of variables printed
  1793. *            d1,d2,d3,d4 -    variables to print
  1794. *  Returns    :    none
  1795. *  Calls    :    none
  1796. *  Called by    :    anywhere...
  1797. **********************************************************************/
  1798. void debug(s1,d1,s2,d2,s3,d3,s4,d4)
  1799. char *s1,*s2,*s3,*s4;
  1800. int    d1,d2,d3,d4;        /* variables to display */
  1801. {
  1802.     if(mouse && mode==NO9)
  1803.         makecur9(mx,my);
  1804.     textmode();
  1805.     printf("%s=%d\n",s1,d1);
  1806.     printf("%s=%d\n",s2,d2);
  1807.     printf("%s=%d\n",s3,d3);
  1808.     printf("%s=%d\n",s4,d4);
  1809.     printf("Hit any key to continue\n");
  1810.     waitq();
  1811.     grafmode();
  1812.     updatescrn();
  1813. }    /* end debug() */
  1814.  
  1815. /**********************************************************************
  1816. *  Function    :    usage
  1817. *  Purpose    :    print the command line parameters
  1818. *  Parameters    :    none
  1819. *  Returns    :    none
  1820. *  Calls    :    none
  1821. *  Called by    :    main()
  1822. **********************************************************************/
  1823. void usage()
  1824. {
  1825.     printf("Command line options are as follows:\n");
  1826.     printf("\t-b xdim ydim\t: The dimensions of the binary file, and specifies\n");
  1827.     printf("\t\t\tbinary file type of data\n");
  1828.     printf("\t-m mapfile\t: The name of a palette for a binary file\n");
  1829.     printf("\t-v\t: Sets VGA display mode\n");
  1830.     printf("\t-e\t: Sets EGA display mode (default)\n");
  1831.     printf("\t-9\t: Sets Number 9 display mode\n");
  1832.     printf("\t-h\t: Specifies reading from an HDF file (default)\n");
  1833.     printf("\t-c\t: Center the image on the screen\n");
  1834.     printf("\t-p xwhere ywhere  : Specify an x & y position for the upper left hand\n");
  1835.     printf("\t\t\tcorner of the screen\n");
  1836.     printf("\t-a\t: Set animation mode\n");
  1837.     printf("\t-A\t: Set animation mode and specify animation of multiple\n");
  1838.     printf("\t\t\timages from only one hdf file\n");
  1839.     printf("\t-s\t: Specifies only one binary image and name is filename\n");
  1840.     printf("\tfilename\t: The file containing a list of binary images or\n");
  1841.     printf("\t\t\tan HDF file containing images\n\n");
  1842.     printf("Note:  for binary file usage, both the -b xdim ydim and the -m mapfile\n");
  1843.     printf("\toptions must be used for the correct displaying of the image.\n");
  1844.     printf("\tThe file name at the end of the command line is a mandatory command\n");
  1845.     printf("\tline parameter, the rest of the command line parameters are optional.\n");
  1846. }
  1847.  
  1848. /**********************************************************************
  1849. *  Function    :    do_hdf
  1850. *  Purpose    :    read in and display all the images in an HDF file
  1851. *  Parameters    :
  1852.         filename - the name of the HDF file to animate
  1853. *  Returns    :    none
  1854. *  Calls    :    dozens...
  1855. *  Called by    :    main()
  1856. **********************************************************************/
  1857. void do_hdf(filename)
  1858. char *filename;
  1859. {
  1860. /* Open DF file for reading */
  1861.     if(NULL==(dff=DFopen(filename,DFACC_ALL,16))) {
  1862.         printf("Cannot open HDF file\n");
  1863.         chdir(old_path);                /* restore the old path */
  1864.         exit(1);
  1865.       }    /* end if */
  1866.  
  1867. /* get the dimensions */
  1868.  
  1869.     if(first_hdf==1) {
  1870.         DFsetfind(dff,DFTG_ID8,DFREF_WILDCARD);    /* set wildcard find to image dimension searching */
  1871.  
  1872.         if(!DFfind(dff,&ddstr)) {            /* look for image dimensions */
  1873.             if(0>DFgetelement(dff,ddstr.tag,ddstr.ref,&dims)) {
  1874.                 printf("Error getting dimensions from file\n");
  1875.                 chdir(old_path);                /* restore the old path */
  1876.                 exit(1);
  1877.               }    /* end if */
  1878.           }    /* end if */
  1879.         else {
  1880.             printf("error in DFfind\n");
  1881.             chdir(old_path);                /* restore the old path */
  1882.             exit(1);
  1883.           }    /* end else */
  1884.  
  1885.         xdim=intswap(dims.xdim);
  1886.         ydim=intswap(dims.ydim);
  1887.  
  1888.         if(center) {                /* find the image's position on the screen */
  1889.             xwhere=(maxx-xdim)/2;
  1890.             ywhere=(maxy-ydim)/2;
  1891.             if(xwhere<0) 
  1892.                 xwhere=0;            /* if larger than screen, */
  1893.             if(ywhere<0)            /* don't center it */
  1894.                 ywhere=0;
  1895.           }    /* end if */
  1896.  
  1897. /* allocate memory for the image, if not enough, then error */
  1898.  
  1899.         if(enoughspace(xdim,ydim)) {
  1900.             printf(mem_error);
  1901.             chdir(old_path);                /* restore the old path */
  1902.             exit(1);
  1903.           } /* end if */
  1904.         first_hdf=0;                /* reset the first_hdf bit */
  1905.       }    /* end if */
  1906.  
  1907. /* cycle through all of the raster images in the file */
  1908.  
  1909.     c=' ';
  1910.     cycled=0;
  1911.     DFsetfind(dff,DFTG_RI8,DFREF_WILDCARD);        /* set wildcard search for raster 8 images */
  1912.     
  1913.     while(!DFfind(dff,&ddstr) && animate && !getout) {        /* look for the raster 8 images */
  1914.         imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  1915.         imref=ddstr.ref;
  1916.         readpic(xdim,ydim);        /* block read from disk */
  1917.         if(!cycled)
  1918.             initpal();            /* loads in the palette from disk */
  1919.         showpic();        /* put the picture on the screen */
  1920.         cycled=1;        /* set flag to display that an animation has occured */
  1921.         if(kbhit() || !animate) {    /* check for keypress to interupt animation */
  1922.             i=getch();    /* flush the keyboard buffer */
  1923.             if(i==0)                /* get extended code if necessary */
  1924.                 i=getch();
  1925.             if(show)        /* if palette is supposed to be display then display it again */
  1926.                 showpal();
  1927.             options();
  1928.           }    /* end if */
  1929.       } /* end while */
  1930.     
  1931. /* cycle through all of the run length compressed images in the file */
  1932.  
  1933.     c=' ';
  1934.     DFsetfind(dff,DFTG_CI8,DFREF_WILDCARD);
  1935.  
  1936.     while(!DFfind(dff,&ddstr) && animate && !getout) {
  1937.         imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  1938.         imref=ddstr.ref;
  1939.             readcpic(xdim,ydim);        /* block read run length compressed image from disk */
  1940.         if(!cycled)
  1941.             initpal();            /* loads in the palette from disk */
  1942.         showpic();            /* put image on the screen */
  1943.         cycled=1;        /* set flag to display that an animation has occured */
  1944.         if(kbhit() || !animate) {    /* check for keypress to interupt animation */
  1945.             i=getch();    /* flush the keyboard buffer */
  1946.             if(i==0)                /* get extended code if necessary */
  1947.                 i=getch();
  1948.             if(show)        /* if palette is supposed to be display then display it again */
  1949.                 showpal();
  1950.             options();
  1951.           }    /* end if */
  1952.       } /* end while */
  1953.  
  1954. /* cycle through all of the imcomp compressed images in the file */
  1955.  
  1956.     c=' ';
  1957.     DFsetfind(dff,DFTG_II8,DFREF_WILDCARD);    /* set wildcard search for imcomp compressed images */
  1958.  
  1959.     while(!DFfind(dff,&ddstr) && animate && !getout) {        /* look for imcomp compressed images */
  1960.         imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  1961.         imref=ddstr.ref;
  1962.         if(!doneinit) {        /* if the palette hasn't been initialized */
  1963.             initpal();        /* then initialize it for incomp compressed images */
  1964.             doneinit=1;
  1965.           }    /* end if */
  1966.         if(mode==EGA) {            /* initialize things a certain way for ega mode */
  1967.              read_pic(xdim,ydim);        /* block read from disk for ega mode */
  1968.             select_pal(xdim,ydim,0);     /* set up the translation table for ega */
  1969.             memcpy(egapals[0],regrs,16);
  1970.           }    /* end if */
  1971.         else
  1972.             readipic(xdim,ydim);    /* block read from disk for other modes */
  1973.         showpic();            /* put the image on the screen */
  1974.         cycled=1;        /* set flag to display that an animation has occured */
  1975.         if(kbhit() || !animate) {    /* check for keypress to interupt animation */
  1976.             i=getch();    /* flush the keyboard buffer */
  1977.             if(i==0)                /* get extended code if necessary */
  1978.                 i=getch();
  1979.             if(show)        /* if palette is supposed to be display then display it again */
  1980.                 showpal();
  1981.             options();
  1982.           }    /* end if */
  1983.       } /* end while */
  1984.     DFclose(dff);            /* close the hdf file */
  1985. }    /* end do_hdf() */
  1986.  
  1987. /**********************************************************************
  1988. *  Function    :    do_raster
  1989. *  Purpose    :    read in and display a raster image
  1990. *  Parameters    :
  1991.         filename - the name of the raster file to display
  1992. *  Returns    :    none
  1993. *  Calls    :    dozens...
  1994. *  Called by    :    main()
  1995. **********************************************************************/
  1996. void do_raster(filename)
  1997. char *filename;
  1998. {
  1999.     if(0>(file=open(filename,O_RDONLY | O_RAW))) {        /* open the image file */
  2000.         textmode();
  2001.         printf("\nError opening image file: %s",filename);
  2002.         chdir(old_path);                /* restore the old path */
  2003.         exit(1);
  2004.       }    /* end if */
  2005.     rawpic(file,xdim,ydim);            /* block read image from from disk */
  2006.     close(file);/* close the image file */
  2007.     showpic();            /* put the image on the screen */
  2008.     if(kbhit() || !animate) {    /* check for keypress to interupt animation */
  2009.         i=getch();    /* flush the keyboard buffer */
  2010.         if(i==0)                /* get extended code if necessary */
  2011.             i=getch();
  2012.         if(show)        /* if palette is supposed to be display then display it again */
  2013.             showpal();
  2014.         options();
  2015.       }    /* end if */
  2016. }    /* end do_raster() */
  2017.  
  2018. #ifdef FUTURE 
  2019. /**********************************************************************
  2020. *  Function    :    t16line
  2021. *  Purpose    :    Convert Image to Targa format and display
  2022. *  Parameters    :
  2023. *            x,y    -    x & y coor. for the position of the line
  2024. *            store -    the pointer for the image
  2025. *            xoff -     offset into the line
  2026. *            len -    length of a line of the image
  2027. *  Returns    :    none
  2028. *  Calls    :    pline()
  2029. *  Called by    :    showpic()
  2030. **********************************************************************/
  2031. void t16line(x,y,store,xoff,len)
  2032. int x,y,xoff,len;
  2033. unsigned char *store;
  2034. {
  2035.     short int i,        /* counter variable */
  2036.             v,            /* variable to keep track of the color for the targa */
  2037.             ind;        /* data from the image line */
  2038.      unsigned short int *ip;    /* pointer to the current targa image line */
  2039.  
  2040.      ip=l16;
  2041.  
  2042.     for (i=0; i<len; i++) {
  2043.         ind=*(store+i);        /* get the data from image in memory */
  2044.         v=bmap[ind]>>3;        /* convert into targa form */
  2045.         v |= (gmap[ind]>>3)<<5;
  2046.          v |= (rmap[ind]>>3)<<10;
  2047.          *ip++=v;                /* store in targa line */
  2048.       }    /* end for */
  2049.      pline(x,485-y,l16,len);        /* call targa line routine */
  2050. }
  2051. #endif
  2052.  
  2053. void NO9setup()
  2054. /* prepare variables for use in other functions */
  2055. {
  2056.     NO9ram=(char *)0xA0000;
  2057.     NO9bank=(int *)0xC0705;
  2058. }
  2059.  
  2060.  
  2061. void clrbuf(bank)
  2062. unsigned int bank;
  2063. {
  2064.     *NO9bank=bank;
  2065.     memset(NO9ram,0,0xFFFF);
  2066.     NO9ram[0xFFFF]=0;
  2067. }
  2068.  
  2069.  
  2070. void RG9gmode()
  2071. /* go into NO9 graphics mode -- not yet implemented */
  2072. {
  2073.     clrbuf(0x0000);
  2074.     clrbuf(0x00ff);
  2075.     clrbuf(0xff00);
  2076.     clrbuf(0xffff);
  2077. }
  2078.  
  2079. void RG9clrscr()
  2080. /* 
  2081.     Clear the screen.
  2082. */
  2083. {
  2084.         NO9setup();
  2085.         RG9gmode();
  2086. }
  2087.