summaryrefslogtreecommitdiff
path: root/gs/base/gdevsun.c
blob: fef535d3a09e766f74f35443585e653d67b62b21 (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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/* 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$*/
/* SunView driver */
#include "gx.h"			/* for gx_bitmap; includes std.h */

#include <suntool/sunview.h>
#include <suntool/canvas.h>
#include <sunwindow/cms_mono.h>
#include <stdio.h>

#include "gscdefs.h"
#include "gsmatrix.h"			/* needed for gxdevice.h */
#include "gxdevice.h"
#include "malloc_.h"

#ifndef DEFAULT_DPI
#  define DEFAULT_DPI 75		/* Sun standard monitor */
#endif

#ifdef A4
#  define PAPER_X 8.27			/* A4 paper */
#  define PAPER_Y 11.69
#endif

#ifndef PAPER_X
#  define PAPER_X 8.5			/* US letter paper */
#  define PAPER_Y 11
#endif
/* Procedures */
dev_proc_open_device(sun_open);
dev_proc_sync_output(sun_sync);
dev_proc_close_device(sun_close);
dev_proc_map_rgb_color(sun_map_rgb_color);
dev_proc_map_color_rgb(sun_map_color_rgb);
dev_proc_fill_rectangle(sun_fill_rectangle);
dev_proc_copy_mono(sun_copy_mono);
dev_proc_copy_color(sun_copy_color);
dev_proc_draw_line(sun_draw_line);

/* The device descriptor */
static gx_device_procs sun_procs = {
	sun_open,
	NULL,			/* get_initial_matrix */
	sun_sync,
	NULL,			/* output_page */
	sun_close,
	sun_map_rgb_color,
	sun_map_color_rgb,
	sun_fill_rectangle,
	NULL,			/* tile_rectangle */
	sun_copy_mono,
	sun_copy_color,
	sun_draw_line
};

#define CMSNAME	"GHOSTVIEW"		/* SunView colormap name */

/* Define the SunView device */
typedef struct gx_device_sun {
	gx_device_common;
	Frame frame;
	Canvas canvas;
	Pixwin *pw;
	struct mpr_data mpr;
	Pixrect	pr;
	int truecolor;			/* use truecolor mapping */
	int freecols;			/* unallocated colors */
	byte *red, *green, *blue;	/* colormap */
	char cmsname[sizeof(CMSNAME)+9];/* color map name */
#if !arch_is_big_endian			/* need to swap bits & bytes */
#  define BUF_WIDTH_BYTES (((int)(8.5*DEFAULT_DPI)+15)/16*2)
	byte swap_buf[BUF_WIDTH_BYTES];
#endif
} gx_device_sun;

#if !arch_is_big_endian
/* Define a table for reversing bit order. */
static byte reverse_bits[256] = {
  0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
  8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
  4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
  12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
  2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
  10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
  6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
  14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
  1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
  9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
  5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
  13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
  3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
  11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
  7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
  15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
};
#endif

/* The instance is public. */
gx_device_sun far_data gs_sunview_device = {
	std_device_std_body(gx_device_sun, &sun_procs, "sunview",
	  (int)(PAPER_X*DEFAULT_DPI), (int)(PAPER_Y*DEFAULT_DPI),	/* x and y extent */
	  DEFAULT_DPI, DEFAULT_DPI	/* x and y density */
	),	/* fill in color_info later from display depth */
	 { 0 },			/* std_procs */
 	0,			/* connection not initialized */
};

/* Macro for casting gx_device argument */
#define xdev ((gx_device_sun *)dev)

/*
 * The macros below define the colormap configuration used on 8-bit
 * pseudo-color displays.
 */
/*
 * The following macros define the number of bits used to represent rgb colors.
 * The total must not exceed the display depth.
 * Note that the RGB dimensions could have an uneven number of bits assigned
 * to them, but that will cause dithering to not work very well, since
 * gs assumes the dither ramp is the same for all 3 color dimensions.
 *
 * Setting RED_BITS to n will pre-allocate a color-cube of 2^(3n) entries.
 * The remaining entries are allocated on demand for colors requested by
 * sun_map_rgb_color(), until the color map is full. At that point gs will
 * fall back onto dithering using the pre-allocated colors.
 * As a special case, if RED_BITS = GREEN_BITS = BLUE_BITS = 0, only
 * black and white are pre-allocated.
 */
#define RED_BITS	2		/* everything depends on this one */
#define GREEN_BITS	RED_BITS
#define BLUE_BITS	RED_BITS
#define DEPTH		8		/* don't change this */
#define RGB_BITS	(RED_BITS + GREEN_BITS + BLUE_BITS)
/*
 * Smallest # bits per dimension
 */
#define MAX_BITS	RED_BITS
#if (GREEN_BITS > MAX_BITS)
#undef MAX_BITS
#define MAX_BITS	GREEN_BITS
#endif
#if (BLUE_BITS > MAX_BITS)
#undef MAX_BITS
#define MAX_BITS	BLUE_BITS
#endif
/*
 * masks to pull out rgb components
 */
#define BLUE_MASK	((1 << BLUE_BITS) - 1)
#define GREEN_MASK	((1 << (BLUE_BITS + GREEN_BITS)) - 1 - BLUE_MASK)
#define RED_MASK	((1 << (BLUE_BITS + GREEN_BITS + RED_BITS)) - 1 \
			 - BLUE_MASK - GREEN_MASK)
/*
 * number of colors on rgb dimensions
 */
#define RED_COLS	(1 << RED_BITS)
#define GREEN_COLS	(1 << GREEN_BITS)
#define BLUE_COLS	(1 << BLUE_BITS)
#define RGB_COLS	(RED_COLS * GREEN_COLS * BLUE_COLS)
#define MAX_COLS	(1 << MAX_BITS)
/*
 * maximum number of colors in map
 */
#define ALL_COLS	(1 << DEPTH)	/* 256 */
#define CMS_SIZE	ALL_COLS	/* cut down to 64 or 128 for
					   more cooperative behaviour */

#if (RGB_COLS > CMS_SIZE)		/* one is reserved for the scrollbar */
CMS_SIZE_too_small_for_color_cube
#endif
#if (RGB_BITS < 0) || (RGB_BITS > DEPTH) 
Display_does_not_support_this_many_colors
#endif

/*
 * The macros below define the color mapping used on 24-bit true-color
 * displays.
 * FAKE_TRUE_COLOR is used for debugging only.  It simulates a true-color
 * type mapping on an 8-bit pseudo-color display.
#define FAKE_TRUE_COLOR
 */
#ifdef FAKE_TRUE_COLOR
# define TRUE_RED_BITS	3		/* everything depends on this one */
# define TRUE_GREEN_BITS 2
# define TRUE_BLUE_BITS	(DEPTH - TRUE_RED_BITS - TRUE_GREEN_BITS)
#else
# define TRUE_RED_BITS	8		/* everything depends on this one */
# define TRUE_GREEN_BITS TRUE_RED_BITS
# define TRUE_BLUE_BITS	TRUE_RED_BITS
#endif ./* FAKE_TRUE_COLOR */
#define TRUE_DEPTH	(TRUE_RED_BITS + TRUE_GREEN_BITS + TRUE_BLUE_BITS)
/*
 * Masks to pull out rgb components.  Note that the bit order is BGR from
 * high to low order bits.
 */
#define TRUE_RED_MASK	((1 << TRUE_RED_BITS) - 1)
#define TRUE_GREEN_MASK	((1 << (TRUE_RED_BITS + TRUE_GREEN_BITS)) - 1 \
			 - TRUE_RED_MASK)
#define TRUE_BLUE_MASK	((1 << (TRUE_RED_BITS + TRUE_GREEN_BITS \
				+ TRUE_BLUE_BITS)) - 1 \
			 - TRUE_GREEN_MASK - TRUE_RED_MASK)
/*
 * number of colors on rgb dimensions
 */
#define TRUE_RED_COLS	(1 << TRUE_RED_BITS)
#define TRUE_GREEN_COLS	(1 << TRUE_GREEN_BITS)
#define TRUE_BLUE_COLS	(1 << TRUE_BLUE_BITS)

/* Initialize the device. */
static Notify_value destroy_func();
int
sun_open(register gx_device *dev)
{
#ifdef gs_DEBUG
if ( gs_debug['X'] )
	{ extern int _Xdebug;
	  _Xdebug = 1;
	}
#endif
	if (xdev->frame == (Frame)0)
	    xdev->frame =
		window_create(NULL, FRAME, FRAME_LABEL, gs_product,
			WIN_WIDTH, min(xdev->width + 24, 900),
			WIN_HEIGHT, min(xdev->height + 36, 900),
			WIN_Y, 0,
			WIN_X, 200,
			0);
	if (xdev->frame == (Frame)0)
	    return -1;
	xdev->canvas = window_create(xdev->frame, CANVAS,
			CANVAS_AUTO_EXPAND,		FALSE,
			CANVAS_AUTO_SHRINK,		FALSE,
			CANVAS_WIDTH,			xdev->width,
			CANVAS_HEIGHT,			xdev->height,
#ifndef PRE_IBIS	/* try to use 24-bit visual if OS supports it */
			CANVAS_COLOR24,			TRUE,
#endif
			CANVAS_RETAINED,		FALSE,
		0);
	xdev->pw = canvas_pixwin(xdev->canvas);

	switch (xdev->pw->pw_pixrect->pr_depth) {
	     static gx_device_color_info mono_ci =
		dci_black_and_white;
	     /*
	      * If the pre-allocated color cube leaves room for spare entries,
	      * tell gs we can render colors exactly.  Otherwise admit our
	      * limitations.
	      */
	     static gx_device_color_info color_ci =
#if (RGB_COLS < CMS_SIZE)
		dci_color(DEPTH, 31, MAX_COLS);
#else
		dci_color(DEPTH, MAX_COLS - 1, MAX_COLS);
#endif
	     static gx_device_color_info truecolor_ci =
		dci_color(TRUE_DEPTH,31,4);
	case 1:
	     /* mono display */
	     xdev->color_info = mono_ci;
	     break;
#ifndef FAKE_TRUE_COLOR
	case DEPTH:
	     /* pseudo-color display */
	     xdev->color_info = color_ci;
	     xdev->truecolor = 0;
	     break;
#endif /* FAKE_TRUE_COLOR */
	case TRUE_DEPTH:
	case TRUE_DEPTH+8:	/* I'm not sure whether the XBGR frame buffer
				   returns depth 24 or 32. */
	     /* pseudo-color display */
	     xdev->color_info = truecolor_ci;
	     xdev->truecolor = 1;
	     break;
	default:
	     emprintf1(dev->memory,
                       "gs: Cannot handle display of depth %d.\n",
	               xdev->pw->pw_pixrect->pr_depth);
	     return -1;
	}
		
	if ( gx_device_has_color(xdev)
#ifndef FAKE_TRUE_COLOR
	     && !xdev->truecolor
#endif
	   )
	   {	
		int j;
		int color;

		/*
		 * Create the pre-allocated colorcube.
		 */
		xdev->red = (byte *)malloc(CMS_SIZE);
		xdev->green = (byte *)malloc(CMS_SIZE);
		xdev->blue = (byte *)malloc(CMS_SIZE);
		if (!xdev->red || !xdev->green || !xdev->blue) {
			emprintf(dev->memory, "gs: no memory for colormap\n");
			return -1;
		}

#ifdef FAKE_TRUE_COLOR
		/*
		 * Fit the largest possible color cube into the colormap.
		 */
		for ( j = 0; j < ALL_COLS; j++ ) {
		   xdev->blue[j] =
			(double)((j & TRUE_BLUE_MASK)
			         >> (TRUE_GREEN_BITS + TRUE_RED_BITS))
			/ (TRUE_BLUE_COLS - 1)
			* (ALL_COLS - 1);
		   xdev->green[j] =
			(double)((j & TRUE_GREEN_MASK) >> TRUE_RED_BITS)
			/ (TRUE_GREEN_COLS - 1)
			* (ALL_COLS - 1);
		   xdev->red[j] =
			(double)((j & TRUE_RED_MASK))
			/ (TRUE_RED_COLS - 1)
			* (ALL_COLS - 1);
		}

		xdev->freecols = 0;
#else /* !FAKE_TRUE_COLOR */
		/*
		 * Black and white are allocated in the last two slots,
		 * so as to be compatible with the monochrome colormap.
		 * This prevents most text etc. to go technicolor as focus
		 * changes into the window.
		 *
	         * The requirement that these two entries be at the end
		 * of the colormap makes it most convenient to allocate
		 * the remmaining entries from back to the front as well.
		 * Therefore xdev->freecols is the minimal allocated
		 * color index, and decreases as new ones are allocated.
		 */
		j = CMS_SIZE - 2;
		cms_monochromeload(xdev->red + j,
		                   xdev->green + j,
				   xdev->blue + j);

		/*
		 * The remaining slots down to CMS_SIZE - RGB_COLS are filled
		 * with evenly spaced points from the colorcube.
		 */
		for ( color = 1; color < RGB_COLS - 1; color++ ) {
		   j--;
		   xdev->red[j] =
			(double)((color & RED_MASK) >> (GREEN_BITS + BLUE_BITS))
			/ (RED_COLS - 1)
			* (ALL_COLS - 1);
		   xdev->green[j] =
			(double)((color & GREEN_MASK) >> BLUE_BITS)
			/ (GREEN_COLS - 1)
			* (ALL_COLS - 1);
		   xdev->blue[j] =
			(double)((color & BLUE_MASK))
			/ (BLUE_COLS - 1)
			* (ALL_COLS - 1);
		}


		/*
		 * Set the low-water mark to the beginning of the colorcube.
		 */
		xdev->freecols = j;

		/*
		 * The unused entries are filled so that the last entry is
		 * always different from the 0th entry.  This is a requirement
		 * for SunWindows.
		 */
		for (j-- ; j >= 0 ; j--) {
		   xdev->red[j] = xdev->green[j] = xdev->blue[j] =
			~xdev->red[CMS_SIZE - 1];
		}
#endif /* FAKE_TRUE_COLOR */

		/*
		 * Install the colormap.
		 */
		sprintf(xdev->cmsname, "%s-%d", CMSNAME, getpid());
		pw_setcmsname(xdev->pw, xdev->cmsname);
		pw_putcolormap(xdev->pw, 0, CMS_SIZE,
		               xdev->red, xdev->green, xdev->blue);
	   }
	else {
		xdev->freecols = 0;
		xdev->red = (byte *)0;
		xdev->green = (byte *)0;
		xdev->blue = (byte *)0;
	}

	/*
	 * Reset to retained after colormap length is changed 
	 */
	window_set(xdev->canvas, 
		CANVAS_RETAINED, 		TRUE,
		WIN_VERTICAL_SCROLLBAR,		scrollbar_create(0),
		WIN_HORIZONTAL_SCROLLBAR,	scrollbar_create(0),
		0);
	window_set(xdev->frame, WIN_SHOW, TRUE, 0);
	/* Interpose a destroy function to keep the driver bookkeeping */
	/* machinery from getting confused if the user closes the window. */
	notify_interpose_destroy_func(xdev->frame, destroy_func);
	(void) notify_do_dispatch();
	(void) notify_dispatch();
	return 0;
}
/* Prevent the user from closing the window. */
static Notify_value
destroy_func(Frame frame, Destroy_status status)
{	if ( status == DESTROY_CHECKING )
	   {	notify_veto_destroy(frame);
		return (NOTIFY_DONE);
	   }
	return (notify_next_destroy_func(frame, status));
}

/* Close the device. */
int
sun_close(gx_device *dev)
{	window_destroy(xdev->frame);
	xdev->frame = (Frame)0;
	xdev->canvas = (Canvas)0;
	xdev->pw = (Pixwin *)0;
	xdev->freecols = 0;
	if (xdev->red)
	    free(xdev->red);
	if (xdev->green)
	    free(xdev->green);
	if (xdev->blue)
	    free(xdev->blue);
	return 0;
}

/* Synchronize the display with the commands already given */
int
sun_sync(register gx_device *dev)
{	(void) notify_dispatch();
	return 0;
}

/* Map RGB to color number -
	Look for existing entry in colormap, or create a new one, or
	give up if no free colormap entries (requesting dithering).
 */
gx_color_index
sun_map_rgb_color(gx_device *dev, unsigned short red,
	unsigned short green, unsigned short blue)
{	if ( !xdev->frame || !gx_device_has_color(dev) )
		/*
		 * Invert default color index to match mono display
		 * pixel values (black = 1, white = 0).
		 */
		return !gx_default_map_rgb_color(dev, red, green, blue);
	else if ( !xdev->truecolor ) {
		byte red_val, green_val, blue_val;
		int i;
		static int warn = 1;

		/*
		 * Determine the RGB values at display resolution we
		 * ideally would want this color to be mapped into.
		 */
		red_val = (double)red/gx_max_color_value * (ALL_COLS - 1);
		green_val = (double)green/gx_max_color_value * (ALL_COLS - 1);
		blue_val = (double)blue/gx_max_color_value * (ALL_COLS - 1);

		/*
		 * Look for an exact match among the colors already allocated.
		 * This includes the pre-allocated default color cube.
		 */
		for (i = CMS_SIZE - 1; i >= xdev->freecols; i--) {
			if (xdev->red[i] == red_val &&
			    xdev->green[i] == green_val &&
			    xdev->blue[i] == blue_val) {
				return i;
			}
		}
		
		/*
		 * If we run out of space in the color map, let gs know.
		 * It will call us again to request colors to do the
		 * dithering, and hopefully request only RGB values that
		 * match the colorcube entries. IF NOT, WE WILL LOOP
		 * FOREVER!
		 * NOTE: Leave the zero'th colormap entry alone lest the
		 * scrollbar be colored.
		 */
		if (xdev->freecols <= 1) {
		    if (warn) {
			emprintf(dev->memory,
                                 "gs: last spare color map entry allocated\n");
			warn = 0;
		    }
		    return gx_no_color_index; 
		}

		/*
		 * Allocate new color in map.
		 */
		xdev->red[i] = red_val;
		xdev->green[i] = green_val;
		xdev->blue[i] = blue_val;
		pw_setcmsname(xdev->pw, xdev->cmsname);
		pw_putcolormap(xdev->pw, i, 1,
		               &xdev->red[i], &xdev->green[i], &xdev->blue[i]);
		
		xdev->freecols = i;
		return i;
	}
	else {	/* true color mapping --
			color index encodes all 3 RGB values */
		return ((blue >> (gx_color_value_bits - TRUE_BLUE_BITS))
			<< (TRUE_GREEN_BITS + TRUE_RED_BITS)) |
		       ((green >> (gx_color_value_bits - TRUE_GREEN_BITS))
			<< TRUE_RED_BITS) |
		       (red >> (gx_color_value_bits - TRUE_RED_BITS));
	}
}

/* Map color number back to RGB values  - see sun_map_rgb_color(), above */
int
sun_map_color_rgb(gx_device *dev, gx_color_index color,
	unsigned short rgb[3])
{	if ( !xdev->frame || !gx_device_has_color(dev) )
		return gx_default_map_color_rgb(dev, !color, rgb);
	else if ( !xdev->truecolor ) {
		/*
		 * We just use the colormap to map back to rgb values.
		 */
		if (color < xdev->freecols || color >= CMS_SIZE) {
			emprintf1(dev->memory,
                                  "gs: attempt to get RGB values for unallocated color index %d\n",
                                  (int)color);
			return -1;
		}
		rgb[0] = (double)xdev->red[color] / (ALL_COLS - 1)
			 * gx_max_color_value;
		rgb[1] = (double)xdev->green[color] / (ALL_COLS - 1)
			 * gx_max_color_value;
		rgb[2] = (double)xdev->blue[color] / (ALL_COLS - 1)
			 * gx_max_color_value;
		return 0;
	}
	else {	/* true color mapping */
		rgb[0] = (double)((unsigned short)(color & TRUE_RED_MASK))
			 / (TRUE_RED_COLS - 1)
			 * gx_max_color_value;
		rgb[1] = (double)((unsigned short)(color & TRUE_GREEN_MASK)
			  >> TRUE_RED_BITS)
			 / (TRUE_GREEN_COLS - 1)
			 * gx_max_color_value;
		rgb[2] = (double)((unsigned short)(color & TRUE_BLUE_MASK)
			  >> (TRUE_GREEN_BITS + TRUE_RED_BITS))
			 / (TRUE_BLUE_COLS - 1)
			 * gx_max_color_value;
		return 0;
	}
}

/* Fill a rectangle with a color. */
int
sun_fill_rectangle(register gx_device *dev,
  int x, int y, int w, int h, gx_color_index color)
{	fit_fill(dev, x, y, w, h);

	pw_write(xdev->pw, x, y, w, h, PIX_SRC | PIX_COLOR((int)(color)),
		 (Pixrect *)0, 0, 0);
	(void) notify_dispatch();
	return 0;
}

/* Copy a monochrome bitmap. */
int
sun_copy_mono(register 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)
{
/* We define a non-const pointer to the data so we can invert it or */
/* byte-swap it in place temporarily (we restore it at the end). */
/* Yes, this is a bad and wicked thing to do! */
#define non_const_base ((byte *)base)

	register int i;
	int nbytes;
	extern struct pixrectops mem_ops;
#if !arch_is_big_endian			/* need to swap bits & bytes */
#  define BUF_WIDTH_BYTES (((int)(8.5*DEFAULT_DPI)+15)/16*2)
	byte swap_buf[BUF_WIDTH_BYTES];
#endif

	fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
	nbytes = h * raster;

	xdev->pr.pr_ops = &mem_ops;
	xdev->pr.pr_width = w + sourcex + 8;
	xdev->pr.pr_height = h;
	xdev->pr.pr_depth = 1;
	xdev->pr.pr_data = (caddr_t)&(xdev->mpr);
	xdev->mpr.md_linebytes = raster;
	xdev->mpr.md_image = (short *)((ulong)base & ~1);
#if !arch_is_big_endian
	/* Reverse the bit order in each byte. */
	for ( i = 0; i < nbytes; i++ )
		non_const_base[i] = reverse_bits[base[i]];
#endif
	pw_batch_on(xdev->pw);
	if (one != gx_no_color_index)
	{	pw_stencil(xdev->pw, x, y, w, h,
			PIX_SRC | PIX_COLOR(one), &(xdev->pr),
			((int)base & 1) ? sourcex + 8 : sourcex, 0,
			(Pixrect *)0, 0, 0);
	}
	if (zero != gx_no_color_index)
	{	for (i = 0; i < nbytes; i++)
			non_const_base[i] = ~base[i];
		pw_stencil(xdev->pw, x, y, w, h,
			PIX_SRC | PIX_COLOR(zero), &(xdev->pr),
			((int)base & 1) ? sourcex + 8 : sourcex, 0,
			(Pixrect *)0, 0, 0);
		for (i = 0; i < nbytes; i++)
			non_const_base[i] = ~base[i];
	}
	pw_batch_off(xdev->pw);
#if !arch_is_big_endian
	/* Reverse the bits back again. */
	for ( i = 0; i < nbytes; i++ )
		non_const_base[i] = reverse_bits[base[i]];
#endif
	(void) notify_dispatch();
	return 0;
}

/* Copy a color bitmap. */
int
sun_copy_color(register gx_device *dev,
  const byte *base, int sourcex, int raster, gx_bitmap_id id,
  int x, int y, int w, int h)
{
	extern struct pixrectops mem_ops;

	if ( !gx_device_has_color(dev) )
		return sun_copy_mono(dev, base, sourcex, raster, id,
				     x, y, w, h,
				     (gx_color_index)0, (gx_color_index)1);

	fit_copy(dev, base, sourcex, raster, id, x, y, w, h);

	xdev->pr.pr_ops = &mem_ops;
	xdev->pr.pr_width = w + sourcex + 8;
	xdev->pr.pr_height = h;
	xdev->pr.pr_depth = 8;
	xdev->pr.pr_data = (caddr_t)&(xdev->mpr);
	xdev->mpr.md_linebytes = raster;
	xdev->mpr.md_image = (short *)((ulong)base & ~1);
	pw_write(xdev->pw, x, y, w, h,
		 PIX_SRC, &(xdev->pr),
		 (((int)base & 1) ? sourcex + 8 : sourcex), 0);
	(void) notify_dispatch();
	return 0;
}

/* Draw a line */
int
sun_draw_line(register gx_device *dev,
  int x0, int y0, int x1, int y1, gx_color_index color)
{	pw_vector(xdev->pw, x0, y0, x1, y1, PIX_SRC, color);
	(void) notify_dispatch();
	return 0;
}