summaryrefslogtreecommitdiff
path: root/base/gsgstate.c
blob: 7fab694b4a0bab22bdee8fde96b14009c9faf622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* gs_gstate housekeeping */
#include "gx.h"
#include "gserrors.h"
#include "gscspace.h"
#include "gscie.h"
#include "gsstruct.h"
#include "gsutil.h"		/* for gs_next_ids */
#include "gxbitmap.h"
#include "gxcmap.h"
#include "gxdht.h"
#include "gxgstate.h"
#include "gzht.h"
#include "gzline.h"
#include "gxfmap.h"
#include "gsicc_cache.h"
#include "gsicc_manage.h"
#include "gsicc_profilecache.h"

/******************************************************************************
 * See gsstate.c for a discussion of graphics state memory management. *
 ******************************************************************************/

/* Imported values */
/* The following should include a 'const', but for some reason */
/* the Watcom compiler won't accept it, even though it happily accepts */
/* the same construct everywhere else. */
extern /*const*/ gx_color_map_procs *const cmap_procs_default;

/* GC procedures for gx_line_params */
static
ENUM_PTRS_WITH(line_params_enum_ptrs, gx_line_params *plp) return 0;
    case 0: return ENUM_OBJ((plp->dash.pattern_size == 0 ?
                             NULL : plp->dash.pattern));
ENUM_PTRS_END
static RELOC_PTRS_WITH(line_params_reloc_ptrs, gx_line_params *plp)
{
    if (plp->dash.pattern_size)
        RELOC_VAR(plp->dash.pattern);
} RELOC_PTRS_END
private_st_line_params();

/*
 * GC procedures for gs_gstate
 *
 * See comments in gixstate.h before the definition of gs_cr_state_do_rc and
 * st_cr_state_num_ptrs for an explanation about why the effective_transfer
 * pointers are handled in this manner.
 */
public_st_gs_gstate();
static
ENUM_PTRS_WITH(gs_gstate_enum_ptrs, gs_gstate *gisvptr)
    ENUM_SUPER(gs_gstate, st_line_params, line_params, st_gs_gstate_num_ptrs - st_line_params_num_ptrs);
#define e1(i,elt) ENUM_PTR(i,gs_gstate,elt);
    gs_gstate_do_ptrs(e1)
#undef e1
#define E1(i,elt) ENUM_PTR(i + gs_gstate_num_ptrs,gs_gstate,elt);
    gs_cr_state_do_ptrs(E1)
#undef E1
    case (gs_gstate_num_ptrs + st_cr_state_num_ptrs): /* handle device specially */
        ENUM_RETURN(gx_device_enum_ptr(gisvptr->device));
ENUM_PTRS_END

static RELOC_PTRS_WITH(gs_gstate_reloc_ptrs, gs_gstate *gisvptr)
{
    RELOC_SUPER(gs_gstate, st_line_params, line_params);
#define r1(i,elt) RELOC_PTR(gs_gstate,elt);
    gs_gstate_do_ptrs(r1)
#undef r1
#define R1(i,elt) RELOC_PTR(gs_gstate,elt);
    gs_cr_state_do_ptrs(R1)
#undef R1

    gisvptr->device = gx_device_reloc_ptr(gisvptr->device, gcst);
    {
        int i = GX_DEVICE_COLOR_MAX_COMPONENTS - 1;

        for (; i >= 0; i--)
            RELOC_PTR(gs_gstate, effective_transfer[i]);
    }
} RELOC_PTRS_END

/* Initialize an gs_gstate, other than the parts covered by */
/* GS_STATE_INIT_VALUES(). */
int
gs_gstate_initialize(gs_gstate * pgs, gs_memory_t * mem)
{
    int i;
    pgs->memory = mem;
    pgs->client_data = 0;
    pgs->trans_device = 0;
    /* Color rendering state */
    pgs->halftone = 0;
    {
        int i;

        for (i = 0; i < gs_color_select_count; ++i)
            pgs->screen_phase[i].x = pgs->screen_phase[i].y = 0;
    }
    for (i=0; i < HT_OBJTYPE_COUNT; i++)
        pgs->dev_ht[i] = NULL;
    pgs->cie_render = 0;
    pgs->cie_to_xyz = false;
    pgs->black_generation = 0;
    pgs->undercolor_removal = 0;
    /* Allocate an initial transfer map. */
    rc_alloc_struct_n(pgs->set_transfer.gray,
                      gx_transfer_map, &st_transfer_map,
                      mem, return_error(gs_error_VMerror),
                      "gs_gstate_init(transfer)", 1);
    pgs->set_transfer.gray->proc = gs_identity_transfer;
    pgs->set_transfer.gray->id = gs_next_ids(pgs->memory, 1);
    pgs->set_transfer.gray->values[0] = frac_0;
    pgs->set_transfer.red =
        pgs->set_transfer.green =
        pgs->set_transfer.blue = NULL;
    for (i = 0; i < GX_DEVICE_COLOR_MAX_COMPONENTS; i++)
        pgs->effective_transfer[i] = pgs->set_transfer.gray;
    pgs->cie_joint_caches = NULL;
    pgs->cie_joint_caches_alt = NULL;
    pgs->cmap_procs = cmap_procs_default;
    pgs->pattern_cache = NULL;
    pgs->have_pattern_streams = false;
    pgs->devicergb_cs = gs_cspace_new_DeviceRGB(mem);
    pgs->devicecmyk_cs = gs_cspace_new_DeviceCMYK(mem);
    if (pgs->devicergb_cs == NULL || pgs->devicecmyk_cs == NULL)
        return_error(gs_error_VMerror);
    pgs->icc_link_cache = gsicc_cache_new(pgs->memory);
    if (pgs->icc_link_cache == NULL)
        return_error(gs_error_VMerror);
    pgs->icc_manager = gsicc_manager_new(pgs->memory);
    if (pgs->icc_manager == NULL)
        return_error(gs_error_VMerror);
    pgs->icc_profile_cache = gsicc_profilecache_new(pgs->memory);
    if (pgs->icc_profile_cache == NULL)
        return_error(gs_error_VMerror);
    pgs->black_textvec_state = NULL;
#if ENABLE_CUSTOM_COLOR_CALLBACK
    pgs->custom_color_callback = INIT_CUSTOM_COLOR_PTR;
#endif
    return 0;
}

/* Increment reference counts to note that an gs_gstate has been copied. */
void
gs_gstate_copied(gs_gstate * pgs)
{
    int i;

    rc_increment(pgs->halftone);
    for (i=0; i < HT_OBJTYPE_COUNT; i++)
        rc_increment(pgs->dev_ht[i]);
    rc_increment(pgs->cie_render);
    rc_increment(pgs->black_generation);
    rc_increment(pgs->undercolor_removal);
    rc_increment(pgs->set_transfer.gray);
    rc_increment(pgs->set_transfer.red);
    rc_increment(pgs->set_transfer.green);
    rc_increment(pgs->set_transfer.blue);
    rc_increment(pgs->cie_joint_caches);
    rc_increment(pgs->cie_joint_caches_alt);
    rc_increment(pgs->devicergb_cs);
    rc_increment(pgs->devicecmyk_cs);
    rc_increment(pgs->icc_link_cache);
    rc_increment(pgs->icc_profile_cache);
    rc_increment(pgs->icc_manager);
    rc_increment(pgs->black_textvec_state);
}

/* Adjust reference counts before assigning one gs_gstate to another. */
void
gs_gstate_pre_assign(gs_gstate *pto, const gs_gstate *pfrom)
{
    const char *const cname = "gs_gstate_pre_assign";
    int i;

#define RCCOPY(element)\
    rc_pre_assign(pto->element, pfrom->element, cname)

    RCCOPY(cie_joint_caches);
    RCCOPY(cie_joint_caches_alt);
    RCCOPY(set_transfer.blue);
    RCCOPY(set_transfer.green);
    RCCOPY(set_transfer.red);
    RCCOPY(set_transfer.gray);
    RCCOPY(undercolor_removal);
    RCCOPY(black_generation);
    RCCOPY(cie_render);
    for (i=0; i < HT_OBJTYPE_COUNT; i++)
        RCCOPY(dev_ht[i]);
    RCCOPY(halftone);
    RCCOPY(devicergb_cs);
    RCCOPY(devicecmyk_cs);
    RCCOPY(icc_link_cache);
    RCCOPY(icc_profile_cache);
    RCCOPY(icc_manager);
    RCCOPY(black_textvec_state);
#undef RCCOPY
}

/* Release a gs_gstate. */
void
gs_gstate_release(gs_gstate * pgs)
{
    const char *const cname = "gs_gstate_release";
    gx_device_halftone *pdht;
    int i;

#define RCDECR(element)\
    rc_decrement(pgs->element, cname);\
    pgs->element = NULL	/* prevent subsequent decrements from this gs_gstate */

    RCDECR(cie_joint_caches);
    RCDECR(set_transfer.gray);
    RCDECR(set_transfer.blue);
    RCDECR(set_transfer.green);
    RCDECR(set_transfer.red);
    RCDECR(undercolor_removal);
    RCDECR(black_generation);
    RCDECR(cie_render);
    /*
     * If we're going to free a device halftone, make sure we free the
     * dependent structures as well.
     */
    for (i=0; i < HT_OBJTYPE_COUNT; i++) {
        pdht = pgs->dev_ht[i];
        if (pdht != NULL && pdht->rc.ref_count == 1) {
            gx_device_halftone_release(pdht, pdht->rc.memory);
        }
        RCDECR(dev_ht[i]);
    }
    RCDECR(halftone);
    RCDECR(devicergb_cs);
    RCDECR(devicecmyk_cs);
    RCDECR(icc_link_cache);
    RCDECR(icc_profile_cache);
    RCDECR(icc_manager);
    RCDECR(black_textvec_state);
#undef RCDECR
}