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
|
#include "mupdf/pdf.h"
/* FIXME: Remove this somehow */
#define FUNSEGS 32 /* size of sampled mesh for function-based shadings */
/* Sample various functions into lookup tables */
static void
pdf_sample_composite_shade_function(fz_context *ctx, fz_shade *shade, fz_function *func, float t0, float t1)
{
int i;
float t;
for (i = 0; i < 256; i++)
{
t = t0 + (i / 255.0f) * (t1 - t0);
fz_eval_function(ctx, func, &t, 1, shade->function[i], shade->colorspace->n);
shade->function[i][shade->colorspace->n] = 1;
}
}
static void
pdf_sample_component_shade_function(fz_context *ctx, fz_shade *shade, int funcs, fz_function **func, float t0, float t1)
{
int i, k;
float t;
for (i = 0; i < 256; i++)
{
t = t0 + (i / 255.0f) * (t1 - t0);
for (k = 0; k < funcs; k++)
fz_eval_function(ctx, func[k], &t, 1, &shade->function[i][k], 1);
shade->function[i][k] = 1;
}
}
static void
pdf_sample_shade_function(fz_context *ctx, fz_shade *shade, int funcs, fz_function **func, float t0, float t1)
{
shade->use_function = 1;
if (funcs == 1)
pdf_sample_composite_shade_function(ctx, shade, func[0], t0, t1);
else
pdf_sample_component_shade_function(ctx, shade, funcs, func, t0, t1);
}
/* Type 1-3 -- Function-based, linear and radial shadings */
static void
pdf_load_function_based_shading(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, fz_function *func)
{
pdf_obj *obj;
float x0, y0, x1, y1;
float fv[2];
fz_matrix matrix;
int xx, yy;
float *p;
x0 = y0 = 0;
x1 = y1 = 1;
obj = pdf_dict_get(ctx, dict, PDF_NAME_Domain);
if (obj)
{
x0 = pdf_to_real(ctx, pdf_array_get(ctx, obj, 0));
x1 = pdf_to_real(ctx, pdf_array_get(ctx, obj, 1));
y0 = pdf_to_real(ctx, pdf_array_get(ctx, obj, 2));
y1 = pdf_to_real(ctx, pdf_array_get(ctx, obj, 3));
}
obj = pdf_dict_get(ctx, dict, PDF_NAME_Matrix);
if (obj)
pdf_to_matrix(ctx, obj, &matrix);
else
matrix = fz_identity;
shade->u.f.matrix = matrix;
shade->u.f.xdivs = FUNSEGS;
shade->u.f.ydivs = FUNSEGS;
shade->u.f.fn_vals = fz_malloc(ctx, (FUNSEGS+1)*(FUNSEGS+1)*shade->colorspace->n*sizeof(float));
shade->u.f.domain[0][0] = x0;
shade->u.f.domain[0][1] = y0;
shade->u.f.domain[1][0] = x1;
shade->u.f.domain[1][1] = y1;
p = shade->u.f.fn_vals;
for (yy = 0; yy <= FUNSEGS; yy++)
{
fv[1] = y0 + (y1 - y0) * yy / FUNSEGS;
for (xx = 0; xx <= FUNSEGS; xx++)
{
fv[0] = x0 + (x1 - x0) * xx / FUNSEGS;
fz_eval_function(ctx, func, fv, 2, p, shade->colorspace->n);
p += shade->colorspace->n;
}
}
}
static void
pdf_load_linear_shading(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, fz_function **func)
{
pdf_obj *obj;
float d0, d1;
int e0, e1;
obj = pdf_dict_get(ctx, dict, PDF_NAME_Coords);
shade->u.l_or_r.coords[0][0] = pdf_to_real(ctx, pdf_array_get(ctx, obj, 0));
shade->u.l_or_r.coords[0][1] = pdf_to_real(ctx, pdf_array_get(ctx, obj, 1));
shade->u.l_or_r.coords[1][0] = pdf_to_real(ctx, pdf_array_get(ctx, obj, 2));
shade->u.l_or_r.coords[1][1] = pdf_to_real(ctx, pdf_array_get(ctx, obj, 3));
d0 = 0;
d1 = 1;
obj = pdf_dict_get(ctx, dict, PDF_NAME_Domain);
if (obj)
{
d0 = pdf_to_real(ctx, pdf_array_get(ctx, obj, 0));
d1 = pdf_to_real(ctx, pdf_array_get(ctx, obj, 1));
}
e0 = e1 = 0;
obj = pdf_dict_get(ctx, dict, PDF_NAME_Extend);
if (obj)
{
e0 = pdf_to_bool(ctx, pdf_array_get(ctx, obj, 0));
e1 = pdf_to_bool(ctx, pdf_array_get(ctx, obj, 1));
}
pdf_sample_shade_function(ctx, shade, funcs, func, d0, d1);
shade->u.l_or_r.extend[0] = e0;
shade->u.l_or_r.extend[1] = e1;
}
static void
pdf_load_radial_shading(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, fz_function **func)
{
pdf_obj *obj;
float d0, d1;
int e0, e1;
obj = pdf_dict_get(ctx, dict, PDF_NAME_Coords);
shade->u.l_or_r.coords[0][0] = pdf_to_real(ctx, pdf_array_get(ctx, obj, 0));
shade->u.l_or_r.coords[0][1] = pdf_to_real(ctx, pdf_array_get(ctx, obj, 1));
shade->u.l_or_r.coords[0][2] = pdf_to_real(ctx, pdf_array_get(ctx, obj, 2));
shade->u.l_or_r.coords[1][0] = pdf_to_real(ctx, pdf_array_get(ctx, obj, 3));
shade->u.l_or_r.coords[1][1] = pdf_to_real(ctx, pdf_array_get(ctx, obj, 4));
shade->u.l_or_r.coords[1][2] = pdf_to_real(ctx, pdf_array_get(ctx, obj, 5));
d0 = 0;
d1 = 1;
obj = pdf_dict_get(ctx, dict, PDF_NAME_Domain);
if (obj)
{
d0 = pdf_to_real(ctx, pdf_array_get(ctx, obj, 0));
d1 = pdf_to_real(ctx, pdf_array_get(ctx, obj, 1));
}
e0 = e1 = 0;
obj = pdf_dict_get(ctx, dict, PDF_NAME_Extend);
if (obj)
{
e0 = pdf_to_bool(ctx, pdf_array_get(ctx, obj, 0));
e1 = pdf_to_bool(ctx, pdf_array_get(ctx, obj, 1));
}
pdf_sample_shade_function(ctx, shade, funcs, func, d0, d1);
shade->u.l_or_r.extend[0] = e0;
shade->u.l_or_r.extend[1] = e1;
}
/* Type 4-7 -- Triangle and patch mesh shadings */
struct mesh_params
{
int vprow;
int bpflag;
int bpcoord;
int bpcomp;
float x0, x1;
float y0, y1;
float c0[FZ_MAX_COLORS];
float c1[FZ_MAX_COLORS];
};
static void
pdf_load_mesh_params(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict)
{
pdf_obj *obj;
int i, n;
shade->u.m.x0 = shade->u.m.y0 = 0;
shade->u.m.x1 = shade->u.m.y1 = 1;
for (i = 0; i < FZ_MAX_COLORS; i++)
{
shade->u.m.c0[i] = 0;
shade->u.m.c1[i] = 1;
}
shade->u.m.vprow = pdf_to_int(ctx, pdf_dict_get(ctx, dict, PDF_NAME_VerticesPerRow));
shade->u.m.bpflag = pdf_to_int(ctx, pdf_dict_get(ctx, dict, PDF_NAME_BitsPerFlag));
shade->u.m.bpcoord = pdf_to_int(ctx, pdf_dict_get(ctx, dict, PDF_NAME_BitsPerCoordinate));
shade->u.m.bpcomp = pdf_to_int(ctx, pdf_dict_get(ctx, dict, PDF_NAME_BitsPerComponent));
obj = pdf_dict_get(ctx, dict, PDF_NAME_Decode);
if (pdf_array_len(ctx, obj) >= 6)
{
n = (pdf_array_len(ctx, obj) - 4) / 2;
shade->u.m.x0 = pdf_to_real(ctx, pdf_array_get(ctx, obj, 0));
shade->u.m.x1 = pdf_to_real(ctx, pdf_array_get(ctx, obj, 1));
shade->u.m.y0 = pdf_to_real(ctx, pdf_array_get(ctx, obj, 2));
shade->u.m.y1 = pdf_to_real(ctx, pdf_array_get(ctx, obj, 3));
for (i = 0; i < n; i++)
{
shade->u.m.c0[i] = pdf_to_real(ctx, pdf_array_get(ctx, obj, 4 + i * 2));
shade->u.m.c1[i] = pdf_to_real(ctx, pdf_array_get(ctx, obj, 5 + i * 2));
}
}
if (shade->u.m.vprow < 2 && shade->type == 5)
{
fz_warn(ctx, "Too few vertices per row (%d)", shade->u.m.vprow);
shade->u.m.vprow = 2;
}
if (shade->u.m.bpflag != 2 && shade->u.m.bpflag != 4 && shade->u.m.bpflag != 8 &&
shade->type != 5)
{
fz_warn(ctx, "Invalid number of bits per flag (%d)", shade->u.m.bpflag);
shade->u.m.bpflag = 8;
}
if (shade->u.m.bpcoord != 1 && shade->u.m.bpcoord != 2 && shade->u.m.bpcoord != 4 &&
shade->u.m.bpcoord != 8 && shade->u.m.bpcoord != 12 && shade->u.m.bpcoord != 16 &&
shade->u.m.bpcoord != 24 && shade->u.m.bpcoord != 32)
{
fz_warn(ctx, "Invalid number of bits per coordinate (%d)", shade->u.m.bpcoord);
shade->u.m.bpcoord = 8;
}
if (shade->u.m.bpcomp != 1 && shade->u.m.bpcomp != 2 && shade->u.m.bpcomp != 4 &&
shade->u.m.bpcomp != 8 && shade->u.m.bpcomp != 12 && shade->u.m.bpcomp != 16)
{
fz_warn(ctx, "Invalid number of bits per component (%d)", shade->u.m.bpcomp);
shade->u.m.bpcomp = 8;
}
}
static void
pdf_load_type4_shade(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, fz_function **func)
{
pdf_load_mesh_params(ctx, doc, shade, dict);
if (funcs > 0)
pdf_sample_shade_function(ctx, shade, funcs, func, shade->u.m.c0[0], shade->u.m.c1[0]);
shade->buffer = pdf_load_compressed_stream(ctx, doc, pdf_to_num(ctx, dict), pdf_to_gen(ctx, dict));
}
static void
pdf_load_type5_shade(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, fz_function **func)
{
pdf_load_mesh_params(ctx, doc, shade, dict);
if (funcs > 0)
pdf_sample_shade_function(ctx, shade, funcs, func, shade->u.m.c0[0], shade->u.m.c1[0]);
shade->buffer = pdf_load_compressed_stream(ctx, doc, pdf_to_num(ctx, dict), pdf_to_gen(ctx, dict));
}
/* Type 6 & 7 -- Patch mesh shadings */
static void
pdf_load_type6_shade(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, fz_function **func)
{
pdf_load_mesh_params(ctx, doc, shade, dict);
if (funcs > 0)
pdf_sample_shade_function(ctx, shade, funcs, func, shade->u.m.c0[0], shade->u.m.c1[0]);
shade->buffer = pdf_load_compressed_stream(ctx, doc, pdf_to_num(ctx, dict), pdf_to_gen(ctx, dict));
}
static void
pdf_load_type7_shade(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, fz_function **func)
{
pdf_load_mesh_params(ctx, doc, shade, dict);
if (funcs > 0)
pdf_sample_shade_function(ctx, shade, funcs, func, shade->u.m.c0[0], shade->u.m.c1[0]);
shade->buffer = pdf_load_compressed_stream(ctx, doc, pdf_to_num(ctx, dict), pdf_to_gen(ctx, dict));
}
/* Load all of the shading dictionary parameters, then switch on the shading type. */
static fz_shade *
pdf_load_shading_dict(fz_context *ctx, pdf_document *doc, pdf_obj *dict, const fz_matrix *transform)
{
fz_shade *shade = NULL;
fz_function *func[FZ_MAX_COLORS] = { NULL };
pdf_obj *obj;
int funcs = 0;
int type = 0;
int i, in, out;
fz_var(shade);
fz_var(func);
fz_var(funcs);
fz_var(type);
fz_try(ctx)
{
shade = fz_malloc_struct(ctx, fz_shade);
FZ_INIT_STORABLE(shade, 1, fz_drop_shade_imp);
shade->type = FZ_MESH_TYPE4;
shade->use_background = 0;
shade->use_function = 0;
shade->matrix = *transform;
shade->bbox = fz_infinite_rect;
shade->colorspace = NULL;
funcs = 0;
obj = pdf_dict_get(ctx, dict, PDF_NAME_ShadingType);
type = pdf_to_int(ctx, obj);
obj = pdf_dict_get(ctx, dict, PDF_NAME_ColorSpace);
if (!obj)
fz_throw(ctx, FZ_ERROR_GENERIC, "shading colorspace is missing");
shade->colorspace = pdf_load_colorspace(ctx, doc, obj);
obj = pdf_dict_get(ctx, dict, PDF_NAME_Background);
if (obj)
{
shade->use_background = 1;
for (i = 0; i < shade->colorspace->n; i++)
shade->background[i] = pdf_to_real(ctx, pdf_array_get(ctx, obj, i));
}
obj = pdf_dict_get(ctx, dict, PDF_NAME_BBox);
if (pdf_is_array(ctx, obj))
pdf_to_rect(ctx, obj, &shade->bbox);
obj = pdf_dict_get(ctx, dict, PDF_NAME_Function);
if (pdf_is_dict(ctx, obj))
{
funcs = 1;
if (type == 1)
in = 2;
else
in = 1;
out = shade->colorspace->n;
func[0] = pdf_load_function(ctx, doc, obj, in, out);
if (!func[0])
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot load shading function (%d %d R)", pdf_to_num(ctx, obj), pdf_to_gen(ctx, obj));
}
else if (pdf_is_array(ctx, obj))
{
funcs = pdf_array_len(ctx, obj);
if (funcs != 1 && funcs != shade->colorspace->n)
{
funcs = 0;
fz_throw(ctx, FZ_ERROR_GENERIC, "incorrect number of shading functions");
}
if (funcs > FZ_MAX_COLORS)
{
funcs = 0;
fz_throw(ctx, FZ_ERROR_GENERIC, "too many shading functions");
}
if (type == 1)
in = 2;
else
in = 1;
out = 1;
for (i = 0; i < funcs; i++)
{
func[i] = pdf_load_function(ctx, doc, pdf_array_get(ctx, obj, i), in, out);
if (!func[i])
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot load shading function (%d %d R)", pdf_to_num(ctx, obj), pdf_to_gen(ctx, obj));
}
}
else if (type < 4)
{
/* Functions are compulsory for types 1,2,3 */
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot load shading function (%d %d R)", pdf_to_num(ctx, obj), pdf_to_gen(ctx, obj));
}
shade->type = type;
switch (type)
{
case 1: pdf_load_function_based_shading(ctx, doc, shade, dict, func[0]); break;
case 2: pdf_load_linear_shading(ctx, doc, shade, dict, funcs, func); break;
case 3: pdf_load_radial_shading(ctx, doc, shade, dict, funcs, func); break;
case 4: pdf_load_type4_shade(ctx, doc, shade, dict, funcs, func); break;
case 5: pdf_load_type5_shade(ctx, doc, shade, dict, funcs, func); break;
case 6: pdf_load_type6_shade(ctx, doc, shade, dict, funcs, func); break;
case 7: pdf_load_type7_shade(ctx, doc, shade, dict, funcs, func); break;
default:
fz_throw(ctx, FZ_ERROR_GENERIC, "unknown shading type: %d", type);
}
}
fz_always(ctx)
{
for (i = 0; i < funcs; i++)
if (func[i])
fz_drop_function(ctx, func[i]);
}
fz_catch(ctx)
{
fz_drop_shade(ctx, shade);
fz_rethrow(ctx);
}
return shade;
}
static unsigned int
fz_shade_size(fz_shade *s)
{
if (s == NULL)
return 0;
if (s->type == FZ_FUNCTION_BASED)
return sizeof(*s) + sizeof(float) * s->u.f.xdivs * s->u.f.ydivs * s->colorspace->n;
return sizeof(*s) + fz_compressed_buffer_size(s->buffer);
}
fz_shade *
pdf_load_shading(fz_context *ctx, pdf_document *doc, pdf_obj *dict)
{
fz_matrix mat;
pdf_obj *obj;
fz_shade *shade;
if ((shade = pdf_find_item(ctx, fz_drop_shade_imp, dict)) != NULL)
{
return shade;
}
/* Type 2 pattern dictionary */
if (pdf_dict_get(ctx, dict, PDF_NAME_PatternType))
{
obj = pdf_dict_get(ctx, dict, PDF_NAME_Matrix);
if (obj)
pdf_to_matrix(ctx, obj, &mat);
else
mat = fz_identity;
obj = pdf_dict_get(ctx, dict, PDF_NAME_ExtGState);
if (obj)
{
if (pdf_dict_get(ctx, obj, PDF_NAME_CA) || pdf_dict_get(ctx, obj, PDF_NAME_ca))
{
fz_warn(ctx, "shading with alpha not supported");
}
}
obj = pdf_dict_get(ctx, dict, PDF_NAME_Shading);
if (!obj)
fz_throw(ctx, FZ_ERROR_GENERIC, "syntaxerror: missing shading dictionary");
shade = pdf_load_shading_dict(ctx, doc, obj, &mat);
}
/* Naked shading dictionary */
else
{
shade = pdf_load_shading_dict(ctx, doc, dict, &fz_identity);
}
pdf_store_item(ctx, dict, shade, fz_shade_size(shade));
return shade;
}
|