1 /****************************************************************************\
2  Part of the XeTeX typesetting system
3  Copyright (c) 1994-2006 by SIL International
4 
5  SIL Author(s): Jonathan Kew
6 
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 \****************************************************************************/
21 
22 /* this file is derived from the dvipdfmx project;
23    the original header follows... */
24 
25 /*  $Header: /home/cvsroot/dvipdfmx/src/pngimage.c,v 1.24 2004/09/11 14:50:29 hirata Exp $
26 
27     This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
28 
29     Copyright (C) 2002 by Jin-Hwan Cho and Shunsaku Hirata,
30     the dvipdfmx project team <dvipdfmx@project.ktug.or.kr>
31 
32     Copyright (C) 1998, 1999 by Mark A. Wicks <mwicks@kettering.edu>
33 
34     This program is free software; you can redistribute it and/or modify
35     it under the terms of the GNU General Public License as published by
36     the Free Software Foundation; either version 2 of the License, or
37     (at your option) any later version.
38 
39     This program is distributed in the hope that it will be useful,
40     but WITHOUT ANY WARRANTY; without even the implied warranty of
41     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42     GNU General Public License for more details.
43 
44     You should have received a copy of the GNU General Public License
45     along with this program; if not, write to the Free Software
46     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
47 */
48 
49 #if HAVE_CONFIG_H
50 #include <w2c/config.h>
51 #endif
52 
53 /*
54  * PNG SUPPORT
55  *
56  *  All bitdepth less than 16 is supported.
57  *  Supported color types are: PALETTE, RGB, GRAY, RGB_ALPHA, GRAY_ALPHA.
58  *  Supported ancillary chunks: tRNS, cHRM + gAMA, (sRGB), (iCCP)
59  *
60  *  gAMA support is available only when cHRM exists. cHRM support is not
61  *  tested well. CalRGB/CalGray colorspace is used for PNG images that
62  *  have cHRM chunk (but not sRGB).
63  *
64  * LIMITATIONS
65  *
66  *   Recent version of PDF (>= 1.5) support 16 bpc, but 16 bit bitdepth PNG
67  *   images are automatically converted to 8 bit bitpedth image.
68  *
69  * TODO
70  *
71  *  sBIT ? iTXT, tEXT and tIME as MetaData ?, pHYS (see below)
72  *  16 bpc support for PDF-1.5. JBIG compression for monochrome image.
73  *  Predictor for deflate ?
74  */
75 
76 #define PNG_DEBUG_STR "PNG"
77 #define PNG_DEBUG     3
78 
79 /*
80  * Write, MNG, Progressive not required.
81  */
82 #define PNG_NO_WRITE_SUPPORTED
83 #define PNG_NO_MNG_FEATURES
84 #define PNG_NO_PROGRESSIVE_READ
85 #if 0
86 /* 16_TO_8 required. */
87 #define PNG_NO_READ_TRANSFORMS
88 #endif
89 
90 #include <png.h>
91 #include "pngimage.h"
92 
93 #define PDF_TRANS_TYPE_NONE   0
94 #define PDF_TRANS_TYPE_BINARY 1
95 #define PDF_TRANS_TYPE_ALPHA  2
96 
warn(png_structp png_ptr,png_const_charp msg)97 static void warn(png_structp png_ptr, png_const_charp msg)
98 {
99   (void)png_ptr; (void)msg; /* Make compiler happy */
100 }
101 
102 int
check_for_png(FILE * png_file)103 check_for_png (FILE *png_file)
104 {
105   unsigned char sigbytes[4];
106 
107   rewind (png_file);
108   if (fread (sigbytes, 1, sizeof(sigbytes), png_file) !=
109       sizeof(sigbytes) ||
110       (png_sig_cmp (sigbytes, 0, sizeof(sigbytes))))
111     return 0;
112   else
113     return 1;
114 }
115 
116 int
png_scan_file(struct png_info * info,FILE * png_file)117 png_scan_file (struct png_info *info, FILE *png_file)
118 {
119   /* Libpng stuff */
120   png_structp png_ptr;
121   png_infop   png_info_ptr;
122   png_byte    bpc, color_type;
123   png_uint_32 width, height;
124 
125   rewind (png_file);
126   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, warn);
127   if (png_ptr == NULL ||
128       (png_info_ptr = png_create_info_struct (png_ptr)) == NULL) {
129     fprintf(stderr, "WARNING: %s: Creating Libpng read/info struct failed.", PNG_DEBUG_STR);
130     if (png_ptr)
131       png_destroy_read_struct(&png_ptr, NULL, NULL);
132     return -1;
133   }
134 
135   /* Inititializing file IO. */
136   png_init_io (png_ptr, png_file);
137 
138   /* Read PNG info-header and get some info. */
139   png_read_info(png_ptr, png_info_ptr);
140   color_type = png_get_color_type  (png_ptr, png_info_ptr);
141   width      = png_get_image_width (png_ptr, png_info_ptr);
142   height     = png_get_image_height(png_ptr, png_info_ptr);
143   bpc        = png_get_bit_depth   (png_ptr, png_info_ptr);
144 
145   info->xdpi  = png_get_x_pixels_per_meter(png_ptr, png_info_ptr) * 0.0254;
146   info->ydpi  = png_get_y_pixels_per_meter(png_ptr, png_info_ptr) * 0.0254;
147 
148   if (info->xdpi == 0)
149     info->xdpi = 72;
150   if (info->ydpi == 0)
151     info->ydpi = 72;
152 
153   /* Values listed below will not be modified in the remaining process. */
154   info->width  = width;
155   info->height = height;
156   info->bits_per_component = bpc;
157 
158   /* Cleanup */
159   if (png_info_ptr)
160     png_destroy_info_struct(png_ptr, &png_info_ptr);
161   if (png_ptr)
162     png_destroy_read_struct(&png_ptr, NULL, NULL);
163 
164   return 0;
165 }
166