home *** CD-ROM | disk | FTP | other *** search
/ Der Mediaplex Sampler - Die 6 von Plex / 6_v_plex.zip / 6_v_plex / DISK5 / DOS_14 / GS252DVX.ZIP / GDEVPCCM.C < prev    next >
C/C++ Source or Header  |  1992-08-17  |  4KB  |  134 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevpccm.c */
  21. /* Support routines for PC color mapping */
  22. #include "gs.h"
  23. #include "gsmatrix.h"            /* for gxdevice.h */
  24. #include "gxdevice.h"
  25. #include "gdevpccm.h"            /* interface */
  26.  
  27. /* Color mapping routines for EGA/VGA-style color. */
  28. /* Colors are 4 bits: 8=intensity, 4=R, 2=G, 1=B. */
  29.  
  30. #define black 0
  31. #define blue 1
  32. #define green 2
  33. #define cyan 3
  34. #define red 4
  35. #define magenta 5
  36. #define brown 6
  37. #define white 7
  38. #define dgray 8                /* dark gray is not very usable */
  39. #define lblue 9
  40. #define lgreen 10
  41. #define lcyan 11
  42. #define lred 12
  43. #define lmagenta 13
  44. #define yellow 14
  45. #define bwhite 15
  46. gx_color_index
  47. pc_4bit_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  48.   gx_color_value b)
  49. {
  50. #define c13 (gx_max_color_value / 3)
  51. #define c23 (gx_max_color_value - c13)
  52.     static byte g0[3][3] =
  53.      {{black,blue,lblue},{red,magenta,lmagenta},{lred,lmagenta,lmagenta}};
  54.     static byte g1[3][3] =
  55.      {{green,cyan,lcyan},{brown,white,lcyan},{yellow,yellow,lmagenta}};
  56.     static byte g2[3][3] =
  57.      {{lgreen,lgreen,lcyan},{lgreen,lgreen,lcyan},{yellow,yellow,bwhite}};
  58.     return (gx_color_index)
  59.         ((g >= c23 ? g2 : g >= c13 ? g1 : g0)
  60.          [r >= c23 ? 2 : r >= c13 ? 1 : 0]
  61.          [b >= c23 ? 2 : b >= c13 ? 1 : 0]);
  62. #undef c13
  63. #undef c23
  64. }
  65. int
  66. pc_4bit_map_color_rgb(gx_device *dev, gx_color_index color,
  67.   gx_color_value prgb[3])
  68. {
  69. #define icolor (int)color
  70.     gx_color_value one =
  71.         (icolor & 8 ? gx_max_color_value : gx_max_color_value / 3);
  72.     prgb[0] = (icolor & 4 ? one : 0);
  73.     prgb[1] = (icolor & 2 ? one : 0);
  74.     prgb[2] = (icolor & 1 ? one : 0);
  75.     return 0;
  76. #undef icolor
  77. }
  78.  
  79. /* Color mapping routines for 8-bit color with a fixed palette */
  80. /* (3 bits of R, 3 bits of G, 2 bits of B). */
  81. /* We have to trade off even spacing of colors along each axis */
  82. /* against the desire to have real gray shades; */
  83. /* we compromise by using a 7x7x4 "cube" with extra gray shades */
  84. /* (1/6, 1/2, and 5/6), instead of the obvious 8x8x4. */
  85.  
  86. gx_color_index
  87. pc_8bit_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  88.   gx_color_value b)
  89. {    uint rv = r / (gx_max_color_value / 7 + 1);
  90.     uint gv = g / (gx_max_color_value / 7 + 1);
  91.     uint bv = b / (gx_max_color_value / 7 + 1);
  92.     return (gx_color_index)
  93.         (rv == gv && gv == bv ? rv + (256-7) :
  94.          (rv << 5) + (gv << 2) + (bv >> 1));
  95. }
  96. int
  97. pc_8bit_map_color_rgb(gx_device *dev, gx_color_index color,
  98.   gx_color_value prgb[3])
  99. {    static const gx_color_value ramp[8] =
  100.     {    0, gx_max_color_value / 6, gx_max_color_value / 3,
  101.         gx_max_color_value / 2, 2 * (gx_max_color_value / 3),
  102.         5 * (gx_max_color_value / 6), gx_max_color_value,
  103.         /* The 8th entry is not actually ever used, */
  104.         /* except to fill out the palette. */
  105.         gx_max_color_value
  106.     };
  107. #define icolor (uint)color
  108.     if ( icolor >= 256-7 )
  109.     {    prgb[0] = prgb[1] = prgb[2] = ramp[icolor - (256-7)];
  110.     }
  111.     else
  112.     {    prgb[0] = ramp[(icolor >> 5) & 7];
  113.         prgb[1] = ramp[(icolor >> 2) & 7];
  114.         prgb[2] = ramp[(icolor & 3) << 1];
  115.     }
  116. #undef icolor
  117.     return 0;
  118. }
  119.  
  120. /* Write a palette on a file. */
  121. int
  122. pc_write_palette(gx_device *dev, uint max_index, FILE *file)
  123. {    uint i, c;
  124.     gx_color_value rgb[3];
  125.     for ( i = 0; i < max_index; i++ )
  126.     {    (*dev->procs->map_color_rgb)(dev, (gx_color_index)i, rgb);
  127.         for ( c = 0; c < 3; c++ )
  128.         {    byte b = rgb[c] >> (gx_color_value_bits - 8);
  129.             fputc(b, file);
  130.         }
  131.     }
  132.     return 0;
  133. }
  134.