home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 336_01 / edipal.c < prev    next >
C/C++ Source or Header  |  1990-10-29  |  15KB  |  629 lines

  1. /*
  2. *                          E D I P A L . C
  3. *
  4. *                 written by marwan el-augi  14-oct-1990 v1.0
  5. *
  6. *
  7. *                 EGA/VGA palette editor for specific purpose
  8. *
  9. *     Copyright (c) 1990 by Marwan EL AUGI, MEASoftlines,France.
  10. *     Copyright (c) 1987,1988  by Borland International
  11. *         for some parts of the graphics routines.
  12. *
  13. *   No part (partial or total) may be modified without mentioning
  14. *   the above copyrights provided the fact that you don't either
  15. *   change the original name of the software (edipal.*).
  16. *
  17. *   Despite these restrictions you can copy and distribute the software
  18. *   knowing that it is available in shareware form which is a user
  19. *   supported way of selling , you benefit from the try-before-buy
  20. *   concept. So if you find that it is useful to you or that the source
  21. *   is valuable to you, please send a contribution of 5 US$ to the address
  22. *   below.
  23. *
  24. *   To help improve it send any comment to :
  25. *
  26. *
  27. *          Mr. Marwan EL-AUGI
  28. *          Res. PROBY  BAT.A5  App.527
  29. *          115, imp. Villehardouin
  30. *          F-34090  MONTPELLIER
  31. *               FRANCE
  32. *
  33. *
  34. */
  35.  
  36. /*
  37. HEADER:        ;
  38. TITLE:          EDIPAL
  39. VERSION:        1.0;
  40.  
  41. DESCRIPTION:    "Lets you view, edit and save an EGA color palette."
  42.  
  43.                  The whole program is in graphics mode and will
  44.                  run with EGA and mostly with VGA. You can edit
  45.                  modify any of the 16 colors of the palette
  46.                  (any to all at once) choosing the new color from
  47.                  the 64 colors set of any EGA compatible card.
  48.  
  49.  
  50.  
  51. KEYWORDS:       EGA_colors,editor, palette;
  52. SYSTEM:         DOS 3.21 and higher
  53. FILENAME:       EDIPAL.C & OUT.C (provided with companion compiled
  54.                                   .exe files)
  55. WARNINGS:       needs a TurboC to compile (copyrighted to Borland
  56.                                             International)
  57.                 Some routines are copyrighted to Borland International
  58.                 these routines are mentioned in the source file
  59.                 and must not be copied without clear mention
  60.                 in source and executable of the following statement :
  61.                 "Copyright (c) 1987,1988 Borland International"
  62.  
  63.  
  64.  
  65. SEE-ALSO:       README.DOC, OUT.C, ega.pal,
  66. AUTHOR :        Marwan EL-AUGI
  67. COMPILERS:      TurboC 2.0 (should compile with v1.5);
  68.  
  69. */
  70.  
  71. /* include files */
  72.  
  73. #include <graphics.h>
  74. #include <stdlib.h>
  75. #include <stdio.h>
  76. #include <conio.h>
  77.  
  78. /* some #defines'  */
  79.  
  80. #define ESC     0x1b            /*  define ESC key  */
  81. #define TRUE     1            /*  handy constants */
  82. #define FALSE    0
  83. #define END      0
  84. #define CONT     1
  85. #define SAVE     3
  86. #define ABNORMAL 1
  87. #define ERROR    2
  88. #define NORMAL   0
  89.  
  90. /*  message in a msg...   form used in edipal.c */
  91.  
  92. char *msgcop1  = "Copyright (c) 1990 by Marwan El-Augi, MEASoftlines, France";
  93. char *msgcop2  = "Copyright (c) 1987,1988 by Borland International";
  94. char *msggiv   = "Give the color number   :";
  95. char *msgerr1  = "Abnormal program termination";
  96. char *msgbye   = "Good Bye from EDIPAL by MEASoftlines...";
  97.  
  98. char *msgstd   = "ESC aborts or press a key to continue...";
  99. char *msgent   = "Esc aborts or press a key to begin process...";
  100. char *msgentry = "Enter the colors now or press ESC to change palette";
  101. char *msginp1  = "Invalid entry, only 0 to 6 are allowed : try again...";
  102. char *msginp2  = "Invalid entry, only 0 to 9 are allowed : try again...";
  103. char *msgcont  = "ESC saves your palette or press a key to continue editing";
  104. char *msgsav   = "Press Y/y for saving new palette or any key aborts";
  105. char *msgsure  = "Are you sure  ? ESC aborts or any key for yes";
  106. char *msgext   = "Press ESC for exiting";
  107.  
  108. char *msginit  = "The initial palette configuration";
  109. char *msgnew   = "The modified palette configuration";
  110. char *msgend1  = "The Quit/Save ending screen showing your palette";
  111. char *msged    = "Now you can change the palette settings";
  112. char *msgset   = "Now the new palette is set as you see it on the screen";
  113. char *msgbeg[] = {"Graphics device       :",
  114.           "Graphics mode         :",
  115.           "Screen resolution     :",
  116.           "Available colors      :",
  117.           "Actual color          :",
  118.           "Current fill color    :"};
  119.  
  120.  
  121. /*  some useful global variables */
  122.  
  123. int    graphdriver;            /*  graph device driver ega, vga... */
  124. int     graphmode;                 /*  graphics mode value             */
  125. int    MaxX, MaxY;            /*  maximal screen resolution       */
  126. int    Maxcolors;            /*  maximal number of colors avail. */
  127. int    errorcode;            /*  graphics errors code variables  */
  128. struct  palettetype  oldpal,newpal; /*  palette variables               */
  129.  
  130.  
  131. /*
  132. *     function prototypes
  133. */
  134.  
  135. void init(void);            /* graphics initialization function    */
  136. void graphstat(void);            /* prints report of the actual config. */
  137. void editpal(void);                 /* EGA palette editing                 */
  138. void colorprint(char *msg);         /* print the palette                   */
  139. void mainscreen(char *header);      /* formats a screen and writes on it   */
  140. void drawborder(void);              /* draws a border                      */
  141. void pause(char *header, int flag); /* pauses and waits for a key          */
  142. void bye(void);                     /* exits the program giving a status   */
  143. void end(int status);               /* the real exit routine               */
  144. void save(void);                    /* save the palette config             */
  145. void statlin(char *msg);            /* prints a status line                */
  146. void getcolors(void);               /* gets the new numbers of the palette */
  147. char input1(void);                  /* gets the  first color digit         */
  148. char input2(void);                  /* gets the second color digit         */
  149.  
  150.  
  151.  
  152. /*
  153. *
  154. *    main() : Main function
  155. *
  156. */
  157.  
  158. void main(void)
  159. {
  160.  
  161.     init();              /* init graphics mode */
  162.     graphstat();          /* report mode and configuration settings */
  163.     colorprint(msginit);
  164.     pause(msgstd,END);
  165.  
  166.     editpal();            /* actual main function of modifying palet.*/
  167.  
  168.     end(NORMAL);
  169.  
  170. }
  171.  
  172. /*
  173. *
  174. *    functions library
  175. *
  176. */
  177.  
  178.  
  179. /*   init() : initialization function  */
  180.  
  181. void init(void)
  182. {
  183.  
  184.    graphdriver = DETECT;      /* autodetection of driver requested ega...*/
  185.  
  186.    initgraph( &graphdriver, &graphmode, "");    /* init graph mode      */
  187.    errorcode = graphresult();                   /* exit if an error     */
  188.    if( errorcode != grOk )                      /* occured at init time */
  189.      {
  190.     printf(" Graphics System error : %s\n",grapherrormsg( errorcode));
  191.     end(ERROR);
  192.      }
  193.  
  194.    getpalette( &oldpal );           /* save actual palette      */
  195.    getpalette( &newpal );               /* initialize both palettes */
  196.                  /* newpal = oldpal is another solution */
  197.  
  198.    Maxcolors = getmaxcolor() + 1;       /* maximal number of colors */
  199.  
  200.    MaxX = getmaxx();                    /* get size of screen*/
  201.    MaxY = getmaxy();
  202.  
  203.  
  204. }
  205.  
  206.  
  207.  
  208. /*   graphstat() : actual configuration function */
  209.  
  210. void graphstat(void)
  211. {
  212.  
  213.    struct fillsettingstype  fillinfo;
  214.  
  215.    char *driver, *mode;
  216.    char xval[3], yval[3], Max[3], filcol[3], curcol[3];
  217.    int height , width;
  218.    int i,j;
  219.  
  220.    getfillsettings( &fillinfo );
  221.  
  222.    mainscreen( "Actual Graphics System configuration" );
  223.    settextjustify(LEFT_TEXT, TOP_TEXT);
  224.  
  225.    driver = getdrivername();
  226.    mode   = getmodename(graphmode);       /* get current settings */
  227.  
  228.    height = textheight("H");
  229.    width  = textwidth( "W");
  230.  
  231.    MaxX = getmaxx(); itoa(MaxX, xval, 10);
  232.    MaxY = getmaxy(); itoa(MaxY, yval, 10);
  233.  
  234.    itoa(Maxcolors, Max, 10);
  235.    itoa(getcolor(), curcol, 10);
  236.    itoa(fillinfo.color, filcol, 10);
  237.  
  238.  
  239. /* prints the configuration  */
  240.  
  241.       for (i=2,j=0; i <= 12; i+=2,j++)
  242.       {
  243.         outtextxy(25, i * height, msgbeg[j]);
  244.        }
  245.       outtextxy(27 * width, 2 * height, driver );
  246.       outtextxy(27 * width, 4 * height, mode);
  247.       outtextxy(27 * width, 6 * height, "( 0 , 0 ,     ,     )");
  248.      outtextxy(37 * width, 6 * height,  xval );
  249.      outtextxy(43 * width, 6 * height,  yval );
  250.       outtextxy(27 * width,  8 * height, Max );
  251.       outtextxy(27 * width, 10 * height, curcol);
  252.       outtextxy(27 * width, 12 * height, filcol);
  253.  
  254.       outtextxy(25, 38 * height, msgcop1);
  255.       outtextxy(25, 39 * height, msgcop2);
  256.  
  257.    pause(msgstd, END);
  258.  
  259. }
  260.  
  261.  
  262.  
  263.  
  264.  
  265. /*   colorprint() : prints the window with the colors based on palette */
  266.  
  267. void colorprint(char *msg)
  268. {
  269.     struct viewporttype  vp;
  270.     struct palettetype   curpal;
  271.  
  272.     int color, height, width;
  273.     int x, y, i, j;
  274.  
  275.     char colnum[3];
  276.     signed char actcol;
  277.  
  278.     mainscreen(msg);
  279.  
  280.     color = 1;
  281.     getviewsettings( &vp );        /* Get the current window size    */
  282.     width  = 2 * ( (vp.right+1) / 16 ); /* Get box dimensions       */
  283.     height = 2 * ( (vp.bottom-10) / 10 );
  284.  
  285.     x = width / 2;
  286.     y = height / 2;    /* Leave 1/2 box border     */
  287.  
  288.     for( j=0 ; j<3 ; ++j )        /* Row loop    */
  289.        {
  290.      for( i=0 ; i<5 ; ++i )        /* Column loop    */
  291.         {
  292.           setfillstyle(SOLID_FILL, color);    /* Set to solid fill in color*/
  293.           setcolor( color );        /* Set the same border color*/
  294.  
  295.           bar( x, y, x+width, y+height );        /* Draw the rectangle*/
  296.           rectangle( x, y, x+width, y+height );  /* outline the rectangle*/
  297.  
  298.           if( color == BLACK )        /* If box was black...    */
  299.         {
  300.          setcolor( WHITE );               /* Set drawing color to white*/
  301.          rectangle( x, y, x+width, y+height ); /* Outline black in white*/
  302.         }
  303.  
  304.  
  305.  
  306.           getpalette(&curpal);
  307.           actcol = curpal.colors[color];
  308.  
  309.           sprintf(colnum, "%d", actcol);
  310.           outtextxy( x+(width/2), y+height+4, colnum );
  311.  
  312.           color = ++color % Maxcolors;  /* Advance to the next color*/
  313.           x += (width / 2) * 3;        /* move the column base     */
  314.  
  315.         }                /* End of Column loop        */
  316.  
  317.      y += (height / 2) * 3;        /* move the row base        */
  318.      x = width / 2;            /* reset column base        */
  319.  
  320.        }                /* End of Row loop        */
  321.  
  322. }
  323.  
  324.  
  325.  
  326. /*   mainscreen() : prints the main screen */
  327.  
  328. void mainscreen(char *header)
  329. {
  330.    int height;
  331.  
  332.    cleardevice();               /* clear graphics screen */
  333.    setcolor(Maxcolors - 1);             /* set color to white    */
  334.    setviewport( 0, 0, MaxX, MaxY, 1);   /* viewport = fullscreen */
  335.  
  336.    height = textheight("H");             /* basic text height     */
  337.  
  338.    settextstyle( DEFAULT_FONT, HORIZ_DIR, 1);
  339.    settextjustify( CENTER_TEXT, TOP_TEXT);;
  340.    outtextxy( MaxX/2, 2, header);
  341.    setviewport( 0, height+4, MaxX, MaxY-(height+4), 1);
  342.    drawborder();
  343.    setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1);
  344.  
  345. }
  346.  
  347.  
  348. /*  drawborder() : prints the viewport */
  349.  
  350. void drawborder(void)
  351. {
  352.    struct  viewporttype  vp ;
  353.  
  354.    setcolor( Maxcolors - 1 );          /* set color to white  */
  355.  
  356.    setlinestyle( SOLID_LINE, 0, NORM_WIDTH );
  357.  
  358.    getviewsettings( &vp );
  359.    rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top );
  360.  
  361. }
  362.  
  363.  
  364. /*  pause() : pauses and waits for a key */
  365.  
  366. void pause(char *header, int flag)
  367. {
  368.  
  369.    int c;
  370.  
  371.    statlin(header);
  372.  
  373.    c = getch();                 /* reads kbd input */
  374.  
  375.    if( ESC == c)
  376.      {
  377.        switch(flag)
  378.      {
  379.        case CONT : return;
  380.        case END  : end(NORMAL);
  381.        case SAVE : end(SAVE);
  382.  
  383.        default   : return;
  384.      }
  385.      }
  386.  
  387.    if( 0 == c)
  388.       {
  389.  
  390.     c = getch();
  391.  
  392.       }
  393.  
  394.    cleardevice();        /* clear the screen */
  395.  
  396. }
  397.  
  398.  
  399.  
  400.  
  401. /*  statlin() : display a status line at the bottom of the screen */
  402.  
  403. void statlin( char *msg)
  404. {
  405.    int height;
  406.  
  407.    setviewport( 0, 0, MaxX, MaxY, 1 );
  408.    setcolor(Maxcolors - 1);
  409.    settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  410.    settextjustify( CENTER_TEXT, TOP_TEXT );
  411.    setlinestyle( SOLID_LINE, 0, NORM_WIDTH);
  412.    setfillstyle( EMPTY_FILL, 0 );
  413.  
  414.    height = textheight( "H" );
  415.  
  416.    bar( 0, MaxY-(height + 4), MaxX, MaxY );
  417.    rectangle( 0, MaxY-(height+4), MaxX, MaxY );
  418.    outtextxy( MaxX/2, MaxY-(height+2), msg);
  419.    setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1);
  420.  
  421. }
  422.  
  423. /*   editpal() : palette edition function  */
  424.  
  425. void editpal(void)
  426. {
  427.    int c;
  428.  
  429.  
  430.    mainscreen(msged);
  431.    statlin(msgent);
  432.  
  433.    c = getch();
  434.    while ( ESC != c)
  435.      {
  436.     getcolors();
  437.     colorprint(msgnew);
  438.     statlin(msgcont);
  439.     c = getch();
  440.     switch(c)
  441.       {
  442.         case ESC : save();
  443.         default  : break;
  444.       }
  445.  
  446.  
  447.     mainscreen(msged);
  448.     statlin(msgent);
  449.     c = getch();
  450.  
  451.      }
  452.  
  453.    end(NORMAL);
  454.  
  455. }
  456.  
  457.  
  458.  
  459. /* getcolors(); function to get the colors numbers */
  460.  
  461. void getcolors(void)
  462. {
  463.    int   i, y, val;
  464.    int height, width;
  465.  
  466.    char c, d;
  467.  
  468.  
  469.    char *value;
  470.    char string1[1], string2[1];
  471.  
  472.    value = (char *) malloc(1);
  473.  
  474.    statlin(msgentry);
  475.  
  476.    height = textheight( "H");
  477.    width  = textwidth(  "W");
  478.  
  479.    for( i = 1; i <= Maxcolors - 2; i++)
  480.       {
  481.     itoa( i, value, 10);
  482.     outtextxy( 15 * width, 2 * i * height, msggiv );
  483.     outtextxy( 25 * width, y = 2 * i * height, value);
  484.  
  485.     c = input1();
  486.     if(c != ESC)
  487.       {
  488.         sprintf(string1, "%c", c);
  489.         outtextxy(29 * width, y, string1);
  490.       }
  491.       else
  492.        {
  493.           break;
  494.        }
  495.  
  496.     d = input2();
  497.     if( d != ESC)
  498.       {
  499.         sprintf(string2, "%c",d);
  500.         outtextxy(30 * width, y, string2);
  501.        }
  502.       else
  503.        {
  504.  
  505.          break;
  506.        }
  507.  
  508.  
  509.     val = atoi(string1) * 10 + atoi(string2);
  510.     setpalette(i, val);
  511.  
  512.       }
  513.  
  514. }
  515.  
  516. char input1(void)
  517. {
  518.   char car;
  519.  
  520.   car = getch();
  521.   if( ESC == car)
  522.     {
  523.        return(car);
  524.     }
  525.     else
  526.      {
  527.       while(car < '0' || car > '6')
  528.         {
  529.            statlin(msginp1);
  530.            car = getch();
  531.  
  532.          }
  533.       statlin(msgentry);
  534.  
  535.      }
  536.  
  537.    return(car);
  538. }
  539.  
  540. char input2(void)
  541. {
  542.   char car;
  543.  
  544.   car = getch();
  545.   if( ESC == car)
  546.     {
  547.        return(car);
  548.     }
  549.     else
  550.      {
  551.       while(car < '0' || car > '9')
  552.         {
  553.            statlin(msginp2);
  554.            car = getch();
  555.  
  556.          }
  557.  
  558.       statlin(msgentry);
  559.  
  560.      }
  561.    return(car);
  562. }
  563.  
  564. /*  bye() : exits the program */
  565.  
  566. void bye(void)
  567. {
  568.  
  569.      getpalette(&newpal);
  570.      colorprint(msgset);
  571.      pause(msgext,SAVE);
  572.  
  573.  
  574.  
  575. }
  576.  
  577. /*   end(); the real ending routine */
  578.  
  579. void end(int status)
  580. {
  581.  
  582.  
  583.  
  584.      switch(status)
  585.     {
  586.       case ERROR    : closegraph();                       break;
  587.       case ABNORMAL : closegraph();clrscr();printf("\n%s\n",msgerr1);  break;
  588.       case NORMAL   : closegraph();clrscr();              break;
  589.       case SAVE     : clrscr();puts(msgbye);exit(status);
  590.       default       : closegraph();clrscr();              break;
  591.  
  592.     }
  593.  
  594.  
  595.                 /* Some housekeeping    */
  596.       puts(msgbye);             /* before               */
  597.       exit(status);             /* quitting             */
  598. }
  599.  
  600.  
  601. /*   save();   the save routine    */
  602.  
  603. void save(void)
  604. {
  605.  
  606.     char c;
  607.  
  608.     colorprint(msgend1);
  609.     statlin(msgsav);
  610.  
  611.     c = getch();
  612.  
  613.     switch(c)
  614.      {
  615.        case 'y'  :
  616.        case 'Y'  : colorprint(msgend1);statlin(msgsure);
  617.            c = getch();
  618.            if( ESC == c)
  619.             {
  620.               return;
  621.             }
  622.             else bye();break;
  623.  
  624.        case ESC  :
  625.        default   : return;
  626.      }
  627.  
  628. }
  629.