summaryrefslogtreecommitdiff
path: root/gs/base/gdevl256.c
blob: b16bf76f869b00ba2c4457b12213c2f5c0a5ca7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* Copyright (C) 2001-2006 Artifex Software, Inc.
   All Rights Reserved.
  
   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/
/* $Id$ */
/* Ghostscript driver for 256-color VGA modes with Linux and vgalib */
/* This Driver was derived from the BGI-Driver. It was written
   only for my own purpose. I never planned to release it or send
   it to others. So, if something doesn't work, you may send
   me a short note, but don't expect me to correct it. I will
   try my very best, but i have some work to do.

   Ludger Kunz           |    ____________|Tel.: 02371/566-230
   FernUniversitaet Hagen|   /| /   / \   |FAX:  02371/52212           
   Lehrgebiet ES         |  / |/   /_  \  |EMAIL:                      
   Frauenstuhlweg 31     | /  |\  /     \ |ludger.kunz@fernuni-hagen.de
   58644 Iserlohn        |/___|_\/_______\|
 */
#include "memory_.h"
#include "gx.h"
#include "gxdevice.h"
#include "gserrors.h"

#include <errno.h>
#include <vga.h>
#include <vgagl.h>

/* The color map for dynamically assignable colors. */
#define first_dc_index 64
static int next_dc_index;

#define dc_hash_size 293	/* prime, >num_dc */
typedef struct {
    ushort rgb, index;
} dc_entry;
static dc_entry dynamic_colors[dc_hash_size + 1];

#define XDPI   60		/* to get a more-or-less square aspect ratio */
#define YDPI   60

#ifndef A4			/*Letter size */
#define YSIZE (20.0 * YDPI / 2.5)
#define XSIZE (8.5 / 11)*YSIZE	/* 8.5 x 11 inch page, by default */
#else /* A4 paper */
#define XSIZE 8.3		/*8.27 */
#define YSIZE 11.7		/*11.69 */
#endif


/* The device descriptor */
typedef struct gx_device_lvga256 {
    gx_device_common;
} gx_device_lvga256;

#define lvga256dev ((gx_device_lvga256 *)dev)

static dev_proc_open_device(lvga256_open);
static dev_proc_close_device(lvga256_close);
static dev_proc_map_rgb_color(lvga256_map_rgb_color);
static dev_proc_map_color_rgb(lvga256_map_color_rgb);
static dev_proc_fill_rectangle(lvga256_fill_rectangle);
static dev_proc_tile_rectangle(lvga256_tile_rectangle);
static dev_proc_copy_mono(lvga256_copy_mono);
static dev_proc_copy_color(lvga256_copy_color);
static dev_proc_draw_line(lvga256_draw_line);

static gx_device_procs lvga256_procs =
{
    lvga256_open,
    NULL,			/* get_initial_matrix */
    NULL,			/* sync_output */
    NULL,			/* output_page */
    lvga256_close,
    lvga256_map_rgb_color,
    lvga256_map_color_rgb,
    lvga256_fill_rectangle,
    lvga256_tile_rectangle,
    lvga256_copy_mono,
    lvga256_copy_color,
    lvga256_draw_line
};

gx_device_lvga256 far_data gs_lvga256_device =
{std_device_color_body(gx_device_lvga256, &lvga256_procs, "lvga256",
		       0, 0,	/* width and height are set in lvga256_open */
		       1, 1,	/* density is set in lvga256_open */
		       /*dci_color( */ 8, 31, 4 /*) */ )
};

/* Open the LINUX driver for graphics mode */
int
lvga256_open(gx_device * dev)
{
    int vgamode;
    int width, height;

    vga_init();
    vgamode = vga_getdefaultmode();
    if (vgamode == -1)
	vgamode = G320x200x256;
    vga_setmode(vgamode);
    gl_setcontextvga(vgamode);
    width = vga_getxdim();
    height = vga_getydim();
    dev->y_pixels_per_inch = height / 12.0;
    dev->x_pixels_per_inch = dev->y_pixels_per_inch;
    gx_device_set_width_height(dev, width, height);
    {
	int c;

	for (c = 0; c < 64; c++) {
	    static const byte c2[10] =
	    {0, 42, 0, 0, 0, 0, 0, 0, 21, 63};

	    gl_setpalettecolor(c, c2[(c >> 2) & 9], c2[(c >> 1) & 9], c2[c & 9]);
	}
    }
    /* Initialize the dynamic color table. */
    memset(dynamic_colors, 0, (dc_hash_size + 1) * sizeof(dc_entry));
    next_dc_index = first_dc_index;

    return 0;
}

/* Close the LINUX driver */
int
lvga256_close(gx_device * dev)
{
    vga_setmode(TEXT);
    return 0;
}

/* Map a r-g-b color to a palette index. */
/* The first 64 entries of the color map are set */
/* for compatibility with the older display modes: */
/* these are indexed as 0.0.R0.G0.B0.R1.G1.B1. */
gx_color_index
lvga256_map_rgb_color(gx_device * dev, const gx_color_value cv[])
{
    gx_color_value r, g, b;
    r = cv[0]; g = cv[1]; b = cv[2];
#define cv_bits(v,n) (v >> (gx_color_value_bits - n))
    ushort r5 = cv_bits(r, 5), g5 = cv_bits(g, 5), b5 = cv_bits(b, 5);
    static const byte cube_bits[32] =
    {0, 128, 128, 128, 128, 128, 128, 128, 128, 128,
     8, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
     1, 128, 128, 128, 128, 128, 128, 128, 128, 128,
     9
    };
    uint cx = ((uint) cube_bits[r5] << 2) + ((uint) cube_bits[g5] << 1) +
    (uint) cube_bits[b5];
    ushort rgb;
    register dc_entry *pdc;

    /* Check for a color on the cube. */
    if (cx < 64)
	return (gx_color_index) cx;
    /* Not on the cube, check the dynamic color table. */
    rgb = (r5 << 10) + (g5 << 5) + b5;
    for (pdc = &dynamic_colors[rgb % dc_hash_size]; pdc->rgb != 0; pdc++) {
	if (pdc->rgb == rgb)
	    return (gx_color_index) (pdc->index);
    }
    if (pdc == &dynamic_colors[dc_hash_size]) {		/* Wraparound */
	for (pdc = &dynamic_colors[0]; pdc->rgb != 0; pdc++) {
	    if (pdc->rgb == rgb)
		return (gx_color_index) (pdc->index);
	}
    }
    if (next_dc_index == 256) {	/* No space left, report failure. */
	return gx_no_color_index;
    }
    /* Not on the cube, and not in the dynamic table. */
    /* Put in the dynamic table if space available. */
    {
	int i = next_dc_index++;

	pdc->rgb = rgb;
	pdc->index = i;
	gl_setpalettecolor(i, cv_bits(r, 6), cv_bits(g, 6), cv_bits(b, 6));
	return (gx_color_index) i;
    }
}

int
lvga256_map_color_rgb(gx_device * dev, gx_color_index color,
		      unsigned short prgb[3])
{
/*   gl_getpalettecolor (color,(int *)&prgb[0],(int *)&prgb[1],(int *)&prgb[2]); */
    prgb[0] = gx_max_color_value;
    prgb[1] = gx_max_color_value;
    prgb[2] = gx_max_color_value;
    return 0;
}

/* Copy a monochrome bitmap.  The colors are given explicitly. */
/* Color = gx_no_color_index means transparent (no effect on the image). */
int
lvga256_copy_mono(gx_device * dev,
		const byte * base, int sourcex, int raster, gx_bitmap_id id,
		  int x, int y, int w, int h,
		  gx_color_index zero, gx_color_index one)
{
    const byte *ptr_line = base + (sourcex >> 3);
    int left_bit = 0x80 >> (sourcex & 7);
    int dest_y = y, end_x = x + w;
    int invert = 0;
    int color;

    fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
    if (zero == gx_no_color_index) {
	if (one == gx_no_color_index)
	    return 0;
	color = (int)one;
    } else {
	if (one == gx_no_color_index) {
	    color = (int)zero;
	    invert = -1;
	} else {		/* Pre-clear the rectangle to zero */
	    gl_fillbox(x, y, w, h, 0);
	    color = (int)one;
	}
    }
    while (h--) {		/* for each line */
	const byte *ptr_source = ptr_line;
	register int dest_x = x;
	register int bit = left_bit;

	while (dest_x < end_x) {	/* for each bit in the line */
	    if ((*ptr_source ^ invert) & bit) {
		gl_setpixel(dest_x, dest_y, color);
	    }
	    dest_x++;
	    if ((bit >>= 1) == 0)
		bit = 0x80, ptr_source++;
	}
	dest_y++;
	ptr_line += raster;
    }
    return 0;
}

/* Copy a color pixel map.  This is just like a bitmap, except that */
/* each pixel takes 4 bits instead of 1 when device driver has color. */
int
lvga256_copy_color(gx_device * dev,
		const byte * base, int sourcex, int raster, gx_bitmap_id id,
		   int x, int y, int w, int h)
{
    fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
    if (gx_device_has_color(dev)) {	/* color device, four bits per pixel */
	const byte *line = base + sourcex;

	gl_putbox(x, y, w, h, line);
    } else {			/* monochrome device: one bit per pixel */
	/* bit map is the same as lvga256_copy_mono: one bit per pixel */
	lvga256_copy_mono(dev, base, sourcex, raster, id, x, y, w, h,
			  (gx_color_index) 0, (gx_color_index) 255);
    }
    return 0;
}

/* Fill a rectangle. */
int
lvga256_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
		       gx_color_index color)
{
    fit_fill(dev, x, y, w, h);
    gl_fillbox(x, y, w, h, color);
    return 0;
}

/* Tile a rectangle.  If neither color is transparent, */
/* pre-clear the rectangle to color0 and just tile with color1. */
/* This is faster because of how lvga256_copy_mono is implemented. */
/* Note that this also does the right thing for colored tiles. */
int
lvga256_tile_rectangle(gx_device * dev, const gx_tile_bitmap * tile,
      int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
		       int px, int py)
{
    if (czero != gx_no_color_index && cone != gx_no_color_index) {
	lvga256_fill_rectangle(dev, x, y, w, h, czero);
	czero = gx_no_color_index;
    }
    return gx_default_tile_rectangle(dev, tile, x, y, w, h, czero, cone, px, py);
}

/* Draw a line */
int
lvga256_draw_line(gx_device * dev, int x0, int y0, int x1, int y1,
		  gx_color_index color)
{
    gl_line(x0, y0, x1, y1, color);
    return 0;
}

#ifdef GS_DEVS_SHARED
extern void gs_lib_register_device(const gx_device *dev);
void
gs_shared_init(void)
{
  gs_lib_register_device(&gs_lvga256_device);
}
#endif