home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / SVGALIB / SVGALIB1.TAR / svgalib / utils / restorepalette.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-18  |  4.5 KB  |  126 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <vga.h>
  5. #include "../src/libvga.h"
  6.  
  7.  
  8. /* default palette values */
  9.  
  10. static char default_red[256]   
  11.              = { 0, 0, 0, 0,42,42,42,42,21,21,21,21,63,63,63,63,
  12.          0, 5, 8,11,14,17,20,24,28,32,36,40,45,50,56,63,
  13.          0,16,31,47,63,63,63,63,63,63,63,63,63,47,31,16,
  14.          0, 0, 0, 0, 0, 0, 0, 0,31,39,47,55,63,63,63,63,
  15.         63,63,63,63,63,55,47,39,31,31,31,31,31,31,31,31,
  16.         45,49,54,58,63,63,63,63,63,63,63,63,63,58,54,49,
  17.         45,45,45,45,45,45,45,45, 0, 7,14,21,28,28,28,28,
  18.         28,28,28,28,28,21,14, 7, 0, 0, 0, 0, 0, 0, 0, 0,
  19.         14,17,21,24,28,28,28,28,28,28,28,28,28,24,21,17,
  20.         14,14,14,14,14,14,14,14,20,22,24,26,28,28,28,28,
  21.         28,28,28,28,28,26,24,22,20,20,20,20,20,20,20,20,
  22.          0, 4, 8,12,16,16,16,16,16,16,16,16,16,12, 8, 4,
  23.          0, 0, 0, 0, 0, 0, 0, 0, 8,10,12,14,16,16,16,16,
  24.         16,16,16,16,16,14,12,10, 8, 8, 8, 8, 8, 8, 8, 8,
  25.         11,12,13,15,16,16,16,16,16,16,16,16,16,15,13,12,
  26.         11,11,11,11,11,11,11,11, 0, 0, 0, 0, 0, 0, 0, 0};
  27. static char default_green[256] 
  28.          = { 0, 0,42,42, 0, 0,21,42,21,21,63,63,21,21,63,63,
  29.          0, 5, 8,11,14,17,20,24,28,32,36,40,45,50,56,63,
  30.          0, 0, 0, 0, 0, 0, 0, 0, 0,16,31,47,63,63,63,63,
  31.         63,63,63,63,63,47,31,16,31,31,31,31,31,31,31,31,
  32.         31,39,47,55,63,63,63,63,63,63,63,63,63,55,47,39,
  33.         45,45,45,45,45,45,45,45,45,49,54,58,63,63,63,63,
  34.         63,63,63,63,63,58,54,49, 0, 0, 0, 0, 0, 0, 0, 0,
  35.          0, 7,14,21,29,28,28,28,28,28,28,28,28,21,14, 7,
  36.         14,14,14,14,14,14,14,14,14,17,21,24,28,28,28,28,
  37.         28,28,28,28,28,24,21,17,20,20,20,20,20,20,20,20,
  38.         20,22,24,26,28,28,28,28,28,28,28,28,28,26,24,22,
  39.          0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8,12,16,16,16,16,
  40.         16,16,16,16,16,12, 8, 4, 8, 8, 8, 8, 8, 8, 8, 8,
  41.          8,10,12,14,16,16,16,16,16,16,16,16,16,14,12,10,
  42.         11,11,11,11,11,11,11,11,11,12,13,15,16,16,16,16,
  43.         16,16,16,16,16,15,13,12, 0, 0, 0, 0, 0, 0, 0, 0};
  44. static char default_blue[256]  
  45.              = { 0,42, 0,42, 0,42, 0,42,21,63,21,63,21,63,21,63,
  46.          0, 5, 8,11,14,17,20,24,28,32,36,40,45,50,56,63,
  47.         63,63,63,63,63,47,31,16, 0, 0, 0, 0, 0, 0, 0, 0,
  48.          0,16,31,47,63,63,63,63,63,63,63,63,63,55,47,39,
  49.         31,31,31,31,31,31,31,31,31,39,47,55,63,63,63,63,
  50.         63,63,63,63,63,58,54,49,45,45,45,45,45,45,45,45,
  51.         45,49,54,58,63,63,63,63,28,28,28,28,28,21,14, 7,
  52.          0, 0, 0, 0, 0, 0, 0, 0, 0, 7,14,21,28,28,28,28,
  53.         28,28,28,28,28,24,21,17,14,14,14,14,14,14,14,14,
  54.         14,17,21,24,28,28,28,28,28,28,28,28,28,26,24,22,
  55.         20,20,20,20,20,20,20,20,20,22,24,26,28,28,28,28,
  56.         16,16,16,16,16,12, 8, 4, 0, 0, 0, 0, 0, 0, 0, 0,
  57.          0, 4, 8,12,16,16,16,16,16,16,16,16,16,14,12,10,
  58.          8, 8, 8, 8, 8, 8, 8, 8, 8,10,12,14,16,16,16,16,
  59.         16,16,16,16,16,15,13,12,11,11,11,11,11,11,11,11,
  60.         11,12,13,15,16,16,16,16, 0, 0, 0, 0, 0, 0, 0, 0};
  61.  
  62. void delay() {
  63.     int i;
  64.     for (i = 0; i < 10; i++);
  65. }
  66.  
  67. /* Modifications to restorepalette to allow custom palette setting from a file.
  68.  * The format of the file is simply a table of color number -> color mappings:
  69.  * <color(0-255)> <red(0-63)> <green(0-63)> <blue(0-63)> <rest of line ignored>
  70.  *                       By Charles Blake, chuckb@alice.wonderland.caltech.edu
  71.  */
  72.  
  73. void process_palette_file(char** argv)  {
  74.     int i, n, r, g, b;
  75.     FILE* palette_file;
  76.     
  77.     if ((palette_file = fopen(argv[1], "r") ) == NULL) {
  78.     fprintf(stderr, "%s opening %s: %s\n", argv[0], argv[1], strerror(errno));
  79.     exit(1);
  80.     }
  81. /*
  82.  * NOTE:The tricky %*[^\n] conversion specifier allows arbitrary text following
  83.  * any line with a valid entry on it. This is a simple comment syntax device.
  84.  */
  85.     for (n=1; fscanf(palette_file,"%d %d %d %d %*[^\n]",&i,&r,&g,&b)==4; n++) {
  86.     default_red[i] = r;    /* deferred assignment prevents partial */
  87.     default_green[i] = g;  /* assignment in case of in-line errors */
  88.     default_blue[i] = b;
  89.     }
  90.     if (!feof(palette_file)) {
  91.     fprintf(stderr, "%s parse error: %s: line %d\n", argv[0], argv[1], n);
  92.     exit(1);
  93.     }
  94. }
  95.     
  96. void main(int argc, char* argv[]) {
  97.     int i;
  98.  
  99.     vga_init();
  100.  
  101.     if (argc > 1)
  102.         process_palette_file(argv);
  103.  
  104.     if (vga_getcurrentchipset() != EGA) {
  105.         /* Restore textmode/16-color mode palette */
  106.         for (i = 0; i < 16; i++) {
  107.                   port_in(IS1_RC);
  108.                   delay();
  109.                 port_out(i, ATT_IW);    /* set palette color number */
  110.                 delay();
  111.                 port_out(i, ATT_IW);    /* set palette color value */
  112.                 delay();
  113.         }
  114.  
  115.         /* Restore 256-color VGA RGB look-up table */
  116.         for (i = 0; i < 256; i++)
  117.             vga_setpalette(i, default_red[i], default_green[i],
  118.                 default_blue[i]);
  119.  
  120.         port_in(IS1_RC);
  121.               delay();
  122.             port_out(0x20, ATT_IW);        /* enable display */
  123.     }
  124.     exit(0);
  125. }
  126.