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

  1. /* Copyright (C) 1993, 1994 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. /* gxpcmap.c */
  20. /* Pattern color mapping for Ghostscript 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 "gsmatrix.h"
  28. #include "gscspace.h"        /* for gscolor2.h */
  29. #include "gxcolor2.h"
  30. #include "gxdevice.h"
  31. #include "gxdevmem.h"
  32. #include "gxpcolor.h"
  33. #include "gzstate.h"
  34.  
  35. /* Define the size of the Pattern cache.  These are adhoc.... */
  36.  
  37. /*** Big memory machines ***/
  38. #define max_cached_patterns_LARGE 50
  39. #define max_pattern_bits_LARGE 100000
  40. /*** Small memory machines ***/
  41. #define max_cached_patterns_SMALL 5
  42. #define max_pattern_bits_SMALL 1000
  43.  
  44. #if arch_ints_are_short
  45. const uint pattern_cache_default_max_tiles = max_cached_patterns_SMALL;
  46. const ulong pattern_cache_default_max_bits = max_pattern_bits_SMALL;
  47. #else
  48. const uint pattern_cache_default_max_tiles = max_cached_patterns_LARGE;
  49. const ulong pattern_cache_default_max_bits = max_pattern_bits_LARGE;
  50. #endif
  51.  
  52. /* Define the structures for Pattern rendering and caching. */
  53. private_st_color_tile();
  54. private_st_color_tile_element();
  55. private_st_pattern_cache();
  56. private_st_device_pattern_accum();
  57.  
  58. /* ------ Pattern rendering ------ */
  59.  
  60. /* Device procedures */
  61. private dev_proc_open_device(pattern_accum_open);
  62. private dev_proc_close_device(pattern_accum_close);
  63. private dev_proc_fill_rectangle(pattern_accum_fill_rectangle);
  64. private dev_proc_copy_mono(pattern_accum_copy_mono);
  65. private dev_proc_copy_color(pattern_accum_copy_color);
  66.  
  67. /* The device descriptor */
  68. private const gx_device_pattern_accum gs_pattern_accum_device =
  69. {    sizeof(gx_device_pattern_accum),
  70.     0,            /* &gs_pattern_accum_device.std_procs */
  71.     "pattern accumulator",
  72.     0, 0, 72, 72, no_margins, dci_black_and_white,
  73.     dev_init_misc_open(true),
  74.       { pattern_accum_open,
  75.         NULL,
  76.         NULL,
  77.         NULL,
  78.         pattern_accum_close,
  79.         NULL,
  80.         NULL,
  81.         pattern_accum_fill_rectangle,
  82.         NULL,
  83.         pattern_accum_copy_mono,
  84.         pattern_accum_copy_color,
  85.         NULL,
  86.         NULL,
  87.         NULL,
  88.         NULL,
  89.         NULL,
  90.         NULL,
  91.         NULL,
  92.         NULL,
  93.         NULL
  94.       },
  95.     0,                /* target */
  96.     0, 0, 0, 0            /* bitmap_memory, bits, mask, instance */
  97. };
  98. #define padev ((gx_device_pattern_accum *)dev)
  99.  
  100. /* Allocate a pattern accumulator. */
  101. gx_device_pattern_accum *
  102. gx_pattern_accum_alloc(gs_memory_t *mem, client_name_t cname)
  103. {    gx_device_pattern_accum *adev =
  104.       gs_alloc_struct(mem, gx_device_pattern_accum,
  105.               &st_device_pattern_accum, cname);
  106.     if ( adev == 0 )
  107.       return 0;
  108.     *adev = gs_pattern_accum_device;
  109.     adev->memory = mem;
  110.     gx_device_forward_fill_in_procs((gx_device_forward *)adev);    /* (should only do once) */
  111.     return adev;
  112. }
  113.  
  114. /* Initialize a pattern accumulator. */
  115. /* Client must already have set instance and bitmap_memory. */
  116. private int
  117. pattern_accum_open(gx_device *dev)
  118. {    const gs_pattern_instance *pinst = padev->instance;
  119.     gs_memory_t *mem = padev->bitmap_memory;
  120.     gx_device_memory *mask =
  121.       gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
  122.               "pattern_accum_open(mask)");
  123.     gx_device_memory *bits = 0;
  124.     gx_device *target = gs_currentdevice(pinst->saved);
  125.     int width = pinst->size.x;
  126.     int height = pinst->size.y;
  127.     int code;
  128.     if ( mask == 0 )
  129.       return_error(gs_error_VMerror);
  130. #define pdset(dev)\
  131.   (dev)->width = width, (dev)->height = height,\
  132.   (dev)->x_pixels_per_inch = target->x_pixels_per_inch,\
  133.   (dev)->y_pixels_per_inch = target->y_pixels_per_inch
  134.     pdset(padev);
  135.     padev->color_info = target->color_info;
  136.     gs_make_mem_mono_device(mask, mem);
  137.     pdset(mask);
  138.     mask->bitmap_memory = mem;
  139.     mask->base = 0;
  140.     code = (*dev_proc(mask, open_device))((gx_device *)mask);
  141.     if ( code >= 0 )
  142.       {    memset(mask->base, 0, mask->raster * mask->height);
  143.         switch ( pinst->template.PaintType )
  144.           {
  145.           case 2:                /* uncolored */
  146.             padev->target = target;
  147.             break;
  148.           case 1:                /* colored */
  149.             bits = gs_alloc_struct(mem, gx_device_memory,
  150.                            &st_device_memory,
  151.                            "pattern_accum_open(bits)");
  152.             if ( bits == 0 )
  153.               return_error(gs_error_VMerror);
  154.             padev->target = (gx_device *)bits;
  155.             gs_make_mem_device(bits, gdev_mem_device_for_bits(target->color_info.depth), mem, -1);
  156.             pdset(bits);
  157. #undef pdset
  158.             bits->color_info = target->color_info;
  159.             bits->target = target;
  160.             bits->bitmap_memory = mem;
  161.             /* Forward the color mappers to the target. */
  162.             /* (Maybe the memory device should do this by */
  163.             /* default if there is a target?) */
  164.             set_dev_proc(bits, map_rgb_color,
  165.                      gx_forward_map_rgb_color);
  166.             set_dev_proc(bits, map_color_rgb,
  167.                      gx_forward_map_color_rgb);
  168.             set_dev_proc(bits, map_cmyk_color,
  169.                      gx_forward_map_cmyk_color);
  170.             set_dev_proc(bits, map_rgb_alpha_color,
  171.                      gx_forward_map_rgb_alpha_color);
  172.             code = (*dev_proc(bits, open_device))((gx_device *)bits);
  173.           }
  174.     }
  175.     if ( code < 0 )
  176.       {    if ( bits != 0 )
  177.           gs_free_object(mem, bits, "pattern_accum_open(bits)");
  178.         (*dev_proc(mask, close_device))((gx_device *)mask);
  179.         gs_free_object(mem, mask, "pattern_accum_open(mask)");
  180.         return code;
  181.       }
  182.     padev->mask = mask;
  183.     padev->bits = bits;
  184.     return code;
  185. }
  186.  
  187. /* Close an accumulator. */
  188. private int
  189. pattern_accum_close(gx_device *dev)
  190. {    gs_memory_t *mem = padev->bitmap_memory;
  191.     if ( padev->bits != 0 )
  192.       {    (*dev_proc(padev->bits, close_device))((gx_device *)padev->bits);
  193.         gs_free_object(mem, padev->bits, "pattern_accum_close(bits)");
  194.         padev->bits = 0;
  195.       }
  196.     (*dev_proc(padev->mask, close_device))((gx_device *)padev->mask);
  197.     gs_free_object(mem, padev->mask, "pattern_accum_close(mask)");
  198.     padev->mask = 0;
  199.     return 0;
  200. }
  201.  
  202. /* Fill a rectangle */
  203. private int
  204. pattern_accum_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  205.   gx_color_index color)
  206. {    if ( padev->bits )
  207.         (*dev_proc(padev->target, fill_rectangle))(padev->target,
  208.             x, y, w, h, color);
  209.     return (*dev_proc(padev->mask, fill_rectangle))((gx_device *)padev->mask,
  210.         x, y, w, h, (gx_color_index)1);
  211. }
  212.  
  213. /* Copy a monochrome bitmap. */
  214. private int
  215. pattern_accum_copy_mono(gx_device *dev, const byte *data, int data_x,
  216.   int raster, gx_bitmap_id id, int x, int y, int w, int h,
  217.   gx_color_index color0, gx_color_index color1)
  218. {    if ( padev->bits )
  219.         (*dev_proc(padev->target, copy_mono))(padev->target,
  220.             data, data_x, raster, id, x, y, w, h, color0, color1);
  221.     if ( color0 != gx_no_color_index )
  222.         color0 = 1;
  223.     if ( color1 != gx_no_color_index )
  224.         color1 = 1;
  225.     if ( color0 == 1 && color1 == 1 )
  226.         return (*dev_proc(padev->mask, fill_rectangle))((gx_device *)padev->mask,
  227.             x, y, w, h, (gx_color_index)1);
  228.     else
  229.         return (*dev_proc(padev->mask, copy_mono))((gx_device *)padev->mask,
  230.             data, data_x, raster, id, x, y, w, h, color0, color1);
  231. }
  232.  
  233. /* Copy a color bitmap. */
  234. private int
  235. pattern_accum_copy_color(gx_device *dev, const byte *data, int data_x,
  236.   int raster, gx_bitmap_id id, int x, int y, int w, int h)
  237. {    if ( padev->bits )
  238.         (*dev_proc(padev->target, copy_color))(padev->target,
  239.             data, data_x, raster, id, x, y, w, h);
  240.     return (*dev_proc(padev->mask, fill_rectangle))((gx_device *)padev->mask,
  241.         x, y, w, h, (gx_color_index)1);
  242. }
  243.  
  244. #undef padev
  245.  
  246. /* ------ Color space implementation ------ */
  247.  
  248. /* Allocate a Pattern cache. */
  249. gx_pattern_cache *
  250. gx_pattern_alloc_cache(gs_memory_t *mem, uint num_tiles, ulong max_bits)
  251. {    gx_pattern_cache *pcache =
  252.       gs_alloc_struct(mem, gx_pattern_cache, &st_pattern_cache,
  253.               "pattern_cache_alloc(struct)");
  254.     gx_color_tile *tiles =
  255.       gs_alloc_struct_array(mem, num_tiles, gx_color_tile,
  256.                 &st_color_tile_element,
  257.                 "pattern_cache_alloc(tiles)");
  258.     uint i;
  259.     if ( pcache == 0 || tiles == 0 )
  260.       { gs_free_object(mem, tiles, "pattern_cache_alloc(tiles)");
  261.         gs_free_object(mem, pcache, "pattern_cache_alloc(struct)");
  262.         return 0;
  263.       }
  264.     pcache->memory = mem;
  265.     pcache->tiles = tiles;
  266.     pcache->num_tiles = num_tiles;
  267.     pcache->tiles_used = 0;
  268.     pcache->next = 0;
  269.     pcache->bits_used = 0;
  270.     pcache->max_bits = max_bits;
  271.     for ( i = 0; i < num_tiles; tiles++, i++ )
  272.       {    tiles->id = gx_no_bitmap_id;
  273.         /* Clear the pointers to pacify the GC. */
  274.         tiles->bits.data = 0;
  275.         tiles->mask.data = 0;
  276.       }
  277.     return pcache;
  278. }
  279. /* Ensure that a gstate has a Pattern cache. */
  280. private int
  281. ensure_pattern_cache(gs_state *pgs)
  282. {    if ( pgs->pattern_cache == 0 )
  283.       { gx_pattern_cache *pcache =
  284.           gx_pattern_alloc_cache(pgs->memory,
  285.                      pattern_cache_default_max_tiles,
  286.                      pattern_cache_default_max_bits);
  287.         if ( pcache == 0 )
  288.           return_error(gs_error_VMerror);
  289.         pgs->pattern_cache = pcache;
  290.       }
  291.     return 0;
  292. }
  293.  
  294. /* Get and set the Pattern cache in a gstate. */
  295. gx_pattern_cache *
  296. gstate_pattern_cache(gs_state *pgs)
  297. {    return pgs->pattern_cache;
  298. }
  299. void
  300. gstate_set_pattern_cache(gs_state *pgs, gx_pattern_cache *pcache)
  301. {    pgs->pattern_cache = pcache;
  302. }
  303.  
  304. /* Free a Pattern cache entry. */
  305. private void
  306. gx_pattern_cache_free_entry(gx_pattern_cache *pcache, gx_color_tile *ctile)
  307. {    if ( ctile->id != gx_no_bitmap_id )
  308.       { if ( ctile->mask.data != 0 )
  309.           { pcache->bits_used -=
  310.           (ulong)ctile->mask.raster * ctile->mask.size.y;
  311.         gs_free_object(pcache->memory, ctile->mask.data,
  312.                    "free_pattern_cache_entry(mask data)");
  313.           }
  314.         if ( ctile->bits.data != 0 )
  315.           { pcache->bits_used -=
  316.           (ulong)ctile->bits.raster * ctile->bits.size.y;
  317.         gs_free_object(pcache->memory, ctile->bits.data,
  318.                    "free_pattern_cache_entry(bits data)");
  319.           }
  320.         ctile->id = gx_no_bitmap_id;
  321.         pcache->tiles_used--;
  322.       }
  323. }
  324.  
  325. /* Add a Pattern cache entry.  This is exported for the interpreter. */
  326. private void make_bitmap(P3(gx_tile_bitmap *, const gx_device_memory *, gx_bitmap_id));
  327. int
  328. gx_pattern_cache_add_entry(gs_state *pgs, gx_device_pattern_accum *padev,
  329.   gx_color_tile **pctile)
  330. {    const gx_device_memory *mbits = padev->bits;
  331.     const gx_device_memory *mmask = padev->mask;
  332.     gx_pattern_cache *pcache;
  333.     ulong used = 0;
  334.     gx_bitmap_id id = padev->instance->id;
  335.     gx_color_tile *ctile;
  336.     int code = ensure_pattern_cache(pgs);
  337.     if ( code < 0 )
  338.       return code;
  339.     pcache = pgs->pattern_cache;
  340.     /* Check whether the pattern completely fills its box. */
  341.     /* If so, we can avoid the expensive masking operations */
  342.     /* when using the pattern. */
  343.     {    int y;
  344.         for ( y = 0; y < mmask->height; y++ )
  345.           { const byte *row = scan_line_base(mmask, y);
  346.             int w;
  347.             for ( w = mmask->width; w > 8; w -= 8 )
  348.               if ( *row++ != 0xff )
  349.             goto keep;
  350.             if ( (*row | (0xff >> w)) != 0xff )
  351.               goto keep;
  352.           }
  353.         /* We don't need a mask. */
  354.         mmask = 0;
  355. keep:        ;
  356.     }
  357.     if ( mbits != 0 )
  358.       used += gdev_mem_bitmap_size(mbits);
  359.     if ( mmask != 0 )
  360.       used += gdev_mem_bitmap_size(mmask);
  361.     ctile = &pcache->tiles[id % pcache->num_tiles];
  362.     gx_pattern_cache_free_entry(pcache, ctile);
  363.     while ( pcache->bits_used + used > pcache->max_bits &&
  364.         pcache->bits_used != 0     /* allow 1 oversized entry (?) */
  365.           )
  366.       { pcache->next = (pcache->next + 1) % pcache->num_tiles;
  367.         gx_pattern_cache_free_entry(pcache, &pcache->tiles[pcache->next]);
  368.       }
  369.     ctile->id = id;
  370.     ctile->depth = padev->color_info.depth;
  371.     if ( mbits != 0 )
  372.       make_bitmap(&ctile->bits, mbits, gs_next_ids(1));
  373.     else
  374.       ctile->bits.data = 0;
  375.     if ( mmask != 0 )
  376.       make_bitmap(&ctile->mask, mmask, id);
  377.     else
  378.       ctile->mask.data = 0;
  379.     pcache->bits_used += used;
  380.     *pctile = ctile;
  381.     return 0;
  382. }
  383. private void
  384. make_bitmap(register gx_tile_bitmap *pbm, const gx_device_memory *mdev,
  385.   gx_bitmap_id id)
  386. {    pbm->data = mdev->base;
  387.     pbm->raster = mdev->raster;
  388.     pbm->rep_width = pbm->size.x = mdev->width;
  389.     pbm->rep_height = pbm->size.y = mdev->height;
  390.     pbm->id = id;
  391. }
  392.  
  393. /* Remap a Pattern color. */
  394. int
  395. gx_remap_Pattern(const gs_client_color *pc, const gs_color_space *pcs,
  396.   gx_device_color *pdc, gs_state *pgs)
  397. {    gs_pattern_instance *pinst = pc->pattern;
  398.     gs_memory_t *mem = pgs->memory;
  399.     gs_state *saved;
  400.     gx_device_pattern_accum adev;
  401.     gx_color_tile *ctile;
  402.     int code;
  403.     if ( pinst == 0 )
  404.     {    /* Null pattern */
  405.         color_set_pattern(pdc, 0);
  406.         pdc->mask = 0;
  407.         return 0;
  408.     }
  409.     if ( pinst->template.PaintType == 2 )    /* uncolored */
  410.     {    code = (*pcs->params.pattern.base_space.type->remap_color)
  411.             (pc, (const gs_color_space *)&pcs->params.pattern.base_space, pdc, pgs);
  412.         if ( code < 0 )
  413.           return code;
  414. #define replace_dc_type(t, pt)\
  415.   if ( pdc->type == &t ) pdc->type = &pt
  416.         replace_dc_type(gx_dc_pure, gx_dc_pure_masked);
  417.         else
  418.             replace_dc_type(gx_dc_ht_binary, gx_dc_binary_masked);
  419.         else
  420.             replace_dc_type(gx_dc_ht_colored, gx_dc_colored_masked);
  421.         else
  422.             return_error(gs_error_unregistered);
  423. #undef replace_dc_type
  424.     }
  425.     else
  426.         color_set_pattern(pdc, 0);
  427.     pdc->id = pinst->id;
  428.     pdc->mask = 0;
  429.     if ( gx_pattern_cache_lookup(pdc, pgs) )
  430.       return 0;
  431.     code = ensure_pattern_cache(pgs);
  432.     if ( code < 0 )
  433.       return code;
  434.     adev = gs_pattern_accum_device;
  435.     gx_device_forward_fill_in_procs((gx_device_forward *)&adev);    /* (should only do once) */
  436.     adev.instance = pinst;
  437.     adev.bitmap_memory = mem;
  438.     code = (*dev_proc(&adev, open_device))((gx_device *)&adev);
  439.     if ( code < 0 )
  440.       return code;
  441.     saved = gs_gstate(pinst->saved);
  442.     if ( saved == 0 )
  443.       return_error(gs_error_VMerror);
  444.     if ( saved->pattern_cache == 0 )
  445.       saved->pattern_cache = pgs->pattern_cache;
  446.     gx_set_device_only(saved, (gx_device *)&adev);
  447.     code = (*pinst->template.PaintProc)(pc, saved);
  448.     gs_free_object(mem, saved, "gx_remap_Pattern(saved)");
  449.     if ( code < 0 )
  450.       { (*dev_proc(&adev, close_device))((gx_device *)&adev);
  451.         return code;
  452.       }
  453.     code = gx_pattern_cache_add_entry(pgs, &adev, &ctile);
  454.     if ( code < 0 )
  455.       (*dev_proc(&adev, close_device))((gx_device *)&adev);
  456.     return code;
  457. }
  458.