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

  1. /* Copyright (C) 1989, 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. /* gxht.c */
  20. /* Halftone rendering routines for Ghostscript imaging library */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gsutil.h"            /* for gs_next_ids */
  26. #include "gxfixed.h"
  27. #include "gzstate.h"
  28. #include "gxdevice.h"            /* for gzht.h */
  29. #include "gzht.h"
  30.  
  31. /*** Big memory machines ***/
  32. #define max_cached_tiles_LARGE 257
  33. #define max_ht_bits_LARGE 35000
  34. /*** Small memory machines ***/
  35. #define max_cached_tiles_SMALL 25
  36. #define max_ht_bits_SMALL 1000
  37.  
  38. #if arch_ints_are_short
  39. const uint ht_cache_default_max_tiles = max_cached_tiles_SMALL;
  40. const uint ht_cache_default_max_bits = max_ht_bits_SMALL;
  41. #else
  42. const uint ht_cache_default_max_tiles = max_cached_tiles_LARGE;
  43. const uint ht_cache_default_max_bits = max_ht_bits_LARGE;
  44. #endif
  45.  
  46. private_st_ht_cache();
  47.  
  48. /* Allocate a halftone cache. */
  49. gx_ht_cache *
  50. gx_ht_alloc_cache(gs_memory_t *mem, uint max_tiles, uint max_bits)
  51. {    gx_ht_cache *pcache = gs_alloc_struct(mem, gx_ht_cache, &st_ht_cache,
  52.                           "alloc_ht_cache(struct)");
  53.     byte *cbits = gs_alloc_bytes(mem, max_bits,
  54.                      "alloc_ht_cache(bits)");
  55.     gx_ht_tile *tiles = (gx_ht_tile *)gs_alloc_byte_array(mem,
  56.                 max_tiles, sizeof(gx_ht_tile),
  57.                 "alloc_ht_cache(tiles)");
  58.     if ( pcache == 0 || cbits == 0 || tiles == 0 )
  59.     {    gs_free_object(mem, tiles, "alloc_ht_cache(tiles)");
  60.         gs_free_object(mem, cbits, "alloc_ht_cache(bits)");
  61.         gs_free_object(mem, pcache, "alloc_ht_cache(struct)");
  62.         return 0;
  63.     }
  64.     pcache->bits = cbits;
  65.     pcache->bits_size = max_bits;
  66.     pcache->tiles = tiles;
  67.     pcache->num_tiles = max_tiles;
  68.     gx_ht_clear_cache(pcache);
  69.     return pcache;
  70. }
  71.  
  72. /* Make the cache order current, and return whether */
  73. /* there is room for all possible tiles in the cache. */
  74. bool
  75. gx_check_tile_cache(gs_state *pgs)
  76. {    const gx_ht_order *porder = &pgs->dev_ht->order;
  77.     gx_ht_cache *pcache = pgs->ht_cache;
  78.     if ( pcache->order.bits != porder->bits )
  79.         gx_ht_init_cache(pcache, porder);
  80.     return pcache->levels_per_tile == 1;
  81. }
  82.  
  83. /* Determine whether a given (width, y, height) might fit into a */
  84. /* single tile. If so, return the byte offset of the appropriate row */
  85. /* from the beginning of the tile; if not, return -1. */
  86. int
  87. gx_check_tile_size(gs_state *pgs, int w, int y, int h)
  88. {    int tsy;
  89.     const gx_tile_bitmap *ptile0 =
  90.         &pgs->ht_cache->tiles[0].tile;    /* a typical tile */
  91. #define tile0 (*ptile0)
  92.     if ( h > tile0.rep_height || w > tile0.rep_width )
  93.       return -1;
  94.     tsy = (y + pgs->dev_ht->order.phase.y) % tile0.rep_height;
  95.     if ( tsy + h > tile0.size.y )
  96.       return -1;
  97.     /* Tile fits in Y, might fit in X. */
  98.     return tsy * tile0.raster;
  99. #undef tile0
  100. }
  101.  
  102. /* Render a given level into a halftone cache. */
  103. private int render_ht(P4(gx_ht_tile *, int, const gx_ht_order *,
  104.              gx_bitmap_id));
  105. gx_tile_bitmap *
  106. gx_render_ht(gx_ht_cache *pcache, int b_level)
  107. {    gx_ht_order *porder = &pcache->order;
  108.     int level = porder->levels[b_level];
  109.     gx_ht_tile *bt = &pcache->tiles[level / pcache->levels_per_tile];
  110.     if ( bt->level != level )
  111.     {    int code = render_ht(bt, level, porder, pcache->base_id);
  112.         if ( code < 0 ) return 0;
  113.     }
  114.     return &bt->tile;
  115. }
  116.  
  117. /* Load the device color into the halftone cache if needed. */
  118. int
  119. gx_dc_ht_binary_load(gx_device_color *pdevc, const gs_state *pgs)
  120. {    const gx_ht_order *porder = &pgs->dev_ht->order;
  121.     gx_ht_cache *pcache = pgs->ht_cache;
  122.     gx_tile_bitmap *tile;
  123.     if ( pcache->order.bits != porder->bits )
  124.         gx_ht_init_cache(pcache, porder);
  125.     tile = gx_render_ht(pcache, pdevc->colors.binary.b_level);
  126.     if ( tile == 0 )
  127.         return_error(gs_error_Fatal);
  128.     pdevc->colors.binary.b_tile = tile;
  129.     return 0;
  130. }
  131.  
  132. /* Initialize the tile cache for a given screen. */
  133. /* Cache as many different levels as will fit. */
  134. void
  135. gx_ht_init_cache(gx_ht_cache *pcache, const gx_ht_order *porder)
  136. {    uint width = porder->width;
  137.     uint height = porder->height;
  138.     uint size = width * height + 1;
  139.     int width_unit =
  140.         (width <= ht_mask_bits / 2 ? ht_mask_bits / width * width :
  141.          width);
  142.     int height_unit = height;
  143.     uint raster = porder->raster;
  144.     uint tile_bytes = raster * height;
  145.     int num_cached;
  146.     int i;
  147.     byte *tbits = pcache->bits;
  148.     /* Make sure num_cached is within bounds */
  149.     num_cached = pcache->bits_size / tile_bytes;
  150.     if ( num_cached > size )
  151.         num_cached = size;
  152.     if ( num_cached > pcache->num_tiles )
  153.         num_cached = pcache->num_tiles;
  154.     if ( num_cached == size &&
  155.          tile_bytes * num_cached <= pcache->bits_size / 2
  156.        )
  157.        {    /* We can afford to replicate every tile vertically, */
  158.         /* which will reduce breakage when tiling. */
  159.         height_unit <<= 1, tile_bytes <<= 1;
  160.        }
  161.     pcache->base_id = gs_next_ids(size);
  162.     pcache->order = *porder;
  163.     pcache->num_cached = num_cached;
  164.     pcache->levels_per_tile = (size + num_cached - 1) / num_cached;
  165.     memset(tbits, 0, pcache->bits_size);
  166.     for ( i = 0; i < num_cached; i++, tbits += tile_bytes )
  167.     {    register gx_ht_tile *bt = &pcache->tiles[i];
  168.         bt->level = 0;
  169.         bt->tile.data = tbits;
  170.         bt->tile.raster = raster;
  171.         bt->tile.size.x = width_unit;
  172.         bt->tile.size.y = height_unit;
  173.         bt->tile.rep_width = width;
  174.         bt->tile.rep_height = height;
  175.     }
  176. }
  177.  
  178. /*
  179.  * Compute and save the rendering of a given gray level
  180.  * with the current halftone.  The cache holds multiple tiles,
  181.  * where each tile covers a range of possible levels.
  182.  * We adjust the tile whose range includes the desired level incrementally;
  183.  * this saves a lot of time for the average image, where gray levels
  184.  * don't change abruptly.  Note that the "level" is the number of bits,
  185.  * not the index in the levels vector.
  186.  */
  187. private int
  188. render_ht(gx_ht_tile *pbt, int level /* [1..num_bits-1] */,
  189.   const gx_ht_order *porder, gx_bitmap_id base_id)
  190. {    int old_level = pbt->level;
  191.     register gx_ht_bit *p = &porder->bits[old_level];
  192.     register byte *data = pbt->tile.data;
  193.     if_debug7('H', "[H]Halftone cache slot 0x%lx: old=%d, new=%d, w=%d(%d), h=%d(%d):\n",
  194.          (ulong)data, old_level, level, pbt->tile.size.x,
  195.          porder->width, pbt->tile.size.y, porder->height);
  196. #ifdef DEBUG
  197.     if ( level < 0 || level > porder->num_bits )
  198.     {    lprintf3("Error in render_ht: level=%d, old_level=%d, num_bits=%d\n", level, old_level, porder->num_bits);
  199.         return_error(gs_error_Fatal);
  200.     }
  201. #endif
  202.     /* Invert bits between the two pointers. */
  203.     /* Note that we can use the same loop to turn bits either */
  204.     /* on or off, using xor. */
  205.     /* The Borland compiler generates truly dreadful code */
  206.     /* if we don't assign the offset to a temporary. */
  207. #if arch_ints_are_short
  208. #  define invert(i)\
  209.      { uint off = p[i].offset; *(ht_mask_t *)&data[off] ^= p[i].mask; }
  210. #else
  211. #  define invert(i) *(ht_mask_t *)&data[p[i].offset] ^= p[i].mask
  212. #endif
  213. sw:    switch ( level - old_level )
  214.     {
  215.     default:
  216.         if ( level > old_level )
  217.         {    invert(0); invert(1); invert(2); invert(3);
  218.             p += 4; old_level += 4;
  219.         }
  220.         else
  221.         {    invert(-1); invert(-2); invert(-3); invert(-4);
  222.             p -= 4; old_level -= 4;
  223.         }
  224.         goto sw;
  225.     case 7: invert(6);
  226.     case 6: invert(5);
  227.     case 5: invert(4);
  228.     case 4: invert(3);
  229.     case 3: invert(2);
  230.     case 2: invert(1);
  231.     case 1: invert(0);
  232.     case 0: break;            /* Shouldn't happen! */
  233.     case -7: invert(-7);
  234.     case -6: invert(-6);
  235.     case -5: invert(-5);
  236.     case -4: invert(-4);
  237.     case -3: invert(-3);
  238.     case -2: invert(-2);
  239.     case -1: invert(-1);
  240.     }
  241. #undef invert
  242. #ifdef DEBUG
  243. if ( gs_debug_c('H') )
  244.     {    byte *p = data;
  245.         int wb = pbt->tile.raster;
  246.         byte *ptr = data + wb * pbt->tile.size.y;
  247.         while ( p < ptr )
  248.         {    dprintf8(" %d%d%d%d%d%d%d%d",
  249.                  *p >> 7, (*p >> 6) & 1, (*p >> 5) & 1,
  250.                  (*p >> 4) & 1, (*p >> 3) & 1, (*p >> 2) & 1,
  251.                  (*p >> 1) & 1, *p & 1);
  252.             if ( (++p - data) % wb == 0 ) dputc('\n');
  253.         }
  254.     }
  255. #endif
  256.     pbt->level = level;
  257.     pbt->tile.id = base_id + level;
  258.     if ( pbt->tile.size.y > pbt->tile.rep_height )
  259.       { /* Replicate the tile in Y.  We only do this when */
  260.         /* all the renderings will fit in the cache, */
  261.         /* so we only do it once per level, and it doesn't */
  262.         /* have to be very efficient. */
  263.         uint rh = pbt->tile.rep_height;
  264.         uint h = pbt->tile.size.y;
  265.         uint tsize = pbt->tile.raster * rh;
  266.         while ( (h -= rh) != 0 )
  267.           { memcpy(data + tsize, data, tsize);
  268.         data += tsize;
  269.           }
  270.       }
  271.     return 0;
  272. }
  273.