home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GDEVSUN.C < prev    next >
C/C++ Source or Header  |  1992-09-11  |  21KB  |  692 lines

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