home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / UsingPDF / GhostScript / source / gs5.10 / gdevxalt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-21  |  19.5 KB  |  716 lines

  1. /* Copyright (C) 1994, 1995, 1996, 1997 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. /* gdevxalt.c */
  20. /* Alternative X Windows drivers for help in driver debugging */
  21. #include "gx.h"            /* for gx_bitmap; includes std.h */
  22. #include "math_.h"
  23. #include "memory_.h"
  24. #include "x_.h"
  25. #include "gserrors.h"
  26. #include "gsparam.h"
  27. #include "gxdevice.h"
  28. #include "gdevx.h"
  29.  
  30. extern gx_device_X gs_x11_device;
  31.  
  32. /*
  33.  * Define a forwarding device with a cache for the first 16 colors,
  34.  * which avoids all of the time-consuming color mapping calls for
  35.  * the black-and-white, 2-bit gray, and CMYK devices defined here.
  36.  */
  37. typedef struct {
  38.   gx_device_forward_common;
  39.   gx_color_index color_cache[16];
  40. } gx_device_X_wrapper;
  41.  
  42. /* ---------------- Generic procedures ---------------- */
  43.  
  44. /* Forward declarations */
  45. private int get_dev_target(P2(gx_device **, gx_device *));
  46. #define set_dev_target(tdev, dev)\
  47.   do {\
  48.     int code_ = get_dev_target(&tdev, dev);\
  49.     if ( code_ < 0 )\
  50.       return code_;\
  51.   } while (0)
  52. private int get_target_info(P1(gx_device *));
  53. private gx_color_index x_alt_map_color(P2(gx_device *, gx_color_index));
  54.  
  55. /* Clear the color mapping cache. */
  56. private void
  57. x_clear_color_cache(gx_device /*gx_device_X_wrapper*/ *dev)
  58. {    gx_device_X_wrapper *xdev = (gx_device_X_wrapper *)dev;
  59.     int i;
  60.  
  61.     for ( i = 0; i < countof(xdev->color_cache); ++i )
  62.       xdev->color_cache[i] = gx_no_color_index;
  63. }
  64.  
  65. /* "Wrappers" for driver procedures */
  66.  
  67. private int
  68. x_wrap_open(gx_device *dev)
  69. {    gx_device *tdev;
  70.     int rcode, code;
  71.  
  72.     set_dev_target(tdev, dev);
  73.     rcode = (*dev_proc(tdev, open_device))(tdev);
  74.     if ( rcode < 0 )
  75.       return rcode;
  76.     tdev->is_open = true;
  77.     code = get_target_info(dev);
  78.     return (code < 0 ? code : rcode);
  79. }
  80.  
  81. private int
  82. x_forward_sync_output(gx_device *dev)
  83. {    gx_device *tdev;
  84.  
  85.     set_dev_target(tdev, dev);
  86.     return (*dev_proc(tdev, sync_output))(tdev);
  87. }
  88.  
  89. private int
  90. x_forward_output_page(gx_device *dev, int num_copies, int flush)
  91. {    gx_device *tdev;
  92.  
  93.     set_dev_target(tdev, dev);
  94.     return (*dev_proc(tdev, output_page))(tdev, num_copies, flush);
  95. }
  96.  
  97. private int
  98. x_wrap_close(gx_device *dev)
  99. {    gx_device *tdev;
  100.     /* If Ghostscript is exiting, we might have closed the */
  101.     /* underlying x11 device already.... */
  102.     int code;
  103.  
  104.     set_dev_target(tdev, dev);
  105.     if ( tdev->is_open )
  106.       {    code = (*dev_proc(tdev, close_device))(tdev);
  107.         x_clear_color_cache(dev);
  108.         if ( code < 0 )
  109.           return code;
  110.         tdev->is_open = false;
  111.       }
  112.     else
  113.       code = 0;
  114.     return code;
  115. }
  116.  
  117. private int
  118. x_wrap_map_color_rgb(gx_device *dev, gx_color_index color,
  119.   gx_color_value prgb[3])
  120. {    gx_device *tdev;
  121.  
  122.     set_dev_target(tdev, dev);
  123.     return (*dev_proc(tdev, map_color_rgb))(tdev,
  124.                         x_alt_map_color(dev, color),
  125.                         prgb);
  126. }
  127.  
  128. private int
  129. x_wrap_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  130.   gx_color_index color)
  131. {    gx_device *tdev;
  132.  
  133.     set_dev_target(tdev, dev);
  134.     return (*dev_proc(tdev, fill_rectangle))(tdev, x, y, w, h,
  135.                          x_alt_map_color(dev, color));
  136. }
  137.  
  138. private int
  139. x_wrap_copy_mono(gx_device *dev,
  140.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  141.   int x, int y, int w, int h,
  142.   gx_color_index zero, gx_color_index one)
  143. {    gx_device *tdev;
  144.  
  145.     set_dev_target(tdev, dev);
  146.     return (*dev_proc(tdev, copy_mono))(tdev, base, sourcex, raster, id,
  147.                         x, y, w, h,
  148.                         x_alt_map_color(dev, zero),
  149.                         x_alt_map_color(dev, one));
  150.  
  151. }
  152.  
  153. private int
  154. x_wrap_copy_color(gx_device *dev, const byte *base, int sourcex,
  155.   int raster, gx_bitmap_id id, int x, int y, int w, int h)
  156. {    gx_device *tdev;
  157. #define mapped_bytes 480    /* must be a multiple of 3 & 4 */
  158.     int depth_bytes, source_bits;
  159.     int block_w, block_h;
  160.     int xblock, yblock;
  161.     byte mapped[mapped_bytes];
  162.  
  163.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  164.     set_dev_target(tdev, dev);
  165.     /* Device pixels must be an integral number of bytes. */
  166.     if ( tdev->color_info.depth & 7 )
  167.       return gx_default_copy_color(dev, base, sourcex, raster, id,
  168.                        x, y, w, h);
  169.     depth_bytes = tdev->color_info.depth >> 3;
  170.     source_bits = dev->color_info.depth;
  171.     { int mapped_pixels = mapped_bytes / depth_bytes;
  172.       if ( w > mapped_pixels >> 1 )
  173.         block_w = min(w, mapped_pixels), block_h = 1;
  174.       else
  175.         block_w = w, block_h = mapped_pixels / w;
  176.     }
  177.     for ( yblock = y; yblock < y + h; yblock += block_h )
  178.       for ( xblock = x; xblock < x + w; xblock += block_w ) {
  179.         byte *p = mapped;
  180.         int xend = min(xblock + block_w, x + w);
  181.         int yend = min(yblock + block_h, y + h);
  182.         int xcur, ycur;
  183.         int code;
  184.  
  185.         for ( ycur = yblock; ycur < yend; ++ycur )
  186.           for ( xcur = xblock; xcur < xend; ++xcur ) {
  187.         int sbit = (xcur - x + sourcex) * source_bits;
  188.         uint sbyte =
  189.           base[(ycur - y) * raster + (sbit >> 3)];
  190.         uint spixel =
  191.           ((sbyte << (sbit & 7)) & 0xff) >> (8 - source_bits);
  192.         gx_color_index cindex =
  193.           ((gx_device_X_wrapper *)dev)->color_cache[spixel];
  194.  
  195.         if ( cindex == gx_no_color_index )
  196.           cindex = x_alt_map_color(dev, spixel);
  197.         switch ( depth_bytes ) {
  198.           case 4: *p++ = (byte)(cindex >> 24);
  199.           case 3: *p++ = (byte)(cindex >> 16);
  200.           case 2: *p++ = (byte)(cindex >> 8);
  201.           default /*case 1*/: *p++ = (byte)cindex;
  202.         }
  203.           }
  204.         code = (*dev_proc(tdev, copy_color))
  205.           (tdev, mapped, 0, (xend - xblock) * depth_bytes, gx_no_bitmap_id,
  206.            xblock, yblock, xend - xblock, yend - yblock);
  207.         if ( code < 0 )
  208.           return code;
  209.       }
  210.     return 0;
  211. }
  212.  
  213.  
  214. private int
  215. x_forward_copy_color(gx_device *dev, const byte *base, int sourcex,
  216.              int raster, gx_bitmap_id id, int x, int y, int w, int h)
  217. {    gx_device *tdev;
  218.  
  219.     set_dev_target(tdev, dev);
  220.     return (*dev_proc(tdev, copy_color))(tdev, base, sourcex, raster, id,
  221.                          x, y, w, h);
  222. }
  223.  
  224. private int
  225. x_forward_get_bits(gx_device *dev, int y, byte *str, byte **actual_data)
  226. {    gx_device *tdev;
  227.  
  228.     set_dev_target(tdev, dev);
  229.     return (*dev_proc(tdev, get_bits))(tdev, y, str, actual_data);
  230. }
  231.  
  232. private int
  233. x_wrap_get_bits(gx_device *dev, int y, byte *str, byte **actual_data)
  234. {    int depth = dev->color_info.depth;
  235.     gx_device *tdev;
  236.     int width;
  237.     int sdepth;
  238.     byte smask;
  239.     uint dsize;
  240.     gs_memory_t *mem =
  241.       (dev->memory == 0 ? &gs_memory_default : dev->memory);
  242.     byte *row;
  243.     byte *base;
  244.     int code;
  245.     gx_color_index pixel_in = gx_no_color_index;
  246.     gx_color_index pixel_out;
  247.     int xi;
  248.     int sbit;
  249.     declare_line_accum(str, depth, 0);
  250.  
  251.     set_dev_target(tdev, dev);
  252.     width = tdev->width;
  253.     sdepth = tdev->color_info.depth;
  254.     smask = (sdepth <= 8 ? (1 << sdepth) - 1 : 0xff);
  255.     dsize = (width * sdepth + 7) / 8;
  256.     row = gs_alloc_bytes(mem, dsize, "x_wrap_get_bits");
  257.     if ( row == 0 )
  258.       return_error(gs_error_VMerror);
  259.     code = (*dev_proc(tdev, get_bits))(tdev, y, row, &base);
  260.     if ( code < 0 )
  261.       goto gx;
  262.     for ( sbit = 0, xi = 0; xi < width; sbit += sdepth, ++xi )
  263.       {    const byte *sptr = base + (sbit >> 3);
  264.         gx_color_index pixel;
  265.         gx_color_value rgb[3];
  266.         int i;
  267.  
  268.         if ( sdepth <= 8 )
  269.           pixel = (*sptr >> (8 - sdepth - (sbit & 7))) & smask;
  270.         else
  271.           { pixel = 0;
  272.             for ( i = 0; i < depth; i += 8, ++sptr )
  273.               pixel = (pixel << 8) + *sptr;
  274.           }
  275.         if ( pixel != pixel_in )
  276.           { (*dev_proc(tdev, map_color_rgb))(tdev, pixel, rgb);
  277.             pixel_in = pixel;
  278.             pixel_out = (*dev_proc(dev, map_rgb_color))(dev, rgb[0], rgb[1], rgb[2]);
  279.           }
  280.         line_accum(pixel_out, depth);
  281.       }
  282.     line_accum_store(depth);
  283. gx:    gs_free_object(mem, row, "x_wrap_get_bits");
  284.     *actual_data = str;
  285.     return code;
  286. }
  287.  
  288. private int
  289. x_wrap_get_params(gx_device *dev, gs_param_list *plist)
  290. {    gx_device *tdev;
  291.     /* We assume that a get_params call has no side effects.... */
  292.     gx_device_X save_dev;
  293.     int code;
  294.  
  295.     set_dev_target(tdev, dev);
  296.     save_dev = *(gx_device_X *)tdev;
  297.     if ( tdev->is_open )
  298.       tdev->color_info = dev->color_info;
  299.     tdev->dname = dev->dname;
  300.     code = (*dev_proc(tdev, get_params))(tdev, plist);
  301.     *(gx_device_X *)tdev = save_dev;
  302.     return code;
  303. }
  304.  
  305. private int
  306. x_wrap_put_params(gx_device *dev, gs_param_list *plist)
  307. {    gx_device *tdev;
  308.     gx_device_color_info cinfo;
  309.     const char *dname;
  310.     int rcode, code;
  311.  
  312.     set_dev_target(tdev, dev);
  313.     /*
  314.      * put_params will choke if we simply feed it the output of
  315.      * get_params; we have to substitute color_info the same way.
  316.      */
  317.     cinfo = tdev->color_info;
  318.     dname = tdev->dname;
  319.     tdev->color_info = dev->color_info;
  320.     tdev->dname = dev->dname;
  321.     rcode = (*dev_proc(tdev, put_params))(tdev, plist);
  322.     tdev->color_info = cinfo;
  323.     tdev->dname = dname;
  324.     if ( rcode < 0 )
  325.       return rcode;
  326.     code = get_target_info(dev);
  327.     return (code < 0 ? code : rcode);
  328. }
  329.  
  330. /* Internal procedures */
  331.  
  332. /* Get the target, creating it if necessary. */
  333. private int
  334. get_dev_target(gx_device **ptdev, gx_device *dev)
  335. {    gx_device *tdev = ((gx_device_forward *)dev)->target;
  336.  
  337.     if ( tdev == 0 )
  338.       {    /* Create or link to an X device instance. */
  339.         if ( dev->memory == 0 )    /* static instance */
  340.           tdev = (gx_device *)&gs_x11_device;
  341.         else
  342.           {    tdev = (gx_device *)gs_alloc_bytes(dev->memory,
  343.                         (gs_x11_device).params_size,
  344.                         "dev_target");
  345.             if ( tdev == 0 )
  346.               return_error(gs_error_VMerror);
  347.             *(gx_device_X *)tdev = gs_x11_device;
  348.             tdev->memory = dev->memory;
  349.             tdev->is_open = false;
  350.           }
  351.         gx_device_fill_in_procs(tdev);
  352.         ((gx_device_forward *)dev)->target = tdev;
  353.         x_clear_color_cache(dev);
  354.       }
  355.     *ptdev = tdev;
  356.     return 0;
  357. }
  358.  
  359. /* Copy parameters back from the target. */
  360. private int
  361. get_target_info(gx_device *dev)
  362. {    gx_device *tdev;
  363.  
  364.     set_dev_target(tdev, dev);
  365.  
  366. #define copy(m) dev->m = tdev->m;
  367. #define copy2(m) copy(m[0]); copy(m[1])
  368. #define copy4(m) copy2(m); copy(m[2]); copy(m[3])
  369.  
  370.     copy(width); copy(height);
  371.     copy2(MediaSize);
  372.     copy4(ImagingBBox);
  373.     copy(ImagingBBox_set);
  374.     copy2(HWResolution);
  375.     copy2(MarginsHWResolution);
  376.     copy2(Margins);
  377.     copy4(HWMargins);
  378.     if ( dev->color_info.num_components == 3 )
  379.       copy(color_info);
  380.  
  381. #undef copy4
  382. #undef copy2
  383. #undef copy
  384.  
  385.     x_clear_color_cache(dev);
  386.     return 0;
  387. }
  388.  
  389. /* Map a fake CMYK or black/white color to a real X color if necessary. */
  390. private gx_color_index
  391. x_alt_map_color(gx_device *dev, gx_color_index color)
  392. {    gx_device *tdev;
  393.     gx_color_value r, g, b;
  394.     gx_color_index cindex;
  395.  
  396.     if ( color == gx_no_color_index )
  397.       return color;
  398.     if ( color < 16 )
  399.       { cindex = ((gx_device_X_wrapper *)dev)->color_cache[color];
  400.         if ( cindex != gx_no_color_index )
  401.           return cindex;
  402.       }
  403.     set_dev_target(tdev, dev);
  404.     switch ( dev->color_info.num_components )
  405.       {
  406.       case 3:    /* RGB, this is the real thing (possibly + alpha) */
  407.         return color & 0xffffff;
  408.       case 4:    /* CMYK */
  409.         if ( color & 1 )
  410.           r = g = b = 0;
  411.         else
  412.           { r = (color & 8 ? 0 : gx_max_color_value);
  413.         g = (color & 4 ? 0 : gx_max_color_value);
  414.         b = (color & 2 ? 0 : gx_max_color_value);
  415.           }
  416.         break;
  417.       default /*case 1*/:
  418.         if ( dev->color_info.depth == 1 )
  419.           {     /* 0 = white, 1 = black */
  420.         r = g = b = (color ? 0 : gx_max_color_value);
  421.           }
  422.         else
  423.           { r = g = b =
  424.           color * gx_max_color_value / dev->color_info.max_gray;
  425.           }
  426.         break;
  427.       }
  428.     cindex = (*dev_proc(tdev, map_rgb_color))(tdev, r, g, b);
  429.     if ( color < 16 )
  430.       ((gx_device_X_wrapper *)dev)->color_cache[color] = cindex;
  431.     return cindex;
  432. }
  433.  
  434. /* ---------------- CMYK procedures ---------------- */
  435.  
  436. /* Device procedures */
  437. private dev_proc_map_rgb_color(x_cmyk_map_rgb_color);
  438. private dev_proc_map_cmyk_color(x_cmyk_map_cmyk_color);
  439.  
  440. /* The device descriptor */
  441. private gx_device_procs x_cmyk_procs = {
  442.     x_wrap_open,
  443.     gx_forward_get_initial_matrix,
  444.     x_forward_sync_output,
  445.     x_forward_output_page,
  446.     x_wrap_close,
  447.     x_cmyk_map_rgb_color,
  448.     x_wrap_map_color_rgb,
  449.     x_wrap_fill_rectangle,
  450.     gx_default_tile_rectangle,
  451.     x_wrap_copy_mono,
  452.     x_wrap_copy_color,
  453.     gx_default_draw_line,
  454.     x_wrap_get_bits,
  455.     x_wrap_get_params,
  456.     x_wrap_put_params,
  457.     x_cmyk_map_cmyk_color,
  458.     gx_forward_get_xfont_procs,
  459.     gx_forward_get_xfont_device,
  460.     NULL,            /* map_rgb_alpha_color */
  461.     gx_forward_get_page_device,
  462.     gx_forward_get_alpha_bits,
  463.     NULL            /* copy_alpha */
  464. };
  465.  
  466. /* The instance is public. */
  467. gx_device_X_wrapper far_data gs_x11cmyk_device = {
  468.     std_device_dci_body(gx_device_X_wrapper, &x_cmyk_procs, "x11cmyk",
  469.       FAKE_RES*85/10, FAKE_RES*11,    /* x and y extent (nominal) */
  470.       FAKE_RES, FAKE_RES,    /* x and y density (nominal) */
  471.       4, 4, 1, 1, 2, 2),
  472.     { 0 },            /* std_procs */
  473.     0            /* target */
  474. };
  475.  
  476. /* Device procedures */
  477.  
  478. private gx_color_index
  479. x_cmyk_map_rgb_color(gx_device *dev,
  480.   gx_color_value r, gx_color_value g, gx_color_value b)
  481. {    /* This should never be called! */
  482.     return gx_no_color_index;
  483. }
  484.  
  485. private gx_color_index
  486. x_cmyk_map_cmyk_color(gx_device *dev,
  487.   gx_color_value c, gx_color_value m, gx_color_value y,
  488.   gx_color_value k)
  489. {    return
  490.       (gx_color_index)
  491.         (((c >> (gx_color_value_bits - 4)) & 8) |
  492.          ((m >> (gx_color_value_bits - 3)) & 4) |
  493.          ((y >> (gx_color_value_bits - 2)) & 2) |
  494.          ((k >> (gx_color_value_bits - 1)) & 1));
  495. }
  496.  
  497. /* ---------------- Black-and-white procedures ---------------- */
  498.  
  499. /* The device descriptor */
  500. private gx_device_procs x_mono_procs = {
  501.     x_wrap_open,
  502.     gx_forward_get_initial_matrix,
  503.     x_forward_sync_output,
  504.     x_forward_output_page,
  505.     x_wrap_close,
  506.     gx_default_b_w_map_rgb_color,
  507.     x_wrap_map_color_rgb,
  508.     x_wrap_fill_rectangle,
  509.     gx_default_tile_rectangle,
  510.     x_wrap_copy_mono,
  511.     gx_default_copy_color,    /* this is fast for the 1-bit case */
  512.     gx_default_draw_line,
  513.     x_wrap_get_bits,
  514.     x_wrap_get_params,
  515.     x_wrap_put_params,
  516.     gx_default_map_cmyk_color,
  517.     gx_forward_get_xfont_procs,
  518.     gx_forward_get_xfont_device,
  519.     NULL,            /* map_rgb_alpha_color */
  520.     gx_forward_get_page_device,
  521.     gx_forward_get_alpha_bits,
  522.     NULL            /* copy_alpha */
  523. };
  524.  
  525. /* The instance is public. */
  526. gx_device_X_wrapper far_data gs_x11mono_device = {
  527.     std_device_dci_body(gx_device_X_wrapper, &x_mono_procs, "x11mono",
  528.       FAKE_RES*85/10, FAKE_RES*11,    /* x and y extent (nominal) */
  529.       FAKE_RES, FAKE_RES,    /* x and y density (nominal) */
  530.       1, 1, 1, 0, 2, 0),
  531.     { 0 },            /* std_procs */
  532.     0            /* target */
  533. };
  534.  
  535. /* ---------------- 2-bit gray-scale procedures ---------------- */
  536.  
  537. /* The device descriptor */
  538. private gx_device_procs x_gray2_procs = {
  539.     x_wrap_open,
  540.     gx_forward_get_initial_matrix,
  541.     x_forward_sync_output,
  542.     x_forward_output_page,
  543.     x_wrap_close,
  544.     gx_default_gray_map_rgb_color,
  545.     x_wrap_map_color_rgb,
  546.     x_wrap_fill_rectangle,
  547.     gx_default_tile_rectangle,
  548.     x_wrap_copy_mono,
  549.     x_wrap_copy_color,
  550.     gx_default_draw_line,
  551.     x_wrap_get_bits,
  552.     x_wrap_get_params,
  553.     x_wrap_put_params,
  554.     gx_default_map_cmyk_color,
  555.     gx_forward_get_xfont_procs,
  556.     gx_forward_get_xfont_device,
  557.     NULL,            /* map_rgb_alpha_color */
  558.     gx_forward_get_page_device,
  559.     gx_forward_get_alpha_bits,
  560.     NULL            /* copy_alpha */
  561. };
  562.  
  563. /* The instance is public. */
  564. gx_device_X_wrapper far_data gs_x11gray2_device = {
  565.     std_device_dci_body(gx_device_X_wrapper, &x_gray2_procs, "x11gray2",
  566.       FAKE_RES*85/10, FAKE_RES*11,    /* x and y extent (nominal) */
  567.       FAKE_RES, FAKE_RES,    /* x and y density (nominal) */
  568.       1, 2, 3, 0, 4, 0),
  569.     { 0 },            /* std_procs */
  570.     0            /* target */
  571. };
  572.  
  573. /* ---------------- Alpha procedures ---------------- */
  574.  
  575. /* Device procedures */
  576. private dev_proc_map_color_rgb(x_alpha_map_color_rgb);
  577. private dev_proc_map_rgb_alpha_color(x_alpha_map_rgb_alpha_color);
  578. private dev_proc_get_alpha_bits(x_alpha_get_alpha_bits);
  579. private dev_proc_copy_alpha(x_alpha_copy_alpha);
  580.  
  581. /* The device descriptor */
  582. private gx_device_procs x_alpha_procs = {
  583.     x_wrap_open,
  584.     gx_forward_get_initial_matrix,
  585.     x_forward_sync_output,
  586.     x_forward_output_page,
  587.     x_wrap_close,
  588.     gx_forward_map_rgb_color,
  589.     x_alpha_map_color_rgb,
  590.     x_wrap_fill_rectangle,
  591.     gx_default_tile_rectangle,
  592.     x_wrap_copy_mono,
  593.     x_forward_copy_color,
  594.     gx_default_draw_line,
  595.     x_forward_get_bits,
  596.     x_wrap_get_params,
  597.     x_wrap_put_params,
  598.     gx_forward_map_cmyk_color,
  599.     gx_forward_get_xfont_procs,
  600.     gx_forward_get_xfont_device,
  601.     x_alpha_map_rgb_alpha_color,
  602.     gx_forward_get_page_device,
  603.     x_alpha_get_alpha_bits,
  604.     /*gx_default_copy_alpha*/    x_alpha_copy_alpha
  605. };
  606.  
  607. /* The instance is public. */
  608. gx_device_X_wrapper far_data gs_x11alpha_device = {
  609.     std_device_dci_body(gx_device_X_wrapper, &x_alpha_procs, "x11alpha",
  610.       FAKE_RES*85/10, FAKE_RES*11,    /* x and y extent (nominal) */
  611.       FAKE_RES, FAKE_RES,    /* x and y density (nominal) */
  612.       3, 32, 255, 255, 256, 256),
  613.     { 0 },            /* std_procs */
  614.     0            /* target */
  615. };
  616.  
  617. /* Device procedures */
  618.  
  619. /* We encode a complemented alpha value in the top 8 bits of the */
  620. /* device color. */
  621. private int
  622. x_alpha_map_color_rgb(gx_device *dev, gx_color_index color,
  623.   gx_color_value prgb[3])
  624. {    return gx_forward_map_color_rgb(dev, color & 0xffffff, prgb);
  625. }
  626. private gx_color_index
  627. x_alpha_map_rgb_alpha_color(gx_device *dev,
  628.   gx_color_value r, gx_color_value g, gx_color_value b, gx_color_value alpha)
  629. {    gx_color_index color = gx_forward_map_rgb_color(dev, r, g, b);
  630.     byte abyte = alpha >> (gx_color_value_bits - 8);
  631.  
  632.     return (abyte == 0 ? 0xff000000 :
  633.         ((gx_color_index)(abyte ^ 0xff) << 24) + color);
  634. }
  635.  
  636. private int
  637. x_alpha_get_alpha_bits(gx_device *dev, graphics_object_type type)
  638. {    return 4;
  639. }
  640.  
  641. private int
  642. x_alpha_copy_alpha(gx_device *dev, const unsigned char *base, int sourcex,
  643.            int raster, gx_bitmap_id id, int x, int y, int w, int h,
  644.            gx_color_index color, int depth)
  645. {    gx_device *tdev;
  646.     int xi, yi;
  647.     const byte *row = base;
  648.     gx_color_index base_color = color & 0xffffff;
  649.     /* We fake alpha by interpreting it as saturation, i.e., */
  650.     /* alpha = 0 is white, alpha = 15/15 is the full color. */
  651.     gx_color_value rgb[3];
  652.     gx_color_index shades[16];
  653.     int i;
  654.  
  655. /**************** PATCH for measuring rasterizer speed ****************/
  656. /*if ( 1 ) return 0;*/
  657.  
  658.     set_dev_target(tdev, dev);
  659.     for ( i = 0; i < 15; ++i )
  660.       shades[i] = gx_no_color_index;
  661.     shades[15] = base_color;
  662.     (*dev_proc(tdev, map_color_rgb))(tdev, base_color, rgb);
  663.     /* Do the copy operation pixel-by-pixel. */
  664.     /* For the moment, if the base color has alpha in it, we ignore it. */
  665.     for ( yi = y; yi < y + h; row += raster, ++yi )
  666.       {    int prev_x = x;
  667.         gx_color_index prev_color = gx_no_color_index;
  668.         uint prev_alpha = 0x10;        /* not a possible value */
  669.  
  670.         for ( xi = x; xi < x + w; ++xi )
  671.           {    int sx = sourcex + xi - x;
  672.             uint alpha2 = row[sx >> 1];
  673.             uint alpha = (sx & 1 ? alpha2 & 0xf : alpha2 >> 4);
  674.             gx_color_index a_color;
  675.  
  676.             if ( alpha == prev_alpha )
  677.               continue;
  678.             prev_alpha = alpha;
  679.             if ( alpha == 0 )
  680.               a_color = gx_no_color_index;
  681.             else
  682.               while ( (a_color = shades[alpha]) == gx_no_color_index )
  683.               {    /* Map the color now. */
  684. #define make_shade(v, alpha)\
  685.   (gx_max_color_value -\
  686.    ((gx_max_color_value - (v)) * (alpha) / 15))
  687.                 gx_color_value r = make_shade(rgb[0], alpha);
  688.                 gx_color_value g = make_shade(rgb[1], alpha);
  689.                 gx_color_value b = make_shade(rgb[2], alpha);
  690. #undef make_shade
  691.                 a_color = (*dev_proc(tdev, map_rgb_color))(tdev, r, g, b);
  692.                 if ( a_color != gx_no_color_index )
  693.                   {    shades[alpha] = a_color;
  694.                     break;
  695.                   }
  696.                 /* Try a higher saturation.  (We know */
  697.                 /* the fully saturated color exists.) */
  698.                 alpha += (16 - alpha) >> 1;
  699.               }
  700.             if ( a_color != prev_color )
  701.               { if ( prev_color != gx_no_color_index )
  702.                   (*dev_proc(tdev, fill_rectangle))(tdev,
  703.                     prev_x, yi, xi - prev_x, 1,
  704.                     prev_color);
  705.                 prev_x = xi;
  706.                 prev_color = a_color;
  707.               }
  708.           }
  709.         if ( prev_color != gx_no_color_index )
  710.           (*dev_proc(tdev, fill_rectangle))(tdev,
  711.                     prev_x, yi, x + w - prev_x, 1,
  712.                     prev_color);
  713.       }
  714.     return 0;
  715. }
  716.