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

  1. /* Copyright (C) 1991, 1992, 1993 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. /* gxclist.c */
  20. /* Command list writing for Ghostscript. */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gpcheck.h"
  24. #include "gserrors.h"
  25. #include "gxdevice.h"
  26. #include "gxdevmem.h"            /* must precede gxcldev.h */
  27. #include "gxcldev.h"
  28. #include "strimpl.h"
  29. #include "srlx.h"
  30.  
  31. /* Forward declarations of procedures */
  32. private dev_proc_open_device(clist_open);
  33. private dev_proc_output_page(clist_output_page);
  34. private dev_proc_fill_rectangle(clist_fill_rectangle);
  35. private dev_proc_tile_rectangle(clist_tile_rectangle);
  36. private dev_proc_copy_mono(clist_copy_mono);
  37. private dev_proc_copy_color(clist_copy_color);
  38. extern dev_proc_get_bits(clist_get_bits);    /* in gxclread.c */
  39.  
  40. /* The device descriptor */
  41. /* The device template itself is never used, only the procs. */
  42. gx_device_procs gs_clist_device_procs =
  43. {    clist_open,
  44.     gx_forward_get_initial_matrix,
  45.     gx_default_sync_output,
  46.     clist_output_page,
  47.     gx_default_close_device,
  48.     gx_forward_map_rgb_color,
  49.     gx_forward_map_color_rgb,
  50.     clist_fill_rectangle,
  51.     clist_tile_rectangle,
  52.     clist_copy_mono,
  53.     clist_copy_color,
  54.     gx_default_draw_line,
  55.     clist_get_bits,
  56.     gx_forward_get_params,
  57.     gx_forward_put_params,
  58.     gx_forward_map_cmyk_color,
  59.     gx_forward_get_xfont_procs,
  60.     gx_forward_get_xfont_device,
  61.     gx_forward_map_rgb_alpha_color,
  62.     gx_forward_get_page_device
  63. };
  64.  
  65. /* ------ Define the command set and syntax ------ */
  66.  
  67. #ifdef DEBUG
  68. const char *cmd_op_names[16] = { cmd_op_name_strings };
  69. const char *cmd_misc_op_names[16] = { cmd_misc_op_name_strings };
  70. private ulong far_data cmd_op_counts[256];
  71. private ulong cmd_tile_count, cmd_copy_count, cmd_copy_rle_count,
  72.   cmd_delta_tile_count;
  73. private ulong cmd_tile_reset, cmd_tile_found, cmd_tile_added;
  74. private int
  75. count_op(int op)
  76. {    ++cmd_op_counts[op];
  77.     if_debug2('L', ", %s %d\n", cmd_op_names[op >> 4], op & 0xf);
  78.     fflush(dstderr);
  79.     return op;
  80. }
  81. #  define count_add(v, n) (v += (n))
  82. #else
  83. #  define count_op(store_op) store_op
  84. #  define count_add(v, n) DO_NOTHING
  85. #endif
  86. #define count_add1(v) count_add(v, 1)
  87.  
  88. /* Initialize the device state */
  89. private void clist_init_tiles(P1(gx_device_clist *));
  90. private int
  91. clist_open(gx_device *dev)
  92. {    /*
  93.      * The buffer area (data, data_size) holds a tile cache and a
  94.      * set of block range bit masks when both writing and reading.
  95.      * The rest of the space is used for
  96.      * the command buffer and band state bookkeeping when writing,
  97.      * and for the rendering buffer (image device) when reading.
  98.      * For the moment, we divide the space up arbitrarily.
  99.      *
  100.      * This routine requires only data, data_size, target, and mdev
  101.      * to have been set in the device structure, and is idempotent,
  102.      * so it can be used to check whether a given-size buffer
  103.      * is large enough.
  104.      */
  105.     byte *data = cdev->data;
  106.     uint size = cdev->data_size;
  107. #define alloc_data(n) data += (n), size -= (n)
  108.     gx_device *target = cdev->target;
  109.     uint raster, nbands, band;
  110.     gx_clist_state *states;
  111.     ulong state_size;
  112.     cdev->ymin = cdev->ymax = -1;    /* render_init not done yet */
  113.     cdev->tile_data = data;
  114.     cdev->tile_data_size = (size / 5) & -4;    /* arbitrary! */
  115.     alloc_data(cdev->tile_data_size);
  116.     raster = gx_device_raster(target, 1) + sizeof(byte *);
  117.     cdev->band_height = size / raster;
  118.     if ( cdev->band_height == 0 )    /* can't even fit one scan line */
  119.         return_error(gs_error_limitcheck);
  120.     nbands = target->height / cdev->band_height + 1;
  121.     cdev->nbands = nbands;
  122.     if_debug4('l', "[l]width=%d, raster=%d, band_height=%d, nbands=%d\n",
  123.              target->width, raster, cdev->band_height, cdev->nbands);
  124.     state_size = nbands * (ulong)sizeof(gx_clist_state);
  125.     if ( state_size + sizeof(cmd_prefix) + cmd_largest_size + raster + 4 > size )        /* not enough room */
  126.         return_error(gs_error_limitcheck);
  127.     cdev->mdev.base = data;
  128.     cdev->states = states = (gx_clist_state *)data;
  129.     alloc_data((uint)state_size);
  130.     cdev->cbuf = data;
  131.     cdev->cnext = data;
  132.     cdev->cend = data + size;
  133.     cdev->ccls = 0;
  134.     for ( band = 0; band < nbands; band++, states++ )
  135.       *states = cls_initial;
  136. #undef alloc_data
  137.     cdev->tile_band_mask_size = (nbands + 31) / 32 * 4;
  138.     cdev->tile_max_size = cdev->tile_data_size -
  139.         (sizeof(tile_hash) * 2 + sizeof(tile_slot) +
  140.          cdev->tile_band_mask_size);
  141.     clist_init_tiles(cdev);
  142.     return 0;
  143. }
  144.  
  145. /* (Re)initialize the tile cache. */
  146. private void
  147. clist_init_tiles(register gx_device_clist *cldev)
  148. {    gx_clist_state *pcls;
  149.     int i, hc;
  150.     cldev->tile_slot_size =
  151.       sizeof(tile_slot) + cldev->tile_band_mask_size +
  152.       cldev->tile.raster * cldev->tile.size.y;
  153.     cldev->tile_max_count = cldev->tile_data_size /
  154.       (sizeof(tile_hash) * 3 /*(worst case)*/ + cldev->tile_slot_size);
  155.     hc = (cldev->tile_max_count - 1) * 2;
  156.     while ( (hc + 1) & hc ) hc |= hc >> 1;    /* make mask */
  157.     if ( hc >= cldev->tile_max_count * 3 ) hc >>= 1;
  158.     if ( hc > 255 )        /* slot index in set_tile is only 1 byte */
  159.        {    hc = 255;
  160.         if ( cldev->tile_max_count > 200 )
  161.             cldev->tile_max_count = 200;
  162.        }
  163.     cldev->tile_hash_mask = hc;
  164.     hc++;                /* make actual size */
  165.     if_debug5('l', "[l]tile.size=%dx%d, slot_size=%d, max_count=%d, hc=%d\n",
  166.          cldev->tile.size.x, cldev->tile.size.y,
  167.          cldev->tile_slot_size, cldev->tile_max_count, hc);
  168.     cldev->tile_hash_table =
  169.         (tile_hash *)(cldev->tile_data + cldev->tile_data_size) - hc;
  170.     cldev->tile_count = 0;
  171.     memset(cldev->tile_data, 0, cldev->tile_data_size);
  172.     memset(cldev->tile_hash_table, -1, hc * sizeof(tile_hash));
  173.     for ( i = 0, pcls = cldev->states; i < cldev->nbands; i++, pcls++ )
  174.         pcls->tile = &no_tile;
  175.     count_add1(cmd_tile_reset);
  176. }
  177.  
  178. /* Clean up after rendering a page. */
  179. private int
  180. clist_output_page(gx_device *dev, int num_copies, int flush)
  181. {    if ( flush )
  182.        {    rewind(cdev->cfile);
  183.         rewind(cdev->bfile);
  184.         cdev->bfile_end_pos = 0;
  185.        }
  186.     else
  187.        {    fseek(cdev->cfile, 0L, SEEK_END);
  188.         fseek(cdev->bfile, 0L, SEEK_END);
  189.        }
  190.     return clist_open(dev);        /* reinitialize */
  191. }
  192.  
  193. /* Print statistics. */
  194. #ifdef DEBUG
  195. void
  196. cmd_print_stats(void)
  197. {    int ci, cj;
  198.     dprintf4("[l]counts: tile = %ld, copy = %ld, copy_rle = %ld, delta = %ld\n",
  199.              cmd_tile_count, cmd_copy_count, cmd_copy_rle_count, cmd_delta_tile_count);
  200.     dprintf3("           reset = %ld, found = %ld, added = %ld\n",
  201.              cmd_tile_reset, cmd_tile_found, cmd_tile_added);
  202.     for ( ci = 0; ci < 0x100; ci += 0x10 )
  203.        {    dprintf1("[l]  %s =", cmd_op_names[ci >> 4]);
  204.         for ( cj = ci; cj < ci + 0x10; cj++ )
  205.             dprintf1(" %ld", cmd_op_counts[cj]);
  206.         dputs("\n");
  207.        }
  208. }
  209. #endif
  210.  
  211. /* ------ Writing ------ */
  212.  
  213. /* Utilities */
  214.  
  215. #define cmd_set_rect(rect)\
  216.   ((rect).x = x, (rect).y = y,\
  217.    (rect).width = width, (rect).height = height)
  218.  
  219. /* Write out the buffered commands, and reset the buffer. */
  220. private int
  221. cmd_write_buffer(gx_device_clist *cldev)
  222. {    FILE *cfile = cldev->cfile;
  223.     FILE *bfile = cldev->bfile;
  224.     int nbands = cldev->nbands;
  225.     gx_clist_state *pcls;
  226.     int band;
  227.     for ( band = 0, pcls = cldev->states; band < nbands; band++, pcls++ )
  228.     {    const cmd_prefix *cp = pcls->head;
  229.         if ( cp != 0 )
  230.         {    cmd_block cb;
  231.             cb.band = band;
  232.             cb.pos = ftell(cfile);
  233.             if_debug2('l', "[l]writing for band %d at %ld\n",
  234.                   band, cb.pos);
  235.             clist_write(bfile, (const byte *)&cb, sizeof(cb));
  236.             pcls->tail->next = 0;    /* terminate the list */
  237.             for ( ; cp != 0; cp = cp->next )
  238.               clist_write(cfile, (const byte *)(cp + 1), cp->size);
  239.             pcls->head = pcls->tail = 0;
  240.             fputc(cmd_opv_end_run, cfile);
  241.         }
  242.     }
  243.     cldev->cnext = cldev->cbuf;
  244.     cldev->ccls = 0;
  245.     if ( ferror(bfile) || ferror(cfile) )
  246.         return_error(gs_error_ioerror);
  247.     return_check_interrupt(0);
  248. }
  249. /* Export under a different name for gxclread.c */
  250. int
  251. clist_flush_buffer(gx_device_clist *cldev)
  252. {    return cmd_write_buffer(cldev);
  253. }
  254.  
  255. /* Add a command to the appropriate band list, */
  256. /* and allocate space for its data. */
  257. /* Return the pointer to the data area. */
  258. private byte *
  259. cmd_put_op(gx_device_clist *cldev, gx_clist_state *pcls, uint size)
  260. {    byte *dp = cldev->cnext;
  261.     if_debug3('L', "[L]band %d: size=%u, left=%u",
  262.           (int)(pcls - cldev->states),
  263.           size, (uint)(cldev->cend - dp));
  264.     if ( size + (sizeof(cmd_prefix) + 4) > cldev->cend - dp )
  265.       { cmd_write_buffer(cldev);
  266.         return cmd_put_op(cldev, pcls, size);
  267.       }
  268.     if ( cldev->ccls == pcls )
  269.       { /* We're adding another command for the same band. */
  270.         /* Tack it onto the end of the previous one. */
  271.         pcls->tail->size += size;
  272.       }
  273.     else
  274.       { cmd_prefix *cp = (cmd_prefix *)(dp + (((byte *)0 - dp) & 3));
  275.         dp = (byte *)(cp + 1);
  276.         if ( pcls->tail != 0 ) pcls->tail->next = cp;
  277.         else pcls->head = cp;
  278.         pcls->tail = cp;
  279.         cldev->ccls = pcls;
  280.         cp->size = size;
  281.       }
  282.     cldev->cnext = dp + size;
  283.     return dp;
  284. }
  285.  
  286. /* Shorten the last allocated command. */
  287. private void
  288. cmd_shorten_op(gx_device_clist *cldev, gx_clist_state *pcls, uint delta)
  289. {    pcls->tail->size -= delta;
  290.     cldev->cnext -= delta;
  291. }
  292.  
  293. /* Write a variable-size positive integer. */
  294. /* (This works for negative integers also; they are written as though */
  295. /* they were unsigned.) */
  296. #define w1byte(w) (!((w) & ~0x7f))
  297. #define w2byte(w) (!((w) & ~0x3fff))
  298. #define cmd_sizew(w)\
  299.   (w1byte(w) ? 1 : w2byte(w) ? 2 : cmd_w_size((uint)(w)))
  300. #define cmd_sizexy(xy)\
  301.   (w1byte((xy).x | (xy).y) ? 2 :\
  302.    cmd_w_size((uint)(xy).x) + cmd_w_size((uint)(xy).y))
  303. private int near
  304. cmd_w_size(register uint w)
  305. {    register int size = 1;
  306.     while ( w > 0x7f ) w >>= 7, size++;
  307.     return size;
  308. }
  309. #define cmd_putw(w,dp)\
  310.   (w1byte(w) ? (*dp = w, ++dp) :\
  311.    w2byte(w) ? (*dp = (w) | 0x80, dp[1] = (w) >> 7, dp += 2) :\
  312.    (dp = cmd_w_put((uint)(w), dp)))
  313. #define cmd_putxy(xy,dp)\
  314.   (w1byte((xy).x | (xy).y) ? (dp[0] = (xy).x, dp[1] = (xy).y, dp += 2) :\
  315.    (dp = cmd_w_put((uint)(xy).y, cmd_w_put((uint)(xy).x, dp))))
  316. private byte *near
  317. cmd_w_put(register uint w, register byte *dp)
  318. {    while ( w > 0x7f ) *dp++ = w | 0x80, w >>= 7;
  319.     *dp = w;
  320.     return dp + 1;
  321. }
  322.  
  323. /* Write a rectangle. */
  324. private int
  325. cmd_size_rect(register const gx_cmd_rect *prect)
  326. {    return cmd_sizew(prect->x) + cmd_sizew(prect->y) +
  327.         cmd_sizew(prect->width) + cmd_sizew(prect->height);
  328. }
  329. private byte *
  330. cmd_put_rect(register const gx_cmd_rect *prect, register byte *dp)
  331. {    cmd_putw(prect->x, dp);
  332.     cmd_putw(prect->y, dp);
  333.     cmd_putw(prect->width, dp);
  334.     cmd_putw(prect->height, dp);
  335.     return dp;
  336. }
  337.  
  338. /* Write a short bitmap.  1 <= bwidth <= 3. */
  339. private void
  340. cmd_put_short_bits(register byte *dp, register const byte *data,
  341.   int raster, register int bwidth, register int height)
  342. {    while ( --height >= 0 )
  343.        {    switch ( bwidth )
  344.            {
  345.         case 3: dp[2] = data[2];
  346.         case 2: dp[1] = data[1];
  347.         case 1: dp[0] = data[0];
  348.            }
  349.         dp += bwidth, data += raster;
  350.        }
  351. }
  352.  
  353. private int
  354. cmd_write_rect_cmd(gx_device *dev, gx_clist_state *pcls,
  355.   int op, int x, int y, int width, int height)
  356. {    int dx = x - pcls->rect.x;
  357.     int dy = y - pcls->rect.y;
  358.     int dwidth = width - pcls->rect.width;
  359.     int dheight = height - pcls->rect.height;
  360. #define check_ranges_1()\
  361.   ((unsigned)(dx - rmin) <= (rmax - rmin) &&\
  362.    (unsigned)(dy - rmin) <= (rmax - rmin) &&\
  363.    (unsigned)(dwidth - rmin) <= (rmax - rmin))
  364. #define check_ranges()\
  365.   (check_ranges_1() &&\
  366.    (unsigned)(dheight - rmin) <= (rmax - rmin))
  367. #define rmin cmd_min_tiny
  368. #define rmax cmd_max_tiny
  369.     cmd_set_rect(pcls->rect);
  370.     if ( dheight == 0 && check_ranges_1() )
  371.        {    byte *dp = cmd_put_op(cdev, pcls, 2);
  372.         count_op(*dp = op + 0x20 + dwidth - rmin);
  373.         dp[1] = (dx << 4) + dy - (rmin * 0x11);
  374.        }
  375. #undef rmin
  376. #undef rmax
  377. #define rmin cmd_min_short
  378. #define rmax cmd_max_short
  379.     else if ( check_ranges() )
  380.        {    int dh = dheight - cmd_min_tiny;
  381.         byte *dp;
  382.         if ( (unsigned)dh <= cmd_max_tiny - cmd_min_tiny && dh != 0 &&
  383.              dy == 0
  384.            )
  385.            {    op += dh;
  386.             dp = cmd_put_op(cdev, pcls, 3);
  387.            }
  388.         else
  389.            {    dp = cmd_put_op(cdev, pcls, 5);
  390.             dp[3] = dy - rmin;
  391.             dp[4] = dheight - rmin;
  392.            }
  393.         count_op(*dp = op + 0x10);
  394.         dp[1] = dx - rmin;
  395.         dp[2] = dwidth - rmin;
  396.        }
  397.     else
  398.        {    byte *dp = cmd_put_op(cdev, pcls, 1 + cmd_size_rect(&pcls->rect));
  399.         count_op(*dp = op);
  400.         dp = cmd_put_rect(&pcls->rect, dp + 1);
  401.        }
  402.     return 0;
  403. }
  404.  
  405. private void
  406. cmd_put_color(gx_device *dev, gx_clist_state *pcls,
  407.   int op, gx_color_index color)
  408. {    if ( (long)color >= -1 && (long)color <= 13 )
  409.         count_op(*cmd_put_op(cdev, pcls, 1) = op + (int)color + 2);
  410.     else
  411.        {    byte *dp = cmd_put_op(cdev, pcls, 1 + sizeof(color));
  412.         count_op(*dp = op);
  413.         memcpy(dp + 1, &color, sizeof(color));
  414.        }
  415. }
  416. private void
  417. cmd_set_colors(gx_device *dev, gx_clist_state *pcls,
  418.   gx_color_index color0, gx_color_index color1)
  419. {    if ( color0 != pcls->color0 )
  420.        {    cmd_put_color(dev, pcls, cmd_op_set_color0, color0);
  421.         pcls->color0 = color0;
  422.        }
  423.     if ( color1 != pcls->color1 )
  424.        {    cmd_put_color(dev, pcls, cmd_op_set_color1, color1);
  425.         pcls->color1 = color1;
  426.        }
  427. }
  428.  
  429. /* Driver interface */
  430.  
  431. /* Macros for dividing up a single call into bands */
  432. #define BEGIN_RECT\
  433.    {    int yend = y + height;\
  434.     int band_height = cdev->band_height;\
  435.     do\
  436.        {    int band = y / band_height;\
  437.         gx_clist_state *pcls = cdev->states + band;\
  438.         height = band_height - y % band_height;\
  439.         if ( yend - y < height ) height = yend - y;\
  440.            {
  441. #define END_RECT\
  442.            }\
  443.         y += height;\
  444.        }\
  445.     while ( y < yend );\
  446.    }
  447.  
  448. private int
  449. clist_fill_rectangle(gx_device *dev, int x, int y, int width, int height,
  450.   gx_color_index color)
  451. {    fit_fill(dev, x, y, width, height);
  452.     BEGIN_RECT
  453.     if ( color != pcls->color1 )
  454.         cmd_set_colors(dev, pcls, pcls->color0, color);
  455.     cmd_write_rect_cmd(dev, pcls, cmd_op_fill_rect, x, y, width, height);
  456.     END_RECT
  457.     return 0;
  458. }
  459.  
  460. /* Compare unequal tiles.  Return -1 if unrelated, */
  461. /* or 2<=N<=50 for the size of the delta encoding. */
  462. private int
  463. tile_diff(const byte *old_data, const byte *new_data, uint tsize,
  464.   byte _ss *delta)
  465. {    register const ushort *old2, *new2;
  466.     register ushort diff;
  467.     int count;
  468.     register int i;
  469.     byte _ss *pd;
  470.     if ( tsize > 128 ) return -1;
  471.     old2 = (const ushort *)old_data;
  472.     new2 = (const ushort *)new_data;
  473.     count = 0;
  474.     pd = delta + 2;            /* skip slot index */
  475.     for ( i = 0; i < tsize; i += 2, old2++, new2++ )
  476.       if ( (diff = *new2 ^ *old2) != 0 )
  477. #if arch_is_big_endian
  478. #  define i_hi 0
  479. #  define b_0(w) ((w) >> 8)
  480. #  define b_1(w) ((byte)(w))
  481. #else
  482. #  define i_hi 1
  483. #  define b_0(w) ((byte)(w))
  484. #  define b_1(w) ((w) >> 8)
  485. #endif
  486.        {    if ( count == 16 ) return -1;
  487.         if ( diff & 0xff00 )
  488.            {    if ( diff & 0xff )
  489.                 *pd++ = 0x80 + i,
  490.                 *pd++ = b_0(diff),
  491.                 *pd++ = b_1(diff);
  492.             else
  493.                 *pd++ = i + i_hi, *pd++ = diff >> 8;
  494.            }
  495.         else            /* know diff != 0 */
  496.             *pd++ = i + (1 - i_hi), *pd++ = (byte)diff;
  497.         count++;
  498.        }
  499. #undef b_0
  500. #undef b_1
  501. #undef i_hi
  502.     if ( count == 0 )
  503.     {    /* Tiles are identical.  This is highly unusual, */
  504.         /* but not impossible. */
  505.         pd[0] = pd[1] = 0;
  506.         pd += 2;
  507.         count = 1;
  508.     }
  509.     delta[0] = (byte)cmd_op_delta_tile_bits + count - 1;
  510.     return pd - delta;
  511. }
  512.  
  513. /* Handle changing tiles for clist_tile_rectangle. */
  514. /* We put this in a separate routine, even though it is called only once, */
  515. /* to avoid cluttering up the main-line case of tile_rectangle. */
  516. private int
  517. clist_change_tile(gx_device_clist *cldev, gx_clist_state *pcls,
  518.   const gx_tile_bitmap *tile)
  519. {    uint tile_size = tile->raster * tile->size.y;
  520.     tile_slot *old_tile, *new_tile;
  521.     int slot_index;
  522.     /* Look up the tile in the cache. */
  523. top:       {    gx_bitmap_id id = tile->id;
  524.         uint probe = (uint)(id >> 16) + (uint)(id);
  525.         old_tile = pcls->tile;
  526.         for ( ; ; probe += 25 /* semi-random odd # */ )
  527.            {    tile_hash *hptr = cldev->tile_hash_table +
  528.               (probe & cldev->tile_hash_mask);
  529.             if ( (slot_index = hptr->slot_index) < 0 ) /* empty entry */
  530.                {    /* Must change tiles.  Check whether the */
  531.                 /* tile size has changed. */
  532.                 if ( tile->size.x != cldev->tile.size.x ||
  533.                      tile->size.y != cldev->tile.size.y
  534.                    )
  535.                    {    if ( tile->raster !=
  536.                          bitmap_raster(tile->size.x) ||
  537.                          tile_size > cldev->tile_max_size
  538.                        )
  539.                         return -1;
  540.                     cldev->tile = *tile;    /* reset size, raster */
  541.                     clist_init_tiles(cldev);
  542.                     goto top;
  543.                    }
  544.                 if ( cldev->tile_count == cldev->tile_max_count )
  545.                    {    /* Punt. */
  546.                     clist_init_tiles(cldev);
  547.                     goto top;
  548.                    }
  549.                 hptr->slot_index = slot_index =
  550.                   cldev->tile_count++;
  551.                 new_tile = tile_slot_ptr(cldev, slot_index);
  552.                 new_tile->id = id;
  553.                 memcpy(ts_bits(cldev, new_tile), tile->data, tile_size);
  554.                 count_add1(cmd_tile_added);
  555.                 if_debug3('L', "[L]adding tile %d, hash=%d, id=0x%lx\n",
  556.                      slot_index,
  557.                      (int)(hptr - cldev->tile_hash_table),
  558.                      id);
  559.                 break;
  560.                }
  561.             new_tile = tile_slot_ptr(cldev, slot_index);
  562.             if ( new_tile->id == id )
  563.                {    count_add1(cmd_tile_found);
  564.                 if_debug1('L', "[L]found tile %d\n",
  565.                       slot_index);
  566.                 break;
  567.                }
  568.            }
  569.        }
  570.     /* Check whether this band knows about this tile yet. */
  571.        {    int band_index = pcls - cldev->states;
  572.         byte pmask = 1 << (band_index & 7);
  573.         byte *ppresent = ts_mask(new_tile) + (band_index >> 3);
  574.         if ( *ppresent & pmask )
  575.            {    /* Tile is known, just put out the index. */
  576.             byte *dp = cmd_put_op(cldev, pcls, 2);
  577.             count_op(*dp = cmd_opv_set_tile_index);
  578.             dp[1] = slot_index;
  579.            }
  580.         else
  581.            {    /* Tile is not known, put out the bits.  Use a */
  582.             /* delta encoding or a short encoding if possible. */
  583.             byte *new_data = ts_bits(cldev, new_tile);
  584.             byte *dp;
  585.             byte delta[2+16*3];
  586.             int diff;
  587.             *ppresent |= pmask;
  588.             if ( old_tile != &no_tile &&
  589.                  (diff = tile_diff(ts_bits(cldev, old_tile), new_data, tile_size, delta)) >= 0
  590.                )
  591.                {    /* Use delta representation */
  592.                 dp = cmd_put_op(cldev, pcls, diff);
  593.                 count_op(delta[0]);
  594.                 delta[1] = slot_index;
  595.                 memcpy(dp, delta, diff);
  596.                 count_add(cmd_delta_tile_count, diff - 2);
  597.                }
  598.             else
  599.                {    if ( old_tile == &no_tile )
  600.                    {    byte *dp = cmd_put_op(cldev, pcls,
  601.                         1 + cmd_sizexy(cldev->tile.size));
  602.                     count_op(*dp++ = (byte)cmd_opv_set_tile_size);
  603.                     cmd_putxy(cldev->tile.size, dp);
  604.                    }
  605.                 if ( tile->size.x <= 16 )
  606.                    {    dp = cmd_put_op(cldev, pcls, 2 + (tile_size >> 1));
  607.                     cmd_put_short_bits(dp + 2, new_data, tile->raster, 2, tile->size.y);
  608.                     count_add(cmd_tile_count, tile_size >> 1);
  609.                    }
  610.                 else
  611.                    {    dp = cmd_put_op(cldev, pcls, 2 + tile_size);
  612.                     memcpy(dp + 2, new_data, tile_size);
  613.                     count_add(cmd_tile_count, tile_size);
  614.                    }
  615.                 count_op(*dp = (byte)cmd_opv_set_tile_bits);
  616.                 dp[1] = slot_index;
  617.                }
  618.            }
  619.        }
  620.     pcls->tile = new_tile;
  621.     return 0;
  622. }
  623. private int
  624. clist_tile_rectangle(gx_device *dev, const gx_tile_bitmap *tile, int x, int y,
  625.   int width, int height, gx_color_index color0, gx_color_index color1,
  626.   int px, int py)
  627. {    fit_fill(dev, x, y, width, height);
  628.     BEGIN_RECT
  629.     if ( tile->id != pcls->tile->id )
  630.        {    if ( clist_change_tile(cdev, pcls, tile) < 0 )
  631.             return gx_default_tile_rectangle(dev, tile, x, y, width, height, color0, color1, px, py);
  632.        }
  633.     if ( color0 != pcls->color0 || color1 != pcls->color1 )
  634.         cmd_set_colors(dev, pcls, color0, color1);
  635.     if ( px != pcls->tile_phase.x || py != pcls->tile_phase.y )
  636.        {    byte *dp = cmd_put_op(cdev, pcls, 1 + cmd_sizexy(pcls->tile_phase));
  637.         count_op(*dp++ = (byte)cmd_opv_set_tile_phase);
  638.         pcls->tile_phase.x = px;
  639.         pcls->tile_phase.y = py;
  640.         cmd_putxy(pcls->tile_phase, dp);
  641.        }
  642.     cmd_write_rect_cmd(dev, pcls, cmd_op_tile_rect, x, y, width, height);
  643.     END_RECT
  644.     return 0;
  645. }
  646.  
  647. private int
  648. clist_copy_mono(gx_device *dev,
  649.     const byte *data, int data_x, int raster, gx_bitmap_id id,
  650.     int x, int y, int width, int height,
  651.     gx_color_index color0, gx_color_index color1)
  652. {    int y0;
  653.     fit_copy(dev, data, data_x, raster, id, x, y, width, height);
  654.     y0 = y;
  655.     BEGIN_RECT
  656.     gx_cmd_rect rect;
  657.     int dx, row_bytes;
  658.     uint dsize;
  659.     int rsize;
  660.     int bwidth;
  661.     const byte *row = data + (y - y0) * raster;
  662.     byte *dp;
  663.     if ( color0 != pcls->color0 || color1 != pcls->color1 )
  664.         cmd_set_colors(dev, pcls, color0, color1);
  665.     cmd_set_rect(rect);
  666.     rsize = cmd_size_rect(&rect);
  667.     if ( width >= 2 && (bwidth = (width + (data_x & 7) + 7) >> 3) <= 3 &&
  668.          height <= 255 &&
  669.          height <= (cbuf_size - cmd_largest_size) / align_bitmap_mod
  670.        )
  671.        {    dsize = height * bwidth;
  672.         dp = cmd_put_op(cdev, pcls, 1 + rsize + dsize);
  673.         count_op(*dp++ = (byte)cmd_op_copy_mono + (data_x & 7) + 1);
  674.         dp = cmd_put_rect(&rect, dp);
  675.         row += data_x >> 3;
  676.         cmd_put_short_bits(dp, row, raster, bwidth, height);
  677.         pcls->rect = rect;
  678.         count_add(cmd_copy_count, dsize);
  679.        }
  680.     else
  681.        {    if ( height == 1 )
  682.           {    /* We don't need to write the entire row, */
  683.             /* only the part that is being copied. */
  684.             dx = data_x & 7;
  685.             row_bytes = (uint)((data_x & 7) + width + 7) >> 3;
  686.             dsize = row_bytes;
  687.             row += data_x >> 3;
  688.           }
  689.         else
  690.           {    dx = data_x;
  691.             row_bytes = raster;
  692.             dsize = height * raster;
  693.           }
  694.         if ( dsize > cbuf_size )
  695.            {    /* We have to split it into pieces. */
  696.             if ( height > 1 )
  697.                {    int h2 = height >> 1;
  698.                 clist_copy_mono(dev, row, data_x, raster,
  699.                     gx_no_bitmap_id, x, y, width, h2,
  700.                     color0, color1);
  701.                 clist_copy_mono(dev, row + h2 * raster,
  702.                     data_x, raster, gx_no_bitmap_id,
  703.                     x, y + h2, width, height - h2,
  704.                     color0, color1);
  705.                }
  706.             else
  707.             /* Split a single (very long) row. */
  708.                {    int w2 = width >> 1;
  709.                 clist_copy_mono(dev, row, dx, row_bytes,
  710.                     gx_no_bitmap_id, x, y, w2, 1,
  711.                     color0, color1);
  712.                 clist_copy_mono(dev, row, dx + w2,
  713.                     row_bytes, gx_no_bitmap_id, x + w2, y,
  714.                     width - w2, 1, color0, color1);
  715.                }
  716.            }
  717.         else
  718.         {    uint csize =
  719.               1 + rsize + cmd_sizew(dx) + cmd_sizew(row_bytes);
  720.             dp = cmd_put_op(cdev, pcls, csize + dsize);
  721.             /* See if compressing with RLE is worthwhile. */
  722.             if ( dsize >= 50 )
  723.               {    stream_RLE_state sstate;
  724.                 stream_cursor_read r;
  725.                 byte *wbase = dp + (csize - 1);
  726.                 stream_cursor_write w;
  727.                 int status;
  728.                 uint wcount;
  729.                 sstate.record_size = 0;
  730.                 s_RLE_init_inline(&sstate);
  731.                 r.ptr = row - 1;
  732.                 r.limit = r.ptr + dsize;
  733.                 w.ptr = wbase;
  734.                 w.limit = w.ptr + dsize;
  735.                 status =
  736.                   (*s_RLE_template.process)
  737.                     ((stream_state *)&sstate, &r, &w, true);
  738.                 if ( status == 0 && (wcount = w.ptr - wbase) <= dsize >> 1 )
  739.                   {    /* Use compressed representation. */
  740.                     cmd_shorten_op(cdev, pcls, dsize - wcount);
  741.                     count_op(*dp++ = (byte)cmd_op_copy_mono_rle);
  742.                     count_add(cmd_copy_rle_count, wcount);
  743.                     goto out;
  744.                   }
  745.               }
  746.             memcpy(dp + csize, row, dsize);
  747.             count_op(*dp++ = (byte)cmd_op_copy_mono);
  748.             count_add(cmd_copy_count, dsize);
  749. out:            dp = cmd_put_rect(&rect, dp);
  750.             cmd_putw(dx, dp);
  751.             cmd_putw(row_bytes, dp);
  752.             pcls->rect = rect;
  753.         }
  754.        }
  755.     END_RECT
  756.     return 0;
  757. }
  758.  
  759. private int
  760. clist_copy_color(gx_device *dev,
  761.     const byte *data, int data_x, int raster, gx_bitmap_id id,
  762.     int x, int y, int width, int height)
  763. {    int y0;
  764.     fit_copy(dev, data, data_x, raster, id, x, y, width, height);
  765.     y0 = y;
  766.     BEGIN_RECT
  767.     int dx, row_bytes;
  768.     uint dsize;
  769.     const byte *row = data + (y - y0) * raster;
  770.     if ( height == 1 )
  771.       {    /* We don't need to write the entire row, */
  772.         /* only the part that is being copied. */
  773.         int depth = dev->color_info.depth;
  774.         uint dbit = data_x * depth;
  775.         dx = (dbit & 7) / depth;
  776.         row_bytes = (uint)((dbit & 7) + (width * depth) + 7) >> 3;
  777.         dsize = row_bytes;
  778.         row += dbit >> 3;
  779.       }
  780.     else
  781.       {    dx = data_x;
  782.         row_bytes = raster;
  783.         dsize = height * raster;
  784.       }
  785.     if ( dsize > cbuf_size )
  786.        {    /* We have to split it into pieces. */
  787.         if ( height > 1 )
  788.            {    int h2 = height >> 1;
  789.             clist_copy_color(dev, row, data_x, raster,
  790.                      gx_no_bitmap_id, x, y, width, h2);
  791.             clist_copy_color(dev, row + h2 * raster,
  792.                      data_x, raster,
  793.                      gx_no_bitmap_id, x, y + h2,
  794.                      width, height - h2);
  795.            }
  796.         else
  797.            {    /* Split a single (very long) row. */
  798.             int w2 = width >> 1;
  799.             clist_copy_color(dev, row, dx, row_bytes,
  800.                      gx_no_bitmap_id, x, y, w2, 1);
  801.             clist_copy_color(dev, row, dx + w2, row_bytes,
  802.                      gx_no_bitmap_id, x + w2, y,
  803.                      width - w2, 1);
  804.            }
  805.        }
  806.     else
  807.       {    gx_cmd_rect rect;
  808.         byte *dp;
  809.         cmd_set_rect(rect);
  810.         dp = cmd_put_op(cdev, pcls, 1 + cmd_size_rect(&rect) + cmd_sizew(dx) + cmd_sizew(row_bytes) + dsize);
  811.         count_op(*dp++ = (byte)cmd_op_copy_color);
  812.         dp = cmd_put_rect(&rect, dp);
  813.         pcls->rect = rect;
  814.         cmd_putw(dx, dp);
  815.         cmd_putw(row_bytes, dp);
  816.         memcpy(dp, row, dsize);
  817.         count_add(cmd_copy_count, dsize);
  818.       }
  819.     END_RECT
  820.     return 0;
  821. }
  822.