1 #ifndef MUPDF_FITZ_SHADE_H
2 #define MUPDF_FITZ_SHADE_H
3 
4 #include "mupdf/fitz/system.h"
5 #include "mupdf/fitz/context.h"
6 #include "mupdf/fitz/geometry.h"
7 #include "mupdf/fitz/store.h"
8 #include "mupdf/fitz/pixmap.h"
9 #include "mupdf/fitz/compressed-buffer.h"
10 
11 /**
12  * The shading code uses gouraud shaded triangle meshes.
13  */
14 
15 enum
16 {
17 	FZ_FUNCTION_BASED = 1,
18 	FZ_LINEAR = 2,
19 	FZ_RADIAL = 3,
20 	FZ_MESH_TYPE4 = 4,
21 	FZ_MESH_TYPE5 = 5,
22 	FZ_MESH_TYPE6 = 6,
23 	FZ_MESH_TYPE7 = 7
24 };
25 
26 /**
27 	Structure is public to allow derived classes. Do not
28 	access the members directly.
29 */
30 typedef struct
31 {
32 	fz_storable storable;
33 
34 	fz_rect bbox;		/* can be fz_infinite_rect */
35 	fz_colorspace *colorspace;
36 
37 	fz_matrix matrix;	/* matrix from pattern dict */
38 	int use_background;	/* background color for fills but not 'sh' */
39 	float background[FZ_MAX_COLORS];
40 
41 	/* Just to be confusing, PDF Shadings of Type 1 (Function Based
42 	 * Shadings), do NOT use_function, but all the others do. This
43 	 * is because Type 1 shadings take 2 inputs, whereas all the
44 	 * others (when used with a function take 1 input. The type 1
45 	 * data is in the 'f' field of the union below. */
46 	int use_function;
47 	float function[256][FZ_MAX_COLORS + 1];
48 
49 	int type; /* function, linear, radial, mesh */
50 	union
51 	{
52 		struct
53 		{
54 			int extend[2];
55 			float coords[2][3]; /* (x,y,r) twice */
56 		} l_or_r;
57 		struct
58 		{
59 			int vprow;
60 			int bpflag;
61 			int bpcoord;
62 			int bpcomp;
63 			float x0, x1;
64 			float y0, y1;
65 			float c0[FZ_MAX_COLORS];
66 			float c1[FZ_MAX_COLORS];
67 		} m;
68 		struct
69 		{
70 			fz_matrix matrix;
71 			int xdivs;
72 			int ydivs;
73 			float domain[2][2];
74 			float *fn_vals;
75 		} f;
76 	} u;
77 
78 	fz_compressed_buffer *buffer;
79 } fz_shade;
80 
81 /**
82 	Increment the reference count for the shade structure. The
83 	same pointer is returned.
84 
85 	Never throws exceptions.
86 */
87 fz_shade *fz_keep_shade(fz_context *ctx, fz_shade *shade);
88 
89 /**
90 	Decrement the reference count for the shade structure. When
91 	the reference count hits zero, the structure is freed.
92 
93 	Never throws exceptions.
94 */
95 void fz_drop_shade(fz_context *ctx, fz_shade *shade);
96 
97 /**
98 	Bound a given shading.
99 
100 	shade: The shade to bound.
101 
102 	ctm: The transform to apply to the shade before bounding.
103 
104 	r: Pointer to storage to put the bounds in.
105 
106 	Returns r, updated to contain the bounds for the shading.
107 */
108 fz_rect fz_bound_shade(fz_context *ctx, fz_shade *shade, fz_matrix ctm);
109 
110 /**
111 	Render a shade to a given pixmap.
112 
113 	shade: The shade to paint.
114 
115 	override_cs: NULL, or colorspace to override the shades
116 	inbuilt colorspace.
117 
118 	ctm: The transform to apply.
119 
120 	dest: The pixmap to render into.
121 
122 	color_params: The color rendering settings
123 
124 	bbox: Pointer to a bounding box to limit the rendering
125 	of the shade.
126 
127 	op: NULL, or pointer to overprint bitmap.
128 */
129 void fz_paint_shade(fz_context *ctx, fz_shade *shade, fz_colorspace *override_cs, fz_matrix ctm, fz_pixmap *dest, fz_color_params color_params, fz_irect bbox, const fz_overprint *eop);
130 
131 /**
132  *	Handy routine for processing mesh based shades
133  */
134 typedef struct
135 {
136 	fz_point p;
137 	float c[FZ_MAX_COLORS];
138 } fz_vertex;
139 
140 /**
141 	Callback function type for use with
142 	fz_process_shade.
143 
144 	arg: Opaque pointer from fz_process_shade caller.
145 
146 	v: Pointer to a fz_vertex structure to populate.
147 
148 	c: Pointer to an array of floats used to populate v.
149 */
150 typedef void (fz_shade_prepare_fn)(fz_context *ctx, void *arg, fz_vertex *v, const float *c);
151 
152 /**
153 	Callback function type for use with
154 	fz_process_shade.
155 
156 	arg: Opaque pointer from fz_process_shade caller.
157 
158 	av, bv, cv: Pointers to a fz_vertex structure describing
159 	the corner locations and colors of a triangle to be
160 	filled.
161 */
162 typedef void (fz_shade_process_fn)(fz_context *ctx, void *arg, fz_vertex *av, fz_vertex *bv, fz_vertex *cv);
163 
164 /**
165 	Process a shade, using supplied callback functions. This
166 	decomposes the shading to a mesh (even ones that are not
167 	natively meshes, such as linear or radial shadings), and
168 	processes triangles from those meshes.
169 
170 	shade: The shade to process.
171 
172 	ctm: The transform to use
173 
174 	prepare: Callback function to 'prepare' each vertex.
175 	This function is passed an array of floats, and populates
176 	a fz_vertex structure.
177 
178 	process: This function is passed 3 pointers to vertex
179 	structures, and actually performs the processing (typically
180 	filling the area between the vertexes).
181 
182 	process_arg: An opaque argument passed through from caller
183 	to callback functions.
184 */
185 void fz_process_shade(fz_context *ctx, fz_shade *shade, fz_matrix ctm, fz_rect scissor,
186 			fz_shade_prepare_fn *prepare,
187 			fz_shade_process_fn *process,
188 			void *process_arg);
189 
190 
191 /* Implementation details: subject to change. */
192 
193 /**
194 	Internal function to destroy a
195 	shade. Only exposed for use with the fz_store.
196 
197 	shade: The reference to destroy.
198 */
199 void fz_drop_shade_imp(fz_context *ctx, fz_storable *shade);
200 
201 #endif
202