1 /* Copyright (C) 2001-2012 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
13    CA  94903, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* Internal definitions for triangle shading rendering */
18 
19 #ifndef gxshade4_INCLUDED
20 #  define gxshade4_INCLUDED
21 
22 /* Configuration flags for development needs only. Users should not modify them. */
23 #define USE_LINEAR_COLOR_PROCS 1 /* Old code = 0, new code = 1. */
24 
25 #define QUADRANGLES 0 /* 0 = decompose by triangles, 1 = by quadrangles. */
26 /* The code QUADRANGLES 1 appears unuseful.
27    We keep it because it stores a valuable code for constant_color_quadrangle,
28    which decomposes a random quadrangle into 3 or 4 trapezoids.
29    The color approximation looks worse than with triangles, and works some slower.
30  */
31 #define INTERPATCH_PADDING (fixed_1 / 2) /* Emulate a trapping for poorly designed documents. */
32 /* When INTERPATCH_PADDING > 0, it generates paddings between patches,
33    i.e. performs a patch expansion, being similar
34    to the path adjustment in the filling algorithm.
35    The expansion is an emulation of Adobe's trapping.
36    The value specifies the width of paddings.
37    We did some testing of Adobe RIP, and it looks as applying
38    same logicks as for clipping - any part of pixel inside.
39    Therefore the expansion should be half pixel size.
40  */
41 #define COLOR_CONTIGUITY 1 /* A smothness divisor for triangulation. */
42 /* This is a coefficient used to rich
43    a better color contiguity. The value 1 corresponds to PLRM,
44    bigger values mean more contiguity. The speed decreases as
45    a square of COLOR_CONTIGUITY.
46  */
47 #define LAZY_WEDGES 1 /* 0 = fill immediately, 1 = fill lazy. */
48 /* This mode delays creating wedges for a boundary until
49    both neoghbour areas are painted. At that moment we can know
50    all subdivision points for both right and left areas,
51    and skip wedges for common points. Therefore the number of wadges
52    dramatically reduces, causing a significant speedup.
53    The LAZY_WEDGES 0 mode was not systematically tested.
54  */
55 #define VD_TRACE_DOWN 1 /* Developer's needs, not important for production. */
56 #define NOFILL_TEST 0 /* Developer's needs, must be off for production. */
57 #define SKIP_TEST 0 /* Developer's needs, must be off for production. */
58 /* End of configuration flags (we don't mean that users should modify the rest). */
59 
60 typedef struct mesh_frame_s {	/* recursion frame */
61     mesh_vertex_t va, vb, vc;	/* current vertices */
62     bool check_clipping;
63 } mesh_frame_t;
64 /****** NEED GC DESCRIPTOR ******/
65 
66 /*
67  * Define the fill state structure for triangle shadings.  This is used
68  * both for the Gouraud triangle shading types and for the Coons and
69  * tensor patch types.
70  *
71  * The shading pointer is named pshm rather than psh in case subclasses
72  * also want to store a pointer of a more specific type.
73  */
74 #define mesh_fill_state_common\
75   shading_fill_state_common;\
76   const gs_shading_mesh_t *pshm;\
77   gs_fixed_rect rect
78 typedef struct mesh_fill_state_s {
79     mesh_fill_state_common;
80 } mesh_fill_state_t;
81 /****** NEED GC DESCRIPTOR ******/
82 
83 typedef struct wedge_vertex_list_elem_s wedge_vertex_list_elem_t;
84 struct wedge_vertex_list_elem_s {
85     gs_fixed_point p;
86     int level;
87     bool divide_count;
88     wedge_vertex_list_elem_t *next, *prev;
89 };
90 typedef struct {
91     bool last_side;
92     wedge_vertex_list_elem_t *beg, *end;
93 } wedge_vertex_list_t;
94 
95 #define LAZY_WEDGES_MAX_LEVEL 9 /* memory consumption is
96     sizeof(wedge_vertex_list_elem_t) * LAZY_WEDGES_MAX_LEVEL * (1 << LAZY_WEDGES_MAX_LEVEL) */
97 
98 #define SHADING_COLOR_STACK_SIZE 200; /* Should be enough for max 64 decomposition levels. */
99 
100 /* Define a color to be used in curve rendering. */
101 /* This may be a real client color, or a parametric function argument. */
102 struct patch_color_s {
103     float t[2];			/* parametric value */
104     gs_client_color cc;
105     /* NOTE : The structure gs_client_color ends with a big array, but only few elements
106        are used in most cases. Therefore sometimes we allocate a shorter area,
107        so that ending elements are not allocated and must not be accessed/modified.
108        The number of allocated elements are known from the shading color space
109        and from patch_fill_state_s::num_components. */
110 };
111 
112 #ifndef patch_color_t_DEFINED
113 #  define patch_color_t_DEFINED
114 typedef struct patch_color_s patch_color_t;
115 #endif
116 
117 #ifndef gs_color_index_cache_DEFINED
118 #  define gs_color_index_cache_DEFINED
119 typedef struct gs_color_index_cache_s gs_color_index_cache_t;
120 #endif
121 
122 #ifndef patch_fill_state_t_DEFINED
123 #  define patch_fill_state_t_DEFINED
124 typedef struct patch_fill_state_s  patch_fill_state_t;
125 #endif
126 
127 /* Define the common state for rendering Coons and tensor patches. */
128 struct patch_fill_state_s {
129     mesh_fill_state_common;
130     const gs_function_t *Function;
131     int function_arg_shift;
132     bool vectorization;
133     int n_color_args;
134     fixed max_small_coord; /* Length restriction for intersection_of_small_bars. */
135     wedge_vertex_list_elem_t *wedge_vertex_list_elem_buffer;
136     wedge_vertex_list_elem_t *free_wedge_vertex;
137     int wedge_vertex_list_elem_count;
138     int wedge_vertex_list_elem_count_max;
139     gs_client_color color_domain;
140     fixed decomposition_limit;
141     fixed fixed_flat;
142     double smoothness;
143     bool maybe_self_intersecting;
144     bool monotonic_color;
145     bool linear_color;
146     bool unlinear;
147     bool inside;
148     int color_stack_size;
149     int color_stack_step;
150     byte *color_stack_ptr;
151     byte *color_stack; /* A storage for shortened patch_color_t structures. */
152     byte *color_stack_limit;
153     gs_memory_t *memory; /* Where color_buffer is allocated. */
154     gs_color_index_cache_t *pcic;
155 } ;
156 
157 /* Define a structure for mesh or patch vertex. */
158 struct shading_vertex_s {
159     gs_fixed_point p;
160     const patch_color_t *c;
161 };
162 
163 /* Define one segment (vertex and next control points) of a curve. */
164 typedef struct patch_curve_s {
165     mesh_vertex_t vertex;
166     gs_fixed_point control[2];
167     bool straight;
168 } patch_curve_t;
169 
170 typedef struct {
171     const shading_vertex_t *p[2][2]; /* [v][u] */
172     wedge_vertex_list_t *l0001, *l0111, *l1110, *l1000;
173 } quadrangle_patch;
174 
175 /* Initialize the fill state for triangle shading. */
176 int mesh_init_fill_state(mesh_fill_state_t * pfs,
177                           const gs_shading_mesh_t * psh,
178                           const gs_fixed_rect * rect_clip,
179                           gx_device * dev, gs_imager_state * pis);
180 
181 int init_patch_fill_state(patch_fill_state_t *pfs);
182 bool term_patch_fill_state(patch_fill_state_t *pfs);
183 int gx_init_patch_fill_state_for_clist(gx_device *dev, patch_fill_state_t *pfs, gs_memory_t *memory);
184 
185 int mesh_triangle(patch_fill_state_t *pfs,
186     const shading_vertex_t *p0, const shading_vertex_t *p1, const shading_vertex_t *p2);
187 
188 int mesh_padding(patch_fill_state_t *pfs, const gs_fixed_point *p0, const gs_fixed_point *p1,
189             const patch_color_t *c0, const patch_color_t *c1);
190 
191 int patch_fill(patch_fill_state_t * pfs, const patch_curve_t curve[4],
192            const gs_fixed_point interior[4],
193            void (*transform) (gs_fixed_point *, const patch_curve_t[4],
194                               const gs_fixed_point[4], floatp, floatp));
195 
196 int constant_color_quadrangle(patch_fill_state_t *pfs, const quadrangle_patch *p, bool self_intersecting);
197 
198 int wedge_vertex_list_elem_buffer_alloc(patch_fill_state_t *pfs);
199 void wedge_vertex_list_elem_buffer_free(patch_fill_state_t *pfs);
200 
201 void patch_resolve_color(patch_color_t * ppcr, const patch_fill_state_t *pfs);
202 
203 int gx_shade_background(gx_device *pdev, const gs_fixed_rect *rect,
204         const gx_device_color *pdevc, gs_logical_operation_t log_op);
205 
206 int patch_color_to_device_color(const patch_fill_state_t *pfs,
207         const patch_color_t *c, gx_device_color *pdevc);
208 
209 byte *reserve_colors(patch_fill_state_t *pfs, patch_color_t *c0[], int n);
210 void release_colors(patch_fill_state_t *pfs, byte *ptr, int n);
211 
212 dev_proc_fill_linear_color_triangle(gx_fill_triangle_small);
213 
214 #endif /* gxshade4_INCLUDED */
215