comparison mupdf-source/source/pdf/pdf-shade.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright (C) 2004-2021 Artifex Software, Inc.
2 //
3 // This file is part of MuPDF.
4 //
5 // MuPDF is free software: you can redistribute it and/or modify it under the
6 // terms of the GNU Affero General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or (at your option)
8 // any later version.
9 //
10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Affero General Public License
16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17 //
18 // Alternative licensing terms are available from the licensor.
19 // For commercial licensing, see <https://www.artifex.com/> or contact
20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21 // CA 94129, USA, for further information.
22
23 #include "mupdf/fitz.h"
24 #include "mupdf/pdf.h"
25
26 /* Sample various functions into lookup tables */
27
28 static void
29 pdf_sample_composite_shade_function(fz_context *ctx, float *shade, int n, pdf_function *func, float t0, float t1)
30 {
31 int i;
32 float t;
33
34 for (i = 0; i < 256; i++)
35 {
36 t = t0 + (i / 255.0f) * (t1 - t0);
37 pdf_eval_function(ctx, func, &t, 1, shade, n);
38 shade += n;
39 *shade++ = 1;
40 }
41 }
42
43 static void
44 pdf_sample_component_shade_function(fz_context *ctx, float *shade, int funcs, pdf_function **func, float t0, float t1)
45 {
46 int i, k;
47 float t;
48
49 for (i = 0; i < 256; i++)
50 {
51 t = t0 + (i / 255.0f) * (t1 - t0);
52 for (k = 0; k < funcs; k++)
53 pdf_eval_function(ctx, func[k], &t, 1, shade++, 1);
54 *shade++ = 1;
55 }
56 }
57
58 void
59 pdf_sample_shade_function(fz_context *ctx, float *samples, int n, int funcs, pdf_function **func, float t0, float t1)
60 {
61 if (funcs == 1)
62 pdf_sample_composite_shade_function(ctx, samples, n, func[0], t0, t1);
63 else
64 pdf_sample_component_shade_function(ctx, samples, funcs, func, t0, t1);
65 }
66
67 static void
68 make_sampled_shade_function(fz_context *ctx, fz_shade *shade, int funcs, pdf_function **func, float t0, float t1)
69 {
70 int n = shade->colorspace->n;
71 if (funcs != 1)
72 n = funcs;
73 shade->function_stride = n + 1;
74 shade->function = Memento_label(fz_malloc(ctx, sizeof(float) * 256 * shade->function_stride), "shade samples");
75
76 pdf_sample_shade_function(ctx, shade->function, n, funcs, func, t0, t1);
77 }
78
79 /* Type 1-3 -- Function-based, linear and radial shadings */
80
81 #define FUNSEGS 64 /* size of sampled mesh for function-based shadings */
82
83 static void
84 pdf_load_function_based_shading(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func)
85 {
86 pdf_obj *obj;
87 float x0, y0, x1, y1;
88 float fv[2];
89 int xx, yy, zz;
90 float *p;
91 int n = fz_colorspace_n(ctx, shade->colorspace);
92
93 x0 = y0 = 0;
94 x1 = y1 = 1;
95 obj = pdf_dict_get(ctx, dict, PDF_NAME(Domain));
96 if (obj)
97 {
98 x0 = pdf_array_get_real(ctx, obj, 0);
99 x1 = pdf_array_get_real(ctx, obj, 1);
100 y0 = pdf_array_get_real(ctx, obj, 2);
101 y1 = pdf_array_get_real(ctx, obj, 3);
102 }
103
104 shade->u.f.matrix = pdf_dict_get_matrix(ctx, dict, PDF_NAME(Matrix));
105 shade->u.f.xdivs = FUNSEGS;
106 shade->u.f.ydivs = FUNSEGS;
107 shade->u.f.fn_vals = Memento_label(fz_malloc(ctx, (FUNSEGS+1)*(FUNSEGS+1)*n*sizeof(float)), "shade_fn_vals");
108 shade->u.f.domain[0][0] = x0;
109 shade->u.f.domain[0][1] = y0;
110 shade->u.f.domain[1][0] = x1;
111 shade->u.f.domain[1][1] = y1;
112
113 p = shade->u.f.fn_vals;
114 if (funcs == 1)
115 {
116 for (yy = 0; yy <= FUNSEGS; yy++)
117 {
118 fv[1] = y0 + (y1 - y0) * yy / FUNSEGS;
119
120 for (xx = 0; xx <= FUNSEGS; xx++)
121 {
122 fv[0] = x0 + (x1 - x0) * xx / FUNSEGS;
123
124 pdf_eval_function(ctx, func[0], fv, 2, p, n);
125 p += n;
126 }
127 }
128 }
129 else
130 {
131 if (funcs != n)
132 fz_throw(ctx, FZ_ERROR_SYNTAX, "Expected 1 2in, n-out function, or n 2 in, 1-out functions");
133
134 for (yy = 0; yy <= FUNSEGS; yy++)
135 {
136 fv[1] = y0 + (y1 - y0) * yy / FUNSEGS;
137
138 for (xx = 0; xx <= FUNSEGS; xx++)
139 {
140 fv[0] = x0 + (x1 - x0) * xx / FUNSEGS;
141
142 for (zz = 0; zz < n; zz++)
143 {
144 pdf_eval_function(ctx, func[zz], fv, 2, p, 1);
145 p ++;
146 }
147 }
148 }
149 }
150 }
151
152 static void
153 pdf_load_linear_shading(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func)
154 {
155 pdf_obj *obj;
156 float d0, d1;
157 int e0, e1;
158
159 obj = pdf_dict_get(ctx, dict, PDF_NAME(Coords));
160 shade->u.l_or_r.coords[0][0] = pdf_array_get_real(ctx, obj, 0);
161 shade->u.l_or_r.coords[0][1] = pdf_array_get_real(ctx, obj, 1);
162 shade->u.l_or_r.coords[1][0] = pdf_array_get_real(ctx, obj, 2);
163 shade->u.l_or_r.coords[1][1] = pdf_array_get_real(ctx, obj, 3);
164
165 d0 = 0;
166 d1 = 1;
167 obj = pdf_dict_get(ctx, dict, PDF_NAME(Domain));
168 if (obj)
169 {
170 d0 = pdf_array_get_real(ctx, obj, 0);
171 d1 = pdf_array_get_real(ctx, obj, 1);
172 }
173
174 e0 = e1 = 0;
175 obj = pdf_dict_get(ctx, dict, PDF_NAME(Extend));
176 if (obj)
177 {
178 e0 = pdf_array_get_bool(ctx, obj, 0);
179 e1 = pdf_array_get_bool(ctx, obj, 1);
180 }
181
182 make_sampled_shade_function(ctx, shade, funcs, func, d0, d1);
183
184 shade->u.l_or_r.extend[0] = e0;
185 shade->u.l_or_r.extend[1] = e1;
186 }
187
188 static void
189 pdf_load_radial_shading(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func)
190 {
191 pdf_obj *obj;
192 float d0, d1;
193 int e0, e1;
194
195 obj = pdf_dict_get(ctx, dict, PDF_NAME(Coords));
196 shade->u.l_or_r.coords[0][0] = pdf_array_get_real(ctx, obj, 0);
197 shade->u.l_or_r.coords[0][1] = pdf_array_get_real(ctx, obj, 1);
198 shade->u.l_or_r.coords[0][2] = pdf_array_get_real(ctx, obj, 2);
199 shade->u.l_or_r.coords[1][0] = pdf_array_get_real(ctx, obj, 3);
200 shade->u.l_or_r.coords[1][1] = pdf_array_get_real(ctx, obj, 4);
201 shade->u.l_or_r.coords[1][2] = pdf_array_get_real(ctx, obj, 5);
202
203 d0 = 0;
204 d1 = 1;
205 obj = pdf_dict_get(ctx, dict, PDF_NAME(Domain));
206 if (obj)
207 {
208 d0 = pdf_array_get_real(ctx, obj, 0);
209 d1 = pdf_array_get_real(ctx, obj, 1);
210 }
211
212 e0 = e1 = 0;
213 obj = pdf_dict_get(ctx, dict, PDF_NAME(Extend));
214 if (obj)
215 {
216 e0 = pdf_array_get_bool(ctx, obj, 0);
217 e1 = pdf_array_get_bool(ctx, obj, 1);
218 }
219
220 make_sampled_shade_function(ctx, shade, funcs, func, d0, d1);
221
222 shade->u.l_or_r.extend[0] = e0;
223 shade->u.l_or_r.extend[1] = e1;
224 }
225
226 /* Type 4-7 -- Triangle and patch mesh shadings */
227
228 struct mesh_params
229 {
230 int vprow;
231 int bpflag;
232 int bpcoord;
233 int bpcomp;
234 float x0, x1;
235 float y0, y1;
236 float c0[FZ_MAX_COLORS];
237 float c1[FZ_MAX_COLORS];
238 };
239
240 static void
241 pdf_load_mesh_params(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict)
242 {
243 pdf_obj *obj;
244 int i, n;
245
246 shade->u.m.x0 = shade->u.m.y0 = 0;
247 shade->u.m.x1 = shade->u.m.y1 = 1;
248 for (i = 0; i < FZ_MAX_COLORS; i++)
249 {
250 shade->u.m.c0[i] = 0;
251 shade->u.m.c1[i] = 1;
252 }
253
254 shade->u.m.vprow = pdf_dict_get_int(ctx, dict, PDF_NAME(VerticesPerRow));
255 shade->u.m.bpflag = pdf_dict_get_int(ctx, dict, PDF_NAME(BitsPerFlag));
256 shade->u.m.bpcoord = pdf_dict_get_int(ctx, dict, PDF_NAME(BitsPerCoordinate));
257 shade->u.m.bpcomp = pdf_dict_get_int(ctx, dict, PDF_NAME(BitsPerComponent));
258
259 obj = pdf_dict_get(ctx, dict, PDF_NAME(Decode));
260 if (pdf_array_len(ctx, obj) >= 6)
261 {
262 n = fz_mini(FZ_MAX_COLORS, (pdf_array_len(ctx, obj) - 4) / 2);
263 shade->u.m.x0 = pdf_array_get_real(ctx, obj, 0);
264 shade->u.m.x1 = pdf_array_get_real(ctx, obj, 1);
265 shade->u.m.y0 = pdf_array_get_real(ctx, obj, 2);
266 shade->u.m.y1 = pdf_array_get_real(ctx, obj, 3);
267 for (i = 0; i < n; i++)
268 {
269 shade->u.m.c0[i] = pdf_array_get_real(ctx, obj, 4 + i * 2);
270 shade->u.m.c1[i] = pdf_array_get_real(ctx, obj, 5 + i * 2);
271 }
272 }
273
274 if (shade->u.m.vprow < 2 && shade->type == 5)
275 {
276 fz_warn(ctx, "Too few vertices per row (%d)", shade->u.m.vprow);
277 shade->u.m.vprow = 2;
278 }
279
280 if (shade->u.m.bpflag != 2 && shade->u.m.bpflag != 4 && shade->u.m.bpflag != 8 &&
281 shade->type != 5)
282 {
283 fz_warn(ctx, "Invalid number of bits per flag (%d)", shade->u.m.bpflag);
284 shade->u.m.bpflag = 8;
285 }
286
287 if (shade->u.m.bpcoord != 1 && shade->u.m.bpcoord != 2 && shade->u.m.bpcoord != 4 &&
288 shade->u.m.bpcoord != 8 && shade->u.m.bpcoord != 12 && shade->u.m.bpcoord != 16 &&
289 shade->u.m.bpcoord != 24 && shade->u.m.bpcoord != 32)
290 {
291 fz_warn(ctx, "Invalid number of bits per coordinate (%d)", shade->u.m.bpcoord);
292 shade->u.m.bpcoord = 8;
293 }
294
295 if (shade->u.m.bpcomp != 1 && shade->u.m.bpcomp != 2 && shade->u.m.bpcomp != 4 &&
296 shade->u.m.bpcomp != 8 && shade->u.m.bpcomp != 12 && shade->u.m.bpcomp != 16)
297 {
298 fz_warn(ctx, "Invalid number of bits per component (%d)", shade->u.m.bpcomp);
299 shade->u.m.bpcomp = 8;
300 }
301 }
302
303 static void
304 pdf_load_type4_shade(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func)
305 {
306 pdf_load_mesh_params(ctx, doc, shade, dict);
307
308 if (funcs > 0)
309 make_sampled_shade_function(ctx, shade, funcs, func, shade->u.m.c0[0], shade->u.m.c1[0]);
310
311 shade->buffer = pdf_load_compressed_stream(ctx, doc, pdf_to_num(ctx, dict), 0);
312 }
313
314 static void
315 pdf_load_type5_shade(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func)
316 {
317 pdf_load_mesh_params(ctx, doc, shade, dict);
318
319 if (funcs > 0)
320 make_sampled_shade_function(ctx, shade, funcs, func, shade->u.m.c0[0], shade->u.m.c1[0]);
321
322 shade->buffer = pdf_load_compressed_stream(ctx, doc, pdf_to_num(ctx, dict), 0);
323 }
324
325 /* Type 6 & 7 -- Patch mesh shadings */
326
327 static void
328 pdf_load_type6_shade(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func)
329 {
330 pdf_load_mesh_params(ctx, doc, shade, dict);
331
332 if (funcs > 0)
333 make_sampled_shade_function(ctx, shade, funcs, func, shade->u.m.c0[0], shade->u.m.c1[0]);
334
335 shade->buffer = pdf_load_compressed_stream(ctx, doc, pdf_to_num(ctx, dict), 0);
336 }
337
338 static void
339 pdf_load_type7_shade(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func)
340 {
341 pdf_load_mesh_params(ctx, doc, shade, dict);
342
343 if (funcs > 0)
344 make_sampled_shade_function(ctx, shade, funcs, func, shade->u.m.c0[0], shade->u.m.c1[0]);
345
346 shade->buffer = pdf_load_compressed_stream(ctx, doc, pdf_to_num(ctx, dict), 0);
347 }
348
349 /* Load all of the shading dictionary parameters, then switch on the shading type. */
350
351 static fz_shade *
352 pdf_load_shading_dict(fz_context *ctx, pdf_document *doc, pdf_obj *dict, fz_matrix transform)
353 {
354 fz_shade *shade = NULL;
355 pdf_function *func[FZ_MAX_COLORS] = { NULL };
356 pdf_obj *obj;
357 int funcs = 0;
358 int type = 0;
359 int i, in, out, n;
360
361 fz_var(shade);
362 fz_var(func);
363 fz_var(funcs);
364 fz_var(type);
365
366 fz_try(ctx)
367 {
368 shade = fz_malloc_struct(ctx, fz_shade);
369 FZ_INIT_STORABLE(shade, 1, fz_drop_shade_imp);
370 shade->type = FZ_MESH_TYPE4;
371 shade->use_background = 0;
372 shade->function_stride = 0;
373 shade->matrix = transform;
374 shade->bbox = fz_infinite_rect;
375
376 shade->colorspace = NULL;
377
378 funcs = 0;
379
380 obj = pdf_dict_get(ctx, dict, PDF_NAME(ShadingType));
381 type = pdf_to_int(ctx, obj);
382
383 obj = pdf_dict_get(ctx, dict, PDF_NAME(ColorSpace));
384 if (!obj)
385 fz_throw(ctx, FZ_ERROR_SYNTAX, "shading colorspace is missing");
386 shade->colorspace = pdf_load_colorspace(ctx, obj);
387 n = fz_colorspace_n(ctx, shade->colorspace);
388
389 obj = pdf_dict_get(ctx, dict, PDF_NAME(Background));
390 if (obj)
391 {
392 shade->use_background = 1;
393 for (i = 0; i < n; i++)
394 shade->background[i] = pdf_array_get_real(ctx, obj, i);
395 }
396
397 obj = pdf_dict_get(ctx, dict, PDF_NAME(BBox));
398 if (pdf_is_array(ctx, obj))
399 shade->bbox = pdf_to_rect(ctx, obj);
400
401 obj = pdf_dict_get(ctx, dict, PDF_NAME(Function));
402 if (pdf_is_dict(ctx, obj))
403 {
404 funcs = 1;
405
406 if (type == 1)
407 in = 2;
408 else
409 in = 1;
410 out = n;
411
412 func[0] = pdf_load_function(ctx, obj, in, out);
413 if (!func[0])
414 fz_throw(ctx, FZ_ERROR_SYNTAX, "cannot load shading function (%d 0 R)", pdf_to_num(ctx, obj));
415 }
416 else if (pdf_is_array(ctx, obj))
417 {
418 funcs = pdf_array_len(ctx, obj);
419 if (funcs != 1 && funcs != n)
420 {
421 funcs = 0;
422 fz_throw(ctx, FZ_ERROR_SYNTAX, "incorrect number of shading functions");
423 }
424 if (funcs > FZ_MAX_COLORS)
425 {
426 funcs = 0;
427 fz_throw(ctx, FZ_ERROR_SYNTAX, "too many shading functions");
428 }
429
430 if (type == 1)
431 in = 2;
432 else
433 in = 1;
434 out = 1;
435
436 for (i = 0; i < funcs; i++)
437 {
438 func[i] = pdf_load_function(ctx, pdf_array_get(ctx, obj, i), in, out);
439 if (!func[i])
440 fz_throw(ctx, FZ_ERROR_SYNTAX, "cannot load shading function (%d 0 R)", pdf_to_num(ctx, obj));
441 }
442 }
443 else if (type < 4)
444 {
445 /* Functions are compulsory for types 1,2,3 */
446 fz_throw(ctx, FZ_ERROR_SYNTAX, "cannot load shading function (%d 0 R)", pdf_to_num(ctx, obj));
447 }
448
449 shade->type = type;
450 switch (type)
451 {
452 case 1: pdf_load_function_based_shading(ctx, doc, shade, dict, funcs, func); break;
453 case 2: pdf_load_linear_shading(ctx, doc, shade, dict, funcs, func); break;
454 case 3: pdf_load_radial_shading(ctx, doc, shade, dict, funcs, func); break;
455 case 4: pdf_load_type4_shade(ctx, doc, shade, dict, funcs, func); break;
456 case 5: pdf_load_type5_shade(ctx, doc, shade, dict, funcs, func); break;
457 case 6: pdf_load_type6_shade(ctx, doc, shade, dict, funcs, func); break;
458 case 7: pdf_load_type7_shade(ctx, doc, shade, dict, funcs, func); break;
459 default:
460 fz_throw(ctx, FZ_ERROR_SYNTAX, "unknown shading type: %d", type);
461 }
462 }
463 fz_always(ctx)
464 {
465 for (i = 0; i < funcs; i++)
466 pdf_drop_function(ctx, func[i]);
467 }
468 fz_catch(ctx)
469 {
470 fz_drop_shade(ctx, shade);
471 fz_rethrow(ctx);
472 }
473 return shade;
474 }
475
476 static size_t
477 fz_shade_size(fz_context *ctx, fz_shade *s)
478 {
479 size_t f = 0;
480 if (s == NULL)
481 return 0;
482 if (s->function_stride)
483 f = sizeof(float) * s->function_stride * 256;
484 if (s->type == FZ_FUNCTION_BASED)
485 return sizeof(*s) + sizeof(float) * s->u.f.xdivs * s->u.f.ydivs * fz_colorspace_n(ctx, s->colorspace) + f;
486 return sizeof(*s) + fz_compressed_buffer_size(s->buffer) + f;
487 }
488
489 fz_shade *
490 pdf_load_shading(fz_context *ctx, pdf_document *doc, pdf_obj *dict)
491 {
492 fz_matrix mat;
493 pdf_obj *obj;
494 fz_shade *shade;
495
496 if ((shade = pdf_find_item(ctx, fz_drop_shade_imp, dict)) != NULL)
497 {
498 return shade;
499 }
500
501 /* Type 2 pattern dictionary */
502 if (pdf_dict_get(ctx, dict, PDF_NAME(PatternType)))
503 {
504 mat = pdf_dict_get_matrix(ctx, dict, PDF_NAME(Matrix));
505
506 obj = pdf_dict_get(ctx, dict, PDF_NAME(ExtGState));
507 if (obj)
508 {
509 if (pdf_dict_get(ctx, obj, PDF_NAME(CA)) || pdf_dict_get(ctx, obj, PDF_NAME(ca)))
510 {
511 fz_warn(ctx, "shading with alpha not supported");
512 }
513 }
514
515 obj = pdf_dict_get(ctx, dict, PDF_NAME(Shading));
516 if (!obj)
517 fz_throw(ctx, FZ_ERROR_SYNTAX, "missing shading dictionary");
518
519 shade = pdf_load_shading_dict(ctx, doc, obj, mat);
520 }
521
522 /* Naked shading dictionary */
523 else
524 {
525 shade = pdf_load_shading_dict(ctx, doc, dict, fz_identity);
526 }
527
528 pdf_store_item(ctx, dict, shade, fz_shade_size(ctx, shade));
529
530 return shade;
531 }