home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEVSUN.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  21KB  |  688 lines

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