| 1 | #include "mupdf/fitz.h" |
| 2 | #include "mupdf/pdf.h" |
| 3 | |
| 4 | /* Sample various functions into lookup tables */ |
| 5 | |
| 6 | static void |
| 7 | pdf_sample_composite_shade_function(fz_context *ctx, fz_shade *shade, pdf_function *func, float t0, float t1) |
| 8 | { |
| 9 | int i, n; |
| 10 | float t; |
| 11 | |
| 12 | n = fz_colorspace_n(ctx, shade->colorspace); |
| 13 | for (i = 0; i < 256; i++) |
| 14 | { |
| 15 | t = t0 + (i / 255.0f) * (t1 - t0); |
| 16 | pdf_eval_function(ctx, func, &t, 1, shade->function[i], n); |
| 17 | shade->function[i][n] = 1; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | static void |
| 22 | pdf_sample_component_shade_function(fz_context *ctx, fz_shade *shade, int funcs, pdf_function **func, float t0, float t1) |
| 23 | { |
| 24 | int i, k; |
| 25 | float t; |
| 26 | |
| 27 | for (i = 0; i < 256; i++) |
| 28 | { |
| 29 | t = t0 + (i / 255.0f) * (t1 - t0); |
| 30 | for (k = 0; k < funcs; k++) |
| 31 | pdf_eval_function(ctx, func[k], &t, 1, &shade->function[i][k], 1); |
| 32 | shade->function[i][k] = 1; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | static void |
| 37 | pdf_sample_shade_function(fz_context *ctx, fz_shade *shade, int funcs, pdf_function **func, float t0, float t1) |
| 38 | { |
| 39 | shade->use_function = 1; |
| 40 | if (funcs == 1) |
| 41 | pdf_sample_composite_shade_function(ctx, shade, func[0], t0, t1); |
| 42 | else |
| 43 | pdf_sample_component_shade_function(ctx, shade, funcs, func, t0, t1); |
| 44 | } |
| 45 | |
| 46 | /* Type 1-3 -- Function-based, linear and radial shadings */ |
| 47 | |
| 48 | #define FUNSEGS 64 /* size of sampled mesh for function-based shadings */ |
| 49 | |
| 50 | static void |
| 51 | pdf_load_function_based_shading(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, pdf_function *func) |
| 52 | { |
| 53 | pdf_obj *obj; |
| 54 | float x0, y0, x1, y1; |
| 55 | float fv[2]; |
| 56 | int xx, yy; |
| 57 | float *p; |
| 58 | int n = fz_colorspace_n(ctx, shade->colorspace); |
| 59 | |
| 60 | x0 = y0 = 0; |
| 61 | x1 = y1 = 1; |
| 62 | obj = pdf_dict_get(ctx, dict, PDF_NAME(Domain)); |
| 63 | if (obj) |
| 64 | { |
| 65 | x0 = pdf_array_get_real(ctx, obj, 0); |
| 66 | x1 = pdf_array_get_real(ctx, obj, 1); |
| 67 | y0 = pdf_array_get_real(ctx, obj, 2); |
| 68 | y1 = pdf_array_get_real(ctx, obj, 3); |
| 69 | } |
| 70 | |
| 71 | shade->u.f.matrix = pdf_dict_get_matrix(ctx, dict, PDF_NAME(Matrix)); |
| 72 | shade->u.f.xdivs = FUNSEGS; |
| 73 | shade->u.f.ydivs = FUNSEGS; |
| 74 | shade->u.f.fn_vals = fz_malloc(ctx, (FUNSEGS+1)*(FUNSEGS+1)*n*sizeof(float)); |
| 75 | shade->u.f.domain[0][0] = x0; |
| 76 | shade->u.f.domain[0][1] = y0; |
| 77 | shade->u.f.domain[1][0] = x1; |
| 78 | shade->u.f.domain[1][1] = y1; |
| 79 | |
| 80 | p = shade->u.f.fn_vals; |
| 81 | for (yy = 0; yy <= FUNSEGS; yy++) |
| 82 | { |
| 83 | fv[1] = y0 + (y1 - y0) * yy / FUNSEGS; |
| 84 | |
| 85 | for (xx = 0; xx <= FUNSEGS; xx++) |
| 86 | { |
| 87 | fv[0] = x0 + (x1 - x0) * xx / FUNSEGS; |
| 88 | |
| 89 | pdf_eval_function(ctx, func, fv, 2, p, n); |
| 90 | p += n; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | pdf_load_linear_shading(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func) |
| 97 | { |
| 98 | pdf_obj *obj; |
| 99 | float d0, d1; |
| 100 | int e0, e1; |
| 101 | |
| 102 | obj = pdf_dict_get(ctx, dict, PDF_NAME(Coords)); |
| 103 | shade->u.l_or_r.coords[0][0] = pdf_array_get_real(ctx, obj, 0); |
| 104 | shade->u.l_or_r.coords[0][1] = pdf_array_get_real(ctx, obj, 1); |
| 105 | shade->u.l_or_r.coords[1][0] = pdf_array_get_real(ctx, obj, 2); |
| 106 | shade->u.l_or_r.coords[1][1] = pdf_array_get_real(ctx, obj, 3); |
| 107 | |
| 108 | d0 = 0; |
| 109 | d1 = 1; |
| 110 | obj = pdf_dict_get(ctx, dict, PDF_NAME(Domain)); |
| 111 | if (obj) |
| 112 | { |
| 113 | d0 = pdf_array_get_real(ctx, obj, 0); |
| 114 | d1 = pdf_array_get_real(ctx, obj, 1); |
| 115 | } |
| 116 | |
| 117 | e0 = e1 = 0; |
| 118 | obj = pdf_dict_get(ctx, dict, PDF_NAME(Extend)); |
| 119 | if (obj) |
| 120 | { |
| 121 | e0 = pdf_array_get_bool(ctx, obj, 0); |
| 122 | e1 = pdf_array_get_bool(ctx, obj, 1); |
| 123 | } |
| 124 | |
| 125 | pdf_sample_shade_function(ctx, shade, funcs, func, d0, d1); |
| 126 | |
| 127 | shade->u.l_or_r.extend[0] = e0; |
| 128 | shade->u.l_or_r.extend[1] = e1; |
| 129 | } |
| 130 | |
| 131 | static void |
| 132 | pdf_load_radial_shading(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func) |
| 133 | { |
| 134 | pdf_obj *obj; |
| 135 | float d0, d1; |
| 136 | int e0, e1; |
| 137 | |
| 138 | obj = pdf_dict_get(ctx, dict, PDF_NAME(Coords)); |
| 139 | shade->u.l_or_r.coords[0][0] = pdf_array_get_real(ctx, obj, 0); |
| 140 | shade->u.l_or_r.coords[0][1] = pdf_array_get_real(ctx, obj, 1); |
| 141 | shade->u.l_or_r.coords[0][2] = pdf_array_get_real(ctx, obj, 2); |
| 142 | shade->u.l_or_r.coords[1][0] = pdf_array_get_real(ctx, obj, 3); |
| 143 | shade->u.l_or_r.coords[1][1] = pdf_array_get_real(ctx, obj, 4); |
| 144 | shade->u.l_or_r.coords[1][2] = pdf_array_get_real(ctx, obj, 5); |
| 145 | |
| 146 | d0 = 0; |
| 147 | d1 = 1; |
| 148 | obj = pdf_dict_get(ctx, dict, PDF_NAME(Domain)); |
| 149 | if (obj) |
| 150 | { |
| 151 | d0 = pdf_array_get_real(ctx, obj, 0); |
| 152 | d1 = pdf_array_get_real(ctx, obj, 1); |
| 153 | } |
| 154 | |
| 155 | e0 = e1 = 0; |
| 156 | obj = pdf_dict_get(ctx, dict, PDF_NAME(Extend)); |
| 157 | if (obj) |
| 158 | { |
| 159 | e0 = pdf_array_get_bool(ctx, obj, 0); |
| 160 | e1 = pdf_array_get_bool(ctx, obj, 1); |
| 161 | } |
| 162 | |
| 163 | pdf_sample_shade_function(ctx, shade, funcs, func, d0, d1); |
| 164 | |
| 165 | shade->u.l_or_r.extend[0] = e0; |
| 166 | shade->u.l_or_r.extend[1] = e1; |
| 167 | } |
| 168 | |
| 169 | /* Type 4-7 -- Triangle and patch mesh shadings */ |
| 170 | |
| 171 | struct mesh_params |
| 172 | { |
| 173 | int vprow; |
| 174 | int bpflag; |
| 175 | int bpcoord; |
| 176 | int bpcomp; |
| 177 | float x0, x1; |
| 178 | float y0, y1; |
| 179 | float c0[FZ_MAX_COLORS]; |
| 180 | float c1[FZ_MAX_COLORS]; |
| 181 | }; |
| 182 | |
| 183 | static void |
| 184 | pdf_load_mesh_params(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict) |
| 185 | { |
| 186 | pdf_obj *obj; |
| 187 | int i, n; |
| 188 | |
| 189 | shade->u.m.x0 = shade->u.m.y0 = 0; |
| 190 | shade->u.m.x1 = shade->u.m.y1 = 1; |
| 191 | for (i = 0; i < FZ_MAX_COLORS; i++) |
| 192 | { |
| 193 | shade->u.m.c0[i] = 0; |
| 194 | shade->u.m.c1[i] = 1; |
| 195 | } |
| 196 | |
| 197 | shade->u.m.vprow = pdf_dict_get_int(ctx, dict, PDF_NAME(VerticesPerRow)); |
| 198 | shade->u.m.bpflag = pdf_dict_get_int(ctx, dict, PDF_NAME(BitsPerFlag)); |
| 199 | shade->u.m.bpcoord = pdf_dict_get_int(ctx, dict, PDF_NAME(BitsPerCoordinate)); |
| 200 | shade->u.m.bpcomp = pdf_dict_get_int(ctx, dict, PDF_NAME(BitsPerComponent)); |
| 201 | |
| 202 | obj = pdf_dict_get(ctx, dict, PDF_NAME(Decode)); |
| 203 | if (pdf_array_len(ctx, obj) >= 6) |
| 204 | { |
| 205 | n = fz_mini(FZ_MAX_COLORS, (pdf_array_len(ctx, obj) - 4) / 2); |
| 206 | shade->u.m.x0 = pdf_array_get_real(ctx, obj, 0); |
| 207 | shade->u.m.x1 = pdf_array_get_real(ctx, obj, 1); |
| 208 | shade->u.m.y0 = pdf_array_get_real(ctx, obj, 2); |
| 209 | shade->u.m.y1 = pdf_array_get_real(ctx, obj, 3); |
| 210 | for (i = 0; i < n; i++) |
| 211 | { |
| 212 | shade->u.m.c0[i] = pdf_array_get_real(ctx, obj, 4 + i * 2); |
| 213 | shade->u.m.c1[i] = pdf_array_get_real(ctx, obj, 5 + i * 2); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (shade->u.m.vprow < 2 && shade->type == 5) |
| 218 | { |
| 219 | fz_warn(ctx, "Too few vertices per row (%d)" , shade->u.m.vprow); |
| 220 | shade->u.m.vprow = 2; |
| 221 | } |
| 222 | |
| 223 | if (shade->u.m.bpflag != 2 && shade->u.m.bpflag != 4 && shade->u.m.bpflag != 8 && |
| 224 | shade->type != 5) |
| 225 | { |
| 226 | fz_warn(ctx, "Invalid number of bits per flag (%d)" , shade->u.m.bpflag); |
| 227 | shade->u.m.bpflag = 8; |
| 228 | } |
| 229 | |
| 230 | if (shade->u.m.bpcoord != 1 && shade->u.m.bpcoord != 2 && shade->u.m.bpcoord != 4 && |
| 231 | shade->u.m.bpcoord != 8 && shade->u.m.bpcoord != 12 && shade->u.m.bpcoord != 16 && |
| 232 | shade->u.m.bpcoord != 24 && shade->u.m.bpcoord != 32) |
| 233 | { |
| 234 | fz_warn(ctx, "Invalid number of bits per coordinate (%d)" , shade->u.m.bpcoord); |
| 235 | shade->u.m.bpcoord = 8; |
| 236 | } |
| 237 | |
| 238 | if (shade->u.m.bpcomp != 1 && shade->u.m.bpcomp != 2 && shade->u.m.bpcomp != 4 && |
| 239 | shade->u.m.bpcomp != 8 && shade->u.m.bpcomp != 12 && shade->u.m.bpcomp != 16) |
| 240 | { |
| 241 | fz_warn(ctx, "Invalid number of bits per component (%d)" , shade->u.m.bpcomp); |
| 242 | shade->u.m.bpcomp = 8; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | pdf_load_type4_shade(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func) |
| 248 | { |
| 249 | pdf_load_mesh_params(ctx, doc, shade, dict); |
| 250 | |
| 251 | if (funcs > 0) |
| 252 | pdf_sample_shade_function(ctx, shade, funcs, func, shade->u.m.c0[0], shade->u.m.c1[0]); |
| 253 | |
| 254 | shade->buffer = pdf_load_compressed_stream(ctx, doc, pdf_to_num(ctx, dict)); |
| 255 | } |
| 256 | |
| 257 | static void |
| 258 | pdf_load_type5_shade(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func) |
| 259 | { |
| 260 | pdf_load_mesh_params(ctx, doc, shade, dict); |
| 261 | |
| 262 | if (funcs > 0) |
| 263 | pdf_sample_shade_function(ctx, shade, funcs, func, shade->u.m.c0[0], shade->u.m.c1[0]); |
| 264 | |
| 265 | shade->buffer = pdf_load_compressed_stream(ctx, doc, pdf_to_num(ctx, dict)); |
| 266 | } |
| 267 | |
| 268 | /* Type 6 & 7 -- Patch mesh shadings */ |
| 269 | |
| 270 | static void |
| 271 | pdf_load_type6_shade(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func) |
| 272 | { |
| 273 | pdf_load_mesh_params(ctx, doc, shade, dict); |
| 274 | |
| 275 | if (funcs > 0) |
| 276 | pdf_sample_shade_function(ctx, shade, funcs, func, shade->u.m.c0[0], shade->u.m.c1[0]); |
| 277 | |
| 278 | shade->buffer = pdf_load_compressed_stream(ctx, doc, pdf_to_num(ctx, dict)); |
| 279 | } |
| 280 | |
| 281 | static void |
| 282 | pdf_load_type7_shade(fz_context *ctx, pdf_document *doc, fz_shade *shade, pdf_obj *dict, int funcs, pdf_function **func) |
| 283 | { |
| 284 | pdf_load_mesh_params(ctx, doc, shade, dict); |
| 285 | |
| 286 | if (funcs > 0) |
| 287 | pdf_sample_shade_function(ctx, shade, funcs, func, shade->u.m.c0[0], shade->u.m.c1[0]); |
| 288 | |
| 289 | shade->buffer = pdf_load_compressed_stream(ctx, doc, pdf_to_num(ctx, dict)); |
| 290 | } |
| 291 | |
| 292 | /* Load all of the shading dictionary parameters, then switch on the shading type. */ |
| 293 | |
| 294 | static fz_shade * |
| 295 | pdf_load_shading_dict(fz_context *ctx, pdf_document *doc, pdf_obj *dict, fz_matrix transform) |
| 296 | { |
| 297 | fz_shade *shade = NULL; |
| 298 | pdf_function *func[FZ_MAX_COLORS] = { NULL }; |
| 299 | pdf_obj *obj; |
| 300 | int funcs = 0; |
| 301 | int type = 0; |
| 302 | int i, in, out, n; |
| 303 | |
| 304 | fz_var(shade); |
| 305 | fz_var(func); |
| 306 | fz_var(funcs); |
| 307 | fz_var(type); |
| 308 | |
| 309 | fz_try(ctx) |
| 310 | { |
| 311 | shade = fz_malloc_struct(ctx, fz_shade); |
| 312 | FZ_INIT_STORABLE(shade, 1, fz_drop_shade_imp); |
| 313 | shade->type = FZ_MESH_TYPE4; |
| 314 | shade->use_background = 0; |
| 315 | shade->use_function = 0; |
| 316 | shade->matrix = transform; |
| 317 | shade->bbox = fz_infinite_rect; |
| 318 | |
| 319 | shade->colorspace = NULL; |
| 320 | |
| 321 | funcs = 0; |
| 322 | |
| 323 | obj = pdf_dict_get(ctx, dict, PDF_NAME(ShadingType)); |
| 324 | type = pdf_to_int(ctx, obj); |
| 325 | |
| 326 | obj = pdf_dict_get(ctx, dict, PDF_NAME(ColorSpace)); |
| 327 | if (!obj) |
| 328 | fz_throw(ctx, FZ_ERROR_SYNTAX, "shading colorspace is missing" ); |
| 329 | shade->colorspace = pdf_load_colorspace(ctx, obj); |
| 330 | n = fz_colorspace_n(ctx, shade->colorspace); |
| 331 | |
| 332 | obj = pdf_dict_get(ctx, dict, PDF_NAME(Background)); |
| 333 | if (obj) |
| 334 | { |
| 335 | shade->use_background = 1; |
| 336 | for (i = 0; i < n; i++) |
| 337 | shade->background[i] = pdf_array_get_real(ctx, obj, i); |
| 338 | } |
| 339 | |
| 340 | obj = pdf_dict_get(ctx, dict, PDF_NAME(BBox)); |
| 341 | if (pdf_is_array(ctx, obj)) |
| 342 | shade->bbox = pdf_to_rect(ctx, obj); |
| 343 | |
| 344 | obj = pdf_dict_get(ctx, dict, PDF_NAME(Function)); |
| 345 | if (pdf_is_dict(ctx, obj)) |
| 346 | { |
| 347 | funcs = 1; |
| 348 | |
| 349 | if (type == 1) |
| 350 | in = 2; |
| 351 | else |
| 352 | in = 1; |
| 353 | out = n; |
| 354 | |
| 355 | func[0] = pdf_load_function(ctx, obj, in, out); |
| 356 | if (!func[0]) |
| 357 | fz_throw(ctx, FZ_ERROR_SYNTAX, "cannot load shading function (%d 0 R)" , pdf_to_num(ctx, obj)); |
| 358 | } |
| 359 | else if (pdf_is_array(ctx, obj)) |
| 360 | { |
| 361 | funcs = pdf_array_len(ctx, obj); |
| 362 | if (funcs != 1 && funcs != n) |
| 363 | { |
| 364 | funcs = 0; |
| 365 | fz_throw(ctx, FZ_ERROR_SYNTAX, "incorrect number of shading functions" ); |
| 366 | } |
| 367 | if (funcs > FZ_MAX_COLORS) |
| 368 | { |
| 369 | funcs = 0; |
| 370 | fz_throw(ctx, FZ_ERROR_SYNTAX, "too many shading functions" ); |
| 371 | } |
| 372 | |
| 373 | if (type == 1) |
| 374 | in = 2; |
| 375 | else |
| 376 | in = 1; |
| 377 | out = 1; |
| 378 | |
| 379 | for (i = 0; i < funcs; i++) |
| 380 | { |
| 381 | func[i] = pdf_load_function(ctx, pdf_array_get(ctx, obj, i), in, out); |
| 382 | if (!func[i]) |
| 383 | fz_throw(ctx, FZ_ERROR_SYNTAX, "cannot load shading function (%d 0 R)" , pdf_to_num(ctx, obj)); |
| 384 | } |
| 385 | } |
| 386 | else if (type < 4) |
| 387 | { |
| 388 | /* Functions are compulsory for types 1,2,3 */ |
| 389 | fz_throw(ctx, FZ_ERROR_SYNTAX, "cannot load shading function (%d 0 R)" , pdf_to_num(ctx, obj)); |
| 390 | } |
| 391 | |
| 392 | shade->type = type; |
| 393 | switch (type) |
| 394 | { |
| 395 | case 1: pdf_load_function_based_shading(ctx, doc, shade, dict, func[0]); break; |
| 396 | case 2: pdf_load_linear_shading(ctx, doc, shade, dict, funcs, func); break; |
| 397 | case 3: pdf_load_radial_shading(ctx, doc, shade, dict, funcs, func); break; |
| 398 | case 4: pdf_load_type4_shade(ctx, doc, shade, dict, funcs, func); break; |
| 399 | case 5: pdf_load_type5_shade(ctx, doc, shade, dict, funcs, func); break; |
| 400 | case 6: pdf_load_type6_shade(ctx, doc, shade, dict, funcs, func); break; |
| 401 | case 7: pdf_load_type7_shade(ctx, doc, shade, dict, funcs, func); break; |
| 402 | default: |
| 403 | fz_throw(ctx, FZ_ERROR_SYNTAX, "unknown shading type: %d" , type); |
| 404 | } |
| 405 | } |
| 406 | fz_always(ctx) |
| 407 | { |
| 408 | for (i = 0; i < funcs; i++) |
| 409 | pdf_drop_function(ctx, func[i]); |
| 410 | } |
| 411 | fz_catch(ctx) |
| 412 | { |
| 413 | fz_drop_shade(ctx, shade); |
| 414 | fz_rethrow(ctx); |
| 415 | } |
| 416 | return shade; |
| 417 | } |
| 418 | |
| 419 | static size_t |
| 420 | fz_shade_size(fz_context *ctx, fz_shade *s) |
| 421 | { |
| 422 | if (s == NULL) |
| 423 | return 0; |
| 424 | if (s->type == FZ_FUNCTION_BASED) |
| 425 | return sizeof(*s) + sizeof(float) * s->u.f.xdivs * s->u.f.ydivs * fz_colorspace_n(ctx, s->colorspace); |
| 426 | return sizeof(*s) + fz_compressed_buffer_size(s->buffer); |
| 427 | } |
| 428 | |
| 429 | fz_shade * |
| 430 | pdf_load_shading(fz_context *ctx, pdf_document *doc, pdf_obj *dict) |
| 431 | { |
| 432 | fz_matrix mat; |
| 433 | pdf_obj *obj; |
| 434 | fz_shade *shade; |
| 435 | |
| 436 | if ((shade = pdf_find_item(ctx, fz_drop_shade_imp, dict)) != NULL) |
| 437 | { |
| 438 | return shade; |
| 439 | } |
| 440 | |
| 441 | /* Type 2 pattern dictionary */ |
| 442 | if (pdf_dict_get(ctx, dict, PDF_NAME(PatternType))) |
| 443 | { |
| 444 | mat = pdf_dict_get_matrix(ctx, dict, PDF_NAME(Matrix)); |
| 445 | |
| 446 | obj = pdf_dict_get(ctx, dict, PDF_NAME(ExtGState)); |
| 447 | if (obj) |
| 448 | { |
| 449 | if (pdf_dict_get(ctx, obj, PDF_NAME(CA)) || pdf_dict_get(ctx, obj, PDF_NAME(ca))) |
| 450 | { |
| 451 | fz_warn(ctx, "shading with alpha not supported" ); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | obj = pdf_dict_get(ctx, dict, PDF_NAME(Shading)); |
| 456 | if (!obj) |
| 457 | fz_throw(ctx, FZ_ERROR_SYNTAX, "missing shading dictionary" ); |
| 458 | |
| 459 | shade = pdf_load_shading_dict(ctx, doc, obj, mat); |
| 460 | } |
| 461 | |
| 462 | /* Naked shading dictionary */ |
| 463 | else |
| 464 | { |
| 465 | shade = pdf_load_shading_dict(ctx, doc, dict, fz_identity); |
| 466 | } |
| 467 | |
| 468 | pdf_store_item(ctx, dict, shade, fz_shade_size(ctx, shade)); |
| 469 | |
| 470 | return shade; |
| 471 | } |
| 472 | |