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
|
/* Copyright (C) 2001-2006 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 that
license. Refer to licensing information at http://www.artifex.com/
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
*/
/* $Id$ */
/* Shading rendering support */
#include "math_.h"
#include "gx.h"
#include "gserrors.h"
#include "gsrect.h"
#include "gxcspace.h"
#include "gscindex.h"
#include "gscie.h" /* requires gscspace.h */
#include "gxdevcli.h"
#include "gxistate.h"
#include "gxdht.h" /* for computing # of different colors */
#include "gxpaint.h"
#include "gxshade.h"
#include "gxshade4.h"
#include "gsicc.h"
#include "gsicc_cache.h"
/* Define a maximum smoothness value. */
/* smoothness > 0.2 produces severely blocky output. */
#define MAX_SMOOTHNESS 0.2
/* ================ Packed coordinate streams ================ */
/* Forward references */
static int cs_next_packed_value(shade_coord_stream_t *, int, uint *);
static int cs_next_array_value(shade_coord_stream_t *, int, uint *);
static int cs_next_packed_decoded(shade_coord_stream_t *, int,
const float[2], float *);
static int cs_next_array_decoded(shade_coord_stream_t *, int,
const float[2], float *);
static void cs_packed_align(shade_coord_stream_t *cs, int radix);
static void cs_array_align(shade_coord_stream_t *cs, int radix);
static bool cs_eod(const shade_coord_stream_t * cs);
/* Initialize a packed value stream. */
void
shade_next_init(shade_coord_stream_t * cs,
const gs_shading_mesh_params_t * params,
const gs_imager_state * pis)
{
cs->params = params;
cs->pctm = &pis->ctm;
if (data_source_is_stream(params->DataSource)) {
/*
* Rewind the data stream iff it is reusable -- either a reusable
* file or a reusable string.
*/
stream *s = cs->s = params->DataSource.data.strm;
if ((s->file != 0 && s->file_limit != max_long) ||
(s->file == 0 && s->strm == 0)
)
sseek(s, 0);
} else {
s_init(&cs->ds, NULL);
sread_string(&cs->ds, params->DataSource.data.str.data,
params->DataSource.data.str.size);
cs->s = &cs->ds;
}
if (data_source_is_array(params->DataSource)) {
cs->get_value = cs_next_array_value;
cs->get_decoded = cs_next_array_decoded;
cs->align = cs_array_align;
} else {
cs->get_value = cs_next_packed_value;
cs->get_decoded = cs_next_packed_decoded;
cs->align = cs_packed_align;
}
cs->is_eod = cs_eod;
cs->left = 0;
cs->ds_EOF = false;
}
/* Check for the End-Of-Data state form a stream. */
static bool
cs_eod(const shade_coord_stream_t * cs)
{
return cs->ds_EOF;
}
/* Get the next (integer) value from a packed value stream. */
/* 1 <= num_bits <= sizeof(uint) * 8. */
static int
cs_next_packed_value(shade_coord_stream_t * cs, int num_bits, uint * pvalue)
{
uint bits = cs->bits;
int left = cs->left;
if (left >= num_bits) {
/* We can satisfy this request with the current buffered bits. */
cs->left = left -= num_bits;
*pvalue = (bits >> left) & ((1 << num_bits) - 1);
} else {
/* We need more bits. */
int needed = num_bits - left;
uint value = bits & ((1 << left) - 1); /* all the remaining bits */
for (; needed >= 8; needed -= 8) {
int b = sgetc(cs->s);
if (b < 0) {
cs->ds_EOF = true;
return_error(gs_error_rangecheck);
}
value = (value << 8) + b;
}
if (needed == 0) {
cs->left = 0;
*pvalue = value;
} else {
int b = sgetc(cs->s);
if (b < 0) {
cs->ds_EOF = true;
return_error(gs_error_rangecheck);
}
cs->bits = b;
cs->left = left = 8 - needed;
*pvalue = (value << needed) + (b >> left);
}
}
return 0;
}
/*
* Get the next (integer) value from an unpacked array. Note that
* num_bits may be 0 if we are reading a coordinate or color value.
*/
static int
cs_next_array_value(shade_coord_stream_t * cs, int num_bits, uint * pvalue)
{
float value;
uint read;
if (sgets(cs->s, (byte *)&value, sizeof(float), &read) < 0 ||
read != sizeof(float)) {
cs->ds_EOF = true;
return_error(gs_error_rangecheck);
}
if (value < 0 || (num_bits != 0 && num_bits < sizeof(uint) * 8 &&
value >= (1 << num_bits)) ||
value != (uint)value
)
return_error(gs_error_rangecheck);
*pvalue = (uint) value;
return 0;
}
/* Get the next decoded floating point value. */
static int
cs_next_packed_decoded(shade_coord_stream_t * cs, int num_bits,
const float decode[2], float *pvalue)
{
uint value;
int code = cs->get_value(cs, num_bits, &value);
double max_value = (double)(uint)
(num_bits == sizeof(uint) * 8 ? ~0 : ((1 << num_bits) - 1));
if (code < 0)
return code;
*pvalue =
(decode == 0 ? value / max_value :
decode[0] + value * (decode[1] - decode[0]) / max_value);
return 0;
}
/* Get the next floating point value from an array, without decoding. */
static int
cs_next_array_decoded(shade_coord_stream_t * cs, int num_bits,
const float decode[2], float *pvalue)
{
float value;
uint read;
if (sgets(cs->s, (byte *)&value, sizeof(float), &read) < 0 ||
read != sizeof(float)
) {
cs->ds_EOF = true;
return_error(gs_error_rangecheck);
}
*pvalue = value;
return 0;
}
static void
cs_packed_align(shade_coord_stream_t *cs, int radix)
{
cs->left = cs->left / radix * radix;
}
static void
cs_array_align(shade_coord_stream_t *cs, int radix)
{
}
/* Get the next flag value. */
/* Note that this always starts a new data byte. */
int
shade_next_flag(shade_coord_stream_t * cs, int BitsPerFlag)
{
uint flag;
int code;
cs->left = 0; /* start a new byte if packed */
code = cs->get_value(cs, BitsPerFlag, &flag);
return (code < 0 ? code : flag);
}
/* Get one or more coordinate pairs. */
int
shade_next_coords(shade_coord_stream_t * cs, gs_fixed_point * ppt,
int num_points)
{
int num_bits = cs->params->BitsPerCoordinate;
const float *decode = cs->params->Decode;
int code = 0;
int i;
for (i = 0; i < num_points; ++i) {
float x, y;
if ((code = cs->get_decoded(cs, num_bits, decode, &x)) < 0 ||
(code = cs->get_decoded(cs, num_bits, decode + 2, &y)) < 0 ||
(code = gs_point_transform2fixed(cs->pctm, x, y, &ppt[i])) < 0
)
break;
}
return code;
}
/* Get a color. Currently all this does is look up Indexed colors. */
int
shade_next_color(shade_coord_stream_t * cs, float *pc)
{
const float *decode = cs->params->Decode + 4; /* skip coord decode */
const gs_color_space *pcs = cs->params->ColorSpace;
gs_color_space_index index = gs_color_space_get_index(pcs);
int num_bits = cs->params->BitsPerComponent;
if (index == gs_color_space_index_Indexed) {
int ncomp = gs_color_space_num_components(gs_cspace_base_space(pcs));
int ci;
float cf;
int code = cs->get_decoded(cs, num_bits, decode, &cf);
gs_client_color cc;
int i;
if (code < 0)
return code;
if (cf < 0)
return_error(gs_error_rangecheck);
ci = (int)cf;
if (ci >= gs_cspace_indexed_num_entries(pcs))
return_error(gs_error_rangecheck);
code = gs_cspace_indexed_lookup(pcs, ci, &cc);
if (code < 0)
return code;
for (i = 0; i < ncomp; ++i)
pc[i] = cc.paint.values[i];
} else {
int i, code;
int ncomp = (cs->params->Function != 0 ? 1 :
gs_color_space_num_components(pcs));
for (i = 0; i < ncomp; ++i) {
if ((code = cs->get_decoded(cs, num_bits, decode + i * 2, &pc[i])) < 0)
return code;
if (cs->params->Function) {
gs_function_params_t *params = &cs->params->Function->params;
if (pc[i] < params->Domain[i + i])
pc[i] = params->Domain[i + i];
else if (pc[i] > params->Domain[i + i + 1])
pc[i] = params->Domain[i + i + 1];
}
}
}
return 0;
}
/* Get the next vertex for a mesh element. */
int
shade_next_vertex(shade_coord_stream_t * cs, shading_vertex_t * vertex, patch_color_t *c)
{ /* Assuming p->c == c, provides a non-const access. */
int code = shade_next_coords(cs, &vertex->p, 1);
if (code >= 0)
code = shade_next_color(cs, c->cc.paint.values);
if (code >= 0)
cs->align(cs, 8); /* CET 09-47J.PS SpecialTestI04Test01. */
return code;
}
/* ================ Shading rendering ================ */
/* Initialize the common parts of the recursion state. */
void
shade_init_fill_state(shading_fill_state_t * pfs, const gs_shading_t * psh,
gx_device * dev, gs_imager_state * pis)
{
const gs_color_space *pcs = psh->params.ColorSpace;
float max_error = min(pis->smoothness, MAX_SMOOTHNESS);
bool is_lab;
/*
* There's no point in trying to achieve smoothness beyond what
* the device can implement, i.e., the number of representable
* colors times the number of halftone levels.
*/
long num_colors =
max(dev->color_info.max_gray, dev->color_info.max_color) + 1;
const gs_range *ranges = 0;
int ci;
gsicc_rendering_param_t rendering_params;
pfs->dev = dev;
pfs->pis = pis;
top:
pfs->direct_space = pcs;
pfs->num_components = gs_color_space_num_components(pcs);
switch ( gs_color_space_get_index(pcs) )
{
case gs_color_space_index_Indexed:
pcs = gs_cspace_base_space(pcs);
goto top;
case gs_color_space_index_CIEDEFG:
ranges = pcs->params.defg->RangeDEFG.ranges;
break;
case gs_color_space_index_CIEDEF:
ranges = pcs->params.def->RangeDEF.ranges;
break;
case gs_color_space_index_CIEABC:
ranges = pcs->params.abc->RangeABC.ranges;
break;
case gs_color_space_index_CIEA:
ranges = &pcs->params.a->RangeA;
break;
case gs_color_space_index_ICC:
ranges = pcs->cmm_icc_profile_data->Range.ranges;
break;
default:
break;
}
if (num_colors <= 32) {
gx_ht_order_component *components = pis->dev_ht->components;
if (components && components[0].corder.wts)
num_colors = 256;
else
/****** WRONG FOR MULTI-PLANE HALFTONES ******/
num_colors *= pis->dev_ht->components[0].corder.num_levels;
}
if (psh->head.type == 2 || psh->head.type == 3) {
max_error *= 0.25;
num_colors *= 2;
}
if (max_error < 1.0 / num_colors)
max_error = 1.0 / num_colors;
for (ci = 0; ci < pfs->num_components; ++ci)
pfs->cc_max_error[ci] =
(ranges == 0 ? max_error :
max_error * (ranges[ci].rmax - ranges[ci].rmin));
if (pis->has_transparency && pis->trans_device != NULL) {
pfs->trans_device = pis->trans_device;
} else {
pfs->trans_device = dev;
}
/* If the CS is PS based and we have not yet converted to the ICC form
then go ahead and do that now */
if (gs_color_space_is_PSCIE(pcs) && pcs->icc_equivalent == NULL) {
gs_colorspace_set_icc_equivalent(pcs, &(is_lab), pis->memory);
}
rendering_params.black_point_comp = BP_ON;
rendering_params.object_type = GS_PATH_TAG;
rendering_params.rendering_intent = pis->renderingintent;
/* Grab the icc link transform that we need now */
if (pcs->cmm_icc_profile_data != NULL) {
pfs->icclink = gsicc_get_link(pis, pis->trans_device, pcs, NULL,
&rendering_params, pis->memory, false);
} else {
if (pcs->icc_equivalent != NULL ) {
/* We have a PS equivalent ICC profile. We may need to go
through special range adjustments in this case */
pfs->icclink = gsicc_get_link(pis, pis->trans_device,
pcs->icc_equivalent, NULL,
&rendering_params, pis->memory, false);
} else {
pfs->icclink = NULL;
}
}
}
/* Fill one piece of a shading. */
int
shade_fill_path(const shading_fill_state_t * pfs, gx_path * ppath,
gx_device_color * pdevc, const gs_fixed_point *fill_adjust)
{
gx_fill_params params;
params.rule = -1; /* irrelevant */
params.adjust = *fill_adjust;
params.flatness = 0; /* irrelevant */
return (*dev_proc(pfs->dev, fill_path)) (pfs->dev, pfs->pis, ppath,
¶ms, pdevc, NULL);
}
|