summaryrefslogtreecommitdiff
path: root/base/gsshade.c
blob: b4379f96bd359e3ddc9e298a5b15e8cc1e70d98a (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* 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.
*/


/* Constructors for shadings */
#include "gx.h"
#include "gscspace.h"
#include "gserrors.h"
#include "gsstruct.h"
#include "gsptype2.h"
#include "gxdevcli.h"
#include "gxcpath.h"
#include "gxcspace.h"
#include "gxdcolor.h"		/* for filling background rectangle */
#include "gxgstate.h"
#include "gxpaint.h"
#include "gxpath.h"
#include "gxshade.h"
#include "gxshade4.h"
#include "gzpath.h"
#include "gzcpath.h"

/* ================ Initialize shadings ================ */

/* ---------------- Generic services ---------------- */

/* GC descriptors */
private_st_shading();

static
ENUM_PTRS_WITH(shading_mesh_enum_ptrs, gs_shading_mesh_t *psm)
{
    index -= 2;
    if (index < st_data_source_max_ptrs)
        return ENUM_USING(st_data_source, &psm->params.DataSource,
                          sizeof(psm->params.DataSource), index);
    return ENUM_USING_PREFIX(st_shading, st_data_source_max_ptrs);
}
ENUM_PTR2(0, gs_shading_mesh_t, params.Function, params.Decode);
ENUM_PTRS_END

static
RELOC_PTRS_WITH(shading_mesh_reloc_ptrs, gs_shading_mesh_t *psm)
{
    RELOC_PREFIX(st_shading);
    RELOC_USING(st_data_source, &psm->params.DataSource,
                sizeof(psm->params.DataSource));
    RELOC_PTR2(gs_shading_mesh_t, params.Function, params.Decode);
}
RELOC_PTRS_END

/* Check ColorSpace, BBox, and Function (if present). */
/* Free variables: params. */
static int
check_CBFD(const gs_shading_params_t * params,
           const gs_function_t * function, const float *domain, int m)
{
    int ncomp = gs_color_space_num_components(params->ColorSpace);

    if (ncomp < 0 ||
        (params->have_BBox &&
         (params->BBox.p.x > params->BBox.q.x ||
          params->BBox.p.y > params->BBox.q.y))
        )
        return_error(gs_error_rangecheck);
    if (function != 0) {
        if (function->params.m != m || function->params.n != ncomp)
            return_error(gs_error_rangecheck);
        /*
         * The Adobe documentation says that the function's domain must
         * be a superset of the domain defined in the shading dictionary.
         * However, Adobe implementations apparently don't necessarily
         * check this ahead of time; therefore, we do the same.
         */
    }
    return 0;
}

/* Check parameters for a mesh shading. */
static int
check_mesh(const gs_shading_mesh_params_t * params)
{
    const float *domain;

    if (data_source_is_array(params->DataSource))
        domain = 0;
    else {
        domain = params->Decode;
        switch (params->BitsPerCoordinate) {
            case  1: case  2: case  4: case  8:
            case 12: case 16: case 24: case 32:
                break;
            default:
                return_error(gs_error_rangecheck);
        }
        switch (params->BitsPerComponent) {
            case  1: case  2: case  4: case  8:
            case 12: case 16:
                break;
            default:
                return_error(gs_error_rangecheck);
        }
    }
    return check_CBFD((const gs_shading_params_t *)params,
                      params->Function, domain, 1);
}

/* Check the BitsPerFlag value.  Return the value or an error code. */
static int
check_BPF(const gs_data_source_t *pds, int bpf)
{
    if (data_source_is_array(*pds))
        return 2;
    switch (bpf) {
    case 2: case 4: case 8:
        return bpf;
    default:
        return_error(gs_error_rangecheck);
    }
}

/* Initialize common shading parameters. */
static void
shading_params_init(gs_shading_params_t *params)
{
    params->ColorSpace = 0;	/* must be set by client */
    params->cie_joint_caches = 0;
    params->Background = 0;
    params->have_BBox = false;
    params->AntiAlias = false;
}

/* Initialize common mesh shading parameters. */
static void
mesh_shading_params_init(gs_shading_mesh_params_t *params)
{
    shading_params_init((gs_shading_params_t *)params);
    data_source_init_floats(&params->DataSource, NULL, 0);/* client must set */
    /* Client must set BitsPerCoordinate and BitsPerComponent */
    /* if DataSource is not an array. */
    params->Decode = 0;
    params->Function = 0;
}

/* Allocate and initialize a shading. */
#define ALLOC_SHADING(ppsh, psh, mem, sttype, stype, sprocs, cname, params)\
  BEGIN\
    psh = gs_alloc_struct(mem, void, sttype, cname);\
    if ( psh == 0 )\
      return_error(gs_error_VMerror);\
    psh->head.type = stype;\
    psh->head.procs = sprocs;\
    psh->params = *params;\
    *ppsh = (gs_shading_t *)psh;\
  END

/* ---------------- Function-based shading ---------------- */

private_st_shading_Fb();

/* Initialize parameters for a Function-based shading. */
void
gs_shading_Fb_params_init(gs_shading_Fb_params_t * params)
{
    shading_params_init((gs_shading_params_t *)params);
    params->Domain[0] = params->Domain[2] = 0;
    params->Domain[1] = params->Domain[3] = 1;
    gs_make_identity(&params->Matrix);
    params->Function = 0;	/* must be set by client */
}

/* Allocate and initialize a Function-based shading. */
static const gs_shading_procs_t shading_Fb_procs = {
    gs_shading_Fb_fill_rectangle
};
int
gs_shading_Fb_init(gs_shading_t ** ppsh,
                   const gs_shading_Fb_params_t * params, gs_memory_t * mem)
{
    gs_shading_Fb_t *psh;
    gs_matrix imat;
    int code = check_CBFD((const gs_shading_params_t *)params,
                          params->Function, params->Domain, 2);

    if (code < 0 ||
        (code = gs_matrix_invert(&params->Matrix, &imat)) < 0
        )
        return code;
    ALLOC_SHADING(ppsh, psh, mem, &st_shading_Fb, shading_type_Function_based,
                  shading_Fb_procs, "gs_shading_Fb_init", params);
    return 0;
}

/* ---------------- Axial shading ---------------- */

private_st_shading_A();

/* Initialize parameters for an Axial shading. */
void
gs_shading_A_params_init(gs_shading_A_params_t * params)
{
    shading_params_init((gs_shading_params_t *)params);
    /* Coords must be set by client */
    params->Domain[0] = 0;
    params->Domain[1] = 1;
    params->Function = 0;	/* must be set by client */
    params->Extend[0] = params->Extend[1] = false;
}

/* Allocate and initialize an Axial shading. */
static const gs_shading_procs_t shading_A_procs = {
    gs_shading_A_fill_rectangle
};
int
gs_shading_A_init(gs_shading_t ** ppsh,
                  const gs_shading_A_params_t * params, gs_memory_t * mem)
{
    gs_shading_A_t *psh;
    int code = check_CBFD((const gs_shading_params_t *)params,
                          params->Function, params->Domain, 1);

    if (code < 0)
        return code;
    ALLOC_SHADING(ppsh, psh, mem, &st_shading_A, shading_type_Axial,
                  shading_A_procs, "gs_shading_A_init", params);
    return 0;
}

/* ---------------- Radial shading ---------------- */

private_st_shading_R();

/* Initialize parameters for a Radial shading. */
void
gs_shading_R_params_init(gs_shading_R_params_t * params)
{
    shading_params_init((gs_shading_params_t *)params);
    /* Coords must be set by client */
    params->Domain[0] = 0;
    params->Domain[1] = 1;
    params->Function = 0;	/* must be set by client */
    params->Extend[0] = params->Extend[1] = false;
}

/* Allocate and initialize a Radial shading. */
static const gs_shading_procs_t shading_R_procs = {
    gs_shading_R_fill_rectangle
};
int
gs_shading_R_init(gs_shading_t ** ppsh,
                  const gs_shading_R_params_t * params, gs_memory_t * mem)
{
    gs_shading_R_t *psh;
    int code;

    if (params == NULL || params->Domain[0] == params->Domain[1] ||
        params->Coords[2] < 0 || params->Coords[5] < 0)
        return_error(gs_error_rangecheck);
    code = check_CBFD((const gs_shading_params_t *)params,
                          params->Function, params->Domain, 1);

    if (code < 0)
        return code;
    ALLOC_SHADING(ppsh, psh, mem, &st_shading_R, shading_type_Radial,
                  shading_R_procs, "gs_shading_R_init", params);
    return 0;
}

/* ---------------- Free-form Gouraud triangle mesh shading ---------------- */

private_st_shading_FfGt();

/* Initialize parameters for a Free-form Gouraud triangle mesh shading. */
void
gs_shading_FfGt_params_init(gs_shading_FfGt_params_t * params)
{
    mesh_shading_params_init((gs_shading_mesh_params_t *)params);
    /* Client must set BitsPerFlag if DataSource is not an array. */
}

/* Allocate and initialize a Free-form Gouraud triangle mesh shading. */
static const gs_shading_procs_t shading_FfGt_procs = {
    gs_shading_FfGt_fill_rectangle
};
int
gs_shading_FfGt_init(gs_shading_t ** ppsh,
                     const gs_shading_FfGt_params_t * params,
                     gs_memory_t * mem)
{
    gs_shading_FfGt_t *psh;
    int code = check_mesh((const gs_shading_mesh_params_t *)params);
    int bpf = check_BPF(&params->DataSource, params->BitsPerFlag);

    if (code < 0)
        return code;
    if (bpf < 0)
        return bpf;
    ALLOC_SHADING(ppsh, psh, mem, &st_shading_FfGt, shading_type_Free_form_Gouraud_triangle,
                  shading_FfGt_procs, "gs_shading_FfGt_init", params);
    psh->params.BitsPerFlag = bpf;
    return 0;
}

/* -------------- Lattice-form Gouraud triangle mesh shading -------------- */

private_st_shading_LfGt();

/* Initialize parameters for a Lattice-form Gouraud triangle mesh shading. */
void
gs_shading_LfGt_params_init(gs_shading_LfGt_params_t * params)
{
    mesh_shading_params_init((gs_shading_mesh_params_t *)params);
    /* Client must set VerticesPerRow. */
}

/* Allocate and initialize a Lattice-form Gouraud triangle mesh shading. */
static const gs_shading_procs_t shading_LfGt_procs = {
    gs_shading_LfGt_fill_rectangle
};
int
gs_shading_LfGt_init(gs_shading_t ** ppsh,
                 const gs_shading_LfGt_params_t * params, gs_memory_t * mem)
{
    gs_shading_LfGt_t *psh;
    int code = check_mesh((const gs_shading_mesh_params_t *)params);

    if (code < 0)
        return code;
    if (params->VerticesPerRow < 2)
        return_error(gs_error_rangecheck);
    ALLOC_SHADING(ppsh, psh, mem, &st_shading_LfGt, shading_type_Lattice_form_Gouraud_triangle,
                  shading_LfGt_procs, "gs_shading_LfGt_init", params);
    return 0;
}

/* ---------------- Coons patch mesh shading ---------------- */

private_st_shading_Cp();

/* Initialize parameters for a Coons patch mesh shading. */
void
gs_shading_Cp_params_init(gs_shading_Cp_params_t * params)
{
    mesh_shading_params_init((gs_shading_mesh_params_t *)params);
    /* Client must set BitsPerFlag if DataSource is not an array. */
}

/* Allocate and initialize a Coons patch mesh shading. */
static const gs_shading_procs_t shading_Cp_procs = {
    gs_shading_Cp_fill_rectangle
};
int
gs_shading_Cp_init(gs_shading_t ** ppsh,
                   const gs_shading_Cp_params_t * params, gs_memory_t * mem)
{
    gs_shading_Cp_t *psh;
    int code = check_mesh((const gs_shading_mesh_params_t *)params);
    int bpf = check_BPF(&params->DataSource, params->BitsPerFlag);

    if (code < 0)
        return code;
    if (bpf < 0)
        return bpf;
    ALLOC_SHADING(ppsh, psh, mem, &st_shading_Cp, shading_type_Coons_patch,
                  shading_Cp_procs, "gs_shading_Cp_init", params);
    psh->params.BitsPerFlag = bpf;
    return 0;
}

/* ---------------- Tensor product patch mesh shading ---------------- */

private_st_shading_Tpp();

/* Initialize parameters for a Tensor product patch mesh shading. */
void
gs_shading_Tpp_params_init(gs_shading_Tpp_params_t * params)
{
    mesh_shading_params_init((gs_shading_mesh_params_t *)params);
    /* Client must set BitsPerFlag if DataSource is not an array. */
}

/* Allocate and initialize a Tensor product patch mesh shading. */
static const gs_shading_procs_t shading_Tpp_procs = {
    gs_shading_Tpp_fill_rectangle
};
int
gs_shading_Tpp_init(gs_shading_t ** ppsh,
                  const gs_shading_Tpp_params_t * params, gs_memory_t * mem)
{
    gs_shading_Tpp_t *psh;
    int code = check_mesh((const gs_shading_mesh_params_t *)params);
    int bpf = check_BPF(&params->DataSource, params->BitsPerFlag);

    if (code < 0)
        return code;
    if (bpf < 0)
        return bpf;
    ALLOC_SHADING(ppsh, psh, mem, &st_shading_Tpp, shading_type_Tensor_product_patch,
                  shading_Tpp_procs, "gs_shading_Tpp_init", params);
    psh->params.BitsPerFlag = bpf;
    return 0;
}

/* ================ Shading rendering ================ */

/* Add a user-space rectangle to a path. */
int
gs_shading_path_add_box(gx_path *ppath, const gs_rect *pbox,
                     const gs_matrix_fixed *pmat)
{
    gs_fixed_point pt;
    gs_fixed_point pts[3];
    int code;

    if ((code = gs_point_transform2fixed(pmat, pbox->p.x, pbox->p.y,
                                         &pt)) < 0 ||
        (code = gx_path_add_point(ppath, pt.x, pt.y)) < 0 ||
        (code = gs_point_transform2fixed(pmat, pbox->q.x, pbox->p.y,
                                         &pts[0])) < 0 ||
        (code = gs_point_transform2fixed(pmat, pbox->q.x, pbox->q.y,
                                         &pts[1])) < 0 ||
        (code = gs_point_transform2fixed(pmat, pbox->p.x, pbox->q.y,
                                         &pts[2])) < 0 ||
        (code = gx_path_add_lines(ppath, pts, 3)) < 0
        )
        DO_NOTHING;
    return code;
}

/* Fill a path with a shading. */
int
gs_shading_do_fill_rectangle(const gs_shading_t *psh,
                     const gs_fixed_rect *prect, gx_device *dev,
                     gs_gstate *pgs, bool fill_background)
{   /* If you need to fill a path, clip the output device before calling this function. */
    const gs_matrix_fixed *pmat = &pgs->ctm;
    gs_fixed_rect path_box;
    gs_rect path_rect;
    gs_rect rect;
    int code = 0;

    dev_proc(dev, get_clipping_box)(dev, &path_box);
    if (prect)
        rect_intersect(path_box, *prect);
    if (psh->params.Background && fill_background) {
        const gs_color_space *pcs = psh->params.ColorSpace;
        gs_client_color cc;
        gx_device_color dev_color;

        cc = *psh->params.Background;
        (*pcs->type->restrict_color)(&cc, pcs);
        code = (*pcs->type->remap_color)(&cc, pcs, &dev_color, pgs,
                                         dev, gs_color_select_texture);

        /****** WRONG IF NON-IDEMPOTENT RasterOp ******/
        if (code >= 0)
            code = gx_shade_background(dev, &path_box, &dev_color, pgs->log_op);
    }
    if (code >= 0) {
        path_rect.p.x = fixed2float(path_box.p.x);
        path_rect.p.y = fixed2float(path_box.p.y);
        path_rect.q.x = fixed2float(path_box.q.x);
        path_rect.q.y = fixed2float(path_box.q.y);
        code = gs_bbox_transform_inverse(&path_rect, (const gs_matrix *)pmat, &rect);
        if (code >= 0)
            code = gs_shading_fill_rectangle(psh, &rect, &path_box, dev, pgs);
    }
    return code;
}