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