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 / GXCLIST.C < prev    next >
C/C++ Source or Header  |  1992-09-19  |  38KB  |  1,219 lines

  1. /* Copyright (C) 1991, 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. /* gxclist.c */
  21. /* Command list 'device' for Ghostscript. */
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxdevice.h"
  26. #include "gxdevmem.h"            /* must precede gxclist.h */
  27. #include "gxclist.h"
  28.  
  29. /* Patch a couple of things possibly missing from stdio.h. */
  30. #ifndef SEEK_SET
  31. #  define SEEK_SET 0
  32. #endif
  33. #ifndef SEEK_END
  34. #  define SEEK_END 2
  35. #endif
  36.  
  37. #define cdev ((gx_device_clist *)dev)
  38.  
  39. /* Forward declarations of procedures */
  40. private dev_proc_open_device(clist_open);
  41. private dev_proc_get_initial_matrix(clist_get_initial_matrix);
  42. private dev_proc_output_page(clist_output_page);
  43. private dev_proc_map_rgb_color(clist_map_rgb_color);
  44. private dev_proc_map_color_rgb(clist_map_color_rgb);
  45. private dev_proc_fill_rectangle(clist_fill_rectangle);
  46. private dev_proc_tile_rectangle(clist_tile_rectangle);
  47. private dev_proc_copy_mono(clist_copy_mono);
  48. private dev_proc_copy_color(clist_copy_color);
  49. private dev_proc_get_bits(clist_get_bits);
  50. private dev_proc_get_props(clist_get_props);
  51. private dev_proc_put_props(clist_put_props);
  52.  
  53. /* The device descriptor */
  54. /* The device template itself is never used, only the procs. */
  55. gx_device_procs gs_clist_device_procs =
  56. {    clist_open,
  57.     clist_get_initial_matrix,
  58.     gx_default_sync_output,
  59.     clist_output_page,
  60.     gx_default_close_device,
  61.     clist_map_rgb_color,
  62.     clist_map_color_rgb,
  63.     clist_fill_rectangle,
  64.     clist_tile_rectangle,
  65.     clist_copy_mono,
  66.     clist_copy_color,
  67.     gx_default_draw_line,
  68.     clist_get_bits,
  69.     clist_get_props,
  70.     clist_put_props
  71. };
  72.  
  73. /* ------ Define the command set and syntax ------ */
  74.  
  75. /* A command always consists of an operation followed by operands. */
  76. /* The operands are implicit in the procedural code. */
  77. typedef enum {
  78.     cmd_op_misc = 0x00,        /* (see below) */
  79.       cmd_opv_end_run = 0x00,    /* (nothing) */
  80.       cmd_opv_set_tile_size = 0x01,    /* width, height */
  81.       cmd_opv_set_tile_phase = 0x02, /* x, y */
  82.     cmd_op_set_color0 = 0x10,    /* color+2 in op byte, [color] */
  83.     cmd_op_set_color1 = 0x20,    /* color+2 in op byte, [color] */
  84.     cmd_op_set_tile_index = 0x30,    /* index */
  85.     cmd_op_fill_rect = 0x40,    /* rect */
  86.     cmd_op_fill_rect_short = 0x50,    /* dh in op byte,dx,dw | rect_short */
  87.     cmd_op_fill_rect_tiny = 0x60,    /* dw in op byte, rect_tiny */
  88.     cmd_op_tile_rect = 0x70,    /* rect */
  89.     cmd_op_tile_rect_short = 0x80,    /* dh in op byte,dx,dw | rect_short */
  90.     cmd_op_tile_rect_tiny = 0x90,    /* dw in op byte, rect_tiny */
  91.     cmd_op_copy_mono = 0xa0,    /* rect, data_x, raster | */
  92.                     /* d_x+1 in op byte, x, y, w, h */
  93.     cmd_op_copy_color = 0xb0,    /* rect, data_x, raster */
  94.     cmd_op_set_tile_bits = 0xc0,    /* index, <bits> */
  95.     cmd_op_delta_tile_bits = 0xd0,    /* n-1 in op byte, n x <offset, bits> */
  96.     cmd_op_end
  97. } gx_cmd_op;
  98. /* Define the size of the largest command, */
  99. /* not counting any bitmap. */
  100. #define w sizeof(short)            /* size of coordinate */
  101. #define d 3                /* size of tile delta */
  102. private uint cmd_largest_size =
  103.   max(1 + 6 * sizeof(short) /* copy_mono */, 2 + 16 * d /* delta_tile 15 */);
  104. #undef d
  105. #undef w
  106. #ifdef DEBUG
  107. private char *cmd_op_names[16] = {
  108.   "misc", "set_color_0", "set_color_1", "set_tile",
  109.   "fill_rect", "fill_rect_short", "fill_rect_tiny", "tile_rect",
  110.   "tile_rect_short", "tile_rect_tiny", "copy_mono", "copy_color",
  111.   "set_tile_bits", "delta_tile_bits", "?e0?", "?f0?"
  112. };
  113. private char *cmd_misc_op_names[16] = {
  114.   "end_run", "set_tile_size", "set_tile_phase", "?03?",
  115.   "?04?", "?05?", "?06?", "?07?",
  116.   "?08?", "?09?", "?0a?", "?0b?",
  117.   "?0c?", "?0d?", "?0e?", "?0f?"
  118. };
  119. private ulong cmd_op_counts[256];
  120. private ulong cmd_tile_count, cmd_copy_count, cmd_delta_tile_count;
  121. private ulong cmd_tile_reset, cmd_tile_found, cmd_tile_added;
  122. private int
  123. count_op(int op)
  124. {    ++cmd_op_counts[op];
  125.     if_debug2('L', ", %s %d\n", cmd_op_names[op >> 4], op & 0xf),
  126.     fflush(dstderr);
  127.     return op;
  128. }
  129. #  define count_add(v, n) (v += (n))
  130. #else
  131. #  define count_op(store_op) store_op
  132. #  define count_add(v, n) 0
  133. #endif
  134. #define count_add1(v) count_add(v, 1)
  135.  
  136. typedef struct {
  137.     short x, y, width, height;
  138. } gx_cmd_rect;
  139. typedef struct {
  140.     byte dx, dwidth, dy, dheight;    /* dy and dheight are optional */
  141. } gx_cmd_rect_short;
  142. #define cmd_min_short (-128)
  143. #define cmd_max_short 127
  144. typedef struct {
  145.     unsigned dx : 4;
  146.     unsigned dy : 4;
  147. } gx_cmd_rect_tiny;
  148. #define cmd_min_tiny (-8)
  149. #define cmd_max_tiny 7
  150.  
  151. /* Define the prefix on each command run in the writing buffer. */
  152. typedef struct cmd_prefix_s cmd_prefix;
  153. struct cmd_prefix_s {
  154.     cmd_prefix *next;
  155.     uint size;
  156. };
  157.  
  158. /* Define the entries in the block file. */
  159. typedef struct cmd_block_s {
  160.     int band;
  161.     long pos;            /* starting position in cfile */
  162. } cmd_block;
  163.  
  164. /* Remember the current state of one band when writing or reading. */
  165. struct gx_clist_state_s {
  166.     gx_color_index color0, color1;    /* most recent colors */
  167.     tile_slot *tile;        /* most recent tile */
  168.     gs_int_point tile_phase;    /* most recent tile phase */
  169.     gx_cmd_rect rect;        /* most recent rectangle */
  170.     /* Following are only used when writing */
  171.     cmd_prefix *head, *tail;    /* list of commands for band */
  172. };
  173. private tile_slot no_tile = { ~0L };
  174.  
  175. /* The initial values for a band state */
  176. private gx_clist_state cls_initial =
  177.    {    gx_no_color_index, gx_no_color_index, &no_tile,
  178.      { 0, 0 }, { 0, 0, 0, 0 },
  179.     0, 0
  180.    };
  181.  
  182. /* Define the size of the command buffer used for reading. */
  183. /* This is needed to split up very large copy_ operations. */
  184. #define cbuf_size 500
  185.  
  186. /* Initialize the device state */
  187. private void clist_init_tiles(P1(gx_device_clist *));
  188. private int
  189. clist_open(gx_device *dev)
  190. {    /*
  191.      * The buffer area (data, data_size) holds a tile cache and a
  192.      * set of block range bit masks when both writing and reading.
  193.      * The rest of the space is used for
  194.      * the command buffer and band state bookkeeping when writing,
  195.      * and for the rendering buffer (image device) when reading.
  196.      * For the moment, we divide the space up arbitrarily.
  197.      *
  198.      * This routine requires only data, data_size, target, and mdev
  199.      * to have been set in the device structure, and is idempotent,
  200.      * so it can be used to check whether a given-size buffer
  201.      * is large enough.
  202.      */
  203.     byte *data = cdev->data;
  204.     uint size = cdev->data_size;
  205. #define alloc_data(n) data += (n), size -= (n)
  206.     gx_device *target = cdev->target;
  207.     uint raster, nbands, band;
  208.     gx_clist_state *states;
  209.     ulong state_size;
  210.     cdev->ymin = cdev->ymax = -1;    /* render_init not done yet */
  211.     cdev->tile_data = data;
  212.     cdev->tile_data_size = (size / 5) & -4;    /* arbitrary! */
  213.     alloc_data(cdev->tile_data_size);
  214.     raster = gx_device_raster(target, 1) + sizeof(byte *);
  215.     cdev->band_height = size / raster;
  216.     if ( cdev->band_height == 0 )    /* can't even fit one scan line */
  217.         return_error(gs_error_limitcheck);
  218.     nbands = target->height / cdev->band_height + 1;
  219.     cdev->nbands = nbands;
  220.     if_debug4('l', "[l]width=%d, raster=%d, band_height=%d, nbands=%d\n",
  221.              target->width, raster, cdev->band_height, cdev->nbands);
  222.     state_size = nbands * (ulong)sizeof(gx_clist_state);
  223.     if ( state_size + sizeof(cmd_prefix) + cmd_largest_size + raster + 4 > size )        /* not enough room */
  224.         return_error(gs_error_limitcheck);
  225.     cdev->mdev.base = data;
  226.     cdev->states = states = (gx_clist_state *)data;
  227.     alloc_data((uint)state_size);
  228.     cdev->cbuf = data;
  229.     cdev->cnext = data;
  230.     cdev->cend = data + size;
  231.     cdev->ccls = 0;
  232.     for ( band = 0; band < nbands; band++, states++ )
  233.       *states = cls_initial;
  234. #undef alloc_data
  235.     cdev->tile_band_mask_size = (nbands + 31) / 32 * 4;
  236.     cdev->tile_max_size = cdev->tile_data_size -
  237.         (sizeof(tile_hash) * 2 + sizeof(tile_slot) +
  238.          cdev->tile_band_mask_size);
  239.     clist_init_tiles(cdev);
  240.     return 0;
  241. }
  242.  
  243. /* (Re)initialize the tile cache. */
  244. private void
  245. clist_init_tiles(register gx_device_clist *cldev)
  246. {    gx_clist_state *pcls;
  247.     int i, hc;
  248.     cldev->tile_slot_size =
  249.       sizeof(tile_slot) + cldev->tile_band_mask_size +
  250.       cldev->tile.raster * cldev->tile.size.y;
  251.     cldev->tile_max_count = cldev->tile_data_size /
  252.       (sizeof(tile_hash) * 3 /*(worst case)*/ + cldev->tile_slot_size);
  253.     hc = (cldev->tile_max_count - 1) * 2;
  254.     while ( (hc + 1) & hc ) hc |= hc >> 1;    /* make mask */
  255.     if ( hc >= cldev->tile_max_count * 3 ) hc >>= 1;
  256.     if ( hc > 255 )        /* slot index in set_tile is only 1 byte */
  257.        {    hc = 255;
  258.         if ( cldev->tile_max_count > 200 )
  259.             cldev->tile_max_count = 200;
  260.        }
  261.     cldev->tile_hash_mask = hc;
  262.     hc++;                /* make actual size */
  263.     if_debug5('l', "[l]tile.size=%dx%d, slot_size=%d, max_count=%d, hc=%d\n",
  264.          cldev->tile.size.x, cldev->tile.size.y,
  265.          cldev->tile_slot_size, cldev->tile_max_count, hc);
  266.     cldev->tile_hash_table =
  267.         (tile_hash *)(cldev->tile_data + cldev->tile_data_size) - hc;
  268.     cldev->tile_count = 0;
  269.     memset(cldev->tile_data, 0, cldev->tile_data_size);
  270.     memset(cldev->tile_hash_table, -1, hc * sizeof(tile_hash));
  271.     for ( i = 0, pcls = cldev->states; i < cldev->nbands; i++, pcls++ )
  272.         pcls->tile = &no_tile;
  273.     count_add1(cmd_tile_reset);
  274. }
  275.  
  276. /* Forward the non-displaying operations to the target device. */
  277. private void
  278. clist_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  279. {    (*cdev->target->procs->get_initial_matrix)(dev, pmat);
  280. }
  281. private gx_color_index
  282. clist_map_rgb_color(gx_device *dev, gx_color_value red, gx_color_value green,
  283.   gx_color_value blue)
  284. {    return (*cdev->target->procs->map_rgb_color)(dev, red, green, blue);
  285. }
  286. private int
  287. clist_map_color_rgb(gx_device *dev, gx_color_index color,
  288.   gx_color_value rgb[3])
  289. {    return (*cdev->target->procs->map_color_rgb)(dev, color, rgb);
  290. }
  291. private int
  292. clist_get_props(gx_device *dev, gs_prop_item *plist)
  293. {    gx_device *tdev = cdev->target;
  294.     return (*tdev->procs->get_props)(tdev, plist);
  295. }
  296. private int
  297. clist_put_props(gx_device *dev, gs_prop_item *plist, int count)
  298. {    gx_device *tdev = cdev->target;
  299.     return (*tdev->procs->put_props)(tdev, plist, count);
  300. }
  301.  
  302. /* Print a bitmap for tracing */
  303. #ifdef DEBUG
  304. private void
  305. cmd_print_bits(const byte *data, int height, int raster)
  306. {    int i, j;
  307.     for ( i = 0; i < height; i++ )
  308.        {    const byte *row = data + i * raster;
  309.         dprintf("[L]");
  310.         for ( j = 0; j < raster; j++ )
  311.           dprintf1(" %02x", row[j]);
  312.         dputc('\n');
  313.        }
  314. }
  315. #else
  316. #  define cmd_print_bits(data, height, raster)
  317. #endif
  318.  
  319. /* ------ Writing ------ */
  320.  
  321. /* Utilities */
  322.  
  323. #define cmd_set_rect(rect)\
  324.   ((rect).x = x, (rect).y = y,\
  325.    (rect).width = width, (rect).height = height)
  326.  
  327. #define clist_write(f, str, len)\
  328.   fwrite(str, 1, len, f)
  329.  
  330. /* Write out the buffered commands, and reset the buffer. */
  331. private void
  332. cmd_write_buffer(gx_device_clist *cldev)
  333. {    FILE *cfile = cldev->cfile;
  334.     FILE *bfile = cldev->bfile;
  335.     int nbands = cldev->nbands;
  336.     gx_clist_state *pcls;
  337.     int band;
  338.     for ( band = 0, pcls = cldev->states; band < nbands; band++, pcls++ )
  339.        {    const cmd_prefix *cp = pcls->head;
  340.         if ( cp != 0 )
  341.            {    cmd_block cb;
  342.             cb.band = band;
  343.             cb.pos = ftell(cfile);
  344.             if_debug2('l', "[l]writing for band %d at %ld\n",
  345.                   band, cb.pos);
  346.             clist_write(bfile, (const byte *)&cb, sizeof(cb));
  347.             for ( ; cp != 0; cp = cp->next )
  348.               clist_write(cfile, (const byte *)(cp + 1), cp->size);
  349.             pcls->head = pcls->tail = 0;
  350.             fputc(cmd_opv_end_run, cfile);
  351.            }
  352.        }
  353.     cldev->cnext = cldev->cbuf;
  354.     cldev->ccls = 0;
  355. }
  356.  
  357. /* Add a command to the appropriate band list, */
  358. /* and allocate space for its data. */
  359. /* Return the pointer to the data area. */
  360. private byte *
  361. cmd_put_op(gx_device_clist *cldev, gx_clist_state *pcls, uint size)
  362. {    byte *dp = cldev->cnext;
  363.     if_debug3('L', "[L]band %d: size=%u, left=%d",
  364.           (int)(pcls - cldev->states), size, (int)(cldev->cend - dp));
  365.     if ( size + (sizeof(cmd_prefix) + 4) > cldev->cend - dp )
  366.       { cmd_write_buffer(cldev);
  367.         return cmd_put_op(cldev, pcls, size);
  368.       }
  369.     if ( cldev->ccls == pcls )
  370.       { /* We're adding another command for the same band. */
  371.         /* Tack it onto the end of the previous one. */
  372.         pcls->tail->size += size;
  373.       }
  374.     else
  375.       { cmd_prefix *cp = (cmd_prefix *)(dp + (((byte *)0 - dp) & 3));
  376.         dp = (byte *)(cp + 1);
  377.         if ( pcls->tail != 0 ) pcls->tail->next = cp;
  378.         else pcls->head = cp;
  379.         pcls->tail = cp;
  380.         cldev->ccls = pcls;
  381.         cp->next = 0;
  382.         cp->size = size;
  383.       }
  384.     cldev->cnext = dp + size;
  385.     return dp;
  386. }
  387.  
  388. /* We store all short quantities little-endian. */
  389. /* This is OK, because we read them back little-endian explicitly. */
  390. #define cmd_putw(w, dp)\
  391.   (*dp = (w) & 0xff, dp[1] = (w) >> 8, dp += 2)
  392.  
  393. /* Write a short bitmap.  1 <= bwidth <= 3. */
  394. private void
  395. cmd_put_short_bits(register byte *dp, register const byte *data,
  396.   int raster, register int bwidth, register int height)
  397. {    while ( --height >= 0 )
  398.        {    switch ( bwidth )
  399.            {
  400.         case 3: dp[2] = data[2];
  401.         case 2: dp[1] = data[1];
  402.         case 1: dp[0] = data[0];
  403.            }
  404.         dp += bwidth, data += raster;
  405.        }
  406. }
  407.  
  408. private int
  409. cmd_write_rect_cmd(gx_device *dev, gx_clist_state *pcls,
  410.   int op, int x, int y, int width, int height)
  411. {    int dx = x - pcls->rect.x;
  412.     int dy = y - pcls->rect.y;
  413.     int dwidth = width - pcls->rect.width;
  414.     int dheight = height - pcls->rect.height;
  415. #define check_ranges_1()\
  416.   ((unsigned)(dx - rmin) <= (rmax - rmin) &&\
  417.    (unsigned)(dy - rmin) <= (rmax - rmin) &&\
  418.    (unsigned)(dwidth - rmin) <= (rmax - rmin))
  419. #define check_ranges()\
  420.   (check_ranges_1() &&\
  421.    (unsigned)(dheight - rmin) <= (rmax - rmin))
  422. #define rmin cmd_min_tiny
  423. #define rmax cmd_max_tiny
  424.     cmd_set_rect(pcls->rect);
  425.     if ( dheight == 0 && check_ranges_1() )
  426.        {    byte *dp = cmd_put_op(cdev, pcls, 2);
  427.         count_op(*dp = op + 0x20 + dwidth - rmin);
  428.         dp[1] = (dx << 4) + dy - (rmin * 0x11);
  429.        }
  430. #undef rmin
  431. #undef rmax
  432. #define rmin cmd_min_short
  433. #define rmax cmd_max_short
  434.     else if ( check_ranges() )
  435.        {    int dh = dheight - cmd_min_tiny;
  436.         byte *dp;
  437.         if ( (unsigned)dh <= cmd_max_tiny - cmd_min_tiny && dh != 0 &&
  438.              dy == 0
  439.            )
  440.            {    op += dh;
  441.             dp = cmd_put_op(cdev, pcls, 3);
  442.            }
  443.         else
  444.            {    dp = cmd_put_op(cdev, pcls, 5);
  445.             dp[3] = dy - rmin;
  446.             dp[4] = dheight - rmin;
  447.            }
  448.         count_op(*dp = op + 0x10);
  449.         dp[1] = dx - rmin;
  450.         dp[2] = dwidth - rmin;
  451.        }
  452.     else
  453.        {    byte *dp = cmd_put_op(cdev, pcls, 1 + sizeof(pcls->rect));
  454.         count_op(*dp = op);
  455.         memcpy(dp + 1, &pcls->rect, sizeof(pcls->rect));
  456.        }
  457.     return 0;
  458. }
  459.  
  460. private void
  461. cmd_put_color(gx_device *dev, gx_clist_state *pcls,
  462.   int op, gx_color_index color)
  463. {    if ( (long)color >= -1 && (long)color <= 13 )
  464.         count_op(*cmd_put_op(cdev, pcls, 1) = op + (int)color + 2);
  465.     else
  466.        {    byte *dp = cmd_put_op(cdev, pcls, 1 + sizeof(color));
  467.         count_op(*dp = op);
  468.         memcpy(dp + 1, &color, sizeof(color));
  469.        }
  470. }
  471. private void
  472. cmd_set_colors(gx_device *dev, gx_clist_state *pcls,
  473.   gx_color_index color0, gx_color_index color1)
  474. {    if ( color0 != pcls->color0 )
  475.        {    cmd_put_color(dev, pcls, cmd_op_set_color0, color0);
  476.         pcls->color0 = color0;
  477.        }
  478.     if ( color1 != pcls->color1 )
  479.        {    cmd_put_color(dev, pcls, cmd_op_set_color1, color1);
  480.         pcls->color1 = color1;
  481.        }
  482. }
  483.  
  484. /* Driver interface */
  485.  
  486. /* Macros for dividing up a single call into bands */
  487. #define BEGIN_RECT\
  488.    {    int yend = y + height;\
  489.     int band_height = cdev->band_height;\
  490.     do\
  491.        {    int band = y / band_height;\
  492.         gx_clist_state *pcls = cdev->states + band;\
  493.         height = band_height - y % band_height;\
  494.         if ( yend - y < height ) height = yend - y;\
  495.            {
  496. #define END_RECT\
  497.            }\
  498.         y += height;\
  499.        }\
  500.     while ( y < yend );\
  501.    }
  502.  
  503. private int
  504. clist_fill_rectangle(gx_device *dev, int x, int y, int width, int height,
  505.   gx_color_index color)
  506. {    fit_fill(dev, x, y, width, height);
  507.     BEGIN_RECT
  508.     if ( color != pcls->color1 )
  509.         cmd_set_colors(dev, pcls, pcls->color0, color);
  510.     cmd_write_rect_cmd(dev, pcls, cmd_op_fill_rect, x, y, width, height);
  511.     END_RECT
  512.     return 0;
  513. }
  514.  
  515. /* Compare unequal tiles.  Return -1 if unrelated, */
  516. /* or 2<=N<=50 for the size of the delta encoding. */
  517. private int
  518. tile_diff(const byte *old_data, const byte *new_data, uint tsize,
  519.   byte _ss *delta)
  520. {    register const ushort *old2, *new2;
  521.     register ushort diff;
  522.     int count;
  523.     register int i;
  524.     byte _ss *pd;
  525.     if ( tsize > 128 ) return -1;
  526.     old2 = (const ushort *)old_data;
  527.     new2 = (const ushort *)new_data;
  528.     count = 0;
  529.     pd = delta + 2;            /* skip slot index */
  530.     for ( i = 0; i < tsize; i += 2, old2++, new2++ )
  531.       if ( (diff = *new2 ^ *old2) != 0 )
  532. #if arch_is_big_endian
  533. #  define i_hi 0
  534. #  define b_0(w) ((w) >> 8)
  535. #  define b_1(w) ((byte)(w))
  536. #else
  537. #  define i_hi 1
  538. #  define b_0(w) ((byte)(w))
  539. #  define b_1(w) ((w) >> 8)
  540. #endif
  541.        {    if ( count == 16 ) return -1;
  542.         if ( diff & 0xff00 )
  543.            {    if ( diff & 0xff )
  544.                 *pd++ = 0x80 + i,
  545.                 *pd++ = b_0(diff),
  546.                 *pd++ = b_1(diff);
  547.             else
  548.                 *pd++ = i + i_hi, *pd++ = diff >> 8;
  549.            }
  550.         else            /* know diff != 0 */
  551.             *pd++ = i + (1 - i_hi), *pd++ = (byte)diff;
  552.         count++;
  553.        }
  554. #undef b_0
  555. #undef b_1
  556. #undef i_hi
  557.     if ( count == 0 )
  558.     {    /* Tiles are identical.  This is highly unusual, */
  559.         /* but not impossible. */
  560.         pd[0] = pd[1] = 0;
  561.         pd += 2;
  562.         count = 1;
  563.     }
  564.     delta[0] = (byte)cmd_op_delta_tile_bits + count - 1;
  565.     return pd - delta;
  566. }
  567.  
  568. /* Handle changing tiles for clist_tile_rectangle. */
  569. /* We put this in a separate routine, even though it is called only once, */
  570. /* to avoid cluttering up the main-line case of tile_rectangle. */
  571. private int
  572. clist_change_tile(gx_device_clist *cldev, gx_clist_state *pcls,
  573.   const gx_bitmap *tile)
  574. {    uint tile_size = tile->raster * tile->size.y;
  575.     tile_slot *old_tile, *new_tile;
  576.     int slot_index;
  577.     /* Look up the tile in the cache. */
  578. top:       {    gx_bitmap_id id = tile->id;
  579.         uint probe = (uint)(id >> 16) + (uint)(id);
  580.         old_tile = pcls->tile;
  581.         for ( ; ; probe += 25 /* semi-random odd # */ )
  582.            {    tile_hash *hptr = cldev->tile_hash_table +
  583.               (probe & cldev->tile_hash_mask);
  584.             if ( (slot_index = hptr->slot_index) < 0 ) /* empty entry */
  585.                {    /* Must change tiles.  Check whether the */
  586.                 /* tile size has changed. */
  587.                 if ( tile->size.x != cldev->tile.size.x ||
  588.                      tile->size.y != cldev->tile.size.y
  589.                    )
  590.                    {    if ( tile->raster !=
  591.                          ((tile->size.x + 31) >> 5) << 2 ||
  592.                          tile_size > cldev->tile_max_size
  593.                        )
  594.                         return -1;
  595.                     cldev->tile = *tile;    /* reset size, raster */
  596.                     clist_init_tiles(cldev);
  597.                     goto top;
  598.                    }
  599.                 if ( cldev->tile_count == cldev->tile_max_count )
  600.                    {    /* Punt. */
  601.                     clist_init_tiles(cldev);
  602.                     goto top;
  603.                    }
  604.                 hptr->slot_index = slot_index =
  605.                   cldev->tile_count++;
  606.                 new_tile = tile_slot_ptr(cldev, slot_index);
  607.                 new_tile->id = id;
  608.                 memcpy(ts_bits(cldev, new_tile), tile->data, tile_size);
  609.                 count_add1(cmd_tile_added);
  610.                 if_debug3('L', "[L]adding tile %d, hash=%d, id=%lx\n",
  611.                      slot_index,
  612.                      (int)(hptr - cldev->tile_hash_table),
  613.                      id);
  614.                 break;
  615.                }
  616.             new_tile = tile_slot_ptr(cldev, slot_index);
  617.             if ( new_tile->id == id )
  618.                {    count_add1(cmd_tile_found);
  619.                 if_debug1('L', "[L]found tile %d\n",
  620.                       slot_index);
  621.                 break;
  622.                }
  623.            }
  624.        }
  625.     /* Check whether this band knows about this tile yet. */
  626.        {    int band_index = pcls - cldev->states;
  627.         byte pmask = 1 << (band_index & 7);
  628.         byte *ppresent = ts_mask(new_tile) + (band_index >> 3);
  629.         if ( *ppresent & pmask )
  630.            {    /* Tile is known, just put out the index. */
  631.             byte *dp = cmd_put_op(cldev, pcls, 2);
  632.             count_op(*dp = cmd_op_set_tile_index);
  633.             dp[1] = slot_index;
  634.            }
  635.         else
  636.            {    /* Tile is not known, put out the bits.  Use a */
  637.             /* delta encoding or a short encoding if possible. */
  638.             byte *new_data = ts_bits(cldev, new_tile);
  639.             byte *dp;
  640.             byte delta[2+16*3];
  641.             int diff;
  642.             *ppresent |= pmask;
  643.             if ( old_tile != &no_tile &&
  644.                  (diff = tile_diff(ts_bits(cldev, old_tile), new_data, tile_size, delta)) >= 0
  645.                )
  646.                {    /* Use delta representation */
  647.                 dp = cmd_put_op(cldev, pcls, diff);
  648.                 count_op(delta[0]);
  649.                 delta[1] = slot_index;
  650.                 memcpy(dp, delta, diff);
  651.                 count_add(cmd_delta_tile_count, diff - 2);
  652.                }
  653.             else
  654.                {    if ( old_tile == &no_tile )
  655.                    {    byte *dp = cmd_put_op(cldev, pcls,
  656.                         1 + sizeof(cldev->tile.size));
  657.                     count_op(*dp = (byte)cmd_opv_set_tile_size);
  658.                     memcpy(dp + 1, &cldev->tile.size,
  659.                            sizeof(cldev->tile.size));
  660.                    }
  661.                 if ( tile->size.x <= 16 )
  662.                    {    dp = cmd_put_op(cldev, pcls, 2 + (tile_size >> 1));
  663.                     cmd_put_short_bits(dp + 2, new_data, tile->raster, 2, tile->size.y);
  664.                     count_add(cmd_tile_count, tile_size >> 1);
  665.                    }
  666.                 else
  667.                    {    dp = cmd_put_op(cldev, pcls, 2 + tile_size);
  668.                     memcpy(dp + 2, new_data, tile_size);
  669.                     count_add(cmd_tile_count, tile_size);
  670.                    }
  671.                 count_op(*dp = (byte)cmd_op_set_tile_bits);
  672.                 dp[1] = slot_index;
  673.                }
  674.            }
  675.        }
  676.     pcls->tile = new_tile;
  677.     return 0;
  678. }
  679. private int
  680. clist_tile_rectangle(gx_device *dev, const gx_bitmap *tile, int x, int y,
  681.   int width, int height, gx_color_index color0, gx_color_index color1,
  682.   int px, int py)
  683. {    fit_fill(dev, x, y, width, height);
  684.     BEGIN_RECT
  685.     if ( tile->id != pcls->tile->id )
  686.        {    if ( clist_change_tile(cdev, pcls, tile) < 0 )
  687.             return gx_default_tile_rectangle(dev, tile, x, y, width, height, color0, color1, px, py);
  688.        }
  689.     if ( color0 != pcls->color0 || color1 != pcls->color1 )
  690.         cmd_set_colors(dev, pcls, color0, color1);
  691.     if ( px != pcls->tile_phase.x || py != pcls->tile_phase.y )
  692.        {    byte *dp = cmd_put_op(cdev, pcls, 1 + sizeof(pcls->tile_phase));
  693.         count_op(*dp = (byte)cmd_opv_set_tile_phase);
  694.         pcls->tile_phase.x = px;
  695.         pcls->tile_phase.y = py;
  696.         memcpy(dp + 1, &pcls->tile_phase, sizeof(pcls->tile_phase));
  697.        }
  698.     cmd_write_rect_cmd(dev, pcls, cmd_op_tile_rect, x, y, width, height);
  699.     END_RECT
  700.     return 0;
  701. }
  702.  
  703. private int
  704. clist_copy_mono(gx_device *dev,
  705.     const byte *data, int data_x, int raster, gx_bitmap_id id,
  706.     int x, int y, int width, int height,
  707.     gx_color_index color0, gx_color_index color1)
  708. {    int y0;
  709.     fit_copy(dev, data, data_x, raster, id, x, y, width, height);
  710.     y0 = y;
  711.     BEGIN_RECT
  712.     gx_cmd_rect rect;
  713.     uint dsize;
  714.     int bwidth;
  715.     const byte *row = data + (y - y0) * raster;
  716.     byte *dp;
  717.     if ( color0 != pcls->color0 || color1 != pcls->color1 )
  718.         cmd_set_colors(dev, pcls, color0, color1);
  719.     cmd_set_rect(rect);
  720.     if ( width >= 2 && (bwidth = (width + (data_x & 7) + 7) >> 3) <= 3 &&
  721.         height <= min(255, (cbuf_size - (1 + 2 * 2 + 2)) / bwidth)
  722.        )
  723.        {    dsize = height * bwidth;
  724.         dp = cmd_put_op(cdev, pcls, 1 + 2 * 2 + 2 + dsize);
  725.         count_op(*dp++ = (byte)cmd_op_copy_mono + (data_x & 7) + 1);
  726.         cmd_putw(x, dp);
  727.         cmd_putw(y, dp);
  728.         *dp++ = width;
  729.         *dp++ = height;
  730.         row += data_x >> 3;
  731.         cmd_put_short_bits(dp, row, raster, bwidth, height);
  732.         pcls->rect = rect;
  733.         count_add(cmd_copy_count, dsize);
  734.        }
  735.     else
  736.        {    dsize = height * raster;
  737.         if ( dsize > cbuf_size )
  738.            {    /* We have to split it into pieces. */
  739.             if ( height > 1 )
  740.                {    int h2 = height >> 1;
  741.                 clist_copy_mono(dev, row, data_x, raster,
  742.                     gx_no_bitmap_id, x, y, width, h2,
  743.                     color0, color1);
  744.                 clist_copy_mono(dev, row + h2 * raster,
  745.                     data_x, raster, gx_no_bitmap_id,
  746.                     x, y + h2, width, height - h2,
  747.                     color0, color1);
  748.                }
  749.             /* Split a single (very long) row. */
  750.                {    int w2 = width >> 1;
  751.                 clist_copy_mono(dev, row, data_x, raster,
  752.                     gx_no_bitmap_id, x, y, w2, 1,
  753.                     color0, color1);
  754.                 clist_copy_mono(dev, row, data_x + w2,
  755.                     raster, gx_no_bitmap_id, x + w2, y,
  756.                     width - w2, 1, color0, color1);
  757.                }
  758.            }
  759.         else
  760.         {    dp = cmd_put_op(cdev, pcls, 1 + sizeof(rect) + 4 + dsize);
  761.             count_op(*dp++ = (byte)cmd_op_copy_mono);
  762.             memcpy(dp, (byte *)&rect, sizeof(rect));
  763.             dp += sizeof(rect);
  764.             cmd_putw(data_x, dp);
  765.             cmd_putw(raster, dp);
  766.             memcpy(dp, row, dsize);
  767.             pcls->rect = rect;
  768.             count_add(cmd_copy_count, dsize);
  769.         }
  770.        }
  771.     END_RECT
  772.     return 0;
  773. }
  774.  
  775. private int
  776. clist_copy_color(gx_device *dev,
  777.     const byte *data, int data_x, int raster, gx_bitmap_id id,
  778.     int x, int y, int width, int height)
  779. {    int y0;
  780.     fit_copy(dev, data, data_x, raster, id, x, y, width, height);
  781.     y0 = y;
  782.     BEGIN_RECT
  783.     gx_cmd_rect rect;
  784.     uint dsize = height * raster;
  785.     const byte *row = data + (y - y0) * raster;
  786.     byte *dp;
  787.     if ( dsize > cbuf_size )
  788.        {    /* We have to split it into pieces. */
  789.         if ( height > 1 )
  790.            {    int h2 = height >> 1;
  791.             clist_copy_color(dev, row, data_x, raster,
  792.                 gx_no_bitmap_id, x, y, width, h2);
  793.             clist_copy_color(dev, row + h2 * raster, data_x,
  794.                 raster, gx_no_bitmap_id, x, y + h2, width, height - h2);
  795.            }
  796.         else
  797.            {    /* Split a single (very long) row. */
  798.             int w2 = width >> 1;
  799.             clist_copy_color(dev, row, data_x, raster,
  800.                 gx_no_bitmap_id, x, y, w2, 1);
  801.             clist_copy_color(dev, row, data_x + w2,
  802.                 raster, gx_no_bitmap_id, x + w2, y,
  803.                 width - w2, 1);
  804.            }
  805.        }
  806.     else
  807.     {    cmd_set_rect(rect);
  808.         dp = cmd_put_op(cdev, pcls, 1 + sizeof(rect) + 4 + dsize);
  809.         count_op(*dp++ = (byte)cmd_op_copy_color);
  810.         memcpy(dp, (byte *)&rect, sizeof(rect));
  811.         pcls->rect = rect;
  812.         dp += sizeof(rect);
  813.         cmd_putw(data_x, dp);
  814.         cmd_putw(raster, dp);
  815.         memcpy(dp, row, dsize);
  816.         count_add(cmd_copy_count, dsize);
  817.     }
  818.     END_RECT
  819.     return 0;
  820. }
  821.  
  822. /* ------ Reading/rendering ------ */
  823.  
  824. /* Clean up after rendering a page. */
  825. private int
  826. clist_output_page(gx_device *dev, int num_copies, int flush)
  827. {    if ( flush )
  828.        {    rewind(cdev->cfile);
  829.         rewind(cdev->bfile);
  830.         cdev->bfile_end_pos = 0;
  831.        }
  832.     else
  833.        {    fseek(cdev->cfile, 0L, SEEK_END);
  834.         fseek(cdev->bfile, 0L, SEEK_END);
  835.        }
  836.     return clist_open(dev);        /* reinitialize */
  837. }
  838.  
  839. private int clist_render_init(P1(gx_device_clist *));
  840. private int clist_render(P3(gx_device_clist *, gx_device *, int));
  841.  
  842. /* Copy scan lines to the client.  This is where rendering gets done. */
  843. private int
  844. clist_get_bits(gx_device *dev, int start_y,
  845.   byte *str, uint size, int pad_to_word)
  846. {    int y = start_y;
  847.     byte *dest = str;
  848.     gx_device_memory *mdev = &cdev->mdev;
  849.     uint bytes_per_line;
  850.     uint count, left;
  851.     /* Initialize for rendering if we haven't done so yet. */
  852.     if ( cdev->ymin < 0 )
  853.         clist_render_init(cdev);
  854.     bytes_per_line = gx_device_raster((gx_device *)mdev, pad_to_word);
  855.     count = min(size / bytes_per_line,
  856.             cdev->target->height - start_y);
  857.     /* Render bands and copy them incrementally. */
  858.     for ( left = count; left; )
  859.        {    int n;
  860.         if ( !(y >= cdev->ymin && y < cdev->ymax) )
  861.            {    int band = y / mdev->height;
  862.             int code;
  863.             rewind(cdev->bfile);
  864.             (*mdev->procs->open_device)((gx_device *)mdev);    /* reinitialize */
  865.             code = clist_render(cdev, (gx_device *)mdev, band);
  866.             if ( code < 0 ) return code;
  867.             cdev->ymin = band * mdev->height;
  868.             cdev->ymax = cdev->ymin + mdev->height;
  869.            }
  870.         n = min(cdev->ymax - y, left);
  871.         (*mdev->procs->get_bits)((gx_device *)mdev,
  872.                      y - cdev->ymin, dest,
  873.                      bytes_per_line * n, pad_to_word);
  874.         y += n, dest += bytes_per_line * n, left -= n;
  875.        }
  876.     return count;
  877. }
  878.  
  879. #undef cdev
  880.  
  881. /* Initialize for reading. */
  882. private int
  883. clist_render_init(gx_device_clist *cdev)
  884. {    gx_device *target = cdev->target;
  885.     byte *base = cdev->mdev.base;    /* save */
  886.     int depth = target->color_info.depth;
  887.     uint raster = gx_device_raster(target, 1);
  888.     const gx_device_memory *mdev = gdev_mem_device_for_bits(depth);
  889.     if ( mdev == 0 )
  890.         return_error(gs_error_rangecheck);
  891.     cmd_write_buffer(cdev);        /* flush buffer */
  892.     /* Write the terminating entry in the block file. */
  893.     /* Note that because of copypage, there may be many such entries. */
  894.        {    cmd_block cb;
  895.         cb.band = -1;
  896.         cb.pos = ftell(cdev->cfile);
  897.         clist_write(cdev->bfile, (byte *)&cb, sizeof(cb));
  898.         cdev->bfile_end_pos = ftell(cdev->bfile);
  899.        }
  900.     cdev->mdev = *mdev;
  901.     cdev->mdev.base = base;        /* restore */
  902.     (*target->procs->get_initial_matrix)(target, &cdev->mdev.initial_matrix);
  903.     cdev->mdev.width = target->width;
  904.     cdev->mdev.height = cdev->band_height;
  905.     cdev->mdev.raster = raster;
  906.     cdev->ymin = cdev->ymax = 0;
  907. #ifdef DEBUG
  908. if ( gs_debug['l'] | gs_debug['L'] )
  909.    {    int ci, cj;
  910.     dprintf3("[l]counts: tile = %ld, copy = %ld, delta = %ld\n",
  911.              cmd_tile_count, cmd_copy_count, cmd_delta_tile_count);
  912.     dprintf3("           reset = %ld, found = %ld, added = %ld\n",
  913.              cmd_tile_reset, cmd_tile_found, cmd_tile_added);
  914.     for ( ci = 0; ci < 0x100; ci += 0x10 )
  915.        {    dprintf1("[l]  %s =", cmd_op_names[ci >> 4]);
  916.         for ( cj = ci; cj < ci + 0x10; cj++ )
  917.             dprintf1(" %ld", cmd_op_counts[cj]);
  918.         dputs("\n");
  919.        }
  920.    }
  921. #endif
  922.     return 0;
  923. }
  924.  
  925. /* Render one band to a specified target device. */
  926. #define assign_getw(var, p)\
  927.   (var = *p + ((uint)p[1] << 8), p += 2)
  928. typedef byte _ss *cb_ptr;
  929. private void clist_read(P3(FILE *, byte *, uint));
  930. private cb_ptr clist_read_short_bits(P6(FILE *, byte *, int, int, cb_ptr, cb_ptr));
  931. private int
  932. clist_render(gx_device_clist *cdev, gx_device *tdev, int band)
  933. {    byte cbuf[cbuf_size];
  934.     byte bits[4 * 255];        /* for short copy_mono bits */
  935.     register cb_ptr cbp;
  936.     cb_ptr cb_limit;
  937.     cb_ptr cb_end;
  938.     FILE *file = cdev->cfile;
  939.     FILE *bfile = cdev->bfile;
  940.     int y0 = band * cdev->band_height;
  941.     gx_clist_state state;
  942.     gx_bitmap state_tile;
  943.     uint tile_bits_size;        /* size of bits of each tile */
  944.     gs_int_point tile_phase;
  945.     cmd_block b_this;
  946.     long pos;
  947.     uint left;
  948. #define cmd_read_var(ptr, cbp)\
  949.   memcpy(ptr, cbp, sizeof(*ptr)),\
  950.   cbp += sizeof(*ptr)
  951. #define cmd_read(ptr, vsize, cbp)\
  952.   if ( cb_end - cbp >= vsize )\
  953.     memcpy(ptr, cbp, vsize), cbp += vsize;\
  954.   else\
  955.    { uint cleft = cb_end - cbp;\
  956.      memcpy(ptr, cbp, cleft); vsize -= cleft;\
  957.      clist_read(file, ptr + cleft, vsize);\
  958.      cbp = cb_end;\
  959.    }
  960. #define cmd_read_short_bits(ptr, bw, ht, cbp)\
  961.   cbp = clist_read_short_bits(file, ptr, bw, ht, cbp, cb_end)
  962.     state = cls_initial;
  963.     state_tile.id = 0;
  964.     tile_phase.x = tile_phase.y = 0;
  965. trd:    clist_read(bfile, (byte *)&b_this, sizeof(b_this));
  966. top:    /* Find the next run of commands for this band. */
  967.     if ( b_this.band < 0 && ftell(bfile) == cdev->bfile_end_pos )
  968.         return 0;    /* end of bfile */
  969.     if ( b_this.band != band ) goto trd;
  970.     pos = b_this.pos;
  971.     clist_read(bfile, (byte *)&b_this, sizeof(b_this));
  972.     fseek(file, pos, SEEK_SET);
  973.     left = (uint)(b_this.pos - pos);
  974.     cb_limit = cbuf + (cbuf_size - cmd_largest_size);
  975.     cb_end = cbuf + cbuf_size;
  976.     cbp = cb_end;
  977.     for ( ; ; )
  978.        {    int op;
  979.         uint bytes;
  980.         int data_x, raster;
  981.         int code;
  982.         cb_ptr source;
  983.         gx_color_index _ss *pcolor;
  984.         /* Make sure the buffer contains a full command. */
  985.         if ( cbp > cb_limit )
  986.            {    uint nread;
  987.             memcpy(cbuf, cbp, cb_end - cbp);
  988.             cbp = cbuf + (cb_end - cbp);
  989.             nread = cb_end - cbp;
  990.             if ( nread > left ) nread = left;
  991.             clist_read(file, cbp, nread);
  992.             cb_end = cbp + nread;
  993.             cbp = cbuf;
  994.             left -= nread;
  995.             if ( cb_limit > cb_end ) cb_limit = cb_end;
  996.            }
  997.         op = *cbp++;
  998.         if_debug2('L', "[L]%s %d:\n",
  999.               cmd_op_names[op >> 4], op & 0xf);
  1000.         switch ( op >> 4 )
  1001.            {
  1002.         case cmd_op_misc >> 4:
  1003.             switch ( op )
  1004.                {
  1005.             case cmd_opv_end_run:
  1006.                 goto top;
  1007.             case cmd_opv_set_tile_size:
  1008.                 cmd_read_var(&state_tile.size, cbp);
  1009.                 state_tile.raster = ((state_tile.size.x + 31) >> 5) << 2;
  1010.                 /* We can't actually know the rep_size, */
  1011.                 /* so we play it safe. */
  1012.                 state_tile.rep_width = state_tile.size.x;
  1013.                 state_tile.rep_height = state_tile.size.y;
  1014.                 cdev->tile_slot_size = tile_bits_size =
  1015.                     state_tile.raster * state_tile.size.y;
  1016.                 break;
  1017.             case cmd_opv_set_tile_phase:
  1018.                 cmd_read_var(&state.tile_phase, cbp);
  1019.                 break;
  1020.             default:
  1021.                 goto bad_op;
  1022.                }
  1023.             tile_phase.x = state.tile_phase.x % state_tile.size.x;
  1024.             tile_phase.y = (state.tile_phase.y + y0) % state_tile.size.y;
  1025.             continue;
  1026.         case cmd_op_set_color0 >> 4:
  1027.             pcolor = &state.color0;
  1028.             goto set_color;
  1029.         case cmd_op_set_color1 >> 4:
  1030.             pcolor = &state.color1;
  1031. set_color:        if ( op & 0xf )
  1032.                 *pcolor = (gx_color_index)(long)((op & 0xf) - 2);
  1033.             else
  1034.                 cmd_read_var(pcolor, cbp);
  1035.             continue;
  1036.         case cmd_op_set_tile_index >> 4:
  1037.             state_tile.data = (byte *)tile_slot_ptr(cdev, *cbp);
  1038.             cbp++;
  1039.             continue;
  1040.         case cmd_op_copy_mono >> 4:
  1041.             if ( op & 0xf )
  1042.                {    assign_getw(state.rect.x, cbp);
  1043.                 assign_getw(state.rect.y, cbp);
  1044.                 state.rect.width = *cbp++;
  1045.                 state.rect.height = *cbp++;
  1046.                 break;
  1047.                }
  1048.             /* falls through */
  1049.         case cmd_op_fill_rect >> 4:
  1050.         case cmd_op_tile_rect >> 4:
  1051.         case cmd_op_copy_color >> 4:
  1052.             cmd_read_var(&state.rect, cbp);
  1053.             break;
  1054.         case cmd_op_fill_rect_short >> 4:
  1055.         case cmd_op_tile_rect_short >> 4:
  1056.             state.rect.x += *cbp + cmd_min_short;
  1057.             state.rect.width += cbp[1] + cmd_min_short;
  1058.             if ( op & 0xf )
  1059.                {    state.rect.height += (op & 0xf) + cmd_min_tiny;
  1060.                 cbp += 2;
  1061.                }
  1062.             else
  1063.                {    state.rect.y += cbp[2] + cmd_min_short;
  1064.                 state.rect.height += cbp[3] + cmd_min_short;
  1065.                 cbp += 4;
  1066.                }
  1067.             break;
  1068.         case cmd_op_fill_rect_tiny >> 4:
  1069.         case cmd_op_tile_rect_tiny >> 4:
  1070.            {    int txy = *cbp++;
  1071.             state.rect.x += (txy >> 4) + cmd_min_tiny;
  1072.             state.rect.y += (txy & 0xf) + cmd_min_tiny;
  1073.             state.rect.width += (op & 0xf) + cmd_min_tiny;
  1074.            }    break;
  1075.         case cmd_op_set_tile_bits >> 4:
  1076.             state_tile.data = (byte *)tile_slot_ptr(cdev, *cbp);
  1077.             cbp++;
  1078.             if ( state_tile.size.x <= 16 )
  1079.                {    cmd_read_short_bits(state_tile.data, 2, state_tile.size.y, cbp);
  1080.                }
  1081.             else
  1082.                {    bytes = tile_bits_size;
  1083.                 cmd_read(state_tile.data, bytes, cbp);
  1084.                }
  1085. #ifdef DEBUG
  1086. if ( gs_debug['L'] )
  1087.             cmd_print_bits(state_tile.data, state_tile.size.y,
  1088.                        state_tile.raster);
  1089. #endif
  1090.             continue;
  1091.         case cmd_op_delta_tile_bits >> 4:
  1092.            {    byte *new_data = (byte *)tile_slot_ptr(cdev, *cbp);
  1093.             cbp++;
  1094.             memcpy(new_data, state_tile.data, tile_bits_size);
  1095.             state_tile.data = new_data;
  1096.             do
  1097.                {    uint offset = *cbp;
  1098.                 if ( offset < 0x80 )
  1099.                     new_data[offset] ^= cbp[1],
  1100.                     cbp += 2;
  1101.                 else
  1102.                     offset -= 0x80,
  1103.                     new_data[offset] ^= cbp[1],
  1104.                     new_data[offset + 1] ^= cbp[2],
  1105.                     cbp += 3;
  1106.                }
  1107.             while ( op-- & 0xf );
  1108.            }    continue;
  1109.         default:
  1110. bad_op:            lprintf5("Bad op %02x band %d file pos %ld buf pos %d/%d\n",
  1111.                  op, band, ftell(file), (int)(cbp - cbuf), (int)(cb_end - cbuf));
  1112.                {    cb_ptr pp;
  1113.                 for ( pp = cbuf; pp < cb_end; pp += 10 )
  1114.                   lprintf10(" %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1115.                        pp[0], pp[1], pp[2], pp[3], pp[4],
  1116.                        pp[5], pp[6], pp[7], pp[8], pp[9]);
  1117.                }
  1118.             return_error(gs_error_Fatal);
  1119.            }
  1120.         if_debug4('L', "[L]  x=%d y=%d w=%d h=%d\n",
  1121.               state.rect.x, state.rect.y, state.rect.width,
  1122.               state.rect.height);
  1123.         switch ( op >> 4 )
  1124.            {
  1125.         case cmd_op_fill_rect >> 4:
  1126.         case cmd_op_fill_rect_short >> 4:
  1127.         case cmd_op_fill_rect_tiny >> 4:
  1128.             code = (*tdev->procs->fill_rectangle)
  1129.               (tdev, state.rect.x, state.rect.y - y0,
  1130.                state.rect.width, state.rect.height, state.color1);
  1131.             break;
  1132.         case cmd_op_tile_rect >> 4:
  1133.         case cmd_op_tile_rect_short >> 4:
  1134.         case cmd_op_tile_rect_tiny >> 4:
  1135.             code = (*tdev->procs->tile_rectangle)
  1136.               (tdev, &state_tile,
  1137.                state.rect.x, state.rect.y - y0,
  1138.                state.rect.width, state.rect.height,
  1139.                state.color0, state.color1,
  1140.                tile_phase.x, tile_phase.y);
  1141.             break;
  1142.         case cmd_op_copy_mono >> 4:
  1143.             if ( op & 0xf )
  1144.                {    data_x = (op & 0xf) - 1;
  1145.                 raster = 4;
  1146.                 cmd_read_short_bits(bits, (data_x + state.rect.width + 7) >> 3, state.rect.height, cbp);
  1147.                 source = bits;
  1148.                 goto copy;
  1149.                }
  1150.             /* falls through */
  1151.         case cmd_op_copy_color >> 4:
  1152.             assign_getw(data_x, cbp);
  1153.             assign_getw(raster, cbp);
  1154.             bytes = state.rect.height * raster;
  1155.             /* copy_mono and copy_color have ensured that */
  1156.             /* the bits will fit in a single buffer. */
  1157.             cmd_read(cbuf, bytes, cbp);
  1158.             source = cbuf;
  1159. copy:
  1160. #ifdef DEBUG
  1161. if ( gs_debug['L'] )
  1162.    {            dprintf2("[L]  data_x=%d raster=%d\n",
  1163.                  data_x, raster);
  1164.             cmd_print_bits(source, state.rect.height, raster);
  1165.    }
  1166. #endif
  1167.             code = (op >> 4 == (byte)cmd_op_copy_mono >> 4 ?
  1168.               (*tdev->procs->copy_mono)
  1169.                 (tdev, source, data_x, raster, gx_no_bitmap_id,
  1170.                  state.rect.x, state.rect.y - y0,
  1171.                  state.rect.width, state.rect.height,
  1172.                  state.color0, state.color1) :
  1173.               (*tdev->procs->copy_color)
  1174.                 (tdev, source, data_x, raster, gx_no_bitmap_id,
  1175.                  state.rect.x, state.rect.y - y0,
  1176.                  state.rect.width, state.rect.height));
  1177.             break;
  1178.            }
  1179.         if ( code < 0 ) return_error(code);
  1180.        }
  1181. }
  1182. /* The typical implementations of fread and fseek */
  1183. /* are extremely inefficient for small counts, */
  1184. /* so we use loops instead. */
  1185. private void
  1186. clist_read(FILE *f, byte *str, uint len)
  1187. {    switch ( len )
  1188.        {
  1189.     default: fread(str, 1, len, f); break;
  1190.     case 8: *str++ = (byte)getc(f);
  1191.     case 7: *str++ = (byte)getc(f);
  1192.     case 6: *str++ = (byte)getc(f);
  1193.     case 5: *str++ = (byte)getc(f);
  1194.     case 4: *str++ = (byte)getc(f);
  1195.     case 3: *str++ = (byte)getc(f);
  1196.     case 2: *str++ = (byte)getc(f);
  1197.     case 1: *str = (byte)getc(f);
  1198.        }
  1199. }
  1200. /* Read a short bitmap */
  1201. private cb_ptr
  1202. clist_read_short_bits(FILE *file, byte *data, register int bwidth, int height,
  1203.   cb_ptr cbp, cb_ptr cb_end)
  1204. {    uint bytes = bwidth * height;
  1205.     byte *pdata = data + bytes;
  1206.     byte *udata = data + (height << 2);
  1207.     cmd_read(data, bytes, cbp);
  1208.     while ( --height > 0 )        /* first row is in place already */
  1209.        {    udata -= 4, pdata -= bwidth;
  1210.         switch ( bwidth )
  1211.            {
  1212.         case 3: udata[2] = pdata[2];
  1213.         case 2: udata[1] = pdata[1];
  1214.         case 1: udata[0] = pdata[0];
  1215.            }
  1216.        }
  1217.     return cbp;
  1218. }
  1219.