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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
/* Copyright (C) 1989, 1995, 1996, 1998, 1999, 2000 Aladdin Enterprises. All rights reserved.
This file is part of Aladdin Ghostscript.
Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author
or distributor accepts any responsibility for the consequences of using it,
or for whether it serves any particular purpose or works at all, unless he
or she says so in writing. Refer to the Aladdin Ghostscript Free Public
License (the "License") for full details.
Every copy of Aladdin Ghostscript must include a copy of the License,
normally in a plain ASCII text file named PUBLIC. The License grants you
the right to copy, modify and redistribute Aladdin Ghostscript, but only
under certain conditions described in the License. Among other things, the
License requires that the copyright notice and this notice be preserved on
all copies.
*/
/*$Id$ */
/*
* Microsoft Windows 3.n driver for Ghostscript.
*
* Original version by Russell Lang and Maurice Castro with help from
* Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
* created from gdevbgi.c and gnuplot/term/win.trm 5th June 1992.
* Extensively modified by L. Peter Deutsch, Aladdin Enterprises.
*/
#include "gdevmswn.h"
#include "gp.h"
#include "gpcheck.h"
#include "gsparam.h"
#include "gdevpccm.h"
#include "gsdll.h"
#include "gsdllwin.h"
/* Forward references */
private int win_set_bits_per_pixel(P2(gx_device_win *, int));
#define TIMER_ID 1
/* Open the win driver */
int
win_open(gx_device * dev)
{
HDC hdc;
int code;
if (dev->width == INITIAL_WIDTH)
dev->width = (int)(8.5 * dev->x_pixels_per_inch);
if (dev->height == INITIAL_HEIGHT)
dev->height = (int)(11.0 * dev->y_pixels_per_inch);
if (wdev->BitsPerPixel == 0) {
int depth;
/* Set parameters that were unknown before opening device */
/* Find out if the device supports color */
/* We recognize 1, 4, 8, 16, 24 bit/pixel devices */
hdc = GetDC(NULL); /* get hdc for desktop */
depth = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL);
if (depth > 16) {
wdev->BitsPerPixel = 24;
} else if (depth > 8) {
wdev->BitsPerPixel = 16;
} else if (depth >= 8) {
wdev->BitsPerPixel = 8;
} else if (depth >= 4) {
wdev->BitsPerPixel = 4;
} else {
wdev->BitsPerPixel = 1;
}
ReleaseDC(NULL, hdc);
wdev->mapped_color_flags = 0;
}
if ((code = win_set_bits_per_pixel(wdev, wdev->BitsPerPixel)) < 0)
return code;
if (wdev->nColors > 0) {
/* create palette for display */
if ((wdev->limgpalette = win_makepalette(wdev))
== (LPLOGPALETTE) NULL)
return win_nomemory();
wdev->himgpalette = CreatePalette(wdev->limgpalette);
}
return 0;
}
/* Make the output appear on the screen. */
int
win_sync_output(gx_device * dev)
{
(*pgsdll_callback) (GSDLL_SYNC, (unsigned char *)wdev, 0);
return (0);
}
/* Make the window visible, and display the output. */
int
win_output_page(gx_device * dev, int copies, int flush)
{
(*pgsdll_callback) (GSDLL_PAGE, (unsigned char *)wdev, 0);
return gx_finish_output_page(dev, copies, flush);;
}
/* Close the win driver */
int
win_close(gx_device * dev)
{
/* Free resources */
if (wdev->nColors > 0) {
gs_free(wdev->mapped_color_flags, 4096, 1, "win_set_bits_per_pixel");
DeleteObject(wdev->himgpalette);
gs_free((char *)(wdev->limgpalette), 1, sizeof(LOGPALETTE) +
(1 << (wdev->color_info.depth)) * sizeof(PALETTEENTRY),
"win_close");
}
return (0);
}
/* Map a r-g-b color to the colors available under Windows */
gx_color_index
win_map_rgb_color(gx_device * dev, gx_color_value r, gx_color_value g,
gx_color_value b)
{
switch (wdev->BitsPerPixel) {
case 24:
return (((unsigned long)b >> (gx_color_value_bits - 8)) << 16) +
(((unsigned long)g >> (gx_color_value_bits - 8)) << 8) +
(((unsigned long)r >> (gx_color_value_bits - 8)));
case 16:{
gx_color_index color = ((r >> (gx_color_value_bits - 5)) << 11) +
((g >> (gx_color_value_bits - 6)) << 5) +
(b >> (gx_color_value_bits - 5));
#if arch_is_big_endian
ushort color16 = (ushort)color;
#else
ushort color16 = (ushort)((color << 8) | (color >> 8));
#endif
return color16;
}
case 15:{
gx_color_index color = ((r >> (gx_color_value_bits - 5)) << 10) +
((g >> (gx_color_value_bits - 5)) << 5) +
(b >> (gx_color_value_bits - 5));
#if arch_is_big_endian
ushort color15 = (ushort)color;
#else
ushort color15 = (ushort)((color << 8) | (color >> 8));
#endif
return color15;
}
case 8:{
int i;
LPLOGPALETTE lpal = wdev->limgpalette;
PALETTEENTRY *pep;
byte cr, cg, cb;
int mc_index;
byte mc_mask;
/* Check for a color in the palette of 64. */
{
static const byte pal64[32] =
{
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1
};
if (pal64[r >> (gx_color_value_bits - 5)] &&
pal64[g >> (gx_color_value_bits - 5)] &&
pal64[b >> (gx_color_value_bits - 5)]
)
return (gx_color_index) (
((r >> (gx_color_value_bits - 2)) << 4) +
((g >> (gx_color_value_bits - 2)) << 2) +
(b >> (gx_color_value_bits - 2))
);
}
/* map colors to 0->255 in 32 steps */
cr = win_color_value(r);
cg = win_color_value(g);
cb = win_color_value(b);
/* Search in palette, skipping the first 64. */
mc_index = ((cr >> 3) << 7) + ((cg >> 3) << 2) + (cb >> 6);
mc_mask = 0x80 >> ((cb >> 3) & 7);
if (wdev->mapped_color_flags[mc_index] & mc_mask)
for (i = wdev->nColors, pep = &lpal->palPalEntry[i];
--pep, --i >= 64;
) {
if (cr == pep->peRed &&
cg == pep->peGreen &&
cb == pep->peBlue
)
return ((gx_color_index) i); /* found it */
}
/* next try adding it to palette */
i = wdev->nColors;
if (i < 220) { /* allow 36 for windows and other apps */
LPLOGPALETTE lipal = wdev->limgpalette;
wdev->nColors = i + 1;
DeleteObject(wdev->himgpalette);
lipal->palPalEntry[i].peFlags = 0;
lipal->palPalEntry[i].peRed = cr;
lipal->palPalEntry[i].peGreen = cg;
lipal->palPalEntry[i].peBlue = cb;
lipal->palNumEntries = wdev->nColors;
wdev->himgpalette = CreatePalette(lipal);
wdev->mapped_color_flags[mc_index] |= mc_mask;
return ((gx_color_index) i); /* return new palette index */
}
return (gx_no_color_index); /* not found - dither instead */
}
case 4:
if ((r == g) && (g == b) && (r >= gx_max_color_value / 3 * 2 - 1)
&& (r < gx_max_color_value / 4 * 3))
return ((gx_color_index) 8); /* light gray */
return pc_4bit_map_rgb_color(dev, r, g, b);
}
return (gx_default_map_rgb_color(dev, r, g, b));
}
/* Map a color code to r-g-b. */
int
win_map_color_rgb(gx_device * dev, gx_color_index color,
gx_color_value prgb[3])
{
gx_color_value one;
ushort value;
switch (wdev->BitsPerPixel) {
case 24:
one = (gx_color_value) (gx_max_color_value / 255);
prgb[0] = ((color) & 255) * one;
prgb[1] = ((color >> 8) & 255) * one;
prgb[2] = ((color >> 16) & 255) * one;
break;
case 16:
value = (color >> 11) & 0x1f;
prgb[0] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
value = (color >> 5) & 0x3f;
prgb[1] = ((value << 10) + (value << 4) + (value >> 2)) >> (16 - gx_color_value_bits);
value = (color) & 0x1f;
prgb[2] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
break;
case 15:
value = (color >> 10) & 0x1f;
prgb[0] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
value = (color >> 5) & 0x1f;
prgb[1] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
value = (color) & 0x1f;
prgb[2] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
break;
case 8:
if (!dev->is_open)
return -1;
one = (gx_color_value) (gx_max_color_value / 255);
prgb[0] = wdev->limgpalette->palPalEntry[(int)color].peRed * one;
prgb[1] = wdev->limgpalette->palPalEntry[(int)color].peGreen * one;
prgb[2] = wdev->limgpalette->palPalEntry[(int)color].peBlue * one;
break;
case 4:
if (color == 8) /* VGA light gray */
prgb[0] = prgb[1] = prgb[2] = (gx_max_color_value / 4 * 3);
else
pc_4bit_map_color_rgb(dev, color, prgb);
break;
default:
prgb[0] = prgb[1] = prgb[2] =
(int)color ? gx_max_color_value : 0;
}
return 0;
}
/* Get Win parameters */
int
win_get_params(gx_device * dev, gs_param_list * plist)
{
int code = gx_default_get_params(dev, plist);
return code;
}
/* Put parameters. */
/* Set window parameters -- size and resolution. */
/* We implement this ourselves so that we can do it without */
/* closing and opening the device. */
int
win_put_params(gx_device * dev, gs_param_list * plist)
{
int ecode = 0, code;
bool is_open = dev->is_open;
int width = dev->width;
int height = dev->height;
int old_bpp = dev->color_info.depth;
int bpp = old_bpp;
byte *old_flags = wdev->mapped_color_flags;
/* Handle extra parameters */
switch (code = param_read_int(plist, "BitsPerPixel", &bpp)) {
case 0:
if (dev->is_open && bpp != old_bpp)
ecode = gs_error_rangecheck;
else { /* Don't release existing mapped_color_flags. */
if (bpp != 8)
wdev->mapped_color_flags = 0;
code = win_set_bits_per_pixel(wdev, bpp);
if (code < 0)
ecode = code;
else
break;
}
goto bppe;
default:
ecode = code;
bppe:param_signal_error(plist, "BitsPerPixel", ecode);
case 1:
break;
}
if (ecode >= 0) { /* Prevent gx_default_put_params from closing the device. */
dev->is_open = false;
ecode = gx_default_put_params(dev, plist);
dev->is_open = is_open;
}
if (ecode < 0) { /* If we allocated mapped_color_flags, release it. */
if (wdev->mapped_color_flags != 0 && old_flags == 0)
gs_free(wdev->mapped_color_flags, 4096, 1,
"win_put_params");
wdev->mapped_color_flags = old_flags;
if (bpp != old_bpp)
win_set_bits_per_pixel(wdev, old_bpp);
return ecode;
}
if (wdev->mapped_color_flags == 0 && old_flags != 0) { /* Release old mapped_color_flags. */
gs_free(old_flags, 4096, 1, "win_put_params");
}
/* Hand off the change to the implementation. */
if (is_open && (bpp != old_bpp ||
dev->width != width || dev->height != height)
) {
int ccode;
(*wdev->free_bitmap) (wdev);
ccode = (*wdev->alloc_bitmap) (wdev, (gx_device *) wdev);
if (ccode < 0) { /* Bad news! Some of the other device parameters */
/* may have changed. We don't handle this. */
/* This is ****** WRONG ******. */
dev->width = width;
dev->height = height;
win_set_bits_per_pixel(wdev, old_bpp);
(*wdev->alloc_bitmap) (wdev, dev);
return ccode;
}
}
return 0;
}
/* ------ Internal routines ------ */
#undef wdev
/* out of memory error message box */
int
win_nomemory(void)
{
MessageBox((HWND) NULL, (LPSTR) "Not enough memory", (LPSTR) szAppName, MB_ICONSTOP);
return gs_error_limitcheck;
}
LPLOGPALETTE
win_makepalette(gx_device_win * wdev)
{
int i, val;
LPLOGPALETTE logpalette;
logpalette = (LPLOGPALETTE) gs_malloc(1, sizeof(LOGPALETTE) +
(1 << (wdev->color_info.depth)) * sizeof(PALETTEENTRY),
"win_makepalette");
if (logpalette == (LPLOGPALETTE) NULL)
return (0);
logpalette->palVersion = 0x300;
logpalette->palNumEntries = wdev->nColors;
for (i = 0; i < wdev->nColors; i++) {
logpalette->palPalEntry[i].peFlags = 0;
switch (wdev->nColors) {
case 64:
/* colors are rrggbb */
logpalette->palPalEntry[i].peRed = ((i & 0x30) >> 4) * 85;
logpalette->palPalEntry[i].peGreen = ((i & 0xC) >> 2) * 85;
logpalette->palPalEntry[i].peBlue = (i & 3) * 85;
break;
case 16:
/* colors are irgb */
val = (i & 8 ? 255 : 128);
logpalette->palPalEntry[i].peRed = i & 4 ? val : 0;
logpalette->palPalEntry[i].peGreen = i & 2 ? val : 0;
logpalette->palPalEntry[i].peBlue = i & 1 ? val : 0;
if (i == 8) { /* light gray */
logpalette->palPalEntry[i].peRed =
logpalette->palPalEntry[i].peGreen =
logpalette->palPalEntry[i].peBlue = 192;
}
break;
case 2:
logpalette->palPalEntry[i].peRed =
logpalette->palPalEntry[i].peGreen =
logpalette->palPalEntry[i].peBlue = (i ? 255 : 0);
break;
}
}
return (logpalette);
}
private int
win_set_bits_per_pixel(gx_device_win * wdev, int bpp)
{
static const gx_device_color_info win_24bit_color = dci_color(24, 255, 255);
static const gx_device_color_info win_16bit_color = dci_color(16, 255, 255);
static const gx_device_color_info win_8bit_color = dci_color(8, 31, 4);
static const gx_device_color_info win_ega_color = dci_pc_4bit;
static const gx_device_color_info win_vga_color = dci_pc_4bit;
static const gx_device_color_info win_mono_color = dci_black_and_white;
/* remember old anti_alias info */
gx_device_anti_alias_info anti_alias = wdev->color_info.anti_alias;
HDC hdc;
switch (bpp) {
case 24:
wdev->color_info = win_24bit_color;
wdev->nColors = -1;
break;
case 16:
case 15:
wdev->color_info = win_16bit_color;
wdev->nColors = -1;
break;
case 8:
/* use 64 static colors and 166 dynamic colors from 8 planes */
wdev->color_info = win_8bit_color;
wdev->nColors = 64;
break;
case 4:
hdc = GetDC(NULL);
if (GetDeviceCaps(hdc, VERTRES) <= 350)
wdev->color_info = win_ega_color;
else
wdev->color_info = win_vga_color;
ReleaseDC(NULL, hdc);
wdev->nColors = 16;
break;
case 1:
wdev->color_info = win_mono_color;
wdev->nColors = 2;
break;
default:
return (gs_error_rangecheck);
}
wdev->BitsPerPixel = bpp;
/* If necessary, allocate and clear the mapped color flags. */
if (bpp == 8) {
if (wdev->mapped_color_flags == 0) {
wdev->mapped_color_flags = gs_malloc(4096, 1, "win_set_bits_per_pixel");
if (wdev->mapped_color_flags == 0)
return_error(gs_error_VMerror);
}
memset(wdev->mapped_color_flags, 0, 4096);
} else {
gs_free(wdev->mapped_color_flags, 4096, 1, "win_set_bits_per_pixel");
wdev->mapped_color_flags = 0;
}
/* restore old anti_alias info */
wdev->color_info.anti_alias = anti_alias;
return 0;
}
|