1.1 diff -r 9a505be7e7fd -r 301f6f17c364 tumble_png.c 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/tumble_png.c Mon Dec 14 15:51:53 2009 +0000 1.4 @@ -0,0 +1,233 @@ 1.5 +/* 1.6 + * tumble: build a PDF file from image files 1.7 + * 1.8 + * Copyright 2004 Daniel Gloeckner 1.9 + * 1.10 + * Derived from tumble_jpeg.c written 2003 by Eric Smith <eric at brouhaha.com> 1.11 + * 1.12 + * This program is free software; you can redistribute it and/or modify 1.13 + * it under the terms of the GNU General Public License version 2 as 1.14 + * published by the Free Software Foundation. Note that permission is 1.15 + * not granted to redistribute this program under the terms of any 1.16 + * other version of the General Public License. 1.17 + * 1.18 + * This program is distributed in the hope that it will be useful, 1.19 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 1.20 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.21 + * GNU General Public License for more details. 1.22 + * 1.23 + * You should have received a copy of the GNU General Public License 1.24 + * along with this program; if not, write to the Free Software 1.25 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA 1.26 + */ 1.27 + 1.28 + 1.29 +#include <stdbool.h> 1.30 +#include <stdint.h> 1.31 +#include <stdio.h> 1.32 +#include <strings.h> /* strcasecmp() is a BSDism */ 1.33 + 1.34 + 1.35 +#include "semantics.h" 1.36 +#include "tumble.h" 1.37 +#include "bitblt.h" 1.38 +#include "pdf.h" 1.39 +#include "tumble_input.h" 1.40 + 1.41 + 1.42 +static FILE *png_f; 1.43 + 1.44 +static struct { 1.45 + uint32_t palent; 1.46 + uint8_t bpp; 1.47 + uint8_t color; 1.48 + char pal[256*3]; 1.49 +} cinfo; 1.50 + 1.51 + 1.52 +static bool match_png_suffix (char *suffix) 1.53 +{ 1.54 + return (strcasecmp (suffix, ".png") == 0); 1.55 +} 1.56 + 1.57 +static bool close_png_input_file (void) 1.58 +{ 1.59 + return (1); 1.60 +} 1.61 + 1.62 +#define BENUM(p) (((p)[0]<<24)+((p)[1]<<16)+((p)[2]<<8)+(p)[3]) 1.63 + 1.64 +static bool open_png_input_file (FILE *f, char *name) 1.65 +{ 1.66 + const char sig [8]="\211PNG\r\n\032\n"; 1.67 + uint8_t buf [8]; 1.68 + int l; 1.69 + 1.70 + l = fread (buf, 1, sizeof (sig), f); 1.71 + if (l != sizeof (sig) || memcmp(buf,sig,sizeof(sig))) { 1.72 + rewind(f); 1.73 + return 0; 1.74 + } 1.75 + 1.76 + png_f = f; 1.77 + 1.78 + return 1; 1.79 +} 1.80 + 1.81 + 1.82 +static bool last_png_input_page (void) 1.83 +{ 1.84 + return 1; 1.85 +} 1.86 + 1.87 + 1.88 +static bool get_png_image_info (int image, 1.89 + input_attributes_t input_attributes, 1.90 + image_info_t *image_info) 1.91 +{ 1.92 + uint8_t buf [20], unit; 1.93 + uint32_t width,height,xppu,yppu; 1.94 + size_t l; 1.95 + bool seen_IHDR,seen_PLTE,seen_pHYs; 1.96 + 1.97 + seen_IHDR=seen_PLTE=seen_pHYs=false; 1.98 + memset(&cinfo,0,sizeof(cinfo)); 1.99 + unit=0; 1.100 + xppu=yppu=1; 1.101 + 1.102 + for(;;) 1.103 + { 1.104 + l = fread (buf, 1, 8, png_f); 1.105 + if(l != 8) 1.106 + return 0; 1.107 + l=BENUM(buf); 1.108 + if(!memcmp(buf+4,"IHDR",4)) { 1.109 + if(seen_IHDR || l!=13) 1.110 + return 0; 1.111 + seen_IHDR=true; 1.112 + l = fread (buf, 1, 17, png_f); 1.113 + if(l!=17) 1.114 + return 0; 1.115 + width=BENUM(buf); 1.116 + height=BENUM(buf+4); 1.117 + cinfo.bpp=buf[8]; 1.118 + cinfo.color=buf[9]; 1.119 + if(buf[8]>8 || buf[10] || buf[11] || buf[12]) 1.120 + return 0; 1.121 + continue; 1.122 + } 1.123 + if(!seen_IHDR) 1.124 + return 0; 1.125 + if(!memcmp(buf+4,"PLTE",4)) { 1.126 + size_t i; 1.127 + if(seen_PLTE || l>256*3 || l%3 || !cinfo.color) 1.128 + return 0; 1.129 + seen_PLTE=true; 1.130 + i = fread (cinfo.pal, 1, l, png_f); 1.131 + if(i != l) 1.132 + return 0; 1.133 + cinfo.palent=l/3; 1.134 + fseek(png_f,4,SEEK_CUR); 1.135 + } else if(!memcmp(buf+4,"pHYs",4)) { 1.136 + if(seen_pHYs || l!=9) 1.137 + return 0; 1.138 + seen_pHYs=true; 1.139 + l = fread (buf, 1, 13, png_f); 1.140 + if(l != 13) 1.141 + return 0; 1.142 + xppu=BENUM(buf); 1.143 + yppu=BENUM(buf+4); 1.144 + unit=buf[8]; 1.145 + } else if(!memcmp(buf+4,"IDAT",4)) { 1.146 + fseek(png_f,-8,SEEK_CUR); 1.147 + break; 1.148 + } else { 1.149 + fseek(png_f,l+4,SEEK_CUR); 1.150 + } 1.151 + } 1.152 + if(cinfo.color==3 && !seen_PLTE) 1.153 + return 0; 1.154 + 1.155 +#ifdef DEBUG_JPEG 1.156 + printf ("color type: %d\n", cinfo.color); 1.157 + printf ("bit depth: %d\n", cinfo.bpp); 1.158 + printf ("density unit: %d\n", unit); 1.159 + printf ("x density: %d\n", xppu); 1.160 + printf ("y density: %d\n", yppu); 1.161 + printf ("width: %d\n", width); 1.162 + printf ("height: %d\n", height); 1.163 +#endif 1.164 + 1.165 + switch (cinfo.color) 1.166 + { 1.167 + case 0: 1.168 + image_info->color = 0; 1.169 + break; 1.170 + case 2: 1.171 + case 3: 1.172 + image_info->color = 1; 1.173 + break; 1.174 + default: 1.175 + fprintf (stderr, "PNG color type %d not supported\n", cinfo.color); 1.176 + return (0); 1.177 + } 1.178 + image_info->width_samples = width; 1.179 + image_info->height_samples = height; 1.180 + 1.181 + switch (unit==1) 1.182 + { 1.183 + case 1: 1.184 + image_info->width_points = ((image_info->width_samples * POINTS_PER_INCH) / 1.185 + (xppu * 0.0254)); 1.186 + image_info->height_points = ((image_info->height_samples * POINTS_PER_INCH) / 1.187 + (yppu * 0.0254)); 1.188 + break; 1.189 + case 0: 1.190 + /* assume 300 DPI - not great, but what else can we do? */ 1.191 + image_info->width_points = (image_info->width_samples * POINTS_PER_INCH) / 300.0; 1.192 + image_info->height_points = ((double) yppu * image_info->height_samples * POINTS_PER_INCH) / ( 300.0 * xppu); 1.193 + break; 1.194 + default: 1.195 + fprintf (stderr, "PNG pHYs unit %d not supported\n", unit); 1.196 + } 1.197 + 1.198 + return 1; 1.199 +} 1.200 + 1.201 + 1.202 +static bool process_png_image (int image, /* range 1 .. n */ 1.203 + input_attributes_t input_attributes, 1.204 + image_info_t *image_info, 1.205 + pdf_page_handle page) 1.206 +{ 1.207 + pdf_write_png_image (page, 1.208 + 0, 0, /* x, y */ 1.209 + image_info->width_points, 1.210 + image_info->height_points, 1.211 + cinfo.color, 1.212 + cinfo.color==3?cinfo.pal:NULL, 1.213 + cinfo.palent, 1.214 + cinfo.bpp, 1.215 + image_info->width_samples, 1.216 + image_info->height_samples, 1.217 + png_f); 1.218 + 1.219 + return (1); 1.220 +} 1.221 + 1.222 + 1.223 +input_handler_t png_handler = 1.224 + { 1.225 + match_png_suffix, 1.226 + open_png_input_file, 1.227 + close_png_input_file, 1.228 + last_png_input_page, 1.229 + get_png_image_info, 1.230 + process_png_image 1.231 + }; 1.232 + 1.233 + 1.234 +void init_png_handler (void) 1.235 +{ 1.236 + install_input_handler (& png_handler); 1.237 +}