/* Copyright (C) 1989, 1992 Aladdin Enterprises. All rights reserved.
Distributed by Free Software Foundation, Inc.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
/* SunView driver for Ghostscript */
#include "gx.h" /* for gx_bitmap; includes std.h */
#include <suntool/sunview.h>
#include <suntool/canvas.h>
#include <sunwindow/cms_mono.h>
#include "gsmatrix.h" /* needed for gxdevice.h */
# define DEFAULT_DPI 75 /* Sun standard monitor */
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 */
private gx_device_procs sun_procs
= {
gx_default_get_initial_matrix
,
gx_default_tile_rectangle
,
#define CMSNAME "GHOSTVIEW" /* SunView colormap name */
/* Define the SunView device */
typedef struct gx_device_sun
{
int truecolor
; /* use truecolor mapping */
gx_color_index ncols
; /* allocated 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
];
/* 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
/* The instance is public. */
gx_device_sun gs_sunview_device
= {
(int)(8.5*DEFAULT_DPI
), (int)(11*DEFAULT_DPI
), /* x and y extent */
DEFAULT_DPI
, DEFAULT_DPI
, /* x and y density */
dci_color(0,0,0), /* fill in later from display depth */
0, /* connection not initialized */
/* Macro for casting gx_device argument */
#define xdev ((gx_device_sun *)dev)
/* Macro to validate arguments */
if ( w <= 0 || h <= 0 ) return 0;\
if ( x < 0 || x > xdev->width - w || y < 0 || y > xdev->height - h )\
* The macros below define the colormap configuration used on 8-bit
* 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)
#define MAX_BITS GREEN_BITS
#if (BLUE_BITS > MAX_BITS)
#define MAX_BITS BLUE_BITS
* 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 */
#if (RGB_BITS < 0) || (RGB_BITS > DEPTH)
Display_does_not_support_this_many_colors
* The macros below define the color mapping used on 24-bit true-color
* FAKE_TRUE_COLOR is used for debugging only. It simulates a true-color
* type mapping on an 8-bit pseudo-color display.
# 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)
# 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 \
#define TRUE_BLUE_MASK ((1 << (TRUE_RED_BITS + TRUE_GREEN_BITS \
- 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. */
private Notify_value
destroy_func();
sun_open(register gx_device
*dev
)
if (xdev
->frame
== (Frame
)0)
window_create(NULL
, FRAME
, FRAME_LABEL
, "ghostscript",
WIN_HEIGHT
, xdev
->width
+ 20,
WIN_HEIGHT
, xdev
->height
+ 40, 0);
if (xdev
->frame
== (Frame
)0)
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 */
WIN_VERTICAL_SCROLLBAR
, scrollbar_create(0),
WIN_HORIZONTAL_SCROLLBAR
, scrollbar_create(0),
xdev
->pw
= canvas_pixwin(xdev
->canvas
);
switch (xdev
->pw
->pw_pixrect
->pr_depth
) {
static gx_device_color_info mono_ci
=
* If the pre-allocated color cube leaves room for spare entries,
* tell gs we can render colors exactly. Otherwise admit our
static gx_device_color_info color_ci
=
#if (RGB_COLS < ALL_COLS)
dci_color(DEPTH
, 31, MAX_COLS
);
dci_color(DEPTH
, MAX_COLS
- 1, MAX_COLS
);
static gx_device_color_info truecolor_ci
=
dci_color(TRUE_DEPTH
,31,4);
xdev
->color_info
= mono_ci
;
/* pseudo-color display */
xdev
->color_info
= color_ci
;
#endif /* FAKE_TRUE_COLOR */
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
;
eprintf1("gs: Cannot handle display of depth %d.\n",
xdev
->pw
->pw_pixrect
->pr_depth
);
if ( gx_device_has_color(xdev
)
* Create the pre-allocated colorcube.
xdev
->red
= (byte
*)malloc(ALL_COLS
);
xdev
->green
= (byte
*)malloc(ALL_COLS
);
xdev
->blue
= (byte
*)malloc(ALL_COLS
);
if (!xdev
->red
|| !xdev
->green
|| !xdev
->blue
) {
eprintf("gs: no memory for colomap\n");
* Fit the largest possible color cube into the colormap.
for ( j
= 0; j
< ALL_COLS
; j
++ ) {
(double)((j
& TRUE_BLUE_MASK
)
>> (TRUE_GREEN_BITS
+ TRUE_RED_BITS
))
(double)((j
& TRUE_GREEN_MASK
) >> TRUE_RED_BITS
)
(double)((j
& TRUE_RED_MASK
))
#else /* !FAKE_TRUE_COLOR */
* Black and white are allocated in the first two slots,
* so as to be compatible with the monochrome colormap.
* This prevents most text etc. to go technicolor as focus
* changes into the ghostscript window.
cms_monochromeload(xdev
->red
, xdev
->green
, xdev
->blue
);
* The remaining slots up to RGB_COLS-1 are filled with
* evenly spaced points from the colorcube.
for ( j
= 2; j
< RGB_COLS
; j
++ ) {
int color
= j
- 1; /* bit pattern corresponding to color */
(double)((color
& RED_MASK
) >> (GREEN_BITS
+ BLUE_BITS
))
(double)((color
& GREEN_MASK
) >> BLUE_BITS
)
(double)((color
& BLUE_MASK
))
#endif /* FAKE_TRUE_COLOR */
* Set the high-water mark to the end of the colorcube.
* The unusued entries are filled so that the last entry is
* always different from the 0th entry. This is a requirement
for ( ; j
< ALL_COLS
; j
++) {
xdev
->red
[j
] = xdev
->green
[j
] = xdev
->blue
[j
] =
sprintf(xdev
->cmsname
, "%s-%d", CMSNAME
, getpid());
pw_setcmsname(xdev
->pw
, xdev
->cmsname
);
pw_putcolormap(xdev
->pw
, 0, ALL_COLS
,
xdev
->red
, xdev
->green
, xdev
->blue
);
window_set(xdev
->frame
, WIN_SHOW
, TRUE
, 0);
/* Interpose a destroy function to keep Ghostscript from */
/* getting confused if the user closes the window. */
notify_interpose_destroy_func(xdev
->frame
, destroy_func
);
(void) notify_do_dispatch();
(void) notify_dispatch();
/* Prevent the user from closing the window. */
destroy_func(Frame frame
, Destroy_status status
)
{ if ( status
== DESTROY_CHECKING
)
{ notify_veto_destroy(frame
);
return (notify_next_destroy_func(frame
, status
));
sun_close(gx_device
*dev
)
{ window_destroy(xdev
->frame
);
xdev
->canvas
= (Canvas
)0;
/* Synchronize the display with the commands already given */
sun_sync(register gx_device
*dev
)
{ (void) notify_dispatch();
/* 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).
sun_map_rgb_color(gx_device
*dev
, unsigned short red
,
unsigned short green
, unsigned short blue
)
{ if ( !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
;
* 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
= 0; i
< xdev
->ncols
; i
++) {
if (xdev
->red
[i
] == red_val
&&
xdev
->green
[i
] == green_val
&&
xdev
->blue
[i
] == blue_val
) {
* 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
if (xdev
->ncols
== ALL_COLS
) {
eprintf("gs: last spare color map entry allocated\n");
return gx_no_color_index
;
* Allocate new color in map.
xdev
->green
[i
] = green_val
;
xdev
->blue
[i
] = blue_val
;
pw_setcmsname(xdev
->pw
, xdev
->cmsname
);
pw_putcolormap(xdev
->pw
, xdev
->ncols
, 1,
&xdev
->red
[i
], &xdev
->green
[i
], &xdev
->blue
[i
]);
if (xdev
->ncols
== ALL_COLS
)
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
))
(red
>> (gx_color_value_bits
- TRUE_RED_BITS
));
/* Map color number back to RGB values - see sun_map_rgb_color(), above */
sun_map_color_rgb(gx_device
*dev
, gx_color_index color
,
{ if ( !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
->ncols
) {
eprintf1("gs: attempt to get RGB values for unallocated color index %d\n", color
);
rgb
[0] = (double)xdev
->red
[color
] / (ALL_COLS
- 1)
rgb
[1] = (double)xdev
->green
[color
] / (ALL_COLS
- 1)
rgb
[2] = (double)xdev
->blue
[color
] / (ALL_COLS
- 1)
else { /* true color mapping */
rgb
[0] = (double)((unsigned short)(color
& TRUE_RED_MASK
))
rgb
[1] = (double)((unsigned short)(color
& TRUE_GREEN_MASK
)
rgb
[2] = (double)((unsigned short)(color
& TRUE_BLUE_MASK
)
>> (TRUE_GREEN_BITS
+ TRUE_RED_BITS
))
/* Fill a rectangle with a color. */
sun_fill_rectangle(register gx_device
*dev
,
int x
, int y
, int w
, int h
, gx_color_index color
)
pw_write(xdev
->pw
, x
, y
, w
, h
, PIX_SRC
| PIX_COLOR((int)(color
)),
(void) notify_dispatch();
/* Copy a monochrome bitmap. */
sun_copy_mono(register gx_device
*dev
,
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
)
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
];
xdev
->pr
.pr_ops
= &mem_ops
;
xdev
->pr
.pr_width
= w
+ sourcex
+ 8;
xdev
->pr
.pr_data
= (caddr_t
)&(xdev
->mpr
);
xdev
->mpr
.md_linebytes
= raster
;
xdev
->mpr
.md_image
= (short *)((ulong
)base
& ~1);
/* Reverse the bit order in each byte. */
for ( i
= 0; i
< nbytes
; i
++ ) base
[i
] = reverse_bits
[base
[i
]];
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,
if (zero
!= gx_no_color_index
)
{ for (i
= 0; i
< nbytes
; i
++) 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,
for (i
= 0; i
< nbytes
; i
++) base
[i
] = ~base
[i
];
/* Reverse the bits back again. */
for ( i
= 0; i
< nbytes
; i
++ ) base
[i
] = reverse_bits
[base
[i
]];
(void) notify_dispatch();
/* Copy a color bitmap. */
sun_copy_color(register gx_device
*dev
,
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
,
(gx_color_index
)0, (gx_color_index
)1);
xdev
->pr
.pr_ops
= &mem_ops
;
xdev
->pr
.pr_width
= w
+ sourcex
+ 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
,
(((int)base
& 1) ? sourcex
+ 8 : sourcex
), 0);
(void) notify_dispatch();
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();