1.1 diff -r 9a505be7e7fd -r 301f6f17c364 pdf_png.c 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/pdf_png.c Mon Dec 14 15:51:53 2009 +0000 1.4 @@ -0,0 +1,200 @@ 1.5 +/* 1.6 + * tumble: build a PDF file from image files 1.7 + * 1.8 + * PDF routines 1.9 + * Copyright 2004 Daniel Gloeckner 1.10 + * 1.11 + * Derived from pdf_jpeg.c written 2003 by Eric Smith <eric at brouhaha.com> 1.12 + * 1.13 + * This program is free software; you can redistribute it and/or modify 1.14 + * it under the terms of the GNU General Public License version 2 as 1.15 + * published by the Free Software Foundation. Note that permission is 1.16 + * not granted to redistribute this program under the terms of any 1.17 + * other version of the General Public License. 1.18 + * 1.19 + * This program is distributed in the hope that it will be useful, 1.20 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 1.21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.22 + * GNU General Public License for more details. 1.23 + * 1.24 + * You should have received a copy of the GNU General Public License 1.25 + * along with this program; if not, write to the Free Software 1.26 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA 1.27 + */ 1.28 + 1.29 + 1.30 +#include <stdbool.h> 1.31 +#include <stdint.h> 1.32 +#include <stdio.h> 1.33 +#include <stdlib.h> 1.34 +#include <string.h> 1.35 + 1.36 + 1.37 +#include "bitblt.h" 1.38 +#include "pdf.h" 1.39 +#include "pdf_util.h" 1.40 +#include "pdf_prim.h" 1.41 +#include "pdf_private.h" 1.42 + 1.43 + 1.44 +struct pdf_png_image 1.45 +{ 1.46 + double width, height; 1.47 + double x, y; 1.48 + bool color; /* false for grayscale */ 1.49 + uint32_t width_samples, height_samples; 1.50 + FILE *f; 1.51 + char XObject_name [4]; 1.52 +}; 1.53 + 1.54 + 1.55 +static void pdf_write_png_content_callback (pdf_file_handle pdf_file, 1.56 + struct pdf_obj *stream, 1.57 + void *app_data) 1.58 +{ 1.59 + struct pdf_png_image *image = app_data; 1.60 + 1.61 + /* transformation matrix is: width 0 0 height x y cm */ 1.62 + pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ", 1.63 + image->width, image->height, 1.64 + image->x, image->y); 1.65 + pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n", 1.66 + image->XObject_name); 1.67 +} 1.68 + 1.69 + 1.70 +static void pdf_write_png_image_callback (pdf_file_handle pdf_file, 1.71 + struct pdf_obj *stream, 1.72 + void *app_data) 1.73 +{ 1.74 + struct pdf_png_image *image = app_data; 1.75 + int rlen, wlen; 1.76 + uint8_t *wp; 1.77 + uint8_t buffer [8192]; 1.78 + 1.79 + while (! feof (image->f)) 1.80 + { 1.81 + uint32_t clen; 1.82 + rlen = fread (buffer, 1, 8, image->f); 1.83 + if (rlen != 8) 1.84 + pdf_fatal ("unexpected EOF on input file\n"); 1.85 + clen=(buffer[0]<<24)+(buffer[1]<<16)+(buffer[2]<<8)+buffer[3]; 1.86 + if (!memcmp(buffer+4,"IEND",4)) 1.87 + break; 1.88 + if (memcmp(buffer+4,"IDAT",4)) { 1.89 + fseek(image->f, clen+4, SEEK_CUR); 1.90 + continue; 1.91 + } 1.92 + while (clen) 1.93 + { 1.94 + rlen = fread (buffer, 1, (clen<sizeof(buffer))?clen:sizeof(buffer), image->f); 1.95 + if(!rlen) 1.96 + pdf_fatal ("unexpected EOF on input file\n"); 1.97 + clen -= rlen; 1.98 + wp = buffer; 1.99 + while (rlen) 1.100 + { 1.101 + wlen = fwrite (wp, 1, rlen, pdf_file->f); 1.102 + if (feof (pdf_file->f)) 1.103 + pdf_fatal ("unexpected EOF on output file\n"); 1.104 + if (ferror (pdf_file->f)) 1.105 + pdf_fatal ("error on output file\n"); 1.106 + rlen -= wlen; 1.107 + wp += wlen; 1.108 + } 1.109 + if (ferror (image->f)) 1.110 + pdf_fatal ("error on input file\n"); 1.111 + } 1.112 + fseek(image->f, 4, SEEK_CUR); 1.113 + } 1.114 +} 1.115 + 1.116 + 1.117 +void pdf_write_png_image (pdf_page_handle pdf_page, 1.118 + double x, 1.119 + double y, 1.120 + double width, 1.121 + double height, 1.122 + int color, 1.123 + char *indexed, 1.124 + int palent, 1.125 + int bpp, 1.126 + uint32_t width_samples, 1.127 + uint32_t height_samples, 1.128 + FILE *f) 1.129 +{ 1.130 + struct pdf_png_image *image; 1.131 + 1.132 + struct pdf_obj *stream; 1.133 + struct pdf_obj *stream_dict; 1.134 + struct pdf_obj *flateparams; 1.135 + 1.136 + struct pdf_obj *content_stream; 1.137 + 1.138 + image = pdf_calloc (1, sizeof (struct pdf_png_image)); 1.139 + 1.140 + image->width = width; 1.141 + image->height = height; 1.142 + image->x = x; 1.143 + image->y = y; 1.144 + 1.145 + image->f = f; 1.146 + 1.147 + image->color = color; 1.148 + image->width_samples = width_samples; 1.149 + image->height_samples = height_samples; 1.150 + 1.151 + pdf_add_array_elem_unique (pdf_page->procset, 1.152 + pdf_new_name (palent ? "ImageI" : image->color ? "ImageC" : "ImageB")); 1.153 + 1.154 + stream_dict = pdf_new_obj (PT_DICTIONARY); 1.155 + 1.156 + stream = pdf_new_ind_ref (pdf_page->pdf_file, 1.157 + pdf_new_stream (pdf_page->pdf_file, 1.158 + stream_dict, 1.159 + & pdf_write_png_image_callback, 1.160 + image)); 1.161 + 1.162 + strcpy (& image->XObject_name [0], "Im "); 1.163 + image->XObject_name [2] = pdf_new_XObject (pdf_page, stream); 1.164 + 1.165 + flateparams = pdf_new_obj (PT_DICTIONARY); 1.166 + 1.167 + pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject")); 1.168 + pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image")); 1.169 +// Name is required in PDF 1.0 but obsoleted in later PDF versions 1.170 +// pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0])); 1.171 + pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->width_samples)); 1.172 + pdf_set_dict_entry (flateparams, "Columns", pdf_new_integer (image->width_samples)); 1.173 + pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->height_samples)); 1.174 + if(palent) { 1.175 + struct pdf_obj *space; 1.176 + space = pdf_new_obj (PT_ARRAY); 1.177 + pdf_add_array_elem (space, pdf_new_name ("Indexed")); 1.178 + pdf_add_array_elem (space, pdf_new_name ("DeviceRGB")); 1.179 + pdf_add_array_elem (space, pdf_new_integer (palent-1)); 1.180 + pdf_add_array_elem (space, pdf_new_string_n (indexed,3*palent)); 1.181 + pdf_set_dict_entry (stream_dict, "ColorSpace", space); 1.182 + } else 1.183 + pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name (image->color ? "DeviceRGB" : "DeviceGray")); 1.184 + pdf_set_dict_entry (flateparams, "Colors", pdf_new_integer ((!indexed && image->color) ? 3 : 1)); 1.185 + pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (bpp)); 1.186 + pdf_set_dict_entry (flateparams, "BitsPerComponent", pdf_new_integer (bpp)); 1.187 + pdf_set_dict_entry (flateparams, "Predictor", pdf_new_integer (15)); 1.188 + 1.189 + pdf_stream_add_filter (stream, "FlateDecode", flateparams); 1.190 + 1.191 + /* the following will write the stream, using our callback function to 1.192 + get the actual data */ 1.193 + pdf_write_ind_obj (pdf_page->pdf_file, stream); 1.194 + 1.195 + content_stream = pdf_new_ind_ref (pdf_page->pdf_file, 1.196 + pdf_new_stream (pdf_page->pdf_file, 1.197 + pdf_new_obj (PT_DICTIONARY), 1.198 + & pdf_write_png_content_callback, 1.199 + image)); 1.200 + 1.201 + pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream); 1.202 + 1.203 + pdf_write_ind_obj (pdf_page->pdf_file, content_stream); 1.204 +}