home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / specific / c / gdevsun < prev    next >
Encoding:
Text File  |  1991-10-25  |  8.5 KB  |  275 lines

  1. /* Copyright (C) 1989, 1990, 1991 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. /* (monochrome) SunView driver for Ghostscript library */
  22. #include <suntool/sunview.h>
  23. #include <suntool/canvas.h>
  24. #include <stdio.h>
  25. #include "arch.h"            /* we need to know byte order */
  26. /* Hack to get around the fact that something in the SunView library */
  27. /* defines uint and ushort.... */
  28. #  define uint _uint
  29. #  define ushort _ushort
  30. #include "gx.h"            /* for gx_bitmap; includes std.h */
  31. #  undef _uint
  32. #  undef _ushort
  33. #include "gsmatrix.h"            /* needed for gxdevice.h */
  34. #include "gxdevice.h"
  35.  
  36. #ifndef DEFAULT_DPI
  37. #  define DEFAULT_DPI 75        /* Sun standard monitor */
  38. #endif
  39.  
  40. /* Procedures */
  41. int sun_open(P1(gx_device *));
  42. int sun_close(P1(gx_device *));
  43. int sun_sync(P1(gx_device *));
  44. int sun_fill_rectangle(P6(gx_device *, int, int, int, int, gx_color_index));
  45. int sun_copy_mono(P10(gx_device *, byte *, int, int, int, int, int, int,
  46.   gx_color_index, gx_color_index));
  47. int sun_copy_color(P8(gx_device *, byte *, int, int, int, int, int, int));
  48. int sun_draw_line(P6(gx_device *, int, int, int, int, gx_color_index));
  49.  
  50. /* The device descriptor */
  51. private gx_device_procs sun_procs = {
  52.     sun_open,
  53.     gx_default_get_initial_matrix,
  54.     sun_sync,
  55.     gx_default_output_page,
  56.     sun_close,
  57.     gx_default_map_rgb_color,
  58.     gx_default_map_color_rgb,
  59.     sun_fill_rectangle,
  60.     gx_default_tile_rectangle,
  61.     sun_copy_mono,
  62.     sun_copy_color,
  63.     sun_draw_line,
  64.     gx_default_fill_trapezoid,
  65.     gx_default_tile_trapezoid
  66. };
  67.  
  68. /* Define the SunView device */
  69. typedef struct gx_device_sun {
  70.     gx_device_common;
  71.     Frame frame;
  72.     Canvas canvas;
  73.     Pixwin *pw;
  74.     struct mpr_data mpr;
  75.     Pixrect    pr;
  76. #if !arch_is_big_endian            /* need to swap bits & bytes */
  77. #  define BUF_WIDTH_BYTES (((int)(8.5*DEFAULT_DPI)+15)/16*2)
  78.     byte swap_buf[BUF_WIDTH_BYTES];
  79. #endif
  80. } gx_device_sun;
  81.  
  82. #if !arch_is_big_endian
  83. /* Define a table for reversing bit order. */
  84. static byte reverse_bits[256] = {
  85.   0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
  86.   8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
  87.   4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
  88.   12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
  89.   2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
  90.   10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
  91.   6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
  92.   14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
  93.   1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
  94.   9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
  95.   5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
  96.   13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
  97.   3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
  98.   11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
  99.   7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
  100.   15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
  101. };
  102. #endif
  103.  
  104. /* The instance is public. */
  105. gx_device_sun gs_sunview_device = {
  106.     sizeof(gx_device_sun),
  107.     &sun_procs,
  108.     "sunview",
  109.      (int)(8.5*DEFAULT_DPI), (int)(11*DEFAULT_DPI),    /* x and y extent */
  110.      DEFAULT_DPI, DEFAULT_DPI,    /* x and y density */
  111.     no_margins,
  112.         /* Following parameters are initialized for monochrome */
  113.     0,            /* has color */
  114.     1,            /* max r-g-b value */
  115.     1,            /* bits per color pixel */
  116.      0,            /* connection not initialized */
  117.         /* End of monochrome/color parameters */
  118. };
  119.  
  120. #define x_device gs_sunview_device    /* just an abbreviation */
  121.  
  122. /* Macro for casting gx_device argument */
  123. #define xdev ((gx_device_sun *)dev)
  124.  
  125. /* Macro to validate arguments */
  126. #define check_rect()\
  127.     if ( w <= 0 || h <= 0 ) return 0;\
  128.     if ( x < 0 || x > xdev->width - w || y < 0 || y > xdev->height - h )\
  129.         return -1
  130.  
  131. void
  132. sun_window_create(int *argc_ptr, char **argv)
  133. {
  134.     x_device.frame =
  135.         window_create(NULL, FRAME, FRAME_LABEL, "ghostscript",
  136.                     FRAME_ARGC_PTR_ARGV, argc_ptr, argv, 0);
  137. }
  138.  
  139. int
  140. sun_open(register gx_device *dev)
  141. {
  142. #ifdef DEBUG
  143. if ( gs_debug['X'] )
  144.     { extern int _Xdebug;
  145.       _Xdebug = 1;
  146.     }
  147. #endif
  148.     if (xdev->frame == (Frame)0)
  149.         xdev->frame =
  150.         window_create(NULL, FRAME, FRAME_LABEL, "ghostscript", 0);
  151.     xdev->canvas = window_create(xdev->frame, CANVAS,
  152.             CANVAS_AUTO_EXPAND,        FALSE,
  153.             CANVAS_AUTO_SHRINK,        FALSE,
  154.             CANVAS_WIDTH,            xdev->width,
  155.             CANVAS_HEIGHT,            xdev->height,
  156.             WIN_VERTICAL_SCROLLBAR,        scrollbar_create(0),
  157.             WIN_HORIZONTAL_SCROLLBAR,    scrollbar_create(0),
  158.             0);
  159.     xdev->pw = canvas_pixwin(xdev->canvas);
  160.     window_set(xdev->frame, WIN_SHOW, TRUE, 0);
  161.     (void) notify_do_dispatch();
  162.     (void) notify_dispatch();
  163.     return 0;
  164. }
  165.  
  166. /* Close the device. */
  167. int
  168. sun_close(gx_device *dev)
  169. {    window_destroy(xdev->frame);
  170.     xdev->frame = (Frame)0;
  171.     xdev->canvas = (Canvas)0;
  172.     xdev->pw = (Pixwin *)0;
  173.     return 0;
  174. }
  175.  
  176. /* Synchronize the display with the commands already given */
  177. int
  178. sun_sync(register gx_device *dev)
  179. {    (void) notify_dispatch();
  180.     return 0;
  181. }
  182.  
  183. /* Fill a rectangle with a color. */
  184. int
  185. sun_fill_rectangle(register gx_device *dev,
  186.   int x, int y, int w, int h, gx_color_index color)
  187. {    check_rect();
  188.     if ( color != gx_no_color_index )
  189.         pw_write(xdev->pw, x, y, w, h, (color ? PIX_CLR : PIX_SET),
  190.             (Pixrect *)0, 0, 0);
  191.     (void) notify_dispatch();
  192.     return 0;
  193. }
  194.  
  195. /* Copy a monochrome bitmap. */
  196. int
  197. sun_copy_mono(register gx_device *dev, byte *base, int sourcex, int raster,
  198.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  199. {    static int optab[3][3] = {
  200.         PIX_DST,    PIX_SRC|PIX_DST, PIX_NOT(PIX_SRC)&PIX_DST,
  201.         PIX_NOT(PIX_SRC)|PIX_DST, PIX_SET,    PIX_NOT(PIX_SRC),
  202.         PIX_SRC&PIX_DST,    PIX_SRC,    PIX_CLR,
  203.     };
  204.     int op;
  205.     register int i;
  206.     extern struct pixrectops mem_ops;
  207.  
  208.     check_rect();
  209.     if ((op = optab[zero+1][one+1]) == PIX_DST)
  210.         return 0;
  211.     xdev->pr.pr_ops = &mem_ops;
  212.     xdev->pr.pr_width = w + sourcex + 8;
  213.     xdev->pr.pr_height = h;
  214.     xdev->pr.pr_depth = 1;
  215.     xdev->pr.pr_data = (caddr_t)&(xdev->mpr);
  216. #if arch_is_big_endian            /* otherwise must swap order */
  217.     if ((raster & 1) == 0)
  218.        {    xdev->mpr.md_linebytes = raster;
  219.         xdev->mpr.md_image = (short *)((int)base & ~1);
  220.         pw_write(xdev->pw, x, y, w, h, op, &(xdev->pr), 
  221.             ((int)base & 1) ? sourcex + 8 : sourcex, 0);
  222.        }
  223.     else
  224. #endif
  225.        {    xdev->pr.pr_height = 1;
  226.         for (i = 0; i < h; i++) {
  227.             xdev->mpr.md_linebytes = raster;
  228. #if arch_is_big_endian
  229.             xdev->mpr.md_image = (short *)((int)base & ~1);
  230.             pw_write(xdev->pw, x, y, w, 1, op, &(xdev->pr), 
  231.                 ((int)base & 1) ? sourcex + 8 : sourcex, 0);
  232. #else                    /* swap bits & bytes */
  233.                {    byte *buf_ptr = xdev->swap_buf;
  234.                 byte *src_ptr = base + (sourcex >> 3);
  235.                 register int j;
  236.                 int jend = sourcex + w;
  237.                 xdev->mpr.md_image = (short *)buf_ptr;
  238.                 for ( j = sourcex & ~7; j < jend; j += 16 )
  239.                    {    *buf_ptr = reverse_bits[*src_ptr];
  240.                     buf_ptr[1] = reverse_bits[src_ptr[1]];
  241.                     buf_ptr += 2;
  242.                     src_ptr += 2;
  243.                    }
  244.                 pw_write(xdev->pw, x, y, w, 1, op,
  245.                      &(xdev->pr), sourcex & 7, 0);
  246.                }
  247. #endif
  248.             base += raster;
  249.             y++;
  250.         }
  251.        }
  252.     (void) notify_dispatch();
  253.     return 0;
  254. }
  255.  
  256. /* Copy a "color" bitmap.  Since "color" is the same as monochrome, */
  257. /* this just reduces to copying a monochrome bitmap. */
  258. int
  259. sun_copy_color(register gx_device *dev, byte *base, int sourcex, int raster,
  260.   int x, int y, int w, int h)
  261. {    return sun_copy_mono(dev, base, sourcex, raster, x, y, w, h,
  262.       (gx_color_index)0, (gx_color_index)1);
  263. }
  264.  
  265. /* Draw a line */
  266. int
  267. sun_draw_line(register gx_device *dev,
  268.   int x0, int y0, int x1, int y1, gx_color_index color)
  269. {    if ( color != gx_no_color_index )
  270.         pw_vector(xdev->pw, x0, y0, x1, y1,
  271.               (color ? PIX_CLR : PIX_SET), 0);
  272.     (void) notify_dispatch();
  273.     return 0;
  274. }
  275.