1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/pdf_png.c Mon Dec 14 15:51:53 2009 +0000 1.3 @@ -0,0 +1,200 @@ 1.4 +/* 1.5 + * tumble: build a PDF file from image files 1.6 + * 1.7 + * PDF routines 1.8 + * Copyright 2004 Daniel Gloeckner 1.9 + * 1.10 + * Derived from pdf_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 <stdlib.h> 1.33 +#include <string.h> 1.34 + 1.35 + 1.36 +#include "bitblt.h" 1.37 +#include "pdf.h" 1.38 +#include "pdf_util.h" 1.39 +#include "pdf_prim.h" 1.40 +#include "pdf_private.h" 1.41 + 1.42 + 1.43 +struct pdf_png_image 1.44 +{ 1.45 + double width, height; 1.46 + double x, y; 1.47 + bool color; /* false for grayscale */ 1.48 + uint32_t width_samples, height_samples; 1.49 + FILE *f; 1.50 + char XObject_name [4]; 1.51 +}; 1.52 + 1.53 + 1.54 +static void pdf_write_png_content_callback (pdf_file_handle pdf_file, 1.55 + struct pdf_obj *stream, 1.56 + void *app_data) 1.57 +{ 1.58 + struct pdf_png_image *image = app_data; 1.59 + 1.60 + /* transformation matrix is: width 0 0 height x y cm */ 1.61 + pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ", 1.62 + image->width, image->height, 1.63 + image->x, image->y); 1.64 + pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n", 1.65 + image->XObject_name); 1.66 +} 1.67 + 1.68 + 1.69 +static void pdf_write_png_image_callback (pdf_file_handle pdf_file, 1.70 + struct pdf_obj *stream, 1.71 + void *app_data) 1.72 +{ 1.73 + struct pdf_png_image *image = app_data; 1.74 + int rlen, wlen; 1.75 + uint8_t *wp; 1.76 + uint8_t buffer [8192]; 1.77 + 1.78 + while (! feof (image->f)) 1.79 + { 1.80 + uint32_t clen; 1.81 + rlen = fread (buffer, 1, 8, image->f); 1.82 + if (rlen != 8) 1.83 + pdf_fatal ("unexpected EOF on input file\n"); 1.84 + clen=(buffer[0]<<24)+(buffer[1]<<16)+(buffer[2]<<8)+buffer[3]; 1.85 + if (!memcmp(buffer+4,"IEND",4)) 1.86 + break; 1.87 + if (memcmp(buffer+4,"IDAT",4)) { 1.88 + fseek(image->f, clen+4, SEEK_CUR); 1.89 + continue; 1.90 + } 1.91 + while (clen) 1.92 + { 1.93 + rlen = fread (buffer, 1, (clen<sizeof(buffer))?clen:sizeof(buffer), image->f); 1.94 + if(!rlen) 1.95 + pdf_fatal ("unexpected EOF on input file\n"); 1.96 + clen -= rlen; 1.97 + wp = buffer; 1.98 + while (rlen) 1.99 + { 1.100 + wlen = fwrite (wp, 1, rlen, pdf_file->f); 1.101 + if (feof (pdf_file->f)) 1.102 + pdf_fatal ("unexpected EOF on output file\n"); 1.103 + if (ferror (pdf_file->f)) 1.104 + pdf_fatal ("error on output file\n"); 1.105 + rlen -= wlen; 1.106 + wp += wlen; 1.107 + } 1.108 + if (ferror (image->f)) 1.109 + pdf_fatal ("error on input file\n"); 1.110 + } 1.111 + fseek(image->f, 4, SEEK_CUR); 1.112 + } 1.113 +} 1.114 + 1.115 + 1.116 +void pdf_write_png_image (pdf_page_handle pdf_page, 1.117 + double x, 1.118 + double y, 1.119 + double width, 1.120 + double height, 1.121 + int color, 1.122 + char *indexed, 1.123 + int palent, 1.124 + int bpp, 1.125 + uint32_t width_samples, 1.126 + uint32_t height_samples, 1.127 + FILE *f) 1.128 +{ 1.129 + struct pdf_png_image *image; 1.130 + 1.131 + struct pdf_obj *stream; 1.132 + struct pdf_obj *stream_dict; 1.133 + struct pdf_obj *flateparams; 1.134 + 1.135 + struct pdf_obj *content_stream; 1.136 + 1.137 + image = pdf_calloc (1, sizeof (struct pdf_png_image)); 1.138 + 1.139 + image->width = width; 1.140 + image->height = height; 1.141 + image->x = x; 1.142 + image->y = y; 1.143 + 1.144 + image->f = f; 1.145 + 1.146 + image->color = color; 1.147 + image->width_samples = width_samples; 1.148 + image->height_samples = height_samples; 1.149 + 1.150 + pdf_add_array_elem_unique (pdf_page->procset, 1.151 + pdf_new_name (palent ? "ImageI" : image->color ? "ImageC" : "ImageB")); 1.152 + 1.153 + stream_dict = pdf_new_obj (PT_DICTIONARY); 1.154 + 1.155 + stream = pdf_new_ind_ref (pdf_page->pdf_file, 1.156 + pdf_new_stream (pdf_page->pdf_file, 1.157 + stream_dict, 1.158 + & pdf_write_png_image_callback, 1.159 + image)); 1.160 + 1.161 + strcpy (& image->XObject_name [0], "Im "); 1.162 + image->XObject_name [2] = pdf_new_XObject (pdf_page, stream); 1.163 + 1.164 + flateparams = pdf_new_obj (PT_DICTIONARY); 1.165 + 1.166 + pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject")); 1.167 + pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image")); 1.168 +// Name is required in PDF 1.0 but obsoleted in later PDF versions 1.169 +// pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0])); 1.170 + pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->width_samples)); 1.171 + pdf_set_dict_entry (flateparams, "Columns", pdf_new_integer (image->width_samples)); 1.172 + pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->height_samples)); 1.173 + if(palent) { 1.174 + struct pdf_obj *space; 1.175 + space = pdf_new_obj (PT_ARRAY); 1.176 + pdf_add_array_elem (space, pdf_new_name ("Indexed")); 1.177 + pdf_add_array_elem (space, pdf_new_name ("DeviceRGB")); 1.178 + pdf_add_array_elem (space, pdf_new_integer (palent-1)); 1.179 + pdf_add_array_elem (space, pdf_new_string_n (indexed,3*palent)); 1.180 + pdf_set_dict_entry (stream_dict, "ColorSpace", space); 1.181 + } else 1.182 + pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name (image->color ? "DeviceRGB" : "DeviceGray")); 1.183 + pdf_set_dict_entry (flateparams, "Colors", pdf_new_integer ((!indexed && image->color) ? 3 : 1)); 1.184 + pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (bpp)); 1.185 + pdf_set_dict_entry (flateparams, "BitsPerComponent", pdf_new_integer (bpp)); 1.186 + pdf_set_dict_entry (flateparams, "Predictor", pdf_new_integer (15)); 1.187 + 1.188 + pdf_stream_add_filter (stream, "FlateDecode", flateparams); 1.189 + 1.190 + /* the following will write the stream, using our callback function to 1.191 + get the actual data */ 1.192 + pdf_write_ind_obj (pdf_page->pdf_file, stream); 1.193 + 1.194 + content_stream = pdf_new_ind_ref (pdf_page->pdf_file, 1.195 + pdf_new_stream (pdf_page->pdf_file, 1.196 + pdf_new_obj (PT_DICTIONARY), 1.197 + & pdf_write_png_content_callback, 1.198 + image)); 1.199 + 1.200 + pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream); 1.201 + 1.202 + pdf_write_ind_obj (pdf_page->pdf_file, content_stream); 1.203 +}