summaryrefslogtreecommitdiff
path: root/gs/src/gxshade1.c
blob: 0adee431107ec604e02427ed921dd74fec21faec (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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* Copyright (C) 1998 Aladdin Enterprises.  All rights reserved.

   This file is part of Aladdin Ghostscript.

   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
   or distributor accepts any responsibility for the consequences of using it,
   or for whether it serves any particular purpose or works at all, unless he
   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
   License (the "License") for full details.

   Every copy of Aladdin Ghostscript must include a copy of the License,
   normally in a plain ASCII text file named PUBLIC.  The License grants you
   the right to copy, modify and redistribute Aladdin Ghostscript, but only
   under certain conditions described in the License.  Among other things, the
   License requires that the copyright notice and this notice be preserved on
   all copies.
 */

/*$Id$ */
/* Rendering for non-mesh shadings */
#include "math_.h"
#include "gx.h"
#include "gserrors.h"
#include "gsmatrix.h"		/* for gscoord.h */
#include "gscoord.h"
#include "gspath.h"
#include "gxcspace.h"
#include "gxdcolor.h"
#include "gxfarith.h"
#include "gxfixed.h"
#include "gxistate.h"
#include "gxpath.h"
#include "gxshade.h"

/* ================ Utilities ================ */

/* Check whether 2 colors fall within the smoothness criterion. */
private bool
shade_colors2_converge(const gs_client_color cc[2],
		       const shading_fill_state_t * pfs)
{
    int ci;

    for (ci = pfs->num_components - 1; ci >= 0; --ci)
	if (fabs(cc[1].paint.values[ci] - cc[0].paint.values[ci]) >
	    pfs->cc_max_error[ci]
	    )
	    return false;
    return true;
}

/* Fill a user space rectangle that is also a device space rectangle. */
private int
shade_fill_device_rectangle(const shading_fill_state_t * pfs,
			    const gs_fixed_point * p0, const gs_fixed_point * p1, gx_device_color * pdevc)
{
    gs_imager_state *pis = pfs->pis;
    fixed xmin, ymin, xmax, ymax;
    int x, y;

    if (p0->x < p1->x)
	xmin = p0->x, xmax = p1->x;
    else
	xmin = p1->x, xmax = p0->x;
    if (p0->y < p1->y)
	ymin = p0->y, ymax = p1->y;
    else
	ymin = p1->y, ymax = p0->y;
/****** CONFINE TO rect ******/
/****** NOT QUITE RIGHT FOR PIXROUND ******/
    xmin -= pis->fill_adjust.x;
    xmax += pis->fill_adjust.x;
    ymin -= pis->fill_adjust.y;
    ymax += pis->fill_adjust.y;
    x = fixed2int_var(xmin);
    y = fixed2int_var(ymin);
    return
	gx_fill_rectangle_device_rop(x, y,
				     fixed2int_var(xmax) - x,
				     fixed2int_var(ymax) - y,
				     pdevc, pfs->dev, pis->log_op);
}

/* ================ Specific shadings ================ */

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

typedef struct Fb_fill_state_s {
    shading_fill_state_common;
    const gs_shading_Fb_t *psh;
    gs_matrix_fixed ptm;	/* parameter space -> device space */
    gs_client_color cc[4];
} Fb_fill_state_t;

private int
Fb_fill_region(Fb_fill_state_t * pfs, floatp x0, floatp y0, floatp x1,
	       floatp y1)
{
    const gs_shading_Fb_t *psh = pfs->psh;
    gs_imager_state *pis = pfs->pis;

  top:if (!shade_colors4_converge(pfs->cc,
				(const shading_fill_state_t *)pfs)
	) {
	/*
	 * The colors don't converge.  Does the region color more than
	 * a single pixel?
	 */
	gs_rect region;

	region.p.x = x0, region.p.y = y0;
	region.q.x = x1, region.q.y = y1;
	gs_bbox_transform(&region, (const gs_matrix *)&pfs->ptm, &region);
	if (region.q.x - region.p.x >= 1 || region.q.y - region.p.y >= 1)
	    goto recur;
	{
	    /*
	     * More precisely, does the bounding box of the region,
	     * taking fill adjustment into account, span more than 1
	     * pixel center in either X or Y?
	     */
	    fixed ax = pis->fill_adjust.x;
	    int nx =
	    fixed2int_pixround(float2fixed(region.q.x) + ax) -
	    fixed2int_pixround(float2fixed(region.p.x) - ax);
	    fixed ay = pis->fill_adjust.y;
	    int ny =
	    fixed2int_pixround(float2fixed(region.q.y) + ay) -
	    fixed2int_pixround(float2fixed(region.p.y) - ay);

	    if ((nx > 1 && ny != 0) || (ny > 1 && nx != 0))
		goto recur;
	}
	/* We could do the 1-pixel case a lot faster! */
    }
    /* Fill the region with the color. */
    {
	gx_device_color dev_color;
	const gs_color_space *pcs = psh->params.ColorSpace;
	gs_fixed_point pts[4];
	int code;

	if_debug0('|', "[|]... filling region\n");
	(*pcs->type->restrict_color) (&pfs->cc[0], pcs);
	(*pcs->type->remap_color) (&pfs->cc[0], pcs, &dev_color, pis,
				   pfs->dev, gs_color_select_texture);
	gs_point_transform2fixed(&pfs->ptm, x0, y0, &pts[0]);
	gs_point_transform2fixed(&pfs->ptm, x1, y1, &pts[2]);
	if (is_xxyy(&pfs->ptm) || is_xyyx(&pfs->ptm)) {
	    code =
		shade_fill_device_rectangle((const shading_fill_state_t *)pfs,
					    &pts[0], &pts[2], &dev_color);
	} else {
	    gx_path *ppath = gx_path_alloc(pis->memory, "Fb_fill");

	    gs_point_transform2fixed(&pfs->ptm, x1, y0, &pts[1]);
	    gs_point_transform2fixed(&pfs->ptm, x0, y1, &pts[3]);
	    gx_path_add_point(ppath, pts[0].x, pts[0].y);
	    gx_path_add_lines(ppath, pts + 1, 3);
	    code = shade_fill_path((const shading_fill_state_t *)pfs,
				   ppath, &dev_color);
	    gx_path_free(ppath, "Fb_fill");
	}
	return code;
    }

    /*
     * No luck.  Subdivide the region and recur.
     *
     * We should subdivide on the axis that has the largest color
     * discrepancy, but for now we subdivide on the axis with the
     * largest coordinate difference.
     */
  recur:{
	gs_client_color cc[4];
	gs_function_t *pfn = psh->params.Function;
	float v[2];

	if (y1 - y0 > x1 - x0) {
	    /* Subdivide in Y. */
	    float ym = (y0 + y1) * 0.5;

	    if_debug1('|', "[|]dividing at y=%g\n", ym);
	    v[1] = ym;
	    v[0] = x0;
	    gs_function_evaluate(pfn, v, cc[0].paint.values);
	    v[0] = x1;
	    gs_function_evaluate(pfn, v, cc[1].paint.values);
	    cc[2].paint = pfs->cc[2].paint;
	    cc[3].paint = pfs->cc[3].paint;
	    pfs->cc[2].paint = cc[0].paint;
	    pfs->cc[3].paint = cc[1].paint;
	    Fb_fill_region(pfs, x0, y0, x1, ym);
	    y0 = ym;
	} else {
	    /* Subdivide in X. */
	    float xm = (x0 + x1) * 0.5;

	    if_debug1('|', "[|]dividing at x=%g\n", xm);
	    v[0] = xm;
	    v[1] = y0;
	    gs_function_evaluate(pfn, v, cc[0].paint.values);
	    v[1] = y1;
	    gs_function_evaluate(pfn, v, cc[2].paint.values);
	    cc[1].paint = pfs->cc[1].paint;
	    cc[3].paint = pfs->cc[3].paint;
	    pfs->cc[1].paint = cc[0].paint;
	    pfs->cc[3].paint = cc[2].paint;
	    Fb_fill_region(pfs, x0, y0, xm, y1);
	    x0 = xm;
	}
	pfs->cc[0].paint = cc[0].paint;
	pfs->cc[1].paint = cc[1].paint;
	pfs->cc[2].paint = cc[2].paint;
	pfs->cc[3].paint = cc[3].paint;
    }
    goto top;
}

int
gs_shading_Fb_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
			     gx_device * dev, gs_imager_state * pis)
{
    const gs_shading_Fb_t *psh = (const gs_shading_Fb_t *)psh0;
    gs_matrix save_ctm;
    int xi, yi, code;
    float x[2], y[2];
    Fb_fill_state_t state;

    shade_init_fill_state((shading_fill_state_t *) & state, psh0, dev, pis);
    state.psh = psh;
/****** HACK ******/
    gs_currentmatrix((gs_state *) pis, &save_ctm);
    gs_concat((gs_state *) pis, &psh->params.Matrix);
    state.ptm = pis->ctm;
    /* Compute the parameter X and Y ranges. */
    {
	gs_rect pbox;

	gs_bbox_transform_inverse(rect, &psh->params.Matrix, &pbox);
	x[0] = max(pbox.p.x, psh->params.Domain[0]);
	x[1] = min(pbox.q.x, psh->params.Domain[1]);
	y[0] = max(pbox.p.y, psh->params.Domain[2]);
	y[1] = min(pbox.q.y, psh->params.Domain[3]);
    }
    for (xi = 0; xi < 2; ++xi)
	for (yi = 0; yi < 2; ++yi) {
	    float v[2];

	    v[0] = x[xi], v[1] = y[yi];
	    gs_function_evaluate(psh->params.Function, v,
				 state.cc[yi * 2 + xi].paint.values);
	}
    code = Fb_fill_region(&state, x[0], y[0], x[1], y[1]);
/****** HACK ******/
    gs_setmatrix((gs_state *) pis, &save_ctm);
    return code;
}

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

typedef struct A_fill_state_s {
    shading_fill_state_common;
    const gs_shading_A_t *psh;
    gs_rect rect;
    gs_client_color cc[2];
    gs_point delta;
    double length, dd;
} A_fill_state_t;

/* Note t0 and t1 vary over [0..1], not the Domain. */
private int
A_fill_region(A_fill_state_t * pfs, floatp t0, floatp t1)
{
    const gs_shading_A_t *psh = pfs->psh;

  top:if (!shade_colors2_converge(pfs->cc,
				(const shading_fill_state_t *)pfs)
	) {
	/*
	 * The colors don't converge.  Is the stripe less than 1 pixel wide?
	 */
	if (pfs->length * (t1 - t0) > 1)
	    goto recur;
	/* We could do the 1-pixel case a lot faster! */
    }
    /* Fill the region with the color. */
    {
	gx_device_color dev_color;
	const gs_color_space *pcs = psh->params.ColorSpace;
	gs_imager_state *pis = pfs->pis;
	double
	       x0 = psh->params.Coords[0] + pfs->delta.x * t0, y0 = psh->params.Coords[1] + pfs->delta.y * t0;
	double
	       x1 = psh->params.Coords[0] + pfs->delta.x * t1, y1 = psh->params.Coords[1] + pfs->delta.y * t1;
	gs_fixed_point pts[4];
	int code;

	(*pcs->type->restrict_color) (&pfs->cc[0], pcs);
	(*pcs->type->remap_color) (&pfs->cc[0], pcs, &dev_color, pis,
				   pfs->dev, gs_color_select_texture);
	if (x0 == x1) {
	    /* Stripe is horizontal. */
	    x0 = pfs->rect.p.x;
	    x1 = pfs->rect.q.x;
	} else if (y0 == y1) {
	    /* Stripe is vertical. */
	    y0 = pfs->rect.p.y;
	    y1 = pfs->rect.q.y;
	} else {
	    /*
	     * Stripe is neither horizontal nor vertical.
	     * Extend it to the edges of the rectangle.
	     */
	    gx_path *ppath = gx_path_alloc(pis->memory, "A_fill");

	    /*
	     * Figuring out how far to extend the stripe is hard,
	     * since one or both of x0/y0 and x1/y1 may lie outside
	     * the rectangle but part of the stripe may lie inside.
	     * For now, we do something simple and ****** WRONG ******.
	     */
	    double dist = max(pfs->rect.q.x - pfs->rect.p.x,
			      pfs->rect.q.y - pfs->rect.p.y);
	    double denom = hypot(pfs->delta.x, pfs->delta.y);
	    double dx = dist * pfs->delta.y / denom, dy = -dist * pfs->delta.x / denom;

	    if_debug6('|', "[|]p0=(%g,%g), p1=(%g,%g), dxy=(%g,%g)\n",
		      x0, y0, x1, y1, dx, dy);
	    gs_point_transform2fixed(&pis->ctm, x0 - dx, y0 - dy, &pts[0]);
	    gs_point_transform2fixed(&pis->ctm, x0 + dx, y0 + dy, &pts[1]);
	    gs_point_transform2fixed(&pis->ctm, x1 + dx, y1 + dy, &pts[2]);
	    gs_point_transform2fixed(&pis->ctm, x1 - dx, y1 - dy, &pts[3]);
	    gx_path_add_point(ppath, pts[0].x, pts[0].y);
	    gx_path_add_lines(ppath, pts + 1, 3);
	    code = shade_fill_path((const shading_fill_state_t *)pfs,
				   ppath, &dev_color);
	    gx_path_free(ppath, "A_fill");
	}
	/* Stripe is horizontal or vertical. */
	gs_point_transform2fixed(&pis->ctm, x0, y0, &pts[0]);
	gs_point_transform2fixed(&pis->ctm, x1, y1, &pts[1]);
	return
	    shade_fill_device_rectangle((const shading_fill_state_t *)pfs,
					&pts[0], &pts[1], &dev_color);
    }

    /*
     * No luck.  Subdivide the interval and recur.
     */
  recur:{
	gs_client_color ccm, cc1;
	gs_function_t *pfn = psh->params.Function;
	float tm = (t0 + t1) * 0.5;
	float dm = tm * pfs->dd + psh->params.Domain[0];

	cc1.paint = pfs->cc[1].paint;
	gs_function_evaluate(pfn, &dm, ccm.paint.values);
	pfs->cc[1].paint = ccm.paint;
	A_fill_region(pfs, t0, tm);
	pfs->cc[0].paint = ccm.paint;
	pfs->cc[1].paint = cc1.paint;
	t0 = tm;
	goto top;
    }
    return 0;
}

int
gs_shading_A_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
			    gx_device * dev, gs_imager_state * pis)
{
    const gs_shading_A_t *const psh = (const gs_shading_A_t *)psh0;
    A_fill_state_t state;
    float d0 = psh->params.Domain[0], d1 = psh->params.Domain[1], dd = d1 - d0;
    float t[2];
    gs_point dist;
    int i;

    shade_init_fill_state((shading_fill_state_t *) & state, psh0, dev, pis);
    state.psh = psh;
    state.rect = *rect;
    /* Compute the parameter range. */
    t[0] = d0;
    t[1] = d1;
/****** INTERSECT Domain WITH rect ******/
    for (i = 0; i < 2; ++i)
	gs_function_evaluate(psh->params.Function, &t[i],
			     state.cc[i].paint.values);
    state.delta.x = psh->params.Coords[2] - psh->params.Coords[0];
    state.delta.y = psh->params.Coords[3] - psh->params.Coords[1];
    gs_distance_transform(state.delta.x, state.delta.y, &ctm_only(pis),
			  &dist);
    state.length = hypot(dist.x, dist.y);	/* device space line length */
    state.dd = dd;
/****** DOESN'T HANDLE Extend ******/
    return A_fill_region(&state, (t[0] - d0) / dd, (t[1] - d0) / dd);
}

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

typedef struct R_fill_state_s {
    shading_fill_state_common;
    const gs_shading_R_t *psh;
    gs_rect rect;
    gs_client_color cc[2];
    gs_point delta;
    double dr, width, dd;
} R_fill_state_t;

/* Note t0 and t1 vary over [0..1], not the Domain. */
private int
R_fill_region(R_fill_state_t * pfs, floatp t0, floatp t1)
{
    const gs_shading_R_t *psh = pfs->psh;

  top:if (!shade_colors2_converge(pfs->cc,
				(const shading_fill_state_t *)pfs)
	) {
	/*
	 * The colors don't converge.  Is the annulus less than 1 pixel wide?
	 */
	if (pfs->width * (t1 - t0) > 1)
	    goto recur;
	/* We could do the 1-pixel case a lot faster! */
    }
    /* Fill the region with the color. */
    {
	gx_device_color dev_color;
	const gs_color_space *pcs = psh->params.ColorSpace;
	gs_imager_state *pis = pfs->pis;
	double
	       x0 = psh->params.Coords[0] + pfs->delta.x * t0, y0 = psh->params.Coords[1] + pfs->delta.y * t0,
	       r0 = psh->params.Coords[2] + pfs->dr * t0;
	double
	       x1 = psh->params.Coords[0] + pfs->delta.x * t1, y1 = psh->params.Coords[1] + pfs->delta.y * t1,
	       r1 = psh->params.Coords[2] + pfs->dr * t1;
	gx_path *ppath = gx_path_alloc(pis->memory, "R_fill");
	int code;

	(*pcs->type->restrict_color) (&pfs->cc[0], pcs);
	(*pcs->type->remap_color) (&pfs->cc[0], pcs, &dev_color, pis,
				   pfs->dev, gs_color_select_texture);
	if ((code = gs_imager_arc_add(ppath, pis, false, x0, y0, r0,
				      0.0, 360.0, false)) >= 0 &&
	    (code = gs_imager_arc_add(ppath, pis, true, x1, y1, r1,
				      0.0, 360.0, false)) >= 0
	    ) {
	    code = shade_fill_path((const shading_fill_state_t *)pfs,
				   ppath, &dev_color);
	}
	gx_path_free(ppath, "R_fill");
	return code;
    }

    /*
     * No luck.  Subdivide the interval and recur.
     */
  recur:{
	gs_client_color ccm, cc1;
	gs_function_t *pfn = psh->params.Function;
	float tm = (t0 + t1) * 0.5;
	float dm = tm * pfs->dd + psh->params.Domain[0];

	cc1.paint = pfs->cc[1].paint;
	gs_function_evaluate(pfn, &dm, ccm.paint.values);
	pfs->cc[1].paint = ccm.paint;
	R_fill_region(pfs, t0, tm);
	pfs->cc[0].paint = ccm.paint;
	pfs->cc[1].paint = cc1.paint;
	t0 = tm;
	goto top;
    }
    return 0;
}

int
gs_shading_R_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
			    gx_device * dev, gs_imager_state * pis)
{
    const gs_shading_R_t *const psh = (const gs_shading_R_t *)psh0;
    R_fill_state_t state;
    float d0 = psh->params.Domain[0], d1 = psh->params.Domain[1], dd = d1 - d0;
    float t[2];
    int i;

    shade_init_fill_state((shading_fill_state_t *) & state, psh0, dev, pis);
    state.psh = psh;
    state.rect = *rect;
    /* Compute the parameter range. */
    t[0] = d0;
    t[1] = d1;
/****** INTERSECT Domain WITH rect ******/
    for (i = 0; i < 2; ++i)
	gs_function_evaluate(psh->params.Function, &t[i],
			     state.cc[i].paint.values);
    state.delta.x = psh->params.Coords[3] - psh->params.Coords[0];
    state.delta.y = psh->params.Coords[4] - psh->params.Coords[1];
    state.dr = psh->params.Coords[5] - psh->params.Coords[2];
    /*
     * Compute the annulus width in its thickest direction.  This is
     * only used for a conservative check, so it can be pretty crude
     * (and it is!).
     */
    state.width =
	(fabs(pis->ctm.xx) + fabs(pis->ctm.xy) + fabs(pis->ctm.yx) +
	 fabs(pis->ctm.yy)) * fabs(state.dr);
    state.dd = dd;
/****** DOESN'T HANDLE Extend ******/
    return R_fill_region(&state, (t[0] - d0) / dd, (t[1] - d0) / dd);
}