home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GSSTATE.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  19KB  |  601 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. /* gsstate.c */
  20. /* Miscellaneous graphics state operators for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gzstate.h"
  26. #include "gscspace.h"
  27. #include "gscolor2.h"
  28. #include "gscoord.h"        /* for gs_initmatrix */
  29. #include "gscie.h"
  30. #include "gxcmap.h"
  31. #include "gzht.h"
  32. #include "gzline.h"
  33. #include "gzpath.h"
  34.  
  35. /* Imported values */
  36. /* The following should include a 'const', but */
  37. /* the Watcom compiler won't accept it. */
  38. extern /*const*/ gx_color_map_procs *cmap_procs_default;
  39.  
  40. /* Forward references */
  41. private gs_state *alloc_gstate(P3(gs_memory_t *, const gs_state *,
  42.   client_name_t));
  43. private int alloc_gstate_contents(P1(gs_state *));
  44. private void free_gstate_contents(P1(gs_state *));
  45. private void copy_gstate_contents(P2(gs_state *pto, const gs_state *pfrom));
  46.  
  47. /* The structure for allocating (most of) the contents of a gstate */
  48. /* all at once.  The typedef is in gzstate.c. */
  49. struct gs_state_contents_s {
  50.     gx_path path;
  51.     gx_clip_path clip_path;
  52.     gx_line_params line_params;
  53.     gs_halftone halftone;
  54.     gx_device_halftone dev_ht;
  55.     gs_color_space color_space;
  56.     gs_client_color ccolor;
  57.     gx_device_color dev_color;
  58. };
  59.  
  60. /* Enumerate the pointers in a graphics state, other than the ones */
  61. /* that point to the gs_state_contents. */
  62. #define gs_state_do_ptrs(m)\
  63.   m(0,saved) m(1,ht_cache) m(2,contents)\
  64.   m(3,cie_render) m(4,black_generation) m(5,undercolor_removal)\
  65.   m(6,transfer.red) m(7,transfer.green) m(8,transfer.blue)\
  66.   m(9,transfer.gray)\
  67.   m(10,cie_joint_caches) m(11,pattern_cache) m(12,font) m(13,root_font)\
  68.   m(14,show_gstate) /*m(---,device)*/ m(15,client_data)
  69. #define gs_state_num_ptrs 16
  70. /* Enumerate the pointers to the gs_state_contents. */
  71. #define gs_state_do_contents_ptrs(m)\
  72.   m(0,path) m(1,clip_path) m(2,line_params) m(3,halftone) m(4,dev_ht)\
  73.   m(5,color_space) m(6,ccolor) m(7,dev_color)
  74. #define gs_state_num_contents_ptrs 8
  75.  
  76. public_st_gstate();
  77. /* Components of the graphics state */
  78. gs_private_st_composite(st_gstate_contents, gs_state_contents,
  79.   "gs_state_contents", state_contents_enum_ptrs, state_contents_reloc_ptrs);
  80. public_st_transfer_map();
  81. private_st_line_params();
  82.  
  83. /* GC procedures for gs_state */
  84. #define gsvptr ((gs_state *)vptr)
  85. private ENUM_PTRS_BEGIN(gs_state_enum_ptrs) return 0;
  86. #define e1(i,elt) ENUM_PTR(i,gs_state,elt);
  87.     gs_state_do_ptrs(e1)
  88.     case gs_state_num_ptrs:        /* handle device specially */
  89.       *pep = gx_device_enum_ptr(gsvptr->device);
  90.       break;
  91. #undef e1
  92. ENUM_PTRS_END
  93. private RELOC_PTRS_BEGIN(gs_state_reloc_ptrs) {
  94.     byte *cont = (byte *)gsvptr->contents;
  95.     uint reloc;
  96. #define r1(i,elt) RELOC_PTR(gs_state,elt);
  97.     gs_state_do_ptrs(r1)
  98. #undef r1
  99.     gsvptr->device = gx_device_reloc_ptr(gsvptr->device, gcst);
  100.     reloc = cont - (byte *)gsvptr->contents;
  101. #define r1(i,elt)\
  102.   gsvptr->elt = (void *)((byte *)gsvptr->elt - reloc);
  103.     gs_state_do_contents_ptrs(r1)
  104. #undef r1
  105. } RELOC_PTRS_END
  106. #undef gsvptr
  107.  
  108. /* GC procedures for gs_state_contents */
  109. #define cptr ((gs_state_contents *)vptr)
  110. private ENUM_PTRS_BEGIN_PROC(state_contents_enum_ptrs) {
  111.     gs_ptr_type_t ret;
  112. #define next_comp(np, st, e)\
  113.   if ( index < np ) { ret = (*st.enum_ptrs)(&cptr->e, sizeof(cptr->e), index, pep); goto rx; }\
  114.   index -= np
  115. #define last_comp(np, st, e)\
  116.   return (*st.enum_ptrs)(&cptr->e, sizeof(cptr->e), index, pep)
  117.     next_comp(st_path_max_ptrs, st_path, path);
  118.     next_comp(st_clip_path_max_ptrs, st_clip_path, clip_path);
  119.     next_comp(st_line_params_max_ptrs, st_line_params, line_params);
  120.     next_comp(st_halftone_max_ptrs, st_halftone, halftone);
  121.     next_comp(st_device_halftone_max_ptrs, st_device_halftone, dev_ht);
  122.     next_comp(st_color_space_max_ptrs, st_color_space, color_space);
  123.     next_comp(st_client_color_max_ptrs, st_client_color, ccolor);
  124.     last_comp(st_device_color_max_ptrs, st_device_color, dev_color);
  125. #undef next_comp
  126. rx:    if ( ret == 0 )
  127.     {    /* A component ran out of pointers early. */
  128.         /* Just return a null so we can keep going. */
  129.         *pep = 0;
  130.         return ptr_struct_type;
  131.     }
  132.     return ret;
  133. ENUM_PTRS_END_PROC }
  134. private RELOC_PTRS_BEGIN(state_contents_reloc_ptrs) {
  135.     (*st_path.reloc_ptrs)(&cptr->path, sizeof(gx_path), gcst);
  136.     (*st_clip_path.reloc_ptrs)(&cptr->clip_path, sizeof(gx_clip_path), gcst);
  137.     (*st_line_params.reloc_ptrs)(&cptr->line_params, sizeof(gx_line_params), gcst);
  138.     (*st_halftone.reloc_ptrs)(&cptr->halftone, sizeof(gs_halftone), gcst);
  139.     (*st_device_halftone.reloc_ptrs)(&cptr->dev_ht, sizeof(gx_device_halftone), gcst);
  140.     (*st_color_space.reloc_ptrs)(&cptr->color_space, sizeof(gs_color_space), gcst);
  141.     (*st_client_color.reloc_ptrs)(&cptr->ccolor, sizeof(gs_client_color), gcst);
  142.     (*st_device_color.reloc_ptrs)(&cptr->dev_color, sizeof(gx_device_color), gcst);
  143. } RELOC_PTRS_END
  144. #undef cptr
  145.  
  146. /* ------ Operations on the entire graphics state ------ */
  147.  
  148. /* Allocate and initialize a graphics state. */
  149. private float
  150. null_transfer(const gs_state *pgs, floatp gray)
  151. {    return gray;
  152. }
  153. gs_state *
  154. gs_state_alloc(gs_memory_t *mem)
  155. {    register gs_state *pgs =
  156.         alloc_gstate(mem, (gs_state *)0, "gs_state_alloc");
  157.     if ( pgs == 0 )
  158.         return 0;
  159.     pgs->saved = 0;
  160.     /* Initialize things not covered by initgraphics */
  161.     gx_path_init(pgs->path, mem);
  162.     gx_cpath_init(pgs->clip_path, mem);
  163.     /* The following is a hack for the GC. */
  164.     pgs->clip_path->list.container_offset =
  165.       offset_of(gs_state_contents, clip_path.list);
  166.     pgs->ht_cache = gx_ht_alloc_cache(mem,
  167.                 ht_cache_default_max_tiles,
  168.                 ht_cache_default_max_bits);
  169.     {    /* It would be great if we could use statically initialized */
  170.         /* structures for the levels and bits arrays, */
  171.         /* but that would confuse the GC. */
  172.         uint *levels =
  173.           (uint *)gs_alloc_bytes(mem, sizeof(uint),
  174.                      "gs_state_alloc(ht levels)");
  175.         gx_ht_bit *bits =
  176.           (gx_ht_bit *)gs_alloc_bytes(mem, sizeof(gx_ht_bit),
  177.                           "gs_state_alloc(ht bits)");
  178.         pgs->dev_ht->order.width = pgs->dev_ht->order.height =
  179.             pgs->dev_ht->order.num_levels =
  180.             pgs->dev_ht->order.num_bits = 1;
  181.         levels[0] = 1;
  182.         pgs->dev_ht->order.levels = levels;
  183.         bits[0].offset = 0;
  184.         bits[0].mask = 0;
  185.         pgs->dev_ht->order.bits = bits;
  186.         pgs->dev_ht->order.cache = 0;
  187.         pgs->dev_ht->components = 0;
  188.     }
  189.     pgs->pattern_cache = 0;
  190.     gs_sethalftonephase(pgs, 0, 0);
  191.     /* Initialize things so that gx_remap_color won't crash. */
  192.     gx_set_black(pgs);
  193.     pgs->overprint = 0;
  194.     pgs->black_generation = 0;
  195.     pgs->undercolor_removal = 0;
  196.     pgs->cmap_procs = cmap_procs_default;
  197.     gs_nulldevice(pgs);
  198.     gs_setalpha(pgs, 1.0);
  199.     gs_settransfer(pgs, null_transfer);
  200.     gs_setflat(pgs, 1.0);
  201.     gs_setstrokeadjust(pgs, 1);
  202.     pgs->font = 0;        /* Not right, but acceptable until the */
  203.                 /* PostScript code does the first setfont. */
  204.     pgs->root_font = 0;    /* ditto */
  205.  
  206.     pgs->in_cachedevice = pgs->in_charpath = 0;
  207.     pgs->show_gstate = 0;
  208.     pgs->level = 0;
  209.     pgs->fill_adjust = float2fixed(0.25);
  210.     pgs->client_data = 0;
  211.     if ( gs_initgraphics(pgs) < 0 )
  212.        {    /* Something went very wrong */
  213.         return 0;
  214.        }
  215.     return pgs;
  216. }
  217.  
  218. /* Set the client data in a graphics state. */
  219. /* This should only be done to a newly created state. */
  220. void
  221. gs_state_set_client(gs_state *pgs, void *pdata,
  222.   const gs_state_client_procs *pprocs)
  223. {    pgs->client_data = pdata;
  224.     pgs->client_procs = *pprocs;
  225. }
  226.  
  227. /* Get the client data from a graphics state. */
  228. void *
  229. gs_state_client_data(const gs_state *pgs)
  230. {    return pgs->client_data;
  231. }
  232.  
  233. /* Free a graphics state */
  234. int
  235. gs_state_free(gs_state *pgs)
  236. {    free_gstate_contents(pgs);
  237.     gs_free_object(pgs->memory, pgs, "gs_state_free");
  238.     return 0;
  239. }
  240.  
  241. /* Save the graphics state */
  242. int
  243. gs_gsave(gs_state *pgs)
  244. {    gs_state *pnew =
  245.       gs_alloc_struct(pgs->memory, gs_state, &st_gstate, "gs_gsave");
  246.     if ( pnew == 0 )
  247.         return_error(gs_error_VMerror);
  248.     *pnew = *pgs;
  249.     if ( alloc_gstate_contents(pgs) < 0 )
  250.     {    *pgs = *pnew;        /* undo partial alloc */
  251.         gs_free_object(pgs->memory, pnew, "gs_gsave");
  252.         return_error(gs_error_VMerror);
  253.     }
  254.     copy_gstate_contents(pgs, pnew);
  255.     /* Make pnew, not pgs, have the newly-allocated client data. */
  256.     {    void *pdata = pgs->client_data;
  257.         pgs->client_data = pnew->client_data;
  258.         pnew->client_data = pdata;
  259.     }
  260.     pgs->saved = pnew;
  261.     if ( pgs->show_gstate == pgs )
  262.         pgs->show_gstate = pnew->show_gstate = pnew;
  263.     pgs->level++;
  264.     if_debug2('g', "[g]gsave -> 0x%lx, level = %d\n",
  265.           (ulong)pnew, pgs->level);
  266.     return 0;
  267. }
  268.  
  269. /* Restore the graphics state. */
  270. int
  271. gs_grestore(gs_state *pgs)
  272. {    gs_state *saved = pgs->saved;
  273.     void *pdata = pgs->client_data;
  274.     void *sdata;
  275.     if_debug2('g', "[g]grestore 0x%lx, level was %d\n",
  276.           (ulong)saved, pgs->level);
  277.     if ( !saved ) return gs_gsave(pgs);    /* shouldn't happen */
  278.     sdata = saved->client_data;
  279.     if ( saved->pattern_cache == 0 )
  280.       saved->pattern_cache = pgs->pattern_cache;
  281.     /* Swap back the client data pointers. */
  282.     pgs->client_data = sdata;
  283.     saved->client_data = pdata;
  284.     if ( pdata != 0 && sdata != 0 )
  285.         (*pgs->client_procs.copy)(pdata, sdata);
  286.     free_gstate_contents(pgs);
  287.     *pgs = *saved;
  288.     if ( pgs->show_gstate == saved )
  289.         pgs->show_gstate = pgs;
  290.     gs_free_object(pgs->memory, saved, "gs_grestore");
  291.     return (pgs->saved == 0 ? gs_gsave(pgs) : 0);
  292. }
  293.  
  294. /* Restore to the bottommost graphics state.  Also clear */
  295. /* the halftone caches, so stale pointers don't survive a restore. */
  296. int
  297. gs_grestoreall(gs_state *pgs)
  298. {    int code;
  299.     if ( !pgs->saved )        /* shouldn't happen */
  300.         return gs_gsave(pgs);
  301.     while ( pgs->saved->saved )
  302.     {    int code = gs_grestore(pgs);
  303.         if ( code < 0 )
  304.             return code;
  305.     }
  306.     code = gs_grestore(pgs);
  307.     if ( code < 0 )
  308.       return code;
  309.     gx_ht_clear_cache(pgs->ht_cache);
  310.     return code;
  311. }
  312.  
  313. /* Allocate and return a new graphics state. */
  314. gs_state *
  315. gs_gstate(gs_state *pgs)
  316. {    gs_state *pnew = alloc_gstate(pgs->memory, pgs, "gs_gstate");
  317.     if ( pnew == 0 ) return 0;
  318.     copy_gstate_contents(pnew, pgs);
  319.     pnew->saved = 0;
  320.     return pnew;
  321. }
  322.  
  323. /* Copy one previously allocated graphics state to another. */
  324. int
  325. gs_copygstate(gs_state *pto, const gs_state *pfrom)
  326. {    /* This is the same as currentgstate. */
  327.     return gs_currentgstate(pto, pfrom);
  328. }
  329.  
  330. /* Copy the current graphics state to a previously allocated one. */
  331. int
  332. gs_currentgstate(gs_state *pto, const gs_state *pgs)
  333. {    /* We have to copy both the scalar and composite parts */
  334.     /* of the state. */
  335.     gs_state sgs;
  336.     sgs = *pto;
  337.     *pto = *pgs;
  338.     /* Put back the composite part pointers. */
  339. #define gcopy(element)\
  340.     pto->element = sgs.element
  341.     gcopy(contents);
  342.     if ( pto->pattern_cache == 0 )
  343.       gcopy(pattern_cache);
  344.     gcopy(path);
  345.     gcopy(clip_path);
  346.     gcopy(line_params);
  347.     gcopy(halftone);
  348.     gcopy(dev_ht);
  349.     gcopy(color_space);
  350.     gcopy(ccolor);
  351.     gcopy(dev_color);
  352.     gcopy(cie_render);
  353.     gcopy(black_generation);
  354.     gcopy(undercolor_removal);
  355.     gcopy(transfer.red);
  356.     gcopy(transfer.green);
  357.     gcopy(transfer.blue);
  358.     gcopy(transfer.gray);
  359.     gcopy(device);
  360.     gcopy(client_data);
  361. #undef gcopy
  362.     copy_gstate_contents(pto, pgs);
  363.     return 0;
  364. }
  365.  
  366. /* Restore the current graphics state from a previously allocated one. */
  367. int
  368. gs_setgstate(gs_state *pgs, const gs_state *pfrom)
  369. {    /* The implementation is the same as currentgstate, */
  370.     /* except we must preserve the saved pointer and the level. */
  371.     gs_state *saved = pgs->saved;
  372.     int level = pgs->level;
  373.     int code = gs_currentgstate(pgs, pfrom);
  374.     if ( code < 0 ) return code;
  375.     pgs->saved = saved;
  376.     pgs->level = level;
  377.     return 0;
  378. }
  379.  
  380. /* Get the allocator pointer of a graphics state. */
  381. /* This is provided only for the interpreter. */
  382. gs_memory_t *
  383. gs_state_memory(const gs_state *pgs)
  384. {    return pgs->memory;
  385. }
  386.  
  387. /* Get the saved pointer of the graphics state. */
  388. /* This is provided only for Level 2 grestore. */
  389. gs_state *
  390. gs_state_saved(const gs_state *pgs)
  391. {    return pgs->saved;
  392. }
  393.  
  394. /* Swap the saved pointer of the graphics state. */
  395. /* This is provided only for save/restore. */
  396. gs_state *
  397. gs_state_swap_saved(gs_state *pgs, gs_state *new_saved)
  398. {    gs_state *saved = pgs->saved;
  399.     pgs->saved = new_saved;
  400.     return saved;
  401. }
  402.  
  403. /* Swap the memory pointer of the graphics state. */
  404. /* This is provided only for the interpreter. */
  405. gs_memory_t *
  406. gs_state_swap_memory(gs_state *pgs, gs_memory_t *mem)
  407. {    gs_memory_t *memory = pgs->memory;
  408.     pgs->memory = mem;
  409.     return memory;
  410. }
  411.  
  412. /* ------ Operations on components ------ */
  413.  
  414. /* Reset most of the graphics state */
  415. int
  416. gs_initgraphics(register gs_state *pgs)
  417. {    int code;
  418.     gs_initmatrix(pgs);
  419.     if (    (code = gs_newpath(pgs)) < 0 ||
  420.         (code = gs_initclip(pgs)) < 0 ||
  421.         (code = gs_setlinewidth(pgs, 1.0)) < 0 ||
  422.         (code = gs_setlinecap(pgs, gs_cap_butt)) < 0 ||
  423.         (code = gs_setlinejoin(pgs, gs_join_miter)) < 0 ||
  424.         (code = gs_setdash(pgs, (float *)0, 0, 0.0)) < 0 ||
  425.         (code = gs_setgray(pgs, 0.0)) < 0 ||
  426.         (code = gs_setmiterlimit(pgs, 10.0)) < 0
  427.        ) return code;
  428.     return 0;
  429. }
  430.  
  431. /* setflat */
  432. int
  433. gs_setflat(gs_state *pgs, floatp flat)
  434. {    if ( flat <= 0.2 ) flat = 0.2;
  435.     else if ( flat > 100 ) flat = 100;
  436.     pgs->flatness = flat;
  437.     return 0;
  438. }
  439.  
  440. /* currentflat */
  441. float
  442. gs_currentflat(const gs_state *pgs)
  443. {    return pgs->flatness;
  444. }
  445.  
  446. /* setstrokeadjust */
  447. int
  448. gs_setstrokeadjust(gs_state *pgs, int stroke_adjust)
  449. {    pgs->stroke_adjust = stroke_adjust;
  450.     return 0;
  451. }
  452.  
  453. /* currentstrokeadjust */
  454. int
  455. gs_currentstrokeadjust(const gs_state *pgs)
  456. {    return pgs->stroke_adjust;
  457. }
  458.  
  459. /* ------ Internal routines ------ */
  460.  
  461. /* Allocate a graphics state object and its contents, */
  462. /* optionally initializing it from an existing object. */
  463. /* Return 0 if the allocation fails. */
  464. private gs_state *
  465. alloc_gstate(gs_memory_t *mem, const gs_state *pold, client_name_t cname)
  466. {    gs_state *pgs = gs_alloc_struct(mem, gs_state, &st_gstate, cname);
  467.     if ( pgs == 0 ) return 0;
  468.     if ( pold != 0 )
  469.         *pgs = *pold;
  470.     else
  471.         pgs->cie_render = 0,
  472.         pgs->black_generation = 0,
  473.         pgs->undercolor_removal = 0,
  474.         pgs->transfer.red = pgs->transfer.green =
  475.           pgs->transfer.blue = pgs->transfer.gray = 0,
  476.         pgs->cie_joint_caches = 0,
  477.         pgs->client_data = 0;
  478.     pgs->memory = mem;
  479.     if ( alloc_gstate_contents(pgs) < 0 )
  480.     {    gs_free_object(mem, pgs, cname);
  481.         return 0;
  482.     }
  483.     return pgs;
  484. }
  485.  
  486. /* Allocate the contents of a graphics state object. */
  487. /* Return -1 if the allocation fails. */
  488. /* Note that the contents have been smashed in this case. */
  489. private int
  490. alloc_gstate_contents(register gs_state *pgs)
  491. {    gs_memory_t *mem = pgs->memory;
  492.     static const char cname[] = "alloc_gstate_contents";
  493.     gs_state_contents *cont =
  494.       gs_alloc_struct(mem, gs_state_contents, &st_gstate_contents, cname);
  495.     if ( cont == 0 ) return -1;
  496.     pgs->contents = cont;
  497. #define gset(element)\
  498.   pgs->element = &cont->element;
  499.     gset(path);
  500.     gset(clip_path);
  501.     gset(line_params);
  502.     gset(halftone);
  503.     gset(dev_ht);
  504.     gset(color_space);
  505.     cont->color_space.type = &gs_color_space_type_DeviceGray;    /* for cs_adjust_counts */
  506.     gset(ccolor);
  507.     gset(dev_color);
  508. #undef gset
  509.     /* Only allocate one transfer map if none exist. */
  510.     if ( pgs->transfer.gray == 0 )
  511.     {    rc_alloc_struct_0(pgs->transfer.gray, gx_transfer_map,
  512.                   &st_transfer_map, mem, goto uty, cname);
  513.         pgs->transfer.gray->proc = null_transfer;
  514.         pgs->transfer.gray->values[0] = frac_0;
  515.     }
  516.     rc_increment(pgs->transfer.gray);
  517. #define gtalloc(element)\
  518.   if ( pgs->element == 0 )\
  519.     pgs->element = pgs->transfer.gray;\
  520.   rc_increment(pgs->element)
  521.     gtalloc(transfer.red);
  522.     gtalloc(transfer.green);
  523.     gtalloc(transfer.blue);
  524. #undef gtalloc
  525.     if ( pgs->client_data != 0 )
  526.     {    if ( (pgs->client_data =
  527.                (*pgs->client_procs.alloc)(mem)) == 0
  528.            )
  529.             goto ud;
  530.     }
  531.     rc_increment(pgs->cie_render);
  532.     rc_increment(pgs->black_generation);
  533.     rc_increment(pgs->undercolor_removal);
  534.     rc_increment(pgs->cie_joint_caches);
  535.     return 0;
  536.     /* Undo partial allocations if an allocation failed. */
  537. #define gunalloc(element)\
  538.   rc_decrement(pgs->element, mem, cname)
  539. ud:    gunalloc(transfer.gray);
  540.     gunalloc(transfer.blue);
  541.     gunalloc(transfer.green);
  542.     gunalloc(transfer.red);
  543. uty:    gs_free_object(mem, cont, cname);
  544.     return -1;
  545. #undef gunalloc
  546. }
  547.  
  548. /* Free the contents of a graphics state, but not the state itself. */
  549. private void
  550. free_gstate_contents(gs_state *pgs)
  551. {    gs_memory_t *mem = pgs->memory;
  552.     static const char cname[] = "free_gstate_contents";
  553. #define gfree(element, pstype)\
  554.     gs_free_object(mem, pgs->element, cname)
  555.     gx_path_release(pgs->path);
  556.     gx_cpath_release(pgs->clip_path);
  557.     rc_decrement(pgs->cie_joint_caches, mem, cname);
  558.     rc_decrement(pgs->transfer.gray, mem, cname);
  559.     rc_decrement(pgs->transfer.blue, mem, cname);
  560.     rc_decrement(pgs->transfer.green, mem, cname);
  561.     rc_decrement(pgs->transfer.red, mem, cname);
  562.     rc_decrement(pgs->undercolor_removal, mem, cname);
  563.     rc_decrement(pgs->black_generation, mem, cname);
  564.     rc_decrement(pgs->cie_render, mem, cname);
  565.     cs_adjust_counts(pgs, -1);
  566.     if ( pgs->client_data != 0 )
  567.         (*pgs->client_procs.free)(pgs->client_data, mem);
  568.     gs_free_object(mem, pgs->contents, cname);
  569. #undef gfree
  570. }
  571.  
  572. /* Copy the composite parts of a graphics state. */
  573. private void
  574. copy_gstate_contents(gs_state *pto, const gs_state *pfrom)
  575. {    gs_memory_t *mem = pto->memory;
  576.     static const char cname[] = "copy_gstate_contents";
  577.     /* It's OK to decrement the counts before incrementing them, */
  578.     /* because anything that is going to survive has a count of */
  579.     /* at least 2 (pto and somewhere else) initially. */
  580.     cs_adjust_counts(pto, -1);
  581.     *pto->contents = *pfrom->contents;
  582.     cs_adjust_counts(pto, 1);
  583. #define rccopy(element)\
  584.     rc_assign(pto->element, pfrom->element, mem, cname);
  585.     if ( pfrom->pattern_cache != 0 )
  586.       pto->pattern_cache = pfrom->pattern_cache;
  587.     rccopy(cie_joint_caches);
  588.     rccopy(transfer.gray);
  589.     rccopy(transfer.blue);
  590.     rccopy(transfer.green);
  591.     rccopy(transfer.red);
  592.     rccopy(undercolor_removal);
  593.     rccopy(black_generation);
  594.     rccopy(cie_render);
  595. #undef rccopy
  596.     if ( pfrom->client_data != 0 )
  597.         (*pfrom->client_procs.copy)(pto->client_data, pfrom->client_data);
  598.     gx_path_share(pto->path);
  599.     gx_cpath_share(pto->clip_path);
  600. }
  601.