home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / msdos / viewers / pcshow / parse.c < prev    next >
C/C++ Source or Header  |  1991-07-01  |  17KB  |  644 lines

  1. /*
  2. *    parse.c
  3. *
  4. *    This set of routines takes care of the image and palette manipulations
  5. *  after an image has been read in and displayed.
  6. *
  7. *  National Center for Supercomputing Applications, University of Illinois
  8. *  153 Computing Applications Building
  9. *  605 E. Springfield Ave.
  10. *  Champaign, IL  61820     (217)244-0072
  11. *
  12. *  Quincey Koziol            August 1988
  13. *
  14. */
  15.  
  16. #include "showext.h"
  17.  
  18. /**********************************************************************
  19. *  Function    :    parse
  20. *  Purpose    :    parse the commands that options() receives and perform the actions
  21. *  Parameters    :
  22. *                c -    the character command to parse
  23. *  Returns    :    none
  24. *  Calls    :    nopal(), textmode(), grafmode(), showpic(), showpal(),
  25. *            findpal(), putpal(), printdir(), vgaon(), vgaoff(), DFsetfind(),
  26. *            DFfind(), DFdup(), DFaccess(), DFputelement(),
  27. *            squishpal(), mousefunc(), updatescrn()
  28. *  Called by    :    options()
  29. **********************************************************************/
  30. void parse(c)
  31. unsigned char c;
  32. {
  33.     switch (c){                /* check for each command possible */
  34.         case 'w':        /* switch preference between expanding and interpolating */
  35.             expandit=!expandit;
  36.             break;
  37.  
  38.         case 'Q':        /* get out of the program entirely */
  39.             getout=1;
  40.             break;
  41.  
  42.         case '/':        /* slow down the animation speed */
  43.             anispeed+=100;
  44.             break;
  45.  
  46.         case '*':        /* speed up the animation speed */
  47.             anispeed-=100;
  48.             if(anispeed<0)
  49.                 anispeed=0;
  50.             break;
  51.  
  52.         case ',':        /* expand a portion of the screen to fill the screen */
  53.             if(mouse && mode==NO9)
  54.                 makecur9(mx,my);
  55.             textmode();
  56.             printf("Type Position Of The Upper Left Hand Corner Of The Square to Magnify:\n");
  57.             printf("Input X-coordinate for upper left hand corner:");
  58.             scanf("%d",&i);
  59.             printf("\n\nInput Y-coordinate for upper left hand corner:");
  60.             scanf("%d",&j);
  61.             printf("\n\nType Position Of The Lower Right Hand Corner Of The Square to Magnify:\n");
  62.             printf("Input X-coordinate for lower right hand corner:");
  63.             scanf("%d",&k);
  64.             printf("\n\nInput Y-coordinate for lower right hand corner:");
  65.             scanf("%d",&l);
  66.             grafmode();                                    /* clear the screen */
  67.             if(expandit) {                /* if preference is to expand the image */
  68.                 if(expand(i+xoff,j+yoff,k+xoff,l+yoff)==1)            /* expand chosen area to fill screen */
  69.                     waitq();    /* wait for keypress */
  70.               }    /* end if */
  71.             else {                        /* if preference is to interpolate the image */
  72.                 if(interpolate(i+xoff,j+yoff,k+xoff,l+yoff)==1)            /* expand and smooth the chosen area to fill the screen */
  73.                     waitq();    /* wait for keypress */
  74.               }    /* end else */
  75.             grafmode();
  76.             updatescrn();
  77.             break;
  78.  
  79. #ifdef QAK
  80.         case '&':        /* bounce ball on screen, undocumented feature */
  81.             bounce();
  82.             break;
  83. #endif
  84.  
  85.         case 'p':        /* move the color bar on the screen */
  86.             if(mouse && mode==NO9)
  87.                 makecur9(mx,my);
  88.             textmode();
  89.             printf("Type Position To Move Upper Left Hand Corner Of The Color Bar To:\n");
  90.             printf("Input X-coordinate for upper left hand corner:");
  91.             scanf("%d",&i);
  92.             printf("\n\nInput Y-coordinate for upper left hand corner:");
  93.             scanf("%d",&j);
  94.             if(i<0)    /* move color bar to those coor. */
  95.                 pal_xoff=0;
  96.             else
  97.                 if(i>(maxx-256))
  98.                     pal_xoff=maxx-256;
  99.                 else
  100.                     pal_xoff=i;
  101.             if(j<0)
  102.                 pal_yoff=0;
  103.             else
  104.                 if(j>(maxy-8))
  105.                     pal_yoff=maxy-8;
  106.                 else
  107.                     pal_yoff=j;
  108.             grafmode();
  109.             updatescrn();
  110.             break;
  111.  
  112.         case 'm':        /* attempt to activate the mouse */
  113. #ifdef MOUSE
  114.             if(!mouse) {    /* if the mouse is not activated then do so */
  115.                 if(before) {    /* if the mouse has been activated before, then just show cursor */
  116.                     if(mode==NO9)
  117.                         makecur9(mx,my);
  118.                     else {
  119.                         m1=1;            /* show mouse cursor */
  120.                         mousecl(&m1,&m2,&m3,&m4);
  121.                       }    /* end else */
  122.                     mouse=1;
  123.                   }    /* end if */
  124.                 else {            /* if the mouse has never been activated */
  125.                     m1=0;            /* reset mouse */
  126.                     m2=0;
  127.                     m3=0;
  128.                     m4=0;
  129.                     mousecl(&m1,&m2,&m3,&m4);
  130.                     if(!m1)        /* mouse not there */
  131.                         displayerr("PC Mouse not installed");
  132.                     else {    /* mouse is there */
  133.                         m1=7;                /* set min/max horizontal position */
  134.                         m3=0;
  135.                         switch(mode) {        /* get different sizes for different screens */
  136.                             case NO9:
  137.                                 m4=4095;
  138.                                 break;
  139.  
  140.                             case VGA:
  141.                             case EGA:
  142.                             default:
  143.                                 m4=639;
  144.                                 break;
  145.                           }    /* end switch */
  146.                         mousecl(&m1,&m2,&m3,&m4);
  147.                         m1=8;                /* set min/max vertical position */
  148.                         m3=0;
  149.                         switch(mode) {        /* get different sizes for different screens */
  150.                             case NO9:
  151.                                 m4=4095;
  152.                                 break;
  153.  
  154.                             case VGA:
  155.                                 m4=199;
  156.                                 break;
  157.  
  158.                             case EGA:
  159.                             default:
  160.                                 m4=349;
  161.                                 break;
  162.                           }    /* end switch */
  163.                         mousecl(&m1,&m2,&m3,&m4);
  164.                         m1=4;                /* set mouse cursor position */
  165.                         switch(mode) {        /* get different sizes for different screens */
  166.                             case NO9:
  167.                                 m3=2048;
  168.                                 m4=2048;
  169.                                 break;
  170.  
  171.                             case VGA:
  172.                                 m3=320;
  173.                                 m4=100;
  174.                                 break;
  175.  
  176.                             case EGA:
  177.                             default:
  178.                                 m3=320;
  179.                                 m4=175;
  180.                                 break;
  181.                           }    /* end switch */
  182.                         mousecl(&m1,&m2,&m3,&m4);
  183.                         switch(mode) {        /* decide the inital mouse positions */
  184.                             case VGA:
  185.                                 mx=160;                /* set old mouse positions */
  186.                                 my=100;
  187.                                 break;
  188.  
  189.                             case NO9:
  190.                                 mx=my=256;
  191.                                 break;
  192.  
  193.                             case EGA:
  194.                             default:
  195.                                 mx=160;
  196.                                 my=175;
  197.                                 break;
  198.                           }    /* end switch */
  199.                         mouse=1;
  200.                         before=1;
  201.                         if(mode==NO9)            /* put the mouse cursor on the no9 screen */
  202.                             makecur9(mx,my);
  203.                         else
  204.                             makemouse();        /* routine to make the mouse cursor into a cross hair */
  205.                       }    /* end else if */
  206.                   }    /* end else */
  207.               }    /* end if */
  208.             else {
  209.                 if(mode==NO9)
  210.                     makecur9(mx,my);
  211.                 else {
  212.                     m1=2;        /* function to remove the cursor */
  213.                     mousecl(&m1,&m2,&m3,&m4);
  214.                   }    /* end if */
  215.                 mouse=0;    /* deactivate the mouse */
  216.               }    /* end else */
  217. #endif
  218.             break;
  219.     
  220.         case 'k':        /* end animation mode */
  221.             animate=0;
  222.             break;
  223.  
  224.         case 'i':        /* show information about screen */
  225.             if(mouse && mode==NO9)
  226.                 makecur9(mx,my);
  227.             textmode();
  228.             printf("Information Screen\n\n");
  229.             printf("Coor. Of Upper Left Hand Corner = %d,%d\n\n",xoff,yoff);
  230.             printf("X & Y Dimensions of the Image = %d,%d\n\n",xdim,ydim);
  231.             if(expandit)
  232.                 printf("Expand Image Preference Set\n\n");
  233.             else
  234.                 printf("Interpolate Image Preference Set\n\n");
  235.             if(animate)
  236.                 printf("Animation Mode Active\n\n");
  237.             else
  238.                 printf("Animation Mode Not Active\n\n");
  239.             if(mouse)
  240.                 printf("Mouse Activated\n\n");
  241.             else
  242.                 printf("Mouse Not Activated\n\n");
  243.             printf("Hit <enter> To Continue\n");
  244.             waitq();    /* wait for keypress */
  245.             grafmode();
  246.             updatescrn();
  247.             break;
  248.  
  249.         case 'h':        /* enter fiddle mode */
  250.             c=' ';
  251.             if (kbhit()) {
  252.                 c=getch();
  253.                 if(c==0)        /* check for extended character code */
  254.                     c=getch()|(unsigned char)128;    /* set high bit to indicate extended code */
  255.               }    /* end if */
  256.             offset=0;
  257.             slope=10;
  258.             while(c!=13) {
  259.                 switch(c) {        /* check for proper fiddle commands */
  260.                     case '?':        /* help on fiddle commands */
  261.                         if(mouse && mode==NO9)
  262.                             makecur9(mx,my);
  263.                         textmode();
  264.                         printf("Fiddle Help Screen\n\n");
  265.                         printf("<left arrow>\t - Shift Palette To The Left\n");
  266.                         printf("<right arrow>\t - Shift Palette To The Right\n");
  267.                         printf("<up arrow>\t - Compress Palette\n");
  268.                         printf("<down arrow>\t - Expand Palette\n");
  269.                         printf("<enter>\t\t - Exit Fiddle Mode\n");
  270.                         printf("?\t\t - Show Help Screen\n\n");
  271.                         printf("Hit <enter> To Resume Fiddling\n");
  272.                         waitq();    /* wait for keypress */
  273.                         grafmode();
  274.                         updatescrn();
  275.                         break;
  276.  
  277.                     case SCRLLEFT:        /* shift palette left */
  278.                         offset--;
  279.                         squishpal(slope,offset);
  280.                         putpal();
  281.                         break;
  282.  
  283.                     case SCRLUP:        /* compress palette together */
  284.                         slope++;
  285.                         squishpal(slope,offset);
  286.                         putpal();
  287.                         break;
  288.  
  289.                     case SCRLDOWN:        /* expand palette */
  290.                         slope--;
  291.                         squishpal(slope,offset);
  292.                         putpal();
  293.                         break;
  294.  
  295.                     case SCRLRIGHT:        /* shift palette right */
  296.                         offset++;
  297.                         squishpal(slope,offset);
  298.                         putpal();
  299.                         break;
  300.                   } /* end switch */
  301.                 c=getch();        /* get the next command */
  302.                 if(c==0)
  303.                     c=getch()|(unsigned char)128;    /* set high bit to indicate extended code */
  304.               } /* end while */
  305.             break;
  306.  
  307.         case 'g':        /* invert the palette bitwise */
  308.             for(j=0; j<256; j++) {
  309.                 rmap[j]=~(int)rmap[j];
  310.                 gmap[j]=~(int)gmap[j];
  311.                 bmap[j]=~(int)bmap[j];
  312.               }    /* end for j */
  313.             putpal();
  314.             break;
  315.  
  316.         case 't':        /* transpose palette */
  317.             for(j=0; j<128; j++) {
  318.                 swap(&rmap[j],&rmap[255-j]);
  319.                 swap(&gmap[j],&gmap[255-j]);
  320.                 swap(&bmap[j],&bmap[255-j]);
  321.               }    /* end for j */
  322.             putpal();
  323.             break;
  324.  
  325.         case 'd':        /* make the current pallete into the default palette */
  326.             if(mode!=EGA) {
  327.                 memcpy(rpal[palnum],rmap,256);
  328.                 memcpy(gpal[palnum],gmap,256);
  329.                 memcpy(bpal[palnum],bmap,256);
  330.               } /* end if */
  331.             else 
  332.                 memcpy(egapals[palnum],regrs,16);
  333.             break;
  334.  
  335.         case '+':        /* speed up the palettes rotation */
  336.             if(mode!=EGA)
  337.                 palspeed+=100;
  338.             else
  339.                 palspeed+=20;
  340.             break;
  341.  
  342.         case '-':        /* slow down the palettes rotation */
  343.             if(mode!=EGA)
  344.                 palspeed-=100;
  345.             else
  346.                 palspeed-=20;
  347.             if(palspeed<0)
  348.                 palspeed=0;
  349.             break;
  350.  
  351.         case 'u':        /* swap the red, green, and blue palettes */
  352.             memcpy(trans1,rmap,256);
  353.             memcpy(rmap,gmap,256);
  354.             memcpy(gmap,bmap,256);
  355.             memcpy(bmap,trans1,256);
  356.             putpal();
  357.             break;
  358.  
  359.         case 's':        /* save palette to disk */
  360.             if(filetype==HDF) { /* save the HDF palette */
  361.                 DFsetfind(dff, DFTG_IP8, DFREF_WILDCARD);    /* set the HDF file to a palette */
  362.                 while(!DFfind( dff, &ddstr)){ /* step through all the palettes */
  363.                     lasttag=ddstr.tag;
  364.                     lastref=ddstr.ref;
  365.                   } /* end while */
  366.                 newref=lastref+1;        /* make a new reference number */
  367.                 if(DFdup(dff,imtag,newref,imtag,imref))        /* try to make a new palette space */
  368.                     displayerr("Error On DFdup");
  369.                 else { /* ok DFdup call */
  370.                     for(i=0; i<256; i++) {    /* put individual palettes into the big one */
  371.                         pal[i*3]=rmap[i];
  372.                         pal[i*3+1]=gmap[i];
  373.                         pal[i*3+2]=bmap[i];
  374.                       } /* end for */
  375.                     i=DFaccess(dff,DFTG_IP8,newref,"w");    /* tell HDF to start writing */
  376.                     if(i<0) {
  377.                         printf("return code from DFaccess = %d\n",i);
  378.                         i=getchar();
  379.                       } /* end if */
  380.                     if((i=DFputelement(dff,DFTG_IP8,newref,pal,(long) 768))<=0)
  381.                         displayerr("Error On DFputelement In Save Palette Subroutine");
  382.                     else
  383.                         DFupdate(dff);
  384.                     DFaccess( dff, lasttag, newref, "r");    /* tell HDF to start reading */
  385.                   } /* end else */
  386.               }    /* end if */
  387.             else{        /* save a binary palette */
  388.                 if(mouse && mode==NO9)
  389.                     makecur9(mx,my);
  390.                 textmode();
  391.                 printf("Input Name Of Palette File (or <return> to exit) ");
  392.                 readstr(&palfile[0]);
  393.                 printf("\n");
  394.                 if(*palfile!=0) {    /* if its not a null name attempt to write out file */
  395.                     if (NULL == (pfp = fopen(palfile,"wb"))) {    /* open the palette file */
  396.                         printf("Error on palette file open\n");
  397.                         printf("Hit Any Key To Continue\n");
  398.                         waitq();
  399.                       } /* end if */
  400.                     else {    /* write out the palette */
  401.                         fwrite(rmap,1,256,pfp);
  402.                         fwrite(gmap,1,256,pfp);
  403.                         fwrite(bmap,1,256,pfp);
  404.                         fclose(pfp);
  405.                       } /* end else */
  406.                   }    /* end if */
  407.                 grafmode();
  408.                 updatescrn();
  409.               }    /* end else */
  410.             break;
  411.  
  412.         case 'l':        /* load in a new palette */
  413.             if(filetype==HDF) {    /* load in a new HDF palette */
  414.                 findpal();
  415.                 putpal();
  416.               } /* end if */
  417.             else {    /* load in a new binary palette */
  418.                 if(mouse && mode==NO9)
  419.                     makecur9(mx,my);
  420.                 textmode();
  421.                 *palfile='?';
  422.                 while(*palfile=='?') {
  423.                     printf("Input Name Of Palette File (or ? to display palettes): ");
  424.                     readstr(&palfile[0]);
  425.                     printf("\n");
  426.                     if(*palfile=='?')    /* print directory of palettes */
  427.                         printdir();
  428.                     else    /* just load in the palette */
  429.                         if(*palfile!=0)
  430.                             newpal(palfile);
  431.                   } /* end while */
  432.                 grafmode();
  433.                 updatescrn();
  434.               } /* end else */
  435.             break;
  436.  
  437.         case 'x':        /* get x,y coor. for upper left corner */
  438.             if(mouse && mode==NO9)
  439.                 makecur9(mx,my);
  440.             textmode();
  441.             printf("Input X-coordinates for upper left hand corner:");
  442.             scanf("%d",&i);
  443.             printf("\n\nInput Y-coordinates for upper left hand corner:");
  444.             scanf("%d",&j);
  445.             if((i<0) || ((xdim-maxx)<0))    /* move image to those coor. */
  446.                 xoff=0;
  447.             else
  448.                 if(i>(xdim-maxx))
  449.                     xoff=xdim-maxx;
  450.                 else
  451.                     xoff=i;
  452.             if((j<0) || ((ydim-maxy)<0))
  453.                 yoff=0;
  454.             else
  455.                 if(j>(ydim-maxy))
  456.                     yoff=ydim-maxy;
  457.                 else
  458.                     yoff=j;
  459.             grafmode();
  460.             updatescrn();
  461.             break;
  462.  
  463.         case SCRLLEFT:        /* move screen left ten pixels */
  464.             scroll('l');
  465.             break;
  466.  
  467.         case SCRLUP:        /* move screen up ten pixels */
  468.             scroll('u');
  469.             break;
  470.  
  471.         case SCRLDOWN:        /* move screen down ten pixels */
  472.             scroll('d');
  473.             break;
  474.  
  475.         case SCRLRIGHT:        /* move screen right ten pixels */
  476.             scroll('r');
  477.             break;
  478.  
  479.         case PAGEUP:        /* move screen up a full screen */
  480.             scroll('U');
  481.             break;
  482.  
  483.         case PAGEDOWN:        /* move screen down a full screen */
  484.             scroll('D');
  485.             break;
  486.  
  487.         case HOME:        /* move screen to the upper left hand corner */
  488.             scroll('H');
  489.             break;
  490.  
  491.         case END:        /* move screen to lower left hand corner */
  492.             scroll('E');
  493.             break;
  494.  
  495.         case 'c':        /* show color bar on the screen */
  496.             if(!show)
  497.                 showpal();
  498.             else
  499.                 nopal(xoff,yoff);
  500.             show=!show;
  501.             break;
  502.  
  503.         case 'n':         /* change current palette */
  504.             palnum++;
  505.             if (palnum>palmax)
  506.                 palnum=0;
  507.             if(mode!=EGA) {
  508.                 memcpy(rmap,rpal[palnum],256);
  509.                 memcpy(gmap,gpal[palnum],256);
  510.                 memcpy(bmap,bpal[palnum],256);
  511.               } /* end if */
  512.             else
  513.                 memcpy(regrs,egapals[palnum],16);
  514.             putpal();
  515.              break;
  516.  
  517. #ifdef QAK
  518.         case 'o':    /* turn vga screen off or on */
  519.             if(mode==VGA)
  520.                 if(screen) {
  521.                     vgaoff();
  522.                     screen=0;
  523.                   }    /* end if */
  524.                 else {
  525.                     vgaon();
  526.                     screen=1;
  527.                   }    /* end else if */
  528.             break;
  529. #endif
  530.  
  531.         case 'f':        /* rotate palette forwards continuously */
  532.             l='z';
  533.             while (l!=' ') {
  534.                 if(mode!=EGA) {
  535.                     i=rmap[0];
  536.                     j=gmap[0];
  537.                     k=bmap[0];
  538.                     memcpy(rmap,rmap+1,255);    /* shift palettes down one byte */
  539.                     memcpy(gmap,gmap+1,255);
  540.                     memcpy(bmap,bmap+1,255);
  541.                     rmap[255]=i;
  542.                     gmap[255]=j;
  543.                     bmap[255]=k;
  544.                     j=palspeed;
  545.                   } /* end if */
  546.                 else {
  547.                     i=regrs[0];
  548.                     for(j=0; j<=14; j++)
  549.                         regrs[j]=regrs[j+1];
  550.                     regrs[15]=i;
  551.                     j=palspeed+2000;
  552.                   } /* end else */
  553.                 putpal();
  554.                 if(kbhit()) {
  555.                     l=getch();
  556.                     if(l==0)                /* get extended code if necessary */
  557.                         l=getch()|(unsigned char)128;    /* set high bit to indicate extended code */
  558.                   }    /* end if */
  559.                 if(l=='+') {        /* increase the palettes rotation speed */
  560.                     if(mode!=EGA)
  561.                         palspeed+=100;
  562.                     else
  563.                         palspeed+=20;
  564.                     l='x';
  565.                   }    /* end if */
  566.                 if(l=='-') {        /* decrease palette rotation speed */
  567.                     if(mode!=EGA)
  568.                         palspeed-=100;
  569.                     else
  570.                         palspeed-=20;
  571.                     l='x';
  572.                   }    /* end if */
  573.                 for(; j<=2500; j++); 
  574.               } /* end while */
  575.               break;
  576.  
  577.         case 'b':        /* rotate palette backwards continuously */
  578.             l='x';
  579.             while (l!=' ') {
  580.                 if(mode!=EGA) {
  581.                     i=rmap[255];
  582.                     j=gmap[255];
  583.                     k=bmap[255];
  584.                     memcpy(rmap+1,rmap,255);        /* shift palettes up one byte */
  585.                     memcpy(gmap+1,gmap,255);
  586.                     memcpy(bmap+1,bmap,255);
  587.                     rmap[0]=i;
  588.                     gmap[0]=j;
  589.                     bmap[0]=k;
  590.                     j=palspeed;
  591.                   } /* end if */
  592.                 else {
  593.                     i=regrs[15];
  594.                     for(j=15; j>=1; j--)
  595.                         regrs[j]=regrs[j-1];
  596.                     regrs[0]=i;
  597.                     j=palspeed+2000;
  598.                   } /* end else */
  599.                 putpal();
  600.                 if(kbhit()) {
  601.                     l=getch();
  602.                     if(l==0)                /* get extended code if necessary */
  603.                         l=getch()|(unsigned char)128;    /* set high bit to indicate extended code */
  604.                   }    /* end if */
  605.                 if(l=='+') {        /* increase the palettes rotation speed */
  606.                     if(mode!=EGA)
  607.                         palspeed+=100;
  608.                     else
  609.                         palspeed+=20;
  610.                     l='x';
  611.                   }    /* end if */
  612.                 if(l=='-') {        /* decrease palette rotation speed */
  613.                     if(mode!=EGA)
  614.                         palspeed-=100;
  615.                     else
  616.                         palspeed-=20;
  617.                     l='x';
  618.                   } /* end if */
  619.                 for(; j<=2500; j++);
  620.               } /* end while */
  621.               break;
  622.  
  623.         case 'e':        /* rotate palette forwards once */
  624.             rotate('r');
  625.               break;
  626.  
  627.         case 'v':        /* rotate palette backwards once */
  628.             rotate('l');
  629.             break;
  630.  
  631.         case 'r':    /* reset the palette to its initial values */
  632.             if(mode!=EGA) {
  633.                 memcpy(rmap,rpal[palnum],256);
  634.                 memcpy(gmap,gpal[palnum],256);
  635.                 memcpy(bmap,bpal[palnum],256);
  636.               } /* end if */
  637.             else 
  638.                 memcpy(regrs,egapals[palnum],16);
  639.             palspeed=0;    /* set the palette rotation speed back to zero also */
  640.             putpal();
  641.             break;
  642.       } /* end switch */
  643. }    /* end parse() */
  644.