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