home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxclbits.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  23.8 KB  |  747 lines

  1. /* Copyright (C) 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxclbits.c,v 1.2.2.2 2000/11/09 23:36:53 rayjj Exp $ */
  20. /* Halftone and bitmap writing for command lists */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gpcheck.h"
  24. #include "gserrors.h"
  25. #include "gsbitops.h"
  26. #include "gxdevice.h"
  27. #include "gxdevmem.h"        /* must precede gxcldev.h */
  28. #include "gxcldev.h"
  29. #include "gxfmap.h"
  30.  
  31. /*
  32.  * Define when, if ever, to write character bitmaps in all bands.
  33.  * Set this to:
  34.  *      0 to always write in all bands;
  35.  *      N to write in all bands when the character has been seen in N+1
  36.  *         bands on a page;
  37.  *      max_ushort to never write in all bands.
  38.  */
  39. #define CHAR_ALL_BANDS_COUNT max_ushort
  40.  
  41. /* ------ Writing ------ */
  42.  
  43. /*
  44.  * Determine the (possibly unpadded) width in bytes for writing a bitmap,
  45.  * per the algorithm in gxcldev.h.  If compression_mask has any of the
  46.  * cmd_mask_compress_any bits set, we assume the bitmap will be compressed.
  47.  * Return the total size of the bitmap.
  48.  */
  49. uint
  50. clist_bitmap_bytes(uint width_bits, uint height, int compression_mask,
  51.            uint * width_bytes, uint * raster)
  52. {
  53.     uint full_raster = *raster = bitmap_raster(width_bits);
  54.     uint short_raster = (width_bits + 7) >> 3;
  55.     uint width_bytes_last;
  56.  
  57.     if (compression_mask & cmd_mask_compress_any)
  58.     *width_bytes = width_bytes_last = full_raster;
  59.     else if (short_raster <= cmd_max_short_width_bytes ||
  60.          height <= 1 ||
  61.          (compression_mask & decompress_spread) != 0
  62.     )
  63.     *width_bytes = width_bytes_last = short_raster;
  64.     else
  65.     *width_bytes = full_raster, width_bytes_last = short_raster;
  66.     return
  67.     (height == 0 ? 0 : *width_bytes * (height - 1) + width_bytes_last);
  68. }
  69.  
  70. /*
  71.  * Compress a bitmap, skipping extra padding bytes at the end of each row if
  72.  * necessary.  We require height >= 1, raster >= bitmap_raster(width_bits).
  73.  */
  74. private int
  75. cmd_compress_bitmap(stream_state * st, const byte * data, uint width_bits,
  76.             uint raster, uint height, stream_cursor_write * pw)
  77. {
  78.     uint width_bytes = bitmap_raster(width_bits);
  79.     int status = 0;
  80.     stream_cursor_read r;
  81.  
  82.     r.ptr = data - 1;
  83.     if (raster == width_bytes) {
  84.     r.limit = r.ptr + raster * height;
  85.     status = (*st->template->process) (st, &r, pw, true);
  86.     } else {            /* Compress row-by-row. */
  87.     uint y;
  88.  
  89.     for (y = 1; (r.limit = r.ptr + width_bytes), y < height; ++y) {
  90.         status = (*st->template->process) (st, &r, pw, false);
  91.         if (status)
  92.         break;
  93.         if (r.ptr != r.limit) {    /* We don't attempt to handle compressors that */
  94.         /* require >1 input byte to make progress. */
  95.         status = -1;
  96.         break;
  97.         }
  98.         r.ptr += raster - width_bytes;
  99.     }
  100.     if (status == 0)
  101.         status = (*st->template->process) (st, &r, pw, true);
  102.     }
  103.     if (st->template->release)
  104.     (*st->template->release) (st);
  105.     return status;
  106. }
  107.  
  108. /*
  109.  * Put a bitmap in the buffer, compressing if appropriate.
  110.  * pcls == 0 means put the bitmap in all bands.
  111.  * Return <0 if error, otherwise the compression method.
  112.  * A return value of gs_error_limitcheck means that the bitmap was too big
  113.  * to fit in the command reading buffer.
  114.  * Note that this leaves room for the command and initial arguments,
  115.  * but doesn't fill them in.
  116.  */
  117. int
  118. cmd_put_bits(gx_device_clist_writer * cldev, gx_clist_state * pcls,
  119.   const byte * data, uint width_bits, uint height, uint raster, int op_size,
  120.          int compression_mask, byte ** pdp, uint * psize)
  121. {
  122.     uint short_raster, full_raster;
  123.     uint short_size =
  124.     clist_bitmap_bytes(width_bits, height,
  125.                compression_mask & ~cmd_mask_compress_any,
  126.                &short_raster, &full_raster);
  127.     uint uncompressed_raster;
  128.     uint uncompressed_size =
  129.     clist_bitmap_bytes(width_bits, height, compression_mask,
  130.                &uncompressed_raster, &full_raster);
  131.     uint max_size = cbuf_size - op_size;
  132.     gs_memory_t *mem = (cldev->memory ? cldev->memory : &gs_memory_default);
  133.     byte *dp;
  134.     int compress = 0;
  135.  
  136.     /*
  137.      * See if compressing the bits is possible and worthwhile.
  138.      * Currently we can't compress if the compressed data won't fit in
  139.      * the command reading buffer, or if the decompressed data won't fit
  140.      * in the buffer and decompress_elsewhere isn't set.
  141.      */
  142.     if (short_size >= 50 &&
  143.     (compression_mask & cmd_mask_compress_any) != 0 &&
  144.     (uncompressed_size <= max_size ||
  145.      (compression_mask & decompress_elsewhere) != 0)
  146.     ) {
  147.     union ss_ {
  148.         stream_state ss;
  149.         stream_CFE_state cf;
  150.         stream_RLE_state rl;
  151.     } sstate;
  152.     int code;
  153.     int try_size = op_size + min(uncompressed_size, max_size);
  154.  
  155.     *psize = try_size;
  156.     code = (pcls != 0 ?
  157.         set_cmd_put_op(dp, cldev, pcls, 0, try_size) :
  158.         set_cmd_put_all_op(dp, cldev, 0, try_size));
  159.     if (code < 0)
  160.         return code;
  161.     cmd_uncount_op(0, try_size);
  162.     /*
  163.      * Note that we currently keep all the padding if we are
  164.      * compressing.  This is ridiculous, but it's too hard to
  165.      * change right now.
  166.      */
  167.     if (compression_mask & (1 << cmd_compress_cfe)) {
  168.         /* Try CCITTFax compression. */
  169.         clist_cfe_init(&sstate.cf,
  170.                uncompressed_raster << 3 /*width_bits*/,
  171.                mem);
  172.         compress = cmd_compress_cfe;
  173.     } else if (compression_mask & (1 << cmd_compress_rle)) {
  174.         /* Try RLE compression. */
  175.         clist_rle_init(&sstate.rl);
  176.         compress = cmd_compress_rle;
  177.     }
  178.     if (compress) {
  179.         byte *wbase = dp + (op_size - 1);
  180.         stream_cursor_write w;
  181.  
  182.         /*
  183.          * We can give up on compressing if we generate too much
  184.          * output to fit in the command reading buffer, or too
  185.          * much to make compression worthwhile.
  186.          */
  187.         uint wmax = min(uncompressed_size, max_size);
  188.         int status;
  189.  
  190.         w.ptr = wbase;
  191.         w.limit = w.ptr + min(wmax, short_size >> 1);
  192.         status = cmd_compress_bitmap((stream_state *) & sstate, data,
  193.                   uncompressed_raster << 3 /*width_bits */ ,
  194.                      raster, height, &w);
  195.         if (status == 0) {    /* Use compressed representation. */
  196.         uint wcount = w.ptr - wbase;
  197.  
  198.         cmd_shorten_list_op(cldev,
  199.                  (pcls ? &pcls->list : &cldev->band_range_list),
  200.                     try_size - (op_size + wcount));
  201.         *psize = op_size + wcount;
  202.         goto out;
  203.         }
  204.     }
  205.     if (uncompressed_size > max_size) {
  206.         /* Shorten to zero, erasing the operation altogether */
  207.         if_debug1 ('L', "[L]Uncompressed bits %u too large for buffer\n",
  208.                uncompressed_size);
  209.         cmd_shorten_list_op(cldev,
  210.                  (pcls ? &pcls->list : &cldev->band_range_list),
  211.                 try_size);
  212.         return_error(gs_error_limitcheck);
  213.     }
  214.     if (uncompressed_size != short_size) {
  215.         if_debug2 ('L', "[L]Shortening bits from %u to %u\n",
  216.                try_size, op_size + short_size);
  217.         cmd_shorten_list_op(cldev,
  218.                  (pcls ? &pcls->list : &cldev->band_range_list),
  219.                 try_size - (op_size + short_size));
  220.         *psize = op_size + short_size;
  221.     }
  222.     compress = 0;
  223.     } else if (uncompressed_size > max_size)
  224.     return_error(gs_error_limitcheck);
  225.     else {
  226.     int code;
  227.  
  228.     *psize = op_size + short_size;
  229.     code = (pcls != 0 ?
  230.         set_cmd_put_op(dp, cldev, pcls, 0, *psize) :
  231.         set_cmd_put_all_op(dp, cldev, 0, *psize));
  232.     if (code < 0)
  233.         return code;
  234.     cmd_uncount_op(0, *psize);
  235.     }
  236.     bytes_copy_rectangle(dp + op_size, short_raster, data, raster,
  237.              short_raster, height);
  238. out:
  239.     *pdp = dp;
  240.     return compress;
  241. }
  242.  
  243. /* Add a command to set the tile size and depth. */
  244. private uint
  245. cmd_size_tile_params(const gx_strip_bitmap * tile)
  246. {
  247.     return 2 + cmd_size_w(tile->rep_width) + cmd_size_w(tile->rep_height) +
  248.     (tile->rep_width == tile->size.x ? 0 :
  249.      cmd_size_w(tile->size.x / tile->rep_width)) +
  250.     (tile->rep_height == tile->size.y ? 0 :
  251.      cmd_size_w(tile->size.y / tile->rep_height)) +
  252.     (tile->rep_shift == 0 ? 0 : cmd_size_w(tile->rep_shift));
  253. }
  254. private void
  255. cmd_store_tile_params(byte * dp, const gx_strip_bitmap * tile, int depth,
  256.               uint csize)
  257. {
  258.     byte *p = dp + 2;
  259.     byte bd = depth - 1;
  260.  
  261.     *dp = cmd_count_op(cmd_opv_set_tile_size, csize);
  262.     p = cmd_put_w(tile->rep_width, p);
  263.     p = cmd_put_w(tile->rep_height, p);
  264.     if (tile->rep_width != tile->size.x) {
  265.     p = cmd_put_w(tile->size.x / tile->rep_width, p);
  266.     bd |= 0x20;
  267.     }
  268.     if (tile->rep_height != tile->size.y) {
  269.     p = cmd_put_w(tile->size.y / tile->rep_height, p);
  270.     bd |= 0x40;
  271.     }
  272.     if (tile->rep_shift != 0) {
  273.     cmd_put_w(tile->rep_shift, p);
  274.     bd |= 0x80;
  275.     }
  276.     dp[1] = bd;
  277. }
  278.  
  279. /* Add a command to set the tile index. */
  280. /* This is a relatively high-frequency operation, so we declare it `inline'. */
  281. inline private int
  282. cmd_put_tile_index(gx_device_clist_writer *cldev, gx_clist_state *pcls,
  283.            uint indx)
  284. {
  285.     int idelta = indx - pcls->tile_index + 8;
  286.     byte *dp;
  287.     int code;
  288.  
  289.     if (!(idelta & ~15)) {
  290.     code = set_cmd_put_op(dp, cldev, pcls,
  291.                   cmd_op_delta_tile_index + idelta, 1);
  292.     if (code < 0)
  293.         return code;
  294.     } else {
  295.     code = set_cmd_put_op(dp, cldev, pcls,
  296.                   cmd_op_set_tile_index + (indx >> 8), 2);
  297.     if (code < 0)
  298.         return code;
  299.     dp[1] = indx & 0xff;
  300.     }
  301.     if_debug2('L', "[L]writing index=%u, offset=%lu\n",
  302.           indx, cldev->tile_table[indx].offset);
  303.     return 0;
  304. }
  305.  
  306. /* If necessary, write out data for a single color map. */
  307. int
  308. cmd_put_color_map(gx_device_clist_writer * cldev, cmd_map_index map_index,
  309.           const gx_transfer_map * map, gs_id * pid)
  310. {
  311.     byte *dp;
  312.     int code;
  313.  
  314.     if (map == 0) {
  315.     if (pid && *pid == gs_no_id)
  316.         return 0;    /* no need to write */
  317.     code = set_cmd_put_all_op(dp, cldev, cmd_opv_set_misc, 2);
  318.     if (code < 0)
  319.         return code;
  320.     dp[1] = cmd_set_misc_map + (cmd_map_none << 4) + map_index;
  321.     if (pid)
  322.         *pid = gs_no_id;
  323.     } else {
  324.     if (pid && map->id == *pid)
  325.         return 0;    /* no need to write */
  326.     if (map->proc == gs_identity_transfer) {
  327.         code = set_cmd_put_all_op(dp, cldev, cmd_opv_set_misc, 2);
  328.         if (code < 0)
  329.         return code;
  330.         dp[1] = cmd_set_misc_map + (cmd_map_identity << 4) + map_index;
  331.     } else {
  332.         code = set_cmd_put_all_op(dp, cldev, cmd_opv_set_misc,
  333.                       2 + sizeof(map->values));
  334.         if (code < 0)
  335.         return code;
  336.         dp[1] = cmd_set_misc_map + (cmd_map_other << 4) + map_index;
  337.         memcpy(dp + 2, map->values, sizeof(map->values));
  338.     }
  339.     if (pid)
  340.         *pid = map->id;
  341.     }
  342.     return 0;
  343. }
  344.  
  345. /* ------ Tile cache management ------ */
  346.  
  347. /* We want consecutive ids to map to consecutive hash slots if possible, */
  348. /* so we can use a delta representation when setting the index. */
  349. /* NB that we cannot emit 'delta' style tile indices if VM error recovery */
  350. /* is in effect, since reader & writer's tile indices may get out of phase */
  351. /* as a consequence of error recovery occurring. */
  352. #define tile_id_hash(id) (id)
  353. #define tile_hash_next(index) ((index) + 413)    /* arbitrary large odd # */
  354. typedef struct tile_loc_s {
  355.     uint index;
  356.     tile_slot *tile;
  357. } tile_loc;
  358.  
  359. /* Look up a tile or character in the cache.  If found, set the index and */
  360. /* pointer; if not, set the index to the insertion point. */
  361. private bool
  362. clist_find_bits(gx_device_clist_writer * cldev, gx_bitmap_id id, tile_loc * ploc)
  363. {
  364.     uint index = tile_id_hash(id);
  365.     const tile_hash *table = cldev->tile_table;
  366.     uint mask = cldev->tile_hash_mask;
  367.     ulong offset;
  368.  
  369.     for (; (offset = table[index &= mask].offset) != 0;
  370.      index = tile_hash_next(index)
  371.     ) {
  372.     tile_slot *tile = (tile_slot *) (cldev->data + offset);
  373.  
  374.     if (tile->id == id) {
  375.         ploc->index = index;
  376.         ploc->tile = tile;
  377.         return true;
  378.     }
  379.     }
  380.     ploc->index = index;
  381.     return false;
  382. }
  383.  
  384. /* Delete a tile from the cache. */
  385. private void
  386. clist_delete_tile(gx_device_clist_writer * cldev, tile_slot * slot)
  387. {
  388.     tile_hash *table = cldev->tile_table;
  389.     uint mask = cldev->tile_hash_mask;
  390.     uint index = slot->index;
  391.     ulong offset;
  392.  
  393.     if_debug2('L', "[L]deleting index=%u, offset=%lu\n",
  394.           index, (ulong) ((byte *) slot - cldev->data));
  395.     gx_bits_cache_free(&cldev->bits, (gx_cached_bits_head *) slot,
  396.                &cldev->chunk);
  397.     table[index].offset = 0;
  398.     /* Delete the entry from the hash table. */
  399.     /* We'd like to move up any later entries, so that we don't need */
  400.     /* a deleted mark, but it's too difficult to note this in the */
  401.     /* band list, so instead, we just delete any entries that */
  402.     /* would need to be moved. */
  403.     while ((offset = table[index = tile_hash_next(index) & mask].offset) != 0) {
  404.     tile_slot *tile = (tile_slot *) (cldev->data + offset);
  405.     tile_loc loc;
  406.  
  407.     if (!clist_find_bits(cldev, tile->id, &loc)) {    /* We didn't find it, so it should be moved into a slot */
  408.         /* that we just vacated; instead, delete it. */
  409.         if_debug2('L', "[L]move-deleting index=%u, offset=%lu\n",
  410.               index, offset);
  411.         gx_bits_cache_free(&cldev->bits,
  412.                  (gx_cached_bits_head *) (cldev->data + offset),
  413.                    &cldev->chunk);
  414.         table[index].offset = 0;
  415.     }
  416.     }
  417. }
  418.  
  419. /* Add a tile to the cache. */
  420. /* tile->raster holds the raster for the replicated tile; */
  421. /* we pass the raster of the actual data separately. */
  422. private int
  423. clist_add_tile(gx_device_clist_writer * cldev, const gx_strip_bitmap * tiles,
  424.            uint sraster, int depth)
  425. {
  426.     uint raster = tiles->raster;
  427.     uint size_bytes = raster * tiles->size.y;
  428.     uint tsize =
  429.     sizeof(tile_slot) + cldev->tile_band_mask_size + size_bytes;
  430.     gx_cached_bits_head *slot_head;
  431.  
  432. #define slot ((tile_slot *)slot_head)
  433.  
  434.     if (cldev->bits.csize == cldev->tile_max_count) {    /* Don't let the hash table get too full: delete an entry. */
  435.     /* Since gx_bits_cache_alloc returns an entry to delete when */
  436.     /* it fails, just force it to fail. */
  437.     gx_bits_cache_alloc(&cldev->bits, (ulong) cldev->chunk.size,
  438.                 &slot_head);
  439.     if (slot_head == 0) {    /* Wrap around and retry. */
  440.         cldev->bits.cnext = 0;
  441.         gx_bits_cache_alloc(&cldev->bits, (ulong) cldev->chunk.size,
  442.                 &slot_head);
  443. #ifdef DEBUG
  444.         if (slot_head == 0) {
  445.         lprintf("No entry to delete!\n");
  446.         return_error(gs_error_Fatal);
  447.         }
  448. #endif
  449.     }
  450.     clist_delete_tile(cldev, slot);
  451.     }
  452.     /* Allocate the space for the new entry, deleting entries as needed. */
  453.     while (gx_bits_cache_alloc(&cldev->bits, (ulong) tsize, &slot_head) < 0) {
  454.     if (slot_head == 0) {    /* Wrap around. */
  455.         if (cldev->bits.cnext == 0) {    /* Too big to fit.  We should probably detect this */
  456.         /* sooner, since if we get here, we've cleared the */
  457.         /* cache. */
  458.         return_error(gs_error_limitcheck);
  459.         }
  460.         cldev->bits.cnext = 0;
  461.     } else
  462.         clist_delete_tile(cldev, slot);
  463.     }
  464.     /* Fill in the entry. */
  465.     slot->cb_depth = depth;
  466.     slot->cb_raster = raster;
  467.     slot->width = tiles->rep_width;
  468.     slot->height = tiles->rep_height;
  469.     slot->shift = slot->rep_shift = tiles->rep_shift;
  470.     slot->x_reps = slot->y_reps = 1;
  471.     slot->id = tiles->id;
  472.     memset(ts_mask(slot), 0, cldev->tile_band_mask_size);
  473.     bytes_copy_rectangle(ts_bits(cldev, slot), raster,
  474.              tiles->data, sraster,
  475.              (tiles->rep_width * depth + 7) >> 3,
  476.              tiles->rep_height);
  477.     /* Make the hash table entry. */
  478.     {
  479.     tile_loc loc;
  480.  
  481. #ifdef DEBUG
  482.     if (clist_find_bits(cldev, tiles->id, &loc))
  483.         lprintf1("clist_find_bits(0x%lx) should have failed!\n",
  484.              (ulong) tiles->id);
  485. #else
  486.     clist_find_bits(cldev, tiles->id, &loc);    /* always fails */
  487. #endif
  488.     slot->index = loc.index;
  489.     cldev->tile_table[loc.index].offset =
  490.         (byte *) slot_head - cldev->data;
  491.     if_debug2('L', "[L]adding index=%u, offset=%lu\n",
  492.           loc.index, cldev->tile_table[loc.index].offset);
  493.     }
  494.     slot->num_bands = 0;
  495.     return 0;
  496. }
  497.  
  498. /* ------ Driver procedure support ------ */
  499.  
  500. /* Change the tile parameters (size and depth). */
  501. /* Currently we do this for all bands at once. */
  502. private void
  503. clist_new_tile_params(gx_strip_bitmap * new_tile, const gx_strip_bitmap * tiles,
  504.               int depth, const gx_device_clist_writer * cldev)
  505. {                /*
  506.                  * Adjust the replication factors.  If we can, we replicate
  507.                  * the tile in X up to 32 bytes, and then in Y up to 4 copies,
  508.                  * as long as we don't exceed a total tile size of 256 bytes,
  509.                  * or more than 255 repetitions in X or Y, or make the tile so
  510.                  * large that not all possible tiles will fit in the cache.
  511.                  * Also, don't attempt Y replication if shifting is required. 
  512.                  */
  513. #define max_tile_reps_x 255
  514. #define max_tile_bytes_x 32
  515. #define max_tile_reps_y 4
  516. #define max_tile_bytes 256
  517.     uint rep_width = tiles->rep_width;
  518.     uint rep_height = tiles->rep_height;
  519.     uint rep_width_bits = rep_width * depth;
  520.     uint tile_overhead =
  521.     sizeof(tile_slot) + cldev->tile_band_mask_size;
  522.     uint max_bytes = cldev->chunk.size / (rep_width_bits * rep_height);
  523.  
  524.     max_bytes -= min(max_bytes, tile_overhead);
  525.     if (max_bytes > max_tile_bytes)
  526.     max_bytes = max_tile_bytes;
  527.     *new_tile = *tiles;
  528.     {
  529.     uint max_bits_x = max_bytes * 8 / rep_height;
  530.     uint reps_x =
  531.     min(max_bits_x, max_tile_bytes_x * 8) / rep_width_bits;
  532.     uint reps_y;
  533.  
  534.     while (reps_x > max_tile_reps_x)
  535.         reps_x >>= 1;
  536.     new_tile->size.x = max(reps_x, 1) * rep_width;
  537.     new_tile->raster = bitmap_raster(new_tile->size.x * depth);
  538.     if (tiles->shift != 0)
  539.         reps_y = 1;
  540.     else {
  541.         reps_y = max_bytes / (new_tile->raster * rep_height);
  542.         if (reps_y > max_tile_reps_y)
  543.         reps_y = max_tile_reps_y;
  544.         else if (reps_y < 1)
  545.         reps_y = 1;
  546.     }
  547.     new_tile->size.y = reps_y * rep_height;
  548.     }
  549. #undef max_tile_reps_x
  550. #undef max_tile_bytes_x
  551. #undef max_tile_reps_y
  552. #undef max_tile_bytes
  553. }
  554.  
  555. /* Change tile for clist_tile_rectangle. */
  556. int
  557. clist_change_tile(gx_device_clist_writer * cldev, gx_clist_state * pcls,
  558.           const gx_strip_bitmap * tiles, int depth)
  559. {
  560.     tile_loc loc;
  561.     int code;
  562.  
  563. #define tile_params_differ(cldev, tiles, depth)\
  564.   ((tiles)->rep_width != (cldev)->tile_params.rep_width ||\
  565.    (tiles)->rep_height != (cldev)->tile_params.rep_height ||\
  566.    (tiles)->rep_shift != (cldev)->tile_params.rep_shift ||\
  567.    (depth) != (cldev)->tile_depth)
  568.  
  569.   top:if (clist_find_bits(cldev, tiles->id, &loc)) {    /* The bitmap is in the cache.  Check whether this band */
  570.     /* knows about it. */
  571.     int band_index = pcls - cldev->states;
  572.     byte *bptr = ts_mask(loc.tile) + (band_index >> 3);
  573.     byte bmask = 1 << (band_index & 7);
  574.  
  575.     if (*bptr & bmask) {    /* Already known.  Just set the index. */
  576.         if (pcls->tile_index == loc.index)
  577.         return 0;
  578.         if ((code = cmd_put_tile_index(cldev, pcls, loc.index)) < 0)
  579.             return code;
  580.     } else {
  581.         uint extra = 0;
  582.  
  583.         if tile_params_differ
  584.         (cldev, tiles, depth) {        /*
  585.                          * We have a cached tile whose parameters differ from
  586.                          * the current ones.  Because of the way tile IDs are
  587.                          * managed, this is currently only possible when mixing
  588.                          * Patterns and halftones, but if we didn't generate new
  589.                          * IDs each time the main halftone cache needed to be
  590.                          * refreshed, this could also happen simply from
  591.                          * switching screens.
  592.                          */
  593.         int band;
  594.  
  595.         clist_new_tile_params(&cldev->tile_params, tiles, depth,
  596.                       cldev);
  597.         cldev->tile_depth = depth;
  598.         /* No band knows about the new parameters. */
  599.         for (band = cldev->tile_known_min;
  600.              band <= cldev->tile_known_max;
  601.              ++band
  602.             )
  603.             cldev->states[band].known &= ~tile_params_known;
  604.         cldev->tile_known_min = cldev->nbands;
  605.         cldev->tile_known_max = -1;
  606.         }
  607.         if (!(pcls->known & tile_params_known)) {    /* We're going to have to write the tile parameters. */
  608.         extra = cmd_size_tile_params(&cldev->tile_params);
  609.         } {            /*
  610.                  * This band doesn't know this tile yet, so output the
  611.                  * bits.  Note that the offset we write is the one used by
  612.                  * the reading phase, not the writing phase.  Note also
  613.                  * that the size of the cached and written tile may differ
  614.                  * from that of the client's tile.  Finally, note that
  615.                  * this tile's size parameters are guaranteed to be
  616.                  * compatible with those stored in the device
  617.                  * (cldev->tile_params).
  618.                  */
  619.         ulong offset = (byte *) loc.tile - cldev->chunk.data;
  620.         uint rsize =
  621.             extra + 1 + cmd_size_w(loc.index) + cmd_size_w(offset);
  622.         byte *dp;
  623.         uint csize;
  624.         int code =
  625.         cmd_put_bits(cldev, pcls, ts_bits(cldev, loc.tile),
  626.                  tiles->rep_width * depth, tiles->rep_height,
  627.                  loc.tile->cb_raster, rsize,
  628.                  (cldev->tile_params.size.x > tiles->rep_width ?
  629.                   decompress_elsewhere | decompress_spread :
  630.                   decompress_elsewhere),
  631.                  &dp, &csize);
  632.  
  633.         if (code < 0)
  634.             return code;
  635.         if (extra) {    /* Write the tile parameters before writing the bits. */
  636.             cmd_store_tile_params(dp, &cldev->tile_params, depth,
  637.                       extra);
  638.             dp += extra;
  639.             /* This band now knows the parameters. */
  640.             pcls->known |= tile_params_known;
  641.             if (band_index < cldev->tile_known_min)
  642.             cldev->tile_known_min = band_index;
  643.             if (band_index > cldev->tile_known_max)
  644.             cldev->tile_known_max = band_index;
  645.         }
  646.         *dp = cmd_count_op(cmd_opv_set_tile_bits, csize - extra);
  647.         dp++;
  648.         dp = cmd_put_w(loc.index, dp);
  649.         cmd_put_w(offset, dp);
  650.         *bptr |= bmask;
  651.         loc.tile->num_bands++;
  652.         }
  653.     }
  654.     pcls->tile_index = loc.index;
  655.     pcls->tile_id = loc.tile->id;
  656.     return 0;
  657.     }
  658.     /* The tile is not in the cache, add it. */
  659.     {
  660.     gx_strip_bitmap new_tile;
  661.     gx_strip_bitmap *ptile;
  662.  
  663.     /* Ensure that the tile size is compatible. */
  664.     if (tile_params_differ(cldev, tiles, depth)) {    /* We'll reset cldev->tile_params when we write the bits. */
  665.         clist_new_tile_params(&new_tile, tiles, depth, cldev);
  666.         ptile = &new_tile;
  667.     } else {
  668.         cldev->tile_params.id = tiles->id;
  669.         cldev->tile_params.data = tiles->data;
  670.         ptile = &cldev->tile_params;
  671.     }
  672.     code = clist_add_tile(cldev, ptile, tiles->raster, depth);
  673.     if (code < 0)
  674.         return code;
  675.     }
  676.     goto top;
  677. #undef tile_params_differ
  678. }
  679.  
  680. /* Change "tile" for clist_copy_*.  tiles->[rep_]shift must be zero. */
  681. int
  682. clist_change_bits(gx_device_clist_writer * cldev, gx_clist_state * pcls,
  683.           const gx_strip_bitmap * tiles, int depth)
  684. {
  685.     tile_loc loc;
  686.     int code;
  687.  
  688.   top:if (clist_find_bits(cldev, tiles->id, &loc)) {    /* The bitmap is in the cache.  Check whether this band */
  689.     /* knows about it. */
  690.     uint band_index = pcls - cldev->states;
  691.     byte *bptr = ts_mask(loc.tile) + (band_index >> 3);
  692.     byte bmask = 1 << (band_index & 7);
  693.  
  694.     if (*bptr & bmask) {    /* Already known.  Just set the index. */
  695.         if (pcls->tile_index == loc.index)
  696.         return 0;
  697.         cmd_put_tile_index(cldev, pcls, loc.index);
  698.     } else {        /* Not known yet.  Output the bits. */
  699.         /* Note that the offset we write is the one used by */
  700.         /* the reading phase, not the writing phase. */
  701.         ulong offset = (byte *) loc.tile - cldev->chunk.data;
  702.         uint rsize = 2 + cmd_size_w(loc.tile->width) +
  703.         cmd_size_w(loc.tile->height) + cmd_size_w(loc.index) +
  704.         cmd_size_w(offset);
  705.         byte *dp;
  706.         uint csize;
  707.         gx_clist_state *bit_pcls = pcls;
  708.         int code;
  709.  
  710.         if (loc.tile->num_bands == CHAR_ALL_BANDS_COUNT)
  711.         bit_pcls = NULL;
  712.         code = cmd_put_bits(cldev, bit_pcls, ts_bits(cldev, loc.tile),
  713.                 loc.tile->width * depth,
  714.                 loc.tile->height, loc.tile->cb_raster,
  715.                 rsize,
  716.                  (1 << cmd_compress_cfe) | decompress_elsewhere,
  717.                 &dp, &csize);
  718.  
  719.         if (code < 0)
  720.         return code;
  721.         *dp = cmd_count_op(cmd_opv_set_bits, csize);
  722.         dp[1] = (depth << 2) + code;
  723.         dp += 2;
  724.         dp = cmd_put_w(loc.tile->width, dp);
  725.         dp = cmd_put_w(loc.tile->height, dp);
  726.         dp = cmd_put_w(loc.index, dp);
  727.         cmd_put_w(offset, dp);
  728.         if (bit_pcls == NULL) {
  729.         memset(ts_mask(loc.tile), 0xff,
  730.                cldev->tile_band_mask_size);
  731.         loc.tile->num_bands = cldev->nbands;
  732.         } else {
  733.         *bptr |= bmask;
  734.         loc.tile->num_bands++;
  735.         }
  736.     }
  737.     pcls->tile_index = loc.index;
  738.     pcls->tile_id = loc.tile->id;
  739.     return 0;
  740.     }
  741.     /* The tile is not in the cache. */
  742.     code = clist_add_tile(cldev, tiles, tiles->raster, depth);
  743.     if (code < 0)
  744.     return code;
  745.     goto top;
  746. }
  747.