Debian Codegraph @

texk/dvipdfm-x/pngimage.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 /*
29 * PNG SUPPORT
30 *
31 * All bitdepth supported.
32 * Supported color types are: PALETTE, RGB, GRAY, RGB_ALPHA, GRAY_ALPHA.
33 * Supported ancillary chunks: tRNS, cHRM, gAMA, (sRGB), (iCCP)
34 *
35 * cHRM support is not tested well. CalRGB/CalGray colorspace is used for
36 * PNG images that have cHRM chunk.
37 *
38 */
39
40 #include "system.h"
41 #include "error.h"
42 #include "mem.h"
43
44 #include "dpxconf.h"
45
46 #include "pdfcolor.h"
47 #include "pdfobj.h"
48
49 #define PNG_DEBUG_STR "PNG"
50 #define PNG_DEBUG 3
51
52 #ifdef HAVE_LIBPNG
53
54 /*
55 * Write, MNG, Progressive not required.
56 */
57 #define PNG_NO_WRITE_SUPPORTED
58 #define PNG_NO_MNG_FEATURES
59 #define PNG_NO_PROGRESSIVE_READ
60
61 #include <png.h>
62 #include "pngimage.h"
63
64 #include "pdfximage.h"
65
66 #define DPX_PNG_DEFAULT_GAMMA 2.2
67
68 #define PDF_TRANS_TYPE_NONE 0
69 #define PDF_TRANS_TYPE_BINARY 1
70 #define PDF_TRANS_TYPE_ALPHA 2
71
72 /* ColorSpace */
73 static pdf_obj *create_cspace_Indexed (png_structp png_ptr, png_infop info_ptr);
74
75 /* CIE-Based: CalRGB/CalGray */
76 static pdf_obj *create_cspace_CalRGB (png_structp png_ptr, png_infop info_ptr);
77 static pdf_obj *create_cspace_CalGray (png_structp png_ptr, png_infop info_ptr);
78 static pdf_obj *make_param_Cal (png_byte color_type,
79 double G,
80 double xw, double yw,
81 double xr, double yr,
82 double xg, double yg,
83 double xb, double yb);
84
85 /* sRGB:
86 *
87 * We (and PDF) do not have direct sRGB support. The sRGB color space can be
88 * precisely represented by ICC profile, but we use approximate CalRGB color
89 * space.
90 */
91 static pdf_obj *create_cspace_sRGB (png_structp png_ptr, png_infop info_ptr);
92 static pdf_obj *get_rendering_intent (png_structp png_ptr, png_infop info_ptr);
93
94 /* ICCBased:
95 *
96 * Not supported yet.
97 * Must check if ICC profile is valid and can be imported to PDF.
98 * There are few restrictions (should be applied to PNG too?) in ICC profile
99 * support in PDF. Some information should be obtained from profile.
100 */
101 static pdf_obj *create_cspace_ICCBased (png_structp png_ptr, png_infop info_ptr);
102
103 /* Transparency */
104 static int check_transparency (png_structp png_ptr, png_infop info_ptr);
105 /* Color-Key Mask */
106 static pdf_obj *create_ckey_mask (png_structp png_ptr, png_infop info_ptr);
107 /* Soft Mask:
108 *
109 * create_soft_mask() is for PNG_COLOR_TYPE_PALLETE.
110 * Images with alpha chunnel use strip_soft_mask().
111 * An object representing mask itself is returned.
112 */
113 static pdf_obj *create_soft_mask (png_structp png_ptr, png_infop info_ptr,
114 png_bytep image_data_ptr,
115 png_uint_32 width, png_uint_32 height);
116 static pdf_obj *strip_soft_mask (png_structp png_ptr, png_infop info_ptr,
117 png_bytep image_data_ptr,
118 png_uint_32p rowbytes_ptr,
119 png_uint_32 width, png_uint_32 height);
120
121 /* Read image body */
122 static void read_image_data (png_structp png_ptr,
123 png_bytep dest_ptr,
124 png_uint_32 height, png_uint_32 rowbytes);
125
126 int
127 check_for_png (FILE *png_file)
128 {
129 unsigned char sigbytes[4];
130
131 rewind (png_file);
132 if (fread (sigbytes, 1, sizeof(sigbytes), png_file) !=
133 sizeof(sigbytes) ||
134 (png_sig_cmp (sigbytes, 0, sizeof(sigbytes))))
135 return 0;
136 else
137 return 1;
138 }
139
140 static void warn(png_structp png_ptr, png_const_charp msg)
141 {
142 (void)png_ptr; (void)msg; /* Make compiler happy */
143 }
144
145 int
146 png_include_image (pdf_ximage *ximage, FILE *png_file)
147 {
148 pdf_obj *stream;
149 pdf_obj *stream_dict;
150 pdf_obj *colorspace, *mask, *intent;
151 png_bytep stream_data_ptr;
152 int trans_type;
153 ximage_info info;
154 /* Libpng stuff */
155 png_structp png_ptr;
156 png_infop png_info_ptr;
157 png_byte bpc, color_type;
158 png_uint_32 width, height, rowbytes;
159
160 pdf_ximage_init_image_info(&info);
161
162 stream = NULL;
163 stream_dict = NULL;
164 colorspace = mask = intent = NULL;
165
166 rewind (png_file);
167 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, warn);
168 if (png_ptr == NULL ||
169 (png_info_ptr = png_create_info_struct (png_ptr)) == NULL) {
170 WARN("%s: Creating Libpng read/info struct failed.", PNG_DEBUG_STR);
171 if (png_ptr)
172 png_destroy_read_struct(&png_ptr, NULL, NULL);
173 return -1;
174 }
175
176 #if PNG_LIBPNG_VER >= 10603
177 /* ignore possibly incorrect CMF bytes */
178 png_set_option(png_ptr, PNG_MAXIMUM_INFLATE_WINDOW, PNG_OPTION_ON);
179 #endif
180
181 /* Inititializing file IO. */
182 png_init_io (png_ptr, png_file);
183
184 /* Read PNG info-header and get some info. */
185 png_read_info(png_ptr, png_info_ptr);
186 color_type = png_get_color_type (png_ptr, png_info_ptr);
187 width = png_get_image_width (png_ptr, png_info_ptr);
188 height = png_get_image_height(png_ptr, png_info_ptr);
189 bpc = png_get_bit_depth (png_ptr, png_info_ptr);
190
191 /* Ask libpng to convert down to 8-bpc. */
192 if (bpc > 8) {
193 if (pdf_check_version(1, 5) < 0) {
194 WARN("%s: 16-bpc PNG requires PDF version 1.5.", PNG_DEBUG_STR);
195 png_set_strip_16(png_ptr);
196 bpc = 8;
197 }
198 }
199 /* Ask libpng to gamma-correct.
200 * It is wrong to assume screen gamma value 2.2 but...
201 * We do gamma correction here only when uncalibrated color space is used.
202 */
203 if (!png_get_valid(png_ptr, png_info_ptr, PNG_INFO_iCCP) &&
204 !png_get_valid(png_ptr, png_info_ptr, PNG_INFO_sRGB) &&
205 !png_get_valid(png_ptr, png_info_ptr, PNG_INFO_cHRM) &&
206 png_get_valid(png_ptr, png_info_ptr, PNG_INFO_gAMA)) {
207 double G = 1.0;
208 png_get_gAMA (png_ptr, png_info_ptr, &G);
209 png_set_gamma(png_ptr, 2.2, G);
210 }
211
212 trans_type = check_transparency(png_ptr, png_info_ptr);
213 /* check_transparency() does not do updata_info() */
214 png_read_update_info(png_ptr, png_info_ptr);
215 rowbytes = png_get_rowbytes(png_ptr, png_info_ptr);
216
217 /* Values listed below will not be modified in the remaining process. */
218 info.width = width;
219 info.height = height;
220 info.bits_per_component = bpc;
221
222 if (dpx_conf.compat_mode == dpx_mode_compat_mode)
223 info.xdensity = info.ydensity = 72.0 / 100.0;
224 else
225 {
226 png_uint_32 xppm = png_get_x_pixels_per_meter(png_ptr, png_info_ptr);
227 png_uint_32 yppm = png_get_y_pixels_per_meter(png_ptr, png_info_ptr);
228
229 if (xppm > 0)
230 info.xdensity = 72.0 / 0.0254 / xppm;
231 if (yppm > 0)
232 info.ydensity = 72.0 / 0.0254 / yppm;
233 }
234
235 stream = pdf_new_stream (STREAM_COMPRESS);
236 stream_dict = pdf_stream_dict(stream);
237
238 stream_data_ptr = (png_bytep) NEW(rowbytes*height, png_byte);
239 read_image_data(png_ptr, stream_data_ptr, height, rowbytes);
240
241 /* Non-NULL intent means there is valid sRGB chunk. */
242 intent = get_rendering_intent(png_ptr, png_info_ptr);
243 if (intent)
244 pdf_add_dict(stream_dict, pdf_new_name("Intent"), intent);
245
246 switch (color_type) {
247 case PNG_COLOR_TYPE_PALETTE:
248
249 colorspace = create_cspace_Indexed(png_ptr, png_info_ptr);
250
251 switch (trans_type) {
252 case PDF_TRANS_TYPE_BINARY:
253 /* Color-key masking */
254 mask = create_ckey_mask(png_ptr, png_info_ptr);
255 break;
256 case PDF_TRANS_TYPE_ALPHA:
257 /* Soft mask */
258 mask = create_soft_mask(png_ptr, png_info_ptr, stream_data_ptr, width, height);
259 break;
260 default:
261 /* Nothing to be done here.
262 * No tRNS chunk or image already composited with background color.
263 */
264 break;
265 }
266 info.num_components = 1;
267
268 break;
269 case PNG_COLOR_TYPE_RGB:
270 case PNG_COLOR_TYPE_RGB_ALPHA:
271
272 if (png_get_valid(png_ptr, png_info_ptr, PNG_INFO_iCCP))
273 colorspace = create_cspace_ICCBased(png_ptr, png_info_ptr);
274 else if (intent) {
275 colorspace = create_cspace_sRGB(png_ptr, png_info_ptr);
276 } else {
277 colorspace = create_cspace_CalRGB(png_ptr, png_info_ptr);
278 }
279 if (!colorspace)
280 colorspace = pdf_new_name("DeviceRGB");
281
282 switch (trans_type) {
283 case PDF_TRANS_TYPE_BINARY:
284 mask = create_ckey_mask(png_ptr, png_info_ptr);
285 break;
286 /* rowbytes changes 4 to 3 at here */
287 case PDF_TRANS_TYPE_ALPHA:
288 mask = strip_soft_mask(png_ptr, png_info_ptr,
289 stream_data_ptr, &rowbytes, width, height);
290 break;
291 default:
292 mask = NULL;
293 }
294 info.num_components = 3;
295 break;
296
297 case PNG_COLOR_TYPE_GRAY:
298 case PNG_COLOR_TYPE_GRAY_ALPHA:
299
300 if (png_get_valid(png_ptr, png_info_ptr, PNG_INFO_iCCP))
301 colorspace = create_cspace_ICCBased(png_ptr, png_info_ptr);
302 else if (intent) {
303 colorspace = create_cspace_sRGB(png_ptr, png_info_ptr);
304 } else {
305 colorspace = create_cspace_CalGray(png_ptr, png_info_ptr);
306 }
307 if (!colorspace)
308 colorspace = pdf_new_name("DeviceGray");
309
310 switch (trans_type) {
311 case PDF_TRANS_TYPE_BINARY:
312 mask = create_ckey_mask(png_ptr, png_info_ptr);
313 break;
314 case PDF_TRANS_TYPE_ALPHA:
315 mask = strip_soft_mask(png_ptr, png_info_ptr,
316 stream_data_ptr, &rowbytes, width, height);
317 break;
318 default:
319 mask = NULL;
320 }
321 info.num_components = 1;
322 break;
323
324 default:
325 WARN("%s: Unknown PNG colortype %d.", PNG_DEBUG_STR, color_type);
326 }
327 pdf_add_dict(stream_dict, pdf_new_name("ColorSpace"), colorspace);
328
329 pdf_add_stream(stream, stream_data_ptr, rowbytes*height);
330 RELEASE(stream_data_ptr);
331
332 if (mask) {
333 if (trans_type == PDF_TRANS_TYPE_BINARY)
334 pdf_add_dict(stream_dict, pdf_new_name("Mask"), mask);
335 else if (trans_type == PDF_TRANS_TYPE_ALPHA) {
336 if (info.bits_per_component >= 8 && info.width > 64) {
337 pdf_stream_set_predictor(mask, 2, info.width,
338 info.bits_per_component, 1);
339 }
340 pdf_add_dict(stream_dict, pdf_new_name("SMask"), pdf_ref_obj(mask));
341 pdf_release_obj(mask);
342 } else {
343 WARN("%s: Unknown transparency type...???", PNG_DEBUG_STR);
344 pdf_release_obj(mask);
345 }
346 }
347
348 /* Finally read XMP Metadata
349 * See, XMP Specification Part 3, Storage in Files
350 * http://www.adobe.com/jp/devnet/xmp.html
351 *
352 * We require libpng version >= 1.6.14 since prior versions
353 * of libpng had a bug that incorrectly treat the compression
354 * flag of iTxt chunks.
355 */
356 #if PNG_LIBPNG_VER >= 10614
357 if (pdf_check_version(1, 4) >= 0) {
358 png_textp text_ptr;
359 pdf_obj *XMP_stream, *XMP_stream_dict;
360 int i, num_text;
361 int have_XMP = 0;
362
363 num_text = png_get_text(png_ptr, png_info_ptr, &text_ptr, NULL);
364 for (i = 0; i < num_text; i++) {
365 if (!memcmp(text_ptr[i].key, "XML:com.adobe.xmp", 17)) {
366 /* XMP found */
367 if (text_ptr[i].compression != PNG_ITXT_COMPRESSION_NONE ||
368 text_ptr[i].itxt_length == 0)
369 WARN("%s: Invalid value(s) in iTXt chunk for XMP Metadata.", PNG_DEBUG_STR);
370 else if (have_XMP)
371 WARN("%s: Multiple XMP Metadata. Don't know how to treat it.", PNG_DEBUG_STR);
372 else {
373 /* We compress XMP metadata for included images here.
374 * It is not recommended to compress XMP metadata for PDF documents but
375 * we compress XMP metadata for included images here to avoid confusing
376 * application programs that only want PDF document global XMP metadata
377 * and scan for that.
378 */
379 XMP_stream = pdf_new_stream(STREAM_COMPRESS);
380 XMP_stream_dict = pdf_stream_dict(XMP_stream);
381 pdf_add_dict(XMP_stream_dict,
382 pdf_new_name("Type"), pdf_new_name("Metadata"));
383 pdf_add_dict(XMP_stream_dict,
384 pdf_new_name("Subtype"), pdf_new_name("XML"));
385 pdf_add_stream(XMP_stream, text_ptr[i].text, text_ptr[i].itxt_length);
386 pdf_add_dict(stream_dict,
387 pdf_new_name("Metadata"), pdf_ref_obj(XMP_stream));
388 pdf_release_obj(XMP_stream);
389 have_XMP = 1;
390 }
391 }
392 }
393 }
394 #endif /* PNG_LIBPNG_VER */
395
396 png_read_end(png_ptr, NULL);
397
398 /* Cleanup */
399 if (png_info_ptr)
400 png_destroy_info_struct(png_ptr, &png_info_ptr);
401 if (png_ptr)
402 png_destroy_read_struct(&png_ptr, NULL, NULL);
403 if (color_type != PNG_COLOR_TYPE_PALETTE &&
404 info.bits_per_component >= 8 &&
405 info.height > 64) {
406 pdf_stream_set_predictor(stream, 15, info.width,
407 info.bits_per_component, info.num_components);
408 }
409 pdf_ximage_set_image(ximage, &info, stream);
410
411 return 0;
412 }
413
414 /*
415 * The returned value trans_type is the type of transparency to be used for
416 * this image. Possible values are:
417 *
418 * PDF_TRANS_TYPE_NONE No Masking will be used/required.
419 * PDF_TRANS_TYPE_BINARY Pixels are either fully opaque/fully transparent.
420 * PDF_TRANS_TYPE_ALPHA Uses alpha channel, requies SMask.(PDF-1.4)
421 *
422 * check_transparency() must check the current setting of output PDF version
423 * and must choose appropriate trans_type value according to PDF version of
424 * current output PDF document.
425 *
426 * If the PDF version is less than 1.3, no transparency is supported for this
427 * version of PDF, hence PDF_TRANS_TYPE_NONE must be returned. And when the PDF
428 * version is equal to 1.3, possible retrun values are PDF_TRANS_TYPE_BINARY or
429 * PDF_TRANS_TYPE_NONE. The latter case arises when PNG file uses alpha channel
430 * explicitly (color type PNG_COLOR_TYPE_XXX_ALPHA), or the tRNS chunk for the
431 * PNG_COLOR_TYPE_PALETTE image contains intermediate values of opacity.
432 *
433 * Finally, in the case of PDF version 1.4, all kind of translucent pixels can
434 * be represented with Soft-Mask.
435 */
436
437 static int
438 check_transparency (png_structp png_ptr, png_infop info_ptr)
439 {
440 int trans_type;
441 png_byte color_type;
442 png_color_16p trans_values;
443 png_bytep trans;
444 int num_trans;
445
446 color_type = png_get_color_type(png_ptr, info_ptr);
447
448 /*
449 * First we set trans_type to appropriate value for PNG image.
450 */
451 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
452 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
453 trans_type = PDF_TRANS_TYPE_ALPHA;
454 } else if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) &&
455 png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values)) {
456 /* Have valid tRNS chunk. */
457 switch (color_type) {
458 case PNG_COLOR_TYPE_PALETTE:
459 /* Use color-key mask if possible. */
460 trans_type = PDF_TRANS_TYPE_BINARY;
461 while (num_trans-- > 0) {
462 if (trans[num_trans] != 0x00 && trans[num_trans] != 0xff) {
463 /* This seems not binary transparency */
464 trans_type = PDF_TRANS_TYPE_ALPHA;
465 break;
466 }
467 }
468 break;
469 case PNG_COLOR_TYPE_GRAY:
470 case PNG_COLOR_TYPE_RGB:
471 /* RGB or GRAY, single color specified by trans_values is transparent. */
472 trans_type = PDF_TRANS_TYPE_BINARY;
473 break;
474 default:
475 /* Else tRNS silently ignored. */
476 trans_type = PDF_TRANS_TYPE_NONE;
477 }
478 } else { /* no transparency */
479 trans_type = PDF_TRANS_TYPE_NONE;
480 }
481
482 /*
483 * Now we check PDF version.
484 * We can convert alpha cahnnels to explicit mask via user supplied alpha-
485 * threshold value. But I will not do that.
486 */
487 if (( pdf_check_version(1, 3) < 0 && trans_type != PDF_TRANS_TYPE_NONE ) ||
488 ( pdf_check_version(1, 4) < 0 && trans_type == PDF_TRANS_TYPE_ALPHA )) {
489 /*
490 * No transparency supported but PNG uses transparency, or Soft-Mask
491 * required but no support for it is available in this version of PDF.
492 * We must do pre-composition of image with the background image here. But,
493 * we cannot do that in general since dvipdfmx is not a rasterizer. What we
494 * can do here is to composite image with a rectangle filled with the
495 * background color. However, images are stored as an Image XObject which
496 * can be referenced anywhere in the PDF document content. Hence, we cannot
497 * know the correct background color at this time. So we will choose white
498 * as background color, which is most probable color in our cases.
499 * We ignore bKGD chunk.
500 */
501 png_color_16 bg;
502 bg.red = 255; bg.green = 255; bg.blue = 255; bg.gray = 255; bg.index = 0;
503 png_set_background(png_ptr, &bg, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
504 WARN("%s: Transparency will be ignored. (no support in PDF ver. < 1.3)", PNG_DEBUG_STR);
505 if (pdf_check_version(1, 3) < 0)
506 WARN("%s: Please use -V 3 option to enable binary transparency support.", PNG_DEBUG_STR);
507 if (pdf_check_version(1, 4) < 0)
508 WARN("%s: Please use -V 4 option to enable full alpha channel support.", PNG_DEBUG_STR);
509 trans_type = PDF_TRANS_TYPE_NONE;
510 }
511
512 return trans_type;
513 }
514
515 /*
516 * sRGB:
517 *
518 * If sRGB chunk is present, cHRM and gAMA chunk must be ignored.
519 *
520 */
521 static pdf_obj *
522 get_rendering_intent (png_structp png_ptr, png_infop info_ptr)
523 {
524 pdf_obj *intent;
525 int srgb_intent;
526
527 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB) &&
528 png_get_sRGB (png_ptr, info_ptr, &srgb_intent)) {
529 switch (srgb_intent) {
530 case PNG_sRGB_INTENT_SATURATION:
531 intent = pdf_new_name("Saturation");
532 break;
533 case PNG_sRGB_INTENT_PERCEPTUAL:
534 intent = pdf_new_name("Perceptual");
535 break;
536 case PNG_sRGB_INTENT_ABSOLUTE:
537 intent = pdf_new_name("AbsoluteColorimetric");
538 break;
539 case PNG_sRGB_INTENT_RELATIVE:
540 intent = pdf_new_name("RelativeColorimetric");
541 break;
542 default:
543 WARN("%s: Invalid value in PNG sRGB chunk: %d", PNG_DEBUG_STR, srgb_intent);
544 intent = NULL;
545 }
546 } else
547 intent = NULL;
548
549 return intent;
550 }
551
552 /* Approximated sRGB */
553 static pdf_obj *
554 create_cspace_sRGB (png_structp png_ptr, png_infop info_ptr)
555 {
556 pdf_obj *colorspace;
557 pdf_obj *cal_param;
558 png_byte color_type;
559
560 color_type = png_get_color_type(png_ptr, info_ptr);
561
562 /* Parameters taken from PNG spec. section 4.2.2.3. */
563 cal_param = make_param_Cal(color_type,
564 2.2,
565 0.3127, 0.329,
566 0.64, 0.33, 0.3, 0.6, 0.15, 0.06);
567 if (!cal_param)
568 return NULL;
569
570 colorspace = pdf_new_array();
571
572 switch (color_type) {
573 case PNG_COLOR_TYPE_RGB:
574 case PNG_COLOR_TYPE_RGB_ALPHA:
575 case PNG_COLOR_TYPE_PALETTE:
576 pdf_add_array(colorspace, pdf_new_name("CalRGB"));
577 break;
578 case PNG_COLOR_TYPE_GRAY:
579 case PNG_COLOR_TYPE_GRAY_ALPHA:
580 pdf_add_array(colorspace, pdf_new_name("CalGray"));
581 break;
582 }
583 pdf_add_array(colorspace, cal_param);
584
585 return colorspace;
586 }
587
588 static pdf_obj *
589 create_cspace_ICCBased (png_structp png_ptr, png_infop info_ptr)
590 {
591 pdf_obj *colorspace;
592 int csp_id, colortype;
593 png_byte color_type;
594 png_charp name;
595 int compression_type; /* Manual page for libpng does not
596 * clarify whether profile data is inflated by libpng.
597 */
598 #if PNG_LIBPNG_VER_MINOR < 5
599 png_charp profile;
600 #else
601 png_bytep profile;
602 #endif
603 png_uint_32 proflen;
604
605 if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_iCCP) ||
606 !png_get_iCCP(png_ptr, info_ptr, &name, &compression_type, &profile, &proflen))
607 return NULL;
608
609 color_type = png_get_color_type(png_ptr, info_ptr);
610
611 if (color_type & PNG_COLOR_MASK_COLOR) {
612 colortype = PDF_COLORSPACE_TYPE_RGB;
613 #if 0
614 alternate = create_cspace_CalRGB(png_ptr, info_ptr);
615 #endif
616 } else {
617 colortype = PDF_COLORSPACE_TYPE_GRAY;
618 #if 0
619 alternate = create_cspace_CalGray(png_ptr, info_ptr);
620 #endif
621 }
622
623 #if 0
624 if (alternate)
625 pdf_add_dict(dict, pdf_new_name("Alternate"), alternate);
626 #endif
627
628 if (iccp_check_colorspace(colortype, profile, proflen) < 0)
629 colorspace = NULL;
630 else {
631 csp_id = iccp_load_profile(name, profile, proflen);
632 if (csp_id < 0) {
633 colorspace = NULL;
634 } else {
635 colorspace = pdf_get_colorspace_reference(csp_id);
636 }
637 }
638
639 /* Rendering intent ... */
640
641 return colorspace;
642 }
643
644 /*
645 * gAMA, cHRM:
646 *
647 * If cHRM is present, we use CIE-Based color space. gAMA is also used here
648 * if available.
649 */
650
651 #define INVALID_CHRM_VALUE(xw,yw,xr,yr,xg,yg,xb,yb) (\
652 (xw) <= 0.0 || (yw) < 1.0e-10 || \
653 (xr) < 0.0 || (yr) < 0.0 || (xg) < 0.0 || (yg) < 0.0 || \
654 (xb) < 0.0 || (yb) < 0.0)
655
656 static pdf_obj *
657 create_cspace_CalRGB (png_structp png_ptr, png_infop info_ptr)
658 {
659 pdf_obj *colorspace;
660 pdf_obj *cal_param;
661 double xw, yw, xr, yr, xg, yg, xb, yb;
662 double G;
663
664 if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_cHRM) ||
665 !png_get_cHRM(png_ptr, info_ptr, &xw, &yw, &xr, &yr, &xg, &yg, &xb, &yb))
666 return NULL;
667
668 if (xw <= 0.0 || yw < 1.0e-10 ||
669 xr < 0.0 || yr < 0.0 || xg < 0.0 || yg < 0.0 || xb < 0.0 || yb < 0.0) {
670 WARN("%s: Invalid cHRM chunk parameters found.", PNG_DEBUG_STR);
671 return NULL;
672 }
673
674 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA) &&
675 png_get_gAMA (png_ptr, info_ptr, &G)) {
676 if (G < 1.0e-2) {
677 WARN("%s: Unusual Gamma value: 1.0 / %g", PNG_DEBUG_STR, G);
678 return NULL;
679 }
680 G = 1.0 / G; /* Gamma is inverted. */
681 } else {
682 G = DPX_PNG_DEFAULT_GAMMA;
683 }
684
685 cal_param = make_param_Cal(PNG_COLOR_TYPE_RGB, G, xw, yw, xr, yr, xg, yg, xb, yb);
686
687 if (!cal_param)
688 return NULL;
689
690 colorspace = pdf_new_array();
691 pdf_add_array(colorspace, pdf_new_name("CalRGB"));
692 pdf_add_array(colorspace, cal_param);
693
694 return colorspace;
695 }
696
697 static pdf_obj *
698 create_cspace_CalGray (png_structp png_ptr, png_infop info_ptr)
699 {
700 pdf_obj *colorspace;
701 pdf_obj *cal_param;
702 double xw, yw, xr, yr, xg, yg, xb, yb;
703 double G;
704
705 if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_cHRM) ||
706 !png_get_cHRM(png_ptr, info_ptr, &xw, &yw, &xr, &yr, &xg, &yg, &xb, &yb))
707 return NULL;
708
709 if (xw <= 0.0 || yw < 1.0e-10 ||
710 xr < 0.0 || yr < 0.0 || xg < 0.0 || yg < 0.0 || xb < 0.0 || yb < 0.0) {
711 WARN("%s: Invalid cHRM chunk parameters found.", PNG_DEBUG_STR);
712 return NULL;
713 }
714
715 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA) &&
716 png_get_gAMA (png_ptr, info_ptr, &G)) {
717 if (G < 1.0e-2) {
718 WARN("%s: Unusual Gamma value: 1.0 / %g", PNG_DEBUG_STR, G);
719 return NULL;
720 }
721 G = 1.0 / G; /* Gamma is inverted. */
722 } else {
723 G = DPX_PNG_DEFAULT_GAMMA;
724 }
725
726 cal_param = make_param_Cal(PNG_COLOR_TYPE_GRAY, G, xw, yw, xr, yr, xg, yg, xb, yb);
727
728 if (!cal_param)
729 return NULL;
730
731 colorspace = pdf_new_array();
732 pdf_add_array(colorspace, pdf_new_name("CalGray"));
733 pdf_add_array(colorspace, cal_param);
734
735 return colorspace;
736 }
737
738 static pdf_obj *
739 make_param_Cal (png_byte color_type,
740 double G, /* Gamma */
741 double xw, double yw,
742 double xr, double yr, double xg, double yg, double xb, double yb)
743 {
744 pdf_obj *cal_param;
745 pdf_obj *white_point, *matrix, *dev_gamma;
746 double Xw, Yw, Zw; /* Yw = 1.0 */
747 double Xr, Xg, Xb, Yr, Yb, Yg, Zr, Zg, Zb;
748
749 #ifndef ABS
750 #define ABS(x) ((x) < 0 ? -(x) : (x))
751 #endif
752 /*
753 * TODO: Check validity
754 *
755 * Conversion found in
756 *
757 * com.sixlegs.image.png - Java package to read and display PNG images
758 * Copyright (C) 1998, 1999, 2001 Chris Nokleberg
759 *
760 * http://www.sixlegs.com/software/png/
761 *
762 */
763 {
764 double zw, zr, zg, zb;
765 double fr, fg, fb;
766 double det;
767
768 /* WhitePoint */
769 zw = 1 - (xw + yw);
770 zr = 1 - (xr + yr); zg = 1 - (xg + yg); zb = 1 - (xb + yb);
771 Xw = xw / yw; Yw = 1.0; Zw = zw / yw;
772
773 /* Matrix */
774 det = xr * (yg * zb - zg * yb) - xg * (yr * zb - zr * yb) + xb * (yr * zg - zr * yg);
775 if (ABS(det) < 1.0e-10) {
776 WARN("Non invertible matrix: Maybe invalid value(s) specified in cHRM chunk.");
777 return NULL;
778 }
779 fr = (Xw * (yg * zb - zg * yb) - xg * (zb - Zw * yb) + xb * (zg - Zw * yg)) / det;
780 fg = (xr * (zb - Zw * yb) - Xw * (yr * zb - zr * yb) + xb * (yr * Zw - zr)) / det;
781 fb = (xr * (yg * Zw - zg) - xg * (yr * Zw - zr) + Xw * (yr * zg - zr * yg)) / det;
782 Xr = fr * xr; Yr = fr * yr; Zr = fr * zr;
783 Xg = fg * xg; Yg = fg * yg; Zg = fg * zg;
784 Xb = fb * xb; Yb = fb * yb; Zb = fb * zb;
785 }
786
787 if (G < 1.0e-2) {
788 WARN("Unusual Gamma specified: 1.0 / %g", G);
789 return NULL;
790 }
791
792 cal_param = pdf_new_dict();
793
794 /* White point is always required. */
795 white_point = pdf_new_array();
796 pdf_add_array(white_point, pdf_new_number(ROUND(Xw, 0.00001)));
797 pdf_add_array(white_point, pdf_new_number(ROUND(Yw, 0.00001)));
798 pdf_add_array(white_point, pdf_new_number(ROUND(Zw, 0.00001)));
799 pdf_add_dict(cal_param, pdf_new_name("WhitePoint"), white_point);
800
801 /* Matrix - default: Identity */
802 if (color_type & PNG_COLOR_MASK_COLOR) {
803 if (G != 1.0) {
804 dev_gamma = pdf_new_array();
805 pdf_add_array(dev_gamma, pdf_new_number(ROUND(G, 0.00001)));
806 pdf_add_array(dev_gamma, pdf_new_number(ROUND(G, 0.00001)));
807 pdf_add_array(dev_gamma, pdf_new_number(ROUND(G, 0.00001)));
808 pdf_add_dict(cal_param, pdf_new_name("Gamma"), dev_gamma);
809 }
810
811 matrix = pdf_new_array();
812 pdf_add_array(matrix, pdf_new_number(ROUND(Xr, 0.00001)));
813 pdf_add_array(matrix, pdf_new_number(ROUND(Yr, 0.00001)));
814 pdf_add_array(matrix, pdf_new_number(ROUND(Zr, 0.00001)));
815 pdf_add_array(matrix, pdf_new_number(ROUND(Xg, 0.00001)));
816 pdf_add_array(matrix, pdf_new_number(ROUND(Yg, 0.00001)));
817 pdf_add_array(matrix, pdf_new_number(ROUND(Zg, 0.00001)));
818 pdf_add_array(matrix, pdf_new_number(ROUND(Xb, 0.00001)));
819 pdf_add_array(matrix, pdf_new_number(ROUND(Yb, 0.00001)));
820 pdf_add_array(matrix, pdf_new_number(ROUND(Zb, 0.00001)));
821 pdf_add_dict (cal_param, pdf_new_name("Matrix"), matrix);
822 } else { /* Gray */
823 if (G != 1.0)
824 pdf_add_dict(cal_param,
825 pdf_new_name("Gamma"),
826 pdf_new_number(ROUND(G, 0.00001)));
827 }
828
829 return cal_param;
830 }
831
832 /*
833 * Set up Indexed ColorSpace for color-type PALETTE:
834 *
835 * PNG allows only RGB color for base color space. If gAMA and/or cHRM
836 * chunk is available, we can use CalRGB color space instead of DeviceRGB
837 * for base color space.
838 *
839 */
840 static pdf_obj *
841 create_cspace_Indexed (png_structp png_ptr, png_infop info_ptr)
842 {
843 pdf_obj *colorspace;
844 pdf_obj *base, *lookup;
845 png_byte *data_ptr;
846 png_colorp plte;
847 int num_plte, i;
848
849 if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE) ||
850 !png_get_PLTE(png_ptr, info_ptr, &plte, &num_plte)) {
851 WARN("%s: PNG does not have valid PLTE chunk.", PNG_DEBUG_STR);
852 return NULL;
853 }
854
855 /* Order is important. */
856 colorspace = pdf_new_array ();
857 pdf_add_array(colorspace, pdf_new_name("Indexed"));
858
859 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_iCCP))
860 base = create_cspace_ICCBased(png_ptr, info_ptr);
861 else {
862 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB))
863 base = create_cspace_sRGB(png_ptr, info_ptr);
864 else
865 base = create_cspace_CalRGB(png_ptr, info_ptr);
866 }
867
868 if (!base)
869 base = pdf_new_name("DeviceRGB");
870
871 pdf_add_array(colorspace, base);
872 pdf_add_array(colorspace, pdf_new_number(num_plte-1));
873 data_ptr = NEW(num_plte*3, png_byte);
874 for (i = 0; i < num_plte; i++) {
875 data_ptr[3*i] = plte[i].red;
876 data_ptr[3*i+1] = plte[i].green;
877 data_ptr[3*i+2] = plte[i].blue;
878 }
879 lookup = pdf_new_string(data_ptr, num_plte*3);
880 RELEASE(data_ptr);
881 pdf_add_array(colorspace, lookup);
882
883 return colorspace;
884 }
885
886 /*
887 * Colorkey Mask: array
888 *
889 * [component_0_min component_0_max ... component_n_min component_n_max]
890 *
891 */
892
893 static pdf_obj *
894 create_ckey_mask (png_structp png_ptr, png_infop info_ptr)
895 {
896 pdf_obj *colorkeys;
897 png_byte color_type;
898 png_bytep trans;
899 int num_trans, i;
900 png_color_16p colors;
901
902 if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ||
903 !png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &colors)) {
904 WARN("%s: PNG does not have valid tRNS chunk!", PNG_DEBUG_STR);
905 return NULL;
906 }
907
908 colorkeys = pdf_new_array();
909 color_type = png_get_color_type(png_ptr, info_ptr);
910
911 switch (color_type) {
912 case PNG_COLOR_TYPE_PALETTE:
913 for (i = 0; i < num_trans; i++) {
914 if (trans[i] == 0x00) {
915 pdf_add_array(colorkeys, pdf_new_number(i));
916 pdf_add_array(colorkeys, pdf_new_number(i));
917 } else if (trans[i] != 0xff) {
918 WARN("%s: You found a bug in pngimage.c.", PNG_DEBUG_STR);
919 }
920 }
921 break;
922 case PNG_COLOR_TYPE_RGB:
923 pdf_add_array(colorkeys, pdf_new_number(colors->red));
924 pdf_add_array(colorkeys, pdf_new_number(colors->red));
925 pdf_add_array(colorkeys, pdf_new_number(colors->green));
926 pdf_add_array(colorkeys, pdf_new_number(colors->green));
927 pdf_add_array(colorkeys, pdf_new_number(colors->blue));
928 pdf_add_array(colorkeys, pdf_new_number(colors->blue));
929 break;
930 case PNG_COLOR_TYPE_GRAY:
931 pdf_add_array(colorkeys, pdf_new_number(colors->gray));
932 pdf_add_array(colorkeys, pdf_new_number(colors->gray));
933 break;
934 default:
935 WARN("%s: You found a bug in pngimage.c.", PNG_DEBUG_STR);
936 pdf_release_obj(colorkeys);
937 colorkeys = NULL;
938 }
939
940 return colorkeys;
941 }
942
943 /*
944 * Soft-Mask: stream
945 *
946 * <<
947 * /Type /XObject
948 * /Subtype /Image
949 * /Width -int-
950 * /Height -int-
951 * /BitsPerComponent bpc
952 * >>
953 * stream .... endstream
954 *
955 * ColorSpace, Mask, SMask must be absent. ImageMask must be false or absent.
956 */
957
958 static pdf_obj *
959 create_soft_mask (png_structp png_ptr, png_infop info_ptr,
960 png_bytep image_data_ptr, png_uint_32 width, png_uint_32 height)
961 {
962 pdf_obj *smask, *dict;
963 png_bytep smask_data_ptr;
964 png_bytep trans;
965 int num_trans;
966 png_uint_32 i;
967 png_byte bpc, mask, shift;
968
969 if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ||
970 !png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, NULL)) {
971 WARN("%s: PNG does not have valid tRNS chunk but tRNS is requested.", PNG_DEBUG_STR);
972 return NULL;
973 }
974 bpc = png_get_bit_depth(png_ptr, info_ptr);
975 mask = 0xff >> (8 - bpc);
976 shift = 8 - bpc;
977
978 smask = pdf_new_stream(STREAM_COMPRESS);
979 dict = pdf_stream_dict(smask);
980 smask_data_ptr = (png_bytep) NEW(width*height, png_byte);
981 pdf_add_dict(dict, pdf_new_name("Type"), pdf_new_name("XObject"));
982 pdf_add_dict(dict, pdf_new_name("Subtype"), pdf_new_name("Image"));
983 pdf_add_dict(dict, pdf_new_name("Width"), pdf_new_number(width));
984 pdf_add_dict(dict, pdf_new_name("Height"), pdf_new_number(height));
985 pdf_add_dict(dict, pdf_new_name("ColorSpace"), pdf_new_name("DeviceGray"));
986 pdf_add_dict(dict, pdf_new_name("BitsPerComponent"), pdf_new_number(8));
987 for (i = 0; i < width*height; i++) {
988 /* data is packed for 1/2/4 bpc formats, msb first */
989 png_byte idx = (image_data_ptr[bpc * i / 8] >> (shift - bpc * i % 8)) & mask;
990 smask_data_ptr[i] = (idx < num_trans) ? trans[idx] : 0xff;
991 }
992 pdf_add_stream(smask, (char *)smask_data_ptr, width*height);
993 RELEASE(smask_data_ptr);
994
995 return smask;
996 }
997
998 /* bitdepth is always 8 (16 is not supported) */
999 static pdf_obj *
1000 strip_soft_mask (png_structp png_ptr, png_infop info_ptr,
1001 /* next two values will be modified. */
1002 png_bytep image_data_ptr, png_uint_32p rowbytes_ptr,
1003 png_uint_32 width, png_uint_32 height)
1004 {
1005 pdf_obj *smask, *dict;
1006 png_byte color_type, bpc;
1007 png_bytep smask_data_ptr;
1008 png_uint_32 i;
1009
1010 color_type = png_get_color_type(png_ptr, info_ptr);
1011 bpc = png_get_bit_depth (png_ptr, info_ptr);
1012 if (color_type & PNG_COLOR_MASK_COLOR) {
1013 int bps = (bpc == 8) ? 4 : 8;
1014 if (*rowbytes_ptr != bps*width*sizeof(png_byte)) { /* Something wrong */
1015 WARN("%s: Inconsistent rowbytes value.", PNG_DEBUG_STR);
1016 return NULL;
1017 }
1018 } else {
1019 int bps = (bpc == 8) ? 2 : 4;
1020 if (*rowbytes_ptr != bps*width*sizeof(png_byte)) { /* Something wrong */
1021 WARN("%s: Inconsistent rowbytes value.", PNG_DEBUG_STR);
1022 return NULL;
1023 }
1024 }
1025
1026 smask = pdf_new_stream(STREAM_COMPRESS);
1027 dict = pdf_stream_dict(smask);
1028 pdf_add_dict(dict, pdf_new_name("Type"), pdf_new_name("XObject"));
1029 pdf_add_dict(dict, pdf_new_name("Subtype"), pdf_new_name("Image"));
1030 pdf_add_dict(dict, pdf_new_name("Width"), pdf_new_number(width));
1031 pdf_add_dict(dict, pdf_new_name("Height"), pdf_new_number(height));
1032 pdf_add_dict(dict, pdf_new_name("ColorSpace"), pdf_new_name("DeviceGray"));
1033 pdf_add_dict(dict, pdf_new_name("BitsPerComponent"), pdf_new_number(bpc));
1034
1035 smask_data_ptr = (png_bytep) NEW((bpc/8)*width*height, png_byte);
1036
1037 switch (color_type) {
1038 case PNG_COLOR_TYPE_RGB_ALPHA:
1039 if (bpc == 8) {
1040 for (i = 0; i < width*height; i++) {
1041 memmove(image_data_ptr+(3*i), image_data_ptr+(4*i), 3);
1042 smask_data_ptr[i] = image_data_ptr[4*i+3];
1043 }
1044 *rowbytes_ptr = 3*width*sizeof(png_byte);
1045 } else {
1046 for (i = 0; i < width*height; i++) {
1047 memmove(image_data_ptr+(6*i), image_data_ptr+(8*i), 6);
1048 smask_data_ptr[2*i] = image_data_ptr[8*i+6];
1049 smask_data_ptr[2*i+1] = image_data_ptr[8*i+7];
1050 }
1051 *rowbytes_ptr = 6*width*sizeof(png_byte);
1052 }
1053 break;
1054 case PNG_COLOR_TYPE_GRAY_ALPHA:
1055 if (bpc == 8) {
1056 for (i = 0; i < width*height; i++) {
1057 image_data_ptr[i] = image_data_ptr[2*i];
1058 smask_data_ptr[i] = image_data_ptr[2*i+1];
1059 }
1060 *rowbytes_ptr = width*sizeof(png_byte);
1061 } else {
1062 for (i = 0; i < width*height; i++) {
1063 image_data_ptr[2*i] = image_data_ptr[4*i];
1064 image_data_ptr[2*i+1] = image_data_ptr[4*i+1];
1065 smask_data_ptr[2*i] = image_data_ptr[4*i+2];
1066 smask_data_ptr[2*i+1] = image_data_ptr[4*i+3];
1067 }
1068 *rowbytes_ptr = 2*width*sizeof(png_byte);
1069 }
1070 break;
1071 default:
1072 WARN("You found a bug in pngimage.c!");
1073 pdf_release_obj(smask);
1074 RELEASE(smask_data_ptr);
1075 return NULL;
1076 }
1077
1078 pdf_add_stream(smask, smask_data_ptr, (bpc/8)*width*height);
1079 RELEASE(smask_data_ptr);
1080
1081 return smask;
1082 }
1083
1084 static void
1085 read_image_data (png_structp png_ptr, png_bytep dest_ptr,
1086 png_uint_32 height, png_uint_32 rowbytes)
1087 {
1088 png_bytepp rows_p;
1089 png_uint_32 i;
1090
1091 rows_p = (png_bytepp) NEW (height, png_bytep);
1092 for (i=0; i< height; i++)
1093 rows_p[i] = dest_ptr + (rowbytes * i);
1094 png_read_image(png_ptr, rows_p);
1095 RELEASE(rows_p);
1096 }
1097
1098 int
1099 png_get_bbox (FILE *png_file, uint32_t *width, uint32_t *height,
1100 double *xdensity, double *ydensity)
1101 {
1102 png_structp png_ptr;
1103 png_infop png_info_ptr;
1104
1105 rewind (png_file);
1106 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, warn);
1107 if (png_ptr == NULL ||
1108 (png_info_ptr = png_create_info_struct (png_ptr)) == NULL) {
1109 WARN("%s: Creating Libpng read/info struct failed.", PNG_DEBUG_STR);
1110 if (png_ptr)
1111 png_destroy_read_struct(&png_ptr, NULL, NULL);
1112 return -1;
1113 }
1114
1115 /* Inititializing file IO. */
1116 png_init_io (png_ptr, png_file);
1117
1118 /* Read PNG info-header and get some info. */
1119 png_read_info(png_ptr, png_info_ptr);
1120 *width = png_get_image_width (png_ptr, png_info_ptr);
1121 *height = png_get_image_height(png_ptr, png_info_ptr);
1122
1123 if (dpx_conf.compat_mode == dpx_mode_compat_mode)
1124 *xdensity = *ydensity = 72.0 / 100.0;
1125 else
1126 {
1127 png_uint_32 xppm = png_get_x_pixels_per_meter(png_ptr, png_info_ptr);
1128 png_uint_32 yppm = png_get_y_pixels_per_meter(png_ptr, png_info_ptr);
1129
1130 *xdensity = xppm ? 72.0 / 0.0254 / xppm : 1.0;
1131 *ydensity = yppm ? 72.0 / 0.0254 / yppm : 1.0;
1132 }
1133
1134 /* Cleanup */
1135 if (png_info_ptr)
1136 png_destroy_info_struct(png_ptr, &png_info_ptr);
1137 if (png_ptr)
1138 png_destroy_read_struct(&png_ptr, NULL, NULL);
1139
1140 return 0;
1141 }
1142
1143 #endif /* HAVE_LIBPNG */