xref: /plan9/sys/src/cmd/gs/src/gdevsun.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1989, 1992, 1994, 1996 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gdevsun.c,v 1.4 2002/02/21 22:24:52 giles Exp $*/
18 /* SunView driver */
19 #include "gx.h"			/* for gx_bitmap; includes std.h */
20 
21 #include <suntool/sunview.h>
22 #include <suntool/canvas.h>
23 #include <sunwindow/cms_mono.h>
24 #include <stdio.h>
25 
26 #include "gscdefs.h"
27 #include "gsmatrix.h"			/* needed for gxdevice.h */
28 #include "gxdevice.h"
29 #include "malloc_.h"
30 
31 #ifndef DEFAULT_DPI
32 #  define DEFAULT_DPI 75		/* Sun standard monitor */
33 #endif
34 
35 #ifdef A4
36 #  define PAPER_X 8.27			/* A4 paper */
37 #  define PAPER_Y 11.69
38 #endif
39 
40 #ifndef PAPER_X
41 #  define PAPER_X 8.5			/* US letter paper */
42 #  define PAPER_Y 11
43 #endif
44 /* Procedures */
45 dev_proc_open_device(sun_open);
46 dev_proc_sync_output(sun_sync);
47 dev_proc_close_device(sun_close);
48 dev_proc_map_rgb_color(sun_map_rgb_color);
49 dev_proc_map_color_rgb(sun_map_color_rgb);
50 dev_proc_fill_rectangle(sun_fill_rectangle);
51 dev_proc_copy_mono(sun_copy_mono);
52 dev_proc_copy_color(sun_copy_color);
53 dev_proc_draw_line(sun_draw_line);
54 
55 /* The device descriptor */
56 private gx_device_procs sun_procs = {
57 	sun_open,
58 	NULL,			/* get_initial_matrix */
59 	sun_sync,
60 	NULL,			/* output_page */
61 	sun_close,
62 	sun_map_rgb_color,
63 	sun_map_color_rgb,
64 	sun_fill_rectangle,
65 	NULL,			/* tile_rectangle */
66 	sun_copy_mono,
67 	sun_copy_color,
68 	sun_draw_line
69 };
70 
71 #define CMSNAME	"GHOSTVIEW"		/* SunView colormap name */
72 
73 /* Define the SunView device */
74 typedef struct gx_device_sun {
75 	gx_device_common;
76 	Frame frame;
77 	Canvas canvas;
78 	Pixwin *pw;
79 	struct mpr_data mpr;
80 	Pixrect	pr;
81 	int truecolor;			/* use truecolor mapping */
82 	int freecols;			/* unallocated colors */
83 	byte *red, *green, *blue;	/* colormap */
84 	char cmsname[sizeof(CMSNAME)+9];/* color map name */
85 #if !arch_is_big_endian			/* need to swap bits & bytes */
86 #  define BUF_WIDTH_BYTES (((int)(8.5*DEFAULT_DPI)+15)/16*2)
87 	byte swap_buf[BUF_WIDTH_BYTES];
88 #endif
89 } gx_device_sun;
90 
91 #if !arch_is_big_endian
92 /* Define a table for reversing bit order. */
93 static byte reverse_bits[256] = {
94   0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
95   8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
96   4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
97   12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
98   2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
99   10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
100   6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
101   14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
102   1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
103   9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
104   5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
105   13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
106   3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
107   11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
108   7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
109   15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
110 };
111 #endif
112 
113 /* The instance is public. */
114 gx_device_sun far_data gs_sunview_device = {
115 	std_device_std_body(gx_device_sun, &sun_procs, "sunview",
116 	  (int)(PAPER_X*DEFAULT_DPI), (int)(PAPER_Y*DEFAULT_DPI),	/* x and y extent */
117 	  DEFAULT_DPI, DEFAULT_DPI	/* x and y density */
118 	),	/* fill in color_info later from display depth */
119 	 { 0 },			/* std_procs */
120  	0,			/* connection not initialized */
121 };
122 
123 /* Macro for casting gx_device argument */
124 #define xdev ((gx_device_sun *)dev)
125 
126 /*
127  * The macros below define the colormap configuration used on 8-bit
128  * pseudo-color displays.
129  */
130 /*
131  * The following macros define the number of bits used to represent rgb colors.
132  * The total must not exceed the display depth.
133  * Note that the RGB dimensions could have an uneven number of bits assigned
134  * to them, but that will cause dithering to not work very well, since
135  * gs assumes the dither ramp is the same for all 3 color dimensions.
136  *
137  * Setting RED_BITS to n will pre-allocate a color-cube of 2^(3n) entries.
138  * The remaining entries are allocated on demand for colors requested by
139  * sun_map_rgb_color(), until the color map is full. At that point gs will
140  * fall back onto dithering using the pre-allocated colors.
141  * As a special case, if RED_BITS = GREEN_BITS = BLUE_BITS = 0, only
142  * black and white are pre-allocated.
143  */
144 #define RED_BITS	2		/* everything depends on this one */
145 #define GREEN_BITS	RED_BITS
146 #define BLUE_BITS	RED_BITS
147 #define DEPTH		8		/* don't change this */
148 #define RGB_BITS	(RED_BITS + GREEN_BITS + BLUE_BITS)
149 /*
150  * Smallest # bits per dimension
151  */
152 #define MAX_BITS	RED_BITS
153 #if (GREEN_BITS > MAX_BITS)
154 #undef MAX_BITS
155 #define MAX_BITS	GREEN_BITS
156 #endif
157 #if (BLUE_BITS > MAX_BITS)
158 #undef MAX_BITS
159 #define MAX_BITS	BLUE_BITS
160 #endif
161 /*
162  * masks to pull out rgb components
163  */
164 #define BLUE_MASK	((1 << BLUE_BITS) - 1)
165 #define GREEN_MASK	((1 << (BLUE_BITS + GREEN_BITS)) - 1 - BLUE_MASK)
166 #define RED_MASK	((1 << (BLUE_BITS + GREEN_BITS + RED_BITS)) - 1 \
167 			 - BLUE_MASK - GREEN_MASK)
168 /*
169  * number of colors on rgb dimensions
170  */
171 #define RED_COLS	(1 << RED_BITS)
172 #define GREEN_COLS	(1 << GREEN_BITS)
173 #define BLUE_COLS	(1 << BLUE_BITS)
174 #define RGB_COLS	(RED_COLS * GREEN_COLS * BLUE_COLS)
175 #define MAX_COLS	(1 << MAX_BITS)
176 /*
177  * maximum number of colors in map
178  */
179 #define ALL_COLS	(1 << DEPTH)	/* 256 */
180 #define CMS_SIZE	ALL_COLS	/* cut down to 64 or 128 for
181 					   more cooperative behaviour */
182 
183 #if (RGB_COLS > CMS_SIZE)		/* one is reserved for the scrollbar */
184 CMS_SIZE_too_small_for_color_cube
185 #endif
186 #if (RGB_BITS < 0) || (RGB_BITS > DEPTH)
187 Display_does_not_support_this_many_colors
188 #endif
189 
190 /*
191  * The macros below define the color mapping used on 24-bit true-color
192  * displays.
193  * FAKE_TRUE_COLOR is used for debugging only.  It simulates a true-color
194  * type mapping on an 8-bit pseudo-color display.
195 #define FAKE_TRUE_COLOR
196  */
197 #ifdef FAKE_TRUE_COLOR
198 # define TRUE_RED_BITS	3		/* everything depends on this one */
199 # define TRUE_GREEN_BITS 2
200 # define TRUE_BLUE_BITS	(DEPTH - TRUE_RED_BITS - TRUE_GREEN_BITS)
201 #else
202 # define TRUE_RED_BITS	8		/* everything depends on this one */
203 # define TRUE_GREEN_BITS TRUE_RED_BITS
204 # define TRUE_BLUE_BITS	TRUE_RED_BITS
205 #endif ./* FAKE_TRUE_COLOR */
206 #define TRUE_DEPTH	(TRUE_RED_BITS + TRUE_GREEN_BITS + TRUE_BLUE_BITS)
207 /*
208  * Masks to pull out rgb components.  Note that the bit order is BGR from
209  * high to low order bits.
210  */
211 #define TRUE_RED_MASK	((1 << TRUE_RED_BITS) - 1)
212 #define TRUE_GREEN_MASK	((1 << (TRUE_RED_BITS + TRUE_GREEN_BITS)) - 1 \
213 			 - TRUE_RED_MASK)
214 #define TRUE_BLUE_MASK	((1 << (TRUE_RED_BITS + TRUE_GREEN_BITS \
215 				+ TRUE_BLUE_BITS)) - 1 \
216 			 - TRUE_GREEN_MASK - TRUE_RED_MASK)
217 /*
218  * number of colors on rgb dimensions
219  */
220 #define TRUE_RED_COLS	(1 << TRUE_RED_BITS)
221 #define TRUE_GREEN_COLS	(1 << TRUE_GREEN_BITS)
222 #define TRUE_BLUE_COLS	(1 << TRUE_BLUE_BITS)
223 
224 /* Initialize the device. */
225 private Notify_value destroy_func();
226 int
sun_open(register gx_device * dev)227 sun_open(register gx_device *dev)
228 {
229 #ifdef gs_DEBUG
230 if ( gs_debug['X'] )
231 	{ extern int _Xdebug;
232 	  _Xdebug = 1;
233 	}
234 #endif
235 	if (xdev->frame == (Frame)0)
236 	    xdev->frame =
237 		window_create(NULL, FRAME, FRAME_LABEL, gs_product,
238 			WIN_WIDTH, min(xdev->width + 24, 900),
239 			WIN_HEIGHT, min(xdev->height + 36, 900),
240 			WIN_Y, 0,
241 			WIN_X, 200,
242 			0);
243 	if (xdev->frame == (Frame)0)
244 	    return -1;
245 	xdev->canvas = window_create(xdev->frame, CANVAS,
246 			CANVAS_AUTO_EXPAND,		FALSE,
247 			CANVAS_AUTO_SHRINK,		FALSE,
248 			CANVAS_WIDTH,			xdev->width,
249 			CANVAS_HEIGHT,			xdev->height,
250 #ifndef PRE_IBIS	/* try to use 24-bit visual if OS supports it */
251 			CANVAS_COLOR24,			TRUE,
252 #endif
253 			CANVAS_RETAINED,		FALSE,
254 		0);
255 	xdev->pw = canvas_pixwin(xdev->canvas);
256 
257 	switch (xdev->pw->pw_pixrect->pr_depth) {
258 	     static gx_device_color_info mono_ci =
259 		dci_black_and_white;
260 	     /*
261 	      * If the pre-allocated color cube leaves room for spare entries,
262 	      * tell gs we can render colors exactly.  Otherwise admit our
263 	      * limitations.
264 	      */
265 	     static gx_device_color_info color_ci =
266 #if (RGB_COLS < CMS_SIZE)
267 		dci_color(DEPTH, 31, MAX_COLS);
268 #else
269 		dci_color(DEPTH, MAX_COLS - 1, MAX_COLS);
270 #endif
271 	     static gx_device_color_info truecolor_ci =
272 		dci_color(TRUE_DEPTH,31,4);
273 	case 1:
274 	     /* mono display */
275 	     xdev->color_info = mono_ci;
276 	     break;
277 #ifndef FAKE_TRUE_COLOR
278 	case DEPTH:
279 	     /* pseudo-color display */
280 	     xdev->color_info = color_ci;
281 	     xdev->truecolor = 0;
282 	     break;
283 #endif /* FAKE_TRUE_COLOR */
284 	case TRUE_DEPTH:
285 	case TRUE_DEPTH+8:	/* I'm not sure whether the XBGR frame buffer
286 				   returns depth 24 or 32. */
287 	     /* pseudo-color display */
288 	     xdev->color_info = truecolor_ci;
289 	     xdev->truecolor = 1;
290 	     break;
291 	default:
292 	     eprintf1("gs: Cannot handle display of depth %d.\n",
293 	              xdev->pw->pw_pixrect->pr_depth);
294 	     return -1;
295 	}
296 
297 	if ( gx_device_has_color(xdev)
298 #ifndef FAKE_TRUE_COLOR
299 	     && !xdev->truecolor
300 #endif
301 	   )
302 	   {
303 		int j;
304 		int color;
305 
306 		/*
307 		 * Create the pre-allocated colorcube.
308 		 */
309 		xdev->red = (byte *)malloc(CMS_SIZE);
310 		xdev->green = (byte *)malloc(CMS_SIZE);
311 		xdev->blue = (byte *)malloc(CMS_SIZE);
312 		if (!xdev->red || !xdev->green || !xdev->blue) {
313 			eprintf("gs: no memory for colormap\n");
314 			return -1;
315 		}
316 
317 #ifdef FAKE_TRUE_COLOR
318 		/*
319 		 * Fit the largest possible color cube into the colormap.
320 		 */
321 		for ( j = 0; j < ALL_COLS; j++ ) {
322 		   xdev->blue[j] =
323 			(double)((j & TRUE_BLUE_MASK)
324 			         >> (TRUE_GREEN_BITS + TRUE_RED_BITS))
325 			/ (TRUE_BLUE_COLS - 1)
326 			* (ALL_COLS - 1);
327 		   xdev->green[j] =
328 			(double)((j & TRUE_GREEN_MASK) >> TRUE_RED_BITS)
329 			/ (TRUE_GREEN_COLS - 1)
330 			* (ALL_COLS - 1);
331 		   xdev->red[j] =
332 			(double)((j & TRUE_RED_MASK))
333 			/ (TRUE_RED_COLS - 1)
334 			* (ALL_COLS - 1);
335 		}
336 
337 		xdev->freecols = 0;
338 #else /* !FAKE_TRUE_COLOR */
339 		/*
340 		 * Black and white are allocated in the last two slots,
341 		 * so as to be compatible with the monochrome colormap.
342 		 * This prevents most text etc. to go technicolor as focus
343 		 * changes into the window.
344 		 *
345 	         * The requirement that these two entries be at the end
346 		 * of the colormap makes it most convenient to allocate
347 		 * the remmaining entries from back to the front as well.
348 		 * Therefore xdev->freecols is the minimal allocated
349 		 * color index, and decreases as new ones are allocated.
350 		 */
351 		j = CMS_SIZE - 2;
352 		cms_monochromeload(xdev->red + j,
353 		                   xdev->green + j,
354 				   xdev->blue + j);
355 
356 		/*
357 		 * The remaining slots down to CMS_SIZE - RGB_COLS are filled
358 		 * with evenly spaced points from the colorcube.
359 		 */
360 		for ( color = 1; color < RGB_COLS - 1; color++ ) {
361 		   j--;
362 		   xdev->red[j] =
363 			(double)((color & RED_MASK) >> (GREEN_BITS + BLUE_BITS))
364 			/ (RED_COLS - 1)
365 			* (ALL_COLS - 1);
366 		   xdev->green[j] =
367 			(double)((color & GREEN_MASK) >> BLUE_BITS)
368 			/ (GREEN_COLS - 1)
369 			* (ALL_COLS - 1);
370 		   xdev->blue[j] =
371 			(double)((color & BLUE_MASK))
372 			/ (BLUE_COLS - 1)
373 			* (ALL_COLS - 1);
374 		}
375 
376 
377 		/*
378 		 * Set the low-water mark to the beginning of the colorcube.
379 		 */
380 		xdev->freecols = j;
381 
382 		/*
383 		 * The unused entries are filled so that the last entry is
384 		 * always different from the 0th entry.  This is a requirement
385 		 * for SunWindows.
386 		 */
387 		for (j-- ; j >= 0 ; j--) {
388 		   xdev->red[j] = xdev->green[j] = xdev->blue[j] =
389 			~xdev->red[CMS_SIZE - 1];
390 		}
391 #endif /* FAKE_TRUE_COLOR */
392 
393 		/*
394 		 * Install the colormap.
395 		 */
396 		sprintf(xdev->cmsname, "%s-%d", CMSNAME, getpid());
397 		pw_setcmsname(xdev->pw, xdev->cmsname);
398 		pw_putcolormap(xdev->pw, 0, CMS_SIZE,
399 		               xdev->red, xdev->green, xdev->blue);
400 	   }
401 	else {
402 		xdev->freecols = 0;
403 		xdev->red = (byte *)0;
404 		xdev->green = (byte *)0;
405 		xdev->blue = (byte *)0;
406 	}
407 
408 	/*
409 	 * Reset to retained after colormap length is changed
410 	 */
411 	window_set(xdev->canvas,
412 		CANVAS_RETAINED, 		TRUE,
413 		WIN_VERTICAL_SCROLLBAR,		scrollbar_create(0),
414 		WIN_HORIZONTAL_SCROLLBAR,	scrollbar_create(0),
415 		0);
416 	window_set(xdev->frame, WIN_SHOW, TRUE, 0);
417 	/* Interpose a destroy function to keep the driver bookkeeping */
418 	/* machinery from getting confused if the user closes the window. */
419 	notify_interpose_destroy_func(xdev->frame, destroy_func);
420 	(void) notify_do_dispatch();
421 	(void) notify_dispatch();
422 	return 0;
423 }
424 /* Prevent the user from closing the window. */
425 private Notify_value
destroy_func(Frame frame,Destroy_status status)426 destroy_func(Frame frame, Destroy_status status)
427 {	if ( status == DESTROY_CHECKING )
428 	   {	notify_veto_destroy(frame);
429 		return (NOTIFY_DONE);
430 	   }
431 	return (notify_next_destroy_func(frame, status));
432 }
433 
434 /* Close the device. */
435 int
sun_close(gx_device * dev)436 sun_close(gx_device *dev)
437 {	window_destroy(xdev->frame);
438 	xdev->frame = (Frame)0;
439 	xdev->canvas = (Canvas)0;
440 	xdev->pw = (Pixwin *)0;
441 	xdev->freecols = 0;
442 	if (xdev->red)
443 	    free(xdev->red);
444 	if (xdev->green)
445 	    free(xdev->green);
446 	if (xdev->blue)
447 	    free(xdev->blue);
448 	return 0;
449 }
450 
451 /* Synchronize the display with the commands already given */
452 int
sun_sync(register gx_device * dev)453 sun_sync(register gx_device *dev)
454 {	(void) notify_dispatch();
455 	return 0;
456 }
457 
458 /* Map RGB to color number -
459 	Look for existing entry in colormap, or create a new one, or
460 	give up if no free colormap entries (requesting dithering).
461  */
462 gx_color_index
sun_map_rgb_color(gx_device * dev,unsigned short red,unsigned short green,unsigned short blue)463 sun_map_rgb_color(gx_device *dev, unsigned short red,
464 	unsigned short green, unsigned short blue)
465 {	if ( !xdev->frame || !gx_device_has_color(dev) )
466 		/*
467 		 * Invert default color index to match mono display
468 		 * pixel values (black = 1, white = 0).
469 		 */
470 		return !gx_default_map_rgb_color(dev, red, green, blue);
471 	else if ( !xdev->truecolor ) {
472 		byte red_val, green_val, blue_val;
473 		int i;
474 		static int warn = 1;
475 
476 		/*
477 		 * Determine the RGB values at display resolution we
478 		 * ideally would want this color to be mapped into.
479 		 */
480 		red_val = (double)red/gx_max_color_value * (ALL_COLS - 1);
481 		green_val = (double)green/gx_max_color_value * (ALL_COLS - 1);
482 		blue_val = (double)blue/gx_max_color_value * (ALL_COLS - 1);
483 
484 		/*
485 		 * Look for an exact match among the colors already allocated.
486 		 * This includes the pre-allocated default color cube.
487 		 */
488 		for (i = CMS_SIZE - 1; i >= xdev->freecols; i--) {
489 			if (xdev->red[i] == red_val &&
490 			    xdev->green[i] == green_val &&
491 			    xdev->blue[i] == blue_val) {
492 				return i;
493 			}
494 		}
495 
496 		/*
497 		 * If we run out of space in the color map, let gs know.
498 		 * It will call us again to request colors to do the
499 		 * dithering, and hopefully request only RGB values that
500 		 * match the colorcube entries. IF NOT, WE WILL LOOP
501 		 * FOREVER!
502 		 * NOTE: Leave the zero'th colormap entry alone lest the
503 		 * scrollbar be colored.
504 		 */
505 		if (xdev->freecols <= 1) {
506 		    if (warn) {
507 			eprintf("gs: last spare color map entry allocated\n");
508 			warn = 0;
509 		    }
510 		    return gx_no_color_index;
511 		}
512 
513 		/*
514 		 * Allocate new color in map.
515 		 */
516 		xdev->red[i] = red_val;
517 		xdev->green[i] = green_val;
518 		xdev->blue[i] = blue_val;
519 		pw_setcmsname(xdev->pw, xdev->cmsname);
520 		pw_putcolormap(xdev->pw, i, 1,
521 		               &xdev->red[i], &xdev->green[i], &xdev->blue[i]);
522 
523 		xdev->freecols = i;
524 		return i;
525 	}
526 	else {	/* true color mapping --
527 			color index encodes all 3 RGB values */
528 		return ((blue >> (gx_color_value_bits - TRUE_BLUE_BITS))
529 			<< (TRUE_GREEN_BITS + TRUE_RED_BITS)) |
530 		       ((green >> (gx_color_value_bits - TRUE_GREEN_BITS))
531 			<< TRUE_RED_BITS) |
532 		       (red >> (gx_color_value_bits - TRUE_RED_BITS));
533 	}
534 }
535 
536 /* Map color number back to RGB values  - see sun_map_rgb_color(), above */
537 int
sun_map_color_rgb(gx_device * dev,gx_color_index color,unsigned short rgb[3])538 sun_map_color_rgb(gx_device *dev, gx_color_index color,
539 	unsigned short rgb[3])
540 {	if ( !xdev->frame || !gx_device_has_color(dev) )
541 		return gx_default_map_color_rgb(dev, !color, rgb);
542 	else if ( !xdev->truecolor ) {
543 		/*
544 		 * We just use the colormap to map back to rgb values.
545 		 */
546 		if (color < xdev->freecols || color >= CMS_SIZE) {
547 			eprintf1("gs: attempt to get RGB values for unallocated color index %d\n", (int)color);
548 			return -1;
549 		}
550 		rgb[0] = (double)xdev->red[color] / (ALL_COLS - 1)
551 			 * gx_max_color_value;
552 		rgb[1] = (double)xdev->green[color] / (ALL_COLS - 1)
553 			 * gx_max_color_value;
554 		rgb[2] = (double)xdev->blue[color] / (ALL_COLS - 1)
555 			 * gx_max_color_value;
556 		return 0;
557 	}
558 	else {	/* true color mapping */
559 		rgb[0] = (double)((unsigned short)(color & TRUE_RED_MASK))
560 			 / (TRUE_RED_COLS - 1)
561 			 * gx_max_color_value;
562 		rgb[1] = (double)((unsigned short)(color & TRUE_GREEN_MASK)
563 			  >> TRUE_RED_BITS)
564 			 / (TRUE_GREEN_COLS - 1)
565 			 * gx_max_color_value;
566 		rgb[2] = (double)((unsigned short)(color & TRUE_BLUE_MASK)
567 			  >> (TRUE_GREEN_BITS + TRUE_RED_BITS))
568 			 / (TRUE_BLUE_COLS - 1)
569 			 * gx_max_color_value;
570 		return 0;
571 	}
572 }
573 
574 /* Fill a rectangle with a color. */
575 int
sun_fill_rectangle(register gx_device * dev,int x,int y,int w,int h,gx_color_index color)576 sun_fill_rectangle(register gx_device *dev,
577   int x, int y, int w, int h, gx_color_index color)
578 {	fit_fill(dev, x, y, w, h);
579 
580 	pw_write(xdev->pw, x, y, w, h, PIX_SRC | PIX_COLOR((int)(color)),
581 		 (Pixrect *)0, 0, 0);
582 	(void) notify_dispatch();
583 	return 0;
584 }
585 
586 /* Copy a monochrome bitmap. */
587 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)588 sun_copy_mono(register gx_device *dev,
589   const byte *base, int sourcex, int raster, gx_bitmap_id id,
590   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
591 {
592 /* We define a non-const pointer to the data so we can invert it or */
593 /* byte-swap it in place temporarily (we restore it at the end). */
594 /* Yes, this is a bad and wicked thing to do! */
595 #define non_const_base ((byte *)base)
596 
597 	register int i;
598 	int nbytes;
599 	extern struct pixrectops mem_ops;
600 #if !arch_is_big_endian			/* need to swap bits & bytes */
601 #  define BUF_WIDTH_BYTES (((int)(8.5*DEFAULT_DPI)+15)/16*2)
602 	byte swap_buf[BUF_WIDTH_BYTES];
603 #endif
604 
605 	fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
606 	nbytes = h * raster;
607 
608 	xdev->pr.pr_ops = &mem_ops;
609 	xdev->pr.pr_width = w + sourcex + 8;
610 	xdev->pr.pr_height = h;
611 	xdev->pr.pr_depth = 1;
612 	xdev->pr.pr_data = (caddr_t)&(xdev->mpr);
613 	xdev->mpr.md_linebytes = raster;
614 	xdev->mpr.md_image = (short *)((ulong)base & ~1);
615 #if !arch_is_big_endian
616 	/* Reverse the bit order in each byte. */
617 	for ( i = 0; i < nbytes; i++ )
618 		non_const_base[i] = reverse_bits[base[i]];
619 #endif
620 	pw_batch_on(xdev->pw);
621 	if (one != gx_no_color_index)
622 	{	pw_stencil(xdev->pw, x, y, w, h,
623 			PIX_SRC | PIX_COLOR(one), &(xdev->pr),
624 			((int)base & 1) ? sourcex + 8 : sourcex, 0,
625 			(Pixrect *)0, 0, 0);
626 	}
627 	if (zero != gx_no_color_index)
628 	{	for (i = 0; i < nbytes; i++)
629 			non_const_base[i] = ~base[i];
630 		pw_stencil(xdev->pw, x, y, w, h,
631 			PIX_SRC | PIX_COLOR(zero), &(xdev->pr),
632 			((int)base & 1) ? sourcex + 8 : sourcex, 0,
633 			(Pixrect *)0, 0, 0);
634 		for (i = 0; i < nbytes; i++)
635 			non_const_base[i] = ~base[i];
636 	}
637 	pw_batch_off(xdev->pw);
638 #if !arch_is_big_endian
639 	/* Reverse the bits back again. */
640 	for ( i = 0; i < nbytes; i++ )
641 		non_const_base[i] = reverse_bits[base[i]];
642 #endif
643 	(void) notify_dispatch();
644 	return 0;
645 }
646 
647 /* Copy a color bitmap. */
648 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)649 sun_copy_color(register gx_device *dev,
650   const byte *base, int sourcex, int raster, gx_bitmap_id id,
651   int x, int y, int w, int h)
652 {
653 	extern struct pixrectops mem_ops;
654 
655 	if ( !gx_device_has_color(dev) )
656 		return sun_copy_mono(dev, base, sourcex, raster, id,
657 				     x, y, w, h,
658 				     (gx_color_index)0, (gx_color_index)1);
659 
660 	fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
661 
662 	xdev->pr.pr_ops = &mem_ops;
663 	xdev->pr.pr_width = w + sourcex + 8;
664 	xdev->pr.pr_height = h;
665 	xdev->pr.pr_depth = 8;
666 	xdev->pr.pr_data = (caddr_t)&(xdev->mpr);
667 	xdev->mpr.md_linebytes = raster;
668 	xdev->mpr.md_image = (short *)((ulong)base & ~1);
669 	pw_write(xdev->pw, x, y, w, h,
670 		 PIX_SRC, &(xdev->pr),
671 		 (((int)base & 1) ? sourcex + 8 : sourcex), 0);
672 	(void) notify_dispatch();
673 	return 0;
674 }
675 
676 /* Draw a line */
677 int
sun_draw_line(register gx_device * dev,int x0,int y0,int x1,int y1,gx_color_index color)678 sun_draw_line(register gx_device *dev,
679   int x0, int y0, int x1, int y1, gx_color_index color)
680 {	pw_vector(xdev->pw, x0, y0, x1, y1, PIX_SRC, color);
681 	(void) notify_dispatch();
682 	return 0;
683 }
684