386BSD 0.1 development
[unix-history] / usr / othersrc / public / ghostscript-2.4.1 / gdevpccm.c
CommitLineData
c1aa0403
WJ
1/* Copyright (C) 1992 Aladdin Enterprises. All rights reserved.
2 Distributed by Free Software Foundation, Inc.
3
4This file is part of Ghostscript.
5
6Ghostscript is distributed in the hope that it will be useful, but
7WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
8to anyone for the consequences of using it or for whether it serves any
9particular purpose or works at all, unless he says so in writing. Refer
10to the Ghostscript General Public License for full details.
11
12Everyone is granted permission to copy, modify and redistribute
13Ghostscript, but only under the conditions described in the Ghostscript
14General Public License. A copy of this license is supposed to have been
15given to you along with Ghostscript so you can know your rights and
16responsibilities. It should be in a file named COPYING. Among other
17things, the copyright notice and this notice must be preserved on all
18copies. */
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
46gx_color_index
47pc_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}
65int
66pc_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
82gx_color_index
83pc_8bit_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
84 gx_color_value b)
85{ return (gx_color_index)
86 (((r >> (gx_color_value_bits - 3)) << 5) +
87 ((g >> (gx_color_value_bits - 3)) << 2) +
88 ((b >> (gx_color_value_bits - 2))));
89}
90int
91pc_8bit_map_color_rgb(gx_device *dev, gx_color_index color,
92 gx_color_value prgb[3])
93{
94#define icolor (uint)color
95 prgb[0] = ((icolor >> 5) & 7) * (gx_max_color_value / 7);
96 prgb[1] = ((icolor >> 2) & 7) * (gx_max_color_value / 7);
97 prgb[2] = (icolor & 3) * (gx_max_color_value / 3);
98#undef icolor
99 return 0;
100}
101
102/* Write a palette on a file. */
103int
104pc_write_palette(gx_device *dev, uint max_index, FILE *file)
105{ uint i, c;
106 gx_color_value rgb[3];
107 for ( i = 0; i < max_index; i++ )
108 { (*dev->procs->map_color_rgb)(dev, (gx_color_index)i, rgb);
109 for ( c = 0; c < 3; c++ )
110 { byte b = rgb[c] >> (gx_color_value_bits - 8);
111 fputc(b, file);
112 }
113 }
114 return 0;
115}